How can I simulate a moving patch? - netlogo

I'm trying to make a Frogger-like game where the patches move around and the turtle can only move onto a safe patch. If I have a few patches that are red, how can I 'move' them around as if they were turtles? I currently have this, but it seems to move more than 1 patch at a time occasionally and as a result, some red patches will be destroyed if there are more than one that is red:
if pcolor = red
[ ask patch-at 0 1
[ set pcolor red]
set pcolor black ]

You could ask the neighbor patch (left or right) to be painted red and the actual patch to be painted back to black or whatever the default color is.
For this you'll want to get the actual patch coordinates. Using patch-at asks the patch at 0,1 relative to the whole world.

Related

How to set patch color in the diagonal Netlogo

Want to draw a river diagonal to the center of the of the world, however ive found that i would have to do a list with all the patches i want to color, is there a faster way to color the diagonal?
Thanks
I need this diagonal, form the middle of the map upwards, Its even tougher to me as its going backwords and doesnt end in the top most middle.
This code:
ask patches with [pxcor = pycor] [
set pcolor green
]
Draws this:
And here's a version that does the diagonal in the upper right corner:
ask patches with [pxcor + pycor = max-pxcor] [
set pcolor green
]

turtles are not moving through the door

Here is my code. I have walls in the colors grey and brown, which turtles should avoid when moving. I have to move my turtles when Eat = 1 toward the door with the color green and enter the room if Eat =0 turtle has to move in black patches only.
I have an issue as turtles are not moving through the door and also when facing the wall they stop there instead of
to go
ask students [
pd
set heading 90
forward 1
if (eat = 1)
[
if (([pcolor] of patch-left-and-ahead 90 1 = green) or ([pcolor] of patch-left-and-ahead 90 2 = green) or
([pcolor] of patch-left-and-ahead 90 3 = green) or ([pcolor] of patch-left-and-ahead 90 4 = green) or
([pcolor] of patch-left-and-ahead 90 5 = green) and pycor <= -10 )
[
set heading towards one-of patches with [pcolor = green]
forward 1
]
if ([pcolor] of patch-ahead 1 = green)
[
set heading 0
forward 3
]
if (pycor >= -9 and [pcolor] of patch-ahead 1 = black)
[
set heading 270
forward 1
]
]
while [ any? patches in-radius 1 with [ pcolor = grey ] or any? patches in-radius 1 with [ pcolor = brown - 2]] [
bk 1
lt (random 180) - 90
fd 1
]
]
tick
end
Your code looks quite chaotic to me so it is hard to give immediate pointers. Instead, let me describe what happens at each part:
let new-patch patch-ahead 2
You define new-patch as the patch in the direction your student is currently facing
if ([pcolor] of patch-here = black ) [
ifelse (eat = 0)
[set heading random 360
forward 1]
[
set heading random 360
forward 1
set heading towards one-of patches with [pcolor = green]
forward 5
]
When a turtle is standing on a black patch, it has two options, depending on whether or not it has any eat. If it has no eat, it faces a random direction and takes a step forward. If it has eat, it also faces a random direction and takes a step forward. After that it chooses a random door and moves 5 steps towards it.
The immediate problems I see with this part are:
What is the purpose of turning in a completely random direction and stepping forward?
Since both the turtles with eat and those without eat have the random walk it, it should not be inside the ifelse blocks.
The random step could make your turtle step onto a grey or brown patch, which I assume you don't want
The students with eat != 1 move towards a random green patch but this green patch they choose can be a different one each tick, causing them to move back and forward between two doors. I suggest either having them pick one door as destination and storing it as a turtle variable, always moving towards that one until they reach it (set my-door one-of patches with [pcolor = green]) or having them move towards the closest door (set heading towards min-one-of (patches with [pcolor = green]) [distance myself])
Here as well, your turtle could walk onto a wall if it is in the direct line towards your door
Onto the next part
if ([pcolor] of new-patch = grey or [pcolor] of new-patch = brown - 2 ) [
right 180
forward 1
]
Here you have the turtle turn around and take a step in the other direction, if the patch that was 2 in front of them at the start was grey or brown.
This part of the code is executed regardless of whether or not the first part was executed, meaning that if the turtle stood on a black patch before, it first chose new-patch as the patch that was 2 in front of it, then it did some random turning and a step forward, and now it checks what color the patch they initially defined as new-patch had, even if they are now looking in a completely opposite direction. This could actually make them turn back towards the patch you are trying to avoid
I'm just wondering, why have new-patch be the patch that is 2 steps ahead and not 1 step ahead?
And finally
if ( pxcor >= -9 and [pcolor] of patch-here = grey or [pcolor] of patch-here = brown - 2)[
left 180
forward 1
]
Here you have a turtle turn around if the patch they are standing on is grey or brown. Not necessarily a bad way of solving them walking into walls but it does not solve all of those instances and furthermove provides problems once you are stuck in those walls
Let's assume you are standing on a grey patch while facing the door. This means that there is generally grey in front and grey behind. Since the student is not standing on black, there is no random walk. The patch 2 ahead is grey so you turn around and take a step. Now you are standing on another grey patch so you turn back and take a step, ending right where you started this tick.
Of course for this to happen you first need to actually end up in the wall. For that we have the forward 5 from earlier to blame. A turtle that is close to the wall and moves 5 steps toward a door can do so through part of a wall. It gets deep enough for both of your 180° turns not to be able to get them out.
Instead of all this turning around, I suggest you add in a local variable called destination let destination patch-ahead 1, let it check whether or not that patch is a wall and only start randomly turning if it is a wall. Then set destination patch-ahead 1 and check again. Only let it move towards destination after all the checking is done.
In general, I suggest you let the turtles put their pen down to more accurately trace their movement ask turtles [pd], reduce the number of turtles and see what happens from step to step
As to your final question of why they don't move through the door: well, in the code there is no command that tells them what to do if the patch they are standing on is green so once they step onto the green they will simply stop. (or if they manage to move through the door, they will turn back towards it (or another door) and with the random walks added in, they will eventually end up either in a wall or on top of the door, both of which make them stop)
Last but certainly not least: start a bit smaller. Use just one or a few turtles, a short wall and a single door. Make sure your movement works on that before scaling up to all those different rooms
I hope this points you in the right direction and will be glad to further elaborate if you have questions

How to make turtles avoid "land" patches while still moving toward predefined target patch?

Im struggling to get turtles to avoid certain patches with a colour green while still pathing towards a target patch.
my netlogo model simulates a countries ocean fishing with boats (turtle) leaving from ports into the sea and returning. Each boat currently sets a target patch in the ocean based on underlying GIS fishing effort data points (patch variables).
Currently, on the way to their target, some boats have to travel out of their semi enclosed ports and if using line of site will travel across land (green patches). I cannot figure out how to get the boats to choose a path which avoids green patches while still pathing towards the same target patch?
Most answers I have found here only accommodate relatively random movement where the heading is changed if a green patch is within a cone X or in front. I am trying to figure out a pathing technique which doesnt involve head towards X until you hit green then go random left or right but this model needs to be able to determine whether heading left or right after hitting green patch (in the cone) is the right way to go. very confused!!
Patches-own
land
vessels-own
home-port ; patch of the home port
target ; target patch for fishing
ask patches ; sets the color of land and sea patches
[ ifelse (land = 1)
[set pcolor green]
[set pcolor blue]
]
to choose-target
set target one-of patches in-radius 60 with [(fishing-
effort > 100) and (fishing-effort < 1010)] ; sets the
fishermen target to one of the patches with a fishing
effort score
end
to movement
ifelse distance target < 1
[stop] ;
[ face target
forward 1
set fuel fuel - 1] ; uses one fuel per movement forwards
if distance target < 1 [ stop]
end
Thanks!

Netlogo: Creating a square area of patches in the center of the grid

I am trying to create a square of brown patches centered at the origin of my grid. Previously, I had a slider on the interface called "sink-patch-radius" that goes from 0 to 20. Then in my code, I created a circular set of patches centered at the origin that were colored brown + 2 and had a radius of "sink-patch-radius", and the surrounding patches were green. Here is the code that worked for that (thank you to JenB for this!):
;;create the 'sink'
let sink-centre patch 0 0
set sink-patches [patches in-radius sink-patch-radius] of sink-centre
ask sink-patches [ set pcolor brown + 2 ]
;; create the 'source'
set source-patches patches with [pcolor != brown + 2]
ask source-patches [ set pcolor green ]
So now, I want that idea to stay the same, but instead of a circular "sink area", I want it to be a square. I know the above code will have to change, maybe even quite a bit, because you cannot use "in-radius" to make a square. I'm thinking of maybe changing the slider to be "sink-patch-length" so that it adjusts the length of the sides of the square. My question would then be: how do I incorporate that into my code, so that I get a square of brown patches centered at the origin? For example, if "sink-patch-length" is set to 20, then I would want a 20x20 square (400 cells) centered at the origin to have a pcolor of brown + 2.
Any help is greatly appreciated! Thank you.
Use the inbuilt coordinate system. So you want (something like) patches with [abs(pxcor) <= sink-patch-length and abs(pycor) <= sink-patch-length]. If you want it centred somewhere other than the middle, you will need to do some fiddling to make the boundaries correct.

Netlogo random patches setup without them touching at all

I am trying to set up random patches that have no corners touching at all, and the color of those patches are different colors of green (to visually represent each patch having a random quality value in my model). My conceptual idea of how to do this is to have the “false” part of my “ifelse” statement to basically tell the program to keep trying to assign the patch a different place if it has another patch touching any part of it, until all patches have a place with no other patches touching it all (including corners). I just have no idea if that is possible or what code could do that. Is there a netlogo primitive that is equivalent to "until" ? Any help or ideas would be appreciated!
Here is a part of my code:
patches-own [ quality ]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask n-of number-of-patches patches
[ ifelse sum [ pcolor ] of neighbors = 0
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10
]
[ set pcolor black ; this is the line I need to change
]
]
end
Have a look at the primitives while and neighbors in the NetLogo dictionary. I am not completely clear on the sequencing you want in your code so I can't provide a full answer. However, you want something like:
while any? neighbors [(however you define the ones you don't want to touch]
[(try again)
]
Note that patches always touch other patches because the world is constructed as a grid of patches. So you presumably want patches with specific conditions (such as high values of quality) to not touch each other.
As JenB says, patches are always touching other patches, so I'm assuming the question is "How can I generate number-of-patches green patches that aren't touching any other green patches?". Then I'd do this:
to setup-patches
while [ count patches with [ shade-of? green pcolor ] < number-of-patches ]
[ ask one-of patches with [ quality = 0 and count neighbors with [ shade-of? green pcolor ] = 0 ]
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10 ] ]
end
Basically, while the number of green patches is less than the desired number of green patches, choose one of the patches that isn't green and doesn't have any green neighbors. Then turn it green.