How do I check if there is an occupied patch in the radius of another patch in NetLogo?
I want my function to make the turtles, occupy the patch the turtle is currently on, by setting the patch variable "occupation" to true. But only if there is enough distance between that patch and an already occupied patch.
turtles-own [food health strength my-net] patches-own[nutrients occupation] ;other stuff omitted
to setup clear-all move set occupation false ;other stuff omitted ] reset-ticks end
to createNet ask turtles [ if patches with [distance myself < 5 and occupation = true] != nobody [ set my-net patch-here set pcolor gray set occupation true ] ] end
I expected the function to first make a turtle occupy a patch and then to do nothing until the turtle moves far enough away so that the distance to an occupied patch is >= 5 again.
Instead the function always makes a turtle occupy and paint a patch, no matter the distance.
Solution – 1
You’re on the right track, you just need to flip it so that instead of checking that all patches nearby are unoccupied, you instead check if any of them are occupied. How to approach this will depend on your need, but here’s one way:
turtles-own [my-net] patches-own[occupation] to setup ; Toy setup ca ask patches [ set occupation false ] crt 10 [ set my-net nobody ] reset-ticks end to go ; Ask all turtles without a net to do things ask turtles with [my-net = nobody] [ rt random 30 - 15 fd 1 try-create-net ] tick end to try-create-net ; First, list any occupied patches let nearby-occupied-patches patches in-radius 5 with [ occupation = True] ; If there *aren't* any, stake a claim! if not any? nearby-occupied-patches [ set my-net patch-here ask my-net [ set occupation true set pcolor gray ] ] end