ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.233
Committed: Mon Oct 12 14:00:59 2009 UTC (14 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_81
Changes since 1.232: +7 -6 lines
Log Message:
clarify license

File Contents

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