summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/uncancel.cc
blob: 58862caa90f844d89b14fb247a1598b461471b16 (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
/**
 * @file
 * @brief User interactions that need to be restarted if the game is forcibly
 *        saved (via SIGHUP/window close).
**/

#include "AppHdr.h"
#include "acquire.h"
#include "decks.h"
#include "godabil.h"
#include "libutil.h"
#include "player.h"
#include "state.h"
#include "uncancel.h"
#include "unwind.h"

void add_uncancel(uncancellable_type kind, int arg)
{
    you.uncancel.push_back(pair<uncancellable_type, int>(kind, arg));
}

static bool running = false;

void run_uncancels()
{
    // Run uncancels iteratively rather than recursively.
    if (running)
        return;
    unwind_var<bool> run(running, true);

    while (!you.uncancel.empty() && !crawl_state.seen_hups)
    {
        // changed to -1 if we pop prematurely
        int act = you.uncancel.size() - 1;

        // Beware of race conditions: it is not enough to check seen_hups,
        // the action must actually fail as well!  And if it fails but there
        // was no HUP, it's due to some other reason.

        int arg = you.uncancel[act].second;
        dprf("Running uncancel type=%d arg=%d", you.uncancel[act].first, arg);

        switch (you.uncancel[act].first)
        {
        case UNC_ACQUIREMENT:
            if (!acquirement(OBJ_RANDOM, arg) && crawl_state.seen_hups)
                return;
            break;

        case UNC_DRAW_THREE:
            if (!draw_three(arg) && crawl_state.seen_hups)
                return;
            break;

        case UNC_STACK_FIVE:
            if (!stack_five(arg) && crawl_state.seen_hups)
                return;
            break;

        case UNC_MERCENARY:
            if (!recruit_mercenary(arg) && crawl_state.seen_hups)
                return;
            break;

        case UNC_POTION_PETITION:
            if (!gozag_potion_petition() && crawl_state.seen_hups)
                return;
            break;

        case UNC_CALL_MERCHANT:
            if (!gozag_call_merchant() && crawl_state.seen_hups)
                return;
            break;
        }

        if (act != -1)
            erase_any(you.uncancel, act);
    }
}