From 513b77752efc4f6370adf33e68b9de46f5a39437 Mon Sep 17 00:00:00 2001 From: Marto Date: Thu, 16 Apr 2026 01:03:24 +0200 Subject: [PATCH] Implementation of adding new roads, bug incoming, TODO entity viewer, something akin to imgui --- src/infrastructure/road_manager.zig | 4 +++- src/simulator.zig | 35 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/infrastructure/road_manager.zig b/src/infrastructure/road_manager.zig index 0ed3372..65b5fd4 100644 --- a/src/infrastructure/road_manager.zig +++ b/src/infrastructure/road_manager.zig @@ -35,7 +35,7 @@ pub const RoadManager = struct { } /// Creates a new road with nodes; Then it references the said road to the nodes themselves - pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !void { + pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !*Road { const road: Road = .init(self.getNewID(), start_node, end_node); self.roads.appendBounded(road) catch |err| { @@ -47,6 +47,8 @@ pub const RoadManager = struct { try start_node.referenceRoad(allocator, road_ref); try end_node.referenceRoad(allocator, road_ref); + + return road_ref; } /// Returns the reference to the last element in the list; returns null if there are no elements in the list diff --git a/src/simulator.zig b/src/simulator.zig index 6e04f20..d2a2373 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -156,7 +156,6 @@ pub const Simulator = struct { c.HEIGHT - 2 * c.TEXT_SIZE, c.TEXT_SIZE, .black); - // Displays the mode the simulation is currently in rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black); @@ -210,10 +209,14 @@ pub const Simulator = struct { } fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *const Node, end: *const Node) void { - _ = end; // autofix - _ = start; // autofix - // todo plan it out if (intersections.len == 0) return; + + const first_intersection_road = intersections[0]; + const first_intersection_node = self.node_man.getSelectedNode(first_intersection_road.point); + const first_road = self.road_man.add(self.allocator, start, first_intersection_node) catch |err| { + std.debug.panic("Failed to create the first split road: {}\n", .{err}); + }; + first_intersection_node.referenceRoad(self.allocator, first_road); // We have done that every intersection creates new node and reconnects existing connections // TODO @@ -233,11 +236,31 @@ pub const Simulator = struct { // As is the end node's road reference road_old_node.unreferenceRoad(intersection.road); - self.road_man.add(self.allocator, new_node, road_old_node) catch |err| { - std.debug.panic("Failed to create the new split road: {}\n", .{err}); + _ = self.road_man.add(self.allocator, new_node, road_old_node) catch |err| { + std.debug.panic("Failed to create the last split road: {}\n", .{err}); }; + + if (i == intersections.len - 1) continue; + + const connecting_road = self.road_man.add( + self.allocator, + self.node_man.getSelectedNode(intersection.point), + self.node_man.getSelectedNode(intersections[i+1].point)) catch |err| { + std.debug.panic("Failed to create the connecting road: {}\n", .{err}); + }; + + // todo + // okay so here we connect the road to current and next node + // and then this should hopefully be it + // 20 new bugs incoming } + const last_intersection_road = intersections[intersections.len - 1]; + const last_intersection_node = self.node_man.getSelectedNode(last_intersection_road.point); + const road = self.road_man.add(self.allocator, last_intersection_node, end) catch |err| { + std.debug.panic("Failed to add the road: {}\n", .{err}); + }; + last_intersection_node.referenceRoad(self.allocator, road); } pub fn update(self: *Simulator) void {