From fb7ec40915d2180561189e4ccb10e145d20f1328 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 14 Aug 2010 18:53:51 +0200 Subject: added simple parser for .rle format --- life.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'life.lua') diff --git a/life.lua b/life.lua index fca6d4c..41e2912 100644 --- a/life.lua +++ b/life.lua @@ -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 -- cgit v1.2.3