Skaruts

life_sum.lua

Jul 9th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1.  
  2. -- life.lua
  3. -- input: mouse, keyboard
  4. -- title:  Life
  5. -- author: Skaruts
  6. -- desc:   A game of life
  7. -- script: lua
  8.  
  9. local -- shortcuts
  10. floor,      ceil,      max,      min,      abs =
  11. math.floor, math.ceil, math.max, math.min, math.abs
  12.  
  13. local   -- keys
  14. PGUP, PGDN, CTRL, SHIFT, ALT, SPC, ENT, TAB =
  15. 54,   55,   63,   64,    65 , 48,  50,  49
  16. local
  17. 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 =
  18. 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
  19.  
  20. local -- mouse, real pos / grid pos / mouse btns
  21. rmx, rmy, mx, my, m1,    m2,    m3 =
  22. 0,   0,   0,  0,  false, false, false
  23.  
  24. local   -- colors
  25. col_bg, col_dead, col_alive, col_selected =
  26. 0,      3,        9,         11
  27.  
  28. -- previous/current/selction cellmaps / dead cells
  29. local pre, cur, draw_d_cells = 1, 2, false
  30. local cells = {}  -- actual cellmaps
  31.  
  32. -- zoom scalar (higher scalar = smaller cells,
  33. -- 1|2|4|8
  34. local m = 4
  35.  
  36. -- sprite size
  37. local SS = 8
  38.  
  39. -- cell size, grid width/height
  40. local CS, GW, GH = SS//m, 30*m, 17*m
  41.  
  42.  -- padding for cell rects
  43. local pad = (CS == 8) and 2 or (CS == 1 and 0 or 1)
  44.  
  45. local paused, rand_at_start = true, false
  46.  
  47. ---------------------------------------
  48. --    GAME     ------------------------
  49. ---------------------------------------
  50. function init()
  51.     create_cellmaps()
  52.     if rand_at_start then rand_cells() end
  53. end
  54.  
  55. function TIC()
  56.     update()
  57.     draw()
  58. end
  59.  
  60. function update()
  61.     rmx, rmy, m1, m2, m3 = mouse()
  62.     mx, my = rmx//CS+1, rmy//CS+1
  63.     mx = clamp(mx, 1, GW)
  64.     my = clamp(my, 1, GH)
  65.  
  66.     handle_mouse()
  67.     handle_keys()
  68.     if not paused then next_gen() end
  69. end
  70.  
  71. function draw()
  72.     cls(col_bg)
  73.     if CS > 1 then draw_cells()
  74.     else           draw_pix() end
  75.     if paused then  writec("PAUSED", "x", 0, 0, 6) end
  76. end
  77.  
  78. function draw_cells()
  79.     for j=1, GH do
  80.         for i=1, GW do
  81.             local x = (i-1) * CS + pad
  82.             local y = (j-1) * CS + pad
  83.             local w, h = CS-pad, CS-pad
  84.  
  85.             if mx == i and my == j then        rect(x, y, w, h, col_selected)
  86.             elseif cells[cur][j][i] == 1 then  rect(x, y, w, h, col_alive)
  87.             elseif draw_d_cells then           rect(x, y, w, h, col_dead)
  88.             end
  89.         end
  90.     end
  91. end
  92.  
  93. function draw_pix()
  94.     for j=1, GH do
  95.         for i=1, GW do
  96.             local x = (i-1)
  97.             local y = (j-1)
  98.  
  99.             if mx == i and my == j then       pix(x, y, col_selected)
  100.             elseif cells[cur][j][i] == 1 then pix(x, y, col_alive)
  101.             elseif draw_d_cells then          pix(x, y, col_dead)
  102.             end
  103.         end
  104.     end
  105. end
  106.  
  107. function handle_keys()
  108.     if     keyp(SPC)      then pause()
  109.     elseif keyp(G, 20, 5) then next_gen()
  110.     elseif keyp(ENT)      then rand_cells()
  111.     elseif keyp(D)        then toggle_d_cells()
  112.     elseif keyp(C)        then fill_grid(false)
  113.     elseif keyp(F)        then fill_grid(true)
  114.     end
  115. end
  116.  
  117. function handle_mouse()
  118.     if     m1 then cells[cur][my][mx] = 1
  119.     elseif m3 then cells[cur][my][mx] = 0
  120.     end
  121. end
  122.  
  123. function pause() paused = not paused end
  124. function toggle_d_cells() draw_d_cells = not draw_d_cells end
  125.  
  126. function next_gen()
  127.     if algo == SUM then next_gen_sum()
  128.     end
  129. end
  130.  
  131. function next_gen_sum()
  132.     pre, cur = cur, pre
  133.     for j=1, GH do
  134.         for i=1, GW do
  135.             local n = -- TODO: modify this for optional no-wrapping
  136.                       (cells[pre][ j-1 < 1 and GH or j-1 ][ i-1 <  1 and GW or i-1 ] & 1)
  137.                     + (cells[pre][ j-1 < 1 and GH or j-1 ][ i                      ] & 1)
  138.                     + (cells[pre][ j-1 < 1 and GH or j-1 ][ i+1 > GW and  1 or i+1 ] & 1)
  139.                     + (cells[pre][ j                     ][ i-1 <  1 and GW or i-1 ] & 1)
  140.                     + (cells[pre][ j                     ][ i+1 > GW and  1 or i+1 ] & 1)
  141.                     + (cells[pre][ j+1 > GH and 1 or j+1 ][ i-1 <  1 and GW or i-1 ] & 1)
  142.                     + (cells[pre][ j+1 > GH and 1 or j+1 ][ i                      ] & 1)
  143.                     + (cells[pre][ j+1 > GH and 1 or j+1 ][ i+1 > GW and  1 or i+1 ] & 1)
  144.  
  145.             local oc = cells[pre][j][i] == 1 -- old cell
  146.  
  147.             cells[cur][j][i] = num(n==3 or (n==2 and oc))
  148. end end end
  149.  
  150. function rand_cells()
  151.     for j=1, GH do
  152.         for i=1, GW do
  153.             cells[cur][j][i] = num(math.random() < 0.5)
  154.         end
  155.     end
  156. end
  157.  
  158. function create_cellmaps()
  159.     for g=1, 2 do cells[g] = {} end
  160.  
  161.     for j=1, GH do
  162.         for g=1, 2 do cells[g][j] = {} end
  163.         for i=1, GW do
  164.             for g=1, 2 do cells[g][j][i] = 0 end end end
  165. end
  166.  
  167. function fill_grid(fill)
  168.     for j=1, GH do
  169.         for i=1, GW do
  170.             cells[cur][j][i] = num(enable)
  171.         end end
  172. end
  173.  
  174. ---------------------------------------
  175.  
  176. --    HELPERS    ----------------------
  177.  
  178. ---------------------------------------
  179. -- print text with drop shadow
  180. function write(txt, x, y, col, fix)
  181.     print(txt, 2+x*8, 2+y*8, 3, fix, 1)
  182.     print(txt, 1+x*8, 1+y*8, col, fix, 1) end
  183.  
  184. -- print centered with drop shadow
  185. function writec(t, axis, x, y, c, fix)
  186.     if axis == 'x' then
  187.         print(t, 0+(240-print(t, 0, -6))//2, 2+y*SS, 3, fix) -- x
  188.         print(t, (240-print(t, 0, -6))//2, 1+y*SS, c, fix) -- x
  189.     elseif axis == 'y' then
  190.         print(t, 3+x*SS, 1+(136-SS)//2, 3, fix) -- y
  191.         print(t, 2+x*SS, (136-SS)//2, c, fix) -- y
  192.     else
  193.         print(t, 1+(240-print(t, 0, -6))//2, 1+(136-SS)//2, 3, fix) -- xy
  194.         print(t, (240-print(t, 0, -6))//2, (136-SS)//2, c, fix) -- xy
  195.     end
  196. end
  197.  
  198. function num(v, base)
  199.     if     type(v) == 'boolean' then return v and 1 or 0
  200.     elseif type(v) == 'string' then return base == nil and tonumber(v) or tonumber(v, base)
  201.     else return v == nil and 0 or v end end
  202.  
  203. function clamp(v, l, h)
  204.     return max(l, min(h, v)) end
  205.  
  206. init()
Advertisement
Add Comment
Please, Sign In to add comment