Compare commits
25 Commits
37e2f36ae9
...
base-road-
| Author | SHA1 | Date | |
|---|---|---|---|
| 3274692631 | |||
| 1558d5f57c | |||
| fdf672de4b | |||
| f7a1340500 | |||
| dd64ec648a | |||
| afd7aa50c4 | |||
| 643712f529 | |||
| 2a3064b0fe | |||
| e475814c85 | |||
| c02b2a5121 | |||
| 680e07c976 | |||
| 5040bbbe47 | |||
| 0d77c912a8 | |||
| e72aa8fc5f | |||
| 4ec32252cf | |||
| fc0341d9ad | |||
| 081e690dc3 | |||
| e75fc6dbe9 | |||
| 7348861145 | |||
| 9defec8448 | |||
| db16bafd6c | |||
| 750bad7f83 | |||
| d7c7320443 | |||
| be06634232 | |||
| fc67fe3d7e |
16
build.zig
16
build.zig
@@ -69,6 +69,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
// don't need and to put everything under a single module.
|
// don't need and to put everything under a single module.
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "base_road_network",
|
.name = "base_road_network",
|
||||||
|
.use_llvm = true,
|
||||||
.root_module = b.createModule(.{
|
.root_module = b.createModule(.{
|
||||||
// b.createModule defines a new module just like b.addModule but,
|
// b.createModule defines a new module just like b.addModule but,
|
||||||
// unlike b.addModule, it does not expose the module to consumers of
|
// unlike b.addModule, it does not expose the module to consumers of
|
||||||
@@ -130,6 +131,20 @@ pub fn build(b: *std.Build) void {
|
|||||||
run_cmd.addArgs(args);
|
run_cmd.addArgs(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tests = b.addTest(.{
|
||||||
|
.root_module = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/test.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
.imports = &.{
|
||||||
|
.{ .name = "raylib", .module = raylib },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
tests.root_module.linkLibrary(raylib_artifact);
|
||||||
|
const run_tests = b.addRunArtifact(tests);
|
||||||
|
|
||||||
// Creates an executable that will run `test` blocks from the provided module.
|
// Creates an executable that will run `test` blocks from the provided module.
|
||||||
// Here `mod` needs to define a target, which is why earlier we made sure to
|
// Here `mod` needs to define a target, which is why earlier we made sure to
|
||||||
// set the releative field.
|
// set the releative field.
|
||||||
@@ -156,6 +171,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
const test_step = b.step("test", "Run tests");
|
const test_step = b.step("test", "Run tests");
|
||||||
// test_step.dependOn(&run_mod_tests.step);
|
// test_step.dependOn(&run_mod_tests.step);
|
||||||
test_step.dependOn(&run_exe_tests.step);
|
test_step.dependOn(&run_exe_tests.step);
|
||||||
|
test_step.dependOn(&run_tests.step);
|
||||||
|
|
||||||
// Just like flags, top level steps are also listed in the `--help` menu.
|
// Just like flags, top level steps are also listed in the `--help` menu.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
const clr = @import("raylib").Color;
|
const clr = @import("raylib").Color;
|
||||||
|
|
||||||
/// Screen Width
|
/// Screen Width
|
||||||
pub const WIDTH = 1366;
|
pub const WIDTH = 1920;
|
||||||
/// Screen Height
|
/// Screen Height
|
||||||
pub const HEIGHT = 768;
|
pub const HEIGHT = 1080;
|
||||||
pub const BACKGROUND_COLOR = clr.dark_gray;
|
pub const BACKGROUND_COLOR = clr.light_gray;
|
||||||
pub const ALLOC_SIZE = 50;
|
|
||||||
|
|
||||||
/// Base node radius
|
/// Base node radius
|
||||||
pub const NODE_RADIUS = 20;
|
pub const NODE_RADIUS = 20;
|
||||||
@@ -17,6 +16,7 @@ pub const NODE_COLOUR = clr.brown;
|
|||||||
pub const NODE_TEMP_COLOUR = clr.orange;
|
pub const NODE_TEMP_COLOUR = clr.orange;
|
||||||
/// The colour of the node being at the cursor
|
/// The colour of the node being at the cursor
|
||||||
pub const NODE_CURSOR_COLOUR = clr.blue;
|
pub const NODE_CURSOR_COLOUR = clr.blue;
|
||||||
|
pub const NODE_RELATED_COLOUR = clr.purple;
|
||||||
|
|
||||||
/// Road (line) size
|
/// Road (line) size
|
||||||
pub const ROAD_SIZE = 20;
|
pub const ROAD_SIZE = 20;
|
||||||
@@ -24,3 +24,10 @@ pub const ROAD_SIZE = 20;
|
|||||||
pub const ROAD_COLOUR = clr.black;
|
pub const ROAD_COLOUR = clr.black;
|
||||||
/// Colour of the road that is highlighted
|
/// Colour of the road that is highlighted
|
||||||
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
|
pub const ROAD_HIGHLIGHTED_COLOUR = clr.green;
|
||||||
|
|
||||||
|
/// Regular text size
|
||||||
|
pub const TEXT_SIZE = 50;
|
||||||
|
/// Text size that is used for displaying entity IDs
|
||||||
|
pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2;
|
||||||
|
/// Text colour in which entity IDs are displayed if toggled
|
||||||
|
pub const ENTITY_DATA_TEXT_COLOUR = clr.orange;
|
||||||
31
src/common/structures.zig
Normal file
31
src/common/structures.zig
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
const Vector2 = @import("raylib").Vector2;
|
||||||
|
|
||||||
|
const Road = @import("../infrastructure/road.zig").Road;
|
||||||
|
const Node = @import("../infrastructure/node.zig").Node;
|
||||||
|
|
||||||
|
/// This is a simple equivalent to something like abstract class, so the simulator class has only one variable
|
||||||
|
/// which tracks which object is highlighted and the logic that seeks to find the current highlighted class doesn't need
|
||||||
|
/// to figure out which highlighted entity has priority in case of overlap;
|
||||||
|
/// given that car can be on node and road, and node itself is on road(s)
|
||||||
|
///
|
||||||
|
/// TLDR: Can point to either entity type (node, road, etc.)
|
||||||
|
pub const Entity = union(enum) {
|
||||||
|
node: *Node,
|
||||||
|
road: *Road,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Represents intersection data, mainly used in cases where we draw over existing roads
|
||||||
|
/// and wish to see data about such intersections or collisions
|
||||||
|
pub const IntersectionData = struct {
|
||||||
|
/// Tracks the location of the intersection
|
||||||
|
pos: Vector2,
|
||||||
|
/// Points to the road where the intersection occured
|
||||||
|
road: *Road,
|
||||||
|
/// Tracks whether this intersection is actually an origin point
|
||||||
|
/// (In simple terms this means whether the point/node that intersects is also the start/end node)
|
||||||
|
///
|
||||||
|
/// We do this because the referencing logic is very strict and will return an error
|
||||||
|
/// if we try to reference something that is already referenced, due to stricter approach helping with
|
||||||
|
/// earlier bug and error detection
|
||||||
|
origin: bool,
|
||||||
|
};
|
||||||
29
src/common/utils.zig
Normal file
29
src/common/utils.zig
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const Vector2 = @import("raylib").Vector2;
|
||||||
|
|
||||||
|
const st = @import("structures.zig");
|
||||||
|
const Node = @import("../infrastructure/node.zig").Node;
|
||||||
|
|
||||||
|
/// Returns distance between two nodes, used mostly for road length and possibly for A* heuristics
|
||||||
|
pub fn calculate_length(start: Vector2, end: Vector2) f32 {
|
||||||
|
const x_diff = end.x - start.x;
|
||||||
|
const y_diff = end.y - start.y;
|
||||||
|
|
||||||
|
const squared_solution = x_diff * x_diff + y_diff * y_diff;
|
||||||
|
return @sqrt(squared_solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Comparator function that compares intersection proximity from the node in order to aid the sorting function
|
||||||
|
pub fn compareIntersections(ctx: *const Node, inter_a: st.IntersectionData, inter_b: st.IntersectionData) bool {
|
||||||
|
const distance_a = ctx.getRelativeInterDistance(inter_a);
|
||||||
|
const distance_b = ctx.getRelativeInterDistance(inter_b);
|
||||||
|
|
||||||
|
return distance_a < distance_b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns vector from P1 to P2
|
||||||
|
pub fn getVectorP1P2(p1: Vector2, p2: Vector2) Vector2 {
|
||||||
|
return .{
|
||||||
|
.x = p2.x - p1.x,
|
||||||
|
.y = p2.y - p1.y,
|
||||||
|
};
|
||||||
|
}
|
||||||
6
src/errors.zig
Normal file
6
src/errors.zig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/// Contains collection of errors connected with an entity (road, node, car, etc.)
|
||||||
|
pub const Entity = error {
|
||||||
|
AlreadyReferenced,
|
||||||
|
NotFound,
|
||||||
|
HasReferences,
|
||||||
|
};
|
||||||
@@ -1,14 +1,21 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("../constants.zig");
|
const c = @import("../common/constants.zig");
|
||||||
|
const e = @import("../errors.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
|
const ut = @import("../common/utils.zig");
|
||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
|
|
||||||
pub const Node = struct {
|
pub const Node = struct {
|
||||||
|
/// Possibly unnecessary, but for now it's good as a secondary mean of identification
|
||||||
id: usize,
|
id: usize,
|
||||||
|
/// Node's position on the simulation 'field'
|
||||||
pos: rl.Vector2,
|
pos: rl.Vector2,
|
||||||
|
/// Contains references of all the roads this node is connected to
|
||||||
roads: std.ArrayList(*Road),
|
roads: std.ArrayList(*Road),
|
||||||
|
|
||||||
|
/// Simple constructor
|
||||||
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
|
||||||
return .{
|
return .{
|
||||||
.id = new_id,
|
.id = new_id,
|
||||||
@@ -17,26 +24,90 @@ pub const Node = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Node, allocator: std.mem.Allocator) void {
|
/// This function frees the list that holds road references that are connected to this node
|
||||||
|
///
|
||||||
|
/// Next it invalidates itself (the pointer), making it invalid
|
||||||
|
pub fn deinit(self: *Node, allocator: std.mem.Allocator) !void {
|
||||||
|
if (self.roads.items.len != 0) return e.Entity.AlreadyReferenced;
|
||||||
|
|
||||||
self.roads.deinit(allocator);
|
self.roads.deinit(allocator);
|
||||||
|
allocator.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const Node, direct_colour: ?rl.Color) void {
|
/// Simple function which draws the node
|
||||||
|
pub fn draw(self: *const Node, direct_colour: ?rl.Color, display_info: bool) void {
|
||||||
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
|
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
|
||||||
|
|
||||||
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
|
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
|
||||||
|
|
||||||
|
if (!display_info) return;
|
||||||
|
|
||||||
|
var buf: [100]u8 = undefined;
|
||||||
|
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
|
||||||
|
std.debug.panic("Failed to allocate space for ID???: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
const info_x = @as(i32, @trunc(self.pos.x)) - c.NODE_RADIUS / 2;
|
||||||
|
const info_y = @as(i32, @trunc(self.pos.y)) - c.NODE_RADIUS / 2;
|
||||||
|
|
||||||
|
rl.drawText(entity_info, info_x, info_y,
|
||||||
|
c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn posWithinRadius(self: *const Node, pos: rl.Vector2) bool {
|
/// Determines whether the pos (location) is within the snapping radius of the node
|
||||||
|
pub fn withinSnapRadius(self: *const Node, pos: rl.Vector2) bool {
|
||||||
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
|
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Determines whether the pos (location) is within the strict (visual representation) radius of the node
|
||||||
|
pub fn withinRadius(self: *const Node, pos: rl.Vector2) bool {
|
||||||
|
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_RADIUS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tries to reference the passed road to self
|
||||||
|
///
|
||||||
|
/// Returns an error if the passed road cannot be appended to the list or if said road is already in the list
|
||||||
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road_to_add: *Road) !void {
|
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road_to_add: *Road) !void {
|
||||||
// Note the road_to_add pointer must be one from the roads list as otherwise the pointer is dangling one
|
// Note the road_to_add pointer must be one from the roads list as otherwise the pointer is dangling one
|
||||||
for (self.roads.items) |road| {
|
for (self.roads.items) |road| {
|
||||||
if (road.*.id == road_to_add.*.id) return error.RoadAlreadyReferenced;
|
if (road == road_to_add) return e.Entity.AlreadyReferenced;
|
||||||
}
|
}
|
||||||
|
|
||||||
try self.roads.append(allocator, road_to_add);
|
try self.roads.append(allocator, road_to_add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to unreference the passed road from self (node)
|
||||||
|
///
|
||||||
|
/// Returns whether the node still has node references (true) or not (false)
|
||||||
|
///
|
||||||
|
/// Returns an error if the road is not referenced to self in the first place
|
||||||
|
pub fn unreferenceRoad(self: *Node, road_to_remove: *const Road) !void {
|
||||||
|
for (0..self.roads.items.len) |i| {
|
||||||
|
if (self.roads.items[i] != road_to_remove) continue;
|
||||||
|
|
||||||
|
_ = self.roads.swapRemove(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Entity.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns distance between the node and intersection object location
|
||||||
|
pub fn getRelativeInterDistance(self: *const Node, intersection: st.IntersectionData) f32 {
|
||||||
|
return ut.calculate_length(intersection.pos, self.pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Searches for the road pointer within roads list
|
||||||
|
pub fn roadsContains(self: *const Node, road_to_search: *const Road) bool {
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
if (road == road_to_search) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO tests
|
||||||
|
// road reference test
|
||||||
|
// pos within node test
|
||||||
|
// deinit test
|
||||||
|
// roads ptr list test
|
||||||
@@ -1,15 +1,20 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Vector2 = @import("raylib").Vector2;
|
const Vector2 = @import("raylib").Vector2;
|
||||||
|
|
||||||
const c = @import("../constants.zig");
|
const c = @import("../common/constants.zig");
|
||||||
|
const e = @import("../errors.zig");
|
||||||
const Node = @import("node.zig").Node;
|
const Node = @import("node.zig").Node;
|
||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
|
|
||||||
pub const NodeManager = struct {
|
pub const NodeManager = struct {
|
||||||
|
/// Tracks which ID the next added node will get
|
||||||
next_id: usize,
|
next_id: usize,
|
||||||
nodes: std.ArrayList(Node),
|
/// Tracks all the node (pointers)
|
||||||
|
nodes: std.ArrayList(*Node),
|
||||||
|
/// Tracks the temporary node, being part of the newly built road
|
||||||
temp_node: ?*Node,
|
temp_node: ?*Node,
|
||||||
|
|
||||||
|
/// Constructor (for convenience)
|
||||||
pub fn init() NodeManager {
|
pub fn init() NodeManager {
|
||||||
return .{
|
return .{
|
||||||
.next_id = 0,
|
.next_id = 0,
|
||||||
@@ -18,16 +23,19 @@ pub const NodeManager = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
|
/// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself
|
||||||
for (self.nodes.items) |*node| {
|
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
||||||
node.deinit(allocator);
|
for (self.nodes.items) |node| {
|
||||||
|
node.roads.clearRetainingCapacity();
|
||||||
|
try node.deinit(allocator);
|
||||||
}
|
}
|
||||||
self.nodes.deinit(allocator);
|
self.nodes.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const NodeManager, pos: Vector2) void {
|
/// Regular draw function
|
||||||
|
pub fn draw(self: *const NodeManager, pos: Vector2, display_info: bool) void {
|
||||||
for (self.nodes.items) |node| {
|
for (self.nodes.items) |node| {
|
||||||
node.draw(null);
|
node.draw(null, display_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.temp_node) |node| {
|
if (self.temp_node) |node| {
|
||||||
@@ -35,25 +43,41 @@ pub const NodeManager = struct {
|
|||||||
var cur_node = Node.init(0, pos);
|
var cur_node = Node.init(0, pos);
|
||||||
// Temporary road that is to be drawn as one in the making
|
// Temporary road that is to be drawn as one in the making
|
||||||
const road: Road = .init(0, node, &cur_node);
|
const road: Road = .init(0, node, &cur_node);
|
||||||
road.draw();
|
road.draw(false, false);
|
||||||
|
|
||||||
node.*.draw(c.NODE_TEMP_COLOUR);
|
node.draw(c.NODE_TEMP_COLOUR, display_info);
|
||||||
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
cur_node.draw(c.NODE_CURSOR_COLOUR, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tries to find a node which snapping radius covers the pos
|
||||||
|
///
|
||||||
|
/// If it does it returns a reference to it, otherwise null
|
||||||
|
pub fn getNodeIfExists(self: *const NodeManager, pos: Vector2) ?*Node {
|
||||||
|
for (self.nodes.items) |node| {
|
||||||
|
if (node.withinSnapRadius(pos)) return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if there is a node pointer within the snap radius of pos coordinate;
|
||||||
|
/// otherwise creates a new node and returns its pointer
|
||||||
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
|
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
|
||||||
for (self.nodes.items) |*node| {
|
if (self.getNodeIfExists(pos)) |node| {
|
||||||
if (node.posWithinRadius(pos)) return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No node is within that position, so we must create a new one
|
// No node is within that position, so we must create a new one
|
||||||
const node: Node = .init(self.getNextID(), pos);
|
const node: Node = .init(self.getNextID(), pos);
|
||||||
try self.nodes.append(allocator, node);
|
const node_ptr = try allocator.create(Node);
|
||||||
|
node_ptr.* = node;
|
||||||
|
try self.nodes.append(allocator, node_ptr);
|
||||||
|
|
||||||
return &self.nodes.items[self.nodes.items.len - 1];
|
return self.nodes.items[self.nodes.items.len - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets next id, resets only on clear()
|
||||||
fn getNextID(self: *NodeManager) usize {
|
fn getNextID(self: *NodeManager) usize {
|
||||||
const id = self.next_id;
|
const id = self.next_id;
|
||||||
self.next_id += 1;
|
self.next_id += 1;
|
||||||
@@ -61,12 +85,81 @@ pub const NodeManager = struct {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
|
/// Clears all existing nodes connected, not deinitialisation
|
||||||
|
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
||||||
self.temp_node = null;
|
self.temp_node = null;
|
||||||
for (self.nodes.items) |*node| {
|
for (self.nodes.items) |node| {
|
||||||
node.deinit(allocator);
|
node.roads.clearRetainingCapacity();
|
||||||
|
try node.deinit(allocator);
|
||||||
}
|
}
|
||||||
self.nodes.clearRetainingCapacity();
|
self.nodes.clearRetainingCapacity();
|
||||||
self.next_id = 0;
|
self.next_id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes node, returns error if node still has road references
|
||||||
|
pub fn deleteNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
|
||||||
|
for (0..self.nodes.items.len) |i| {
|
||||||
|
if (self.nodes.items[i] != node_to_delete) continue;
|
||||||
|
|
||||||
|
try node_to_delete.deinit(allocator);
|
||||||
|
_ = self.nodes.swapRemove(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Entity.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs a scan through all the nodes and returns the reference to one that is within the radius
|
||||||
|
pub fn getHighlightedNode(self: *const NodeManager, pos: Vector2) ?*Node {
|
||||||
|
for (self.nodes.items) |node| {
|
||||||
|
if (node.withinRadius(pos)) return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Essentially what it does is it sets temp node pointer to null and
|
||||||
|
/// if the node it pointed at had no road references (essentially it was a new node for road building),
|
||||||
|
/// it deletes the node from the node list as well
|
||||||
|
pub fn deleteTempNode(self: *NodeManager, allocator: std.mem.Allocator) void {
|
||||||
|
if (self.temp_node == null) return;
|
||||||
|
|
||||||
|
const node = self.temp_node.?;
|
||||||
|
self.temp_node = null;
|
||||||
|
|
||||||
|
if (node.roads.items.len != 0) return;
|
||||||
|
self.deleteNode(allocator, node) catch |err| {
|
||||||
|
std.debug.panic("Failed to delete the temporary node: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const expect = std.testing.expect;
|
||||||
|
|
||||||
|
test "id tracking" {
|
||||||
|
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var node_man: NodeManager = .init();
|
||||||
|
defer node_man.deinit(allocator);
|
||||||
|
const n = 5;
|
||||||
|
|
||||||
|
for (0..n) |_| {
|
||||||
|
const node: Node = .init(node_man.getNextID(), Vector2 {
|
||||||
|
.x = 500,
|
||||||
|
.y = 500,
|
||||||
|
});
|
||||||
|
const node_ptr = try allocator.create(Node);
|
||||||
|
node_ptr.* = node;
|
||||||
|
try node_man.nodes.append(allocator, node_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
try expect(node_man.next_id == n);
|
||||||
|
try expect(node_man.nodes.items.len == n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO tests
|
||||||
|
// force resize pointer test
|
||||||
|
// deleting nodes
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("../constants.zig");
|
const c = @import("../common/constants.zig");
|
||||||
|
const e = @import("../errors.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
|
const ut = @import("../common/utils.zig");
|
||||||
const Node = @import("node.zig").Node;
|
const Node = @import("node.zig").Node;
|
||||||
|
|
||||||
pub const Road = struct {
|
pub const Road = struct {
|
||||||
@@ -11,6 +14,7 @@ pub const Road = struct {
|
|||||||
/// Calculated road length
|
/// Calculated road length
|
||||||
length: f32,
|
length: f32,
|
||||||
|
|
||||||
|
/// Constructor, which sets nodes for the road and calculates its length
|
||||||
pub fn init(new_id: usize, start: *Node, end: *Node) Road {
|
pub fn init(new_id: usize, start: *Node, end: *Node) Road {
|
||||||
var road: Road = .{
|
var road: Road = .{
|
||||||
.id = new_id,
|
.id = new_id,
|
||||||
@@ -18,23 +22,109 @@ pub const Road = struct {
|
|||||||
.length = 0,
|
.length = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
road.length = road.calculate_length();
|
road.length = ut.calculate_length(start.pos, end.pos);
|
||||||
return road;
|
return road;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This makes the road ptr DEAD
|
||||||
|
pub fn deinit(self: *Road, allocator: std.mem.Allocator) void {
|
||||||
|
allocator.destroy(self);
|
||||||
|
}
|
||||||
|
|
||||||
/// Calculates length of the road by taking its two nodes
|
/// Calculates length of the road by taking its two nodes
|
||||||
fn calculate_length(self: *const Road) f32 {
|
fn calculate_length(self: *const Road) f32 {
|
||||||
const start = self.nodes[0];
|
const start = self.nodes[0];
|
||||||
const end = self.nodes[1];
|
const end = self.nodes[1];
|
||||||
|
|
||||||
const x_diff = end.*.pos.x - start.*.pos.x;
|
const x_diff = end.pos.x - start.pos.x;
|
||||||
const y_diff = end.*.pos.y - start.*.pos.y;
|
const y_diff = end.pos.y - start.pos.y;
|
||||||
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
||||||
|
|
||||||
return @sqrt(square_diff);
|
return @sqrt(square_diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const Road) void {
|
/// Simple function which draws the node
|
||||||
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR);
|
///
|
||||||
|
/// In the future as we improve and make roads more complex with multiple lanes and such
|
||||||
|
/// it will gradually become more complex
|
||||||
|
pub fn draw(self: *const Road, highlighted: bool, display_info: bool) void {
|
||||||
|
const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR;
|
||||||
|
rl.drawLineEx(self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE, colour);
|
||||||
|
|
||||||
|
if (!display_info) return;
|
||||||
|
|
||||||
|
var buf: [100]u8 = undefined;
|
||||||
|
const entity = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
|
||||||
|
std.debug.panic("Could not allocate ID into string???: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
const distance = ut.getVectorP1P2(self.nodes[0].pos, self.nodes[1].pos);
|
||||||
|
const entity_info_pos: rl.Vector2 = .{
|
||||||
|
.x = self.nodes[0].pos.x + distance.x / 2 - c.ROAD_SIZE / 2,
|
||||||
|
.y = self.nodes[0].pos.y + distance.y / 2 - c.ROAD_SIZE / 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
rl.drawText(entity, @trunc(entity_info_pos.x), @trunc(entity_info_pos.y),
|
||||||
|
c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Important: after this function executes, this road is no longer reachable from its bounding nodes
|
||||||
|
///
|
||||||
|
/// The function unreferences self (road) from its bounding nodes
|
||||||
|
pub fn unreferenceNodes(self: *const Road) !void {
|
||||||
|
try self.nodes[0].unreferenceRoad(self);
|
||||||
|
try self.nodes[1].unreferenceRoad(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks whether pos coordinate is on the referenced road
|
||||||
|
pub fn collides(self: *const Road, pos: rl.Vector2) bool {
|
||||||
|
return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates node reference old_node => new node; returns error if old_node does not exist
|
||||||
|
pub fn updateNodeReference(self: *Road, old_node: *Node, new_node: *Node) !void {
|
||||||
|
for (0..self.nodes.len) |i| {
|
||||||
|
if (self.nodes[i] != old_node) continue;
|
||||||
|
|
||||||
|
self.nodes[i] = new_node;
|
||||||
|
// As nodes change, road's length must be recalculated
|
||||||
|
self.length = ut.calculate_length(self.nodes[0].pos, self.nodes[1].pos);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Entity.NotFound;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const expect = std.testing.expect;
|
||||||
|
|
||||||
|
test "valid road nodes" {
|
||||||
|
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
const start: Node = .init(34, .{.x = 500, .y = 500});
|
||||||
|
const start_ptr = try allocator.create(Node);
|
||||||
|
defer {
|
||||||
|
start_ptr.deinit(allocator);
|
||||||
|
allocator.destroy(start_ptr);
|
||||||
|
}
|
||||||
|
start_ptr.* = start;
|
||||||
|
|
||||||
|
const end: Node = .init(227, .{.x = 600, .y = 500});
|
||||||
|
const end_ptr = try allocator.create(Node);
|
||||||
|
defer {
|
||||||
|
end_ptr.deinit(allocator);
|
||||||
|
allocator.destroy(end_ptr);
|
||||||
|
}
|
||||||
|
end_ptr.* = end;
|
||||||
|
|
||||||
|
const road: Road = .init(11, start_ptr, end_ptr);
|
||||||
|
|
||||||
|
try expect(road.nodes[0].id == 34);
|
||||||
|
try expect(road.nodes[1].id == 227);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO tests
|
||||||
|
// test every case error for every function that can return an error
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
const e = @import("../errors.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
|
const ut = @import("../common/utils.zig");
|
||||||
const Road = @import("road.zig").Road;
|
const Road = @import("road.zig").Road;
|
||||||
const Node = @import("node.zig").Node;
|
const Node = @import("node.zig").Node;
|
||||||
|
|
||||||
pub const RoadManager = struct {
|
pub const RoadManager = struct {
|
||||||
next_id: usize,
|
next_id: usize,
|
||||||
roads: std.ArrayList(Road),
|
roads: std.ArrayList(*Road),
|
||||||
|
|
||||||
pub fn init() RoadManager {
|
pub fn init() RoadManager {
|
||||||
return .{
|
return .{
|
||||||
@@ -13,25 +18,35 @@ pub const RoadManager = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deinitialises every road (pointer) and then the list itself
|
||||||
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
|
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
road.deinit(allocator);
|
||||||
|
}
|
||||||
self.roads.deinit(allocator);
|
self.roads.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const RoadManager) void {
|
/// Draws all the roads in the list, sends the information ahead whether the road drawn should be highlighted
|
||||||
|
pub fn draw(self: *const RoadManager, highlighted_road: ?*Road, display_info: bool) void {
|
||||||
for (self.roads.items) |road| {
|
for (self.roads.items) |road| {
|
||||||
road.draw();
|
const is_highlighted = if (highlighted_road) |h_road| road == h_road else false;
|
||||||
|
road.draw(is_highlighted, display_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Function which creates the road object, its pointer, adds it to the list
|
||||||
|
/// and then also references that same road to the bounding nodes
|
||||||
pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void {
|
pub fn addRoad(self: *RoadManager, allocator: std.mem.Allocator, start: *Node, end: *Node) !void {
|
||||||
const road: Road = .init(self.getNextID(), start, end);
|
const road_ptr = try allocator.create(Road);
|
||||||
try self.roads.append(allocator, road);
|
road_ptr.* = Road.init(self.getNextID(), start, end);
|
||||||
|
try self.roads.append(allocator, road_ptr);
|
||||||
|
|
||||||
const ref = &self.roads.items[self.roads.items.len - 1];
|
const ref = self.roads.items[self.roads.items.len - 1];
|
||||||
try start.referenceRoad(allocator, ref);
|
try start.referenceRoad(allocator, ref);
|
||||||
try end.referenceRoad(allocator, ref);
|
try end.referenceRoad(allocator, ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the id, and increases it by one; used for generating ID's for new entities
|
||||||
fn getNextID(self: *RoadManager) usize {
|
fn getNextID(self: *RoadManager) usize {
|
||||||
const id = self.next_id;
|
const id = self.next_id;
|
||||||
self.next_id += 1;
|
self.next_id += 1;
|
||||||
@@ -39,8 +54,84 @@ pub const RoadManager = struct {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(self: *RoadManager) void {
|
/// Deinits all the roads, clears them but not deiniting the list itself; also resets the next ID var
|
||||||
|
pub fn clear(self: *RoadManager, allocator: std.mem.Allocator) void {
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
road.deinit(allocator);
|
||||||
|
}
|
||||||
self.roads.clearRetainingCapacity();
|
self.roads.clearRetainingCapacity();
|
||||||
self.next_id = 0;
|
self.next_id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the references of the road, from the nodes that bound that road
|
||||||
|
///
|
||||||
|
/// Then it deinitialises the road and removes it from the list
|
||||||
|
///
|
||||||
|
/// Will return an error if the road itself is not present in the list
|
||||||
|
pub fn deleteRoad(self: *RoadManager, allocator: std.mem.Allocator, road_to_delete: *Road) !void {
|
||||||
|
// unreference the road from its bounding functions
|
||||||
|
road_to_delete.unreferenceNodes() catch |err| {
|
||||||
|
std.debug.panic("Failed to unreference the road from its nodes: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
for (0..self.roads.items.len) |i| {
|
||||||
|
if (self.roads.items[i] != road_to_delete) continue;
|
||||||
|
|
||||||
|
road_to_delete.deinit(allocator);
|
||||||
|
_ = self.roads.swapRemove(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Entity.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns if pos is pointing at a road, or null if it isn't at any
|
||||||
|
pub fn getHighlightedRoad(self: *const RoadManager, pos: Vector2) ?*Road {
|
||||||
|
for (self.roads.items) |road| {
|
||||||
|
if (road.collides(pos)) return road;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Vector2 = @import("raylib").Vector2;
|
||||||
|
const expect = std.testing.expect;
|
||||||
|
|
||||||
|
test "id tracking" {
|
||||||
|
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var road_man: RoadManager = .init();
|
||||||
|
defer road_man.deinit(allocator);
|
||||||
|
const n = 5;
|
||||||
|
|
||||||
|
const start: Node = .init(0, .{.x = 0, .y = 0});
|
||||||
|
const start_ptr = try allocator.create(Node);
|
||||||
|
start_ptr.* = start;
|
||||||
|
|
||||||
|
const end: Node = .init(1, .{.x = 100, .y = 100});
|
||||||
|
const end_ptr = try allocator.create(Node);
|
||||||
|
end_ptr.* = end;
|
||||||
|
|
||||||
|
defer {
|
||||||
|
start_ptr.deinit(allocator);
|
||||||
|
end_ptr.deinit(allocator);
|
||||||
|
|
||||||
|
allocator.destroy(start_ptr);
|
||||||
|
allocator.destroy(end_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (0..n) |_| {
|
||||||
|
try road_man.addRoad(allocator, start_ptr, end_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
try expect(road_man.next_id == n);
|
||||||
|
try expect(road_man.roads.items.len == n);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO tests
|
||||||
|
// force resize pointer test
|
||||||
|
// add, remove road
|
||||||
|
// destroy road and then verify the nodes do not have a pointer to it
|
||||||
13
src/main.zig
13
src/main.zig
@@ -1,15 +1,9 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("constants.zig");
|
const c = @import("common/constants.zig");
|
||||||
const Simulator = @import("simulator.zig").Simulator;
|
const Simulator = @import("simulator.zig").Simulator;
|
||||||
|
|
||||||
// TODO PLANS
|
|
||||||
// Implement that roads, nodes in managers will be stored as pointers (allocated in heap)
|
|
||||||
// So when appending causes the list to relocate, nothing will have to change
|
|
||||||
// since the allocated nodes/roads will remain where they were
|
|
||||||
// Additionally add more robust error and test handling
|
|
||||||
|
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const allocator = init.gpa;
|
const allocator = init.gpa;
|
||||||
|
|
||||||
@@ -25,7 +19,9 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
rl.setTargetFPS(rl.getMonitorRefreshRate(monitor));
|
rl.setTargetFPS(rl.getMonitorRefreshRate(monitor));
|
||||||
|
|
||||||
var sim: Simulator = .init(allocator);
|
var sim: Simulator = .init(allocator);
|
||||||
defer sim.deinit();
|
defer sim.deinit() catch |err| {
|
||||||
|
std.debug.panic("Failed to deinitialise the sim: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
while (!rl.windowShouldClose()) {
|
while (!rl.windowShouldClose()) {
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
@@ -33,6 +29,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
|
|
||||||
const pos = rl.getMousePosition();
|
const pos = rl.getMousePosition();
|
||||||
sim.handleInput(pos);
|
sim.handleInput(pos);
|
||||||
|
sim.update(pos);
|
||||||
|
|
||||||
sim.draw(pos);
|
sim.draw(pos);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,54 +1,148 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
const c = @import("constants.zig");
|
const c = @import("common/constants.zig");
|
||||||
|
const st = @import("common/structures.zig");
|
||||||
|
const ut = @import("common/utils.zig");
|
||||||
|
const Road = @import("infrastructure/road.zig").Road;
|
||||||
|
const Node = @import("infrastructure/node.zig").Node;
|
||||||
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
||||||
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||||
|
|
||||||
pub const Simulator = struct {
|
pub const Simulator = struct {
|
||||||
|
/// allocator for convenience
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
|
/// 'class' tracking all the nodes (and appropriate functions)
|
||||||
node_man: NodeManager,
|
node_man: NodeManager,
|
||||||
|
/// 'class' tracking all the roads (and appropriate functions)
|
||||||
road_man: RoadManager,
|
road_man: RoadManager,
|
||||||
// vars
|
// vars
|
||||||
|
/// Tracks whether next road will start building from the node the last road was built at
|
||||||
auto_continue: bool,
|
auto_continue: bool,
|
||||||
|
/// Tracks whether the system will delete the road cursor is pointed at
|
||||||
|
/// (in such case, the road-to-be-deleted will also be highlighted)
|
||||||
|
delete_mode: bool,
|
||||||
|
/// Tracks whether highlighting all entities that are connected to hovered entity is enabled
|
||||||
|
///
|
||||||
|
/// For example, if I hover over a node it will highlight all roads that are connected to it;
|
||||||
|
/// Same goes for hovering over a road or in the future, a car (might show destination and path to it)
|
||||||
|
///
|
||||||
|
/// Note: It only works outside of the delete mode
|
||||||
|
show_connections: bool,
|
||||||
|
/// Toggle that tracks whether ID (or possibly something more in the future) of every entity is displayed in GUI
|
||||||
|
display_entity_info: bool,
|
||||||
|
highlighted_entity: ?st.Entity,
|
||||||
|
|
||||||
|
/// Constructor for convenience
|
||||||
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
pub fn init(new_allocator: std.mem.Allocator) Simulator {
|
||||||
return .{
|
return .{
|
||||||
.allocator = new_allocator,
|
.allocator = new_allocator,
|
||||||
.node_man = .init(),
|
.node_man = .init(),
|
||||||
.road_man = .init(),
|
.road_man = .init(),
|
||||||
.auto_continue = false,
|
.auto_continue = false,
|
||||||
|
.delete_mode = false,
|
||||||
|
.show_connections = false,
|
||||||
|
.display_entity_info = false,
|
||||||
|
.highlighted_entity = null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Simulator) void {
|
/// Deinitialisation of node and road objects
|
||||||
|
pub fn deinit(self: *Simulator) !void {
|
||||||
self.road_man.deinit(self.allocator);
|
self.road_man.deinit(self.allocator);
|
||||||
self.node_man.deinit(self.allocator);
|
try self.node_man.deinit(self.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Main draw function exposed to RayLib's loop
|
||||||
pub fn draw(self: *const Simulator, pos: rl.Vector2) void {
|
pub fn draw(self: *const Simulator, pos: rl.Vector2) void {
|
||||||
rl.clearBackground(c.BACKGROUND_COLOR);
|
rl.clearBackground(c.BACKGROUND_COLOR);
|
||||||
|
|
||||||
self.road_man.draw();
|
var highlighted_road: ?*Road = null;
|
||||||
self.node_man.draw(pos);
|
if (self.delete_mode) {
|
||||||
|
if (self.highlighted_entity) |entity| {
|
||||||
|
if (entity == .road) highlighted_road = entity.road;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.road_man.draw(highlighted_road, self.display_entity_info);
|
||||||
|
self.node_man.draw(pos, self.display_entity_info);
|
||||||
|
|
||||||
|
self.drawRelatedSelectedEntities();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drawRelatedSelectedEntities(self: *const Simulator) void {
|
||||||
|
if (!self.show_connections or self.highlighted_entity == null) return;
|
||||||
|
const h_entity = self.highlighted_entity.?;
|
||||||
|
|
||||||
|
switch (h_entity) {
|
||||||
|
.node => {
|
||||||
|
const node = h_entity.node;
|
||||||
|
|
||||||
|
for (node.roads.items) |road| {
|
||||||
|
road.draw(true, self.display_entity_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
node.draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
|
||||||
|
},
|
||||||
|
.road => {
|
||||||
|
const road = h_entity.road;
|
||||||
|
|
||||||
|
road.draw(true, self.display_entity_info);
|
||||||
|
|
||||||
|
road.nodes[0].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
|
||||||
|
road.nodes[1].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update tick
|
||||||
|
pub fn update(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
self.updateHighlightedEntity(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exposed input handling function exposed to raylib
|
||||||
pub fn handleInput(self: *Simulator, pos: rl.Vector2) void {
|
pub fn handleInput(self: *Simulator, pos: rl.Vector2) void {
|
||||||
self.handleKeyboardInput();
|
self.handleKeyboardInput();
|
||||||
self.handleMouseInput(pos);
|
self.handleMouseInput(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sub input handling function for keyboard input only
|
||||||
fn handleKeyboardInput(self: *Simulator) void {
|
fn handleKeyboardInput(self: *Simulator) void {
|
||||||
self.auto_continue = rl.isKeyDown(.left_control);
|
self.auto_continue = rl.isKeyDown(.left_control);
|
||||||
|
self.delete_mode = rl.isKeyDown(.left_shift);
|
||||||
|
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
|
||||||
|
|
||||||
if (rl.isKeyReleased(.c)) self.clear();
|
if (rl.isKeyReleased(.tab)) self.display_entity_info = !self.display_entity_info;
|
||||||
|
|
||||||
|
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
|
||||||
|
std.debug.panic("Failed to clear the entities: {}\n", .{err});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sub input handling function for mouse input only
|
||||||
fn handleMouseInput(self: *Simulator, pos: rl.Vector2) void {
|
fn handleMouseInput(self: *Simulator, pos: rl.Vector2) void {
|
||||||
if (rl.isMouseButtonReleased(.left)) self.leftClickEvent(pos);
|
if (rl.isMouseButtonReleased(.left)) {
|
||||||
|
self.leftClickEvent(pos);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rl.isMouseButtonReleased(.right)) self.node_man.deleteTempNode(self.allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function that handles functionality that executes upon left click
|
||||||
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
|
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
if (self.delete_mode and self.highlighted_entity != null and self.highlighted_entity.? == .road) {
|
||||||
|
self.delete_road() catch |err| {
|
||||||
|
std.debug.panic("Failed to delete the road: {}\n", .{err});
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.new_road(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// User initiated road building functionality
|
||||||
|
fn new_road(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
if (self.show_connections) return;
|
||||||
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
|
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
|
||||||
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
|
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
|
||||||
pos.x, pos.y, err
|
pos.x, pos.y, err
|
||||||
@@ -56,9 +150,14 @@ pub const Simulator = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (self.node_man.temp_node) |temp| {
|
if (self.node_man.temp_node) |temp| {
|
||||||
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
|
// Prevents the road from being attached to 2 identical nodes (0 length road)
|
||||||
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
|
if (temp.id == cur_node.id) return;
|
||||||
|
|
||||||
|
const intersections = self.getIntersectingRoads(self.allocator, temp, cur_node) catch |err| {
|
||||||
|
std.debug.panic("Intersection selection failure: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
defer self.allocator.free(intersections);
|
||||||
|
self.splitRoadsByIntersections(intersections, temp, cur_node);
|
||||||
|
|
||||||
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
||||||
return;
|
return;
|
||||||
@@ -67,8 +166,218 @@ pub const Simulator = struct {
|
|||||||
self.node_man.temp_node = cur_node;
|
self.node_man.temp_node = cur_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear(self: *Simulator) void {
|
/// User initiated road destroying functionality
|
||||||
self.road_man.clear();
|
fn delete_road(self: *Simulator) !void {
|
||||||
self.node_man.clear(self.allocator);
|
// We can trust this because this only gets called if valid and if type is road
|
||||||
|
std.debug.assert(self.highlighted_entity != null and self.highlighted_entity.? == .road);
|
||||||
|
const h_road = self.highlighted_entity.?.road;
|
||||||
|
|
||||||
|
const start_node = h_road.nodes[0];
|
||||||
|
const end_node = h_road.nodes[1];
|
||||||
|
|
||||||
|
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
||||||
|
std.debug.panic("Road deletion failed: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (start_node.roads.items.len == 0)
|
||||||
|
try self.node_man.deleteNode(self.allocator, start_node);
|
||||||
|
|
||||||
|
if (end_node.roads.items.len == 0)
|
||||||
|
try self.node_man.deleteNode(self.allocator, end_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clearing node and road lists without deinitialising them (only the children)
|
||||||
|
fn clear(self: *Simulator) !void {
|
||||||
|
self.highlighted_entity = null;
|
||||||
|
self.road_man.clear(self.allocator);
|
||||||
|
try self.node_man.clear(self.allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the variable that tracks the highlighted entity
|
||||||
|
fn updateHighlightedEntity(self: *Simulator, pos: rl.Vector2) void {
|
||||||
|
if (self.node_man.getHighlightedNode(pos)) |node| {
|
||||||
|
self.highlighted_entity = .{ .node = node };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.road_man.getHighlightedRoad(pos)) |road| {
|
||||||
|
self.highlighted_entity = .{ .road = road };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.highlighted_entity = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns array of IntersectionData struct, containing pointers to roads that got intersected and exact position
|
||||||
|
fn getIntersectingRoads(self: *const 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;
|
||||||
|
|
||||||
|
var start_node_collision: ?*Road = null;
|
||||||
|
var end_node_collision: ?*Road = null;
|
||||||
|
|
||||||
|
// Here we will check if any road collides with start and end node
|
||||||
|
for (self.road_man.roads.items) |road| {
|
||||||
|
if (start_node_collision == null and road.collides(start.pos) and !start.roadsContains(road))
|
||||||
|
start_node_collision = road;
|
||||||
|
if (end_node_collision == null and road.collides(end.pos) and !end.roadsContains(road))
|
||||||
|
end_node_collision = road;
|
||||||
|
|
||||||
|
if (start_node_collision != null and end_node_collision != null) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if road node is placed on the road it is added as a collision with said road
|
||||||
|
if (start_node_collision) |road| {
|
||||||
|
try intersections.append(self.allocator, .{
|
||||||
|
.road = road,
|
||||||
|
.pos = start.pos,
|
||||||
|
.origin = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
outer: for (self.road_man.roads.items) |road| {
|
||||||
|
if (!rl.checkCollisionLines(
|
||||||
|
start.pos,end.pos,
|
||||||
|
road.nodes[0].pos, road.nodes[1].pos,
|
||||||
|
&collision_point))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const intersection = st.IntersectionData {
|
||||||
|
.road = road,
|
||||||
|
.pos = collision_point,
|
||||||
|
.origin = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
// We put a 0 here, just to satisfy the constructor function,
|
||||||
|
// it is not getting appended to the node list anyways
|
||||||
|
const node: Node = .init(0, intersection.pos);
|
||||||
|
// If the newly acquired intersection node is within the snapping radius of already existing nodes,
|
||||||
|
// we don't add it to the list
|
||||||
|
for (intersections.items) |inter_collision| {
|
||||||
|
if (node.withinSnapRadius(inter_collision.pos)) continue :outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is an existing node that covers our position within its snapping radius,
|
||||||
|
// then such position will not be saved as intersection
|
||||||
|
if (self.node_man.getNodeIfExists(node.pos) != null) continue;
|
||||||
|
|
||||||
|
try intersections.append(allocator, intersection);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if end node is placed on the road it is added as a collision with said road
|
||||||
|
if (end_node_collision) |road| {
|
||||||
|
try intersections.append(self.allocator, .{
|
||||||
|
.road = road,
|
||||||
|
.pos = end.pos,
|
||||||
|
.origin = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const sorted_intersection = try intersections.toOwnedSlice(allocator);
|
||||||
|
std.sort.block(st.IntersectionData, sorted_intersection, start, ut.compareIntersections);
|
||||||
|
|
||||||
|
return sorted_intersection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes the data about intersections and adds new nodes there alongside with linking existing roads to them
|
||||||
|
///
|
||||||
|
/// Important: This function assumes the intersection array is sorted by distance from the start node (ascending)
|
||||||
|
fn splitRoadsByIntersections(self: *Simulator, intersections: []st.IntersectionData, start: *Node, end: *Node) void {
|
||||||
|
if (intersections.len == 0) {
|
||||||
|
self.road_man.addRoad(self.allocator, start, end) catch |err| {
|
||||||
|
std.debug.panic("Failed creating the road out of origin nodes: {}\n", .{err});
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const first_node = self.node_man.getSelectedNode(self.allocator, intersections[0].pos) catch |err| {
|
||||||
|
std.debug.panic("Failed to add the first node of the intersection: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
var override_node: ?*Node = null;
|
||||||
|
// This if statement essentially checks that IF we only have one intersection and that one is one of the origin nodes,
|
||||||
|
// it means that we have to enable one of start => intersection, or, end => intersection road building logic
|
||||||
|
//
|
||||||
|
// However due to the possibility that we link the road to itself (intersection[0] is start that we then connect
|
||||||
|
// that one to start node; so intersection[0] => start = start => start),
|
||||||
|
// we have to essentially realise which node is that first intersection and essentially store that info and only
|
||||||
|
// let the opposite node form a road with the intersection
|
||||||
|
// and that is what override_node, override_start and override_end variables are all about
|
||||||
|
if (intersections.len == 1 and intersections[0].origin) {
|
||||||
|
override_node = if (first_node == start) end else start;
|
||||||
|
}
|
||||||
|
|
||||||
|
const override_start = override_node != null and override_node.? == start;
|
||||||
|
if (!intersections[0].origin or override_start) {
|
||||||
|
// Here we connect the start node with the first intersection node (via road)
|
||||||
|
self.road_man.addRoad(self.allocator, start, first_node) catch |err| {
|
||||||
|
std.debug.panic("Failed to add a road of origin (start) node and the first intersection node: {}\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(self.allocator, intersection.pos) catch |err| {
|
||||||
|
std.debug.panic("Failed to create a node based on the intersection index {d}: {}\n", .{
|
||||||
|
i,
|
||||||
|
err
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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 old_node_of_road = 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,
|
||||||
|
// as is the end node's road reference
|
||||||
|
|
||||||
|
// So the intersected road loses old node (at the far end) and gets new node that intersects it
|
||||||
|
intersection.road.updateNodeReference(old_node_of_road, new_node) catch |err| {
|
||||||
|
std.debug.panic("Failed to update the road's node references: {}\n", .{err});
|
||||||
|
};
|
||||||
|
// Now the old node must not point at the intersection road
|
||||||
|
old_node_of_road.unreferenceRoad(intersection.road) catch |err| {
|
||||||
|
std.debug.panic("Failed to unreference the intersection road from the old node: {}\n", .{err});
|
||||||
|
};
|
||||||
|
new_node.referenceRoad(self.allocator, intersection.road) catch |err| {
|
||||||
|
std.debug.panic("Failed to reference the intersection road to the intersecting node: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Now we add the road (to the road list) and references the road at both bounding nodes
|
||||||
|
self.road_man.addRoad(self.allocator, new_node, old_node_of_road) catch |err| {
|
||||||
|
std.debug.panic("Failed to create a road of new node and former node of prior intersecting road: {}\n", .{
|
||||||
|
err
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Here we work on creating new roads between intersection nodes and as such because we need nodes
|
||||||
|
// at 2 different intersections, it means we have to be sure next one exists
|
||||||
|
if (i == intersections.len - 1) continue;
|
||||||
|
|
||||||
|
const next_intersection = self.node_man.getSelectedNode(self.allocator, intersections[i+1].pos) catch |err| {
|
||||||
|
std.debug.panic("Failed to create node of next intersection (current index={d}: {}\n", .{i, err});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Creating the road connecting current intersection with the next one
|
||||||
|
self.road_man.addRoad(self.allocator, new_node, next_intersection) catch |err| {
|
||||||
|
std.debug.panic("Failed to create the road of current and next intersection nodes: {}\n", .{err});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const override_end = override_node != null and override_node.? == end;
|
||||||
|
|
||||||
|
// Finally we create final road by connecting last intersection node to the end origin node
|
||||||
|
const final_intersection = intersections[intersections.len - 1];
|
||||||
|
const final_intersection_node = self.node_man.getSelectedNode(self.allocator, final_intersection.pos) catch |err| {
|
||||||
|
std.debug.panic("Failed to create node based on last intersection position: {}\n", .{err});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (final_intersection.origin and !override_end) return;
|
||||||
|
self.road_man.addRoad(self.allocator, final_intersection_node, end) catch |err| {
|
||||||
|
std.debug.panic("Failed to create a road of final intersection and end origin node: {}\n", .{err});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
6
src/test.zig
Normal file
6
src/test.zig
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
// Main test file that imports all files that have testing blocks in them
|
||||||
|
test {
|
||||||
|
_ = @import("infrastructure/road.zig");
|
||||||
|
_ = @import("infrastructure/node_manager.zig");
|
||||||
|
_ = @import("infrastructure/road_manager.zig");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user