ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.234
Committed: Thu Oct 15 21:40:42 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82
Changes since 1.233: +1 -122 lines
Log Message:
cleanups

File Contents

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