ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.39
Committed: Mon Sep 29 10:20:49 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.38: +9 -11 lines
Log Message:
do the same everywhere else

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