ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.41
Committed: Wed Dec 31 17:35:37 2008 UTC (15 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.40: +42 -93 lines
Log Message:
refactoring of shstr classe,s new shstr_tmp, lots of minor rewriting

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