ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.99
Committed: Thu Jan 18 19:42:10 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.98: +3 -3 lines
Log Message:
just experimenting

File Contents

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