cartridges/conway.lua

97 lines
2.2 KiB
Lua

-- title: Conway TIC of life
-- author: Fabien Freling
-- desc: short description
-- script: lua
t=0
width=240
height=136
buffer={}
ib={} -- integral-buffer
for i=0,width*height-1 do
buffer[i]=0
end
-- initial state
math.randomseed(1234)
for i=0,2500 do
buffer[math.random(0, width*height-1)]=1
end
function display()
for y=0,height-1 do
for x=0,width-1 do
local color=0
if buffer[x+y*width]==1 then
color=13
end
pix(x,y,color)
end
end
end
function compute_ib()
ib[0]=buffer[0]
for x=1,width-1 do
ib[x]=buffer[x]+ib[x-1]
end
for y=1,height-1 do
ib[y*width]=buffer[y*width]+ib[(y-1)*width]
end
for y=1,height-1 do
for x=1,width-1 do
ib[x+y*width]=buffer[x+y*width]+ib[x-1+y*width]+ib[x+(y-1)*width]-ib[x-1+(y-1)*width]
end
end
end
function update()
compute_ib()
-- deal with border later
for y=2,height-3 do
for x=2,width-3 do
i=x+y*width
local n=ib[x+1+(y+1)*width]-ib[x-2+(y+1)*width]-ib[x+1+(y-2)*width]+ib[x-2+(y-2)*width]
n=n-buffer[i]
if n < 2 then
buffer[i]=0
elseif n == 3 then
buffer[i]=1
elseif n > 3 then
buffer[i]=0
end
end
end
end
function TIC()
display()
if t%30==0 then update() end
t=t+1
end
-- <TILES>
-- 001:efffffffff222222f8888888f8222222f8fffffff8ff0ffff8ff0ffff8ff0fff
-- 002:fffffeee2222ffee88880fee22280feefff80fff0ff80f0f0ff80f0f0ff80f0f
-- 003:efffffffff222222f8888888f8222222f8fffffff8fffffff8ff0ffff8ff0fff
-- 004:fffffeee2222ffee88880fee22280feefff80ffffff80f0f0ff80f0f0ff80f0f
-- 017:f8fffffff8888888f888f888f8888ffff8888888f2222222ff000fffefffffef
-- 018:fff800ff88880ffef8880fee88880fee88880fee2222ffee000ffeeeffffeeee
-- 019:f8fffffff8888888f888f888f8888ffff8888888f2222222ff000fffefffffef
-- 020:fff800ff88880ffef8880fee88880fee88880fee2222ffee000ffeeeffffeeee
-- </TILES>
-- <WAVES>
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>
-- <SFX>
-- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
-- </SFX>
-- <PALETTE>
-- 000:140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6
-- </PALETTE>