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

@@ -33,8 +33,8 @@
// internet connectivity. // internet connectivity.
.dependencies = .{ .dependencies = .{
.raylib_zig = .{ .raylib_zig = .{
.url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#4b6a05cd102d51f00523dd5c5e18e628df359d20", .url = "git+https://github.com/raylib-zig/raylib-zig#aa9ee05f2246b813f75206bf817c9fbdfcb06101",
.hash = "raylib_zig-5.6.0-dev-KE8REKNmBQDek2Sz27ULioxYpY9IYR0K0CeIC-iJRLCI", .hash = "raylib_zig-5.6.0-dev-KE8RECFQBQBn0BrEyEyK5NST461oRzpk6XeGeF9qBU2M",
}, },
}, },
.paths = .{ .paths = .{

View File

@@ -24,7 +24,7 @@ pub const Node = struct {
/// References the passed road with the node /// References the passed road with the node
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void { pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void {
if (self.roadInList(road) != null) return; if (self.roadInList(road) != null) return error.RoadAlreadyWithinNode;
try self.roads.append(allocator, road); try self.roads.append(allocator, road);
} }
@@ -38,7 +38,7 @@ pub const Node = struct {
return; return;
} }
return error.InvalidNode; return error.RoadNotWithinNode;
} }
/// Returns bool whether the road passed is part of the roads list /// Returns bool whether the road passed is part of the roads list
fn roadInList(self: *const Node, road_to_check: *const Road) ?usize { fn roadInList(self: *const Node, road_to_check: *const Road) ?usize {

View File

@@ -1,3 +1,4 @@
const std = @import("std");
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
pub const Road = struct { pub const Road = struct {
@@ -15,4 +16,11 @@ pub const Road = struct {
try self.nodes[0].unreferenceRoad(self); try self.nodes[0].unreferenceRoad(self);
try self.nodes[1].unreferenceRoad(self); try self.nodes[1].unreferenceRoad(self);
} }
pub fn updateNodeReference(self: *Road, old_reference: *const Node, new_ref: *const Node) void {
_ = new_ref; // autofix
_ = old_reference; // autofix
_ = self; // autofix
}
}; };

View File

@@ -35,7 +35,7 @@ pub const RoadManager = struct {
} }
/// Creates a new road with nodes; Then it references the said road to the nodes themselves /// 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) !*Road { pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !void {
const road: Road = .init(self.getNewID(), start_node, end_node); const road: Road = .init(self.getNewID(), start_node, end_node);
self.roads.appendBounded(road) catch |err| { self.roads.appendBounded(road) catch |err| {
@@ -47,8 +47,6 @@ pub const RoadManager = struct {
try start_node.referenceRoad(allocator, road_ref); try start_node.referenceRoad(allocator, road_ref);
try end_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 /// Returns the reference to the last element in the list; returns null if there are no elements in the list

View File

@@ -55,8 +55,12 @@ pub const Simulator = struct {
self.road_man.roads.clearRetainingCapacity(); self.road_man.roads.clearRetainingCapacity();
self.node_man.clear(self.allocator); self.node_man.clear(self.allocator);
self.node_man.nodes.clearRetainingCapacity(); self.node_man.nodes.clearRetainingCapacity();
return;
} }
if (rl.isKeyReleased(.i)) self.printDebugInfo();
self.mode = switch (rl.getKeyPressed()) { self.mode = switch (rl.getKeyPressed()) {
.v => .VISUAL, .v => .VISUAL,
.d => .DELETE, .d => .DELETE,
@@ -96,19 +100,14 @@ 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 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 // 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});
}; };
defer self.allocator.free(data); defer self.allocator.free(data);
self.splitRoadsByPoints(data, temp, node);
std.debug.print("Displaying intersection points in order from start (temp):\n", .{}); std.debug.print("Displaying intersection points in order from start (temp):\n", .{});
for (0..data.len) |i| { for (0..data.len) |i| {
std.debug.print("{d}: ({d}, {d})\n", .{i+1, data[i].point.x, data[i].point.y}); 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; return sorted_intersections;
} }
fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *const Node, end: *const Node) void { fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *Node, end: *Node) void {
if (intersections.len == 0) return; 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(intersections[0].point);
const first_intersection_node = self.node_man.getSelectedNode(first_intersection_road.point); self.road_man.add(self.allocator, start, first_intersection_node) catch |err| {
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}); 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| { for (0..intersections.len) |i| {
const intersection = intersections[i]; const intersection = intersections[i];
// The node created at the point of intersection // 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, // and the old node is removed from the road's end node reference,
intersection.road.nodes[1] = new_node; intersection.road.nodes[1] = new_node;
// As is the end node's road reference // 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}); std.debug.panic("Failed to create the last split road: {}\n", .{err});
}; };
if (i == intersections.len - 1) continue; if (i == intersections.len - 1) continue;
const connecting_road = self.road_man.add( self.road_man.add(
self.allocator, self.allocator,
self.node_man.getSelectedNode(intersection.point), self.node_man.getSelectedNode(intersection.point),
self.node_man.getSelectedNode(intersections[i+1].point)) catch |err| { self.node_man.getSelectedNode(intersections[i+1].point)) catch |err| {
std.debug.panic("Failed to create the connecting road: {}\n", .{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_road = intersections[intersections.len - 1];
const last_intersection_node = self.node_man.getSelectedNode(last_intersection_road.point); 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}); std.debug.panic("Failed to add the road: {}\n", .{err});
}; };
last_intersection_node.referenceRoad(self.allocator, road);
} }
pub fn update(self: *Simulator) void { pub fn update(self: *Simulator) void {
@@ -280,4 +276,35 @@ pub const Simulator = struct {
return ""; 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", .{});
}
}
}; };