zombie-attack_v2-0.4


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.



Procedures

NetLogo Version: NetLogo 4.0.4

globals
[   g   ; gravity constant (for gibbets)
    kills ;; number of player kills
    shots ;; number of player shots fired
    hits  ;; number of shots that hit
    misses ;; number of shots that missed
    speed  ;; overall zombie speed
    counter ;; counter for level advancement
    level  ;; current game level / wave
    score  ; current score
    gun-type ;; the type of gun/bullet pattern 
    barrier  ;; the patches that contain the barrier
    game-over?  ;; is the gamve over?
    tocks       ;; occur every 5 ticks
    crosshairs  ;; the turtle that is the crosshair
    gun  ;; the turtle that is the gun
    bullet  ;; 
    fire?  ;; has the fire button been pressed (or, with auto, is it time to fire?)
    full-auto? ;; is full auto mode on?
    target  ;; the thing that is the current gun target
    point-blank  ;; the xcoord beyond which damage is 8
    mid-range ;; the xcoord beyond which damage is 4 
    long-range ;; the xcoord beyond which damage is 2
    truck! ;; keeps track of the last appearance of the zombie truck
    mid-pxcor ;; half-way bewteen min- and max- pxcor
    mid-pycor ;; likewise
    fire-rate ;; how fast does the gun fire (timer mod fire-rate = 0 = fire!)
    ; message ;; text of message to scroll on screen
    ; msg-row ;; current row of message
]
   
breed [ bullets a-bullet ] ;; bullet is really just a line
breed [ zombies a-zombie ] ;; zombies attack in waves
breed [ trucks truck ] ;; a truck is driven by a zombie
breed [ giants giant ] ;; a giant zombie
breed [ trees a-tree ] ;; an innocent tree, can be destroyed by giants and vehicles
breed [ guns a-gun ] ;; the turret that rotates and fires bullets
breed [ gibbets a-gibbet ] ;; gobs of zombie gore and truck parts: results of zombie destruction: a particle system
breed [ new-gibbets ]
breed [ hairs a-hair ] ;; the cross-hair
breed [ messengers a-messanger ] ;; used to display messages

gibbets-own
[ vx vy ;; velocity in x and y, for particle system
  ttl ;; time-to-live : count-down to removal
  burning? ;; true if gibbets are burning
]

guns-own
[ base-shape ;; base of the name..add suffix for flash shapes
]
zombies-own
[ pace   ;; how fast this particular zombie moves
  integrity ;; the current physical integrity of the zombie--at zero, zombie disintegrates
  frame  ;; the current animation frame of this zombie
  base-shape ;; the base of the shape name for this zombie
  bonus      ;; the score bonus obtained from killing this zombie
  sub-breed  ;; the sub-breed of this zombie (zombie, giant, trucks)
  inflammable? ;; can this zombie burn?
  burning?   ;; is this zombie currently  burning?
]

to startup ;; run setup when model loads 
   setup
end 

;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
to setup
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   display
   clear-all 
   setup-background
   setup-default-shapes
   setup-globals
   setup-barrier
   setup-gun-and-bullet
   setup-trees
   setup-crosshair
   zombie-birth
   setup-first-message
end
   
to setup-gun-and-bullet
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ask patch (min-pxcor + 5) 0
   [ sprout-guns 1
     [ set gun self
       set color gray 
       set heading 90
       set size 5
       set base-shape "gun"
       hatch-bullets 1
       [ set bullet self
         set shape "line"
         set size world-width 
         set color white
         __set-line-thickness .2
         hide-turtle
       ]
     ]
   ]
end

to setup-trees
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   let row max-pycor
   repeat world-height
   [ ask patches with [ pycor = row and pxcor > min-pxcor + 5 and random 10 < 5 and (min-pxcor + random world-width ) < pxcor  ]
     [ sprout-trees 1
       [ set shape "tree"
         set size 3
         stamp
         if random 10 > 2 [ die ]
         set shape "tree-top"
       ]
     ]
     set row row - 1
   ]
end

to setup-crosshair   
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   create-hairs 1
   [ set crosshairs self
     set color white 
     set heading 0
     set size 3
     home
   ] 
end   

to setup-first-message
   message-create "STOP THE ZOMBIES!"
end

to message-create [ #message ]
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; TRICK: since anything can ask a patch to do something,
   ;; and a patch can always SPROUT, this trick uses a patch to sprout a turtle
   ;; so that either the observer, a turtle, or even another patch can create a turtle.
   ask patch mid-pxcor max-pycor
   [ sprout-messengers 1
     [ set shape "message"
       set size 1
       set color white
       set label (word #message "   ")
     ]
   ]
end

to setup-default-shapes   
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set-default-shape zombies "zombie0"
   set-default-shape guns "gun"
   set-default-shape gibbets "gibbet"
   set-default-shape bullets "bullet"
   set-default-shape hairs "cross-hair"
end
   
to setup-globals
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set g -0.03  ;; gravity constant
   set game-over? false ;; game over flag 
   set kills 0
   set counter 1
   set speed .1
   set level 0
   set gun-type 0
   ; set ticks 0
   set tocks random 5
   set full-auto? false
   set long-range min-pxcor + world-width * .5
   set mid-range min-pxcor + world-width * .25
   set point-blank min-pxcor + world-width * .125
   set truck! 0
   set full-auto? false
   set mid-pxcor min-pxcor + world-width * .5
   set mid-pycor min-pycor + world-height * .5
   set fire-rate 5
end
     
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
to go
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
    every ( 1 / 60 )
    [
     ifelse game-over?
     [ if not ( any? zombies or any? gibbets or any? messengers ) [ stop ]
     ]
     [ aim
       fire
       if ticks mod 100 = 0 [ barrier-repair ]
     ]
     zombies-move
     gibbets-fly
     message-update
     tick
    ]
end

to aim
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ifelse full-auto?
   [ ifelse any? zombies with [ pxcor < (max-pxcor - 5) ]
     [ if not is-a-zombie? target or random-float 1.0 < 0.05
       [ let targets (zombies with [ pxcor < (max-pxcor - 5) ]) 
         ifelse any? targets with [ not burning? ]
         [ set target min-one-of targets with [ not burning? ] [ distance gun ] ]
         [ set target min-one-of targets [ distance gun ] ]
       ]
       if is-a-zombie? target
       [
         ask crosshairs
         [ setxy (.6 * xcor + .4 * [ xcor ] of target) (.6 * ycor + .4 * [ ycor ] of target )]
       ]
     ]
     [ if not is-a-zombie? target
       [ ask crosshairs
         [ setxy (.8 * xcor + .2 * mid-pxcor) ( .8 * ycor + .2 * ( mid-pycor + world-height * .4 * sin (3 * ticks) ))
         ]
       ]
     ]
     ask gun [ face crosshairs ]
     ask bullet
     [ set heading [ heading  ] of gun
     ]
   ]
   [
   if mouse-inside? 
   [ let mx mouse-xcor
     let my mouse-ycor
     ask crosshairs
     [ setxy mx my
       ask gun
       [ face myself 
         ;ifelse pycor < [pycor] of myself
         ;[ set ycor ycor + .5 ]
         ;[ if pycor > [pycor] of myself
         ;  [ set ycor ycor - .5 ]
         ;]
       ]
       ask bullet
       [ set heading [ heading  ] of gun
       ]
     ]
   ]
   ]
   set fire? mouse-down? or (full-auto? and is-a-zombie? target )
end

to fire
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ifelse ticks mod fire-rate = 0 and fire?
   [ ask bullet
     [ show-turtle ]
     ask gun
     [ set shape (word base-shape "-fire")
       set shots shots + 1
       let hit min-one-of zombies with 
       [ abs subtract-headings (safe-towards myself ) (180 + [heading] of myself ) < 2
       ] [ distance myself ]
       ifelse is-a-zombie? hit
       [ set hits hits + 1
         ask bullet
         [ set size distance hit ]
         ask hit
         [ zombie-injury ]
       ]
       [ set misses misses + 1
         ask bullet [ set size world-width ]
       ]
     ]
   ]
   [ ask bullet [ hide-turtle ]
     ask gun [ if shape != base-shape [ set shape base-shape ] ]
   ]
end

to-report safe-towards [ agent ]
   ifelse patch-here = [ patch-here ] of agent
   [ report 0 ]
   [ report towards agent ]
end
   

;;;
;;; Previous fire routine, when gun fired individual pellets, in various patterns
;;;to fire   
;;;   ask gun
;;;     [ 
;;;       ifelse gun-type = -1  [ make-bullets 1 0 0 0 0 ]
;;;       [ ifelse gun-type = 0  [ make-bullets 2 0 0 -.5 1 ]
;;;       [ ifelse gun-type = 1  [ make-bullets 3 0 0 -1 1 ]
;;;       [ ifelse gun-type = 2  [ make-bullets 3 -5 5 0 0 ]
;;;       [ ifelse gun-type = 3  [ make-bullets 4 0 0 -2 1.25 ]
;;;       [ ifelse gun-type = 4  [ make-bullets 5 -20 10 0 0 ]
;;;       [ ifelse gun-type = 5  [ make-bullets 5 0 0 -4 2 ]
;;;       [ ifelse gun-type = 6  [ make-bullets 7 -15 5 -3 1 ]
;;;                              [ make-bullets 7 -15 5 -6 2 ]
;;;       ]]]]]]]
;;;     ]
;;;end


;to bullets-fly
;;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
;   ask bullets
;   [ if not can-move? 1
;     [ die ]
;     jump 1
;     let targets (zombies-on neighbors) with [ abs (pxcor - [pxcor] of myself) < 1.5 and abs (pycor - [pycor] of myself) < 3 ]
;     if any? targets
;     [ ask one-of targets
;       [ zombie-injury
;       ]
;       die
;     ]
;   ]
;end


to gibbets-fly
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ask gibbets
   [ 
     set ttl ttl - 1 
     if ttl <= 0 
     [ die ]
     set vy vy + g
     let nx xcor + vx
     let ny ycor + vy
     if nx < min-pxcor or nx > max-pxcor
        or ny < min-pycor or ny > max-pycor  
     [ die
     ]

     setxy nx ny
     rt 10
   ]
end

to gibbets-birth [ qty looks new-size]
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   hatch-gibbets qty
   [ gibbet-props
     ifelse looks = ""
     [ ;; standard gore
       ifelse burning? [ set color orange ] [ set color red ]
       set size one-of [1.0 1.5 2.0 ]
     ]
     [ ;; different size or shape gore (e.g.: trucks, mutant )
       set shape looks set size new-size
     ]
   ]
end  

to gibbet-props 
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set vx -.3  + random-float 2.0
   set vy random-float .5
   set ttl 20
   set label ""
end

to zombies-move
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   if ticks mod 2 != 0 [ stop ]
   ;; make new zombies
   if game-over? != true and count zombies < 35 and random 100 <= counter
   [ zombie-birth
   ]
   ask zombies
   [ ;; if random 100 <= counter [ face one-of guns ]
     if not is-patch? patch-ahead 1
     [ zombie-death ]
     if not ( any? (other zombies-on patch-ahead 1)  
              or any? other zombies-here with [ who < [who] of myself ] )
     [ if sub-breed = trucks
       [ stamp-erase ]
       if sub-breed = giants
       [ ask trees in-radius (size * .5) [ die ] ]
       jump pace
     ] 
     set frame frame + .5
     if frame >= 4
     [ set frame 0 
       if burning?
       [ set integrity integrity - 1
         ask zombies in-radius ( size  ) 
         [ zombie-start-burning ]
         if integrity <= 0
         [ update-counters
           zombie-death
         ]
       ]
     ]
     set shape (word base-shape floor frame)
     if shade-of? pcolor barrier-base-color 
     [ let rad size * .5
       if burning? [ set rad size ]
       ask barrier in-radius rad [ set pcolor background-color ]
       zombie-death
     ]
   
     if pxcor <= min-pxcor
     [ zombie-death ]
   ]
   ;; set label integrity

end

to-report in-far-right-column
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   report pxcor = max-pxcor
end      
   
to-report in-the-middle-rows
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   report pycor < max-pycor - 5 and pycor > min-pycor + 5
end

to-report no [ #things ]
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   report not any? #things
end   


to zombie-birth
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   let birth-patches patches with [ in-far-right-column and in-the-middle-rows and no zombies-here ]
   if any? birth-patches
   [ ask one-of birth-patches
     [ sprout-zombies 1
       [ zombie-initialize-regular
         ;; aim the zombie at some barrier brick
         ;; when the barrier is broken down, makes the zombies more deadly
         if any? barrier with [ shade-of? pcolor barrier-base-color ]
         [ face one-of barrier with [ shade-of? pcolor barrier-base-color ]
         ]
         ;; maybe make a fast zombie..
         if-else random 100 < 5 + level
         [ zombie-modify-make-fast
         ][
         ;; maybe make giants!!
         ifelse level >= 10 and random 100 < 2 
         [ zombie-modify-make-giant
         ][
         ;; maybe make trucks (or bulldozers)
         if truck! < level and (level mod 4 = 0 or level > 20)
         [ zombie-modify-make-truck
         ]]]
       ]
     ]
   ]
end

to zombie-initialize-regular
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set heading  270
   set color green + level * 10 - 1 + random-float 2
   set size 3
   set frame 0
   set pace speed + random .1
   set integrity 1 + int (level * .1)
   set base-shape "zombie"
   set bonus 0
   set sub-breed zombies
   set burning? false
   set inflammable? true
end

to zombie-modify-make-fast
   set pace 1
   set color color - color mod 10 + 8
   set integrity 1 + int (.25 * integrity)
end

to zombie-modify-make-giant         
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set size size * 3
   set pace .1 + pace * .1
   set integrity integrity * 10
   set bonus 1000
   set sub-breed giants
   set base-shape "zombie"
          message-create "ALERT! GIANT MUTANT ZOMBIE!"
end

to zombie-modify-make-truck         
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set heading 270
   set truck! level 
   set base-shape one-of [ "truck" "dozer" ]
   set size size * 2
   set pace .1
   set integrity integrity * 50
   set bonus 3000
   set sub-breed trucks
   set burning? false
   set inflammable? false ;; trucks don't burn
   message-create (word "ALERT! " caps base-shape " RIDING ZOMBIE!" )
end   
  
to zombie-injury
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   let damage 0
   if gun-type = 1 
   [ zombie-start-burning ]
   set damage damage-by-range
   set integrity integrity - damage
   ifelse integrity <= 0
   [ update-counters
     zombie-death
   ]
   [ gibbets-birth damage "" 0
   ]
   if xcor + .5 < max-pxcor  [ set xcor xcor + .5 ]
end

to zombie-start-burning
   if inflammable? and not burning?
   [ set burning? true 
     ;; permanently modify base-shape
     set base-shape (word base-shape "-burning" )
   ]
end       

to-report damage-by-range
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ifelse pxcor < point-blank [ report 8 ][
   ifelse pxcor < mid-range [ report 4 ][
   ifelse pxcor < long-range [ report 2 ][
   report 1]]]
end   

to zombie-death
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   let gc count gibbets + count zombies ;; gibbet-count
   ;; ngc = new gibbet count
   let ngc ifelse-value ( gc < 100 ) [ 5 ] [ 0 ]
   let ngs 1
   set base-shape remove "-burning" base-shape
   if gc < 175 [ gibbets-birth 1 (word base-shape "-part0") size ]
   if gc < 150 [ let index 1 repeat 4 [ gibbets-birth 1 (word base-shape "-part" index) size  set index index + 1 ] ]
   ifelse gc < 50 [ gibbets-birth 10 "" (1 + random-float 1.0) ] [ if gc < 100 [ gibbets-birth 5 "" (1 + random-float 1.0) ] ]
   
   if not any? barrier with [ shade-of? pcolor blue ]
   [ game-over ]
   die
end

to make-bullets [ qty offset spread y-off y-inc ]
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set offset heading + offset
   hatch-bullets qty
   [ jump 2.5
     set heading offset
     let nx xcor + y-off * sin ( heading - 90)
     let ny ycor + y-off * cos ( heading - 90)

     set offset offset + spread
     set y-off y-off + y-inc

     if nx < min-pxcor or ny < min-pycor or ny > max-pycor [ die ]
     setxy nx ny
     set color 8 + 10 * random 13
   ]
end

;to update-score [ amt ]
;;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
;   set score score + amt
;   ; set [plabel] of scoreboard (word score ":" level )
;end
   
to update-counters
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   set kills kills + 1
   set score score + level + bonus
   set counter counter + 1
   if counter >= 100
   [ set counter counter - 100
     set speed speed * 1.03
     if speed > .5 [ set speed .1 ]
     set level level + 1
     ifelse gun-type = 0
     [ if fire-rate > 2 and level mod 5 = 0
       [ message-create "RELOAD RATE UPGRADE!"
         set fire-rate fire-rate - 1
       ]
       if level >= 20
       [ set gun-type 1
         ask bullet [ set color red ]
         ask gun [ set base-shape "gun1" ]
         set fire-rate 6
         message-create "WEAPON UPGRADE!"
       ]
     ]
     [ if fire-rate > 1 and level mod 5 = 0
       [ set fire-rate fire-rate - 1
         __set-line-thickness .1 * fire-rate
         message-create "BEAM REFRESH UPGRADE!"
       ]
     ]
   ]
end

to game-over
   ask crosshairs
   [ home
   ]
   message-create "GAME OVER!"
   set game-over? true
end

to setup-background
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ask patches
   [ set pcolor background-color
   ]
end

to-report background-color
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
    ;; reports a textured background color for the current patch, based on patch location, etc.
    report brown - 5 + .1 * random 5 + 6  * ((pycor - min-pycor) / world-height) ; 
end

to setup-barrier
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; defines and colors the patches that comprise the barrier
   set barrier patches with [ pxcor <= min-pxcor + 5 ]
   ask barrier [ set pcolor barrier-color ]
end

to-report barrier-color
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; reports the barrier color for the current patch
   report barrier-base-color - 3 + pxcor - min-pxcor + ((pycor + pxcor) mod 2) 
end

to-report barrier-base-color
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; reports the barrier base color
   ;; hack--for better speed should really be a global
   report blue
end   
       
to barrier-repair
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; if barrier needs repair, repair one cell of the barrier
   if any? barrier with [ not shade-of? pcolor barrier-base-color ]
   [ ask one-of barrier with [not shade-of? pcolor barrier-base-color and any? neighbors4 with [ shade-of? pcolor barrier-base-color ] ]
     [ set pcolor barrier-color ]
   ]
end

to auto-fire
;; ~~._.~~._.~~._.~~._.~~._.~~._.~~._.~~._.
   ;; toggle / initialize if needed, the full-auto variable
   set full-auto? (full-auto? = false)
   let state "DE-"
   if full-auto? [ set state "" ]
   message-create (word "AUTO-FIRE " state "ACTIVATED")
end
 
 
to message-update
   ask messengers
   [ if pycor <= min-pycor [ die ]
     set ycor ycor - .5
     set color 9.9 - color
   ]
end

to-report caps [ #text ]
   let uc "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   let lc "abcdefghijklmnopqrstuvwxyz"
   let result ""
   let c ""
   foreach n-values length #text [ item ? #text ]
   [ set c ?
     let i position c lc
     if i != false
     [ set c item i uc ]
     set result (word result c)
   ]
   report result
end

to-report promote [ #char ]
end

   
   
   

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download zombie-attack_v2-0.4 --