ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.38
Committed: Thu Sep 25 04:09:57 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.37: +5 -8 lines
Log Message:
*** empty log message ***

File Contents

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