Max
Max

Reputation: 23

NetLogo-Let turtles only move on a street/specific patches with a certain color?

I'm new to NetLogo and I want to let move my turtles only on a street(patches with pcolor=grey), but I don't know how to do it. I have a street in my model on which the turtles should move. I already looked at the models-library but couldn't find anything helpful.

Here is my code:

extensions [gis]
globals [streets-dataset 
  roads] 
to setup
  clear-all
   gis:load-coordinate-system (word "data/" projection ".prj")
  set streets-dataset gis:load-dataset "data/Test4.shp"
  gis:set-world-envelope (gis:envelope-of streets-dataset)

  ask patches gis:intersecting streets-dataset
  [
    set pcolor grey]
  set roads patches with [pcolor = grey]
  create-turtles 5 [spawn-cars]
  reset-ticks
end

to spawn-cars
  move-to patch -336 226
end

to go
  ask turtles [if any? patches with [pcolor = grey] [set heading towards one-of patches with[pcolor = grey] fd 1]]
end

Upvotes: 1

Views: 758

Answers (2)

Luke C
Luke C

Reputation: 10336

The most useful example in the Model Library for this case is not the most obviously named; have a look at the Look Ahead Example model in the Code Examples section of the Model Library for an example of almost what you're after, with a small twist. Turtles in 'Look Ahead Example' can not move onto the blue patches in their world- you may be able to figure out how to invert the code in that example (relevant section quoted below) to make your turtles stay on your roads etc.

ifelse [pcolor] of patch-ahead 1 = blue
  [ lt random-float 360 ]   ;; We see a blue patch in front of us. Turn a random amount.
  [ fd 1 ]                  ;; Otherwise, it is safe to move forward.

Upvotes: 2

Emily
Emily

Reputation: 87

You could try setting accessible patches which are the grey pcolor patches and then restricting the turtles to only accessible patches.

Upvotes: -1

Related Questions