Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- life.lua
- -- input: mouse, keyboard
- -- title: Life
- -- author: Skaruts
- -- desc: A game of life
- -- script: lua
- local -- shortcuts
- floor, ceil, max, min, abs =
- math.floor, math.ceil, math.max, math.min, math.abs
- local -- keys
- PGUP, PGDN, CTRL, SHIFT, ALT, SPC, ENT, TAB =
- 54, 55, 63, 64, 65 , 48, 50, 49
- local
- A,B,C,D,E,F,G,H,I,J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z =
- 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
- local -- mouse, real pos / grid pos / mouse btns
- rmx, rmy, mx, my, m1, m2, m3 =
- 0, 0, 0, 0, false, false, false
- local -- colors
- col_bg, col_dead, col_alive, col_selected =
- 0, 3, 9, 11
- -- previous/current/selction cellmaps / dead cells
- local pre, cur, draw_d_cells = 1, 2, false
- local cells = {} -- actual cellmaps
- -- zoom scalar (higher scalar = smaller cells,
- -- 1|2|4|8
- local m = 4
- -- sprite size
- local SS = 8
- -- cell size, grid width/height
- local CS, GW, GH = SS//m, 30*m, 17*m
- -- padding for cell rects
- local pad = (CS == 8) and 2 or (CS == 1 and 0 or 1)
- local paused, rand_at_start = true, false
- ---------------------------------------
- -- GAME ------------------------
- ---------------------------------------
- function init()
- create_cellmaps()
- if rand_at_start then rand_cells() end
- end
- function TIC()
- update()
- draw()
- end
- function update()
- rmx, rmy, m1, m2, m3 = mouse()
- mx, my = rmx//CS+1, rmy//CS+1
- mx = clamp(mx, 1, GW)
- my = clamp(my, 1, GH)
- handle_mouse()
- handle_keys()
- if not paused then next_gen() end
- end
- function draw()
- cls(col_bg)
- if CS > 1 then draw_cells()
- else draw_pix() end
- if paused then writec("PAUSED", "x", 0, 0, 6) end
- end
- function draw_cells()
- for j=1, GH do
- for i=1, GW do
- local x = (i-1) * CS + pad
- local y = (j-1) * CS + pad
- local w, h = CS-pad, CS-pad
- if mx == i and my == j then rect(x, y, w, h, col_selected)
- elseif cells[cur][j][i] == 1 then rect(x, y, w, h, col_alive)
- elseif draw_d_cells then rect(x, y, w, h, col_dead)
- end
- end
- end
- end
- function draw_pix()
- for j=1, GH do
- for i=1, GW do
- local x = (i-1)
- local y = (j-1)
- if mx == i and my == j then pix(x, y, col_selected)
- elseif cells[cur][j][i] == 1 then pix(x, y, col_alive)
- elseif draw_d_cells then pix(x, y, col_dead)
- end
- end
- end
- end
- function handle_keys()
- if keyp(SPC) then pause()
- elseif keyp(G, 20, 5) then next_gen()
- elseif keyp(ENT) then rand_cells()
- elseif keyp(D) then toggle_d_cells()
- elseif keyp(C) then fill_grid(false)
- elseif keyp(F) then fill_grid(true)
- end
- end
- function handle_mouse()
- if m1 then cells[cur][my][mx] = 1
- elseif m3 then cells[cur][my][mx] = 0
- end
- end
- function pause() paused = not paused end
- function toggle_d_cells() draw_d_cells = not draw_d_cells end
- function next_gen()
- if algo == SUM then next_gen_sum()
- end
- end
- function next_gen_sum()
- pre, cur = cur, pre
- for j=1, GH do
- for i=1, GW do
- local n = -- TODO: modify this for optional no-wrapping
- (cells[pre][ j-1 < 1 and GH or j-1 ][ i-1 < 1 and GW or i-1 ] & 1)
- + (cells[pre][ j-1 < 1 and GH or j-1 ][ i ] & 1)
- + (cells[pre][ j-1 < 1 and GH or j-1 ][ i+1 > GW and 1 or i+1 ] & 1)
- + (cells[pre][ j ][ i-1 < 1 and GW or i-1 ] & 1)
- + (cells[pre][ j ][ i+1 > GW and 1 or i+1 ] & 1)
- + (cells[pre][ j+1 > GH and 1 or j+1 ][ i-1 < 1 and GW or i-1 ] & 1)
- + (cells[pre][ j+1 > GH and 1 or j+1 ][ i ] & 1)
- + (cells[pre][ j+1 > GH and 1 or j+1 ][ i+1 > GW and 1 or i+1 ] & 1)
- local oc = cells[pre][j][i] == 1 -- old cell
- cells[cur][j][i] = num(n==3 or (n==2 and oc))
- end end end
- function rand_cells()
- for j=1, GH do
- for i=1, GW do
- cells[cur][j][i] = num(math.random() < 0.5)
- end
- end
- end
- function create_cellmaps()
- for g=1, 2 do cells[g] = {} end
- for j=1, GH do
- for g=1, 2 do cells[g][j] = {} end
- for i=1, GW do
- for g=1, 2 do cells[g][j][i] = 0 end end end
- end
- function fill_grid(fill)
- for j=1, GH do
- for i=1, GW do
- cells[cur][j][i] = num(enable)
- end end
- end
- ---------------------------------------
- -- HELPERS ----------------------
- ---------------------------------------
- -- print text with drop shadow
- function write(txt, x, y, col, fix)
- print(txt, 2+x*8, 2+y*8, 3, fix, 1)
- print(txt, 1+x*8, 1+y*8, col, fix, 1) end
- -- print centered with drop shadow
- function writec(t, axis, x, y, c, fix)
- if axis == 'x' then
- print(t, 0+(240-print(t, 0, -6))//2, 2+y*SS, 3, fix) -- x
- print(t, (240-print(t, 0, -6))//2, 1+y*SS, c, fix) -- x
- elseif axis == 'y' then
- print(t, 3+x*SS, 1+(136-SS)//2, 3, fix) -- y
- print(t, 2+x*SS, (136-SS)//2, c, fix) -- y
- else
- print(t, 1+(240-print(t, 0, -6))//2, 1+(136-SS)//2, 3, fix) -- xy
- print(t, (240-print(t, 0, -6))//2, (136-SS)//2, c, fix) -- xy
- end
- end
- function num(v, base)
- if type(v) == 'boolean' then return v and 1 or 0
- elseif type(v) == 'string' then return base == nil and tonumber(v) or tonumber(v, base)
- else return v == nil and 0 or v end end
- function clamp(v, l, h)
- return max(l, min(h, v)) end
- init()
Advertisement
Add Comment
Please, Sign In to add comment