ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.260
Committed: Fri Apr 2 03:41:25 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.259: +14 -30 lines
Log Message:
preliminary check-in with dbeugging code

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