Fixed splitting quirks when only one intersection is found and the intersection itself is origin node

This commit is contained in:
2026-05-01 20:46:09 +02:00
parent f7a1340500
commit fdf672de4b
2 changed files with 42 additions and 7 deletions

View File

@@ -239,6 +239,7 @@ pub const Simulator = struct {
try intersections.append(self.allocator, .{
.road = road,
.pos = start.pos,
.origin = true,
});
}
@@ -252,6 +253,7 @@ pub const Simulator = struct {
const intersection = st.IntersectionData {
.road = road,
.pos = collision_point,
.origin = false,
};
// We put a 0 here, just to satisfy the constructor function,
@@ -275,6 +277,7 @@ pub const Simulator = struct {
try intersections.append(self.allocator, .{
.road = road,
.pos = end.pos,
.origin = true,
});
}
@@ -295,13 +298,30 @@ pub const Simulator = struct {
return;
}
// Here we connect the start node with the first intersection node (via road)
const first_node = self.node_man.getSelectedNode(self.allocator, intersections[0].pos) catch |err| {
std.debug.panic("Failed to add the first node of the intersection: {}\n", .{err});
};
self.road_man.addRoad(self.allocator, start, first_node) catch |err| {
std.debug.panic("Failed to add a road of origin (start) node and the first intersection node: {}\n", .{err});
};
var override_node: ?*Node = null;
// This if statement essentially checks that IF we only have one intersection and that one is one of the origin nodes,
// it means that we have to enable one of start => intersection, or, end => intersection road building logic
//
// However due to the possibility that we link the road to itself (intersection[0] is start that we then connect
// that one to start node; so intersection[0] => start = start => start),
// we have to essentially realise which node is that first intersection and essentially store that info and only
// let the opposite node form a road with the intersection
// and that is what override_node, override_start and override_end variables are all about
if (intersections.len == 1 and intersections[0].origin) {
override_node = if (first_node == start) end else start;
}
const override_start = override_node != null and override_node.? == start;
if (!intersections[0].origin or override_start) {
// Here we connect the start node with the first intersection node (via road)
self.road_man.addRoad(self.allocator, start, first_node) catch |err| {
std.debug.panic("Failed to add a road of origin (start) node and the first intersection node: {}\n", .{err});
};
}
for (0..intersections.len) |i| {
const intersection = intersections[i];
@@ -355,12 +375,15 @@ pub const Simulator = struct {
};
}
const override_end = override_node != null and override_node.? == end;
// Finally we create final road by connecting last intersection node to the end origin node
const final_intersection_pos = intersections[intersections.len - 1].pos;
const final_intersection_node = self.node_man.getSelectedNode(self.allocator, final_intersection_pos) catch |err| {
const final_intersection = intersections[intersections.len - 1];
const final_intersection_node = self.node_man.getSelectedNode(self.allocator, final_intersection.pos) catch |err| {
std.debug.panic("Failed to create node based on last intersection position: {}\n", .{err});
};
if (final_intersection.origin and !override_end) return;
self.road_man.addRoad(self.allocator, final_intersection_node, end) catch |err| {
std.debug.panic("Failed to create a road of final intersection and end origin node: {}\n", .{err});
};