Fixing road splitting, debugging and returning to 0.15.x until ZLS catches up

This commit is contained in:
2026-04-16 11:19:55 +02:00
parent 513b77752e
commit b716340e07
5 changed files with 67 additions and 34 deletions

View File

@@ -55,8 +55,12 @@ pub const Simulator = struct {
self.road_man.roads.clearRetainingCapacity();
self.node_man.clear(self.allocator);
self.node_man.nodes.clearRetainingCapacity();
return;
}
if (rl.isKeyReleased(.i)) self.printDebugInfo();
self.mode = switch (rl.getKeyPressed()) {
.v => .VISUAL,
.d => .DELETE,
@@ -96,19 +100,14 @@ pub const Simulator = struct {
const temp = self.node_man.temp_node.?;
if (temp.id == node.id) return;
// 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
// 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});
};
defer self.allocator.free(data);
self.splitRoadsByPoints(data, temp, node);
std.debug.print("Displaying intersection points in order from start (temp):\n", .{});
for (0..data.len) |i| {
std.debug.print("{d}: ({d}, {d})\n", .{i+1, data[i].point.x, data[i].point.y});
@@ -208,20 +207,20 @@ pub const Simulator = struct {
return sorted_intersections;
}
fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *const Node, end: *const Node) void {
if (intersections.len == 0) return;
fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *Node, end: *Node) void {
if (intersections.len == 0) {
self.road_man.add(self.allocator, start, end) catch |err| {
std.debug.panic("Failed to create the sole connecting road: {}\n", .{err});
};
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| {
const first_intersection_node = self.node_man.getSelectedNode(intersections[0].point);
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
// 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];
// The node created at the point of intersection
@@ -234,33 +233,30 @@ pub const Simulator = struct {
// 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);
road_old_node.unreferenceRoad(intersection.road) catch |err| {
std.debug.panic("Failed to unreference previous road: {}\n", .{err});
};
_ = self.road_man.add(self.allocator, new_node, road_old_node) catch |err| {
// This adds the road (to the road manager) and also references the road at both nodes (pointers)
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.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| {
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 {
@@ -280,4 +276,35 @@ pub const Simulator = struct {
return "";
}
/// Prints out the list of all roads and nodes connecting them
fn printDebugInfo(self: *const Simulator) void {
std.debug.print("[DEBUG]\n", .{});
std.debug.print("Displaying all the nodes:\n", .{});
// First we print out all the nodes
for (self.node_man.nodes.items) |node| {
std.debug.print("Node {d}: ({d},{d})\n", .{node.id, node.pos.x, node.pos.y});
std.debug.print("Road connections:\n", .{});
for (node.roads.items) |road| {
std.debug.print("Road {d}: ({d},{d} => {d},{d})\n",
.{road.id, road.nodes[0].pos.x, road.nodes[0].pos.y, road.nodes[1].pos.x, road.nodes[1].pos.y});
}
std.debug.print("\n", .{});
}
std.debug.print("Displaying all the roads:\n", .{});
// Next we print out all the roads
for (self.road_man.roads.items) |road| {
std.debug.print("Road {d}:\n", .{road.id});
std.debug.print("End nodes:\n", .{});
for (0..road.nodes.len) |i| {
std.debug.print("Node {d}: ({d},{d})\n",
.{road.nodes[i].id, road.nodes[i].pos.x, road.nodes[i].pos.y});
}
std.debug.print("\n", .{});
}
}
};