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

File Contents

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