ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.261
Committed: Sat Apr 3 02:46:19 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.260: +2 -2 lines
Log Message:
looks good, no totally obvious bugs

File Contents

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