-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.js
1908 lines (1423 loc) · 53.3 KB
/
tile.js
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Tile(type, r, c) {
this.type = type;
this.nextType = type;
this.previousType = type;
this.r = r;
this.c = c;
this.col = 0;
this.actions = [];
//stage is a reusable variable to time a type change
this.stage = 0;
this.nextStage = -1;
this.previousStage = -1;
this.frame = 0;
this.turn = 0;
this.turns = 0;
this.spill = 0;
//state is a reusable variable string
this.state = "";
this.baseSprite = null;
this.transition = "";
this.transitionFrame = 0;
this.visible = false;
this.draw = function() {
var y = 0;
if (this.visible) {
if (this.baseSprite != null) {
image(tileGraphics[this.baseSprite], 0, 0, TILE, TILE);
if (this.type == GREEN_CUBE || this.type == BLUE_CUBE || this.type == STONE_CUBE || this.type == GLITCH_CUBE || this.type == BLACK_CUBE || this.type == WHITE_CUBE)
y = floor(map(sin(time), 0, 1, 10, 20));
}
if (this.unglitchedSprite != null) {
image(tileGraphics[this.unglitchedSprite], 0, 0, TILE, TILE);
}
//figure this out for preview
var action = "";
//it meeee
if (this == currentTile && currentCube != null) {
action = ecology.getRow(this.type).getString(id[currentCube.id]);
}
//draw the actual tile
if (action == "" && this.sprite != "") {
if (this.frame >= this.sprite.length)
this.frame = 0;
//case 1: no transition -> draw the tile
//also fps is lagging
if (this.transition == "") {
this.transitionFrame = TRANSITION_FRAMES;
image(tileGraphics[this.sprite[this.frame]], 0, -y, TILE, TILE);
} else {
//there is a transition
//case 2: the old type is tall and had a transition -> draw the new first and fade the old out out
if (this.previousTall && this.previousTransition != "") {
image(tileGraphics[this.sprite[this.frame]], 0, -y, TILE, TILE);
if (this.transitionFrame < TRANSITION_FRAMES && this.previousSprite != null) {
var frame = transitions[this.previousTransition].getImageAt(TRANSITION_FRAMES - 1 - this.transitionFrame);
//old one to be masked
var maskedSprite = this.previousSprite.get();
maskedSprite.mask(frame);
image(maskedSprite, 0, -y, TILE, TILE);
this.transitionFrame++; // -= deltaTime / TURN;
}
} //case 3: the new type is tall (or both short) -> draw the old first and fade in
else {
//transition in progress
if (this.transitionFrame < TRANSITION_FRAMES && this.previousSprite != null && this.transition != null) {
//draw previous first
image(this.previousSprite, 0, -y, TILE, TILE);
//var f = floor(map(this.transition, 1, 0, 0, transition.getLastFrame()));
if (transitions[this.transition] == null) {
return;
}
var frame = transitions[this.transition].getImageAt(this.transitionFrame);
//new one to be masked
var maskedSprite = tileGraphics[this.sprite[this.frame]].get();
maskedSprite.mask(frame);
image(maskedSprite, 0, -y, TILE, TILE);
this.transitionFrame++; // -= deltaTime / TURN;
} else {
//end of transition draw normal
image(tileGraphics[this.sprite[this.frame]], 0, -y, TILE, TILE);
}
}
} //end transition
if (this == currentTile && currentCube != null) {
tint(0, 80); //
image(tileGraphics[currentCube.sprite], 0, -y, TILE, TILE);
//blend(tileGraphics[currentCube.sprite], 0, 0, TILE, TILE, 0, 0, TILE, TILE, SCREEN);
}
} else {
//if valid position draw cube on the top
if (action != "") {
image(tileGraphics[currentCube.sprite], 0, 0, TILE, TILE);
}
}
} else {
if (random(100) < 50) {
//invisible make it appear organically
var n = this.neighbors8[floor(random(0, this.neighbors8.length))];
if (n.visible)
this.visible = true;
}
}
if (this.dissolve > 0) {
animation(dissolve, TILE_W / 2, TILE_H);
this.dissolve--;
}
} //end draw
this.changeType = function(_t) {
this.previousType = this.type;
this.type = _t;
this.nextType = _t;
this.turn = 0;
this.turns = ecology.getRow(_t).getNum("time");
var action = ecology.getRow(_t).getString("evolution");
this.actions = split(action, ",");
if (this.sprite[this.frame] != null)
this.previousSprite = tileGraphics[this.sprite[this.frame]]; //make a copy of the img
this.transitionFrame = 0;
//flag for transition drawing order
this.previousTall = this.tall;
this.previousTransition = this.transition;
this.tall = (this.type == WATER || this.type == FOREST || this.type == BURNED_FOREST || this.type == CITY || this.type == CONCRETE || this.type == RUIN);
this.transition = ecology.getRow(_t).getString("transition");
var spr = ecology.getRow(_t).getString("sprite").split(",");
if (spr.length == 1) {
this.sprite = [];
this.sprite[0] = spr[0];
this.frame = 0;
} else if (this.nextFrame != null) {
this.sprite = spr;
this.frame = this.nextFrame;
} else {
this.sprite = spr;
this.frame = floor(random(0, spr.length));
}
this.id = id[_t];
//if moving toward a path change
this.path = null;
//ugly exception
if (this.previousType != OIL) {
var fx = ecology.getRow(_t).getString("sound");
if (!this.isOffScreen())
randomSound(fx, 1);
else
randomSound(fx, VOLUME_OFFSCREEN);
}
}
this.stateChange = function() {
if (this.nextType != this.type) {
this.changeType(this.nextType);
this.previousStage = this.stage;
//stage can be passed
if (this.nextStage != -1) {
this.stage = this.nextStage;
this.nextStage = -1;
} else
this.stage = 0;
}
}
this.evolve = function() {
if (this.actions != null)
for (var a = 0; a < this.actions.length; a++) {
switch (this.actions[a]) {
//spread in 4 directions
case "spread4":
if (this.N != null)
this.N.receive(this.type);
if (this.S != null)
this.S.receive(this.type);
if (this.W != null)
this.W.receive(this.type);
if (this.E != null)
this.E.receive(this.type);
break;
//spread in one random direction
case "spread1":
var n = this.neighbors4[floor(random(0, this.neighbors4.length))];
n.receive(this.type);
break;
//spread in 2 non repeated random directions
case "spread2":
shuffle(this.neighbors4, true);
if (this.neighbors4.length > 0) {
var n = this.neighbors4[shuffle4[0]];
n.receive(this.type);
}
if (this.neighbors4.length > 1) {
n = this.neighbors4[shuffle4[1]];
n.receive(this.type);
}
break;
case "spread3":
shuffle(this.neighbors4, true);
if (this.neighbors4.length > 0) {
var n = this.neighbors4[0];
n.receive(this.type);
}
if (this.neighbors4.length > 1) {
n = this.neighbors4[shuffle4[1]];
n.receive(this.type);
}
if (this.neighbors4.length > 2) {
n = this.neighbors4[shuffle4[2]];
n.receive(this.type);
}
break;
case "extinguish":
this.stage++;
if (this.stage >= 8) {
if (this.previousType == FOREST) {
this.nextType = BURNED_FOREST;
} else if (this.previousType == RUIN) {
plasticCounter++;
if (plasticCounter % PLASTIC_FREQUENCY == 0 && plastiglomerates < 16 && turns > 500) {
this.nextType = PLASTIC;
if (plastiglomerates == 0)
storyMessage(story.plastigromerate);
//the sequence is shuffled
var index = plasticSequence[plastiglomerates];
//create an area around the plastic
var area = this.getNeighbors8Except(PLASTIC, WATER, OIL);
for (var i = 0; i < area.length; i++) {
area[i].nextType = GRASS;
}
this.addMarker(plastics[index]);
this.nextFrame = index;
plastiglomerates++;
} else
this.nextType = BURNED;
} else {
this.nextType = BURNED;
}
}
break;
//black cube slowly destroy stuff around it
case "annihilate":
if (this.stage % ANNIHILATION == 0 && this.stage < ANNIHILATION_SIZE) {
var targets = getCircle(this.c, this.r, this.stage / ANNIHILATION);
if (!this.isOffScreen())
diegeticSound(sounds.annihilation, 2);
else
diegeticSound(sounds.annihilation, 1);
for (var i = 0; i < targets.length; i++) {
for (var j = 0; j < targets[i].neighbors4.length; j++) {
var t = targets[i].neighbors4[j];
if (t.type != BLACK_CUBE && t.type != GREEN_CUBE && t.type != BLUE_CUBE && t.type != STONE_CUBE && t.type != GLITCH_CUBE && t.type != PLASTIC) {
if (t.type == FOREST || t.type == GRASS)
t.nextType = FIRE;
else
t.nextType = BLACK_BURN;
}
}
}
}
//remove
if (this.stage > ANNIHILATION_SIZE) {
this.nextType = BLACK_BURN;
this.baseSprite = null;
//this.dissolveCube();
}
this.stage++;
break;
//blackburn turns into wasteland
case "fade":
this.stage++;
if (this.stage > 20 && random(0, 100) < 10) {
this.nextType = WASTELAND;
}
break;
//burned ground regrows
case "regrowth":
this.stage++;
if (this.stage > random(REGROWTH_TIME, REGROWTH_TIME * 2))
this.nextType = GRASS;
break;
case "regrowthForest":
this.stage++;
if (this.stage > random(REGROWTH_TIME, REGROWTH_TIME * 2))
this.nextType = BURNED;
break;
//water spills
case "spill":
//spill is accumulation
if (this.spill > 0) {
//get non water
var goodN = this.getNeighbors8Except(BLUE_CUBE, GREEN_CUBE);
if (goodN.length > 0) {
var dest = goodN[floor(random(0, goodN.length))];
//not water -> turn into water
if (dest.type != WATER && dest.nextType != WATER) {
dest.nextType = WATER;
this.spill--;
if (this.movedWater != "") {
dest.movedWater = this.movedWater;
}
} else if (dest.spill == 0) {
if (dest.nextType != WATER)
dest.spill = 1;
dest.spill = 1;
this.spill--;
if (this.movedWater != "") {
dest.movedWater = this.movedWater;
}
}
}
}
break;
case "flooding":
if (floodCounter > 0) {
var endangered = this.getNeighbors8Except(WATER, FLOOD, WETLAND);
if (endangered.length > 0) {
if (random(FLOOD_DURATION) < FLOOD_SEVERITY) {
var tgt = endangered[floor(random(0, endangered.length))];
if (this.getNeighbors8Except(WATER).length > 4 || seaLevelRise < SEA_RISE || seaLevelRise > SEA_RISE + 2) //flood is temporary
tgt.nextType = FLOOD;
else
tgt.nextType = WATER;
}
}
}
break;
//grass grows a bit
case "hurricane":
if (floodCounter > 0) {
if (floor(random(60)) < this.getNeighbors8(GRASS, FOREST).length) //less green neigh -> more
this.nextType = GRASS;
}
break;
case "dry":
this.stage++;
if (this.stage > FLOOD_DURATION) {
//city leaves crap
if (this.previousType == CITY || this.previousType == CONCRETE) {
this.nextType = CONCRETE;
} else
this.nextType = this.previousType;
}
break;
case "spring":
var goodN = this.getNeighbors8(WATER);
if (goodN.length > 0) {
var dest = goodN[floor(random(0, goodN.length))];
if (dest.stage == 0)
dest.stage = 1;
}
break;
//blue cube finds the closest water and brings it next to it
case "soak":
var SOAK_SPEED = 6;
//unfortunately I can't use the turns because it messes the framerate
this.stage++;
if (this.stage % SOAK_SPEED == 0) {
//this.stage = 0;
var goodN = [];
var trySelf = false;
//if no path is defined, find the closest destination
if (this.path == null) {
//makeGrid with all water sources and oil which will be purified
var terrainGrid = makeGrid([], [WATER, OIL]);
//remove the waters that have not been already moved by this cube
for (var i = 0; i < ROWS; i++) {
for (var j = 0; j < COLS; j++) {
if (terrain[i][j].movedWater == (this.r + "-" + this.c))
//not a goal then
terrainGrid[i][j] = 0;
}
}
this.path = findShortestPath([this.r, this.c], terrainGrid, true);
}
//find the actual tile
if (this.path != false) {
var closestWater = pathToTile(this.r, this.c, this.path)
if (closestWater != null) {
//start with the tile underneath
if (trySelf) {
this.previousType = WATER;
closestWater.nextType = WASTELAND;
this.path = null;
} else { //otherwise find a closeby one
var goodN = this.getNeighbors8Except(WATER, GREEN_CUBE);
//flood non waters
if (goodN.length > 0) {
var dest = goodN[floor(random(0, goodN.length))];
dest.nextType = WATER;
dest.spill = closestWater.spill;
closestWater.nextType = WASTELAND;
this.path = null;
dest.movedWater = (this.r + "-" + this.c);
} //try spill
else { //flood waters, they will spill
var goodN = this.getNeighbors8(WATER);
if (goodN.length > 0) {
var dest = goodN[floor(random(0, goodN.length))];
//if(dest.stage == 0) {
if (dest.spill == 0) {
dest.spill = 1;
closestWater.nextType = WASTELAND;
this.path = null;
dest.movedWater = (this.r + "-" + this.c);
} //else no success
}
}
}
//
}
}
}
if (this.stage > SOAK_SPEED * 20) {
this.nextType = WATER;
this.baseSprite = null;
this.dissolveCube();
}
break;
//green cube keeps growing weed
case "growWeed":
this.stage++;
if (this.stage % WEED_POWER == 0) {
var n = this.neighbors4[floor(random(0, this.neighbors4.length))];
var taken = n.receive(WEED);
if (taken) {
n.nextStage = WEED_POWER; //random(4, 8));
}
}
if (this.stage > WEED_POWER * 10) {
this.nextType = GRASS;
this.baseSprite = null;
this.dissolveCube(); //trigger anim
}
break;
//inert weed turns to grass slowly
case "weedToGrass":
if (this.stage == -1) {
if (random(100) < 5)
this.nextType = GRASS;
}
break;
case "spreadWeed":
if (this.stage > 1) {
//take ruins first
var con = this.getNeighbors8(CONCRETE);
if (con.length > 0) {
var c = con[floor(random(0, con.length))];
c.nextStage = this.stage - 1;
c.nextType = RUIN;
//this.nextStage = 1;
} else {
//spread
//check weed neigh
var n = this.neighbors4[floor(random(0, this.neighbors4.length))];
//check it's spreading out / not going back
if (n.getNeighbors4(WEED).length <= 1) {
var taken = n.receive(WEED);
//pass stage
if (taken) {
n.nextStage = this.stage - 1;
//sticks more on city and dead city
//if (n.type != CONCRETE && n.type != CITY)
//this.nextStage = 1;
}
}
}
}
break;
case "wetlandDeath":
var goodN = this.getNeighbors8(WATER);
if (goodN.length <= 0) {
this.nextType = WASTELAND;
}
break;
//wetland grows on coast
case "growCoast":
if (this.stage < WETLAND_STRENGTH) {
var goodN = this.getNeighbors8Except(WATER, WETLAND, CITY);
if (goodN.length > 0) {
//get one an check if coastal
var n = goodN[floor(random(0, goodN.length))];
var nN = n.getNeighbors8(WATER);
if (nN.length >= 1) {
this.stage++;
n.nextType = WETLAND;
n.nextStage = this.stage + 1;
}
}
}
break;
//UNUSED: edge spreads on the tiles that are the same type AND are on an edge
case "spreadEdge":
//<6 still looking to spread
if (this.stage < 6) {
//current type
var cType = terrain[this.r][this.c].previousType;
//find edge type
var eType;
for (var n = 0; n < this.neighbors4.length; n++) {
if (this.neighbors4[n].type != cType && this.neighbors4[n].type != EDGE) {
eType = this.neighbors4[n].type;
}
}
//there is an edge
if (eType != null) {
//find all the n of the current type
var goodN = this.getNeighbors8(cType);
//check if they have the same edge
var edgeN = [];
for (var n = 0; n < goodN.length; n++) {
var nN;
nN = goodN[n].getNeighbors4Except(cType, EDGE);
//they do
if (nN.length > 0) {
edgeN.push(goodN[n]);
}
}
//print((this.stage+1)+"stage");
for (var n = 0; n < edgeN.length; n++) {
edgeN[n].nextType = EDGE;
edgeN[n].nextStage = this.stage + edgeN.length;
}
this.stage = 10;
} //no edge
else {
//wither outside edge
if (this.stage < 0) {
this.stage--;
if (this.stage < -3)
this.nextType = this.previousType;
}
if (this.path == null) {
//find any terrain that is not this one
var dest = [];
for (var d = 0; d < id.length; d++)
if (d != EDGE && d != this.previousType)
dest.push(d);
var terrainGrid = makeGrid([terrain[this.r][this.c].previousType], dest);
this.path = findShortestPath([this.r, this.c], terrainGrid);
this.stage = -1;
} else {
//there is a path, follow
//there is a path make next move
var move = this.path[0];
if (move == "North")
this.N.receive(this.type);
if (move == "South")
this.S.receive(this.type);
if (move == "West")
this.W.receive(this.type);
if (move == "East")
this.E.receive(this.type);
//move
if (this.path.length > 1) {
this.path.shift();
//disable pathfinding here
//this.stage = 10;
}
}
}
}
break;
case "random":
this.frame += floor(random(1, this.sprite.length - 1));
this.frame = this.frame % this.sprite.length;
break;
case "glitchEvolution":
if (!glitchPreview) {
this.frame = floor(random(0, this.sprite.length));
} else {
this.frame = turns % 3;
}
if (this.contagion != null) {
//wait
var n = this.getNeighbors8(GLITCH_CITY);
//spread to the next glitches
for (var i = 0; i < n.length; i++) {
//already contracted
n[i].contagion = this.contagion;
}
//change this
this.nextType = this.contagion;
this.contagion = null;
}
//don't disappear before it moves
if (this.stage >= 0) {
this.stage++;
}
if (this.stage >= GLITCH_DURATION) {
//transform
if (this.previousType == WASTELAND)
this.nextType = GRASS;
else if (this.previousType == GRASS || this.previousType == WEED)
this.nextType = FOREST;
else if (this.previousType == CONCRETE || this.previousType == RUIN)
this.nextType = CITY;
else if (this.previousType == WATER)
this.nextType = GRASS;
else if (this.previousType == OIL)
this.nextType = WATER;
else
this.nextType = WASTELAND;
this.baseSprite = null;
}
break;
case "glitchAttraction":
if (cubes.white.tile != null) { // && this.stuck != -1) {
if (random(100) < 60) //speed
{
//find next position
var nc = this.c;
var nr = this.r;
if (this.c < cubes.white.tile.c)
nc++;
else if (this.c > cubes.white.tile.c)
nc--;
if (this.r < cubes.white.tile.r)
nr++;
else if (this.r > cubes.white.tile.r)
nr--;
if (nc != this.c || nr != this.r) {
if (terrain[nr][nc].nextType != GLITCH_CITY && terrain[nr][nc].contagion == null) {
var taken = terrain[nr][nc].receive(GLITCH_CITY);
if (taken & this.stage < 0) {
//first move start disappearing
this.stage = 0;
}
}
}
}
}
break;
//white cube disappears if touched
case "touched":
if (this.getNeighbors4(GLITCH_CITY).length > 0) {
this.nextType = GLITCH_CITY;
this.baseSprite = null;
} else if (this.getNeighbors4(CITY).length > 0) {
this.nextType = CITY;
this.life = CITY_RESILIENCE;
this.baseSprite = null;
}
break;
//alternates between previous state and glitch
case "glitch":
if (random(100) < 50) {
if (this.unglitchedSprite != null)
this.sprite = this.unglitchedSprite;
this.frame = 0;
} else {
if (!this.isOffScreen())
randomSound("glitch", 1);
else
randomSound("glitch", VOLUME_OFFSCREEN);
this.sprite = ecology.getRow(GLITCH_CITY).getString("sprite").split(",");
this.frame += floor(random(1, this.sprite.length - 1));
this.frame = this.frame % this.sprite.length;
}
break;
case "cycle":
if (this.f == null) {
this.f = floor(random(0, this.sprite.length));
//this.f = 0;
this.frame = this.f;
}
this.frame++;
if (this.frame >= this.sprite.length)
this.frame = 0;
break;
//concrete turns into ruins if surrounded by grass forest etc
case "rewild":
var livingN = this.getNeighbors8(WEED, GRASS, FOREST, RUIN);
if (livingN.length >= 4) {
this.nextType = RUIN;
}
break;
//concrete turns into city if surrounded
case "renovate":
if (resources > 0) {
var livingN = this.getNeighbors8(CITY, ROAD);
if (livingN.length > 4) {
this.nextType = CITY;
}
}
break;
//grass turns into forest if surrounded by grass
case "grow":
var score = 0;
for (var n = 0; n < this.neighbors8.length; n++) {
var neigh = this.neighbors8[n];
if (neigh.type == GRASS)
score++;
//weed cube proximity = big bonus for trees but not for oasification
if (neigh.type == WEED || neigh.type == GREEN_CUBE)
score += 3;
if (neigh.type == WASTELAND || neigh.type == CONCRETE)
score--;
}
if (score > FOREST_LEVELS) {
this.stage++;
}
if (this.stage > random(10, 30)) {
this.nextType = FOREST;
}
break;
case "deforest":
var score = 0;
for (var n = 0; n < this.neighbors8.length; n++) {
var neigh = this.neighbors8[n];
if (neigh.type == FOREST)
score += 2;
if (neigh.type == GRASS)
score++;
//allow for parks
if (neigh.type == CITY)
score++;
//grass is neutral
if (neigh.type == WASTELAND || neigh.type == CONCRETE)
score--;
}
if (score < DEFOREST_LEVELS) {
this.stage--;
}
if (this.stage < random(-10, -30)) {
this.nextType = GRASS;
}
break;
//wasteland is taken over if next to 2+ grass
case "oasify":
var score = 0;
for (var n = 0; n < this.neighbors8.length; n++) {
var neigh = this.neighbors8[n];
//clean water
if (neigh.type == WATER)
score += 4;
if (neigh.type == WASTELAND || neigh.type == CONCRETE)
score--;
}
if (score > OASIFY_LEVELS) {
this.stage++;
}
if (score > random(5, 10))
this.nextType = GRASS;
break;
//grass desertify edges with heat wave, too subtle add visual feedback?
case "heatWave":
if (heatWaveCounter > 0 && this.previousType != WASTELAND) {
var n = this.getNeighbors4(WASTELAND);
if (n.length > 2) {
var r = random(HEAT_WAVE_DURATION);
if (r < HEAT_WAVE_SEVERITY / 20) {
this.nextType = WASTELAND;
this.heatEffect();
}
}
}
break;
//desertify water with heat wave, too subtle?
case "drought":
if (heatWaveCounter > 0) {
var n = this.getNeighbors4(WATER, WETLAND);
if (n.length <= 2)