From 205c630dbfef7166867cb2c547b03434a45c064c Mon Sep 17 00:00:00 2001 From: Marto Date: Mon, 27 Apr 2026 09:52:31 +0200 Subject: [PATCH] Error maxxing --- src/infrastructure_helpers.odin | 4 +++- src/input.odin | 2 +- src/pathfinding.odin | 2 +- src/vehicles/car.odin | 8 ++++---- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/infrastructure_helpers.odin b/src/infrastructure_helpers.odin index b8ce09b..6712592 100644 --- a/src/infrastructure_helpers.odin +++ b/src/infrastructure_helpers.odin @@ -99,7 +99,7 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) } // Returns a random node that has no cars on it -get_free_node :: proc(self: ^Simulator) -> u32 { +get_free_node :: proc(self: ^Simulator) -> Maybe(u32) { car_occupied_nodes: [dynamic]u32 for car in self.cars { @@ -110,6 +110,8 @@ get_free_node :: proc(self: ^Simulator) -> u32 { append(&car_occupied_nodes, node) } + if len(car_occupied_nodes) == len(self.nodes) do return nil + for { node := rand.uint32_max(u32(len(self.nodes))) diff --git a/src/input.odin b/src/input.odin index 930f815..f87e2ba 100644 --- a/src/input.odin +++ b/src/input.odin @@ -27,7 +27,7 @@ handle_keyboard_input :: proc(self: ^Simulator) { if !rl.IsKeyReleased(.N) || len(self.nodes) == 0 do return - car := v.car_init(u32(len(self.nodes))) + car := v.car_init(get_free_node(self), self.nodes[:]) append(&self.cars, car) } diff --git a/src/pathfinding.odin b/src/pathfinding.odin index 5235044..39beb15 100644 --- a/src/pathfinding.odin +++ b/src/pathfinding.odin @@ -12,7 +12,7 @@ get_path_to_destination :: proc(self: ^Simulator, source: u32, destination: u32) } // 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 { +get_destination_reachable :: proc(self: ^Simulator, node_to_search: u32, destination: u32, nodes_to_ignore: ^[dynamic]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) diff --git a/src/vehicles/car.odin b/src/vehicles/car.odin index 330f797..9d19cf8 100644 --- a/src/vehicles/car.odin +++ b/src/vehicles/car.odin @@ -29,13 +29,13 @@ Car :: struct { } // Constructor -car_init :: proc(node: u32, nodes: []inf.Node) -> Car { +car_init :: proc(spawn_node: u32, nodes: []inf.Node) -> Car { return { fuel_level = common.FUEL_MAX, max_speed = common.CAR_MAX_SPEED, - origin = node, - node_pos = node, - actual_pos = nodes[node].pos + origin = spawn_node, + node_pos = spawn_node, + actual_pos = nodes[spawn_node].pos } }