ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.47
Committed: Thu Jan 1 20:49:48 2009 UTC (15 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_76, rel-2_77, rel-2_75, rel-2_79, rel-2_78
Changes since 1.46: +1 -1 lines
Log Message:
slim down perl interface

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.31 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.18 *
4 root 1.32 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.24 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.18 *
8 root 1.31 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.28 * 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 pippijn 1.18 *
13 root 1.28 * 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 pippijn 1.18 *
18 root 1.28 * 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 root 1.24 *
21 root 1.31 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.18 */
23 elmex 1.1
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 root 1.14 #include <sproto.h>
36 elmex 1.1
37     /**
38     * Returns the id of specified god.
39     */
40 root 1.6 int
41 root 1.43 lookup_god_by_name (shstr_cmp name)
42 root 1.6 {
43 root 1.43 if (name)
44 root 1.41 for (godlink *gl = first_god; gl; gl = gl->next)
45     if (gl->name == name)
46     return gl->id;
47 root 1.27
48 root 1.41 return -1;
49 elmex 1.1 }
50    
51     /**
52     * Returns pointer to specified god's object through pntr_to_god_obj..
53     */
54 root 1.6 object *
55 root 1.43 find_god (shstr_cmp name)
56 root 1.6 {
57     if (name)
58 root 1.41 for (godlink *gl = first_god; gl; gl = gl->next)
59 root 1.43 if (gl->name == name)
60 root 1.41 return pntr_to_god_obj (gl);
61 elmex 1.1
62 root 1.41 return 0;
63 elmex 1.1 }
64    
65     /**
66 root 1.43 * 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 elmex 1.1 * 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 root 1.41 shstr_tmp
89 root 1.6 determine_god (object *op)
90     {
91     /* spells */
92     if ((op->type == SPELL || op->type == SPELL_EFFECT) && op->title)
93 root 1.41 if (lookup_god_by_name (op->title) >= 0)
94     return op->title;
95 root 1.6
96 root 1.41 if (!op->is_player () && op->flag [FLAG_ALIVE])
97 root 1.6 {
98     /* find a god based on race */
99     if (!op->title)
100 root 1.41 op->title = get_god_for_race (op->race);
101 root 1.3
102 root 1.6 /* find a random god */
103     if (!op->title)
104     {
105 root 1.41 int godnr = rndm (1, first_god->id);
106 root 1.6
107 root 1.46 for (godlink *gl = first_god; gl; gl = gl->next)
108 root 1.41 if (gl->id == godnr)
109     {
110     op->title = gl->name;
111 root 1.6 break;
112 root 1.41 }
113 root 1.3 }
114 elmex 1.1
115 root 1.6 return op->title;
116 elmex 1.1 }
117    
118 root 1.6 /* 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 root 1.41 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 root 1.29
133 root 1.41 return shstr_none;
134 elmex 1.1 }
135    
136     /**
137     * Returns 1 if s1 and s2 are the same - either both NULL, or strcmp( ) == 0
138     */
139 root 1.6 static int
140     same_string (const char *s1, const char *s2)
141 elmex 1.1 {
142 root 1.41 if (s1 == s2)
143     return 1;
144     else if (s1 && s2)
145     return !strcmp (s1, s2);
146     else
147 root 1.6 return 0;
148 elmex 1.1 }
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 root 1.6 static void
156     follower_remove_similar_item (object *op, object *item)
157 elmex 1.1 {
158 root 1.6 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 root 1.39 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 root 1.6
176 root 1.40 tmp->destroy ();
177 root 1.3 }
178 root 1.10
179 root 1.6 if (tmp->inv)
180     follower_remove_similar_item (tmp, item);
181 root 1.3 }
182 elmex 1.1 }
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 root 1.6 static int
190     follower_has_similar_item (object *op, object *item)
191 elmex 1.1 {
192 root 1.6 object *tmp;
193 elmex 1.1
194 root 1.6 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 elmex 1.1 }
203 root 1.6 return 0;
204 elmex 1.1 }
205    
206     /**
207     * God gives an item to the player.
208     */
209 root 1.6 static int
210     god_gives_present (object *op, object *god, treasure *tr)
211 elmex 1.1 {
212 elmex 1.16 if (!tr->item)
213     return 0;
214    
215 root 1.26 if (follower_has_similar_item (op, tr->item))
216 root 1.6 return 0;
217 elmex 1.1
218 root 1.33 object *tmp = arch_to_object (tr->item);
219 root 1.6 new_draw_info_format (NDI_UNIQUE, 0, op, "%s lets %s appear in your hands.", &god->name, query_short_name (tmp));
220 root 1.33 op->insert (tmp);
221 elmex 1.16
222 root 1.6 return 1;
223 elmex 1.1 }
224    
225     /**
226     * Player prays at altar.
227     * Checks for god changing, divine intervention, and so on.
228     */
229 root 1.6 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 elmex 1.1
237 root 1.6 /* 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 root 1.26 become_follower (pl, altar->other_arch);
245 root 1.2 return;
246 root 1.6 }
247 root 1.26 else if (!strcmp (&pl_god->name, altar->other_arch->object::name))
248 root 1.6 {
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 root 1.30
256 root 1.6 /* we can super-charge grace to 2x max */
257 root 1.30 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 root 1.6
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 root 1.25 if (pl_god->other_arch && (altar->other_arch->archname == pl_god->other_arch->archname))
283 root 1.6 {
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 elmex 1.1
301 root 1.6 /* 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 root 1.26 become_follower (pl, altar->other_arch);
314 root 1.6 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 root 1.23 pl->contr->fire_on = 0;
319     pl->speed_left = 1.f;
320 root 1.6 move_player (pl, absdir (pl->facing + 4)); /* back him off the way he came. */
321     }
322 elmex 1.1 }
323     }
324    
325     /**
326     * Removes special prayers given by a god.
327     */
328 root 1.6 static void
329     check_special_prayers (object *op, object *god)
330 elmex 1.1 {
331 root 1.6 /* 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 elmex 1.1
349 root 1.6 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 root 1.3 }
355 elmex 1.1
356 root 1.6 /* 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 elmex 1.16 if (!tr->item)
365 root 1.6 continue;
366 root 1.19
367 root 1.26 item = tr->item;
368 elmex 1.1
369 root 1.6 /* Basically, see if the matching spell is granted by this god. */
370 elmex 1.1
371 root 1.26 if (tr->item->type == SPELL && tr->item->object::name == tmp->name)
372 root 1.6 {
373     remove = 0;
374     break;
375 root 1.3 }
376     }
377 root 1.36
378 root 1.6 if (remove)
379 root 1.36 do_forget_spell (op, tmp->name);
380 elmex 1.1 }
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 root 1.6 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 root 1.36 for (item = op->inv; item; item = next)
401 root 1.6 {
402     next = item->below;
403 root 1.36
404 elmex 1.12 // remove all invisible startequip items which are not skill, exp or force
405 root 1.6 if (QUERY_FLAG (item, FLAG_STARTEQUIP) && item->invisible &&
406 elmex 1.12 (item->type != SKILL) && (item->type != FORCE))
407 root 1.6 {
408     if (item->type == SPELL)
409 root 1.36 {
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 root 1.10
414 root 1.6 player_unready_range_ob (op->contr, item);
415 root 1.40 item->destroy ();
416 root 1.6 }
417     }
418    
419     /* remove any godgiven items from the old god */
420     if (old_god)
421 root 1.19 for (tr = old_god->randomitems->items; tr; tr = tr->next)
422 root 1.26 if (tr->item && QUERY_FLAG (tr->item, FLAG_STARTEQUIP))
423     follower_remove_similar_item (op, tr->item);
424 root 1.6
425     if (!op || !new_god)
426     return;
427    
428 root 1.47 if (new_god->slaying && op->race.contains (new_god->slaying))
429 root 1.6 {
430     new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, op, "Fool! %s detests your kind!", &new_god->name);
431 root 1.19
432 root 1.6 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 root 1.19
439 root 1.6 return;
440     }
441    
442     /* give the player any special god-characteristic-items. */
443 root 1.19 for (tr = new_god->randomitems->items; tr; tr = tr->next)
444 root 1.6 {
445 root 1.26 if (tr->item && tr->item->invisible && tr->item->type != SPELLBOOK
446     && tr->item->type != BOOK && tr->item->type != SPELL)
447 root 1.6 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 root 1.37 for (skop = op->inv; skop; skop = skop->below)
453 root 1.6 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
454     break;
455    
456     /* Player has no skill - give them the skill */
457     if (!skop)
458 root 1.38 /* 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 root 1.6
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 root 1.38 if (old_god && QUERY_FLAG (old_god, FLAG_UNDEAD))
470 root 1.6 {
471     CLEAR_FLAG (skop, FLAG_UNDEAD);
472     undeadified = 1;
473     }
474    
475     if (skop->title)
476 root 1.34 {
477     /* get rid of old god */
478 root 1.6 new_draw_info_format (NDI_UNIQUE, 0, op, "%s's blessing is withdrawn from you.", &skop->title);
479 root 1.34
480 root 1.6 /* 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 root 1.34 change_abil (op, skop);
485 root 1.6 }
486 elmex 1.1 }
487    
488 root 1.6 /* 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 root 1.36
530 root 1.6 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 elmex 1.1
544 root 1.6 SET_FLAG (skop, FLAG_APPLIED);
545     (void) change_abil (op, skop);
546 elmex 1.1
547 root 1.6 /* return to previous skill status */
548     if (!sk_applied)
549     CLEAR_FLAG (skop, FLAG_APPLIED);
550    
551     check_special_prayers (op, new_god);
552 elmex 1.1 }
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 root 1.6 int
563     worship_forbids_use (object *op, object *exp_obj, uint32 flag, const char *string)
564     {
565 root 1.26 if (QUERY_FLAG (op->arch, flag))
566 root 1.6 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 elmex 1.1 }
577 root 1.36
578 elmex 1.1 return 0;
579     }
580    
581     /**
582     * Unapplies up to number worth of items of type
583     */
584 root 1.6 void
585     stop_using_item (object *op, int type, int number)
586     {
587 elmex 1.1 object *tmp;
588    
589 root 1.6 for (tmp = op->inv; tmp && number; tmp = tmp->below)
590     if (tmp->type == type && QUERY_FLAG (tmp, FLAG_APPLIED))
591     {
592 elmex 1.1 apply_special (op, tmp, AP_UNAPPLY | AP_IGNORE_CURSE);
593 root 1.3 number--;
594 root 1.6 }
595 elmex 1.1 }
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 root 1.6 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 elmex 1.1 /* if (!(QUERY_FLAG(&(exp_ob->arch->clone),flag)))*/
620 root 1.6 CLEAR_FLAG (exp_ob, flag);
621     };
622 elmex 1.1 }
623    
624 root 1.6 archetype *
625 root 1.43 determine_holy_arch (object *god, shstr_cmp type)
626 elmex 1.1 {
627 root 1.6 treasure *tr;
628 elmex 1.1
629 root 1.6 if (!god || !god->randomitems)
630     {
631     LOG (llevError, "BUG: determine_holy_arch(): no god or god without " "randomitems\n");
632 root 1.35 return 0;
633 elmex 1.1 }
634    
635 root 1.19 for (tr = god->randomitems->items; tr; tr = tr->next)
636 root 1.6 {
637     if (!tr->item)
638     continue;
639 elmex 1.16
640 root 1.26 object *item = tr->item;
641 elmex 1.1
642 root 1.43 if (item->type == BOOK && item->invisible && item->name == type)
643 root 1.6 return item->other_arch;
644 elmex 1.1 }
645 root 1.35
646     return 0;
647 elmex 1.1 }
648    
649     /**
650     * God helps player by removing curse and/or damnation.
651     */
652 root 1.6 static int
653     god_removes_curse (object *op, int remove_damnation)
654 elmex 1.1 {
655 root 1.6 int success = 0;
656 elmex 1.1
657 root 1.33 for (object *tmp = op->inv; tmp; tmp = tmp->below)
658 root 1.6 {
659     if (tmp->invisible)
660     continue;
661 root 1.33
662 root 1.6 if (QUERY_FLAG (tmp, FLAG_DAMNED) && !remove_damnation)
663     continue;
664 root 1.33
665 root 1.6 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 root 1.33
672     if (object *pl = tmp->visible_to ())
673     esrv_update_item (UPD_FLAGS, pl, tmp);
674 elmex 1.1 }
675     }
676    
677 root 1.6 if (success)
678     new_draw_info (NDI_UNIQUE, 0, op, "You feel like someone is helping you.");
679 root 1.33
680 root 1.6 return success;
681 elmex 1.1 }
682    
683 root 1.6 static int
684     follower_level_to_enchantments (int level, int difficulty)
685 elmex 1.1 {
686 root 1.6 if (difficulty < 1)
687     {
688     LOG (llevError, "follower_level_to_enchantments(): " "difficulty %d is invalid\n", difficulty);
689     return 0;
690 elmex 1.1 }
691    
692 root 1.6 if (level <= 20)
693     return level / difficulty;
694 root 1.35
695 root 1.6 if (level <= 40)
696     return (20 + (level - 20) / 2) / difficulty;
697 root 1.20
698 root 1.6 return (30 + (level - 40) / 4) / difficulty;
699 elmex 1.1 }
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 root 1.6 static int
708     god_enchants_weapon (object *op, object *god, object *tr, object *skill)
709 elmex 1.1 {
710 root 1.6 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 root 1.20
718 root 1.35 if (!weapon || god_examines_item (god, weapon) <= 0)
719 root 1.6 return 0;
720    
721     /* First give it a title, so other gods won't touch it */
722     if (!weapon->title)
723     {
724 root 1.35 weapon->title = format ("of %s", &god->name);
725 root 1.33
726     if (object *pl = weapon->visible_to ())
727     esrv_update_item (UPD_NAME, pl, weapon);
728    
729 root 1.6 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 elmex 1.1 }
739    
740 root 1.6 /* 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 elmex 1.1 }
748    
749 root 1.6 /* 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 root 1.33
756     if (object *pl = weapon->visible_to ())
757     esrv_update_item (UPD_NAME, pl, weapon);
758    
759 root 1.6 return 1;
760 elmex 1.1 }
761    
762 root 1.6 return 0;
763 elmex 1.1 }
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 root 1.6 void
772     god_intervention (object *op, object *god, object *skill)
773 elmex 1.1 {
774 root 1.6 treasure *tr;
775 elmex 1.1
776 root 1.6 if (!god || !god->randomitems)
777     {
778     LOG (llevError, "BUG: god_intervention(): no god or god without randomitems\n");
779     return;
780 elmex 1.1 }
781    
782 root 1.6 check_special_prayers (op, god);
783 elmex 1.1
784 root 1.6 /* lets do some checks of whether we are kosher with our god */
785     if (god_examines_priest (op, god) < 0)
786     return;
787 elmex 1.1
788 root 1.29 op->play_sound (sound_find ("god_intervention"));
789 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "You feel a holy presence!");
790 elmex 1.1
791 root 1.19 for (tr = god->randomitems->items; tr; tr = tr->next)
792 root 1.6 {
793     object *item;
794    
795     if (tr->chance <= random_roll (0, 99, op, PREFER_HIGH))
796     continue;
797 elmex 1.1
798 root 1.6 /* Treasurelist - generate some treasure for the follower */
799     if (tr->name)
800     {
801 root 1.19 treasurelist *tl = treasurelist::find (tr->name);
802 root 1.6
803     if (tl == NULL)
804 elmex 1.1 continue;
805    
806 root 1.29 new_draw_info (NDI_UNIQUE, 0, op, "Something appears before your eyes. You catch it before it falls to the ground.");
807 elmex 1.1
808 root 1.33 create_treasure (tl, op, GT_STARTEQUIP | GT_ONLY_GOOD, skill->level, 0);
809 root 1.6 return;
810 elmex 1.1 }
811    
812 root 1.6 if (!tr->item)
813 elmex 1.16 continue;
814    
815 root 1.26 item = tr->item;
816 elmex 1.1
817 root 1.6 /* Grace limit */
818 root 1.43 if (item->type == BOOK && item->invisible && item->name == shstr_grace_limit)
819 root 1.6 {
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 root 1.43 object *tmp = get_archetype (HOLY_POSSESSION);
826 root 1.6 cast_change_ability (op, op, tmp, 0, 1);
827 root 1.40 tmp->destroy ();
828 root 1.6 return;
829 elmex 1.1 }
830 root 1.29
831 root 1.6 continue;
832 elmex 1.1 }
833    
834 root 1.6 /* Restore grace */
835 root 1.43 if (item->type == BOOK && item->invisible && item->name == shstr_restore_grace)
836 root 1.6 {
837     if (op->stats.grace >= 0)
838     continue;
839 root 1.43
840 root 1.6 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 elmex 1.1 }
844    
845 root 1.6 /* 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 root 1.43
851 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "A white light surrounds and heals you!");
852     op->stats.hp = op->stats.maxhp;
853     return;
854 elmex 1.1 }
855    
856 root 1.6 /* Restore spellpoints */
857     if (item->type == BOOK && item->invisible && strcmp (item->name, "restore spellpoints") == 0)
858 elmex 1.1 {
859 root 1.6 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 root 1.35
867     new_draw_info (NDI_UNIQUE, 0, op, "A blue lightning strikes your head but doesn't hurt you!");
868 root 1.6 op->stats.sp = new_sp;
869 elmex 1.1 }
870    
871 root 1.6 /* Various heal spells */
872     if (item->type == BOOK && item->invisible && strcmp (item->name, "heal spell") == 0)
873 elmex 1.1 {
874 root 1.35 object *tmp = archetype::get (item->slaying);
875     int success = cast_heal (op, op, tmp, 0);
876 root 1.40 tmp->destroy ();
877 elmex 1.1
878 root 1.6 if (success)
879     return;
880     else
881     continue;
882 elmex 1.1 }
883    
884 root 1.6 /* Remove curse */
885     if (item->type == BOOK && item->invisible && strcmp (item->name, "remove curse") == 0)
886 elmex 1.1 {
887 root 1.6 if (god_removes_curse (op, 0))
888     return;
889     else
890     continue;
891 elmex 1.1 }
892    
893 root 1.6 /* Remove damnation */
894     if (item->type == BOOK && item->invisible && strcmp (item->name, "remove damnation") == 0)
895 elmex 1.1 {
896 root 1.6 if (god_removes_curse (op, 1))
897     return;
898     else
899     continue;
900 elmex 1.1 }
901    
902 root 1.6 /* Heal depletion */
903     if (item->type == BOOK && item->invisible && strcmp (item->name, "heal depletion") == 0)
904 elmex 1.1 {
905 root 1.6 object *depl;
906     archetype *at;
907     int i;
908    
909 root 1.7 if ((at = archetype::find (ARCH_DEPLETION)) == NULL)
910 root 1.6 {
911     LOG (llevError, "Could not find archetype depletion.\n");
912     continue;
913 elmex 1.1 }
914 root 1.43
915 root 1.6 depl = present_arch_in_ob (at, op);
916 root 1.10
917 root 1.6 if (depl == NULL)
918     continue;
919 root 1.10
920 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "Shimmering light surrounds and restores you!");
921 root 1.10
922 root 1.6 for (i = 0; i < NUM_STATS; i++)
923 root 1.21 if (depl->stats.stat (i))
924 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, restore_msg[i]);
925 root 1.10
926 root 1.40 depl->destroy ();
927 root 1.13 op->update_stats ();
928 root 1.6 return;
929 elmex 1.1 }
930 root 1.6
931     /* Voices */
932     if (item->type == BOOK && item->invisible && strcmp (item->name, "voice_behind") == 0)
933     {
934 root 1.29 new_draw_info (NDI_UNIQUE, 0, op, "You hear a voice from behind you, but you don't dare to turn around:");
935 root 1.6 new_draw_info (NDI_WHITE, 0, op, item->msg);
936     return;
937 elmex 1.1 }
938    
939 root 1.6 /* Messages */
940     if (item->type == BOOK && item->invisible && strcmp (item->name, "message") == 0)
941 elmex 1.1 {
942 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, item->msg);
943     return;
944 elmex 1.1 }
945    
946 root 1.6 /* Enchant weapon */
947     if (item->type == BOOK && item->invisible && strcmp (item->name, "enchant weapon") == 0)
948 elmex 1.1 {
949 root 1.6 if (god_enchants_weapon (op, god, item, skill))
950     return;
951     else
952     continue;
953 elmex 1.1 }
954    
955 root 1.6 /* Spellbooks - works correctly only for prayers */
956     if (item->type == SPELL)
957 elmex 1.1 {
958 root 1.6 if (check_spell_known (op, item->name))
959     continue;
960     if (item->level > skill->level)
961     continue;
962 elmex 1.1
963 root 1.6 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 elmex 1.1
967 root 1.3 }
968 elmex 1.1
969 root 1.6 /* Other gifts */
970     if (!item->invisible)
971     {
972     if (god_gives_present (op, god, tr))
973     return;
974     else
975     continue;
976 elmex 1.1 }
977 root 1.6 /* else ignore it */
978 elmex 1.1 }
979    
980 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "You feel rapture.");
981 elmex 1.1 }
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 root 1.6 int
989     god_examines_priest (object *op, object *god)
990     {
991     int reaction = 1;
992     object *item = NULL, *skop;
993 elmex 1.1
994 root 1.6 for (item = op->inv; item; item = item->below)
995 root 1.37 if (QUERY_FLAG (item, FLAG_APPLIED))
996     reaction += god_examines_item (god, item) * (item->magic ? abs (item->magic) : 1);
997 elmex 1.1
998 root 1.6 /* 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 elmex 1.1
1004 root 1.37 for (skop = op->inv; skop; skop = skop->below)
1005 root 1.6 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
1006     break;
1007    
1008     if (skop)
1009 root 1.37 loss = 0.05f * skop->stats.exp;
1010    
1011 root 1.6 change_exp (op, -random_roll (0, loss * angry - 1, op, PREFER_LOW), skop ? &skop->skill : "none", SK_SUBTRACT_SKILL_EXP);
1012 root 1.37
1013 root 1.6 if (random_roll (0, angry, op, PREFER_LOW))
1014     {
1015     object *tmp = get_archetype (LOOSE_MANA);
1016 elmex 1.1
1017 root 1.6 cast_magic_storm (op, tmp, op->level + (angry * 3));
1018 root 1.3 }
1019 root 1.37
1020 root 1.6 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, op, "%s becomes angry and punishes you!", &god->name);
1021 elmex 1.1 }
1022 root 1.37
1023 root 1.6 return reaction;
1024 elmex 1.1 }
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 root 1.6 int
1033     god_examines_item (object *god, object *item)
1034     {
1035 elmex 1.1 char buf[MAX_BUF];
1036    
1037 root 1.6 if (!god || !item)
1038     return 0;
1039 elmex 1.1
1040 root 1.6 if (!item->title)
1041     return 1; /* unclaimed item are ok */
1042 elmex 1.1
1043 root 1.6 sprintf (buf, "of %s", &god->name);
1044 root 1.45 if (!strcmp (&item->title, buf))
1045 root 1.6 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 root 1.45 if (!strcmp (&item->title, buf))
1051 root 1.6 {
1052     if (item->env)
1053     {
1054     char buf[MAX_BUF];
1055 elmex 1.1
1056 root 1.6 sprintf (buf, "Heretic! You are using %s!", query_name (item));
1057     new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, item->env, buf);
1058     }
1059 root 1.45
1060 root 1.6 return -1;
1061     }
1062 elmex 1.1 }
1063    
1064 root 1.6 return 0; /* item is sacred to a non-enemy god/or is otherwise magical */
1065 elmex 1.1 }
1066    
1067     /**
1068     * Returns priest's god's id.
1069     * Straight calls lookup_god_by_name
1070     */
1071 root 1.6 int
1072     get_god (object *priest)
1073     {
1074 root 1.41 return lookup_god_by_name (determine_god (priest));
1075 elmex 1.1 }
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 root 1.6 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 root 1.11 if (!god && caster->owner)
1096     god = find_god (determine_god (caster->owner));
1097 root 1.6
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 root 1.10
1105 root 1.40 spellop->destroy ();
1106 root 1.6 return 0;
1107 elmex 1.1 }
1108    
1109 root 1.6 /* 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 root 1.4
1115 root 1.6 if (!caster_is_spell)
1116     spellop->slaying = god->slaying;
1117     else if (caster->slaying)
1118     spellop->slaying = caster->slaying;
1119 elmex 1.1 }
1120    
1121 root 1.6 /* 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 elmex 1.1
1125 root 1.6 /* 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 root 1.3 }
1136 root 1.6 }
1137 elmex 1.1
1138 root 1.6 return 1;
1139 elmex 1.1 }