ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.C
Revision: 1.28
Committed: Sun Jul 1 05:00:20 2007 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.27: +10 -11 lines
Log Message:
- upgrade crossfire trt to the GPL version 3 (hopefully correctly).
- add a single file covered by the GNU Affero General Public License
  (which is not yet released, so I used the current draft, which is
  legally a bit wavy, but its likely better than nothing as it expresses
  direct intent by the authors, and we can upgrade as soon as it has been
  released).
  * this should ensure availability of source code for the server at least
    and hopefully also archetypes and maps even when modified versions
    are not being distributed, in accordance of section 13 of the agplv3.

File Contents

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