Stary2001

Untitled

Feb 16th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. local args={...}
  2.  
  3.  
  4. local delay = 0.5
  5.  
  6. local grid={} -- stores cell states
  7. local gridActions={} -- used to store births and deaths
  8.  
  9. local aliveColor = colors.lime
  10. local deadColor = colors.black
  11.  
  12. local xSize,ySize = term.getSize()
  13.  
  14. -- 0,dimSize+1 because we index
  15. -- using offsets xOff and yOff later
  16. -- which can be -1!
  17.  
  18. for i=0,xSize+1 do
  19.   grid[i]={}
  20.   gridActions[i]={}
  21. end
  22.  
  23. for i=0,xSize+1 do
  24.   for j=0,ySize+1 do
  25.     grid[i][j]=false
  26.   end
  27. end
  28.  
  29. if args[1]=="monitor" then
  30.   term.redirect(peripheral.wrap(args[2]))
  31. elseif args[1]== "load" then
  32.   local name = fs.getName(args[2])
  33.  
  34.   local ext = string.match(name,".-[^\\/]-%.?([^%.\\/]*)$")
  35.  
  36.   local f = fs.open(shell.resolve(args[2]),"r")
  37.   if ext == "cells" then
  38.     local line
  39.     local y = 1
  40.     while line do
  41.       line = f.readLine()
  42.       if line:sub(1,1) ~= "!" then
  43.         for x=1,#line do
  44.           grid[x][y] = line:sub(x,x) == "O"
  45.         end
  46.         y = y + 1
  47.       end
  48.     end
  49.   elseif ext == "rle" then
  50.     local line
  51.     local origX,x,y
  52.     line = f.readLine() -- header
  53.  
  54.     local j = 1
  55.  
  56.     while line do
  57.       if line:sub(1,1) == "x" then
  58.         -- header
  59.         local patternXSize,patternYSize = string.match(line,"x = (%d+), y = (%d+)")
  60.         origX = math.floor(xSize / 2) - math.floor(tonumber(patternXSize) / 2)
  61.         x=origX
  62.         y=math.floor(ySize / 2) - math.floor(tonumber(patternYSize) / 2)
  63.       elseif line:sub(1,1) ~= "#" then -- just ignore those..
  64.         for i=1,#line do
  65.           local c = line:sub(i,i)
  66.           if c == "o" then
  67.             print("Alive, x="..x.." y="..y.." j="..j)
  68.             for k=1,j do
  69.               grid[x][y] = true
  70.               x = x + 1
  71.             end
  72.             j=1
  73.           elseif c == "b" then
  74.             print("Dead, x="..x.." y="..y.." j="..j)
  75.             for k=1,j do
  76.               grid[x][y] = false
  77.               x = x + 1
  78.             end
  79.             j=1
  80.           elseif c == "$" then
  81.             x = origX
  82.             y = y + 1
  83.             print("New line, y ="..y)
  84.           elseif c == "!" then
  85.            break
  86.           else
  87.             local s = c
  88.             i=i+1
  89.             while true do
  90.               local c = line:sub(i,i)
  91.               if c ~= "o"
  92.                 and c ~= "b"
  93.                 and c ~= "$"
  94.                 and c ~= "!" then
  95.                 s = s .. c
  96.               else
  97.                 break
  98.               end
  99.               i=i+1
  100.             end
  101.             j = tonumber(s)
  102.           end
  103.         end
  104.       end
  105.       line = f.readLine()
  106.     end
  107.   elseif ext == "lif" then
  108.     local line = f.readLine() -- header
  109.     if line ~= "" then
  110.       error("Malformed Life 1.06 file!")
  111.     end
  112.     while line do
  113.       line = f.readLine()
  114.       local x,y = string.gmatch("^(%d-) (%d-)$")
  115.       grid[x][y] = true
  116.     end
  117.   end
  118.   f.close()
  119. elseif args[1] == "get" then
  120.   -- use http.get to load.. LATER
  121. end
  122.  
  123. term.clear()
  124. term.setCursorBlink(false)
  125.  
  126. local paused = true
  127.  
  128. os.startTimer(0.1) -- makes screen draw the first time
  129.  
  130. while true do
  131.   local _,__,x,y = os.pullEvent()
  132.  
  133.   if _ == "key" then
  134.     if __ == 197 then -- pause key ( exit! )
  135.       break
  136.     end
  137.  
  138.     paused = not paused
  139.     term.setCursorPos(1,10)
  140.     io.write(paused and "Paused" or "      ")
  141.   elseif _ == "monitor_touch" or _ == "mouse_click" then
  142.     grid[x][y] = not grid[x][y]
  143.   end
  144.  
  145.   if _ ~= "timer" then
  146.     local ev
  147.     while ev ~= "timer" do
  148.       ev = os.pullEvent()
  149.     end
  150.   end
  151.  
  152.   if not paused then
  153.     for x=1,xSize do
  154.       for y=1,ySize do
  155.         local neighbors = 0
  156.         for xOff = -1,1 do
  157.           for yOff = -1,1 do
  158.             if not (xOff == 0 and yOff == 0) and grid[x+xOff][y+yOff] then
  159.               neighbors = neighbors + 1
  160.             end
  161.           end
  162.         end
  163.         if not grid[x][y] and neighbors == 3 then
  164.           gridActions[x][y]="birth"
  165.         elseif neighbors < 2 or neighbors > 3 then
  166.           gridActions[x][y]="die"
  167.         end
  168.       end
  169.     end
  170.  
  171.     for x=1,xSize do
  172.       for y=1,ySize do
  173.         if gridActions[x][y]=="die" then
  174.           grid[x][y]=false
  175.         elseif gridActions[x][y]=="birth" then
  176.           grid[x][y]=true
  177.         end
  178.         gridActions[x][y]=nil
  179.       end
  180.     end
  181.   end
  182.  
  183.   for x=1,xSize do
  184.     for y=1,ySize do
  185.       term.setCursorPos(x,y)
  186.       if grid[x][y] then
  187.         term.setBackgroundColor(aliveColor)
  188.       end
  189.       if paused and y == ySize then
  190.         if x == 1 then io.write("P")
  191.         elseif x==2 then io.write("a")
  192.         elseif x==3 then io.write("u")
  193.         elseif x==4 then io.write("s")
  194.         elseif x==5 then io.write("e")
  195.         elseif x == 6 then io.write("d")
  196.         end
  197.       else
  198.         io.write(" ")
  199.       end
  200.  
  201.       term.setBackgroundColor(deadColor)
  202.     end
  203.   end
  204.  
  205.   os.startTimer(delay)
  206. end
  207.  
  208. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment