File: fearmonger.cc

package info (click to toggle)
crawl 2%3A0.23.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,948 kB
  • sloc: cpp: 303,973; ansic: 28,797; python: 4,074; perl: 3,247; makefile: 1,632; java: 792; sh: 327; objc: 250; xml: 32; cs: 15; sed: 9; lisp: 3
file content (188 lines) | stat: -rw-r--r-- 4,973 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * @file
 * @brief player methods dealing with mesmerisation.
**/

#include "AppHdr.h"

#include "player.h"

#include "areas.h"
#include "coord.h"
#include "env.h"
#include "fprop.h"
#include "monster.h"
#include "mon-util.h"
#include "state.h"

// Add a monster to the list of fearmongers.
bool player::add_fearmonger(const monster* mon)
{
    ASSERT(mon); // XXX: change to const monster &mon
    if (is_sanctuary(pos()))
    {
        if (can_see(*mon))
        {
            mprf("%s's aura of fear is muted, and has no effect on you.",
                 mon->name(DESC_THE).c_str());
        }
        else
            mpr("The fearful aura is strangely muted, and has no effect on you.");

        return false;
    }

    if (!duration[DUR_AFRAID])
    {
        set_duration(DUR_AFRAID, 7, 12);
        fearmongers.push_back(mon->mid);
        mprf(MSGCH_WARN, "You are terrified of %s!",
                         mon->name(DESC_THE).c_str());
    }
    else
    {
        increase_duration(DUR_AFRAID, 5, 12);
        if (!afraid_of(mon))
            fearmongers.push_back(mon->mid);
    }

    return true;
}

// Whether player is afraid.
bool player::afraid() const
{
    ASSERT((duration[DUR_AFRAID] > 0) == !fearmongers.empty());
    return duration[DUR_AFRAID] > 0;
}

// Whether player is afraid of the given monster.
bool player::afraid_of(const monster* mon) const
{
    return find(begin(fearmongers), end(fearmongers), mon->mid)
               != end(fearmongers);
}

// Checks whether a fearmonger keeps you from moving to
// target, and returns one if it exists.
monster* player::get_fearmonger(const coord_def &target) const
{
    for (mid_t monger : fearmongers)
    {
        monster* mon = monster_by_mid(monger);
        // The monster may have died.
        if (!mon)
            continue;
        const int olddist = grid_distance(pos(), mon->pos());
        const int newdist = grid_distance(target, mon->pos());

        if (olddist > newdist)
            return mon;
    }
    return nullptr;
}

monster* player::get_any_fearmonger() const
{
    if (!fearmongers.empty())
        return monster_by_mid(fearmongers[0]);
    else
        return nullptr;
}

// Removes a monster from the list of fearmongers if present.
void player::remove_fearmonger(const monster* mon)
{
    for (unsigned int i = 0; i < fearmongers.size(); i++)
        if (fearmongers[i] == mon->mid)
        {
            fearmongers.erase(fearmongers.begin() + i);
            _removed_fearmonger();
            return;
        }
}

// Clear the list of fearmongers. Doesn't message.
void player::clear_fearmongers()
{
    fearmongers.clear();
    duration[DUR_AFRAID] = 0;
}

static void _removed_fearmonger_msg(const monster* mon)
{
    return;
}

// Update all fearmongers' status after changes.
void player::update_fearmongers()
{
    if (!afraid())
        return;
    bool removed = false;
    for (int i = fearmongers.size() - 1; i >= 0; i--)
    {
        const monster* mon = monster_by_mid(fearmongers[i]);
        if (!_possible_fearmonger(mon))
        {
            fearmongers.erase(fearmongers.begin() + i);
            removed = true;

            // If that was the last one, clear the duration before
            // printing any subsequent messages, or a --more-- can
            // crash (#6547).
            _removed_fearmonger(true);
            _removed_fearmonger_msg(mon);
        }
    }
    if (removed)
        _removed_fearmonger();
}

// Update a single fearmonger.
void player::update_fearmonger(const monster* mon)
{
    if (_possible_fearmonger(mon))
        return;
    for (unsigned int i = 0; i < fearmongers.size(); i++)
        if (fearmongers[i] == mon->mid)
        {
            fearmongers.erase(fearmongers.begin() + i);
            // Do this dance to clear the duration before printing messages
            // (#8844), but still print all messages in the right order.
            _removed_fearmonger(true);
            _removed_fearmonger_msg(mon);
            _removed_fearmonger();
            return;
        }
}

// Helper function that resets the duration and messages if the player
// is no longer afraid.
void player::_removed_fearmonger(bool quiet)
{
    if (fearmongers.empty())
    {
        duration[DUR_AFRAID] = 0;
        if (!quiet)
            mprf(MSGCH_DURATION, "You are no longer terrified.");
    }
}

// Helper function that checks whether the given monster is a possible
// fearmonger.
bool player::_possible_fearmonger(const monster* mon) const
{
    if (crawl_state.game_is_arena())
        return false;

    return mon && mon->alive()
        && !silenced(pos()) && !silenced(mon->pos())
        && see_cell(mon->pos()) && mon->see_cell(pos())
        && !mon->submerged() && !mon->confused()
        && !mon->asleep() && !mon->cannot_move()
        && !mon->wont_attack() && !mon->pacified()
        && !mon->berserk_or_insane()
        && !mons_is_fleeing(*mon)
        && !is_sanctuary(pos());
}