Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
1116 lines (1037 loc) · 39.7 KB
/
Copy pathindex.js
File metadata and controls
1116 lines (1037 loc) · 39.7 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
//-----------------------------Map Generator Canvas------------------------------//
//Set onscreen canvas and its context
let onScreenCVS = document.querySelector(".map");
let onScreenCTX = onScreenCVS.getContext("2d");
onScreenCVS.width = 2048*2;
onScreenCVS.height = 1280*2;
onScreenCVS.style.width = "1024px";
onScreenCVS.style.height = "640px";
let scale = 2*2;
onScreenCTX.scale(scale, scale);
//Create an offscreen canvas. This is where we will actually be drawing,
//in order to keep the image consistent and free of distortions.
let offScreenCVS = document.createElement('canvas');
let offScreenCTX = offScreenCVS.getContext("2d");
//Set Tile Size
let tileSize = 16;
//Set the dimensions of the drawing canvas
offScreenCVS.width = 64;
offScreenCVS.height = 40;
//Create an Image with a default source of the existing onscreen canvas
let img = new Image;
let source = offScreenCVS.toDataURL();
//Add event listeners for the mouse moving, downclick, and upclick
onScreenCVS.addEventListener('mousemove', handleMouseMove);
onScreenCVS.addEventListener('mousedown', handleMouseDown);
onScreenCVS.addEventListener('mouseup', handleMouseUp);
//We only want the mouse to move if the mouse is down, so we need a
//variable to disable drawing while the mouse is not clicked.
let clicked = false;
function handleMouseMove(e) {
// onScreenCTX.fillRect(e.offsetX, e.offsetY, 1, 1)
if (clicked) {
draw(e)
// generateMap();
}
}
function handleMouseDown(e) {
clicked = true;
cancelPathfinding();
draw(e);
// generateMap();
}
function handleMouseUp() {
clicked = false;
}
//Helper functions
//Draw a single pixel on the canvas. Get the ratio of the difference in
//size of the on and offscreen canvases to calculate where to draw on the
//offscreen canvas based on the coordinates of clicking on the onscreen canvas.
function draw(e) {
let ratio = onScreenCVS.width/scale/offScreenCVS.width;
if (offScreenCTX.fillStyle === "rgba(0, 0, 0, 0)") {
offScreenCTX.clearRect(Math.floor(e.offsetX/ratio),Math.floor(e.offsetY/ratio),1,1);
} else if (offScreenCTX.fillStyle === "#ffa500") {
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
for (let i=0; i<imageData.data.length; i+=4) {
let x = i/4%offScreenCVS.width, y = (i/4-x)/offScreenCVS.width;
let color = `rgba(${imageData.data[i]}, ${imageData.data[i+1]}, ${imageData.data[i+2]}, ${imageData.data[i+3]})`
//Clear other pixels of same color
if (color === "rgba(255, 165, 0, 255)") {
offScreenCTX.clearRect(x,y,1,1);
}
}
//Draw pixel
offScreenCTX.fillRect(Math.floor(e.offsetX/ratio),Math.floor(e.offsetY/ratio),1,1);
} else if (offScreenCTX.fillStyle === "#0000ff") {
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
for (let i=0; i<imageData.data.length; i+=4) {
let x = i/4%offScreenCVS.width, y = (i/4-x)/offScreenCVS.width;
let color = `rgba(${imageData.data[i]}, ${imageData.data[i+1]}, ${imageData.data[i+2]}, ${imageData.data[i+3]})`
//Clear other pixels of same color
if (color === "rgba(0, 0, 255, 255)") {
offScreenCTX.clearRect(x,y,1,1);
}
}
//Draw pixel
offScreenCTX.fillRect(Math.floor(e.offsetX/ratio),Math.floor(e.offsetY/ratio),1,1);
} else {
offScreenCTX.fillRect(Math.floor(e.offsetX/ratio),Math.floor(e.offsetY/ratio),1,1);
}
//Set the source of the image to the offscreen canvas
source = offScreenCVS.toDataURL();
renderImage();
}
//Once the image is loaded, draw the image onto the onscreen canvas.
function renderImage() {
img.onload = () => {
//Prevent blurring
onScreenCTX.imageSmoothingEnabled = false;
onScreenCTX.clearRect(0,0,onScreenCVS.width/scale,onScreenCVS.height/scale);
onScreenCTX.drawImage(img,0,0,onScreenCVS.width/scale,onScreenCVS.height/scale)
onScreenCTX.fillStyle = "black";
generateMap();
if (mapNodes) {
freeTiles.forEach(n => {
onScreenCTX.fillStyle = "rgb(196, 188, 178)";
onScreenCTX.fillRect(n.x*tileSize+0.5,n.y*tileSize+0.5,tileSize-1,tileSize-1);
})
}
}
img.src = source;
}
//-------------------------------Palette----------------------------------//
let palette = document.querySelector('.color-select');
palette.addEventListener('click', selectColor)
function selectColor(e) {
offScreenCTX.fillStyle = e.target.id;
palette.childNodes.forEach(c => {
if (c.childNodes[1]) {
if (c.childNodes[1].id === e.target.id) {
c.childNodes[1].className = "swatch-selected";
} else {
c.childNodes[1].className = "swatch";
}
}
});
}
//-------------------------------Output-----------------------------------//
let steps = document.querySelector('.steps');
let pathLength = document.querySelector('.path');
let gDisplay = document.querySelector('.gCost');
let hDisplay = document.querySelector('.hCost');
let fDisplay = document.querySelector('.fCost');
//Set rounding precision
let decPlace = 1000;
//------------------------------Settings----------------------------------//
//Wall Buffer. Tiles adjacent to walls will cost more
let cornerBuffer = false;
let mapNodes = false;
let cornerCheckbox = document.querySelector('#cornerBuffer');
let nodeCheckbox = document.querySelector('#nodeRedundancy');
cornerCheckbox.addEventListener('input',updateCornerBuffer);
function updateCornerBuffer(e) {
cornerBuffer = cornerCheckbox.checked;
}
nodeCheckbox.addEventListener('input',updateNodeMap);
function updateNodeMap(e) {
mapNodes = nodeCheckbox.checked;
renderImage();
}
//Set Diagonals On/Off
let diagonals = true;
//Sliders
let delaySlider = document.querySelector('#stepDelay');
let delayDisplay = document.querySelector('.delay');
delaySlider.addEventListener('input', updateDelay);
function updateDelay(e) {
delayDisplay.textContent = delaySlider.value;
}
let tileSlider = document.querySelector('#tileSlider');
let tileSizeDisplay = document.querySelector('.tileSize');
tileSlider.addEventListener('input', updateTiles);
function updateTiles(e) {
cancelPathfinding();
tileSize = Math.pow(2, tileSlider.value);
offScreenCVS.width = onScreenCVS.width/scale/tileSize;
offScreenCVS.height = onScreenCVS.height/scale/tileSize;
tileSizeDisplay.textContent = tileSize;
onScreenCTX.clearRect(0,0,onScreenCVS.width/scale,onScreenCVS.height)
offScreenCTX.clearRect(0,0,offScreenCVS.width,offScreenCVS.height)
generateMap();
}
let precSlider = document.querySelector('#precSlider');
let precisionDisplay = document.querySelector('.precision');
precSlider.addEventListener('input', updatePrecision);
function updatePrecision(e) {
decPlace = Math.pow(10, precSlider.value);
precisionDisplay.textContent = decPlace;
}
//Heuristics
let hButtons = document.querySelector('#heuristic');
hButtons.addEventListener('input',updateHeuristic);
function updateHeuristic(e) {
if (e.target.value === "manhattan") {
calcHCost = manhattan;
diagonals = false;
} else if (e.target.value === "octile") {
calcHCost = octile;
diagonals = true;
} else if (e.target.value === "euclidean") {
calcHCost = euclid;
diagonals = true;
}
}
//fCost Calc Methods
let fButtons = document.querySelector('#fCost');
fButtons.addEventListener('input',updateFCalc);
function updateFCalc(e) {
if (e.target.value === "ignoreG") {
calcFCost = ignoreG;
} else if (e.target.value === "sum") {
calcFCost = sumCost;
}
}
//Tie Breakers
let tieButtons = document.querySelector('#tieBreak');
tieButtons.addEventListener('input',updateTieBreak);
function updateTieBreak(e) {
if (e.target.value === "cross") {
tieBreak = crossBreak;
compareFCost = naiveRank;
} else if (e.target.value === "proximity") {
tieBreak = proximBreak;
compareFCost = naiveRank;
} else if (e.target.value === "hCost") {
tieBreak = noBreak;
compareFCost = deferToH;
} else if (e.target.value === "noBreak") {
tieBreak = noBreak;
compareFCost = naiveRank;
}
}
//----------------------------Calc Functions------------------------------//
//********* Calculate gCost ***********//
let calcGCost = calcPath;
//Calculate path distance
function calcPath(node) {
let curr = node;
let cost = 0;
while(curr.parent) {
let step = Math.floor(euclid(curr,curr.parent)*decPlace)/decPlace;
cost += step*curr.cost;
curr = curr.parent;
}
cost = Math.floor(cost*decPlace)/decPlace;
return cost;
}
//********* Calculate hCost ***********//
//node1 is current node, node2 is end node
let calcHCost = octile;
//Manhattan Distance (without diagonal movement)
function manhattan(node1, node2) {
let a = Math.abs(node1.x - node2.x);
let b = Math.abs(node1.y - node2.y);
return a+b;
}
//Octile Distance (with diagonal movement)
function octile(node1, node2) {
let a = Math.abs(node1.x - node2.x);
let b = Math.abs(node1.y - node2.y);
function leastSide() {
if (a > b) {return b;} else {return a;}
}
let diagonalCost = leastSide()*Math.sqrt(2);
let horizontalCost = Math.abs(b-a);
let sum = diagonalCost+horizontalCost;
return Math.floor(sum*decPlace)/decPlace;
}
//Euclidean Distance (with diagonal movement)
function euclid(node1, node2) {
let distance = Math.hypot(node1.x - node2.x,node1.y - node2.y);
return Math.floor(distance*decPlace)/decPlace;
}
//********* fCost Tie Breakers using hCost ***********//
let tieBreak = crossBreak;
//Tiebreak with cross product to favor paths closer to a straight line to the goal
function crossBreak(node) {
let dx1 = node.x - end.x;
let dy1 = node.y - end.y;
let dx2 = start.x - end.x;
let dy2 = start.y - end.y;
let cross = Math.abs(dx1*dy2 - dx2*dy1);
let breaker = cross*(1/decPlace)
return breaker;
}
//Prioritize closest to goal
function proximBreak(node) {
//dwarf gCost
let breaker = euclid(node, end)*(1/decPlace);
return breaker;
}
//No Tie Break
function noBreak(node) {
return 0;
}
//************* Calc fCost **************//
let calcFCost = sumCost;
//Simple sum
function sumCost(g, h) {
return Math.floor((g + h)*decPlace)/decPlace;
}
//Ignore gCost
function ignoreG(g, h) {
return h;
}
//************* Rank fCost **************//
let compareFCost = deferToH;
//Rank by fCost, then hCost if equal.
function deferToH(obj1,obj2) {
if (obj1.fCost === obj2.fCost) {
if (obj1.hCost > obj2.hCost) {
return 1;
} else {
return -1;
}
} else if (obj1.fCost > obj2.fCost) {
return 1;
} else if (obj1.fCost < obj2.fCost) {
return -1;
}
return 0;
}
//Rank by fCost, naive
function naiveRank(obj1,obj2) {
if (obj1.fCost > obj2.fCost) {
return 1;
} else if (obj1.fCost < obj2.fCost) {
return -1;
}
return 0;
}
//--------------------------------Grid------------------------------------//
let gameGrid = [];
let start = {};
let end = {};
//walls list for drawing during pathfinding steps
let walls = [];
let nodes = [];
let freeTiles = [];
function getNeighbors(tile) {
let neighbors = {};
if (gameGrid[tile.y][tile.x+1]) {
//east
neighbors.east = gameGrid[tile.y][tile.x+1];
}
if (gameGrid[tile.y][tile.x-1]) {
//west
neighbors.west = gameGrid[tile.y][tile.x-1];
}
if (gameGrid[tile.y+1]) {
//south
neighbors.south = gameGrid[tile.y+1][tile.x];
if (diagonals) {
if (gameGrid[tile.y+1][tile.x-1]) {
//southwest
neighbors.southwest = gameGrid[tile.y+1][tile.x-1];
}
if (gameGrid[tile.y+1][tile.x+1]) {
//southeast
neighbors.southeast = gameGrid[tile.y+1][tile.x+1];
}
}
}
if (gameGrid[tile.y-1]) {
//north
neighbors.north = gameGrid[tile.y-1][tile.x];
if (diagonals) {
if (gameGrid[tile.y-1][tile.x-1]) {
//northwest
neighbors.northwest = gameGrid[tile.y-1][tile.x-1];
}
if (gameGrid[tile.y-1][tile.x+1]) {
//northeast
neighbors.northeast = gameGrid[tile.y-1][tile.x+1];
}
}
}
return neighbors;
}
function generateMap(e) {
gameGrid = [];
start = {};
end = {};
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
//Make the 2D array to hold all objects
for (let i=0; i<offScreenCVS.height; i++) {
gameGrid[i] = [];
for (let j=0; j<offScreenCVS.width; j++) {
gameGrid[i][j] = {parent: null, cost: 1, type: "free", x: j, y: i, gCost: 0, hCost: 0, fCost: 0}
//Draw the grid lines
onScreenCTX.beginPath();
onScreenCTX.rect(j*tileSize, i*tileSize, tileSize, tileSize);
onScreenCTX.strokeStyle = "rgb(214, 206, 197)";
onScreenCTX.lineWidth = 1;
onScreenCTX.stroke();
}
}
//Reset walls
walls = [];
nodes = [];
freeTiles = [];
//Iterate through pixels and set objects type each time a color matches
for (let i=0; i<imageData.data.length; i+=4) {
let x = i/4%offScreenCVS.width, y = (i/4-x)/offScreenCVS.width;
gameGrid[y][x].neighbors = getNeighbors(gameGrid[y][x]);
let color = `rgba(${imageData.data[i]}, ${imageData.data[i+1]}, ${imageData.data[i+2]}, ${imageData.data[i+3]})`
switch(color) {
case "rgba(0, 0, 0, 255)":
//black pixel
gameGrid[y][x].type = "wall";
walls.push(gameGrid[y][x])
break;
case "rgba(255, 165, 0, 255)":
//orange pixel
gameGrid[y][x].type = "start";
start = gameGrid[y][x];
break;
case "rgba(0, 0, 255, 255)":
//blue pixel
gameGrid[y][x].type = "end";
end = gameGrid[y][x];
break;
default:
//transparent pixel
//check neighbors
let freeNeighbors = 0;
function getColor(n, dir) {
// console.log(i, dir, n%offScreenCVS.width*4, `rgba(${imageData.data[n]}, ${imageData.data[n+1]}, ${imageData.data[n+2]}, ${imageData.data[n+3]})`)
//check left right edges
if (dir === "east" && n%(offScreenCVS.width*4) === 0) {return false}
if (dir === "west" && (n+4)%(offScreenCVS.width*4) === 0) {return false}
//bottom edge
if (n > imageData.data.length) {return false};
//top edge
if (n < 0) {return false};
//if it's not a wall, increment free neighbors
let black = "rgba(0, 0, 0, 255)";
let color = `rgba(${imageData.data[n]}, ${imageData.data[n+1]}, ${imageData.data[n+2]}, ${imageData.data[n+3]})`
if (black != color) {
freeNeighbors += 1;
return true;
} else {
return false;
}
}
let n = getColor(i-offScreenCVS.width*4, "north");
let s = getColor(i+offScreenCVS.width*4, "south");
let e = getColor(i+4, "east");
let w = getColor(i-4, "west");
let ne = getColor(i+4-offScreenCVS.width*4, "east");
let nw = getColor(i-4-offScreenCVS.width*4, "west");
let se = getColor(i+4+offScreenCVS.width*4, "east");
let sw = getColor(i-4+offScreenCVS.width*4, "west");
// console.log(freeNeighbors)
if (freeNeighbors > 2) {
gameGrid[y][x].type = "node";
nodes.push(gameGrid[y][x]);
} else if (n&&s) {
gameGrid[y][x].type = "free";
gameGrid[y][x].dir = "vertical";
freeTiles.push(gameGrid[y][x]);
} else if (e&&w) {
gameGrid[y][x].type = "free";
gameGrid[y][x].dir = "horizontal";
freeTiles.push(gameGrid[y][x]);
} else {
gameGrid[y][x].type = "node";
nodes.push(gameGrid[y][x]);
}
}
}
}
//------------------------------Pathfinder-------------------------------//
function findPath() {
//Search
//Priority queue
let open = new Set();
open.add(start);
start.gCost = 0;
start.hCost = Math.floor((calcHCost(start, end)+tieBreak(start))*decPlace)/decPlace;
start.fCost = calcFCost(start.gCost, start.hCost);
//empty set
let closed = new Set();
let current = start;
//Draw Path
function drawPath(path,delay) {
let pathIndex = 0;
function recursor() {
let tile = path[pathIndex]
onScreenCTX.fillStyle = "rgb(204,204,255)"
onScreenCTX.fillRect(tile.x*tileSize+0.5,tile.y*tileSize+0.5,tileSize-1,tileSize-1)
if (pathIndex === 0) {
onScreenCTX.fillStyle = "rgb(255,248,43)"
onScreenCTX.fillRect(tile.x*tileSize+0.5,tile.y*tileSize+0.5,tileSize-1,tileSize-1)
}
if (pathIndex === path.length-1) {
onScreenCTX.fillStyle = "rgb(97,92,255)"
onScreenCTX.fillRect(tile.x*tileSize+0.5,tile.y*tileSize+0.5,tileSize-1,tileSize-1)
generateBtn.disabled = false;
}
if (path[pathIndex-1]) {
pathLen += Math.hypot(path[pathIndex].x - path[pathIndex-1].x,path[pathIndex].y - path[pathIndex-1].y);
}
pathLength.textContent = Math.floor(pathLen*100)/100;
pathIndex+=1;
if (pathIndex < path.length) {setTimeout(recursor, 1)}
}
recursor();
}
//Draw tempPath
function drawTempPath(path) {
let pathIndex = 0;
function recursor() {
let tile = path[pathIndex]
onScreenCTX.fillStyle = "rgba(229, 124, 255, 255)"
onScreenCTX.fillRect(tile.x*tileSize+0.5,tile.y*tileSize+0.5,tileSize-1,tileSize-1)
pathIndex+=1;
if (pathIndex < path.length) {recursor()}
}
recursor();
}
let stepCount = 0;
steps.textContent = 0;
let pathLen = 0;
pathLength.textContent = 0;
recursiveLoop();
function recursiveLoop() {
if (cancelPath) {return 0};
stepCount += 1;
steps.textContent = stepCount;
gDisplay.textContent = current.gCost;
hDisplay.textContent = current.hCost;
fDisplay.textContent = current.fCost;
//-------------------------Draw Progress------------------------//
onScreenCTX.clearRect(0,0,onScreenCVS.width,onScreenCVS.height);
for (let i=0; i<offScreenCVS.height; i++) {
for (let j=0; j<offScreenCVS.width; j++) {
onScreenCTX.beginPath();
onScreenCTX.rect(j*tileSize, i*tileSize, tileSize, tileSize);
onScreenCTX.stroke();
}
}
if (mapNodes) {
freeTiles.forEach(n => {
onScreenCTX.fillStyle = "rgb(196, 188, 178)";
onScreenCTX.fillRect(n.x*tileSize+0.5,n.y*tileSize+0.5,tileSize-1,tileSize-1);
})
}
walls.forEach(w => {
onScreenCTX.fillStyle = "black";
onScreenCTX.fillRect(w.x*tileSize+0.5,w.y*tileSize+0.5,tileSize-1,tileSize-1);
})
open.forEach(n => {
onScreenCTX.fillStyle = "rgb(33,181,235)";
onScreenCTX.fillRect(n.x*tileSize+0.5,n.y*tileSize+0.5,tileSize-1,tileSize-1);
if (tileSize === 64) {
onScreenCTX.fillStyle = "black";
onScreenCTX.font = `${tileSize/5}px Arial`;
onScreenCTX.fillText(n.fCost, n.x*tileSize,n.y*tileSize+(tileSize/4));
}
});
closed.forEach(n => {
onScreenCTX.fillStyle = `rgba(222,0,0,${n.hCost/n.fCost})`;
onScreenCTX.fillRect(n.x*tileSize+0.5,n.y*tileSize+0.5,tileSize-1,tileSize-1);
onScreenCTX.fillStyle = `rgba(83,222,2,${n.gCost/n.fCost})`;
onScreenCTX.fillRect(n.x*tileSize+0.5,n.y*tileSize+0.5,tileSize-1,tileSize-1);
})
function progressPath() {
let curr = current;
let tempPath = [];
while(curr.parent) {
tempPath.push(curr);
curr = curr.parent;
}
if (tempPath.length>1) {drawTempPath(tempPath.reverse())};
}
progressPath()
onScreenCTX.fillStyle = "orange";
onScreenCTX.fillRect(start.x*tileSize+0.5,start.y*tileSize+0.5,tileSize-1,tileSize-1);
onScreenCTX.fillStyle = "blue";
onScreenCTX.fillRect(end.x*tileSize+0.5,end.y*tileSize+0.5,tileSize-1,tileSize-1);
onScreenCTX.fillStyle = "purple";
onScreenCTX.fillRect(current.x*tileSize+0.5,current.y*tileSize+0.5,tileSize-1,tileSize-1);
if (tileSize === 64) {
onScreenCTX.fillStyle = "black";
onScreenCTX.fillText(current.fCost, current.x*tileSize,current.y*tileSize+(tileSize/4));
}
//------------------------Drawing Done----------------------------//
//Remove lowest fCost from open and add it to closed
open.delete(current);
closed.add(current);
//End case
if (current === end) {
let curr = current;
let tempPath = [];
while(curr.parent) {
tempPath.push(curr);
curr = curr.parent;
}
tempPath.push(curr);
//-------------------Draw Path-----------------------//
let truePath = tempPath.reverse();
drawPath(truePath, delaySlider.value);
return truePath;
}
let neighbArray = Object.entries(current.neighbors);
for (let i=0; i<neighbArray.length; i++) {
let neighbor = neighbArray[i][1];
let dir = neighbArray[i][0];
if (neighbor.type === "wall" || closed.has(neighbor)) {
continue;
}
//Check corners
if (dir === "northeast") {
if ((current.neighbors.north.type === "wall")&&(current.neighbors.east.type === "wall")) {
continue;
}
if (cornerBuffer) {
if ((current.neighbors.east.type === "wall")) {
continue;
}
if ((current.neighbors.north.type === "wall")) {
continue;
}
}
}
if (dir === "northwest") {
if ((current.neighbors.north.type === "wall")&&(current.neighbors.west.type === "wall")) {
continue;
}
if (cornerBuffer) {
if ((current.neighbors.west.type === "wall")) {
continue;
}
if ((current.neighbors.north.type === "wall")) {
continue;
}
}
}
if (dir === "southeast") {
if ((current.neighbors.south.type === "wall")&&(current.neighbors.east.type === "wall")) {
continue;
}
if (cornerBuffer) {
if ((current.neighbors.east.type === "wall")) {
continue;
}
if ((current.neighbors.south.type === "wall")) {
continue;
}
}
}
if (dir === "southwest") {
if ((current.neighbors.south.type === "wall")&&(current.neighbors.west.type === "wall")) {
continue;
}
if (cornerBuffer) {
if ((current.neighbors.west.type === "wall")) {
continue;
}
if ((current.neighbors.south.type === "wall")) {
continue;
}
}
}
function checkFree(tile, prev) {
//Is this actually more efficient? Free tiles don't need their neighbors iterated over.
if (tile.type === "free") {
progressSearch(tile, prev);
open.delete(tile);
closed.add(tile);
if (tile.dir === "horizontal") {
if (tile.neighbors.east === prev) {
checkFree(tile.neighbors.west, tile);
} else if (tile.neighbors.west === prev) {
checkFree(tile.neighbors.east, tile);
}
} else if (tile.dir === "vertical") {
if (tile.neighbors.north === prev) {
checkFree(tile.neighbors.south, tile);
} else if (tile.neighbors.south === prev) {
checkFree(tile.neighbors.north, tile);
}
}
} else {
progressSearch(tile, prev)
}
}
function progressSearch(tile, current) {
let tCost = euclid(tile,current)*tile.cost;
if (!(open.has(tile)||closed.has(tile))) {
if (tile!=start) {tile.parent = current;}
open.add(tile);
//Round the costs to take care of floating point errors.
tile.gCost = calcGCost(tile);
tile.hCost = Math.floor((calcHCost(tile, end) + tieBreak(tile))*decPlace)/decPlace;
tile.fCost = calcFCost(tile.gCost, tile.hCost);
} else if (open.has(tile)&&tile.gCost > current.gCost+tCost) {
if (tile!=start) {tile.parent = current;}
tile.gCost = calcGCost(tile);
tile.fCost = calcFCost(tile.gCost, tile.hCost);
}
}
if (mapNodes) {
//extend past free tiles
checkFree(neighbor, current);
} else {
//For new tiles
progressSearch(neighbor, current);
}
}
//make current lowest fCost
let arr = [...open]
arr.sort(compareFCost)
current = arr[0]
if (open.size>0) {setTimeout(recursiveLoop, delaySlider.value)} else {cancelPathfinding()};
//instant
// if (true) {recursiveLoop()};
}
}
//---------------------------Find Path-----------------------------//
let generateBtn = document.querySelector(".generate-btn")
generateBtn.addEventListener("click", makePath);
generateMap();
function makePath() {
generateMap();
cancelPath = false;
findPath();
generateBtn.disabled = true;
}
//--------------------------Clear Grid-----------------------------//
let clearBtn = document.querySelector(".clear-btn");
let cancelBtn = document.querySelector(".cancel-btn");
let cancelPath = false;
clearBtn.addEventListener("click", clearGrid);
cancelBtn.addEventListener("click",cancelPathfinding);
function cancelPathfinding() {
generateMap();
cancelPath = true;
generateBtn.disabled = false;
// renderImage();
}
function clearGrid(e) {
cancelPathfinding();
onScreenCTX.clearRect(0,0,onScreenCVS.width,onScreenCVS.height);
offScreenCTX.clearRect(0,0,offScreenCVS.width,offScreenCVS.height);
generateMap();
}
//----------------------Adjust Tile Cost---------------------------//
//Create options for drawing tiles that cost more than 1
//-----------------------Node Efficiency---------------------------//
//Create an option for mapping key nodes of map to reduce calculation time
//-------------------------Maze methods----------------------------//
//naive algorithm, not guaranteed solvable, O(n)
function generateNaiveMaze(e) {
cancelPathfinding();
offScreenCTX.fillStyle = "black";
offScreenCTX.clearRect(0,0,offScreenCVS.width,offScreenCVS.height);
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
let cells = [];
for (let y = 0; y < imageData.height; y++) {
if (y%2 === 1) {
continue;
}
cells[y] = [];
for (let x = 0; x < imageData.width; x++) {
if (x%2 === 1) {
continue;
}
cells[y][x] = {x: x, y: y}
}
}
//draw
function drawMaze1() {
cells.forEach(r => {
r.forEach(c => {
offScreenCTX.fillRect(c.x,c.y,1,1);
})
})
source = offScreenCVS.toDataURL();
renderImage();
window.setTimeout(drawMaze2, delaySlider.value)
}
function drawMaze2() {
cells.forEach(r => {
r.forEach(c => {
let rand = [[0,1],[0,-1],[1,0],[-1,0]];
let randC = rand[Math.floor(Math.random() * 4)];
offScreenCTX.fillRect(c.x+randC[0],c.y+randC[1],1,1);
})
})
source = offScreenCVS.toDataURL();
renderImage();
}
drawMaze1();
}
// Eller's algorithm
function generateEllerMaze(e) {
cancelPathfinding();
offScreenCTX.fillStyle = "black";
offScreenCTX.fillRect(0,0,offScreenCVS.width,offScreenCVS.height);
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
let cells = [];
//Generate Maze
for (let y = 0; y < imageData.height; y++) {
if (y%2 === 0) {
continue;
}
//Step 1: Initialize empty row if it doesn't exist
let rowSets = {};
if (!cells[y]) {cells[y] = []};
for (let x = 0; x < imageData.width; x++) {
if (x%2 === 0) {
continue;
}
if (!cells[y][x]) {
//Step 2: create each cell in this row if it doesn't exist yet, assign a unique set
let setID = `${y}${x}`;
let uniqueSet = new Set()
let cell = {x: x, y: y, set: setID, connections: {}};
cells[y][x] = cell;
//add to set
uniqueSet.add(cell);
//add to row sets
rowSets[setID] = uniqueSet;
} else {
//add existing cells to row sets
let cell = cells[y][x];
if (rowSets[cell.set]) {
rowSets[cell.set].add(cell);
} else {
let uniqueSet = new Set();
uniqueSet.add(cell);
rowSets[cell.set] = uniqueSet;
}
}
}
function removeWall() {return Math.random() > 0.5;}
//Step 3: Create right connections
cells[y].forEach(c => {
let rightCell = cells[y][c.x+2];
//if right cell are in different sets, check remove wall
if (rightCell) {
if (c.set !== rightCell.set) {
if (removeWall() || y===imageData.height-1) {
//open the right path
c.connections.right = true;
let oldSet = rightCell.set;
//merge right cell's set into left cell's set
rowSets[oldSet].forEach(rc => {
rc.set = c.set;
rowSets[c.set].add(rc);
})
delete rowSets[oldSet];
}
}
}
})
//Step 4: Create down connections
//only continue if not on last row
if (y < imageData.height-1) {
Object.entries(rowSets).forEach(kv => {
let connects = 0;
let last;
let thisSet = kv[1];
let thisSetID = kv[0];
//if set only has one entry, create a path down
thisSet.forEach(c => {
//check removeWall or if this is the last row of the maze
if (removeWall() || thisSet.size === 1) {
//open the down path
c.connections.down = true;
connects += 1;
if (!cells[y+2]) {cells[y+2] = []};
let downCell = {x: c.x, y: y+2, set: thisSetID, connections: {}};
cells[y+2][c.x] = downCell;
}
last = c;
})
//make sure at least one connects
if (connects === 0) {
//open the down path
last.connections.down = true;
if (!cells[y+2]) {cells[y+2] = []};
let downCell = {x: last.x, y: y+2, set: thisSetID, connections: {}};
cells[y+2][last.x] = downCell;
}
})
}
}
//draw
let j = 1;
function recursiveDrawMaze() {
cells[j].forEach(c => {
if (c) {
offScreenCTX.clearRect(c.x,c.y,1,1);
if (c.connections.right) {
offScreenCTX.clearRect(c.x+1,c.y,1,1);
}
if (c.connections.down) {
offScreenCTX.clearRect(c.x,c.y+1,1,1);
}
}
})
j+=2;
source = offScreenCVS.toDataURL();
renderImage();
if (j < cells.length) {
window.setTimeout(recursiveDrawMaze, delaySlider.value)
}
}
recursiveDrawMaze();
}
//Prim's Algorithm - spanning tree
function generatePrimMaze(e) {
cancelPathfinding();
offScreenCTX.fillStyle = "black";
offScreenCTX.fillRect(0,0,offScreenCVS.width,offScreenCVS.height);
let imageData = offScreenCTX.getImageData(0,0,offScreenCVS.width,offScreenCVS.height);
let cells = [];
let mazeHeight = imageData.height/2;
let mazeWidth = imageData.width/2;
//Generate grid template
for (let y = 0; y < mazeHeight; y++) {
//Step 1: Initialize empty row
cells[y] = [];
// mazeWidth = y+1;
for (let x = 0; x < mazeWidth; x++) {
//Step 2: create each cell in this row
let cell = {x: x, y: y, index: [x,y], status: "unvisited", adjacents: [], connections: []};
cells[y][x] = cell;
//add adjacents
if (cells[y-1]) {
if (cells[y-1][x]) {
let up = cells[y-1][x];
cell.adjacents.push(up);
up.adjacents.push(cell);
}
}
if (cells[y][x-1]) {
let left = cells[y][x-1];
cell.adjacents.push(left);
left.adjacents.push(cell);
}
}
}
//initialize empty visited set and frontier set
let visited = new Set();
let frontier = new Set();
//get random index pair as starting point and add it to visited set
//MAKE EACH ROW A UNIQUE SET TO ALLOW COORDINATE OFFSET?
let startY = Math.floor(Math.random() * cells.length);
let startX = Math.floor(Math.random() * cells[startY].length);
let start = cells[startY][startX];
//Initialize starting cell as frontier
frontier.add(start);
//Set start as current
let current = start;
recursiveSpanningTree();
function recursiveSpanningTree() {
//remove current from unvisited and add it to visited
frontier.delete(current);