Repository navigation
Expand file tree
/
Copy pathmodloadercode.js
More file actions
1440 lines (1221 loc) · 65 KB
/
Copy pathmodloadercode.js
File metadata and controls
1440 lines (1221 loc) · 65 KB
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
window.levelEditorMod = {};
////////////////////////////////////////////////////////////////////
//RUNCODEBEFORE
////////////////////////////////////////////////////////////////////
window.levelEditorMod.runCodeBefore = function() {
///////////////////////////////////////
//Taken from shared.js
///////////////////////////////////////
window.wholeSnakeObject;//Set to the big snake object that includes everything in snake.
window.megaWholeSnakeObject;//Contains wholeSnakeObject
//Allow running code at particular points in time (e.g. after board reset, after death, after eating an apple)
window.simpleHookManager = {
hooks: {
'afterResetBoard':[],//Format for an element {name: myHook, callback: myFunction}
},
runHook: function(hookType) {
if(this.hooks.hasOwnProperty(hookType) && Array.isArray(this.hooks[hookType])) {
for(let hook of this.hooks[hookType]) {
hook.callback();
}
} else {
throw new Error(`Hook type "${hookType}" not found`);
}
},
registerHook: function(hookType, hookName, hookCallback) {
if(!this.hooks.hasOwnProperty(hookType) || !Array.isArray(this.hooks[hookType])) throw new Error(`Hook type not found`);
if(typeof hookName !== 'string' || typeof hookCallback !== 'function') {
throw new Error('hookName must be a string, hookCallback must be a function.');
}
//Check hook not already registered
if(this.hooks[hookType].some(el=>el.name === hookName)) return;
this.hooks[hookType].push({name: hookName, callback: hookCallback});
}
}
///////////////////////////////////////
//Taken from level-editor.js
///////////////////////////////////////
//For localhost vs github version
const isProd = true;
globalThis.rootUrl;
globalThis.imagePresetsFolder;
globalThis.sharedUrl;
globalThis.randomHamUrl;
globalThis.challengeUrl;
if(isProd) {
//Live
globalThis.rootUrl = 'https://raw.githubusercontent.com/DarkSnakeGang/GoogleSnakeLevelEditor/main/';
globalThis.imagePresetsFolder = 'image-presets/';
globalThis.sharedUrl = 'https://raw.githubusercontent.com/DarkSnakeGang/GoogleSnakeLevelEditor/main/shared.js';
globalThis.randomHamUrl = 'https://raw.githubusercontent.com/DarkSnakeGang/GoogleSnakeLevelEditor/main/random_ham.txt';
globalThis.challengeUrl = 'https://raw.githubusercontent.com/DarkSnakeGang/GoogleSnakeLevelEditor/main/challenge.txt';
} else {
//Dev
globalThis.rootUrl = 'http://localhost:3000/';
globalThis.imagePresetsFolder = 'presets-windows-symlink/';
globalThis.sharedUrl = 'https://raw.githubusercontent.com/DarkSnakeGang/GoogleSnakeLevelEditor/main/shared.js';
globalThis.randomHamUrl = 'http://localhost:3000/random_ham.txt';
globalThis.challengeUrl = 'http://localhost:3000/challenge.txt';
}
//Used by make pattern
globalThis.roundApplePos = true;//For placing apples with mousedown
globalThis.mousePlaceMode = {category: 'apple', type:0};//Category could be apple, wall, drag or box, type corresponds to which apple
globalThis.disableWallMode = true;//Whether wall mode should place walls every 2 turns
globalThis.disableAppleInitialSpeed = false;
globalThis.customSnakeStart = {isActive:false, x:4, y:1};
globalThis.hasShownWarnings = {wall: false, sokoban: false};//Whether we have shown the warning about walls/boxes not having collisions without the right mode selected.
globalThis.customPresetManager = {
canvasWidth:370,
canvasHeight:340,
currentMapSize:'standard',
currentBoardWidth:17,
currentBoardHeight:15,
brush:'apple',
pixelList:[{x: 11, y: 7, category: 'apple', type: 0}],
isDrawing:false,
isErasing:false,
lastSpotDrawnOn:{x:undefined,y:undefined},
changeMapSize: function(newSize) {
let newSizeSetting = null;
this.currentMapSize = newSize;
switch(newSize) {
case 'small':
this.currentBoardWidth = 10;
this.currentBoardHeight = 9;
newSizeSetting = 1;
break;
case 'standard':
this.currentBoardWidth = 17;
this.currentBoardHeight = 15;
newSizeSetting = 0;
break;
case 'large':
this.currentBoardWidth = 24;
this.currentBoardHeight = 21;
newSizeSetting = 2;
break;
default:
throw new Error(`Unrecognised map size! Found ${newSize}`);
}
//Clear out old map (and triggers a draw afterwards)
this.clearAll();
this.placeInitialApple();
this.draw();//Technically we draw twice in a row since clearAll also draws, but ¯\_(ツ)_/¯
//Also change real map size
selectNewSizeSettingAndHardReset(newSizeSetting);
},
draw: function() {
const tileWidth = this.canvasWidth/this.currentBoardWidth;
const tileHeight = this.canvasHeight/this.currentBoardHeight;
this.ctx.fillStyle = "#a2d149";
this.ctx.fillRect(0,0,this.canvasWidth,this.canvasHeight);
this.ctx.fillStyle = "#aad751";
for(let j = 0; j < this.currentBoardHeight; j++) {
for(let i = 0; i < this.currentBoardWidth; i++) {
if((i + j) % 2 === 0) {
this.ctx.fillRect(i*tileWidth, j*tileHeight, tileWidth, tileHeight);
}
}
}
for(let k of this.pixelList) {
this.drawEntity(k.x * tileWidth, k.y * tileHeight, tileWidth, tileHeight, k.category, k.type);
}
},
drawEntity: function(xCoord, yCoord, width, height, category, type) {
switch(category) {
case 'apple':
this.ctx.fillStyle = "#E05826";
this.ctx.fillRect(xCoord, yCoord, width, height);
break;
case 'wall':
this.ctx.fillStyle = "#578A34";
this.ctx.fillRect(xCoord, yCoord, width, height);
break;
case 'box':
this.ctx.fillStyle = "#F0A036";
this.ctx.fillRect(xCoord, yCoord, width, height);
break;
case 'snakehead':
this.ctx.fillStyle = "#4673E8";
this.ctx.fillRect(xCoord, yCoord, width, height);
break;
default:
throw new Error(`Unexpected item to draw on custom preset canvas. Received: ${category}`);
}
},
clearAll: function() {
this.pixelList = [];
this.draw();
},
handleMouseDownOnBoard: function(event) {
event.preventDefault();
if(event.button === 0) {
customPresetManager.isDrawing = true;
} else if(event.button === 2) {
customPresetManager.isErasing = true;
} else {
return;
}
const rect = document.getElementById('custom-preset-canvas').getBoundingClientRect();
customPresetManager.attemptPlace(event.clientX - rect.left, event.clientY - rect.top, customPresetManager.isErasing);
},
handleMouseUpAnywhere: function(event) {
event.preventDefault();
if(event.button === 0) {
customPresetManager.isDrawing = false;
} else if(event.button === 2) {
customPresetManager.isErasing = false;
} else {
return;
}
customPresetManager.lastSpotDrawnOn = {x:undefined,y:undefined};
},
handleMouseMoveOnBoard: function(event) {
if(!customPresetManager.isDrawing && !customPresetManager.isErasing) return;
const rect = document.getElementById('custom-preset-canvas').getBoundingClientRect();
customPresetManager.attemptPlace(event.clientX - rect.left, event.clientY - rect.top, customPresetManager.isErasing);
},
attemptPlace: function(xPixelCoord, yPixelCoord, isErase=false) {
//Try to place entity corresponding to current brush into the pixelList.
//Convert pixels coords into board coords
const tileWidth = this.canvasWidth/this.currentBoardWidth;
const tileHeight = this.canvasHeight/this.currentBoardHeight;
const boardXCoord = Math.floor(xPixelCoord/tileWidth);
const boardYCoord = Math.floor(yPixelCoord/tileHeight);
//Exit early if the mouse is still on the same square
if(this.lastSpotDrawnOn.x === boardXCoord && this.lastSpotDrawnOn.y === boardYCoord) return;
this.lastSpotDrawnOn = {x:boardXCoord, y:boardYCoord};
//Exit early if out of bounds
if(boardXCoord < 0 || boardYCoord < 0 || boardXCoord >= this.currentBoardWidth || boardYCoord >= this.currentBoardHeight) return;
this.removeAtCoord(boardXCoord, boardYCoord);
//If we're erasing then exit just after erasing
if(isErase) {
this.draw();
return;
}
switch(this.brush) {
case 'apple':
this.pixelList.push({ x: boardXCoord, y: boardYCoord, category: 'apple', type: 0});
break;
case 'wall':
this.pixelList.push({ x: boardXCoord, y: boardYCoord, category: 'wall', type: -1});
break;
case 'box':
this.pixelList.push({ x: boardXCoord, y: boardYCoord, category: 'box', type: -1});
break;
case 'snakehead':
//Also remove any other snakehead instances.
this.removeSnakeHeads();
this.pixelList.push({ x: boardXCoord, y: boardYCoord, category: 'snakehead', type: -1});
break;
case 'erase':
//Do nothing since we remove earlier
break;
default:
throw new Error(`Unrecognised brush type ${this.brush}`);
}
//Always redraw (this might be unecessary if we're placing on an occupied spot, but there's no real harm in this)
this.draw();
},
removeAtCoord: function(boardX, boardY) {
let i = this.pixelList.length;
while(i--) {
if(this.pixelList[i].x === boardX && this.pixelList[i].y === boardY) {
this.pixelList.splice(i, 1);
}
}
},
removeSnakeHeads: function() {
let i = this.pixelList.length;
while(i--) {
if(this.pixelList[i].category === 'snakehead') {
this.pixelList.splice(i, 1);
}
}
},
placeInitialApple: function() {
//Places a single apple. This is because otherwise the game will be an instant-win
this.pixelList.push({x: Math.floor(this.currentBoardWidth * 3/4), y: Math.floor(this.currentBoardHeight/2), category: 'apple', type: 0});
},
getExportCode: function() {
let codesArray = [];
//First part gives the map size.
codesArray.push(`${this.currentBoardWidth}x${this.currentBoardHeight}`);
for(let entity of this.pixelList) {
switch(entity.category) {
case 'apple':
codesArray.push(`A${entity.x},${entity.y}`);
break;
case 'wall':
codesArray.push(`W${entity.x},${entity.y}`);
break;
case 'box':
codesArray.push(`B${entity.x},${entity.y}`);
break;
case 'snakehead':
codesArray.push(`S${entity.x},${entity.y}`);
break;
case 'default':
throw new Error('Unrecognise category when getting export code');
}
}
return codesArray.join(' ');
},
importCode: function(fullCode) {
fullCode = fullCode.trim();
//Process the main part of the code.
this.pixelList = this.getPixelListFromLevelCode(fullCode);
//First part gives map size
try {
let mapPart = fullCode.split(' ')[0]
let [boardWidth, boardHeight] = mapPart.split('x');
boardWidth = parseInt(boardWidth);
boardHeight = parseInt(boardHeight);
if(typeof boardWidth === 'number' && typeof boardHeight === 'number' && isFinite(boardWidth) && isFinite(boardHeight) && boardWidth > 0 && boardHeight > 0) {
this.currentBoardWidth = boardWidth;
this.currentBoardHeight = boardHeight;
let newSizeSetting = null;
if(boardWidth === 10 && boardHeight === 9) {
this.currentMapSize = 'small';
newSizeSetting = 1;
} else if(boardWidth === 17 && boardHeight === 15) {
this.currentMapSize = 'standard';
newSizeSetting = 0;
} else if(boardWidth === 24 && boardHeight === 21) {
this.currentMapSize = 'large';
newSizeSetting = 2;
} else {
//Maybe they are trying to do a custom board size? Just keep map size the same for now.
newSizeSetting = null;
}
document.getElementById('custom-map-size').value = this.currentMapSize;
//Also change real map size
selectNewSizeSettingAndHardReset(newSizeSetting);
}
} catch (err) {
//Just skip and keep current map size.
}
this.draw();
},
getPixelListFromLevelCode: function(levelCode) {
levelCode = levelCode.trim();
let newPixelList = [];
let codesArray = levelCode.split(' ');
//Start from 1 since 0 contains board size which we don't need for pixelList
for(let i = 1; i < codesArray.length; i++) {
try {
let entityLetter = codesArray[i][0]; //First letter indicates where it is apple, box...
let coordPart = codesArray[i].substr(1);
let coords = coordPart.split(',');
//Skip if coords are bad.
let coordX = parseInt(coords[0]);
let coordY = parseInt(coords[1]);
if(!isFinite(coordX) || !isFinite(coordY) || coordX < 0 || coordY < 0) continue;
switch(entityLetter) {
case 'A':
//Apple
newPixelList.push({x: coordX, y: coordY, category: 'apple', type: 0});
break;
case 'W':
//Wall
newPixelList.push({x: coordX, y: coordY, category: 'wall', type: -1});
break;
case 'B':
//Box
newPixelList.push({x: coordX, y: coordY, category: 'box', type: -1});
break;
case 'S':
//Snakehead
newPixelList.push({x: coordX, y: coordY, category: 'snakehead', type: -1});
break;
default:
throw new Error('Unrecognise entity letter');
}
} catch(err) {
//Just skip and process next code
}
}
return newPixelList;
}
};
window.otherPresetmanager = {
isHamsLoaded: false,
isChallengeLoaded: false,
hamLevelCodes: [],
challengeLevelCodes: [],
randomHamAppleCount: 5,
challengelevel: 1,
loadRandomHams: function() {
fetch(randomHamUrl)
.then(response=>response.text())
.then(allHamCodes=>{
otherPresetmanager.hamLevelCodes = allHamCodes.split('\n');
otherPresetmanager.isHamsLoaded = true;
});
},
loadChallenge: function() {
fetch(challengeUrl)
.then(response=>response.text())
.then(allChallengeCodes=>{
otherPresetmanager.challengeLevelCodes = allChallengeCodes.split('\n');
otherPresetmanager.isChallengeLoaded = true;
});
},
getRandomHamPixelList: function() {
if(!this.isHamsLoaded) return [];
//Choose random level
const chosenLevelCode = this.hamLevelCodes[Math.floor(Math.random() * this.hamLevelCodes.length)];
//Get pixelList
let hamPixelList = customPresetManager.getPixelListFromLevelCode(chosenLevelCode);
//Add some apples
let appleDesiredCoords;
switch(this.randomHamAppleCount) {
case 1:
appleDesiredCoords = [{x:7,y:4}];
break;
case 3:
appleDesiredCoords = [{x:5,y:2},{x:7,y:4},{x:5,y:6}];
break;
case 5:
appleDesiredCoords = [{x:4,y:2},{x:8,y:2},{x:6,y:4},{x:4,y:6},{x:8,y:6}];
break;
default:
throw new Error('Unexpected apple count for random ham');
}
for(let apple of appleDesiredCoords) {
//Add the apples to the pixelList
if(hamPixelList.some(entity=>entity.x === apple.x && entity.y === apple.y)) {
//If the spot an apple would normally be is occupied, then move it one space up.
apple.y--;
}
hamPixelList.push({x: apple.x, y: apple.y, category: 'apple', type: 0});
}
return hamPixelList;
},
getChallengePixelList: function() {
if(!this.isChallengeLoaded) return [];
const chosenLevelCode = this.challengeLevelCodes[this.challengelevel - 1];
//Get pixelList
let challengePixelList = customPresetManager.getPixelListFromLevelCode(chosenLevelCode);
return challengePixelList;
}
};
window.setPixelData = function(target, url, callback = null) {
target.complete = false;
fetch(url)
.then(function (response) {
if (!response.ok) {
throw new Error('HTTP error, status = ' + response.status);
}
return response.blob();
})
.then(function (myBlob) {
let objectURL = URL.createObjectURL(myBlob);
let img = document.createElement('img');
img.src = objectURL;
if (img.complete) {
extractImageData(img, objectURL);
} else {
img.addEventListener('load', function () { extractImageData(img, objectURL) });
img.addEventListener('error', function () { alert('Error loading image') })
}
})
.catch(function (error) {
console.log(error);
});
function extractImageData(img, objectURL) {
let canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
let ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
let imageDataArray = ctx.getImageData(0, 0, img.width, img.height).data;
let pixelList = [];//Contains hex and coords
for (let i = 0; i < imageDataArray.length; i += 4) {
let hex = rgbToHex(imageDataArray[i], imageDataArray[i + 1], imageDataArray[i + 2]);
let pixelDetails = hexToPixelDetails(hex);
pixelList.push({ x: i / 4 % img.width, y: Math.floor((i / 4) / img.width), hex: hex, category: pixelDetails.category, type: pixelDetails.type });
}
URL.revokeObjectURL(objectURL);
target.pixelList = pixelList;
target.complete = true;
if (callback) {
callback();
}
}
}
window.blitPattern = function(pixelList, offsetX=0, offsetY=0) {
customSnakeStart.isActive = false;
emptyApples();
emptySokoboxes();
emptySokogoals();
for(let i=0;i<pixelList.length;i++) {
switch(pixelList[i].category) {
case 'apple':
if(pixelList[i].type != -1) {
let intialSpeed = undefined;//Default to using speed given by winged mode, or whatever mode is selected
if(disableAppleInitialSpeed) {
intialSpeed = {x:0, y:0};
}
window.placeApple(pixelList[i].x + offsetX,pixelList[i].y + offsetY,pixelList[i].type, intialSpeed);
}
break;
case 'wall':
window.placeWall(pixelList[i].x, pixelList[i].y, true);
break;
case 'box':
window.placeSokobox(pixelList[i].x, pixelList[i].y);
break;
case 'snakehead':
//Do nothing since we handle the snakehead stuff somewhere else
//setSnakeHead(pixelList[i].x, pixelList[i].y);
break;
default:
throw Error('Unrecognised category!');
}
}
}
window.blitSelectedPreset = function() {
let presetEl = document.getElementsByClassName('chosen-preset')?.[0];
if(!presetEl) {
throw new Error('No element with chosen preset class?!? Should never happen.');
}
if(presetEl.classList.contains('preset-none')) return;
let isUsingCustomPreset = presetEl.classList.contains('preset-custom');
let isUsingRandomHamPreset = presetEl.classList.contains('preset-random-ham');
let isUsingChallengePreset = presetEl.classList.contains('preset-challenge');
let patternPixelList;
if(isUsingCustomPreset) {
patternPixelList = customPresetManager.pixelList;
} else if(isUsingRandomHamPreset) {
patternPixelList = otherPresetmanager.getRandomHamPixelList();
} else if(isUsingChallengePreset) {
patternPixelList = otherPresetmanager.getChallengePixelList();
} else {
//Blit Pattern
let imageUrl = presetEl.src;
//Remove the start of the url so we are just left with a file path which can be used as a key for the pattern.
let patternName = imageUrl.replace(rootUrl,'');
patternPixelList = presetPatterns[patternName].pixelList
}
//Make sure they aren't using a big preset on a small map.
if(!checkPatternInBounds(patternPixelList)) {
alert('The current board size is too small to use the currently selected pattern.');
return;
}
let spawnOffset = getAppleSpawnPointOffset();
blitPattern(patternPixelList, spawnOffset.x, spawnOffset.y);
}
window.setSelectedSnakeHead = function() {
customSnakeStart.isActive = false;
let presetEl = document.getElementsByClassName('chosen-preset')?.[0];
if(!presetEl) {
throw new Error('No element with chosen preset class?!? Should never happen.');
}
if(presetEl.classList.contains('preset-none')) return;
if(presetEl.classList.contains('preset-random-ham')) return;
let isUsingCustomPreset = presetEl.classList.contains('preset-custom');
let isUsingChallengePreset = presetEl.classList.contains('preset-challenge');
let patternPixelList;
if(isUsingCustomPreset) {
patternPixelList = customPresetManager.pixelList;
} else if(isUsingChallengePreset) {
patternPixelList = otherPresetmanager.getChallengePixelList();
} else {
//Blit Pattern
let imageUrl = presetEl.src;
//Remove the start of the url so we are just left with a file path which can be used as a key for the pattern.
let patternName = imageUrl.replace(rootUrl,'');
patternPixelList = presetPatterns[patternName].pixelList
}
//Make sure they aren't using a big preset on a small map.
if(!checkPatternInBounds(patternPixelList)) return;
let pixelWithSnakeHead = patternPixelList.find(x=>x.category==='snakehead');
if(pixelWithSnakeHead) {
setSnakeHead(pixelWithSnakeHead.x, pixelWithSnakeHead.y);
}
}
window.componentToHex = function(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
/*
Used to tell if a pixel corresponds to a apple or a wall or a sokoban box
Also gives info on what type of apple (e.g. apples or oranges)
*/
window.hexToPixelDetails = function(hex) {
hex = hex.toUpperCase();
//Value gives the category, if it's not found then this is assumed to be "apple"
const hexToCategory = {
'#578A34': 'wall',//Wall
'#F0A036': 'box',//Boxes from sokoban, color is slight off average, but good enough imo
'#4673E8': 'snakehead'//Start Position of the snake
};
let category = 'apple';
if(hexToCategory.hasOwnProperty(hex)) {
category = hexToCategory[hex];
} else {
category = 'apple'; //Defensive programming, though kinda unnecessary idk?
}
//Index gives the type, if it isn't found then it would be -1 corresponding to no apple
const hexToTypeMapping = [
'#E05826',//Apple
'#DABC1F',//Banana
'#C5830E',//Pineapple
'#8D2DA0',//Grapes
'#E68A1B',//Apricot?
'#D4D2D1',//Onion
'#904FA3',//Aubergine
'#CD6512',//Strawberry
'#CE6A29',//Cherry
'#DE9713',//Carrot
'#E4A588',//Mushroom
'#009000',//Brocolli
'#C77B4A',//Watermelon
'#14A019',//Green pepper
'#6AB927',//Kiwi
'#EFCF1E',//Lemon
'#F28F13',//Orange
'#F38768',//Peach
'#C7913C',//Peanut
'#E74738',//Raspberry
'#F24311'//Tomato
];
//For reference - the full color list is
/*
E05826//Apple
DABC1F//Banana
C5830E//Pineapple
8D2DA0//Grapes
E68A1B//Apricot?
D4D2D1//Onion
904FA3//Aubergine
CD6512//Strawberry
CE6A29//Cherry
DE9713//Carrot
E4A588//Mushroom
009000//Brocolli
C77B4A//Watermelon
14A019//Green pepper
6AB927//Kiwi
EFCF1E//Lemon
F28F13//Orange
F38768//Peach
C7913C//Peanut
E74738//Raspberry
F24311//Tomato
*/
return {category: category, type: hexToTypeMapping.indexOf(hex)};
}
window.rgbToHex = function(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
window.placeAppleAtMouse = function(event) {
let canvasRect = gameCanvasElMakePattern.getBoundingClientRect();
const offsetFromBorder = {x:26,y:26};
if(window.wholeSnakeObject && tileWidth && window.placeApple && window.placeWall && window.placeSokobox) {
const calculatedTileWidth = eval(`window.wholeSnakeObject.${tileWidth}`);
mouseX = event.clientX - canvasRect.left - offsetFromBorder.x - calculatedTileWidth/2;
mouseY = event.clientY - canvasRect.top - offsetFromBorder.y - calculatedTileWidth/2;
gameCoordX = mouseX / calculatedTileWidth;
gameCoordY = mouseY / calculatedTileWidth;
switch(mousePlaceMode.category) {
case 'apple':
//Undo offset for spawn point
var spawnOffset = getAppleSpawnPointOffset();
gameCoordX += spawnOffset.x; gameCoordY += spawnOffset.y;
roundApplePos ? window.placeApple(Math.round(gameCoordX), Math.round(gameCoordY), mousePlaceMode.type) : window.placeApple(gameCoordX, gameCoordY, mousePlaceMode.type);
break;
case 'wall':
window.placeWall(gameCoordX, gameCoordY, true);
break;
case 'box':
window.placeSokobox(gameCoordX, gameCoordY);
break;
default:
console.log('mousePlaceCategory set incorrectly. Ignoring');
}
} else {
console.log('Mouse click happened before proper setup. Ignoring');
}
}
window.setupMakePatternHtml = function() {
//Add message on canvas saying that level editor mod is being used
let modIndicator = document.createElement('div');
modIndicator.style='position:absolute;font-family:roboto;color:white;font-size:14px;padding-top:4px;padding-left:30px;user-select: none;';
modIndicator.textContent = 'Level Editor Mod';
let canvasNode = document.getElementsByClassName('jNB0Ic')[0];
document.getElementsByClassName('EjCLSb')[0].insertBefore(modIndicator, canvasNode);
let visiblePanel = 'place';//"place" or "preset" - indicates whether to show the panel for placing individual fruit or preset patterns
//Add html
const style = "position: absolute; left:100%;z-index:1001";
const presetImages = [
`${imagePresetsFolder}ez_loop.png`,
`${imagePresetsFolder}straight_line.png`,
`${imagePresetsFolder}rooms.png`,
`${imagePresetsFolder}pacman.png`,
`${imagePresetsFolder}maze_for_key_statue_large.png`,
`${imagePresetsFolder}regular_grid.png`,
`${imagePresetsFolder}knight_grid.png`,
`${imagePresetsFolder}max_rooms.png`,
`${imagePresetsFolder}soko2.png`,
`${imagePresetsFolder}soko1.png`,
`${imagePresetsFolder}soko3_beta.png`,
`${imagePresetsFolder}soko_widegrid.png`,
`${imagePresetsFolder}use_reversing_at_will.png`,
`${imagePresetsFolder}ham_path.png`,
`${imagePresetsFolder}maze_beta.png`,
];
const htmlToInsert = `
<div id="place-panel" style="height: 650px; width: 200px; background-color: #bfde80; display: grid; grid-template-columns: 60px 60px 60px; justify-content: space-evenly;box-sizing: border-box; border: 10px solid #507f30;">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v13/trophy_01.png" data-type="-1" data-category="wall">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v13/trophy_09.png" data-type="-1" data-category="box">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(0%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_00.png" data-type="0" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_01.png" data-type="1" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_02.png" data-type="2" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_03.png" data-type="3" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_04.png" data-type="4" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_05.png" data-type="5" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_06.png" data-type="6" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_07.png" data-type="7" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_08.png" data-type="8" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_09.png" data-type="9" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_10.png" data-type="10" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_11.png" data-type="11" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_12.png" data-type="12" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_13.png" data-type="13" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_14.png" data-type="14" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_15.png" data-type="15" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_16.png" data-type="16" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_17.png" data-type="17" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_18.png" data-type="18" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_19.png" data-type="19" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_20.png" data-type="20" data-category="apple">
<img class="place-option" style="max-width:100%; height:auto; object-fit:cover; cursor:pointer; filter: grayscale(100%)" draggable="false" src="https://www.google.com/logos/fnbx/snake_arcade/v17/apple_21.png" data-type="21" data-category="apple">
<div id="link-to-preset" style="height: 40px;background-color: #4a752c;grid-column: 1 / 4;font-size: 2em;font-family: 'Roboto';text-align: center;color: white;line-height: 40px;border: 3px solid #bfde80;border-radius: 4px;cursor: pointer;">Presets ></div>
</div>
<div id="preset-panel" style="height: 650px; width: 200px; background-color: #bfde80; display: none; grid-template-columns: 60px 60px 60px; justify-content: space-evenly;box-sizing: border-box; border: 10px solid #507f30;">
<img class="chosen-preset preset-none preset-option" style="width:100%; height:auto; object-fit:cover; cursor:pointer;" draggable="false" src="${rootUrl}none.png">
<div class="preset-option preset-custom" style="height: 40px;background-color: #2d3f76;grid-column: 2 / 4;font-size: 1.5em;font-family: 'Roboto';text-align: center;color: white;line-height: 40px;cursor: pointer; margin-top:9px;">CUSTOM</div>
${presetImages.map(el=>'<img class="preset-option" style="width:100%; height:auto; object-fit:cover; cursor:pointer; image-rendering:pixelated;" draggable="false" src="' + rootUrl + el + '">').join('\n')}
<div class="preset-option preset-random-ham" style="height: 40px;background-color: #2d3f76;grid-column: 1 / 4;font-size: 1.5em;font-family: 'Roboto';text-align: center;color: white;line-height: 40px;cursor: pointer; margin-top:9px;">RANDOM HAM</div>
<div class="preset-option preset-challenge" style="height: 40px;background-color: #2d3f76;grid-column: 1 / 4;font-size: 1.5em;font-family: 'Roboto';text-align: center;color: white;line-height: 40px;cursor: pointer; margin-top:9px;">CHALLENGE</div>
<div id="link-to-place" style="height: 40px;background-color: #4a752c;grid-column: 1 / 4;font-size: 2em;font-family: 'Roboto';text-align: center;color: white;line-height: 40px;border: 3px solid #bfde80;border-radius: 4px;cursor: pointer;">< Place</div>
</div>
`;
const customPresetHtmlToInsert = `
<div id="custom-panel" style="height: 650px; width: 390px; background-color: rgb(191, 222, 128); display: none;box-sizing: border-box; border: 10px solid rgb(80, 127, 48);">
<div style="padding:10px;font-family:'Roboto'">
<p style="font-size: 1.17em;">Use the editor below to make your own starting pattern. Left click to place an entity, right click to erase.</p>
<button id="custom-import" style="font-family: 'Roboto';background-color: #2d3f76;color: white;border: none;padding: 7px;border-radius: 2px;cursor: pointer;margin-right: 7px;margin-bottom: 10px">IMPORT</button>
<button id="custom-export" style="font-family: 'Roboto';background-color: #2d3f76;color: white;border: none;padding: 7px;border-radius: 2px;cursor: pointer;margin-right: 7px;margin-bottom: 10px">EXPORT</button>
<button id="custom-clear" style="font-family: 'Roboto';background-color: #b41111;color: white;border: none;padding: 7px;border-radius: 2px;cursor: pointer;margin-right: 7px;margin-bottom: 10px">CLEAR ALL</button>
<button id="custom-refresh" style="font-family: 'Roboto';background-color: #027400;color: white;border: none;padding: 7px;border-radius: 2px;cursor: pointer;margin-right: 7px;margin-bottom: 10px">REFRESH</button>
<br><label for="custom-map-size">Map Size: </label>
<select name="map-size" id="custom-map-size" style="margin-bottom:4px">
<option value="standard">Standard</option>
<option value="large">Large</option>
<option value="small">Small</option>
</select><br>
<label for="custom-brush">Brush: </label>
<select name="brush" id="custom-brush">
<option value="apple">Apple</option>
<option value="wall">Wall</option>
<option value="box">Sokobox</option>
<option value="snakehead">Snake Start Position</option>
<option value="erase">Erase</option>
</select>
</div>
<canvas width="370" height="340" style="margin:3px; border:2px solid #507f30" id="custom-preset-canvas"></canvas>
</div>
<div id="challenge-panel" style="height: 650px; width: 390px; background-color: rgb(191, 222, 128); display: none;box-sizing: border-box; border: 10px solid rgb(80, 127, 48);">
<div style="padding:10px;font-family:'Roboto'">
<p style="font-size: 1.17em;">Choose level below. These get progressively harder. These should be played with the wall mode setting. Try with the fast speed for an extra challenge!</p>
<label for="challenge-level">Level: </label>
<select name="challenge-level" id="challenge-level">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
</div>
</div>
`;
const exportModal = `
<div id="custom-export-dialogue" style="display: block;margin:50px auto;padding:10px;border:1px solid black;height: 320px; width:316.4px; background-color:beige;border-radius:5px">
<p>Copy the text below and save it somewhere.</p>
<textarea id="custom-export-textarea" rows="15" cols="40"></textarea>
<button id="close-custom-export">Close</button>
</div>
`;
//Insert main html
let myEl = document.createElement('div');
myEl.innerHTML = htmlToInsert;
myEl.style = style;
document.getElementsByClassName('sEOCsb')[0].appendChild(myEl);
//Insert html for the custom preset stuff
let myCustomEl = document.createElement('div');
myCustomEl.innerHTML = customPresetHtmlToInsert;
myCustomEl.style = 'position: absolute; right: 100%;z-index:1001;';
document.getElementsByClassName('sEOCsb')[0].appendChild(myCustomEl);
//Collapse borders
document.getElementById('custom-panel').style.borderRight = 'none';
document.getElementById('challenge-panel').style.borderRight = 'none';
//Insert html for custom preset export dialogue box.
let importModalContainer = document.createElement('div');
importModalContainer.innerHTML = exportModal;
importModalContainer.id = 'custom-export-dialogue-container';
importModalContainer.style = 'display:none; position:fixed; width:100%; height:100%; z-index: 99999; left:0; top:0';
document.body.appendChild(importModalContainer);
//Adjust styling
document.getElementsByClassName('HIonyd')[0].style.color = '#FAFFFF';
document.getElementsByClassName('HIonyd')[1].style.color = '#FFFFFA';
Array.from(document.getElementsByClassName('place-option')).forEach(
(element)=>{
element.addEventListener('click',function() {
//Set global setting for place type
mousePlaceMode.type = this.dataset.type;
mousePlaceMode.category = this.dataset.category;
//Highlight chosen option and darken other options
Array.from(document.getElementsByClassName('place-option')).forEach(el=>{el.style.filter = 'grayscale(100%)';});
this.style.filter = 'grayscale(0%)';
});
}
);
//Preload presets
window.presetPatterns = {} || window.presetPatterns;
for(let el of presetImages) {
presetPatterns[el] = {};
setPixelData(presetPatterns[el],`${rootUrl}${el}`);
}
Array.from(document.getElementsByClassName('preset-option')).forEach(
(element)=>{
element.addEventListener('click',function() {
let newSizeSetting = null;
//Exit early if this option is already selected
if(this.classList.contains('chosen-preset')) return;
//Change css to "select" this preset
Array.from(document.getElementById('preset-panel').children).forEach(c=>{c.classList.remove('chosen-preset')});
this.classList.add('chosen-preset');
//Show the panel for editing the custom preset if it has been selected
document.getElementById('custom-panel').style.display = this.classList.contains('preset-custom') ? 'block' : 'none';
//Likewise for challenge panel
document.getElementById('challenge-panel').style.display = this.classList.contains('preset-challenge') ? 'block' : 'none';
//Change the size of the map
if(this.classList.contains('preset-custom')) {
//For the custom preset, change the map-size to whatever it's set in the custom panel.
switch(customPresetManager.currentMapSize) {
case 'standard':
newSizeSetting = 0;
break;
case 'small':
newSizeSetting = 1;
break;
case 'large':
newSizeSetting = 2;
break;
default:
throw new Error(`Illegal value for map size in customPresetManager: ${customPresetManager.currentMapSize}`);
}
} else if(this.classList.contains('preset-challenge')) {
//For the challenge just set the new size to normal
newSizeSetting = 0;
} else if(this.classList.contains('preset-random-ham')) {
//For the random ham just set the new size to small
newSizeSetting = 1;
} else if(this.classList.contains('preset-none')) {
//Keep map size the same
newSizeSetting = null;
} else {
//Figure out map size from whatever preset they selected.
switch(this.naturalWidth) {
case 17:
//standard
newSizeSetting = 0;
break;
case 10:
//small
newSizeSetting = 1;
break;
case 24:
//large
newSizeSetting = 2;
break;
default:
throw new Error(`Unexpected value for preset width: ${this.naturalWidth}`);
}
}
selectNewSizeSettingAndHardReset(newSizeSetting);
});
}
);
//Adjust style
document.querySelector('.FL0z2d[jsaction="rxqFXd"]').children[0].children[0].style.transform = 'rotate(90deg)';
//Allow switching tabs with ] key
document.addEventListener('keydown',(event)=>{if(event.key == ']') {
//Toggle which panel is shown
visiblePanel = visiblePanel === 'place' ? 'preset' : 'place';
let placePanelEl = document.getElementById('place-panel');
let presetPanelEl = document.getElementById('preset-panel');