From b4c5f9ae1acbf541d1aea969f668b0d8406fd085 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 7 Aug 2010 13:43:59 +0200 Subject: drawing simple backgrounds successively --- life.lua | 43 ++++++++++++++----- mplife.cld | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- run.lua | 13 +++--- 3 files changed, 160 insertions(+), 36 deletions(-) diff --git a/life.lua b/life.lua index 6d5a2cb..3961830 100644 --- a/life.lua +++ b/life.lua @@ -18,6 +18,10 @@ 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, Ct, P, R, S, match = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match @@ -100,29 +104,44 @@ function gol.parse_rule (raw_rule) return { birth = b, stay = s } end -function gol.apply_rule (cell, cnt, rule) - local live, dead = "1", "0" - local new = dead +function gol.apply_rule (cell, cnt, rule, fade) + --local live, dead = "1", "0" + --local new = dead + local new = 0 + local live = P("1") + local dead = 1 - live local stay = rule.stay local birth = rule.birth -- checking if cnt matches the numbers from the conditions list - if cell == live then + if live:match(cell) then for _, cond in ipairs(stay) do if cnt == cond then - new = live + new = "1" break end end else -- ==dead for _, cond in ipairs(birth) do if cnt == cond then - new = live + new = "1" break end end end + if fade then + if not live:match(new) then + local add = tonumber (cell) + if add and add < 9 and add ~= 0 then + add = add + 1 + else + add = 0 + end + new = tostring(add) + end + end + return new end @@ -151,8 +170,12 @@ end function gol.next_line (rows, rule) local new = "" - local dead = "0" - local live = "1" + --local dead = "0" + --local live = "1" + local live = P("1") + local dead = 1 - live + + local fade = gol.setup.current.fade or gol.setup.fade local n = 1 local max = string.len(rows[2]) @@ -199,13 +222,13 @@ function gol.next_line (rows, rule) -- counting live cells in the environment local cnt = 0 for _, chr in pairs(env) do - if chr == live then + if live:match(chr) then cnt = cnt + 1 end end -- adding new cell according to ruleset - new = new .. gol.apply_rule(current, cnt, rule) + new = new .. gol.apply_rule(current, cnt, rule, fade) n = n + 1 until n > max diff --git a/mplife.cld b/mplife.cld index 11392e8..b83ef17 100644 --- a/mplife.cld +++ b/mplife.cld @@ -19,6 +19,55 @@ require "run" local mplife = {} +gol.setup = gol.setup or {} +gol.setup.current = gol.setup.current or {} + +do + local c = gol.setup.current + + c.file = c.file or "examples/glider_gen-p55.gol" + c.rule = gol.parse_rule("B3/S23") -- default Conway + c.init = gol.parse_file(c.file) + c.last = c.init -- for successive mode + + c.pensize = .03 + c.fade = true + c.firstframe = 1 + c.frames = 5 + c.preamble = [[ +\setupcolors[state=start] +\setupbackgrounds[state=start] +\setuppapersize[S6][S6] +]] + --c.before_frame = [[ +--\defineoverlay + --[back] + --[\bgroup +--]] + --c.after_frame = [[ +--\egroup] + +--\setupbackgrounds [paper] [background=back] +--\page +--]] + c.before_frame = [[ +\startuseMPgraphic{back} +pickup pensquare scaled ]] .. c.pensize .. [[pt; + +]] + c.after_frame = [[ +currentpicture := currentpicture xysized (PaperHeight*.6,PaperWidth*.6) ; +\stopuseMPgraphic +\defineoverlay[back][\useMPgraphic{back}] + +\setupbackgrounds [page] [background=back] +]] + + gol.setup.current = c +end + + + mplife.fade = true mplife.format = { @@ -27,10 +76,10 @@ mplife.format = { opar = "(", cpar = ")", - hypen = "-", comma = ",", + hyphen = "-", comma = ",", scolon = ";", path = "--", - filldraw = "fill", + filldraw = "fill", -- "draw", draw = "draw", cycle = "cycle", @@ -40,9 +89,9 @@ mplife.format = { } function mplife.runcode (mpcode) - context.startMPcode() + --context.startMPcode() context(mpcode) - context.stopMPcode() + --context.stopMPcode() --context.MPdrawing(mpcode) -- comes with next beta! end @@ -60,7 +109,7 @@ function mplife.p_square (from, filled, fade_level) -- from - d.y from + d.x && - d.y -- p4 p3 - local d = { x = 1, y = 1, unit = "cm" } + local d = { x = 1, y = 1, unit = "" } local points = { [1] = { x = from.x, y = from.y }, [2] = { x = from.x+d.x, y = from.y }, @@ -86,7 +135,14 @@ function mplife.p_square (from, filled, fade_level) if filled and fade_level then --tmp = tmp .. f.space .. "withcolor" .. f.space .. 1/fade_level .. "black" - tmp = tmp .. f.space .. "withcolor" .. f.space .. .1*fade_level .. "white" + if fade_level == 0 then + fade_level = 1 + elseif fade_level == 1 then + fade_level = 0 + else + fade_level = .05 * fade_level + .5 + end + tmp = tmp .. f.space .. "withcolor" .. f.space .. fade_level .. "white" end tmp = tmp .. f.scolon @@ -99,7 +155,7 @@ end function mplife.draw_grid(grid) print (grid) local h = gol.helpers - local pat = "" + local pat = "" --"StartPage; " if type(grid) == "string" then grid = h.split(grid, "\n") -- may leave an empty string as last item if grid[#grid] == "" then grid[#grid] = nil end @@ -113,16 +169,20 @@ function mplife.draw_grid(grid) local pos = 1 repeat local cell = row:sub(pos, pos) - local p = { x = pos, y = 1 - n, unit = "cm" } + local p = { x = pos, y = 1 - n, unit = "" } local fill, fade_level - if cell ~= "0" then fill = true end + --if cell ~= "0" then fill = true end + fill = true if fill and mplife.fade then fade_level = tonumber(cell) end + --if fade_level ~= 0 then -- dont draw dead cells (workaround for rounded pens) + --pat = pat .. mplife.p_square( p, fill, fade_level ) + --end pat = pat .. mplife.p_square( p, fill, fade_level ) --.. "\nlabel.bot(btex $" .. pos .. "$ etex, (" .. --pos .. "cm," .. 1 - n .. "cm));" @@ -146,7 +206,7 @@ function mplife.checker(n) for x=1, n, 1 do local fill if cnt % 2 == 0 then fill = true end - local p = { x = x, y = y, unit = "cm" } + local p = { x = x, y = y, unit = "" } squares = squares .. mplife.p_square( p, fill ) cnt = cnt + 1 end @@ -169,24 +229,62 @@ end --print(mplife.rand_pattern(10,1)) - +--- interfacing with life.lua -function mplife.main () - mpgraphic = [[ -fill fullcircle scaled 200pt withcolor .625yellow; -]] +function mplife.life_frame (frame) + --local c = gol.setup.current - context.setupcolors({state = "start"}) - context.starttext() + mplife.draw_grid(frame) + + return true +end - --apoint = { x = 3, y = 4, unit = "cm" } - --bpoint = { x = 7, y = 5, unit = "cm" } - --mplife.runcode (mplife.p_square( apoint, true) .. mplife.p_square( bpoint, false)) +function mplife.life_movie (frames, next_page) + local c = gol.setup.current + for i, frame in ipairs(frames) do + context(c.before_frame) + mplife.life_frame(frame) + context(c.after_frame) + end +end + +function mplife.life_frames () + local c = gol.setup.current + + + local frames = gol.frames( c.init, c.rule, c.frames, c.firstframe ) + mplife.life_movie( frames, false ) + return true +end + +function mplife.successive () + local c = gol.setup.current + context(c.before_frame) + mplife.draw_grid(c.last) + context(c.after_frame) + gol.setup.current.last = gol.next_frame( c.last, c.rule ) + return true +end + + + + +function mplife.main () + local c = gol.setup.current + context(c.preamble) -- only once + context.starttext() --mplife.runcode (mplife.checker(20)) - mplife.draw_grid(mplife.rand_pattern(10,6)) + --mplife.draw_grid(mplife.rand_pattern(10,6)) + --mplife.life_frames() + + for i=1,55,1 do + mplife.successive() + context(Test) + context.page() + end context.stoptext() return 0 end diff --git a/run.lua b/run.lua index db1c70a..af54412 100644 --- a/run.lua +++ b/run.lua @@ -64,6 +64,10 @@ function main () current.kv_args = life.get_args() current.file = current.kv_args.file or "10x10_glider.gol" + -- sustaining dead cells + current.sustain = current.kv_args.sustain or 0 -- TODO + current.fadeout = current.kv_args.fadeout or false + -- check for debug flag if tonumber(current.kv_args.debug) == 0 then life.debug = false @@ -93,16 +97,15 @@ function main () if life.debug then gol.pre_frame(current.init) end - local lots = gol.frames( current.init, current.rule, 55 ) - gol.pre_movie (lots, true) + local many = gol.frames( current.init, current.rule, 55 ) + gol.pre_movie (many, true) + --local lots = gol.frames( current.init, current.rule, 55 ) + --gol.pre_movie (lots, true) else io.write"\nCheck your input file for consistency, please.\n" return 1 end - -- sustaining dead cells - current.sustain = current.kv_args.sustain or 0 - current.fadeout = current.kv_args.fadeout or 0 -- TODO return 0 end -- cgit v1.2.3