forked from R74nCom/sandboxels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lite.html
1600 lines (1505 loc) · 61.8 KB
/
lite.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
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
<html>
<head>
<meta charset="utf-8">
<title>Sandboxels Lite</title>
<meta charset="utf-8">
<meta name="description" content="A falling sand simulation game.">
<meta name="keywords" content="falling sand, elements, pixel art, simulator, powder">
<meta name="author" content="R74n">
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://R74n.com/load.js"></script>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
font-family: 'Press Start 2P';
background-color: #000000;
color: #ffffff;
}
h1 {
padding: 10px;
}
a {
color: rgb(255, 0, 255);
text-decoration: none;
}
a:hover {
color: rgb(255, 121, 255);
}
a:active,
a:hover:active {
color: rgb(255, 179, 255);
}
#gameDiv {
/*game canvas*/
border: 1px solid #ffffff;
position: absolute;
left: 50%;
transform: translate(-50%, -0%);
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-user-select: none;
/* Safari */
-khtml-user-select: none;
/* Konqueror HTML */
-moz-user-select: none;
/* Old versions of Firefox */
-ms-user-select: none;
/* Internet Explorer/Edge */
user-select: none;
}
button,
input[type="submit"],
input[type="reset"] {
background: none;
color: inherit;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
}
#underBox {
position: absolute;
left: 50%;
transform: translate(-50%, -0%);
margin-top: 10px;
width: 100%;
}
#controls button {
padding: 5px 10px;
border-radius: 5px;
font-size: 1em;
text-shadow: 0.5px 1px 4px #000000;
color: rgba(255, 255, 255, 0.75);
border: 1px solid #797979;
margin: 0px 5px 5px 5px;
font-variant: small-caps;
}
#controls button.bright {
text-shadow: 0.5px 1px 4px #ffffff;
color: rgba(0, 0, 0, 0.75);
}
/*Darken when active*/
#controls button:active,
#controls button:active:hover {
filter: brightness(60%);
}
#controls button:hover {
filter: brightness(90%);
}
#controls button:disabled {
cursor: not-allowed;
}
#controls button[current="true"],
#controls button[on="true"] {
border: 1px solid #ffffff;
filter: brightness(110%);
box-shadow: 0 5px 15px rgba(255, 255, 255, .4);
color: rgba(255, 255, 255, 1);
}
#controls button.bright[current="true"] {
color: rgba(0, 0, 0, 1);
}
#controls button[on="true"] {
border-color: lime;
color: lime;
}
#controls div {
display: block;
}
.stat {
margin-right: 25px;
margin-bottom: 5px;
float: right;
}
#stats {
margin: 0px 5px 5px 5px;
font-size: 0.75em;
height: 2em;
}
#stat-pos,
#stat-pixels,
#stat-shift,
#stat-tps,
#stat-ticks {
float: left;
}
.categoryName {
font-size: 0.75em;
text-transform: uppercase;
margin-left: 5px;
vertical-align: middle;
}
#extraInfo {
margin: 5px
}
</style>
<script>
//replaceAll polyfill
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (str, newStr) {
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
return this.replace(new RegExp(str, 'g'), newStr);
};
}
</script>
<script>
behaviors = {
"POWDER": [
"XX|XX|XX",
"XX|XX|XX",
"M2|M1|M2",
],
"AGPOWDER": [
"M2|M1|M2",
"XX|XX|XX",
"XX|XX|XX",
],
"LIQUID": [
"XX|XX|XX",
"M2|XX|M2",
"M1|M1|M1",
],
"AGLIQUID": [
"M1|M1|M1",
"M2|XX|M2",
"XX|XX|XX",
],
"WALL": [
"XX|XX|XX",
"XX|XX|XX",
"XX|XX|XX",
],
"UL_UR": [
"M1|M1|M1",
"M2|XX|M2",
"XX|XX|XX",
],
"GAS": [
"M2|M1|M2",
"M1|XX|M1",
"M2|M1|M2",
],
"DGAS": [
"M2|M1|M2",
"M1|DL%5|M1",
"M2|M1|M2",
],
"SUPPORT": [
"XX|XX|XX",
"SP|XX|SP",
"XX|M1|XX",
],
"SUPPORTPOWDER": [
"XX|XX|XX",
"SP|XX|SP",
"M2|M1|M2",
],
"DELETE": [
"XX|DL|XX",
"DL|XX|DL",
"XX|DL|XX",
],
"FILL": [
"XX|CL|XX",
"CL|XX|CL",
"XX|CL|XX",
],
"STURDYPOWDER": [
"XX|XX|XX",
"XX|XX|XX",
"XX|M1|XX",
],
"SELFDELETE": [
"XX|XX|XX",
"XX|DL|XX",
"XX|XX|XX",
],
"FOAM": [
"SW:water%10|SW:water%10|SW:water%10",
"XX|DL%5|XX",
"M2%25|M1%25|M2%25",
],
"BUBBLE": [
"SW:water%25|SW:water%25|SW:water%25",
"M1%5|DL%0.25|M1%5",
"M1%2|M1%1|M1%2",
],
"STICKY": [
"XX|ST|XX",
"ST|XX|ST",
"XX|ST AND M1|XX",
],
"MOLTEN": [
"XX|CR:fire%2.5|XX",
"M2|XX|M2",
"M1|M1|M1",
],
}
eLists = {
"ANIMAL": ["flea", "ant", "fly", "firefly", "bee", "frog", "fish", "worm", "termite", "rat"],
"CLEANANIMAL": ["ant", "firefly", "bee", "frog", "fish"],
}
airDensity = 1.225; // kg/m^3
airTemp = 20; // Celsius
// Object storing various powders, called elements
// name - name of the element
// color - color of the element's pixel
// density - density of the element [unused currently] (kg/m^3)
// behavior - behavior of the element
// flam - temperature of combustion [unused currently]
// temp - default temperature of the element (Celsius)
// tempHigh - highest temperature before state change
// tempLow - lowest temperature before state change
// stateHigh - element transformed into when tempHigh is reached
// stateLow - element transformed into when tempLow is reached
// viscosity - how slow a liquid will move (higher = slower) (cps)
// [Future]
// changeAfter - seconds before element dissolves
// change - element transformed into after time
elements = {
"heat": {
"name": "heat",
"color": "#ff0000",
"behavior": behaviors.WALL,
"temp": 2,
"category": "tools",
},
"cool": {
"name": "cool",
"color": "#0000ff",
"behavior": behaviors.WALL,
"temp": -2,
"category": "tools",
},
"erase": {
"name": "erase",
"color": "#ff00ff",
"behavior": behaviors.WALL,
"category": "tools",
},
"sand": {
"name": "sand",
"color": "#ffff00",
"behavior": behaviors.POWDER,
"density": 1602,
"category": "land",
},
"water": {
"name": "water",
"color": "#0055ff",
"behavior": behaviors.LIQUID,
"density": 997,
"tempHigh": 100,
"stateHigh": "steam",
"tempLow": 0,
"stateLow": "ice",
"viscosity": 1,
"category": "liquids",
},
"dirt": {
"name": "dirt",
"color": "#555500",
"behavior": behaviors.POWDER,
"category": "land",
},
"grass": {
"name": "grass",
"color": "#00ff00",
"behavior": behaviors.WALL,
"category": "land",
},
"wall": {
"name": "wall",
"color": "#3a3a3a",
"behavior": behaviors.WALL,
"density": 0,
"category": "solids",
},
"steam": {
"name": "steam",
"color": "#70fff5",
"behavior": behaviors.GAS,
"density": 0.013,
"temp": 100,
"stateLow": "water",
"category": "gases",
},
"ice": {
"name": "ice",
"color": "#00a6ff",
"behavior": behaviors.WALL,
"density": 917,
"temp": 0,
"tempHigh": 5,
"stateHigh": "water",
"category": "solids",
},
"snow": {
"name": "snow",
"color": "#ffffff",
"behavior": behaviors.POWDER,
"density": 100,
"temp": 0,
"tempHigh": 5,
"stateHigh": "water",
"category": "land",
},
"wood": {
"name": "wood",
"color": "#5b3a00",
"behavior": behaviors.WALL,
"density": 745,
"category": "solids",
},
}
// Loop through behaviors and each behavior, if it is a string, split the items and replace the value with the array
for (var behavior in behaviors) {
if (typeof behaviors[behavior][0] === "string") {
var newbehavior = [];
for (var i = 0; i < behaviors[behavior].length; i++) {
newbehavior.push(behaviors[behavior][i].split("|"));
}
behaviors[behavior] = newbehavior;
}
}
// Loop through each element. If it has a tempHigh, but not a stateHigh, create a new molten element
for (element in elements) {
if (elements[element].tempHigh && !elements[element].stateHigh) {
var newname = "molten_" + element;
elements[element].stateHigh = newname;
elements[newname] = {
"name": newname.replaceAll("_", " "),
"color": ["#ff6f00", "#ff8c00", "#ff4d00"],
"behavior": behaviors.MOLTEN,
"density": elements[element].density / 2,
"temp": elements[element].tempHigh,
"tempLow": elements[element].tempHigh - 100,
"stateLow": element,
"viscosity": 10000,
"hidden": true,
}
}
if (elements[element].behavior && typeof elements[element].behavior[0] === "string") {
var newbehavior = [];
for (var i = 0; i < elements[element].behavior.length; i++) {
newbehavior.push(elements[element].behavior[i].split("|"));
}
elements[element].behavior = newbehavior;
}
}
function hexToRGB(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
// convert every color in the elements object to rgb
for (var key in elements) {
if (elements.hasOwnProperty(key)) {
// if the color is an array, loop over each one
if (elements[key].color instanceof Array) {
var rgbs = [];
var rgbos = [];
for (var i = 0; i < elements[key].color.length; i++) {
var c = elements[key].color[i];
if (c.startsWith("#")) {
var rgb = hexToRGB(c);
rgbs.push("rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")");
rgbos.push(rgb);
} else {
rgbs.push(c);
}
}
elements[key].color = rgbs;
elements[key].colorObject = rgbos;
} else {
// if elements[key].color starts with #
if (elements[key].color.startsWith("#")) {
var rgb = hexToRGB(elements[key].color);
elements[key].color = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")";
elements[key].colorObject = rgb;
}
}
}
}
currentPixels = [];
currentID = 0;
// Pixel class, with attributes such as x, y, and element
class Pixel {
constructor(x, y, element) {
this.x = x;
this.y = y;
this.element = element;
var elementInfo = elements[element];
this.color = pixelColorPick(this);
// If element doesn't have temp attribute, set temp to airTemp
if (elementInfo.temp == undefined) {
this.temp = airTemp;
} else {
this.temp = elementInfo.temp;
}
this.start = pixelTicks;
this.id = currentID;
if (elementInfo.burning) {
this.burning = true;
this.burnStart = pixelTicks;
} else {
this.burning = false;
}
currentID++;
pixelMap[x][y] = this;
}
}
pixelSize = 10;
function outOfBounds(x, y) {
// Returns true if the pixel is out of bounds
return y > height - 1 || y < 1 || x > width - 1 || x < 1
}
function isEmpty(x, y, ignoreBounds = false) {
if (outOfBounds(x, y)) {
return ignoreBounds;
}
return pixelMap[x][y] == undefined;
}
function canMove(pixel, x, y) {
if (isEmpty(x, y)) {
return true;
}
}
function movePixel(pixel, x, y, leaveBehind = null) {
// if the pixel isn't in currentPixels, return
if (!currentPixels.includes(pixel)) {
return;
}
// Delete the pixel from the old position
delete pixelMap[pixel.x][pixel.y];
if (leaveBehind != null && isEmpty(pixel.x, pixel.y)) {
createPixel(leaveBehind, pixel.x, pixel.y);
}
pixel.x = x;
pixel.y = y;
pixelMap[x][y] = pixel;
}
function clonePixel(pixel, x, y) {
currentPixels.push(new Pixel(x, y, pixel.element));
}
function createPixel(element, x, y) {
currentPixels.push(new Pixel(x, y, element));
}
function deletePixel(x, y, id = null) {
delete pixelMap[x][y];
if (id != null) {
for (var i = 0; i < currentPixels.length; i++) {
if (currentPixels[i].id == id) {
currentPixels.splice(i, 1);
return;
}
}
}
for (var i = 0; i < currentPixels.length; i++) {
if (currentPixels[i].x == x && currentPixels[i].y == y) {
currentPixels.splice(i, 1);
break;
}
}
}
function swapPixels(pixel1, pixel2) {
var tempX = pixel1.x;
var tempY = pixel1.y;
pixel1.x = pixel2.x;
pixel1.y = pixel2.y;
pixel2.x = tempX;
pixel2.y = tempY;
pixelMap[pixel1.x][pixel1.y] = pixel1;
pixelMap[pixel2.x][pixel2.y] = pixel2;
}
function behaviorCoords(x, y, bx, by) {
bx -= 1;
by -= 1;
x += bx;
y += by;
return {
x: x,
y: y
};
}
/* New Behavior Example (Sand)
[
["XX","XX","XX"],
["XX","XX","XX"],
["M2","M1","M2"]
] */
/* Behavior Rules
XX = Ignore
M1 = Move (First Priority)
M2 = Move (Second Priority)
SP = Support (Doesn't move if all aren't empty)
DL = Delete
CL = Clone
CH = Change
RP = Replace (Move to an existing element)
CR:element_name = Create a pixel of element_name
LB:element_name = Leave behind a pixel of element_name when moved (Must be center cell)
%(num) = Chance of rule happening
L1:element_name = Leave behind only on M1 moves
L2:element_name = Leave behind only on M2 moves
SW = Swap
HT = Heat
CO = Cool
CC = Change Color (Hexadecimal)
ST = Stick
*/
function pixelTick(pixel) {
if (pixel.start == pixelTicks) {
return
}
var info = elements[pixel.element];
var behavior = info.behavior;
var x = pixel.x;
var y = pixel.y;
var move1Spots = [];
var move2Spots = [];
var supportSpots = [];
var replaceSpots = [];
var swapSpots = [];
var sticking = false;
var deleted = false;
var leaveBehind = null;
var leaveBehind1 = null;
var leaveBehind2 = null;
// Parse behavior
for (var by = 0; by < behavior.length; by++) {
for (var bx = 0; bx < behavior[by].length; bx++) {
var b0 = behavior[by][bx].trim();
//if (b.includes(" OR ")) {
// b = b.split(" OR ")[Math.floor(Math.random()*b.split(" OR ").length)];
//}
// Loop through b0.split(" AND ")
for (var i = 0; i < b0.split(" AND ").length; i++) {
var b = b0.split(" AND ")[i];
var arg = null;
if (b.includes(":")) {
arg = b.split(":")[1].split(/[\:\%]/)[0];
if (!b.includes("%")) {
b = b.split(/[\:\%]/)[0];
}
}
// If b has "%" followed by a number in it, it's a chance to move
if (b.includes("%")) {
// Split the string at the "%" and use the second half as the chance (float)
var chance = parseFloat(b.split("%")[1]);
//console.log(b+": "+(Math.random()*100 < chance));
b = b.split(/[\:\%]/)[0];
} else {
var chance = 100;
}
if (b == "XX" || b == "") {
continue
}
if (Math.random() * 100 < chance) {
var newCoords = behaviorCoords(x, y, bx, by);
if (b == "M1") {
if (!((Math.random() * 100) < 100 / ((info.viscosity || 1) ** 0.25))) {
newCoords.x = x;
}
move1Spots.push(newCoords);
} else if (b == "M2") {
if (!((Math.random() * 100) < 100 / ((info.viscosity || 1) ** 0.25))) {
newCoords.x = x;
}
move2Spots.push(newCoords);
} else if (b == "SP") {
supportSpots.push({
x: newCoords.x,
y: newCoords.y,
arg: arg
});
} else if (b == "DL") {
if ((!isEmpty(newCoords.x, newCoords.y)) && !outOfBounds(newCoords.x, newCoords.y)) {
// if the pixel at newCoords is the same element as the pixel, ignore
newPixel = pixelMap[newCoords.x][newCoords.y];
if ((!(newPixel.element == pixel.element)) || (newCoords.x == x && newCoords.y ==
y)) {
if (arg != null) {
var args = arg.split(",");
}
if (arg == null || args.includes(newPixel.element)) {
deletePixel(newCoords.x, newCoords.y);
if (newCoords.x == x && newCoords.y == y) {
deleted = true;
}
}
}
}
} else if (b == "CL") {
if (isEmpty(newCoords.x, newCoords.y)) {
clonePixel(pixel, newCoords.x, newCoords.y);
}
}
//Change pixel
else if (b == "CH") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (arg.includes(">")) {
var argfrom = arg.split(">")[0];
var argto = arg.split(">")[1];
} else {
var argfrom = null;
var argto = arg;
}
if (argto.includes(",")) {
var argto = argto.split(",")[Math.floor(Math.random() * argto.split(",")
.length)];
}
if ((newPixel.element != argto) && (argfrom == null || argfrom == newPixel
.element)) {
newPixel.element = argto;
newPixel.color = pixelColorPick(newPixel);
newPixel.start = pixelTicks;
if (elements[argto].burning != true) {
newPixel.burning = false;
}
}
}
}
//Replace pixel
/*else if (b == "RP") {
replaceSpots.push({x:newCoords.x,y:newCoords.y,arg:arg});
}*/
//Create pixel
else if (b == "CR") {
if (isEmpty(newCoords.x, newCoords.y)) {
if (arg == null) {
arg = pixel.element;
} else if (arg.includes(",")) {
arg = arg.split(",")[Math.floor(Math.random() * arg.split(",").length)];
}
createPixel(arg, newCoords.x, newCoords.y);
}
}
//Leave behind element
else if (b == "LB" || b == "L1" || b == "L2") {
if (arg != null && arg.includes(",")) {
arg = arg.split(",")[Math.floor(Math.random() * arg.split(",").length)];
}
if (b == "LB") {
leaveBehind = arg;
} else if (b == "L1") {
leaveBehind1 = arg;
} else if (b == "L2") {
leaveBehind2 = arg;
}
}
//Swap
else if (b == "SW") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (arg != null) {
var args = arg.split(",");
}
if (arg == null || args.includes(newPixel.element)) {
swapSpots.push({
x: newCoords.x,
y: newCoords.y
});
}
}
}
//Heat
else if (b == "HT") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (arg != null) {
arg = parseFloat(arg)
} else {
arg = 1
}
if (arg == NaN) {
arg = 1
}
newPixel.temp += arg;
pixelTempCheck(newPixel);
}
}
//Cool
else if (b == "CO") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (arg != null) {
arg = parseFloat(arg)
} else {
arg = 1
}
if (arg == NaN) {
arg = 1
}
newPixel.temp -= arg;
pixelTempCheck(newPixel);
}
}
//Change color
else if (b == "CC") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (arg == null) {
arg = newPixel.colorObject
} else {
if (arg.includes(",")) {
arg = arg.split(",")[Math.floor(Math.random() * arg.split(",").length)];
}
if (!arg.startsWith("#")) {
arg = "#" + arg;
}
}
newPixel.color = pixelColorPick(newPixel, arg);
}
}
//Stick
else if (b == "ST") {
if (!isEmpty(newCoords.x, newCoords.y) && !outOfBounds(newCoords.x, newCoords.y)) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
if (newPixel.element != pixel.element && (arg == null || newPixel.element == arg)) {
sticking = true
}
}
}
}
}
}
}
if (deleted) {
return;
}
var move = true;
if (supportSpots.length > 0) {
var supportCount = 0;
var allEmpty = true;
for (var i = 0; i < supportSpots.length; i++) {
var bx = supportSpots[i].x;
var by = supportSpots[i].y;
var arg = supportSpots[i].arg;
if ((!isEmpty(bx, by)) && !outOfBounds(bx, by)) {
if (pixelMap[bx][by].element == arg || arg == null) {
supportCount++;
}
}
}
if (supportCount == supportSpots.length) {
move = false;
}
}
/*replaceSpotsCopy = replaceSpots.slice();
if (replaceSpots.length > 0) {
while (move1Spots.length > 0) {
// coords = random item of replaceSpots
var newCoords = replaceSpots[Math.floor(Math.random()*replaceSpots.length)];
if (!isEmpty(newCoords.x,newCoords.y) && !outOfBounds(newCoords.x,newCoords.y)) {
if (newPixel.element == newCoords.arg || newCoords.arg == null) {
var newPixel = pixelMap[newCoords.x][newCoords.y];
var newCoords = replaceSpots[Math.floor(Math.random()*replaceSpots.length)];
deletePixel(newCoords.x,newCoords.y);
pixelMap[newCoords.x][newCoords.y] = pixel;
pixel.x = newCoords.x;
pixel.y = newCoords.y;
moved = true;
replaceSpotsCopy.splice(replaceSpotsCopy.indexOf(newCoords),1);
break;
}
}
replaceSpots.splice(replaceSpots.indexOf(newCoords),1);
}
}
if (replaceSpotsCopy.length > 0 && !moved) {
// combine replaceSpotsCopy and move2Spots
move2Spots = move2Spots.concat(replaceSpotsCopy);
}*/
var moved = false;
if (swapSpots.length > 0) {
var coords = swapSpots[Math.floor(Math.random() * swapSpots.length)];
swapPixels(pixel, pixelMap[coords.x][coords.y]);
move = false;
moved = true;
}
if (sticking) {
move = false;
}
// Move First Priority
if (move) {
if (move1Spots.length > 0) {
// While move1Spots is not empty
while (move1Spots.length > 0) {
// coords = random item of move1Spots
var coords = move1Spots[Math.floor(Math.random() * move1Spots.length)];
var nx = coords.x;
var ny = coords.y;
if (isEmpty(nx, ny)) { // If coords is empty, move to coords
movePixel(pixel, nx, ny, leaveBehind1 || leaveBehind);
moved = true;
break;
} else { // Remove from move1Spots
move1Spots.splice(move1Spots.indexOf(coords), 1);
}
}
}
// Move Second Priority
if (!moved && move2Spots.length > 0) {
// While move2Spots is not empty
while (move2Spots.length > 0) {
// coords = random item of move2Spots
var coords = move2Spots[Math.floor(Math.random() * move2Spots.length)];
var nx = coords.x;
var ny = coords.y;
if (isEmpty(nx, ny)) { // If coords is empty, move to coords
movePixel(pixel, nx, ny, leaveBehind2 || leaveBehind);
moved = true;
break;
} else { // Remove from move2Spots
move2Spots.splice(move2Spots.indexOf(coords), 1);
}
}
}
}
if (pixel.burning) {
pixel.temp += 1;
pixelTempCheck(pixel);
var burnSpots = [{
x: pixel.x + 1,
y: pixel.y
},
{
x: pixel.x - 1,
y: pixel.y
},
{
x: pixel.x,
y: pixel.y + 1
},
{
x: pixel.x,
y: pixel.y - 1
},
];
// loop through burnspots
for (var i = 0; i < burnSpots.length; i++) {
var burnSpot = burnSpots[i];
if (!isEmpty(burnSpot.x, burnSpot.y) && !outOfBounds(burnSpot.x, burnSpot.y)) {
var newPixel = pixelMap[burnSpot.x][burnSpot.y];
if (elements[newPixel.element].burn && !newPixel.burning) {
if (Math.floor(Math.random() * 100) < elements[newPixel.element].burn) {
newPixel.burning = true;
newPixel.burnStart = pixelTicks;
}
}
}
}
if ((pixelTicks - pixel.burnStart > (info.burnTime || 200)) && Math.floor(Math.random() * 100) < (info
.burn || 10)) {
var burnInto = info.burnInto;
if (burnInto == undefined) {
burnInto = 'fire';
} else if (burnInto instanceof Array) {
burnInto = burnInto[Math.floor(Math.random() * burnInto.length)];
}
pixel.element = burnInto;
if (info.fireColor != undefined && burnInto == "fire") {
pixel.color = pixelColorPick(pixel, info.fireColor);
} else {
pixel.color = pixelColorPick(pixel)
}
pixel.start = pixelTicks;
if (elements[burnInto].burning != true) {
pixel.burning = false;
delete pixel.burnStart;
} else {
pixel.burning = true;
pixel.burnStart = pixelTicks;
}
if (elements[burnInto].temp != undefined) {
pixel.temp = elements[burnInto].temp;
pixelTempCheck(pixel)
}
} else if (pixel.element != "fire" && isEmpty(pixel.x, pixel.y - 1) && Math.floor(Math.random() * 100) <
10) {
createPixel("fire", pixel.x, pixel.y - 1);
pixelMap[pixel.x][pixel.y - 1].temp = pixel.temp + (pixelTicks - (pixel.burnStart || 0));
if (info.fireColor != undefined) {
pixelMap[pixel.x][pixel.y - 1].color = pixelColorPick(pixelMap[pixel.x][pixel.y - 1], info
.fireColor);
}
}
}
if (info.tempChange != undefined) {
pixel.temp += info.tempChange;
pixelTempCheck(pixel);
}
}