Last active
May 1, 2017 23:04
-
-
Save wybo/36f24ba1b335aea212eb to your computer and use it in GitHub Desktop.
Fire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AgentBase is Free Software, available under GPL v3 or any later version. | |
# Original AgentScript code @ 2013, 2014 Owen Densmore and RedfishGroup LLC. | |
# AgentBase (c) 2014, Wybo Wiersma. | |
# Fire is a cellular automata model of fire spreading. | |
u = ABM.util | |
class ABM.FireModel extends ABM.Model | |
setup: -> | |
@agentBreeds ["embers", "fires"] | |
@agents.setDefault "shape", "square" | |
@agents.setDefault "color", u.color.red | |
@agents.setDefault "heading", 0 # override promotion to random angle | |
@refreshPatches = false | |
# No optimizations: 50-55fps | |
# @patches.usePixels() # 50-55fps .. not used, refresh off | |
# None of the optimizations particularly useful, other than refresh | |
# If refresh on, fastPatches is in the 50+fps range. | |
@density = 60 # percent | |
@burnedTrees = 0 | |
@initialTrees = 0 | |
# defaults | |
# @animator.setRate 10, true | |
@animator.setRate 60, false | |
for patch in @patches.create() when u.randomInt(100) < @density | |
patch.color = u.color.green # override default, set per patch color | |
for patch in @patches | |
if patch.position.x is @patches.min.x | |
@ignite patch | |
trees = [] | |
for patch in @patches | |
if patch.color.equals [0, 255, 0] | |
trees.push patch | |
@initialTrees = trees.length | |
console.log "burnedTrees #{@burnedTrees}" | |
@burnedTrees = 0 # reset from initial ignites | |
ignite: (patch) -> | |
patch.sprout 1, @fires | |
# in original model but apparently not needed, refresh off? | |
patch.color = u.color.black | |
@burnedTrees++ | |
fadeEmbers: -> | |
for ember in @embers by -1 # -1: allow die() in loop | |
ember.color = ember.color.fraction(0.8) | |
# or (Math.max a.color...) < 100 , needs parens | |
if 100 > Math.max ember.color... | |
ember.patch.color = ember.color | |
ember.patch.draw @contexts.patches | |
ember.die() | |
step: -> | |
if @animator.ticks % 100 is 0 | |
console.log @animator.toString() | |
unless @agents.any() | |
console.log "..stopping, fire done at tick: #{@animator.ticks}" | |
@stop() | |
for fire in @fires by -1 # -1: allow changeBreed() in loop | |
for patch in fire.patch.neighbors(diamond: 1) | |
if patch.color.equals u.color.green | |
@ignite patch | |
@embers.reBreed fire | |
@fadeEmbers() | |
window.model = new ABM.FireModel { | |
div: "world", | |
patchSize: 2, | |
mapSize: 250 | |
} | |
window.model.start() | |
# Uses the AgentBase library (03-02-2017 release) | |
# https://github.com/wybo/agentbase/commit/3501302567c018da82601cd858be2ea6d0b9c74d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment