NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.
NetLogo Version: NetLogo 4.0.4
;; SUMMARY ;;;; Zombies ruin everything. ;; COPYRIGHT & LICENSE ;;;; Copyright (C) 2005 James P. Steiner ;;;; Some Rights Reserved. ;;;; Creative Commons Attribution-NonCommercial-ShareAlike License v. 2.0. ;;;; Visit http://creativecommons.org/licenses/by-nc-sa/2.0/ for more information. ;; APPLET DIMENSIONS (width x height) ;;;; 1000 x 600 ;; ;; to __DEFINE_GLOBALS end ;; globals [ wall-color space-color crumb-color human-color armed-color panic-color infected-color zombie-color chase-color dead-color clock armory ;; a patch that contains the armory that contains weapons that kill zombies last-tick ] ;; to __DEFINE_BREEDS__ end ;; breed [ clocks a_clock ] ;; displays passage of time breed [ humans a_human ] ;; a normal human being breed [ corpses a_corpse ] ;; a dead human being that may become a zombie breed [ zombies a_zombie ] ;; an undead human being ;; Human roles breed [ regular] breed [ smartcalm ] breed [ hunter ] breed [ armories an_armory ] armories-own [ quantity ] ;; to __DEFINE-HUMANS-OWN__ end ;; humans-own [ role ;; what kind of human is this? (see breeds} mood-change? ;; is the human changing mood? will-panic? ;; is the human about to panic? panic? ;; is the human in a panic? calmness ;; probability that the human will panic when frightened panic-countdown ;; for how much longer will the human panic infected? ;; is the human infected by zombie-germs? threats ;; what threats does the human perceive speed phase armed? ;; can this human kill zombies? ammo ;; how many rounds of ammunition (how many zombies can I kill?) skill ;; goes up with each shot ] ;; to __DEFINE-CORPSES-OWN__ end ;; corpses-own [ infected? incubation-countdown ;; how long until the corpse rises as a zombie ] ;; to __DEFINE-ZOMBIES-OWN__ end ;; zombies-own [ phase ;; movement phase (0 - 4) frezy-level ;; probability that the zombie will go into a killing frenzy when tempted frenzy? ;; is the zombie in a killing frenzy? mood-change? ;; is the zombie experienceing a mood change? frenzy-countdown ;; for how much longer is the zombie is a frenzy? decomposition ;; what is the state of the zombie's physical body? resting-countdown ;; for how much longer is the zombie resting? targets ;; what targets does the zombie perceive? speed ] ;; to __DEFINE-PATCHES-OWN__ end ;; patches-own [ wall? wall-damage ;; aount of damage sustained by the wall wall-integrity ;; amount of damage that the wall can sustain before a zombie can walk over/through it dropped-ammo dropped-guns ] to-report sign [ value ] ifelse value < 0 [ report -1 ][ ifelse value > 0 [ report 1 ][ ifelse value = 0 [ report 0 ][ ]]] end to setup--------------- ca set space-color black set wall-color gray + 1 set human-color pink set armed-color yellow set panic-color pink + 2 set dead-color cyan set zombie-color white set infected-color cyan set chase-color yellow foreach (list turtles humans zombies corpses) [ set-default-shape ? "square" ] run layout ask patches with [ is-wall? self ] [ set wall-integrity 100 set wall-damage 0 ] place-clock reset end to armory-setup if not weapons-available? [ ask armories [ die ] stop ] let rad int (1 + max-pxcor * plaza-size * .01) let freq rad * 3 ask patches with [ pxcor mod freq = 0 and pycor mod freq = 0 ] [ if not limited? OR self = patch 0 0 [ sprout-armories 1 [ set color armed-color set quantity max (list 1 (population * .01)) ] ] ] blink armories "Weapons->" end to reset ask turtles with [ breed != clocks ] [ die ] ask patches [ set wall-damage 0 color-wall ] humans-setup armory-setup end to humans-setup make-people end to make-people let spaces patches with [ pcolor = space-color ] let pop min (list population (count spaces) ) ask n-of pop spaces [ sprout 1 [ human-setup ] ] ask n-of 10 humans [ die-infected ] ;; blink the corpse for a while blink corpses "Infection!->" end to blink [ agents message ] ask agents [ set label message ] repeat 3 [ ask agents [ set size world-width * .2 ] display wait .5 ask agents [ set size world-width * .1 ] display wait .5 ] ask agents [ set size 1 set label "" ] display end to human-setup set breed humans set heading 90 * random 4 set panic? false set infected? false set panic-countdown 0 set color human-color set calmness random-float 100 set speed 2 set phase random 2 set armed? false set ammo 0 set skill .1 end to-report new-clock let me nobody create-clocks 1 [ set shape "clock" set heading 0 set size 0 setxy max-pxcor max-pycor set me self ] report me end to place-clock if not is-turtle? clock [ set clock new-clock ] ask clock [ let clock-size clock-scale * .01 * max-pxcor * .5 if size != clock-size [ set size clock-size set xcor ((max-pxcor - clock-size * .5) * sign xcor ) set ycor ((max-pycor - clock-size * .5) * sign ycor ) ] ] end to clock-update ask clock [ rt .1 set label (word (pad (ticks / 3600)) ":" (pad ((ticks / 60) mod 60)) ":" (pad (ticks mod 60)) " ") ] tick end to-report pad [ number ] set number (word "00" precision (floor number) 0) set number substring number (length number - 2) (length number) report number end to monitor-mouse-move-clock if mouse-down? [ ask clock [ if abs (mouse-xcor - xcor) < (size * 2) and abs (mouse-ycor - ycor) < (size * 2 ) [ setxy (ycor) (- xcor) ] ] ] end to die-infected if weapons-available? and armed? [ ;; drop weapons and ammo where attacked set dropped-ammo dropped-ammo + ammo set ammo 0 set dropped-guns dropped-guns + 1 set armed? false ] set breed corpses set incubation-countdown incubation-period set color infected-color set infected? true end to go if not any? humans or not (any? zombies or any? corpses) [ if not any? humans [ create-zombies 1 [ set size world-width set color green set shape "zombie" set label "Z O M B I E !" ] ] stop ] ifelse watch? [ if subject = nobody [ ifelse any? zombies [ watch one-of zombies ][ ifelse any? corpses [ watch one-of corpses ][ watch one-of humans ]]] ] [ if is-turtle? subject [ reset-perspective ] ] ask humans with [ ticks mod speed = phase ] [ human-do-things ] ask zombies with [ ticks mod speed = phase ] [ zombie-do-things ] ask corpses [ corpse-do-things ] humans-reproduce set-current-plot-pen "people" plot count humans set-current-plot-pen "zombies" plot count zombies monitor-mouse-move-clock clock-update end to humans-reproduce if count humans >= 2 and random-float 1.0 < human-repro-rate ;; * (count humans) [ ask one-of (patches with [ (pcolor = space-color) and any? humans-on neighbors and not any? humans-here] ) [ sprout 1 [ human-setup ] ] ] end to human-do-things ;; locals [ index ] if infected? [ set breed corpses set color infected-color set incubation-countdown incubation-period stop ] if weapons-available? and not armed? and ( ( any? armories-here and [ quantity ] of one-of armories-here > 0 ) or ( dropped-guns > 0 ) ) [ set armed? true set color armed-color ifelse dropped-guns > 0 [ set dropped-guns dropped-guns - 1 set ammo ammo + dropped-ammo set dropped-ammo 0 ] [ set ammo min (list 100 (population * .05)) if limited? [ set ammo ammo * 5 ] ask one-of armories-here [ set quantity quantity - 1 if quantity <= 0 [ make-wall set armory nobody die ] ] ] ] ;; set index 1 ;; human-assess-threats ;; if armed, and chance says able to take a shot, ;; and there is a zombie in range, then take a shot ;; 10% channce that shot is effective if weapons-available? and armed? [ let target-zombies zombies in-radius 5 if any? target-zombies [ if random-float 1.0 < skill [ ask one-of target-zombies [ die ] ] set skill min (list 1.0 (skill * 1.01) ) set ammo ammo - 1 if ammo <= 0 [ set armed? false set color armed-color - 2 ] stop ] ] ;; nothing else happend to prevent it, so take a step ;; if possible ;; prepare variable for the available paths ;; and the selected path let path nobody let paths no-patches ;; store path-ahead for reuse let path-ahead patch-ahead 1 ;; if the path-ahead contains a zombie ;; and if the path-behind exists and is open, ;; then move away from the zombie ifelse [ not safe-patch ] of path-ahead [ if [ is-space? self and safe-patch ] of patch-ahead -1 [ set path patch-ahead -1 ] if humans-make-barricades? [ ;; 1:100 chance the human will erect a barricade ;; but die in the process if random 100 < 1 [ make-wall die ] ] ] [ ;; otherwise, choose a random path ;; but bias the selection 75% toward the current direction. ;; get available paths set paths [ self ] of neighbors4 with [ is-space? self and safe-patch ] ;; if nowhere to go, just stop if length paths = 0 [ stop ] ;; if path-ahead is open, and is the only path available or with %75 probability, ;; take the path ahead ifelse member? path-ahead paths and ((length paths = 1) or (random 100 < 75)) [ set path path-ahead ] [ ;; otherwise, take a step in a random direction, other than the path ahead. ;; (if path-ahead is in the selection, remove it) set paths remove path-ahead paths set path first shuffle paths ] ] if is-patch? path [ face path move-to path ] end to-report safe-patch report not (any? zombies-here or any? corpses-here ) end ;; ;; CORPSE STUFF ;; to corpse-do-things set incubation-countdown incubation-countdown - 1 if incubation-countdown <= 0 [ zombie-hatch ] end ;; ;; ZOMBIE STUFF ;; to zombie-vision-old ; let index 0 ; ; set index 1 ; let pir patches at-points ( n-values zombie-vision-range [ (list (dx * (? + 1)) (dy * (? + 1)) ) ]) ; set pir pir end to zombie-do-things ;; decompose, possibly die ;; zombie-eww!-decompose ;; stop if the zombie became a rotter if breed != zombies [ stop ] let index 0 ;; if resting, countdown and stop if resting-countdown > 0 [ set resting-countdown resting-countdown - 1 stop ] ;; take a stumbling step in a random direction, ;; biased %50 towards the current direction let paths [self] of neighbors4 with [ is-space? self ] let path-ahead patch-ahead 1 if is-space? path-ahead [ set paths (sentence paths (n-values (length paths - 1) [ path-ahead ]) ) ] if length paths > 0 [ let path first shuffle paths face path move-to path ;; if any humans here, attack, then stop if any? humans-here [ zombie-attack! ] ] end to zombie-hatch set breed zombies set heading one-of [ 0 90 180 270 ] set decomposition 0 set speed 6 set phase random 6 set color zombie-color ; zombie-relax... end to zombie-attack! ask one-of humans-here [ set infected? true ] ; set resting-countdown after-kill-rest ; ifelse blood-lust? ; [ zombie-frenzy! ] ; [ zombie-relax... ] end to zombie-relax... ; set frenzy? false ; set frenzy-countdown 0 ; set color zombie-color end to zombie-frenzy! ; set mood-change? "frenzy!" end ;; ;; ENVIRONMENT GENERATORS ;; to open ask patches [ make-space ] end to corridors let h 0 let w 0 let x1 0 let y1 0 let x2 0 let y2 0 let region 0 let flipper 0 let index 0 ask patches [ make-wall ] let invex 0 set index 2 * int (.5 * sqrt (world-width * world-height) ) repeat (index / 2) [ ifelse random 2 = 0 [ set h 3 + int (index * random-float 1 ) * 2 set w 3 + int (invex * random-float 1 ) * 2 ] [ set h 3 + int (invex * random-float 1 ) * 2 set w 3 + int (index * random-float 1 ) * 2 ] set x1 min-pxcor + random ( world-width - w) set y1 ( max-pycor) - random ( world-height - h) set x2 x1 + w set y2 y1 - h set region patches with [ is-between? pxcor x1 x2 and is-between? pycor y1 y2 ] ask region [ make-space ] if index mod 8 != 0 ;; random 10 < 8 [ ask region with [ pxcor > x1 and pxcor < x2 and pycor < y1 and pycor > y2 ] [ make-wall ] ] set index index - 2 set invex invex + 2 ] ask patches with [ abs pxcor + 1 = max-pxcor or abs pycor + 1 = max-pycor ] [ make-space ] ask patches with [ abs pxcor = max-pxcor or abs pycor = max-pycor ] [ make-wall ] end to mazes clear-patches clear-turtles set crumb-color yellow ;; fill in as all walls ask patches [ set pcolor wall-color set wall? true set wall-damage 0 set wall-integrity 100 ] ;; make open-space border around edges (keep maze from wrapping) ask patches with [ abs pxcor > max-pxcor - 2 or abs pycor > max-pycor - 2 ] [ set pcolor space-color ] ;; create a turtle, and do the maze thing crt 1 [ setxy 0 0 set pcolor crumb-color do-maze timer + 1 make-space die ] ask patches with [ abs pxcor > max-pxcor - 2 or abs pycor > max-pycor - 2] [ make-wall ] end to make-space set pcolor space-color set wall? false end to make-wall set pcolor wall-color set wall? true end to mazes&plazas mazes let rad int (1 + max-pxcor * plaza-size * .01) let freq rad * 3 ask patches with [ pxcor mod freq = 0 and pycor mod freq = 0 ] [ ask patches in-radius rad [ make-space ] ] ask patches with [ abs pxcor > max-pxcor - 2 or abs pycor > max-pycor - 2] [ make-wall ] end to do-maze [ timeout ] if timer > timeout [ stop ] let more?? true while [ more?? and timer <= timeout ] [ set more?? false let opens patches at-points [[0 2][0 -2][2 0][-2 0]] with [ pcolor = wall-color ] ;; take steps forward until you get stuck while [ any? opens ] [ let choice nobody ifelse random 100 < 75 and member? patch-ahead 2 [ self ] of opens [ set choice patch-ahead 2 ] [ set choice one-of opens ] face choice repeat 2 [ jump 1 set pcolor crumb-color ] set opens patches at-points [[0 2][0 -2][2 0][-2 0]] with [ pcolor = wall-color ] set more?? true ] ;; back up along the path until you get to a place where there are choices again let crumbs patches at-points [[0 1][0 -1][1 0][-1 0]] with [ pcolor = crumb-color ] if any? crumbs [ let choice one-of crumbs set heading towards choice repeat 2 [ make-space jump 1 ] set more?? true ] ] end ;; ;; UTILITY PROCEDURES ;; to-report is-between? [ value limit1 limit2 ] ifelse limit1 <= limit2 [ report (value >= limit1 and value <= limit2 ) ] [ report (value >= limit2 and value <= limit1 ) ] end to goto-patch [ agent ] move-to agent ;; setxy [pxcor] of agent [pycor] of agent end to goto-turtle [ agent ] move-to agent ;; setxy [xcor] of agent [ycor] of agent end ;; ;; GETTERS AND SETTERS ;; to-report is-wall? [ agent ] report [ wall? = true] of agent end to color-wall ifelse wall? = true [ set pcolor wall-color ] [ set pcolor 0 ] end to-report is-intact-wall? [ agent ] report [ wall? = true and not wall-breached? ] of agent end to-report wall-breached? report ( wall-damage > wall-integrity ) end to-report is-space? [ agent ] report [ wall? = false] of agent end
View or download the complete model file (to download: right-click, save-link-as):
-- Download zombie-outbreak-simulator --