I am trying to have my turtles "patrol" a certain area of the environment but when I added code to contain them they would walk to the edge of the territory and then stop and the ticks would also stop.
Conversely, is it possible to allow them to leave the territory if their "energy" variable drops below [x]
For the perimeter problem, if I understand your question you could do like this in your patches setup to define a perimeter at the max coordinates :
ask patches with [
pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor ] [
set pcolor red ;; This setup a red perimeter
]
Otherwise you could choose precise coordinates like this (16x16 square exemple):
ask patches with [ pycor >= -16 and pycor >= 16]
[ set pcolor red ]
ask patches with [ pycor <= -16 and pycor <= 16]
[ set pcolor red ]
ask patches with [ pxcor >= -16 and pxcor >= 16]
[ set pcolor red ]
ask patches with [ pxcor <= -16 and pxcor <= 16]
[ set pcolor red ]
Then place this in your patrol step to forbid walking on red patches :
ifelse [pcolor] of patch-ahead 1 = red
[ lt random-float 360 ] ;; See a red patch ahead : turn left randomly
[ fd 1 ] ;; Otherwise, its safe to go foward.