Splitting functionality partially implemented

This commit is contained in:
2026-04-14 01:23:58 +02:00
parent dc0d91997a
commit fe5bea6aaa
2 changed files with 25 additions and 8 deletions

View File

@@ -105,7 +105,8 @@ pub const NodeManager = struct {
for (self.nodes.items, 0..) |*node, i| { for (self.nodes.items, 0..) |*node, i| {
if (node.id != node_to_remove.id) continue; 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); node.deinit(allocator);
_ = self.nodes.swapRemove(i); _ = self.nodes.swapRemove(i);
return; return;

View File

@@ -96,16 +96,13 @@ pub const Simulator = struct {
const temp = self.node_man.temp_node.?; const temp = self.node_man.temp_node.?;
if (temp.id == node.id) return; 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| { self.road_man.add(self.allocator, temp, node) catch |err| {
std.debug.panic("Error while attempting to add a road: {}\n", .{err}); std.debug.panic("Error while attempting to add a road: {}\n", .{err});
}; };
// get intersections made // get intersections made
// todo plan out the implementation // 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 // create road connection for each nodes that are to be connected via connection
const data = self.getIntersectingRoads(self.allocator, temp, node) catch |err| { const data = self.getIntersectingRoads(self.allocator, temp, node) catch |err| {
std.debug.panic("Failed to save intersection data: {}\n", .{err}); std.debug.panic("Failed to save intersection data: {}\n", .{err});
@@ -212,14 +209,33 @@ pub const Simulator = struct {
return sorted_intersections; return sorted_intersections;
} }
fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData) void { fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *const Node, end: *const Node) void {
_ = self; // autofix _ = end; // autofix
_ = start; // autofix
// todo plan it out // todo plan it out
if (intersections.len == 0) return; 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| { for (0..intersections.len) |i| {
const intersection = intersections[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});
};
} }
} }