diff options
author | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-08-14 18:53:51 +0200 |
---|---|---|
committer | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-08-14 18:53:51 +0200 |
commit | fb7ec40915d2180561189e4ccb10e145d20f1328 (patch) | |
tree | 8e0271eaa21ecd3a2483c54303044a86f94960f1 | |
parent | 731fea55e3c23336abc487907df1d0a9c1ed79d2 (diff) | |
download | automata-backgrounds-fb7ec40915d2180561189e4ccb10e145d20f1328.tar.gz |
added simple parser for .rle format
-rw-r--r-- | life.lua | 57 | ||||
-rw-r--r-- | mplife.lua | 2 | ||||
-rw-r--r-- | mpliferc-global.lua | 3 | ||||
-rw-r--r-- | mpliferc.lua | 3 | ||||
-rw-r--r-- | parsers/rle.lua | 95 | ||||
-rw-r--r-- | simpleslides-s-Automaton.tex | 14 |
6 files changed, 170 insertions, 4 deletions
@@ -178,13 +178,68 @@ gol.formats[".gol"] = function (fname) end end +gol.formats[".cell"] = function (fname) + return false -- stub +end + +gol.formats[".rle"] = function (fname) + local parser = require "parsers/rle" + return parser(fname) +end + +function gol.extend_area(rows) + local tmp = {} + local c = mplife.setup.current + + context.writestatus("simpleslides","Extending area horizontally by "..c.extendx.."cells.") + context.writestatus("simpleslides","Extending area vertically by "..c.extendy.."cells.") + + if c.extendy > 0 then + local row = "" + for i=1, rows[1]:len(), 1 do -- buildling an empty row + row = row.."0" + end + + for i=1, c.extendy, 1 do + table.insert(tmp, row) + end + + for _, r in ipairs(rows) do + table.insert(tmp, r) + end + + for i=1, c.extendy, 1 do + table.insert(tmp, row) + end + end + + if c.extendx > 0 then + local add = "" + for i=1, c.extendx, 1 do -- building an empty pre- and suffix + add = add .. "0" + end + + for n, r in ipairs(tmp) do + tmp[n] = add .. r .. add + end + end + + --for i,j in ipairs(tmp) do print(i,j) end + return tmp +end function gol.parse_file (fname) local p_suf = P"." * (1-P".")^3 * -P(1) local p_fn = (1-p_suf)^1 * C(p_suf) local suffix = p_fn:match(fname) or ".gol" -- assume .gol format as default - return gol.formats[suffix] ( fname ) + local tmp = gol.formats[suffix] ( fname ) + if mplife and mplife.setup.current and ( + mplife.setup.current.extendx > 0 or + mplife.setup.current.extendy > 0) then + tmp = gol.extend_area(tmp) + end + return tmp end @@ -36,7 +36,7 @@ mplife.setup.current = {} do local g, r, c = mplife.setup.global, mplife.setup.rc, mplife.setup.current for key,_ in pairs(g) do - print(key, ">>>", tostring(r[key]) .. " or " .. tostring(g[key])) + --print(key, ">>>", tostring(r[key]) .. " or " .. tostring(g[key])) c[key] = r[key] or g[key] -- prefer local settings end mplife.setup.current = c diff --git a/mpliferc-global.lua b/mpliferc-global.lua index 83ab6eb..3c735ed 100644 --- a/mpliferc-global.lua +++ b/mpliferc-global.lua @@ -21,6 +21,9 @@ do c.init = gol.parse_file(c.file) c.last = c.init -- for successive mode + c.extendx = 0 + c.extendy = 0 + c.pensize = .1 c.color = { r = .6, g = .6, b = .8 } c.opacity = 1/3 diff --git a/mpliferc.lua b/mpliferc.lua index 00bd880..518ed21 100644 --- a/mpliferc.lua +++ b/mpliferc.lua @@ -13,6 +13,9 @@ do c.init = gol.parse_file(c.file) c.last = c.init -- for successive mode + c.extendx = 0 + c.extendy = 0 + c.pensize = .1 c.color = { r = .6, g = .6, b = .8 } c.opacity = 1/3 diff --git a/parsers/rle.lua b/parsers/rle.lua new file mode 100644 index 0000000..cd10493 --- /dev/null +++ b/parsers/rle.lua @@ -0,0 +1,95 @@ +-- http://psoup.math.wisc.edu/mcell/ca_files_formats.html#RLE +local C, Cs, Ct, P, R, S, match = lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match + +local rows = {} +local coords = {} + +local n_row = 1 +rows[n_row] = "" + +local eol = P"\n" +local hashmark = P"#" +local space = S" \t" +local whitespace = S(" \n\t")^0 +local num = R"09" +local comma = P"," +local exclam = P"!" +local equals = P"=" +local dollar = P"$" +local state = S"bo" +--local sign = S("+-")^0 -- support negative grid values? not yet + +local comment = hashmark * (1-eol)^0 * eol + +local getx = space^0 * P"x" * space^0 * equals * space^0 * C(num^1) / function (x) + coords.x = tonumber(x) +end +local gety = space^0 * P"y" * space^0 * equals * space^0 * C(num^1) / function (y) + coords.y = tonumber(y) +end +geometry = getx * comma * gety * (1-eol)^0 * eol + +local function n_of_t (n, c) + local tmp = "" + if c == "o" then -- live + c = "1" + else -- dead + c = "0" + end + + for i=1, n, 1 do + tmp = tmp .. c + end + return tmp +end + +local cell = C(num^0) * C(state) / function (n, c) + if n == "" then + n = 1 + end + + if n_of_t (tonumber(n), c) == nil then + print("OVER HERE", n_row,n, c) + end + rows[n_row] = rows[n_row] .. n_of_t (tonumber(n), c) +end + +local line = whitespace * (cell * whitespace)^0 * C(num^0) / function (empty) + + if rows[n_row]:len() < coords.x then -- fill grid with dead cells + rows[n_row] = rows[n_row] .. n_of_t( coords.x - rows[n_row]:len(), "0" ) + end + + if empty ~= "" then + for n=1, tonumber(empty)-1, 1 do + n_row = n_row + 1 + rows[n_row] = n_of_t( coords.x, "0" ) + end + end + + n_row = n_row + 1 + + if n_row <= coords.y then + rows[n_row] = "" + end +end + +local p_rle = comment^0 * geometry * line * (dollar * line)^0 * whitespace * exclam + +local function parser(fname) + local file = assert(io.open(fname, "r"), "Not a file: " .. fname) + local data = file:read("*all") + file:close() + + if p_rle:match(data) then + if context then + context.writestatus("simpleslides", "Sucessfully loaded frame from "..fname..".") + end + return rows + else + return false + end +end + +return parser + diff --git a/simpleslides-s-Automaton.tex b/simpleslides-s-Automaton.tex index 3300844..c1e3181 100644 --- a/simpleslides-s-Automaton.tex +++ b/simpleslides-s-Automaton.tex @@ -15,11 +15,11 @@ %D Initially based on the BigNumber theme by A. Mahajan and Th. Schmitz. (Many, %D many thanks!) -\writestatus{simpleslides}{loading Test} +\writestatus{simpleslides}{loading Theme Automata} \startmodule[simpleslides-s-Automaton] -\setupmodule[file=,rule=] % using defaults from lua config +\setupmodule[file=,rule=,extendxy=] % using defaults from lua config \ctxlua{environment.loadluafile( "mplife" )} \ctxlua{mplife.slides = true} @@ -96,6 +96,16 @@ %D Set the initial Game of Life snapshot (from file) and the rule, if given. +\doifsomething{\moduleparameter{simpleslides}{extendxy}}{\ctxlua + {mplife.setup.current.extendx = tonumber("\luaescapestring{\moduleparameter{simpleslides}{extendxy}}") + mplife.setup.current.extendy = tonumber("\luaescapestring{\moduleparameter{simpleslides}{extendxy}}") }} + +\doifsomething{\moduleparameter{simpleslides}{extendx}}{\ctxlua + {mplife.setup.current.extendx = tonumber("\luaescapestring{\moduleparameter{simpleslides}{extendx}}") }} + +\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) |