Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- eca.lua <rule_number> <1/2>
- local rule = 22
- local r, tn = math.random, tonumber
- local gpu = require("component").gpu
- local w, h = gpu.getResolution()
- local tWorld, tBuffer, tRule = {}, {}, {}
- local tArgs = {...}
- local function set(a)
- if a == 1 then
- for i = 1, w do tWorld[i] = 0 end
- tWorld[math.floor(w/2)] = 1
- elseif a == 2 then
- for i = 1, w do tWorld[i] = r(0, 1) end
- end
- end
- local function dot(x, y, color)
- local n = y%2
- y = (y+n)/2
- local c, Fc, Bc = gpu.get(x, y)
- local c1, Fc1, Bc1 = c, Fc, Bc
- if c ~= "▄" then Fc = Bc end
- if n == 0 then Fc = color else Bc = color end
- if Fc1 ~= Fc or Bc1 ~= Bc1 then
- gpu.setForeground(Fc)
- gpu.setBackground(Bc)
- gpu.set(x, y, "▄")
- end
- end
- if #tArgs == 2 then
- rule = tn(tArgs[1])
- set(tn(tArgs[2]))
- else
- set(2)
- end
- for m = 0, 7 do tRule[m] = bit32.extract(rule, m) end
- gpu.fill(1, 1, w, h, " ")
- for j = 1, h*2 do
- tWorld, tBuffer = {table.unpack(tBuffer)}, {table.unpack(tWorld)}
- for x = 1, #tWorld do
- if tWorld[x] == 1 then
- dot(x, j-1, 65280)
- end
- end
- for i = 1, #tWorld do
- if tWorld[i-1] and tWorld[i+1] then
- tBuffer[i] = tRule[tn(tWorld[i-1]..tWorld[i]..tWorld[i+1], 2)]
- elseif not tWorld[i-1] then
- tBuffer[i] = tRule[tn(tWorld[#tWorld]..tWorld[i]..tWorld[i+1], 2)]
- elseif not tWorld[i+1] then
- tBuffer[i] = tRule[tn(tWorld[i-1]..tWorld[i]..tWorld[1], 2)]
- end
- end
- os.sleep()
- end
- gpu.setBackground(0)
- gpu.setForeground(0xFFFFFF)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement