ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.233
Committed: Mon Oct 12 14:00:59 2009 UTC (14 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_81
Changes since 1.232: +7 -6 lines
Log Message:
clarify license

File Contents

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