summaryrefslogtreecommitdiff
path: root/run.lua
diff options
context:
space:
mode:
Diffstat (limited to 'run.lua')
-rw-r--r--run.lua111
1 files changed, 111 insertions, 0 deletions
diff --git a/run.lua b/run.lua
new file mode 100644
index 0000000..611120e
--- /dev/null
+++ b/run.lua
@@ -0,0 +1,111 @@
+--
+--------------------------------------------------------------------------------
+-- FILE: run.lua
+-- USAGE: ./run.lua
+-- DESCRIPTION: Game of Life CLI frontend
+-- OPTIONS: ---
+-- REQUIREMENTS: ---
+-- BUGS: ---
+-- NOTES: ---
+-- AUTHOR: Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
+-- COMPANY:
+-- VERSION: 1.0
+-- CREATED: 05/08/10 13:09:52 CEST
+-- REVISION: ---
+--------------------------------------------------------------------------------
+--
+
+-- check for a capable interpreter
+if not (arg[-1] == "texlua" or
+ context ~= nil) then
+ print ([[
+
+···············································································
+Please use the LuaTeX interpreter or modify the sources to fit your
+Lua machine of choice!
+GOTO http://www.luatex.org/
+···············································································
+]])
+ return 1
+end
+
+require "life"
+
+local help = gol.helpers
+
+life = {}
+life.debug = 1
+
+function life.get_args ()
+ gol.arg = arg
+ if gol.arg[-1] == "texlua" then
+ gol.machine = gol.arg[-1]
+ gol.self_name = gol.arg[0]
+ gol.arg[-1], gol.arg[0] = nil, nil
+ elseif context ~= nil then
+ -- TODO
+ end
+
+ local kv_args = function ()
+ local tmp = {}
+ local so = help.split_once
+ for _, a in ipairs(gol.arg) do
+ local lr = so(a, "=")
+ tmp[help.strip(lr[1], "-")] = lr[2]
+ end
+ return tmp
+ end
+
+ return kv_args()
+end
+
+function main ()
+ local current = {}
+ current.kv_args = life.get_args()
+ current.file = current.kv_args.file or "10x10_glider.gol"
+
+ -- check for debug flag
+ if tonumber(current.kv_args.debug) == 0 then
+ life.debug = false
+ else
+ life.debug = current.kv_args.debug or life.debug
+ end
+
+
+ -- prepare the rule
+ if current.kv_args.rule then
+ current.rule = gol.parse_rule (current.kv_args.rule)
+ else
+ current.rule = gol.parse_rule ("B3/S23") -- Conway's rule
+ end
+
+ if life.debug then for n, a in pairs(current.kv_args) do print(n, a) end end
+ if life.debug then for i, j in pairs(current.rule) do print(i, #j) end end
+
+ -- read the initial state (get an array of strings)
+ if current.file then
+ current.init = gol.parse_file (current.file)
+ else
+ return 1
+ end
+
+ if current.init then
+ if life.debug then
+ gol.pre_frame(current.init)
+ end
+ 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
+
+return main()
+