# Turtles and Zombies

Model was written in NetLogo 5.3.1
•
Viewed 256 times
•
Downloaded 20 times
•
Run 0 times

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## Comments and Questions

Please start the discussion about this model!
(You'll first need to log in.)

Click to Run Model

globals [deaths maxRadius] turtles-own [energy] patches-own [friction isWater] breed [tortoises tortoise] breed [zombies zombie] to clear clear-all ask patches [ set pcolor green - ( random-float 0.8 )] reset-ticks end to setup clear ;; maxRadius is used in smartMovement set maxRadius 5 set-patch-size 13 setup-patches set deaths 0 ;; make this "5" a controllable variable repeat 5 [ setup2-patches ] repeat 5 [ smoothWater ] setup-turtles reset-ticks stop end ;; Creates some grass and a bit of water to setup-patches ask patches [ set isWater false ifelse random-float 100 < newLakeChance [ intoWater ] [ set pcolor green - ( random-float 0.8 ) ] ] end ;; Grows water areas (this method is iterated) to setup2-patches ask patches [ ;; Only neighbours of water can turn into water ;; That's why setup-patches creates some water seeds! if isWater = true [ ask neighbors [ ;; If a neighbour is grass, attempts conversion if isWater = false [ ;; Patches with more water neighbours will be visited more ofte, so ;; they have a higher chance of becoming water! ;; Important detail: the order in which patches are visited ;; will introduce a bias (if we visit from left to right, there is a ;; chance that the neighbour with xcoord + 1 will become water, and ;; when this is visited it may also turn xcoord + 2 into water, but ;; the same is not true to the left). Unimportant for this toy program. ;; A solution would be to create a second label that stores the old ;; isWater status and only update this when the iteration is finished (for example) if random 100 < lakeGrowth [ intoWater ] ] ] ] ] tick end ;; Water growth produces some unpleasant structures. Here water edges are smoothed. to smoothWater ask patches [ ;; Only turns grass into water! if isWater = false [ ;; Counts the number of neighbours that are water let waterNeighbours 0 ask neighbors [ if isWater = true [ set waterNeighbours waterNeighbours + 1 ] ] ;; We want a path with 4 neighbours to be much more likely to become water than ;; one with 2. We multiply to get squares, so 2 neighbours gets 4 "chances" ;; and 4 neighbours get 16 "chances". set waterNeighbours waterNeighbours * waterNeighbours ;; We get the random number up to 65 so there is a very small chance of ;; finding super small islands (max neighbours squared is 64) if random 65 < waterNeighbours [ intoWater ] ] ] end to intoWater set pcolor 105 - (random-float 0.4 ) set friction 1 set isWater true end to setup-turtles ask turtles [ die ] create-turtles numberTurtles ;; tortoises is an old, confusing class name refering to turtles that are alive ;; the other turtle sub-class is "zombies" ask turtles [ set breed tortoises set energy starvationEnergy ] ask tortoises [ set shape "turtle" setxy random-xcor random-ycor set color 15 ] end to go if count turtles = 0 [ stop ] ask tortoises [ move-tortoises tortoises-eat reproduce ] ask zombies [ move-zombies eat-tortoise ] death grow tick do-plots end to move-tortoises ifelse smartMovement = false [ normalTortoiseMove ] ;; else: smart movement (away from monsters) [ ;; smart movement only if enemies are present! ;; commented code for avoiding other turtles ;;let turtlesInRange 0 ;;ask tortoises in-Radius maxRadius [ ;; set turtlesInRange ( turtlesInRange + 1 ) ;;] ;;ifelse turtlesInRange > 1 ifelse any? zombies in-Radius maxRadius [ set heading awayDirection forward random 2 * (2 - friction) / 2 ] ;; else: normal movement! [ normalTortoiseMove ] ] end to normalTortoiseMove right random lessStraightMovement - ( lessStraightMovement / 2 ) forward random 2 * (2 - friction) / 2 end ;; finds the direction away from most nearby monsters to-report awayDirection let callingTurtle self ;; let's try to run away from the weighted center of mass of enemies! let awayX 0 let awayY 0 ;;ask turtles in-radius maxRadius [ ask zombies in-radius maxRadius [ let localX 0 let localY 0 ;; for every neighbour gets the local x and y distance set localX ( xcor - ( [xcor] of callingTurtle ) ) set localY ( ycor - ( [ycor] of callingTurtle ) ) ;; corrects border effects set localX ( fixDistBorder localX max-pxcor ) set localY ( fixDistBorder localY max-pycor ) ;; the closer the monster is, the larger the weight for the total let dist ( maxRadius - distance callingturtle ) ;; adds the weighted contribution to the total set awayX ( awayX + ( localX * dist ) ) set awayY ( awayY + ( localY * dist ) ) ] ;; We don't need to divide by the total weight, since this only ;; rescales the (awayX, awayY) vector. Note that any ;; k·(a, b) defines the same direction as (a, b)! ;; Invert the values to head AWAY from the monsters! set awayX ( - awayX ) set awayY ( - awayY ) ;; finally converts these local coordinates into an angle! let awayAngle 0 ifelse ( awayX != 0 or awayY != 0 ) [ set awayAngle ( atan awayX awayY ) ] ;; else: undefined angle! [ set awayAngle random 360 ] report awayAngle end ;; checks if the x/y distance to the neighbour is too big ;; which means we have to correct a jump over the edges ;; (so from -max to +max we get distance 1) to-report fixDistBorder [ increment maxCoord ] let minJump ( maxCoord * 2 ) - maxRadius let twoTimesOne ( maxCoord * 2 ) + 1 ifelse abs increment > minJump [ if increment > 0 [ report ( - twoTimesOne + increment ) ] if increment < 0 [ report ( twoTimesOne + increment ) ] ] ;; no changes [ report increment ] end to move-zombies ifelse smartMovementZ = false [ normalZombiMove ] ;; else: smart movement (away from monsters) [ ifelse any? tortoises in-Radius maxRadius [ let target ( one-of tortoises in-Radius maxRadius ) ;; if target is in the same spot (should not happen) this creates an exception ifelse distance target > 0 [ set heading towards target forward random 2 * (2 - friction) / 2 ] [ normalZombiMove ] ] [ normalZombiMove ] ] ;; Zombies die in water! ;;ifelse isWater = true ;; [ die ] ;; [ set energy (energy - moveEnergyConsumption) ] ;; better and faster: water simply takes more energy from them ;; otherwise large amounts of turtles gather in the water ;; remember: friction is 1 in water, 0 otherwise set energy (energy - ( friction + 1 ) * moveEnergyConsumption) end to normalZombiMove right random lessStraightMovementZ - ( lessStraightMovementZ / 2 ) forward random 2 * (2 - friction) / 2 end to tortoises-eat ;; turtles in water gain a bit of energy, otherwise they spend some moving ifelse isWater = true [ set energy (energy + 1) ] ;; no water. first takes the movement penalty [ set energy (energy - moveEnergyConsumption) ;; if there is grass they also eat if pcolor < 101 [ if pcolor > 0 [ set pcolor black set energy (energy + foodEfficiency) ;;set color (color + 10) ] ] ] end to reproduce if energy > energyToReproduce [ set energy (energy - (energyToReproduce / 2) ) hatch 1 [ set energy (energyToReproduce / 2) ] ] end to death ask turtles [ ;; if energy falls below a threshold turtles die. if energy < starvationEnergy [ set deaths (deaths + 1) ifelse random-float 100 < chanceToZombiAtDeath [ set breed zombies set shape "monster" set color 45 ] [ die ] ] ] end to grow ask patches [ if pcolor = 0 [ if random 100 < 1 [ set pcolor 75 - (random-float 0.8 ) ] ] ] end to eat-tortoise if any? tortoises in-radius 3 [ ifelse any? tortoises in-radius 3 with [ energy <= 31 ] [ set energy (energy + 21) ask one-of tortoises in-radius 3 with [ energy <= 31 ] [ die ] ] ;; else [ set energy (energy + 10) ask one-of tortoises in-radius 3 [ set energy (energy - 10) if random-float 100 < chanceInfect [ set breed zombies set shape "monster" set color 125 ] ] ] ] end to do-plots set-current-plot "totals" set-current-plot-pen "tortoises" plot count tortoises ;;set-current-plot-pen "grass" ;;plot count patches with [pcolor > 0] / 10 ;;set-current-plot-pen "deaths" ;;plot (deaths * 10) / ticks set-current-plot-pen "zombies" plot count zombies end

There are 2 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

Turtles and Zombies.png | preview | preview | over 6 years ago, by Pablo Gonzalez de Prado | Download |

This model does not have any ancestors.

This model does not have any descendants.