Crossfire Server, Trunk  R21246
gods.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
24 #include "global.h"
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "living.h"
30 #include "object.h"
31 #include "sounds.h"
32 #include "spells.h"
33 #include "sproto.h"
34 
35 static int worship_forbids_use(object *op, object *exp_obj, uint32_t flag, const char *string);
36 static void stop_using_item(object *op, int type, int number);
37 static void update_priest_flag(const object *god, object *exp_ob, uint32_t flag);
38 static void god_intervention(object *op, const object *god, object *skill, object *altar);
39 static int god_examines_priest(object *op, const object *god);
40 static int god_examines_item(const object *god, object *item);
41 static const char *get_god_for_race(const char *race);
42 static void remove_special_prayers(object *op, const object *god);
43 
54 static int lookup_god_by_name(const char *name) {
55  int godnr = -1;
56  size_t nmlen = strlen(name);
57 
58  if (name && strcmp(name, "none")) {
59  godlink *gl;
60  for (gl = first_god; gl; gl = gl->next)
61  if (!strncmp(name, gl->name, MIN(strlen(gl->name), nmlen)))
62  break;
63  if (gl)
64  godnr = gl->id;
65  }
66  return godnr;
67 }
68 
80 const object *find_god(const char *name) {
81  godlink *gl;
82 
83  if (!name)
84  return NULL;
85 
86  for (gl = first_god; gl; gl = gl->next) {
87  if (!strcmp(name, gl->name))
88  return pntr_to_god_obj(gl);
89  }
90 
91  return NULL;
92 }
93 
106 const char *determine_god(object *op) {
107  int godnr = -1;
108  const char *godname;
109 
110  /* spells */
111  if ((op->type == SPELL || op->type == SPELL_EFFECT)
112  && op->title) {
113  if (lookup_god_by_name(op->title) >= 0)
114  return op->title;
115  }
116 
117  if (op->type != PLAYER && QUERY_FLAG(op, FLAG_ALIVE)) {
118  /* find a god based on race */
119  if (!op->title) {
120  if (op->race != NULL) {
121  godname = get_god_for_race(op->race);
122  if (godname != NULL) {
123  op->title = add_string(godname);
124  }
125  }
126  }
127 
128  /* find a random god */
129  if (!op->title) {
130  godlink *gl = first_god;
131 
132  godnr = rndm(1, gl->id);
133  while (gl) {
134  if (gl->id == godnr)
135  {
136  // This used to be after the loop, but if we do not find a god,
137  // it would have made a null pointer dereference. Moved here
138  // for better clarity and (hopefully) fewer bugs.
139  // SilverNexus 2018-01-17
140  op->title = add_string(gl->name);
141 
142  break;
143  }
144  gl = gl->next;
145  }
146  }
147 
148  return op->title;
149  }
150 
151  /* The god the player worships is in the praying skill (native skill
152  * not skill tool). Since a player can only have one instance of
153  * that skill, once we find it, we can return, either with the
154  * title or "none".
155  */
156  if (op->type == PLAYER) {
157  object *tmp;
158 
160  if (tmp != NULL) {
161  return tmp->title != NULL ? tmp->title : "none";
162  }
163  }
164  return ("none");
165 }
166 
175 static int same_string(const char *s1, const char *s2) {
176  if (s1 == NULL)
177  return s2 == NULL;
178  else
179  return s2 != NULL && strcmp(s1, s2) == 0;
180 }
181 
197 static void follower_remove_given_items(object *pl, object *op, const object *god) {
198  const char *given_by;
199 
200  /* search the inventory */
201  FOR_INV_PREPARE(op, tmp) {
202  given_by = object_get_value(tmp, "divine_giver_name");
203  if (given_by == god->name) {
204  char name[HUGE_BUF];
205 
206  query_short_name(tmp, name, HUGE_BUF);
207  /* Send the client a message. */
208  if (tmp->nrof > 1)
210  "The %s crumble to dust!",
211  name);
212  else
214  "The %s crumbles to dust!",
215  name);
216 
217  object_remove(tmp); /* remove obj from players inv. */
219  } else if (tmp->inv)
220  follower_remove_given_items(pl, tmp, god);
221  } FOR_INV_FINISH();
222 }
223 
233 static int follower_has_similar_item(object *op, object *item) {
234  FOR_INV_PREPARE(op, tmp) {
235  if (tmp->type == item->type
236  && same_string(tmp->name, item->name)
237  && same_string(tmp->title, item->title)
238  && tmp->msg == item->msg
239  && same_string(tmp->slaying, item->slaying))
240  return 1;
241  if (tmp->inv && follower_has_similar_item(tmp, item))
242  return 1;
243  } FOR_INV_FINISH();
244  return 0;
245 }
246 
259 static int god_gives_present(object *op, const object *god, treasure *tr) {
260  object *tmp;
261  char name[HUGE_BUF];
262 
263  if (follower_has_similar_item(op, &tr->item->clone))
264  return 0;
265 
266  tmp = arch_to_object(tr->item);
267 
268  /*
269  * Give inventory if needed, for instance Lythander's pipe.
270  * Use high level and magic so something IS put in inventory.
271  */
272  fix_generated_item(tmp, NULL, 120, 120, GT_ONLY_GOOD);
273 
274  /* And just in case nothing was put and something is needed, bail out. */
275  if ((tmp->type == ROD || tmp->type == WAND) && (tmp->inv == NULL)) {
276  object_free(tmp, 0);
277  return 0;
278  }
279 
280  query_short_name(tmp, name, HUGE_BUF);
282  "%s lets %s appear in your hands.",
283  god->name, name);
287  object_set_value(tmp, "divine_giver_name", god->name, TRUE);
288  object_insert_in_ob(tmp, op);
289  return 1;
290 }
291 
296 static bool try_leave_cult(object* pl, object* skill, int angry) {
297  const uint64_t loss = angry * (skill->stats.exp / 10);
298  if (loss)
299  change_exp(pl, -random_roll64(0, loss, pl, PREFER_LOW),
300  skill ? skill->skill : "none", SK_SUBTRACT_SKILL_EXP);
301 
302  /* random chance based on our current level
303  * note it gets harder the higher we get
304  */
305  if ((angry == 1) && !(random_roll(0, skill->level, pl, PREFER_LOW))) {
306  return true;
307  } else {
308  return false;
309  }
310 }
311 
323 void pray_at_altar(object *pl, object *altar, object *skill) {
324  const object *pl_god = find_god(determine_god(pl));
325 
326  /* Lauwenmark: Handle for plugin altar-praying (apply) event */
327  if (execute_event(altar, EVENT_APPLY, pl, NULL, NULL, SCRIPT_FIX_ALL) != 0)
328  return;
329 
330  /* If non consecrate altar, don't do anything */
331  if (!altar->other_arch)
332  return;
333 
334  /* hmm. what happend depends on pl's current god, level, etc */
335  if (!pl_god) { /*new convert */
336  become_follower(pl, &altar->other_arch->clone);
337  return;
338  }
339 
340  if (!strcmp(pl_god->name, altar->other_arch->clone.name)) {
341  /* pray at your gods altar */
342  int bonus = (pl->stats.Wis+skill->level)/10;
343 
344  /* we can get neg grace up faster */
345  if (pl->stats.grace < 0)
346  pl->stats.grace += (bonus > -1*(pl->stats.grace/10) ? bonus : -1*(pl->stats.grace/10));
347  /* we can super-charge grace to 2x max */
348  if (pl->stats.grace < (2*pl->stats.maxgrace)) {
349  pl->stats.grace += bonus/2;
350  }
351  if (pl->stats.grace > (2*pl->stats.maxgrace)) {
352  pl->stats.grace = (2*pl->stats.maxgrace);
353  }
354 
355  /* Every once in a while, the god decides to checkup on their
356  * follower, and may intervene to help them out.
357  */
358  bonus = MAX(1, bonus+MAX(pl->stats.luck, -3)); /* -- DAMN -- */
359 
360  if (((random_roll(0, 399, pl, PREFER_LOW))-bonus) < 0)
361  god_intervention(pl, pl_god, skill, altar);
362  } else { /* praying to another god! */
363  int angry = 1;
364 
365  /* I believe the logic for detecting opposing gods was completely
366  * broken - I think it should work now. altar->other_arch
367  * points to the god of this altar (which we have
368  * already verified is non null). pl_god->other_arch
369  * is the opposing god - we need to verify that exists before
370  * using its values.
371  */
372  if (pl_god->other_arch
373  && (altar->other_arch->name == pl_god->other_arch->name)) {
374  angry = 2;
375  if (random_roll(0, skill->level+2, pl, PREFER_LOW)-5 > 0) {
376  object *tmp;
377 
378  /* you really screwed up */
379  angry = 3;
381  "Foul Priest! %s punishes you!",
382  pl_god->name);
384  cast_magic_storm(pl, tmp, pl_god->level+20);
385  } else
387  "Foolish heretic! %s is livid!",
388  pl_god->name);
389  } else
391  "Heretic! %s is angered!",
392  pl_god->name);
393 
394  if (try_leave_cult(pl, skill, angry)) {
395  become_follower(pl, &altar->other_arch->clone);
396  } else {
397  /* toss this player off the altar. He can try again. */
399  "A divine force pushes you off the altar.");
400 
401  move_player(pl, absdir(pl->facing+4)); /* back him off the way he came. */
402  }
403  }
404 }
405 
416 static void remove_special_prayers(object *op, const object *god) {
417  treasure *tr;
418  int remove = 0;
419 
420  if (god->randomitems == NULL) {
421  LOG(llevError, "BUG: remove_special_prayers(): god %s without randomitems\n", god->name);
422  return;
423  }
424 
425 
426  /* Outer loop iterates over all special prayer marks */
427  FOR_INV_PREPARE(op, tmp) {
428  /* we mark special prayers with the STARTEQUIP flag, so if it isn't
429  * in that category, not something we need to worry about.
430  */
431  if (tmp->type != SPELL || !QUERY_FLAG(tmp, FLAG_STARTEQUIP))
432  continue;
433 
434  /* Inner loop tries to find the special prayer in the god's treasure
435  * list. We default that the spell should not be removed.
436  */
437  remove = 0;
438  for (tr = god->randomitems->items; tr; tr = tr->next) {
439  if (tr->item == NULL)
440  continue;
441 
442  /* Basically, see if the matching spell is granted by this god. */
443 
444  if (tr->item->clone.type == SPELL && tr->item->clone.name == tmp->name) {
445  remove = 1;
446  break;
447  }
448  }
449  if (remove) {
450  /* just do the work of removing the spell ourselves - we already
451  * know that the player knows the spell
452  */
455  "You lose knowledge of %s.",
456  tmp->name);
457  player_unready_range_ob(op->contr, tmp);
458  object_remove(tmp);
460  }
461  } FOR_INV_FINISH();
462 }
463 
479 int become_follower(object *op, const object *new_god) {
480  /* take away any special god-characteristic items. */
481  FOR_INV_PREPARE(op, item) {
482  /* remove all invisible startequip items which are
483  * not skill, exp or force
484  */
485  if (QUERY_FLAG(item, FLAG_STARTEQUIP)
486  && item->invisible
487  && (item->type != SKILL)
488  && (item->type != FORCE)
489  && (item->type != SPELL)) {
490  player_unready_range_ob(op->contr, item);
491  object_remove(item);
493  }
494  } FOR_INV_FINISH();
495 
496  /* remove any items given by the old god */
497  const object *old_god = find_god(determine_god(op));
498  if (old_god) {
499  /* Changed to use the new "divine_giver_name" key_value
500  * so it can reliably delete enchanted items. Now it loops
501  * through the player's inventory, instead of the god's
502  * treasure list.
503  */
504  follower_remove_given_items(op, op, old_god);
505  remove_special_prayers(op, old_god);
506  }
507 
508  if (!op || !new_god)
509  return 0;
510 
511  if (op->race && new_god->slaying && strstr(op->race, new_god->slaying)) {
513  "Fool! %s detests your kind!",
514  new_god->name);
515  if (random_roll(0, op->level-1, op, PREFER_LOW)-5 > 0) {
516  object *tmp = create_archetype(LOOSE_MANA);
517  cast_magic_storm(op, tmp, new_god->level+10);
518  }
519  return 0;
520  }
521 
522  /* give the player any special god-characteristic-items. */
523  for (treasure *tr = new_god->randomitems->items; tr != NULL; tr = tr->next) {
524  if (tr->item
525  && tr->item->clone.invisible
526  && tr->item->clone.type != SPELLBOOK
527  && tr->item->clone.type != BOOK
528  && tr->item->clone.type != SPELL)
529  god_gives_present(op, new_god, tr);
530  }
531 
533  "You become a follower of %s!",
534  new_god->name);
535 
536  object *skop = object_find_by_type_subtype(op, SKILL, SK_PRAYING);
537  /* Player has no skill - give them the skill */
538  if (!skop) {
539  /* The archetype should always be defined - if we crash here because it doesn't,
540  * things are really messed up anyways.
541  */
543  link_player_skills(op);
544  }
545 
546  int sk_applied = QUERY_FLAG(skop, FLAG_APPLIED); /* save skill status */
547 
548  int undeadified = 0; /* Turns to true if changing god can changes the undead
549  * status of the player.*/
550  /* Clear the "undead" status. We also need to force a call to change_abil,
551  * so I set undeadified for that.
552  * - gros, 21th July 2006.
553  */
554  if ((old_god) && (QUERY_FLAG(old_god, FLAG_UNDEAD))) {
555  CLEAR_FLAG(skop, FLAG_UNDEAD);
556  undeadified = 1;
557  }
558 
559  if (skop->title) { /* get rid of old god */
561  "%s's blessing is withdrawn from you.",
562  skop->title);
563 
564  /* The point of this is to really show what abilities the player just lost */
565  if (sk_applied || undeadified) {
566  CLEAR_FLAG(skop, FLAG_APPLIED);
567  (void)change_abil(op, skop);
568  }
569  free_string(skop->title);
570  }
571 
572  /* now change to the new gods attributes to exp_obj */
573  skop->title = add_string(new_god->name);
574  skop->path_attuned = new_god->path_attuned;
575  skop->path_repelled = new_god->path_repelled;
576  skop->path_denied = new_god->path_denied;
577  /* copy god's resistances */
578  memcpy(skop->resist, new_god->resist, sizeof(new_god->resist));
579 
580  /* make sure that certain immunities do NOT get passed
581  * to the follower!
582  */
583  for (int i = 0; i < NROFATTACKS; i++)
584  if (skop->resist[i] > 30
585  && (i == ATNR_FIRE || i == ATNR_COLD || i == ATNR_ELECTRICITY || i == ATNR_POISON))
586  skop->resist[i] = 30;
587 
588  skop->stats.hp = (int16_t)new_god->last_heal;
589  skop->stats.sp = (int16_t)new_god->last_sp;
590  skop->stats.grace = (int16_t)new_god->last_grace;
591  skop->stats.food = (int16_t)new_god->last_eat;
592  skop->stats.luck = (int8_t)new_god->stats.luck;
593  /* gods may pass on certain flag properties */
594  update_priest_flag(new_god, skop, FLAG_SEE_IN_DARK);
595  update_priest_flag(new_god, skop, FLAG_REFL_SPELL);
596  update_priest_flag(new_god, skop, FLAG_REFL_MISSILE);
597  update_priest_flag(new_god, skop, FLAG_STEALTH);
598  update_priest_flag(new_god, skop, FLAG_MAKE_INVIS);
599  update_priest_flag(new_god, skop, FLAG_UNDEAD);
600  update_priest_flag(new_god, skop, FLAG_BLIND);
601  update_priest_flag(new_god, skop, FLAG_XRAYS); /* better have this if blind! */
602  update_priest_flag(new_god, skop, FLAG_USE_WEAPON);
603  update_priest_flag(new_god, skop, FLAG_USE_ARMOUR);
604  update_priest_flag(new_god, skop, FLAG_USE_SHIELD);
605 
607  "You are bathed in %s's aura.",
608  new_god->name);
609 
610  /* Weapon/armour use are special...handle flag toggles here as this can
611  * only happen when gods are worshipped and if the new priest could
612  * have used armour/weapons in the first place.
613  *
614  * This also can happen for monks which cannot use weapons. In this case
615  * do not allow to use weapons even if the god otherwise would allow it.
616  */
617  if (!object_present_in_ob_by_name(FORCE, "no weapon force", op)) {
618  if (worship_forbids_use(op, skop, FLAG_USE_WEAPON, "weapons"))
619  stop_using_item(op, WEAPON, 2);
620  }
621  update_priest_flag(new_god, skop, FLAG_USE_ARMOUR);
622 
623  if (worship_forbids_use(op, skop, FLAG_USE_ARMOUR, "armour")) {
624  stop_using_item(op, ARMOUR, 1);
625  stop_using_item(op, HELMET, 1);
626  stop_using_item(op, BOOTS, 1);
627  stop_using_item(op, GLOVES, 1);
628  }
629 
630  if (worship_forbids_use(op, skop, FLAG_USE_SHIELD, "shield"))
631  stop_using_item(op, SHIELD, 1);
632 
633  SET_FLAG(skop, FLAG_APPLIED);
634  (void)change_abil(op, skop);
635 
636  /* return to previous skill status */
637  if (!sk_applied)
638  CLEAR_FLAG(skop, FLAG_APPLIED);
639 
640  return 1;
641 }
642 
656 static int worship_forbids_use(object *op, object *exp_obj, uint32_t flag, const char *string) {
657  if (QUERY_FLAG(&op->arch->clone, flag)) {
658  if (QUERY_FLAG(op, flag) != QUERY_FLAG(exp_obj, flag)) {
659  update_priest_flag(exp_obj, op, flag);
660  if (QUERY_FLAG(op, flag))
662  "You may use %s again.",
663  string);
664  else {
666  "You are forbidden to use %s.",
667  string);
668  return 1;
669  }
670  }
671  }
672  return 0;
673 }
674 
686 static void stop_using_item(object *op, int type, int number) {
687  FOR_INV_PREPARE(op, tmp)
688  if (tmp->type == type && QUERY_FLAG(tmp, FLAG_APPLIED)) {
690  number--;
691  if (number <= 0)
692  break;
693  }
694  FOR_INV_FINISH();
695 }
696 
709 static void update_priest_flag(const object *god, object *exp_ob, uint32_t flag) {
710  if (QUERY_FLAG(god, flag) && !QUERY_FLAG(exp_ob, flag))
711  SET_FLAG(exp_ob, flag);
712  else if (QUERY_FLAG(exp_ob, flag) && !QUERY_FLAG(god, flag)) {
713  /* When this is called with the exp_ob set to the player,
714  * this check is broken, because most all players arch
715  * allow use of weapons. I'm not actually sure why this
716  * check is here - I guess if you had a case where the
717  * value in the archetype (wisdom) should over ride the restrictions
718  * the god places on it, this may make sense. But I don't think
719  * there is any case like that.
720  */
721 
722 /* if (!(QUERY_FLAG(&(exp_ob->arch->clone), flag)))*/
723  CLEAR_FLAG(exp_ob, flag);
724  }
725 }
726 
727 
741 archetype *determine_holy_arch(const object *god, const char *type) {
742  treasure *tr;
743  int count;
744  object *item;
745 
746  if (!god || !god->randomitems) {
747  LOG(llevError, "BUG: determine_holy_arch(): no god or god without randomitems\n");
748  return NULL;
749  }
750 
751  count = 0;
752  for (tr = god->randomitems->items; tr != NULL; tr = tr->next) {
753  if (!tr->item)
754  continue;
755  item = &tr->item->clone;
756  if (item->type == BOOK && item->invisible && item->name == type)
757  count++;
758  }
759  if (count == 0) {
760  return NULL;
761  }
762 
763  count = rndm(1, count);
764 
765  for (tr = god->randomitems->items; tr != NULL; tr = tr->next) {
766  if (!tr->item)
767  continue;
768  item = &tr->item->clone;
769  if (item->type == BOOK && item->invisible && item->name == type) {
770  count--;
771  if (count == 0)
772  return item->other_arch;
773  }
774  }
775 
776  return NULL;
777 }
778 
789 static int god_removes_curse(object *op, int remove_damnation) {
790  int success = 0;
791 
792  FOR_INV_PREPARE(op, tmp) {
793  if (tmp->invisible)
794  continue;
795  if (QUERY_FLAG(tmp, FLAG_DAMNED) && !remove_damnation)
796  continue;
797  if (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)) {
798  success = 1;
799  CLEAR_FLAG(tmp, FLAG_DAMNED);
800  CLEAR_FLAG(tmp, FLAG_CURSED);
802  if (op->type == PLAYER)
803  esrv_update_item(UPD_FLAGS, op, tmp);
804  }
805  } FOR_INV_FINISH();
806 
807  if (success)
809  "You feel like someone is helping you.");
810  return success;
811 }
812 
822 static int follower_level_to_enchantments(int level, int difficulty) {
823  if (difficulty < 1) {
824  LOG(llevError, "follower_level_to_enchantments(): difficulty %d is invalid\n", difficulty);
825  return 0;
826  }
827 
828  if (level <= 20)
829  return level/difficulty;
830  if (level <= 40)
831  return (20+(level-20)/2)/difficulty;
832  return (30+(level-40)/4)/difficulty;
833 }
834 
853 static int improve_weapon_magic(object *op, object *tr, object *weapon, object *skill) {
854  int tmp = follower_level_to_enchantments(skill->level, tr->level);
855 
856  if (weapon->magic < tmp) {
858  "A phosphorescent glow envelops your weapon!");
859  weapon->magic++;
860  if (op->type == PLAYER)
861  esrv_update_item(UPD_NAME, op, weapon);
862  weapon->item_power++;
863  return 1;
864  }
865 
866  return 0;
867 }
868 
886 static int god_enchants_weapon(object *op, const object *god, object *tr, object *skill) {
887  char buf[MAX_BUF];
888  object *weapon;
889  uint32_t attacktype;
890 
891  weapon = object_find_by_type_applied(op, WEAPON);
892  if (weapon == NULL)
893  weapon = object_find_by_type_applied(op, BOW);
894  if (weapon == NULL || god_examines_item(god, weapon) <= 0)
895  return 0;
896 
897  if (weapon->item_power >= MAX_WEAPON_ITEM_POWER) {
899  "%s considers your %s is not worthy to be enchanted any more.",
900  god->name,
901  weapon->name);
902  return 0;
903  }
904 
905  /* If personalized_blessings is activated, then the god's name is
906  * associated with the weapon, as well as the owner (the one who blesses it),
907  * and a "weapon willpower", which is equivalent to the owner's experience
908  * in praying when the weapon is blessed.
909  * Those values are used later, when another player attempts to wield the
910  * weapon - nasty things may happen to those who do not deserve to use it ! :)
911  */
913  const char *divine_owner = object_get_value(weapon, "divine_blessing_name");
914  const char *owner = object_get_value(weapon, "item_owner");
915  object *skillop = NULL;
916 
917  if (divine_owner != NULL && strcmp(divine_owner, god->name) != 0) {
918  /* Huho... Another god already blessed this one ! */
920  "Your %s already belongs to %s !",
921  weapon->name, divine_owner);
922  return 0;
923  }
924 
925  if ((owner != NULL) && (strcmp(owner, op->name) != 0)) {
926  /* Maybe the weapon itself will not agree ? */
928  "The %s is not yours, and is magically protected against such changes !",
929  weapon->name);
930  return 0;
931  }
932  skillop = find_skill_by_number(op, SK_PRAYING);
933  if (skillop == NULL) {
934  LOG(llevError, "god_enchants_weapon: no praying skill object found ?!\n");
935  snprintf(buf, sizeof(buf), "%d", 1);
936  } else
937  snprintf(buf, sizeof(buf), "%"FMT64, skillop->stats.exp);
938  object_set_value(weapon, "divine_blessing_name", god->name, TRUE);
939  object_set_value(weapon, "item_owner", op->name, TRUE);
940  object_set_value(weapon, "item_willpower", buf, TRUE);
941  }
942 
943  /* First give it a title, so other gods won't touch it */
944  if (!weapon->title) {
945  snprintf(buf, sizeof(buf), "of %s", god->name);
946  weapon->title = add_string(buf);
947  if (op->type == PLAYER)
948  esrv_update_item(UPD_NAME, op, weapon);
950  "Your weapon quivers as if struck!");
951  }
952 
953  /* Allow the weapon to slay enemies */
954  if (!weapon->slaying && god->slaying) {
955  weapon->slaying = add_string(god->slaying);
957  "Your %s now hungers to slay enemies of your god!",
958  weapon->name);
959  weapon->item_power++;
960  return 1;
961  }
962 
963  /* Add the gods attacktype */
964  attacktype = (weapon->attacktype == 0) ? AT_PHYSICAL : weapon->attacktype;
965  if ((attacktype&god->attacktype) != god->attacktype) {
967  "Your weapon suddenly glows!");
968  weapon->attacktype = attacktype|god->attacktype;
969  weapon->item_power++;
970  return 1;
971  }
972 
973  /* Higher magic value */
974  return improve_weapon_magic(op, tr, weapon, skill);
975 }
976 
992 static void god_intervention(object *op, const object *god, object *skill, object *altar) {
993  treasure *tr;
994 
995  if (!god || !god->randomitems) {
996  LOG(llevError, "BUG: god_intervention(): no god or god without randomitems\n");
997  return;
998  }
999 
1000  /* lets do some checks of whether we are kosher with our god */
1001  if (god_examines_priest(op, god) < 0)
1002  return;
1003 
1005  "You feel a holy presence!");
1006 
1007  if (altar->anim_suffix != NULL)
1008  apply_anim_suffix(altar, altar->anim_suffix);
1009 
1010  for (tr = god->randomitems->items; tr != NULL; tr = tr->next) {
1011  object *item;
1012 
1013  if (tr->chance <= random_roll(0, 99, op, PREFER_HIGH))
1014  continue;
1015 
1016  /* Treasurelist - generate some treasure for the follower */
1017  if (tr->name) {
1018  treasurelist *tl = find_treasurelist(tr->name);
1019  if (tl == NULL)
1020  continue;
1021 
1023  "Something appears before your eyes. You catch it before it falls to the ground.");
1024 
1026  return;
1027  }
1028 
1029  if (!tr->item) {
1030  LOG(llevError, "BUG: empty entry in %s's treasure list\n", god->name);
1031  continue;
1032  }
1033  item = &tr->item->clone;
1034 
1035  /* Grace limit */
1036  if (item->type == BOOK && item->invisible
1037  && strcmp(item->name, "grace limit") == 0) {
1038  if (op->stats.grace < item->stats.grace
1039  || op->stats.grace < op->stats.maxgrace) {
1040  object *tmp;
1041 
1042  /* Follower lacks the required grace for the following
1043  * treasure list items. */
1044 
1046  cast_change_ability(op, op, tmp, 0, 1);
1048  return;
1049  }
1050  continue;
1051  }
1052 
1053  /* Restore grace */
1054  if (item->type == BOOK && item->invisible
1055  && strcmp(item->name, "restore grace") == 0) {
1056  if (op->stats.grace >= 0)
1057  continue;
1058  op->stats.grace = random_roll(0, 9, op, PREFER_HIGH);
1060  "You are returned to a state of grace.");
1061  return;
1062  }
1063 
1064  /* Heal damage */
1065  if (item->type == BOOK && item->invisible
1066  && strcmp(item->name, "restore hitpoints") == 0) {
1067  if (op->stats.hp >= op->stats.maxhp)
1068  continue;
1070  "A white light surrounds and heals you!");
1071  op->stats.hp = op->stats.maxhp;
1072  return;
1073  }
1074 
1075  /* Restore spellpoints */
1076  if (item->type == BOOK
1077  && item->invisible
1078  && strcmp(item->name, "restore spellpoints") == 0) {
1079  int max = op->stats.maxsp*(item->stats.maxsp/100.0);
1080  /* Restore to 50 .. 100%, if sp < 50% */
1081  int new_sp = random_roll(1000, 1999, op, PREFER_HIGH)/2000.0*max;
1082  if (op->stats.sp >= max/2)
1083  continue;
1085  "A blue lightning strikes your head but doesn't hurt you!");
1086  op->stats.sp = new_sp;
1087  }
1088 
1089  /* Various heal spells */
1090  if (item->type == BOOK && item->invisible
1091  && strcmp(item->name, "heal spell") == 0) {
1092  object *tmp;
1093  int success;
1094 
1096 
1097  success = cast_heal(op, op, tmp, 0);
1099  if (success)
1100  return;
1101 
1102  continue;
1103  }
1104 
1105  /* Remove curse */
1106  if (item->type == BOOK && item->invisible
1107  && strcmp(item->name, "remove curse") == 0) {
1108  if (god_removes_curse(op, 0))
1109  return;
1110 
1111  continue;
1112  }
1113 
1114  /* Remove damnation */
1115  if (item->type == BOOK && item->invisible
1116  && strcmp(item->name, "remove damnation") == 0) {
1117  if (god_removes_curse(op, 1))
1118  return;
1119 
1120  continue;
1121  }
1122 
1123  /* Heal depletion */
1124  if (item->type == BOOK && item->invisible
1125  && strcmp(item->name, "heal depletion") == 0) {
1126  if (remove_depletion(op, -1)) {
1128  "Shimmering light surrounds and restores you!");
1129  }
1130  }
1131 
1132  /* Voices */
1133  if (item->type == BOOK && item->invisible
1134  && strcmp(item->name, "voice_behind") == 0) {
1136  "You hear a voice from behind you, but you don't dare to "
1137  "turn around:");
1139  item->msg);
1140  return;
1141  }
1142 
1143  /* Messages */
1144  if (item->type == BOOK && item->invisible
1145  && strcmp(item->name, "message") == 0) {
1147  item->msg);
1148  return;
1149  }
1150 
1151  /* Enchant weapon */
1152  if (item->type == BOOK && item->invisible
1153  && strcmp(item->name, "enchant weapon") == 0) {
1154  if (god_enchants_weapon(op, god, item, skill))
1155  return;
1156 
1157  continue;
1158  }
1159 
1160  /* Spellbooks - works correctly only for prayers */
1161  if (item->type == SPELL) {
1162  if (check_spell_known(op, item->name))
1163  continue;
1164  if (item->level > skill->level)
1165  continue;
1166 
1168  "%s grants you use of a special prayer!",
1169  god->name);
1170  do_learn_spell(op, item, 1);
1171  return;
1172  }
1173 
1174  /* Other gifts */
1175  if (!item->invisible) {
1176  if (god_gives_present(op, god, tr))
1177  return;
1178 
1179  continue;
1180  }
1181  /* else ignore it */
1182  }
1183 
1185  "You feel rapture.");
1186 }
1187 
1200 static int god_examines_priest(object *op, const object *god) {
1201  int reaction = 1;
1202  object *skop;
1203 
1204  FOR_INV_PREPARE(op, item)
1205  if (QUERY_FLAG(item, FLAG_APPLIED)) {
1206  reaction += god_examines_item(god, item)*(item->magic ? abs(item->magic) : 1);
1207  }
1208  FOR_INV_FINISH();
1209 
1210  /* well, well. Looks like we screwed up. Time for god's revenge */
1211  if (reaction < 0) {
1212  int loss = 10000000;
1213  int angry = abs(reaction);
1214 
1216  if (skop)
1217  loss = 0.05*(float)skop->stats.exp;
1218  change_exp(op, -random_roll(0, loss*angry-1, op, PREFER_LOW), skop ? skop->skill : "none", SK_SUBTRACT_SKILL_EXP);
1219  if (random_roll(0, angry, op, PREFER_LOW)) {
1220  object *tmp = create_archetype(LOOSE_MANA);
1221 
1222  cast_magic_storm(op, tmp, op->level+(angry*3));
1223  }
1225  "%s becomes angry and punishes you!",
1226  god->name);
1227  }
1228  return reaction;
1229 }
1230 
1247 static int god_examines_item(const object *god, object *item) {
1248  char buf[MAX_BUF];
1249 
1250  if (!god || !item)
1251  return 0;
1252 
1253  if (!item->title)
1254  return 1; /* unclaimed item are ok */
1255 
1256  snprintf(buf, sizeof(buf), "of %s", god->name);
1257  if (!strcmp(item->title, buf))
1258  return 1; /* belongs to that God */
1259 
1260  if (god->title) { /* check if we have any enemy blessed item*/
1261  snprintf(buf, sizeof(buf), "of %s", god->title);
1262  if (!strcmp(item->title, buf)) {
1263  if (item->env) {
1264  char name[MAX_BUF];
1265 
1266  query_name(item, name, MAX_BUF);
1268  "Heretic! You are using %s!",
1269  name);
1270  }
1271  return -1;
1272  }
1273  }
1274  return 0; /* item is sacred to a non-enemy god/or is otherwise magical */
1275 }
1276 
1287 static const char *get_god_for_race(const char *race) {
1288  godlink *gl = first_god;
1289  const char *godname = NULL;
1290 
1291  if (race == NULL)
1292  return NULL;
1293  while (gl) {
1294  if (gl->arch->clone.race && !strcasecmp(gl->arch->clone.race, race)) {
1295  godname = gl->name;
1296  break;
1297  }
1298  gl = gl->next;
1299  }
1300  return godname;
1301 }
1302 
1314 int tailor_god_spell(object *spellop, object *caster) {
1315  const object *god = find_god(determine_god(caster));
1316  int caster_is_spell = 0;
1317 
1318  if (caster->type == SPELL_EFFECT || caster->type == SPELL)
1319  caster_is_spell = 1;
1320 
1321  /* if caster is a rune or the like, it doesn't worship anything. However,
1322  * if this object is owned by someone, then the god that they worship
1323  * is relevant, so use that.
1324  */
1325  if (!god && object_get_owner(caster))
1326  god = find_god(determine_god(object_get_owner(caster)));
1327 
1328  if (!god || (spellop->attacktype&AT_HOLYWORD && !god->race)) {
1329  if (!caster_is_spell)
1331  "This prayer is useless unless you worship an appropriate god");
1332  else
1333  LOG(llevError, "BUG: tailor_god_spell(): no god\n");
1334  object_free_drop_inventory(spellop);
1335  return 0;
1336  }
1337 
1338  /* either holy word or godpower attacks will set the slaying field */
1339  if (spellop->attacktype&AT_HOLYWORD || spellop->attacktype&AT_GODPOWER) {
1340  if (spellop->slaying) {
1341  free_string(spellop->slaying);
1342  spellop->slaying = NULL;
1343  }
1344  if (!caster_is_spell)
1345  spellop->slaying = god->slaying ? add_string(god->slaying) : NULL;
1346  else if (caster->slaying)
1347  spellop->slaying = add_string(caster->slaying);
1348  }
1349 
1350  /* only the godpower attacktype adds the god's attack onto the spell */
1351  if (spellop->attacktype&AT_GODPOWER)
1352  spellop->attacktype = spellop->attacktype|god->attacktype;
1353 
1354  /* tack on the god's name to the spell */
1355  if (spellop->attacktype&AT_HOLYWORD || spellop->attacktype&AT_GODPOWER) {
1356  if (spellop->title)
1357  free_string(spellop->title);
1358  spellop->title = add_string(god->name);
1359  if (spellop->title) {
1360  char buf[MAX_BUF];
1361 
1362  snprintf(buf, sizeof(buf), "%s of %s", spellop->name, spellop->title);
1363  FREE_AND_COPY(spellop->name, buf);
1364  FREE_AND_COPY(spellop->name_pl, buf);
1365  }
1366  }
1367 
1368  return 1;
1369 }
int64_t random_roll64(int64_t min, int64_t max, const object *op, int goodbad)
Definition: utils.c:77
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:315
#define AP_UNAPPLY
Definition: define.h:613
#define AT_HOLYWORD
Definition: attack.h:97
#define MSG_TYPE_SKILL_PRAY
Definition: newclient.h:586
Definition: player.h:92
#define FLAG_SEE_IN_DARK
Definition: define.h:338
int apply_special(object *who, object *op, int aflags)
Definition: apply.c:1078
#define MSG_TYPE_ITEM_CHANGE
Definition: newclient.h:640
#define FLAG_DAMNED
Definition: define.h:318
static int improve_weapon_magic(object *op, object *tr, object *weapon, object *skill)
Definition: gods.c:853
#define MSG_TYPE_ATTRIBUTE_GOD
Definition: newclient.h:570
#define MSG_TYPE_ITEM
Definition: newclient.h:388
void object_free(object *ob, int flags)
Definition: object.c:1409
int move_player(object *op, int dir)
Definition: player.c:3009
object * check_spell_known(object *op, const char *name)
Definition: spell_util.c:433
const char * race
Definition: object.h:318
archetype * determine_holy_arch(const object *god, const char *type)
Definition: gods.c:741
#define SET_FLAG(xyz, p)
Definition: define.h:223
object * object_find_by_type_applied(const object *who, int type)
Definition: object.c:3994
#define HOLY_POSSESSION
Definition: spells.h:168
#define MSG_TYPE_ITEM_ADD
Definition: newclient.h:639
int cast_change_ability(object *op, object *caster, object *spell_ob, int dir, int silent)
void link_player_skills(object *op)
Definition: player.c:279
#define NDI_WHITE
Definition: newclient.h:222
#define MSG_TYPE_ITEM_INFO
Definition: newclient.h:641
#define FLAG_USE_ARMOUR
Definition: define.h:296
static int lookup_god_by_name(const char *name)
Definition: gods.c:54
struct archt * arch
Definition: god.h:14
treasurelist * find_treasurelist(const char *name)
Definition: treasure.c:290
int32_t last_heal
Definition: object.h:357
int16_t maxgrace
Definition: living.h:45
void free_string(sstring str)
Definition: shstr.c:280
#define HUGE_BUF
Definition: define.h:37
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:342
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4266
struct treasureliststruct * randomitems
Definition: object.h:385
static void update_priest_flag(const object *god, object *exp_ob, uint32_t flag)
Definition: gods.c:709
object clone
Definition: object.h:470
int16_t invisible
Definition: object.h:360
#define PREFER_LOW
Definition: define.h:602
Definition: object.h:119
Definition: object.h:136
const char * slaying
Definition: object.h:319
int32_t last_sp
Definition: object.h:358
#define FLAG_STEALTH
Definition: define.h:313
Definition: object.h:109
Definition: god.h:12
#define FLAG_USE_WEAPON
Definition: define.h:297
int64_t exp
Definition: living.h:47
#define TRUE
Definition: compat.h:10
const char * name
Definition: god.h:13
object * create_archetype_by_object_name(const char *name)
Definition: arch.c:160
uint32_t path_attuned
Definition: object.h:343
#define MAX(x, y)
Definition: compat.h:20
#define MSG_TYPE_ITEM_REMOVE
Definition: newclient.h:638
int16_t sp
Definition: living.h:42
int become_follower(object *op, const object *new_god)
Definition: gods.c:479
uint32_t path_repelled
Definition: object.h:344
Definition: object.h:212
#define SCRIPT_FIX_ALL
Definition: global.h:361
struct archt * other_arch
Definition: object.h:413
int absdir(int d)
Definition: object.c:3654
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
Definition: object.h:465
Definition: object.h:220
struct archt * item
Definition: treasure.h:64
int16_t maxsp
Definition: living.h:43
#define MIN(x, y)
Definition: compat.h:17
int16_t hp
Definition: living.h:40
#define NDI_NAVY
Definition: newclient.h:223
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.c:488
#define LOOSE_MANA
Definition: spells.h:162
object * give_skill_by_name(object *op, const char *skill_name)
Definition: living.c:1752
int rndm(int min, int max)
Definition: utils.c:162
#define FLAG_UNDEAD
Definition: define.h:270
int object_set_value(object *op, const char *key, const char *value, int add_key)
Definition: object.c:4395
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
int change_abil(object *op, object *tmp)
Definition: living.c:394
const char * title
Definition: object.h:317
int16_t maxhp
Definition: living.h:41
#define AT_GODPOWER
Definition: attack.h:96
static void follower_remove_given_items(object *pl, object *op, const object *god)
Definition: gods.c:197
static int god_examines_priest(object *op, const object *god)
Definition: gods.c:1200
Definition: object.h:118
uint32_t path_denied
Definition: object.h:345
#define FLAG_ALIVE
Definition: define.h:230
#define AP_IGNORE_CURSE
Definition: define.h:619
#define FLAG_REFL_SPELL
Definition: define.h:275
Definition: object.h:114
const char * name_pl
Definition: object.h:315
object * create_archetype(const char *name)
Definition: arch.c:620
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2707
signed short int16_t
Definition: win32.h:160
int tailor_god_spell(object *spellop, object *caster)
Definition: gods.c:1314
const char * anim_suffix
Definition: object.h:316
int8_t Wis
Definition: living.h:36
static const char * get_god_for_race(const char *race)
Definition: gods.c:1287
#define FLAG_USE_SHIELD
Definition: define.h:237
#define snprintf
Definition: win32.h:46
#define MSG_TYPE_ATTRIBUTE
Definition: newclient.h:380
#define FOR_INV_FINISH()
Definition: define.h:714
#define FMT64
Definition: compat.h:12
const char * name
Definition: object.h:311
struct obj * env
Definition: object.h:293
const char * determine_god(object *op)
Definition: gods.c:106
int16_t last_grace
Definition: object.h:359
static int follower_has_similar_item(object *op, object *item)
Definition: gods.c:233
#define MAX_WEAPON_ITEM_POWER
Definition: define.h:475
const object * pntr_to_god_obj(godlink *godlnk)
Definition: holy.c:123
#define UPD_FLAGS
Definition: newclient.h:290
void do_learn_spell(object *op, object *spell, int special_prayer)
Definition: apply.c:391
struct pl * contr
Definition: object.h:276
int8_t item_power
Definition: object.h:362
static int god_examines_item(const object *god, object *item)
Definition: gods.c:1247
void player_unready_range_ob(player *pl, object *ob)
Definition: player.c:4449
#define FLAG_XRAYS
Definition: define.h:301
void cast_magic_storm(object *op, object *tmp, int lvl)
Definition: spell_effect.c:44
EXTERN godlink * first_god
Definition: global.h:124
static void stop_using_item(object *op, int type, int number)
Definition: gods.c:686
static bool try_leave_cult(object *pl, object *skill, int angry)
Definition: gods.c:296
int8_t luck
Definition: living.h:39
#define ATNR_ELECTRICITY
Definition: attack.h:52
#define ATNR_POISON
Definition: attack.h:59
#define AT_PHYSICAL
Definition: attack.h:76
unsigned __int64 uint64_t
Definition: win32.h:167
Definition: object.h:214
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
struct glnk * next
Definition: god.h:16
void change_exp(object *op, int64_t exp, const char *skill_name, int flag)
Definition: living.c:2126
void fix_generated_item(object *op, object *creator, int difficulty, int max_magic, int flags)
Definition: treasure.c:1108
static int god_removes_curse(object *op, int remove_damnation)
Definition: gods.c:789
#define MAX_BUF
Definition: define.h:35
static int god_gives_present(object *op, const object *god, treasure *tr)
Definition: gods.c:259
static int follower_level_to_enchantments(int level, int difficulty)
Definition: gods.c:822
struct treasurestruct * items
Definition: treasure.h:90
const char * skill
Definition: object.h:321
int32_t last_eat
Definition: object.h:356
int16_t resist[NROFATTACKS]
Definition: object.h:341
#define FLAG_KNOWN_CURSED
Definition: define.h:321
#define FLAG_CURSED
Definition: define.h:317
Definition: object.h:107
unsigned int uint32_t
Definition: win32.h:162
static void god_intervention(object *op, const object *god, object *skill, object *altar)
Definition: gods.c:992
Definition: object.h:135
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:4215
static int god_enchants_weapon(object *op, const object *god, object *tr, object *skill)
Definition: gods.c:886
uint8_t chance
Definition: treasure.h:70
#define PREFER_HIGH
Definition: define.h:601
uint32_t attacktype
Definition: object.h:342
#define FLAG_BLIND
Definition: define.h:337
int cast_heal(object *op, object *caster, object *spell, int dir)
#define FREE_AND_COPY(sv, nv)
Definition: global.h:211
int16_t grace
Definition: living.h:44
signed char int8_t
Definition: win32.h:158
Definition: object.h:143
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.c:136
#define EVENT_APPLY
Definition: plugin.h:64
living stats
Definition: object.h:368
struct archt * arch
Definition: object.h:412
uint8_t type
Definition: object.h:338
struct Settings settings
Definition: init.c:40
static void remove_special_prayers(object *op, const object *god)
Definition: gods.c:416
int remove_depletion(object *op, int level)
Definition: living.c:751
#define FLAG_APPLIED
Definition: define.h:235
#define NROFATTACKS
Definition: attack.h:17
void query_short_name(const object *op, char *buf, size_t size)
Definition: item.c:548
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
#define UPD_NAME
Definition: newclient.h:293
const char * msg
Definition: object.h:322
#define FLAG_MAKE_INVIS
Definition: define.h:329
#define FLAG_STARTEQUIP
Definition: define.h:268
sstring add_string(const char *str)
Definition: shstr.c:124
int strcasecmp(const char *s1, const char *s2)
Definition: porting.c:256
#define MSG_TYPE_SKILL
Definition: newclient.h:383
struct obj * inv
Definition: object.h:290
#define NDI_UNIQUE
Definition: newclient.h:245
void apply_anim_suffix(object *who, sstring suffix)
Definition: anim.c:319
Definition: object.h:213
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
static int same_string(const char *s1, const char *s2)
Definition: gods.c:175
#define ATNR_COLD
Definition: attack.h:53
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:623
#define SK_SUBTRACT_SKILL_EXP
Definition: skills.h:81
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:351
int random_roll(int min, int max, const object *op, int goodbad)
Definition: utils.c:42
Definition: object.h:120
struct treasurestruct * next
Definition: treasure.h:66
#define FLAG_REFL_MISSILE
Definition: define.h:273
int16_t level
Definition: object.h:351
int8_t facing
Definition: object.h:335
const char * name
Definition: treasure.h:65
int id
Definition: god.h:15
object * arch_to_object(archetype *at)
Definition: arch.c:571
object * object_get_owner(object *op)
Definition: object.c:590
int8_t magic
Definition: object.h:348
const char * name
Definition: object.h:466
void pray_at_altar(object *pl, object *altar, object *skill)
Definition: gods.c:323
object * object_present_in_ob_by_name(int type, const char *str, const object *op)
Definition: object.c:3056
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
void object_remove(object *op)
Definition: object.c:1666
const object * find_god(const char *name)
Definition: gods.c:80
uint8_t personalized_blessings
Definition: global.h:309
int32_t food
Definition: living.h:48
Definition: object.h:224
#define ATNR_FIRE
Definition: attack.h:51
static int worship_forbids_use(object *op, object *exp_obj, uint32_t flag, const char *string)
Definition: gods.c:656