diff options
| -rw-r--r-- | examples/ggun.gol | 37 | ||||
| -rw-r--r-- | examples/gliders.gol | 21 | ||||
| -rw-r--r-- | life.lua | 49 | ||||
| -rw-r--r-- | mplife.cld | 81 | ||||
| -rw-r--r-- | run.lua | 8 | 
5 files changed, 138 insertions, 58 deletions
| diff --git a/examples/ggun.gol b/examples/ggun.gol new file mode 100644 index 0000000..4855d59 --- /dev/null +++ b/examples/ggun.gol @@ -0,0 +1,37 @@ +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000100000000000000000000000000000000000000000000000000000000000 +000000000000000000000000010100000000000000000000000000000000000000000000000000000000000 +000000000000000110000001100000000000011000000000000000000000000000000000000000000000000 +000000000000001000100001100000000000011000000000000000000000000000000000000000000000000 +000110000000010000010001100000000000000000000000000000000000000000000000000000000000000 +000110000000010001011000010100000000000000000000000000000000000000000000000000000000000 +000000000000010000010000000100000000000000000000000000000000000000000000000000000000000 +000000000000001000100000000000000000000000000000000000000000000000000000000000000000000 +000000000000000110000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/examples/gliders.gol b/examples/gliders.gol new file mode 100644 index 0000000..190e50f --- /dev/null +++ b/examples/gliders.gol @@ -0,0 +1,21 @@ +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000011100000000000000000000000 +000000000000000000000000000000000000000000000000000000100000000000000000000000 +000000000000000000000000000000000000000000000000000001000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000010000000000000000000000000000000000000000000000000000000000000 +000000000000000001000000000000000000000000000000000000000000000000000000000000 +000000000000000111000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000000000000 @@ -52,18 +52,11 @@ function gol.helpers.split_once (s, sep)  end  -- Modified stripper from Roberto to support trimming other chars than whitespace -function gol.helpers.strip(str, char) -    local space    = S(" \t\v\n") - -    if char then -        char = P(char) -    else -        char = space -    end - -    local nochar = 1 - char - -    local stripper = char^0 * C((char^0 * nochar^1)^0) +function gol.helpers.strip(str, chars) +    local chars = chars or " \t\v\n" +    chars = S(chars) +    local nochars = 1 - chars +    local stripper = chars^0 * C((chars^0 * nochars^1)^0)      return match(stripper,str) or ""  end @@ -104,17 +97,16 @@ function gol.parse_rule (raw_rule)      return { birth = b, stay = s }  end -function gol.apply_rule (cell, cnt, rule, fade) +function gol.apply_rule (cell, cnt, rule, fade, keep)      --local live, dead = "1", "0"      --local new = dead      local new = 0 -    local live = P("1") -    local dead = 1 - live +    local live = "1"      local stay  = rule.stay      local birth = rule.birth      -- checking if cnt matches the numbers from the conditions list -    if live:match(cell) then +    if cell == live then          for _, cond in ipairs(stay) do              if cnt == cond then                  new = "1" @@ -131,12 +123,18 @@ function gol.apply_rule (cell, cnt, rule, fade)      end      if fade then -        if not live:match(new) then +        if not (live == new) then              local add = tonumber (cell) -            if add and add < 9 and add ~= 0 then -                add = add + 1 -            else  -                add = 0 +            if not add then -- == "D" +                add = "D" +            else +                if add and add < 9 and add ~= 0 then +                    add = add + 1 +                elseif keep and add and add == 9 then -- marking dead cells once alive +                    add = "D" +                else  +                    add = 0 +                end              end              new = tostring(add)          end @@ -170,10 +168,7 @@ end  function gol.next_line (rows, rule)      local new  = "" -    --local dead = "0" -    --local live = "1" -    local live = P("1") -    local dead = 1 - live +    local live = "1"      local fade = gol.setup.current.fade or gol.setup.fade @@ -222,13 +217,13 @@ function gol.next_line (rows, rule)          -- counting live cells in the environment          local cnt = 0          for _, chr in pairs(env) do -            if live:match(chr) then +            if chr == live then                  cnt = cnt + 1              end          end          -- adding new cell according to ruleset -        new = new .. gol.apply_rule(current, cnt, rule, fade) +        new = new .. gol.apply_rule(current, cnt, rule, fade, true)          n = n + 1      until n > max @@ -25,19 +25,24 @@ gol.setup.current = gol.setup.current or {}  do      local c = gol.setup.current -    c.file = c.file or "examples/puffertrain.gol" +    --c.file = c.file or "examples/10x10_glider.gol" +    c.file = c.file or "examples/gliders.gol" +    --c.file = c.file or "examples/ggun.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.pensize = .1 +    c.color = { r = .6, g = .6, b = .8 } +    c.fade = false      c.firstframe = 1      c.frames = 5      c.preamble = [[  \setupcolors[state=start]  \setupbackgrounds[state=start]  \setuppapersize[S6][S6] + +\setuppagenumbering[state=stop,location=]  ]]      --c.before_frame = [[  --\defineoverlay @@ -52,17 +57,16 @@ do  --]]      c.before_frame = [[  \startuseMPgraphic{back} -pickup pensquare scaled ]] .. c.pensize .. [[pt; - +pickup pencircle xyscaled (.25*]] .. c.pensize .. [[pt, ]] .. c.pensize .. [[pt) rotated 45;  ]]      c.after_frame = [[ -currentpicture := currentpicture xysized (PaperHeight*.6,PaperWidth*.6) ; +currentpicture := currentpicture xysized (\the\paperwidth, \the\paperheight);  \stopuseMPgraphic  \defineoverlay[back][\useMPgraphic{back}]  \setupbackgrounds [page] [background=back]  ]] -     +      gol.setup.current = c  end @@ -142,7 +146,7 @@ function mplife.p_square (from, filled, fade_level)              else                  fade_level = .05 * fade_level + .5              end -            tmp = tmp .. f.space .. "withcolor" .. f.space .. fade_level .. "white" +            tmp = tmp .. f.space .. "scaled 200 " .. "withcolor" .. f.space .. fade_level .. "white"          end          tmp = tmp .. f.scolon @@ -153,46 +157,65 @@ function mplife.p_square (from, filled, fade_level)  end  function mplife.draw_grid(grid) -    print (grid)      local h = gol.helpers -    local pat = "" --"StartPage; " +    local pat = ""      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      end -    local n = 1 -    --for n, row in ipairs(grid) do -    while grid[n] ~= nil do -        local row = grid[n] +    local max = {} +    max.x, max.y = grid[1]:len(), #grid + +    local n = 0 +    local pos -- kept in broader scope for picture border estimation +    while grid[#grid-n] ~= nil do + +        local row = grid[#grid-n] +        pos = 1 -        local pos = 1          repeat +            local opacity              local cell = row:sub(pos, pos) -            local p = { x = pos, y = 1 - n, unit = "" } +            local p = { x = pos-1, y = n, unit = "" }              local fill, fade_level -            --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));"  + +            if not fade_level then  -- no number --> cell once dead (marked "D") +                fade_level = 9.3 +            end + +            if fade_level ~= 0 then -- don't draw dead cells +                if fade_level == 1 then +                    opacity = .33 +                    pat = pat .. string.format("%s unitsquare shifted (%s,%s) withcolor transparent(1,%s,\\MPcolor{lifesquare});\n",  +                             "draw", p.x, p.y, opacity*2) +                else +                    opacity = (1/3) - (fade_level / 30) +                end +                pat = pat .. string.format("%s unitsquare scaled .9 shifted (%s+.05,%s+.05) withcolor transparent(1,%s,\\MPcolor{lifesquare});\n",  +                             "filldraw", p.x, p.y, opacity) +            end              pos = pos + 1 -            --print ("pos: " .. pos .. "," .. n .. " -->" .. row .. "<-- " .. type(row))          until pos > row:len()          pat = pat .. "\n"          n = n + 1      end +    pat = pat .. string.format([[ +path border; +border = (0,0)--(%s,0)--(%s,%s)--(0,%s)--cycle; +fill border withcolor transparent (1,.75, white) ; +setbounds currentpicture to boundingbox border ; +]], max.x, max.x, max.y, max.y) + +--fill border withcolor transparent (1,.3,1white) ;  +    --print("Pic\n", pat)      mplife.runcode(pat)      return true  end @@ -271,6 +294,8 @@ end  function mplife.main ()      local c = gol.setup.current + +    context.definecolor({ "lifesquare" }, c.color)      context(c.preamble) -- only once      context.starttext() @@ -280,9 +305,9 @@ function mplife.main ()      --mplife.life_frames() -    for i=1,55,1 do +    for i=1,10,1 do          mplife.successive() -        context(Test) +        --context("\\input knuth")          context.page()      end      context.stoptext() @@ -97,9 +97,9 @@ function main ()          if life.debug then               gol.pre_frame(current.init)          end -        local many = gol.frames( current.init, current.rule, 55 ) +        local many = gol.frames( current.init, current.rule, 40 )          gol.pre_movie (many, true) -        --local lots = gol.frames( current.init, current.rule, 55 ) +        --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" @@ -110,5 +110,7 @@ function main ()      return 0  end ---return main() +if not context then +    return main() +end | 
