Last active
February 14, 2023 17:45
-
-
Save Quenty/71c89d0543e8ebd2136405a611dc3944 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x__ Air | |
x__ Air | |
x__ Air | |
x__ Air | |
_x_ Grass | |
__x Rock | |
__x Rock | |
__x Rock | |
__x Rock | |
__x Rock | |
----- | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
-- @module WaveCollapseUtils | |
-- @author Quenty | |
local WaveCollapseUtils = {} | |
local EDGE_DATA = { | |
{ | |
2, -- Edge index (forward) | |
1, -- Neighbor's edgeindex (backward) | |
1, -- Number to add to index to get to neighbor | |
}, | |
{ | |
1, | |
2, | |
-1, | |
}, | |
} | |
local MODULE_INDEX_TO_NAME = { | |
[1] = "Air"; | |
[2] = "Grass"; | |
[3] = "Rock"; | |
} | |
function WaveCollapseUtils.update(cells, index) | |
local cell = cells[index] | |
local toUpdate = {} | |
for _, edgeData in pairs(EDGE_DATA) do | |
local thisEdgeIndex = edgeData[1] | |
local neighborEdgeIndex = edgeData[2] | |
local validHashes = {} | |
for _, _module in pairs(cell) do | |
validHashes[_module[thisEdgeIndex]] = true | |
end | |
local neighborIndex = index + edgeData[3] | |
local neighbor = cells[neighborIndex] | |
if neighbor then | |
local removed = false | |
for moduleIndex, _module in pairs(neighbor) do | |
if not validHashes[_module[neighborEdgeIndex]] then | |
neighbor[moduleIndex] = nil | |
removed = true | |
end | |
end | |
if removed then | |
table.insert(toUpdate, neighborIndex) | |
end | |
end | |
end | |
for _, cellIndex in pairs(toUpdate) do | |
WaveCollapseUtils.update(cells, cellIndex) | |
end | |
end | |
function WaveCollapseUtils.visualize(cells) | |
for i=1, #cells do | |
local cell = cells[i] | |
local line = "" | |
local cellNames = {} | |
for j=1, #MODULE_INDEX_TO_NAME do | |
if cell[j] then | |
line = line .. "x" | |
table.insert(cellNames, MODULE_INDEX_TO_NAME[j]) | |
else | |
line = line .. "_" | |
end | |
end | |
line = line .. " " .. table.concat(cellNames, ", ") | |
print(line) | |
end | |
print("-----") | |
end | |
local function air() | |
return { | |
0; -- first edge (above) | |
0; -- second edge (below) | |
} | |
end | |
local function rock() | |
return { | |
1; | |
1; | |
} | |
end | |
local function grass() | |
return { | |
0; | |
1; | |
} | |
end | |
-- Creates a cell | |
local function cellMaker() | |
return { | |
air(); | |
grass(); | |
rock(); | |
} | |
end | |
local cells = {} | |
for _=1, 10 do | |
table.insert(cells, cellMaker()) | |
end | |
cells[5][1] = nil -- remove air from 5th cell | |
cells[5][3] = nil -- remove rock from 5th cell | |
WaveCollapseUtils.update(cells, 5) -- update 5th cell recursively | |
WaveCollapseUtils.visualize(cells) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment