File: prog_goanim.lua

package info (click to toggle)
fillets-ng-data 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180,924 kB
  • sloc: makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (8)
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
115
116
117
118
119
120
121

-- -----------------------------------------------------------------
-- setanim() and gonanim()
-- -----------------------------------------------------------------
-- Example: setanim(model, "a3d3a4a5a6a7d2a6a5a4a3")

function resetanim(model)
    model.anim_delay = 0
    model.anim_pos = 1
    model.anim_label = 1
end

function endanim(model)
    -- returns true for just finished animation
    local result = false
    if model.anim_pos > string.len(model.anim) and model.anim ~= "" then
        result = true
    end
    return result
end

function setanim(model, anim_string)
    resetanim(model)
    model.anim = anim_string
end

local function anim_getSymbol(model)
    return string.sub(model.anim, model.anim_pos, model.anim_pos)
end
local function anim_incPos(model)
    model.anim_pos = model.anim_pos + 1
end

local function anim_var(model)
    -- var := \[[a-zA-Z]+\]
    local istart, iend, str_var = string.find(model.anim,
            "%[(%w+)%]", model.anim_pos)
    if istart == nil then
        error("SCRIPT_ERROR empty anim_var; anim="..model.anim)
    end
    return str_var
end

local function anim_number(model)
    -- N := -?[0-9]+ | \?N.N
    local symbol = anim_getSymbol(model)
    if symbol == "?" then
        anim_incPos(model)
        local int1 = anim_number(model)
        anim_incPos(model)
        local int2 = anim_number(model)
        return randint(int1, int2)
    end

    local istart, iend, str_number = string.find(model.anim,
            "(%-?%d+)", model.anim_pos)
    local number = 0
    if istart == nil then
        print("SCRIPT_WARNING empty number; anim="..model.anim)
    else
        model.anim_pos = iend + 1
        number = tonumber(str_number)
        if number == nil then
            error("SCRIPT_ERROR bad number; str_number="..str_number)
        end
    end
    return number
end

local function anim_next(model)
    -- Process next symbol, uppercase letters are not blocking.
    local symbol = "A"
    while string.lower(symbol) ~= symbol and model.anim_pos <= string.len(model.anim) do
        symbol = anim_getSymbol(model)
        anim_incPos(model)
        switch(string.lower(symbol)){
            ["d"] = function()
                model.anim_delay = anim_number(model)
                if model.anim_delay < 0 then
                    model.anim_delay = 0
                    symbol = "D"
                end
            end,
            ["a"] = function()
                model.afaze = anim_number(model)
            end,
            ["s"] = function()
                local var = anim_var(model)
                anim_incPos(model)
                local value = anim_number(model)
                model[var] = value
            end,
            ["l"] = function()
                model.anim_label = model.anim_pos
            end,
            ["g"] = function()
                model.anim_pos = model.anim_label
            end,
            ["r"] = function()
                model.anim_pos = 1
            end,
            default = function()
                error("SCRIPT_ERROR unknown anim symbol; symbol="..symbol.."; pos="..model.anim_pos.."; anim="..model.anim)
            end,
        }
    end
end


function goanim(model)
    -- goanim process next anim command
    if model.anim_delay > 0 then
        model.anim_delay = model.anim_delay - 1
    elseif model.anim_pos > string.len(model.anim) then
        model.anim = ""
        model.anim_pos = 1
    else
        anim_next(model)
    end
end