forked from concord-consortium/agentscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfire.html
84 lines (76 loc) · 3.09 KB
/
fire.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<html>
<head>
<title>Fire Model</title>
<script src="../lib/agentscript.js"></script>
<script src="../tools/coffee-script.js"></script>
<script type="text/coffeescript">
u = ABM.Util # ABM.Util alias
class MyModel extends ABM.Model
setup: ->
@agentBreeds "embers fires"
@turtles.setDefault "shape", "square"
@turtles.setDefault "color", [255,0,0]
@turtles.setDefault "heading", 0 # override promotion to random angle
@refreshPatches = false # <1fps->50-55fps, no surprise, app optimized for this
# No optimizations: 50-55fps
# @patches.usePixels() # 50-55fps .. not used, refresh off
# @patches.cacheTurtlesHere() # 50-55fps, not needed * overhead
# 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
# @anim.setRate 10, true
@anim.setRate 60, false
for p in @patches when u.randomInt(100) < @density
# u.setColor p.color, 0, 255, 0
p.color = [0, 255, 0] # override default, set per patch color
@ignite p for p in @patches when p.x is @patches.minX
@initialTrees = (p for p in @patches when u.colorsEqual p.color, [0, 255, 0]).length
console.log "patches: #{@patches.length}, turtles #{@turtles.length}"
console.log "initialTrees: #{@initialTrees}, burnedTrees #{@burnedTrees}"
@burnedTrees = 0 # reset from initial ignites
# console.log "embers: ", @embers; window.embers = @embers
# console.log "fires: ", @fires; window.fires = @fires
# console.log "turtles:", @turtles
ignite: (p) ->
p.sprout 1, @fires, (a) ->
#a.breed = "fires"
#a.color = [255, 0, 0]
p.color = [0, 0, 0] # in original model but apparently not needed, refresh off?
@burnedTrees++
fadeEmbers: ->
for a in @embers by -1 # -1: allow die() in loop
a.scaleColor a.color, .8
if 100 > Math.max a.color.subarray(0,3)... # or (Math.max a.color...) < 100 , needs parens
a.p.color = a.color
a.p.draw @contexts.patches
a.die()
step: ->
# console.log @anim.toString() if @anim.ticks % 100 is 0
unless @turtles.any()
console.log "..stopping, fire done at tick: #{@anim.ticks}"
@stop()
for a in @fires by -1 # -1: allow setBreed() in loop
@ignite p for p in a.p.n4 when u.colorsEqual p.color, [0, 255, 0]
@embers.setBreed a
@fadeEmbers()
# div, patchSize, minX, maxX, minY, maxY, isTorus, hasNeighbors
# Defaults: 13, -16, 16, -16, 16, false, true
model = new MyModel {
div: "layers",
size: 2,
minX: -125,
maxX: 125,
minY: -125,
maxY: 125
}
model.debug() # Debug: Put Model vars in global name space
model.start() # Run model immediately after startup initialization
</script>
</head>
<body>
<div id="layers"></div>
</body>
</html>