-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
436 lines (372 loc) · 11.5 KB
/
main.go
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
package main
import (
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"math/rand"
"strconv"
"syscall/js"
"time"
"unsafe"
)
// Simple timing functions, put
// tr(ace(message))
// at the top of a timed function.
func ace(message string) (string, time.Time) {
return message, time.Now()
}
func tr(message string, start time.Time) {
fmt.Printf("%v: %v\n", message, time.Since(start))
}
// The frame buffer storing our image.
var frameBuffer *image.RGBA = nil
// Mazes are simple structures.
type maze struct {
start, finish position
height, width int
cells []cell
rng *rand.Rand
}
func (m *maze) at(p position) *cell {
return &m.cells[p.y*m.width+p.x]
}
const (
maxDimension = 200 // Maximum number of cells in height and/or width
border = 40 // Border (in pixels) around the maze
cellWidth = 12 // Width/height (in pixels) of a single cell
halfCellWidth = cellWidth / 2 // Used to find the midpoint of a cell
minImageWidth = cellWidth*4 + border*2 // Minimum width of generated image (in pixels)
)
// Directions, and displacements to move in a given direction.
type direction int
const (
north direction = iota
south
east
west
)
var outOfBounds = errors.New("out of bounds")
func (d direction) translate(p position, m *maze) (position, error) {
switch d {
case north:
if p.y > 0 {
return position{x: p.x, y: p.y - 1}, nil
}
case south:
if p.y < m.height-1 {
return position{x: p.x, y: p.y + 1}, nil
}
case west:
if p.x > 0 {
return position{x: p.x - 1, y: p.y}, nil
}
case east:
if p.x < m.width-1 {
return position{x: p.x + 1, y: p.y}, nil
}
}
return p, outOfBounds
}
func (d direction) opposite() direction {
return map[direction]direction{
north: south,
south: north,
east: west,
west: east,
}[d]
}
// A single cell.
type cell struct {
openings [4]bool // Whether a given wall is open.
}
// Build a new maze with the given height and width.
// Randomness is taken from the given RNG.
// oppositeStart means to place start/end at opposing corners.
func newMaze(height, width int, rng *rand.Rand, oppositeStart bool) *maze {
if width < 2 || height < 2 || rng == nil {
panic("invalid call to newMaze")
}
start := position{rng.Intn(width), 0}
end := position{rng.Intn(width), height - 1}
if oppositeStart {
start = position{0, 0}
end = position{width - 1, height - 1}
}
return &maze{
start: start,
finish: end,
height: height,
width: width,
cells: make([]cell, height*width),
rng: rng,
}
}
// Position is simply x/y coordinates.
type position struct {
x, y int
}
// We use a stack of positions when generating and solving
// the maze. This avoids using the call stack. Go has a very
// deep call stack on most targets, but I'm not comfortable
// asking WASM to give us a ~1500-level stack.
type stack struct {
stack []position
}
func (s *stack) push(p position) {
s.stack = append(s.stack, p)
}
func (s stack) peek() position {
if len(s.stack) == 0 {
panic("stack underflow")
}
return s.stack[len(s.stack)-1]
}
func (s *stack) pop() position {
p := s.peek()
s.stack = s.stack[:len(s.stack)-1]
return p
}
func (s stack) empty() bool {
return len(s.stack) == 0
}
func (s stack) len() int {
return len(s.stack)
}
// We precompute all possible permutations of orders to try digging.
// This speeds up maze generation by ~25% from shuffling the directions
// on each iteration through the maze generation loop.
var permutations = [][]direction{
[]direction{north, south, east, west},
[]direction{north, south, west, east},
[]direction{north, east, south, west},
[]direction{north, east, west, south},
[]direction{north, west, south, east},
[]direction{north, west, east, south},
[]direction{south, north, east, west},
[]direction{south, north, west, east},
[]direction{south, east, north, west},
[]direction{south, east, west, north},
[]direction{south, west, north, east},
[]direction{south, west, east, north},
[]direction{east, north, south, west},
[]direction{east, north, west, south},
[]direction{east, south, north, west},
[]direction{east, south, west, north},
[]direction{east, west, north, south},
[]direction{east, west, south, north},
[]direction{west, north, south, east},
[]direction{west, north, east, south},
[]direction{west, south, north, east},
[]direction{west, south, east, north},
[]direction{west, east, north, south},
[]direction{west, east, south, north},
}
type visitedMap map[position]bool
func (m visitedMap) contains(p position) (ok bool) {
_, ok = m[p]
return
}
func (m *maze) carve(p position, d direction) {
m.at(p).openings[d] = true
if np, err := d.translate(p, m); err == nil {
m.at(np).openings[d.opposite()] = true
}
}
func (m *maze) generate() {
defer tr(ace("generating maze"))
stack := stack{[]position{m.start}}
visited := make(visitedMap)
for !stack.empty() {
found := false
p := stack.peek()
dirs := permutations[m.rng.Intn(len(permutations))]
for _, dir := range dirs {
np, err := dir.translate(p, m)
if err == nil && !visited.contains(np) {
m.carve(p, dir)
visited[np] = true
stack.push(np)
found = true
break
}
}
if !found {
stack.pop()
}
}
m.at(m.start).openings[north] = true
m.at(m.finish).openings[south] = true
}
// Solve via depth-first search.
// Use the same stack mechanism as the maze generator.
func (m maze) solve() []position {
defer tr(ace("solving maze"))
stack := stack{[]position{m.start}}
visited := make(visitedMap)
visited[m.start] = true
SEARCH:
for !stack.empty() {
if visited.contains(m.finish) {
return stack.stack
}
pos := stack.peek()
for _, dir := range []direction{north, south, east, west} {
if np, err := dir.translate(pos, &m); err == nil && !visited.contains(np) && m.at(pos).openings[dir] {
visited[np] = true
stack.push(np)
continue SEARCH
}
}
stack.pop()
}
panic("maze has no solution")
}
// Draw the maze to an image.
func (m *maze) draw() *image.RGBA {
defer tr(ace("drawing maze"))
width := m.width*cellWidth + border*2
if width < minImageWidth {
width = minImageWidth
}
bounds := image.Rect(0, 0, width, m.height*cellWidth+border*2)
if frameBuffer == nil || frameBuffer.Bounds() != bounds {
frameBuffer = image.NewRGBA(bounds)
}
fill(frameBuffer, 0, m.height*cellWidth+border*2, 0, width, image.White)
for y := 0; y < m.height; y++ {
for x := 0; x < m.width; x++ {
m.drawCell(frameBuffer, x, y, m.at(position{x: x, y: y}))
}
}
return frameBuffer
}
// Preallocate the red image; this is what we use as a
// stamp to draw our solution if requested.
var red = image.NewUniform(color.RGBA{255, 0, 0, 255})
// Draw the solution path.
// Note that we sort our origin and destination points
// so that we don't have any gaps and it's a nice smooth
// connected path.
func (m *maze) drawPath(img *image.RGBA, path []position) {
defer tr(ace("drawing solution"))
prev := path[0]
for _, pos := range path[1:] {
if pos.x == prev.x {
first, last := prev, pos
if first.y > last.y {
first, last = last, first
}
vLine(img, first.x*cellWidth+border+halfCellWidth, first.y*cellWidth+border+halfCellWidth, last.y*cellWidth+border+halfCellWidth, red)
}
if pos.y == prev.y {
first, last := prev, pos
if first.x > last.x {
first, last = last, first
}
hLine(img, first.x*cellWidth+border+halfCellWidth, first.y*cellWidth+border+halfCellWidth, last.x*cellWidth+border+halfCellWidth, red)
}
prev = pos
}
}
// Fill the image with a given color.
func fill(img *image.RGBA, y0, y1, x0, x1 int, color color.Color) {
defer tr(ace("clearing image"))
draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.Point{0, 0}, draw.Src)
}
// Draw a horizontal line from p1 -> p2.
func hLine(img *image.RGBA, x1, y, x2 int, col image.Image) {
draw.Draw(img, image.Rect(x1, y, x2+1, y+1), col, image.Point{0, 0}, draw.Over)
}
// Draw a vertical line from p1 -> p2.
func vLine(img *image.RGBA, x, y1, y2 int, col image.Image) {
draw.Draw(img, image.Rect(x, y1, x+1, y2+1), col, image.Point{0, 0}, draw.Over)
}
// Draw an individual cell.
func (m *maze) drawCell(img *image.RGBA, x, y int, c *cell) {
if !c.openings[north] {
hLine(img, x*cellWidth+border, y*cellWidth+border, x*cellWidth+border+cellWidth, image.Black)
}
if !c.openings[south] {
hLine(img, x*cellWidth+border, y*cellWidth+border+cellWidth, x*cellWidth+border+cellWidth, image.Black)
}
if !c.openings[west] {
vLine(img, x*cellWidth+border, y*cellWidth+border, y*cellWidth+border+cellWidth, image.Black)
}
if !c.openings[east] {
vLine(img, x*cellWidth+border+cellWidth, y*cellWidth+border, y*cellWidth+border+cellWidth, image.Black)
}
}
// We import a function called putMaze, which is written in JavaScript.
// TinyGo makes this slightly easier, but this really isn't too bad:
var putMaze js.Value = js.Global().Get("putMaze")
func main() {
generateCb := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
generateCallback()
args[0].Call("preventDefault")
return nil
})
defer generateCb.Release()
js.Global().Get("document").
Call("getElementById", "generateButton").
Call("addEventListener", "click", generateCb)
// spin a while...spin FOREVER
// we do this so that we don't fall off the end of main and collect
// garbage, which could move our framebuffer pointer or do other
// breaking things
select {}
}
// The actual function called to generate mazes.
func generateCallback() {
defer tr(ace("total time"))
height, width, solution, label, oppositeStart, seed, err := getArguments()
if err != nil || height < 2 || width < 2 || height > maxDimension || width > maxDimension {
fmt.Printf("Error: %s\n", err)
return
}
if seed == 0 {
seed = time.Now().UnixNano()
}
m := newMaze(int(height), int(width), rand.New(rand.NewSource(seed)), oppositeStart)
m.generate()
img := m.draw()
if solution {
m.drawPath(img, m.solve())
}
labelText := ""
if label {
labelText = fmt.Sprintf("%dx%d %x", m.height, m.width, seed)
}
export(labelText)
}
// Grab our parameters from JS land.
func getArguments() (height, width int64, solution, label, oppositeStart bool, seed int64, err error) {
document := js.Global().Get("document")
height, err = strconv.ParseInt(document.Call("getElementById", "mazeHeight").Get("value").String(), 10, 16)
width, err = strconv.ParseInt(document.Call("getElementById", "mazeWidth").Get("value").String(), 10, 16)
seed, err = strconv.ParseInt(document.Call("getElementById", "randomSeed").Get("value").String(), 10, 64)
solution = document.Call("getElementById", "showSolution").Get("checked").Truthy()
label = document.Call("getElementById", "labelMaze").Get("checked").Truthy()
oppositeStart = document.Call("getElementById", "oppositeStart").Get("checked").Truthy()
return
}
// Export the frame buffer. We invoke putMaze here, which actually
// puts the pixel data into the canvas.
//
// Note that image.RGBA.Pix just happens to be in the correct format
// for Canvas ImageData. This means that we can simply pass a pointer
// into WASM linear memory and the JS side can pick it up with no
// copying. We do a safe cast from the slice to the underlying array,
// and then an unsafe cast to a uintptr, which is the offset of the
// frame buffer in linear memory.
func export(label string) {
defer tr(ace("exporting frame buffer"))
putMaze.Invoke(
js.ValueOf(frameBuffer.Bounds().Dy()),
js.ValueOf(frameBuffer.Bounds().Dx()),
js.ValueOf(uintptr(unsafe.Pointer((*[1]uint8)(frameBuffer.Pix)))),
js.ValueOf(len(frameBuffer.Pix)),
js.ValueOf(label),
)
}