ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.46
Committed: Thu Jan 1 16:32:53 2009 UTC (15 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.45: +1 -1 lines
Log Message:
doh

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