This repository has been archived on 2026-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zig-base-road-network/src/simulator.zig

310 lines
12 KiB
Zig

const std = @import("std");
const rl = @import("raylib");
const c = @import("constants.zig");
const st = @import("structures.zig");
const utils = @import("utils.zig");
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
const Node = @import("infrastructure/node.zig").Node;
const Road = @import("infrastructure/road.zig").Road;
pub const Simulator = struct {
allocator: std.mem.Allocator,
/// Contains data and functions about nodes
node_man: NodeManager,
/// Contains data and functions about roads
road_man: RoadManager,
/// Tracks whether to display node snapping radius
display_details: bool,
/// Tracks whether to automatically start build a new road after the creation of previous one is finished
auto_continue: bool,
mode: st.Mode,
pub fn init(allocator: std.mem.Allocator) !Simulator {
return .{
.allocator = allocator,
.node_man = try .init(allocator),
.road_man = try .init(allocator),
.display_details = false,
.auto_continue = false,
.mode = .VISUAL,
};
}
pub fn deinit(self: *Simulator) void {
self.node_man.deinit(self.allocator);
self.road_man.deinit(self.allocator);
}
/// Input handling function that is exposed to the public
pub fn handleInput(self: *Simulator) void {
self.handleKeyboardInput();
self.handleMouseInput();
}
/// Every keyboard event is checked here
fn handleKeyboardInput(self: *Simulator) void {
self.display_details = rl.isKeyDown(.left_alt);
self.auto_continue = rl.isKeyDown(.left_control);
if (rl.isKeyReleased(.c) and self.mode == .DELETE) {
self.node_man.temp_node = null;
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,
.b => .BUILD,
else => self.mode,
};
if (self.mode != .BUILD) self.cancelBuildingRoad();
}
/// every mouse event is checked here
fn handleMouseInput(self: *Simulator) void {
const pos = rl.getMousePosition();
if (self.mode == .BUILD and rl.isMouseButtonReleased(.left)) {
self.leftClickEvent(pos);
}
else if (self.mode == .BUILD and rl.isMouseButtonReleased(.right)) {
self.cancelBuildingRoad();
}
else if (self.mode == .DELETE and rl.isMouseButtonReleased(.left) and self.road_man.highlighted_road != null) {
const nodes = self.road_man.highlighted_road.?.nodes;
self.road_man.remove(self.road_man.highlighted_road.?) catch |err| {
std.debug.panic("Failed to remove the road: {}\n", .{err});
};
self.node_man.remove(self.allocator, nodes[0]) catch |err| {
std.debug.panic("Failed to remove the first node of the road to be deleted: {}\n", .{err});
};
self.node_man.remove(self.allocator, nodes[1]) catch |err| {
std.debug.panic("Failed to remove the second node of the road to be deleted: {}\n", .{err});
};
}
}
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
if (self.node_man.add(pos)) |node| {
const temp = self.node_man.temp_node.?;
if (temp.id == node.id) return;
// 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});
}
std.debug.print("\n", .{});
self.node_man.temp_node = if (self.auto_continue) node else null;
}
}
fn cancelBuildingRoad(self: *Simulator) void {
if (self.node_man.temp_node == null) return;
self.node_man.remove(self.allocator, self.node_man.temp_node.?) catch |err| {
std.debug.panic("Node doesn't exist: {}\n", .{err});
};
self.node_man.temp_node = null;
}
/// The main drawing function that is exposed upwards
pub fn draw(self: *const Simulator) void {
self.road_man.draw(self.mode == .DELETE);
self.node_man.draw(self.display_details);
self.drawUI();
rl.clearBackground(.light_gray);
}
/// Draws UI elements like text
fn drawUI(self: *const Simulator) void {
var buf: [1024]u8 = undefined;
// Displays how many nodes and roads are currently in the lists
const entities_list =
std.fmt.bufPrintZ(
&buf,
"Nodes: {d}\nRoads: {d}",
.{self.node_man.nodes.items.len, self.road_man.roads.items.len}) catch |err| {
std.debug.panic("Insufficient space for displaying entire entity list: {}\n", .{err});
};
rl.drawText(
entities_list,
c.WIDTH - 13 * @as(i32, @intCast(entities_list.len)),
c.HEIGHT - 2 * c.TEXT_SIZE,
c.TEXT_SIZE, .black);
// Displays the mode the simulation is currently in
rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black);
// displays id of the element we hover over over
const entityInfo = self.getHighlightedEntityInfo(&buf) catch |err| {
std.debug.panic("Failed to capture highlighted entity info: {}\n", .{err});
};
rl.drawText(entityInfo, c.WIDTH - 30 * @as(i32, @intCast(entityInfo.len)), c.TEXT_SIZE / 2, c.TEXT_SIZE, .black);
}
/// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it
fn getIntersectingRoads(self: *Simulator, allocator: std.mem.Allocator, start: *const Node, end: *const Node) ![]st.IntersectionData {
var intersections: std.ArrayList(st.IntersectionData) = .empty;
var collision_point: rl.Vector2 = undefined;
outer: for (self.road_man.roads.items) |*road| {
// If there is no collision check the next road
if (!rl.checkCollisionLines(
start.pos,
end.pos,
road.nodes[0].pos,
road.nodes[1].pos,
&collision_point)) continue;
// Save the collision info
const data = st.IntersectionData {
.road = road,
.point = collision_point,
};
const node: Node = .init(0, data.point);
// here we need to check if the points captured already are already within the reach
for (intersections.items) |collision| {
if (node.withinSnappingRadius(collision.point))
continue :outer;
}
// This checks whether our collision point is actually to close to the existing node(s)
// to form another intersection there
if (self.node_man.getNodeWithinRadius(data.point) != null) continue;
try intersections.append(self.allocator, data);
}
// we must sort the items by the distance from the first (usually temp) node
const sorted_intersections = try intersections.toOwnedSlice(allocator);
std.sort.block(st.IntersectionData, sorted_intersections, start, utils.compareIntersections);
return sorted_intersections;
}
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_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});
};
for (0..intersections.len) |i| {
const intersection = intersections[i];
// 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) catch |err| {
std.debug.panic("Failed to unreference previous road: {}\n", .{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;
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});
};
}
const last_intersection_road = intersections[intersections.len - 1];
const last_intersection_node = self.node_man.getSelectedNode(last_intersection_road.point);
self.road_man.add(self.allocator, last_intersection_node, end) catch |err| {
std.debug.panic("Failed to add the road: {}\n", .{err});
};
}
pub fn update(self: *Simulator) void {
const pos = rl.getMousePosition();
self.road_man.highlighted_road = self.road_man.getSelectedRoad(pos);
self.node_man.highlighted_node = self.node_man.getNodeWithinRadius(pos);
}
/// Get ID info of the highlighted info and returns it
fn getHighlightedEntityInfo(self: *const Simulator, buf: *[1024]u8) ![:0]const u8 {
if (self.node_man.highlighted_node) |node|
return std.fmt.bufPrintZ(buf, "Node ID: {d}", .{node.id});
if (self.road_man.highlighted_road) |road|
return std.fmt.bufPrintZ(buf, "Road ID: {d}", .{road.id});
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", .{});
}
}
};