ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/gods.c
Revision: 1.1.1.1 (vendor branch)
Committed: Fri Feb 3 07:14:31 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_03_15, UPSTREAM_2006_02_22, difficulty_fix_merge_060810_2300, UPSTREAM_2006_02_03
Branch point for: difficulty_fix
Changes since 1.1: +0 -0 lines
Log Message:
initial import

File Contents

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