diff --git a/build.zig.zon b/build.zig.zon index 5d9bd23..d3e53cd 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -33,8 +33,8 @@ // internet connectivity. .dependencies = .{ .raylib_zig = .{ - .url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#4b6a05cd102d51f00523dd5c5e18e628df359d20", - .hash = "raylib_zig-5.6.0-dev-KE8REKNmBQDek2Sz27ULioxYpY9IYR0K0CeIC-iJRLCI", + .url = "git+https://github.com/raylib-zig/raylib-zig#aa9ee05f2246b813f75206bf817c9fbdfcb06101", + .hash = "raylib_zig-5.6.0-dev-KE8RECFQBQBn0BrEyEyK5NST461oRzpk6XeGeF9qBU2M", }, }, .paths = .{ diff --git a/src/infrastructure/node.zig b/src/infrastructure/node.zig index ce84139..49a069a 100644 --- a/src/infrastructure/node.zig +++ b/src/infrastructure/node.zig @@ -24,7 +24,7 @@ pub const Node = struct { /// References the passed road with the node 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); } @@ -38,7 +38,7 @@ pub const Node = struct { return; } - return error.InvalidNode; + return error.RoadNotWithinNode; } /// Returns bool whether the road passed is part of the roads list fn roadInList(self: *const Node, road_to_check: *const Road) ?usize { diff --git a/src/infrastructure/road.zig b/src/infrastructure/road.zig index 4f6cbf1..81c08d6 100644 --- a/src/infrastructure/road.zig +++ b/src/infrastructure/road.zig @@ -1,3 +1,4 @@ +const std = @import("std"); const Node = @import("node.zig").Node; pub const Road = struct { @@ -15,4 +16,11 @@ pub const Road = struct { try self.nodes[0].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 + + } }; \ No newline at end of file diff --git a/src/infrastructure/road_manager.zig b/src/infrastructure/road_manager.zig index 65b5fd4..0ed3372 100644 --- a/src/infrastructure/road_manager.zig +++ b/src/infrastructure/road_manager.zig @@ -35,7 +35,7 @@ pub const RoadManager = struct { } /// 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); self.roads.appendBounded(road) catch |err| { @@ -47,8 +47,6 @@ pub const RoadManager = struct { try start_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 diff --git a/src/simulator.zig b/src/simulator.zig index d2a2373..4679bf0 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -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", .{}); + } + } }; \ No newline at end of file