From d196ac884220763c0f4662462155b11306c4aba4 Mon Sep 17 00:00:00 2001 From: Marto Date: Sat, 25 Apr 2026 19:18:56 +0200 Subject: [PATCH] Partially fixed index based deleting --- .gitignore | 1 + simulator.odin | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 4bab22b..2af98c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ debug_build +base-road-network diff --git a/simulator.odin b/simulator.odin index 7b41cf3..13ff02b 100644 --- a/simulator.odin +++ b/simulator.odin @@ -77,7 +77,10 @@ handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) { @(private="file") left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) { // 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 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) // 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 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) } @@ -324,7 +330,7 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) index_change: [2]u32 // 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 := false + swap_made: bool switch type { case .Node: @@ -342,19 +348,29 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) switch type { case .Node: 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 { - ok := inf.road_update_node_reference(&road, index_change[0], index_change[1]) - if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0]) + road.nodes[0] -= 1 + road.nodes[1] -= 1 } case .Road: 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 { - ok := inf.node_update_road_reference(&node, index_change[0], index_change[1]) - if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0]) + for &road in node.roads { + road -= 1 + } } } } \ No newline at end of file