diff --git a/src/infrastructure/road.zig b/src/infrastructure/road.zig index 9a0288c..494ca36 100644 --- a/src/infrastructure/road.zig +++ b/src/infrastructure/road.zig @@ -87,6 +87,8 @@ pub const Road = struct { if (self.nodes[i] != old_node) continue; self.nodes[i] = new_node; + // As nodes change, road's length must be recalculated + self.length = ut.calculate_length(self.nodes[0].pos, self.nodes[1].pos); return; } diff --git a/src/infrastructure/road_manager.zig b/src/infrastructure/road_manager.zig index 55444bd..1a0e394 100644 --- a/src/infrastructure/road_manager.zig +++ b/src/infrastructure/road_manager.zig @@ -37,9 +37,8 @@ pub const RoadManager = struct { /// Function which creates the road object, its pointer, adds it to the list /// and then also references that same road to the bounding nodes pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void { - const road: Road = .init(self.getNextID(), start, end); const road_ptr = try allocator.create(Road); - road_ptr.* = road; + road_ptr.* = Road.init(self.getNextID(), start, end); try self.roads.append(allocator, road_ptr); const ref = self.roads.items[self.roads.items.len - 1]; diff --git a/src/simulator.zig b/src/simulator.zig index fbae2dc..1790102 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -153,11 +153,6 @@ pub const Simulator = struct { // Prevents the road from being attached to 2 identical nodes (0 length road) if (temp.id == cur_node.id) return; - // TODO replace with road splitting - self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| { - std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err}); - }; - const intersections = self.getIntersectingRoads(self.allocator, temp, cur_node) catch |err| { std.debug.panic("Intersection selection failure: {}\n", .{err}); }; @@ -170,6 +165,8 @@ pub const Simulator = struct { std.debug.print("Road ID={d} Pos: ({d}, {d})\n", .{int.road.id, int.pos.x, int.pos.y}); } + self.splitRoadsByIntersections(intersections, temp, cur_node); + self.node_man.temp_node = if (self.auto_continue) cur_node else null; return; } @@ -290,7 +287,7 @@ pub const Simulator = struct { /// Takes the data about intersections and adds new nodes there alongside with linking existing roads to them /// /// Important: This function assumes the intersection array is sorted by distance from the start node (ascending) - fn splitRoadsByIntersections(self: *Simulator, intersections: []st.IntersectionData, start: *Node, end: *Node) !void { + fn splitRoadsByIntersections(self: *Simulator, intersections: []st.IntersectionData, start: *Node, end: *Node) void { if (intersections.len == 0) { self.road_man.addRoad(self.allocator, start, end) catch |err| { std.debug.panic("Failed creating the road out of origin nodes: {}\n", .{err}); @@ -329,10 +326,17 @@ pub const Simulator = struct { intersection.road.updateNodeReference(old_node_of_road, new_node) catch |err| { std.debug.panic("Failed to update the road's node references: {}\n", .{err}); }; + // Now the old node must not point at the intersection road + old_node_of_road.unreferenceRoad(intersection.road) catch |err| { + std.debug.panic("Failed to unreference the intersection road from the old node: {}\n", .{err}); + }; + new_node.referenceRoad(self.allocator, intersection.road) catch |err| { + std.debug.panic("Failed to reference the intersection road to the intersecting node: {}\n", .{err}); + }; // Now we add the road (to the road list) and references the road at both bounding nodes self.road_man.addRoad(self.allocator, new_node, old_node_of_road) catch |err| { - std.debug.panic("Failed to create a road of new node and former node of prior intersecting road", .{ + std.debug.panic("Failed to create a road of new node and former node of prior intersecting road: {}\n", .{ err }); };