ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
Revision: 1.159
Committed: Wed Aug 15 04:59:46 2007 UTC (16 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.158: +6 -6 lines
Log Message:
fix a server crash when unpaid items get dropped on logout due to the socket being released first, but we require contr->ns to be valid at all times when a player is on a map

File Contents

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