ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.141
Committed: Fri May 18 15:05:09 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.140: +2 -8 lines
Log Message:
- change weapon speed inside server to be the real speed value.
- send speed and weapon_sp as actions/second to the client.
  this breaks cfclient which divides speed/weapon_speed,
  but makes more sense.

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