ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.142
Committed: Fri May 18 19:46:22 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.141: +160 -168 lines
Log Message:
new speed management:
- weapon speed and object speed is now completekly decoupled for players.
- both can be used at the same time, or indeepndent, when running or firing.
- still only one command per object speed can be issued.

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