ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.32
Committed: Tue Apr 22 02:46:18 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +2 -4 lines
Log Message:
- remove all "obviously safe" uses of esrv_del_item.

File Contents

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