ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.272
Committed: Thu Apr 15 02:51:39 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.271: +1 -1 lines
Log Message:
better need_identify

File Contents

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