ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.c
Revision: 1.1.1.2 (vendor branch)
Committed: Wed Feb 22 18:03:23 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_02_22
Changes since 1.1.1.1: +48 -17 lines
Log Message:
cvs -z7 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_02_22

File Contents

# User Rev Content
1 root 1.1 /*
2     * static char *rcsid_player_c =
3 elmex 1.1.1.2 * "$Id: player.c,v 1.193 2006/02/10 23:59:27 akirschbaum Exp $";
4 root 1.1 */
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 author can be reached via e-mail to crossfire-devel@real-time.com
27     */
28    
29     #include <global.h>
30     #ifndef WIN32 /* ---win32 remove headers */
31     #include <pwd.h>
32     #endif
33     #ifndef __CEXTRACT__
34     #include <sproto.h>
35     #endif
36     #include <sounds.h>
37     #include <living.h>
38     #include <object.h>
39     #include <spells.h>
40     #include <skills.h>
41     #include <newclient.h>
42    
43     player *find_player(const char *plname)
44     {
45     player *pl;
46     for(pl=first_player;pl!=NULL;pl=pl->next)
47     {
48     if(pl->ob != NULL && !strcmp(query_name(pl->ob),plname))
49     return pl;
50     };
51     return NULL;
52     }
53    
54     player* find_player_partial_name( const char* plname )
55     {
56     player* pl;
57     player* found = NULL;
58     size_t namelen = strlen( plname );
59     for ( pl = first_player; pl != NULL; pl = pl->next )
60     {
61     if ( strlen( pl->ob->name ) < namelen )
62     continue;
63    
64     if ( !strcmp( pl->ob->name, plname) )
65     return pl;
66    
67     if ( !strncasecmp( pl->ob->name, plname, namelen ) )
68     {
69     if ( found )
70     return NULL;
71    
72     found = pl;
73     }
74     }
75     return found;
76     }
77    
78 elmex 1.1.1.2 void display_motd(const object *op) {
79 root 1.1 char buf[MAX_BUF];
80     char motd[HUGE_BUF];
81     FILE *fp;
82     int comp;
83     int size;
84    
85     sprintf(buf, "%s/%s", settings.confdir, settings.motd);
86     if ((fp=open_and_uncompress(buf, 0, &comp)) == NULL) {
87     return;
88     }
89     motd[0]='\0';
90     size=0;
91     while (fgets(buf, MAX_BUF, fp) != NULL) {
92     if( *buf == '#')
93     continue;
94     strncat(motd+size,buf,HUGE_BUF-size);
95     size+=strlen(buf);
96     }
97     draw_ext_info(NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_MOTD, MSG_SUBTYPE_NONE, motd, NULL);
98     close_and_delete(fp, comp);
99     }
100    
101 elmex 1.1.1.2 void send_rules(const object *op) {
102 root 1.1 char buf[MAX_BUF];
103     char rules[HUGE_BUF];
104     FILE *fp;
105     int comp;
106     int size;
107    
108     sprintf(buf, "%s/%s", settings.confdir, settings.rules);
109     if ((fp=open_and_uncompress(buf, 0, &comp)) == NULL) {
110     return;
111     }
112     rules[0]='\0';
113     size=0;
114     while (fgets(buf, MAX_BUF, fp) != NULL) {
115     if( *buf == '#')
116     continue;
117     if (size + strlen(buf)>=HUGE_BUF)
118     {
119     LOG(llevDebug, "Warning, rules size is > %d bytes.\n", HUGE_BUF);
120     break;
121     }
122     strncat(rules+size,buf,HUGE_BUF-size);
123     size+=strlen(buf);
124     }
125     draw_ext_info(NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_RULES, rules, NULL);
126     close_and_delete(fp, comp);
127     }
128    
129 elmex 1.1.1.2 void send_news(const object *op) {
130 root 1.1 char buf[MAX_BUF];
131     char news[HUGE_BUF];
132     char subject[MAX_BUF];
133     FILE *fp;
134     int comp;
135     int size;
136    
137     sprintf(buf, "%s/%s", settings.confdir, settings.news);
138     if ((fp=open_and_uncompress(buf, 0, &comp)) == NULL)
139     return;
140     news[0]='\0';
141     subject[0]='\0';
142     size=0;
143     while (fgets(buf, MAX_BUF, fp) != NULL) {
144     if( *buf == '#')
145     continue;
146     if ( *buf =='%'){ /* send one news */
147     if (size>0)
148     draw_ext_info_format(NDI_UNIQUE | NDI_GREEN, 0, op,
149     MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS,
150     "!! informations: %s\n%s",
151     "%s\n%s",
152     subject, news); /*send previously read news*/
153     strcpy(subject,buf+1);
154     strip_endline(subject);
155     size=0;
156     news[0]='\0';
157     }
158     else{
159     if (size + strlen(buf)>=HUGE_BUF)
160     {
161     LOG(llevDebug, "Warning, one news item has size > %d bytes.\n", HUGE_BUF);
162     break;
163     }
164     strncat(news+size,buf,HUGE_BUF-size);
165     size+=strlen(buf);
166     }
167     }
168    
169     draw_ext_info_format(NDI_UNIQUE | NDI_GREEN, 0, op,
170     MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS,
171     "!! informations: %s\n%s\n",
172     "%s\n%s",
173     subject, news);
174     close_and_delete(fp, comp);
175     }
176    
177     int playername_ok(const char *cp) {
178     /* Don't allow - or _ as first character in the name */
179     if (*cp == '-' || *cp == '_') return 0;
180    
181     for(;*cp!='\0';cp++)
182     if(!((*cp>='a'&&*cp<='z')||(*cp>='A'&&*cp<='Z'))&&*cp!='-'&&*cp!='_')
183     return 0;
184     return 1;
185     }
186    
187     /* This no longer sets the player map. Also, it now updates
188     * all the pointers so the caller doesn't need to do that.
189     * Caller is responsible for setting the correct map.
190     */
191    
192     /* Redo this to do both get_player_ob and get_player.
193     * Hopefully this will be less bugfree and simpler.
194     * Returns the player structure. If 'p' is null,
195     * we create a new one. Otherwise, we recycle
196     * the one that is passed.
197     */
198     static player* get_player(player *p) {
199     object *op=arch_to_object(get_player_archetype(NULL));
200     int i;
201    
202     if (!p) {
203     player *tmp;
204    
205     p = (player *) malloc(sizeof(player));
206     if(p==NULL)
207     fatal(OUT_OF_MEMORY);
208    
209     /* This adds the player in the linked list. There is extra
210     * complexity here because we want to add the new player at the
211     * end of the list - there is in fact no compelling reason that
212     * that needs to be done except for things like output of
213     * 'who'.
214     */
215     tmp=first_player;
216     while(tmp!=NULL&&tmp->next!=NULL)
217     tmp=tmp->next;
218     if(tmp!=NULL)
219     tmp->next=p;
220     else
221     first_player=p;
222    
223     p->next = NULL;
224     }
225    
226     /* Clears basically the entire player structure except
227     * for next and socket.
228     */
229     memset((void*)((char*)p + offsetof(player, maplevel)), 0,
230     sizeof(player) - offsetof(player, maplevel));
231    
232     /* There are some elements we want initialized to non zero value -
233     * we deal with that below this point.
234     */
235     p->party=NULL;
236     p->outputs_sync=16; /* Every 2 seconds */
237     p->outputs_count=1; /* Keeps present behaviour */
238     p->unapply = unapply_nochoice;
239     p->Swap_First = -1;
240    
241     #ifdef AUTOSAVE
242     p->last_save_tick = 9999999;
243     #endif
244    
245     strcpy(p->savebed_map, first_map_path); /* Init. respawn position */
246    
247     op->contr=p; /* this aren't yet in archetype */
248     p->ob = op;
249     op->speed_left=0.5;
250     op->speed=1.0;
251     op->direction=5; /* So player faces south */
252     op->stats.wc=2;
253     op->run_away = 25; /* Then we panick... */
254    
255     roll_stats(op);
256     p->state=ST_ROLL_STAT;
257     clear_los(op);
258    
259     p->gen_sp_armour=10;
260     p->last_speed= -1;
261     p->shoottype=range_none;
262     p->bowtype=bow_normal;
263     p->petmode=pet_normal;
264     p->listening=9;
265     p->last_weapon_sp= -1;
266     p->peaceful=1; /* default peaceful */
267     p->do_los=1;
268     p->explore=0;
269     p->no_shout=0; /* default can shout */
270    
271     strncpy(p->title, op->arch->clone.name, sizeof(p->title)-1);
272     p->title[sizeof(p->title)-1] = '\0';
273     op->race = add_string (op->arch->clone.race);
274    
275     CLEAR_FLAG(op,FLAG_READY_SKILL);
276    
277     /* we need to clear these to -1 and not zero - otherwise,
278     * if a player quits and starts a new character, we wont
279     * send new values to the client, as things like exp start
280     * at zero.
281     */
282     for (i=0; i < NUM_SKILLS; i++) {
283     p->last_skill_exp[i] = -1;
284     p->last_skill_ob[i] = NULL;
285     }
286     for (i=0; i < NROFATTACKS; i++) {
287     p->last_resist[i] = -1;
288     }
289     p->last_stats.exp = -1;
290     p->last_weight = (uint32)-1;
291    
292     p->socket.update_look=0;
293     p->socket.look_position=0;
294     return p;
295     }
296    
297    
298     /* This loads the first map an puts the player on it. */
299     static void set_first_map(object *op)
300     {
301     strcpy(op->contr->maplevel, first_map_path);
302     op->x = -1;
303     op->y = -1;
304     enter_exit(op, NULL);
305     }
306    
307     /* Tries to add player on the connection passwd in ns.
308     * All we can really get in this is some settings like host and display
309     * mode.
310     */
311    
312     int add_player(NewSocket *ns) {
313     player *p;
314    
315     p=get_player(NULL);
316     memcpy(&p->socket, ns, sizeof(NewSocket));
317     p->socket.faces_sent = malloc(p->socket.faces_sent_len*sizeof(*p->socket.faces_sent));
318     if(p->socket.faces_sent == NULL)
319     fatal(OUT_OF_MEMORY);
320     memcpy(p->socket.faces_sent, ns->faces_sent, p->socket.faces_sent_len*sizeof(*p->socket.faces_sent));
321     /* Needed because the socket we just copied over needs to be cleared.
322     * Note that this can result in a client reset if there is partial data
323     * on the uncoming socket.
324     */
325     p->socket.inbuf.len = 0;
326     set_first_map(p->ob);
327    
328     CLEAR_FLAG(p->ob, FLAG_FRIENDLY);
329     add_friendly_object(p->ob);
330     send_rules(p->ob);
331     send_news(p->ob);
332     display_motd(p->ob);
333     get_name(p->ob);
334     return 0;
335     }
336    
337     /*
338     * get_player_archetype() return next player archetype from archetype
339     * list. Not very efficient routine, but used only creating new players.
340     * Note: there MUST be at least one player archetype!
341     */
342     archetype *get_player_archetype(archetype* at)
343     {
344     archetype *start = at;
345     for (;;) {
346     if (at==NULL || at->next==NULL)
347     at=first_archetype;
348     else
349     at=at->next;
350     if(at->clone.type==PLAYER)
351     return at;
352     if (at == start) {
353     LOG (llevError, "No Player archetypes\n");
354     exit (-1);
355     }
356     }
357     }
358    
359    
360     object *get_nearest_player(object *mon) {
361     object *op = NULL;
362     player *pl = NULL;
363     objectlink *ol;
364     unsigned lastdist;
365     rv_vector rv;
366    
367     for(ol=first_friendly_object,lastdist=1000;ol!=NULL;ol=ol->next) {
368     /* We should not find free objects on this friendly list, but it
369     * does periodically happen. Given that, lets deal with it.
370     * While unlikely, it is possible the next object on the friendly
371     * list is also free, so encapsulate this in a while loop.
372     */
373     while (QUERY_FLAG(ol->ob, FLAG_FREED) || !QUERY_FLAG(ol->ob, FLAG_FRIENDLY)) {
374     object *tmp=ol->ob;
375    
376     /* Can't do much more other than log the fact, because the object
377     * itself will have been cleared.
378     */
379     LOG(llevDebug,"get_nearest_player: Found free/non friendly object on friendly list\n");
380     ol = ol->next;
381     remove_friendly_object(tmp);
382     if (!ol) return op;
383     }
384    
385     /* Remove special check for player from this. First, it looks to cause
386     * some crashes (ol->ob->contr not set properly?), but secondly, a more
387     * complicated method of state checking would be needed in any case -
388     * as it was, a clever player could type quit, and the function would
389     * skip them over while waiting for confirmation. Remove
390     * on_same_map check, as can_detect_enemy also does this
391     */
392     if (!can_detect_enemy(mon,ol->ob,&rv))
393     continue;
394    
395     if(lastdist>rv.distance) {
396     op=ol->ob;
397     lastdist=rv.distance;
398     }
399     }
400     for (pl=first_player; pl != NULL; pl=pl->next) {
401     if (on_same_map(mon, pl->ob)&& can_detect_enemy(mon, pl->ob,&rv)) {
402    
403     if(lastdist>rv.distance) {
404     op=pl->ob;
405     lastdist=rv.distance;
406     }
407     }
408     }
409     #if 0
410     LOG(llevDebug,"get_nearest_player() finds player: %s\n",op?op->name:"(null)");
411     #endif
412     return op;
413     }
414    
415     /* I believe this can safely go to 2, 3 is questionable, 4 will likely
416     * result in a monster paths backtracking. It basically determines how large a
417     * detour a monster will take from the direction path when looking
418     * for a path to the player. The values are in the amount of direction
419     * the deviation is
420     */
421     #define DETOUR_AMOUNT 2
422    
423     /* This is used to prevent infinite loops. Consider a case where the
424     * player is in a chamber (with gate closed), and monsters are outside.
425     * with DETOUR_AMOUNT==2, the function will turn each corner, trying to
426     * find a path into the chamber. This is a good thing, but since there
427     * is no real path, it will just keep circling the chamber for
428     * ever (this could be a nice effect for monsters, but not for the function
429     * to get stuck in. I think for the monsters, if max is reached and
430     * we return the first direction the creature could move would result in the
431     * circling behaviour. Unfortunately, this function is also used to determined
432     * if the creature should cast a spell, so returning a direction in that case
433     * is probably not a good thing.
434     */
435     #define MAX_SPACES 50
436    
437    
438     /*
439     * Returns the direction to the player, if valid. Returns 0 otherwise.
440     * modified to verify there is a path to the player. Does this by stepping towards
441     * player and if path is blocked then see if blockage is close enough to player that
442     * direction to player is changed (ie zig or zag). Continue zig zag until either
443     * reach player or path is blocked. Thus, will only return true if there is a free
444     * path to player. Though path may not be a straight line. Note that it will find
445     * player hiding along a corridor at right angles to the corridor with the monster.
446     *
447     * Modified by MSW 2001-08-06 to handle tiled maps. Various notes:
448     * 1) With DETOUR_AMOUNT being 2, it should still go and find players hiding
449     * down corriders.
450     * 2) I think the old code was broken if the first direction the monster
451     * should move was blocked - the code would store the first direction without
452     * verifying that the player can actually move in that direction. The new
453     * code does not store anything in firstdir until we have verified that the
454     * monster can in fact move one space in that direction.
455     * 3) I'm not sure how good this code will be for moving multipart monsters,
456     * since only simple checks to blocked are being called, which could mean the monster
457     * is blocking itself.
458     */
459     int path_to_player(object *mon, object *pl, unsigned mindiff) {
460     rv_vector rv;
461     sint16 x,y;
462     int lastx,lasty,dir,i,diff, firstdir=0,lastdir, max=MAX_SPACES, mflags, blocked;
463     mapstruct *m ,*lastmap;
464    
465     get_rangevector(mon, pl, &rv, 0);
466    
467     if (rv.distance<mindiff) return 0;
468    
469     x=mon->x;
470     y=mon->y;
471     m=mon->map;
472     dir = rv.direction;
473     lastdir = firstdir = rv.direction; /* perhaps we stand next to pl, init firstdir too */
474     diff = FABS(rv.distance_x)>FABS(rv.distance_y)?FABS(rv.distance_x):FABS(rv.distance_y);
475     /* If we can't solve it within the search distance, return now. */
476     if (diff>max) return 0;
477     while (diff >1 && max>0) {
478     lastx = x;
479     lasty = y;
480     lastmap = m;
481     x = lastx + freearr_x[dir];
482     y = lasty + freearr_y[dir];
483    
484     mflags = get_map_flags(m, &m, x, y, &x, &y);
485     blocked = (mflags & P_OUT_OF_MAP) ? MOVE_ALL : GET_MAP_MOVE_BLOCK(m, x, y);
486    
487     /* Space is blocked - try changing direction a little */
488     if ((mflags & P_OUT_OF_MAP) || ((OB_TYPE_MOVE_BLOCK(mon, blocked))
489     && (m == mon->map && blocked_link(mon, m, x, y)))) {
490     /* recalculate direction from last good location. Possible
491     * we were not traversing ideal location before.
492     */
493     get_rangevector_from_mapcoord(lastmap, lastx, lasty, pl, &rv, 0);
494     if (rv.direction != dir) {
495     /* OK - says direction should be different - lets reset the
496     * the values so it will try again.
497     */
498     x = lastx;
499     y = lasty;
500     m = lastmap;
501     dir = firstdir = rv.direction;
502     } else {
503     /* direct path is blocked - try taking a side step to
504     * either the left or right.
505     * Note increase the values in the loop below to be
506     * more than -1/1 respectively will mean the monster takes
507     * bigger detour. Have to be careful about these values getting
508     * too big (3 or maybe 4 or higher) as the monster may just try
509     * stepping back and forth
510     */
511     for (i=-DETOUR_AMOUNT; i<=DETOUR_AMOUNT; i++) {
512     if (i==0) continue; /* already did this, so skip it */
513     /* Use lastdir here - otherwise,
514     * since the direction that the creature should move in
515     * may change, you could get infinite loops.
516     * ie, player is northwest, but monster can only
517     * move west, so it does that. It goes some distance,
518     * gets blocked, finds that it should move north,
519     * can't do that, but now finds it can move east, and
520     * gets back to its original point. lastdir contains
521     * the last direction the creature has successfully
522     * moved.
523     */
524    
525     x = lastx + freearr_x[absdir(lastdir+i)];
526     y = lasty + freearr_y[absdir(lastdir+i)];
527     m = lastmap;
528     mflags = get_map_flags(m, &m, x, y, &x, &y);
529     if (mflags & P_OUT_OF_MAP) continue;
530     blocked = GET_MAP_MOVE_BLOCK(m, x, y);
531     if (OB_TYPE_MOVE_BLOCK(mon, blocked)) continue;
532    
533     if (m == mon->map && blocked_link(mon, m, x, y)) break;
534     }
535     /* go through entire loop without finding a valid
536     * sidestep to take - thus, no valid path.
537     */
538     if (i==(DETOUR_AMOUNT+1))
539     return 0;
540     diff--;
541     lastdir=dir;
542     max--;
543     if (!firstdir) firstdir = dir+i;
544     } /* else check alternate directions */
545     } /* if blocked */
546     else {
547     /* we moved towards creature, so diff is less */
548     diff--;
549     max--;
550     lastdir=dir;
551     if (!firstdir) firstdir = dir;
552     }
553     if (diff<=1) {
554     /* Recalculate diff (distance) because we may not have actually
555     * headed toward player for entire distance.
556     */
557     get_rangevector_from_mapcoord(m, x, y, pl, &rv, 0);
558     diff = FABS(rv.distance_x)>FABS(rv.distance_y)?FABS(rv.distance_x):FABS(rv.distance_y);
559     }
560     if (diff>max) return 0;
561     }
562     /* If we reached the max, didn't find a direction in time */
563     if (!max) return 0;
564    
565     return firstdir;
566     }
567    
568     void give_initial_items(object *pl,treasurelist *items) {
569     object *op,*next=NULL;
570    
571     if(pl->randomitems!=NULL)
572     create_treasure(items,pl,GT_STARTEQUIP | GT_ONLY_GOOD,1,0);
573    
574     for (op=pl->inv; op; op=next) {
575     next = op->below;
576    
577     /* Forces get applied per default, unless they have the
578     * flag "neutral" set. Sorry but I can't think of a better way
579     */
580     if(op->type==FORCE && !QUERY_FLAG(op, FLAG_NEUTRAL))
581     SET_FLAG(op,FLAG_APPLIED);
582    
583     /* we never give weapons/armour if these cannot be used
584     * by this player due to race restrictions
585     */
586     if (pl->type == PLAYER) {
587     if ((!QUERY_FLAG(pl, FLAG_USE_ARMOUR) &&
588     (op->type == ARMOUR || op->type == BOOTS ||
589     op->type == CLOAK || op->type == HELMET ||
590     op->type == SHIELD || op->type == GLOVES ||
591     op->type == BRACERS || op->type == GIRDLE)) ||
592     (!QUERY_FLAG(pl, FLAG_USE_WEAPON) && op->type == WEAPON)) {
593     remove_ob (op);
594     free_object (op);
595     continue;
596     }
597     }
598    
599     /* This really needs to be better - we should really give
600     * a substitute spellbook. The problem is that we don't really
601     * have a good idea what to replace it with (need something like
602     * a first level treasurelist for each skill.)
603     * remove duplicate skills also
604     */
605     if(op->type==SPELLBOOK || op->type == SKILL) {
606     object *tmp;
607    
608     for (tmp=op->below; tmp; tmp=tmp->below)
609     if (tmp->type == op->type && tmp->name == op->name) break;
610    
611     if (tmp) {
612     remove_ob(op);
613     free_object(op);
614     LOG(llevError,"give_initial_items: Removing duplicate object %s\n",
615     tmp->name);
616     continue;
617     }
618     if (op->nrof > 1) op->nrof = 1;
619     }
620    
621     if (op->type == SPELLBOOK && op->inv) {
622     CLEAR_FLAG(op->inv, FLAG_STARTEQUIP);
623     }
624    
625     /* Give starting characters identified, uncursed, and undamned
626     * items. Just don't identify gold or silver, or it won't be
627     * merged properly.
628     */
629     if (need_identify(op)) {
630     SET_FLAG(op, FLAG_IDENTIFIED);
631     CLEAR_FLAG(op, FLAG_CURSED);
632     CLEAR_FLAG(op, FLAG_DAMNED);
633     }
634     if(op->type==SPELL) {
635     remove_ob(op);
636     free_object(op);
637     continue;
638     }
639 elmex 1.1.1.2 else if(op->type==SKILL) {
640 root 1.1 SET_FLAG(op, FLAG_CAN_USE_SKILL);
641     op->stats.exp = 0;
642     op->level = 1;
643     }
644 elmex 1.1.1.2 /* lock all 'normal items by default */
645     else SET_FLAG(op, FLAG_INV_LOCKED);
646 root 1.1 } /* for loop of objects in player inv */
647    
648     /* Need to set up the skill pointers */
649     link_player_skills(pl);
650     }
651    
652     void get_name(object *op) {
653     op->contr->write_buf[0]='\0';
654     op->contr->state=ST_GET_NAME;
655     send_query(&op->contr->socket,0,"What is your name?\n:");
656     }
657    
658     void get_password(object *op) {
659     op->contr->write_buf[0]='\0';
660     op->contr->state=ST_GET_PASSWORD;
661     send_query(&op->contr->socket,CS_QUERY_HIDEINPUT, "What is your password?\n:");
662     }
663    
664     void play_again(object *op)
665     {
666     op->contr->state=ST_PLAY_AGAIN;
667     op->chosen_skill = NULL;
668     send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "Do you want to play again (a/q)?");
669     /* a bit of a hack, but there are various places early in th
670     * player creation process that a user can quit (eg, roll
671     * stats) that isn't removing the player. Taking a quick
672     * look, there are many places that call play_again without
673     * removing the player - it probably makes more sense
674     * to leave it to play_again to remove the object in all
675     * cases.
676     */
677     if (!QUERY_FLAG(op, FLAG_REMOVED))
678     remove_ob(op);
679     /* Need to set this to null - otherwise, it could point to garbage,
680     * and draw() doesn't check to see if the player is removed, only if
681     * the map is null or not swapped out.
682     */
683     op->map = NULL;
684     }
685    
686    
687     int receive_play_again(object *op, char key)
688     {
689     if(key=='q'||key=='Q') {
690     remove_friendly_object(op);
691     leave(op->contr,0); /* ericserver will draw the message */
692     return 2;
693     }
694     else if(key=='a'||key=='A') {
695     player *pl = op->contr;
696     const char *name = op->name;
697    
698     add_refcount(name);
699     remove_friendly_object(op);
700     free_object(op);
701     pl = get_player(pl);
702     op = pl->ob;
703     add_friendly_object(op);
704     op->contr->password[0]='~';
705     FREE_AND_CLEAR_STR(op->name);
706     FREE_AND_CLEAR_STR(op->name_pl);
707    
708     /* Lets put a space in here */
709     new_draw_info(NDI_UNIQUE, 0, op, "\n");
710     get_name(op);
711     op->name = name; /* Alrady added a refcount above */
712     op->name_pl = add_string(name);
713     set_first_map(op);
714     } else {
715     /* user pressed something else so just ask again... */
716     play_again(op);
717     }
718     return 0;
719     }
720    
721     void confirm_password(object *op) {
722    
723     op->contr->write_buf[0]='\0';
724     op->contr->state=ST_CONFIRM_PASSWORD;
725     send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, "Please type your password again.\n:");
726     }
727    
728     void get_party_password(object *op, partylist *party) {
729     if (party == NULL) {
730     LOG(llevError, "get_party_password(): tried to make player %s join a NULL party", op->name);
731     return;
732     }
733     op->contr->write_buf[0]='\0';
734     op->contr->state=ST_GET_PARTY_PASSWORD;
735     op->contr->party_to_join = party;
736     send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, "What is the password?\n:");
737     }
738    
739    
740     /* This rolls four 1-6 rolls and sums the best 3 of the 4. */
741     int roll_stat(void) {
742     int a[4],i,j,k;
743    
744     for(i=0;i<4;i++)
745     a[i]=(int)RANDOM()%6+1;
746    
747     for(i=0,j=0,k=7;i<4;i++)
748     if(a[i]<k)
749     k=a[i],j=i;
750    
751     for(i=0,k=0;i<4;i++) {
752     if(i!=j)
753     k+=a[i];
754     }
755     return k;
756     }
757    
758     void roll_stats(object *op) {
759     int sum=0;
760     int i = 0, j = 0;
761     int statsort[7];
762    
763     do {
764     op->stats.Str=roll_stat();
765     op->stats.Dex=roll_stat();
766     op->stats.Int=roll_stat();
767     op->stats.Con=roll_stat();
768     op->stats.Wis=roll_stat();
769     op->stats.Pow=roll_stat();
770     op->stats.Cha=roll_stat();
771     sum=op->stats.Str+op->stats.Dex+op->stats.Int+
772     op->stats.Con+op->stats.Wis+op->stats.Pow+
773     op->stats.Cha;
774     } while(sum<82||sum>116);
775    
776     /* Sort the stats so that rerolling is easier... */
777     statsort[0] = op->stats.Str;
778     statsort[1] = op->stats.Dex;
779     statsort[2] = op->stats.Int;
780     statsort[3] = op->stats.Con;
781     statsort[4] = op->stats.Wis;
782     statsort[5] = op->stats.Pow;
783     statsort[6] = op->stats.Cha;
784    
785     /* a quick and dirty bubblesort? */
786     do {
787     if (statsort[i] < statsort[i + 1]) {
788     j = statsort[i];
789     statsort[i] = statsort[i + 1];
790     statsort[i + 1] = j;
791     i = 0;
792     } else {
793     i++;
794     }
795     } while (i < 6);
796    
797     op->stats.Str = statsort[0];
798     op->stats.Dex = statsort[1];
799     op->stats.Con = statsort[2];
800     op->stats.Int = statsort[3];
801     op->stats.Wis = statsort[4];
802     op->stats.Pow = statsort[5];
803     op->stats.Cha = statsort[6];
804    
805    
806     op->contr->orig_stats.Str=op->stats.Str;
807     op->contr->orig_stats.Dex=op->stats.Dex;
808     op->contr->orig_stats.Int=op->stats.Int;
809     op->contr->orig_stats.Con=op->stats.Con;
810     op->contr->orig_stats.Wis=op->stats.Wis;
811     op->contr->orig_stats.Pow=op->stats.Pow;
812     op->contr->orig_stats.Cha=op->stats.Cha;
813    
814     op->level=1;
815     op->stats.exp=0;
816     op->stats.ac=0;
817    
818     op->contr->levhp[1] = 9;
819     op->contr->levsp[1] = 6;
820     op->contr->levgrace[1] = 3;
821    
822     fix_player(op);
823     op->stats.hp = op->stats.maxhp;
824     op->stats.sp = op->stats.maxsp;
825     op->stats.grace = op->stats.maxgrace;
826     op->contr->orig_stats=op->stats;
827     }
828    
829     void Roll_Again(object *op)
830     {
831     esrv_new_player(op->contr, 0);
832     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)? ");
833     }
834    
835     void Swap_Stat(object *op,int Swap_Second)
836     {
837     signed char tmp;
838     char buf[MAX_BUF];
839    
840     if ( op->contr->Swap_First == -1 ) {
841     new_draw_info(NDI_UNIQUE, 0,op,"How the hell did you get here?!?!!!");
842     new_draw_info(NDI_UNIQUE, 0,op,"Error in Swap_Stat code,");
843     new_draw_info(NDI_UNIQUE, 0,op,"mail korg@rdt.monash.edu.au");
844     return;
845     }
846    
847     tmp = get_attr_value(&op->contr->orig_stats, op->contr->Swap_First);
848    
849     set_attr_value(&op->contr->orig_stats, op->contr->Swap_First,
850     get_attr_value(&op->contr->orig_stats, Swap_Second));
851    
852     set_attr_value(&op->contr->orig_stats, Swap_Second, tmp);
853    
854     sprintf(buf,"%s done\n", short_stat_name[Swap_Second]);
855     new_draw_info(NDI_UNIQUE, 0,op, buf);
856     op->stats.Str = op->contr->orig_stats.Str;
857     op->stats.Dex = op->contr->orig_stats.Dex;
858     op->stats.Con = op->contr->orig_stats.Con;
859     op->stats.Int = op->contr->orig_stats.Int;
860     op->stats.Wis = op->contr->orig_stats.Wis;
861     op->stats.Pow = op->contr->orig_stats.Pow;
862     op->stats.Cha = op->contr->orig_stats.Cha;
863     op->stats.ac=0;
864    
865     op->level=1;
866     op->stats.exp=0;
867     op->stats.ac=0;
868    
869     op->contr->levhp[1] = 9;
870     op->contr->levsp[1] = 6;
871     op->contr->levgrace[1] = 3;
872    
873     fix_player(op);
874     op->stats.hp = op->stats.maxhp;
875     op->stats.sp = op->stats.maxsp;
876     op->stats.grace = op->stats.maxgrace;
877     op->contr->orig_stats=op->stats;
878     op->contr->Swap_First=-1;
879     }
880    
881    
882     /* This code has been greatly reduced, because with set_attr_value
883     * and get_attr_value, the stats can be accessed just numeric
884     * ids. stat_trans is a table that translate the number entered
885     * into the actual stat. It is needed because the order the stats
886     * are displayed in the stat window is not the same as how
887     * the number's access that stat. The table does that translation.
888     */
889     int key_roll_stat(object *op, char key)
890     {
891     int keynum = key -'0';
892     char buf[MAX_BUF];
893     static sint8 stat_trans[] = {-1, STR, DEX, CON, INT, WIS, POW, CHA};
894    
895     if (keynum>0 && keynum<=7) {
896     if (op->contr->Swap_First==-1) {
897     op->contr->Swap_First=stat_trans[keynum];
898     sprintf(buf,"%s ->", short_stat_name[stat_trans[keynum]]);
899     new_draw_info(NDI_UNIQUE, 0,op,buf);
900     }
901     else
902     Swap_Stat(op,stat_trans[keynum]);
903    
904     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"");
905     return 1;
906     }
907     switch (key) {
908     case 'n':
909     case 'N': {
910     SET_FLAG(op, FLAG_WIZ);
911     if(op->map==NULL) {
912     LOG(llevError,"Map == NULL in state 2\n");
913     break;
914     }
915    
916     #if 0
917     /* So that enter_exit will put us at startx/starty */
918     op->x= -1;
919    
920     enter_exit(op,NULL);
921     #endif
922     SET_ANIMATION(op, 2); /* So player faces south */
923     /* Enter exit adds a player otherwise */
924     add_statbonus(op);
925     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
926     op->contr->state = ST_CHANGE_CLASS;
927     if (op->msg)
928     new_draw_info(NDI_BLUE, 0, op, op->msg);
929     return 0;
930     }
931     case 'y':
932     case 'Y':
933     roll_stats(op);
934     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"");
935     return 1;
936    
937     case 'q':
938     case 'Q':
939     play_again(op);
940     return 1;
941    
942     default:
943     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"Yes, No, Quit or 1-6. Roll again?");
944     return 0;
945     }
946     return 0;
947     }
948    
949     /* This function takes the key that is passed, and does the
950     * appropriate action with it (change class, or other things.
951     */
952    
953     int key_change_class(object *op, char key)
954     {
955     int tmp_loop;
956    
957     if(key=='q'||key=='Q') {
958     remove_ob(op);
959     play_again(op);
960     return 0;
961     }
962     if(key=='d'||key=='D') {
963     char buf[MAX_BUF];
964    
965     /* this must before then initial items are given */
966     esrv_new_player(op->contr, op->weight+op->carrying);
967     create_treasure(find_treasurelist("starting_wealth"),op, 0, 0, 0);
968    
969     /* Lauwenmark : Here we handle the BORN global event */
970     execute_global_event(EVENT_BORN, op);
971    
972     /* Lauwenmark : We then generate a LOGIN event */
973     execute_global_event(EVENT_LOGIN, op->contr, op->contr->socket.host);
974     op->contr->state=ST_PLAYING;
975    
976     if (op->msg) {
977     free_string(op->msg);
978     op->msg=NULL;
979     }
980    
981     /* We create this now because some of the unique maps will need it
982     * to save here.
983     */
984     sprintf(buf,"%s/%s/%s",settings.localdir,settings.playerdir,op->name);
985     make_path_to_file(buf);
986    
987     #ifdef AUTOSAVE
988     op->contr->last_save_tick = pticks;
989     #endif
990     start_info(op);
991     CLEAR_FLAG(op, FLAG_WIZ);
992     give_initial_items(op,op->randomitems);
993     link_player_skills(op);
994     esrv_send_inventory(op, op);
995     fix_player(op);
996     return 0;
997     }
998    
999     /* Following actually changes the class - this is the default command
1000     * if we don't match with one of the options above.
1001     */
1002    
1003     tmp_loop = 0;
1004     while(!tmp_loop) {
1005     const char *name = add_string (op->name);
1006     int x = op->x, y = op->y;
1007     remove_statbonus(op);
1008     remove_ob (op);
1009     op->arch = get_player_archetype(op->arch);
1010     copy_object (&op->arch->clone, op);
1011     op->stats = op->contr->orig_stats;
1012     free_string (op->name);
1013     op->name = name;
1014     free_string(op->name_pl);
1015     op->name_pl = add_string(name);
1016     op->x = x;
1017     op->y = y;
1018     SET_ANIMATION(op, 2); /* So player faces south */
1019     insert_ob_in_map (op, op->map, op,0);
1020     strncpy(op->contr->title, op->arch->clone.name, sizeof(op->contr->title)-1);
1021     op->contr->title[sizeof(op->contr->title)-1] = '\0';
1022     add_statbonus(op);
1023     tmp_loop=allowed_class(op);
1024     }
1025     update_object(op,UP_OBJ_FACE);
1026     esrv_update_item(UPD_FACE,op,op);
1027     fix_player(op);
1028     op->stats.hp=op->stats.maxhp;
1029     op->stats.sp=op->stats.maxsp;
1030     op->stats.grace=0;
1031     if (op->msg)
1032     new_draw_info(NDI_BLUE, 0, op, op->msg);
1033     send_query(&op->contr->socket,CS_QUERY_SINGLECHAR,"Press any key for the next race.\nPress `d' to play this race.\n");
1034     return 0;
1035     }
1036    
1037     int key_confirm_quit(object *op, char key)
1038     {
1039     char buf[MAX_BUF];
1040    
1041     if(key!='y'&&key!='Y'&&key!='q'&&key!='Q') {
1042     op->contr->state=ST_PLAYING;
1043     new_draw_info(NDI_UNIQUE, 0,op,"OK, continuing to play.");
1044     return 1;
1045     }
1046    
1047     /* Lauwenmark : Here we handle the REMOVE global event */
1048     execute_global_event(EVENT_REMOVE, op);
1049     terminate_all_pets(op);
1050     leave_map(op);
1051     op->direction=0;
1052     new_draw_info_format(NDI_UNIQUE | NDI_ALL, 5, NULL,
1053     "%s quits the game.",op->name);
1054    
1055     strcpy(op->contr->killer,"quit");
1056     check_score(op);
1057     op->contr->party=NULL;
1058     if (settings.set_title == TRUE)
1059     op->contr->own_title[0]='\0';
1060    
1061     if(!QUERY_FLAG(op,FLAG_WAS_WIZ)) {
1062     mapstruct *mp, *next;
1063    
1064     /* We need to hunt for any per player unique maps in memory and
1065     * get rid of them. The trailing slash in the path is intentional,
1066     * so that players named 'Ab' won't match against players 'Abe' pathname
1067     */
1068     sprintf(buf,"%s/%s/%s/", settings.localdir, settings.playerdir, op->name);
1069     for (mp=first_map; mp!=NULL; mp=next) {
1070     next = mp->next;
1071     if (!strncmp(mp->path, buf, strlen(buf)))
1072     delete_map(mp);
1073     }
1074    
1075     delete_character(op->name, 1);
1076     }
1077     play_again(op);
1078     return 1;
1079     }
1080    
1081     void flee_player(object *op) {
1082     int dir,diff;
1083     rv_vector rv;
1084    
1085     if(op->stats.hp < 0) {
1086     LOG(llevDebug, "Fleeing player is dead.\n");
1087     CLEAR_FLAG(op, FLAG_SCARED);
1088     return;
1089     }
1090    
1091     if(op->enemy==NULL) {
1092     LOG(llevDebug,"Fleeing player had no enemy.\n");
1093     CLEAR_FLAG(op, FLAG_SCARED);
1094     return;
1095     }
1096    
1097     /* Seen some crashes here. Since we don't store an
1098     * op->enemy_count, it is possible that something destroys the
1099     * actual enemy, and the object is recycled.
1100     */
1101     if (op->enemy->map == NULL) {
1102     CLEAR_FLAG(op, FLAG_SCARED);
1103     op->enemy=NULL;
1104     return;
1105     }
1106    
1107     if(!(random_roll(0, 4, op, PREFER_LOW)) && did_make_save(op, op->level, 0)) {
1108     op->enemy=NULL;
1109     CLEAR_FLAG(op, FLAG_SCARED);
1110     return;
1111     }
1112     get_rangevector(op, op->enemy, &rv, 0);
1113    
1114     dir=absdir(4+rv.direction);
1115     for(diff=0;diff<3;diff++) {
1116     int m=1-(RANDOM()&2);
1117     if(move_ob(op,absdir(dir+diff*m),op)||
1118     (diff==0 && move_ob(op,absdir(dir-diff*m),op))) {
1119     return;
1120     }
1121     }
1122     /* Cornered, get rid of scared */
1123     CLEAR_FLAG(op, FLAG_SCARED);
1124     op->enemy=NULL;
1125     }
1126    
1127    
1128     /* check_pick sees if there is stuff to be picked up/picks up stuff.
1129     * IT returns 1 if the player should keep on moving, 0 if he should
1130     * stop.
1131     */
1132     int check_pick(object *op) {
1133     object *tmp, *next;
1134     tag_t next_tag=0, op_tag;
1135     int stop = 0;
1136     int j, k, wvratio;
1137     char putstring[128], tmpstr[16];
1138    
1139    
1140     /* if you're flying, you cna't pick up anything */
1141     if (op->move_type & MOVE_FLYING)
1142     return 1;
1143    
1144     op_tag = op->count;
1145    
1146     next = op->below;
1147     if (next)
1148     next_tag = next->count;
1149    
1150     /* loop while there are items on the floor that are not marked as
1151     * destroyed */
1152     while (next && ! was_destroyed (next, next_tag))
1153     {
1154     tmp = next;
1155     next = tmp->below;
1156     if (next)
1157     next_tag = next->count;
1158    
1159     if (was_destroyed (op, op_tag))
1160     return 0;
1161    
1162     if ( ! can_pick (op, tmp))
1163     continue;
1164    
1165     if (op->contr->search_str[0]!='\0' && settings.search_items == TRUE)
1166     {
1167     if (item_matched_string (op, tmp, op->contr->search_str))
1168     pick_up (op, tmp);
1169     continue;
1170     }
1171    
1172     /* high not bit set? We're using the old autopickup model */
1173     if(!(op->contr->mode & PU_NEWMODE)) {
1174     switch (op->contr->mode) {
1175     case 0: return 1; /* don't pick up */
1176     case 1: pick_up (op, tmp);
1177     return 1;
1178     case 2: pick_up (op, tmp);
1179     return 0;
1180     case 3: return 0; /* stop before pickup */
1181     case 4: pick_up (op, tmp);
1182     break;
1183     case 5: pick_up (op, tmp);
1184     stop = 1;
1185     break;
1186     case 6:
1187     if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL) &&
1188     ! QUERY_FLAG(tmp, FLAG_KNOWN_CURSED))
1189     pick_up(op, tmp);
1190     break;
1191    
1192     case 7:
1193     if (tmp->type == MONEY || tmp->type == GEM)
1194     pick_up(op, tmp);
1195     break;
1196    
1197     default:
1198     /* use value density */
1199     if ( ! QUERY_FLAG (tmp, FLAG_UNPAID)
1200     && (query_cost (tmp, op, F_TRUE) * 100
1201     / (tmp->weight * MAX (tmp->nrof, 1)))
1202     >= op->contr->mode)
1203     pick_up(op,tmp);
1204     }
1205     }
1206     else { /* old model */
1207     /* NEW pickup handling */
1208     if(op->contr->mode & PU_DEBUG)
1209     {
1210     /* some debugging code to figure out item information */
1211     if(tmp->name!=NULL)
1212     sprintf(putstring,"item name: %s item type: %d weight/value: %d",
1213     tmp->name, tmp->type,
1214     (int)(query_cost(tmp, op, F_TRUE)*100 / (tmp->weight * MAX(tmp->nrof,1))));
1215     else
1216     sprintf(putstring,"item name: %s item type: %d weight/value: %d",
1217     tmp->arch->name, tmp->type,
1218     (int)(query_cost(tmp, op, F_TRUE)*100 / (tmp->weight * MAX(tmp->nrof,1))));
1219     new_draw_info(NDI_UNIQUE, 0,op,putstring);
1220    
1221     sprintf(putstring,"...flags: ");
1222     for(k=0;k<4;k++)
1223     {
1224     for(j=0;j<32;j++)
1225     {
1226     if((tmp->flags[k]>>j)&0x01)
1227     {
1228     sprintf(tmpstr,"%d ",k*32+j);
1229     strcat(putstring, tmpstr);
1230     }
1231     }
1232     }
1233     new_draw_info(NDI_UNIQUE, 0,op,putstring);
1234    
1235     #if 0
1236     /* print the flags too */
1237     for(k=0;k<4;k++)
1238     {
1239     fprintf(stderr,"%d [%d] ", k, k*32+31);
1240     for(j=0;j<32;j++)
1241     {
1242     fprintf(stderr,"%d",tmp->flags[k]>>(31-j)&0x01);
1243     if(!((j+1)%4))fprintf(stderr," ");
1244     }
1245     fprintf(stderr," [%d]\n", k*32);
1246     }
1247     #endif
1248     }
1249     /* philosophy:
1250     * It's easy to grab an item type from a pile, as long as it's
1251     * generic. This takes no game-time. For more detailed pickups
1252     * and selections, select-items shoul dbe used. This is a
1253     * grab-as-you-run type mode that's really useful for arrows for
1254     * example.
1255     * The drawback: right now it has no frontend, so you need to
1256     * stick the bits you want into a calculator in hex mode and then
1257     * convert to decimal and then 'pickup <#>
1258     */
1259    
1260     /* the first two modes are exclusive: if NOTHING we return, if
1261     * STOP then we stop. All the rest are applied sequentially,
1262     * meaning if any test passes, the item gets picked up. */
1263    
1264     /* if mode is set to pick nothing up, return */
1265    
1266     if(op->contr->mode & PU_NOTHING) return 1;
1267    
1268     /* if mode is set to stop when encountering objects, return */
1269     /* take STOP before INHIBIT since it doesn't actually pick
1270     * anything up */
1271    
1272     if(op->contr->mode & PU_STOP) return 0;
1273    
1274     /* useful for going into stores and not losing your settings... */
1275     /* and for battles wher you don't want to get loaded down while
1276     * fighting */
1277     if(op->contr->mode & PU_INHIBIT) return 1;
1278    
1279     /* prevent us from turning into auto-thieves :) */
1280     if (QUERY_FLAG (tmp, FLAG_UNPAID)) continue;
1281    
1282     /* ignore known cursed objects */
1283     if (QUERY_FLAG (tmp, FLAG_KNOWN_CURSED) && op->contr->mode & PU_NOT_CURSED) continue;
1284    
1285     /* all food and drink if desired */
1286     /* question: don't pick up known-poisonous stuff? */
1287     if(op->contr->mode & PU_FOOD)
1288     if (tmp->type == FOOD)
1289     { pick_up(op, tmp); if(0)fprintf(stderr,"FOOD\n"); continue; }
1290     if(op->contr->mode & PU_DRINK)
1291     if (tmp->type == DRINK || (tmp->type == POISON && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)))
1292     { pick_up(op, tmp); if(0)fprintf(stderr,"DRINK\n"); continue; }
1293    
1294     if(op->contr->mode & PU_POTION)
1295     if (tmp->type == POTION)
1296     { pick_up(op, tmp); if(0)fprintf(stderr,"POTION\n"); continue; }
1297    
1298     /* spellbooks, skillscrolls and normal books/scrolls */
1299     if(op->contr->mode & PU_SPELLBOOK)
1300     if (tmp->type == SPELLBOOK)
1301     { pick_up(op, tmp); if(0)fprintf(stderr,"SPELLBOOK\n"); continue; }
1302     if(op->contr->mode & PU_SKILLSCROLL)
1303     if (tmp->type == SKILLSCROLL)
1304     { pick_up(op, tmp); if(0)fprintf(stderr,"SKILLSCROLL\n"); continue; }
1305     if(op->contr->mode & PU_READABLES)
1306     if (tmp->type == BOOK || tmp->type == SCROLL)
1307     { pick_up(op, tmp); if(0)fprintf(stderr,"READABLES\n"); continue; }
1308    
1309     /* wands/staves/rods/horns */
1310     if (op->contr->mode & PU_MAGIC_DEVICE)
1311     if (tmp->type == WAND || tmp->type == ROD || tmp->type == HORN)
1312     { pick_up(op, tmp); if(0)fprintf(stderr,"MAGIC_DEVICE\n"); continue; }
1313    
1314     /* pick up all magical items */
1315     if(op->contr->mode & PU_MAGICAL)
1316     if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL) && ! QUERY_FLAG(tmp, FLAG_KNOWN_CURSED))
1317     { pick_up(op, tmp); if(0)fprintf(stderr,"MAGICAL\n"); continue; }
1318    
1319     if(op->contr->mode & PU_VALUABLES)
1320     {
1321     if (tmp->type == MONEY || tmp->type == GEM)
1322     { pick_up(op, tmp); if(0)fprintf(stderr,"MONEY/GEM\n"); continue; }
1323     }
1324    
1325     /* rings & amulets - talismans seems to be typed AMULET */
1326     if(op->contr->mode & PU_JEWELS)
1327     if (tmp->type == RING || tmp->type == AMULET)
1328     { pick_up(op, tmp); if(0)fprintf(stderr,"JEWELS\n"); continue; }
1329    
1330     /* bows and arrows. Bows are good for selling! */
1331     if(op->contr->mode & PU_BOW)
1332     if (tmp->type == BOW)
1333     { pick_up(op, tmp); if(0)fprintf(stderr,"BOW\n"); continue; }
1334     if(op->contr->mode & PU_ARROW)
1335     if (tmp->type == ARROW)
1336     { pick_up(op, tmp); if(0)fprintf(stderr,"ARROW\n"); continue; }
1337    
1338     /* all kinds of armor etc. */
1339     if(op->contr->mode & PU_ARMOUR)
1340     if (tmp->type == ARMOUR)
1341     { pick_up(op, tmp); if(0)fprintf(stderr,"ARMOUR\n"); continue; }
1342     if(op->contr->mode & PU_HELMET)
1343     if (tmp->type == HELMET)
1344     { pick_up(op, tmp); if(0)fprintf(stderr,"HELMET\n"); continue; }
1345     if(op->contr->mode & PU_SHIELD)
1346     if (tmp->type == SHIELD)
1347     { pick_up(op, tmp); if(0)fprintf(stderr,"SHIELD\n"); continue; }
1348     if(op->contr->mode & PU_BOOTS)
1349     if (tmp->type == BOOTS)
1350     { pick_up(op, tmp); if(0)fprintf(stderr,"BOOTS\n"); continue; }
1351     if(op->contr->mode & PU_GLOVES)
1352     if (tmp->type == GLOVES)
1353     { pick_up(op, tmp); if(0)fprintf(stderr,"GLOVES\n"); continue; }
1354     if(op->contr->mode & PU_CLOAK)
1355     if (tmp->type == CLOAK)
1356     { pick_up(op, tmp); if(0)fprintf(stderr,"GLOVES\n"); continue; }
1357    
1358     /* hoping to catch throwing daggers here */
1359     if(op->contr->mode & PU_MISSILEWEAPON)
1360     if(tmp->type == WEAPON && QUERY_FLAG(tmp, FLAG_IS_THROWN))
1361     { pick_up(op, tmp); if(0)fprintf(stderr,"MISSILEWEAPON\n"); continue; }
1362    
1363     /* careful: chairs and tables are weapons! */
1364     if(op->contr->mode & PU_ALLWEAPON)
1365     {
1366     if(tmp->type == WEAPON && tmp->name!=NULL)
1367     {
1368     if(strstr(tmp->name,"table")==NULL && strstr(tmp->arch->name,"table")==NULL &&
1369     strstr(tmp->name,"chair") && strstr(tmp->arch->name,"chair")==NULL)
1370     { pick_up(op, tmp); if(0)fprintf(stderr,"WEAPON\n"); continue; }
1371     }
1372     if(tmp->type == WEAPON && tmp->name==NULL)
1373     {
1374     if(strstr(tmp->arch->name,"table")==NULL &&
1375     strstr(tmp->arch->name,"chair")==NULL)
1376     { pick_up(op, tmp); if(0)fprintf(stderr,"WEAPON\n"); continue; }
1377     }
1378     }
1379    
1380     /* misc stuff that's useful */
1381     if(op->contr->mode & PU_KEY)
1382     if (tmp->type == KEY || tmp->type == SPECIAL_KEY)
1383     { pick_up(op, tmp); if(0)fprintf(stderr,"KEY\n"); continue; }
1384    
1385     /* any of the last 4 bits set means we use the ratio for value
1386     * pickups */
1387     if(op->contr->mode & PU_RATIO)
1388     {
1389     /* use value density to decide what else to grab */
1390     /* >=7 was >= op->contr->mode */
1391     /* >=7 is the old standard setting. Now we take the last 4 bits
1392     * and multiply them by 5, giving 0..15*5== 5..75 */
1393     wvratio=(op->contr->mode & PU_RATIO) * 5;
1394     if ((query_cost(tmp, op, F_TRUE)*100 / (tmp->weight * MAX(tmp->nrof, 1))) >= wvratio)
1395     {
1396     pick_up(op, tmp);
1397     #if 0
1398     fprintf(stderr,"HIGH WEIGHT/VALUE [");
1399     if(tmp->name!=NULL) {
1400     fprintf(stderr,"%s", tmp->name);
1401     }
1402     else fprintf(stderr,"%s",tmp->arch->name);
1403     fprintf(stderr,",%d] = ", tmp->type);
1404     fprintf(stderr,"%d\n",(int)(query_cost(tmp,op,F_TRUE)*100 / (tmp->weight * MAX(tmp->nrof,1))));
1405     #endif
1406     continue;
1407     }
1408     }
1409     } /* the new pickup model */
1410     }
1411     return ! stop;
1412     }
1413    
1414     /*
1415     * Find an arrow in the inventory and after that
1416     * in the right type container (quiver). Pointer to the
1417     * found object is returned.
1418     */
1419     object *find_arrow(object *op, const char *type)
1420     {
1421     object *tmp = NULL;
1422    
1423     for(op=op->inv; op; op=op->below)
1424     if(!tmp && op->type==CONTAINER && op->race==type &&
1425     QUERY_FLAG(op,FLAG_APPLIED))
1426     tmp = find_arrow (op, type);
1427     else if (op->type==ARROW && op->race==type)
1428     return op;
1429     return tmp;
1430     }
1431    
1432     /*
1433     * Similar to find_arrow, but looks for (roughly) the best arrow to use
1434     * against the target. A full test is not performed, simply a basic test
1435     * of resistances. The archer is making a quick guess at what he sees down
1436     * the hall. Failing that it does it's best to pick the highest plus arrow.
1437     */
1438    
1439     object *find_better_arrow(object *op, object *target, const char *type, int *better)
1440     {
1441     object *tmp = NULL, *arrow, *ntmp;
1442     int attacknum, attacktype, betterby=0, i;
1443    
1444     if (!type)
1445     return NULL;
1446    
1447     for (arrow=op->inv; arrow; arrow=arrow->below) {
1448     if (arrow->type==CONTAINER && arrow->race==type &&
1449     QUERY_FLAG(arrow, FLAG_APPLIED)) {
1450     i = 0;
1451     ntmp = find_better_arrow(arrow, target, type, &i);
1452     if (i > betterby) {
1453     tmp = ntmp;
1454     betterby = i;
1455     }
1456     } else if (arrow->type==ARROW && arrow->race==type) {
1457     /* allways prefer assasination/slaying */
1458     if (target->race != NULL && arrow->slaying != NULL &&
1459     strstr(arrow->slaying, target->race)) {
1460     if (arrow->attacktype & AT_DEATH) {
1461     *better = 100;
1462     return arrow;
1463     } else {
1464     tmp = arrow;
1465     betterby = (arrow->magic + arrow->stats.dam) * 2;
1466     }
1467     } else {
1468     for (attacknum=0; attacknum < NROFATTACKS; attacknum++) {
1469     attacktype = 1<<attacknum;
1470     if ((arrow->attacktype & attacktype) && (target->arch->clone.resist[attacknum]) < 0)
1471     if (((arrow->magic + arrow->stats.dam)*(100-target->arch->clone.resist[attacknum])/100) > betterby) {
1472     tmp = arrow;
1473     betterby = (arrow->magic + arrow->stats.dam)*(100-target->arch->clone.resist[attacknum])/100;
1474     }
1475     }
1476     if ((2 + arrow->magic + arrow->stats.dam) > betterby) {
1477     tmp = arrow;
1478     betterby = 2 + arrow->magic + arrow->stats.dam;
1479     }
1480     if (arrow->title && (1 + arrow->magic + arrow->stats.dam) > betterby) {
1481     tmp = arrow;
1482     betterby = 1 + arrow->magic + arrow->stats.dam;
1483     }
1484     }
1485     }
1486     }
1487     if (tmp == NULL && arrow == NULL)
1488     return find_arrow(op, type);
1489    
1490     *better = betterby;
1491     return tmp;
1492     }
1493    
1494     /* looks in a given direction, finds the first valid target, and calls
1495     * find_better_arrow to find a decent arrow to use.
1496     * op = the shooter
1497     * type = bow->race
1498     * dir = fire direction
1499     */
1500    
1501     object *pick_arrow_target(object *op, const char *type, int dir)
1502     {
1503     object *tmp = NULL;
1504     mapstruct *m;
1505     int i, mflags, found, number;
1506     sint16 x, y;
1507    
1508     if (op->map == NULL)
1509     return find_arrow(op, type);
1510    
1511     /* do a dex check */
1512     number = (die_roll(2, 40, op, PREFER_LOW)-2)/2;
1513     if (number > (op->stats.Dex + (op->chosen_skill?op->chosen_skill->level:op->level)))
1514     return find_arrow(op, type);
1515    
1516     m = op->map;
1517     x = op->x;
1518     y = op->y;
1519    
1520     /* find the first target */
1521     for (i=0, found=0; i<20; i++) {
1522     x += freearr_x[dir];
1523     y += freearr_y[dir];
1524     mflags = get_map_flags(m, &m, x, y, &x, &y);
1525     if (mflags & P_OUT_OF_MAP || mflags & P_BLOCKSVIEW) {
1526     tmp = NULL;
1527     break;
1528     } else if (GET_MAP_MOVE_BLOCK(m, x, y) == MOVE_FLY_LOW) {
1529     /* This block presumes arrows and the like are MOVE_FLY_SLOW -
1530     * perhaps a bad assumption.
1531     */
1532     tmp = NULL;
1533     break;
1534     }
1535     if (mflags & P_IS_ALIVE) {
1536     for (tmp = GET_MAP_OB(m, x, y); tmp; tmp=tmp->above)
1537     if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
1538     found++;
1539     break;
1540     }
1541     if (found)
1542     break;
1543     }
1544     }
1545     if (tmp == NULL)
1546     return find_arrow(op, type);
1547    
1548     if (tmp->head)
1549     tmp = tmp->head;
1550    
1551     return find_better_arrow(op, tmp, type, &i);
1552     }
1553    
1554     /*
1555     * Creature fires a bow - op can be monster or player. Returns
1556     * 1 if bow was actually fired, 0 otherwise.
1557     * op is the object firing the bow.
1558     * part is for multipart creatures - the part firing the bow.
1559     * dir is the direction of fire.
1560     * wc_mod is any special modifier to give (used in special player fire modes)
1561     * sx, sy are coordinates to fire arrow from - also used in some of the special
1562     * player fire modes.
1563     */
1564     int fire_bow(object *op, object *part, object *arrow, int dir, int wc_mod,
1565     sint16 sx, sint16 sy)
1566     {
1567     object *left, *bow;
1568     tag_t left_tag, tag;
1569     int bowspeed, mflags;
1570     mapstruct *m;
1571    
1572     if (!dir) {
1573     new_draw_info(NDI_UNIQUE, 0, op, "You can't shoot yourself!");
1574     return 0;
1575     }
1576     if (op->type == PLAYER)
1577     bow=op->contr->ranges[range_bow];
1578     else {
1579     for(bow=op->inv; bow; bow=bow->below)
1580     /* Don't check for applied - monsters don't apply bows - in that way, they
1581     * don't need to switch back and forth between bows and weapons.
1582     */
1583     if(bow->type==BOW)
1584     break;
1585    
1586     if (!bow) {
1587     LOG (llevError, "Range: bow without activated bow (%s).\n", op->name);
1588     return 0;
1589     }
1590     }
1591     if( !bow->race || !bow->skill) {
1592     new_draw_info_format(NDI_UNIQUE, 0, op, "Your %s is broken.", bow->name);
1593     return 0;
1594     }
1595    
1596     bowspeed = bow->stats.sp + dex_bonus[op->stats.Dex];
1597    
1598     /* penalize ROF for bestarrow */
1599     if (op->type == PLAYER && op->contr->bowtype == bow_bestarrow)
1600     bowspeed -= dex_bonus[op->stats.Dex] + 5;
1601     if (bowspeed < 1)
1602     bowspeed = 1;
1603    
1604     if (arrow == NULL) {
1605     if ((arrow=find_arrow(op, bow->race)) == NULL) {
1606     if (op->type == PLAYER)
1607     new_draw_info_format(NDI_UNIQUE, 0, op,
1608     "You have no %s left.", bow->race);
1609     /* FLAG_READY_BOW will get reset if the monsters picks up some arrows */
1610     else
1611     CLEAR_FLAG(op, FLAG_READY_BOW);
1612     return 0;
1613     }
1614     }
1615     mflags = get_map_flags(op->map, &m, sx, sy, &sx, &sy);
1616     if (mflags & P_OUT_OF_MAP) {
1617     return 0;
1618     }
1619     if (GET_MAP_MOVE_BLOCK(m, sx, sy) == MOVE_FLY_LOW) {
1620     new_draw_info(NDI_UNIQUE, 0,op,"Something is in the way.");
1621     return 0;
1622     }
1623    
1624     /* this should not happen, but sometimes does */
1625     if (arrow->nrof==0) {
1626     remove_ob(arrow);
1627     free_object(arrow);
1628     return 0;
1629     }
1630    
1631     left = arrow; /* these are arrows left to the player */
1632     left_tag = left->count;
1633     arrow = get_split_ob(arrow, 1);
1634     if (arrow == NULL) {
1635     new_draw_info_format(NDI_UNIQUE, 0, op, "You have no %s left.",
1636     bow->race);
1637     return 0;
1638     }
1639     set_owner(arrow, op);
1640     if (arrow->skill) free_string(arrow->skill);
1641     arrow->skill = add_refcount(bow->skill);
1642    
1643     arrow->direction=dir;
1644     arrow->x = sx;
1645     arrow->y = sy;
1646    
1647     if (op->type == PLAYER) {
1648     op->speed_left = 0.01 - (float)FABS(op->speed) * 100 / bowspeed;
1649     fix_player(op);
1650     }
1651    
1652     SET_ANIMATION(arrow, arrow->direction);
1653     arrow->stats.sp = arrow->stats.wc; /* save original wc and dam */
1654     arrow->stats.hp = arrow->stats.dam;
1655     arrow->stats.grace = arrow->attacktype;
1656     if (arrow->slaying != NULL)
1657     arrow->spellarg = strdup_local(arrow->slaying);
1658    
1659     /* Note that this was different for monsters - they got their level
1660     * added to the damage. I think the strength bonus is more proper.
1661     */
1662    
1663     arrow->stats.dam += (QUERY_FLAG(bow, FLAG_NO_STRENGTH) ?
1664     0 : dam_bonus[op->stats.Str]) +
1665     bow->stats.dam + bow->magic + arrow->magic;
1666    
1667     /* update the speed */
1668     arrow->speed = (float)((QUERY_FLAG(bow, FLAG_NO_STRENGTH) ?
1669     0 : dam_bonus[op->stats.Str]) +
1670     bow->magic + arrow->magic) / 5.0 +
1671     (float)bow->stats.dam / 7.0;
1672    
1673     if (arrow->speed < 1.0)
1674     arrow->speed = 1.0;
1675     update_ob_speed(arrow);
1676     arrow->speed_left = 0;
1677    
1678     if (op->type == PLAYER) {
1679     arrow->stats.wc = 20 - bow->magic - arrow->magic -
1680     (op->chosen_skill?op->chosen_skill->level:op->level) -
1681     dex_bonus[op->stats.Dex] - thaco_bonus[op->stats.Str] -
1682     arrow->stats.wc - bow->stats.wc + wc_mod;
1683    
1684     arrow->level = op->chosen_skill?op->chosen_skill->level:op->level;
1685     } else {
1686     arrow->stats.wc= op->stats.wc - bow->magic - arrow->magic -
1687     arrow->stats.wc + wc_mod;
1688    
1689     arrow->level = op->level;
1690     }
1691     if (arrow->attacktype == AT_PHYSICAL)
1692     arrow->attacktype |= bow->attacktype;
1693     if (bow->slaying != NULL)
1694     arrow->slaying = add_string(bow->slaying);
1695    
1696     arrow->map = m;
1697     arrow->move_type = MOVE_FLY_LOW;
1698     arrow->move_on = MOVE_FLY_LOW | MOVE_WALK;
1699    
1700     play_sound_map(op->map, op->x, op->y, SOUND_FIRE_ARROW);
1701     tag = arrow->count;
1702     insert_ob_in_map(arrow, m, op, 0);
1703    
1704     if (!was_destroyed(arrow, tag))
1705     move_arrow(arrow);
1706    
1707     if (op->type == PLAYER) {
1708     if (was_destroyed (left, left_tag))
1709     esrv_del_item(op->contr, left_tag);
1710     else
1711     esrv_send_item(op, left);
1712     }
1713     return 1;
1714     }
1715    
1716     /* Special fire code for players - this takes into
1717     * account the special fire modes players can have
1718     * but monsters can't. Putting that code here
1719     * makes the fire_bow code much cleaner.
1720     * this function should only be called if 'op' is a player,
1721     * hence the function name.
1722     */
1723     int player_fire_bow(object *op, int dir)
1724     {
1725     int ret=0, wcmod=0;
1726    
1727     if (op->contr->bowtype == bow_bestarrow) {
1728     ret = fire_bow(op, op,
1729     pick_arrow_target(op, op->contr->ranges[range_bow]->race, dir),
1730     dir, 0, op->x, op->y);
1731     } else if (op->contr->bowtype >= bow_n && op->contr->bowtype <= bow_nw) {
1732     if (!similar_direction(dir, op->contr->bowtype - bow_n + 1))
1733     wcmod =-1;
1734     ret = fire_bow(op, op, NULL, op->contr->bowtype - bow_n + 1, wcmod,
1735     op->x, op->y);
1736     } else if (op->contr->bowtype == bow_threewide) {
1737     ret = fire_bow(op, op, NULL, dir, 0, op->x, op->y);
1738     ret |= fire_bow(op, op, NULL, dir, -5, op->x + freearr_x[absdir(dir+2)], op->y + freearr_y[absdir(dir+2)]);
1739     ret |= fire_bow(op, op, NULL, dir, -5, op->x + freearr_x[absdir(dir-2)], op->y + freearr_y[absdir(dir-2)]);
1740     } else if (op->contr->bowtype == bow_spreadshot) {
1741     ret |= fire_bow(op, op, NULL, dir, 0, op->x, op->y);
1742     ret |= fire_bow(op, op, NULL, absdir(dir-1), -5, op->x, op->y);
1743     ret |= fire_bow(op, op, NULL, absdir(dir+1), -5, op->x, op->y);
1744    
1745     } else {
1746     /* Simple case */
1747     ret = fire_bow(op, op, NULL, dir, 0, op->x, op->y);
1748     }
1749     return ret;
1750     }
1751    
1752    
1753     /* Fires a misc (wand/rod/horn) object in 'dir'.
1754     * Broken apart from 'fire' to keep it more readable.
1755     */
1756     void fire_misc_object(object *op, int dir)
1757     {
1758     object *item;
1759    
1760     if (!op->contr->ranges[range_misc]) {
1761     new_draw_info(NDI_UNIQUE, 0,op,"You have range item readied.");
1762     return;
1763     }
1764    
1765     item = op->contr->ranges[range_misc];
1766     if (!item->inv) {
1767     LOG(llevError,"Object %s lacks a spell\n", item->name);
1768     return;
1769     }
1770     if (item->type == WAND) {
1771     if(item->stats.food<=0) {
1772     play_sound_player_only(op->contr, SOUND_WAND_POOF,0,0);
1773     new_draw_info_format(NDI_UNIQUE, 0,op,"The %s goes poof.", query_base_name(item, 0));
1774     return;
1775     }
1776     } else if (item->type == ROD || item->type==HORN) {
1777     if(item->stats.hp<MAX(item->inv->stats.sp, item->inv->stats.grace)) {
1778     play_sound_player_only(op->contr, SOUND_WAND_POOF,0,0);
1779     if (item->type== ROD)
1780     new_draw_info_format(NDI_UNIQUE, 0,op,
1781     "The %s whines for a while, but nothing happens.", query_base_name(item,0));
1782     else
1783     new_draw_info_format(NDI_UNIQUE, 0,op,
1784     "The %s needs more time to charge.", query_base_name(item,0));
1785     return;
1786     }
1787     }
1788    
1789     if(cast_spell(op,item,dir,item->inv,NULL)) {
1790     SET_FLAG(op, FLAG_BEEN_APPLIED); /* You now know something about it */
1791     if (item->type == WAND) {
1792     if (!(--item->stats.food)) {
1793     object *tmp;
1794     if (item->arch) {
1795     CLEAR_FLAG(item, FLAG_ANIMATE);
1796     item->face = item->arch->clone.face;
1797     item->speed = 0;
1798     update_ob_speed(item);
1799     }
1800     if ((tmp=is_player_inv(item)))
1801     esrv_update_item(UPD_ANIM, tmp, item);
1802     }
1803     }
1804     else if (item->type == ROD || item->type==HORN) {
1805     drain_rod_charge(item);
1806     }
1807     }
1808     }
1809    
1810     /* Received a fire command for the player - go and do it.
1811     */
1812     void fire(object *op,int dir) {
1813     int spellcost=0;
1814    
1815     /* check for loss of invisiblity/hide */
1816     if (action_makes_visible(op)) make_visible(op);
1817    
1818     switch(op->contr->shoottype) {
1819     case range_none:
1820     return;
1821    
1822     case range_bow:
1823     player_fire_bow(op, dir);
1824     return;
1825    
1826     case range_magic: /* Casting spells */
1827     spellcost=(cast_spell(op,op,dir,op->contr->ranges[range_magic],op->contr->spellparam[0]?op->contr->spellparam:NULL));
1828     return;
1829    
1830     case range_misc:
1831     fire_misc_object(op, dir);
1832     return;
1833    
1834     case range_golem: /* Control summoned monsters from scrolls */
1835     if(op->contr->ranges[range_golem]==NULL ||
1836     op->contr->golem_count != op->contr->ranges[range_golem]->count) {
1837     op->contr->ranges[range_golem] = NULL;
1838     op->contr->shoottype=range_none;
1839     op->contr->golem_count = 0;
1840     }
1841     else
1842     control_golem(op->contr->ranges[range_golem], dir);
1843     return;
1844    
1845     case range_skill:
1846     if(!op->chosen_skill) {
1847     if(op->type==PLAYER)
1848     new_draw_info(NDI_UNIQUE, 0,op,"You have no applicable skill to use.");
1849     return;
1850     }
1851     (void) do_skill(op,op,op->chosen_skill,dir,NULL);
1852     return;
1853     case range_builder:
1854     apply_map_builder( op, dir );
1855     return;
1856     default:
1857     new_draw_info(NDI_UNIQUE, 0,op,"Illegal shoot type.");
1858     return;
1859     }
1860     }
1861    
1862    
1863    
1864     /* find_key
1865     * We try to find a key for the door as passed. If we find a key
1866     * and successfully use it, we return the key, otherwise NULL
1867     * This function merges both normal and locked door, since the logic
1868     * for both is the same - just the specific key is different.
1869     * pl is the player,
1870     * inv is the objects inventory to searched
1871     * door is the door we are trying to match against.
1872     * This function can be called recursively to search containers.
1873     */
1874    
1875     object * find_key(object *pl, object *container, object *door)
1876     {
1877     object *tmp,*key;
1878    
1879     /* Should not happen, but sanity checking is never bad */
1880     if (container->inv == NULL) return NULL;
1881    
1882     /* First, lets try to find a key in the top level inventory */
1883     for (tmp=container->inv; tmp!=NULL; tmp=tmp->below) {
1884     if (door->type==DOOR && tmp->type==KEY) break;
1885     /* For sanity, we should really check door type, but other stuff
1886     * (like containers) can be locked with special keys
1887     */
1888     if (tmp->slaying && tmp->type==SPECIAL_KEY &&
1889     tmp->slaying==door->slaying) break;
1890     }
1891     /* No key found - lets search inventories now */
1892     /* If we find and use a key in an inventory, return at that time.
1893     * otherwise, if we search all the inventories and still don't find
1894     * a key, return
1895     */
1896     if (!tmp) {
1897     for (tmp=container->inv; tmp!=NULL; tmp=tmp->below) {
1898     /* No reason to search empty containers */
1899     if (tmp->type==CONTAINER && tmp->inv) {
1900     if ((key=find_key(pl, tmp, door))!=NULL) return key;
1901     }
1902     }
1903     if (!tmp) return NULL;
1904     }
1905     /* We get down here if we have found a key. Now if its in a container,
1906     * see if we actually want to use it
1907     */
1908     if (pl!=container) {
1909     /* Only let players use keys in containers */
1910     if (!pl->contr) return NULL;
1911     /* cases where this fails:
1912     * If we only search the player inventory, return now since we
1913     * are not in the players inventory.
1914     * If the container is not active, return now since only active
1915     * containers can be used.
1916     * If we only search keyrings and the container does not have
1917     * a race/isn't a keyring.
1918     * No checking for all containers - to fall through past here,
1919     * inv must have been an container and must have been active.
1920     *
1921     * Change the color so that the message doesn't disappear with
1922     * all the others.
1923     */
1924     if (pl->contr->usekeys == key_inventory ||
1925     !QUERY_FLAG(container, FLAG_APPLIED) ||
1926     (pl->contr->usekeys == keyrings &&
1927     (!container->race || strcmp(container->race, "keys")))
1928     ) {
1929     new_draw_info_format(NDI_UNIQUE|NDI_BROWN, 0, pl,
1930     "The %s in your %s vibrates as you approach the door",
1931     query_name(tmp), query_name(container));
1932     return NULL;
1933     }
1934     }
1935     return tmp;
1936     }
1937    
1938     /* moved door processing out of move_player_attack.
1939     * returns 1 if player has opened the door with a key
1940     * such that the caller should not do anything more,
1941     * 0 otherwise
1942     */
1943     static int player_attack_door(object *op, object *door)
1944     {
1945    
1946     /* If its a door, try to find a use a key. If we do destroy the door,
1947     * might as well return immediately as there is nothing more to do -
1948     * otherwise, we fall through to the rest of the code.
1949     */
1950     object *key=find_key(op, op, door);
1951    
1952     /* IF we found a key, do some extra work */
1953     if (key) {
1954     object *container=key->env;
1955    
1956     play_sound_map(op->map, op->x, op->y, SOUND_OPEN_DOOR);
1957     if(action_makes_visible(op)) make_visible(op);
1958     if(door->inv &&(door->inv->type ==RUNE || door->inv->type ==TRAP)) spring_trap(door->inv,op);
1959     if (door->type == DOOR) {
1960     hit_player(door,9998,op,AT_PHYSICAL,1); /* Break through the door */
1961     }
1962     else if(door->type==LOCKED_DOOR) {
1963     new_draw_info_format(NDI_UNIQUE, NDI_BROWN, op,
1964     "You open the door with the %s", query_short_name(key));
1965     remove_door2(door); /* remove door without violence ;-) */
1966     }
1967     /* Do this after we print the message */
1968     decrease_ob(key); /* Use up one of the keys */
1969     /* Need to update the weight the container the key was in */
1970     if (container != op)
1971     esrv_update_item(UPD_WEIGHT, op, container);
1972     return 1; /* Nothing more to do below */
1973     } else if (door->type==LOCKED_DOOR) {
1974     /* Might as well return now - no other way to open this */
1975     new_draw_info(NDI_UNIQUE | NDI_NAVY, 0, op, door->msg);
1976     return 1;
1977     }
1978     return 0;
1979     }
1980    
1981     /* This function is just part of a breakup from move_player.
1982     * It should keep the code cleaner.
1983     * When this is called, the players direction has been updated
1984     * (taking into account confusion.) The player is also actually
1985     * going to try and move (not fire weapons).
1986     */
1987    
1988     void move_player_attack(object *op, int dir)
1989     {
1990 elmex 1.1.1.2 object *tmp, *mon, *tpl;
1991     sint16 nx, ny;
1992 root 1.1 int on_battleground;
1993     mapstruct *m;
1994    
1995 elmex 1.1.1.2 if (op->contr->transport) tpl = op->contr->transport;
1996     else tpl = op;
1997     nx=freearr_x[dir]+tpl->x;
1998     ny=freearr_y[dir]+tpl->y;
1999    
2000     on_battleground = op_on_battleground(tpl, NULL, NULL);
2001 root 1.1
2002     /* If braced, or can't move to the square, and it is not out of the
2003     * map, attack it. Note order of if statement is important - don't
2004     * want to be calling move_ob if braced, because move_ob will move the
2005     * player. This is a pretty nasty hack, because if we could
2006     * move to some space, it then means that if we are braced, we should
2007     * do nothing at all. As it is, if we are braced, we go through
2008     * quite a bit of processing. However, it probably is less than what
2009     * move_ob uses.
2010     */
2011 elmex 1.1.1.2 if ((op->contr->braced || !move_ob(tpl,dir,tpl)) && !out_of_map(tpl->map,nx,ny)) {
2012     if (OUT_OF_REAL_MAP(tpl->map, nx, ny)) {
2013     m = get_map_from_coord(tpl->map, &nx, &ny);
2014 root 1.1 if (!m) return; /* Don't think this should happen */
2015     }
2016 elmex 1.1.1.2 else m =tpl->map;
2017 root 1.1
2018     if ((tmp=get_map_ob(m,nx,ny))==NULL) {
2019     /* LOG(llevError,"player_move_attack: get_map_ob returns NULL, but player can not more there.\n");*/
2020     return;
2021     }
2022    
2023     mon = NULL;
2024     /* Go through all the objects, and find ones of interest. Only stop if
2025     * we find a monster - that is something we know we want to attack.
2026     * if its a door or barrel (can roll) see if there may be monsters
2027     * on the space
2028     */
2029     while (tmp!=NULL) {
2030     if (tmp == op) {
2031     tmp=tmp->above;
2032     continue;
2033     }
2034     if (QUERY_FLAG(tmp,FLAG_ALIVE)) {
2035     mon = tmp;
2036     break;
2037     }
2038     if (tmp->type==LOCKED_DOOR || QUERY_FLAG(tmp,FLAG_CAN_ROLL))
2039     mon = tmp;
2040     tmp=tmp->above;
2041     }
2042    
2043     if (mon==NULL) /* This happens anytime the player tries to move */
2044     return; /* into a wall */
2045    
2046     if(mon->head != NULL)
2047     mon = mon->head;
2048    
2049     if ((mon->type==DOOR && mon->stats.hp>=0) || (mon->type==LOCKED_DOOR))
2050     if (player_attack_door(op, mon)) return;
2051    
2052     /* The following deals with possibly attacking peaceful
2053     * or frienddly creatures. Basically, all players are considered
2054     * unaggressive. If the moving player has peaceful set, then the
2055     * object should be pushed instead of attacked. It is assumed that
2056     * if you are braced, you will not attack friends accidently,
2057     * and thus will not push them.
2058     */
2059    
2060     /* If the creature is a pet, push it even if the player is not
2061     * peaceful. Our assumption is the creature is a pet if the
2062     * player owns it and it is either friendly or unagressive.
2063     */
2064     if ((op->type==PLAYER) && get_owner(mon)==op &&
2065     (QUERY_FLAG(mon,FLAG_UNAGGRESSIVE) || QUERY_FLAG(mon, FLAG_FRIENDLY)))
2066     {
2067     /* If we're braced, we don't want to switch places with it */
2068     if (op->contr->braced) return;
2069 elmex 1.1.1.2 play_sound_map(tpl->map, tpl->x, tpl->y, SOUND_PUSH_PLAYER);
2070 root 1.1 (void) push_ob(mon,dir,op);
2071     if(op->contr->tmp_invis||op->hide) make_visible(op);
2072     return;
2073     }
2074    
2075     /* in certain circumstances, you shouldn't attack friendly
2076     * creatures. Note that if you are braced, you can't push
2077     * someone, but put it inside this loop so that you won't
2078     * attack them either.
2079     */
2080     if ((mon->type==PLAYER || mon->enemy != op) &&
2081     (mon->type==PLAYER || QUERY_FLAG(mon,FLAG_UNAGGRESSIVE) || QUERY_FLAG(mon, FLAG_FRIENDLY)) &&
2082     (op->contr->peaceful && !on_battleground)) {
2083     if (!op->contr->braced) {
2084 elmex 1.1.1.2 play_sound_map(tpl->map, tpl->x, tpl->y, SOUND_PUSH_PLAYER);
2085 root 1.1 (void) push_ob(mon,dir,op);
2086     } else {
2087     new_draw_info(0, 0,op,"You withhold your attack");
2088     }
2089     if(op->contr->tmp_invis||op->hide) make_visible(op);
2090     }
2091    
2092     /* If the object is a boulder or other rollable object, then
2093     * roll it if not braced. You can't roll it if you are braced.
2094     */
2095     else if(QUERY_FLAG(mon,FLAG_CAN_ROLL)&&(!op->contr->braced)) {
2096     recursive_roll(mon,dir,op);
2097     if(action_makes_visible(op)) make_visible(op);
2098     }
2099    
2100     /* Any generic living creature. Including things like doors.
2101     * Way it works is like this: First, it must have some hit points
2102     * and be living. Then, it must be one of the following:
2103     * 1) Not a player, 2) A player, but of a different party. Note
2104     * that party_number -1 is no party, so attacks can still happen.
2105     */
2106    
2107     else if ((mon->stats.hp>=0) && QUERY_FLAG(mon, FLAG_ALIVE) &&
2108     ((mon->type!=PLAYER || op->contr->party==NULL ||
2109     op->contr->party!=mon->contr->party))) {
2110    
2111     /* If the player hasn't hit something this tick, and does
2112     * so, give them speed boost based on weapon speed. Doing
2113     * it here is better than process_players2, which basically
2114     * incurred a 1 tick offset.
2115     */
2116     if (!op->contr->has_hit) {
2117     op->speed_left += op->speed / op->contr->weapon_sp;
2118    
2119     op->contr->has_hit = 1; /* The last action was to hit, so use weapon_sp */
2120     }
2121    
2122     skill_attack(mon, op, 0, NULL, NULL);
2123    
2124     /* If attacking another player, that player gets automatic
2125     * hitback, and doesn't loose luck either.
2126     * Disable hitback on the battleground or if the target is
2127     * the wiz.
2128     */
2129     if (mon->type == PLAYER && mon->stats.hp >= 0 && !mon->contr->has_hit &&
2130     !on_battleground && !QUERY_FLAG(mon, FLAG_WIZ)) {
2131     short luck = mon->stats.luck;
2132     mon->contr->has_hit = 1;
2133     skill_attack(op, mon, 0, NULL, NULL);
2134     mon->stats.luck = luck;
2135     }
2136     if(action_makes_visible(op)) make_visible(op);
2137     }
2138     } /* if player should attack something */
2139     }
2140    
2141     int move_player(object *op,int dir) {
2142     int pick;
2143 elmex 1.1.1.2 object *transport = op->contr->transport;
2144 root 1.1
2145 elmex 1.1.1.2 if(!transport && (op->map == NULL || op->map->in_memory != MAP_IN_MEMORY))
2146 root 1.1 return 0;
2147    
2148     /* Sanity check: make sure dir is valid */
2149     if ( ( dir < 0 ) || ( dir >= 9 ) ) {
2150     LOG( llevError, "move_player: invalid direction %d\n", dir);
2151     return 0;
2152     }
2153    
2154     /* peterm: added following line */
2155     if(QUERY_FLAG(op,FLAG_CONFUSED) && dir)
2156     dir = absdir(dir + RANDOM()%3 + RANDOM()%3 - 2);
2157    
2158     op->facing = dir;
2159    
2160 elmex 1.1.1.2 if(!transport && op->hide) do_hidden_move(op);
2161    
2162     if (transport) {
2163     /* transport->contr is set up for the person in charge of the boat.
2164     * if that isn't this person, he can't steer it, etc
2165     */
2166     if (transport->contr != op->contr) return 0;
2167    
2168     /* Transport can't move. But update dir so it at least
2169     * will point in the same direction if player is running.
2170     */
2171     if (transport->speed_left < 0.0) {
2172     transport->direction = dir;
2173     op->direction = dir;
2174     return 0;
2175     }
2176     /* Remove transport speed. Give player just a little speed -
2177     * enough so that they will get an action again quickly.
2178     *
2179     */
2180     transport->speed_left -= 1.0;
2181     if (op->speed_left < 0.0) op->speed_left = -0.01;
2182    
2183     }
2184 root 1.1
2185     if(op->contr->fire_on) {
2186     fire(op,dir);
2187     }
2188     else move_player_attack(op,dir);
2189    
2190     pick = check_pick(op);
2191    
2192    
2193     /* Add special check for newcs players and fire on - this way, the
2194     * server can handle repeat firing.
2195     */
2196     if (op->contr->fire_on || (op->contr->run_on && pick!=0)) {
2197     op->direction = dir;
2198     } else {
2199     op->direction=0;
2200     }
2201     /* Update how the player looks. Use the facing, so direction may
2202     * get reset to zero. This allows for full animation capabilities
2203     * for players.
2204     */
2205 elmex 1.1.1.2 if (!transport) animate_object(op, op->facing);
2206 root 1.1 return 0;
2207     }
2208    
2209     /* This is similar to handle_player, below, but is only used by the
2210     * new client/server stuff.
2211     * This is sort of special, in that the new client/server actually uses
2212     * the new speed values for commands.
2213     *
2214     * Returns true if there are more actions we can do.
2215     */
2216     int handle_newcs_player(object *op)
2217     {
2218     if (op->contr->hidden) {
2219     op->invisible = 1000;
2220     /* the socket code flasehs the player visible/invisible
2221     * depending on the value if invisible, so we need to
2222     * alternate it here for it to work correctly.
2223     */
2224     if (pticks & 2) op->invisible--;
2225     }
2226     else if(op->invisible&&!(QUERY_FLAG(op,FLAG_MAKE_INVIS))) {
2227     op->invisible--;
2228     if(!op->invisible) {
2229     make_visible(op);
2230     new_draw_info(NDI_UNIQUE, 0, op, "Your invisibility spell runs out.");
2231     }
2232     }
2233    
2234     if (QUERY_FLAG(op, FLAG_SCARED)) {
2235     flee_player(op);
2236     /* If player is still scared, that is his action for this tick */
2237     if (QUERY_FLAG(op, FLAG_SCARED)) {
2238     op->speed_left--;
2239     return 0;
2240     }
2241     }
2242    
2243     /* I've been seeing crashes where the golem has been destroyed, but
2244     * the player object still points to the defunct golem. The code that
2245     * destroys the golem looks correct, and it doesn't always happen, so
2246     * put this in a a workaround to clean up the golem pointer.
2247     */
2248     if (op->contr->ranges[range_golem] &&
2249     ((op->contr->golem_count != op->contr->ranges[range_golem]->count) ||
2250     QUERY_FLAG(op->contr->ranges[range_golem], FLAG_REMOVED))) {
2251     op->contr->ranges[range_golem] = NULL;
2252     op->contr->golem_count = 0;
2253     }
2254    
2255     /* call this here - we also will call this in do_ericserver, but
2256     * the players time has been increased when doericserver has been
2257     * called, so we recheck it here.
2258     */
2259     HandleClient(&op->contr->socket, op->contr);
2260     if (op->speed_left<0) return 0;
2261    
2262     if(op->direction && (op->contr->run_on || op->contr->fire_on)) {
2263     /* All move commands take 1 tick, at least for now */
2264     op->speed_left--;
2265    
2266     /* Instead of all the stuff below, let move_player take care
2267     * of it. Also, some of the skill stuff is only put in
2268     * there, as well as the confusion stuff.
2269     */
2270     move_player(op, op->direction);
2271     if (op->speed_left>0) return 1;
2272     else return 0;
2273     }
2274     return 0;
2275     }
2276    
2277     int save_life(object *op) {
2278     object *tmp;
2279    
2280     if(!QUERY_FLAG(op,FLAG_LIFESAVE))
2281     return 0;
2282    
2283     for(tmp=op->inv;tmp!=NULL;tmp=tmp->below)
2284     if(QUERY_FLAG(tmp, FLAG_APPLIED)&&QUERY_FLAG(tmp,FLAG_LIFESAVE)) {
2285     play_sound_map(op->map, op->x, op->y, SOUND_OB_EVAPORATE);
2286     new_draw_info_format(NDI_UNIQUE, 0,op,
2287     "Your %s vibrates violently, then evaporates.",
2288     query_name(tmp));
2289     if (op->contr)
2290     esrv_del_item(op->contr, tmp->count);
2291     remove_ob(tmp);
2292     free_object(tmp);
2293     CLEAR_FLAG(op, FLAG_LIFESAVE);
2294     if(op->stats.hp<0)
2295     op->stats.hp = op->stats.maxhp;
2296     if(op->stats.food<0)
2297     op->stats.food = 999;
2298     fix_player(op);
2299     return 1;
2300     }
2301     LOG(llevError,"Error: LIFESAVE set without applied object.\n");
2302     CLEAR_FLAG(op, FLAG_LIFESAVE);
2303     enter_player_savebed(op); /* bring him home. */
2304     return 0;
2305     }
2306    
2307     /* This goes throws the inventory and removes unpaid objects, and puts them
2308     * back in the map (location and map determined by values of env). This
2309     * function will descend into containers. op is the object to start the search
2310     * from.
2311     */
2312     void remove_unpaid_objects(object *op, object *env)
2313     {
2314     object *next;
2315    
2316     while (op) {
2317     next=op->below; /* Make sure we have a good value, in case
2318     * we remove object 'op'
2319     */
2320     if (QUERY_FLAG(op, FLAG_UNPAID)) {
2321     remove_ob(op);
2322     op->x = env->x;
2323     op->y = env->y;
2324     if (env->type == PLAYER)
2325     esrv_del_item(env->contr, op->count);
2326     insert_ob_in_map(op, env->map, NULL,0);
2327     }
2328     else if (op->inv) remove_unpaid_objects(op->inv, env);
2329     op=next;
2330     }
2331     }
2332    
2333    
2334     /*
2335     * Returns pointer a static string containing gravestone text
2336     * Moved from apply.c to player.c - player.c is what
2337     * actually uses this function. player.c may not be quite the
2338     * best, a misc file for object actions is probably better,
2339     * but there isn't one in the server directory.
2340     */
2341     char *gravestone_text (object *op)
2342     {
2343     static char buf2[MAX_BUF];
2344     char buf[MAX_BUF];
2345     time_t now = time (NULL);
2346    
2347     strcpy (buf2, " R.I.P.\n\n");
2348     if (op->type == PLAYER)
2349     sprintf (buf, "%s the %s\n", op->name, op->contr->title);
2350     else
2351     sprintf (buf, "%s\n", op->name);
2352     strncat (buf2, " ", 20 - strlen (buf) / 2);
2353     strcat (buf2, buf);
2354     if (op->type == PLAYER)
2355     sprintf (buf, "who was in level %d when killed\n", op->level);
2356     else
2357     sprintf (buf, "who was in level %d when died.\n\n", op->level);
2358     strncat (buf2, " ", 20 - strlen (buf) / 2);
2359     strcat (buf2, buf);
2360     if (op->type == PLAYER) {
2361     sprintf (buf, "by %s.\n\n", op->contr->killer);
2362     strncat (buf2, " ", 21 - strlen (buf) / 2);
2363     strcat (buf2, buf);
2364     }
2365     strftime (buf, MAX_BUF, "%b %d %Y\n", localtime (&now));
2366     strncat (buf2, " ", 20 - strlen (buf) / 2);
2367     strcat (buf2, buf);
2368     return buf2;
2369     }
2370    
2371    
2372    
2373     void do_some_living(object *op) {
2374     int last_food=op->stats.food;
2375     int gen_hp, gen_sp, gen_grace;
2376     int over_hp, over_sp, over_grace;
2377     int i;
2378     int rate_hp = 1200;
2379     int rate_sp = 2500;
2380     int rate_grace = 2000;
2381     const int max_hp = 1;
2382     const int max_sp = 1;
2383     const int max_grace = 1;
2384    
2385     if (op->contr->outputs_sync) {
2386     for (i=0; i<NUM_OUTPUT_BUFS; i++)
2387     if (op->contr->outputs[i].buf!=NULL &&
2388     (op->contr->outputs[i].first_update+op->contr->outputs_sync)<pticks)
2389     flush_output_element(op, &op->contr->outputs[i]);
2390     }
2391    
2392     if(op->contr->state==ST_PLAYING) {
2393    
2394     /* these next three if clauses make it possible to SLOW DOWN
2395     hp/grace/spellpoint regeneration. */
2396     if(op->contr->gen_hp >= 0 )
2397     gen_hp=(op->contr->gen_hp+1)*op->stats.maxhp;
2398     else {
2399     gen_hp = op->stats.maxhp;
2400     rate_hp -= rate_hp/2 * op->contr->gen_hp;
2401     }
2402     if(op->contr->gen_sp >= 0 )
2403     gen_sp=(op->contr->gen_sp+1)*op->stats.maxsp;
2404     else {
2405     gen_sp = op->stats.maxsp;
2406     rate_sp -= rate_sp/2 * op->contr->gen_sp;
2407     }
2408     if(op->contr->gen_grace >= 0)
2409     gen_grace=(op->contr->gen_grace+1)*op->stats.maxgrace;
2410     else {
2411     gen_grace = op->stats.maxgrace;
2412     rate_grace -= rate_grace/2 * op->contr->gen_grace;
2413     }
2414    
2415     /* Regenerate Spell Points */
2416     if(op->contr->ranges[range_golem]==NULL && --op->last_sp<0) {
2417     gen_sp = gen_sp * 10 / (op->contr->gen_sp_armour < 10? 10 : op->contr->gen_sp_armour);
2418     if(op->stats.sp<op->stats.maxsp) {
2419     op->stats.sp++;
2420     /* dms do not consume food */
2421     if (!QUERY_FLAG(op,FLAG_WIZ)) {
2422     op->stats.food--;
2423     if(op->contr->digestion<0)
2424     op->stats.food+=op->contr->digestion;
2425     else if(op->contr->digestion>0 &&
2426     random_roll(0, op->contr->digestion, op, PREFER_HIGH))
2427     op->stats.food=last_food;
2428     }
2429     }
2430     if (max_sp>1) {
2431     over_sp = (gen_sp+10)/rate_sp;
2432     if (over_sp > 0) {
2433     if(op->stats.sp<op->stats.maxsp) {
2434     op->stats.sp += over_sp>max_sp ? max_sp : over_sp;
2435     if(random_roll(0, rate_sp-1, op, PREFER_LOW) > ((gen_sp+10)%rate_sp))
2436     op->stats.sp--;
2437     if(op->stats.sp>op->stats.maxsp)
2438     op->stats.sp=op->stats.maxsp;
2439     }
2440     op->last_sp=0;
2441     } else {
2442     op->last_sp=rate_sp/(gen_sp<20 ? 30 : gen_sp+10);
2443     }
2444     } else {
2445     op->last_sp=rate_sp/(gen_sp<20 ? 30 : gen_sp+10);
2446     }
2447     }
2448    
2449     /* Regenerate Grace */
2450     /* I altered this a little - maximum grace is ony achieved through prayer -b.t.*/
2451     if(--op->last_grace<0) {
2452     if(op->stats.grace<op->stats.maxgrace/2)
2453     op->stats.grace++; /* no penalty in food for regaining grace */
2454     if(max_grace>1) {
2455     over_grace = (gen_grace<20 ? 30 : gen_grace+10)/rate_grace;
2456     if (over_grace > 0) {
2457     op->stats.sp += over_grace
2458     + (random_roll(0, rate_grace-1, op, PREFER_HIGH) > ((gen_grace<20 ? 30 : gen_grace+10)%rate_grace))? -1 : 0;
2459     op->last_grace=0;
2460     } else {
2461     op->last_grace=rate_grace/(gen_grace<20 ? 30 : gen_grace+10);
2462     }
2463     } else {
2464     op->last_grace=rate_grace/(gen_grace<20 ? 30 : gen_grace+10);
2465     }
2466     /* wearing stuff doesn't detract from grace generation. */
2467     }
2468    
2469     /* Regenerate Hit Points */
2470     if(--op->last_heal<0) {
2471     if(op->stats.hp<op->stats.maxhp) {
2472     op->stats.hp++;
2473     /* dms do not consume food */
2474     if (!QUERY_FLAG(op,FLAG_WIZ)) {
2475     op->stats.food--;
2476     if(op->contr->digestion<0)
2477     op->stats.food+=op->contr->digestion;
2478     else if(op->contr->digestion>0 &&
2479     random_roll(0, op->contr->digestion, op, PREFER_HIGH))
2480     op->stats.food=last_food;
2481     }
2482     }
2483     if(max_hp>1) {
2484     over_hp = (gen_hp<20 ? 30 : gen_hp+10)/rate_hp;
2485     if (over_hp > 0) {
2486     op->stats.sp += over_hp
2487     + (RANDOM()%rate_hp > ((gen_hp<20 ? 30 : gen_hp+10)%rate_hp))? -1 : 0;
2488     op->last_heal=0;
2489     } else {
2490     op->last_heal=rate_hp/(gen_hp<20 ? 30 : gen_hp+10);
2491     }
2492     } else {
2493     op->last_heal=rate_hp/(gen_hp<20 ? 30 : gen_hp+10);
2494     }
2495     }
2496    
2497     /* Digestion */
2498     if(--op->last_eat<0) {
2499     int bonus=op->contr->digestion>0?op->contr->digestion:0,
2500     penalty=op->contr->digestion<0?-op->contr->digestion:0;
2501     if(op->contr->gen_hp > 0)
2502     op->last_eat=25*(1+bonus)/(op->contr->gen_hp+penalty+1);
2503     else
2504     op->last_eat=25*(1+bonus)/(penalty +1);
2505     /* dms do not consume food */
2506     if (!QUERY_FLAG(op,FLAG_WIZ)) op->stats.food--;
2507     }
2508     }
2509    
2510     if(op->contr->state==ST_PLAYING&&op->stats.food<0&&op->stats.hp>=0) {
2511     object *tmp, *flesh=NULL;
2512    
2513     for(tmp=op->inv;tmp!=NULL;tmp=tmp->below) {
2514     if(!QUERY_FLAG(tmp, FLAG_UNPAID)) {
2515     if (tmp->type==FOOD || tmp->type==DRINK || tmp->type==POISON) {
2516     new_draw_info(NDI_UNIQUE, 0,op,"You blindly grab for a bite of food.");
2517     manual_apply(op,tmp,0);
2518     if(op->stats.food>=0||op->stats.hp<0)
2519     break;
2520     }
2521     else if (tmp->type==FLESH) flesh=tmp;
2522     } /* End if paid for object */
2523     } /* end of for loop */
2524     /* If player is still starving, it means they don't have any food, so
2525     * eat flesh instead.
2526     */
2527     if (op->stats.food<0 && op->stats.hp>=0 && flesh) {
2528     new_draw_info(NDI_UNIQUE, 0,op,"You blindly grab for a bite of food.");
2529     manual_apply(op,flesh,0);
2530     }
2531     } /* end if player is starving */
2532    
2533     while(op->stats.food<0&&op->stats.hp>0)
2534     op->stats.food++,op->stats.hp--;
2535    
2536     if (!op->contr->state&&!QUERY_FLAG(op,FLAG_WIZ)&&(op->stats.hp<0||op->stats.food<0))
2537     kill_player(op);
2538     }
2539    
2540    
2541    
2542     /* If the player should die (lack of hp, food, etc), we call this.
2543     * op is the player in jeopardy. If the player can not be saved (not
2544     * permadeath, no lifesave), this will take care of removing the player
2545     * file.
2546     */
2547     void kill_player(object *op)
2548     {
2549     char buf[MAX_BUF];
2550     int x,y,i;
2551     mapstruct *map; /* this is for resurrection */
2552     int z;
2553     int num_stats_lose;
2554     int lost_a_stat;
2555     int lose_this_stat;
2556     int this_stat;
2557     int will_kill_again;
2558     archetype *at;
2559     object *tmp;
2560    
2561     if(save_life(op))
2562     return;
2563    
2564    
2565     /* If player dies on BATTLEGROUND, no stat/exp loss! For Combat-Arenas
2566     * in cities ONLY!!! It is very important that this doesn't get abused.
2567     * Look at op_on_battleground() for more info --AndreasV
2568     */
2569     if (op_on_battleground(op, &x, &y)) {
2570     new_draw_info(NDI_UNIQUE | NDI_NAVY, 0,op,
2571     "You have been defeated in combat!");
2572     new_draw_info(NDI_UNIQUE | NDI_NAVY, 0,op,
2573     "Local medics have saved your life...");
2574    
2575     /* restore player */
2576     at = find_archetype("poisoning");
2577     tmp=present_arch_in_ob(at,op);
2578     if (tmp) {
2579     remove_ob(tmp);
2580     free_object(tmp);
2581     new_draw_info(NDI_UNIQUE, 0,op, "Your body feels cleansed");
2582     }
2583    
2584     at = find_archetype("confusion");
2585     tmp=present_arch_in_ob(at,op);
2586     if (tmp) {
2587     remove_ob(tmp);
2588     free_object(tmp);
2589     new_draw_info(NDI_UNIQUE, 0,tmp, "Your mind feels clearer");
2590     }
2591    
2592     cure_disease(op,0); /* remove any disease */
2593     op->stats.hp=op->stats.maxhp;
2594     if (op->stats.food<=0) op->stats.food=999;
2595    
2596     /* create a bodypart-trophy to make the winner happy */
2597     tmp=arch_to_object(find_archetype("finger"));
2598     if (tmp != NULL)
2599     {
2600     sprintf(buf,"%s's finger",op->name);
2601     tmp->name = add_string(buf);
2602     sprintf(buf," This finger has been cut off %s\n"
2603     " the %s, when he was defeated at\n level %d by %s.\n",
2604     op->name, op->contr->title, (int)(op->level),
2605     op->contr->killer);
2606     tmp->msg=add_string(buf);
2607     tmp->value=0, tmp->material=0, tmp->type=0;
2608     tmp->materialname = NULL;
2609     tmp->x = op->x, tmp->y = op->y;
2610     insert_ob_in_map(tmp,op->map,op,0);
2611     }
2612    
2613     /* teleport defeated player to new destination*/
2614     transfer_ob(op, x, y, 0, NULL);
2615     op->contr->braced=0;
2616     return;
2617     }
2618    
2619     /* Lauwenmark: Handle for plugin death event */
2620     if (execute_event(op, EVENT_DEATH,NULL,NULL,NULL,SCRIPT_FIX_ALL) != 0)
2621     return;
2622    
2623     /* Lauwenmark: Handle for the global death event */
2624     execute_global_event(EVENT_PLAYER_DEATH, op);
2625     if(op->stats.food<0) {
2626     if (op->contr->explore) {
2627     new_draw_info(NDI_UNIQUE, 0,op,"You would have starved, but you are");
2628     new_draw_info(NDI_UNIQUE, 0,op,"in explore mode, so...");
2629     op->stats.food=999;
2630     return;
2631     }
2632     sprintf(buf,"%s starved to death.",op->name);
2633     strcpy(op->contr->killer,"starvation");
2634     }
2635     else {
2636     if (op->contr->explore) {
2637     new_draw_info(NDI_UNIQUE, 0,op,"You would have died, but you are");
2638     new_draw_info(NDI_UNIQUE, 0,op,"in explore mode, so...");
2639     op->stats.hp=op->stats.maxhp;
2640     return;
2641     }
2642     sprintf(buf,"%s died.",op->name);
2643     }
2644     play_sound_player_only(op->contr, SOUND_PLAYER_DIES,0,0);
2645    
2646     /* save the map location for corpse, gravestone*/
2647     x=op->x;y=op->y;map=op->map;
2648    
2649    
2650     if (settings.not_permadeth == TRUE) {
2651     /* NOT_PERMADEATH code. This basically brings the character back to
2652     * life if they are dead - it takes some exp and a random stat.
2653     * See the config.h file for a little more in depth detail about this.
2654     */
2655    
2656     /* Basically two ways to go - remove a stat permanently, or just
2657     * make it depletion. This bunch of code deals with that aspect
2658     * of death.
2659     */
2660    
2661     if (settings.balanced_stat_loss) {
2662     /* If stat loss is permanent, lose one stat only. */
2663     /* Lower level chars don't lose as many stats because they suffer
2664     more if they do. */
2665     /* Higher level characters can afford things such as potions of
2666     restoration, or better, stat potions. So we slug them that
2667     little bit harder. */
2668     /* GD */
2669     if (settings.stat_loss_on_death)
2670     num_stats_lose = 1;
2671     else
2672     num_stats_lose = 1 + op->level/BALSL_NUMBER_LOSSES_RATIO;
2673     } else {
2674     num_stats_lose = 1;
2675     }
2676     lost_a_stat = 0;
2677    
2678     for (z=0; z<num_stats_lose; z++) {
2679     if (settings.stat_loss_on_death) {
2680     /* Pick a random stat and take a point off it. Tell the player
2681     * what he lost.
2682     */
2683     i = RANDOM() % 7;
2684     change_attr_value(&(op->stats), i,-1);
2685     check_stat_bounds(&(op->stats));
2686     change_attr_value(&(op->contr->orig_stats), i,-1);
2687     check_stat_bounds(&(op->contr->orig_stats));
2688     new_draw_info(NDI_UNIQUE, 0,op, lose_msg[i]);
2689     lost_a_stat = 1;
2690     } else {
2691     /* deplete a stat */
2692     archetype *deparch=find_archetype("depletion");
2693     object *dep;
2694    
2695     i = RANDOM() % 7;
2696     dep = present_arch_in_ob(deparch,op);
2697     if(!dep) {
2698     dep = arch_to_object(deparch);
2699     insert_ob_in_ob(dep, op);
2700     }
2701     lose_this_stat = 1;
2702     if (settings.balanced_stat_loss) {
2703     /* GD */
2704     /* Get the stat that we're about to deplete. */
2705     this_stat = get_attr_value(&(dep->stats), i);
2706     if (this_stat < 0) {
2707     int loss_chance = 1 + op->level/BALSL_LOSS_CHANCE_RATIO;
2708     int keep_chance = this_stat * this_stat;
2709     /* Yes, I am paranoid. Sue me. */
2710     if (keep_chance < 1)
2711     keep_chance = 1;
2712    
2713     /* There is a maximum depletion total per level. */
2714     if (this_stat < -1 - op->level/BALSL_MAX_LOSS_RATIO) {
2715     lose_this_stat = 0;
2716     /* Take loss chance vs keep chance to see if we
2717     retain the stat. */
2718     } else {
2719     if (random_roll(0, loss_chance + keep_chance-1,
2720     op, PREFER_LOW) < keep_chance)
2721     lose_this_stat = 0;
2722     /* LOG(llevDebug, "Determining stat loss. Stat: %d Keep: %d Lose: %d Result: %s.\n",
2723     this_stat, keep_chance, loss_chance,
2724     lose_this_stat?"LOSE":"KEEP"); */
2725     }
2726     }
2727     }
2728    
2729     if (lose_this_stat) {
2730     this_stat = get_attr_value(&(dep->stats), i);
2731     /* We could try to do something clever like find another
2732     * stat to reduce if this fails. But chances are, if
2733     * stats have been depleted to -50, all are pretty low
2734     * and should be roughly the same, so it shouldn't make a
2735     * difference.
2736     */
2737     if (this_stat>=-50) {
2738     change_attr_value(&(dep->stats), i, -1);
2739     SET_FLAG(dep, FLAG_APPLIED);
2740     new_draw_info(NDI_UNIQUE, 0,op, lose_msg[i]);
2741     fix_player(op);
2742     lost_a_stat = 1;
2743     }
2744     }
2745     }
2746     }
2747     /* If no stat lost, tell the player. */
2748     if (!lost_a_stat)
2749     {
2750     /* determine_god() seems to not work sometimes... why is this?
2751     Should I be using something else? GD */
2752     const char *god = determine_god(op);
2753     if (god && (strcmp(god, "none")))
2754     new_draw_info_format(NDI_UNIQUE, 0, op, "For a brief "
2755     "moment you feel the holy presence of %s protecting"
2756     " you.", god);
2757     else
2758     new_draw_info(NDI_UNIQUE, 0, op, "For a brief moment you"
2759     " feel a holy presence protecting you.");
2760     }
2761    
2762     /* Put a gravestone up where the character 'almost' died. List the
2763     * exp loss on the stone.
2764     */
2765     tmp=arch_to_object(find_archetype("gravestone"));
2766     sprintf(buf,"%s's gravestone",op->name);
2767     FREE_AND_COPY(tmp->name, buf);
2768     sprintf(buf,"%s's gravestones",op->name);
2769     FREE_AND_COPY(tmp->name_pl, buf);
2770     sprintf(buf,"RIP\nHere rests the hero %s the %s,\n"
2771     "who was killed\n"
2772     "by %s.\n",
2773     op->name, op->contr->title,
2774     op->contr->killer);
2775     tmp->msg = add_string(buf);
2776     tmp->x=op->x,tmp->y=op->y;
2777     insert_ob_in_map (tmp, op->map, NULL,0);
2778    
2779     /**************************************/
2780     /* */
2781     /* Subtract the experience points, */
2782     /* if we died cause of food, give us */
2783     /* food, and reset HP's... */
2784     /* */
2785     /**************************************/
2786    
2787     /* remove any poisoning and confusion the character may be suffering.*/
2788     /* restore player */
2789     at = find_archetype("poisoning");
2790     tmp=present_arch_in_ob(at,op);
2791     if (tmp) {
2792     remove_ob(tmp);
2793     free_object(tmp);
2794     new_draw_info(NDI_UNIQUE, 0,op, "Your body feels cleansed");
2795     }
2796    
2797     at = find_archetype("confusion");
2798     tmp=present_arch_in_ob(at,op);
2799     if (tmp) {
2800     remove_ob(tmp);
2801     free_object(tmp);
2802     new_draw_info(NDI_UNIQUE, 0,tmp, "Your mind feels clearer");
2803     }
2804     cure_disease(op,0); /* remove any disease */
2805    
2806     /*add_exp(op, (op->stats.exp * -0.20)); */
2807     apply_death_exp_penalty(op);
2808     if(op->stats.food < 100) op->stats.food = 900;
2809     op->stats.hp = op->stats.maxhp;
2810     op->stats.sp = MAX(op->stats.sp, op->stats.maxsp);
2811     op->stats.grace = MAX(op->stats.grace, op->stats.maxgrace);
2812    
2813     /*
2814     * Check to see if the player is in a shop. IF so, then check to see if
2815     * the player has any unpaid items. If so, remove them and put them back
2816     * in the map.
2817     */
2818    
2819     for (tmp= get_map_ob(op->map, op->x, op->y); tmp; tmp=tmp->above) {
2820     if (tmp->type == SHOP_FLOOR) {
2821     remove_unpaid_objects(op->inv, op);
2822     break;
2823     }
2824     }
2825    
2826    
2827     /****************************************/
2828     /* */
2829     /* Move player to his current respawn- */
2830     /* position (usually last savebed) */
2831     /* */
2832     /****************************************/
2833    
2834     enter_player_savebed(op);
2835    
2836     /* Save the player before inserting the force to reduce
2837     * chance of abuse.
2838     */
2839     op->contr->braced=0;
2840     save_player(op,1);
2841    
2842     /* it is possible that the player has blown something up
2843     * at his savebed location, and that can have long lasting
2844     * spell effects. So first see if there is a spell effect
2845     * on the space that might harm the player.
2846     */
2847     will_kill_again=0;
2848     for (tmp= get_map_ob(op->map, op->x, op->y); tmp; tmp=tmp->above) {
2849     if (tmp->type ==SPELL_EFFECT)
2850     will_kill_again|=tmp->attacktype;
2851     }
2852     if (will_kill_again) {
2853     object *force;
2854     int at;
2855    
2856     force=get_archetype(FORCE_NAME);
2857     /* 50 ticks should be enough time for the spell to abate */
2858     force->speed=0.1;
2859     force->speed_left=-5.0;
2860     SET_FLAG(force, FLAG_APPLIED);
2861     for (at=0; at<NROFATTACKS; at++) {
2862     if (will_kill_again & (1 << at))
2863     force->resist[at] = 100;
2864     }
2865     insert_ob_in_ob(force, op);
2866     fix_player(op);
2867    
2868     }
2869     /**************************************/
2870     /* */
2871     /* Repaint the characters inv, and */
2872     /* stats, and show a nasty message ;) */
2873     /* */
2874     /**************************************/
2875    
2876     new_draw_info(NDI_UNIQUE, 0,op,"YOU HAVE DIED.");
2877     return;
2878     } /* NOT_PERMADETH */
2879     else {
2880     /* If NOT_PERMADETH is set, then the rest of this is not reachable. This
2881     * should probably be embedded in an else statement.
2882     */
2883    
2884     op->contr->party=NULL;
2885     if (settings.set_title == TRUE)
2886     op->contr->own_title[0]='\0';
2887     new_draw_info(NDI_UNIQUE|NDI_ALL, 0,NULL, buf);
2888     check_score(op);
2889     if(op->contr->ranges[range_golem]!=NULL) {
2890     remove_friendly_object(op->contr->ranges[range_golem]);
2891     remove_ob(op->contr->ranges[range_golem]);
2892     free_object(op->contr->ranges[range_golem]);
2893     op->contr->ranges[range_golem]=NULL;
2894     op->contr->golem_count=0;
2895     }
2896     loot_object(op); /* Remove some of the items for good */
2897     remove_ob(op);
2898     op->direction=0;
2899    
2900     if(!QUERY_FLAG(op,FLAG_WAS_WIZ)&&op->stats.exp) {
2901     delete_character(op->name,0);
2902     if (settings.resurrection == TRUE) {
2903     /* save playerfile sans equipment when player dies
2904     ** then save it as player.pl.dead so that future resurrection
2905     ** type spells will work on them nicely
2906     */
2907     delete_character(op->name,0);
2908     op->stats.hp = op->stats.maxhp;
2909     op->stats.food = 999;
2910    
2911     /* set the location of where the person will reappear when */
2912     /* maybe resurrection code should fix map also */
2913     strcpy(op->contr->maplevel, settings.emergency_mapname);
2914     if(op->map!=NULL)
2915     op->map = NULL;
2916     op->x = settings.emergency_x;
2917     op->y = settings.emergency_y;
2918     save_player(op,0);
2919     op->map = map;
2920     /* please see resurrection.c: peterm */
2921     dead_player(op);
2922     } else {
2923     delete_character(op->name,1);
2924     }
2925     }
2926     play_again(op);
2927    
2928     /* peterm: added to create a corpse at deathsite. */
2929     tmp=arch_to_object(find_archetype("corpse_pl"));
2930     sprintf(buf,"%s", op->name);
2931     FREE_AND_COPY(tmp->name, buf);
2932     FREE_AND_COPY(tmp->name_pl, buf);
2933     tmp->level=op->level;
2934     tmp->x=x;tmp->y=y;
2935     if (tmp->msg)
2936     free_string(tmp->msg);
2937     tmp->msg = add_string (gravestone_text(op));
2938     SET_FLAG (tmp, FLAG_UNIQUE);
2939     insert_ob_in_map (tmp, map, NULL,0);
2940     }
2941     }
2942    
2943    
2944     void loot_object(object *op) { /* Grab and destroy some treasure */
2945     object *tmp,*tmp2,*next;
2946    
2947     if (op->container) { /* close open sack first */
2948     esrv_apply_container (op, op->container);
2949     }
2950    
2951     for(tmp=op->inv;tmp!=NULL;tmp=next) {
2952     next=tmp->below;
2953     if (tmp->type==EXPERIENCE || tmp->invisible) continue;
2954     remove_ob(tmp);
2955     tmp->x=op->x,tmp->y=op->y;
2956     if (tmp->type == CONTAINER) { /* empty container to ground */
2957     loot_object(tmp);
2958     }
2959     if(!QUERY_FLAG(tmp, FLAG_UNIQUE) && (QUERY_FLAG(tmp, FLAG_STARTEQUIP)
2960     || QUERY_FLAG(tmp,FLAG_NO_DROP) || !(RANDOM()%3))) {
2961     if(tmp->nrof>1) {
2962     tmp2=get_split_ob(tmp,1+RANDOM()%(tmp->nrof-1));
2963     free_object(tmp2);
2964     insert_ob_in_map(tmp,op->map,NULL,0);
2965     } else
2966     free_object(tmp);
2967     } else
2968     insert_ob_in_map(tmp,op->map,NULL,0);
2969     }
2970     }
2971    
2972     /*
2973     * fix_weight(): Check recursively the weight of all players, and fix
2974     * what needs to be fixed. Refresh windows and fix speed if anything
2975     * was changed.
2976     */
2977    
2978     void fix_weight(void) {
2979     player *pl;
2980     for (pl = first_player; pl != NULL; pl = pl->next) {
2981     int old = pl->ob->carrying, sum = sum_weight(pl->ob);
2982     if(old == sum)
2983     continue;
2984     fix_player(pl->ob);
2985     LOG(llevDebug,"Fixed inventory in %s (%d -> %d)\n",
2986     pl->ob->name, old, sum);
2987     }
2988     }
2989    
2990     void fix_luck(void) {
2991     player *pl;
2992     for (pl = first_player; pl != NULL; pl = pl->next)
2993     if (!pl->ob->contr->state)
2994     change_luck(pl->ob, 0);
2995     }
2996    
2997    
2998     /* cast_dust() - handles op throwing objects of type 'DUST'.
2999     * This is much simpler in the new spell code - we basically
3000     * just treat this as any other spell casting object.
3001     */
3002    
3003     void cast_dust (object *op, object *throw_ob, int dir) {
3004     object *skop, *spob;
3005    
3006     skop = find_skill_by_name(op, throw_ob->skill);
3007    
3008     /* casting POTION 'dusts' is really a use_magic_item skill */
3009     if(op->type==PLAYER && throw_ob->type==POTION && !skop) {
3010     LOG(llevError,"Player %s lacks critical skill use_magic_item!\n",
3011     op->name);
3012     return;
3013     }
3014     spob = throw_ob->inv;
3015     if (op->type==PLAYER && spob)
3016     new_draw_info_format(NDI_UNIQUE, 0,op,"You cast %s.",spob->name);
3017    
3018     cast_spell(op, throw_ob, dir, spob, NULL);
3019    
3020     if(!QUERY_FLAG(throw_ob,FLAG_REMOVED)) remove_ob(throw_ob);
3021     free_object(throw_ob);
3022     }
3023    
3024     void make_visible (object *op) {
3025     op->hide = 0;
3026     op->invisible = 0;
3027     if(op->type==PLAYER) {
3028     op->contr->tmp_invis = 0;
3029     if (op->contr->invis_race) FREE_AND_CLEAR_STR(op->contr->invis_race);
3030     }
3031     update_object(op,UP_OBJ_FACE);
3032     }
3033    
3034     int is_true_undead(object *op) {
3035     object *tmp=NULL;
3036    
3037     if(QUERY_FLAG(&op->arch->clone,FLAG_UNDEAD)) return 1;
3038    
3039     if(op->type==PLAYER)
3040     for(tmp=op->inv;tmp;tmp=tmp->below)
3041     if(tmp->type==EXPERIENCE && tmp->stats.Wis)
3042     if(QUERY_FLAG(tmp,FLAG_UNDEAD)) return 1;
3043     return 0;
3044     }
3045    
3046     /* look at the surrounding terrain to determine
3047     * the hideability of this object. Positive levels
3048     * indicate greater hideability.
3049     */
3050    
3051     int hideability(object *ob) {
3052     int i,level=0, mflag;
3053     sint16 x,y;
3054    
3055     if(!ob||!ob->map) return 0;
3056    
3057     /* so, on normal lighted maps, its hard to hide */
3058     level=ob->map->darkness - 2;
3059    
3060     /* this also picks up whether the object is glowing.
3061     * If you carry a light on a non-dark map, its not
3062     * as bad as carrying a light on a pitch dark map */
3063     if(has_carried_lights(ob)) level =- (10 + (2*ob->map->darkness));
3064    
3065     /* scan through all nearby squares for terrain to hide in */
3066     for(i=0,x=ob->x,y=ob->y;i<9;i++,x=ob->x+freearr_x[i],y=ob->y+freearr_y[i]) {
3067     mflag = get_map_flags(ob->map, NULL, x, y, NULL, NULL);
3068     if (mflag & P_OUT_OF_MAP) { continue; }
3069     if(mflag & P_BLOCKSVIEW) /* something to hide near! */
3070     level += 2;
3071     else /* open terrain! */
3072     level -= 1;
3073     }
3074    
3075     #if 0
3076     LOG(llevDebug,"hideability of %s is %d\n",ob->name,level);
3077     #endif
3078     return level;
3079     }
3080    
3081     /* For Hidden creatures - a chance of becoming 'unhidden'
3082     * every time they move - as we subtract off 'invisibility'
3083     * AND, for players, if they move into a ridiculously unhideable
3084     * spot (surrounded by clear terrain in broad daylight). -b.t.
3085     */
3086    
3087     void do_hidden_move (object *op) {
3088     int hide=0, num=random_roll(0, 19, op, PREFER_LOW);
3089     object *skop;
3090    
3091     if(!op || !op->map) return;
3092    
3093     skop = find_obj_by_type_subtype(op, SKILL, SK_HIDING);
3094    
3095     /* its *extremely* hard to run and sneak/hide at the same time! */
3096     if(op->type==PLAYER && op->contr->run_on) {
3097     if(!skop || num >= skop->level) {
3098     new_draw_info(NDI_UNIQUE,0,op,"You ran too much! You are no longer hidden!");
3099     make_visible(op);
3100     return;
3101     } else num += 20;
3102     }
3103     num += op->map->difficulty;
3104     hide = hideability(op); /* modify by terrain hidden level */
3105     num -= hide;
3106     if((op->type==PLAYER && hide<-10) || ((op->invisible-=num)<=0)) {
3107     make_visible(op);
3108     if(op->type==PLAYER) new_draw_info(NDI_UNIQUE, 0,op,
3109     "You moved out of hiding! You are visible!");
3110     }
3111     else if (op->type == PLAYER && skop) {
3112     change_exp(op, calc_skill_exp(op, NULL, skop), skop->skill, 0);
3113     }
3114     }
3115    
3116     /* determine if who is standing near a hostile creature. */
3117    
3118     int stand_near_hostile( object *who ) {
3119     object *tmp=NULL;
3120     int i,friendly=0,player=0, mflags;
3121     mapstruct *m;
3122     sint16 x,y;
3123    
3124     if(!who) return 0;
3125    
3126     if(who->type==PLAYER) player=1;
3127     else friendly = QUERY_FLAG(who,FLAG_FRIENDLY);
3128    
3129     /* search adjacent squares */
3130     for(i=1;i<9;i++) {
3131     x = who->x+freearr_x[i];
3132     y = who->y+freearr_y[i];
3133     m = who->map;
3134     mflags = get_map_flags(m, &m, x, y, &x, &y);
3135     /* space must be blocked if there is a monster. If not
3136     * blocked, don't need to check this space.
3137     */
3138     if (mflags & P_OUT_OF_MAP) continue;
3139     if (OB_TYPE_MOVE_BLOCK(who, GET_MAP_MOVE_BLOCK(m, x, y))) continue;
3140    
3141     for(tmp=get_map_ob(m,x,y);tmp;tmp=tmp->above) {
3142     if((player||friendly)
3143     &&QUERY_FLAG(tmp,FLAG_MONSTER)&&!QUERY_FLAG(tmp,FLAG_UNAGGRESSIVE))
3144     return 1;
3145     else if(tmp->type==PLAYER)
3146     {
3147     /*don't let a hidden DM prevent you from hiding*/
3148     if(!QUERY_FLAG(tmp, FLAG_WIZ) || tmp->contr->hidden == 0)
3149     return 1;
3150     }
3151     }
3152     }
3153     return 0;
3154     }
3155    
3156     /* check the player los field for viewability of the
3157     * object op. This function works fine for monsters,
3158     * but we dont worry if the object isnt the top one in
3159     * a pile (say a coin under a table would return "viewable"
3160     * by this routine). Another question, should we be
3161     * concerned with the direction the player is looking
3162     * in? Realistically, most of use cant see stuff behind
3163     * our backs...on the other hand, does the "facing" direction
3164     * imply the way your head, or body is facing? Its possible
3165     * for them to differ. Sigh, this fctn could get a bit more complex.
3166     * -b.t.
3167     * This function is now map tiling safe.
3168     */
3169    
3170     int player_can_view (object *pl,object *op) {
3171     rv_vector rv;
3172     int dx,dy;
3173    
3174     if(pl->type!=PLAYER) {
3175     LOG(llevError,"player_can_view() called for non-player object\n");
3176     return -1;
3177     }
3178     if (!pl || !op) return 0;
3179    
3180     if(op->head) { op = op->head; }
3181     get_rangevector(pl, op, &rv, 0x1);
3182    
3183     /* starting with the 'head' part, lets loop
3184     * through the object and find if it has any
3185     * part that is in the los array but isnt on
3186     * a blocked los square.
3187     * we use the archetype to figure out offsets.
3188     */
3189     while(op) {
3190     dx = rv.distance_x + op->arch->clone.x;
3191     dy = rv.distance_y + op->arch->clone.y;
3192    
3193     /* only the viewable area the player sees is updated by LOS
3194     * code, so we need to restrict ourselves to that range of values
3195     * for any meaningful values.
3196     */
3197     if (FABS(dx) <= (pl->contr->socket.mapx/2) &&
3198     FABS(dy) <= (pl->contr->socket.mapy/2) &&
3199     !pl->contr->blocked_los[dx + (pl->contr->socket.mapx/2)][dy+(pl->contr->socket.mapy/2)] )
3200     return 1;
3201     op = op->more;
3202     }
3203     return 0;
3204     }
3205    
3206     /* routine for both players and monsters. We call this when
3207     * there is a possibility for our action distrubing our hiding
3208     * place or invisiblity spell. Artefact invisiblity is not
3209     * effected by this. If we arent invisible to begin with, we
3210     * return 0.
3211     */
3212     int action_makes_visible (object *op) {
3213    
3214     if(op->invisible && QUERY_FLAG(op,FLAG_ALIVE)) {
3215     if(QUERY_FLAG(op,FLAG_MAKE_INVIS))
3216     return 0;
3217    
3218     if (op->contr && op->contr->tmp_invis == 0) return 0;
3219    
3220     /* If monsters, they should become visible */
3221     if(op->hide || !op->contr || (op->contr && op->contr->tmp_invis)) {
3222     new_draw_info_format(NDI_UNIQUE, 0,op,"You become %s!",op->hide?"unhidden":"visible");
3223     return 1;
3224     }
3225     }
3226     return 0;
3227     }
3228    
3229     /* op_on_battleground - checks if the given object op (usually
3230     * a player) is standing on a valid battleground-tile,
3231     * function returns TRUE/FALSE. If true x, y returns the battleground
3232     * -exit-coord. (and if x, y not NULL)
3233     * 19 March 2005 - josh@woosworld.net modifed to check if the battleground also has slaying, maxhp, and maxsp set
3234     * and if those are all set and the player has a marker that matches the slaying send them to a different x, y
3235     * Default is to do the same as before, so only people wanting to have different points need worry about this
3236     */
3237     int op_on_battleground (object *op, int *x, int *y) {
3238     object *tmp;
3239    
3240     /* A battleground-tile needs the following attributes to be valid:
3241     * is_floor 1 (has to be the FIRST floor beneath the player's feet),
3242     * name="battleground", no_pick 1, type=58 (type BATTLEGROUND)
3243     * and the exit-coordinates sp/hp must both be > 0.
3244     * => The intention here is to prevent abuse of the battleground-
3245     * feature (like pickable or hidden battleground tiles). */
3246     for (tmp=op->below; tmp!=NULL; tmp=tmp->below) {
3247     if (QUERY_FLAG (tmp, FLAG_IS_FLOOR)) {
3248     if (QUERY_FLAG (tmp, FLAG_NO_PICK) &&
3249     strcmp(tmp->name, "battleground")==0 &&
3250     tmp->type == BATTLEGROUND && EXIT_X(tmp) && EXIT_Y(tmp)) {
3251     /*before we assign the exit, check if this is a teambattle*/
3252     if ( EXIT_ALT_X(tmp) && EXIT_ALT_Y(tmp) && EXIT_PATH(tmp) ){
3253     object *invtmp;
3254     for(invtmp=op->inv; invtmp != NULL; invtmp=invtmp->below) {
3255     if(invtmp->type==FORCE && invtmp->slaying &&
3256     !strcmp(EXIT_PATH(tmp), invtmp->slaying)){
3257     if (x != NULL && y != NULL)
3258     *x=EXIT_ALT_X(tmp), *y=EXIT_ALT_Y(tmp);
3259     return 1;
3260     }
3261     }
3262     }
3263     if (x != NULL && y != NULL)
3264     *x=EXIT_X(tmp), *y=EXIT_Y(tmp);
3265     return 1;
3266     }
3267     }
3268     }
3269     /* If we got here, did not find a battleground */
3270     return 0;
3271     }
3272    
3273     /*
3274     * When a dragon-player gains a new stage of evolution,
3275     * he gets some treasure
3276     *
3277     * attributes:
3278     * object *who the dragon player
3279     * int atnr the attack-number of the ability focus
3280     * int level ability level
3281     */
3282     void dragon_ability_gain(object *who, int atnr, int level) {
3283     treasurelist *trlist = NULL; /* treasurelist */
3284     treasure *tr; /* treasure */
3285     object *tmp,*skop; /* tmp. object */
3286     object *item; /* treasure object */
3287     char buf[MAX_BUF]; /* tmp. string buffer */
3288     int i=0, j=0;
3289    
3290     /* get the appropriate treasurelist */
3291     if (atnr == ATNR_FIRE)
3292     trlist = find_treasurelist("dragon_ability_fire");
3293     else if (atnr == ATNR_COLD)
3294     trlist = find_treasurelist("dragon_ability_cold");
3295     else if (atnr == ATNR_ELECTRICITY)
3296     trlist = find_treasurelist("dragon_ability_elec");
3297     else if (atnr == ATNR_POISON)
3298     trlist = find_treasurelist("dragon_ability_poison");
3299    
3300     if (trlist == NULL || who->type != PLAYER)
3301     return;
3302    
3303     for (i=0, tr = trlist->items; tr != NULL && i<level-1;
3304     tr = tr->next, i++);
3305    
3306     if (tr == NULL || tr->item == NULL) {
3307     /* LOG(llevDebug, "-> no more treasure for %s\n", change_resist_msg[atnr]); */
3308     return;
3309     }
3310    
3311     /* everything seems okay - now bring on the gift: */
3312     item = &(tr->item->clone);
3313    
3314     if (item->type == SPELL) {
3315     if (check_spell_known (who, item->name))
3316     return;
3317    
3318     new_draw_info_format(NDI_UNIQUE|NDI_BLUE, 0, who, "You gained the ability of %s", item->name);
3319     do_learn_spell (who, item, 0);
3320     return;
3321     }
3322    
3323     /* grant direct spell */
3324     if (item->type == SPELLBOOK) {
3325     if (!item->inv) {
3326     LOG(llevDebug,"dragon_ability_gain: Broken spellbook %s\n",
3327     item->name);
3328     return;
3329     }
3330     if (check_spell_known (who, item->inv->name))
3331     return;
3332     if (item->invisible) {
3333     new_draw_info_format(NDI_UNIQUE|NDI_BLUE, 0, who, "You gained the ability of %s", item->inv->name);
3334     do_learn_spell (who, item->inv, 0);
3335     return;
3336     }
3337     }
3338     else if (item->type == SKILL_TOOL && item->invisible) {
3339     if (item->subtype == SK_CLAWING && (skop=find_skill_by_name(who, item->skill))!=NULL) {
3340    
3341     /* should this perhaps be (skop->attackyp & item->attacktype)!=item->attacktype ...
3342     * in this way, if the player is missing any of the attacktypes, he gets
3343     * them. As it is now, if the player has any that match the granted skill,
3344     * but not all of them, he gets nothing.
3345     */
3346     if (!(skop->attacktype & item->attacktype)) {
3347     /* Give new attacktype */
3348     skop->attacktype |= item->attacktype;
3349    
3350     /* always add physical if there's none */
3351     skop->attacktype |= AT_PHYSICAL;
3352    
3353     if (item->msg != NULL)
3354     new_draw_info(NDI_UNIQUE|NDI_BLUE, 0, who, item->msg);
3355    
3356     /* Give player new face */
3357     if (item->animation_id) {
3358     who->face = skop->face;
3359     who->animation_id = item->animation_id;
3360     who->anim_speed = item->anim_speed;
3361     who->last_anim = 0;
3362     who->state = 0;
3363     animate_object(who, who->direction);
3364     }
3365     }
3366     }
3367     }
3368     else if (item->type == FORCE) {
3369     /* forces in the treasurelist can alter the player's stats */
3370     object *skin;
3371     /* first get the dragon skin force */
3372     for (skin=who->inv; skin!=NULL && strcmp(skin->arch->name, "dragon_skin_force")!=0;
3373     skin=skin->below);
3374     if (skin == NULL) return;
3375    
3376     /* adding new spellpath attunements */
3377     if (item->path_attuned > 0 && !(skin->path_attuned & item->path_attuned)) {
3378     skin->path_attuned |= item->path_attuned; /* add attunement to skin */
3379    
3380     /* print message */
3381     sprintf(buf, "You feel attuned to ");
3382     for(i=0, j=0; i<NRSPELLPATHS; i++) {
3383     if(item->path_attuned & (1<<i)) {
3384     if (j)
3385     strcat(buf," and ");
3386     else
3387     j = 1;
3388     strcat(buf, spellpathnames[i]);
3389     }
3390     }
3391     strcat(buf,".");
3392     new_draw_info(NDI_UNIQUE|NDI_BLUE, 0, who, buf);
3393     }
3394    
3395     /* evtl. adding flags: */
3396     if(QUERY_FLAG(item, FLAG_XRAYS))
3397     SET_FLAG(skin, FLAG_XRAYS);
3398     if(QUERY_FLAG(item, FLAG_STEALTH))
3399     SET_FLAG(skin, FLAG_STEALTH);
3400     if(QUERY_FLAG(item, FLAG_SEE_IN_DARK))
3401     SET_FLAG(skin, FLAG_SEE_IN_DARK);
3402    
3403     /* print message if there is one */
3404     if (item->msg != NULL)
3405     new_draw_info(NDI_UNIQUE|NDI_BLUE, 0, who, item->msg);
3406     }
3407     else {
3408     /* generate misc. treasure */
3409     tmp = arch_to_object (tr->item);
3410     new_draw_info_format(NDI_UNIQUE|NDI_BLUE, 0, who, "You gained %s", query_short_name(tmp));
3411     tmp = insert_ob_in_ob (tmp, who);
3412     if (who->type == PLAYER)
3413     esrv_send_item(who, tmp);
3414     }
3415     }
3416    
3417     /**
3418     * Unready an object for a player. This function does nothing if the object was
3419     * not readied.
3420     */
3421     void player_unready_range_ob(player *pl, object *ob) {
3422     rangetype i;
3423    
3424     for (i = 0; i < range_size; i++) {
3425     if (pl->ranges[i] == ob) {
3426     pl->ranges[i] = NULL;
3427     if (pl->shoottype == i) {
3428     pl->shoottype = range_none;
3429     }
3430     }
3431     }
3432     }