-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt6.html
407 lines (322 loc) · 10.4 KB
/
pt6.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Maze</title>
<style>
body {
background-color: #888888;
}
</style>
</head>
<body>
<center><canvas id="myCanvas" width="800" height="800" style="border:1px solid #000000;"></canvas></center>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
// You need a context to draw in a HTML5 canvas.
// You can get it by grabbing the canvas (by its ID) then doing .getContext.
// You usually want "2d" but you can ask for "webgl", "webgl2" or "webgpu" to do 3D rendering which is way more advanced than this.
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
// As a final exercise, we upgrade our map so that each ring has twice the number of segments as the one that came before it.
class Cell {
constructor() {
this.inner_wall = true;
this.right_wall = true;
this.visited = false;
}
}
// Stolen functions.
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
function radToDeg(rad) {
return rad / (Math.PI / 180);
}
function getRandomInt(r, min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(r() * (max - min + 1)) + min;
}
function sfc32(a, b, c, d) {
return function() {
a |= 0; b |= 0; c |= 0; d |= 0;
let t = (a + b | 0) + d | 0;
d = d + 1 | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
}
const origin_x = c.width / 2;
const origin_y = c.height / 2;
const ring_height = 40;
const ring_count = 8;
const first_ring_divisions = 6;
// We create our empty maze data here.
var maze = new Array(ring_count);
function get_ring_divisions(ring)
{
return first_ring_divisions * Math.pow(2, Math.floor(ring / 2));
}
for (let i = 0; i < ring_count; i++)
{
let ring_divisions = get_ring_divisions(i);
maze[i] = new Array(ring_divisions);
for (let j = 0; j < ring_divisions; j++)
{
maze[i][j] = new Cell();
}
}
function unvisited_neighbours(ring, division)
{
let out_neighbours = [];
let ring_divisions = get_ring_divisions(ring);
// The division on our left.
let division_left = (division+ring_divisions-1) % ring_divisions;
if (!maze[ring][division_left].visited)
{
out_neighbours.push([ring, division_left]);
}
// The division on our right.
let division_right = (division+1) % ring_divisions;
if (!maze[ring][division_right].visited)
{
out_neighbours.push([ring, division_right]);
}
// These depend on if we are even or odd.
if (ring & 1)
{
// Odd case
// The inner division next to us is the same division as us.
if (ring > 0)
{
let division_inner = division;
if (!maze[ring-1][division_inner].visited)
{
out_neighbours.push([ring-1, division_inner]);
}
}
// We have *two* outer divisions. The first one is our index * 2, the second that + 1
//console.log(ring, division);
if (ring < ring_count-1)
{
let division_outer_1 = division * 2;
let division_outer_2 = (division * 2) + 1
if (!maze[ring+1][division_outer_1].visited)
{
out_neighbours.push([ring+1, division_outer_1]);
}
if (!maze[ring+1][division_outer_2].visited)
{
out_neighbours.push([ring+1, division_outer_2]);
}
}
}
else
{
// Even case
// The inner division next to us is the same for the division on our right if we're odd, or the division on our left if we're even (we don't care about this here just useful info)
if (ring > 0)
{
let division_inner = Math.floor(division / 2);
if (!maze[ring-1][division_inner].visited)
{
out_neighbours.push([ring-1, division_inner]);
}
}
// We have 1 outer division that is unique to us.
if (ring < ring_count-1)
{
let division_outer = division;
if (!maze[ring+1][division_outer].visited)
{
out_neighbours.push([ring+1, division_outer]);
}
}
}
return out_neighbours;
}
function open_wall_between(cand1, cand2)
{
if (cand1[0] == cand2[0])
{
// rings are the same, the division must be different.
// this is the nice, easy case.
let division1 = cand1[1];
let division2 = cand2[1];
let ring_divisions = get_ring_divisions(cand1[0]);
let division1_next = (division1 + 1) % ring_divisions;
if (division2 == division1_next)
{
// We now know cand1 is on the left
maze[cand1[0]][cand1[1]].right_wall = false;
}
else
{
// Just assume the opposite
maze[cand2[0]][cand2[1]].right_wall = false;
}
}
else
{
// its the rings that are different.
// We need to open the inner wall on the outermost ring.
let ring1 = cand1[0];
let ring2 = cand2[0];
let ring1_next = ring1 + 1;
if (ring2 == ring1_next)
{
// We now know cand1 is on the inside
//console.log("opened", cand2[0], cand2[1]);
maze[cand2[0]][cand2[1]].inner_wall = false;
}
else
{
// Just assume the opposite
//console.log("opened", cand1[0], cand1[1]);
maze[cand1[0]][cand1[1]].inner_wall = false;
}
}
}
let current_best_entry_backtrack = [];
// Everything below here is the maze generation code.
if (1)
{
// This is the random number generator
let r = Math.random; //sfc32(11, 12, 15, 2);
// Start at the outer ring, division 0
let ring = ring_count - 1;
let division = 0;
maze[ring][division].visited = true;
// Immediately open the outer wall for that space (this is our maze entrance)
maze[ring][division].outer_wall = false;
// Also immediately remove all walls in ring 0, this is our goal
for (let i = 0; i < first_ring_divisions; i++)
{
maze[0][i].right_wall = false;
// the maze gen is never allowed to enter the the "goal".
// At the end we will open up the wall in the final inner ring that we encountered last in our backtrack, which hopefully will be a really hard one to reach.
maze[0][i].visited = true;
}
backtrack_path = [];
let current_best_entry = 0; // Current hardest to reach entry point division for ring 0
// path loop
let candidates = unvisited_neighbours(ring, division);
do
{
//console.log(ring, division);
// If there are any candidates, choose one at random to visit
if (candidates.length != 0)
{
backtrack_path.push([ring, division]);
let cand = candidates[getRandomInt(r, 0, candidates.length-1)];
open_wall_between([ring, division], cand);
ring = cand[0];
division = cand[1];
maze[ring][division].visited = true;
// Remember to set current_best_entry if we just entered ring 1
if (ring == 1)
{
current_best_entry = division;
current_best_entry_backtrack = [...backtrack_path, [ring, division], [0, division]];
}
//console.log("Entered");
}
// Otherwise, go up the backtrack path
else
{
let cand = backtrack_path.pop();
ring = cand[0];
division = cand[1];
//console.log("Backtracked");
}
candidates = unvisited_neighbours(ring, division);
}
while(candidates.length != 0 || backtrack_path.length != 0)
// Create an entry point to the center at the current hardest to reach entry point and our maze is done!
maze[1][current_best_entry].inner_wall = false;
}
// Everything below here is the drawing code.
for (let i = 0; i < ring_count; i++)
{
const ring_divisions = get_ring_divisions(i);
const division_degrees = degToRad(360) / ring_divisions;
for (let j = 0; j < ring_divisions; j++)
{
let radius_inner = ring_height * i;
let radius_outer = ring_height * (i+1);
let arc_start = division_degrees * j;
let arc_end = (division_degrees * (j+1));
// cell inner arc
if (maze[i][j].inner_wall)
{
ctx.beginPath();
ctx.arc(origin_x, origin_y, radius_inner, arc_start, arc_end);
ctx.stroke();
}
// cell right wall - we have to manually calculate the start position of both arcs
// APIs won't do everything for you! It's worth knowing the equations for circle calculations
if (maze[i][j].right_wall)
{
ctx.beginPath();
let start_x = radius_inner * Math.cos(arc_end) + origin_x;
let start_y = radius_inner * Math.sin(arc_end) + origin_y;
let end_x = radius_outer * Math.cos(arc_end) + origin_x;
let end_y = radius_outer * Math.sin(arc_end) + origin_y;
ctx.moveTo(start_x, start_y);
ctx.lineTo(end_x, end_y);
ctx.stroke();
}
// We don't draw the left wall because *we don't need to*. The cell before us already had a left inner wall in every case.
}
}
// Draw the outer arc, always the same
ctx.beginPath();
ctx.arc(origin_x, origin_y, ring_height * ring_count, 0, degToRad(360) / (get_ring_divisions(ring_count-1)), true);
ctx.stroke();
// Draw the best backtrack path
ctx.strokeStyle = "#FF0000";
ctx.setLineDash([3, 10]);
ctx.lineWidth = 3;
ctx.beginPath();
let last_ring = -1;
let last_division = -1;
for (let i = 0; i < current_best_entry_backtrack.length; i++)
{
const entry = current_best_entry_backtrack[i];
const ring = entry[0];
const division = entry[1];
const ring_divisions = get_ring_divisions(ring);
const division_degrees = degToRad(360) / ring_divisions;
const arc = (division_degrees * division) + (division_degrees / 2);
const radius = (ring * ring_height) + (ring_height / 2);
const x = radius * Math.cos(arc) + origin_x;
const y = radius * Math.sin(arc) + origin_y;
if (i == 0)
{
ctx.moveTo(x, y);
}
else
{
if (last_ring == ring)
{
let arc_begin = (division_degrees * last_division) + (division_degrees / 2);
let arc_end = (division_degrees * division) + (division_degrees / 2);
let next_division = (last_division + 1) % ring_divisions; // incredibly terrible clockwise detection
ctx.arc(origin_x, origin_y, radius, arc_begin, arc_end, division != next_division);
}
else
{
ctx.lineTo(x, y);
}
}
last_ring = ring;
last_division = division;
}
ctx.stroke();
</script>
</body>
</html>