Replaced all u32 instances with uint, implemented road length,
implemented basic pathing checks/algorithms, implemented entities id display for easier debugging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:math"
|
||||
import rl "vendor:raylib"
|
||||
import "core:math/rand"
|
||||
|
||||
@@ -9,9 +10,9 @@ import v "vehicles"
|
||||
|
||||
// This function only returns the index to the node or if it doesn't exist bool in the tuple is false
|
||||
@private
|
||||
get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
|
||||
get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (uint, bool) {
|
||||
for &node, index in self.nodes {
|
||||
if inf.node_within_snapping_radius(&node, pos) do return u32(index), true
|
||||
if inf.node_within_snapping_radius(&node, pos) do return uint(index), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
@@ -20,19 +21,19 @@ get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, boo
|
||||
// Given position, the function will attempt the return the pointer to the node in near vicinity,
|
||||
// or if unsuccesful manually creating the node based on the position in the list and then returning the pointer to it
|
||||
@private
|
||||
get_node_or_new :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 {
|
||||
get_node_or_new :: proc(self: ^Simulator, pos: rl.Vector2) -> uint {
|
||||
if node, ok := get_node_index_if_exists(self, pos); ok do return node
|
||||
|
||||
node := inf.node_init(pos)
|
||||
append(&self.nodes, node)
|
||||
|
||||
return u32(len(self.nodes) - 1)
|
||||
return uint(len(self.nodes) - 1)
|
||||
}
|
||||
|
||||
// Attempts to update node reference to the road;
|
||||
// Returns false if the old reference doesn't exist
|
||||
@private
|
||||
update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u32, new_ref: u32) -> bool {
|
||||
update_node_reference :: proc(self: ^Simulator, road_to_update: uint, old_ref: uint, new_ref: uint) -> bool {
|
||||
road := &self.roads[road_to_update]
|
||||
|
||||
for i in 0..<len(road.nodes) {
|
||||
@@ -51,21 +52,21 @@ update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u3
|
||||
// Function that allows deleting of any entity within the entity list (nodes, roads, etc.) while ensuring valid references
|
||||
// Returns swapped entities if they exist
|
||||
@private
|
||||
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) -> ([2]u32, bool) {
|
||||
mlen: u32
|
||||
delete_entity :: proc(self: ^Simulator, entity_index: uint, type: common.Entity) -> ([2]uint, bool) {
|
||||
mlen: uint
|
||||
// Stores data about old and new index in case the deleted index is not last, meaning the swap occurs
|
||||
index_change: [2]u32
|
||||
index_change: [2]uint
|
||||
// Tracks whether the removal of node/road will cause a swap in the (dynamic) array
|
||||
// and thus forcing the pre-swapped reference to be updated
|
||||
swap_made: bool
|
||||
|
||||
switch type {
|
||||
case .Node:
|
||||
mlen = u32(len(self.nodes))
|
||||
mlen = uint(len(self.nodes))
|
||||
case .Road:
|
||||
mlen = u32(len(self.roads))
|
||||
mlen = uint(len(self.roads))
|
||||
case .Car:
|
||||
mlen = u32(len(self.cars))
|
||||
mlen = uint(len(self.cars))
|
||||
}
|
||||
|
||||
last := mlen - 1
|
||||
@@ -81,7 +82,7 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
|
||||
pos := self.cars[i].pos
|
||||
if pos.type != .Node || pos.ref != entity_index do continue
|
||||
|
||||
delete_entity(self, u32(i), .Car)
|
||||
delete_entity(self, uint(i), .Car)
|
||||
}
|
||||
|
||||
unordered_remove(&self.nodes, entity_index)
|
||||
@@ -96,7 +97,7 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
|
||||
pos := self.cars[i].pos
|
||||
if pos.type != .Road || pos.ref != entity_index do continue
|
||||
|
||||
delete_entity(self, u32(i), .Car)
|
||||
delete_entity(self, uint(i), .Car)
|
||||
}
|
||||
|
||||
unordered_remove(&self.roads, entity_index)
|
||||
@@ -115,8 +116,8 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
|
||||
}
|
||||
|
||||
// Returns a random node that has no cars on it
|
||||
get_free_node :: proc(self: ^Simulator) -> Maybe(u32) {
|
||||
car_occupied_nodes: [dynamic]u32
|
||||
get_free_node :: proc(self: ^Simulator) -> Maybe(uint) {
|
||||
car_occupied_nodes: [dynamic]uint
|
||||
|
||||
for car in self.cars {
|
||||
if car.pos.type != .Node do continue
|
||||
@@ -127,8 +128,19 @@ get_free_node :: proc(self: ^Simulator) -> Maybe(u32) {
|
||||
|
||||
if len(car_occupied_nodes) == len(self.nodes) do return nil
|
||||
for {
|
||||
node := rand.uint32_max(u32(len(self.nodes)))
|
||||
node := rand.uint_max(uint(len(self.nodes)))
|
||||
|
||||
if !common.list_contains(car_occupied_nodes[:], node) do return node
|
||||
}
|
||||
}
|
||||
|
||||
calculate_road_length :: proc(self: ^Simulator, start: uint, end: uint) -> f32 {
|
||||
start_pos := self.nodes[start].pos
|
||||
end_pos := self.nodes[end].pos
|
||||
|
||||
x_diff := end_pos.x - start_pos.x
|
||||
y_diff := end_pos.y - start_pos.y
|
||||
len := math.sqrt(x_diff * x_diff - y_diff * y_diff)
|
||||
|
||||
return len
|
||||
}
|
||||
Reference in New Issue
Block a user