|
1 | 1 |
|
2 |
| -function collapseLowestEntropy() { |
3 |
| - // Build an array of all cells with the lowest entropy |
4 |
| - let lowestEntropy = 10000000; |
5 |
| - let lowestIndexes= []; |
6 |
| - |
7 |
| - for (let i = 0; i < grid.length; i++) { |
8 |
| - let cell = grid[i]; |
9 |
| - if (cell.collapsed) |
10 |
| - continue |
11 |
| - let entropy = cell.options.size() |
12 |
| - if (entropy < lowestEntropy) { |
13 |
| - lowestIndexes = [i] |
14 |
| - lowestEntropy = entropy |
| 2 | +class HybridWFC extends MultiStepsAlgo { |
| 3 | + constructor() { |
| 4 | + super() |
| 5 | + } |
| 6 | + |
| 7 | + *run() { |
| 8 | + while (true) { |
| 9 | + // Build an array of all cells with the lowest entropy |
| 10 | + let lowestEntropy = 10000000; |
| 11 | + let lowestIndexes= []; |
| 12 | + |
| 13 | + for (let i = 0; i < grid.length; i++) { |
| 14 | + let cell = grid[i]; |
| 15 | + if (cell.collapsed) |
| 16 | + continue |
| 17 | + let entropy = cell.options.size() |
| 18 | + if (entropy < lowestEntropy) { |
| 19 | + lowestIndexes = [i] |
| 20 | + lowestEntropy = entropy |
| 21 | + } |
| 22 | + else if (entropy == lowestEntropy) { |
| 23 | + lowestIndexes.push(i) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // No lowest entropy means all cells are collapsed |
| 28 | + // we are done with the WFC |
| 29 | + if (lowestIndexes.length == 0) { |
| 30 | + clearContradictions() |
| 31 | + drawGrid() |
| 32 | + return this.finished() |
| 33 | + } |
| 34 | + |
| 35 | + // If the lowest entropy is zero, it means there is a cell |
| 36 | + // without any option left, we reached a contraction, so |
| 37 | + // rewind history and try again. |
| 38 | + if (lowestEntropy <= 0) { |
| 39 | + rewindHistory() |
| 40 | + yield 'Found zero-entropy contradiction' |
| 41 | + } |
| 42 | + |
| 43 | + // Pick a random cell with lowest entropy. |
| 44 | + let chosenCellIndex = random(lowestIndexes) |
| 45 | + |
| 46 | + if (!grid[chosenCellIndex].collapse()) { |
| 47 | + contradictions[chosenCellIndex] = 200 |
| 48 | + rewindHistory() |
| 49 | + yield 'Cannot collapse cell' |
| 50 | + } |
| 51 | + |
| 52 | + _updateGrid(chosenCellIndex) |
| 53 | + decreaseRewind() |
| 54 | + |
| 55 | + const cellX = chosenCellIndex % DIM |
| 56 | + const cellY = chosenCellIndex / DIM | 0 |
| 57 | + yield `Collapsed cell at ${cellX} / ${cellY}` |
15 | 58 | }
|
16 |
| - else if (entropy == lowestEntropy) { |
17 |
| - lowestIndexes.push(i) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function _updateCell(cellIndex, indexesNeedingUpdate, forceAdd) { |
| 63 | + let cell = grid[cellIndex] |
| 64 | + let newCell |
| 65 | + if (cell.collapsed) { |
| 66 | + newCell = cell |
| 67 | + } |
| 68 | + else { |
| 69 | + newCell = new Cell(cell.options) |
| 70 | + } |
| 71 | + |
| 72 | + const updatedIndexes = [] |
| 73 | + let cellChanged = forceAdd |
| 74 | + |
| 75 | + { |
| 76 | + if (cellIndex >= DIM) { |
| 77 | + let upIndex = cellIndex - DIM; |
| 78 | + const upCell = grid[upIndex] |
| 79 | + updatedIndexes.push(upIndex) |
| 80 | + if (!newCell.collapsed) { |
| 81 | + updateOtherOptions.clear() |
| 82 | + for (let option of upCell.options) { |
| 83 | + updateOtherOptions.in_place_union(tiles[option].down) |
| 84 | + } |
| 85 | + cellChanged |= newCell.options.in_place_intersection(updateOtherOptions) |
| 86 | + } |
18 | 87 | }
|
19 | 88 | }
|
20 | 89 |
|
21 |
| - // No lowest entropy means all cells are collapsed |
22 |
| - // we are done with the WFC |
23 |
| - if (lowestIndexes.length == 0) { |
24 |
| - clearContradictions() |
25 |
| - drawGrid() |
26 |
| - noLoop() |
27 |
| - return false |
| 90 | + { |
| 91 | + if (cellIndex < grid.length - DIM) { |
| 92 | + let downIndex = cellIndex + DIM; |
| 93 | + const downCell = grid[downIndex] |
| 94 | + updatedIndexes.push(downIndex) |
| 95 | + if (!newCell.collapsed) { |
| 96 | + updateOtherOptions.clear() |
| 97 | + for (let option of downCell.options) { |
| 98 | + updateOtherOptions.in_place_union(tiles[option].up) |
| 99 | + } |
| 100 | + cellChanged |= newCell.options.in_place_intersection(updateOtherOptions) |
| 101 | + } |
| 102 | + } |
28 | 103 | }
|
29 | 104 |
|
30 |
| - // If the lowest entropy is zero, it means there is a cell |
31 |
| - // wihtout any option left, we reached a contraction, so |
32 |
| - // rewind history and try again. |
33 |
| - if (lowestEntropy <= 0) { |
34 |
| - for (let cellIndex of lowestIndexes) { |
35 |
| - contradictions[cellIndex] = 200 |
36 |
| - logCellOptions(cellIndex, 'lowest entropy is zero') |
| 105 | + { |
| 106 | + if (cellIndex % DIM != 0) { |
| 107 | + let leftIndex = cellIndex - 1; |
| 108 | + const leftCell = grid[leftIndex] |
| 109 | + updatedIndexes.push(leftIndex) |
| 110 | + if (!newCell.collapsed) { |
| 111 | + updateOtherOptions.clear() |
| 112 | + for (let option of leftCell.options) { |
| 113 | + updateOtherOptions.in_place_union(tiles[option].right) |
| 114 | + } |
| 115 | + cellChanged |= newCell.options.in_place_intersection(updateOtherOptions) |
| 116 | + } |
37 | 117 | }
|
38 |
| - rewindHistory() |
39 |
| - return false |
40 | 118 | }
|
41 | 119 |
|
42 |
| - // Pick a random cell with lowest entropy. |
43 |
| - let chosenCellIndex = random(lowestIndexes) |
| 120 | + { |
| 121 | + let rightIndex = cellIndex + 1; |
| 122 | + if (rightIndex % DIM != 0) { |
| 123 | + const rightCell = grid[rightIndex] |
| 124 | + updatedIndexes.push(rightIndex) |
| 125 | + if (!newCell.collapsed) { |
| 126 | + updateOtherOptions.clear() |
| 127 | + for (let option of rightCell.options) { |
| 128 | + updateOtherOptions.in_place_union(tiles[option].left) |
| 129 | + } |
| 130 | + cellChanged |= newCell.options.in_place_intersection(updateOtherOptions) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
44 | 134 |
|
45 |
| - if (!grid[chosenCellIndex].collapse()) { |
46 |
| - contradictions[chosenCellIndex] = 200 |
47 |
| - logCellOptions(chosenCellIndex, 'cannot collapse cell') |
48 |
| - rewindHistory() |
49 |
| - return false |
| 135 | + if (cellChanged) { |
| 136 | + if (!cell.collapsed) |
| 137 | + cell.updated = true |
| 138 | + for (let index of updatedIndexes) { |
| 139 | + indexesNeedingUpdate.push(index) |
| 140 | + } |
50 | 141 | }
|
51 | 142 |
|
52 |
| - updateGrid(chosenCellIndex) |
| 143 | + return newCell |
| 144 | +} |
53 | 145 |
|
54 |
| - return true |
| 146 | +function _updateGrid(pickedCellIndex) { |
| 147 | + gridHistory.push(grid) |
| 148 | + grid = grid.slice(); |
| 149 | + |
| 150 | + // let updateCount = 0 |
| 151 | + let indexesNeedingUpdate = [] |
| 152 | + _updateCell(pickedCellIndex, indexesNeedingUpdate, true) |
| 153 | + while (indexesNeedingUpdate.length > 0) { |
| 154 | + // updateCount++ |
| 155 | + const index = indexesNeedingUpdate.pop() |
| 156 | + grid[index] = _updateCell(index, indexesNeedingUpdate, false) |
| 157 | + } |
| 158 | + // console.log(`Updated ${updateCount} cells`) |
55 | 159 | }
|
0 commit comments