Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args={...}
- local delay = 0.5
- local grid={} -- stores cell states
- local gridActions={} -- used to store births and deaths
- local aliveColor = colors.lime
- local deadColor = colors.black
- local xSize,ySize = term.getSize()
- -- 0,dimSize+1 because we index
- -- using offsets xOff and yOff later
- -- which can be -1!
- for i=0,xSize+1 do
- grid[i]={}
- gridActions[i]={}
- end
- for i=0,xSize+1 do
- for j=0,ySize+1 do
- grid[i][j]=false
- end
- end
- if args[1]=="monitor" then
- term.redirect(peripheral.wrap(args[2]))
- elseif args[1]== "load" then
- local name = fs.getName(args[2])
- local ext = string.match(name,".-[^\\/]-%.?([^%.\\/]*)$")
- local f = fs.open(shell.resolve(args[2]),"r")
- if ext == "cells" then
- local line
- local y = 1
- while line do
- line = f.readLine()
- if line:sub(1,1) ~= "!" then
- for x=1,#line do
- grid[x][y] = line:sub(x,x) == "O"
- end
- y = y + 1
- end
- end
- elseif ext == "rle" then
- local line
- local origX,x,y
- line = f.readLine() -- header
- local j = 1
- while line do
- if line:sub(1,1) == "x" then
- -- header
- local patternXSize,patternYSize = string.match(line,"x = (%d+), y = (%d+)")
- origX = math.floor(xSize / 2) - math.floor(tonumber(patternXSize) / 2)
- x=origX
- y=math.floor(ySize / 2) - math.floor(tonumber(patternYSize) / 2)
- elseif line:sub(1,1) ~= "#" then -- just ignore those..
- for i=1,#line do
- local c = line:sub(i,i)
- if c == "o" then
- print("Alive, x="..x.." y="..y.." j="..j)
- for k=1,j do
- grid[x][y] = true
- x = x + 1
- end
- j=1
- elseif c == "b" then
- print("Dead, x="..x.." y="..y.." j="..j)
- for k=1,j do
- grid[x][y] = false
- x = x + 1
- end
- j=1
- elseif c == "$" then
- x = origX
- y = y + 1
- print("New line, y ="..y)
- elseif c == "!" then
- break
- else
- local s = c
- i=i+1
- while true do
- local c = line:sub(i,i)
- if c ~= "o"
- and c ~= "b"
- and c ~= "$"
- and c ~= "!" then
- s = s .. c
- else
- break
- end
- i=i+1
- end
- j = tonumber(s)
- end
- end
- end
- line = f.readLine()
- end
- elseif ext == "lif" then
- local line = f.readLine() -- header
- if line ~= "" then
- error("Malformed Life 1.06 file!")
- end
- while line do
- line = f.readLine()
- local x,y = string.gmatch("^(%d-) (%d-)$")
- grid[x][y] = true
- end
- end
- f.close()
- elseif args[1] == "get" then
- -- use http.get to load.. LATER
- end
- term.clear()
- term.setCursorBlink(false)
- local paused = true
- os.startTimer(0.1) -- makes screen draw the first time
- while true do
- local _,__,x,y = os.pullEvent()
- if _ == "key" then
- if __ == 197 then -- pause key ( exit! )
- break
- end
- paused = not paused
- term.setCursorPos(1,10)
- io.write(paused and "Paused" or " ")
- elseif _ == "monitor_touch" or _ == "mouse_click" then
- grid[x][y] = not grid[x][y]
- end
- if _ ~= "timer" then
- local ev
- while ev ~= "timer" do
- ev = os.pullEvent()
- end
- end
- if not paused then
- for x=1,xSize do
- for y=1,ySize do
- local neighbors = 0
- for xOff = -1,1 do
- for yOff = -1,1 do
- if not (xOff == 0 and yOff == 0) and grid[x+xOff][y+yOff] then
- neighbors = neighbors + 1
- end
- end
- end
- if not grid[x][y] and neighbors == 3 then
- gridActions[x][y]="birth"
- elseif neighbors < 2 or neighbors > 3 then
- gridActions[x][y]="die"
- end
- end
- end
- for x=1,xSize do
- for y=1,ySize do
- if gridActions[x][y]=="die" then
- grid[x][y]=false
- elseif gridActions[x][y]=="birth" then
- grid[x][y]=true
- end
- gridActions[x][y]=nil
- end
- end
- end
- for x=1,xSize do
- for y=1,ySize do
- term.setCursorPos(x,y)
- if grid[x][y] then
- term.setBackgroundColor(aliveColor)
- end
- if paused and y == ySize then
- if x == 1 then io.write("P")
- elseif x==2 then io.write("a")
- elseif x==3 then io.write("u")
- elseif x==4 then io.write("s")
- elseif x==5 then io.write("e")
- elseif x == 6 then io.write("d")
- end
- else
- io.write(" ")
- end
- term.setBackgroundColor(deadColor)
- end
- end
- os.startTimer(delay)
- end
- term.restore()
Advertisement
Add Comment
Please, Sign In to add comment