Compare commits

..

3 Commits

6 changed files with 92 additions and 30 deletions

View File

@@ -16,3 +16,15 @@ Entity :: enum {
Road, Road,
Car, Car,
} }
Infrastructure :: enum {
Node,
Road
}
Car_Position :: struct {
// Tracks which infrastructure the vehicle occupies
type: Infrastructure,
// Tracks the reference
ref: u32,
}

View File

@@ -45,7 +45,10 @@ draw_nodes :: proc(self: ^Simulator) {
@(private="file") @(private="file")
draw_cars :: proc(self: ^Simulator) { draw_cars :: proc(self: ^Simulator) {
for &car in self.cars { for &car in self.cars {
pos := self.nodes[car.origin].pos ref := car.pos.ref
// TODO fix in the future
// let's fix it by tracking length of the road and
pos := car.pos.type == .Node ? self.nodes[ref].pos : self.nodes[self.roads[ref].nodes[0]].pos
rect := rl.Rectangle { rect := rl.Rectangle {
x = pos.x, x = pos.x,

View File

@@ -76,6 +76,14 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
switch type { switch type {
case .Node: case .Node:
// get cars that are on that node
for i in 0..<len(self.cars) {
pos := self.cars[i].pos
if pos.type != .Node || pos.ref != entity_index do continue
delete_entity(self, u32(i), .Car)
}
unordered_remove(&self.nodes, entity_index) unordered_remove(&self.nodes, entity_index)
if !swap_made do return {}, false if !swap_made do return {}, false
@@ -83,6 +91,14 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
for &car in self.cars do v.car_update_node_reference(&car, index_change[0], index_change[1]) for &car in self.cars do v.car_update_node_reference(&car, index_change[0], index_change[1])
return index_change, true return index_change, true
case .Road: case .Road:
// get cars that are on that road
for i in 0..<len(self.cars) {
pos := self.cars[i].pos
if pos.type != .Road || pos.ref != entity_index do continue
delete_entity(self, u32(i), .Car)
}
unordered_remove(&self.roads, entity_index) unordered_remove(&self.roads, entity_index)
if !swap_made do return {}, false if !swap_made do return {}, false
@@ -103,15 +119,13 @@ get_free_node :: proc(self: ^Simulator) -> Maybe(u32) {
car_occupied_nodes: [dynamic]u32 car_occupied_nodes: [dynamic]u32
for car in self.cars { for car in self.cars {
node, ok := car.node_pos.? if car.pos.type != .Node do continue
if !ok do continue
if common.list_contains(car_occupied_nodes[:], node) do continue if common.list_contains(car_occupied_nodes[:], car.pos.ref) do continue
append(&car_occupied_nodes, node) append(&car_occupied_nodes, car.pos.ref)
} }
if len(car_occupied_nodes) == len(self.nodes) do return nil if len(car_occupied_nodes) == len(self.nodes) do return nil
for { for {
node := rand.uint32_max(u32(len(self.nodes))) node := rand.uint32_max(u32(len(self.nodes)))

View File

@@ -2,6 +2,7 @@ package main
import rl "vendor:raylib" import rl "vendor:raylib"
import "common"
import inf "infrastructure" import inf "infrastructure"
import v "vehicles" import v "vehicles"
@@ -27,8 +28,12 @@ handle_keyboard_input :: proc(self: ^Simulator) {
if !rl.IsKeyReleased(.N) || len(self.nodes) == 0 do return if !rl.IsKeyReleased(.N) || len(self.nodes) == 0 do return
car := v.car_init(get_free_node(self), self.nodes[:]) if node_id, ok := get_free_node(self).?; ok {
append(&self.cars, car) car := v.car_init(node_id, self.nodes[:])
set_car_route(self, &car)
append(&self.cars, car)
v.car_print_route(u32(len(self.cars)) - 1, &car)
}
} }
// Generally mouse event handler // Generally mouse event handler

View File

@@ -1,7 +1,10 @@
package main package main
import "core:math/rand"
import "common" import "common"
import inf "infrastructure" import inf "infrastructure"
import v "vehicles"
// Returns path to destination node => road => node // Returns path to destination node => road => node
get_path_to_destination :: proc(self: ^Simulator, source: u32, destination: u32) -> []u32 { get_path_to_destination :: proc(self: ^Simulator, source: u32, destination: u32) -> []u32 {
@@ -41,3 +44,24 @@ get_neighbouring_nodes :: proc(self: ^Simulator, node_index: u32) -> []u32 {
return neighbour_nodes[:] return neighbour_nodes[:]
} }
set_car_route :: proc(self: ^Simulator, car: ^v.Car) {
destination_reachable := false
destination: u32
for !destination_reachable {
ignored_nodes: [dynamic]u32
for {
destination = rand.uint32_max(u32(len(self.nodes)))
if car.pos.type != .Node || car.pos.ref != destination do break
}
// TODO this will need be fixed because we have not encountered what to do if car is at the middle of the road
source := car.pos.type == .Node ? car.pos.ref : self.roads[car.pos.ref].nodes[0]
destination_reachable = get_destination_reachable(self, source, destination, &ignored_nodes)
}
car.destination = destination
}

View File

@@ -1,7 +1,8 @@
package vehicles package vehicles
import "core:math/rand" import "core:fmt"
import rl "vendor:raylib" import rl "vendor:raylib"
import sc "core:strconv"
import "../common" import "../common"
import inf "../infrastructure" import inf "../infrastructure"
@@ -14,18 +15,13 @@ Car :: struct {
// Pathfinding // Pathfinding
// Car's origin node // Car's current node/road
origin: u32, pos: common.Car_Position,
// Car's destination node // Car's destination node
destination: Maybe(u32), destination: Maybe(u32),
// Tracks on which node car has been last
// // tracks absolute pos (within canvas)
// if null car is not on node absolute_pos: rl.Vector2,
node_pos: Maybe(u32),
// if null car is not on road
road_pos: Maybe(u32),
// tracks absolute pos
actual_pos: rl.Vector2,
} }
// Constructor // Constructor
@@ -33,21 +29,29 @@ car_init :: proc(spawn_node: u32, nodes: []inf.Node) -> Car {
return { return {
fuel_level = common.FUEL_MAX, fuel_level = common.FUEL_MAX,
max_speed = common.CAR_MAX_SPEED, max_speed = common.CAR_MAX_SPEED,
origin = spawn_node, pos = common.Car_Position {
node_pos = spawn_node, type = .Node,
actual_pos = nodes[spawn_node].pos ref = spawn_node,
},
absolute_pos = nodes[spawn_node].pos
} }
} }
// Sets a (valid) route for the car
//
// Does NOT guarantee the route is reachable (TODO!)
car_set_route :: proc(self: ^Car, nodes_len: u32) {
for self.origin == self.destination do self.destination = rand.uint32_max(nodes_len)
}
// Updates (origin and destination) node reference // Updates (origin and destination) node reference
car_update_node_reference :: proc(self: ^Car, old_ref: u32, new_ref: u32) { car_update_node_reference :: proc(self: ^Car, old_ref: u32, new_ref: u32) {
if self.origin == old_ref do self.origin = new_ref if self.pos.type == .Node && self.pos.ref == old_ref do self.pos.ref = new_ref
if self.destination == old_ref do self.destination = new_ref if self.destination == old_ref do self.destination = new_ref
} }
// Prints car's route
car_print_route :: proc(id: Maybe(u32) = nil, self: ^Car) {
val, ok := self.destination.?
destination := ok ? fmt.aprintf("N%d", val) : "/"
source_type := self.pos.type == .Node ? 'N' : 'R'
car_id, ok_val := id.?
buf: [100]u8
id_str := ok_val ? sc.write_uint(buf[:], u64(car_id), 10) : "N/A"
fmt.printfln("ID=%s Source=%c%d, Destination=%s", id_str, source_type, self.pos.ref, destination)
}