1 Commits

12 changed files with 129 additions and 75 deletions

View File

@@ -132,6 +132,7 @@ pub fn build(b: *std.Build) void {
} }
const tests = b.addTest(.{ const tests = b.addTest(.{
.use_llvm = true,
.root_module = b.createModule(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/test.zig"), .root_source_file = b.path("src/test.zig"),
.target = target, .target = target,

View File

@@ -31,3 +31,5 @@ pub const TEXT_SIZE = 50;
pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2; pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2;
/// Text colour in which entity IDs are displayed if toggled /// Text colour in which entity IDs are displayed if toggled
pub const ENTITY_DATA_TEXT_COLOUR = clr.orange; pub const ENTITY_DATA_TEXT_COLOUR = clr.orange;
pub const CAR_SIZE = 20;

View File

@@ -2,7 +2,7 @@ const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("../common/constants.zig"); const c = @import("../common/constants.zig");
const e = @import("../errors.zig"); const e = @import("../common/errors.zig");
const st = @import("../common/structures.zig"); const st = @import("../common/structures.zig");
const ut = @import("../common/utils.zig"); const ut = @import("../common/utils.zig");
const Road = @import("road.zig").Road; const Road = @import("road.zig").Road;

View File

@@ -2,7 +2,7 @@ const std = @import("std");
const Vector2 = @import("raylib").Vector2; const Vector2 = @import("raylib").Vector2;
const c = @import("../common/constants.zig"); const c = @import("../common/constants.zig");
const e = @import("../errors.zig"); const e = @import("../common/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;
@@ -79,10 +79,8 @@ pub const NodeManager = struct {
/// Gets next id, resets only on clear() /// Gets next id, resets only on clear()
fn getNextID(self: *NodeManager) usize { fn getNextID(self: *NodeManager) usize {
const id = self.next_id; defer self.next_id += 1;
self.next_id += 1; return self.next_id;
return id;
} }
/// Clears all existing nodes connected, not deinitialisation /// Clears all existing nodes connected, not deinitialisation
@@ -143,7 +141,9 @@ test "id tracking" {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var node_man: NodeManager = .init(); var node_man: NodeManager = .init();
defer node_man.deinit(allocator); defer node_man.deinit(allocator) catch |err| {
std.debug.panic("Failed to deinit nodemanager: {}\n", .{err});
};
const n = 5; const n = 5;
for (0..n) |_| { for (0..n) |_| {

View File

@@ -1,7 +1,7 @@
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("../common/constants.zig"); const c = @import("../common/constants.zig");
const e = @import("../errors.zig"); const e = @import("../common/errors.zig");
const st = @import("../common/structures.zig"); const st = @import("../common/structures.zig");
const ut = @import("../common/utils.zig"); const ut = @import("../common/utils.zig");
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
@@ -100,30 +100,7 @@ const std = @import("std");
const expect = std.testing.expect; const expect = std.testing.expect;
test "valid road nodes" { test "valid road nodes" {
var gpa: std.heap.DebugAllocator(.{}) = .init; // TODO rewrite
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 // TODO tests

View File

@@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const e = @import("../errors.zig"); const e = @import("../common/errors.zig");
const st = @import("../common/structures.zig"); const st = @import("../common/structures.zig");
const ut = @import("../common/utils.zig"); const ut = @import("../common/utils.zig");
const Road = @import("road.zig").Road; const Road = @import("road.zig").Road;
@@ -48,10 +48,8 @@ pub const RoadManager = struct {
/// Returns the id, and increases it by one; used for generating ID's for new entities /// 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; defer self.next_id += 1;
self.next_id += 1; return self.next_id;
return id;
} }
/// Deinits all the roads, clears them but not deiniting the list itself; also resets the next ID var /// Deinits all the roads, clears them but not deiniting the list itself; also resets the next ID var
@@ -99,36 +97,7 @@ const Vector2 = @import("raylib").Vector2;
const expect = std.testing.expect; const expect = std.testing.expect;
test "id tracking" { test "id tracking" {
var gpa: std.heap.DebugAllocator(.{}) = .init; // TODO rewrite
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 // TODO tests

View File

@@ -18,7 +18,9 @@ pub fn main(init: std.process.Init) !void {
rl.setWindowMonitor(monitor); rl.setWindowMonitor(monitor);
rl.setTargetFPS(rl.getMonitorRefreshRate(monitor)); rl.setTargetFPS(rl.getMonitorRefreshRate(monitor));
var sim: Simulator = .init(allocator); const rand_impl: std.Random.IoSource = .{ .io = init.io };
var sim: Simulator = .init(allocator, rand_impl);
defer sim.deinit() catch |err| { defer sim.deinit() catch |err| {
std.debug.panic("Failed to deinitialise the sim: {}\n", .{err}); std.debug.panic("Failed to deinitialise the sim: {}\n", .{err});
}; };
@@ -34,3 +36,14 @@ pub fn main(init: std.process.Init) !void {
sim.draw(pos); sim.draw(pos);
} }
} }
test "test rng" {
const io = std.testing.io;
const rng_impl: std.Random.IoSource = .{ .io = io };
const rng = rng_impl.interface();
const n_max = 1000;
const n_rng = rng.uintLessThan(usize, n_max);
std.debug.print("{d}\n", .{n_rng});
}

View File

@@ -6,8 +6,10 @@ const st = @import("common/structures.zig");
const ut = @import("common/utils.zig"); const ut = @import("common/utils.zig");
const Road = @import("infrastructure/road.zig").Road; const Road = @import("infrastructure/road.zig").Road;
const Node = @import("infrastructure/node.zig").Node; const Node = @import("infrastructure/node.zig").Node;
const Car = @import("vehicles/car.zig").Car;
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;
const CarManager = @import("vehicles/car_manager.zig").CarManager;
pub const Simulator = struct { pub const Simulator = struct {
/// allocator for convenience /// allocator for convenience
@@ -16,6 +18,8 @@ pub const Simulator = struct {
node_man: NodeManager, node_man: NodeManager,
/// 'class' tracking all the roads (and appropriate functions) /// 'class' tracking all the roads (and appropriate functions)
road_man: RoadManager, road_man: RoadManager,
/// 'class' tracking all the cars (and appropriate functions)
car_man: CarManager,
// vars // vars
/// Tracks whether next road will start building from the node the last road was built at /// Tracks whether next road will start building from the node the last road was built at
auto_continue: bool, auto_continue: bool,
@@ -31,24 +35,30 @@ pub const Simulator = struct {
show_connections: bool, show_connections: bool,
/// Toggle that tracks whether ID (or possibly something more in the future) of every entity is displayed in GUI /// Toggle that tracks whether ID (or possibly something more in the future) of every entity is displayed in GUI
display_entity_info: bool, display_entity_info: bool,
/// Entity (car/road/node) that is highlighed (hovered over by a mouse)
highlighted_entity: ?st.Entity, highlighted_entity: ?st.Entity,
/// Interface for RNG
random: std.Random,
/// Constructor for convenience /// Constructor for convenience
pub fn init(new_allocator: std.mem.Allocator) Simulator { pub fn init(new_allocator: std.mem.Allocator, rand_impl: std.Random.IoSource) Simulator {
return .{ return .{
.allocator = new_allocator, .allocator = new_allocator,
.node_man = .init(), .node_man = .init(),
.road_man = .init(), .road_man = .init(),
.car_man = .init(),
.auto_continue = false, .auto_continue = false,
.delete_mode = false, .delete_mode = false,
.show_connections = false, .show_connections = false,
.display_entity_info = false, .display_entity_info = false,
.highlighted_entity = null, .highlighted_entity = null,
.random = rand_impl.interface(),
}; };
} }
/// Deinitialisation of node and road objects /// Deinitialisation of node and road objects
pub fn deinit(self: *Simulator) !void { pub fn deinit(self: *Simulator) !void {
self.car_man.deinit(self.allocator);
self.road_man.deinit(self.allocator); self.road_man.deinit(self.allocator);
try self.node_man.deinit(self.allocator); try self.node_man.deinit(self.allocator);
} }
@@ -66,6 +76,7 @@ pub const Simulator = struct {
self.road_man.draw(highlighted_road, self.display_entity_info); self.road_man.draw(highlighted_road, self.display_entity_info);
self.node_man.draw(pos, self.display_entity_info); self.node_man.draw(pos, self.display_entity_info);
self.car_man.draw();
self.drawRelatedSelectedEntities(); self.drawRelatedSelectedEntities();
} }
@@ -92,6 +103,7 @@ pub const Simulator = struct {
road.nodes[0].draw(c.NODE_RELATED_COLOUR, 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); road.nodes[1].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
}, },
// TODO car
} }
} }
@@ -112,8 +124,8 @@ pub const Simulator = struct {
self.delete_mode = rl.isKeyDown(.left_shift); self.delete_mode = rl.isKeyDown(.left_shift);
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode; self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
if (rl.isKeyReleased(.n)) self.createCar();
if (rl.isKeyReleased(.tab)) self.display_entity_info = !self.display_entity_info; if (rl.isKeyReleased(.tab)) self.display_entity_info = !self.display_entity_info;
if (rl.isKeyReleased(.c)) self.clear() catch |err| { if (rl.isKeyReleased(.c)) self.clear() catch |err| {
std.debug.panic("Failed to clear the entities: {}\n", .{err}); std.debug.panic("Failed to clear the entities: {}\n", .{err});
}; };
@@ -132,16 +144,16 @@ pub const Simulator = struct {
/// Function that handles functionality that executes upon left click /// 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) { if (self.delete_mode and self.highlighted_entity != null and self.highlighted_entity.? == .road) {
self.delete_road() catch |err| { self.deleteRoad() catch |err| {
std.debug.panic("Failed to delete the road: {}\n", .{err}); std.debug.panic("Failed to delete the road: {}\n", .{err});
}; };
return; return;
} }
self.new_road(pos); self.createRoad(pos);
} }
/// User initiated road building functionality /// User initiated road building functionality
fn new_road(self: *Simulator, pos: rl.Vector2) void { fn createRoad(self: *Simulator, pos: rl.Vector2) void {
if (self.show_connections) return; 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", .{
@@ -167,7 +179,7 @@ pub const Simulator = struct {
} }
/// User initiated road destroying functionality /// User initiated road destroying functionality
fn delete_road(self: *Simulator) !void { fn deleteRoad(self: *Simulator) !void {
// We can trust this because this only gets called if valid and if type is road // 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); std.debug.assert(self.highlighted_entity != null and self.highlighted_entity.? == .road);
const h_road = self.highlighted_entity.?.road; const h_road = self.highlighted_entity.?.road;
@@ -380,4 +392,17 @@ pub const Simulator = struct {
std.debug.panic("Failed to create a road of final intersection and end origin node: {}\n", .{err}); std.debug.panic("Failed to create a road of final intersection and end origin node: {}\n", .{err});
}; };
} }
/// Creates a car and calls
fn createCar(self: *Simulator) void {
const nodes_len = self.node_man.nodes.items.len;
if (nodes_len == 0) return;
// Grab random node
const i = self.random.uintLessThan(usize, nodes_len);
const ref_node = self.node_man.nodes.items[i];
self.car_man.addCar(self.allocator, ref_node) catch |err| {
std.debug.panic("Unable to create a car or append it to the list of cars: {}\n", .{err});
};
}
}; };

View File

@@ -3,4 +3,5 @@ test {
_ = @import("infrastructure/road.zig"); _ = @import("infrastructure/road.zig");
_ = @import("infrastructure/node_manager.zig"); _ = @import("infrastructure/node_manager.zig");
_ = @import("infrastructure/road_manager.zig"); _ = @import("infrastructure/road_manager.zig");
_ = @import("main.zig");
} }

23
src/vehicles/car.zig Normal file
View File

@@ -0,0 +1,23 @@
const std = @import("std");
const rl = @import("raylib");
const Node = @import("../infrastructure/node.zig").Node;
pub const Car = struct {
id: usize,
pos: rl.Vector2,
pub fn init(new_id: usize, spawn_node: *const Node) Car {
return .{
.id = new_id,
.pos = spawn_node.pos,
};
}
pub fn deinit(self: *Car, allocator: std.mem.Allocator) void {
allocator.destroy(self);
}
pub fn draw(self: *const Car) void {
rl.drawEllipseV(self.pos, 10, 20, .blue);
}
};

View File

@@ -0,0 +1,43 @@
const std = @import("std");
const Vector2 = @import("raylib").Vector2;
const Node = @import("../infrastructure/node.zig").Node;
const Car = @import("car.zig").Car;
pub const CarManager = struct {
next_id: usize,
cars: std.ArrayList(*Car),
pub fn init() CarManager {
return .{
.next_id = 0,
.cars = .empty,
};
}
pub fn deinit(self: *CarManager, allocator: std.mem.Allocator) void {
for (self.cars.items) |car| {
car.deinit(allocator);
}
self.cars.deinit(allocator);
}
pub fn draw(self: *const CarManager) void {
for (self.cars.items) |car| {
car.draw();
}
}
pub fn getNextID(self: *CarManager) usize {
defer self.next_id += 1;
return self.next_id;
}
pub fn addCar(self: *CarManager, allocator: std.mem.Allocator, node_ref: *const Node) !void {
const car_ptr = try allocator.create(Car);
car_ptr.* = .init(self.getNextID(), node_ref);
try self.cars.append(allocator, car_ptr);
}
};