summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--automata.lua (renamed from mplife.lua)39
-rw-r--r--automatarc-global.lua (renamed from mpliferc-global.lua)6
-rw-r--r--automatarc.lua (renamed from mpliferc.lua)4
-rw-r--r--eca.lua109
-rw-r--r--life.lua2
-rw-r--r--run.lua60
-rw-r--r--simpleslides-s-Automaton.tex48
7 files changed, 226 insertions, 42 deletions
diff --git a/mplife.lua b/automata.lua
index 7c67719..a765f2c 100644
--- a/mplife.lua
+++ b/automata.lua
@@ -1,21 +1,17 @@
--
--------------------------------------------------------------------------------
--- FILE: mplife.cld
--- USAGE: ./mplife.cld
+-- FILE: automata.lua
+-- USAGE: use with ConTeXt MkIV
-- DESCRIPTION: Metapost output for Conway's Game of Life
--- OPTIONS: ---
--- REQUIREMENTS: ---
--- BUGS: ---
--- NOTES: ---
-- AUTHOR: Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
-- COMPANY:
-- VERSION: 1.0
-- CREATED: 06/08/10 12:28:35 CEST
--- REVISION: ---
--------------------------------------------------------------------------------
--
-require "run"
+--require "run"
+require "life"
local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
@@ -24,10 +20,10 @@ mplife = {}
mplife.setup = mplife.setup or {}
if context then
- environment.loadluafile "mpliferc-global"
- environment.loadluafile "mpliferc"
+ environment.loadluafile "automatarc-global"
+ environment.loadluafile "automatarc"
else -- assuming we're staying in pwd
- require "mpliferc"
+ require "automatarc"
end
mplife.setup.current = {}
@@ -191,7 +187,7 @@ function mplife.life_frames ()
return true
end
-function mplife.successive ()
+function mplife.gol_successive ()
local c = mplife.setup.current
local settings = {}
if mplife.slides then
@@ -207,6 +203,25 @@ function mplife.successive ()
return true
end
+function mplife.eca_successive ()
+ local c = mplife.setup.current
+ local settings = {}
+
+ if mplife.slides then
+ settings.color = "\\MPcolor{simpleslides:contrastcolor}"
+ else
+ context.definecolor({ "automaton:color" }, c.color)
+ settings.color = "\\MPcolor{automaton:color}"
+ end
+
+ local thisframe
+
+ mplife.setup.current.init, thisframe = eca.gen_frame(c.init, c.framesize, c.rule)
+ context(c.before_frame)
+ mplife.draw_grid(thisframe, settings)
+ context(c.after_frame)
+ return true
+end
function mplife.main ()
local c = mplife.setup.current
diff --git a/mpliferc-global.lua b/automatarc-global.lua
index 3c735ed..95225d9 100644
--- a/mpliferc-global.lua
+++ b/automatarc-global.lua
@@ -1,6 +1,6 @@
--
--------------------------------------------------------------------------------
--- FILE: mpliferc-global.lua
+-- FILE: automatarc-global.lua
-- USAGE: with ConTeXt
-- DESCRIPTION: default settings for the automaton module
-- AUTHOR: Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
@@ -18,9 +18,11 @@ do
c.file = "examples/sships.gol"
c.rule = gol.parse_rule("B3/S23") -- default Conway
- c.init = gol.parse_file(c.file)
+ --c.init = gol.parse_file(c.file)
c.last = c.init -- for successive mode
+ c.aspect = 3/4 -- actually 1/(x/y), for eca mode
+
c.extendx = 0
c.extendy = 0
diff --git a/mpliferc.lua b/automatarc.lua
index 518ed21..506b7f5 100644
--- a/mpliferc.lua
+++ b/automatarc.lua
@@ -10,9 +10,11 @@ do
c.file = "examples/ggun.gol"
--c.file = "examples/sships.gol"
c.rule = gol.parse_rule("B3/S23") -- default Conway
- c.init = gol.parse_file(c.file)
+ --c.init = gol.parse_file(c.file)
c.last = c.init -- for successive mode
+ c.aspect = 3/4
+
c.extendx = 0
c.extendy = 0
diff --git a/eca.lua b/eca.lua
new file mode 100644
index 0000000..6c49e8d
--- /dev/null
+++ b/eca.lua
@@ -0,0 +1,109 @@
+--
+--------------------------------------------------------------------------------
+-- FILE: eca.lua
+-- USAGE: ./eca.lua
+-- DESCRIPTION: drawing elementary cellular automata
+-- OPTIONS: ---
+-- REQUIREMENTS: ---
+-- BUGS: ---
+-- NOTES: ---
+-- AUTHOR: Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
+-- COMPANY:
+-- VERSION: 1.0
+-- CREATED: 14/08/10 19:43:35 CEST
+-- REVISION: ---
+--------------------------------------------------------------------------------
+--
+
+require "life"
+
+local help = gol.helpers
+
+local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
+
+
+eca = {}
+
+
+function eca.parse_file (fname)
+ local f = assert(io.open(fname, "r"), "No such file: "..fname..".")
+ local init = f:read("*all")
+ f:close()
+
+ return help.strip(init)
+end
+
+function eca.to_base(number, base)
+ local alphabet, result, cnt, remainder = "0123456789abcdef", "", 0
+ while number > 0 do
+ cnt = cnt + 1
+ number, remainder = math.floor(number / base), (number % base) + 1
+ result = string.sub(alphabet, remainder, remainder) .. result
+ end
+ return result
+end
+
+function eca.gen_rule(nr)
+ local rules = {}
+ local states = { "111", "110", "101", "100", "011", "010", "001", "000" }
+
+ nr = string.format("%8s",eca.to_base(nr, 2)):gsub(" ", "0")
+
+ local n = 1
+ while n <= #states do
+ rules[states[n]] = nr:sub(n,n)
+ n = n + 1
+ end
+
+ return rules
+end
+
+function eca.next_line(from, rule)
+ local new = ""
+ from = "0"..from.."0" -- assume dead borders
+ --from = string.format("0%s0", from)
+
+ local cell = C(S"01")
+ local cells= Ct(cell * #(cell * cell)) / function (t)
+ local three = t[1]..t[2]..t[3]
+ new = new .. rule[three]
+ end
+ lpeg.match(cells^1, from)
+ return new
+end
+
+function eca.gen_frame(from, lines, rule)
+ local new = { [1] = from }
+
+ local cnt = 1
+ local tmp
+ repeat
+ --print(cnt)
+ tmp = eca.next_line(from, rule)
+ table.insert( new, tmp )
+ from = tmp
+ cnt = cnt + 1
+ until cnt == lines
+ --for i,j in ipairs(new) do
+ --print (string.format("%3s >> %s",i,j))
+ --end
+ return eca.next_line(tmp, rule), new
+end
+
+function eca.successive (current)
+ --if not current then print("NOPE")return 1 end
+ --local current = current or {}
+
+ local thisframe
+ current.from, thisframe = eca.gen_frame(current.from, current.framesize, current.rule)
+ if not context then -- cli invocation
+ for _,j in ipairs(thisframe) do
+ io.write(j:gsub("0","ยท"):gsub("1","O").."\n")
+ end
+ else
+
+ end
+ return 0
+end
+
+return eca
diff --git a/life.lua b/life.lua
index 41e2912..af0cb6e 100644
--- a/life.lua
+++ b/life.lua
@@ -233,6 +233,7 @@ function gol.parse_file (fname)
local p_fn = (1-p_suf)^1 * C(p_suf)
local suffix = p_fn:match(fname) or ".gol" -- assume .gol format as default
+ print("HERE!!!!!!!!!"..suffix)
local tmp = gol.formats[suffix] ( fname )
if mplife and mplife.setup.current and (
mplife.setup.current.extendx > 0 or
@@ -424,4 +425,3 @@ function gol.pre_movie (frames, section)
end
return gol
-
diff --git a/run.lua b/run.lua
index d8c18ad..357f350 100644
--- a/run.lua
+++ b/run.lua
@@ -31,12 +31,13 @@ end
require "life"
+local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
+
local help = gol.helpers
-life = {}
-life.debug = 1
+debug = 1
-function life.get_args ()
+function get_args ()
gol.arg = arg
if gol.arg[-1] == "texlua" then
gol.machine = gol.arg[-1]
@@ -59,10 +60,9 @@ function life.get_args ()
return kv_args()
end
-function main ()
- local current = {}
- current.kv_args = life.get_args()
- current.file = current.kv_args.file or "10x10_glider.gol"
+function life (current)
+ -- how many frames should we draw?
+ current.n = current.kv_args.n or 40
-- sustaining dead cells
current.sustain = current.kv_args.sustain or 0 -- TODO
@@ -70,9 +70,9 @@ function main ()
-- check for debug flag
if tonumber(current.kv_args.debug) == 0 then
- life.debug = false
+ debug = false
else
- life.debug = current.kv_args.debug or life.debug
+ debug = current.kv_args.debug or debug
end
@@ -83,8 +83,8 @@ function main ()
current.rule = gol.parse_rule ("B3/S23") -- Conway's rule
end
- if life.debug then for n, a in pairs(current.kv_args) do print(n, a) end end
- if life.debug then for i, j in pairs(current.rule) do print(i, #j) end end
+ if debug then for n, a in pairs(current.kv_args) do print(n, a) end end
+ if debug then for i, j in pairs(current.rule) do print(i, #j) end end
-- read the initial state (get an array of strings)
if current.file then
@@ -94,22 +94,50 @@ function main ()
end
if current.init then
- if life.debug then
+ if debug then
gol.pre_frame(current.init)
end
- local many = gol.frames( current.init, current.rule, 40 )
+ local many = gol.frames( current.init, current.rule, current.n )
gol.pre_movie (many, true)
- --local lots = gol.frames( current.init, current.rule, 200 )
- --gol.pre_movie (lots, true)
else
io.write"\nCheck your input file for consistency, please.\n"
return 1
end
+ return 0
+end
-
+function eca(current)
+ local eca = require "eca"
+ -- how many lines to draw
+ current.n = tonumber(current.kv_args.n) or 30
+ current.aspect = tonumber(current.kv_args.aspect) or 3/4
+ current.rule = eca.gen_rule(tonumber(current.kv_args.rule) or 110)
+ current.from = eca.parse_file(current.kv_args.file)
+ current.framesize = math.floor(current.aspect * current.from:len())
+
+ for n=1,current.n,1 do
+ gol.pre_section(current.from:len(), n)
+ eca.successive(current)
+ end
return 0
end
+function main ()
+ local current = {}
+ current.kv_args = get_args()
+ current.file = current.kv_args.file or "10x10_glider.gol"
+
+ local p_suf = P"." * (1-P".")^3 * -P(1)
+ local p_fn = (1-p_suf)^1 * C(p_suf)
+ local suffix = p_fn:match(current.file) or ".gol" -- assume .gol format as default
+
+ if suffix == ".eca" then
+ return eca(current)
+ else
+ return life(current)
+ end
+end
+
if not context then
return main()
end
diff --git a/simpleslides-s-Automaton.tex b/simpleslides-s-Automaton.tex
index c1e3181..1fb4e08 100644
--- a/simpleslides-s-Automaton.tex
+++ b/simpleslides-s-Automaton.tex
@@ -19,9 +19,9 @@
\startmodule[simpleslides-s-Automaton]
-\setupmodule[file=,rule=,extendxy=] % using defaults from lua config
+\setupmodule[mode=,file=,rule=,extendxy=] % using defaults from lua config
-\ctxlua{environment.loadluafile( "mplife" )}
+\ctxlua{environment.loadluafile( "automata" )}
\ctxlua{mplife.slides = true}
\unprotect
@@ -94,7 +94,21 @@
\setupcolors[textcolor={simpleslides:textcolor}]
-%D Set the initial Game of Life snapshot (from file) and the rule, if given.
+%D Set the initial snapshot (from file) and the rule, if given.
+
+\doifsomethingelse{\moduleparameter{simpleslides}{mode}}
+{\ctxlua{
+mplife.setup.current.mode = "\luaescapestring{\moduleparameter{simpleslides}{mode}}"
+if mplife.setup.current.mode == "life" then
+ mplife.successive = mplife.gol_successive
+else
+ environment.loadluafile("eca")
+ mplife.successive = mplife.eca_successive
+end}}
+{\ctxlua{mplife.successive = mplife.gol_successive}}
+
+%D The number of dead cells to add left and right / before and after
+%D the initial frame.
\doifsomething{\moduleparameter{simpleslides}{extendxy}}{\ctxlua
{mplife.setup.current.extendx = tonumber("\luaescapestring{\moduleparameter{simpleslides}{extendxy}}")
@@ -106,13 +120,27 @@
\doifsomething{\moduleparameter{simpleslides}{extendy}}{\ctxlua
{mplife.setup.current.extendy = tonumber("\luaescapestring{\moduleparameter{simpleslides}{extendy}}") }}
-\doifsomething{\moduleparameter{simpleslides}{file}}{\ctxlua
- {mplife.setup.current.file = "\luaescapestring{\moduleparameter{simpleslides}{file}}"
- mplife.setup.current.init = gol.parse_file(mplife.setup.current.file)
- mplife.setup.current.last = mplife.setup.current.init }}
-
-\doifsomething{\moduleparameter{simpleslides}{rule}}{\ctxlua
- {mplife.setup.current.rule = gol.parse_rule("\luaescapestring{\moduleparameter{simpleslides}{rule}}") }}
+\doifsomething{\moduleparameter{simpleslides}{file}}{\ctxlua{
+mplife.setup.current.file = "\luaescapestring{\moduleparameter{simpleslides}{file}}"
+}}
+
+\startluacode
+if mplife.setup.current.mode == "life" then
+ mplife.setup.current.init = gol.parse_file(mplife.setup.current.file)
+ mplife.setup.current.last = mplife.setup.current.init
+else
+ mplife.setup.current.init = eca.parse_file(mplife.setup.current.file)
+ mplife.setup.current.framesize = math.floor(mplife.setup.current.aspect * mplife.setup.current.init:len())
+end
+\stopluacode
+
+\doifsomething{\moduleparameter{simpleslides}{rule}}{\ctxlua{
+if mplife.setup.current.mode == "life" then
+ mplife.setup.current.rule = gol.parse_rule("\luaescapestring{\moduleparameter{simpleslides}{rule}}")
+else
+ mplife.setup.current.rule = eca.gen_rule(tonumber("\luaescapestring{\moduleparameter{simpleslides}{rule}}"))
+end
+}}
%D We also use \METAPOST\ to draw the horizontal and vertical page backgrounds.
%D Backgrounds will be of solid color, the ornament gets the transparency.