ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.236
Committed: Thu Nov 5 14:56:06 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.235: +15 -4 lines
Log Message:
implement match for locked doors

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