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

# 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 root 1.41 shstr_cmp sname (name);
44 root 1.6
45 root 1.41 if (sname)
46     for (godlink *gl = first_god; gl; gl = gl->next)
47     if (gl->name == name)
48     return gl->id;
49 root 1.27
50 root 1.41 return -1;
51 elmex 1.1 }
52    
53     /**
54     * Returns pointer to specified god's object through pntr_to_god_obj..
55     */
56 root 1.6 object *
57     find_god (const char *name)
58     {
59     if (name)
60 root 1.41 for (godlink *gl = first_god; gl; gl = gl->next)
61     if (!strcmp (name, gl->name))
62     return pntr_to_god_obj (gl);
63 elmex 1.1
64 root 1.41 return 0;
65 elmex 1.1 }
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 root 1.41 shstr_tmp
75 root 1.6 determine_god (object *op)
76     {
77     /* spells */
78     if ((op->type == SPELL || op->type == SPELL_EFFECT) && op->title)
79 root 1.41 if (lookup_god_by_name (op->title) >= 0)
80     return op->title;
81 root 1.6
82 root 1.41 if (!op->is_player () && op->flag [FLAG_ALIVE])
83 root 1.6 {
84     /* find a god based on race */
85     if (!op->title)
86 root 1.41 op->title = get_god_for_race (op->race);
87 root 1.3
88 root 1.6 /* find a random god */
89     if (!op->title)
90     {
91 root 1.41 int godnr = rndm (1, first_god->id);
92 root 1.6
93 root 1.41 for (godlink *gl = first_god; gl && gl->id != godnr; gl = gl->next)
94     if (gl->id == godnr)
95     {
96     op->title = gl->name;
97 root 1.6 break;
98 root 1.41 }
99 root 1.3 }
100 elmex 1.1
101 root 1.6 return op->title;
102 elmex 1.1 }
103    
104 root 1.6 /* 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 root 1.41 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 root 1.29
119 root 1.41 return shstr_none;
120 elmex 1.1 }
121    
122     /**
123     * Returns 1 if s1 and s2 are the same - either both NULL, or strcmp( ) == 0
124     */
125 root 1.6 static int
126     same_string (const char *s1, const char *s2)
127 elmex 1.1 {
128 root 1.41 if (s1 == s2)
129     return 1;
130     else if (s1 && s2)
131     return !strcmp (s1, s2);
132     else
133 root 1.6 return 0;
134 elmex 1.1 }
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 root 1.6 static void
142     follower_remove_similar_item (object *op, object *item)
143 elmex 1.1 {
144 root 1.6 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 root 1.39 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 root 1.6
162 root 1.40 tmp->destroy ();
163 root 1.3 }
164 root 1.10
165 root 1.6 if (tmp->inv)
166     follower_remove_similar_item (tmp, item);
167 root 1.3 }
168 elmex 1.1 }
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 root 1.6 static int
176     follower_has_similar_item (object *op, object *item)
177 elmex 1.1 {
178 root 1.6 object *tmp;
179 elmex 1.1
180 root 1.6 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 elmex 1.1 }
189 root 1.6 return 0;
190 elmex 1.1 }
191    
192     /**
193     * God gives an item to the player.
194     */
195 root 1.6 static int
196     god_gives_present (object *op, object *god, treasure *tr)
197 elmex 1.1 {
198 elmex 1.16 if (!tr->item)
199     return 0;
200    
201 root 1.26 if (follower_has_similar_item (op, tr->item))
202 root 1.6 return 0;
203 elmex 1.1
204 root 1.33 object *tmp = arch_to_object (tr->item);
205 root 1.6 new_draw_info_format (NDI_UNIQUE, 0, op, "%s lets %s appear in your hands.", &god->name, query_short_name (tmp));
206 root 1.33 op->insert (tmp);
207 elmex 1.16
208 root 1.6 return 1;
209 elmex 1.1 }
210    
211     /**
212     * Player prays at altar.
213     * Checks for god changing, divine intervention, and so on.
214     */
215 root 1.6 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 elmex 1.1
223 root 1.6 /* 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 root 1.26 become_follower (pl, altar->other_arch);
231 root 1.2 return;
232 root 1.6 }
233 root 1.26 else if (!strcmp (&pl_god->name, altar->other_arch->object::name))
234 root 1.6 {
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 root 1.30
242 root 1.6 /* we can super-charge grace to 2x max */
243 root 1.30 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 root 1.6
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 root 1.25 if (pl_god->other_arch && (altar->other_arch->archname == pl_god->other_arch->archname))
269 root 1.6 {
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 elmex 1.1
287 root 1.6 /* 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 root 1.26 become_follower (pl, altar->other_arch);
300 root 1.6 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 root 1.23 pl->contr->fire_on = 0;
305     pl->speed_left = 1.f;
306 root 1.6 move_player (pl, absdir (pl->facing + 4)); /* back him off the way he came. */
307     }
308 elmex 1.1 }
309     }
310    
311     /**
312     * Removes special prayers given by a god.
313     */
314 root 1.6 static void
315     check_special_prayers (object *op, object *god)
316 elmex 1.1 {
317 root 1.6 /* 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 elmex 1.1
335 root 1.6 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 root 1.3 }
341 elmex 1.1
342 root 1.6 /* 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 elmex 1.16 if (!tr->item)
351 root 1.6 continue;
352 root 1.19
353 root 1.26 item = tr->item;
354 elmex 1.1
355 root 1.6 /* Basically, see if the matching spell is granted by this god. */
356 elmex 1.1
357 root 1.26 if (tr->item->type == SPELL && tr->item->object::name == tmp->name)
358 root 1.6 {
359     remove = 0;
360     break;
361 root 1.3 }
362     }
363 root 1.36
364 root 1.6 if (remove)
365 root 1.36 do_forget_spell (op, tmp->name);
366 elmex 1.1 }
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 root 1.6 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 root 1.36 for (item = op->inv; item; item = next)
387 root 1.6 {
388     next = item->below;
389 root 1.36
390 elmex 1.12 // remove all invisible startequip items which are not skill, exp or force
391 root 1.6 if (QUERY_FLAG (item, FLAG_STARTEQUIP) && item->invisible &&
392 elmex 1.12 (item->type != SKILL) && (item->type != FORCE))
393 root 1.6 {
394     if (item->type == SPELL)
395 root 1.36 {
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 root 1.10
400 root 1.6 player_unready_range_ob (op->contr, item);
401 root 1.40 item->destroy ();
402 root 1.6 }
403     }
404    
405     /* remove any godgiven items from the old god */
406     if (old_god)
407 root 1.19 for (tr = old_god->randomitems->items; tr; tr = tr->next)
408 root 1.26 if (tr->item && QUERY_FLAG (tr->item, FLAG_STARTEQUIP))
409     follower_remove_similar_item (op, tr->item);
410 root 1.6
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 root 1.19
418 root 1.6 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 root 1.19
425 root 1.6 return;
426     }
427    
428     /* give the player any special god-characteristic-items. */
429 root 1.19 for (tr = new_god->randomitems->items; tr; tr = tr->next)
430 root 1.6 {
431 root 1.26 if (tr->item && tr->item->invisible && tr->item->type != SPELLBOOK
432     && tr->item->type != BOOK && tr->item->type != SPELL)
433 root 1.6 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 root 1.37 for (skop = op->inv; skop; skop = skop->below)
439 root 1.6 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
440     break;
441    
442     /* Player has no skill - give them the skill */
443     if (!skop)
444 root 1.38 /* 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 root 1.6
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 root 1.38 if (old_god && QUERY_FLAG (old_god, FLAG_UNDEAD))
456 root 1.6 {
457     CLEAR_FLAG (skop, FLAG_UNDEAD);
458     undeadified = 1;
459     }
460    
461     if (skop->title)
462 root 1.34 {
463     /* get rid of old god */
464 root 1.6 new_draw_info_format (NDI_UNIQUE, 0, op, "%s's blessing is withdrawn from you.", &skop->title);
465 root 1.34
466 root 1.6 /* 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 root 1.34 change_abil (op, skop);
471 root 1.6 }
472 elmex 1.1 }
473    
474 root 1.6 /* 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 root 1.36
516 root 1.6 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 elmex 1.1
530 root 1.6 SET_FLAG (skop, FLAG_APPLIED);
531     (void) change_abil (op, skop);
532 elmex 1.1
533 root 1.6 /* return to previous skill status */
534     if (!sk_applied)
535     CLEAR_FLAG (skop, FLAG_APPLIED);
536    
537     check_special_prayers (op, new_god);
538 elmex 1.1 }
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 root 1.6 int
549     worship_forbids_use (object *op, object *exp_obj, uint32 flag, const char *string)
550     {
551 root 1.26 if (QUERY_FLAG (op->arch, flag))
552 root 1.6 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 elmex 1.1 }
563 root 1.36
564 elmex 1.1 return 0;
565     }
566    
567     /**
568     * Unapplies up to number worth of items of type
569     */
570 root 1.6 void
571     stop_using_item (object *op, int type, int number)
572     {
573 elmex 1.1 object *tmp;
574    
575 root 1.6 for (tmp = op->inv; tmp && number; tmp = tmp->below)
576     if (tmp->type == type && QUERY_FLAG (tmp, FLAG_APPLIED))
577     {
578 elmex 1.1 apply_special (op, tmp, AP_UNAPPLY | AP_IGNORE_CURSE);
579 root 1.3 number--;
580 root 1.6 }
581 elmex 1.1 }
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 root 1.6 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 elmex 1.1 /* if (!(QUERY_FLAG(&(exp_ob->arch->clone),flag)))*/
606 root 1.6 CLEAR_FLAG (exp_ob, flag);
607     };
608 elmex 1.1 }
609    
610 root 1.6 archetype *
611     determine_holy_arch (object *god, const char *type)
612 elmex 1.1 {
613 root 1.6 treasure *tr;
614 elmex 1.1
615 root 1.6 if (!god || !god->randomitems)
616     {
617     LOG (llevError, "BUG: determine_holy_arch(): no god or god without " "randomitems\n");
618 root 1.35 return 0;
619 elmex 1.1 }
620    
621 root 1.19 for (tr = god->randomitems->items; tr; tr = tr->next)
622 root 1.6 {
623     if (!tr->item)
624     continue;
625 elmex 1.16
626 root 1.26 object *item = tr->item;
627 elmex 1.1
628 root 1.6 if (item->type == BOOK && item->invisible && strcmp (item->name, type) == 0)
629     return item->other_arch;
630 elmex 1.1 }
631 root 1.35
632     return 0;
633 elmex 1.1 }
634    
635     /**
636     * God helps player by removing curse and/or damnation.
637     */
638 root 1.6 static int
639     god_removes_curse (object *op, int remove_damnation)
640 elmex 1.1 {
641 root 1.6 int success = 0;
642 elmex 1.1
643 root 1.33 for (object *tmp = op->inv; tmp; tmp = tmp->below)
644 root 1.6 {
645     if (tmp->invisible)
646     continue;
647 root 1.33
648 root 1.6 if (QUERY_FLAG (tmp, FLAG_DAMNED) && !remove_damnation)
649     continue;
650 root 1.33
651 root 1.6 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 root 1.33
658     if (object *pl = tmp->visible_to ())
659     esrv_update_item (UPD_FLAGS, pl, tmp);
660 elmex 1.1 }
661     }
662    
663 root 1.6 if (success)
664     new_draw_info (NDI_UNIQUE, 0, op, "You feel like someone is helping you.");
665 root 1.33
666 root 1.6 return success;
667 elmex 1.1 }
668    
669 root 1.6 static int
670     follower_level_to_enchantments (int level, int difficulty)
671 elmex 1.1 {
672 root 1.6 if (difficulty < 1)
673     {
674     LOG (llevError, "follower_level_to_enchantments(): " "difficulty %d is invalid\n", difficulty);
675     return 0;
676 elmex 1.1 }
677    
678 root 1.6 if (level <= 20)
679     return level / difficulty;
680 root 1.35
681 root 1.6 if (level <= 40)
682     return (20 + (level - 20) / 2) / difficulty;
683 root 1.20
684 root 1.6 return (30 + (level - 40) / 4) / difficulty;
685 elmex 1.1 }
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 root 1.6 static int
694     god_enchants_weapon (object *op, object *god, object *tr, object *skill)
695 elmex 1.1 {
696 root 1.6 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 root 1.20
704 root 1.35 if (!weapon || god_examines_item (god, weapon) <= 0)
705 root 1.6 return 0;
706    
707     /* First give it a title, so other gods won't touch it */
708     if (!weapon->title)
709     {
710 root 1.35 weapon->title = format ("of %s", &god->name);
711 root 1.33
712     if (object *pl = weapon->visible_to ())
713     esrv_update_item (UPD_NAME, pl, weapon);
714    
715 root 1.6 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 elmex 1.1 }
725    
726 root 1.6 /* 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 elmex 1.1 }
734    
735 root 1.6 /* 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 root 1.33
742     if (object *pl = weapon->visible_to ())
743     esrv_update_item (UPD_NAME, pl, weapon);
744    
745 root 1.6 return 1;
746 elmex 1.1 }
747    
748 root 1.6 return 0;
749 elmex 1.1 }
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 root 1.6 void
758     god_intervention (object *op, object *god, object *skill)
759 elmex 1.1 {
760 root 1.6 treasure *tr;
761 elmex 1.1
762 root 1.6 if (!god || !god->randomitems)
763     {
764     LOG (llevError, "BUG: god_intervention(): no god or god without randomitems\n");
765     return;
766 elmex 1.1 }
767    
768 root 1.6 check_special_prayers (op, god);
769 elmex 1.1
770 root 1.6 /* lets do some checks of whether we are kosher with our god */
771     if (god_examines_priest (op, god) < 0)
772     return;
773 elmex 1.1
774 root 1.29 op->play_sound (sound_find ("god_intervention"));
775 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "You feel a holy presence!");
776 elmex 1.1
777 root 1.19 for (tr = god->randomitems->items; tr; tr = tr->next)
778 root 1.6 {
779     object *item;
780    
781     if (tr->chance <= random_roll (0, 99, op, PREFER_HIGH))
782     continue;
783 elmex 1.1
784 root 1.6 /* Treasurelist - generate some treasure for the follower */
785     if (tr->name)
786     {
787 root 1.19 treasurelist *tl = treasurelist::find (tr->name);
788 root 1.6
789     if (tl == NULL)
790 elmex 1.1 continue;
791    
792 root 1.29 new_draw_info (NDI_UNIQUE, 0, op, "Something appears before your eyes. You catch it before it falls to the ground.");
793 elmex 1.1
794 root 1.33 create_treasure (tl, op, GT_STARTEQUIP | GT_ONLY_GOOD, skill->level, 0);
795 root 1.6 return;
796 elmex 1.1 }
797    
798 root 1.6 if (!tr->item)
799 elmex 1.16 continue;
800    
801 root 1.26 item = tr->item;
802 elmex 1.1
803 root 1.6 /* 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 root 1.40 tmp->destroy ();
816 root 1.6 return;
817 elmex 1.1 }
818 root 1.29
819 root 1.6 continue;
820 elmex 1.1 }
821    
822 root 1.6 /* 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 elmex 1.1 }
831    
832 root 1.6 /* 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 elmex 1.1 }
841    
842 root 1.6 /* Restore spellpoints */
843     if (item->type == BOOK && item->invisible && strcmp (item->name, "restore spellpoints") == 0)
844 elmex 1.1 {
845 root 1.6 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 root 1.35
853     new_draw_info (NDI_UNIQUE, 0, op, "A blue lightning strikes your head but doesn't hurt you!");
854 root 1.6 op->stats.sp = new_sp;
855 elmex 1.1 }
856    
857 root 1.6 /* Various heal spells */
858     if (item->type == BOOK && item->invisible && strcmp (item->name, "heal spell") == 0)
859 elmex 1.1 {
860 root 1.35 object *tmp = archetype::get (item->slaying);
861     int success = cast_heal (op, op, tmp, 0);
862 root 1.40 tmp->destroy ();
863 elmex 1.1
864 root 1.6 if (success)
865     return;
866     else
867     continue;
868 elmex 1.1 }
869    
870 root 1.6 /* Remove curse */
871     if (item->type == BOOK && item->invisible && strcmp (item->name, "remove curse") == 0)
872 elmex 1.1 {
873 root 1.6 if (god_removes_curse (op, 0))
874     return;
875     else
876     continue;
877 elmex 1.1 }
878    
879 root 1.6 /* Remove damnation */
880     if (item->type == BOOK && item->invisible && strcmp (item->name, "remove damnation") == 0)
881 elmex 1.1 {
882 root 1.6 if (god_removes_curse (op, 1))
883     return;
884     else
885     continue;
886 elmex 1.1 }
887    
888 root 1.6 /* Heal depletion */
889     if (item->type == BOOK && item->invisible && strcmp (item->name, "heal depletion") == 0)
890 elmex 1.1 {
891 root 1.6 object *depl;
892     archetype *at;
893     int i;
894    
895 root 1.7 if ((at = archetype::find (ARCH_DEPLETION)) == NULL)
896 root 1.6 {
897     LOG (llevError, "Could not find archetype depletion.\n");
898     continue;
899 elmex 1.1 }
900 root 1.6 depl = present_arch_in_ob (at, op);
901 root 1.10
902 root 1.6 if (depl == NULL)
903     continue;
904 root 1.10
905 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "Shimmering light surrounds and restores you!");
906 root 1.10
907 root 1.6 for (i = 0; i < NUM_STATS; i++)
908 root 1.21 if (depl->stats.stat (i))
909 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, restore_msg[i]);
910 root 1.10
911 root 1.40 depl->destroy ();
912 root 1.13 op->update_stats ();
913 root 1.6 return;
914 elmex 1.1 }
915 root 1.6
916     /* Voices */
917     if (item->type == BOOK && item->invisible && strcmp (item->name, "voice_behind") == 0)
918     {
919 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:");
920 root 1.6 new_draw_info (NDI_WHITE, 0, op, item->msg);
921     return;
922 elmex 1.1 }
923    
924 root 1.6 /* Messages */
925     if (item->type == BOOK && item->invisible && strcmp (item->name, "message") == 0)
926 elmex 1.1 {
927 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, item->msg);
928     return;
929 elmex 1.1 }
930    
931 root 1.6 /* Enchant weapon */
932     if (item->type == BOOK && item->invisible && strcmp (item->name, "enchant weapon") == 0)
933 elmex 1.1 {
934 root 1.6 if (god_enchants_weapon (op, god, item, skill))
935     return;
936     else
937     continue;
938 elmex 1.1 }
939    
940 root 1.6 /* Spellbooks - works correctly only for prayers */
941     if (item->type == SPELL)
942 elmex 1.1 {
943 root 1.6 if (check_spell_known (op, item->name))
944     continue;
945     if (item->level > skill->level)
946     continue;
947 elmex 1.1
948 root 1.6 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 elmex 1.1
952 root 1.3 }
953 elmex 1.1
954 root 1.6 /* Other gifts */
955     if (!item->invisible)
956     {
957     if (god_gives_present (op, god, tr))
958     return;
959     else
960     continue;
961 elmex 1.1 }
962 root 1.6 /* else ignore it */
963 elmex 1.1 }
964    
965 root 1.6 new_draw_info (NDI_UNIQUE, 0, op, "You feel rapture.");
966 elmex 1.1 }
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 root 1.6 int
974     god_examines_priest (object *op, object *god)
975     {
976     int reaction = 1;
977     object *item = NULL, *skop;
978 elmex 1.1
979 root 1.6 for (item = op->inv; item; item = item->below)
980 root 1.37 if (QUERY_FLAG (item, FLAG_APPLIED))
981     reaction += god_examines_item (god, item) * (item->magic ? abs (item->magic) : 1);
982 elmex 1.1
983 root 1.6 /* 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 elmex 1.1
989 root 1.37 for (skop = op->inv; skop; skop = skop->below)
990 root 1.6 if (skop->type == SKILL && skop->subtype == SK_PRAYING)
991     break;
992    
993     if (skop)
994 root 1.37 loss = 0.05f * skop->stats.exp;
995    
996 root 1.6 change_exp (op, -random_roll (0, loss * angry - 1, op, PREFER_LOW), skop ? &skop->skill : "none", SK_SUBTRACT_SKILL_EXP);
997 root 1.37
998 root 1.6 if (random_roll (0, angry, op, PREFER_LOW))
999     {
1000     object *tmp = get_archetype (LOOSE_MANA);
1001 elmex 1.1
1002 root 1.6 cast_magic_storm (op, tmp, op->level + (angry * 3));
1003 root 1.3 }
1004 root 1.37
1005 root 1.6 new_draw_info_format (NDI_UNIQUE | NDI_NAVY, 0, op, "%s becomes angry and punishes you!", &god->name);
1006 elmex 1.1 }
1007 root 1.37
1008 root 1.6 return reaction;
1009 elmex 1.1 }
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 root 1.6 int
1019     god_examines_item (object *god, object *item)
1020     {
1021 elmex 1.1 char buf[MAX_BUF];
1022    
1023 root 1.6 if (!god || !item)
1024     return 0;
1025 elmex 1.1
1026 root 1.6 if (!item->title)
1027     return 1; /* unclaimed item are ok */
1028 elmex 1.1
1029 root 1.6 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 elmex 1.1
1042 root 1.6 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 elmex 1.1 }
1048    
1049 root 1.6 return 0; /* item is sacred to a non-enemy god/or is otherwise magical */
1050 elmex 1.1 }
1051    
1052     /**
1053     * Returns priest's god's id.
1054     * Straight calls lookup_god_by_name
1055     */
1056    
1057 root 1.6 int
1058     get_god (object *priest)
1059     {
1060 root 1.41 return lookup_god_by_name (determine_god (priest));
1061 elmex 1.1 }
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 root 1.41 shstr_tmp
1069 root 1.6 get_god_for_race (const char *race)
1070     {
1071 root 1.41 if (race)
1072     for (godlink *gl = first_god; gl; gl = gl->next)
1073     if (!strcasecmp (gl->arch->race, race))
1074     return gl->name;
1075 root 1.6
1076 root 1.41 return shstr_null;
1077 elmex 1.1 }
1078 root 1.6
1079 elmex 1.1 /**
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 root 1.6 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 root 1.11 if (!god && caster->owner)
1098     god = find_god (determine_god (caster->owner));
1099 root 1.6
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 root 1.10
1107 root 1.40 spellop->destroy ();
1108 root 1.6 return 0;
1109 elmex 1.1 }
1110    
1111 root 1.6 /* 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 root 1.4
1117 root 1.6 if (!caster_is_spell)
1118     spellop->slaying = god->slaying;
1119     else if (caster->slaying)
1120     spellop->slaying = caster->slaying;
1121 elmex 1.1 }
1122    
1123 root 1.6 /* 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 elmex 1.1
1127 root 1.6 /* 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 root 1.3 }
1138 root 1.6 }
1139 elmex 1.1
1140 root 1.6 return 1;
1141 elmex 1.1 }