FIREFLY SOURCE CODE

Back to Firefly


Turtle Procedures Observer Procedures
turtles-own [excitation countdown neighbors-light neighbor] 

to setup 
setc white 
seth random 360 
fd sqrt random 80 
set excitation 1 + random threshhold 
set countdown -1 
end 

to go1 
ifelse (excitation >= threshhold) or (countdown = 0) 
    [setc green play-sound "clink"] 
    [setc white] 
end 

to go2 
ifelse (excitation >= threshhold) or 
       (trigger < neighbors-light) 
   [if (excitation >= (threshhold - buffer)) 
      [set countdown threshhold - excitation] 
    setexcitation 1] 
    [set excitation excitation + 1] 
if countdown >= 0 
    [set countdown countdown - 1] 
end 

to tally-neighbors-light 
    set neighbor 1 
    set neighbors-light 0 
    repeat (length flashing) 
       [let [:partner item neighbor flashing] 
       set neighbors-light neighbors-light + 
         (100 / (1 + ((distance (xcor-of :partner) (ycor-of :partner)) ^ 2))) 
       set neighbor neighbor + 1] 
end

globals [current flashing] 

to setup 
ca 
crt number 
ask-turtles [setup] 
end 

to go 
ask-turtles [go1] 
tally-light 

; We do this with two ask-turtles to wait for all fireflies 
; to finish go1 before starting go2. 

ask-turtles [go2] 
end 

to tally-light 
;  build a list of green turtles into "flashing"
set flashing []
ask-turtles [if (color = green) [set flashing fput who flashing]] 

; each turtle computes its illumination from those that are flashing 

ask-turtles [tally-neighbors-light] 
end