summaryrefslogtreecommitdiff
path: root/life.lua
diff options
context:
space:
mode:
Diffstat (limited to 'life.lua')
-rw-r--r--life.lua47
1 files changed, 23 insertions, 24 deletions
diff --git a/life.lua b/life.lua
index 8d03a20..a760804 100644
--- a/life.lua
+++ b/life.lua
@@ -18,12 +18,9 @@
gol = {}
gol.helpers = {}
-gol.setup = gol.setup or {}
-gol.setup.current = gol.setup.current or {}
-gol.setup.fade = gol.setup.fade or false
+local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
-local C, Ct, P, R, S, match = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
-- http://lua-users.org/lists/lua-l/2009-06/msg00343.html
gol.helpers.utfchar = R("\000\127") +
@@ -167,17 +164,21 @@ end
--- Computing single lines and whole frames and intervals thereof
function gol.next_line (rows, rule)
- local new = ""
local live = "1"
- local fade = gol.setup.current.fade or gol.setup.fade
+ local fade = false
+ if mplife then
+ fade = mplife.setup.current.fade
+ end
local n = 1
local max = string.len(rows[2])
- repeat
- local env = {}
+ local cell = R("09") + P("D")
+ local nocell = 1-cell
+ local ce = Cs(cell) / function (current)
+ local env = {}
local lpos, rpos -- positions left of / right of current
-- toroidal, flips over at borders
@@ -193,8 +194,6 @@ function gol.next_line (rows, rule)
rpos = n + 1
end
- local current = string.sub( rows[2], n, n )
-
-- +---+---+---+
-- |nw | n | ne|
-- +---+---+---+
@@ -222,13 +221,16 @@ function gol.next_line (rows, rule)
end
end
+ n = n + 1
+ --
-- adding new cell according to ruleset
- new = new .. gol.apply_rule(current, cnt, rule, fade, true)
+ return gol.apply_rule(current, cnt, rule, fade, true)
+ end
- n = n + 1
- until n > max
+ local noce = Cs(nocell) / ""
+ local c = Cs(ce * (noce + ce)^0)
- return new
+ return c:match(rows[2])
end
function gol.next_frame (old, rule)
@@ -245,10 +247,6 @@ function gol.next_frame (old, rule)
rows[1] = old[n-1] or old[#old] -- last
rows[3] = old[n+1] or old[1] -- next
- -- dead borders
- --rows[1] = old[n-1] or gol.helpers.dead_row(string.len(b)) -- last
- --rows[3] = old[n+1] or gol.helpers.dead_row(string.len(b)) -- next
-
new[n] = gol.next_line( rows, rule )
n = n + 1
@@ -321,12 +319,14 @@ function gol.pre_frame (frame)
["9"] = "9",
}
+ local cell = R"09" + P"D"
+ local nocell = 1-cell
+ local ce = Cs(cell) / repl
+ local noce = Cs(nocell) / ""
+ local c = Cs(ce * (noce + ce)^0)
+
for j, row in ipairs(frame) do
- local out = row
- for chr, subst in pairs(repl) do
- out = out:gsub(chr, subst)
- end
- io.write("\n"..out)
+ io.write("\n" .. c:match(row))
end
end
@@ -341,6 +341,5 @@ function gol.pre_movie (frames, section)
io.write("\n")
end
-
return gol