ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.230
Committed: Sat Jun 27 08:25:36 2009 UTC (14 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_79
Changes since 1.229: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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