ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.c
Revision: 1.5
Committed: Fri Feb 10 04:35:33 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

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