Skaruts

life4.lua

Jul 9th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. -- title:  game title
  2. -- author: game developer
  3. -- desc:   short description
  4. -- script: lua
  5.  
  6. local GW = 240
  7. local GH = 136
  8.  
  9. -- current state address / previous state address
  10. local ADRC = 0x8000
  11. local ADRP = 0xBFC0 -- half way between 0x8000 and 0xFF80 (input)
  12. -- local ADRP = 0xFF8C -- after input
  13.  
  14. local ADRC2 = ADRC*2 -- x2 for poke4/peek4
  15. local ADRP2 = ADRP*2
  16.  
  17. function init()
  18.     memset(ADRC, 0x00, GW//2*GH)
  19.     rand_cells()
  20. end
  21.  
  22. function TIC()
  23.     if     btnp(4) then fill_grid(true)
  24.     elseif btnp(5) then fill_grid(false)
  25.     elseif btnp(6) then rand_cells() end
  26.  
  27.     -- cls(13) -- redundant since I'm poking the screen mem directly
  28.     next_gen()
  29.     draw()
  30. end
  31.  
  32. function draw()
  33.     for j=0, GH-1 do
  34.         for i=0, GW-1 do
  35.             local idx_A = ADRC2+(i+j*GW)
  36.             local A = peek4(idx_A) & 1
  37.             local scridx = i+j*240
  38.  
  39.             if A == 1 then poke4(scridx, 9)
  40.             else           poke4(scridx, 0) end
  41.         end
  42.     end
  43. end
  44.  
  45. function fill_grid(fill)
  46.     if fill then memset(ADRC, 0xFF, (GW//2)*GH)
  47.     else         memset(ADRC, 0x00, (GW//2)*GH) end
  48. end
  49.  
  50. function rand_cells()
  51.     for j=0, GH-1 do
  52.         for i=0, GW-1, 2 do
  53.             local idx_A = ADRC2+(i+j*GW)
  54.             local rA = math.random()
  55.             if rA < 0.5 then poke4(idx_A, 0)
  56.             else             poke4(idx_A, 1) end
  57.  
  58.             local idx_B = idx_A+1
  59.             local rB = math.random()
  60.             if rB < 0.5 then poke4(idx_B, 0)
  61.             else             poke4(idx_B, 1) end
  62.         end
  63.     end
  64.     count_neighbors()
  65. end
  66.  
  67. function count_neighbors()
  68.     memcpy(ADRP, ADRC, (GW/2)*GH)
  69.  
  70.     for j=0, GH-1 do
  71.         for i=0, GW-1, 2 do
  72.             local idx = i+j*GW
  73.             local idx_A = ADRC2+idx
  74.             local idx_B = idx_A+1
  75.             local A, B = peek4(idx_A), peek4(idx_B)
  76.  
  77.             local idx_oA = ADRP2+idx
  78.             local idx_oB = idx_oA+1
  79.             local oA, oB = peek4(idx_oA), peek4(idx_oB)
  80.  
  81.             if (oA & 1) == 1 then
  82.                 poke4( idx_A+GW-1 , peek4(idx_A+GW-1) + 2 )
  83.                 poke4( idx_A+GW   , peek4(idx_A+GW  ) + 2 )
  84.                 poke4( idx_A+GW+1 , peek4(idx_A+GW+1) + 2 )
  85.                 poke4( idx_A-1    , peek4(idx_A-1   ) + 2 )
  86.                 poke4( idx_A-GW-1 , peek4(idx_A-GW-1) + 2 )
  87.                 poke4( idx_A-GW   , peek4(idx_A-GW  ) + 2 )
  88.                 poke4( idx_A-GW+1 , peek4(idx_A-GW+1) + 2 )
  89.             end
  90.             if (oB & 1) == 1 then
  91.                 poke4( idx_B+GW-1 , peek4(idx_B+GW-1) + 2 )
  92.                 poke4( idx_B+GW   , peek4(idx_B+GW  ) + 2 )
  93.                 poke4( idx_B+GW+1 , peek4(idx_B+GW+1) + 2 )
  94.                 poke4( idx_B+1    , peek4(idx_B+1   ) + 2 )
  95.                 poke4( idx_B-GW-1 , peek4(idx_B-GW-1) + 2 )
  96.                 poke4( idx_B-GW   , peek4(idx_B-GW  ) + 2 )
  97.                 poke4( idx_B-GW+1 , peek4(idx_B-GW+1) + 2 )
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. function next_gen()
  104.     memcpy(ADRP, ADRC, (GW//2)*GH)
  105.  
  106.     for j=0, GH-1 do
  107.         for i=0, GW-1, 2 do
  108.             local idx_A = ADRC2+(i+j*GW)
  109.             local idx_B = idx_A+1
  110.             local A = peek4(idx_A)
  111.             local B = peek4(idx_B)
  112.  
  113.             local idx_oA = ADRP2+(i+j*GW)
  114.             local idx_oB = idx_oA+1
  115.             local oA = peek4(idx_oA)
  116.             local oB = peek4(idx_oB)
  117.  
  118.             -- cell A
  119.             if oA ~= 0 then
  120.                 local nA = (oA >> 1) + (oB & 1)
  121.  
  122.                 if (oA & 1) == 1 then
  123.                     if ((nA ~= 2) and (nA ~= 3)) then
  124.                         poke4(idx_A, A & ~1)
  125.  
  126.                         poke4( idx_A+GW-1 , peek4(idx_A+GW-1) - 2 )
  127.                         poke4( idx_A+GW   , peek4(idx_A+GW  ) - 2 )
  128.                         poke4( idx_A+GW+1 , peek4(idx_A+GW+1) - 2 )
  129.                         poke4( idx_A-1    , peek4(idx_A-1   ) - 2 )
  130.                         poke4( idx_A-GW-1 , peek4(idx_A-GW-1) - 2 )
  131.                         poke4( idx_A-GW   , peek4(idx_A-GW  ) - 2 )
  132.                         poke4( idx_A-GW+1 , peek4(idx_A-GW+1) - 2 )
  133.                     end
  134.                 else
  135.                     if (nA == 3) then
  136.                         poke4(idx_A, A | 1)
  137.  
  138.                         poke4( idx_A+GW-1 , peek4(idx_A+GW-1) + 2 )
  139.                         poke4( idx_A+GW   , peek4(idx_A+GW  ) + 2 )
  140.                         poke4( idx_A+GW+1 , peek4(idx_A+GW+1) + 2 )
  141.                         poke4( idx_A-1    , peek4(idx_A-1   ) + 2 )
  142.                         poke4( idx_A-GW-1 , peek4(idx_A-GW-1) + 2 )
  143.                         poke4( idx_A-GW   , peek4(idx_A-GW  ) + 2 )
  144.                         poke4( idx_A-GW+1 , peek4(idx_A-GW+1) + 2 )
  145.                     end
  146.                 end
  147.             end
  148.  
  149.             -- cell B
  150.             if oB ~= 0 then
  151.                 local nB = (oB >> 1) + (oA & 1)
  152.  
  153.                 if (oB & 1) == 1 then
  154.                     if ((nB ~= 2) and (nB ~= 3)) then
  155.                         poke4(idx_B, B & ~1)
  156.  
  157.                         poke4( idx_B+GW-1 , peek4(idx_B+GW-1) - 2 )
  158.                         poke4( idx_B+GW   , peek4(idx_B+GW  ) - 2 )
  159.                         poke4( idx_B+GW+1 , peek4(idx_B+GW+1) - 2 )
  160.                         poke4( idx_B+1    , peek4(idx_B+1   ) - 2 )
  161.                         poke4( idx_B-GW-1 , peek4(idx_B-GW-1) - 2 )
  162.                         poke4( idx_B-GW   , peek4(idx_B-GW  ) - 2 )
  163.                         poke4( idx_B-GW+1 , peek4(idx_B-GW+1) - 2 )
  164.                     end
  165.                 else
  166.                     if (nB == 3) then
  167.                         poke4(idx_B, B | 1)
  168.  
  169.                         poke4( idx_B+GW-1 , peek4(idx_B+GW-1) + 2 )
  170.                         poke4( idx_B+GW   , peek4(idx_B+GW  ) + 2 )
  171.                         poke4( idx_B+GW+1 , peek4(idx_B+GW+1) + 2 )
  172.                         poke4( idx_B+1    , peek4(idx_B+1   ) + 2 )
  173.                         poke4( idx_B-GW-1 , peek4(idx_B-GW-1) + 2 )
  174.                         poke4( idx_B-GW   , peek4(idx_B-GW  ) + 2 )
  175.                         poke4( idx_B-GW+1 , peek4(idx_B-GW+1) + 2 )
  176.                     end
  177.                 end
  178.             end
  179.         end
  180.     end
  181. end
  182.  
  183.  
  184. init()
Advertisement
Add Comment
Please, Sign In to add comment