Max
Max

Reputation: 23

How to let my turtles move while checking for other turtles?

I want to let move my turtles forward, if there are no other turtles on patch ahead 1 with the same heading. the turtles slow down at some point until they don't move anymore and there are no turtles in front of them, but I don't know why.

Here is some code I have:

to movefd
  
ask turtles [
    let car-ahead turtles-on patch-ahead 1 
    ifelse car-ahead with [heading = [heading] of myself] != nobody
    [ slow-down-car ]
    [ speed-up-car ]
  
    if speed < speed-min [ set speed speed-min]
    if speed > speed-limit [ set speed speed-limit ]
    fd speed
  ]
end

to slow-down-car
  set speed (speed - deceleration)
end

to speed-up-car
  set speed speed + acceleration
end

Upvotes: 1

Views: 48

Answers (1)

JenB
JenB

Reputation: 17678

I think (but am not sure as I can't test) that your problem is coming from the difference between agentsets and agents. The report turtles-on returns a turtleset, which can have any number of turtles. Even if it returns exactly one turtle, it returns that as a set of one turtle rather than as a turtle. On the other hand, nobody is a turtle, not a turtleset. A set can never be the same as a turtle.

Try this (note, I also changed 'car' to 'cars' as a reminder that it's a set):

ask turtles [
    let cars-ahead turtles-on patch-ahead 1 
    ifelse any? cars-ahead with [heading = [heading] of myself]
    [ slow-down-car ]
    [ speed-up-car ]
  
    ...
  ]
end

Upvotes: 1

Related Questions