Partially fixed index based deleting

This commit is contained in:
2026-04-25 19:18:56 +02:00
parent edce3f67a6
commit d196ac8842
2 changed files with 26 additions and 9 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
debug_build debug_build
base-road-network

View File

@@ -77,7 +77,10 @@ handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
@(private="file") @(private="file")
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) { left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
// DELETE // DELETE
if road, ok := self.highlighted_road.?; ok && self.delete_mode do delete_road(self, road) if road, ok := self.highlighted_road.?; ok && self.delete_mode {
delete_road(self, road)
return
}
// BUILD // BUILD
cur_node_index := get_node_or_new(self, pos) cur_node_index := get_node_or_new(self, pos)
@@ -312,8 +315,11 @@ delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
delete_entity(self, road_to_delete, .Road) delete_entity(self, road_to_delete, .Road)
// After the remove we have to replace references // After the remove we have to replace references
// TODO implement in delete_entity to update references if first element is deleted
if len(start_node.roads) == 0 do delete_entity(self, road.nodes[0], .Node) 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(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node) if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node)
} }
@@ -324,7 +330,7 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
index_change: [2]u32 index_change: [2]u32
// Tracks whether the removal of node/road will cause a swap in the (dynamic) array // 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 // and thus forcing the pre-swapped reference to be updated
swap_made := false swap_made: bool
switch type { switch type {
case .Node: case .Node:
@@ -342,19 +348,29 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
switch type { switch type {
case .Node: case .Node:
unordered_remove(&self.nodes, entity_index) unordered_remove(&self.nodes, entity_index)
if !swap_made do return
if swap_made {
for &road in self.roads do inf.road_update_node_reference(&road, index_change[0], index_change[1])
}
if entity_index != 0 do return
for &road in self.roads { for &road in self.roads {
ok := inf.road_update_node_reference(&road, index_change[0], index_change[1]) road.nodes[0] -= 1
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0]) road.nodes[1] -= 1
} }
case .Road: case .Road:
unordered_remove(&self.roads, entity_index) unordered_remove(&self.roads, entity_index)
if !swap_made do return
if swap_made {
for &node in self.nodes do inf.node_update_road_reference(&node, index_change[0], index_change[1])
}
if entity_index != 0 do return
for &node in self.nodes { for &node in self.nodes {
ok := inf.node_update_road_reference(&node, index_change[0], index_change[1]) for &road in node.roads {
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0]) road -= 1
}
} }
} }
} }