ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.74
Committed: Sat Nov 17 23:40:03 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.73: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 /* Oct 3, 1995 - Code laid down for initial gods, priest alignment, and
27 * monster race initialization. b.t.
28 */
29
30 /* Sept 1996 - moved code over to object -oriented gods -b.t. */
31
32 #include <global.h>
33 #include <living.h>
34 #include <object.h>
35 #include <spells.h>
36 #include <sounds.h>
37 #include <sproto.h>
38
39 /**
40 * Returns the id of specified god.
41 */
42 static int
43 lookup_god_by_name (shstr_cmp name)
44 {
45 if (name)
46 for (godlink *gl = first_god; gl; gl = gl->next)
47 if (gl->name == name)
48 return gl->id;
49
50 return -1;
51 }
52
53 /**
54 * Returns pointer to specified god's object through pntr_to_god_obj..
55 */
56 object *
57 find_god (shstr_cmp name)
58 {
59 if (name)
60 for (godlink *gl = first_god; gl; gl = gl->next)
61 if (gl->name == name)
62 return pntr_to_god_obj (gl);
63
64 return 0;
65 }
66
67 /**
68 * Returns a string that is the name of the god that should be natively worshipped by a
69 * creature of who has race *race
70 * if we can't find a god that is appropriate, we return NULL
71 */
72 static shstr_tmp
73 get_god_for_race (shstr_cmp race)
74 {
75 if (race)
76 for (godlink *gl = first_god; gl; gl = gl->next)
77 if (gl->arch->race == race)
78 return gl->name;
79
80 return shstr_tmp ();
81 }
82
83 /**
84 * Determines if op worships a god.
85 * Returns the godname if they do or "none" if they have no god.
86 * In the case of an NPC, if they have no god, we try and guess
87 * who they should worship based on their race. If that fails we
88 * give them a random one.
89 */
90 shstr_tmp
91 determine_god (object *op)
92 {
93 /* spells */
94 if ((op->type == SPELL || op->type == SPELL_EFFECT) && op->title)
95 if (lookup_god_by_name (op->title) >= 0)
96 return op->title;
97
98 if (!op->is_player () && op->flag [FLAG_ALIVE])
99 {
100 /* find a god based on race */
101 if (!op->title)
102 op->title = get_god_for_race (op->race);
103
104 /* find a random god */
105 if (!op->title)
106 {
107 int godnr = rndm (1, first_god->id);
108
109 for (godlink *gl = first_god; gl; gl = gl->next)
110 if (gl->id == godnr)
111 {
112 op->title = gl->name;
113 break;
114 }
115 }
116
117 return op->title;
118 }
119
120 /* The god the player worships is in the praying skill (native skill
121 * not skill tool). Since a player can only have one instance of
122 * that skill, once we find it, we can return, either with the
123 * title or "none".
124 */
125 if (op->type == PLAYER)
126 for (object *tmp = op->inv; tmp; tmp = tmp->below)
127 if (tmp->type == SKILL && tmp->subtype == SK_PRAYING)
128 {
129 if (tmp->title)
130 return tmp->title;
131
132 break;
133 }
134
135 return shstr_none;
136 }
137
138 /**
139 * Returns 1 if s1 and s2 are the same - either both NULL, or strcmp( ) == 0
140 */
141 static int
142 same_string (const char *s1, const char *s2)
143 {
144 if (s1 == s2)
145 return 1;
146 else if (s1 && s2)
147 return !strcmp (s1, s2);
148 else
149 return 0;
150 }
151
152 /**
153 * Checks for any occurrence of the given 'item' in the inventory of 'op' (recursively).
154 * Any matching items in the inventory are deleted, and a
155 * message is displayed to the player.
156 */
157 static void
158 follower_remove_similar_item (object *op, object *item)
159 {
160 object *tmp, *next;
161
162 if (op && op->type == PLAYER && op->contr)
163 {
164 /* search the inventory */
165 for (tmp = op->inv; tmp != NULL; tmp = next)
166 {
167 next = tmp->below; /* backup in case we remove tmp */
168
169 if (tmp->type == item->type
170 && same_string (tmp->name, item->name)
171 && same_string (tmp->title, item->title) && same_string (tmp->msg, item->msg) && same_string (tmp->slaying, item->slaying))
172 {
173 /* message */
174 new_draw_info_format (NDI_UNIQUE, 0, op,
175 tmp->nrof > 1 ? "The %s crumble to dust!" : "The %s crumbles to dust!",
176 query_short_name (tmp));
177
178 tmp->destroy ();
179 }
180
181 if (tmp->inv)
182 follower_remove_similar_item (tmp, item);
183 }
184 }
185 }
186
187 /**
188 * Checks for any occurrence of the given 'item' in the inventory of 'op' (recursively).
189 * Returns 1 if found, else 0.
190 */
191 static int
192 follower_has_similar_item (object *op, object *item)
193 {
194 object *tmp;
195
196 for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
197 {
198 if (tmp->type == item->type
199 && same_string (tmp->name, item->name)
200 && same_string (tmp->title, item->title) && same_string (tmp->msg, item->msg) && same_string (tmp->slaying, item->slaying))
201 return 1;
202 if (tmp->inv && follower_has_similar_item (tmp, item))
203 return 1;
204 }
205 return 0;
206 }
207
208 /**
209 * God gives an item to the player.
210 */
211 static int
212 god_gives_present (object *op, object *god, treasure *tr)
213 {
214 if (!tr->item)
215 return 0;
216
217 if (follower_has_similar_item (op, tr->item))
218 return 0;
219
220 object *tmp = tr->item->instance ();
221 new_draw_info_format (NDI_UNIQUE, 0, op, "%s lets %s appear in your hands.", &god->name, query_short_name (tmp));
222 op->insert (tmp);
223
224 return 1;
225 }
226
227 /**
228 * Removes special prayers given by a god.
229 */
230 static void
231 check_special_prayers (object *op, object *god)
232 {
233 /* Ensure that 'op' doesn't know any special prayers that are not granted
234 * by 'god'.
235 */
236 treasure *tr;
237 object *tmp, *next_tmp;
238 int remove = 0;
239
240 /* Outer loop iterates over all special prayer marks */
241 for (tmp = op->inv; tmp; tmp = next_tmp)
242 {
243 next_tmp = tmp->below;
244
245 /* we mark special prayers with the STARTEQUIP flag, so if it isn't
246 * in that category, not something we need to worry about.
247 */
248 if (tmp->type != SPELL || !tmp->flag [FLAG_STARTEQUIP])
249 continue;
250
251 if (god->randomitems == NULL)
252 {
253 LOG (llevError, "BUG: check_special_prayers(): god %s without randomitems\n", &god->name);
254 do_forget_spell (op, tmp->name);
255 continue;
256 }
257
258 /* Inner loop tries to find the special prayer in the god's treasure
259 * list. We default that the spell should be removed.
260 */
261 remove = 1;
262 for (tr = god->randomitems->items; tr; tr = tr->next)
263 {
264 object *item;
265
266 if (!tr->item)
267 continue;
268
269 item = tr->item;
270
271 /* Basically, see if the matching spell is granted by this god. */
272
273 if (tr->item->type == SPELL && tr->item->object::name == tmp->name)
274 {
275 remove = 0;
276 break;
277 }
278 }
279
280 if (remove)
281 do_forget_spell (op, tmp->name);
282 }
283 }
284
285 /**
286 * Unapplies up to number worth of items of type
287 */
288 static void
289 stop_using_item (object *op, int type, int number)
290 {
291 for (object *tmp = op->inv; tmp && number; tmp = tmp->below)
292 if (tmp->type == type && tmp->flag [FLAG_APPLIED])
293 {
294 op->apply (tmp, AP_UNAPPLY | AP_IGNORE_CURSE);
295 --number;
296 }
297 }
298
299 /**
300 * If the god does/doesnt have this flag, we
301 * give/remove it from the experience object if it doesnt/does
302 * already exist. For players only!
303 */
304 static inline void
305 update_priest_flag (object *god, object *exp_ob, uint32 flag)
306 {
307 exp_ob->flag [flag] = god->flag [flag];
308 return;
309
310 // old code follows for reference...
311 if (god->flag [flag] && !exp_ob->flag [flag])
312 exp_ob->set_flag (flag);
313 else if (exp_ob->flag [flag] && !god->flag [flag])
314 {
315 /* When this is called with the exp_ob set to the player,
316 * this check is broken, because most all players arch
317 * allow use of weapons. I'm not actually sure why this
318 * check is here - I guess if you had a case where the
319 * value in the archetype (wisdom) should over ride the restrictions
320 * the god places on it, this may make sense. But I don't think
321 * there is any case like that.
322 */
323 /* if (!(QUERY_FLAG(&(exp_ob->arch->clone),flag)))*/
324 exp_ob->clr_flag (flag);
325 };
326 }
327
328 /**
329 * Forbids or let player use something item type.
330 * op is the player.
331 * exp_obj is the widsom experience.
332 * flag is the flag to check against.
333 * string is the string to print out.
334 */
335 static int
336 worship_forbids_use (object *op, object *exp_obj, uint32 flag, const char *string)
337 {
338 if (op->arch->flag [flag])
339 if (op->flag [flag] != exp_obj->flag [flag])
340 {
341 update_priest_flag (exp_obj, op, flag);
342 if (op->flag [flag])
343 new_draw_info_format (NDI_UNIQUE, 0, op, "You may use %s again.", string);
344 else
345 {
346 new_draw_info_format (NDI_UNIQUE, 0, op, "You are forbidden to use %s.", string);
347 return 1;
348 }
349 }
350
351 return 0;
352 }
353
354 /**
355 * This function is called whenever a player has
356 * switched to a new god. It handles basically all the stat changes
357 * that happen to the player, including the removal of godgiven
358 * items (from the former cult).
359 */
360 void
361 object::become_follower (object *new_god)
362 {
363 object *old_god = 0; /* old god */
364 treasure *tr;
365 object *item, *skop, *next;
366 int i, sk_applied, undeadified = 0; /* Turns to true if changing god can changes the undead status of the player. */
367
368 if (!contr)
369 return;
370
371 contr->queue_stats_update ();
372
373 old_god = find_god (determine_god (this));
374
375 /* take away any special god-characteristic items. */
376 for (item = inv; item; item = next)
377 {
378 next = item->below;
379
380 // remove all invisible startequip items which are not skill, exp or force
381 if (item->flag [FLAG_STARTEQUIP] && item->invisible
382 && item->type != SKILL && item->type != FORCE)
383 {
384 if (item->type == SPELL)
385 {
386 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, this, "You lose knowledge of %s.", &item->name);
387 esrv_remove_spell (contr, item);
388 }
389
390 item->destroy ();
391 }
392 }
393
394 /* remove any godgiven items from the old god */
395 if (old_god)
396 for (tr = old_god->randomitems->items; tr; tr = tr->next)
397 if (tr->item && tr->item->flag [FLAG_STARTEQUIP])
398 follower_remove_similar_item (this, tr->item);
399
400 if (!new_god)
401 return;
402
403 if (new_god->slaying && new_god->slaying.contains (race))
404 {
405 failmsgf ("Fool! %s detests your kind! H<This god does not accept followers of your race.>", &new_god->name);
406
407 if (random_roll (0, level - 1, this, PREFER_LOW) - 5 > 0)
408 {
409 object *tmp = archetype::get (LOOSE_MANA);
410
411 cast_magic_storm (this, tmp, new_god->level + 10);
412 }
413
414 return;
415 }
416
417 /* give the player any special god-characteristic-items. */
418 for (tr = new_god->randomitems->items; tr; tr = tr->next)
419 if (tr->item && tr->item->invisible && tr->item->type != SPELLBOOK
420 && tr->item->type != BOOK && tr->item->type != SPELL)
421 god_gives_present (this, new_god, tr);
422
423 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, this, "You become a follower of %s!", &new_god->name);
424
425 for (skop = inv; skop; skop = skop->below)
426 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
427 break;
428
429 /* Player has no skill - give them the skill */
430 if (!skop)
431 /* The archetype should always be defined - if we crash here because it doesn't,
432 * things are really messed up anyways.
433 */
434 skop = give_skill_by_name (this, shstr_praying);
435
436 sk_applied = skop->flag [FLAG_APPLIED]; /* save skill status */
437
438 /* Clear the "undead" status. We also need to force a call to change_abil,
439 * so I set undeadified for that.
440 * - gros, 21th July 2006.
441 */
442 if (old_god && old_god->flag [FLAG_UNDEAD])
443 {
444 skop->clr_flag (FLAG_UNDEAD);
445 undeadified = 1;
446 }
447
448 if (skop->title)
449 {
450 /* get rid of old god */
451 new_draw_info_format (NDI_UNIQUE, 0, this, "%s's blessing is withdrawn from you.", &skop->title);
452
453 /* The point of this is to really show what abilities the player just lost */
454 if (sk_applied || undeadified)
455 {
456 skop->clr_flag (FLAG_APPLIED);
457 change_abil (this, skop);
458 }
459 }
460
461 /* now change to the new gods attributes to exp_obj */
462 skop->title = new_god->name;
463 skop->path_attuned = new_god->path_attuned;
464 skop->path_repelled = new_god->path_repelled;
465 skop->path_denied = new_god->path_denied;
466 /* copy god's resistances */
467 memcpy (skop->resist, new_god->resist, sizeof (new_god->resist));
468
469 /* make sure that certain immunities do NOT get passed
470 * to the follower!
471 */
472 for (i = 0; i < NROFATTACKS; i++)
473 if (skop->resist[i] > 30 && (i == ATNR_FIRE || i == ATNR_COLD || i == ATNR_ELECTRICITY || i == ATNR_POISON))
474 skop->resist[i] = 30;
475
476 skop->stats.hp = (sint16) new_god->last_heal;
477 skop->stats.sp = (sint16) new_god->last_sp;
478 skop->stats.grace = (sint16) new_god->last_grace;
479 skop->stats.food = (sint16) new_god->last_eat;
480 skop->stats.luck = (sint8) new_god->stats.luck;
481 /* gods may pass on certain flag properties */
482 update_priest_flag (new_god, skop, FLAG_SEE_IN_DARK);
483 update_priest_flag (new_god, skop, FLAG_REFL_SPELL);
484 update_priest_flag (new_god, skop, FLAG_REFL_MISSILE);
485 update_priest_flag (new_god, skop, FLAG_STEALTH);
486 update_priest_flag (new_god, skop, FLAG_MAKE_INVIS);
487 update_priest_flag (new_god, skop, FLAG_UNDEAD);
488 update_priest_flag (new_god, skop, FLAG_BLIND);
489 update_priest_flag (new_god, skop, FLAG_XRAYS); /* better have this if blind! */
490
491 new_draw_info_format (NDI_UNIQUE, 0, this, "You are bathed in %s's aura.", &new_god->name);
492
493 /* Weapon/armour use are special...handle flag toggles here as this can
494 * only happen when gods are worshipped and if the new priest could
495 * have used armour/weapons in the first place.
496 *
497 * This also can happen for monks which cannot use weapons. In this case
498 * do not allow to use weapons even if the god otherwise would allow it.
499 */
500 if (!present_in_ob_by_name (FORCE, "no weapon force", this))
501 update_priest_flag (new_god, skop, FLAG_USE_WEAPON);
502
503 update_priest_flag (new_god, skop, FLAG_USE_ARMOUR);
504
505 if (worship_forbids_use (this, skop, FLAG_USE_WEAPON, "weapons"))
506 stop_using_item (this, WEAPON, 2);
507
508 if (worship_forbids_use (this, skop, FLAG_USE_ARMOUR, "armour"))
509 {
510 stop_using_item (this, ARMOUR, 1);
511 stop_using_item (this, HELMET, 1);
512 stop_using_item (this, BOOTS, 1);
513 stop_using_item (this, GLOVES, 1);
514 stop_using_item (this, SHIELD, 1);
515 }
516
517 skop->set_flag (FLAG_APPLIED);
518 change_abil (this, skop);
519
520 /* return to previous skill status */
521 if (!sk_applied)
522 {
523 skop->clr_flag (FLAG_APPLIED);
524 contr->queue_stats_update ();
525 }
526
527 check_special_prayers (this, new_god);
528 }
529
530 archetype *
531 determine_holy_arch (object *god, shstr_cmp type)
532 {
533 treasure *tr;
534
535 if (!god || !god->randomitems)
536 {
537 LOG (llevError, "BUG: determine_holy_arch(): no god or god without " "randomitems\n");
538 return 0;
539 }
540
541 for (tr = god->randomitems->items; tr; tr = tr->next)
542 {
543 if (!tr->item)
544 continue;
545
546 object *item = tr->item;
547
548 if (item->type == BOOK && item->invisible && item->name == type)
549 return item->other_arch;
550 }
551
552 return 0;
553 }
554
555 /**
556 * God helps player by removing curse and/or damnation.
557 */
558 static int
559 god_removes_curse (object *op, int remove_damnation)
560 {
561 int success = 0;
562
563 for (object *tmp = op->inv; tmp; tmp = tmp->below)
564 {
565 if (tmp->invisible)
566 continue;
567
568 if (tmp->flag [FLAG_DAMNED] && !remove_damnation)
569 continue;
570
571 if (tmp->flag [FLAG_CURSED] || tmp->flag [FLAG_DAMNED])
572 {
573 success = 1;
574 tmp->clr_flag (FLAG_DAMNED);
575 tmp->clr_flag (FLAG_CURSED);
576 tmp->clr_flag (FLAG_KNOWN_CURSED);
577
578 if (object *pl = tmp->visible_to ())
579 esrv_update_item (UPD_FLAGS, pl, tmp);
580 }
581 }
582
583 if (success)
584 new_draw_info (NDI_UNIQUE, 0, op, "You feel like someone is helping you.");
585
586 return success;
587 }
588
589 static int
590 follower_level_to_enchantments (int level, int difficulty)
591 {
592 if (difficulty < 1)
593 {
594 LOG (llevError, "follower_level_to_enchantments(): " "difficulty %d is invalid\n", difficulty);
595 return 0;
596 }
597
598 if (level <= 20)
599 return level / difficulty;
600
601 if (level <= 40)
602 return (20 + (level - 20) / 2) / difficulty;
603
604 return (30 + (level - 40) / 4) / difficulty;
605 }
606
607 /**
608 * God checks item the player is using.
609 * Return either -1 (bad), 0 (neutral) or
610 * 1 (item is ok). If you are using the item of an enemy
611 * god, it can be bad...-b.t.
612 */
613 static int
614 god_examines_item (object *god, object *item)
615 {
616 char buf[MAX_BUF];
617
618 if (!god || !item)
619 return 0;
620
621 if (!item->title)
622 return 1; /* unclaimed item are ok */
623
624 sprintf (buf, "of %s", &god->name);
625 if (!strcmp (&item->title, buf))
626 return 1; /* belongs to that God */
627
628 if (god->title)
629 { /* check if we have any enemy blessed item */
630 sprintf (buf, "of %s", &god->title);
631 if (!strcmp (&item->title, buf))
632 {
633 if (item->env)
634 {
635 char buf[MAX_BUF];
636
637 sprintf (buf, "Heretic! You are using %s!", query_name (item));
638 new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, item->env, buf);
639 }
640
641 return -1;
642 }
643 }
644
645 return 0; /* item is sacred to a non-enemy god/or is otherwise magical */
646 }
647
648 /**
649 * God wants to enchant weapon.
650 * Affected weapon is the applied one (weapon or bow). It's checked to make sure
651 * it isn't a weapon for another god. If all is all right, update weapon with
652 * attacktype, slaying and such.
653 */
654 static int
655 god_enchants_weapon (object *op, object *god, object *tr, object *skill)
656 {
657 if (!op->contr)
658 return 0;
659
660 object *weapon = op->contr->combat_ob;
661
662 if (!weapon)
663 return 0;
664
665 if (weapon->type != WEAPON && weapon->type != BOW)
666 return 0;
667
668 if (god_examines_item (god, weapon) <= 0)
669 return 0;
670
671 /* First give it a title, so other gods won't touch it */
672 if (!weapon->title)
673 {
674 weapon->title = format ("of %s", &god->name);
675
676 if (object *pl = weapon->visible_to ())
677 esrv_update_item (UPD_NAME, pl, weapon);
678
679 new_draw_info (NDI_UNIQUE, 0, op, "Your weapon quivers as if struck!");
680 }
681
682 /* Allow the weapon to slay enemies */
683 if (!weapon->slaying && god->slaying)
684 {
685 weapon->slaying = god->slaying;
686 new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s now hungers to slay enemies of your god!", &weapon->name);
687 return 1;
688 }
689
690 /* Add the gods attacktype */
691 uint32 attacktype = (weapon->attacktype == 0) ? AT_PHYSICAL : weapon->attacktype;
692
693 if ((attacktype & god->attacktype) != god->attacktype)
694 {
695 new_draw_info (NDI_UNIQUE, 0, op, "Your weapon suddenly glows!");
696 weapon->attacktype = attacktype | god->attacktype;
697 return 1;
698 }
699
700 /* Higher magic value */
701 if (weapon->magic < follower_level_to_enchantments (skill->level, tr->level))
702 {
703 new_draw_info (NDI_UNIQUE, 0, op, "A phosphorescent glow envelops your weapon!");
704 weapon->magic++;
705
706 if (object *pl = weapon->visible_to ())
707 esrv_update_item (UPD_NAME, pl, weapon);
708
709 return 1;
710 }
711
712 return 0;
713 }
714
715 /**
716 * Checks and maybe punishes someone praying.
717 * All applied items are examined, if player is using more items of other gods,
718 * s/he loses experience in praying or general experience if no praying.
719 */
720 static int
721 god_examines_priest (object *op, object *god)
722 {
723 int reaction = 1;
724 object *item = NULL, *skop;
725
726 for (item = op->inv; item; item = item->below)
727 if (item->flag [FLAG_APPLIED])
728 reaction += god_examines_item (god, item) * (item->magic ? abs (item->magic) : 1);
729
730 /* well, well. Looks like we screwed up. Time for god's revenge */
731 if (reaction < 0)
732 {
733 int loss = 10000000;
734 int angry = abs (reaction);
735
736 for (skop = op->inv; skop; skop = skop->below)
737 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
738 break;
739
740 if (skop)
741 loss = 0.05f * skop->stats.exp;
742
743 change_exp (op, -random_roll (0, loss * angry - 1, op, PREFER_LOW), skop ? skop->skill : shstr_none, SK_SUBTRACT_SKILL_EXP);
744
745 if (random_roll (0, angry, op, PREFER_LOW))
746 {
747 object *tmp = archetype::get (LOOSE_MANA);
748
749 cast_magic_storm (op, tmp, op->level + (angry * 3));
750 }
751
752 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, op, "%s becomes angry and punishes you!", &god->name);
753 }
754
755 return reaction;
756 }
757
758 /**
759 * Every once in a while the god will intervene to help the worshiper.
760 * Later, this fctn can be used to supply quests, etc for the
761 * priest. -b.t.
762 * called from pray_at_altar() currently.
763 */
764 static void
765 god_intervention (object *op, object *god, object *skill)
766 {
767 treasure *tr;
768
769 if (!god || !god->randomitems)
770 {
771 LOG (llevError, "BUG: god_intervention(): no god or god without randomitems\n");
772 return;
773 }
774
775 check_special_prayers (op, god);
776
777 /* lets do some checks of whether we are kosher with our god */
778 if (god_examines_priest (op, god) < 0)
779 return;
780
781 op->play_sound (sound_find ("god_intervention"));
782 new_draw_info (NDI_UNIQUE, 0, op, "You feel a holy presence!");
783
784 for (tr = god->randomitems->items; tr; tr = tr->next)
785 {
786 object *item;
787
788 if (tr->chance <= random_roll (0, 99, op, PREFER_HIGH))
789 continue;
790
791 /* Treasurelist - generate some treasure for the follower */
792 if (tr->name)
793 {
794 treasurelist *tl = treasurelist::find (tr->name);
795
796 if (tl == NULL)
797 continue;
798
799 new_draw_info (NDI_UNIQUE, 0, op, "Something appears before your eyes. You catch it before it falls to the ground.");
800
801 create_treasure (tl, op, GT_STARTEQUIP | GT_ONLY_GOOD, skill->level, 0);
802 return;
803 }
804
805 if (!tr->item)
806 continue;
807
808 item = tr->item;
809
810 /* Grace limit */
811 if (item->type == BOOK && item->invisible && item->name == shstr_grace_limit)
812 {
813 if (op->stats.grace < item->stats.grace || op->stats.grace < op->stats.maxgrace)
814 {
815 /* Follower lacks the required grace for the following
816 * treasure list items. */
817
818 object *tmp = archetype::get (HOLY_POSSESSION);
819 cast_change_ability (op, op, tmp, 0, 1);
820 tmp->destroy ();
821 return;
822 }
823
824 continue;
825 }
826
827 /* Restore grace */
828 if (item->type == BOOK && item->invisible && item->name == shstr_restore_grace)
829 {
830 if (op->stats.grace >= 0)
831 continue;
832
833 op->stats.grace = random_roll (0, 9, op, PREFER_HIGH);
834 new_draw_info (NDI_UNIQUE, 0, op, "You are returned to a state of grace.");
835 return;
836 }
837
838 /* Heal damage */
839 if (item->type == BOOK && item->invisible && strcmp (item->name, "restore hitpoints") == 0)
840 {
841 if (op->stats.hp >= op->stats.maxhp)
842 continue;
843
844 new_draw_info (NDI_UNIQUE, 0, op, "A white light surrounds and heals you!");
845 op->stats.hp = op->stats.maxhp;
846 return;
847 }
848
849 /* Restore spellpoints */
850 if (item->type == BOOK && item->invisible && strcmp (item->name, "restore spellpoints") == 0)
851 {
852 int max = (int) (op->stats.maxsp * (item->stats.maxsp / 100.0));
853
854 /* Restore to 50 .. 100%, if sp < 50% */
855 int new_sp = (int) (random_roll (1000, 1999, op, PREFER_HIGH) / 2000.0 * max);
856
857 if (op->stats.sp >= max / 2)
858 continue;
859
860 new_draw_info (NDI_UNIQUE, 0, op, "A blue lightning strikes your head but doesn't hurt you!");
861 op->stats.sp = new_sp;
862 }
863
864 /* Various heal spells */
865 if (item->type == BOOK && item->invisible && strcmp (item->name, "heal spell") == 0)
866 {
867 object *tmp = archetype::get (item->slaying);
868 int success = cast_heal (op, op, tmp, 0);
869 tmp->destroy ();
870
871 if (success)
872 return;
873 else
874 continue;
875 }
876
877 /* Remove curse */
878 if (item->type == BOOK && item->invisible && strcmp (item->name, "remove curse") == 0)
879 {
880 if (god_removes_curse (op, 0))
881 return;
882 else
883 continue;
884 }
885
886 /* Remove damnation */
887 if (item->type == BOOK && item->invisible && strcmp (item->name, "remove damnation") == 0)
888 {
889 if (god_removes_curse (op, 1))
890 return;
891 else
892 continue;
893 }
894
895 /* Heal depletion */
896 if (item->type == BOOK && item->invisible && strcmp (item->name, "heal depletion") == 0)
897 {
898 object *depl;
899 archetype *at;
900 int i;
901
902 if ((at = archetype::find (shstr_depletion)) == NULL)
903 {
904 LOG (llevError, "Could not find archetype depletion.\n");
905 continue;
906 }
907
908 depl = present_arch_in_ob (at, op);
909
910 if (depl == NULL)
911 continue;
912
913 new_draw_info (NDI_UNIQUE, 0, op, "Shimmering light surrounds and restores you!");
914
915 for (i = 0; i < NUM_STATS; i++)
916 if (depl->stats.stat (i))
917 new_draw_info (NDI_UNIQUE, 0, op, restore_msg[i]);
918
919 depl->destroy ();
920 op->update_stats ();
921 return;
922 }
923
924 /* Voices */
925 if (item->type == BOOK && item->invisible && strcmp (item->name, "voice_behind") == 0)
926 {
927 new_draw_info (NDI_UNIQUE, 0, op, "You hear a voice from behind you, but you don't dare to turn around:");
928 new_draw_info (NDI_WHITE, 0, op, item->msg);
929 return;
930 }
931
932 /* Messages */
933 if (item->type == BOOK && item->invisible && strcmp (item->name, "message") == 0)
934 {
935 new_draw_info (NDI_UNIQUE, 0, op, item->msg);
936 return;
937 }
938
939 /* Enchant weapon */
940 if (item->type == BOOK && item->invisible && strcmp (item->name, "enchant weapon") == 0)
941 {
942 if (god_enchants_weapon (op, god, item, skill))
943 return;
944 else
945 continue;
946 }
947
948 /* Spellbooks - works correctly only for prayers */
949 if (item->type == SPELL)
950 {
951 if (check_spell_known (op, item->name))
952 continue;
953 if (item->level > skill->level)
954 continue;
955
956 new_draw_info_format (NDI_UNIQUE, 0, op, "%s grants you use of a special prayer!", &god->name);
957 do_learn_spell (op, item, 1);
958 return;
959
960 }
961
962 /* Other gifts */
963 if (!item->invisible)
964 {
965 if (god_gives_present (op, god, tr))
966 return;
967 else
968 continue;
969 }
970 /* else ignore it */
971 }
972
973 new_draw_info (NDI_UNIQUE, 0, op, "You feel rapture.");
974 }
975
976 /**
977 * Player prays at altar.
978 * Checks for god changing, divine intervention, and so on.
979 */
980 void
981 pray_at_altar (object *pl, object *altar, object *skill)
982 {
983 object *pl_god = find_god (determine_god (pl));
984
985 if (INVOKE_PLAYER (PRAY_ALTAR, pl->contr, ARG_OBJECT (altar), ARG_OBJECT (skill)))
986 return;
987
988 /* If non consecrate altar, don't do anything */
989 if (!altar->other_arch)
990 return;
991
992 /* hmm. what happend depends on pl's current god, level, etc */
993 if (!pl_god)
994 { /*new convert */
995 pl->become_follower (altar->other_arch);
996 return;
997 }
998 else if (pl_god->name == altar->other_arch->object::name)
999 {
1000 /* pray at your gods altar */
1001 /* this leads to very low levels of wis and pray to result in no doubling! */
1002 int bonus = (pl->stats.Wis + skill->level) / 10;
1003
1004 /* we can get neg grace up faster */
1005 if (pl->stats.grace < 0)
1006 pl->stats.grace += (bonus > -1 * (pl->stats.grace / 10) ? bonus : -1 * (pl->stats.grace / 10));
1007
1008 /* we can super-charge grace to 2x max */
1009 if (pl->stats.grace < 2 * pl->stats.maxgrace)
1010 pl->stats.grace += bonus / 2;
1011 else
1012 pl->stats.grace = 2 * pl->stats.maxgrace;
1013
1014 /* Every once in a while, the god decides to checkup on their
1015 * follower, and may intervene to help them out.
1016 */
1017 bonus = max (1, bonus + max (pl->stats.luck, -3)); /* -- DAMN -- */
1018
1019 if (((random_roll (0, 399, pl, PREFER_LOW)) - bonus) < 0)
1020 god_intervention (pl, pl_god, skill);
1021 }
1022 else
1023 { /* praying to another god! */
1024 uint64 loss = 0;
1025 int angry = 1;
1026
1027 /* I believe the logic for detecting opposing gods was completely
1028 * broken - I think it should work now. altar->other_arch
1029 * points to the god of this altar (which we have
1030 * already verified is non null). pl_god->other_arch
1031 * is the opposing god - we need to verify that exists before
1032 * using its values.
1033 */
1034 if (pl_god->other_arch && (altar->other_arch->archname == pl_god->other_arch->archname))
1035 {
1036 angry = 2;
1037 if (random_roll (0, skill->level + 2, pl, PREFER_LOW) - 5 > 0)
1038 {
1039 object *tmp;
1040
1041 /* you really screwed up */
1042 angry = 3;
1043 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, pl, "Foul Priest! %s punishes you!", &pl_god->name);
1044 tmp = archetype::get (LOOSE_MANA);
1045 cast_magic_storm (pl, tmp, pl_god->level + 20);
1046 }
1047 else
1048 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, pl, "Foolish heretic! %s is livid!", &pl_god->name);
1049 }
1050 else
1051 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, pl, "Heretic! %s is angered!", &pl_god->name);
1052
1053 /* whether we will be successfull in defecting or not -
1054 * we lose experience from the clerical experience obj
1055 */
1056
1057 loss = angry * (skill->stats.exp / 10);
1058 if (loss)
1059 change_exp (pl, -random_roll64 (0, loss, pl, PREFER_LOW), skill ? skill->skill : shstr_none, SK_SUBTRACT_SKILL_EXP);
1060
1061 /* May switch Gods, but its random chance based on our current level
1062 * note it gets harder to swap gods the higher we get
1063 */
1064 if ((angry == 1) && !(random_roll (0, skill->level, pl, PREFER_LOW)))
1065 pl->become_follower (altar->other_arch);
1066 else
1067 {
1068 /* toss this player off the altar. He can try again. */
1069 new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, pl, "A divine force pushes you off the altar.");
1070 pl->contr->fire_on = 0;
1071 pl->speed_left = 1.f;
1072 move_player (pl, absdir (pl->facing + 4)); /* back him off the way he came. */
1073 }
1074 }
1075 }
1076
1077 /**
1078 * Changes the attributes of cone, smite, and ball spells as needed by the code.
1079 * Returns false if there was no race to assign to the slaying field of the spell, but
1080 * the spell attacktype contains AT_HOLYWORD. -b.t.
1081 */
1082 int
1083 tailor_god_spell (object *spellop, object *caster)
1084 {
1085 object *god = find_god (determine_god (caster));
1086 int caster_is_spell = 0;
1087
1088 if (caster->type == SPELL_EFFECT || caster->type == SPELL)
1089 caster_is_spell = 1;
1090
1091 /* if caster is a rune or the like, it doesn't worship anything. However,
1092 * if this object is owned by someone, then the god that they worship
1093 * is relevant, so use that.
1094 */
1095 if (!god && caster->owner)
1096 god = find_god (determine_god (caster->owner));
1097
1098 if (!god || (spellop->attacktype & AT_HOLYWORD && !god->race))
1099 {
1100 if (!caster_is_spell)
1101 new_draw_info (NDI_UNIQUE, 0, caster, "This prayer is useless unless you worship an appropriate god");
1102 else
1103 LOG (llevError, "BUG: tailor_god_spell(): no god\n");
1104
1105 spellop->destroy ();
1106 return 0;
1107 }
1108
1109 /* either holy word or godpower attacks will set the slaying field */
1110 if (spellop->attacktype & AT_HOLYWORD || spellop->attacktype & AT_GODPOWER)
1111 {
1112 if (spellop->slaying)
1113 spellop->slaying = NULL;
1114
1115 if (!caster_is_spell)
1116 spellop->slaying = god->slaying;
1117 else if (caster->slaying)
1118 spellop->slaying = caster->slaying;
1119 }
1120
1121 /* only the godpower attacktype adds the god's attack onto the spell */
1122 if (spellop->attacktype & AT_GODPOWER)
1123 spellop->attacktype = spellop->attacktype | god->attacktype;
1124
1125 /* tack on the god's name to the spell */
1126 if (spellop->attacktype & AT_HOLYWORD || spellop->attacktype & AT_GODPOWER)
1127 {
1128 spellop->title = god->name;
1129 if (spellop->title)
1130 {
1131 char buf[MAX_BUF];
1132
1133 sprintf (buf, "%s of %s", &spellop->name, &spellop->title);
1134 spellop->name = spellop->name_pl = buf;
1135 }
1136 }
1137
1138 return 1;
1139 }