Code Appendix

breed [averagers averager]
breed [elderly senior]
breed [children child]
globals [
  fire-temperature
  spontaneous-combustion-threshold
  normal-combustion-threshold
  seat-color
  max-scent
  wall-color
  safe-color
]

patches-own [
  on-fire?
  temperature
  fuel
  is-wall?
  chemical
  food
  nest?
  scent
  is-safe?
]

turtles-own [
  carrying-food?
  flockmates
  nearest-neighbor
  energy 
  trampled? 
]

; Following are setup procedures:

to setup
    ca
    setup-globals
    setup-patches-from-import
    diffuse-scent
    setup-turtles
    setup-fire
end

to setup-globals
    set max-scent 1000
    set wall-color blue
    set safe-color cyan
    set seat-color brown
    set fire-temperature 2
    set spontaneous-combustion-threshold 1
    set normal-combustion-threshold 0.5
end

to setup-patches-from-import
    import-pcolors "Aud_5426rsd2.png"
       ask patches 
          [set nest? 
            (((abs pxcor) = max-pxcor) or ((abs pycor) = max-pycor))
            if (nest?)[set pcolor violet ]]
     ask patches [
        set is-safe? false
        set is-wall? false
        set scent 0
        ifelse (shade-of? pcolor 115) [
            set nest? true
            set scent max-scent
        ] [
            if (shade-of? pcolor wall-color) [
                set is-wall? true
            ]
        ]]
end

to diffuse-scent
    loop [
        let propagation-set (
            patches with [
                not nest?
                and not is-wall?
                and any? neighbors with [
                    not is-wall? 
                    and ((scent - scent-of myself) > (0.001 + sqrt 2))
                ]
            ]
        )
        ifelse (any? propagation-set) [
            ask propagation-set [
                set scent (
                    max list
                        max values-from neighbors4 [scent - 1]
                        max values-from neighbors [scent - sqrt 2]
                )
            ]
        ] [
            stop
        ]
    ]
end

to setup-turtles
  ask (n-of number-of-averagers
            (patches with [pcolor = seat-color])) [
        sprout-averagers 1[ 
    setxy (xcor - 0.5 + random-float 1) (ycor - 0.5 + random-float 1)
    set size 2 
    set color red
    set carrying-food? true
    set energy 100 
    set trampled? false
    set breed (averagers)] 
]
  ask (n-of number-of-children
            (patches with [pcolor = seat-color])) [
        sprout-children 1[ 
    setxy (xcor - 0.5 + random-float 1) (ycor - 0.5 + random-float 1)
    set size 2 
    set color green
    set carrying-food? true
    set energy 60 
    set trampled? false
    set breed (children)] 
]
  ask (n-of number-of-elderly
            (patches with [pcolor = seat-color])) [
        sprout-elderly 1[ 
    setxy (xcor - 0.5 + random-float 1) (ycor - 0.5 + random-float 1)
    set size 2 
    set color yellow
    set carrying-food? true
    set energy 40 
    set trampled? false
    set breed (elderly)] 
]
end

to setup-fire
   ask patches [
  set on-fire? false
  set temperature 0
  ifelse (shade-of? pcolor cyan) [
    set fuel 0
    set is-safe? true
    ][
    set fuel 50
    set is-safe? false ]
  ifelse (shade-of? pcolor blue) [
    set fuel 1
    set is-wall? true
  ] [
    set fuel 50
    set is-wall? false
  ]
  ifelse (shade-of? pcolor violet) [
  set fuel 0
  set nest? true
  ][
  set fuel 50
  set nest? false
  ]
    draw-color-gradient
  ]
end


; Run Procedures:

to do-fire
  if (mouse-down?) [
      ask patch-at mouse-xcor mouse-ycor [
          catch-fire
          draw-color-gradient
        ]
    ]
end

to iterate
  dissipate-heat
  ask patches with [on-fire?] [
    burn
    set scent 0
  ]
  ask patches [
    spread-fire
    draw-color-gradient
    set temperature (temperature * 0.99)
  ]
  ask turtles [ flock? ] 
  ask turtles [ if carrying-food? [ return-to-nest ]]
    diffuse chemical (diffusion-rate / 100)
  ask turtles [ trample ]
  ask turtles [if pcolor = yellow [die]]
    
end


to dissipate-heat
  diffuse temperature 0.75
  ask patches with [is-wall?] [
    set temperature (temperature * 0.45)
  ]
  ask patches with [is-safe?] [
    set temperature 0
    ]
   ask patches[
      set chemical (chemical * (100 - evaporation-rate) / 100)]
end

to draw-color-gradient
  if (not is-wall?) [
    ifelse (on-fire?) [
      set pcolor yellow
    ] [
      set pcolor scale-color red temperature 0 2
    ]
  ]
end

to burn
  set temperature 2
  set fuel (fuel - 1)
  if (fuel <= 0) [
    set on-fire? false
  ]
end

to catch-fire
  set on-fire? true
  set temperature 2
end

to spread-fire
  if ((not on-fire?) and (fuel > 0)) [
    ifelse (temperature >= spontaneous-combustion-threshold) [
      catch-fire
    ] [
      if ((temperature >= normal-combustion-threshold) and (any? neighbors4 with [on-fire?])) [
        catch-fire
      ]
    ]
  ]
end

to flock?
   if nest?  
  [set carrying-food? false
    stop
  ]
  if (not nest? and not is-wall?)
    [set carrying-food? true 
    if (is-wall?)
     [ set carrying-food? true 
       step 
       find-flockmates
       ]
    find-flockmates
      if any? flockmates
        [ find-nearest-neighbor
          ifelse distance nearest-neighbor < minimum-separation
            [ separate ]
            [ align
              cohere ] 
              ]]
end

to find-flockmates
  set flockmates (turtles in-radius vision) with [self != myself]
end

to find-nearest-neighbor
  set nearest-neighbor min-one-of flockmates [distance myself]
end

to separate
  turn-away (heading-of nearest-neighbor) max-separate-turn
end

to align 
  turn-towards average-flock?mate-heading max-align-turn
end

to-report average-flock?mate-heading  
  report atan sum values-from flockmates [sin heading]
              sum values-from flockmates [cos heading]
end

to cohere  
  turn-towards average-heading-towards-flockmates max-cohere-turn
end

to-report average-heading-towards-flockmates  
  report atan mean values-from flockmates [sin (towards myself + 180)]
              mean values-from flockmates [cos (towards myself + 180)]
end

to turn-towards [new-heading max-turn]  
  turn-at-most (subtract-headings new-heading heading) max-turn
end

to turn-away [new-heading max-turn]  
  turn-at-most (subtract-headings heading new-heading) max-turn
end


to turn-at-most [turn max-turn]  
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end

to return-to-nest  
  ifelse nest?  
  [set carrying-food? false
    stop
  ][
  step
  if (not is-wall? and not nest?)[
    uphill-nest-scent   
    wiggle              
    ]]
end

to step
    if (any? ((patches in-cone 3 60) with [on-fire?])) [
      let danger-patches ((patches in-radius 5) with [on-fire?])
      face max-one-of neighbors [min values-from danger-patches [distance myself]]
      forward 1
]
    if is-wall?-of patch-ahead 1 [
     let x dx + xcor
     let y dy + ycor
     face min-one-of neighbors with [not is-wall?] [distancexy x y]
 ]
fd 1
end

to uphill-nest-scent  
  wiggle
  
   let min-scent min values-from neighbors with [not is-wall?] [scent] - 4
  let selected-neighbor max-one-of neighbors with [not is-wall?] [(scent - min-scent) * random-float 1]
  face selected-neighbor (step)
end

to wiggle 
  if (not is-wall?) [
    rt random 40 - random 40
    if not can-move? 1
      [ rt 180 ]]
end

to-report get-nest-scent [ angle ]
  let p patch-right-and-ahead angle 1
  if p != nobody
  [ report scent-of p ]
  report 0
end

to trample 
if not nest? [ 
let victim one-of (other-turtles-here 
with [energy <= energy-of myself]) 
if victim != nobody [ 
ask victim [ 
get-trampled 
] 
] 
]
end 

to get-trampled 
set trampled? true 
set energy energy - 2 
if energy <= 0 [ 
die 
] 
end