Advertisement
billysback

infect

Oct 19th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. local grid = {}
  2. local width, height = term.getSize()
  3. width = width/2
  4. height = height/2
  5. for x=1,width do grid[x] = {} for y=1,height do grid[x][y] = 0 end end
  6. grid[math.random(width)][math.random(height)] = 1
  7.  
  8. local function getCell(x, y)
  9.     if grid[x] ~= nil then if grid[x][y] ~= nil then return grid[x][y] end end
  10.     return nil
  11. end
  12.  
  13. local function checkCellNeigh(x, y)
  14.     local n = 0
  15.     for o=-1,1,2 do
  16.         if getCell(x+o, y) == 1 then n = n + 1 end
  17.         if getCell(x, y+o) == 1 then n = n + 1 end
  18.     end
  19.     return n
  20. end
  21.  
  22. local function checkCell(x,y)
  23.    
  24.     local coords = { { {-1, 0}, {-1, -1}, {0, -1}}, { {-1, 0}, {-1, 1}, {0, 1} }, { {0, 1}, {1, 1}, {1, 0} }, { {1, 0}, {1, -1}, {0, -1} } }
  25.     local ok = false
  26.     if getCell(x, y) == 1 and checkCellNeigh(x, y) == 2 then
  27.         for i=1,#coords do
  28.             local n = 0
  29.             local coord = coords[i]
  30.             for j=1,#coord do
  31.                 local co = coord[j]
  32.                 --n = n + 1
  33.                 if getCell(co[1]+x, co[2]+y) == 1 then
  34.                     if checkCellNeigh(co[1]+x, co[2]+y) == 2 then
  35.                         n = n + 1
  36.                     end
  37.                 end
  38.             end
  39.             if n == #coord then
  40.                 ok = true
  41.             end
  42.         end
  43.     end
  44.     return ok
  45. end
  46.  
  47. local function update()
  48.     local possible = {}
  49.     for x=1,width do for y=1,height do
  50.         if getCell(x, y) == 1 and not checkCell(x, y) then
  51.             for i=-1,1,2 do
  52.                 if getCell(x+i, y) == 0 then possible[#possible + 1] = {x+i, y} end
  53.                 if getCell(x, y+i) == 0 then possible[#possible + 1] = {x, y+i} end
  54.             end
  55.         end
  56.     end end
  57.     if #possible > 0 then
  58.         local co = possible[math.random(#possible)]
  59.         if getCell(unpack(co)) ~= nil then grid[co[1]][co[2]] = 1 end
  60.         return true
  61.     else return false end
  62. end
  63.  
  64. local selected = nil
  65. local function draw()
  66.     for x=1,width do for y=1,height do
  67.         term.setBackgroundColor(colors.black) term.setTextColor(colors.white)
  68.         if getCell(x, y) == 1 then term.setBackgroundColor(colors.white) end
  69.         if checkCell(x, y) then term.setBackgroundColor(colors.red) end
  70.         if selected ~= nil then if x == selected[1] and y == selected[2] then
  71.             term.setBackgroundColor(colors.blue)
  72.         end end
  73.         for rx=((x-1)*2)+1,x*2 do for ry=((y-1)*2)+1,y*2 do
  74.             term.setCursorPos(rx, ry)
  75.             term.write(" ")
  76.         end end
  77.     end end
  78. end
  79.  
  80. for i=1,7 do update() end
  81.  
  82. local pause = 1.5
  83.  
  84. local timer = os.startTimer(pause)
  85. draw()
  86. local on = true
  87. while on do
  88.     local event, p1, p2, p3 = os.pullEvent()
  89.     if event == "timer" and p1 == timer then
  90.         on = update()
  91.         draw()
  92.         timer = os.startTimer(pause)
  93.     elseif event == "mouse_click" then
  94.         p2 = math.ceil(p2/2)
  95.         p3 = math.ceil(p3/2)
  96.         if p1 == 1 then
  97.             if selected == nil then selected = {p2, p3} draw() else
  98.                 local c1 = getCell(p2, p3)
  99.                 grid[p2][p3] = grid[selected[1]][selected[2]]
  100.                 grid[selected[1]][selected[2]] = c1
  101.                 selected = nil
  102.                 draw()
  103.             end
  104.         elseif p1 == 2 then selected = nil end
  105.     elseif event == "key" then
  106.         if p1 == 29 then
  107.             on = false
  108.         end
  109.     end
  110. end
  111. term.setCursorPos(1,1)
  112. print("GAME OVER, PRESS ENTER TO CONTINUE")
  113. local temp = read()
  114. term.setBackgroundColor(colors.black)
  115. term.clear()
  116. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement