Fixed all deletion bugs

This commit is contained in:
2026-04-25 22:55:21 +02:00
parent d196ac8842
commit b872af06ff

View File

@@ -315,16 +315,18 @@ delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
delete_entity(self, road_to_delete, .Road)
// After the remove we have to replace references
if len(start_node.roads) == 0 do delete_entity(self, road.nodes[0], .Node)
// If the first node that we removed was at the beginning this means the next node reference is now invalid and as such we must reduce it by one
if road.nodes[0] == 0 do road.nodes[1] -= 1
if len(start_node.roads) == 0 {
if result, ok := delete_entity(self, road.nodes[0], .Node); ok && result[0] == road.nodes[1] {
road.nodes[1] = result[1]
}
}
if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node)
}
// Function that allows deleting of any entity within the entity list (nodes, roads, etc.) while ensuring valid references
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) {
// Returns swapped entities if they exist
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) -> ([2]u32, bool) {
mlen: u32
// Stores data about old and new index in case the deleted index is not last, meaning the swap occurs
index_change: [2]u32
@@ -349,28 +351,20 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
case .Node:
unordered_remove(&self.nodes, entity_index)
if swap_made {
for &road in self.roads do inf.road_update_node_reference(&road, index_change[0], index_change[1])
}
if !swap_made do return {}, false
for &road in self.roads do inf.road_update_node_reference(&road, index_change[0], index_change[1])
return index_change, true
if entity_index != 0 do return
for &road in self.roads {
road.nodes[0] -= 1
road.nodes[1] -= 1
}
case .Road:
unordered_remove(&self.roads, entity_index)
if swap_made {
for &node in self.nodes do inf.node_update_road_reference(&node, index_change[0], index_change[1])
}
if !swap_made do return {}, false
if entity_index != 0 do return
for &node in self.nodes do inf.node_update_road_reference(&node, index_change[0], index_change[1])
for &node in self.nodes {
for &road in node.roads {
road -= 1
}
}
return index_change, true
}
return {}, false
}