summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dat/lua/eat.lua
blob: c29f043456b60a876d84e70205007552e9fe3685 (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
---------------------------------------------------------------------------
-- eat.lua:
-- Prompts to eat chunks from inventory.
--
-- To use this, add this line to your init.txt:
--   lua_file = lua/eat.lua
--
-- See c_eat in this file if you want to tweak eating behaviour further.
---------------------------------------------------------------------------
function prompt_eat(i)
    local iname = item.name_coloured(i, "a")
    if item.quantity(i) > 1 then
        iname = "one of " .. iname
    end
    crawl.mpr("Eat " .. iname .. "?", "prompt")

    local res
    res = crawl.getch()
    if res == 27 then
        res = "escape"
    elseif res < 32 or res > 127 then
        res = ""
    else
        res = string.lower(string.format("%c", res))
    end
    return res
end

function chunk_maybe_safe(chunk)
    local rot = food.rotting(chunk)
    local race = you.race()

    if rot then
        return you.saprovorous() > 0
    end

    return true
end

function is_chunk_safe(chunk)
    local rot = food.rotting(chunk)
    local race = you.race()

    -- Check if the user has sourced safechunk.lua and chnkdata.lua
    if not (sc_cont and sc_pois and sc_hcl and sc_mut and sc_safechunk) then
        return false
    end
    
    local cname = item.name(chunk)
    local mon
    _, _, mon = string.find(cname, "chunk of (.+) flesh")
    
    return sc_safechunk(rot, race, mon)
end

-- Called by Crawl. Note that once Crawl sees a c_eat function, it bypasses the
-- built-in (e)at command altogether.
--
function c_eat(floor, inv)
    -- To enable auto_eat_chunks, you also need to source chnkdata.lua and
    -- safechunk.lua. WARNING: These files contain spoily information.
    local auto_eat_chunks = options.auto_eat_chunks == "yes" or
                            options.auto_eat_chunks == "true"
                            
    if auto_eat_chunks then
        local all = { }
        for _, it in ipairs(floor) do table.insert(all, it) end
        for _, it in ipairs(inv)   do table.insert(all, it) end

        for _, it in ipairs(all) do
            if food.ischunk(it) and food.can_eat(it) and is_chunk_safe(it) then
                local iname = item.name(it, "a")
                if item.quantity(it) > 1 then
                    iname = "one of " .. iname
                end
                crawl.mpr("Eating " .. iname)
                food.eat(it)
                return
            end
        end
    end

    -- Prompt to eat chunks off the floor. Returns true if the player chose
    -- to eat a chunk.
    if food.prompt_floor() then
        return
    end
    
    for i, it in ipairs(inv) do
        -- If we have chunks in inventory that the player can eat, prompt.
        if food.ischunk(it) and food.can_eat(it) and chunk_maybe_safe(it) then
            local answer = prompt_eat(it)
            if answer == "q" then
                break
            end
            if answer == "escape" then
                return
            end
            if answer == "y" or answer == "e" then
                food.eat(it)
                return
            end
        end
    end

    -- Allow the player to choose a snack from inventory
    food.prompt_inventory()
end