Crossfire Server, Trunk  R21017
player.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
20 #include "global.h"
21 
22 #include <assert.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #ifndef WIN32 /* ---win32 remove headers */
28 #include <pwd.h>
29 #endif
30 
31 #include "living.h"
32 #include "object.h"
33 #include "shared/newclient.h"
34 #include "shop.h"
35 #include "skills.h"
36 #include "sounds.h"
37 #include "spells.h"
38 #include "sproto.h"
39 
41 
42 static void kill_player_not_permadeath(object *op);
43 static void kill_player_permadeath(object *op);
44 static int action_makes_visible(object *op);
45 
54 player *find_player(const char *plname) {
55  return find_player_options(plname, 0, NULL);
56 }
57 
65 player *find_player_options(const char *plname, int options, const mapstruct *map) {
66  player *pl;
67  player *found = NULL;
68  size_t namelen = strlen(plname);
69  char name[MAX_BUF];
70 
71  for (pl = first_player; pl != NULL; pl = pl->next) {
72  if ((options & FIND_PLAYER_NO_HIDDEN_DM) && (QUERY_FLAG(pl->ob, FLAG_WIZ) && pl->ob->contr->hidden))
73  continue;
74 
75  if (map != NULL && pl->ob->map != map)
76  continue;
77 
78  if (!(options & FIND_PLAYER_PARTIAL_NAME)) {
79  query_name(pl->ob, name, sizeof(name));
80  if (!strcmp(name, plname))
81  return pl;
82  continue;
83  }
84 
85  if (strlen(pl->ob->name) < namelen)
86  continue;
87 
88  if (!strcmp(pl->ob->name, plname))
89  return pl;
90 
91  if (!strncasecmp(pl->ob->name, plname, namelen)) {
92  if (found)
93  return NULL;
94 
95  found = pl;
96  }
97  }
98  return found;
99 }
100 
109 player *find_player_partial_name(const char *plname) {
110  return find_player_options(plname, FIND_PLAYER_PARTIAL_NAME, NULL);
111 }
112 
119  player *pl;
120 
121  for (pl = first_player; pl != NULL; pl = pl->next) {
122  if (&pl->socket == ns)
123  return pl;
124  }
125  return NULL;
126 }
127 
134 void display_motd(const object *op) {
135  char buf[MAX_BUF];
136  char motd[HUGE_BUF];
137  FILE *fp;
138  size_t size;
139 
140  snprintf(buf, sizeof(buf), "%s/%s", settings.confdir, settings.motd);
141  fp = fopen(buf, "r");
142  if (fp == NULL) {
143  return;
144  }
145  motd[0] = '\0';
146  size = 0;
147 
148  while (fgets(buf, MAX_BUF, fp) != NULL) {
149  if (*buf != '#') {
150  safe_strcat(motd, buf, &size, sizeof(motd));
151  }
152  }
153 
155  motd);
156  fclose(fp);
157 }
158 
165 void send_rules(const object *op) {
166  char buf[MAX_BUF];
167  char rules[HUGE_BUF];
168  FILE *fp;
169  size_t size;
170 
171  snprintf(buf, sizeof(buf), "%s/%s", settings.confdir, settings.rules);
172  fp = fopen(buf, "r");
173  if (fp == NULL) {
174  return;
175  }
176  rules[0] = '\0';
177  size = 0;
178 
179  while (fgets(buf, MAX_BUF, fp) != NULL) {
180  if (size+strlen(buf) >= HUGE_BUF) {
181  LOG(llevDebug, "Warning, rules size is > %d bytes.\n", HUGE_BUF);
182  break;
183  }
184 
185  if (*buf != '#') {
186  safe_strcat(rules, buf, &size, sizeof(rules));
187  }
188  }
189 
191  MSG_TYPE_ADMIN_RULES, rules);
192  fclose(fp);
193 }
194 
201 void send_news(const object *op) {
202  char buf[MAX_BUF];
203  char news[HUGE_BUF];
204  char subject[MAX_BUF];
205  FILE *fp;
206  size_t size;
207 
208  snprintf(buf, sizeof(buf), "%s/%s", settings.confdir, settings.news);
209  fp = fopen(buf, "r");
210  if (fp == NULL)
211  return;
212  news[0] = '\0';
213  subject[0] = '\0';
214  size = 0;
215  while (fgets(buf, MAX_BUF, fp) != NULL) {
216  if (*buf == '#')
217  continue;
218  if (*buf == '%') { /* send one news */
219  if (size > 0)
222  "%s:\n%s",
223  subject, news); /*send previously read news*/
224  safe_strncpy(subject, buf + 1, sizeof(subject));
225  strip_endline(subject);
226  size = 0;
227  news[0] = '\0';
228  } else {
229  if (size+strlen(buf) >= HUGE_BUF) {
230  LOG(llevDebug, "Warning, one news item has size > %d bytes.\n", HUGE_BUF);
231  break;
232  }
233  safe_strcat(news, buf, &size, sizeof(news));
234  }
235  }
236 
239  "%s:\n%s",
240  subject, news);
241  fclose(fp);
242 }
243 
252 int playername_ok(const char *cp) {
253  /* Don't allow - or _ as first character in the name */
254  if (*cp == '-' || *cp == '_')
255  return 0;
256 
257  for (; *cp != '\0'; cp++)
258  if (!((*cp >= 'a' && *cp <= 'z') || (*cp >= 'A' && *cp <= 'Z'))
259  && *cp != '-'
260  && *cp != '_')
261  return 0;
262  return 1;
263 }
264 
281  object *op = arch_to_object(get_player_archetype(NULL));
282  int i;
283 
284  if (!p) {
285  player *tmp;
286 
287  p = (player *)malloc(sizeof(player));
288  if (p == NULL)
290 
291  /* This adds the player in the linked list. There is extra
292  * complexity here because we want to add the new player at the
293  * end of the list - there is in fact no compelling reason that
294  * that needs to be done except for things like output of
295  * 'who'.
296  */
297  tmp = first_player;
298  while (tmp != NULL && tmp->next != NULL)
299  tmp = tmp->next;
300  if (tmp != NULL)
301  tmp->next = p;
302  else
303  first_player = p;
304 
305  p->next = NULL;
306  /* This only needs to be done on initial creation of player
307  * object. the callers of get_player() will copy over the
308  * socket structure to p->socket, and if this is an existing
309  * player object, that has been done. The call to
310  * roll_stats() below will try to send spell information to
311  * the client - if this is non zero (eg, garbage from not
312  * being cleared), that will cause problems. So just clear
313  * it, and no spell data is sent.
314  */
315  p->socket.monitor_spells = 0;
316  } else {
317  /* Only needed when reusing existing player. */
318  clear_player(p);
319  }
320 
321  /* Clears basically the entire player structure except
322  * for next and socket.
323  */
324  memset((void *)((char *)p+offsetof(player, maplevel)), 0, sizeof(player)-offsetof(player, maplevel));
325 
326  /* There are some elements we want initialized to non zero value -
327  * we deal with that below this point.
328  */
329  p->party = NULL;
332  p->swap_first = -1;
333 
334 #ifdef AUTOSAVE
335  p->last_save_tick = 9999999;
336 #endif
337 
338  strcpy(p->savebed_map, first_map_path); /* Init. respawn position */
339 
340  op->contr = p; /* this aren't yet in archetype */
341  p->ob = op;
342  op->speed_left = 0.5;
343  op->speed = 1.0;
344  op->direction = 5; /* So player faces south */
345  op->stats.wc = 2;
346  op->run_away = 25; /* Then we panic... */
347 
348  roll_stats(op);
350  clear_los(op);
351 
352  p->gen_sp_armour = 10;
353  p->last_speed = -1;
354  p->shoottype = range_none;
355  p->bowtype = bow_normal;
356  p->petmode = pet_normal;
357  p->listening = 10;
358  p->last_weapon_sp = -1;
359  p->peaceful = 1; /* default peaceful */
360  p->do_los = 1;
361  p->no_shout = 0; /* default can shout */
362  p->language = 0;
363  p->unarmed_skill = NULL;
364  p->ticks_played = 0;
365 
366  strncpy(p->title, op->arch->clone.name, sizeof(p->title)-1);
367  p->title[sizeof(p->title)-1] = '\0';
368  op->race = add_string(op->arch->clone.race);
369 
371 
372  /* we need to clear these to -1 and not zero - otherwise,
373  * if a player quits and starts a new character, we wont
374  * send new values to the client, as things like exp start
375  * at zero.
376  */
377  for (i = 0; i < NUM_SKILLS; i++) {
378  p->last_skill_exp[i] = -1;
379  p->last_skill_ob[i] = NULL;
380  }
381  for (i = 0; i < NROFATTACKS; i++) {
382  p->last_resist[i] = -1;
383  }
384  p->last_stats.exp = -1;
385  p->last_weight = (uint32_t)-1;
386 
387  p->socket.update_look = 0;
388  p->socket.look_position = 0;
389  return p;
390 }
391 
398 void set_first_map(object *op) {
399  strcpy(op->contr->maplevel, first_map_path);
400  op->x = -1;
401  op->y = -1;
403 }
404 
417 static void set_player_socket(player *p, socket_struct *ns) {
418  memcpy(&p->socket, ns, sizeof(socket_struct));
419 
420  /* The memcpy above copies the reference to faces sent. So we need to clear
421  * that pointer in ns, otherwise we get a double free.
422  */
423  ns->faces_sent = NULL;
424 
425  if (p->socket.faces_sent == NULL)
427 
428  /* Needed because the socket we just copied over needs to be cleared.
429  * Note that this can result in a client reset if there is partial data
430  * on the incoming socket.
431  */
433 
434 
435 }
436 
454  player *p;
455 
456  p = get_player(NULL);
457  set_player_socket(p, ns);
458 
460 
461  if (!(flags & ADD_PLAYER_NO_MAP))
462  set_first_map(p->ob);
463 
465 
466  /* In this case, the client is provide all the informatin for the
467  * new character, so just return it. Do not display any messages,
468  * etc
469  */
470  if (flags & ADD_PLAYER_NO_STATS_ROLL)
471  return p;
472 
473  if (flags & ADD_PLAYER_NEW) {
474  roll_again(p->ob);
476  } else {
477  send_rules(p->ob);
478  send_news(p->ob);
479  display_motd(p->ob);
480  get_name(p->ob);
481  }
482  return p;
483 }
484 
496  archetype *start = at;
497 
498  for (;;) {
499  if (at == NULL || at->next == NULL)
500  at = first_archetype;
501  else
502  at = at->next;
503  if (at->clone.type == PLAYER)
504  return at;
505  if (at == start) {
506  LOG(llevError, "No Player archetypes\n");
507  exit(-1);
508  }
509  }
510 }
511 
520 object *get_nearest_player(object *mon) {
521  object *op = NULL;
522  player *pl = NULL;
523  objectlink *ol;
524  unsigned lastdist;
525  rv_vector rv;
526 
527  for (ol = first_friendly_object, lastdist = 1000; ol != NULL; ol = ol->next) {
528  /* We should not find free objects on this friendly list, but it
529  * does periodically happen. Given that, lets deal with it.
530  * While unlikely, it is possible the next object on the friendly
531  * list is also free, so encapsulate this in a while loop.
532  */
533  while (QUERY_FLAG(ol->ob, FLAG_FREED) || !QUERY_FLAG(ol->ob, FLAG_FRIENDLY)) {
534  object *tmp = ol->ob;
535 
536  /* Can't do much more other than log the fact, because the object
537  * itself will have been cleared.
538  */
539  LOG(llevDebug, "get_nearest_player: Found free/non friendly object on friendly list\n");
540  ol = ol->next;
542  if (!ol)
543  return op;
544  }
545 
546  /* Remove special check for player from this. First, it looks to cause
547  * some crashes (ol->ob->contr not set properly?), but secondly, a more
548  * complicated method of state checking would be needed in any case -
549  * as it was, a clever player could type quit, and the function would
550  * skip them over while waiting for confirmation. Remove
551  * on_same_map check, as monster_can_detect_enemy() also does this
552  */
553  if (!monster_can_detect_enemy(mon, ol->ob, &rv))
554  continue;
555 
556  if (lastdist > rv.distance) {
557  op = ol->ob;
558  lastdist = rv.distance;
559  }
560  }
561  for (pl = first_player; pl != NULL; pl = pl->next) {
562  if (monster_can_detect_enemy(mon, pl->ob, &rv)) {
563  if (lastdist > rv.distance) {
564  op = pl->ob;
565  lastdist = rv.distance;
566  }
567  }
568  }
569  return op;
570 }
571 
581 #define DETOUR_AMOUNT 2
582 
596 #define MAX_SPACES 50
597 
629 int path_to_player(object *mon, object *pl, unsigned mindiff) {
630  rv_vector rv;
631  int16_t x, y;
632  int lastx, lasty, dir, i, diff, firstdir = 0, lastdir, max = MAX_SPACES, mflags, blocked;
633  mapstruct *m, *lastmap;
634 
635  if (!get_rangevector(mon, pl, &rv, 0))
636  return 0;
637 
638  if (rv.distance < mindiff)
639  return 0;
640 
641  x = mon->x;
642  y = mon->y;
643  m = mon->map;
644  dir = rv.direction;
645  lastdir = firstdir = rv.direction; /* perhaps we stand next to pl, init firstdir too */
646  diff = MAX(FABS(rv.distance_x), FABS(rv.distance_y));
647  /* If we can't solve it within the search distance, return now. */
648  if (diff > max)
649  return 0;
650  while (diff > 1 && max > 0) {
651  lastx = x;
652  lasty = y;
653  lastmap = m;
654  x = lastx+freearr_x[dir];
655  y = lasty+freearr_y[dir];
656 
657  mflags = get_map_flags(m, &m, x, y, &x, &y);
658  blocked = (mflags&P_OUT_OF_MAP) ? MOVE_ALL : GET_MAP_MOVE_BLOCK(m, x, y);
659 
660  /* Space is blocked - try changing direction a little */
661  if ((mflags&P_OUT_OF_MAP)
662  || ((OB_TYPE_MOVE_BLOCK(mon, blocked) || (mflags&P_IS_ALIVE))
663  && (m == mon->map && blocked_link(mon, m, x, y)))) {
664  /* recalculate direction from last good location. Possible
665  * we were not traversing ideal location before.
666  */
667  if (get_rangevector_from_mapcoord(lastmap, lastx, lasty, pl, &rv, 0) && rv.direction != dir) {
668  /* OK - says direction should be different - lets reset the
669  * the values so it will try again.
670  */
671  x = lastx;
672  y = lasty;
673  m = lastmap;
674  dir = firstdir = rv.direction;
675  } else {
676  /* direct path is blocked - try taking a side step to
677  * either the left or right.
678  * Note increase the values in the loop below to be
679  * more than -1/1 respectively will mean the monster takes
680  * bigger detour. Have to be careful about these values getting
681  * too big (3 or maybe 4 or higher) as the monster may just try
682  * stepping back and forth
683  */
684  for (i = -DETOUR_AMOUNT; i <= DETOUR_AMOUNT; i++) {
685  if (i == 0)
686  continue; /* already did this, so skip it */
687  /* Use lastdir here - otherwise,
688  * since the direction that the creature should move in
689  * may change, you could get infinite loops.
690  * ie, player is northwest, but monster can only
691  * move west, so it does that. It goes some distance,
692  * gets blocked, finds that it should move north,
693  * can't do that, but now finds it can move east, and
694  * gets back to its original point. lastdir contains
695  * the last direction the creature has successfully
696  * moved.
697  */
698 
699  x = lastx+freearr_x[absdir(lastdir+i)];
700  y = lasty+freearr_y[absdir(lastdir+i)];
701  m = lastmap;
702  mflags = get_map_flags(m, &m, x, y, &x, &y);
703  if (mflags&P_OUT_OF_MAP)
704  continue;
705  blocked = GET_MAP_MOVE_BLOCK(m, x, y);
706  if (OB_TYPE_MOVE_BLOCK(mon, blocked))
707  continue;
708  if (mflags&P_IS_ALIVE)
709  continue;
710 
711  if (m == mon->map && blocked_link(mon, m, x, y))
712  break;
713  }
714  /* go through entire loop without finding a valid
715  * sidestep to take - thus, no valid path.
716  */
717  if (i == (DETOUR_AMOUNT+1))
718  return 0;
719  diff--;
720  lastdir = dir;
721  max--;
722  if (!firstdir)
723  firstdir = dir+i;
724  } /* else check alternate directions */
725  } /* if blocked */
726  else {
727  /* we moved towards creature, so diff is less */
728  diff--;
729  max--;
730  lastdir = dir;
731  if (!firstdir)
732  firstdir = dir;
733  }
734  if (diff <= 1) {
735  /* Recalculate diff (distance) because we may not have actually
736  * headed toward player for entire distance.
737  */
738  if (!get_rangevector_from_mapcoord(m, x, y, pl, &rv, 0))
739  return 0;
740  diff = MAX(FABS(rv.distance_x), FABS(rv.distance_y));
741  }
742  if (diff > max)
743  return 0;
744  }
745  /* If we reached the max, didn't find a direction in time */
746  if (!max)
747  return 0;
748 
749  return firstdir;
750 }
751 
762 void give_initial_items(object *pl, treasurelist *items) {
763  if (pl->randomitems != NULL)
764  create_treasure(items, pl, GT_STARTEQUIP|GT_ONLY_GOOD, 1, 0);
765 
766  FOR_INV_PREPARE(pl, op) {
767  /* Forces get applied per default, unless they have the
768  * flag "neutral" set. Sorry but I can't think of a better way
769  */
770  if (op->type == FORCE && !QUERY_FLAG(op, FLAG_NEUTRAL))
771  SET_FLAG(op, FLAG_APPLIED);
772 
773  /* we never give weapons/armour if these cannot be used
774  * by this player due to race restrictions
775  */
776  if (pl->type == PLAYER) {
777  if ((!QUERY_FLAG(pl, FLAG_USE_ARMOUR) && IS_ARMOR(op))
778  || (!QUERY_FLAG(pl, FLAG_USE_WEAPON) && IS_WEAPON(op))
779  || (!QUERY_FLAG(pl, FLAG_USE_SHIELD) && IS_SHIELD(op))) {
780  object_remove(op);
782  continue;
783  }
784  }
785 
786  /* This really needs to be better - we should really give
787  * a substitute spellbook. The problem is that we don't really
788  * have a good idea what to replace it with (need something like
789  * a first level treasurelist for each skill.)
790  * remove duplicate skills also
791  */
792  if (op->type == SPELLBOOK || op->type == SKILL) {
793  int found;
794 
795  found = 0;
796  FOR_BELOW_PREPARE(op, tmp)
797  if (tmp->type == op->type && tmp->name == op->name) {
798  found = 1;
799  break;
800  }
802  if (found) {
803  LOG(llevError, "give_initial_items: Removing duplicate object %s\n", op->name);
804  object_remove(op);
806  continue;
807  }
808  if (op->nrof > 1)
809  op->nrof = 1;
810  }
811 
812  if (op->type == SPELLBOOK && op->inv) {
813  CLEAR_FLAG(op->inv, FLAG_STARTEQUIP);
814  }
815 
816  /* Give starting characters identified, uncursed, and undamned
817  * items. Just don't identify gold or silver, or it won't be
818  * merged properly.
819  */
820  if (need_identify(op)) {
822  CLEAR_FLAG(op, FLAG_CURSED);
823  CLEAR_FLAG(op, FLAG_DAMNED);
824  }
825  if (op->type == SKILL) {
827  op->stats.exp = 0;
828  op->level = 1;
829  }
830  /* lock all 'normal items by default */
831  else
833  } FOR_INV_FINISH(); /* for loop of objects in player inv */
834 
835  /* Need to set up the skill pointers */
836  link_player_skills(pl);
837 
842  FOR_INV_PREPARE(pl, op)
843  if ((IS_ARMOR(op) || IS_WEAPON(op) || IS_SHIELD(op)) && !QUERY_FLAG(op, FLAG_APPLIED))
844  apply_manual(pl, op, AP_NOPRINT);
845  FOR_INV_FINISH();
846 }
847 
854 void get_name(object *op) {
855  op->contr->write_buf[0] = '\0';
857  send_query(&op->contr->socket, 0, "What is your name?\n:");
858 }
859 
866 void get_password(object *op) {
867  op->contr->write_buf[0] = '\0';
869  send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, "What is your password?\n:");
870 }
871 
878 void play_again(object *op) {
879  SockList sl;
880 
882  op->chosen_skill = NULL;
883 
884  /*
885  * For old clients, ask if they want to play again.
886  * For clients with account support, just return to character seletion (see below).
887  */
888  if (op->contr->socket.login_method == 0) {
889  send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "Do you want to play again (a/q)?");
890  }
891  /* a bit of a hack, but there are various places early in th
892  * player creation process that a user can quit (eg, roll
893  * stats) that isn't removing the player. Taking a quick
894  * look, there are many places that call play_again without
895  * removing the player - it probably makes more sense
896  * to leave it to play_again to remove the object in all
897  * cases.
898  */
899  if (!QUERY_FLAG(op, FLAG_REMOVED))
900  object_remove(op);
901  /* Need to set this to null - otherwise, it could point to garbage,
902  * and draw() doesn't check to see if the player is removed, only if
903  * the map is null or not swapped out.
904  */
905  op->map = NULL;
906 
907  SockList_Init(&sl);
908  SockList_AddString(&sl, "player ");
909  SockList_AddInt(&sl, 0);
910  SockList_AddInt(&sl, 0);
911  SockList_AddInt(&sl, 0);
912  SockList_AddChar(&sl, 0);
913 
914  Send_With_Handling(&op->contr->socket, &sl);
915  SockList_Term(&sl);
916 
917  if (op->contr->socket.login_method > 0) {
918  receive_play_again(op, 'a');
919  }
920 }
921 
930 void receive_play_again(object *op, char key) {
931  if (key == 'q' || key == 'Q') {
933  leave(op->contr, 0); /* ericserver will draw the message */
934  return;
935  } else if (key == 'a' || key == 'A') {
936  player *pl = op->contr;
937  const char *name = op->name;
938 
939  add_refcount(name);
942  pl = get_player(pl);
943  op = pl->ob;
945  op->contr->password[0] = '~';
948  if (pl->socket.login_method >= 1 && pl->socket.account_name != NULL) {
949  /* If we are using new login, we send the
950  * list of characters to the client - this should
951  * result in the client popping up this list so
952  * the player can choose which one to play - better
953  * than going to legacy login code.
954  * If the account_name is NULL, it means the client
955  * says it uses account but started playing without logging in.
956  */
959  } else {
960  /* Lets put a space in here */
962  "\n");
963  get_name(op);
964  set_first_map(op);
965  }
966  op->name = name; /* Already added a refcount above */
967  op->name_pl = add_string(name);
968  } else {
969  /* user pressed something else so just ask again... */
970  play_again(op);
971  }
972 }
973 
980 void confirm_password(object *op) {
981  op->contr->write_buf[0] = '\0';
983  send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, "Please type your password again.\n:");
984 }
985 
996 int get_party_password(object *op, partylist *party) {
997  if (*party_get_password(party) == '\0') {
998  return 0;
999  }
1000 
1001  op->contr->write_buf[0] = '\0';
1003  op->contr->party_to_join = party;
1004  send_query(&op->contr->socket, CS_QUERY_HIDEINPUT, "What is the password?\n:");
1005  return 1;
1006 }
1007 
1014 int roll_stat(void) {
1015  int roll[4], i, low_index, k;
1016 
1017  for (i = 0; i < 4; ++i)
1018  roll[i] = (int)RANDOM()%6+1;
1019 
1020  for (i = 0, low_index = 0, k = 7; i < 4; ++i)
1021  if (roll[i] < k)
1022  k = roll[i],
1023  low_index = i;
1024 
1025  for (i = 0, k = 0; i < 4; ++i) {
1026  if (i != low_index)
1027  k += roll[i];
1028  }
1029  return k;
1030 }
1031 
1038 void roll_stats(object *op) {
1039  int sum = 0;
1040  int i = 0, j = 0;
1041  int statsort[7];
1042 
1043  do {
1044  op->stats.Str = roll_stat();
1045  op->stats.Dex = roll_stat();
1046  op->stats.Int = roll_stat();
1047  op->stats.Con = roll_stat();
1048  op->stats.Wis = roll_stat();
1049  op->stats.Pow = roll_stat();
1050  op->stats.Cha = roll_stat();
1051  sum = op->stats.Str+op->stats.Dex+op->stats.Int+op->stats.Con+op->stats.Wis+op->stats.Pow+op->stats.Cha;
1052  } while (sum != settings.roll_stat_points);
1053 
1054  /* Sort the stats so that rerolling is easier... */
1055  statsort[0] = op->stats.Str;
1056  statsort[1] = op->stats.Dex;
1057  statsort[2] = op->stats.Int;
1058  statsort[3] = op->stats.Con;
1059  statsort[4] = op->stats.Wis;
1060  statsort[5] = op->stats.Pow;
1061  statsort[6] = op->stats.Cha;
1062 
1063  /* a quick and dirty bubblesort? */
1064  do {
1065  if (statsort[i] < statsort[i+1]) {
1066  j = statsort[i];
1067  statsort[i] = statsort[i+1];
1068  statsort[i+1] = j;
1069  i = 0;
1070  } else {
1071  i++;
1072  }
1073  } while (i < 6);
1074 
1075  op->stats.Str = statsort[0];
1076  op->stats.Dex = statsort[1];
1077  op->stats.Con = statsort[2];
1078  op->stats.Int = statsort[3];
1079  op->stats.Wis = statsort[4];
1080  op->stats.Pow = statsort[5];
1081  op->stats.Cha = statsort[6];
1082 
1083  op->contr->orig_stats.Str = op->stats.Str;
1084  op->contr->orig_stats.Dex = op->stats.Dex;
1085  op->contr->orig_stats.Int = op->stats.Int;
1086  op->contr->orig_stats.Con = op->stats.Con;
1087  op->contr->orig_stats.Wis = op->stats.Wis;
1088  op->contr->orig_stats.Pow = op->stats.Pow;
1089  op->contr->orig_stats.Cha = op->stats.Cha;
1090 
1091  op->level = 1;
1092  op->stats.exp = 0;
1093  op->stats.ac = 0;
1094 
1095  op->contr->levhp[1] = 9;
1096  op->contr->levsp[1] = 6;
1097  op->contr->levgrace[1] = 3;
1098 
1099  fix_object(op);
1100  op->stats.hp = op->stats.maxhp;
1101  op->stats.sp = op->stats.maxsp;
1102  op->stats.grace = op->stats.maxgrace;
1103  op->contr->orig_stats = op->stats;
1104 }
1105 
1112 void roll_again(object *op) {
1113  esrv_new_player(op->contr, 0);
1114  send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "<y> to roll new stats <n> to use stats\n<1-7> <1-7> to swap stats.\nRoll again (y/n/1-7)? ");
1115 }
1116 
1126 static void swap_stat(object *op, int swap_second) {
1127  signed char tmp;
1128 
1129  if (op->contr->swap_first == -1) {
1130  LOG(llevError, "player.c:swap_stat() - swap_first is -1\n");
1131  return;
1132  }
1133 
1134  tmp = get_attr_value(&op->contr->orig_stats, op->contr->swap_first);
1135 
1136  set_attr_value(&op->contr->orig_stats, op->contr->swap_first, get_attr_value(&op->contr->orig_stats, swap_second));
1137 
1138  set_attr_value(&op->contr->orig_stats, swap_second, tmp);
1139 
1141  "%s done\n",
1142  short_stat_name[swap_second]);
1143 
1144  op->stats.Str = op->contr->orig_stats.Str;
1145  op->stats.Dex = op->contr->orig_stats.Dex;
1146  op->stats.Con = op->contr->orig_stats.Con;
1147  op->stats.Int = op->contr->orig_stats.Int;
1148  op->stats.Wis = op->contr->orig_stats.Wis;
1149  op->stats.Pow = op->contr->orig_stats.Pow;
1150  op->stats.Cha = op->contr->orig_stats.Cha;
1151  op->stats.ac = 0;
1152 
1153  op->level = 1;
1154  op->stats.exp = 0;
1155  op->stats.ac = 0;
1156 
1157  op->contr->levhp[1] = 9;
1158  op->contr->levsp[1] = 6;
1159  op->contr->levgrace[1] = 3;
1160 
1161  fix_object(op);
1162  op->stats.hp = op->stats.maxhp;
1163  op->stats.sp = op->stats.maxsp;
1164  op->stats.grace = op->stats.maxgrace;
1165  op->contr->orig_stats = op->stats;
1166  op->contr->swap_first = -1;
1167 }
1168 
1184 void key_roll_stat(object *op, char key) {
1185  int keynum = key-'0';
1186  static const int8_t stat_trans[] = {
1187  -1,
1188  STRENGTH,
1189  DEXTERITY,
1190  CONSTITUTION,
1191  INTELLIGENCE,
1192  WISDOM,
1193  POWER,
1194  CHARISMA,
1195  };
1196 
1197  if (keynum > 0 && keynum <= 7) {
1198  if (op->contr->swap_first == -1) {
1199  op->contr->swap_first = stat_trans[keynum];
1201  "%s ->",
1202  short_stat_name[stat_trans[keynum]]);
1203  } else
1204  swap_stat(op, stat_trans[keynum]);
1205 
1207  return;
1208  }
1209  switch (key) {
1210  case 'n':
1211  case 'N': {
1212  SET_FLAG(op, FLAG_WIZ);
1213  if (op->map == NULL) {
1214  LOG(llevError, "Map == NULL in state 2\n");
1215  break;
1216  }
1217 
1218  SET_ANIMATION(op, 2); /* So player faces south */
1219  /* Enter exit adds a player otherwise */
1220  add_statbonus(op);
1221  send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n");
1223  if (op->msg)
1224  draw_ext_info(NDI_BLUE, 0, op,
1226  op->msg);
1227  return;
1228  }
1229  case 'y':
1230  case 'Y':
1231  roll_stats(op);
1233  return;
1234 
1235  case 'q':
1236  case 'Q':
1237  play_again(op);
1238  return;
1239 
1240  default:
1241  send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "Yes, No, Quit or 1-6. Roll again?");
1242  return;
1243  }
1244  return;
1245 }
1246 
1260 void key_change_class(object *op, char key) {
1261  int tmp_loop;
1262 
1263  if (key == 'q' || key == 'Q') {
1264  object_remove(op);
1265  play_again(op);
1266  return;
1267  }
1268  if (key == 'd' || key == 'D') {
1269  char buf[MAX_BUF];
1270 
1271  /* this must before then initial items are given */
1272  esrv_new_player(op->contr, op->weight+op->carrying);
1273  create_treasure(find_treasurelist("starting_wealth"), op, 0, 0, 0);
1274 
1275  /* Lauwenmark : Here we handle the BORN global event */
1277 
1278  /* Lauwenmark : We then generate a LOGIN event */
1281 
1282  object_set_msg(op, NULL);
1283 
1284  /* We create this now because some of the unique maps will need it
1285  * to save here.
1286  */
1287  snprintf(buf, sizeof(buf), "%s/%s/%s", settings.localdir, settings.playerdir, op->name);
1288  make_path_to_file(buf);
1289 
1290 #ifdef AUTOSAVE
1291  op->contr->last_save_tick = pticks;
1292 #endif
1295  "Welcome to Crossfire!\n Press `?' for help\n");
1296 
1299  "%s entered the game.", op->name);
1300 
1301  CLEAR_FLAG(op, FLAG_WIZ);
1303  link_player_skills(op);
1304  esrv_send_inventory(op, op);
1305  fix_object(op);
1306 
1307  /* This moves the player to a different start map, if there
1308  * is one for this race
1309  */
1310  if (*first_map_ext_path) {
1311  object *tmp;
1312  char mapname[MAX_BUF];
1313  mapstruct *oldmap;
1314 
1315  oldmap = op->map;
1316 
1317  snprintf(mapname, MAX_BUF-1, "%s/%s", first_map_ext_path, op->arch->name);
1318  /*printf("%s\n", mapname);*/
1319  tmp = object_new();
1320  EXIT_PATH(tmp) = add_string(mapname);
1321  EXIT_X(tmp) = op->x;
1322  EXIT_Y(tmp) = op->y;
1323  enter_exit(op, tmp);
1324 
1325  if (oldmap != op->map) {
1326  /* map exists, update bed of reality location, in case player dies */
1327  op->contr->bed_x = op->x;
1328  op->contr->bed_y = op->y;
1329  snprintf(op->contr->savebed_map, sizeof(op->contr->savebed_map), "%s", mapname);
1330  }
1331 
1333  } else {
1334  LOG(llevDebug, "first_map_ext_path not set\n");
1335  }
1336  return;
1337  }
1338 
1339  /* Following actually changes the race - this is the default command
1340  * if we don't match with one of the options above.
1341  */
1342 
1343  tmp_loop = 0;
1344  while (!tmp_loop) {
1345  const char *name = add_string(op->name);
1346  int x = op->x, y = op->y;
1347 
1348  remove_statbonus(op);
1349  object_remove(op);
1350  /* get_player_archetype() is really misnamed - it will
1351  * get the next archetype from the list.
1352  */
1353  op->arch = get_player_archetype(op->arch);
1354  object_copy(&op->arch->clone, op);
1355  op->stats = op->contr->orig_stats;
1356  free_string(op->name);
1357  op->name = name;
1358  free_string(op->name_pl);
1359  op->name_pl = add_string(name);
1360  SET_ANIMATION(op, 2); /* So player faces south */
1361  object_insert_in_map_at(op, op->map, op, 0, x, y);
1362  strncpy(op->contr->title, op->arch->clone.name, sizeof(op->contr->title)-1);
1363  op->contr->title[sizeof(op->contr->title)-1] = '\0';
1364  add_statbonus(op);
1365  tmp_loop = allowed_class(op);
1366  }
1368  esrv_update_item(UPD_FACE, op, op);
1369  fix_object(op);
1370  op->stats.hp = op->stats.maxhp;
1371  op->stats.sp = op->stats.maxsp;
1372  op->stats.grace = 0;
1373  if (op->msg)
1375  op->msg);
1376  send_query(&op->contr->socket, CS_QUERY_SINGLECHAR, "Press any key for the next race.\nPress `d' to play this race.\n");
1377 }
1378 
1400 int check_race_and_class(living *stats, archetype *race, archetype *opclass)
1401 {
1402  int i, stat, failure=0;
1403 
1404  for (i = 0; i < NUM_STATS; i++) {
1405  stat = get_attr_value(stats, i);
1406  if (race)
1407  stat += get_attr_value(&race->clone.stats, i);
1408 
1409  if (opclass)
1410  stat += get_attr_value(&opclass->clone.stats, i);
1411 
1412  set_attr_value(stats, i, stat);
1413 
1414  /* We process all stats, regardless if there is a failure
1415  * or not.
1416  */
1417  if (stat < MIN_STAT) failure=1;
1418 
1419  /* Maybe this should be an error? Player is losing
1420  * some stats points here, but it is legal.
1421  */
1422  if (stat > settings.max_stat) stat = settings.max_stat;
1423  }
1424  return failure;
1425 
1426 }
1427 
1450 int apply_race_and_class(object *op, archetype *race, archetype *opclass, living *stats)
1451 {
1452  const char *name = add_string(op->name);
1453  char buf[MAX_BUF];
1454  object *inv;
1455 
1456  /* Free any objects in character inventory - they
1457  * shouldn't have any, but there is the potential that
1458  * we give them objects below and then get a creation
1459  * failure (stat out of range), in which case
1460  * those objects would be in the inventory.
1461  */
1462  while (op->inv) {
1463  inv = op->inv;
1464  object_remove(inv);
1465  object_free2(inv, 0);
1466  }
1467 
1468  object_copy(&race->clone, op);
1469  free_string(op->name);
1470  op->name = name;
1471  free_string(op->name_pl);
1472  op->name_pl = add_string(name);
1473  SET_ANIMATION(op, 2); /* So player faces south */
1474  snprintf(op->contr->title, sizeof(op->contr->title), "%s", op->arch->clone.name);
1475 
1476  if (stats) {
1477  /* Copy over the stats. Use this instead a memcpy because
1478  * we only want to copy over a few specific stats, and
1479  * leave things like maxhp, maxsp, etc, unchanged.
1480  */
1481  int i, stat;
1482  for (i = 0; i < NUM_STATS; i++) {
1483  stat = get_attr_value(stats, i);
1484  set_attr_value(&op->stats, i, stat);
1485  set_attr_value(&op->contr->orig_stats, i, stat);
1486  }
1487  } else {
1488  /* Note that this will repeated increase the stat values
1489  * if the caller does not reset them. Only do this
1490  * if stats is not provided - if stats is provided, those
1491  * are already adjusted.
1492  */
1493  add_statbonus(op);
1494 
1495  /* Checks that all stats are greater than 1. Once again,
1496  * only do this if stats are not provided
1497  */
1498  if (!allowed_class(op)) return 1;
1499  }
1500 
1502  op->stats.hp = op->stats.maxhp;
1503  op->stats.sp = op->stats.maxsp;
1504  op->stats.grace = 0;
1505 
1506  /* this must before then initial items are given */
1507  esrv_new_player(op->contr, op->weight+op->carrying);
1508  create_treasure(find_treasurelist("starting_wealth"), op, 0, 0, 0);
1509 
1510  /* This has to be done before class, otherwise the NOCLASSFACECHANGE
1511  * object is not in the inventory, and racial face will get overwritten.
1512  */
1514 
1515  if (stats) {
1516  /* Apply class information */
1518  } else {
1519  apply_changes_to_player(op, &opclass->clone, 0);
1520 
1521  /* Checks that all stats are greater than 1 */
1522  if (!allowed_class(op)) return 2;
1523  }
1524 
1525  /* Lauwenmark : Here we handle the BORN global event */
1527 
1528  /* Lauwenmark : We then generate a LOGIN event */
1530 
1531  object_set_msg(op, NULL);
1532 
1533  /* We create this now because some of the unique maps will need it
1534  * to save here.
1535  */
1536  snprintf(buf, sizeof(buf), "%s/%s/%s", settings.localdir, settings.playerdir, op->name);
1537  make_path_to_file(buf);
1538 
1539 #ifdef AUTOSAVE
1540  op->contr->last_save_tick = pticks;
1541 #endif
1542 
1543  CLEAR_FLAG(op, FLAG_WIZ);
1544  link_player_skills(op);
1545  fix_object(op);
1546 
1547  esrv_send_inventory(op, op);
1548  esrv_update_item(UPD_FACE, op, op);
1549 
1550  return 0;
1551 
1552 }
1553 
1562 void key_confirm_quit(object *op, char key) {
1563  char buf[MAX_BUF];
1564  mapstruct *mp, *next;
1565 
1566  // this was tested when 'quit' command was issued, but better safe than sorry.
1567  if (QUERY_FLAG(op, FLAG_WAS_WIZ)) {
1569  draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOGIN, "Can't quit when in DM mode.");
1570  return;
1571  }
1572 
1573  if (key != 'y' && key != 'Y' && key != 'q' && key != 'Q') {
1576  "OK, continuing to play.");
1577  return;
1578  }
1579 
1580  /* Lauwenmark : Here we handle the REMOVE global event */
1582  pets_terminate_all(op);
1583  object_remove(op);
1584  op->direction = 0;
1586  "%s quits the game.",
1587  op->name);
1588 
1589  strcpy(op->contr->killer, "quit");
1590  hiscore_check(op, 0);
1591  party_leave(op);
1592  if (settings.set_title == TRUE)
1593  player_set_own_title(op->contr, "");
1594 
1595 
1596  /* We need to hunt for any per player unique maps in memory and
1597  * get rid of them. The trailing slash in the path is intentional,
1598  * so that players named 'Ab' won't match against players 'Abe' pathname
1599  */
1600  snprintf(buf, sizeof(buf), "%s/%s/%s/", settings.localdir, settings.playerdir, op->name);
1601  for (mp = first_map; mp != NULL; mp = next) {
1602  next = mp->next;
1603  if (!strncmp(mp->path, buf, strlen(buf)))
1604  delete_map(mp);
1605  }
1606 
1607  delete_character(op->name);
1608 
1609  /* Remove player from account list and send back data if needed */
1610  if (op->contr->socket.account_chars != NULL) {
1613  /* char information is reloaded in send_account_players below */
1615  op->contr->socket.account_chars = NULL;
1618  }
1619 
1620  play_again(op);
1621 }
1622 
1629 static void flee_player(object *op) {
1630  int dir, diff;
1631  rv_vector rv;
1632 
1633  if (op->stats.hp < 0) {
1634  LOG(llevDebug, "Fleeing player is dead.\n");
1635  CLEAR_FLAG(op, FLAG_SCARED);
1636  return;
1637  }
1638 
1639  if (op->enemy == NULL) {
1640  LOG(llevDebug, "Fleeing player had no enemy.\n");
1641  CLEAR_FLAG(op, FLAG_SCARED);
1642  return;
1643  }
1644 
1645  /* Seen some crashes here. Since we don't store an
1646  * op->enemy_count, it is possible that something destroys the
1647  * actual enemy, and the object is recycled.
1648  */
1649  if (op->enemy->map == NULL) {
1650  CLEAR_FLAG(op, FLAG_SCARED);
1651  object_set_enemy(op, NULL);
1652  return;
1653  }
1654 
1655  if (!(random_roll(0, 4, op, PREFER_LOW)) && did_make_save(op, op->level, 0)) {
1656  object_set_enemy(op, NULL);
1657  CLEAR_FLAG(op, FLAG_SCARED);
1658  return;
1659  }
1660  if (!get_rangevector(op, op->enemy, &rv, 0)) {
1661  object_set_enemy(op, NULL);
1662  CLEAR_FLAG(op, FLAG_SCARED);
1663  return;
1664  }
1665 
1666  dir = absdir(4+rv.direction);
1667  for (diff = 0; diff < 3; diff++) {
1668  int m = 1-(RANDOM()&2);
1669  if (move_ob(op, absdir(dir+diff*m), op)
1670  || (diff == 0 && move_ob(op, absdir(dir-diff*m), op))) {
1671  return;
1672  }
1673  }
1674  /* Cornered, get rid of scared */
1675  CLEAR_FLAG(op, FLAG_SCARED);
1676  object_set_enemy(op, NULL);
1677 }
1678 
1688 int check_pick(object *op) {
1689  tag_t op_tag;
1690  int stop = 0;
1691  int j, k, wvratio, current_ratio;
1692  char putstring[128], tmpstr[16];
1693 
1694  /* if you're flying, you can't pick up anything */
1695  if (op->move_type&MOVE_FLYING)
1696  return 1;
1697 
1698  op_tag = op->count;
1699 
1700  FOR_BELOW_PREPARE(op, tmp) {
1701  if (object_was_destroyed(op, op_tag))
1702  return 0;
1703 
1704  if (!object_can_pick(op, tmp))
1705  continue;
1706 
1707  if (op->contr->search_str[0] != '\0' && settings.search_items == TRUE) {
1708  if (object_matches_string(op, tmp, op->contr->search_str))
1709  pick_up(op, tmp);
1710  continue;
1711  }
1712 
1713  /* high not bit set? We're using the old autopickup model */
1714  if (!(op->contr->mode&PU_NEWMODE)) {
1715  switch (op->contr->mode) {
1716  case 0:
1717  return 1; /* don't pick up */
1718 
1719  case 1:
1720  pick_up(op, tmp);
1721  return 1;
1722 
1723  case 2:
1724  pick_up(op, tmp);
1725  return 0;
1726 
1727  case 3:
1728  return 0; /* stop before pickup */
1729 
1730  case 4:
1731  pick_up(op, tmp);
1732  break;
1733 
1734  case 5:
1735  pick_up(op, tmp);
1736  stop = 1;
1737  break;
1738 
1739  case 6:
1740  if (QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)
1741  && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED))
1742  pick_up(op, tmp);
1743  break;
1744 
1745  case 7:
1746  if (tmp->type == MONEY || tmp->type == GEM)
1747  pick_up(op, tmp);
1748  break;
1749 
1750  default:
1751  /* use value density */
1752  if (!QUERY_FLAG(tmp, FLAG_UNPAID)
1753  && (price_base(tmp)*100/(tmp->weight*MAX(tmp->nrof, 1))) >= op->contr->mode)
1754  pick_up(op, tmp);
1755  }
1756  } else { /* old model */
1757  /* NEW pickup handling */
1758  if (op->contr->mode&PU_DEBUG) {
1759  /* some debugging code to figure out item information */
1761  "item name: %s item type: %d weight/value: %d",
1762  tmp->name ? tmp->name : tmp->arch->name, tmp->type,
1763  (int)(price_base(tmp)*100/(tmp->weight*MAX(tmp->nrof, 1))));
1764 
1765 
1766  snprintf(putstring, sizeof(putstring), "...flags: ");
1767  for (k = 0; k < 4; k++) {
1768  for (j = 0; j < 32; j++) {
1769  if ((tmp->flags[k]>>j)&0x01) {
1770  snprintf(tmpstr, sizeof(tmpstr), "%d ", k*32+j);
1771  strcat(putstring, tmpstr);
1772  }
1773  }
1774  }
1776  putstring);
1777  }
1778  /* philosophy:
1779  * It's easy to grab an item type from a pile, as long as it's
1780  * generic. This takes no game-time. For more detailed pickups
1781  * and selections, select-items should be used. This is a
1782  * grab-as-you-run type mode that's really useful for arrows for
1783  * example.
1784  * The drawback: right now it has no frontend, so you need to
1785  * stick the bits you want into a calculator in hex mode and then
1786  * convert to decimal and then 'pickup <#>
1787  */
1788 
1789  /* the first two modes are exclusive: if NOTHING we return, if
1790  * STOP then we stop. All the rest are applied sequentially,
1791  * meaning if any test passes, the item gets picked up. */
1792 
1793  /* if mode is set to pick nothing up, return */
1794 
1795  if (op->contr->mode == PU_NOTHING)
1796  return 1;
1797 
1798  /* if mode is set to stop when encountering objects, return.
1799  * Take STOP before INHIBIT since it doesn't actually pick
1800  * anything up */
1801 
1802  if (op->contr->mode&PU_STOP)
1803  return 0;
1804 
1805  /* useful for going into stores and not losing your settings... */
1806  /* and for battles where you don't want to get loaded down while
1807  * fighting */
1808  if (op->contr->mode&PU_INHIBIT)
1809  return 1;
1810 
1811  /* prevent us from turning into auto-thieves :) */
1812  if (QUERY_FLAG(tmp, FLAG_UNPAID))
1813  continue;
1814 
1815  /* ignore known cursed objects */
1817  continue;
1818 
1819  /* all food and drink if desired */
1820  /* question: don't pick up known-poisonous stuff? */
1821  if (op->contr->mode&PU_FOOD)
1822  if (tmp->type == FOOD) {
1823  pick_up(op, tmp);
1824  continue;
1825  }
1826  if (op->contr->mode&PU_DRINK)
1827  if (tmp->type == DRINK || (tmp->type == POISON && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED))) {
1828  pick_up(op, tmp);
1829  continue;
1830  }
1831  /* we don't forget dragon food */
1832  if (op->contr->mode&PU_FLESH)
1833  if (tmp->type == FLESH) {
1834  pick_up(op, tmp);
1835  continue;
1836  }
1837  if (op->contr->mode&PU_POTION)
1838  if (tmp->type == POTION) {
1839  pick_up(op, tmp);
1840  continue;
1841  }
1842 
1843  /* spellbooks, skillscrolls and normal books/scrolls */
1844  if (op->contr->mode&PU_SPELLBOOK)
1845  if (tmp->type == SPELLBOOK) {
1846  pick_up(op, tmp);
1847  continue;
1848  }
1849  if (op->contr->mode&PU_SKILLSCROLL)
1850  if (tmp->type == SKILLSCROLL) {
1851  pick_up(op, tmp);
1852  continue;
1853  }
1854  if (op->contr->mode&PU_READABLES)
1855  if (tmp->type == BOOK || tmp->type == SCROLL) {
1856  pick_up(op, tmp);
1857  continue;
1858  }
1859 
1860  /* wands/staves/rods/horns/skill tools */
1861  if (op->contr->mode&PU_MAGIC_DEVICE)
1862  if (tmp->type == WAND || tmp->type == ROD || tmp->type == WEAPON_IMPROVER || tmp->type == ARMOUR_IMPROVER || tmp->type == SKILL_TOOL) {
1863  pick_up(op, tmp);
1864  continue;
1865  }
1866 
1867  /* pick up all magical items */
1868  if (op->contr->mode&PU_MAGICAL)
1870  pick_up(op, tmp);
1871  continue;
1872  }
1873 
1874  if (op->contr->mode&PU_VALUABLES) {
1875  if (tmp->type == MONEY || tmp->type == GEM) {
1876  pick_up(op, tmp);
1877  continue;
1878  }
1879  }
1880 
1881  /* rings & amulets - talismans seems to be typed AMULET */
1882  if (op->contr->mode&PU_JEWELS)
1883  if (tmp->type == RING || tmp->type == AMULET) {
1884  pick_up(op, tmp);
1885  continue;
1886  }
1887 
1888  /* bows and arrows. Bows are good for selling! */
1889  if (op->contr->mode&PU_BOW)
1890  if (tmp->type == BOW) {
1891  pick_up(op, tmp);
1892  continue;
1893  }
1894  if (op->contr->mode&PU_ARROW)
1895  if (tmp->type == ARROW) {
1896  pick_up(op, tmp);
1897  continue;
1898  }
1899 
1900  /* all kinds of armor etc. */
1901  if (op->contr->mode&PU_ARMOUR)
1902  if (tmp->type == ARMOUR) {
1903  pick_up(op, tmp);
1904  continue;
1905  }
1906  if (op->contr->mode&PU_HELMET)
1907  if (tmp->type == HELMET) {
1908  pick_up(op, tmp);
1909  continue;
1910  }
1911  if (op->contr->mode&PU_SHIELD)
1912  if (tmp->type == SHIELD) {
1913  pick_up(op, tmp);
1914  continue;
1915  }
1916  if (op->contr->mode&PU_BOOTS)
1917  if (tmp->type == BOOTS) {
1918  pick_up(op, tmp);
1919  continue;
1920  }
1921  if (op->contr->mode&PU_GLOVES)
1922  if (tmp->type == GLOVES) {
1923  pick_up(op, tmp);
1924  continue;
1925  }
1926  if (op->contr->mode&PU_CLOAK)
1927  if (tmp->type == CLOAK) {
1928  pick_up(op, tmp);
1929  continue;
1930  }
1931 
1932  /* hoping to catch throwing daggers here */
1933  if (op->contr->mode&PU_MISSILEWEAPON)
1934  if (tmp->type == WEAPON && QUERY_FLAG(tmp, FLAG_IS_THROWN)) {
1935  pick_up(op, tmp);
1936  continue;
1937  }
1938 
1939  /* careful: chairs and tables are weapons! */
1940  /* Note that tables and chairs have been changed to not be weapons as of 2011. */
1941  if (op->contr->mode&PU_MELEEWEAPON) {
1942  // The code here used to handle avoiding tables and chairs.
1943  // Since there have been many server-arch prerequisites since the change,
1944  // we should be good not handling them here.
1945  // SilverNexus 2018-12-30
1946  if (tmp->type == WEAPON) {
1947  pick_up(op, tmp);
1948  continue;
1949  }
1950  }
1951 
1952  /* misc stuff that's useful */
1953  if (op->contr->mode&PU_KEY)
1954  if (tmp->type == KEY || tmp->type == SPECIAL_KEY) {
1955  pick_up(op, tmp);
1956  continue;
1957  }
1958 
1959  if (op->contr->mode&PU_CONTAINER && tmp->type == CONTAINER) {
1960  pick_up(op, tmp);
1961  continue;
1962  }
1963 
1964  /* any of the last 4 bits set means we use the ratio for value
1965  * pickups */
1966  if (op->contr->mode&PU_RATIO) {
1967  /* use value density to decide what else to grab.
1968  * >=7 was >= op->contr->mode
1969  * >=7 is the old standard setting. Now we take the last 4 bits
1970  * and multiply them by 5, giving 0..15*5== 5..75 */
1971  wvratio = (op->contr->mode&PU_RATIO)*5;
1972  current_ratio = price_base(tmp)*100/(tmp->weight*MAX(tmp->nrof, 1));
1973  if (current_ratio >= wvratio) {
1974  pick_up(op, tmp);
1975  continue;
1976  }
1977  }
1978  } /* the new pickup model */
1979  } FOR_BELOW_FINISH();
1980  return !stop;
1981 }
1982 
1995 static object *find_arrow(object *op, const char *type) {
1996  object *tmp = NULL;
1997 
1998  FOR_INV_PREPARE(op, inv)
1999  if (!tmp
2000  && inv->type == CONTAINER
2001  && inv->race == type
2002  && QUERY_FLAG(inv, FLAG_APPLIED))
2003  tmp = find_arrow(inv, type);
2004  else if (inv->type == ARROW && inv->race == type)
2005  return inv;
2006  FOR_INV_FINISH();
2007  return tmp;
2008 }
2009 
2027 static object *find_better_arrow(object *op, object *target, const char *type, int *better) {
2028  object *tmp = NULL, *ntmp;
2029  int attacknum, attacktype, betterby = 0, i;
2030 
2031  if (!type)
2032  return NULL;
2033 
2034  FOR_INV_PREPARE(op, arrow) {
2035  if (arrow->type == CONTAINER
2036  && arrow->race == type
2037  && QUERY_FLAG(arrow, FLAG_APPLIED)) {
2038  i = 0;
2039  ntmp = find_better_arrow(arrow, target, type, &i);
2040  if (i > betterby) {
2041  tmp = ntmp;
2042  betterby = i;
2043  }
2044  } else if (arrow->type == ARROW && arrow->race == type) {
2045  /* always prefer assassination/slaying */
2046  if (target->race != NULL
2047  && arrow->slaying != NULL
2048  && strstr(arrow->slaying, target->race)) {
2049  if (arrow->attacktype&AT_DEATH) {
2050  if (better)
2051  *better = 100;
2052  return arrow;
2053  } else {
2054  tmp = arrow;
2055  betterby = (arrow->magic+arrow->stats.dam)*2;
2056  }
2057  } else {
2058  for (attacknum = 0; attacknum < NROFATTACKS; attacknum++) {
2059  attacktype = 1<<attacknum;
2060  if ((arrow->attacktype&attacktype) && (target->arch->clone.resist[attacknum]) < 0)
2061  if (((arrow->magic+arrow->stats.dam)*(100-target->arch->clone.resist[attacknum])/100) > betterby) {
2062  tmp = arrow;
2063  betterby = (arrow->magic+arrow->stats.dam)*(100-target->arch->clone.resist[attacknum])/100;
2064  }
2065  }
2066  if ((2+arrow->magic+arrow->stats.dam) > betterby) {
2067  tmp = arrow;
2068  betterby = 2+arrow->magic+arrow->stats.dam;
2069  }
2070  if (arrow->title && (1+arrow->magic+arrow->stats.dam) > betterby) {
2071  tmp = arrow;
2072  betterby = 1+arrow->magic+arrow->stats.dam;
2073  }
2074  }
2075  }
2076  } FOR_INV_FINISH();
2077  if (tmp == NULL)
2078  return find_arrow(op, type);
2079 
2080  if (better)
2081  *better = betterby;
2082  return tmp;
2083 }
2084 
2097 static object *pick_arrow_target(object *op, const char *type, int dir) {
2098  object *tmp = NULL;
2099  mapstruct *m;
2100  int i, mflags, found, number;
2101  int16_t x, y;
2102 
2103  if (op->map == NULL)
2104  return find_arrow(op, type);
2105 
2106  /* do a dex check */
2107  number = (die_roll(2, 40, op, PREFER_LOW)-2)/2;
2108  if (number > (op->stats.Dex+(op->chosen_skill ? op->chosen_skill->level : op->level)))
2109  return find_arrow(op, type);
2110 
2111  m = op->map;
2112  x = op->x;
2113  y = op->y;
2114 
2115  /* find the first target */
2116  for (i = 0, found = 0; i < 20; i++) {
2117  x += freearr_x[dir];
2118  y += freearr_y[dir];
2119  mflags = get_map_flags(m, &m, x, y, &x, &y);
2120  if (mflags&P_OUT_OF_MAP || mflags&P_BLOCKSVIEW) {
2121  tmp = NULL;
2122  break;
2123  } else if (GET_MAP_MOVE_BLOCK(m, x, y) == MOVE_FLY_LOW) {
2124  /* This block presumes arrows and the like are MOVE_FLY_SLOW -
2125  * perhaps a bad assumption.
2126  */
2127  tmp = NULL;
2128  break;
2129  }
2130  if (mflags&P_IS_ALIVE) {
2131  FOR_MAP_PREPARE(m, x, y, tmp2)
2132  if (QUERY_FLAG(tmp2, FLAG_ALIVE)) {
2133  tmp = tmp2;
2134  found++;
2135  break;
2136  }
2137  FOR_MAP_FINISH();
2138  if (found)
2139  break;
2140  }
2141  }
2142  if (tmp == NULL)
2143  return find_arrow(op, type);
2144 
2145  return find_better_arrow(op, HEAD(tmp), type, NULL);
2146 }
2147 
2166 int fire_bow(object *op, object *arrow, int dir, int wc_mod, int16_t sx, int16_t sy) {
2167  object *bow;
2168  tag_t tag;
2169  int bowspeed, mflags;
2170  mapstruct *m;
2171 
2172  if (!dir) {
2174  "You can't shoot yourself!");
2175  return 0;
2176  }
2177  if (op->type == PLAYER)
2178  bow = op->contr->ranges[range_bow];
2179  else {
2180  /* Don't check for applied - monsters don't apply bows - in that way, they
2181  * don't need to switch back and forth between bows and weapons.
2182  */
2183  bow = object_find_by_type(op, BOW);
2184  if (!bow) {
2185  LOG(llevError, "Range: bow without activated bow (%s).\n", op->name);
2186  return 0;
2187  }
2188  }
2189  if (!bow->race || !bow->skill) {
2191  "Your %s is broken.",
2192  bow->name);
2193  return 0;
2194  }
2195 
2196  bowspeed = bow->stats.sp+get_dex_bonus(op->stats.Dex);
2197 
2198  /* penalize ROF for bestarrow */
2199  if (op->type == PLAYER && op->contr->bowtype == bow_bestarrow)
2200  bowspeed -= get_dex_bonus(op->stats.Dex)+5;
2201  if (bowspeed < 1)
2202  bowspeed = 1;
2203 
2204  if (arrow == NULL) {
2205  arrow = find_arrow(op, bow->race);
2206  if (arrow == NULL) {
2207  if (op->type == PLAYER)
2210  "You have no %s left.",
2211  bow->race);
2212  /* FLAG_READY_BOW will get reset if the monsters picks up some arrows */
2213  else
2215  return 0;
2216  }
2217  }
2218  mflags = get_map_flags(op->map, &m, sx, sy, &sx, &sy);
2219  if (mflags&P_OUT_OF_MAP) {
2220  return 0;
2221  }
2222  if (GET_MAP_MOVE_BLOCK(m, sx, sy)&MOVE_FLY_LOW) {
2223  return 0;
2224  }
2225 
2226  /* this should not happen, but sometimes does */
2227  if (arrow->nrof == 0) {
2228  object_remove(arrow);
2230  return 0;
2231  }
2232 
2233  arrow = object_split(arrow, 1, NULL, 0);
2234  if (arrow == NULL) {
2236  "You have no %s left.",
2237  bow->race);
2238  return 0;
2239  }
2240  object_set_owner(arrow, op);
2241  if (arrow->skill)
2242  free_string(arrow->skill);
2243  arrow->skill = add_refcount(bow->skill);
2244 
2245  arrow->direction = dir;
2246 
2247  if (op->type == PLAYER) {
2248  op->speed_left = 0.01-(float)FABS(op->speed)*100/bowspeed;
2249  fix_object(op);
2250  }
2251 
2252  if (bow->anim_suffix != NULL)
2253  apply_anim_suffix(op, bow->anim_suffix);
2254 
2255 /* SET_ANIMATION(arrow, arrow->direction);*/
2256  object_update_turn_face(arrow);
2257  arrow->stats.sp = arrow->stats.wc; /* save original wc and dam */
2258  arrow->stats.hp = arrow->stats.dam;
2259  arrow->stats.grace = arrow->attacktype;
2260  if (arrow->slaying != NULL)
2261  arrow->spellarg = strdup_local(arrow->slaying);
2262 
2263  /* Note that this was different for monsters - they got their level
2264  * added to the damage. I think the strength bonus is more proper.
2265  */
2266 
2267  arrow->stats.dam += (QUERY_FLAG(bow, FLAG_NO_STRENGTH) ? 0 : get_dam_bonus(op->stats.Str))
2268  +bow->stats.dam
2269  +bow->magic
2270  +arrow->magic;
2271 
2272  /* update the speed */
2273  arrow->speed = (float)((QUERY_FLAG(bow, FLAG_NO_STRENGTH) ? 0 : get_dam_bonus(op->stats.Str))+bow->magic+arrow->magic)/5.0
2274  +(float)bow->stats.dam/7.0;
2275 
2276  if (arrow->speed < 1.0)
2277  arrow->speed = 1.0;
2278  object_update_speed(arrow);
2279  arrow->speed_left = 0;
2280 
2281  if (op->type == PLAYER) {
2282  /* we don't want overflows of wc (sint), so cap the value - mod and pl should be subtracted */
2283  int mod = bow->magic
2284  +arrow->magic
2285  +get_dex_bonus(op->stats.Dex)
2286  +get_thaco_bonus(op->stats.Str)
2287  +arrow->stats.wc
2288  +bow->stats.wc
2289  -wc_mod;
2290  int plmod = (op->chosen_skill ? op->chosen_skill->level : op->level);
2291  if (plmod+mod > 140)
2292  plmod = 140-mod;
2293  else if (plmod+mod < -100)
2294  plmod = -100-mod;
2295  arrow->stats.wc = 20-(int8_t)plmod-(int8_t)mod;
2296 
2297  arrow->level = op->chosen_skill ? op->chosen_skill->level : op->level;
2298  } else {
2299  arrow->stats.wc = op->stats.wc
2300  -bow->magic
2301  -arrow->magic
2302  -arrow->stats.wc
2303  +wc_mod;
2304 
2305  arrow->level = op->level;
2306  }
2307  if (arrow->attacktype == AT_PHYSICAL)
2308  arrow->attacktype |= bow->attacktype;
2309  if (bow->slaying != NULL)
2310  arrow->slaying = add_string(bow->slaying);
2311 
2312  /* If move_type is ever changed, monster.c:monster_use_bow() needs to be changed too. */
2313  arrow->move_type = MOVE_FLY_LOW;
2314  arrow->move_on = MOVE_FLY_LOW|MOVE_WALK;
2315 
2316  tag = arrow->count;
2317  object_insert_in_map_at(arrow, m, op, 0, sx, sy);
2318 
2319  if (!object_was_destroyed(arrow, tag)) {
2320  play_sound_map(SOUND_TYPE_ITEM, arrow, arrow->direction, "fire");
2321  ob_process(arrow);
2322  }
2323 
2324  return 1;
2325 }
2326 
2337 static int similar_direction(int a, int b) {
2338  /* shortcut the obvious */
2339  if (a == b)
2340  return 1;
2341  /* Made this cleaner using modulus instead of a switch statement
2342  * We only needed the direction and the two adjacent to it
2343  * (8 is adjacent to 1 here) to return true, so a - 1, a, and a + 1
2344  * are the three directions that get "similar" affirmed.
2345  * -- Daniel Hawkins 2015-05-28
2346  */
2347  // The last one for the offset is added afterwards so we get
2348  // 1-8 instead of 0-7 (specifically, 0 becomes 8 without changing
2349  // the other values).
2350  if ((a % 8) + 1 == b || (a + 6 % 8) + 1 == b)
2351  return 1;
2352  return 0;
2353 }
2354 
2371 static int player_fire_bow(object *op, int dir) {
2372  int ret = 0, wcmod = 0;
2373 
2374  if (op->contr->bowtype == bow_bestarrow) {
2375  ret = fire_bow(op, pick_arrow_target(op, op->contr->ranges[range_bow]->race, dir), dir, 0, op->x, op->y);
2376  } else if (op->contr->bowtype >= bow_n && op->contr->bowtype <= bow_nw) {
2377  if (!similar_direction(dir, op->contr->bowtype-bow_n+1))
2378  wcmod = -1;
2379  ret = fire_bow(op, NULL, op->contr->bowtype-bow_n+1, wcmod, op->x, op->y);
2380  } else if (op->contr->bowtype == bow_threewide) {
2381  ret = fire_bow(op, NULL, dir, 0, op->x, op->y);
2382  ret |= fire_bow(op, NULL, dir, -5, op->x+freearr_x[absdir(dir+2)], op->y+freearr_y[absdir(dir+2)]);
2383  ret |= fire_bow(op, NULL, dir, -5, op->x+freearr_x[absdir(dir-2)], op->y+freearr_y[absdir(dir-2)]);
2384  } else if (op->contr->bowtype == bow_spreadshot) {
2385  ret |= fire_bow(op, NULL, dir, 0, op->x, op->y);
2386  ret |= fire_bow(op, NULL, absdir(dir-1), -5, op->x, op->y);
2387  ret |= fire_bow(op, NULL, absdir(dir+1), -5, op->x, op->y);
2388  } else {
2389  /* Simple case */
2390  ret = fire_bow(op, NULL, dir, 0, op->x, op->y);
2391  }
2392  return ret;
2393 }
2394 
2407 static void fire_misc_object(object *op, int dir) {
2408  object *item;
2409  char name[MAX_BUF];
2410 
2411  item = op->contr->ranges[range_misc];
2412  if (!item) {
2414  "You have no range item readied.");
2415  return;
2416  }
2417  if (!item->inv) {
2418  LOG(llevError, "Object %s lacks a spell\n", item->name);
2419  return;
2420  }
2421  if (item->type == WAND) {
2422  if (item->stats.food <= 0) {
2423  play_sound_player_only(op->contr, SOUND_TYPE_ITEM, item, 0, "poof");
2424  query_base_name(item, 0, name, MAX_BUF);
2426  "The %s goes poof.",
2427  name);
2428  return;
2429  }
2430  } else if (item->type == ROD) {
2431  if (item->stats.hp < SP_level_spellpoint_cost(item, item->inv, SPELL_HIGHEST)) {
2432  play_sound_player_only(op->contr, SOUND_TYPE_ITEM, item, 0, "poof");
2433  query_base_name(item, 0, name, MAX_BUF);
2435  "The %s whines for a while, but nothing happens.",
2436  name);
2437  return;
2438  }
2439  }
2440 
2441  if (cast_spell(op, item, dir, item->inv, NULL)) {
2442  SET_FLAG(op, FLAG_BEEN_APPLIED); /* You now know something about it */
2443  if (item->type == WAND) {
2444  drain_wand_charge(item);
2445  } else if (item->type == ROD) {
2446  drain_rod_charge(item);
2447  }
2448  }
2449 }
2450 
2459 void fire(object *op, int dir) {
2460 
2461  /* check for loss of invisiblity/hide */
2462  if (action_makes_visible(op))
2463  make_visible(op);
2464 
2465  switch (op->contr->shoottype) {
2466  case range_none:
2467  return;
2468 
2469  case range_bow:
2470  player_fire_bow(op, dir);
2471  return;
2472 
2473  case range_magic: /* Casting spells */
2474  cast_spell(op, op, dir, op->contr->ranges[range_magic], op->contr->spellparam[0] ? op->contr->spellparam : NULL);
2475  return;
2476 
2477  case range_misc:
2478  fire_misc_object(op, dir);
2479  return;
2480 
2481  case range_golem: /* Control summoned monsters from scrolls */
2482  if (op->contr->ranges[range_golem] == NULL
2483  || op->contr->golem_count != op->contr->ranges[range_golem]->count) {
2484  op->contr->ranges[range_golem] = NULL;
2485  op->contr->shoottype = range_none;
2486  op->contr->golem_count = 0;
2487  } else
2489  return;
2490 
2491  case range_skill:
2492  if (!op->chosen_skill) {
2493  if (op->type == PLAYER)
2495  "You have no applicable skill to use.");
2496  return;
2497  }
2498  (void)do_skill(op, op, op->chosen_skill, dir, NULL);
2499  return;
2500 
2501  case range_builder:
2502  apply_map_builder(op, dir);
2503  return;
2504 
2505  default:
2507  "Illegal shoot type.");
2508  return;
2509  }
2510 }
2511 
2531 object *find_key(object *pl, object *container, object *door) {
2532  object *tmp, *key;
2533 
2534  /* Should not happen, but sanity checking is never bad */
2535  if (container->inv == NULL)
2536  return NULL;
2537 
2538  /* First, lets try to find a key in the top level inventory */
2539  tmp = NULL;
2540  if (door->type == DOOR) {
2541  tmp = object_find_by_type(container, KEY);
2542  }
2543  /* For sanity, we should really check door type, but other stuff
2544  * (like containers) can be locked with special keys
2545  */
2546  if (!tmp && door->slaying != NULL) {
2547  tmp = object_find_by_type_and_slaying(container, SPECIAL_KEY, door->slaying);
2548  }
2549  /* No key found - lets search inventories now */
2550  /* If we find and use a key in an inventory, return at that time.
2551  * otherwise, if we search all the inventories and still don't find
2552  * a key, return
2553  */
2554  if (!tmp) {
2555  FOR_INV_PREPARE(container, tmp) {
2556  /* No reason to search empty containers */
2557  if (tmp->type == CONTAINER && tmp->inv) {
2558  key = find_key(pl, tmp, door);
2559  if (key != NULL)
2560  return key;
2561  }
2562  } FOR_INV_FINISH();
2563  return NULL;
2564  }
2565  /* We get down here if we have found a key. Now if its in a container,
2566  * see if we actually want to use it
2567  */
2568  if (pl != container) {
2569  /* Only let players use keys in containers */
2570  if (!pl->contr)
2571  return NULL;
2572  /* cases where this fails:
2573  * If we only search the player inventory, return now since we
2574  * are not in the players inventory.
2575  * If the container is not active, return now since only active
2576  * containers can be used.
2577  * If we only search keyrings and the container does not have
2578  * a race/isn't a keyring.
2579  * No checking for all containers - to fall through past here,
2580  * inv must have been an container and must have been active.
2581  *
2582  * Change the color so that the message doesn't disappear with
2583  * all the others.
2584  */
2585  if (pl->contr->usekeys == key_inventory
2586  || !QUERY_FLAG(container, FLAG_APPLIED)
2587  || (pl->contr->usekeys == keyrings && (!container->race || strcmp(container->race, "keys")))) {
2588  char name_tmp[MAX_BUF], name_cont[MAX_BUF];
2589 
2590  query_name(tmp, name_tmp, MAX_BUF);
2591  query_name(container, name_cont, MAX_BUF);
2594  "The %s in your %s vibrates as you approach the door",
2595  name_tmp, name_cont);
2596  return NULL;
2597  }
2598  }
2599  return tmp;
2600 }
2601 
2612 static int player_attack_door(object *op, object *door) {
2613  /* If its a door, try to find a use a key. If we do destroy the door,
2614  * might as well return immediately as there is nothing more to do -
2615  * otherwise, we fall through to the rest of the code.
2616  */
2617  object *key = find_key(op, op, door);
2618 
2619  assert(door->type == DOOR || door->type == LOCKED_DOOR);
2620 
2621  /* IF we found a key, do some extra work */
2622  if (key) {
2623  char name[HUGE_BUF];
2624 
2625  play_sound_map(SOUND_TYPE_GROUND, door, 0, "open");
2626  if (action_makes_visible(op))
2627  make_visible(op);
2628  if (door->inv && (door->inv->type == RUNE || door->inv->type == TRAP))
2629  spring_trap(door->inv, op);
2630 
2631  query_short_name(key, name, HUGE_BUF);
2634  "You open the door with the %s",
2635  name);
2636 
2637  if (door->type == DOOR)
2638  remove_door(door);
2639  else
2640  remove_locked_door(door); /* remove door without violence ;-) */
2641 
2642  /* Do this after we print the message */
2643  object_decrease_nrof_by_one(key); /* Use up one of the keys */
2644 
2645  return 1; /* Nothing more to do below */
2646 
2647  }
2648 
2649  if (door->type == LOCKED_DOOR) {
2650  /* Might as well return now - no other way to open this */
2652  door->msg);
2653  return 1;
2654  }
2655 
2656  if (door->type == DOOR && op->contr && !op->contr->run_on) {
2657  /* Player so try to pick the door */
2658  object *lock = find_skill_by_name(op, "lockpicking");
2659  if (lock) {
2660  /* Even if the lockpicking failed, don't go on moving, player should explicitely attack or run
2661  * to bash the door. */
2662  do_skill(op, op, lock, op->facing, NULL);
2663  return 1;
2664  }
2665  }
2666 
2667  return 0;
2668 }
2669 
2683 void move_player_attack(object *op, int dir) {
2684  object *mon, *tpl, *mon_owner;
2685  int16_t nx, ny;
2686  int on_battleground;
2687  mapstruct *m;
2688 
2689  if (op->contr->transport)
2690  tpl = op->contr->transport;
2691  else
2692  tpl = op;
2693  assert(tpl->map != NULL); // op must be on a map in order to move it
2694  nx = freearr_x[dir]+tpl->x;
2695  ny = freearr_y[dir]+tpl->y;
2696 
2697  on_battleground = op_on_battleground(tpl, NULL, NULL, NULL);
2698 
2699  // Temporarily store the map we are on before movement.
2700  mapstruct *bef = tpl->map;
2701 
2702  /* If braced, or can't move to the square, and it is not out of the
2703  * map, attack it. Note order of if statement is important - don't
2704  * want to be calling move_ob if braced, because move_ob will move the
2705  * player. This is a pretty nasty hack, because if we could
2706  * move to some space, it then means that if we are braced, we should
2707  * do nothing at all. As it is, if we are braced, we go through
2708  * quite a bit of processing. However, it probably is less than what
2709  * move_ob uses.
2710  */
2711  if ((op->contr->braced || !move_ob(tpl, dir, tpl)) && !out_of_map(tpl->map, nx, ny)) {
2712  if (OUT_OF_REAL_MAP(tpl->map, nx, ny)) {
2713  m = get_map_from_coord(tpl->map, &nx, &ny);
2714  if (!m)
2715  return; /* Don't think this should happen */
2716  } else
2717  m = tpl->map;
2718 
2719  if (GET_MAP_OB(m, nx, ny) == NULL) {
2720  /* LOG(llevError, "player_move_attack: GET_MAP_OB returns NULL, but player can not move there.\n");*/
2721  return;
2722  }
2723 
2724  mon = NULL;
2725  /* Go through all the objects, and find ones of interest. Only stop if
2726  * we find a monster - that is something we know we want to attack.
2727  * if its a door or barrel (can roll) see if there may be monsters
2728  * on the space
2729  */
2730  FOR_MAP_PREPARE(m, nx, ny, tmp) {
2731  if (tmp == op) {
2732  continue;
2733  }
2734  if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
2735  mon = tmp;
2736  /* Gros: Objects like (pass-through) doors are alive, but haven't
2737  * their monster flag set - so this is a good way attack real
2738  * monsters in priority.
2739  */
2740  if (QUERY_FLAG(tmp, FLAG_MONSTER))
2741  break;
2742  }
2743  if (tmp->type == LOCKED_DOOR || QUERY_FLAG(tmp, FLAG_CAN_ROLL))
2744  mon = tmp;
2745  } FOR_MAP_FINISH();
2746 
2747  if (mon == NULL) /* This happens anytime the player tries to move */
2748  return; /* into a wall */
2749 
2750  mon = HEAD(mon);
2751  if ((mon->type == DOOR && mon->stats.hp >= 0) || (mon->type == LOCKED_DOOR))
2752  if (player_attack_door(op, mon))
2753  return;
2754 
2755  /* The following deals with possibly attacking peaceful
2756  * or friendly creatures. Basically, all players are considered
2757  * unaggressive. If the moving player has peaceful set, then the
2758  * object should be pushed instead of attacked. It is assumed that
2759  * if you are braced, you will not attack friends accidently,
2760  * and thus will not push them.
2761  */
2762 
2763  /* If the creature is a pet, push it even if the player is not
2764  * peaceful. Our assumption is the creature is a pet if the
2765  * player owns it and it is either friendly or unaggressive.
2766  */
2767  mon_owner = object_get_owner(mon);
2768  if ((op->type == PLAYER)
2769  && (mon_owner == op || (mon_owner != NULL && mon_owner->type == PLAYER && mon_owner->contr->party != NULL && mon_owner->contr->party == op->contr->party))
2770  && (QUERY_FLAG(mon, FLAG_UNAGGRESSIVE) || QUERY_FLAG(mon, FLAG_FRIENDLY))) {
2771  /* If we're braced, we don't want to switch places with it */
2772  if (op->contr->braced)
2773  return;
2774  play_sound_map(SOUND_TYPE_LIVING, mon, dir, "push");
2775  (void)push_ob(mon, dir, op);
2776  if (op->contr->tmp_invis || op->hide)
2777  make_visible(op);
2778  return;
2779  }
2780 
2781  /* in certain circumstances, you shouldn't attack friendly
2782  * creatures. Note that if you are braced, you can't push
2783  * someone, but put it inside this loop so that you won't
2784  * attack them either.
2785  */
2786  if ((mon->type == PLAYER || mon->enemy != op)
2787  && (mon->type == PLAYER || QUERY_FLAG(mon, FLAG_UNAGGRESSIVE) || QUERY_FLAG(mon, FLAG_FRIENDLY))
2788  && (op->contr->peaceful && !on_battleground)) {
2789  if (!op->contr->braced) {
2790  play_sound_map(SOUND_TYPE_LIVING, mon, dir, "push");
2791  (void)push_ob(mon, dir, op);
2792  } else {
2794  "You withhold your attack");
2795  }
2796  if (op->contr->tmp_invis || op->hide)
2797  make_visible(op);
2798  }
2799 
2800  /* If the object is a boulder or other rollable object, then
2801  * roll it if not braced. You can't roll it if you are braced.
2802  */
2803  else if (QUERY_FLAG(mon, FLAG_CAN_ROLL) && (!op->contr->braced)) {
2804  recursive_roll(mon, dir, op);
2805  if (action_makes_visible(op))
2806  make_visible(op);
2807 
2808  /* Any generic living creature. Including things like doors.
2809  * Way it works is like this: First, it must have some hit points
2810  * and be living. Then, it must be one of the following:
2811  * 1) Not a player, 2) A player, but of a different party. Note
2812  * that party_number -1 is no party, so attacks can still happen.
2813  */
2814  } else if ((mon->stats.hp >= 0)
2815  && QUERY_FLAG(mon, FLAG_ALIVE)
2816  && ((mon->type != PLAYER || op->contr->party == NULL || op->contr->party != mon->contr->party))) {
2817  /* If the player hasn't hit something this tick, and does
2818  * so, give them speed boost based on weapon speed. Doing
2819  * it here is better than process_players2, which basically
2820  * incurred a 1 tick offset.
2821  */
2822  if (op->weapon_speed_left < 0) {
2823  op->speed_left = -0.01;
2824  return;
2825  }
2826  op->weapon_speed_left -= 1.0;
2827 
2828  skill_attack(mon, op, 0, NULL, NULL);
2829 
2830  /* If attacking another player, that player gets automatic
2831  * hitback, and doesn't loose luck either.
2832  * Disable hitback on the battleground or if the target is
2833  * the wiz.
2834  */
2835  if (mon->type == PLAYER
2836  && mon->stats.hp >= 0
2837  && !mon->contr->has_hit
2838  && !on_battleground
2839  && !QUERY_FLAG(mon, FLAG_WIZ)) {
2840  short luck = mon->stats.luck;
2841  mon->contr->has_hit = 1;
2842  skill_attack(op, mon, 0, NULL, NULL);
2843  mon->stats.luck = luck;
2844  }
2845  if (action_makes_visible(op))
2846  make_visible(op);
2847  }
2848  } /* if player should attack something */
2849  /* If we changed maps, then try to load the new map music.
2850  * This is not redundant with transfer_ob's call to player_update_bg_music,
2851  * since that call only accounts for teleporters, exits, and such.
2852  * This handles entering tiled maps, which the other could not do.
2853  */
2854  else if (bef != tpl->map)
2855  {
2857  }
2858 }
2859 
2866 static void update_transport_block(object *transport, int dir) {
2867  object *part;
2868  int sx, sy, x, y;
2869 
2870  object_get_multi_size(transport, &sx, &sy, NULL, NULL);
2871  assert(sx == sy);
2872 
2873  if (dir == 1 || dir == 5) {
2874  part = transport;
2875  for (y = 0; y <= sy; y++) {
2876  for (x = 0; x < sx; x++) {
2877  part->move_type = transport->move_type;
2878  part = part->more;
2879  }
2880  part->move_type = 0;
2881  part = part->more;
2882  }
2883  } else if (dir == 3 || dir == 7) {
2884  part = transport;
2885  for (y = 0; y < sy; y++) {
2886  for (x = 0; x <= sx; x++) {
2887  part->move_type = transport->move_type;
2888  part = part->more;
2889  }
2890  }
2891  while (part) {
2892  part->move_type = 0;
2893  part = part->more;
2894  }
2895  } else {
2896  for (part = transport; part; part = part->more) {
2897  part->move_type = transport->move_type;
2898  }
2899  }
2900 }
2901 
2911 static int turn_one_transport(object *transport, object *captain, int dir) {
2912  int x, y, scroll_dir = 0;
2913 
2914  assert(transport->type == TRANSPORT);
2915 
2916  x = transport->x;
2917  y = transport->y;
2918 
2919  if (transport->direction == 1 && dir == 8) {
2920  x--;
2921  } else if (transport->direction == 2 && dir == 3) {
2922  y++;
2923  } else if (transport->direction == 3 && dir == 2) {
2924  y--;
2925  } else if (transport->direction == 5 && dir == 6) {
2926  x--;
2927  } else if (transport->direction == 6 && dir == 5) {
2928  x++;
2929  } else if (transport->direction == 7 && dir == 8) {
2930  y--;
2931  } else if (transport->direction == 8 && dir == 7) {
2932  y++;
2933  } else if (transport->direction == 8 && dir == 1) {
2934  x++;
2935  }
2936 
2937  update_transport_block(transport, dir);
2938  object_remove(transport);
2939  if (ob_blocked(transport, transport->map, x, y)) {
2940  update_transport_block(transport, transport->direction);
2941  object_insert_in_map_at(transport, transport->map, NULL, 0, x, y);
2942  return 2;
2943  }
2944 
2945  if (x != transport->x || y != transport->y) {
2946 /* assert(scroll_dir != 0);*/
2947 
2948  FOR_INV_PREPARE(transport, pl) {
2949  if (pl->type == PLAYER) {
2950  pl->contr->do_los = 1;
2951  pl->map = transport->map;
2952  pl->x = x;
2953  pl->y = y;
2954  esrv_map_scroll(&pl->contr->socket, freearr_x[scroll_dir], freearr_y[scroll_dir]);
2955  pl->contr->socket.update_look = 1;
2956  pl->contr->socket.look_position = 0;
2957  }
2958  } FOR_INV_FINISH();
2959  }
2960 
2961  object_insert_in_map_at(transport, transport->map, NULL, 0, x, y);
2962  transport->direction = dir;
2963  transport->facing = dir;
2964  animate_object(transport, dir);
2965  captain->direction = dir;
2966  return 1;
2967 }
2968 
2981 static int turn_transport(object *transport, object *captain, int dir) {
2982  assert(transport->type == TRANSPORT);
2983 
2984  if (object_get_value(transport, "turnable_transport") == NULL) {
2985  transport->direction = dir;
2986  transport->facing = dir;
2987  if (QUERY_FLAG(transport, FLAG_ANIMATE)) {
2988  animate_object(transport, dir);
2989  }
2990  captain->direction = dir;
2991  return 0;
2992  }
2993 
2994  if (transport->direction == dir)
2995  return 0;
2996 
2997  if (absdir(transport->direction-dir) > 2)
2998  return turn_one_transport(transport, captain, absdir(transport->direction+1));
2999  else
3000  return turn_one_transport(transport, captain, absdir(transport->direction-1));
3001 }
3002 
3014 int move_player(object *op, int dir) {
3015  object *transport = op->contr->transport; //< Transport player is in
3016 
3017  if (!transport && (op->map == NULL || op->map->in_memory != MAP_IN_MEMORY))
3018  return 0;
3019 
3020  /* Sanity check: make sure dir is valid */
3021  if ((dir < 0) || (dir >= 9)) {
3022  LOG(llevError, "move_player: invalid direction %d\n", dir);
3023  return 0;
3024  }
3025 
3026  if (QUERY_FLAG(op, FLAG_CONFUSED) && dir)
3027  dir = get_randomized_dir(dir);
3028 
3029  op->facing = dir;
3030 
3031  if (transport) {
3032  /* transport->contr is set up for the person in charge of the boat.
3033  * if that isn't this person, he can't steer it, etc
3034  */
3035  if (transport->contr != op->contr)
3036  return 0;
3037 
3038  /* Transport is out of movement. But update dir so it at least
3039  * will point in the same direction if player is running.
3040  */
3041  if (transport->speed_left < 0.0) {
3042  return 0;
3043  }
3044  /* Remove transport speed. Give player just a little speed -
3045  * enough so that they will get an action again quickly.
3046  */
3047  transport->speed_left -= 1.0;
3048  if (op->speed_left < 0.0)
3049  op->speed_left = -0.01;
3050 
3051  int turn = turn_transport(transport, op, dir);
3052  if (turn != 0)
3053  return 0;
3054  } else {
3055  if (op->hide) {
3056  do_hidden_move(op);
3057  }
3058 
3059  /* it is important to change the animation now, as fire or move_player_attack can start a compound animation,
3060  * and leave us with state = 0, which we don't want to change again. */
3061  op->state++; /* player moved, so change animation. */
3062  animate_object(op, op->facing);
3063  }
3064 
3065  if (op->contr->fire_on) {
3066  fire(op, dir);
3067  } else
3068  move_player_attack(op, dir);
3069 
3070  int pick = check_pick(op);
3071 
3072 
3073  /* Add special check for newcs players and fire on - this way, the
3074  * server can handle repeat firing.
3075  */
3076  if (op->contr->fire_on || (op->contr->run_on && pick != 0)) {
3077  op->direction = dir;
3078  } else {
3079  op->direction = 0;
3080  }
3081  return 0;
3082 }
3083 
3096 int handle_newcs_player(object *op) {
3097  if (op->contr->hidden) {
3098  op->invisible = 1000;
3099  /* the socket code flashes the player visible/invisible
3100  * depending on the value if invisible, so we need to
3101  * alternate it here for it to work correctly.
3102  */
3103  if (pticks&2)
3104  op->invisible--;
3105  } else if (op->invisible && !(QUERY_FLAG(op, FLAG_MAKE_INVIS))) {
3106  op->invisible--;
3107  if (!op->invisible) {
3108  make_visible(op);
3110  "Your invisibility spell runs out.");
3111  }
3112  }
3113 
3114  if (QUERY_FLAG(op, FLAG_SCARED)) {
3115  flee_player(op);
3116  /* If player is still scared, that is his action for this tick */
3117  if (QUERY_FLAG(op, FLAG_SCARED)) {
3118  op->speed_left--;
3119  return 0;
3120  }
3121  }
3122 
3123  /* I've been seeing crashes where the golem has been destroyed, but
3124  * the player object still points to the defunct golem. The code that
3125  * destroys the golem looks correct, and it doesn't always happen, so
3126  * put this in a a workaround to clean up the golem pointer.
3127  */
3128  if (op->contr->ranges[range_golem]
3130  op->contr->ranges[range_golem] = NULL;
3131  op->contr->golem_count = 0;
3132  }
3133 
3134  /*
3135  * If the player has been paralyzed, we unmark the flag and give a message to the player
3136  */
3137  if (QUERY_FLAG(op, FLAG_PARALYZED)) {
3139  // TODO: Is this check necessary? We are in player.c, after all.
3140  if (op->type == PLAYER)
3141  {
3143  "You can stretch your stiff joints once more.");
3144  }
3145  }
3146 
3147  if (op->direction && (op->contr->run_on || op->contr->fire_on)) {
3148  /* All move commands take 1 tick, at least for now */
3149  op->speed_left--;
3150 
3151  /* Instead of all the stuff below, let move_player take care
3152  * of it. Also, some of the skill stuff is only put in
3153  * there, as well as the confusion stuff.
3154  */
3155  move_player(op, op->direction);
3156  if (op->speed_left > 0)
3157  return 1;
3158  else
3159  return 0;
3160  }
3161  return 0;
3162 }
3163 
3174 static int save_life(object *op) {
3175  object *tmp;
3176 
3177  if (!QUERY_FLAG(op, FLAG_LIFESAVE))
3178  return 0;
3179 
3181  if (tmp != NULL) {
3182  char name[MAX_BUF];
3183 
3184  query_name(tmp, name, MAX_BUF);
3185  play_sound_map(SOUND_TYPE_ITEM, tmp, 0, "evaporate");
3187  "Your %s vibrates violently, then evaporates.",
3188  name);
3189  object_remove(tmp);
3192  if (op->stats.hp < 0)
3193  op->stats.hp = op->stats.maxhp;
3194  if (op->stats.food < 0)
3195  op->stats.food = MAX_FOOD;
3196  fix_object(op);
3197  return 1;
3198  }
3199  LOG(llevError, "Error: LIFESAVE set without applied object.\n");
3201  enter_player_savebed(op); /* bring him home. */
3202  return 0;
3203 }
3204 
3217 void remove_unpaid_objects(object *op, object *env, int free_items) {
3219  if (QUERY_FLAG(op, FLAG_UNPAID)) {
3220  object_remove(op);
3221  if (free_items)
3223  else
3224  object_insert_in_map_at(op, env->map, NULL, 0, env->x, env->y);
3225  } else if (op->inv)
3226  remove_unpaid_objects(op->inv, env, free_items);
3228 }
3229 
3247 static const char *gravestone_text(object *op, char *buf2, int len) {
3248  char buf[MAX_BUF];
3249  time_t now = time(NULL);
3250 
3251  strncpy(buf2, " R.I.P.\n\n", len);
3252  if (op->type == PLAYER)
3253  snprintf(buf, sizeof(buf), "%s the %s\n", op->name, op->contr->title);
3254  else
3255  snprintf(buf, sizeof(buf), "%s\n", op->name);
3256  strncat(buf2, " ", 20-strlen(buf)/2);
3257  strncat(buf2, buf, len-strlen(buf2)-1);
3258  if (op->type == PLAYER)
3259  snprintf(buf, sizeof(buf), "who was in level %d when killed\n", op->level);
3260  else
3261  snprintf(buf, sizeof(buf), "who was in level %d when died.\n\n", op->level);
3262  strncat(buf2, " ", 20-strlen(buf)/2);
3263  strncat(buf2, buf, len-strlen(buf2)-1);
3264  if (op->type == PLAYER) {
3265  snprintf(buf, sizeof(buf), "by %s.\n\n", op->contr->killer);
3266  strncat(buf2, " ", 21-strlen(buf)/2);
3267  strncat(buf2, buf, len-strlen(buf2)-1);
3268  }
3269  strftime(buf, MAX_BUF, "%b %d %Y\n", localtime(&now));
3270  strncat(buf2, " ", 20-strlen(buf)/2);
3271  strncat(buf2, buf, len-strlen(buf2)-1);
3272  return buf2;
3273 }
3274 
3282 void do_some_living(object *op) {
3283  int last_food = op->stats.food;
3284  int gen_hp, gen_sp, gen_grace;
3285  int rate_hp = 1200;
3286  int rate_sp = 2500;
3287  int rate_grace = 2000;
3288 
3289  if (op->contr->state == ST_PLAYING) {
3290  /* these next three if clauses make it possible to SLOW DOWN
3291  hp/grace/spellpoint regeneration. */
3292  if (op->contr->gen_hp >= 0)
3293  gen_hp = (op->contr->gen_hp+1)*op->stats.maxhp;
3294  else {
3295  gen_hp = op->stats.maxhp;
3296  rate_hp -= rate_hp/2*op->contr->gen_hp;
3297  }
3298  if (op->contr->gen_sp >= 0)
3299  gen_sp = (op->contr->gen_sp+1)*op->stats.maxsp;
3300  else {
3301  gen_sp = op->stats.maxsp;
3302  rate_sp -= rate_sp/2*op->contr->gen_sp;
3303  }
3304  if (op->contr->gen_grace >= 0)
3305  gen_grace = (op->contr->gen_grace+1)*op->stats.maxgrace;
3306  else {
3307  gen_grace = op->stats.maxgrace;
3308  rate_grace -= rate_grace/2*op->contr->gen_grace;
3309  }
3310 
3311  /* Regenerate Spell Points */
3312  if (op->contr->ranges[range_golem] == NULL && --op->last_sp < 0) {
3313  gen_sp = gen_sp*10/MAX(op->contr->gen_sp_armour, 10);
3314  if (op->stats.sp < op->stats.maxsp) {
3315  op->stats.sp++;
3316  /* dms do not consume food */
3317  if (!QUERY_FLAG(op, FLAG_WIZ)) {
3318  op->stats.food--;
3319  if (op->contr->digestion < 0)
3320  op->stats.food += op->contr->digestion;
3321  else if (op->contr->digestion > 0
3322  && random_roll(0, op->contr->digestion, op, PREFER_HIGH))
3323  op->stats.food = last_food;
3324  }
3325  }
3326  op->last_sp = rate_sp/(MAX(gen_sp, 20)+10);
3327  }
3328 
3329  /* Regenerate Grace */
3330  /* I altered this a little - maximum grace is only achieved through prayer -b.t.*/
3331  if (--op->last_grace < 0) {
3332  if (op->stats.grace < op->stats.maxgrace/2)
3333  op->stats.grace++; /* no penalty in food for regaining grace */
3334  op->last_grace = rate_grace/(MAX(gen_grace, 20)+10);
3335  /* wearing stuff doesn't detract from grace generation. */
3336  }
3337 
3338  /* Regenerate Hit Points (unless you are a wraith player) */
3339  if (--op->last_heal < 0 && !is_wraith_pl(op)) {
3340  if (op->stats.hp < op->stats.maxhp) {
3341  op->stats.hp++;
3342  /* dms do not consume food */
3343  if (!QUERY_FLAG(op, FLAG_WIZ)) {
3344  op->stats.food--;
3345  if (op->contr->digestion < 0)
3346  op->stats.food += op->contr->digestion;
3347  else if (op->contr->digestion > 0
3348  && random_roll(0, op->contr->digestion, op, PREFER_HIGH))
3349  op->stats.food = last_food;
3350  }
3351  }
3352  op->last_heal = rate_hp/(MAX(gen_hp, 20)+10);
3353  }
3354 
3355  /* Digestion */
3356  if (--op->last_eat < 0) {
3357  int bonus = MAX(op->contr->digestion, 0);
3358  int penalty = MAX(-op->contr->digestion, 0);
3359  if (op->contr->gen_hp > 0)
3360  op->last_eat = 25*(1+bonus)/(op->contr->gen_hp+penalty+1);
3361  else
3362  op->last_eat = 25*(1+bonus)/(penalty+1);
3363  /* dms do not consume food */
3364  if (!QUERY_FLAG(op, FLAG_WIZ))
3365  op->stats.food--;
3366  }
3367  }
3368 
3369  if (op->contr->state == ST_PLAYING && op->stats.food < 0 && op->stats.hp >= 0) {
3370  if (is_wraith_pl(op))
3371  draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_REMOVE, "You feel a hunger for living flesh.");
3372  /* Only allow eat if not paralyzed. Otherwise our paralyzed player is "moving" to eat.
3373  * Daniel Hawkins 2017-08-23
3374  */
3375  else if (!QUERY_FLAG(op, FLAG_PARALYZED)){
3376  object *flesh = NULL;
3377 
3378  FOR_INV_PREPARE(op, tmp) {
3379  if (!QUERY_FLAG(tmp, FLAG_UNPAID)) {
3380  if (tmp->type == FOOD || tmp->type == DRINK || tmp->type == POISON) {
3382  "You blindly grab for a bite of food.");
3383  apply_manual(op, tmp, 0);
3384  if (op->stats.food >= 0 || op->stats.hp < 0)
3385  break;
3386  } else if (tmp->type == FLESH)
3387  flesh = tmp;
3388  } /* End if paid for object */
3389  } FOR_INV_FINISH(); /* end of for loop */
3390  /* If player is still starving, it means they don't have any food, so
3391  * eat flesh instead.
3392  */
3393  if (op->stats.food < 0 && op->stats.hp >= 0 && flesh) {
3395  "You blindly grab for a bite of food.");
3396  apply_manual(op, flesh, 0);
3397  }
3398  } /* end not wraith and not paralyzed */
3399  else { // Print a message for when the player is starving and paralyzed
3400  draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_REMOVE, "Your stomach rumbles, but you can't reach your food.");
3401  } /* end not wraith and is paralyzed */
3402  } /* end if player is starving */
3403 
3404  if (op->stats.food < 0 && op->stats.hp > 0){
3405  // We know food < 0, so we want the absolute value of food
3406  int32_t adjust_by = MIN(-(op->stats.food), op->stats.hp);
3407  op->stats.food += adjust_by;
3408  op->stats.hp -= adjust_by;
3409  }
3410 
3411  if (!op->contr->state && !QUERY_FLAG(op, FLAG_WIZ) && (op->stats.hp < 0 || op->stats.food < 0))
3412  kill_player(op, NULL);
3413 }
3414 
3421 static void loot_object(object *op) {
3422  object *tmp2;
3423 
3424  if (op->container) { /* close open sack first */
3425  apply_container(op, op->container);
3426  }
3427 
3428  FOR_INV_PREPARE(op, tmp) {
3429  if (tmp->invisible)
3430  continue;
3431  object_remove(tmp);
3432  tmp->x = op->x,
3433  tmp->y = op->y;
3434  if (tmp->type == CONTAINER) { /* empty container to ground */
3435  loot_object(tmp);
3436  }
3437  if (!QUERY_FLAG(tmp, FLAG_UNIQUE)
3438  && (QUERY_FLAG(tmp, FLAG_STARTEQUIP) || QUERY_FLAG(tmp, FLAG_NO_DROP) || !(RANDOM()%3))) {
3439  if (tmp->nrof > 1) {
3440  tmp2 = object_split(tmp, 1+RANDOM()%(tmp->nrof-1), NULL, 0);
3442  object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
3443  } else
3445  } else
3446  object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
3447  } FOR_INV_FINISH();
3448 }
3449 
3461 void kill_player(object *op, const object *killer) {
3462  char buf[MAX_BUF];
3463  int x, y;
3464  archetype *at;
3465  object *tmp;
3466  archetype *trophy = NULL;
3467 
3468  /* Don't die if the player's life can be saved. */
3469  if (save_life(op)) {
3470  return;
3471  }
3472 
3473  /* If player dies on BATTLEGROUND, no stat/exp loss! For Combat-Arenas
3474  * in cities ONLY!!! It is very important that this doesn't get abused.
3475  * Look at op_on_battleground() for more info --AndreasV
3476  */
3477  if (op_on_battleground(op, &x, &y, &trophy)) {
3478  assert(trophy != NULL);
3480  "You have been defeated in combat!\n"
3481  "Local medics have saved your life...");
3482 
3483  /* restore player */
3484  at = find_archetype("poisoning");
3485  tmp = arch_present_in_ob(at, op);
3486  if (tmp) {
3487  object_remove(tmp);
3490  "Your body feels cleansed");
3491  }
3492 
3493  at = find_archetype("confusion");
3494  tmp = arch_present_in_ob(at, op);
3495  if (tmp) {
3496  object_remove(tmp);
3499  "Your mind feels clearer");
3500  }
3501 
3502  cure_disease(op, NULL, NULL); /* remove any disease */
3503  op->stats.hp = op->stats.maxhp;
3504  if (op->stats.food <= 0)
3505  op->stats.food = MAX_FOOD;
3506 
3507  /* create a bodypart-trophy to make the winner happy */
3508  tmp = arch_to_object(trophy);
3509  if (tmp != NULL) {
3510  snprintf(buf, sizeof(buf), "%s's %s", op->name, tmp->name);
3511  tmp->name = add_string(buf);
3512 
3513  snprintf(buf, sizeof(buf),
3514  "This %s was %s %s the %s, who was defeated at level %d by %s.\n",
3515  tmp->name, tmp->type == FLESH ? "cut off" : "taken from",
3516  op->name, op->contr->title,
3517  (int)(op->level), op->contr->killer);
3518 
3519  object_set_msg(tmp, buf);
3520  tmp->type = 0;
3521  tmp->value = 0;
3522  tmp->material = 0;
3523  tmp->materialname = NULL;
3524  object_insert_in_map_at(tmp, op->map, op, 0, op->x, op->y);
3525  }
3526 
3527  /* teleport defeated player to new destination*/
3528  transfer_ob(op, x, y, 0, NULL);
3529  op->contr->braced = 0;
3530  return;
3531  }
3532 
3533  /* Lauwenmark: Handle for plugin death event */
3534  if (execute_event(op, EVENT_DEATH, NULL, NULL, NULL, SCRIPT_FIX_ALL) != 0)
3535  return;
3536 
3537  /* Lauwenmark: Handle for the global death event */
3539  if (op->stats.food < 0) {
3540  snprintf(buf, sizeof(buf), "%s starved to death.", op->name);
3541  strcpy(op->contr->killer, "starvation");
3542  } else {
3543  snprintf(buf, sizeof(buf), "%s died.", op->name);
3544  }
3545  play_sound_player_only(op->contr, SOUND_TYPE_LIVING, op, 0, "death");
3546 
3547  if (settings.not_permadeth == TRUE) {
3549  } else {
3551  }
3552 }
3553 
3562 static void kill_player_not_permadeath(object *op) {
3563  int num_stats_lose;
3564  int will_kill_again;
3565  int lost_a_stat;
3566  int z;
3567  object *tmp;
3568  char buf[MAX_BUF];
3569  archetype *at;
3570 
3571  /* Basically two ways to go - remove a stat permanently, or just
3572  * make it depletion. This bunch of code deals with that aspect
3573  * of death.
3574  */
3576  /* If stat loss is permanent, lose one stat only. */
3577  /* Lower level chars don't lose as many stats because they suffer
3578  more if they do. */
3579  /* Higher level characters can afford things such as potions of
3580  restoration, or better, stat potions. So we slug them that
3581  little bit harder. */
3582  /* GD */
3584  num_stats_lose = 1;
3585  else
3586  num_stats_lose = 1+op->level/BALSL_NUMBER_LOSSES_RATIO;
3587  } else {
3588  num_stats_lose = 1;
3589  }
3590  lost_a_stat = 0;
3591 
3592  for (z = 0; z < num_stats_lose; z++) {
3594  int i;
3595 
3596  /* Pick a random stat and take a point off it. Tell the player
3597  * what he lost.
3598  */
3599  i = RANDOM()%7;
3600  change_attr_value(&(op->stats), i, -1);
3602  change_attr_value(&(op->contr->orig_stats), i, -1);
3604  draw_ext_info(NDI_UNIQUE, 0, op,
3606  lose_msg[i]);
3607  lost_a_stat = 1;
3608  } else {
3609  /* deplete a stat */
3611  object *dep;
3612  int lose_this_stat;
3613  int i;
3614 
3615  i = RANDOM()%7;
3616  dep = arch_present_in_ob(deparch, op);
3617  if (!dep) {
3618  dep = arch_to_object(deparch);
3619  object_insert_in_ob(dep, op);
3620  }
3621  lose_this_stat = 1;
3623  int this_stat;
3624 
3625  /* GD */
3626  /* Get the stat that we're about to deplete. */
3627  this_stat = get_attr_value(&(dep->stats), i);
3628  if (this_stat < 0) {
3629  int loss_chance = 1+op->level/BALSL_LOSS_CHANCE_RATIO;
3630  int keep_chance = this_stat*this_stat;
3631  /* Yes, I am paranoid. Sue me. */
3632  if (keep_chance < 1)
3633  keep_chance = 1;
3634 
3635  /* There is a maximum depletion total per level. */
3636  if (this_stat < -1-op->level/BALSL_MAX_LOSS_RATIO) {
3637  lose_this_stat = 0;
3638  /* Take loss chance vs keep chance to see if we
3639  retain the stat. */
3640  } else {
3641  if (random_roll(0, loss_chance+keep_chance-1, op, PREFER_LOW) < keep_chance)
3642  lose_this_stat = 0;
3643  /* LOG(llevDebug, "Determining stat loss. Stat: %d Keep: %d Lose: %d Result: %s.\n", this_stat, keep_chance, loss_chance, lose_this_stat ? "LOSE" : "KEEP"); */
3644  }
3645  }
3646  }
3647 
3648  if (lose_this_stat) {
3649  int this_stat;
3650 
3651  this_stat = get_attr_value(&(dep->stats), i);
3652  /* We could try to do something clever like find another
3653  * stat to reduce if this fails. But chances are, if
3654  * stats have been depleted to -50, all are pretty low
3655  * and should be roughly the same, so it shouldn't make a
3656  * difference.
3657  */
3658  if (this_stat >= -50) {
3659  change_attr_value(&(dep->stats), i, -1);
3660  SET_FLAG(dep, FLAG_APPLIED);
3662  drain_msg[i]);
3663  fix_object(op);
3664  lost_a_stat = 1;
3665  }
3666  }
3667  }
3668  }
3669  /* If no stat lost, tell the player. */
3670  if (!lost_a_stat) {
3671  /* determine_god() seems to not work sometimes... why is this? Should I be using something else? GD */
3672  const char *god = determine_god(op);
3673 
3674  if (god && (strcmp(god, "none")))
3677  "For a brief moment you feel the holy presence of %s protecting you",
3678  god);
3679  else
3680  draw_ext_info(NDI_UNIQUE, 0, op,
3682  "For a brief moment you feel a holy presence protecting you.");
3683  }
3684 
3685  /* Put a gravestone up where the character 'almost' died. List the
3686  * exp loss on the stone.
3687  */
3688  tmp = arch_to_object(find_archetype("gravestone"));
3689  snprintf(buf, sizeof(buf), "%s's gravestone", op->name);
3690  FREE_AND_COPY(tmp->name, buf);
3691  snprintf(buf, sizeof(buf), "%s's gravestones", op->name);
3692  FREE_AND_COPY(tmp->name_pl, buf);
3693  snprintf(buf, sizeof(buf), "RIP\nHere rests the hero %s the %s,\n"
3694  "who was killed\n"
3695  "by %s.\n",
3696  op->name, op->contr->title,
3697  op->contr->killer);
3698  object_set_msg(tmp, buf);
3699  object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
3700 
3701  /* restore player: remove any poisoning, disease and confusion the
3702  * character may be suffering.*/
3703  at = find_archetype("poisoning");
3704  tmp = arch_present_in_ob(at, op);
3705  if (tmp) {
3706  object_remove(tmp);
3709  "Your body feels cleansed");
3710  }
3711 
3712  at = find_archetype("confusion");
3713  tmp = arch_present_in_ob(at, op);
3714  if (tmp) {
3715  object_remove(tmp);
3718  "Your mind feels clearer");
3719  }
3720  cure_disease(op, NULL, NULL); /* remove any disease */
3721 
3722  /* Subtract the experience points, if we died cause of food, give
3723  * us food, and reset HP's...
3724  */
3726  if (op->stats.food < 100)
3727  op->stats.food = 900;
3728  op->stats.hp = op->stats.maxhp;
3729  op->stats.sp = MAX(op->stats.sp, op->stats.maxsp);
3730  op->stats.grace = MAX(op->stats.grace, op->stats.maxgrace);
3731 
3732  /* Check to see if the player is in a shop. IF so, then check to see if
3733  * the player has any unpaid items. If so, remove them and put them back
3734  * in the map.
3735  *
3736  * If they are not in a shop, just free the unpaid items instead of
3737  * putting them back on map.
3738  */
3739  if (shop_contains(op))
3740  remove_unpaid_objects(op->inv, op, 0);
3741  else
3742  remove_unpaid_objects(op->inv, op, 1);
3743 
3744  /* Move player to his current respawn-position (usually last savebed) */
3746 
3747  /* Save the player before inserting the force to reduce chance of abuse. */
3748  op->contr->braced = 0;
3749  save_player(op, 1);
3750 
3751  /* it is possible that the player has blown something up
3752  * at his savebed location, and that can have long lasting
3753  * spell effects. So first see if there is a spell effect
3754  * on the space that might harm the player.
3755  */
3756  will_kill_again = 0;
3757  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp)
3758  if (tmp->type == SPELL_EFFECT)
3759  will_kill_again |= tmp->attacktype;
3760  FOR_MAP_FINISH();
3761  if (will_kill_again) {
3762  object *force;
3763  int at;
3764 
3765  force = create_archetype(FORCE_NAME);
3766  /* 50 ticks should be enough time for the spell to abate */
3767  force->speed = 0.1;
3768  force->speed_left = -5.0;
3769  SET_FLAG(force, FLAG_APPLIED);
3770  for (at = 0; at < NROFATTACKS; at++) {
3771  if (will_kill_again&(1<<at))
3772  force->resist[at] = 100;
3773  }
3774  object_insert_in_ob(force, op);
3775  fix_object(op);
3776  }
3777 
3778  /* Tell the player they have died */
3780  "YOU HAVE DIED.");
3781 }
3782 
3789 static void kill_player_permadeath(object *op) {
3790  char buf[MAX_BUF];
3791  char ac_buf[MAX_BUF];
3792  int x, y;
3793  mapstruct *map;
3794  object *tmp;
3795 
3796  /* save the map location for corpse, gravestone*/
3797  x = op->x;
3798  y = op->y;
3799  map = op->map;
3800 
3801  party_leave(op);
3802  if (settings.set_title == TRUE)
3803  player_set_own_title(op->contr, "");
3804 
3805  /* buf should be the kill message */
3807  buf);
3808  hiscore_check(op, 0);
3809  if (op->contr->ranges[range_golem] != NULL) {
3813  op->contr->ranges[range_golem] = NULL;
3814  op->contr->golem_count = 0;
3815  }
3816  loot_object(op); /* Remove some of the items for good */
3817  object_remove(op);
3818  op->direction = 0;
3819 
3820  if (!QUERY_FLAG(op, FLAG_WAS_WIZ) && op->stats.exp) {
3821  if (settings.resurrection == TRUE) {
3822  /* save playerfile sans equipment when player dies
3823  * -then save it as player.pl.dead so that future resurrection
3824  * -type spells will work on them nicely
3825  */
3826  op->stats.hp = op->stats.maxhp;
3827  op->stats.food = MAX_FOOD;
3828 
3829  /* set the location of where the person will reappear when */
3830  /* maybe resurrection code should fix map also */
3832  sizeof(op->contr->maplevel));
3833  if (op->map != NULL)
3834  op->map = NULL;
3835  op->x = settings.emergency_x;
3836  op->y = settings.emergency_y;
3837  save_player(op, 0);
3838  op->map = map;
3839  /* please see resurrection.c: peterm */
3840  dead_player(op);
3841  } else {
3842  delete_character(op->name);
3843  }
3844  }
3845  play_again(op);
3846 
3847  /* peterm: added to create a corpse at deathsite. */
3848  tmp = arch_to_object(find_archetype("corpse_pl"));
3849  snprintf(buf, sizeof(buf), "%s", op->name);
3850  FREE_AND_COPY(tmp->name, buf);
3851  FREE_AND_COPY(tmp->name_pl, buf);
3852  tmp->level = op->level;
3853  object_set_msg(tmp, gravestone_text(op, buf, sizeof(buf)));
3854  SET_FLAG(tmp, FLAG_UNIQUE);
3855  /*
3856  * Put the account name under slaying.
3857  * Does not seem to cause weird effects, but more testing may ensure this.
3858  */
3859  snprintf(ac_buf, sizeof(ac_buf), "%s", op->contr->socket.account_name);
3860  FREE_AND_COPY(tmp->slaying, ac_buf);
3861  object_insert_in_map_at(tmp, map, NULL, 0, x, y);
3862 }
3863 
3871 void fix_weight(void) {
3872  player *pl;
3873 
3874  for (pl = first_player; pl != NULL; pl = pl->next) {
3875  int old = pl->ob->carrying, sum = object_sum_weight(pl->ob);
3876 
3877  if (old == sum)
3878  continue;
3879  fix_object(pl->ob);
3880  LOG(llevDebug, "Fixed inventory in %s (%d -> %d)\n", pl->ob->name, old, sum);
3881  }
3882 }
3883 
3887 void fix_luck(void) {
3888  player *pl;
3889 
3890  for (pl = first_player; pl != NULL; pl = pl->next)
3891  if (!pl->ob->contr->state)
3892  change_luck(pl->ob, 0);
3893 }
3894 
3895 
3908 void cast_dust(object *op, object *throw_ob, int dir) {
3909  object *skop, *spob;
3910 
3911  skop = find_skill_by_name(op, throw_ob->skill);
3912 
3913  /* casting POTION 'dusts' is really a use_magic_item skill */
3914  if (op->type == PLAYER && throw_ob->type == POTION && !skop) {
3915  LOG(llevError, "Player %s lacks critical skill use_magic_item!\n", op->name);
3916  return;
3917  }
3918  spob = throw_ob->inv;
3919  if (op->type == PLAYER && spob)
3921  "You cast %s.",
3922  spob->name);
3923 
3924  cast_spell(op, throw_ob, dir, spob, NULL);
3925 
3926  if (!QUERY_FLAG(throw_ob, FLAG_REMOVED))
3927  object_remove(throw_ob);
3928  object_free_drop_inventory(throw_ob);
3929 }
3930 
3937 void make_visible(object *op) {
3938  op->hide = 0;
3939  op->invisible = 0;
3940  if (op->type == PLAYER) {
3941  op->contr->tmp_invis = 0;
3942  if (op->contr->invis_race)
3944  }
3946 }
3947 
3957 int is_true_undead(object *op) {
3958  if (QUERY_FLAG(&op->arch->clone, FLAG_UNDEAD))
3959  return 1;
3960 
3961  if (op->type == PLAYER)
3962  FOR_INV_PREPARE(op, tmp) {
3963  if (tmp->type == 44 && tmp->stats.Wis)
3964  if (QUERY_FLAG(tmp, FLAG_UNDEAD))
3965  return 1;
3966  } FOR_INV_FINISH();
3967  return 0;
3968 }
3969 
3980 int hideability(object *ob) {
3981  int i, level = 0, mflag;
3982  int16_t x, y;
3983 
3984  if (!ob || !ob->map)
3985  return 0;
3986 
3987  /* so, on normal lighted maps, its hard to hide */
3988  level = ob->map->darkness-2;
3989 
3990  /* this also picks up whether the object is glowing.
3991  * If you carry a light on a non-dark map, its not
3992  * as bad as carrying a light on a pitch dark map
3993  */
3994  if (has_carried_lights(ob))
3995  level = -(10+(2*ob->map->darkness));
3996 
3997  /* scan through all nearby squares for terrain to hide in */
3998  for (i = 0, x = ob->x, y = ob->y; i < 9; i++, x = ob->x+freearr_x[i], y = ob->y+freearr_y[i]) {
3999  mflag = get_map_flags(ob->map, NULL, x, y, NULL, NULL);
4000  if (mflag&P_OUT_OF_MAP) {
4001  continue;
4002  }
4003  if (mflag&P_BLOCKSVIEW) /* something to hide near! */
4004  level += 2;
4005  else /* open terrain! */
4006  level -= 1;
4007  }
4008 
4009  return level;
4010 }
4011 
4021 void do_hidden_move(object *op) {
4022  int hide = 0, num = random_roll(0, 19, op, PREFER_LOW);
4023  object *skop;
4024 
4025  if (!op || !op->map)
4026  return;
4027 
4029 
4030  /* its *extremely *hard to run and sneak/hide at the same time! */
4031  if (op->type == PLAYER && op->contr->run_on) {
4032  if (!skop || num >= skop->level) {
4034  "You ran too much! You are no longer hidden!");
4035  make_visible(op);
4036  return;
4037  } else
4038  num += 20;
4039  }
4040  num += op->map->difficulty;
4041  hide = hideability(op); /* modify by terrain hidden level */
4042  num -= hide;
4043  if ((op->type == PLAYER && hide < -10)
4044  || ((op->invisible -= num) <= 0)) {
4045  make_visible(op);
4046  if (op->type == PLAYER)
4048  "You moved out of hiding! You are visible!");
4049  } else if (op->type == PLAYER && skop) {
4050  change_exp(op, calc_skill_exp(op, NULL, skop), skop->skill, 0);
4051  }
4052 }
4053 
4062 int stand_near_hostile(object *who) {
4063  int i, friendly = 0, player = 0, mflags;
4064  mapstruct *m;
4065  int16_t x, y;
4066 
4067  if (!who)
4068  return 0;
4069 
4070  if (who->type == PLAYER)
4071  player = 1;
4072  else
4073  friendly = QUERY_FLAG(who, FLAG_FRIENDLY);
4074 
4075  /* search adjacent squares */
4076  for (i = 1; i < 9; i++) {
4077  x = who->x+freearr_x[i];
4078  y = who->y+freearr_y[i];
4079  m = who->map;
4080  mflags = get_map_flags(m, &m, x, y, &x, &y);
4081  /* space must be blocked if there is a monster. If not
4082  * blocked, don't need to check this space.
4083  */
4084  if (mflags&P_OUT_OF_MAP)
4085  continue;
4086  if (OB_TYPE_MOVE_BLOCK(who, GET_MAP_MOVE_BLOCK(m, x, y)))
4087  continue;
4088 
4089  FOR_MAP_PREPARE(m, x, y, tmp) {
4090  if ((player || friendly)
4091  && QUERY_FLAG(tmp, FLAG_MONSTER)
4092  && !QUERY_FLAG(tmp, FLAG_UNAGGRESSIVE))
4093  return 1;
4094  else if (tmp->type == PLAYER) {
4095  /*don't let a hidden DM prevent you from hiding*/
4096  if (!QUERY_FLAG(tmp, FLAG_WIZ) || tmp->contr->hidden == 0)
4097  return 1;
4098  }
4099  } FOR_MAP_FINISH();
4100  }
4101  return 0;
4102 }
4103 
4130 int player_can_view(object *pl, object *op) {
4131  rv_vector rv;
4132  int dx, dy;
4133 
4134  if (pl->type != PLAYER) {
4135  LOG(llevError, "player_can_view() called for non-player object\n");
4136  return -1;
4137  }
4138  if (!pl || !op)
4139  return 0;
4140 
4141  op = HEAD(op);
4142  if (!get_rangevector(pl, op, &rv, 0x1))
4143  return 0;
4144 
4145  /* starting with the 'head' part, lets loop
4146  * through the object and find if it has any
4147  * part that is in the los array but isnt on
4148  * a blocked los square.
4149  * we use the archetype to figure out offsets.
4150  */
4151  while (op) {
4152  dx = rv.distance_x+op->arch->clone.x;
4153  dy = rv.distance_y+op->arch->clone.y;
4154 
4155  /* only the viewable area the player sees is updated by LOS
4156  * code, so we need to restrict ourselves to that range of values
4157  * for any meaningful values.
4158  */
4159  if (FABS(dx) <= (pl->contr->socket.mapx/2)
4160  && FABS(dy) <= (pl->contr->socket.mapy/2)
4161  && !pl->contr->blocked_los[dx+(pl->contr->socket.mapx/2)][dy+(pl->contr->socket.mapy/2)])
4162  return 1;
4163  op = op->more;
4164  }
4165  return 0;
4166 }
4167 
4181 static int action_makes_visible(object *op) {
4182  if (op->invisible && QUERY_FLAG(op, FLAG_ALIVE)) {
4183  if (QUERY_FLAG(op, FLAG_MAKE_INVIS))
4184  return 0;
4185 
4186  if (op->contr && op->contr->tmp_invis == 0)
4187  return 0;
4188 
4189  /* If monsters, they should become visible */
4190  if (op->hide || !op->contr || (op->contr && op->contr->tmp_invis)) {
4192  "You become %s!",
4193  op->hide ? "unhidden" : "visible");
4194  return 1;
4195  }
4196  }
4197  return 0;
4198 }
4199 
4228 int op_on_battleground(object *op, int *x, int *y, archetype **trophy) {
4229  FOR_BELOW_PREPARE(op, tmp) {
4230  if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
4231  if (QUERY_FLAG(tmp, FLAG_NO_PICK)
4232  && strcmp(tmp->name, "battleground") == 0
4233  && tmp->type == BATTLEGROUND
4234  && EXIT_X(tmp)
4235  && EXIT_Y(tmp)) {
4236  /*before we assign the exit, check if this is a teambattle*/
4237  if (EXIT_ALT_X(tmp) && EXIT_ALT_Y(tmp) && EXIT_PATH(tmp)) {
4238  object *invtmp;
4239 
4240  invtmp = object_find_by_type_and_slaying(op, FORCE, EXIT_PATH(tmp));
4241  if (invtmp != NULL) {
4242  if (x != NULL && y != NULL)
4243  *x = EXIT_ALT_X(tmp),
4244  *y = EXIT_ALT_Y(tmp);
4245  return 1;
4246  }
4247  }
4248  if (x != NULL && y != NULL)
4249  *x = EXIT_X(tmp),
4250  *y = EXIT_Y(tmp);
4251 
4252  /* If 'other_arch' is not specified, give a finger. */
4253  if (trophy != NULL) {
4254  if (tmp->other_arch) {
4255  *trophy = tmp->other_arch;
4256  } else {
4257  *trophy = find_archetype("finger");
4258  }
4259  }
4260  return 1;
4261  }
4262  }
4263  } FOR_BELOW_FINISH();
4264  /* If we got here, did not find a battleground */
4265  return 0;
4266 }
4267 
4278 void dragon_ability_gain(object *who, int atnr, int level) {
4279  treasurelist *trlist = NULL; /* treasurelist */
4280  treasure *tr; /* treasure */
4281  object *tmp, *skop; /* tmp. object */
4282  object *item; /* treasure object */
4283  char buf[MAX_BUF]; /* tmp. string buffer */
4284  int i = 0, j = 0;
4285 
4286  /* get the appropriate treasurelist */
4287  if (atnr == ATNR_FIRE)
4288  trlist = find_treasurelist("dragon_ability_fire");
4289  else if (atnr == ATNR_COLD)
4290  trlist = find_treasurelist("dragon_ability_cold");
4291  else if (atnr == ATNR_ELECTRICITY)
4292  trlist = find_treasurelist("dragon_ability_elec");
4293  else if (atnr == ATNR_POISON)
4294  trlist = find_treasurelist("dragon_ability_poison");
4295 
4296  if (trlist == NULL || who->type != PLAYER)
4297  return;
4298 
4299  // tr->magic is being used to define what level of the metabolism the ability is gained at.
4300  for (tr = trlist->items; tr != NULL && tr->magic != level; tr = tr->next)
4301  ;
4302  if (tr == NULL || tr->item == NULL) {
4303  /* LOG(llevDebug, "-> no more treasure for %s\n", change_resist_msg[atnr]); */
4304  return;
4305  }
4306 
4307  /* everything seems okay - now bring on the gift: */
4308  item = &(tr->item->clone);
4309 
4310  if (item->type == SPELL) {
4311  if (check_spell_known(who, item->name))
4312  return;
4313 
4316  "You gained the ability of %s",
4317  item->name);
4318  do_learn_spell(who, item, 0);
4319  return;
4320  }
4321 
4322  /* grant direct spell */
4323  if (item->type == SPELLBOOK) {
4324  if (!item->inv) {
4325  LOG(llevDebug, "dragon_ability_gain: Broken spellbook %s\n", item->name);
4326  return;
4327  }
4328  if (check_spell_known(who, item->inv->name))
4329  return;
4330  if (item->invisible) {
4333  "You gained the ability of %s",
4334  item->inv->name);
4335  do_learn_spell(who, item->inv, 0);
4336  return;
4337  }
4338  } else if (item->type == SKILL_TOOL && item->invisible) {
4339  if (item->subtype == SK_CLAWING && (skop = find_skill_by_name(who, item->skill)) != NULL) {
4340  /* should this perhaps be (skop->attackyp&item->attacktype) != item->attacktype ...
4341  * in this way, if the player is missing any of the attacktypes, he gets
4342  * them. As it is now, if the player has any that match the granted skill,
4343  * but not all of them, he gets nothing.
4344  */
4345  if (!(skop->attacktype&item->attacktype)) {
4346  /* Give new attacktype */
4347  skop->attacktype |= item->attacktype;
4348 
4349  /* always add physical if there's none */
4350  skop->attacktype |= AT_PHYSICAL;
4351 
4352  if (item->msg != NULL)
4355  item->msg);
4356 
4357  /* Give player new face */
4358  if (item->animation_id) {
4359  who->face = skop->face;
4360  who->animation_id = item->animation_id;
4361  who->anim_speed = item->anim_speed;
4362  who->last_anim = 0;
4363  who->state = 0;
4364  animate_object(who, who->direction);
4365  }
4366  }
4367  }
4368  } else if (item->type == FORCE) {
4369  /* forces in the treasurelist can alter the player's stats */
4370  object *skin;
4371 
4372  /* first get the dragon skin force */
4373  skin = object_find_by_arch_name(who, "dragon_skin_force");
4374  if (skin == NULL)
4375  return;
4376 
4377  /* adding new spellpath attunements */
4378  if (item->path_attuned > 0 && !(skin->path_attuned&item->path_attuned)) {
4379  skin->path_attuned |= item->path_attuned; /* add attunement to skin */
4380 
4381  /* print message */
4382  snprintf(buf, sizeof(buf), "You feel attuned to ");
4383  for (i = 0, j = 0; i < NRSPELLPATHS; i++) {
4384  if (item->path_attuned&(1<<i)) {
4385  if (j)
4386  strcat(buf, " and ");
4387  else
4388  j = 1;
4389  strcat(buf, spellpathnames[i]);
4390  }
4391  }
4392  strcat(buf, ".");
4395  buf);
4396  }
4397 
4398  /* evtl. adding flags: */
4399  if (QUERY_FLAG(item, FLAG_XRAYS))
4400  SET_FLAG(skin, FLAG_XRAYS);
4401  if (QUERY_FLAG(item, FLAG_STEALTH))
4402  SET_FLAG(skin, FLAG_STEALTH);
4403  if (QUERY_FLAG(item, FLAG_SEE_IN_DARK))
4404  SET_FLAG(skin, FLAG_SEE_IN_DARK);
4405 
4406  /* print message if there is one */
4407  if (item->msg != NULL)
4410  item->msg);
4411  } else {
4412  /* generate misc. treasure */
4413  char name[HUGE_BUF];
4414 
4415  tmp = arch_to_object(tr->item);
4416  query_short_name(tmp, name, HUGE_BUF);
4419  "You gained %s",
4420  name);
4421  tmp = object_insert_in_ob(tmp, who);
4422  if (who->type == PLAYER)
4423  esrv_send_item(who, tmp);
4424  }
4425 }
4426 
4436 void player_unready_range_ob(player *pl, object *ob) {
4437  rangetype i;
4438 
4439  for (i = 0; i < range_size; i++) {
4440  if (pl->ranges[i] == ob) {
4441  pl->ranges[i] = NULL;
4442  if (pl->shoottype == i) {
4443  pl->shoottype = range_none;
4444  }
4445  }
4446  }
4447 }
4448 
4455 
4456  assert(pl);
4457  assert(state >= ST_PLAYING && state <= ST_CHANGE_PASSWORD_CONFIRM);
4458  pl->state = state;
4459 }
float weapon_speed_left
Definition: object.h:331
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:315
static int player_fire_bow(object *op, int dir)
Definition: player.c:2371
uint8_t login_method
Definition: newserver.h:128
void spring_trap(object *trap, object *victim)
Definition: rune.c:205
static void set_player_socket(player *p, socket_struct *ns)
Definition: player.c:417
char path[HUGE_BUF]
Definition: map.h:365
uint8_t not_permadeth
Definition: global.h:258
int player_can_view(object *pl, object *op)
Definition: player.c:4130
static int player_attack_door(object *op, object *door)
Definition: player.c:2612
void set_attr_value(living *stats, int attr, int8_t value)
Definition: living.c:218
const char * rules
Definition: global.h:275
#define MSG_TYPE_MISC
Definition: newclient.h:389
#define FLAG_NO_DROP
Definition: define.h:289
int8_t Int
Definition: living.h:36
Definition: player.h:92
#define FLAG_SEE_IN_DARK
Definition: define.h:338
int apply_manual(object *op, object *tmp, int aflag)
Definition: apply.c:510
const char * determine_god(object *op)
Definition: gods.c:106
archetype * find_archetype(const char *name)
Definition: arch.c:692
#define FLAG_DAMNED
Definition: define.h:318
#define FLAG_IS_FLOOR
Definition: define.h:303
#define FLAG_UNPAID
Definition: define.h:236
int8_t ac
Definition: living.h:38
#define MOVE_WALK
Definition: define.h:407
#define UP_OBJ_FACE
Definition: object.h:517
#define MSG_TYPE_ADMIN_LOGIN
Definition: newclient.h:478
int swap_first
Definition: player.h:147
#define MSG_TYPE_ATTRIBUTE_GOD
Definition: newclient.h:570
#define ST_GET_PARTY_PASSWORD
Definition: define.h:587
MoveType move_type
Definition: object.h:424
#define EVENT_REMOVE
Definition: plugin.h:93
#define MSG_TYPE_ITEM
Definition: newclient.h:388
uint8_t stat_loss_on_death
Definition: global.h:252
int16_t gen_hp
Definition: player.h:113
Definition: object.h:127
player * find_player_options(const char *plname, int options, const mapstruct *map)
Definition: player.c:65
#define DETOUR_AMOUNT
Definition: player.c:581
MoveType move_on
Definition: object.h:427
Definition: object.h:185
int64_t calc_skill_exp(const object *who, const object *op, const object *skill)
Definition: skill_util.c:657
bool shop_contains(object *ob)
Definition: shop.c:1261
uint8_t max_stat
Definition: global.h:321
void enter_exit(object *op, object *exit_ob)
Definition: server.c:716
Definition: map.h:380
Definition: player.h:39
void leave(player *pl, int draw_exit)
Definition: server.c:1197
static void swap_stat(object *op, int swap_second)
Definition: player.c:1126
static void loot_object(object *op)
Definition: player.c:3421
object * check_spell_known(object *op, const char *name)
Definition: spell_util.c:435
Definition: object.h:442
const char * race
Definition: object.h:318
void remove_door(object *op)
Definition: time.c:37
int account_remove_player(const char *account_name, const char *player_name)
Definition: account.c:497
char * account_name
Definition: newserver.h:126
#define PU_CONTAINER
Definition: define.h:143
void SockList_Init(SockList *sl)
Definition: lowlevel.c:48
void esrv_send_item(object *pl, object *op)
Definition: main.c:339
#define SET_FLAG(xyz, p)
Definition: define.h:223
sstring add_refcount(sstring str)
Definition: shstr.c:210
#define EXIT_ALT_Y(xyz)
Definition: define.h:460
#define NDI_ALL
Definition: newclient.h:246
int get_party_password(object *op, partylist *party)
Definition: player.c:996
int16_t bed_x
Definition: player.h:98
uint32_t run_on
Definition: player.h:128
#define PU_DEBUG
Definition: define.h:108
char title[BIG_NAME]
Definition: player.h:165
#define FABS(x)
Definition: define.h:22
int cure_disease(object *sufferer, object *caster, sstring skill)
Definition: disease.c:685
uint32_t braced
Definition: player.h:124
unsigned char uint8_t
Definition: win32.h:161
Definition: object.h:221
static struct Command_Line_Options options[]
Definition: init.c:284
#define MSG_TYPE_ITEM_ADD
Definition: newclient.h:639
#define EVENT_DEATH
Definition: plugin.h:67
void SockList_ResetRead(SockList *sl)
Definition: lowlevel.c:75
void get_name(object *op)
Definition: player.c:854
int get_rangevector_from_mapcoord(const mapstruct *m, int x, int y, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2612
void make_path_to_file(const char *filename)
Definition: porting.c:312
char spellparam[MAX_BUF]
Definition: player.h:100
static void kill_player_not_permadeath(object *op)
Definition: player.c:3562
int get_dam_bonus(int stat)
Definition: living.c:2297
uint16_t animation_id
Definition: object.h:416
#define MSG_TYPE_COMMAND_FAILURE
Definition: newclient.h:511
#define MSG_TYPE_ITEM_INFO
Definition: newclient.h:641
uint16_t material
Definition: object.h:347
#define FLAG_USE_ARMOUR
Definition: define.h:296
void send_news(const object *op)
Definition: player.c:201
int fire_bow(object *op, object *arrow, int dir, int wc_mod, int16_t sx, int16_t sy)
Definition: player.c:2166
#define BALSL_NUMBER_LOSSES_RATIO
Definition: config.h:143
#define strdup_local
Definition: compat.h:25
uint8_t anim_speed
Definition: object.h:417
object * object_find_by_type(const object *who, int type)
Definition: object.c:3921
void drain_wand_charge(object *wand)
Definition: spell_util.c:829
int16_t last_resist[NROFATTACKS]
Definition: player.h:155
Definition: object.h:204
uint16_t look_position
Definition: newserver.h:114
EXTERN objectlink * first_friendly_object
Definition: global.h:123
Definition: object.h:112
struct obj * container
Definition: object.h:291
#define EVENT_LOGIN
Definition: plugin.h:88
uint8_t last_anim
Definition: object.h:418
#define PU_STOP
Definition: define.h:110
Definition: object.h:117
#define FLAG_FRIENDLY
Definition: define.h:246
int push_ob(object *who, int dir, object *pusher)
Definition: move.c:417
Definition: living.h:14
#define PU_SHIELD
Definition: define.h:122
char motd[MAX_BUF]
Definition: global.h:274
void fatal(enum fatal_error err)
Definition: utils.c:597
object * mon
Definition: comet_perf.c:74
#define NDI_BROWN
Definition: newclient.h:233
int op_on_battleground(object *op, int *x, int *y, archetype **trophy)
Definition: player.c:4228
#define MSG_TYPE_SPELL
Definition: newclient.h:387
static int turn_transport(object *transport, object *captain, int dir)
Definition: player.c:2981
#define SOUND_TYPE_GROUND
Definition: newclient.h:311
treasurelist * find_treasurelist(const char *name)
Definition: treasure.c:292
int save_player(object *op, int flag)
Definition: login.c:211
void dragon_ability_gain(object *who, int atnr, int level)
Definition: player.c:4278
uint32_t in_memory
Definition: map.h:345
const char * playerdir
Definition: global.h:244
int16_t maxgrace
Definition: living.h:45
void free_string(sstring str)
Definition: shstr.c:280
#define PU_KEY
Definition: define.h:128
signed long object_sum_weight(object *op)
Definition: object.c:342
#define HUGE_BUF
Definition: define.h:37
#define SET_ANIMATION(ob, newanim)
Definition: global.h:169
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2380
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:342
void enter_player_maplevel(object *op)
Definition: server.c:661
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4264
Definition: object.h:137
struct treasureliststruct * randomitems
Definition: object.h:385
void pick_up(object *op, object *alt)
Definition: c_object.c:446
#define NDI_BLUE
Definition: newclient.h:226
uint32_t pticks
Definition: time.c:45
void query_base_name(const object *op, int plural, char *buf, size_t size)
Definition: item.c:723
#define ST_CHANGE_PASSWORD_CONFIRM
Definition: define.h:590
int8_t levsp[11]
Definition: player.h:168
object clone
Definition: object.h:470
socket_struct socket
Definition: player.h:94
int16_t invisible
Definition: object.h:360
short freearr_x[SIZEOFFREE]
Definition: object.c:65
#define PREFER_LOW
Definition: define.h:602
Definition: living.h:17
#define ST_CONFIRM_PASSWORD
Definition: define.h:586
rangetype shoottype
Definition: player.h:99
Definition: object.h:240
int16_t SP_level_spellpoint_cost(object *caster, object *spell, int flags)
Definition: spell_util.c:277
Definition: object.h:119
Definition: object.h:136
const char * slaying
Definition: object.h:319
#define IS_WEAPON(op)
Definition: define.h:162
void esrv_send_inventory(object *pl, object *op)
Definition: item.c:307
#define FLAG_CONFUSED
Definition: define.h:312
#define FLAG_STEALTH
Definition: define.h:313
static object * pick_arrow_target(object *op, const char *type, int dir)
Definition: player.c:2097
object * ranges[range_size]
Definition: player.h:103
uint8_t subtype
Definition: object.h:339
const char * party_get_password(const partylist *party)
Definition: party.c:263
Definition: object.h:109
void send_account_players(socket_struct *ns)
Definition: request.c:1874
void send_rules(const object *op)
Definition: player.c:165
#define SOUND_TYPE_LIVING
Definition: newclient.h:308
#define FLAG_USE_WEAPON
Definition: define.h:297
uint8_t hide
Definition: object.h:387
int64_t exp
Definition: living.h:47
void fix_luck(void)
Definition: player.c:3887
void play_again(object *op)
Definition: player.c:878
void change_luck(object *op, int value)
Definition: living.c:792
object * object_find_by_type_and_slaying(const object *who, int type, const char *slaying)
Definition: object.c:4067
method_ret ob_process(object *op)
Definition: ob_methods.c:62
void SockList_AddInt(SockList *sl, uint32_t data)
Definition: lowlevel.c:119
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:217
int language
Definition: player.h:201
Definition: object.h:157
#define object_was_destroyed(op, old_tag)
Definition: object.h:68
#define TRUE
Definition: compat.h:10
#define PU_NOTHING
Definition: define.h:106
#define PU_FLESH
Definition: define.h:142
#define PU_FOOD
Definition: define.h:115
object * arch_present_in_ob(const archetype *at, const object *op)
Definition: object.c:3076
void party_leave(object *op)
Definition: party.c:104
Definition: living.h:35
void do_some_living(object *op)
Definition: player.c:3282
void give_initial_items(object *pl, treasurelist *items)
Definition: player.c:762
Definition: object.h:139
int8_t get_attr_value(const living *stats, int attr)
Definition: living.c:313
void remove_friendly_object(object *op)
Definition: friend.c:56
uint32_t path_attuned
Definition: object.h:343
#define MAX(x, y)
Definition: compat.h:20
void object_update(object *op, int action)
Definition: object.c:1260
#define MSG_TYPE_ITEM_REMOVE
Definition: newclient.h:638
void account_char_free(Account_Char *chars)
Definition: account_char.c:334
void dead_player(object *op)
Definition: resurrection.c:294
int16_t sp
Definition: living.h:42
EXTERN char first_map_ext_path[MAX_BUF]
Definition: global.h:152
void strip_endline(char *buf)
Definition: utils.c:341
Definition: object.h:187
int path_to_player(object *mon, object *pl, unsigned mindiff)
Definition: player.c:629
void get_password(object *op)
Definition: player.c:866
#define safe_strncpy
Definition: compat.h:23
int is_true_undead(object *op)
Definition: player.c:3957
#define SCRIPT_FIX_ALL
Definition: global.h:361
Definition: object.h:212
player * find_player_partial_name(const char *plname)
Definition: player.c:109
#define PU_NEWMODE
Definition: define.h:111
uint32_t hidden
Definition: player.h:132
void pets_terminate_all(object *owner)
Definition: pets.c:231
static const char * gravestone_text(object *op, char *buf2, int len)
Definition: player.c:3247
struct obj * enemy
Definition: object.h:381
object * get_nearest_player(object *mon)
Definition: player.c:520
#define ST_ROLL_STAT
Definition: define.h:581
int distance_y
Definition: map.h:383
int absdir(int d)
Definition: object.c:3652
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
const char *const drain_msg[NUM_STATS]
Definition: living.c:139
Definition: object.h:465
#define ST_PLAYING
Definition: define.h:579
#define PU_MISSILEWEAPON
Definition: define.h:130
char savebed_map[MAX_BUF]
Definition: player.h:97
#define NDI_DK_ORANGE
Definition: newclient.h:227
char * host
Definition: newserver.h:100
Definition: object.h:220
#define MSG_TYPE_APPLY
Definition: newclient.h:384
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:791
const char *const spellpathnames[NRSPELLPATHS]
Definition: init.c:122
struct archt * item
Definition: treasure.h:64
uint16_t emergency_y
Definition: global.h:297
static int turn_one_transport(object *transport, object *captain, int dir)
Definition: player.c:2911
#define PU_CLOAK
Definition: define.h:127
int16_t maxsp
Definition: living.h:43
#define MIN(x, y)
Definition: compat.h:17
int8_t Con
Definition: living.h:36
#define FLAG_REMOVED
Definition: define.h:232
int16_t hp
Definition: living.h:40
int check_pick(object *op)
Definition: player.c:1688
#define FLAG_CAN_ROLL
Definition: define.h:254
short freearr_y[SIZEOFFREE]
Definition: object.c:71
partylist * party
Definition: player.h:186
#define FLAG_KNOWN_MAGICAL
Definition: define.h:320
Definition: player.h:32
#define SOUND_TYPE_ITEM
Definition: newclient.h:310
#define NDI_NAVY
Definition: newclient.h:223
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.c:490
uint32_t no_shout
Definition: player.h:133
object * ob
Definition: object.h:443
#define FIND_PLAYER_PARTIAL_NAME
Definition: player.h:212
#define MOVE_ALL
Definition: define.h:413
uint32_t ticks_played
Definition: player.h:203
void pets_control_golem(object *op, int dir)
Definition: pets.c:648
#define MIN_STAT
Definition: define.h:33
#define FLAG_UNDEAD
Definition: define.h:270
void object_set_owner(object *op, object *owner)
Definition: object.c:632
const char * news
Definition: global.h:276
#define FLAG_READY_SKILL
Definition: define.h:334
void apply_changes_to_player(object *pl, object *change, int limit_stats)
Definition: apply.c:1581
Definition: object.h:241
#define MAP_IN_MEMORY
Definition: map.h:130
struct obj * chosen_skill
Definition: object.h:386
uint32_t update_look
Definition: newserver.h:104
object * find_key(object *pl, object *container, object *door)
Definition: player.c:2531
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
void clear_player(player *pl)
Definition: player.c:33
int16_t y
Definition: object.h:326
void esrv_new_player(player *pl, uint32_t weight)
Definition: request.c:856
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1936
int16_t maxhp
Definition: living.h:41
static void fire_misc_object(object *op, int dir)
Definition: player.c:2407
#define MSG_TYPE_ATTRIBUTE_BAD_EFFECT_END
Definition: newclient.h:561
void object_update_turn_face(object *op)
Definition: object.c:1130
#define MSG_TYPE_COMMAND
Definition: newclient.h:379
#define PU_BOOTS
Definition: define.h:125
Definition: object.h:118
int stand_near_hostile(object *who)
Definition: player.c:4062
#define FLAG_ALIVE
Definition: define.h:230
Account_Char * account_chars
Definition: newserver.h:127
#define ADD_PLAYER_NO_STATS_ROLL
Definition: player.h:224
Definition: object.h:114
#define MSG_TYPE_VICTIM
Definition: newclient.h:392
int get_randomized_dir(int dir)
Definition: utils.c:439
object * object_new(void)
Definition: object.c:1068
const char * name_pl
Definition: object.h:315
object * create_archetype(const char *name)
Definition: arch.c:617
void player_set_own_title(struct pl *pl, const char *title)
Definition: player.c:264
int object_can_pick(const object *who, const object *item)
Definition: object.c:3808
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2705
void send_query(socket_struct *ns, uint8_t flags, const char *text)
Definition: request.c:674
float speed_left
Definition: object.h:329
unapplymode unapply
Definition: player.h:108
int is_wraith_pl(object *op)
Definition: player.c:165
int16_t gen_sp
Definition: player.h:114
#define MSG_TYPE_COMMAND_SUCCESS
Definition: newclient.h:510
player * get_player(player *p)
Definition: player.c:280
#define MSG_TYPE_COMMAND_ERROR
Definition: newclient.h:509
int blocked_link(object *ob, mapstruct *m, int sx, int sy)
Definition: map.c:344
#define PU_SKILLSCROLL
Definition: define.h:136
signed short int16_t
Definition: win32.h:160
uint8_t search_items
Definition: global.h:263
const char * materialname
Definition: object.h:346
int32_t weight
Definition: object.h:365
static void kill_player_permadeath(object *op)
Definition: player.c:3789
#define FLAG_CAN_USE_SKILL
Definition: define.h:322
#define MOVE_FLY_LOW
Definition: define.h:408
const char * anim_suffix
Definition: object.h:316
#define ADD_PLAYER_NEW
Definition: player.h:222
uint32_t tmp_invis
Definition: player.h:125
void drain_rod_charge(object *rod)
Definition: spell_util.c:819
int8_t Wis
Definition: living.h:36
int hide(object *op, object *skill)
Definition: skills.c:491
#define FLAG_UNAGGRESSIVE
Definition: define.h:272
#define FLAG_USE_SHIELD
Definition: define.h:237
player * add_player(socket_struct *ns, int flags)
Definition: player.c:453
#define MSG_TYPE_ADMIN_PLAYER
Definition: newclient.h:474
void receive_play_again(object *op, char key)
Definition: player.c:930
#define PU_ARROW
Definition: define.h:120
struct mapdef * map
Definition: object.h:297
void SockList_Term(SockList *sl)
Definition: lowlevel.c:58
char search_str[MAX_BUF]
Definition: player.h:192
#define snprintf
Definition: win32.h:46
#define ARCH_DEPLETION
Definition: object.h:577
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Definition: map.c:487
#define MSG_TYPE_ATTRIBUTE
Definition: newclient.h:380
object * transport
Definition: player.h:195
#define FLAG_IDENTIFIED
Definition: define.h:261
#define MOVE_FLYING
Definition: define.h:410
#define FOR_INV_FINISH()
Definition: define.h:714
object * object_find_by_flag_applied(const object *who, int flag)
Definition: object.c:4138
int16_t dam
Definition: living.h:46
uint8_t balanced_stat_loss
Definition: global.h:257
void change_attr_value(living *stats, int attr, int8_t value)
Definition: living.c:264
int die_roll(int num, int size, const object *op, int goodbad)
Definition: utils.c:122
void add_friendly_object(object *op)
Definition: friend.c:30
int32_t carrying
Definition: object.h:367
#define NDI_GREEN
Definition: newclient.h:228
const char * name
Definition: object.h:311
int16_t bed_y
Definition: player.h:98
living orig_stats
Definition: player.h:148
#define PU_INHIBIT
Definition: define.h:109
int distance_x
Definition: map.h:382
#define PU_BOW
Definition: define.h:118
int allowed_class(const object *op)
Definition: living.c:1625
#define MSG_TYPE_COMMAND_NEWPLAYER
Definition: newclient.h:516
static int similar_direction(int a, int b)
Definition: player.c:2337
uint8_t state
Definition: object.h:349
#define ST_PLAY_AGAIN
Definition: define.h:580
void roll_again(object *op)
Definition: player.c:1112
int execute_global_event(int eventcode,...)
Definition: main.c:369
#define EXIT_PATH(xyz)
Definition: define.h:455
uint8_t state
Definition: player.h:118
#define MSG_TYPE_SPELL_END
Definition: newclient.h:629
uint64_t price_base(const object *obj)
Definition: shop.c:66
uint8_t magic
Definition: treasure.h:71
void display_motd(const object *op)
Definition: player.c:134
#define offsetof(type, member)
Definition: shstr.h:37
#define CS_QUERY_SINGLECHAR
Definition: newclient.h:67
int monster_can_detect_enemy(object *op, object *enemy, rv_vector *rv)
Definition: monster.c:2386
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:149
#define FLAG_PARALYZED
Definition: define.h:380
#define PU_RATIO
Definition: define.h:113
int8_t direction
Definition: object.h:334
uint32_t nrof
Definition: object.h:333
uint8_t listening
Definition: player.h:120
void fire(object *op, int dir)
Definition: player.c:2459
int8_t Cha
Definition: living.h:36
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:447
void hiscore_check(object *op, int quiet)
Definition: hiscore.c:303
#define AP_NOPRINT
Definition: define.h:623
void remove_locked_door(object *op)
Definition: time.c:63
#define P_OUT_OF_MAP
Definition: map.h:251
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:192
int do_skill(object *op, object *part, object *skill, int dir, const char *string)
Definition: skill_util.c:428
uint16_t emergency_x
Definition: global.h:297
int move_ob(object *op, int dir, object *originator)
Definition: move.c:58
#define EXIT_X(xyz)
Definition: define.h:457
void do_learn_spell(object *op, object *spell, int special_prayer)
Definition: apply.c:391
Definition: object.h:111
uint32_t has_hit
Definition: player.h:129
struct pl * contr
Definition: object.h:276
#define PU_DRINK
Definition: define.h:116
static void flee_player(object *op)
Definition: player.c:1629
void add_statbonus(object *op)
Definition: living.c:864
#define FLAG_SCARED
Definition: define.h:271
uint8_t darkness
Definition: map.h:346
#define MSG_TYPE_SKILL_FAILURE
Definition: newclient.h:585
player * find_player_socket(const socket_struct *ns)
Definition: player.c:118
#define FLAG_XRAYS
Definition: define.h:301
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:206
void set_first_map(object *op)
Definition: player.c:398
char * spellarg
Definition: object.h:409
int8_t blocked_los[MAP_CLIENT_X][MAP_CLIENT_Y]
Definition: player.h:159
uint32_t tag_t
Definition: object.h:12
int8_t luck
Definition: living.h:39
#define ATNR_ELECTRICITY
Definition: attack.h:52
#define ATNR_POISON
Definition: attack.h:59
#define AT_PHYSICAL
Definition: attack.h:76
float speed
Definition: object.h:328
Definition: object.h:214
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define HEAD(op)
Definition: object.h:592
party_rejoin_mode rejoin_party
Definition: player.h:191
#define MSG_TYPE_ADMIN_NEWS
Definition: newclient.h:473
void clear_los(object *op)
Definition: los.c:248
#define FLAG_WIZ
Definition: define.h:231
void change_exp(object *op, int64_t exp, const char *skill_name, int flag)
Definition: living.c:2102
#define FLAG_BEEN_APPLIED
Definition: define.h:324
uint32_t golem_count
Definition: player.h:106
#define EXIT_ALT_X(xyz)
Definition: define.h:459
void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy)
Definition: object.c:4644
#define MSG_TYPE_COMMAND_DEBUG
Definition: newclient.h:508
#define BALSL_LOSS_CHANCE_RATIO
Definition: config.h:142
#define EXIT_Y(xyz)
Definition: define.h:458
void delete_character(const char *name)
Definition: login.c:86
#define MAX_BUF
Definition: define.h:35
#define MSG_TYPE_ATTACK
Definition: newclient.h:385
#define NRSPELLPATHS
Definition: spells.h:40
#define IS_SHIELD(op)
Definition: define.h:169
Definition: object.h:126
#define PU_READABLES
Definition: define.h:137
#define MSG_TYPE_ADMIN
Definition: newclient.h:377
uint32_t peaceful
Definition: player.h:131
Account_Char * account_char_remove(Account_Char *chars, const char *pl_name)
Definition: account_char.c:298
void do_hidden_move(object *op)
Definition: player.c:4021
int16_t x
Definition: object.h:326
void skill_attack(object *tmp, object *pl, int dir, const char *string, object *skill)
Definition: skill_util.c:1239
int strncasecmp(const char *s1, const char *s2, int n)
Definition: porting.c:224
int8_t levhp[11]
Definition: player.h:167
struct treasurestruct * items
Definition: treasure.h:90
#define MSG_TYPE_ATTRIBUTE_STAT_LOSS
Definition: newclient.h:563
const char * skill
Definition: object.h:321
uint8_t mapx
Definition: newserver.h:116
void fix_weight(void)
Definition: player.c:3871
void esrv_map_scroll(socket_struct *ns, int dx, int dy)
Definition: request.c:1510
#define NUM_SKILLS
Definition: skills.h:71
int8_t wc
Definition: living.h:37
int roll_stat(void)
Definition: player.c:1014
const char * confdir
Definition: global.h:241
void kill_player(object *op, const object *killer)
Definition: player.c:3461
void SockList_AddChar(SockList *sl, char c)
Definition: lowlevel.c:98
#define AC_PLAYER_STAT_NO_CHANGE
Definition: define.h:635
#define FLAG_IS_THROWN
Definition: define.h:249
uint16_t difficulty
Definition: map.h:343
static const flag_definition flags[]
#define FOR_MAP_FINISH()
Definition: define.h:767
int8_t Str
Definition: living.h:36
void animate_object(object *op, int dir)
Definition: anim.c:212
int16_t resist[NROFATTACKS]
Definition: object.h:341
#define FLAG_KNOWN_CURSED
Definition: define.h:321
object * ob
Definition: player.h:158
int has_carried_lights(const object *op)
Definition: los.c:311
int need_identify(const object *op)
Definition: item.c:1332
const char *const lose_msg[NUM_STATS]
Definition: living.c:172
#define FLAG_CURSED
Definition: define.h:317
Definition: object.h:107
unsigned int uint32_t
Definition: win32.h:162
void player_set_state(player *pl, uint8_t state)
Definition: player.c:4454
Definition: object.h:113
void safe_strcat(char *dest, const char *orig, size_t *curlen, size_t maxlen)
Definition: porting.c:346
Definition: object.h:135
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:4213
bowtype_t bowtype
Definition: player.h:101
int move_player(object *op, int dir)
Definition: player.c:3014
char killer[BIG_NAME]
Definition: player.h:171
#define FLAG_ANIMATE
Definition: define.h:242
void key_roll_stat(object *op, char key)
Definition: player.c:1184
#define PU_MELEEWEAPON
Definition: define.h:131
#define PREFER_HIGH
Definition: define.h:601
uint32_t attacktype
Definition: object.h:342
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.c:2478
float last_speed
Definition: player.h:154
#define object_decrease_nrof_by_one(xyz)
Definition: compat.h:28
#define MSG_TYPE_VICTIM_DIED
Definition: newclient.h:654
void play_sound_player_only(player *pl, int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:51
#define FLAG_NEUTRAL
Definition: define.h:362
#define RANDOM()
Definition: define.h:681
living last_stats
Definition: player.h:149
int handle_newcs_player(object *op)
Definition: player.c:3096
const char * invis_race
Definition: player.h:135
#define FREE_AND_COPY(sv, nv)
Definition: global.h:211
int16_t grace
Definition: living.h:44
signed char int8_t
Definition: win32.h:158
Definition: object.h:143
uint32_t fire_on
Definition: player.h:127
#define MAX_SPACES
Definition: player.c:596
const char * localdir
Definition: global.h:243
char password[16]
Definition: player.h:176
int32_t last_weight
Definition: player.h:142
tag_t count
Definition: object.h:299
void recursive_roll(object *op, int dir, object *pusher)
Definition: move.c:276
living stats
Definition: object.h:368
static int save_life(object *op)
Definition: player.c:3174
int8_t Dex
Definition: living.h:36
struct archt * arch
Definition: object.h:412
float last_weapon_sp
Definition: player.h:140
#define UPD_FACE
Definition: newclient.h:292
struct oblnk * next
Definition: object.h:444
int playername_ok(const char *cp)
Definition: player.c:252
int apply_container(object *op, object *sack)
Definition: apply.c:216
#define MSG_TYPE_ATTACK_NOATTACK
Definition: newclient.h:613
#define MSG_TYPE_ADMIN_RULES
Definition: newclient.h:472
void account_char_save(const char *account, Account_Char *chars)
Definition: account_char.c:146
int check_race_and_class(living *stats, archetype *race, archetype *opclass)
Definition: player.c:1400
void make_visible(object *op)
Definition: player.c:3937
static archetype * get_player_archetype(archetype *at)
Definition: player.c:495
#define SPELL_HIGHEST
Definition: spells.h:60
unsigned int distance
Definition: map.h:381
uint8_t type
Definition: object.h:338
uint32_t do_los
Definition: player.h:126
void move_player_attack(object *op, int dir)
Definition: player.c:2683
uint32_t mode
Definition: player.h:110
struct Settings settings
Definition: init.c:40
uint32_t monitor_spells
Definition: newserver.h:110
void object_set_enemy(object *op, object *enemy)
Definition: object.c:710
void object_free2(object *ob, int flags)
Definition: object.c:1412
#define ADD_PLAYER_NO_MAP
Definition: player.h:223
struct archt * next
Definition: object.h:467
int direction
Definition: map.h:384
rangetype
Definition: player.h:15
#define FLAG_NO_STRENGTH
Definition: define.h:307
#define FLAG_APPLIED
Definition: define.h:235
#define NROFATTACKS
Definition: attack.h:17
void query_short_name(const object *op, char *buf, size_t size)
Definition: item.c:548
#define PU_SPELLBOOK
Definition: define.h:135
int out_of_map(mapstruct *m, int x, int y)
Definition: map.c:2306
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
#define FLAG_LIFESAVE
Definition: define.h:306
void remove_unpaid_objects(object *op, object *env, int free_items)
Definition: player.c:3217
signed int int32_t
Definition: win32.h:159
void delete_map(mapstruct *m)
Definition: map.c:1753
int64_t last_skill_exp[NUM_SKILLS]
Definition: player.h:138
#define ST_CHANGE_CLASS
Definition: define.h:582
#define MSG_TYPE_APPLY_SUCCESS
Definition: newclient.h:598
const char * msg
Definition: object.h:322
EXTERN char first_map_path[MAX_BUF]
Definition: global.h:151
#define FLAG_MAKE_INVIS
Definition: define.h:329
int object_matches_string(object *pl, object *op, const char *name)
Definition: object.c:4471
void apply_death_exp_penalty(object *op)
Definition: living.c:2152
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:787
#define FLAG_STARTEQUIP
Definition: define.h:268
#define FORCE_NAME
Definition: spells.h:169
#define PU_VALUABLES
Definition: define.h:117
int16_t gen_sp_armour
Definition: player.h:115
#define PU_MAGICAL
Definition: define.h:132
sstring add_string(const char *str)
Definition: shstr.c:124
EXTERN player * first_player
Definition: global.h:117
void link_player_skills(object *op)
Definition: skill_util.c:112
#define FIND_PLAYER_NO_HIDDEN_DM
Definition: player.h:213
struct pl * next
Definition: player.h:93
static const int32_t MAX_FOOD
Definition: define.h:477
#define GET_MAP_OB(M, X, Y)
Definition: map.h:172
player * find_player(const char *plname)
Definition: player.c:54
void confirm_password(object *op)
Definition: player.c:980
#define PU_JEWELS
Definition: define.h:141
#define FLAG_MONSTER
Definition: define.h:245
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:869
#define EVENT_PLAYER_DEATH
Definition: plugin.h:86
#define MSG_TYPE_SKILL
Definition: newclient.h:383
int8_t Pow
Definition: living.h:36
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:300
struct obj * inv
Definition: object.h:290
uint8_t set_title
Definition: global.h:261
petmode_t petmode
Definition: player.h:102
#define NDI_UNIQUE
Definition: newclient.h:245
void apply_anim_suffix(object *who, sstring suffix)
Definition: anim.c:318
char maplevel[MAX_BUF]
Definition: player.h:96
Definition: object.h:213
int apply_race_and_class(object *op, archetype *race, archetype *opclass, living *stats)
Definition: player.c:1450
#define FLAG_READY_BOW
Definition: define.h:300
#define MSG_SUBTYPE_NONE
Definition: newclient.h:398
usekeytype usekeys
Definition: player.h:107
void player_update_bg_music(object *player)
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
#define CS_QUERY_HIDEINPUT
Definition: newclient.h:68
static int action_makes_visible(object *op)
Definition: player.c:4181
#define PU_GLOVES
Definition: define.h:126
int did_make_save(const object *op, int level, int bonus)
Definition: living.c:2201
#define FLAG_WAS_WIZ
Definition: define.h:234
int transfer_ob(object *op, int x, int y, int randomly, object *originator)
Definition: move.c:144
void player_unready_range_ob(player *pl, object *ob)
Definition: player.c:4436
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
void roll_stats(object *op)
Definition: player.c:1038
void play_sound_map(int8_t sound_type, object *emitter, int dir, const char *action)
Definition: sounds.c:101
uint8_t run_away
Definition: object.h:384
#define IS_ARMOR(op)
Definition: define.h:165
object * last_skill_ob[NUM_SKILLS]
Definition: player.h:137
void key_change_class(object *op, char key)
Definition: player.c:1260
#define MSG_TYPE_ATTACK_NOKEY
Definition: newclient.h:612
struct mapdef * next
Definition: map.h:326
#define P_BLOCKSVIEW
Definition: map.h:226
void object_set_msg(object *op, const char *msg)
Definition: object.c:4713
static void update_transport_block(object *transport, int dir)
Definition: player.c:2866
#define MSG_TYPE_ATTRIBUTE_RACE
Definition: newclient.h:557
#define ATNR_COLD
Definition: attack.h:53
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:626
#define PU_ARMOUR
Definition: define.h:123
void apply_map_builder(object *pl, int dir)
Definition: build_map.c:959
#define FOR_BELOW_PREPARE(op_, it_)
Definition: define.h:741
void remove_statbonus(object *op)
Definition: living.c:841
char * emergency_mapname
Definition: global.h:296
Definition: map.h:325
#define P_IS_ALIVE
Definition: map.h:237
int random_roll(int min, int max, const object *op, int goodbad)
Definition: utils.c:42
#define ST_GET_NAME
Definition: define.h:584
uint8_t roll_stat_points
Definition: global.h:320
const New_Face * face
Definition: object.h:332
char write_buf[MAX_BUF]
Definition: player.h:174
Definition: object.h:167
#define FLAG_UNIQUE
Definition: define.h:288
#define FLAG_NO_PICK
Definition: define.h:239
static object * find_better_arrow(object *op, object *target, const char *type, int *better)
Definition: player.c:2027
int8_t levgrace[11]
Definition: player.h:169
#define PU_POTION
Definition: define.h:133
int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2539
const char * unarmed_skill
Definition: player.h:202
Definition: object.h:120
const char *const short_stat_name[NUM_STATS]
Definition: living.c:194
struct treasurestruct * next
Definition: treasure.h:66
void enter_player_savebed(object *op)
Definition: server.c:128
uint8_t mapy
Definition: newserver.h:116
partylist * party_to_join
Definition: player.h:187
int get_dex_bonus(int stat)
Definition: living.c:2273
#define PU_HELMET
Definition: define.h:121
int16_t level
Definition: object.h:351
int8_t facing
Definition: object.h:335
#define FLAG_INV_LOCKED
Definition: define.h:330
void fix_object(object *op)
Definition: living.c:1119
#define BALSL_MAX_LOSS_RATIO
Definition: config.h:144
EXTERN mapstruct * first_map
Definition: global.h:118
void cast_dust(object *op, object *throw_ob, int dir)
Definition: player.c:3908
#define AT_DEATH
Definition: attack.h:93
#define PU_NOT_CURSED
Definition: define.h:140
EXTERN archetype * first_archetype
Definition: global.h:122
struct obj * more
Definition: object.h:295
object * find_skill_by_name(object *who, const char *name)
Definition: skill_util.c:213
object * arch_to_object(archetype *at)
Definition: arch.c:569
void object_update_speed(object *op)
Definition: object.c:1150
int32_t value
Definition: object.h:350
object * object_get_owner(object *op)
Definition: object.c:590
int8_t magic
Definition: object.h:348
uint8_t * faces_sent
Definition: newserver.h:96
static object * find_arrow(object *op, const char *type)
Definition: player.c:1995
const char * name
Definition: object.h:466
#define EVENT_BORN
Definition: plugin.h:83
#define FOR_BELOW_FINISH()
Definition: define.h:748
object * object_find_by_arch_name(const object *who, const char *name)
Definition: object.c:4161
uint8_t resurrection
Definition: global.h:262
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
int cast_spell(object *op, object *caster, int dir, object *spell_ob, char *stringarg)
Definition: spell_util.c:1471
SockList inbuf
Definition: newserver.h:99
void object_remove(object *op)
Definition: object.c:1669
#define MSG_TYPE_MOTD
Definition: newclient.h:376
int hideability(object *ob)
Definition: player.c:3980
#define PU_MAGIC_DEVICE
Definition: define.h:138
#define ST_GET_PASSWORD
Definition: define.h:585
int32_t food
Definition: living.h:48
#define FLAG_FREED
Definition: define.h:233
void key_confirm_quit(object *op, char key)
Definition: player.c:1562
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:434
Definition: object.h:224
#define ATNR_FIRE
Definition: attack.h:51
int get_thaco_bonus(int stat)
Definition: living.c:2277
void check_stat_bounds(living *stats, int8_t min_stat, int8_t max_stat)
Definition: living.c:354