-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
203 lines (182 loc) · 5.46 KB
/
script.js
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
import DEFINITION from './carcassonne/index.js'
// import DEFINITION from './cpu/index.js'
// import DEFINITION from './abstract/index.js'
const GRID_SIZE = [30, 20]
const NEIGHBORS = [
[0, -1],
[1, 0],
[0, 1],
[-1, 0],
]
const canvas = document.querySelector('canvas')
if(!canvas)
throw new Error('No canvas found')
canvas.width = window.innerWidth * devicePixelRatio * 2
canvas.height = window.innerHeight * devicePixelRatio * 2
const ctx = canvas.getContext('2d')
if(!ctx)
throw new Error('No context found')
start(ctx)
async function start(ctx) {
const array = createNewGrid(GRID_SIZE)
const backtrack = { cursor: 0, stack: [] }
loop(ctx, array, backtrack)
}
async function loop(ctx, array, backtrack) {
const cells = getMinEntropyCells(array)
if (cells.length === 0) {
console.log('done')
return
}
const randomCell = cells[Math.floor(Math.random() * cells.length)]
// const randomCell = cells[0]
try {
assignSuitableOption(array, randomCell, backtrack)
collapseNeighbors(array, randomCell)
backtrack.cursor = Math.max(0, backtrack.cursor - DEFINITION.backtrack.decrease)
draw(ctx, array)
await frame()
loop(ctx, array, backtrack)
} catch (error) {
if(backtrack.stack.length > 0) {
const reset = resetToCursor(backtrack)
console.log('backtracking', Math.round(backtrack.cursor), error.message)
loop(ctx, reset, backtrack)
} else {
console.log('restarting', error.message)
start(ctx)
}
}
}
function resetToCursor(backtrack) {
backtrack.cursor += DEFINITION.backtrack.increase
const revertCount = Math.min(Math.floor(backtrack.cursor), backtrack.stack.length)
const [reset] = backtrack.stack.splice(backtrack.stack.length - revertCount)
return JSON.parse(reset)
}
function createNewGrid([width, height]) {
return new Array(width * height).fill(0).map((_, index, array) => {
const x = index % GRID_SIZE[0]
const y = Math.floor(index / GRID_SIZE[0])
const top = y === 0 ? DEFINITION.initial.border : DEFINITION.initial.inside
const right = x === width - 1 ? DEFINITION.initial.border : DEFINITION.initial.inside
const bottom = y === height - 1 ? DEFINITION.initial.border : DEFINITION.initial.inside
const left = x === 0 ? DEFINITION.initial.border : DEFINITION.initial.inside
const entropy = top.length + right.length + bottom.length + left.length
return ({
index,
x,
y,
entropy,
accepts: [
top,
right,
bottom,
left,
],
tile: null,
})
})
}
function assignSuitableOption(array, cell, backtrack) {
const options = DEFINITION.findSuitableTiles(cell.accepts)
if (options.length === 0)
throw new Error('No suitable options')
backtrack.stack.push(JSON.stringify(array))
const randomOption = options[Math.floor(Math.random() * options.length)]
cell.tile = randomOption
}
function collapseNeighbors(array, cell, stack = []) {
for (let i = 0; i < NEIGHBORS.length; i++) {
const vector = NEIGHBORS[i]
const x = cell.x + vector[0]
const y = cell.y + vector[1]
if (x < 0 || x >= GRID_SIZE[0] || y < 0 || y >= GRID_SIZE[1])
continue
const neighbor = getCellFromXY(array, x, y)
if (neighbor.tile)
continue
const possibilities = cell.tile
? [cell.tile.is[i]]
: cell.accepts[i]
const mirrorIndex = (i + 2) % 4
const compatibleOptions = DEFINITION.findSockets(possibilities, neighbor.accepts[mirrorIndex])
if (compatibleOptions.length === 0)
throw new Error('Invalid neighbor')
const entropyDifference = neighbor.accepts[mirrorIndex].length - compatibleOptions.length
if (entropyDifference !== 0)
stack.push(neighbor)
neighbor.accepts[mirrorIndex] = compatibleOptions
neighbor.entropy -= entropyDifference
// if (neighbor.entropy === 4)
// assignSuitableOption(array, neighbor)
}
if (stack.length > 0) {
const nextCell = stack.shift()
collapseNeighbors(array, nextCell, stack)
}
}
function getMinEntropyCells(array) {
let min = Infinity
let cells = []
for(let i = 0; i < array.length; i++) {
const cell = array[i]
if (cell.tile)
continue
if (cell.entropy < min) {
min = cell.entropy
cells = [cell]
} else if (cell.entropy === min) {
cells.push(cell)
}
}
return cells
}
function draw(ctx, array) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
for(let y = 0; y < GRID_SIZE[1]; y++) {
for(let x = 0; x < GRID_SIZE[0]; x++) {
const cell = getCellFromXY(array, x, y)
if(!cell.tile)
continue
const srcCoord = getSourceCoordFromIndex(cell.tile.index)
const destCoord = getDestinationCoordFromXY(ctx, x, y)
ctx.save()
ctx.translate(
destCoord[0] + destCoord[2] / 2,
destCoord[1] + destCoord[3] / 2
)
ctx.rotate(-cell.tile.rotate * Math.PI / 2)
ctx.drawImage(
DEFINITION.tileSet,
...srcCoord,
-destCoord[2] / 2,
-destCoord[3] / 2,
destCoord[2],
destCoord[3])
ctx.restore()
}
}
}
function getDestinationCoordFromXY(ctx, xIndex, yIndex) {
const cellSide = Math.min(
Math.floor(ctx.canvas.width / GRID_SIZE[0]),
Math.floor(ctx.canvas.height / GRID_SIZE[1]),
)
const x = xIndex * cellSide
const y = yIndex * cellSide
return [x, y, cellSide, cellSide]
}
function getSourceCoordFromIndex(i) {
const { width, height } = DEFINITION.params.tile
const { width: gridWidth } = DEFINITION.params.grid
const x = (i % gridWidth) * width
const y = Math.floor(i / gridWidth) * height
return [x, y, width, height]
}
function getCellFromXY(array, x, y) {
return array[y * GRID_SIZE[0] + x]
}
function frame() {
return new Promise(resolve => requestAnimationFrame(resolve))
}