Billy Bob's got a gun! Humans fight back!

This version gives Humans the chance to find a cache of weapons that will allow them to fight back and kill the Zombies.  There may be some human casualties along the way because don't care who they hit.

powered by NetLogo

view/download model file: Zombieland.1.4.nlogo

WHAT IS IT?

Zombies!!

HOW IT WORKS

Eat Brains!!! (Seriously - see the code for information - it is fully notated.)

HOW TO USE IT

1. Click the Draw Buildings button to enable the mouse to create buildings in the world.
2. Left click and drag the mouse to draw.
3. Set initial number of zombies, humans and weapon caches.
4. Set speed for Humans and Zombies.
5. Set Shot Accuracy for weapon use.
6. Set vision radius for Humans and Zombies.
7. Push Setup to get ready to eat brains!
8. Push Go to eat brains!

Use Clear Turtles button to restart simulation with existing buildings.
Use Clear All button to start from scratch.

THINGS TO NOTICE

Zombies now seek brains and Humans run away if they see a Zombie near them.
Modified the movement code to smooth the motion of agents.
Zombies and Humans both treat yellow patches as buildings and will not cross that patch.
The Humans can fight back now. If a Human lands on a weapon cache, he becomes armed (Go, Billy Bob!) and can shoot Zombies. If a Human is hit by a bullet, they will die too.

THINGS TO TRY

Start with just 1 zombie. Slow Zombies will eventually catch faster humans.

EXTENDING THE MODEL

Super Zombies! Humans that hide!

NETLOGO FEATURES

The simulation runs smoother if the model is set to continuous update instead of update on ticks.

RELATED MODELS

None that I know of.

CREDITS AND REFERENCES

Michael Ball, Research Coordinator, Computational Modeling & IT Resources at The Center for Complexity in Health - KSUA

PROCEDURES

;; Zombieland Model ver 1.4 - a basic simulation of a zombie attack with seek and flee
;; functions for agents.  Movement speed and vision radius are also adjustable.  The user
;; can draw buildings in the environment to provide obstacles for the agents. Added
;; clear-turtles and clear-all buttons to user interface. User can now add weapon caches
;; to the environment.  When a human agent lands on a weapon cache, he becomes armed and
;; morphs into a new type of agent - Billybob.  Billybob agents can shoot and kill zombies
;; that come within their shooting radius. Bullets can't tell the difference between humans
;; and zombies, so if a human's path crosses a bullet, they die as well.



breed [human] ;;The poor sods that are getting eaten.
breed [zombie] ;;Brains!!!!!!!!!
breed [billybob] ;;Human with a gun.
breed [gun] ;;Weapon cache agent.
breed [bullet] ;; to kill agents.
breed [dead]  ;; Agent used to leave corpse of killed agents.

globals [
  total-human  ;;Total number of agents that are human.
  total-zombie  ;;Total number of agents that are zombies.
  total-billybob  ;;Total number of agents that are billybobs.
  ]

turtles-own [
  speed  ;;How fast the agents can move.
  scared ;;Agent-subset consisting of zombies within vision radius of a single human.
  seek1 ;;Agent-subset consisting of humans within vision radius of a single zombie.
  seek2 ;;Agent-subset consisting of billybobs within vision radius of a single zombie.
  brains  ;;Agent-subset consisting of humans and billybobs within vision radius of a single zombie.
  nearest-brains  ;;Variable that holds the target human for a single zombie.
  nearest-zombie  ;;Variable that holds the target zombie for a single human.
  turn-check  ;;Holds the random variable for the wander sub-routine.
  wall-turn-check  ;;Holds the random variable for the wall sub-routine.
  aim-check  ;;VHolds the random variable for the shoot sub-routine.
  energy ;; Variable to limit distance bullets travel.
  ]

to building-draw
  if mouse-down?     ;; Use the mouse to draw buildings.
    [
      ask patch mouse-xcor mouse-ycor
        [ set pcolor yellow ]]
end  

to Setup  ;;Clear previous run and setup initial agent locations.
  pop-check
  setup-agents
  update-globals
end

to go  ;;Run the simulation.
   scared-check
  repeat human-speed [ ask human [ fd 0.2 ] display ] ;;Controls the speed of humans, zombies,
  repeat zombie-speed [ ask zombie [ fd 0.2 ] display ] ;;and bullets. it also smooths the
  repeat human-speed [ ask billybob [ fd 0.2 ] display ] ;;  motion in simulation.
  repeat human-speed + 1 [ ask bullet [ fd 0.2 ] display ]
  update-globals
  tick
end

to setup-agents ;; Create the desired number of each breed on random patches.
  set-default-shape zombie "person"
  set-default-shape human "person"
  set-default-shape gun "gun"
  set-default-shape billybob "redneck"
  set-default-shape bullet "dot"
  set-default-shape dead "caterpillar"
  
  ask n-of initial-zombies patches with [pcolor  = black]
     [ sprout-zombie 1
      [ set color red ] ]
      
  ask n-of initial-humans patches with [pcolor = black]
    [ sprout-human 1
      [ set color blue ] ]
      
  ask n-of weapon-cache patches with [pcolor = black]
    [ sprout-gun 1
      [ set color orange ] ]
end  

to scared-check ;; Test if humans are near a zombie and have them run away if they are.
  ask human [
    if any? other turtles-here with [color = orange]
    [lock-n-load]
    zombies-near
    ifelse any? scared
    [run-away]
    [wander]
  ]
  
  ask billybob [ ;; Test if billybobs are near a zombie and have them run away if they are.
    zombies-near
    ifelse any? scared
    [defend]
    [wander]
  ]
  
      ;; Test if zombies are near a human and have them chase them if they are.
  ask zombie [
    if any? other turtles-here with [color = blue]
    [convert]
     if any? other turtles-here with [color = green]
    [convert]
    seek-brains
    ifelse any? brains
    [run-toward]
    [wander]   
  ]
  
  ask bullet [ ;;bullet movement, wall check, target check, and remove bullet after energy = 0.
     if [pcolor] of patch-ahead 1 != black
     [die]
    if energy < 14
    [
      if any? other turtles-here with [color = red]
     [kill]
    if any? other turtles-here with [color = blue]
     [kill]
    if any? other turtles-here with [color = green]
     [kill]
    ]
    set energy energy - 1
    if energy = 0
     [die]  
  ]
end
  
to wander ;; If an agent is not fleeing or chasing, have them wander around aimlessly.
    set turn-check random 20
    if turn-check > 15
    [right-turn]
    if turn-check < 5
    [left-turn]
     if [pcolor] of patch-ahead 1 != black
     [wall]
    ask zombie [ 
      if any? other turtles-here
      [ convert] ]
end  

to wall ;;turn agent away from wall
    set wall-turn-check random 10
    if wall-turn-check >= 6
    [wall-right-turn]
    if wall-turn-check <= 5
    [wall-left-turn]
end

to wall-right-turn ;;Generate a random degree of turn for the wall sub-routine.
  rt 150
end

to wall-left-turn ;;Generate a random degree of turn for the wall sub-routine.
  lt 150
end
   
to right-turn ;;Generate a random degree of turn for the wander sub-routine.
  rt random-float 10
end

to left-turn   ;;Generate a random degree of turn for the wander sub-routine.
  lt random-float 10
end
  
to convert ;;When a zombie lands on patch occupied by human, eat brains and turn human or billybob into zombie.
  ask human-on patch-here[set breed zombie]
  ask billybob-on patch-here[set breed zombie]
  ask zombie-on patch-here [set color red]
end

to zombies-near  ;;adds all zombies in vision radius of human to an agent subset for that agent.
  set scared zombie in-radius human-vision
end

to run-away ;;Make human flee the zombie closest to it.
  set nearest-zombie min-one-of scared [distance myself]
  face nearest-zombie
  rt 180
   if [pcolor] of patch-ahead 1 != black
     [wall]
end

to seek-brains  ;;adds all humans and billybobs in vision radius of zombie to an agent subset for that agent.
  set seek1 human in-radius Zombie-vision
  set seek2 billybob in-radius zombie-vision
  set brains (turtle-set seek1 seek2)
end

to run-toward  ;;Make a zombie chase the human or billybob closest to it.
  set nearest-brains min-one-of brains [distance myself]
  face nearest-brains
  if any? other turtles-here
    [convert]
  if [pcolor] of patch-ahead 1 != black
    [wall]
end  

to lock-n-load ;;When a human lands on patch occupied by a weapon cache, give him a gun.
  ask human-on patch-here[set breed billybob]
  ask billybob-on patch-here [set color green]
end

to defend  ;;Make Billybob shoot at nearest Zombie.
   set nearest-zombie min-one-of scared [distance myself]
   face nearest-zombie
   shoot
   rt 180
   if [pcolor] of patch-ahead 1 != black
     [wall]
end

to shoot ;;Fire that gun, Billy Bob.
  set aim-check random 100
  if aim-check < shot-accuracy
  [
      hatch-bullet 1
      [
        set size .5
        set color white
        set energy 15
      ]
    ]
end
  
to kill ;;When a bullet lands on patch occupied by another, kill the agent and the bullet.
   ask zombie-on patch-here
  [
    set breed dead
    set color white
    ]
  ask human-on patch-here
  [
    set breed dead
    set color pink
    ]
  ask billybob-on patch-here
  [
    set breed dead
    set color green
    ]
   
  die
end

to update-globals ;;Set globals to current values for reporters.
  set total-human (count human)
  set total-zombie (count zombie)
  set total-billybob (count billybob)
end
  
to pop-check  ;; Make sure total population does not exceed total number of patches.
  if initial-zombies + initial-humans > count patches
    [ user-message (word "This Zombieland only has room for " count patches " agents.")
      stop ]
end

    

; *** NetLogo 4.1 Model Copyright Notice ***
;
; Copyright 2010 by Michael D. Ball.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Michael D. Ball.
; Contact Michael D. Ball for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in academic publications, please use:
; Ball, M. (2010).  Zombieland Model ver. 1.4.
; http://www.personal.kent.edu/~mdball/netlogo_models.htm.
; The Center for Complexity in Health,
; Kent State University at Ashtabula, Ashtabula, OH.
;
; In other publications, please use:
; Copyright 2010 Michael D. Ball.  All rights reserved.
; See http://www.personal.kent.edu/~mdball/netlogo_models.htm
; for terms of use.
;
; *** End of NetLogo 4.1 Model Copyright Notice ***