ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.256
Committed: Fri Mar 26 00:59:22 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.255: +2 -2 lines
Log Message:
remove bogus 2007 copyright that was added wrongly by the script, update to affero license

File Contents

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