summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/behold.cc
blob: 82928d7a1223699aecaadc4ce03cd2677f30ca6a (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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * File:     behold.cc
 * Summary:  player methods dealing with mesmerisation.
 */

#include "AppHdr.h"

#include "player.h"

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

// Add a monster to the list of beholders.
void player::add_beholder(const monsters* mon)
{
    if (is_sanctuary(you.pos()))
    {
        if (you.can_see(mon))
        {
            mprf("%s's singing sounds muted, and has no effect on you.",
                 mon->name(DESC_CAP_THE).c_str());
        }
        else
        {
            mpr("The melody is strangely muted, and has no effect on you.");
        }
        return;
    }

    if (!duration[DUR_MESMERISED])
    {
        you.set_duration(DUR_MESMERISED, 7, 12);
        beholders.push_back(mon->mindex());
        mprf(MSGCH_WARN, "You are mesmerised by %s!",
                         mon->name(DESC_NOCAP_THE).c_str());
    }
    else
    {
        you.increase_duration(DUR_MESMERISED, 5, 12);
        if (!beheld_by(mon))
            beholders.push_back(mon->mindex());
    }
}

// Whether player is mesmerised.
bool player::beheld() const
{
    ASSERT(duration[DUR_MESMERISED] > 0 == !beholders.empty());
    return (duration[DUR_MESMERISED] > 0);
}

// Whether player is mesmerised by the given monster.
bool player::beheld_by(const monsters* mon) const
{
    for (unsigned int i = 0; i < beholders.size(); i++)
        if (beholders[i] == mon->mindex())
            return (true);
    return (false);
}

// Checks whether a beholder keeps you from moving to
// target, and returns one if it exists.
monsters* player::get_beholder(const coord_def &target) const
{
    for (unsigned int i = 0; i < beholders.size(); i++)
    {
        monsters *mon = &menv[beholders[i]];
        const int olddist = grid_distance(pos(), mon->pos());
        const int newdist = grid_distance(target, mon->pos());

        if (olddist < newdist)
            return (mon);
    }
    return (NULL);
}

monsters* player::get_any_beholder() const
{
    if (beholders.size() > 0)
        return (&menv[beholders[0]]);
    else
        return (NULL);
}

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

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

// Possibly end mesmerisation if a loud noise happened.
void player::beholders_check_noise(int loudness)
{
    if (loudness >= 20 && beheld())
    {
        mprf("For a moment, you cannot hear the mermaid%s!",
             beholders.size() > 1 ? "s" : "");
        clear_beholders();
        _removed_beholder();
    }
}

static void _removed_beholder_msg(const monsters* mon)
{
    if (!mon->alive() || mons_genus(mon->type) != MONS_MERMAID
        || mon->submerged() || !you.see_cell(mon->pos()))
    {
        return;
    }

    if (is_sanctuary(you.pos()) && !mons_is_fleeing(mon))
    {
        if (you.can_see(mon))
        {
            mprf("%s's singing becomes strangely muted.",
                 mon->name(DESC_CAP_THE).c_str());
        }
        else
            mpr("Something's singing becomes strangely muted.");

        return;
    }

    if (you.can_see(mon))
    {
        if (silenced(you.pos()) || silenced(mon->pos()))
        {
            mprf("You can no longer hear %s's singing!",
                 mon->name(DESC_NOCAP_THE).c_str());
            return;
        }
        mprf("%s stops singing.", mon->name(DESC_CAP_THE).c_str());
        return;
    }

    mpr("Something stops singing.");
}

// Update all beholders' status after changes.
void player::update_beholders()
{
    if (!beheld())
        return;
    bool removed = false;
    for (int i = beholders.size() - 1; i >= 0; i--)
    {
        const monsters* mon = &menv[beholders[i]];
        if (!_possible_beholder(mon))
        {
            beholders.erase(beholders.begin() + i);
            removed = true;
            _removed_beholder_msg(mon);
        }
    }
    if (removed)
        _removed_beholder();
}

// Update a single beholder.
void player::update_beholder(const monsters *mon)
{
    if (_possible_beholder(mon))
        return;
    for (unsigned int i = 0; i < beholders.size(); i++)
        if (beholders[i] == mon->mindex())
        {
            beholders.erase(beholders.begin() + i);
            _removed_beholder_msg(mon);
            _removed_beholder();
            return;
        }
}

// Helper function that resets the duration and messages if the player
// is no longer mesmerised.
void player::_removed_beholder()
{
    if (beholders.empty())
    {
        duration[DUR_MESMERISED] = 0;
        mpr(coinflip() ? "You break out of your daze!"
                       : "You are no longer entranced.",
            MSGCH_DURATION);
    }
}

// Helper function that checks whether the given monster is a possible
// beholder.
bool player::_possible_beholder(const monsters *mon) const
{
    if (crawl_state.arena)
        return (false);

    return (!silenced(pos()) && !silenced(mon->pos())
         && see_cell(mon->pos()) && mon->see_cell(pos())
         && mon->alive() && mons_genus(mon->type) == MONS_MERMAID
         && !mon->submerged() && !mon->confused()
         && !mon->asleep() && !mon->cannot_move()
         && !mon->wont_attack() && !mon->pacified()
         && !mon->berserk() && !mons_is_fleeing(mon)
         && !is_sanctuary(you.pos()));
}