ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.90
Committed: Sun Jan 7 21:54:59 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.89: +0 -1 lines
Log Message:
implement quit

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