ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.89
Committed: Sun Jan 7 02:39:14 2007 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.88: +57 -124 lines
Log Message:
""

File Contents

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