Compare commits
2 Commits
3e6438418b
...
eca857cda1
| Author | SHA1 | Date | |
|---|---|---|---|
| eca857cda1 | |||
| 439482e5a1 |
@@ -2,16 +2,24 @@ const Vector2 = @import("raylib").Vector2;
|
|||||||
|
|
||||||
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;
|
||||||
|
|
||||||
/// This is a simple equivalent to something like abstract class, so the simulator class has only one variable
|
/// 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
|
/// 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;
|
/// 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)
|
/// 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.)
|
/// TLDR: Can point to either `entity` type (node, road, etc.)
|
||||||
pub const Entity = union(enum) {
|
pub const Entity = union(enum) {
|
||||||
node: *Node,
|
node: *Node,
|
||||||
road: *Road,
|
road: *Road,
|
||||||
|
car: *Car,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Similar to `Entity` but only for infrastructure entities, so road and node only (for now)
|
||||||
|
pub const Infrastructure = union(enum) {
|
||||||
|
node: *const Node,
|
||||||
|
road: *const Road,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents intersection data, mainly used in cases where we draw over existing roads
|
/// Represents intersection data, mainly used in cases where we draw over existing roads
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ pub const Node = struct {
|
|||||||
if (!display_info) return;
|
if (!display_info) return;
|
||||||
|
|
||||||
var buf: [100]u8 = undefined;
|
var buf: [100]u8 = undefined;
|
||||||
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
|
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{ self.id }) catch |err| {
|
||||||
std.debug.panic("Failed to allocate space for ID???: {}\n", .{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_x = @as(i32, @trunc(self.pos.x)) - c.NODE_RADIUS / 2;
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ pub const NodeManager = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes node, returns error if node still has road references
|
/// Deletes node, returns error if node still has road references
|
||||||
pub fn deleteNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
|
pub fn removeNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
|
||||||
for (0..self.nodes.items.len) |i| {
|
for (0..self.nodes.items.len) |i| {
|
||||||
if (self.nodes.items[i] != node_to_delete) continue;
|
if (self.nodes.items[i] != node_to_delete) continue;
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ pub const NodeManager = struct {
|
|||||||
self.temp_node = null;
|
self.temp_node = null;
|
||||||
|
|
||||||
if (node.roads.items.len != 0) return;
|
if (node.roads.items.len != 0) return;
|
||||||
self.deleteNode(allocator, node) catch |err| {
|
self.removeNode(allocator, node) catch |err| {
|
||||||
std.debug.panic("Failed to delete the temporary node: {}\n", .{err});
|
std.debug.panic("Failed to delete the temporary node: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
|
|
||||||
const rand_impl: std.Random.IoSource = .{ .io = init.io };
|
const rand_impl: std.Random.IoSource = .{ .io = init.io };
|
||||||
|
|
||||||
var sim: Simulator = .init(allocator, rand_impl);
|
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});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ pub const Simulator = struct {
|
|||||||
random: std.Random,
|
random: std.Random,
|
||||||
|
|
||||||
/// Constructor for convenience
|
/// Constructor for convenience
|
||||||
pub fn init(new_allocator: std.mem.Allocator, rand_impl: std.Random.IoSource) Simulator {
|
pub fn init(new_allocator: std.mem.Allocator, rand_impl: *const std.Random.IoSource) Simulator {
|
||||||
return .{
|
return .{
|
||||||
.allocator = new_allocator,
|
.allocator = new_allocator,
|
||||||
.node_man = .init(),
|
.node_man = .init(),
|
||||||
@@ -76,7 +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.car_man.draw(self.display_entity_info);
|
||||||
|
|
||||||
self.drawRelatedSelectedEntities();
|
self.drawRelatedSelectedEntities();
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,9 @@ 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
|
.car => {
|
||||||
|
// TODO draw the origin and destination, connected by the pathfinding route
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,20 +189,31 @@ pub const Simulator = struct {
|
|||||||
const start_node = h_road.nodes[0];
|
const start_node = h_road.nodes[0];
|
||||||
const end_node = h_road.nodes[1];
|
const end_node = h_road.nodes[1];
|
||||||
|
|
||||||
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
try self.road_man.deleteRoad(self.allocator, h_road);
|
||||||
std.debug.panic("Road deletion failed: {}\n", .{err});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (start_node.roads.items.len == 0)
|
if (start_node.roads.items.len == 0) {
|
||||||
try self.node_man.deleteNode(self.allocator, start_node);
|
const cars = try self.car_man.getCarsOnInf(self.allocator, .{ .node = start_node });
|
||||||
|
defer self.allocator.free(cars);
|
||||||
|
|
||||||
if (end_node.roads.items.len == 0)
|
// TODO replace with removeMultipleCars
|
||||||
try self.node_man.deleteNode(self.allocator, end_node);
|
for (self.car_man.cars.items) |car| {
|
||||||
|
try self.car_man.removeCar(self.allocator, car);
|
||||||
|
}
|
||||||
|
|
||||||
|
try self.node_man.removeNode(self.allocator, start_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end_node.roads.items.len == 0) {
|
||||||
|
// TODO same as above
|
||||||
|
try self.node_man.removeNode(self.allocator, end_node);
|
||||||
|
// TODO after this is done, do the same with the function that removes roads
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clearing node and road lists without deinitialising them (only the children)
|
/// Clearing node and road lists without deinitialising them (only the children)
|
||||||
fn clear(self: *Simulator) !void {
|
fn clear(self: *Simulator) !void {
|
||||||
self.highlighted_entity = null;
|
self.highlighted_entity = null;
|
||||||
|
self.car_man.clear(self.allocator);
|
||||||
self.road_man.clear(self.allocator);
|
self.road_man.clear(self.allocator);
|
||||||
try self.node_man.clear(self.allocator);
|
try self.node_man.clear(self.allocator);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,24 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
const c = @import("../common/constants.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
|
const ut = @import("../common/utils.zig");
|
||||||
const Node = @import("../infrastructure/node.zig").Node;
|
const Node = @import("../infrastructure/node.zig").Node;
|
||||||
|
|
||||||
pub const Car = struct {
|
pub const Car = struct {
|
||||||
|
/// Car ID, for easier debugging and identification
|
||||||
id: usize,
|
id: usize,
|
||||||
pos: rl.Vector2,
|
/// When car progresses along the road its location is determined via entity id and progress
|
||||||
|
progress: f32,
|
||||||
|
/// Tracks which infrastructure (pointer) the car is currently located at
|
||||||
|
inf: st.Infrastructure,
|
||||||
|
|
||||||
pub fn init(new_id: usize, spawn_node: *const Node) Car {
|
pub fn init(new_id: usize, spawn_node: *Node) Car {
|
||||||
return .{
|
return .{
|
||||||
.id = new_id,
|
.id = new_id,
|
||||||
.pos = spawn_node.pos,
|
.progress = 0,
|
||||||
|
.inf = .{ .node = spawn_node },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +26,30 @@ pub const Car = struct {
|
|||||||
allocator.destroy(self);
|
allocator.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const Car) void {
|
pub fn draw(self: *const Car, display_info: bool) void {
|
||||||
rl.drawEllipseV(self.pos, 10, 20, .blue);
|
var pos: rl.Vector2 = undefined;
|
||||||
|
|
||||||
|
switch (self.inf) {
|
||||||
|
.node => |node| pos = node.pos,
|
||||||
|
.road => |road| {
|
||||||
|
const road_vector = ut.getVectorP1P2(road.nodes[0].pos, road.nodes[1].pos);
|
||||||
|
pos = .{
|
||||||
|
.x = road_vector.x / 2,
|
||||||
|
.y = road_vector.y / 2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.drawEllipseV(pos, 20, 15, .red);
|
||||||
|
|
||||||
|
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 });
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
rl.drawText(entity_info, @trunc(pos.x), @trunc(pos.y), c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Vector2 = @import("raylib").Vector2;
|
const Vector2 = @import("raylib").Vector2;
|
||||||
|
|
||||||
|
const e = @import("../common/errors.zig");
|
||||||
|
const ut = @import("../common/structures.zig");
|
||||||
const Node = @import("../infrastructure/node.zig").Node;
|
const Node = @import("../infrastructure/node.zig").Node;
|
||||||
const Car = @import("car.zig").Car;
|
const Car = @import("car.zig").Car;
|
||||||
|
|
||||||
@@ -23,9 +25,9 @@ pub const CarManager = struct {
|
|||||||
self.cars.deinit(allocator);
|
self.cars.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const CarManager) void {
|
pub fn draw(self: *const CarManager, display_info: bool) void {
|
||||||
for (self.cars.items) |car| {
|
for (self.cars.items) |car| {
|
||||||
car.draw();
|
car.draw(display_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,10 +36,60 @@ pub const CarManager = struct {
|
|||||||
return self.next_id;
|
return self.next_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addCar(self: *CarManager, allocator: std.mem.Allocator, node_ref: *const Node) !void {
|
pub fn addCar(self: *CarManager, allocator: std.mem.Allocator, node_ref: *Node) !void {
|
||||||
const car_ptr = try allocator.create(Car);
|
const car_ptr = try allocator.create(Car);
|
||||||
car_ptr.* = .init(self.getNextID(), node_ref);
|
car_ptr.* = .init(self.getNextID(), node_ref);
|
||||||
|
|
||||||
try self.cars.append(allocator, car_ptr);
|
try self.cars.append(allocator, car_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clear(self: *CarManager, allocator: std.mem.Allocator) void {
|
||||||
|
for (self.cars.items) |car| {
|
||||||
|
car.deinit(allocator);
|
||||||
|
}
|
||||||
|
self.cars.clearRetainingCapacity();
|
||||||
|
self.next_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function returns array of cars which are located on the node/road specified in the `inf` parameter
|
||||||
|
pub fn getCarsOnInf(self: *const CarManager, allocator: std.mem.Allocator, inf: ut.Infrastructure) ![]*Car {
|
||||||
|
var cars: std.ArrayList(*Car) = .empty;
|
||||||
|
|
||||||
|
const active_inf = std.meta.activeTag(inf);
|
||||||
|
|
||||||
|
for (self.cars.items) |car| {
|
||||||
|
if (std.meta.activeTag(car.inf) != active_inf) continue;
|
||||||
|
|
||||||
|
switch (car.inf) {
|
||||||
|
inline else => |a, tag| {
|
||||||
|
const b = @field(inf, @tagName(tag));
|
||||||
|
if (a == b) try cars.append(allocator, car);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cars.toOwnedSlice(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to deinit and remove the (passed) car;
|
||||||
|
/// returns an error if car doesn't exist in the list
|
||||||
|
pub fn removeCar(self: *CarManager, allocator: std.mem.Allocator, car_to_delete: *Car) !void {
|
||||||
|
for (self.cars.items, 0..) |car, index| {
|
||||||
|
if (car != car_to_delete) continue;
|
||||||
|
|
||||||
|
car.deinit(allocator);
|
||||||
|
_ = self.cars.swapRemove(index);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Entity.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO
|
||||||
|
pub fn removeMultipleCars(self: *CarManager, allocator: std.mem.Allocator, cars_to_delete: []*Car) !void {
|
||||||
|
_ = cars_to_delete; // autofix
|
||||||
|
_ = allocator; // autofix
|
||||||
|
_ = self; // autofix
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user