File: attitude-change.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 (493 lines) | stat: -rw-r--r-- 14,290 bytes parent folder | download | duplicates (2)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
 * @file
 * @brief Monster attitude changing due to religion.
**/

#include "AppHdr.h"

#include "attitude-change.h"

#include <sstream>

#include "act-iter.h"
#include "branch.h"
#include "coordit.h"
#include "database.h"
#include "env.h"
#include "god-abil.h"
#include "god-companions.h"
#include "god-passive.h" // passive_t::convert_orcs
#include "libutil.h"
#include "message.h"
#include "mon-behv.h"
#include "mon-death.h"
#include "mon-tentacle.h"
#include "religion.h"
#include "state.h"
#include "travel.h"

// Called whenever an already existing monster changes its attitude, possibly
// temporarily.
void mons_att_changed(monster* mon)
{
    const mon_attitude_type att = mon->temp_attitude();
    const monster_type mc = mons_base_type(*mon);

    if (mons_is_tentacle_head(mc)
        || mons_is_solo_tentacle(mc))
    {
        for (monster_iterator mi; mi; ++mi)
            if (mi->is_child_tentacle_of(mon))
            {
                mi->attitude = att;
                if (!mons_is_solo_tentacle(mc))
                {
                    for (monster_iterator connect; connect; ++connect)
                    {
                        if (connect->is_child_tentacle_of(*mi))
                            connect->attitude = att;
                    }
                }

                // It's almost always flipping between hostile and friendly;
                // enslaving a pacified starspawn is still a shock.
                mi->stop_constricting_all();
            }
    }

    if (mon->attitude == ATT_HOSTILE
        && (mons_is_god_gift(*mon, GOD_BEOGH)
           || mons_is_god_gift(*mon, GOD_YREDELEMNUL)))
    {
        remove_companion(mon);
    }

    mon->remove_summons(true);
}

static void _jiyva_convert_slime(monster* slime);
static void _fedhas_neutralise_plant(monster* plant);

void beogh_follower_convert(monster* mons, bool orc_hit)
{
    if (!species_is_orcish(you.species) || crawl_state.game_is_arena())
        return;

    // For followers of Beogh, decide whether orcs will join you.
    if (will_have_passive(passive_t::convert_orcs)
        && mons_genus(mons->type) == MONS_ORC
        && !mons->is_summoned()
        && !mons->is_shapeshifter()
        && !testbits(mons->flags, MF_ATT_CHANGE_ATTEMPT)
        && !mons->friendly())
    {
        mons->flags |= MF_ATT_CHANGE_ATTEMPT;

        const int hd = mons->get_experience_level();

        if (have_passive(passive_t::convert_orcs)
            && random2(you.piety / 15) + random2(4 + you.experience_level / 3)
                 > random2(hd) + hd + random2(5))
        {
            beogh_convert_orc(mons, orc_hit || !mons->alive() ? conv_t::deathbed
                                                              : conv_t::sight);
            stop_running();
        }
    }
}

void slime_convert(monster* mons)
{
    if (have_passive(passive_t::neutral_slimes) && mons_is_slime(*mons)
        && !mons->neutral()
        && !mons->friendly()
        && !testbits(mons->flags, MF_ATT_CHANGE_ATTEMPT))
    {
        mons->flags |= MF_ATT_CHANGE_ATTEMPT;
        if (!player_under_penance())
        {
            _jiyva_convert_slime(mons);
            stop_running();
        }
    }
}

void fedhas_neutralise(monster* mons)
{
    if (have_passive(passive_t::friendly_plants)
        && mons->attitude == ATT_HOSTILE
        && fedhas_neutralises(*mons)
        && !testbits(mons->flags, MF_ATT_CHANGE_ATTEMPT))
    {
        _fedhas_neutralise_plant(mons);
        mons->flags |= MF_ATT_CHANGE_ATTEMPT;
        del_exclude(mons->pos());
    }
}

// Make summoned (temporary) god gifts disappear on penance or when
// abandoning the god in question.
void make_god_gifts_disappear()
{
    const god_type god =
        (crawl_state.is_god_acting()) ? crawl_state.which_god_acting()
                                      : GOD_NO_GOD;
    for (monster_iterator mi; mi; ++mi)
    {
        if (is_follower(**mi)
            && mi->has_ench(ENCH_ABJ)
            && mons_is_god_gift(**mi, god))
        {
            // The monster disappears.
            monster_die(**mi, KILL_DISMISSED, NON_MONSTER);
        }
    }
}

// When under penance, Yredelemnulites can lose all nearby undead slaves.
bool yred_slaves_abandon_you()
{
    int num_reclaim = 0;
    int num_slaves = 0;

    for (radius_iterator ri(you.pos(), LOS_DEFAULT); ri; ++ri)
    {
        monster* mons = monster_at(*ri);
        if (mons == nullptr)
            continue;

        if (is_yred_undead_slave(*mons))
        {
            num_slaves++;

            const int hd = mons->get_experience_level();

            // During penance, followers get a saving throw.
            if (random2(20) > random2(hd))
                continue;

            mons->attitude = ATT_HOSTILE;
            behaviour_event(mons, ME_ALERT, &you);
            // For now CREATED_FRIENDLY stays.
            mons_att_changed(mons);

            num_reclaim++;
        }
    }

    if (num_reclaim > 0)
    {
        if (num_reclaim == 1 && num_slaves > 1)
            simple_god_message(" reclaims one of your granted undead slaves!");
        else if (num_reclaim == num_slaves)
            simple_god_message(" reclaims your granted undead slaves!");
        else
            simple_god_message(" reclaims some of your granted undead slaves!");
        return true;
    }

    return false;
}

// When under penance, Beoghites can lose all nearby orcish followers,
// subject to a few limitations.
bool beogh_followers_abandon_you()
{
    bool reconvert = false;
    int num_reconvert = 0;
    int num_followers = 0;

    for (radius_iterator ri(you.pos(), LOS_DEFAULT); ri; ++ri)
    {
        monster* mons = monster_at(*ri);
        if (mons == nullptr)
            continue;

        // Note that orc high priests' summons are gifts of Beogh,
        // so we can't use is_orcish_follower(*) here.
        if (mons_is_god_gift(*mons, GOD_BEOGH))
        {
            num_followers++;

            if (you.visible_to(mons)
                && !mons->asleep()
                && !mons_is_confused(*mons)
                && !mons->cannot_act())
            {
                const int hd = mons->get_experience_level();

                // During penance, followers get a saving throw.
                if (random2(20) > random2(hd))
                    continue;

                mons->attitude = ATT_HOSTILE;
                behaviour_event(mons, ME_ALERT, &you);
                // For now CREATED_FRIENDLY stays.
                mons_att_changed(mons);

                if (you.can_see(*mons))
                    num_reconvert++; // Only visible ones.

                reconvert = true;
            }
        }
    }

    if (reconvert) // Maybe all of them are invisible.
    {
        simple_god_message("'s voice booms out, \"Who do you think you "
                           "are?\"", GOD_BEOGH);

        ostream& chan = msg::streams(MSGCH_MONSTER_ENCHANT);

        if (num_reconvert > 0)
        {
            if (num_reconvert == 1 && num_followers > 1)
                chan << "One of your followers decides to abandon you.";
            else if (num_reconvert == num_followers)
                chan << "Your followers decide to abandon you.";
            else
                chan << "Some of your followers decide to abandon you.";
        }

        chan << endl;

        return true;
    }

    return false;
}

static void _print_converted_orc_speech(const string& key,
                                        monster* mon,
                                        msg_channel_type channel)
{
    string msg = getSpeakString("beogh_converted_orc_" + key);

    if (!msg.empty())
    {
        msg = do_mon_str_replacements(msg, *mon);
        strip_channel_prefix(msg, channel);
        mprf(channel, "%s", msg.c_str());
    }
}

// Orcs may turn friendly when encountering followers of Beogh, and be
// made gifts of Beogh.
void beogh_convert_orc(monster* orc, conv_t conv)
{
    ASSERT(orc); // XXX: change to monster &orc
    ASSERT(mons_genus(orc->type) == MONS_ORC);

    switch (conv)
    {
    case conv_t::deathbed_follower:
        _print_converted_orc_speech("reaction_battle_follower", orc,
                                    MSGCH_FRIEND_ENCHANT);
        _print_converted_orc_speech("speech_battle_follower", orc,
                                    MSGCH_TALK);
        break;
    case conv_t::deathbed:
        _print_converted_orc_speech("reaction_battle", orc,
                                    MSGCH_FRIEND_ENCHANT);
        _print_converted_orc_speech("speech_battle", orc, MSGCH_TALK);
        break;
    case conv_t::sight:
        _print_converted_orc_speech("reaction_sight", orc,
                                    MSGCH_FRIEND_ENCHANT);

        if (!one_chance_in(3))
            _print_converted_orc_speech("speech_sight", orc, MSGCH_TALK);
        break;
    case conv_t::resurrection:
        _print_converted_orc_speech("resurrection", orc,
                                    MSGCH_FRIEND_ENCHANT);
        break;
    }

    orc->attitude = ATT_FRIENDLY;

    // The monster is not really *created* friendly, but should it
    // become hostile later on, it won't count as a good kill.
    orc->flags |= MF_NO_REWARD;

    if (orc->is_patrolling())
    {
        // Make orcs stop patrolling and forget their patrol point,
        // they're supposed to follow you now.
        orc->patrol_point = coord_def(0, 0);
    }

    if (!orc->alive())
        orc->hit_points = min(random_range(1, 4), orc->max_hit_points);

    mons_make_god_gift(*orc, GOD_BEOGH);
    add_companion(orc);

    // Avoid immobile "followers".
    behaviour_event(orc, ME_ALERT);

    mons_att_changed(orc);
}

static void _fedhas_neutralise_plant(monster* plant)
{
    if (!plant
        || !fedhas_neutralises(*plant)
        || plant->attitude != ATT_HOSTILE
        || testbits(plant->flags, MF_ATT_CHANGE_ATTEMPT))
    {
        return;
    }

    plant->attitude = ATT_GOOD_NEUTRAL;
    plant->flags   |= MF_WAS_NEUTRAL;
    mons_att_changed(plant);
}

static void _jiyva_convert_slime(monster* slime)
{
    ASSERT(slime); // XXX: change to monster &slime
    ASSERT(mons_is_slime(*slime));

    behaviour_event(slime, ME_ALERT);

    if (you.can_see(*slime))
    {
        if (mons_genus(slime->type) == MONS_FLOATING_EYE)
        {
            mprf(MSGCH_GOD, "%s stares at you suspiciously for a moment, "
                            "then relaxes.",

            slime->name(DESC_THE).c_str());
        }
        else
        {
            mprf(MSGCH_GOD, "%s trembles before you.",
                 slime->name(DESC_THE).c_str());
        }
    }

    slime->attitude = ATT_STRICT_NEUTRAL;
    slime->flags   |= MF_WAS_NEUTRAL;

    mons_make_god_gift(*slime, GOD_JIYVA);

    mons_att_changed(slime);
}

void gozag_set_bribe(monster* traitor)
{
    // Try to bribe the monster.
    const int bribability = gozag_type_bribable(traitor->type);

    if (bribability <= 0 || traitor->friendly() || traitor->is_summoned())
        return;

    const monster* leader =
        traitor->props.exists("band_leader")
        ? monster_by_mid(traitor->props["band_leader"].get_int())
        : nullptr;

    if (leader)
    {
        if (leader->has_ench(ENCH_FRIENDLY_BRIBED)
            || leader->props.exists(FRIENDLY_BRIBE_KEY))
        {
            traitor->props[FRIENDLY_BRIBE_KEY].get_bool() = true;
        }
        else if (leader->has_ench(ENCH_NEUTRAL_BRIBED)
                 || leader->props.exists(NEUTRAL_BRIBE_KEY))
        {
            traitor->props[NEUTRAL_BRIBE_KEY].get_bool() = true;
        }
    }
    else if (x_chance_in_y(bribability, GOZAG_MAX_BRIBABILITY))
    {
        // Sometimes get permanent followers
        if (one_chance_in(3))
            traitor->props[FRIENDLY_BRIBE_KEY].get_bool() = true;
        else
            traitor->props[NEUTRAL_BRIBE_KEY].get_bool() = true;
    }
}

void gozag_check_bribe(monster* traitor)
{
    branch_type branch = gozag_fixup_branch(you.where_are_you);

    if (branch_bribe[branch] == 0)
        return; // Do nothing if branch isn't currently bribed.

    const int base_cost = max(1, exper_value(*traitor) / 20);

    int cost = 0;

    string msg;

    if (traitor->props.exists(FRIENDLY_BRIBE_KEY))
    {
        traitor->props.erase(FRIENDLY_BRIBE_KEY);
        traitor->add_ench(mon_enchant(ENCH_FRIENDLY_BRIBED, 0, 0,
                                      INFINITE_DURATION));
        msg = getSpeakString(traitor->name(DESC_DBNAME, true)
                             + " Gozag permabribe");
        if (msg.empty())
            msg = getSpeakString("Gozag permabribe");

        // Actual allies deduct more gold.
        cost = 2 * base_cost;
    }
    else if (traitor->props.exists(NEUTRAL_BRIBE_KEY))
    {
        traitor->props.erase(NEUTRAL_BRIBE_KEY);
        traitor->add_ench(ENCH_NEUTRAL_BRIBED);
        msg = getSpeakString(traitor->name(DESC_DBNAME, true)
                             + " Gozag bribe");
        if (msg.empty())
            msg = getSpeakString("Gozag bribe");

        cost = base_cost;
    }

    if (!msg.empty())
    {
        msg_channel_type channel = MSGCH_FRIEND_ENCHANT;
        msg = do_mon_str_replacements(msg, *traitor);
        strip_channel_prefix(msg, channel);
        mprf(channel, "%s", msg.c_str());
        // !msg.empty means a monster was bribed.
        gozag_deduct_bribe(branch, cost);
    }
}

void gozag_break_bribe(monster* victim)
{
    ASSERT(victim); // XXX: change to monster &victim

    if (!victim->has_ench(ENCH_NEUTRAL_BRIBED)
        && !victim->has_ench(ENCH_FRIENDLY_BRIBED)
        && !victim->props.exists(NEUTRAL_BRIBE_KEY)
        && !victim->props.exists(FRIENDLY_BRIBE_KEY))
    {
        return;
    }

    // Un-bribe the victim.
    victim->del_ench(ENCH_NEUTRAL_BRIBED);
    victim->del_ench(ENCH_FRIENDLY_BRIBED);
    victim->props.erase(NEUTRAL_BRIBE_KEY);
    victim->props.erase(FRIENDLY_BRIBE_KEY);

    // Make other nearby bribed monsters un-bribed, too.
    for (monster_iterator mi; mi; ++mi)
        if (mi->can_see(*victim))
            gozag_break_bribe(*mi);
}

// Conversions and bribes.
void do_conversions(monster* target)
{
        beogh_follower_convert(target);
        gozag_check_bribe(target);
}