Pathfinding initial implementation [2 errors]
This commit is contained in:
43
src/pathfinding.odin
Normal file
43
src/pathfinding.odin
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import "common"
|
||||
import inf "infrastructure"
|
||||
|
||||
// Returns path to destination node => road => node
|
||||
get_path_to_destination :: proc(self: ^Simulator, source: u32, destination: u32) -> []u32 {
|
||||
source_node := self.nodes[source]
|
||||
destination_node := self.nodes[destination]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns if path is reachable from node => destination
|
||||
get_destination_reachable :: proc(self: ^Simulator, node_to_search: u32, destination: u32, nodes_to_ignore: ^[]u32) -> bool {
|
||||
if !self.nodes[node_to_search].enabled || common.list_contains(nodes_to_ignore[:], node_to_search) do return false
|
||||
append(nodes_to_ignore, node_to_search)
|
||||
|
||||
if node_to_search == destination do return true
|
||||
for node in get_neighbouring_nodes(self, node_to_search) {
|
||||
if get_destination_reachable(self, node, destination, nodes_to_ignore) do return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
get_neighbouring_nodes :: proc(self: ^Simulator, node_index: u32) -> []u32 {
|
||||
node := self.nodes[node_index]
|
||||
neighbour_nodes := make([dynamic]u32, 0, len(node.roads))
|
||||
|
||||
for road_index in node.roads {
|
||||
road := self.roads[road_index]
|
||||
|
||||
// We pick the node that is not the original node with which we reached the node
|
||||
// But rather the other node, on the end
|
||||
next_node := road.nodes[0] == node_index ? road.nodes[1] : road.nodes[0]
|
||||
|
||||
if !common.list_contains(neighbour_nodes[:], next_node) do append(&neighbour_nodes, next_node)
|
||||
}
|
||||
|
||||
return neighbour_nodes[:]
|
||||
}
|
||||
Reference in New Issue
Block a user