From 7f5cb42097bbbd856ed0dff2ee7394f3495221da Mon Sep 17 00:00:00 2001 From: Marto Date: Mon, 27 Apr 2026 16:22:23 +0200 Subject: [PATCH] Implemented valid route setting --- src/input.odin | 3 +++ src/pathfinding.odin | 24 ++++++++++++++++++++++++ src/vehicles/car.odin | 23 +++++++++++++++-------- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/input.odin b/src/input.odin index 06415c8..57c56ca 100644 --- a/src/input.odin +++ b/src/input.odin @@ -2,6 +2,7 @@ package main import rl "vendor:raylib" +import "common" import inf "infrastructure" import v "vehicles" @@ -29,7 +30,9 @@ handle_keyboard_input :: proc(self: ^Simulator) { if node_id, ok := get_free_node(self).?; ok { car := v.car_init(node_id, self.nodes[:]) + set_car_route(self, &car) append(&self.cars, car) + v.car_print_route(u32(len(self.cars)) - 1, &car) } } diff --git a/src/pathfinding.odin b/src/pathfinding.odin index 39beb15..64f2ce6 100644 --- a/src/pathfinding.odin +++ b/src/pathfinding.odin @@ -1,7 +1,10 @@ package main +import "core:math/rand" + import "common" import inf "infrastructure" +import v "vehicles" // Returns path to destination node => road => node get_path_to_destination :: proc(self: ^Simulator, source: u32, destination: u32) -> []u32 { @@ -40,4 +43,25 @@ get_neighbouring_nodes :: proc(self: ^Simulator, node_index: u32) -> []u32 { } return neighbour_nodes[:] +} + +set_car_route :: proc(self: ^Simulator, car: ^v.Car) { + destination_reachable := false + destination: u32 + + for !destination_reachable { + ignored_nodes: [dynamic]u32 + + for { + destination = rand.uint32_max(u32(len(self.nodes))) + + if car.pos.type != .Node || car.pos.ref != destination do break + } + + // TODO this will need be fixed because we have not encountered what to do if car is at the middle of the road + source := car.pos.type == .Node ? car.pos.ref : self.roads[car.pos.ref].nodes[0] + destination_reachable = get_destination_reachable(self, source, destination, &ignored_nodes) + } + + car.destination = destination } \ No newline at end of file diff --git a/src/vehicles/car.odin b/src/vehicles/car.odin index 36e3328..6dacba5 100644 --- a/src/vehicles/car.odin +++ b/src/vehicles/car.odin @@ -1,7 +1,8 @@ package vehicles -import "core:math/rand" +import "core:fmt" import rl "vendor:raylib" +import sc "core:strconv" import "../common" import inf "../infrastructure" @@ -36,15 +37,21 @@ car_init :: proc(spawn_node: u32, nodes: []inf.Node) -> Car { } } -// Sets a (valid) route for the car -// -// Does NOT guarantee the route is reachable (TODO!) -car_set_route :: proc(self: ^Car, nodes_len: u32) { - for self.pos.type == .Node && self.pos.ref == self.destination do self.destination = rand.uint32_max(nodes_len) -} - // Updates (origin and destination) node reference car_update_node_reference :: proc(self: ^Car, old_ref: u32, new_ref: u32) { if self.pos.type == .Node && self.pos.ref == old_ref do self.pos.ref = new_ref if self.destination == old_ref do self.destination = new_ref +} + +// Prints car's route +car_print_route :: proc(id: Maybe(u32) = nil, self: ^Car) { + val, ok := self.destination.? + destination := ok ? fmt.aprintf("N%d", val) : "/" + source_type := self.pos.type == .Node ? 'N' : 'R' + + car_id, ok_val := id.? + buf: [100]u8 + id_str := ok_val ? sc.write_uint(buf[:], u64(car_id), 10) : "N/A" + + fmt.printfln("ID=%s Source=%c%d, Destination=%s", id_str, source_type, self.pos.ref, destination) } \ No newline at end of file