Skip to content

Instantly share code, notes, and snippets.

@jdp
Created August 6, 2008 12:39
Show Gist options
  • Save jdp/4213 to your computer and use it in GitHub Desktop.
Save jdp/4213 to your computer and use it in GitHub Desktop.
-- Rules:
-- Winit(p) = rand[0,100) < 40
-- Repeat 4: W?(p) = R1(p) >= 5 || R2(p) == 2
-- Repeat 3: W?(p) = R1(p) >= 5
function ca_cave(map)
local R = function (s, p)
local ret = 0
local rt = {}
for y = -s, s do
for x = -s, s do
table.insert(rt, {x, y})
end
end
for i, r in pairs(rt) do
x = (p % map_width) + r[1]
y = ((p - (p % map_width)) / map_width) - r[2]
if (x < 0) or (x > map_width) then
ret = ret + 1
elseif (y < 0) or (y > map_height) then
ret = ret + 1
elseif (y*map_width+x < 0) or (y*map_width+x > table.maxn(map)) then
ret = ret + 1
else
if map[y*map_width+x] then
ret = ret + 1
end
end
end
return ret
end
for q = 1, 1 do
updated_map = {}
for p = 1, table.maxn(map) do
updated_map[p] = (R(1, p) >= 5) or (R(2, p) == 1)
end
map = updated_map
end
for q = 1, 2 do
updated_map = {}
for p = 1, table.maxn(map) do
updated_map[p] = R(1, p) >= 5
end
map = updated_map
end
return map
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment