From fe5bea6aaa737615ba865c48322e0528b1f51796 Mon Sep 17 00:00:00 2001 From: Marto Date: Tue, 14 Apr 2026 01:23:58 +0200 Subject: [PATCH] Splitting functionality partially implemented --- src/infrastructure/node_manager.zig | 3 ++- src/simulator.zig | 30 ++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index a17a208..a78d10b 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -105,7 +105,8 @@ pub const NodeManager = struct { for (self.nodes.items, 0..) |*node, i| { if (node.id != node_to_remove.id) continue; - + + // This deinit exists here because even if roads list is empty something may have been added and removed, meaning the memory is still being held node.deinit(allocator); _ = self.nodes.swapRemove(i); return; diff --git a/src/simulator.zig b/src/simulator.zig index 9f9d226..a1d0d11 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -96,16 +96,13 @@ pub const Simulator = struct { const temp = self.node_man.temp_node.?; if (temp.id == node.id) return; - // todo remove + // todo remove and instead have the split function create all the new roads self.road_man.add(self.allocator, temp, node) catch |err| { std.debug.panic("Error while attempting to add a road: {}\n", .{err}); }; // get intersections made // todo plan out the implementation - // add both nodes to the intersection array - // first in front, second in last - // all of the road connections will not be added here but rather in the split_roads function as it will // create road connection for each nodes that are to be connected via connection const data = self.getIntersectingRoads(self.allocator, temp, node) catch |err| { std.debug.panic("Failed to save intersection data: {}\n", .{err}); @@ -212,14 +209,33 @@ pub const Simulator = struct { return sorted_intersections; } - fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData) void { - _ = self; // autofix + 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; + // We have done that every intersection creates new node and reconnects existing connections + // TODO + // But now we have to implement that the new roads are being created in the other axis as well and connected + // to these new nodes for (0..intersections.len) |i| { const intersection = intersections[i]; - _ = intersection; // autofix + // The node created at the point of intersection + const new_node = self.node_man.getSelectedNode(intersection.point); + + // Pointer to the node that borders the road that was intersected + // This node and the new_node will become nodes for the new road being created + const road_old_node = intersection.road.nodes[1]; + // The old road that was intersected now borders the new node + // and the old node is removed from the road's end node reference, + intersection.road.nodes[1] = new_node; + // 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}); + }; } }