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