Advertisement
sci4me

CC Wireworld

Jul 13th, 2016
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. local CELL_NONE = 1
  4. local CELL_WIRE = 2
  5. local CELL_HEAD = 3
  6. local CELL_TAIL = 4
  7.  
  8. local COLORS = { colors.black, colors.yellow, colors.blue, colors.red }
  9.  
  10. local width, height = term.getSize()
  11.  
  12. local selectedType = CELL_WIRE
  13. local ticking = false
  14.  
  15. local grid = {}
  16.  
  17. for x = 1, width do
  18.     grid[x] = {}
  19.     for y = 1, height do
  20.         grid[x][y] = CELL_NONE
  21.     end
  22. end
  23.  
  24. local function updateGrid()
  25.     local newGrid = {}
  26.  
  27.     for x = 1, #grid do
  28.         newGrid[x] = {}
  29.  
  30.         for y = 1, #grid[x] do
  31.             local old = grid[x][y]
  32.             local new
  33.  
  34.             if old == CELL_NONE then
  35.                 new = CELL_NONE
  36.             elseif old == CELL_HEAD then
  37.                 new = CELL_TAIL
  38.             elseif old == CELL_TAIL then
  39.                 new = CELL_WIRE
  40.             elseif old == CELL_WIRE then
  41.                 local n = 0
  42.  
  43.                 for j = -1, 1 do
  44.                     for i = -1, 1 do
  45.                         local k = x + i
  46.                         local l = y + j
  47.  
  48.                         if not (k < 1 or l < 1 or k > width or l > height) then
  49.                             if grid[k][l] == CELL_HEAD then
  50.                                 n = n + 1
  51.                             end
  52.                         end
  53.                     end
  54.                 end
  55.  
  56.                 if n == 1 or n == 2 then
  57.                     new = CELL_HEAD
  58.                 else
  59.                     new = CELL_WIRE
  60.                 end
  61.             end
  62.  
  63.             newGrid[x][y] = new
  64.         end
  65.     end
  66.  
  67.     grid = newGrid
  68. end
  69.  
  70. local delay = 0.1
  71. local timer = os.startTimer(delay)
  72. local running = true
  73. while running do
  74.     local e, p1, p2, p3, p4, p5 = os.pullEvent()
  75.     if e == "timer" and p1 == timer then
  76.         timer = os.startTimer(delay)
  77.  
  78.         if ticking then
  79.             updateGrid()
  80.         end
  81.  
  82.         for y = 1, height do
  83.             term.setCursorPos(1, y)
  84.             for x = 1, width do
  85.                 term.setBackgroundColor(COLORS[grid[x][y]])
  86.                 term.write(" ")
  87.             end
  88.         end
  89.     elseif e == "mouse_click" then
  90.         if not grid[p2] then
  91.             grid[p2] = {}
  92.         end
  93.  
  94.         if p1 == 1 then
  95.             grid[p2][p3] = selectedType
  96.         elseif p1 == 2 then
  97.             grid[p2][p3] = CELL_NONE
  98.         end
  99.     elseif e == "key" then
  100.         if p1 == keys.one then
  101.             selectedType = CELL_WIRE
  102.         elseif p1 == keys.two then
  103.             selectedType = CELL_HEAD
  104.         elseif p1 == keys.three then
  105.             selectedType = CELL_TAIL
  106.         elseif p1 == keys.p then
  107.             ticking = not ticking
  108.         elseif p1 == keys.u then
  109.             if not ticking then
  110.                 updateGrid()
  111.             end
  112.         elseif p1 == keys.c then
  113.             if not ticking then
  114.                 for x = 1, width do
  115.                     for y = 1, height do
  116.                         grid[x][y] = CELL_NONE
  117.                     end
  118.                 end
  119.             end
  120.         elseif p1 == keys.r then
  121.             if not ticking then
  122.                 for x = 1, width do
  123.                     for y = 1, height do
  124.                         if grid[x][y] == CELL_HEAD or grid[x][y] == CELL_TAIL then
  125.                             grid[x][y] = CELL_WIRE
  126.                         end
  127.                     end
  128.                 end
  129.             end
  130.         elseif p1 == keys.t then
  131.             running = false
  132.         end
  133.     end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement