ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.253
Committed: Thu Mar 25 23:25:04 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.252: +1 -1 lines
Log Message:
*** empty log message ***

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