-
Notifications
You must be signed in to change notification settings - Fork 1
/
fire.ts
321 lines (256 loc) · 11.6 KB
/
fire.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
namespace forestFire {
class FireState {
period: number;
timer: number;
timeBuffer: Image;
healthBuffer: Image;
tileCache: Image[];
unburntTileCache: Image[];
fireImage: Image;
windSpeed: number;
treeHealth: number;
dryGrass: number;
fireCreatedHandlers: ((location: tiles.Location) => void)[];
fireDestroyedHandlers: ((location: tiles.Location) => void)[];
fireHasStarted: boolean;
updateHasBeenCalled: boolean;
constructor() {
// Every period (in ms) we update the fire spread. The timer tracks how
// much time is remaining in the period
this.period = 300;
this.timer = this.period;
// The time remaining is stored inside an image. Each "pixel" represents a tile
// and the "color" is the time remaining. For example, if the color is 3, the time
// remaining is 3 * this.period
this.timeBuffer = image.create(16, 16);
// Same for fire health
this.healthBuffer = image.create(16, 16);
// The tile cache is for storing tiles so that we don't create the same tile multiple times
this.tileCache = [];
this.unburntTileCache = [];
// These handlers run whenever a fire is created or destroyed
this.fireCreatedHandlers = [];
this.fireDestroyedHandlers = [];
this.windSpeed = 5;
this.treeHealth = 4;
this.dryGrass = 5;
this.fireHasStarted = false;
this.updateHasBeenCalled = false;
game.onUpdate(() => {
let burntTrees = 0;
let unburntTrees = 0;
let activeFires = 0;
for (const tile of this.unburntTileCache) {
unburntTrees += tiles.getTilesByType(tile).length;
}
for (let x = 0; x < this.timeBuffer.width; x++) {
for (let y = 0; y < this.timeBuffer.height; y++) {
if (this.timeBuffer.getPixel(x, y) === 0) continue;
else if (this.timeBuffer.getPixel(x, y) < 15) {
activeFires++;
burntTrees++;
}
else if (this.timeBuffer.getPixel(x, y) === 15) {
burntTrees ++;
}
}
}
const totalTrees = unburntTrees + burntTrees;
hud.updateForestHealth(totalTrees, unburntTrees);
hud.updateFireNumber(activeFires)
if (this.danger < 2.5) {
hud.updateDangerBarColors(7)
} else if (this.danger < 5) {
hud.updateDangerBarColors(5)
} else if (this.danger < 7.5) {
hud.updateDangerBarColors(4)
} else {
hud.updateDangerBarColors(2)
}
if (this.updateHasBeenCalled && this.fireHasStarted && activeFires == 0) {
const score = Math.round((unburntTrees / totalTrees) * 100);
info.setScore(score)
game.splash("You saved " + score + "% of the forest!");
game.over(score > 0);
}
})
}
get danger() {
return (this.windSpeed + this.dryGrass + (10 - this.treeHealth)) / 3
}
get spreadTime() {
return 4500 - (this.danger / 10) * 3500;
}
getRandomSpreadInterval() {
const spreadTime = this.spreadTime + Math.randomRange(0, 1000);
return Math.min(14, Math.idiv(spreadTime, this.period));
}
createFireAtRandomLocation(tile: Image, fire: Image) {
this.fireImage = fire;
this.createFireAtLocation(tiles.getRandomTileByType(tile), tile, fire);
}
createFireAtLocation(location: tiles.Location, tile: Image, fire: Image) {
if (location) {
this.fireHasStarted = true;
let image = tile.clone();
image.drawTransparentImage(fire, 0, 0);
let foundIt = false;
for (const tile of this.tileCache) {
if (tile.equals(image)) {
image = tile;
foundIt = true;
break;
}
}
if (!foundIt) {
this.tileCache.push(image);
this.unburntTileCache.push(tile);
}
tiles.setTileAt(location, image);
this.timeBuffer.setPixel(location.col, location.row, this.getRandomSpreadInterval());
for (const handler of this.fireCreatedHandlers) {
handler(location);
}
}
}
update() {
this.updateHasBeenCalled = true;
// First check to make sure the tilemap hasn't changed size
const tm = game.currentScene().tileMap;
const tmWidth = tm.areaWidth() >> tm.scale;
const tmHeight = tm.areaHeight() >> tm.scale;
if (this.timeBuffer.width !== tmWidth || this.timeBuffer.height !== tmHeight) {
// If it has, create a new time buffer and copy over the old data
const old = this.timeBuffer;
this.timeBuffer = image.create(tmWidth, tmHeight);
this.timeBuffer.drawTransparentImage(old, 0, 0);
const oldHealth = this.healthBuffer;
this.healthBuffer = image.create(tmWidth, tmHeight);
this.healthBuffer.drawTransparentImage(oldHealth, 0, 0);
}
// Decrement timer
this.timer -= game.currentScene().eventContext.deltaTimeMillis;
// Run the fire spread code
while (this.timer < 0) {
this.timer += this.period;
this.updateFireSpread();
}
}
updateFireSpread() {
let current = 0;
// Loop over every location in the timeBuffer and update the times
for (let x = 0; x < this.timeBuffer.width; x++) {
for (let y = 0; y < this.timeBuffer.height; y++) {
if (Math.percentChance(30 * (10 - this.danger) / 10)) continue;
current = this.timeBuffer.getPixel(x, y);
// If the value is 0, the fire hasn't started yet
// If the value is 15, the tile is already burnt
if (current > 0 && current < 15) {
// Decrement the time remaining
this.timeBuffer.setPixel(x, y, current - 1);
// If we were already on our last increment, then spread the tile
if (current === 1) {
this.timeBuffer.setPixel(x, y, this.getRandomSpreadInterval());
const tm = game.currentScene().tileMap;
// Attempt to spread the fire in a random cardinal direction.
// After ten tries, give up
for (let i = 0; i < 10; i++) {
let location: tiles.Location;
if (Math.percentChance(50)) {
location = tiles.getTileLocation(
Math.clamp(
0,
(tm.areaWidth() >> tm.scale) - 1,
x + Math.randomRange(-2, 2)
),
y
);
}
else {
location = tiles.getTileLocation(
x,
Math.clamp(
0,
(tm.areaHeight() >> tm.scale) - 1,
y + Math.randomRange(-2, 2)
)
);
}
// Once the fire has spread, stop trying
if (this.tryFireSpread(location)) break;
}
}
}
}
}
}
tryFireSpread(location: tiles.Location) {
const tileImage = tiles.getTileImage(location);
if (this.timeBuffer.getPixel(location.col, location.row)) return false;
for (const tile of this.unburntTileCache) {
if (tileImage.equals(tile)) {
this.createFireAtLocation(location, tileImage, this.fireImage)
return true;
}
}
return false;
}
onFireCreated(handler: (location: tiles.Location) => void) {
this.fireCreatedHandlers.push(handler);
}
onFireDestroyed(handler: (location: tiles.Location) => void) {
this.fireDestroyedHandlers.push(handler);
}
setFireHealth(location: tiles.Location, health: number) {
health = Math.max(0, Math.min(health, 15));
this.healthBuffer.setPixel(location.col, location.row, health);
if (health === 0) {
// Remove the tile from the tracked fires
this.timeBuffer.setPixel(location.col, location.row, 15);
for (const handler of this.fireDestroyedHandlers) {
handler(location);
}
// // Restore the unburnt tile image
// const currentTile = tiles.getTileImage(location);
// for (let i = 0; i < this.tileCache.length; i++) {
// if (this.tileCache[i].equals(currentTile)) {
// tiles.setTileAt(location, this.unburntTileCache[i]);
// break;
// }
// }
}
}
changeFireHealth(location: tiles.Location, changeBy: number) {
const currentHealth = this.healthBuffer.getPixel(location.col, location.row);
this.setFireHealth(location, currentHealth + changeBy);
}
}
const state = new FireState();
export function createFireAtRandomLocation(tile: Image, fire: Image) {
state.createFireAtRandomLocation(tile, fire);
}
export function setWindSpeed(speed: number) {
state.windSpeed = speed;
}
export function setDryGrass(grass: number) {
state.dryGrass = grass;
}
export function setTreeHealth(treeHealth: number) {
state.treeHealth = treeHealth;
}
export function setFireHealth(location: tiles.Location, health: number) {
state.setFireHealth(location, health);
}
export function changeFireHealth(location: tiles.Location, changeBy: number) {
state.changeFireHealth(location, changeBy);
}
export function updateFireSpread() {
state.update();
}
export function onFireDestroyed(handler: (location: tiles.Location) => void) {
state.onFireDestroyed(handler);
}
export function onFireCreated(handler: (location: tiles.Location) => void) {
state.onFireCreated(handler);
}
}