diff options
author | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-08-07 13:43:59 +0200 |
---|---|---|
committer | Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de> | 2010-08-07 13:43:59 +0200 |
commit | b4c5f9ae1acbf541d1aea969f668b0d8406fd085 (patch) | |
tree | 77aa429cfa52d726b11190c0d7fcf8b5e08caa9d /life.lua | |
parent | d5dcf34a13119496b1a3c7f0ce13e24c84292bac (diff) | |
download | automata-backgrounds-b4c5f9ae1acbf541d1aea969f668b0d8406fd085.tar.gz |
drawing simple backgrounds successively
Diffstat (limited to 'life.lua')
-rw-r--r-- | life.lua | 43 |
1 files changed, 33 insertions, 10 deletions
@@ -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 |