summaryrefslogtreecommitdiff
path: root/run.lua
blob: af54412993d2fbb859988a7827755b5413364d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--
--------------------------------------------------------------------------------
--         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"

    -- 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
    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 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


    return 0
end

--return main()