ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.298
Committed: Sun Jan 29 03:59:15 2017 UTC (7 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.297: +4 -1 lines
Log Message:
*** empty log message ***

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