summaryrefslogtreecommitdiffstats
path: root/stone_soup/crawl-ref/source/lua/runrest.lua
blob: 6f5536506e151aad0442f2b58c6c4325745db6ef (plain) (blame)
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
---------------------------------------------------------------------------
-- runrest.lua: (requires base.lua)
-- Controls shift-running and resting stop conditions.
--
-- To use this, add this line to your init.txt:
--   lua_file = lua/runrest.lua
--
-- What it does:
-- 
--  * Any message in runrest_ignore_message will *not* stop your run.
--  * Poison damage of x will be ignored if you have at least y hp if you've
--    defined a runrest_ignore_poison = x:y option.
--    
-- IMPORTANT: You must define runrest_ options *after* sourcing runrest.lua.
---------------------------------------------------------------------------

g_rr_ignored = { }

chk_interrupt_activity.run = function (iname, cause, extra)
    if not rr_check_params() then
        return false
    end

    if iname == 'message' then
        return rr_handle_message(cause, extra)
    end

    if iname == 'hp_loss' then
        return rr_handle_hploss(cause, extra)
    end

    return false
end

function rr_handle_message(cause, extra)
    local ch, mess = rr_split_channel(cause)
    for _, m in ipairs(g_rr_ignored) do
        if m:matches(mess, ch) then
            return nil
        end
    end
    return false
end

function rr_split_channel(s)
    local chi = string.find(s, ':')
    local channel = -1
    if chi and chi > 1 then
        local chstr = string.sub(s, 1, chi - 1)
        channel = crawl.msgch_num(chstr)
    end

    local sor = s
    if chi then
        s = string.sub(s, chi + 1, -1)
    end

    return channel, s
end

function rr_handle_hploss(hplost, source)
    -- source == 1 for poisoning
    if not g_rr_yhpmin or not g_rr_hplmax or source ~= 1 then
        return false
    end

    -- If the hp lost is smaller than configured, and you have more than the
    -- minimum health, ignore this poison event.
    if hplost <= g_rr_hplmax and you.hp() >= g_rr_yhpmin then
        return nil
    end
    
    return false
end

function rr_check_params()
    if g_rrim ~= options.runrest_ignore_message then
        g_rrim = options.runrest_ignore_message
        rr_add_messages(nil, g_rrim)
    end

    if ( not g_rr_hplmax or not g_rr_yhpmin ) 
            and options.runrest_ignore_poison
    then
        local opt = options.runrest_ignore_poison
        local hpl, hpm
        _, _, hpl, hpm = string.find(opt, "(%d+)%s*:%s*(%d+)")
        if hpl and hpm then
            g_rr_hplmax = tonumber(hpl)
            g_rr_yhpmin = tonumber(hpm)
        end
    end
    return true
end

function rr_add_message(s)
    local channel, str = rr_split_channel(s)
    table.insert( g_rr_ignored, crawl.message_filter( str, channel ) )
end

function rr_add_messages(key, value)
    local segs = crawl.split(value, ',')
    for _, s in ipairs(segs) do
        rr_add_message(s)
    end
end

chk_lua_option.runrest_ignore_message = rr_add_messages