Reputation: 23
I am working on a simulation of a one lane road merging with a two lane road, all going the same direction. The cars that are not merging (cars1) check for merging cars within a radius of 2 to see if they need to change into the left lane and if they do, the car checks the left lane to see if it is safe.
ask cars1[
if (not any? turtles-at -2 4) and (not any? turtles-at -1 4)
and (not any? turtles-at 0 4) and (not any? turtles-at 1 4)
and (not any? turtles-at 2 4) and (not any? turtles-at 3 4)
and (any? merging-cars in-radius 3)
[ set ycor 2]]
I am trying to make the merging cars stop if it is not safe for the cars1 in the closest lane to change to the left lane.
ask merging-cars[
loop[
if[any? cars1 in-radius 2]
[stop]
]]
That is the code that is not working. I haven't figured out a way to make the merging cars stop if it is unsafe and go when it is safe.
Thanks
Upvotes: 0
Views: 125
Reputation: 1605
To get you on the right direction have a look at the not any? documentation and make a note that you can incorporate the angle of which to check as well as right or left with a command such as :
not any? turtles-on patch-right-and-ahead 60 2
this is saying.... check the angle of 60 degrees to the right 2 patch ahead of this turtle.
Upvotes: 0