ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.247
Committed: Fri Nov 13 16:01:30 2009 UTC (14 years, 6 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.246: +11 -4 lines
Log Message:
applied boes' pickup patch.

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