Fully implemented road splitting

This commit is contained in:
2026-04-24 23:43:02 +02:00
parent b9b7f610ac
commit efa14bea7d
2 changed files with 85 additions and 15 deletions

View File

@@ -4,11 +4,16 @@ import "../common"
import rl "vendor:raylib"
Node :: struct {
// Whether the node is reachable
enabled: bool,
// This node's position
pos: rl.Vector2,
// All of the roads that are connected to the node itself;
// Stores the index of the Road object that is stored within Simulator struct in roads dynamic array
roads: [dynamic]u32,
}
// Node Initialisation
node_init :: proc(new_pos: rl.Vector2) -> Node {
return {
enabled = true,
@@ -17,6 +22,7 @@ node_init :: proc(new_pos: rl.Vector2) -> Node {
}
}
// Returns whether passed pos(ition) is within this node's snapping radius
node_within_snapping_radius :: proc(self: ^Node, pos: rl.Vector2) -> bool {
return rl.CheckCollisionPointCircle(
pos,
@@ -24,3 +30,16 @@ node_within_snapping_radius :: proc(self: ^Node, pos: rl.Vector2) -> bool {
common.NODE_SNAP_RADIUS * common.NODE_RADIUS,
)
}
// Tries to remove the road reference from the node;
// Returns false if failed
node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool {
for i in 0..<len(self.roads) {
if self.roads[i] != road_to_unref do continue
unordered_remove(&self.roads, i)
return true
}
return false
}

View File

@@ -3,6 +3,7 @@ package main
import rl "vendor:raylib"
import "common"
import "core:fmt"
import inf "infrastructure"
Simulator :: struct {
@@ -54,13 +55,11 @@ handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
@(private="file")
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
cur_node_index := get_or_create_node(self, pos)
cur_node_index := get_node_or_new(self, pos)
if val, ok := self.temp_node_index.?; ok {
// TODO remove
// replace by splitting functionality
road := inf.road_init(val, cur_node_index)
append(&self.roads, road)
if temp, ok := self.temp_node_index.?; ok {
data := get_intersecting_roads(self, temp, cur_node_index)
split_roads_by_points(self, data, temp, cur_node_index)
self.temp_node_index = self.auto_continue ? cur_node_index : nil
return
@@ -96,7 +95,7 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
// This function only returns the index to the node or if it doesn't exist bool in the tuple is false
@(private="file")
get_selected_node :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
for &node, index in self.nodes {
if inf.node_within_snapping_radius(&node, pos) do return u32(index), true
}
@@ -107,8 +106,8 @@ get_selected_node :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
// 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="file")
get_or_create_node :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 {
if node, ok := get_selected_node(self, pos); ok do return node
get_node_or_new :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 {
if node, ok := get_node_index_if_exists(self, pos); ok do return node
node := inf.node_init(pos)
append(&self.nodes, node)
@@ -141,7 +140,7 @@ get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []comm
}
// Here we check if our new intersected point node is too close to already established nodes
if _, ok := get_selected_node(self, data.point); ok do continue
if _, ok := get_node_index_if_exists(self, data.point); ok do continue
append(&intersections, data)
}
@@ -157,17 +156,69 @@ split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersec
return
}
first_intersection_node := get_or_create_node(self, intersections[0].point)
first_intersection_node := get_node_or_new(self, intersections[0].point)
road := inf.road_init(start, first_intersection_node)
append(&self.roads, road)
for intersection in intersections {
// TODO
for i in 0..<len(intersections) {
intersection := intersections[i]
// The node created at the point of intersection
new_node := get_node_or_new(self, intersection.point)
// Pointer to the node that borders the road that was intersected
// This node and the new node will become nodes for the new road being created
road_old_node := self.roads[intersection.road].nodes[1]
// The old road that was intersected now borders the new node
// and the old node is removed from the road's end node reference,
// as is the end node's road reference
ok := update_node_reference(self, intersection.road, road_old_node, new_node)
if !ok do fmt.panicf("Failed to update the node reference to the Road ID=%d, because I couldn't find old reference ID=%d\n",
intersection.road, road_old_node)
// This adds the road (to the road manager) and also references the road at both nodes (pointers)
add_road(self, new_node, road_old_node)
if (i == len(intersections) - 1) do continue
node_start := get_node_or_new(self, intersection.point)
node_end := get_node_or_new(self, intersections[i + 1].point)
add_road(self, node_start, node_end)
}
last_intersection_road := intersections[len(intersections) - 1]
last_intersection_node := get_or_create_node(self, last_intersection_road.point)
last_intersection_node := get_node_or_new(self, last_intersection_road.point)
road = inf.road_init(last_intersection_node, end)
append(&self.roads, road)
add_road(self, last_intersection_node, end)
}
// Adds a new road into roads array, start and end are indexes of existing nodes
@(private="file")
add_road :: proc(self: ^Simulator, start: u32, end: u32) {
road := inf.road_init(start, end)
append(&self.roads, road)
road_index := u32(len(self.roads) - 1)
append(&self.nodes[start].roads, road_index)
append(&self.nodes[end].roads, road_index)
}
// Attempts to update node reference to the road;
// Returns false if the old reference doesn't exist
@(private="file")
update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u32, new_ref: u32) -> bool {
road := &self.roads[road_to_update]
for i in 0..<len(road.nodes) {
if road.nodes[i] != old_ref do continue
road.nodes[i] = new_ref
inf.node_unreference_road(&self.nodes[old_ref], road_to_update)
append(&self.nodes[new_ref].roads, road_to_update)
return true
}
return false
}