Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- title: game title
- -- author: game developer
- -- desc: short description
- -- script: lua
- local GW = 240
- local GH = 136
- -- current state address / previous state address
- local ADRC = 0x8000
- local ADRP = 0xBFC0 -- half way between 0x8000 and 0xFF80 (input)
- -- local ADRP = 0xFF8C -- after input
- local ADRC2 = ADRC*2 -- x2 for poke4/peek4
- local ADRP2 = ADRP*2
- function init()
- memset(ADRC, 0x00, GW//2*GH)
- rand_cells()
- end
- function TIC()
- if btnp(4) then fill_grid(true)
- elseif btnp(5) then fill_grid(false)
- elseif btnp(6) then rand_cells() end
- -- cls(13) -- redundant since I'm poking the screen mem directly
- next_gen()
- draw()
- end
- function draw()
- for j=0, GH-1 do
- for i=0, GW-1 do
- local idx_A = ADRC2+(i+j*GW)
- local A = peek4(idx_A) & 1
- local scridx = i+j*240
- if A == 1 then poke4(scridx, 9)
- else poke4(scridx, 0) end
- end
- end
- end
- function fill_grid(fill)
- if fill then memset(ADRC, 0xFF, (GW//2)*GH)
- else memset(ADRC, 0x00, (GW//2)*GH) end
- end
- function rand_cells()
- for j=0, GH-1 do
- for i=0, GW-1, 2 do
- local idx_A = ADRC2+(i+j*GW)
- local rA = math.random()
- if rA < 0.5 then poke4(idx_A, 0)
- else poke4(idx_A, 1) end
- local idx_B = idx_A+1
- local rB = math.random()
- if rB < 0.5 then poke4(idx_B, 0)
- else poke4(idx_B, 1) end
- end
- end
- count_neighbors()
- end
- function count_neighbors()
- memcpy(ADRP, ADRC, (GW/2)*GH)
- for j=0, GH-1 do
- for i=0, GW-1, 2 do
- local idx = i+j*GW
- local idx_A = ADRC2+idx
- local idx_B = idx_A+1
- local A, B = peek4(idx_A), peek4(idx_B)
- local idx_oA = ADRP2+idx
- local idx_oB = idx_oA+1
- local oA, oB = peek4(idx_oA), peek4(idx_oB)
- if (oA & 1) == 1 then
- poke4( idx_A+GW-1 , peek4(idx_A+GW-1) + 2 )
- poke4( idx_A+GW , peek4(idx_A+GW ) + 2 )
- poke4( idx_A+GW+1 , peek4(idx_A+GW+1) + 2 )
- poke4( idx_A-1 , peek4(idx_A-1 ) + 2 )
- poke4( idx_A-GW-1 , peek4(idx_A-GW-1) + 2 )
- poke4( idx_A-GW , peek4(idx_A-GW ) + 2 )
- poke4( idx_A-GW+1 , peek4(idx_A-GW+1) + 2 )
- end
- if (oB & 1) == 1 then
- poke4( idx_B+GW-1 , peek4(idx_B+GW-1) + 2 )
- poke4( idx_B+GW , peek4(idx_B+GW ) + 2 )
- poke4( idx_B+GW+1 , peek4(idx_B+GW+1) + 2 )
- poke4( idx_B+1 , peek4(idx_B+1 ) + 2 )
- poke4( idx_B-GW-1 , peek4(idx_B-GW-1) + 2 )
- poke4( idx_B-GW , peek4(idx_B-GW ) + 2 )
- poke4( idx_B-GW+1 , peek4(idx_B-GW+1) + 2 )
- end
- end
- end
- end
- function next_gen()
- memcpy(ADRP, ADRC, (GW//2)*GH)
- for j=0, GH-1 do
- for i=0, GW-1, 2 do
- local idx_A = ADRC2+(i+j*GW)
- local idx_B = idx_A+1
- local A = peek4(idx_A)
- local B = peek4(idx_B)
- local idx_oA = ADRP2+(i+j*GW)
- local idx_oB = idx_oA+1
- local oA = peek4(idx_oA)
- local oB = peek4(idx_oB)
- -- cell A
- if oA ~= 0 then
- local nA = (oA >> 1) + (oB & 1)
- if (oA & 1) == 1 then
- if ((nA ~= 2) and (nA ~= 3)) then
- poke4(idx_A, A & ~1)
- poke4( idx_A+GW-1 , peek4(idx_A+GW-1) - 2 )
- poke4( idx_A+GW , peek4(idx_A+GW ) - 2 )
- poke4( idx_A+GW+1 , peek4(idx_A+GW+1) - 2 )
- poke4( idx_A-1 , peek4(idx_A-1 ) - 2 )
- poke4( idx_A-GW-1 , peek4(idx_A-GW-1) - 2 )
- poke4( idx_A-GW , peek4(idx_A-GW ) - 2 )
- poke4( idx_A-GW+1 , peek4(idx_A-GW+1) - 2 )
- end
- else
- if (nA == 3) then
- poke4(idx_A, A | 1)
- poke4( idx_A+GW-1 , peek4(idx_A+GW-1) + 2 )
- poke4( idx_A+GW , peek4(idx_A+GW ) + 2 )
- poke4( idx_A+GW+1 , peek4(idx_A+GW+1) + 2 )
- poke4( idx_A-1 , peek4(idx_A-1 ) + 2 )
- poke4( idx_A-GW-1 , peek4(idx_A-GW-1) + 2 )
- poke4( idx_A-GW , peek4(idx_A-GW ) + 2 )
- poke4( idx_A-GW+1 , peek4(idx_A-GW+1) + 2 )
- end
- end
- end
- -- cell B
- if oB ~= 0 then
- local nB = (oB >> 1) + (oA & 1)
- if (oB & 1) == 1 then
- if ((nB ~= 2) and (nB ~= 3)) then
- poke4(idx_B, B & ~1)
- poke4( idx_B+GW-1 , peek4(idx_B+GW-1) - 2 )
- poke4( idx_B+GW , peek4(idx_B+GW ) - 2 )
- poke4( idx_B+GW+1 , peek4(idx_B+GW+1) - 2 )
- poke4( idx_B+1 , peek4(idx_B+1 ) - 2 )
- poke4( idx_B-GW-1 , peek4(idx_B-GW-1) - 2 )
- poke4( idx_B-GW , peek4(idx_B-GW ) - 2 )
- poke4( idx_B-GW+1 , peek4(idx_B-GW+1) - 2 )
- end
- else
- if (nB == 3) then
- poke4(idx_B, B | 1)
- poke4( idx_B+GW-1 , peek4(idx_B+GW-1) + 2 )
- poke4( idx_B+GW , peek4(idx_B+GW ) + 2 )
- poke4( idx_B+GW+1 , peek4(idx_B+GW+1) + 2 )
- poke4( idx_B+1 , peek4(idx_B+1 ) + 2 )
- poke4( idx_B-GW-1 , peek4(idx_B-GW-1) + 2 )
- poke4( idx_B-GW , peek4(idx_B-GW ) + 2 )
- poke4( idx_B-GW+1 , peek4(idx_B-GW+1) + 2 )
- end
- end
- end
- end
- end
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment