Road splitting functionality fully fixed
This commit is contained in:
@@ -12,15 +12,24 @@ pub const Road = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Unreferences self (road) from both nodes
|
||||||
pub fn unreferenceNodes(self: *Road) !void {
|
pub fn unreferenceNodes(self: *Road) !void {
|
||||||
try self.nodes[0].unreferenceRoad(self);
|
try self.nodes[0].unreferenceRoad(self);
|
||||||
try self.nodes[1].unreferenceRoad(self);
|
try self.nodes[1].unreferenceRoad(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn updateNodeReference(self: *Road, old_reference: *const Node, new_ref: *const Node) void {
|
/// Updates reference to the node, returns error if the old reference is invalid
|
||||||
_ = new_ref; // autofix
|
pub fn updateNodeReference(self: *Road, allocator: std.mem.Allocator, old_reference: *Node, new_ref: *Node) !void {
|
||||||
_ = old_reference; // autofix
|
for (0..self.nodes.len) |i| {
|
||||||
_ = self; // autofix
|
if (self.nodes[i].id != old_reference.id) continue;
|
||||||
|
|
||||||
|
self.nodes[i] = new_ref;
|
||||||
|
try old_reference.unreferenceRoad(self);
|
||||||
|
try new_ref.referenceRoad(allocator, self);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error.OldRefNotExists;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -231,10 +231,9 @@ pub const Simulator = struct {
|
|||||||
const road_old_node = intersection.road.nodes[1];
|
const road_old_node = intersection.road.nodes[1];
|
||||||
// The old road that was intersected now borders the new node
|
// The old road that was intersected now borders the new node
|
||||||
// and the old node is removed from the road's end node reference,
|
// and the old node is removed from the road's end node reference,
|
||||||
intersection.road.nodes[1] = new_node;
|
|
||||||
// As is the end node's road reference
|
// As is the end node's road reference
|
||||||
road_old_node.unreferenceRoad(intersection.road) catch |err| {
|
intersection.road.updateNodeReference(self.allocator, road_old_node, new_node) catch |err| {
|
||||||
std.debug.panic("Failed to unreference previous road: {}\n", .{err});
|
std.debug.panic("Updating the old road reference failed: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|
||||||
// This adds the road (to the road manager) and also references the road at both nodes (pointers)
|
// This adds the road (to the road manager) and also references the road at both nodes (pointers)
|
||||||
|
|||||||
Reference in New Issue
Block a user