1 /**
2 * @file
3 * @brief Player ghost and random Pandemonium demon handling.
4 **/
5
6 #include "AppHdr.h"
7
8 #include "ghost.h"
9
10 #include <vector>
11
12 #include "act-iter.h"
13 #include "colour.h"
14 #include "database.h"
15 #include "enchant-type.h"
16 #include "env.h"
17 #include "god-type.h"
18 #include "item-name.h"
19 #include "item-prop.h"
20 #include "jobs.h"
21 #include "mon-cast.h"
22 #include "mon-transit.h"
23 #include "mpr.h"
24 #include "ng-input.h"
25 #include "skills.h"
26 #include "spl-util.h"
27 #include "state.h"
28 #include "stringutil.h"
29 #include "tag-version.h"
30 #include "unwind.h"
31
32 #define MAX_GHOST_DAMAGE 50
33 #define MAX_GHOST_HP 400
34 #define MAX_GHOST_EVASION 60
35
36 // Pan lord AOE conjuration spell list.
37 static spell_type search_order_aoe_conj[] =
38 {
39 SPELL_SYMBOL_OF_TORMENT,
40 SPELL_FIRE_STORM,
41 SPELL_GLACIATE,
42 SPELL_CHAIN_LIGHTNING,
43 SPELL_FREEZING_CLOUD,
44 SPELL_POISONOUS_CLOUD,
45 SPELL_METAL_SPLINTERS,
46 SPELL_ENERGY_BOLT,
47 SPELL_ORB_OF_ELECTRICITY,
48 SPELL_CONJURE_BALL_LIGHTNING,
49 };
50
51 // Pan lord conjuration spell list.
52 static spell_type search_order_conj[] =
53 {
54 SPELL_CALL_DOWN_DAMNATION,
55 SPELL_LEHUDIBS_CRYSTAL_SPEAR,
56 SPELL_CORROSIVE_BOLT,
57 SPELL_QUICKSILVER_BOLT,
58 SPELL_IOOD,
59 SPELL_ENERGY_BOLT,
60 SPELL_MINDBURST,
61 SPELL_BOLT_OF_FIRE,
62 SPELL_BOLT_OF_COLD,
63 SPELL_IRON_SHOT,
64 SPELL_POISON_ARROW,
65 SPELL_BOLT_OF_DRAINING,
66 SPELL_LIGHTNING_BOLT,
67 };
68
69 // Pan lord self-enchantment spell list.
70 static spell_type search_order_selfench[] =
71 {
72 SPELL_HASTE,
73 SPELL_SILENCE,
74 SPELL_INVISIBILITY,
75 SPELL_BLINK,
76 SPELL_BLINK_RANGE,
77 };
78
79 // Pan lord summoning spell list.
80 static spell_type search_order_summon[] =
81 {
82 SPELL_HAUNT,
83 SPELL_MALIGN_GATEWAY,
84 SPELL_SUMMON_DRAGON,
85 SPELL_SUMMON_HORRIBLE_THINGS,
86 SPELL_SUMMON_EYEBALLS,
87 SPELL_SUMMON_VERMIN, // funny
88 };
89
90 // Pan lord misc spell list.
91 static spell_type search_order_misc[] =
92 {
93 SPELL_DISPEL_UNDEAD_RANGE,
94 SPELL_PARALYSE,
95 SPELL_SLEEP,
96 SPELL_MASS_CONFUSION,
97 SPELL_DRAIN_MAGIC,
98 SPELL_PETRIFY,
99 SPELL_POLYMORPH,
100 SPELL_FORCE_LANCE,
101 SPELL_SLOW,
102 SPELL_SENTINEL_MARK,
103 SPELL_DIMENSION_ANCHOR,
104 };
105
106 /**
107 * A small set of spells that pan lords without (other) spells can make use of.
108 * All related to closing the gap with the player, or messing with their
109 * movement.
110 */
111 static spell_type search_order_non_spellcaster[] =
112 {
113 SPELL_BLINKBOLT,
114 SPELL_BLINK_CLOSE,
115 SPELL_HARPOON_SHOT,
116 };
117
ghost_demon()118 ghost_demon::ghost_demon()
119 {
120 reset();
121 }
122
reset()123 void ghost_demon::reset()
124 {
125 name.clear();
126 species = SP_UNKNOWN;
127 job = JOB_UNKNOWN;
128 religion = GOD_NO_GOD;
129 best_skill = SK_FIGHTING;
130 best_skill_level = 0;
131 xl = 0;
132 max_hp = 0;
133 ev = 0;
134 ac = 0;
135 damage = 0;
136 speed = 10;
137 move_energy = 10;
138 see_invis = false;
139 brand = SPWPN_NORMAL;
140 att_type = AT_HIT;
141 att_flav = AF_PLAIN;
142 resists = 0;
143 colour = COLOUR_UNDEF;
144 flies = false;
145 cloud_ring_ench = ENCH_NONE;
146 }
147
148 #define ADD_SPELL(which_spell) \
149 do { \
150 const auto spell = (which_spell); \
151 if (spell != SPELL_NO_SPELL) \
152 spells.emplace_back(spell, 0, MON_SPELL_MAGICAL); \
153 } while (0)
154
_panlord_random_resist_level()155 static int _panlord_random_resist_level()
156 {
157 return random_choose_weighted(1, -1,
158 3, 0,
159 3, 1,
160 2, 2,
161 1, 3);
162 }
163
_panlord_random_elec_resist_level()164 static int _panlord_random_elec_resist_level()
165 {
166 return random_choose_weighted(3, 0,
167 6, 1,
168 1, 3);
169 }
170
171 /**
172 * Generate a random attack_type for a pandemonium_lord. Since this is purely
173 * flavour, special attack types are rare.
174 */
_pan_lord_random_attack_type()175 static attack_type _pan_lord_random_attack_type()
176 {
177 attack_type attack = AT_HIT;
178 if (one_chance_in(4))
179 {
180 do
181 {
182 attack = static_cast<attack_type>(random_range(AT_FIRST_ATTACK, AT_LAST_REAL_ATTACK));
183 }
184 while (attack == AT_HIT || !is_plain_attack_type(attack));
185 }
186 return attack;
187 }
188
189 // this is a bit like a union, but not really
190 struct attack_form
191 {
192 brand_type brand = SPWPN_NORMAL;
193 attack_flavour flavour = AF_PLAIN;
194 };
195
_brand_attack(brand_type brand)196 static attack_form _brand_attack(brand_type brand)
197 {
198 attack_form form;
199 form.brand = brand;
200 return form;
201 }
202
_flavour_attack(attack_flavour flavour)203 static attack_form _flavour_attack(attack_flavour flavour)
204 {
205 attack_form form;
206 form.flavour = flavour;
207 return form;
208 }
209
210 /**
211 * Set a random attack type for a pandemonium lord's melee attacks. Some of
212 * these are branded attacks, some are custom attack flavours, some are matching
213 * attack types & flavours. (Pan lord attack type is randomised, but can be
214 * overridden here if required.)
215 */
set_pan_lord_special_attack()216 void ghost_demon::set_pan_lord_special_attack()
217 {
218 const attack_form form = random_choose_weighted(
219 // Low chance
220 10, _brand_attack(SPWPN_VENOM),
221 10, _brand_attack(SPWPN_DRAINING),
222 4, _flavour_attack(AF_DRAIN_STR),
223 4, _flavour_attack(AF_DRAIN_INT),
224 2, _flavour_attack(AF_DRAIN_DEX),
225 10, _flavour_attack(AF_DROWN),
226 // Normal chance
227 20, _brand_attack(SPWPN_FLAMING),
228 20, _brand_attack(SPWPN_FREEZING),
229 20, _brand_attack(SPWPN_ELECTROCUTION),
230 20, _brand_attack(SPWPN_VAMPIRISM),
231 20, _brand_attack(SPWPN_PAIN),
232 20, _flavour_attack(AF_ENSNARE),
233 20, _flavour_attack(AF_DRAIN_SPEED),
234 20, _flavour_attack(AF_CORRODE),
235 20, _flavour_attack(AF_WEAKNESS),
236 // High chance
237 40, _brand_attack(SPWPN_ANTIMAGIC),
238 40, _brand_attack(SPWPN_DISTORTION),
239 40, _brand_attack(SPWPN_CHAOS),
240 40, _flavour_attack(AF_TRAMPLE)
241 );
242
243 brand = form.brand;
244 if (form.flavour != AF_PLAIN)
245 att_flav = form.flavour;
246
247 if (brand == SPWPN_VENOM && coinflip())
248 att_type = AT_STING; // such flavour!
249 switch (att_flav)
250 {
251 case AF_TRAMPLE:
252 att_type = AT_TRAMPLE;
253 break;
254 case AF_DROWN:
255 att_type = AT_ENGULF;
256 break;
257 default:
258 break;
259 }
260 }
261
set_pan_lord_cloud_ring()262 void ghost_demon::set_pan_lord_cloud_ring()
263 {
264 if (brand == SPWPN_ELECTROCUTION)
265 cloud_ring_ench = ENCH_RING_OF_THUNDER;
266 else if (brand == SPWPN_FLAMING)
267 cloud_ring_ench = ENCH_RING_OF_FLAMES;
268 else if (brand == SPWPN_CHAOS)
269 cloud_ring_ench = ENCH_RING_OF_CHAOS;
270 else if (brand == SPWPN_FREEZING)
271 cloud_ring_ench = ENCH_RING_OF_ICE;
272 else if (att_flav == AF_CORRODE)
273 cloud_ring_ench = ENCH_RING_OF_ACID;
274 else if (brand == SPWPN_DRAINING)
275 cloud_ring_ench = ENCH_RING_OF_DRAINING;
276 else
277 {
278 cloud_ring_ench = random_choose_weighted(
279 20, ENCH_RING_OF_THUNDER,
280 20, ENCH_RING_OF_FLAMES,
281 20, ENCH_RING_OF_ICE,
282 10, ENCH_RING_OF_DRAINING,
283 5, ENCH_RING_OF_CHAOS,
284 5, ENCH_RING_OF_ACID,
285 5, ENCH_RING_OF_MIASMA,
286 5, ENCH_RING_OF_MUTATION);
287 }
288 dprf("This pan lord has a cloud ring ench of %d", cloud_ring_ench);
289 }
290
init_pandemonium_lord()291 void ghost_demon::init_pandemonium_lord()
292 {
293 do
294 {
295 name = make_name();
296 }
297 while (!getLongDescription(name).empty());
298
299 // Is demon a spellcaster?
300 // Non-spellcasters always have branded melee and faster/tougher.
301 const bool spellcaster = x_chance_in_y(3,4);
302
303 max_hp = 100 + roll_dice(3, 50);
304
305 // Panlord AC/EV should tend to be weighted towards one or the other.
306 int total_def = 10 + random2avg(40, 3);
307 int split = 1 + biased_random2(4, 2);
308 ac = div_rand_round(total_def * split, 10);
309 ev = total_def - ac;
310 if (coinflip())
311 swap(ac, ev);
312
313 see_invis = true;
314
315 resists = 0;
316 resists |= mrd(MR_RES_FIRE, _panlord_random_resist_level());
317 resists |= mrd(MR_RES_COLD, _panlord_random_resist_level());
318 resists |= mrd(MR_RES_ELEC, _panlord_random_elec_resist_level());
319 // Demons, like ghosts, automatically get poison res. and life prot.
320
321 // HTH damage:
322 damage = 20 + roll_dice(2, 20);
323
324 // Does demon fly?
325 flies = x_chance_in_y(2, 3);
326
327 // hit dice:
328 xl = 10 + roll_dice(2, 10);
329
330 // Non-spellcasters get upgrades to HD, HP, AC, EV and damage
331 if (!spellcaster)
332 {
333 max_hp = max_hp * 3 / 2;
334 ac += 5;
335 ev += 5;
336 damage += 10;
337 xl += 5;
338 }
339
340 att_type = _pan_lord_random_attack_type();
341 if (one_chance_in(3) || !spellcaster)
342 set_pan_lord_special_attack();
343
344 // Spellcasters get a (smaller) chance of a cloud ring below
345 if (!spellcaster && one_chance_in(7))
346 set_pan_lord_cloud_ring();
347
348 // Non-casters are fast, casters may get haste.
349 if (!spellcaster)
350 speed = 11 + roll_dice(2,4);
351 else if (one_chance_in(3))
352 speed = 10;
353 else
354 speed = 8 + roll_dice(2,5);
355
356 spells.clear();
357
358 if (spellcaster)
359 {
360 if (!one_chance_in(10))
361 ADD_SPELL(RANDOM_ELEMENT(search_order_conj));
362
363 if (!one_chance_in(10))
364 {
365 if (coinflip())
366 {
367 // Demon-summoning should be fairly common.
368 if (coinflip())
369 {
370 ADD_SPELL(random_choose(SPELL_SUMMON_DEMON,
371 SPELL_SUMMON_GREATER_DEMON));
372 }
373 else
374 {
375 ADD_SPELL(RANDOM_ELEMENT(search_order_summon));
376 if (coinflip())
377 ADD_SPELL(SPELL_BLINK_ALLIES_ENCIRCLE);
378 }
379 }
380 else
381 {
382 ADD_SPELL(RANDOM_ELEMENT(search_order_aoe_conj));
383 if (one_chance_in(7))
384 set_pan_lord_cloud_ring();
385 }
386 }
387
388 if (coinflip())
389 ADD_SPELL(RANDOM_ELEMENT(search_order_selfench));
390
391 if (coinflip())
392 ADD_SPELL(RANDOM_ELEMENT(search_order_misc));
393 }
394 else
395 {
396 // Non-spellcasters may get one spell
397 if (one_chance_in(3))
398 ADD_SPELL(RANDOM_ELEMENT(search_order_non_spellcaster));
399 }
400
401 if (!spells.empty())
402 {
403 normalize_spell_freq(spells, spellcaster ? spell_freq_for_hd(xl)
404 : spell_freq_for_hd(xl) / 3);
405 }
406
407 colour = one_chance_in(10) ? colour_t{ETC_RANDOM} : random_monster_colour();
408 }
409
410 static const set<brand_type> ghost_banned_brands =
411 { SPWPN_HOLY_WRATH, SPWPN_CHAOS };
412
init_player_ghost()413 void ghost_demon::init_player_ghost()
414 {
415 // don't preserve transformations for ghosty purposes
416 unwind_var<transformation> form(you.form, transformation::none);
417 unwind_var<FixedBitVector<NUM_EQUIP>> melded(you.melded,
418 FixedBitVector<NUM_EQUIP>());
419 unwind_var<bool> fishtail(you.fishtail, false);
420
421 name = you.your_name;
422 max_hp = min(get_real_hp(false, false), MAX_GHOST_HP);
423 ev = min(you.evasion(ev_ignore::helpless), MAX_GHOST_EVASION);
424 ac = you.armour_class();
425 dprf("ghost ac: %d, ev: %d", ac, ev);
426
427 see_invis = you.can_see_invisible();
428 resists = 0;
429 set_resist(resists, MR_RES_FIRE, player_res_fire());
430 set_resist(resists, MR_RES_COLD, player_res_cold());
431 set_resist(resists, MR_RES_ELEC, player_res_electricity());
432 // clones might lack innate rPois, copy it. pghosts don't care.
433 set_resist(resists, MR_RES_POISON, player_res_poison());
434 set_resist(resists, MR_RES_NEG, you.res_negative_energy());
435 set_resist(resists, MR_RES_ACID, player_res_acid());
436 // multi-level for players, boolean as an innate monster resistance
437 set_resist(resists, MR_RES_STEAM, player_res_steam() ? 1 : 0);
438 set_resist(resists, MR_RES_STICKY_FLAME, player_res_sticky_flame());
439 set_resist(resists, MR_RES_MIASMA, you.res_miasma());
440 set_resist(resists, MR_RES_PETRIFY, you.res_petrify());
441
442 move_energy = 10;
443 speed = 10;
444
445 damage = 4;
446 brand = SPWPN_NORMAL;
447
448 if (you.weapon())
449 {
450 // This includes ranged weapons, but they're treated as melee.
451
452 const item_def& weapon = *you.weapon();
453 if (is_weapon(weapon))
454 {
455 damage = property(weapon, PWPN_DAMAGE);
456
457 // Bows skill doesn't make bow-bashing better.
458 skill_type sk = is_range_weapon(weapon) ? SK_FIGHTING
459 : item_attack_skill(weapon);
460 damage *= 25 + you.skills[sk];
461 damage /= 25;
462
463 if (weapon.base_type == OBJ_WEAPONS)
464 {
465 brand = static_cast<brand_type>(get_weapon_brand(weapon));
466
467 // normalize banned weapon brands
468 if (ghost_banned_brands.count(brand) > 0)
469 brand = SPWPN_NORMAL;
470
471 // Don't copy ranged- or artefact-only brands (reaping etc.).
472 if (brand > MAX_GHOST_BRAND)
473 brand = SPWPN_NORMAL;
474 }
475 else if (weapon.base_type == OBJ_STAVES)
476 {
477 switch (static_cast<stave_type>(weapon.sub_type))
478 {
479 // very bad approximations
480 case STAFF_FIRE: brand = SPWPN_FLAMING; break;
481 case STAFF_COLD: brand = SPWPN_FREEZING; break;
482 case STAFF_POISON: brand = SPWPN_VENOM; break;
483 case STAFF_DEATH: brand = SPWPN_PAIN; break;
484 case STAFF_AIR: brand = SPWPN_ELECTROCUTION; break;
485 case STAFF_EARTH: brand = SPWPN_VORPAL; break;
486 default: ;
487 }
488 }
489 }
490 }
491 else
492 {
493 // Unarmed combat.
494 if (you.has_innate_mutation(MUT_CLAWS))
495 damage += you.experience_level;
496
497 damage += you.skills[SK_UNARMED_COMBAT];
498 }
499
500 damage *= 30 + you.skills[SK_FIGHTING];
501 damage /= 30;
502
503 damage += you.strength() / 4;
504
505 if (damage > MAX_GHOST_DAMAGE)
506 damage = MAX_GHOST_DAMAGE;
507
508 species = you.species;
509 job = you.char_class;
510
511 religion = you.religion;
512
513 best_skill = ::best_skill(SK_FIRST_SKILL, SK_LAST_SKILL);
514 best_skill_level = you.skills[best_skill];
515 xl = you.experience_level;
516
517 flies = true;
518
519 add_spells();
520 }
521
_ugly_thing_assign_colour(colour_t force_colour,colour_t force_not_colour)522 static colour_t _ugly_thing_assign_colour(colour_t force_colour,
523 colour_t force_not_colour)
524 {
525 colour_t colour;
526
527 if (force_colour != COLOUR_UNDEF)
528 colour = force_colour;
529 else
530 {
531 do
532 {
533 colour = ugly_thing_random_colour();
534 }
535 while (force_not_colour != COLOUR_UNDEF && colour == force_not_colour);
536 }
537
538 return colour;
539 }
540
_very_ugly_thing_flavour_upgrade(attack_flavour u_att_flav)541 static attack_flavour _very_ugly_thing_flavour_upgrade(attack_flavour u_att_flav)
542 {
543 switch (u_att_flav)
544 {
545 case AF_FIRE:
546 u_att_flav = AF_STICKY_FLAME;
547 break;
548
549 case AF_POISON:
550 u_att_flav = AF_POISON_STRONG;
551 break;
552
553 default:
554 break;
555 }
556
557 return u_att_flav;
558 }
559
_ugly_thing_colour_to_flavour(colour_t u_colour)560 static attack_flavour _ugly_thing_colour_to_flavour(colour_t u_colour)
561 {
562 attack_flavour u_att_flav = AF_PLAIN;
563
564 switch (make_low_colour(u_colour))
565 {
566 case RED:
567 u_att_flav = AF_FIRE;
568 break;
569
570 case BROWN:
571 u_att_flav = AF_ACID;
572 break;
573
574 case GREEN:
575 u_att_flav = AF_POISON;
576 break;
577
578 case CYAN:
579 u_att_flav = AF_ELEC;
580 break;
581
582 case LIGHTGREY:
583 u_att_flav = AF_COLD;
584 break;
585
586 default:
587 break;
588 }
589
590 if (is_high_colour(u_colour))
591 u_att_flav = _very_ugly_thing_flavour_upgrade(u_att_flav);
592
593 return u_att_flav;
594 }
595
596 /**
597 * Init a ghost demon object corresponding to an ugly thing monster.
598 *
599 * @param very_ugly Whether the ugly thing is a very ugly thing.
600 * @param only_mutate Whether to mutate the ugly thing's colour away from its
601 * old colour (the force_colour).
602 * @param force_colour The ugly thing's colour. (Default COLOUR_UNDEF = random)
603 */
init_ugly_thing(bool very_ugly,bool only_mutate,colour_t force_colour)604 void ghost_demon::init_ugly_thing(bool very_ugly, bool only_mutate,
605 colour_t force_colour)
606 {
607 const monster_type type = very_ugly ? MONS_VERY_UGLY_THING
608 : MONS_UGLY_THING;
609 const monsterentry* stats = get_monster_data(type);
610
611 speed = stats->speed;
612 ev = stats->ev;
613 ac = stats->AC;
614 damage = stats->attack[0].damage;
615 move_energy = stats->energy_usage.move;
616
617 // If we're mutating an ugly thing, leave its experience level, hit
618 // dice and maximum hit points as they are.
619 if (!only_mutate)
620 {
621 xl = stats->HD;
622 max_hp = hit_points(stats->avg_hp_10x);
623 }
624
625 const attack_type att_types[] =
626 {
627 AT_BITE, AT_STING, AT_ENGULF, AT_CLAW, AT_PECK, AT_HEADBUTT, AT_PUNCH,
628 AT_KICK, AT_TENTACLE_SLAP, AT_TAIL_SLAP, AT_GORE, AT_TRUNK_SLAP
629 };
630
631 att_type = RANDOM_ELEMENT(att_types);
632
633 // An ugly thing always gets a low-intensity colour. If we're
634 // mutating it, it always gets a different colour from what it had
635 // before.
636 colour = _ugly_thing_assign_colour(make_low_colour(force_colour),
637 only_mutate ? make_low_colour(colour)
638 : colour_t{COLOUR_UNDEF});
639
640 // Pick a compatible attack flavour for this colour.
641 att_flav = _ugly_thing_colour_to_flavour(colour);
642 if (colour == MAGENTA)
643 damage = damage * 4 / 3; // +5 for uglies, +9 for v uglies
644
645 // Pick a compatible resistance for this attack flavour.
646 ugly_thing_add_resistance(false, att_flav);
647
648 // If this is a very ugly thing, upgrade it properly.
649 if (very_ugly)
650 ugly_thing_to_very_ugly_thing();
651 }
652
ugly_thing_to_very_ugly_thing()653 void ghost_demon::ugly_thing_to_very_ugly_thing()
654 {
655 // A very ugly thing always gets a high-intensity colour.
656 colour = make_high_colour(colour);
657
658 // A very ugly thing sometimes gets an upgraded attack flavour.
659 att_flav = _very_ugly_thing_flavour_upgrade(att_flav);
660
661 // Pick a compatible resistance for this attack flavour.
662 ugly_thing_add_resistance(true, att_flav);
663 }
664
_ugly_thing_resists(bool very_ugly,attack_flavour u_att_flav)665 static resists_t _ugly_thing_resists(bool very_ugly, attack_flavour u_att_flav)
666 {
667 switch (u_att_flav)
668 {
669 case AF_FIRE:
670 case AF_STICKY_FLAME:
671 return MR_RES_FIRE * (very_ugly ? 2 : 1) | MR_RES_STICKY_FLAME;
672
673 case AF_ACID:
674 return MR_RES_ACID;
675
676 case AF_POISON:
677 case AF_POISON_STRONG:
678 return MR_RES_POISON * (very_ugly ? 2 : 1);
679
680 case AF_ELEC:
681 return MR_RES_ELEC * (very_ugly ? 2 : 1);
682
683 case AF_COLD:
684 return MR_RES_COLD * (very_ugly ? 2 : 1);
685
686 default:
687 return 0;
688 }
689 }
690
ugly_thing_add_resistance(bool very_ugly,attack_flavour u_att_flav)691 void ghost_demon::ugly_thing_add_resistance(bool very_ugly,
692 attack_flavour u_att_flav)
693 {
694 resists = _ugly_thing_resists(very_ugly, u_att_flav);
695 }
696
init_dancing_weapon(const item_def & weapon,int power)697 void ghost_demon::init_dancing_weapon(const item_def& weapon, int power)
698 {
699 int delay = property(weapon, PWPN_SPEED);
700 int damg = property(weapon, PWPN_DAMAGE);
701
702 if (power > 100)
703 power = 100;
704
705 colour = weapon.get_colour();
706 flies = true;
707
708 // We want Tukima to reward characters who invest heavily in
709 // Hexes skill. Therefore, weapons benefit from very high skill.
710
711 // First set up what the monsters will look like with 100 power.
712 // Daggers are weak here! In the table, "44+22" means d44+d22 with
713 // d22 being base damage and d44 coming from power.
714 // Giant spiked club: speed 12, 44+22 damage, 22 AC, 36 HP, 16 EV
715 // Bardiche: speed 10, 40+20 damage, 18 AC, 40 HP, 15 EV
716 // Dagger: speed 20, 8+ 4 damage, 4 AC, 20 HP, 20 EV
717 // Quick blade: speed 23, 10+ 5 damage, 5 AC, 14 HP, 22 EV
718 // Rapier: speed 18, 14+ 7 damage, 7 AC, 24 HP, 19 EV
719
720 xl = 15;
721
722 speed = 30 - delay;
723 ev = 25 - delay / 2;
724 ac = damg;
725 damage = 2 * damg;
726 max_hp = delay * 2;
727
728 // Don't allow the speed to become too low.
729 speed = max(3, (speed / 2) * (1 + power / 100));
730
731 ev = max(3, ev * power / 100);
732 ac = ac * power / 100;
733 max_hp = max(5, max_hp * power / 100);
734 damage = max(1, damage * power / 100);
735 }
736
init_spectral_weapon(const item_def & weapon)737 void ghost_demon::init_spectral_weapon(const item_def& weapon)
738 {
739 colour = weapon.get_colour();
740 flies = true;
741 xl = 15;
742 damage = property(weapon, PWPN_DAMAGE) * 4 / 3;
743 speed = 30;
744 ev = 15;
745 ac = 7;
746 max_hp = random_range(20, 30);
747 }
748
749 // Used when creating ghosts: goes through and finds spells for the
750 // ghost to cast. Death is a traumatic experience, so ghosts only
751 // remember a few spells.
add_spells()752 void ghost_demon::add_spells()
753 {
754 spells.clear();
755
756 for (int i = 0; i < you.spell_no; i++)
757 {
758 const int chance = max(0, 50 - failure_rate_to_int(raw_spell_fail(you.spells[i])));
759 const spell_type spell = translate_spell(you.spells[i]);
760 if (spell != SPELL_NO_SPELL
761 && !(get_spell_flags(spell) & spflag::no_ghost)
762 && is_valid_mon_spell(spell)
763 && x_chance_in_y(chance*chance, 50*50))
764 {
765 spells.emplace_back(spell, 0, MON_SPELL_WIZARD);
766 }
767 }
768
769 normalize_spell_freq(spells, spell_freq_for_hd(xl));
770 }
771
has_spells() const772 bool ghost_demon::has_spells() const
773 {
774 return spells.size() > 0;
775 }
776
777 // When passed the number for a player spell, returns the equivalent
778 // monster spell. Returns SPELL_NO_SPELL on failure (no equivalent).
translate_spell(spell_type spell) const779 spell_type ghost_demon::translate_spell(spell_type spell) const
780 {
781 switch (spell)
782 {
783 #if TAG_MAJOR_VERSION == 34
784 case SPELL_CONTROLLED_BLINK:
785 return SPELL_BLINK; // approximate
786 #endif
787 case SPELL_DRAGON_CALL:
788 return SPELL_SUMMON_DRAGON;
789 case SPELL_SWIFTNESS:
790 return SPELL_SPRINT;
791 default:
792 break;
793 }
794
795 return spell;
796 }
797
find_ghosts(bool include_player)798 const vector<ghost_demon> ghost_demon::find_ghosts(bool include_player)
799 {
800 vector<ghost_demon> gs;
801
802 if (include_player && you.undead_state(false) == US_ALIVE)
803 {
804 ghost_demon player;
805 player.init_player_ghost();
806 announce_ghost(player);
807 gs.push_back(player);
808 }
809
810 // Pick up any other ghosts that happen to be on the level if we
811 // have space. If the player is undead, add one to the ghost quota
812 // for the level.
813 find_extra_ghosts(gs);
814
815 return gs;
816 }
817
find_transiting_ghosts(vector<ghost_demon> & gs)818 void ghost_demon::find_transiting_ghosts(
819 vector<ghost_demon> &gs)
820 {
821 const m_transit_list *mt = get_transit_list(level_id::current());
822 if (mt)
823 {
824 for (auto i = mt->begin(); i != mt->end(); ++i)
825 {
826 if (i->mons.type == MONS_PLAYER_GHOST)
827 {
828 const monster& m = i->mons;
829 if (m.ghost && !m.props.exists(MIRRORED_GHOST_KEY))
830 {
831 announce_ghost(*m.ghost);
832 gs.push_back(*m.ghost);
833 }
834 }
835 }
836 }
837 }
838
announce_ghost(const ghost_demon & g)839 void ghost_demon::announce_ghost(const ghost_demon &g)
840 {
841 UNUSED(g);
842 #if defined(DEBUG_BONES) || defined(DEBUG_DIAGNOSTICS)
843 mprf(MSGCH_DIAGNOSTICS, "Saving ghost: %s", g.name.c_str());
844 #endif
845 }
846
find_extra_ghosts(vector<ghost_demon> & gs)847 void ghost_demon::find_extra_ghosts(vector<ghost_demon> &gs)
848 {
849 for (monster_iterator mi; mi; ++mi)
850 {
851 if (mi->type == MONS_PLAYER_GHOST
852 && mi->ghost
853 && !mi->props.exists(MIRRORED_GHOST_KEY))
854 {
855 // Bingo!
856 announce_ghost(*(mi->ghost));
857 gs.push_back(*(mi->ghost));
858 }
859 }
860
861 // Check the transit list for the current level.
862 find_transiting_ghosts(gs);
863 }
864
865 static const set<branch_type> ghosts_nosave =
866 { BRANCH_ABYSS, BRANCH_WIZLAB, BRANCH_DESOLATION, BRANCH_TEMPLE,
867 #if TAG_MAJOR_VERSION == 34
868 BRANCH_LABYRINTH,
869 #endif
870 };
871
872
873 /// Is the current location eligible for saving ghosts?
ghost_eligible()874 bool ghost_demon::ghost_eligible()
875 {
876 return !crawl_state.game_is_tutorial()
877 && (!player_in_branch(BRANCH_DUNGEON) || you.depth > 2)
878 && ghosts_nosave.count(you.where_are_you) == 0;
879 }
880
debug_check_ghost(const ghost_demon & ghost)881 bool debug_check_ghost(const ghost_demon &ghost)
882 {
883 // Values greater than the allowed maximum or less then the
884 // allowed minimum signalise bugginess.
885 if (ghost.damage < 0 || ghost.damage > MAX_GHOST_DAMAGE)
886 return false;
887 if (ghost.max_hp < 1 || ghost.max_hp > MAX_GHOST_HP)
888 return false;
889 if (ghost.xl < 1 || ghost.xl > 27)
890 return false;
891 if (ghost.ev > MAX_GHOST_EVASION)
892 return false;
893 if (get_resist(ghost.resists, MR_RES_ELEC) < 0)
894 return false;
895 if (ghost.brand < SPWPN_NORMAL || ghost.brand > MAX_GHOST_BRAND)
896 return false;
897 if (!species::is_valid(ghost.species))
898 return false;
899 if (!job_type_valid(ghost.job))
900 return false;
901 if (ghost.best_skill < SK_FIGHTING || ghost.best_skill >= NUM_SKILLS)
902 return false;
903 if (ghost.best_skill_level < 0 || ghost.best_skill_level > 27)
904 return false;
905 if (ghost.religion < GOD_NO_GOD || ghost.religion >= NUM_GODS)
906 return false;
907
908 if (ghost.brand == SPWPN_HOLY_WRATH)
909 return false;
910
911 // Ghosts don't get non-plain attack types and flavours.
912 if (ghost.att_type != AT_HIT || ghost.att_flav != AF_PLAIN)
913 return false;
914
915 // Name validation.
916 if (!validate_player_name(ghost.name))
917 return false;
918 // Many combining characters can come per every letter, but if there's
919 // that much, it's probably a maliciously forged ghost of some kind.
920 if (ghost.name.length() > MAX_NAME_LENGTH * 10 || ghost.name.empty())
921 return false;
922 if (ghost.name != trimmed_string(ghost.name))
923 return false;
924
925 // Check for non-existing spells.
926 for (const mon_spell_slot &slot : ghost.spells)
927 if (slot.spell < 0 || slot.spell >= NUM_SPELLS)
928 return false;
929
930 return true;
931 }
932
933 // Sanity checks for some ghost values.
debug_check_ghosts(vector<ghost_demon> & ghosts)934 bool debug_check_ghosts(vector<ghost_demon> &ghosts)
935 {
936 for (const ghost_demon &ghost : ghosts)
937 if (!debug_check_ghost(ghost))
938 return false;
939 return true;
940 }
941
ghost_level_to_rank(const int xl)942 int ghost_level_to_rank(const int xl)
943 {
944 if (xl < 4) return 0;
945 if (xl < 7) return 1;
946 if (xl < 11) return 2;
947 if (xl < 16) return 3;
948 if (xl < 22) return 4;
949 if (xl < 26) return 5;
950 if (xl < 27) return 6;
951 return 7;
952 }
953
954 // Approximate inverse, in the middle of the range
ghost_rank_to_level(const int rank)955 int ghost_rank_to_level(const int rank)
956 {
957 switch (rank)
958 {
959 case 0:
960 return 2;
961 case 1:
962 return 5;
963 case 2:
964 return 9;
965 case 3:
966 return 13;
967 case 4:
968 return 19;
969 case 5:
970 return 24;
971 case 6:
972 return 26;
973 case 7:
974 return 27;
975 default:
976 die("Bad ghost rank %d", rank);
977 }
978 }
979