joehigh1
joehigh1

Reputation: 193

Netlogo code problems, turtles can't find patch-at 0,0

I am trying to create a simulation along the same sort of lines as this video (1:29 - 1:45)

https://www.youtube.com/watch?v=pqBSNAOsMDc

I thought a simple way to achieve an infinite circling procress would be to make the turtles face 0,0, then look for empty patches in-radius 90 (So they are always just looking to the right.

I got the error code..

'No heading is defined from a point (3,-6) to that same point. '

Can someone point me in the right direction with my code please?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


turtles-own [ faction ]

to setup
  clear-all

  ask patches [ set pcolor white ]
  set-patch-size 7
  resize-world min-pxcor max-pxcor min-pycor max-pycor 


  ask patch 0 0
   [ ask patches in-radius ( max-pxcor * .6) with [  random-float 100 < density ]
     [ sprout 1
         [
         set shape "circle"
         assign-factions
         set color faction-color
         set size 1 ] ] ]

   ask turtles-on patch 0 0 [ die ]

   reset-ticks
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report faction-color
   report red + faction * 30
end

to assign-factions
   let angle 360 / factions
   foreach n-values factions [?] [
    ask patch 0 0 [ 
      sprout 1 [
        set heading ? * angle
        ask turtles in-cone max-pxcor angle [ set faction ? + 1 ]
        die ] ] ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go 

  ask turtles
  [ set heading (towards patch-at 0 0)  ; adjusts heading to point to centre  
    let empty-patches neighbors with [not any? turtles-here]
    if any? empty-patches in-radius 90
      [ let target one-of empty-patches
        face target
        move-to target ] 
  ]

end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Upvotes: 5

Views: 1425

Answers (2)

Nicolas Payette
Nicolas Payette

Reputation: 14972

In your go procedure, you are using set heading (towards patch-at 0 0), but patch-at gives you the patch in the position relative to the turtle that's asking. So if you are asking for patch-at 0 0 you always get the patch that the turtle is actually on. And the heading towards yourself is undefined, hence the error that you are getting.

Replacing patch-at 0 0 with patch 0 0 (which works with absolute coordinates) will solve half your problem: It is still possible that the turtle you are asking is already at patch 0 0. In that case, you would get the same error as before.

The solution: replace set heading (towards patch-at 0 0) with face patch 0 0. The face primitive doesn't crash if you ask to face the location you are already at.

Upvotes: 2

Jose M Vidal
Jose M Vidal

Reputation: 9142

This is because in-radius can return the turtle itself. To fix this just change the line

ask patches in-radius .....

to

ask other patches in-radius .....

Upvotes: 1

Related Questions