Here is a Day and Night (cellular automaton)
Very short code, no GUI or shortcuts.
Nice algorithm in pure Lua to create procedural generated maps (by stop after 200 steps).
Code: Select all
function love.load()
w = 1920/4
h = 1080/4
love.window.setMode(w, h)
field = {}
for y = 1, h do
field[y] = {}
for x = 1, w do
field[y][x] = love.math.random (2)-1
end
end
born = {
[3]=true,
[6]=true,
[7]=true,
[8]=true}
survive = {
[3]=true,
[4]=true,
[6]=true,
[7]=true,
[8]=true}
canvas = love.graphics.newCanvas(w, h)
end
-- moore-neighborhood
mns = {
{-1,-1},
{ 0,-1},
{ 1,-1},
{ 1, 0},
{ 1, 1},
{ 0, 1},
{-1, 1},
{-1, 0},
}
function getValue (x, y)
return field[(y-1)%h+1][(x-1)%w+1]
end
function getAmount (x, y)
local amount = 0
for i, mn in ipairs (mns) do
amount = amount + getValue (x+mn[1], y+mn[2])
end
return amount
end
function love.update(dt)
local temp = {}
local points = {}
for y, xs in ipairs (field) do
temp[y] = {}
for x, value in ipairs (xs) do
if value == 0 then
if born[getAmount(x, y)] then
temp[y][x] = 1
table.insert (points, {x,y,1,1,1})
else
temp[y][x] = 0
table.insert (points, {x,y,0,0,0})
end
else -- 1
if survive[getAmount(x, y)] then
temp[y][x] = 1
table.insert (points, {x,y,1,1,1})
else
temp[y][x] = 0
table.insert (points, {x,y,0,0,0})
end
end
end
end
field = temp
love.graphics.setCanvas(canvas)
love.graphics.translate(-0.5, -0.5)
love.graphics.points(points)
love.graphics.setCanvas()
end
function love.draw()
love.graphics.draw(canvas)
end