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 gridActilocal 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()
- local spaceOrX = term.isColor() and " " or "x"
- -- 0,dimSize+1 because we index
- -- using offsets xOff and yOff later
- -- which can be -1!
- function sim(grid)
- 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
- return grid
- end
- 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
- line = f.readLine()
- local patternYSize = 0
- while line do
- if line:sub(1,1) ~= "!" then
- patternYSize = patternYSize + 1
- end
- line = f.readLine()
- end
- local y = math.floor(ySize / 2) - math.floor(patternYSize / 2)
- f.close()
- f = fs.open(shell.resolve(args[2]),"r")
- line = f.readLine()
- while line do
- if line:sub(1,1) ~= "!" then
- for x=1,#line do
- grid[x][y] = line:sub(x,x) == "O"
- end
- y = y + 1
- end
- line = f.readLine()
- end
- elseif ext == "rle" then
- -- Not implemented, my implementation I had sucked
- -- kamal dongs
- elseif ext == "lif" then
- local line = f.readLine() -- header
- if line ~= "#Life 1.06" then
- error("Malformed Life 1.06 file!")
- end
- line = f.readLine()
- while line do
- local x,y = string.match(line,"([0-9%-]+) ([0-9%-]+)")
- print(x," ",y)
- x=tonumber(x)
- y=tonumber(y)
- print(x," ",y)
- grid[x][y] = true
- line = f.readLine()
- end
- end
- f.close()
- elseif args[1] == "get" then
- -- use http.get to load.. LATER
- end
- term.setCursorBlink(false)
- local paused = true
- os.startTimer(delay) -- 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
- 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
- grid=sim(grid)
- end
- term.setBackgroundColor(deadColor)
- term.clear()
- term.setCursorPos(1,19)
- io.write(paused and "Paused" or " ")
- for x=1,xSize do
- for y=1,ySize do
- term.setCursorPos(x,y)
- if grid[x][y] then
- term.setBackgroundColor(aliveColor)
- io.write(spaceOrX)
- term.setBackgroundColor(deadColor)
- end
- end
- end
- os.startTimer(delay)
- end
- term.restore()ons={} -- 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!
- function sim(grid)
- 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
- return grid
- end
- 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
- line = f.readLine()
- local patternYSize = 0
- while line do
- if line:sub(1,1) ~= "!" then
- patternYSize = patternYSize + 1
- end
- line = f.readLine()
- end
- local y = math.floor(ySize / 2) - math.floor(patternYSize / 2)
- f.close()
- f = fs.open(shell.resolve(args[2]),"r")
- line = f.readLine()
- while line do
- if line:sub(1,1) ~= "!" then
- for x=1,#line do
- grid[x][y] = line:sub(x,x) == "O"
- end
- y = y + 1
- end
- line = f.readLine()
- end
- elseif ext == "rle" then
- -- Not implemented, my implementation I had sucked
- -- kamal dongs
- elseif ext == "lif" then
- local line = f.readLine() -- header
- if line ~= "#Life 1.06" then
- error("Malformed Life 1.06 file!")
- end
- line = f.readLine()
- while line do
- local x,y = string.match(line,"([0-9%-]+) ([0-9%-]+)")
- print(x," ",y)
- x=tonumber(x)
- y=tonumber(y)
- print(x," ",y)
- grid[x][y] = true
- line = f.readLine()
- 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
- 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
- term.setBackgroundColor(deadColor)
- term.clear()
- term.setCursorPos(1,18)
- io.write(paused and "Paused" or " ")
- if not paused then
- grid=sim(grid)
- for x=1,xSize do
- for y=1,ySize do
- term.setCursorPos(x,y)
- if grid[x][y] then
- term.setBackgroundColor(aliveColor)
- io.write(" ")
- term.setBackgroundColor(deadColor)
- end
- end
- end
- end
- os.startTimer(delay)
- end
- term.restore()
Advertisement
Add Comment
Please, Sign In to add comment