Advertisement
LDDestroier

toy test (cc)

Aug 19th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. local scr_x, scr_y = term.getSize()
  2. local screen = {}
  3. local resetScreen = function()
  4.     screen = {}
  5.     for a = 1, scr_y do
  6.         table.insert(screen,string.rep("0",scr_x))
  7.     end
  8. end
  9.  
  10. substrReplace = function(str,x,char)
  11.     if not char and x then return str end
  12.     if not str then return end
  13.     return str:sub(1,x-1)..char..str:sub(x+1)
  14. end
  15.  
  16. local getchar = function(x,y)
  17.     if not screen[y] then return -1 end
  18.     if x > #screen[y] then return -1 end
  19.     return tonumber(screen[y]:sub(x,x)) or screen[y]:sub(x,x)
  20. end
  21.  
  22. local setchar = function(x,y,char,scr)
  23.     if not scr[y] then return end
  24.     if x < 1 or x > #scr[y] then return end
  25.     scr[y] = substrReplace(scr[y],x,char:sub(1,1))
  26. end
  27.  
  28. local scrcols = {
  29.     ["0"] = colors.black,
  30.     ["1"] = colors.gray,
  31.     ["2"] = colors.lightGray,
  32.     ["3"] = colors.white,
  33. }
  34.  
  35. local render = function()
  36.     for y = 1, #screen do
  37.         for x = 1, #screen[y] do
  38.             term.setCursorPos(x,y)
  39.             term.setBackgroundColor(scrcols[getchar(x,y)] or colors.black)
  40.             term.write(" ")
  41.         end
  42.     end
  43. end
  44.  
  45. local tn = tonumber
  46. local ts = tostring
  47.  
  48. local proceed = function()
  49.     local output = {} for a = 1, #screen do output[a] = screen[a] end
  50.     for y = 1, #screen do
  51.         for x = 1, #screen[y] do
  52.             local num = tn(getchar(x,y))
  53.             if num > 0 then
  54.                 setchar(x,y,ts(num-2),output)
  55.                 setchar(x+1,y,ts(getchar(x+1,y)+1),output)
  56.                 setchar(x-1,y,ts(getchar(x-1,y)+1),output)
  57.                 setchar(x,y+1,ts(getchar(x,y+1)+1),output)
  58.                 setchar(x,y-1,ts(getchar(x,y-1)+1),output)
  59.             end
  60.         end
  61.     end
  62.     for a = 1, #output do screen[a] = output[a] end
  63. end
  64.  
  65.  
  66. resetScreen()
  67. local input = function()
  68.     while true do
  69.         local evt = {os.pullEvent()}
  70.         if evt[1] == "mouse_click" then
  71.             setchar(evt[3],evt[4],"2",screen)
  72.         end
  73.         if evt[1] == "key" then
  74.             if evt[2] == keys.q then return end
  75.             if evt[2] == keys.space then resetScreen() end
  76.         end
  77.     end
  78. end
  79.  
  80. local game = function()
  81.     while true do
  82.         proceed()
  83.         render()
  84.         sleep(0.1)
  85.     end
  86. end
  87.  
  88. term.clear()
  89.  
  90. parallel.waitForAny(game,input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement