ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/player.C
(Generate patch)

Comparing deliantra/server/server/player.C (file contents):
Revision 1.198 by root, Sat May 17 00:17:02 2008 UTC vs.
Revision 1.246 by root, Wed Nov 11 04:45:23 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
24
25//+GPL
23 26
24#include <global.h> 27#include <global.h>
25#include <sproto.h> 28#include <sproto.h>
26#include <sounds.h> 29#include <sounds.h>
27#include <living.h> 30#include <living.h>
32#include <algorithm> 35#include <algorithm>
33#include <functional> 36#include <functional>
34 37
35playervec players; 38playervec players;
36 39
37void
38display_motd (const object *op)
39{
40 char buf[MAX_BUF];
41 char motd[HUGE_BUF];
42 FILE *fp;
43 int comp;
44 int size;
45
46 sprintf (buf, "%s/%s", settings.confdir, settings.motd);
47 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
48 return;
49
50 motd[0] = '\0';
51 size = 0;
52
53 while (fgets (buf, MAX_BUF, fp))
54 {
55 if (*buf == '#')
56 continue;
57
58 strncat (motd + size, buf, HUGE_BUF - size);
59 size += strlen (buf);
60 }
61
62 draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_MOTD, MSG_SUBTYPE_NONE, motd, NULL);
63 close_and_delete (fp, comp);
64}
65
66void
67send_rules (const object *op)
68{
69 char buf[MAX_BUF];
70 char rules[HUGE_BUF];
71 FILE *fp;
72 int comp;
73 int size;
74
75 sprintf (buf, "%s/%s", settings.confdir, settings.rules);
76 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
77 return;
78
79 rules[0] = '\0';
80 size = 0;
81
82 while (fgets (buf, MAX_BUF, fp))
83 {
84 if (*buf == '#')
85 continue;
86
87 if (size + strlen (buf) >= HUGE_BUF)
88 {
89 LOG (llevDebug, "Warning, rules size is > %d bytes.\n", HUGE_BUF);
90 break;
91 }
92
93 strncat (rules + size, buf, HUGE_BUF - size);
94 size += strlen (buf);
95 }
96
97 draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_RULES, rules, NULL);
98 close_and_delete (fp, comp);
99}
100
101void
102send_news (const object *op)
103{
104 char buf[MAX_BUF];
105 char news[HUGE_BUF];
106 char subject[MAX_BUF];
107 FILE *fp;
108 int comp;
109 int size;
110
111 sprintf (buf, "%s/%s", settings.confdir, settings.news);
112 if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
113 return;
114
115 news[0] = '\0';
116 subject[0] = '\0';
117 size = 0;
118
119 while (fgets (buf, MAX_BUF, fp))
120 {
121 if (*buf == '#')
122 continue;
123
124 if (*buf == '%')
125 { /* send one news */
126 if (size > 0)
127 draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, "INFORMATION: %s\n%s", (char *)"%s\n%s", subject, news); /*send previously read news */
128
129 strcpy (subject, buf + 1);
130 strip_endline (subject);
131 size = 0;
132 news[0] = '\0';
133 }
134 else
135 {
136 if (size + strlen (buf) >= HUGE_BUF)
137 {
138 LOG (llevDebug, "Warning, one news item has size > %d bytes.\n", HUGE_BUF);
139 break;
140 }
141 strncat (news + size, buf, HUGE_BUF - size);
142 size += strlen (buf);
143 }
144 }
145
146 draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op,
147 MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, "INFORMATION: %s\n%s\n", (char *)"%s\n%s", subject, news);
148 close_and_delete (fp, comp);
149}
150
151/* This loads the first map an puts the player on it. */ 40/* This loads the first map an puts the player on it. */
152static void 41static void
153set_first_map (object *op) 42set_first_map (object *op)
154{ 43{
155 op->contr->maplevel = first_map_path; 44 op->contr->maplevel = first_map_path;
207 ob->close_container (); //TODO: client-specific 96 ob->close_container (); //TODO: client-specific
208 97
209 ns->update_look = 0; 98 ns->update_look = 0;
210 ns->look_position = 0; 99 ns->look_position = 0;
211 100
212 clear_los (this); 101 clear_los ();
213 102
214 ns->reset_stats (); 103 ns->reset_stats ();
215 104
216 /* make sure he's a player -- needed because of class change. */ 105 /* make sure he's a player -- needed because of class change. */
217 ob->type = PLAYER; // we are paranoid 106 ob->type = PLAYER; // we are paranoid
218 ob->race = ob->arch->race; 107 ob->race = ob->arch->race;
219 108
220 ob->update_weight (); 109 ob->update_weight ();
221 link_player_skills (ob); 110 link_skills ();
222
223 CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER);
224 111
225 assign (title, ob->arch->object::name); 112 assign (title, ob->arch->object::name);
226 113
227 /* if it's a dragon player, set the correct title here */ 114 /* if it's a dragon player, set the correct title here */
228 if (is_dragon_pl (ob)) 115 if (is_dragon_pl (ob))
249 esrv_send_inventory (ob, ob); 136 esrv_send_inventory (ob, ob);
250 esrv_add_spells (this, 0); 137 esrv_add_spells (this, 0);
251 138
252 activate (); 139 activate ();
253 140
254 send_rules (ob);
255 send_news (ob);
256 display_motd (ob);
257
258 INVOKE_PLAYER (CONNECT, this); 141 INVOKE_PLAYER (CONNECT, this);
259 INVOKE_PLAYER (LOGIN, this); 142 INVOKE_PLAYER (LOGIN, this);
260} 143}
261 144
262void 145void
278 ns->reset_stats (); 161 ns->reset_stats ();
279 ns->pl = 0; 162 ns->pl = 0;
280 ns = 0; 163 ns = 0;
281 } 164 }
282 165
283 observe = ob; 166 // this is important for the player scheduler to get the correct refcount
167 // when ns = 0
168 observe = viewpoint = ob;
284 169
285 deactivate (); 170 deactivate ();
286} 171}
172
173//-GPL
287 174
288// the need for this function can be explained 175// the need for this function can be explained
289// by load_object not returning the object 176// by load_object not returning the object
290void 177void
291player::set_object (object *op) 178player::set_object (object *op)
292{ 179{
293 ob = observe = op; 180 ob = observe = viewpoint = op;
294 ob->contr = this; /* this aren't yet in archetype */ 181 ob->contr = this; /* this aren't yet in archetype */
295 182
296 ob->speed = 1.0f; 183 ob->speed = 1.0f;
297 ob->speed_left = 0.5f; 184 ob->speed_left = 0.5f;
298 185
327} 214}
328 215
329void 216void
330player::set_observe (object *op) 217player::set_observe (object *op)
331{ 218{
332 observe = op ? op : ob; 219 observe = viewpoint = op ? op : ob;
333 do_los = 1; 220 do_los = 1;
334} 221}
222
223void
224player::set_viewpoint (object *op)
225{
226 viewpoint = op ? op : (object *)observe;
227 do_los = 1;
228}
229
230//+GPL
335 231
336player::player () 232player::player ()
337{ 233{
338 /* There are some elements we want initialised to non zero value - 234 /* There are some elements we want initialised to non zero value -
339 * we deal with that below this point. 235 * we deal with that below this point.
345 savebed_map = first_map_path; /* Init. respawn position */ 241 savebed_map = first_map_path; /* Init. respawn position */
346 242
347 gen_sp_armour = 10; 243 gen_sp_armour = 10;
348 bowtype = bow_normal; 244 bowtype = bow_normal;
349 petmode = pet_normal; 245 petmode = pet_normal;
350 listening = 10;
351 usekeys = containers; 246 usekeys = containers;
352 peaceful = 1; /* default peaceful */ 247 peaceful = 1; /* default peaceful */
353 do_los = 1; 248 do_los = 1;
354 249
355 weapon_sp = 1.0f; 250 weapon_sp = 1.0f;
367 { 262 {
368 ob->destroy_inv (false); 263 ob->destroy_inv (false);
369 ob->destroy (); 264 ob->destroy ();
370 } 265 }
371 266
372 ob = observe = 0; 267 ob = observe = viewpoint = 0;
373} 268}
374 269
375player::~player () 270player::~player ()
376{ 271{
377 /* Clear item stack */ 272 /* Clear item stack */
378 free (stack_items); 273 free (stack_items);
274}
275
276/*
277 * get_player_archetype() return next player archetype from archetype
278 * list. Not very efficient routine, but used only creating new players.
279 * Note: there MUST be at least one player archetype!
280 */
281static archetype *
282get_player_archetype (archetype *at)
283{
284 // archetypes could have been reloaded
285 archetype *nat = at ? archetype::find (at->archname) : archetypes [0];
286
287 if (!nat)
288 return at;
289
290 archvec::iterator i = archetypes.find (nat);
291
292 for (;;)
293 {
294 if (++i == archetypes.end ())
295 i = archetypes.begin ();
296 else if (*i == at)
297 cleanup ("not a single player archetype found");
298
299 if ((*i)->type == PLAYER)
300 return *i;
301 }
379} 302}
380 303
381/* Tries to add player on the connection passed in ns. 304/* Tries to add player on the connection passed in ns.
382 * All we can really get in this is some settings like host and display 305 * All we can really get in this is some settings like host and display
383 * mode. 306 * mode.
394 pl->ob->run_away = 25; /* Then we panick... */ 317 pl->ob->run_away = 25; /* Then we panick... */
395 318
396 set_first_map (pl->ob); 319 set_first_map (pl->ob);
397 320
398 return pl; 321 return pl;
399}
400
401/*
402 * get_player_archetype() return next player archetype from archetype
403 * list. Not very efficient routine, but used only creating new players.
404 * Note: there MUST be at least one player archetype!
405 */
406archetype *
407get_player_archetype (archetype *at)
408{
409 // archetypes could have been reloaded
410 archetype *nat = at ? archetype::find (at->archname) : archetypes [0];
411
412 if (!nat)
413 return at;
414
415 archvec::iterator i = archetypes.find (nat);
416
417 for (;;)
418 {
419 if (++i == archetypes.end ())
420 i = archetypes.begin ();
421 else if (*i == at)
422 cleanup ("not a single player archetype found");
423
424 if ((*i)->type == PLAYER)
425 return *i;
426 }
427} 322}
428 323
429object * 324object *
430get_nearest_player (object *mon) 325get_nearest_player (object *mon)
431{ 326{
671 op->destroy (); 566 op->destroy ();
672 continue; 567 continue;
673 } 568 }
674 } 569 }
675 570
676 /* This really needs to be better - we should really give 571 /* Here we remove duplicated skills (as duplicated spell objects have
677 * a substitute spellbook. The problem is that we don't really 572 * _very_ confusing effects for players), which could for instance be
678 * have a good idea what to replace it with (need something like 573 * generated by bad treasurelists. - elmex
679 * a first level treasurelist for each skill.)
680 * remove duplicate skills also
681 */ 574 */
682 if (op->type == SPELLBOOK || op->type == SKILL) 575 if (op->type == SKILL)
683 { 576 {
684 object *tmp;
685
686 for (tmp = op->below; tmp; tmp = tmp->below) 577 for (object *tmp = op->below; tmp; tmp = tmp->below)
687 if (tmp->type == op->type && tmp->name == op->name) 578 if (tmp->type == op->type && tmp->name == op->name)
688 break;
689
690 if (tmp)
691 { 579 {
692 op->destroy (); 580 op->destroy ();
581 LOG (llevError,
693 LOG (llevError, "give_initial_items: Removing duplicate object %s\n", &tmp->name); 582 "give_initial_items: Removing duplicate skill %s\n", &tmp->name);
694 continue; 583 break;
695 } 584 }
696 585
697 if (op->nrof > 1) 586 if (op->nrof > 1)
698 op->nrof = 1; 587 op->nrof = 1;
699 } 588 }
700 589
726 else /* lock all 'normal items by default */ 615 else /* lock all 'normal items by default */
727 SET_FLAG (op, FLAG_INV_LOCKED); 616 SET_FLAG (op, FLAG_INV_LOCKED);
728 } /* for loop of objects in player inv */ 617 } /* for loop of objects in player inv */
729 618
730 /* Need to set up the skill pointers */ 619 /* Need to set up the skill pointers */
731 link_player_skills (pl); 620 pl->contr->link_skills ();
732} 621}
733 622
734void 623void
735get_party_password (object *op, partylist *party) 624get_party_password (object *op, partylist *party)
736{ 625{
853player::chargen_race_done () 742player::chargen_race_done ()
854{ 743{
855 /* this must before then initial items are given */ 744 /* this must before then initial items are given */
856 esrv_new_player (ob->contr); 745 esrv_new_player (ob->contr);
857 746
858 treasurelist *tl = treasurelist::find ("starting_wealth"); 747 treasurelist *tl = treasurelist::find (shstr_starting_wealth);
859 if (tl) 748 if (tl)
860 create_treasure (tl, ob, 0, 0, 0); 749 create_treasure (tl, ob, 0, 0, 0);
861 750
862 INVOKE_PLAYER (BIRTH, ob->contr); 751 INVOKE_PLAYER (BIRTH, ob->contr);
863 INVOKE_PLAYER (LOGIN, ob->contr); 752 INVOKE_PLAYER (LOGIN, ob->contr);
868 ob->msg = 0; 757 ob->msg = 0;
869 758
870 start_info (ob); 759 start_info (ob);
871 CLEAR_FLAG (ob, FLAG_WIZ); 760 CLEAR_FLAG (ob, FLAG_WIZ);
872 give_initial_items (ob, ob->randomitems); 761 give_initial_items (ob, ob->randomitems);
873 link_player_skills (ob);
874 esrv_send_inventory (ob, ob); 762 esrv_send_inventory (ob, ob);
875 ob->update_stats (); 763 ob->update_stats ();
876 764
877 /* This moves the player to a different start map, if there 765 /* This moves the player to a different start map, if there
878 * is one for this race 766 * is one for this race
917 ob->stats.hp = ob->stats.maxhp; 805 ob->stats.hp = ob->stats.maxhp;
918 ob->stats.sp = ob->stats.maxsp; 806 ob->stats.sp = ob->stats.maxsp;
919 ob->stats.grace = 0; 807 ob->stats.grace = 0;
920} 808}
921 809
922void 810static void
923flee_player (object *op) 811flee_player (object *op)
924{ 812{
925 int dir, diff; 813 int dir, diff;
926 rv_vector rv; 814 rv_vector rv;
927 815
930 LOG (llevDebug, "Fleeing player is dead.\n"); 818 LOG (llevDebug, "Fleeing player is dead.\n");
931 CLEAR_FLAG (op, FLAG_SCARED); 819 CLEAR_FLAG (op, FLAG_SCARED);
932 return; 820 return;
933 } 821 }
934 822
935 if (op->enemy == NULL) 823 if (!op->enemy)
936 { 824 {
937 LOG (llevDebug, "Fleeing player had no enemy.\n"); 825 LOG (llevDebug, "Fleeing player had no enemy.\n");
938 CLEAR_FLAG (op, FLAG_SCARED); 826 CLEAR_FLAG (op, FLAG_SCARED);
939 return; 827 return;
940 } 828 }
941 829
942 /* Seen some crashes here. Since we don't store an
943 * op->enemy_count, it is possible that something destroys the
944 * actual enemy, and the object is recycled.
945 */
946 if (op->enemy->map == NULL)
947 {
948 CLEAR_FLAG (op, FLAG_SCARED);
949 op->enemy = NULL;
950 return;
951 }
952
953 if (!(random_roll (0, 4, op, PREFER_LOW)) && did_make_save (op, op->level, 0)) 830 if (!(random_roll (0, 4, op, PREFER_LOW)) && did_make_save (op, op->level, 0))
954 { 831 {
955 op->enemy = NULL; 832 op->enemy = NULL;
956 CLEAR_FLAG (op, FLAG_SCARED); 833 CLEAR_FLAG (op, FLAG_SCARED);
957 return; 834 return;
960 get_rangevector (op, op->enemy, &rv, 0); 837 get_rangevector (op, op->enemy, &rv, 0);
961 838
962 dir = absdir (4 + rv.direction); 839 dir = absdir (4 + rv.direction);
963 for (diff = 0; diff < 3; diff++) 840 for (diff = 0; diff < 3; diff++)
964 { 841 {
965 int m = 1 - (RANDOM () & 2); 842 int m = 1 - rndm (2) * 2;
966 843
967 if (move_ob (op, absdir (dir + diff * m), op) || (diff == 0 && move_ob (op, absdir (dir - diff * m), op))) 844 if (move_ob (op, absdir (dir + diff * m), op) || (diff == 0 && move_ob (op, absdir (dir - diff * m), op)))
968 return; 845 return;
969 } 846 }
970 847
981check_pick (object *op) 858check_pick (object *op)
982{ 859{
983 object *tmp, *next; 860 object *tmp, *next;
984 int stop = 0; 861 int stop = 0;
985 int wvratio; 862 int wvratio;
986 char putstring[128];
987 863
988 /* if you're flying, you cna't pick up anything */ 864 /* if you're flying, you cna't pick up anything */
989 if (op->move_type & MOVE_FLYING) 865 if (op->move_type & MOVE_FLYING)
990 return 1; 866 return 1;
991 867
992 next = op->below; 868 next = op->below;
993 869
994 int cnt = MAX_ITEM_PER_DROP; 870 int cnt = MAX_ITEM_PER_ACTION;
995#define CHK_PICK_PICKUP do { pick_up (op, tmp); cnt--; } while (0) 871#define CHK_PICK_PICKUP do { pick_up (op, tmp); cnt--; } while (0)
996 872
997 /* loop while there are items on the floor that are not marked as 873 /* loop while there are items on the floor that are not marked as
998 * destroyed */ 874 * destroyed */
999 while (next && !next->destroyed ()) 875 while (next && !next->destroyed ())
1015 891
1016 if (op->contr->search_str[0] != '\0' && settings.search_items == TRUE) 892 if (op->contr->search_str[0] != '\0' && settings.search_items == TRUE)
1017 { 893 {
1018 if (item_matched_string (op, tmp, op->contr->search_str)) 894 if (item_matched_string (op, tmp, op->contr->search_str))
1019 CHK_PICK_PICKUP; 895 CHK_PICK_PICKUP;
896
1020 continue; 897 continue;
1021 } 898 }
1022 899
1023 /* high not bit set? We're using the old autopickup model */ 900 /* pickup handling */
901 if (op->contr->mode & PU_DEBUG)
902 {
903 /* some debugging code to figure out item information */
904 const char *str = tmp->name
905 ? format ("item name: %s item type: %d weight/value: %d",
906 &tmp->name, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * max (tmp->nrof, 1))))
907 : format ("item name: %s item type: %d weight/value: %d",
908 &tmp->arch->archname, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * max (tmp->nrof, 1))));
909
910 new_draw_info (NDI_UNIQUE, 0, op, str);
911 }
912
913 if (op->contr->mode & PU_INHIBIT)
914 return 1;
915
916 if (!(op->contr->mode & PU_ENABLE)) // TODO: fix client
917 return 1;
918
919 /* philosophy:
920 * It's easy to grab an item type from a pile, as long as it's
921 * generic. This takes no game-time. For more detailed pickups
922 * and selections, select-items should be used. This is a
923 * grab-as-you-run type mode that's really useful for arrows for
924 * example.
925 * The drawback: right now it has no frontend, so you need to
926 * stick the bits you want into a calculator in hex mode and then
927 * convert to decimal and then 'pickup <#>
928 */
929
930 /* the first two modes are exclusive: if NOTHING we return, if
931 * STOP then we stop. All the rest are applied sequentially,
932 * meaning if any test passes, the item gets picked up. */
933
934 /* if mode is set to pick nothing up, return */
935 if (op->contr->mode == PU_NOTHING)
936 return 1;
937
938 /* if mode is set to stop when encountering objects, return */
939 /* take STOP before INHIBIT since it doesn't actually pick
940 * anything up */
941 if (op->contr->mode & PU_STOP)
942 return 0;
943
944 /* useful for going into stores and not losing your settings... */
945 /* and for battles wher you don't want to get loaded down while
946 * fighting */
947 if (op->contr->mode & PU_INHIBIT)
948 return 1;
949
950 /* prevent us from turning into auto-thieves :) */
951 if (QUERY_FLAG (tmp, FLAG_UNPAID))
952 continue;
953
954 /* ignore known cursed objects */
955 if (QUERY_FLAG (tmp, FLAG_KNOWN_CURSED) && op->contr->mode & PU_NOT_CURSED)
956 continue;
957
958 /* all food and drink if desired */
959 /* question: don't pick up known-poisonous stuff? */
1024 if (!(op->contr->mode & PU_NEWMODE)) 960 if (op->contr->mode & PU_FOOD)
961 if (tmp->type == FOOD)
1025 { 962 {
1026 switch (op->contr->mode) 963 CHK_PICK_PICKUP;
964 continue;
965 }
966
967 if (op->contr->mode & PU_DRINK)
968 if (tmp->type == DRINK || (tmp->type == POISON && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED)))
969 {
970 CHK_PICK_PICKUP;
971 continue;
972 }
973
974 if (op->contr->mode & PU_POTION)
975 if (tmp->type == POTION)
976 {
977 CHK_PICK_PICKUP;
978 continue;
979 }
980
981 /* spellbooks, skillscrolls and normal books/scrolls */
982 if (op->contr->mode & PU_SPELLBOOK)
983 if (tmp->type == SPELLBOOK)
984 {
985 CHK_PICK_PICKUP;
986 continue;
987 }
988
989 if (op->contr->mode & PU_SKILLSCROLL)
990 if (tmp->type == SKILLSCROLL)
991 {
992 CHK_PICK_PICKUP;
993 continue;
994 }
995
996 if (op->contr->mode & PU_READABLES)
997 if (tmp->type == BOOK || tmp->type == SCROLL || tmp->type == INSCRIBABLE)
998 {
999 CHK_PICK_PICKUP;
1000 continue;
1001 }
1002
1003 /* wands/staves/rods/horns */
1004 if (op->contr->mode & PU_MAGIC_DEVICE)
1005 if (tmp->type == WAND || tmp->type == ROD || tmp->type == HORN)
1006 {
1007 CHK_PICK_PICKUP;
1008 continue;
1009 }
1010
1011 /* pick up all magical items */
1012 if (op->contr->mode & PU_MAGICAL)
1013 if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL) && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED))
1014 {
1015 CHK_PICK_PICKUP;
1016 continue;
1017 }
1018
1019 if (op->contr->mode & PU_VALUABLES)
1020 {
1021 if (tmp->type == MONEY || tmp->type == GEM)
1027 { 1022 {
1028 case 0:
1029 return 1; /* don't pick up */
1030 case 1:
1031 CHK_PICK_PICKUP; 1023 CHK_PICK_PICKUP;
1032 return 1; 1024 continue;
1033 case 2:
1034 CHK_PICK_PICKUP;
1035 return 0;
1036 case 3:
1037 return 0; /* stop before pickup */
1038 case 4:
1039 CHK_PICK_PICKUP;
1040 break;
1041 case 5:
1042 CHK_PICK_PICKUP;
1043 stop = 1;
1044 break;
1045 case 6:
1046 if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL)
1047 && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED))
1048 CHK_PICK_PICKUP;
1049 break;
1050
1051 case 7:
1052 if (tmp->type == MONEY || tmp->type == GEM)
1053 CHK_PICK_PICKUP;
1054 break;
1055
1056 default:
1057 /* use value density */
1058 if (!QUERY_FLAG (tmp, FLAG_UNPAID)
1059 && (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))) >= op->contr->mode)
1060 CHK_PICK_PICKUP;
1061 } 1025 }
1062 } 1026 }
1063 else 1027
1064 { /* old model */ 1028 /* rings & amulets - talismans seems to be typed AMULET */
1065 /* NEW pickup handling */
1066 if (op->contr->mode & PU_DEBUG) 1029 if (op->contr->mode & PU_JEWELS)
1030 if (tmp->type == RING || tmp->type == AMULET)
1067 { 1031 {
1068 /* some debugging code to figure out item information */ 1032 CHK_PICK_PICKUP;
1069 if (tmp->name != NULL)
1070 sprintf (putstring, "item name: %s item type: %d weight/value: %d",
1071 &tmp->name, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))));
1072 else
1073 sprintf (putstring, "item name: %s item type: %d weight/value: %d",
1074 &tmp->arch->archname, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))));
1075
1076 new_draw_info (NDI_UNIQUE, 0, op, putstring);
1077 }
1078
1079 /* philosophy:
1080 * It's easy to grab an item type from a pile, as long as it's
1081 * generic. This takes no game-time. For more detailed pickups
1082 * and selections, select-items should be used. This is a
1083 * grab-as-you-run type mode that's really useful for arrows for
1084 * example.
1085 * The drawback: right now it has no frontend, so you need to
1086 * stick the bits you want into a calculator in hex mode and then
1087 * convert to decimal and then 'pickup <#>
1088 */
1089
1090 /* the first two modes are exclusive: if NOTHING we return, if
1091 * STOP then we stop. All the rest are applied sequentially,
1092 * meaning if any test passes, the item gets picked up. */
1093
1094 /* if mode is set to pick nothing up, return */
1095
1096 if (op->contr->mode & PU_NOTHING)
1097 return 1;
1098
1099 /* if mode is set to stop when encountering objects, return */
1100 /* take STOP before INHIBIT since it doesn't actually pick
1101 * anything up */
1102
1103 if (op->contr->mode & PU_STOP)
1104 return 0;
1105
1106 /* useful for going into stores and not losing your settings... */
1107 /* and for battles wher you don't want to get loaded down while
1108 * fighting */
1109 if (op->contr->mode & PU_INHIBIT)
1110 return 1;
1111
1112 /* prevent us from turning into auto-thieves :) */
1113 if (QUERY_FLAG (tmp, FLAG_UNPAID))
1114 continue; 1033 continue;
1034 }
1115 1035
1116 /* ignore known cursed objects */ 1036 /* we don't forget dragon food */
1117 if (QUERY_FLAG (tmp, FLAG_KNOWN_CURSED) && op->contr->mode & PU_NOT_CURSED) 1037 if (op->contr->mode & PU_FLESH)
1038 if (tmp->type == FLESH)
1039 {
1040 CHK_PICK_PICKUP;
1118 continue; 1041 continue;
1042 }
1119 1043
1120 /* all food and drink if desired */ 1044 /* bows and arrows. Bows are good for selling! */
1121 /* question: don't pick up known-poisonous stuff? */ 1045 if (op->contr->mode & PU_BOW)
1046 if (tmp->type == BOW)
1047 {
1048 CHK_PICK_PICKUP;
1049 continue;
1050 }
1051
1052 if (op->contr->mode & PU_ARROW)
1053 if (tmp->type == ARROW)
1054 {
1055 CHK_PICK_PICKUP;
1056 continue;
1057 }
1058
1059 /* all kinds of armor etc. */
1060 if (op->contr->mode & PU_ARMOUR)
1061 if (tmp->type == ARMOUR)
1062 {
1063 CHK_PICK_PICKUP;
1064 continue;
1065 }
1066
1067 if (op->contr->mode & PU_HELMET)
1068 if (tmp->type == HELMET)
1069 {
1070 CHK_PICK_PICKUP;
1071 continue;
1072 }
1073
1074 if (op->contr->mode & PU_SHIELD)
1075 if (tmp->type == SHIELD)
1076 {
1077 CHK_PICK_PICKUP;
1078 continue;
1079 }
1080
1122 if (op->contr->mode & PU_FOOD) 1081 if (op->contr->mode & PU_BOOTS)
1123 if (tmp->type == FOOD) 1082 if (tmp->type == BOOTS)
1083 {
1084 CHK_PICK_PICKUP;
1085 continue;
1086 }
1087
1088 if (op->contr->mode & PU_GLOVES)
1089 if (tmp->type == GLOVES)
1090 {
1091 CHK_PICK_PICKUP;
1092 continue;
1093 }
1094
1095 if (op->contr->mode & PU_CLOAK)
1096 if (tmp->type == CLOAK)
1097 {
1098 CHK_PICK_PICKUP;
1099 continue;
1100 }
1101
1102 /* hoping to catch throwing daggers here */
1103 if (op->contr->mode & PU_MISSILEWEAPON)
1104 if (tmp->type == WEAPON && QUERY_FLAG (tmp, FLAG_IS_THROWN))
1105 {
1106 CHK_PICK_PICKUP;
1107 continue;
1108 }
1109
1110 /* careful: chairs and tables are weapons! */
1111 if (op->contr->mode & PU_ALLWEAPON)
1112 {
1113 if (tmp->type == WEAPON)
1114 if (!tmp->arch->archname.contains ("table") && !tmp->arch->archname.contains ("chair"))
1124 { 1115 {
1125 CHK_PICK_PICKUP; 1116 CHK_PICK_PICKUP;
1126 continue; 1117 continue;
1127 } 1118 }
1119 }
1128 1120
1121 /* misc stuff that's useful */
1129 if (op->contr->mode & PU_DRINK) 1122 if (op->contr->mode & PU_KEY)
1130 if (tmp->type == DRINK || (tmp->type == POISON && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED))) 1123 if (tmp->type == KEY || tmp->type == SPECIAL_KEY)
1131 { 1124 {
1132 CHK_PICK_PICKUP; 1125 CHK_PICK_PICKUP;
1133 continue; 1126 continue;
1134 } 1127 }
1135 1128
1129 /* any of the last 4 bits set means we use the ratio for value
1130 * pickups */
1136 if (op->contr->mode & PU_POTION) 1131 if (op->contr->mode & PU_RATIO)
1137 if (tmp->type == POTION) 1132 {
1133 /* use value density to decide what else to grab */
1134 /* >=7 was >= op->contr->mode */
1135 /* >=7 is the old standard setting. Now we take the last 4 bits
1138 { 1136 */
1139 CHK_PICK_PICKUP;
1140 continue;
1141 }
1142
1143 /* spellbooks, skillscrolls and normal books/scrolls */
1144 if (op->contr->mode & PU_SPELLBOOK)
1145 if (tmp->type == SPELLBOOK)
1146 {
1147 CHK_PICK_PICKUP;
1148 continue;
1149 }
1150
1151 if (op->contr->mode & PU_SKILLSCROLL)
1152 if (tmp->type == SKILLSCROLL)
1153 {
1154 CHK_PICK_PICKUP;
1155 continue;
1156 }
1157
1158 if (op->contr->mode & PU_READABLES)
1159 if (tmp->type == BOOK || tmp->type == SCROLL || tmp->type == INSCRIBABLE)
1160 {
1161 CHK_PICK_PICKUP;
1162 continue;
1163 }
1164
1165 /* wands/staves/rods/horns */
1166 if (op->contr->mode & PU_MAGIC_DEVICE)
1167 if (tmp->type == WAND || tmp->type == ROD || tmp->type == HORN)
1168 {
1169 CHK_PICK_PICKUP;
1170 continue;
1171 }
1172
1173 /* pick up all magical items */
1174 if (op->contr->mode & PU_MAGICAL) 1137 wvratio = op->contr->mode & PU_RATIO;
1175 if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL) && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED)) 1138 if (1000 * query_cost (tmp, op, F_TRUE) / tmp->total_weight () >= wvratio * 100)
1176 {
1177 CHK_PICK_PICKUP;
1178 continue;
1179 }
1180
1181 if (op->contr->mode & PU_VALUABLES)
1182 { 1139 {
1183 if (tmp->type == MONEY || tmp->type == GEM) 1140#if 0
1141 fprintf (stderr, "HIGH WEIGHT/VALUE [");
1142 if (tmp->name != NULL)
1184 { 1143 {
1185 CHK_PICK_PICKUP; 1144 fprintf (stderr, "%s", tmp->name);
1186 continue;
1187 } 1145 }
1188 }
1189
1190 /* rings & amulets - talismans seems to be typed AMULET */
1191 if (op->contr->mode & PU_JEWELS)
1192 if (tmp->type == RING || tmp->type == AMULET)
1193 {
1194 CHK_PICK_PICKUP;
1195 continue;
1196 }
1197
1198 /* we don't forget dragon food */
1199 if (op->contr->mode & PU_FLESH)
1200 if (tmp->type == FLESH)
1201 {
1202 CHK_PICK_PICKUP;
1203 continue;
1204 }
1205
1206 /* bows and arrows. Bows are good for selling! */
1207 if (op->contr->mode & PU_BOW)
1208 if (tmp->type == BOW)
1209 {
1210 CHK_PICK_PICKUP;
1211 continue;
1212 }
1213
1214 if (op->contr->mode & PU_ARROW)
1215 if (tmp->type == ARROW)
1216 {
1217 CHK_PICK_PICKUP;
1218 continue;
1219 }
1220
1221 /* all kinds of armor etc. */
1222 if (op->contr->mode & PU_ARMOUR)
1223 if (tmp->type == ARMOUR)
1224 {
1225 CHK_PICK_PICKUP;
1226 continue;
1227 }
1228
1229 if (op->contr->mode & PU_HELMET)
1230 if (tmp->type == HELMET)
1231 {
1232 CHK_PICK_PICKUP;
1233 continue;
1234 }
1235
1236 if (op->contr->mode & PU_SHIELD)
1237 if (tmp->type == SHIELD)
1238 {
1239 CHK_PICK_PICKUP;
1240 continue;
1241 }
1242
1243 if (op->contr->mode & PU_BOOTS)
1244 if (tmp->type == BOOTS)
1245 {
1246 CHK_PICK_PICKUP;
1247 continue;
1248 }
1249
1250 if (op->contr->mode & PU_GLOVES)
1251 if (tmp->type == GLOVES)
1252 {
1253 CHK_PICK_PICKUP;
1254 continue;
1255 }
1256
1257 if (op->contr->mode & PU_CLOAK)
1258 if (tmp->type == CLOAK)
1259 {
1260 CHK_PICK_PICKUP;
1261 continue;
1262 }
1263
1264 /* hoping to catch throwing daggers here */
1265 if (op->contr->mode & PU_MISSILEWEAPON)
1266 if (tmp->type == WEAPON && QUERY_FLAG (tmp, FLAG_IS_THROWN))
1267 {
1268 CHK_PICK_PICKUP;
1269 continue;
1270 }
1271
1272 /* careful: chairs and tables are weapons! */
1273 if (op->contr->mode & PU_ALLWEAPON)
1274 {
1275 if (tmp->type == WEAPON && tmp->name != NULL)
1276 {
1277 if (strstr (tmp->name, "table") == NULL && strstr (tmp->arch->archname, "table") == NULL &&
1278 strstr (tmp->name, "chair") && strstr (tmp->arch->archname, "chair") == NULL)
1279 {
1280 CHK_PICK_PICKUP;
1281 continue;
1282 }
1283 }
1284
1285 if (tmp->type == WEAPON && tmp->name == NULL)
1286 {
1287 if (strstr (tmp->arch->archname, "table") == NULL && strstr (tmp->arch->archname, "chair") == NULL)
1288 {
1289 CHK_PICK_PICKUP;
1290 continue;
1291 }
1292 }
1293 }
1294
1295 /* misc stuff that's useful */
1296 if (op->contr->mode & PU_KEY)
1297 if (tmp->type == KEY || tmp->type == SPECIAL_KEY)
1298 {
1299 CHK_PICK_PICKUP;
1300 continue;
1301 }
1302
1303 /* any of the last 4 bits set means we use the ratio for value
1304 * pickups */
1305 if (op->contr->mode & PU_RATIO)
1306 {
1307 /* use value density to decide what else to grab */
1308 /* >=7 was >= op->contr->mode */
1309 /* >=7 is the old standard setting. Now we take the last 4 bits
1310 * and multiply them by 5, giving 0..15*5== 5..75 */
1311 wvratio = (op->contr->mode & PU_RATIO) * 5;
1312 if ((query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))) >= (unsigned int) wvratio)
1313 {
1314 CHK_PICK_PICKUP;
1315#if 0
1316 fprintf (stderr, "HIGH WEIGHT/VALUE [");
1317 if (tmp->name != NULL)
1318 {
1319 fprintf (stderr, "%s", tmp->name);
1320 }
1321 else 1146 else
1322 fprintf (stderr, "%s", tmp->arch->archname); 1147 fprintf (stderr, "%s", tmp->arch->archname);
1323 fprintf (stderr, ",%d] = ", tmp->type); 1148 fprintf (stderr, ",%d] = ", tmp->type);
1324 fprintf (stderr, "%d\n", (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1)))); 1149 fprintf (stderr, "%d\n", (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * max (tmp->nrof, 1))));
1325#endif 1150#endif
1151 CHK_PICK_PICKUP;
1326 continue; 1152 continue;
1327 }
1328 } 1153 }
1329 } /* the new pickup model */ 1154 } /* the new pickup model */
1330 } 1155 }
1331 1156
1332 return !stop; 1157 return !stop;
1158}
1159
1160/* routine for both players and monsters. We call this when
1161 * there is a possibility for our action distrubing our hiding
1162 * place or invisiblity spell. Artefact invisiblity causes
1163 * "noise" instead. If we arent invisible to begin with, we
1164 * return 0.
1165 */
1166static int
1167action_makes_visible (object *op)
1168{
1169 if (op->invisible && QUERY_FLAG (op, FLAG_ALIVE))
1170 {
1171 if (QUERY_FLAG (op, FLAG_MAKE_INVIS))
1172 {
1173 // artefact invisibility is permanent, but we still make noise
1174 // this is important for game-balance.
1175 if (op->contr)
1176 op->make_noise ();
1177
1178 return 0;
1179 }
1180
1181 if (op->contr && op->contr->tmp_invis == 0)
1182 return 0;
1183
1184 /* If monsters, they should become visible */
1185 if (op->flag [FLAG_HIDDEN] || !op->contr || (op->contr && op->contr->tmp_invis))
1186 {
1187 new_draw_info_format (NDI_UNIQUE, 0, op, "You become %s!", op->flag [FLAG_HIDDEN] ? "unhidden" : "visible");
1188 return 1;
1189 }
1190 }
1191
1192 return 0;
1333} 1193}
1334 1194
1335/* 1195/*
1336 * Find an arrow in the inventory and after that 1196 * Find an arrow in the inventory and after that
1337 * in the right type container (quiver). Pointer to the 1197 * in the right type container (quiver). Pointer to the
1338 * found object is returned. 1198 * found object is returned.
1339 */ 1199 */
1340object * 1200static object *
1341find_arrow (object *op, const char *type) 1201find_arrow (object *op, const char *type)
1342{ 1202{
1343 for (object *tmp = op->inv; tmp; tmp = tmp->below) 1203 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1344 if (tmp->type == ARROW && !strcmp (&tmp->race, type)) 1204 if (tmp->type == ARROW && !strcmp (&tmp->race, type))
1345 return splay (tmp); 1205 return splay (tmp);
1359 * Similar to find_arrow, but looks for (roughly) the best arrow to use 1219 * Similar to find_arrow, but looks for (roughly) the best arrow to use
1360 * against the target. A full test is not performed, simply a basic test 1220 * against the target. A full test is not performed, simply a basic test
1361 * of resistances. The archer is making a quick guess at what he sees down 1221 * of resistances. The archer is making a quick guess at what he sees down
1362 * the hall. Failing that it does it's best to pick the highest plus arrow. 1222 * the hall. Failing that it does it's best to pick the highest plus arrow.
1363 */ 1223 */
1364object * 1224static object *
1365find_better_arrow (object *op, object *target, const char *type, int *better) 1225find_better_arrow (object *op, object *target, shstr_cmp type, int *better)
1366{ 1226{
1367 object *tmp = NULL, *arrow, *ntmp; 1227 object *tmp = NULL, *arrow, *ntmp;
1368 int attacknum, attacktype, betterby = 0, i; 1228 int attacknum, attacktype, betterby = 0, i;
1369 1229
1370 if (!type) 1230 if (!type)
1374 { 1234 {
1375 if (arrow->type == CONTAINER && arrow->race == type && QUERY_FLAG (arrow, FLAG_APPLIED)) 1235 if (arrow->type == CONTAINER && arrow->race == type && QUERY_FLAG (arrow, FLAG_APPLIED))
1376 { 1236 {
1377 i = 0; 1237 i = 0;
1378 ntmp = find_better_arrow (arrow, target, type, &i); 1238 ntmp = find_better_arrow (arrow, target, type, &i);
1239
1379 if (i > betterby) 1240 if (i > betterby)
1380 { 1241 {
1381 tmp = ntmp; 1242 tmp = ntmp;
1382 betterby = i; 1243 betterby = i;
1383 } 1244 }
1384 } 1245 }
1385 else if (arrow->type == ARROW && arrow->race == type) 1246 else if (arrow->type == ARROW && arrow->race == type)
1386 { 1247 {
1387 /* allways prefer assasination/slaying */ 1248 /* allways prefer assasination/slaying */
1388 if (target->race && arrow->slaying && strstr (arrow->slaying, target->race)) 1249 if (target->race && arrow->slaying.contains (target->race))
1389 { 1250 {
1390 if (arrow->attacktype & AT_DEATH) 1251 if (arrow->attacktype & AT_DEATH)
1391 { 1252 {
1392 *better = 100; 1253 *better = 100;
1393 return arrow; 1254 return arrow;
1408 { 1269 {
1409 tmp = arrow; 1270 tmp = arrow;
1410 betterby = (arrow->magic + arrow->stats.dam) * (100 - target->arch->resist[attacknum]) / 100; 1271 betterby = (arrow->magic + arrow->stats.dam) * (100 - target->arch->resist[attacknum]) / 100;
1411 } 1272 }
1412 } 1273 }
1274
1413 if ((2 + arrow->magic + arrow->stats.dam) > betterby) 1275 if ((2 + arrow->magic + arrow->stats.dam) > betterby)
1414 { 1276 {
1415 tmp = arrow; 1277 tmp = arrow;
1416 betterby = 2 + arrow->magic + arrow->stats.dam; 1278 betterby = 2 + arrow->magic + arrow->stats.dam;
1417 } 1279 }
1280
1418 if (arrow->title && (1 + arrow->magic + arrow->stats.dam) > betterby) 1281 if (arrow->title && (1 + arrow->magic + arrow->stats.dam) > betterby)
1419 { 1282 {
1420 tmp = arrow; 1283 tmp = arrow;
1421 betterby = 1 + arrow->magic + arrow->stats.dam; 1284 betterby = 1 + arrow->magic + arrow->stats.dam;
1422 } 1285 }
1435 * find_better_arrow to find a decent arrow to use. 1298 * find_better_arrow to find a decent arrow to use.
1436 * op = the shooter 1299 * op = the shooter
1437 * type = bow->race 1300 * type = bow->race
1438 * dir = fire direction 1301 * dir = fire direction
1439 */ 1302 */
1440object * 1303static object *
1441pick_arrow_target (object *op, const char *type, int dir) 1304pick_arrow_target (object *op, shstr_cmp type, int dir)
1442{ 1305{
1443 object *tmp = NULL; 1306 object *tmp = NULL;
1444 maptile *m; 1307 maptile *m;
1445 int i, mflags, found, number; 1308 int i, mflags, found, number;
1446 sint16 x, y; 1309 sint16 x, y;
1461 for (i = 0, found = 0; i < 20; i++) 1324 for (i = 0, found = 0; i < 20; i++)
1462 { 1325 {
1463 x += freearr_x[dir]; 1326 x += freearr_x[dir];
1464 y += freearr_y[dir]; 1327 y += freearr_y[dir];
1465 mflags = get_map_flags (m, &m, x, y, &x, &y); 1328 mflags = get_map_flags (m, &m, x, y, &x, &y);
1329
1466 if (mflags & P_OUT_OF_MAP || mflags & P_BLOCKSVIEW) 1330 if (mflags & P_OUT_OF_MAP || mflags & P_BLOCKSVIEW)
1467 { 1331 {
1468 tmp = NULL; 1332 tmp = 0;
1469 break; 1333 break;
1470 } 1334 }
1471 else if (GET_MAP_MOVE_BLOCK (m, x, y) == MOVE_FLY_LOW) 1335 else if (GET_MAP_MOVE_BLOCK (m, x, y) == MOVE_FLY_LOW)
1472 { 1336 {
1473 /* This block presumes arrows and the like are MOVE_FLY_SLOW - 1337 /* This block presumes arrows and the like are MOVE_FLY_SLOW -
1474 * perhaps a bad assumption. 1338 * perhaps a bad assumption.
1475 */ 1339 */
1476 tmp = NULL; 1340 tmp = 0;
1477 break; 1341 break;
1478 } 1342 }
1343
1479 if (mflags & P_IS_ALIVE) 1344 if (mflags & P_IS_ALIVE)
1480 {
1481 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above) 1345 for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
1482 if (QUERY_FLAG (tmp, FLAG_ALIVE)) 1346 if (QUERY_FLAG (tmp, FLAG_ALIVE))
1483 {
1484 found++;
1485 break;
1486 }
1487 if (found)
1488 break; 1347 break;
1489 }
1490 } 1348 }
1491 if (tmp == NULL) 1349
1350 if (!tmp)
1492 return find_arrow (op, type); 1351 return find_arrow (op, type);
1493 1352
1494 if (tmp->head) 1353 if (tmp->head)
1495 tmp = tmp->head; 1354 tmp = tmp->head;
1496 1355
1536 LOG (llevError, "Range: bow without activated bow (%s).\n", &op->name); 1395 LOG (llevError, "Range: bow without activated bow (%s).\n", &op->name);
1537 return 0; 1396 return 0;
1538 } 1397 }
1539 1398
1540 // optimisation: move object to top so we will find it quickly again 1399 // optimisation: move object to top so we will find it quickly again
1541 if (bow->below) 1400 splay (bow);
1542 {
1543 bow->remove ();
1544 op->insert (bow);
1545 }
1546
1547 } 1401 }
1548 1402
1549 if (!bow->race || !bow->skill) 1403 if (!bow->race || !bow->skill)
1550 { 1404 {
1551 new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s is broken.", &bow->name); 1405 new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s is broken.", &bow->name);
1577 } 1431 }
1578 1432
1579 /* this should not happen, but sometimes does */ 1433 /* this should not happen, but sometimes does */
1580 if (arrow->nrof == 0) 1434 if (arrow->nrof == 0)
1581 { 1435 {
1436 LOG (llevError | logBacktrace, "arrow (%s) has nrof 0\n", arrow->debug_desc ());
1582 arrow->destroy (); 1437 arrow->destroy ();
1583 return 0; 1438 return 0;
1584 } 1439 }
1585 1440
1586 left = arrow; /* these are arrows left to the player */ 1441 left = arrow; /* these are arrows left to the player */
1596 arrow->direction = dir; 1451 arrow->direction = dir;
1597 1452
1598 arrow->stats.sp = arrow->stats.wc; /* save original wc, dam, attacktype and slaying */ 1453 arrow->stats.sp = arrow->stats.wc; /* save original wc, dam, attacktype and slaying */
1599 arrow->stats.hp = arrow->stats.dam; 1454 arrow->stats.hp = arrow->stats.dam;
1600 arrow->stats.grace = arrow->attacktype; 1455 arrow->stats.grace = arrow->attacktype;
1601 1456 arrow->custom_name = arrow->slaying;
1602 if (arrow->slaying)
1603 arrow->spellarg = strdup (arrow->slaying);
1604 1457
1605#if 0 1458#if 0
1606 if (player *pl = op->contr) 1459 if (player *pl = op->contr)
1607 { 1460 {
1608 float speed = pl->weapon_sp; 1461 float speed = pl->weapon_sp;
1670 * but monsters can't. Putting that code here 1523 * but monsters can't. Putting that code here
1671 * makes the fire_bow code much cleaner. 1524 * makes the fire_bow code much cleaner.
1672 * this function should only be called if 'op' is a player, 1525 * this function should only be called if 'op' is a player,
1673 * hence the function name. 1526 * hence the function name.
1674 */ 1527 */
1675int 1528static int
1676player_fire_bow (object *op, int dir) 1529player_fire_bow (object *op, int dir)
1677{ 1530{
1678 int ret = 0, wcmod = 0; 1531 int ret;
1679 1532
1680 if (op->contr->bowtype == bow_bestarrow) 1533 if (op->contr->bowtype == bow_bestarrow)
1681 { 1534 {
1682 ret = fire_bow (op, op, pick_arrow_target (op, op->contr->ranged_ob->race, dir), dir, 0, op->x, op->y); 1535 ret = fire_bow (op, op, pick_arrow_target (op, op->contr->ranged_ob->race, dir), dir, 0, op->x, op->y);
1683 } 1536 }
1684 else if (op->contr->bowtype >= bow_n && op->contr->bowtype <= bow_nw) 1537 else if (op->contr->bowtype >= bow_n && op->contr->bowtype <= bow_nw)
1685 { 1538 {
1686 if (!similar_direction (dir, op->contr->bowtype - bow_n + 1)) 1539 int wcmod = similar_direction (dir, op->contr->bowtype - bow_n + 1) ? 0 : -1;
1687 wcmod = -1;
1688
1689 ret = fire_bow (op, op, NULL, op->contr->bowtype - bow_n + 1, wcmod, op->x, op->y); 1540 ret = fire_bow (op, op, NULL, op->contr->bowtype - bow_n + 1, wcmod, op->x, op->y);
1690 } 1541 }
1691 else if (op->contr->bowtype == bow_threewide) 1542 else if (op->contr->bowtype == bow_threewide)
1692 { 1543 {
1693 ret = fire_bow (op, op, NULL, dir, 0, op->x, op->y); 1544 ret = fire_bow (op, op, NULL, dir, 0, op->x, op->y);
1694 ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir + 2)], op->y + freearr_y[absdir (dir + 2)]); 1545 ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir + 2)], op->y + freearr_y[absdir (dir + 2)]);
1695 ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir - 2)], op->y + freearr_y[absdir (dir - 2)]); 1546 ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir - 2)], op->y + freearr_y[absdir (dir - 2)]);
1696 } 1547 }
1697 else if (op->contr->bowtype == bow_spreadshot) 1548 else if (op->contr->bowtype == bow_spreadshot)
1698 { 1549 {
1699 ret |= fire_bow (op, op, NULL, dir, 0, op->x, op->y); 1550 ret = fire_bow (op, op, NULL, dir, 0, op->x, op->y);
1700 ret |= fire_bow (op, op, NULL, absdir (dir - 1), -5, op->x, op->y); 1551 ret |= fire_bow (op, op, NULL, absdir (dir - 1), -5, op->x, op->y);
1701 ret |= fire_bow (op, op, NULL, absdir (dir + 1), -5, op->x, op->y); 1552 ret |= fire_bow (op, op, NULL, absdir (dir + 1), -5, op->x, op->y);
1702 } 1553 }
1703 else 1554 else
1704 { 1555 {
1710} 1561}
1711 1562
1712/* Fires a misc (wand/rod/horn) object in 'dir'. 1563/* Fires a misc (wand/rod/horn) object in 'dir'.
1713 * Broken apart from 'fire' to keep it more readable. 1564 * Broken apart from 'fire' to keep it more readable.
1714 */ 1565 */
1715void 1566static void
1716fire_misc_object (object *op, int dir) 1567fire_misc_object (object *op, int dir)
1717{ 1568{
1718 object *item = op->contr->ranged_ob; 1569 object *item = op->contr->ranged_ob;
1719 1570
1720 if (!item) 1571 if (!item)
1742 return; 1593 return;
1743 } 1594 }
1744 } 1595 }
1745 else if (item->type == ROD || item->type == HORN) 1596 else if (item->type == ROD || item->type == HORN)
1746 { 1597 {
1747 sint16 spell_sp = MAX (item->inv->stats.sp, item->inv->stats.grace); 1598 sint16 spell_sp = max (item->inv->stats.sp, item->inv->stats.grace);
1748 1599
1749 // using the maximum of the rods charge allows at least one spell cast 1600 // using the maximum of the rods charge allows at least one spell cast
1750 // for a rod or horn, this fixes some broken rods. 1601 // for a rod or horn, this fixes some broken rods.
1751 if (item->stats.hp < MIN (spell_sp, item->stats.maxhp)) 1602 if (item->stats.hp < min (spell_sp, item->stats.maxhp))
1752 { 1603 {
1753 op->contr->play_sound (sound_find ("wand_poof")); 1604 op->contr->play_sound (sound_find ("wand_poof"));
1754 1605
1755 if (item->type == ROD) 1606 if (item->type == ROD)
1756 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s whines for a while, but nothing happens.", query_base_name (item, 0)); 1607 new_draw_info_format (NDI_UNIQUE, 0, op, "The %s whines for a while, but nothing happens.", query_base_name (item, 0));
1761 } 1612 }
1762 } 1613 }
1763 1614
1764 if (cast_spell (op, item, dir, item->inv, NULL)) 1615 if (cast_spell (op, item, dir, item->inv, NULL))
1765 { 1616 {
1766 SET_FLAG (op, FLAG_BEEN_APPLIED); /* You now know something about it */ 1617 item->flag [FLAG_BEEN_APPLIED] = true; /* You now know something about it */
1618
1767 if (item->type == WAND) 1619 if (item->type == WAND)
1768 { 1620 {
1769 if (!(--item->stats.food)) 1621 if (!(--item->stats.food))
1770 { 1622 {
1771 object *tmp; 1623 object *tmp;
1791bool 1643bool
1792fire (object *op, int dir) 1644fire (object *op, int dir)
1793{ 1645{
1794 int spellcost = 0; 1646 int spellcost = 0;
1795 1647
1796 /* check for loss of invisiblity/hide */
1797 if (action_makes_visible (op))
1798 make_visible (op);
1799
1800 player *pl = op->contr; 1648 player *pl = op->contr;
1801 1649
1802 if (pl->golem) 1650 if (pl->golem)
1803 { 1651 {
1804 control_golem (op->contr->golem, dir); 1652 control_golem (op->contr->golem, dir);
1806 } 1654 }
1807 1655
1808 object *ob = pl->ranged_ob; 1656 object *ob = pl->ranged_ob;
1809 1657
1810 if (!ob) 1658 if (!ob)
1811 return false;
1812
1813 if (!op->change_weapon (ob))
1814 return false; 1659 return false;
1815 1660
1816 if (op->speed_left > 0.f) 1661 if (op->speed_left > 0.f)
1817 --op->speed_left; 1662 --op->speed_left;
1818 else 1663 else
1819 return false; 1664 return false;
1820 1665
1666 if (!op->change_weapon (ob))
1667 return false;
1668
1669 /* check for loss of invisiblity/hide */
1670 if (action_makes_visible (op))
1671 make_visible (op);
1672
1821 switch (ob->type) 1673 switch (ob->type)
1822 { 1674 {
1823 case BOW: 1675 case BOW:
1824 player_fire_bow (op, dir); 1676 player_fire_bow (op, dir);
1825 break; 1677 break;
1826 1678
1827 case SPELL: 1679 case SPELL:
1828 spellcost = cast_spell (op, op, dir, ob, pl->spellparam[0] ? pl->spellparam : 0); 1680 spellcost = cast_spell (op, op, dir, ob, *pl->spellparam ? pl->spellparam : 0);
1829 break; 1681 break;
1830 1682
1831 case BUILDER: 1683 case BUILDER:
1832 apply_map_builder (op, dir); 1684 apply_map_builder (op, dir);
1833 break; 1685 break;
1842 } 1694 }
1843 1695
1844 return true; 1696 return true;
1845} 1697}
1846 1698
1847/* find_key 1699static object *
1848 * We try to find a key for the door as passed. If we find a key
1849 * and successfully use it, we return the key, otherwise NULL
1850 * This function merges both normal and locked door, since the logic
1851 * for both is the same - just the specific key is different.
1852 * pl is the player,
1853 * inv is the objects inventory to searched
1854 * door is the door we are trying to match against.
1855 * This function can be called recursively to search containers.
1856 */
1857object *
1858find_key (object *pl, object *container, object *door) 1700find_key_ (object *pl, object *container, object *door)
1859{ 1701{
1860 object *tmp, *key; 1702 object *tmp, *key;
1861 1703
1862 /* Should not happen, but sanity checking is never bad */ 1704 /* Should not happen, but sanity checking is never bad */
1863 if (!container->inv) 1705 if (!container->inv)
1866 /* First, lets try to find a key in the top level inventory */ 1708 /* First, lets try to find a key in the top level inventory */
1867 for (tmp = container->inv; tmp; tmp = tmp->below) 1709 for (tmp = container->inv; tmp; tmp = tmp->below)
1868 { 1710 {
1869 if (door->type == DOOR && tmp->type == KEY) 1711 if (door->type == DOOR && tmp->type == KEY)
1870 break; 1712 break;
1713
1871 /* For sanity, we should really check door type, but other stuff 1714 /* For sanity, we should really check door type, but other stuff
1872 * (like containers) can be locked with special keys 1715 * (like containers) can be locked with special keys
1873 */ 1716 */
1874 if (tmp->slaying && tmp->type == SPECIAL_KEY && tmp->slaying == door->slaying) 1717 if (tmp->slaying && tmp->type == SPECIAL_KEY && tmp->slaying == door->slaying)
1875 break; 1718 break;
1881 * a key, return 1724 * a key, return
1882 */ 1725 */
1883 if (!tmp) 1726 if (!tmp)
1884 { 1727 {
1885 for (tmp = container->inv; tmp; tmp = tmp->below) 1728 for (tmp = container->inv; tmp; tmp = tmp->below)
1886 {
1887 /* No reason to search empty containers */ 1729 /* No reason to search empty containers */
1888 if (tmp->type == CONTAINER && tmp->inv) 1730 if (tmp->type == CONTAINER && tmp->inv)
1889 {
1890 if ((key = find_key (pl, tmp, door))) 1731 if ((key = find_key_ (pl, tmp, door)))
1891 return key; 1732 return key;
1892 }
1893 }
1894 1733
1895 if (!tmp) 1734 if (!tmp)
1896 return NULL; 1735 return 0;
1897 } 1736 }
1898 1737
1899 /* We get down here if we have found a key. Now if its in a container, 1738 /* We get down here if we have found a key. Now if its in a container,
1900 * see if we actually want to use it 1739 * see if we actually want to use it
1901 */ 1740 */
1902 if (pl != container) 1741 if (pl != container)
1903 { 1742 {
1904 /* Only let players use keys in containers */ 1743 /* Only let players use keys in containers */
1905 if (!pl->contr) 1744 if (!pl->contr)
1906 return NULL; 1745 return 0;
1746
1907 /* cases where this fails: 1747 /* cases where this fails:
1908 * If we only search the player inventory, return now since we 1748 * If we only search the player inventory, return now since we
1909 * are not in the players inventory. 1749 * are not in the players inventory.
1910 * If the container is not active, return now since only active 1750 * If the container is not active, return now since only active
1911 * containers can be used. 1751 * containers can be used.
1915 * inv must have been an container and must have been active. 1755 * inv must have been an container and must have been active.
1916 * 1756 *
1917 * Change the color so that the message doesn't disappear with 1757 * Change the color so that the message doesn't disappear with
1918 * all the others. 1758 * all the others.
1919 */ 1759 */
1920 if (pl->contr->usekeys == key_inventory || 1760 if (pl->contr->usekeys == key_inventory
1921 !QUERY_FLAG (container, FLAG_APPLIED) || 1761 || !QUERY_FLAG (container, FLAG_APPLIED)
1922 (pl->contr->usekeys == keyrings && (!container->race || strcmp (container->race, "keys")))) 1762 || (pl->contr->usekeys == keyrings && container->race != shstr_keys))
1923 { 1763 {
1924 new_draw_info_format (NDI_UNIQUE | NDI_BROWN, 0, pl, 1764 new_draw_info_format (NDI_UNIQUE | NDI_BROWN, 0, pl,
1925 "The %s in your %s vibrates as you approach the door", query_name (tmp), query_name (container)); 1765 "The %s in your %s vibrates as you approach the door", query_name (tmp), query_name (container));
1926 return NULL; 1766 return NULL;
1927 } 1767 }
1928 } 1768 }
1929 1769
1930 return tmp; 1770 return tmp;
1771}
1772
1773/* find_key
1774 * We try to find a key for the door as passed. If we find a key
1775 * and successfully use it, we return the key, otherwise NULL
1776 * This function merges both normal and locked door, since the logic
1777 * for both is the same - just the specific key is different.
1778 * pl is the player,
1779 * inv is the objects inventory to searched
1780 * door is the door we are trying to match against.
1781 * This function can be called recursively to search containers.
1782 */
1783object *
1784find_key (object *pl, object *container, object *door)
1785{
1786 if (door->slaying && is_match_expr (door->slaying))
1787 {
1788 // for match expressions, we try to find the key by applying the match
1789 // to the op itself, which is supposed to find the "key", instead
1790 // of searching through containers ourselves.
1791
1792 return match_one (door->slaying, container, door, pl, pl);
1793 }
1794 else
1795 return find_key_ (pl, container, door);
1931} 1796}
1932 1797
1933/* moved door processing out of move_player_attack. 1798/* moved door processing out of move_player_attack.
1934 * returns 1 if player has opened the door with a key 1799 * returns 1 if player has opened the door with a key
1935 * such that the caller should not do anything more, 1800 * such that the caller should not do anything more,
1985 * going to try and move (not fire weapons). 1850 * going to try and move (not fire weapons).
1986 */ 1851 */
1987bool 1852bool
1988move_player_attack (object *op, int dir) 1853move_player_attack (object *op, int dir)
1989{ 1854{
1855 if (!op->contr->braced && op->speed_left > 0.f && move_ob (op, dir, op))
1856 {
1857 --op->speed_left;
1858 return true;
1859 }
1860
1990 int on_battleground; 1861 int on_battleground;
1991 1862
1992 sint16 nx = freearr_x[dir] + op->x; 1863 sint16 nx = freearr_x[dir] + op->x;
1993 sint16 ny = freearr_y[dir] + op->y; 1864 sint16 ny = freearr_y[dir] + op->y;
1994 1865
1995 on_battleground = op_on_battleground (op, 0, 0); 1866 on_battleground = op_on_battleground (op, 0, 0);
1996 1867
1997 if (out_of_map (op->map, nx, ny)) 1868 if (out_of_map (op->map, nx, ny))
1998 return false; 1869 return false;
1999
2000 if (!op->contr->braced && op->speed_left > 0.f && move_ob (op, dir, op))
2001 {
2002 --op->speed_left;
2003 return true;
2004 }
2005 1870
2006 /* If braced, or can't move to the square, and it is not out of the 1871 /* If braced, or can't move to the square, and it is not out of the
2007 * map, attack it. Note order of if statement is important - don't 1872 * map, attack it. Note order of if statement is important - don't
2008 * want to be calling move_ob if braced, because move_ob will move the 1873 * want to be calling move_ob if braced, because move_ob will move the
2009 * player. This is a pretty nasty hack, because if we could 1874 * player. This is a pretty nasty hack, because if we could
2069 --op->speed_left; 1934 --op->speed_left;
2070 1935
2071 op->play_sound (sound_find ("push_player")); 1936 op->play_sound (sound_find ("push_player"));
2072 push_ob (mon, dir, op); 1937 push_ob (mon, dir, op);
2073 1938
2074 if (op->contr->tmp_invis || op->hide) 1939 if (action_makes_visible (op))
2075 make_visible (op); 1940 make_visible (op);
2076 1941
2077 return true; 1942 return true;
2078 } 1943 }
2079 else 1944 else
2101 push_ob (mon, dir, op); 1966 push_ob (mon, dir, op);
2102 } 1967 }
2103 else 1968 else
2104 op->statusmsg ("You withhold your attack"); 1969 op->statusmsg ("You withhold your attack");
2105 1970
2106 if (op->contr->tmp_invis || op->hide) 1971 if (op->contr->tmp_invis || op->flag [FLAG_HIDDEN])
2107 make_visible (op); 1972 make_visible (op);
2108 1973
2109 return true; 1974 return true;
2110 } 1975 }
2111 } 1976 }
2151} 2016}
2152 2017
2153bool 2018bool
2154move_player (object *op, int dir) 2019move_player (object *op, int dir)
2155{ 2020{
2156 int pick;
2157
2158 if (!op->map || op->map->in_memory != MAP_ACTIVE) 2021 if (!op->map || op->map->in_memory != MAP_ACTIVE)
2159 return 0; 2022 return 0;
2160 2023
2161 /* Sanity check: make sure dir is valid */ 2024 /* Sanity check: make sure dir is valid */
2162 if ((dir < 0) || (dir >= 9)) 2025 if ((dir < 0) || (dir >= 9))
2169 if (QUERY_FLAG (op, FLAG_CONFUSED) && dir) 2032 if (QUERY_FLAG (op, FLAG_CONFUSED) && dir)
2170 dir = absdir (dir + rndm (3) + rndm (3) - 2); 2033 dir = absdir (dir + rndm (3) + rndm (3) - 2);
2171 2034
2172 op->facing = dir; 2035 op->facing = dir;
2173 2036
2174 if (op->hide) 2037 if (op->flag [FLAG_HIDDEN])
2175 do_hidden_move (op); 2038 do_hidden_move (op);
2176 2039
2177 bool retval; 2040 bool retval;
2041 int pick = 0;
2178 2042
2179 if (INVOKE_PLAYER (MOVE, op->contr, ARG_INT (dir))) 2043 if (INVOKE_PLAYER (MOVE, op->contr, ARG_INT (dir)))
2180 retval = RESULT_INT (0); 2044 retval = RESULT_INT (0);
2181 else if (op->contr->fire_on) 2045 else if (op->contr->fire_on)
2182 retval = fire (op, dir); 2046 retval = fire (op, dir);
2239 return move_player (op, op->direction); 2103 return move_player (op, op->direction);
2240 2104
2241 return false; 2105 return false;
2242} 2106}
2243 2107
2244int 2108static int
2245save_life (object *op) 2109save_life (object *op)
2246{ 2110{
2247 if (!QUERY_FLAG (op, FLAG_LIFESAVE)) 2111 if (!QUERY_FLAG (op, FLAG_LIFESAVE))
2248 return 0; 2112 return 0;
2249 2113
2296void 2160void
2297object::drop_unpaid_items () 2161object::drop_unpaid_items ()
2298{ 2162{
2299 if (!flag [FLAG_REMOVED]) 2163 if (!flag [FLAG_REMOVED])
2300 ::drop_unpaid_items (inv, this); 2164 ::drop_unpaid_items (inv, this);
2301}
2302
2303/*
2304 * Returns pointer a static string containing gravestone text
2305 * Moved from apply.c to player.c - player.c is what
2306 * actually uses this function. player.c may not be quite the
2307 * best, a misc file for object actions is probably better,
2308 * but there isn't one in the server directory.
2309 */
2310const char *
2311gravestone_text (object *op)
2312{
2313 static dynbuf_text buf;
2314
2315 buf << "---- R.I.P. ----\n\n";
2316 op->name;
2317
2318 if (op->type == PLAYER)
2319 buf << " the " << op->contr->title;
2320
2321 buf << "\n\n";
2322
2323 buf << "who was level ";
2324 buf << (sint32)op->level << "\n\n" // OO breakdown
2325 << " when " << (op->contr ? "killed" : "died") << "\n\n";
2326
2327 if (op->type == PLAYER)
2328 buf << "by " << op->contr->killer_name () << ".\n\n";
2329
2330 {
2331 static char buf2[128];
2332 time_t now = time (NULL);
2333 strftime (buf2, 128, "%b %d %Y\n\n", localtime (&now));
2334 buf << buf2;
2335 }
2336
2337 return buf;
2338} 2165}
2339 2166
2340void 2167void
2341do_some_living (object *op) 2168do_some_living (object *op)
2342{ 2169{
2497 { 2324 {
2498 over_hp = (gen_hp < 20 ? 30 : gen_hp + 10) / rate_hp; 2325 over_hp = (gen_hp < 20 ? 30 : gen_hp + 10) / rate_hp;
2499 2326
2500 if (over_hp > 0) 2327 if (over_hp > 0)
2501 { 2328 {
2502 op->stats.sp += over_hp + (RANDOM () % rate_hp > ((gen_hp < 20 ? 30 : gen_hp + 10) % rate_hp)) ? -1 : 0; 2329 op->stats.sp += over_hp + (rndm (rate_hp) > ((gen_hp < 20 ? 30 : gen_hp + 10) % rate_hp)) ? -1 : 0;
2503 op->last_heal = 0; 2330 op->last_heal = 0;
2504 } 2331 }
2505 else 2332 else
2506 op->last_heal = rate_hp / (gen_hp < 20 ? 30 : gen_hp + 10); 2333 op->last_heal = rate_hp / (gen_hp < 20 ? 30 : gen_hp + 10);
2507 } 2334 }
2563 2390
2564 if (op->stats.food < 0) 2391 if (op->stats.food < 0)
2565 { 2392 {
2566 op->stats.hp += op->stats.food; 2393 op->stats.hp += op->stats.food;
2567 op->stats.food = 0; 2394 op->stats.food = 0;
2395
2396 if (op->stats.hp < 0)
2397 {
2398 op->contr->killer = archetype::get ("killer_starvation");
2399 op->contr->killer->destroy ();
2568 } 2400 }
2401 }
2569 2402
2403 /* killer should be set here already */
2570 if (op->stats.hp < 0 && !QUERY_FLAG (op, FLAG_WIZ)) 2404 if (op->stats.hp < 0 && !QUERY_FLAG (op, FLAG_WIZ))
2571 kill_player (op); 2405 kill_player (op);
2572 } 2406 }
2573} 2407}
2574 2408
2579 */ 2413 */
2580void 2414void
2581kill_player (object *op) 2415kill_player (object *op)
2582{ 2416{
2583 int x, y; 2417 int x, y;
2584 char buf[MAX_BUF];
2585 maptile *map; /* this is for resurrection */ 2418 maptile *map; /* this is for resurrection */
2586 int will_kill_again; 2419 int will_kill_again;
2587 archetype *at; 2420 archetype *at;
2588 object *tmp; 2421 object *tmp;
2589 2422
2590 if (save_life (op)) 2423 if (save_life (op))
2591 return; 2424 return;
2425
2426 dynbuf_text deathtab;
2427
2428 /* restore player */
2429 at = archetype::find (shstr_poisoning);
2430 if (object *tmp = present_arch_in_ob (at, op))
2431 {
2432 tmp->destroy ();
2433 deathtab << "Your body feels cleansed...\r";
2434 }
2435
2436 at = archetype::find (shstr_confusion);
2437 if (object *tmp = present_arch_in_ob (at, op))
2438 {
2439 tmp->destroy ();
2440 deathtab << "Your mind feels clearer...\r";
2441 }
2442
2443 cure_disease (op, 0, 0); /* remove any disease */
2444
2445 max_it (op->stats.hp , op->stats.maxhp);
2446 max_it (op->stats.sp , op->stats.maxsp);
2447 max_it (op->stats.grace, op->stats.maxgrace);
2448
2449 if (op->stats.food <= 0)
2450 op->stats.food = 999;
2451
2452 // remove all spell effects that are active
2453 // to avoid long-term effects such as word-of-recall
2454 for (object *item = op->inv; item; )
2455 {
2456 object *next = item->below;
2457
2458 if (item->type == SPELL_EFFECT && item->active)
2459 item->destroy ();
2460
2461 item = next;
2462 }
2592 2463
2593 /* If player dies on BATTLEGROUND, no stat/exp loss! For Combat-Arenas 2464 /* If player dies on BATTLEGROUND, no stat/exp loss! For Combat-Arenas
2594 * in cities ONLY!!! It is very important that this doesn't get abused. 2465 * in cities ONLY!!! It is very important that this doesn't get abused.
2595 * Look at op_on_battleground() for more info --AndreasV 2466 * Look at op_on_battleground() for more info --AndreasV
2596 */ 2467 */
2597 if (op_on_battleground (op, &x, &y)) 2468 if (op_on_battleground (op, &x, &y))
2598 { 2469 {
2599 new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, op, "You have been defeated in combat!"); 2470 deathtab << "You almost died in combat, but local medics have saved your life...\r";
2600 new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, op, "Local medics have saved your life...");
2601
2602 /* restore player */
2603 at = archetype::find ("poisoning");
2604 if (object *tmp = present_arch_in_ob (at, op))
2605 {
2606 tmp->destroy ();
2607 new_draw_info (NDI_UNIQUE, 0, op, "Your body feels cleansed");
2608 }
2609
2610 at = archetype::find ("confusion");
2611 if (object *tmp = present_arch_in_ob (at, op))
2612 {
2613 tmp->destroy ();
2614 new_draw_info (NDI_UNIQUE, 0, tmp, "Your mind feels clearer");
2615 }
2616
2617 cure_disease (op, 0, 0); /* remove any disease */
2618 op->stats.hp = op->stats.maxhp;
2619 if (op->stats.food <= 0)
2620 op->stats.food = 999;
2621 2471
2622 /* create a bodypart-trophy to make the winner happy */ 2472 /* create a bodypart-trophy to make the winner happy */
2623 if (object *tmp = arch_to_object (archetype::find ("finger"))) 2473 if (object *tmp = arch_to_object (archetype::find (shstr_finger)))
2624 { 2474 {
2625 tmp->name = format ("%s's finger" , &op->name); 2475 tmp->name = format ("%s's finger" , &op->name);
2626 tmp->name_pl = format ("%s's fingers", &op->name); 2476 tmp->name_pl = format ("%s's fingers", &op->name);
2627 tmp->msg = format ( 2477 tmp->msg = format (
2628 "This finger has been cut off of %s the %s, when he was defeated at level %d by %s.\n", 2478 "This finger has been cut off of %s the %s, when he was defeated at level %d by %s.\n",
2629 &op->name, op->contr->title, 2479 &op->name, op->contr->title,
2630 (int)op->level, 2480 (int)op->level,
2631 op->contr->killer_name () 2481 op->contr->killer_name ()
2632 ); 2482 );
2633 tmp->value = 0, tmp->type = 0; 2483 tmp->value = 0, tmp->type = 0;
2634 tmp->materialname = "organics"; 2484 tmp->material = name_to_material (shstr_organic);
2635 tmp->insert_at (op, tmp); 2485 tmp->insert_at (op, tmp);
2636 } 2486 }
2637 2487
2638 /* teleport defeated player to new destination */ 2488 /* teleport defeated player to new destination */
2639 transfer_ob (op, x, y, 0, NULL); 2489 transfer_ob (op, x, y, 0, NULL);
2640 op->contr->braced = 0; 2490 op->contr->braced = 0;
2491
2492 op->contr->infobox (MSG_CHANNEL ("death"), deathtab);
2641 return; 2493 return;
2642 } 2494 }
2643 2495
2496 deathtab << "You were killed by " << op->contr->killer_name () << ".\n\n";
2497 deathtab << "T<YOU HAVE DIED>\n\n";
2498
2644 INVOKE_PLAYER (DEATH, op->contr); 2499 INVOKE_PLAYER (DEATH, op->contr);
2645 2500
2646 command_kill_pets (op, 0); 2501 command_kill_pets (op, 0);
2647
2648 if (op->stats.food < 0)
2649 {
2650 op->contr->killer = archetype::get ("killer_starvation");
2651 op->contr->killer->destroy ();
2652 }
2653 2502
2654 op->contr->play_sound (sound_find ("player_dies")); 2503 op->contr->play_sound (sound_find ("player_dies"));
2655 2504
2656 /* save the map location for corpse, gravestone */ 2505 /* save the map location for corpse, gravestone */
2657 x = op->x; 2506 x = op->x;
2687 2536
2688 lost_a_stat = 0; 2537 lost_a_stat = 0;
2689 2538
2690 for (z = 0; z < num_stats_lose; z++) 2539 for (z = 0; z < num_stats_lose; z++)
2691 { 2540 {
2692 i = RANDOM () % NUM_STATS; 2541 i = rndm (NUM_STATS);
2693 2542
2694 if (settings.stat_loss_on_death) 2543 if (settings.stat_loss_on_death)
2695 { 2544 {
2696 /* Pick a random stat and take a point off it. Tell the player 2545 /* Pick a random stat and take a point off it. Tell the player
2697 * what he lost. 2546 * what he lost.
2704 lost_a_stat = 1; 2553 lost_a_stat = 1;
2705 } 2554 }
2706 else 2555 else
2707 { 2556 {
2708 /* deplete a stat */ 2557 /* deplete a stat */
2709 archetype *deparch = archetype::find ("depletion"); 2558 archetype *deparch = archetype::find (shstr_depletion);
2710 object *dep; 2559 object *dep;
2711 2560
2712 dep = present_arch_in_ob (deparch, op); 2561 dep = present_arch_in_ob (deparch, op);
2713 if (!dep) 2562 if (!dep)
2714 { 2563 {
2748 } 2597 }
2749 } 2598 }
2750 2599
2751 if (lose_this_stat) 2600 if (lose_this_stat)
2752 { 2601 {
2753 this_stat = get_attr_value (&(dep->stats), i); 2602 this_stat = get_attr_value (&dep->stats, i);
2754 /* We could try to do something clever like find another 2603 /* We could try to do something clever like find another
2755 * stat to reduce if this fails. But chances are, if 2604 * stat to reduce if this fails. But chances are, if
2756 * stats have been depleted to -50, all are pretty low 2605 * stats have been depleted to -50, all are pretty low
2757 * and should be roughly the same, so it shouldn't make a 2606 * and should be roughly the same, so it shouldn't make a
2758 * difference. 2607 * difference.
2772 /* If no stat lost, tell the player. */ 2621 /* If no stat lost, tell the player. */
2773 if (!lost_a_stat) 2622 if (!lost_a_stat)
2774 { 2623 {
2775 /* determine_god() seems to not work sometimes... why is this? 2624 /* determine_god() seems to not work sometimes... why is this?
2776 Should I be using something else? GD */ 2625 Should I be using something else? GD */
2777 const char *god = determine_god (op); 2626 shstr_tmp god = determine_god (op);
2778 2627
2779 if (god && (strcmp (god, "none"))) 2628 if (god != shstr_none)
2780 new_draw_info_format (NDI_UNIQUE, 0, op, "For a brief moment you feel the holy presence of %s protecting" " you.", god); 2629 deathtab << "For a brief moment you feel the holy presence of " << god << " protecting you.\r";
2781 else 2630 else
2782 new_draw_info (NDI_UNIQUE, 0, op, "For a brief moment you feel a holy presence protecting you."); 2631 deathtab << "For a brief moment you feel a holy presence protecting you.\r";
2783 } 2632 }
2784#else 2633#else
2785 new_draw_info (NDI_UNIQUE, 0, op, "For a brief moment you feel a holy presence protecting you from losing yourself completely."); 2634 deathtab << "For a brief moment you feel a holy presence protecting you from losing yourself completely.";
2786#endif 2635#endif
2787 2636
2788 /* Put a gravestone up where the character 'almost' died. List the 2637 /* Put a gravestone up where the character 'almost' died. List the
2789 * exp loss on the stone. 2638 * exp loss on the stone.
2790 */ 2639 */
2791 tmp = arch_to_object (archetype::find ("gravestone")); 2640 tmp = arch_to_object (archetype::find (shstr_gravestone));
2792 tmp->name = format ("%s's gravestone", &op->name); 2641 tmp->name = format ("%s's gravestone", &op->name);
2793 tmp->name_pl = format ("%s's gravestones", &op->name); 2642 tmp->name_pl = format ("%s's gravestones", &op->name);
2794 tmp->msg = format ("RIP\nHere rests the hero %s the %s,\nwho was killed\nby %s.\n", &op->name, op->contr->title, op->contr->killer_name ()); 2643 tmp->msg = format ("T<RIP>\n\nHere rests the hero %s the %s,\rwho was killed\rby %s.\n",
2644 &op->name, op->contr->title, op->contr->killer_name ());
2795 tmp->x = op->x, tmp->y = op->y; 2645 tmp->x = op->x, tmp->y = op->y;
2796 insert_ob_in_map (tmp, op->map, NULL, 0); 2646 insert_ob_in_map (tmp, op->map, NULL, 0);
2797 2647
2798 /**************************************/ 2648 /**************************************/
2799 /* */ 2649 /* */
2800 /* Subtract the experience points, */ 2650 /* Subtract the experience points, */
2801 /* if we died cause of food, give us */
2802 /* food, and reset HP's... */
2803 /* */ 2651 /* */
2804 /**************************************/ 2652 /**************************************/
2805 2653
2806 /* remove any poisoning and confusion the character may be suffering. */
2807 /* restore player */
2808 at = archetype::find ("poisoning");
2809 tmp = present_arch_in_ob (at, op);
2810
2811 if (tmp)
2812 {
2813 tmp->destroy ();
2814 new_draw_info (NDI_UNIQUE, 0, op, "Your body feels cleansed");
2815 }
2816
2817 at = archetype::find ("confusion");
2818 tmp = present_arch_in_ob (at, op);
2819 if (tmp)
2820 {
2821 tmp->destroy ();
2822 new_draw_info (NDI_UNIQUE, 0, tmp, "Your mind feels clearer");
2823 }
2824
2825 cure_disease (op, 0, 0); /* remove any disease */
2826
2827 // remove all spell effects that are active
2828 // to avoid long-term effects such as word-of-recall
2829 for (object *item = op->inv; item; )
2830 {
2831 object *next = item->below;
2832
2833 if (item->type == SPELL_EFFECT && item->active)
2834 item->destroy ();
2835
2836 item = next;
2837 }
2838
2839 /*add_exp(op, (op->stats.exp * -0.20)); */ 2654 /*add_exp(op, (op->stats.exp * -0.20)); */
2840 apply_death_exp_penalty (op); 2655 apply_death_exp_penalty (op);
2841
2842 if (op->stats.food < 100)
2843 op->stats.food = 900;
2844
2845 op->stats.hp = op->stats.maxhp;
2846 op->stats.sp = MAX (op->stats.sp, op->stats.maxsp);
2847 op->stats.grace = MAX (op->stats.grace, op->stats.maxgrace);
2848 2656
2849 /* 2657 /*
2850 * Check to see if the player has any unpaid items. If so, remove them 2658 * Check to see if the player has any unpaid items. If so, remove them
2851 * and put them back in the map. 2659 * and put them back in the map.
2852 */ 2660 */
2887 if (will_kill_again & (1 << at)) 2695 if (will_kill_again & (1 << at))
2888 force->resist[at] = 100; 2696 force->resist[at] = 100;
2889 2697
2890 insert_ob_in_ob (force, op); 2698 insert_ob_in_ob (force, op);
2891 op->update_stats (); 2699 op->update_stats ();
2892
2893 } 2700 }
2894 2701
2895 new_draw_info (NDI_UNIQUE, 0, op, "YOU HAVE DIED."); 2702 op->contr->infobox (MSG_CHANNEL ("death"), deathtab);
2896} 2703}
2897 2704
2898void 2705static void
2899loot_object (object *op) 2706loot_object (object *op)
2900{ /* Grab and destroy some treasure */ 2707{ /* Grab and destroy some treasure */
2901 object *tmp, *tmp2, *next; 2708 object *tmp, *tmp2, *next;
2902 2709
2903 op->close_container (); /* close open sack first */ 2710 op->close_container (); /* close open sack first */
2998} 2805}
2999 2806
3000void 2807void
3001make_visible (object *op) 2808make_visible (object *op)
3002{ 2809{
3003 op->hide = 0; 2810 op->flag [FLAG_HIDDEN] = 0;
3004 op->invisible = 0; 2811 op->invisible = 0;
3005 2812
3006 if (op->type == PLAYER) 2813 if (op->type == PLAYER)
3007 { 2814 {
3008 op->contr->tmp_invis = 0; 2815 op->contr->tmp_invis = 0;
3033 2840
3034 if (!ob || !ob->map) 2841 if (!ob || !ob->map)
3035 return 0; 2842 return 0;
3036 2843
3037 /* so, on normal lighted maps, its hard to hide */ 2844 /* so, on normal lighted maps, its hard to hide */
3038 level = ob->map->darkness - 2; 2845 level = ob->map->darklevel () - 2;
3039 2846
3040 /* this also picks up whether the object is glowing. 2847 /* this also picks up whether the object is glowing.
3041 * If you carry a light on a non-dark map, its not 2848 * If you carry a light on a non-dark map, its not
3042 * as bad as carrying a light on a pitch dark map */ 2849 * as bad as carrying a light on a pitch dark map */
3043 if (has_carried_lights (ob)) 2850 if (ob->has_carried_lights ())
3044 level = -(10 + (2 * ob->map->darkness)); 2851 level = -(10 + (2 * ob->map->darklevel ()));
3045 2852
3046 /* scan through all nearby squares for terrain to hide in */ 2853 /* scan through all nearby squares for terrain to hide in */
3047 for (i = 0, x = ob->x, y = ob->y; 2854 for (i = 0, x = ob->x, y = ob->y;
3048 i <= SIZEOFFREE1; 2855 i <= SIZEOFFREE1;
3049 i++, x = ob->x + freearr_x[i], y = ob->y + freearr_y[i]) 2856 i++, x = ob->x + freearr_x[i], y = ob->y + freearr_y[i])
3196 while (op) 3003 while (op)
3197 { 3004 {
3198 dx = rv.distance_x + op->arch->x; 3005 dx = rv.distance_x + op->arch->x;
3199 dy = rv.distance_y + op->arch->y; 3006 dy = rv.distance_y + op->arch->y;
3200 3007
3201 /* only the viewable area the player sees is updated by LOS 3008 if (pl->contr->blocked_los (dx, dy) != LOS_BLOCKED)
3202 * code, so we need to restrict ourselves to that range of values
3203 * for any meaningful values.
3204 */
3205 if (abs (dx) <= (pl->contr->ns->mapx / 2) &&
3206 abs (dy) <= (pl->contr->ns->mapy / 2) &&
3207 !pl->contr->blocked_los[dx + (pl->contr->ns->mapx / 2)][dy + (pl->contr->ns->mapy / 2)])
3208 return 1; 3009 return 1;
3209 3010
3210 op = op->more; 3011 op = op->more;
3211 }
3212
3213 return 0;
3214}
3215
3216/* routine for both players and monsters. We call this when
3217 * there is a possibility for our action distrubing our hiding
3218 * place or invisiblity spell. Artefact invisiblity is not
3219 * effected by this. If we arent invisible to begin with, we
3220 * return 0.
3221 */
3222int
3223action_makes_visible (object *op)
3224{
3225 if (op->invisible && QUERY_FLAG (op, FLAG_ALIVE))
3226 {
3227 if (QUERY_FLAG (op, FLAG_MAKE_INVIS))
3228 return 0;
3229
3230 if (op->contr && op->contr->tmp_invis == 0)
3231 return 0;
3232
3233 /* If monsters, they should become visible */
3234 if (op->hide || !op->contr || (op->contr && op->contr->tmp_invis))
3235 {
3236 new_draw_info_format (NDI_UNIQUE, 0, op, "You become %s!", op->hide ? "unhidden" : "visible");
3237 return 1;
3238 }
3239 } 3012 }
3240 3013
3241 return 0; 3014 return 0;
3242} 3015}
3243 3016
3468 3241
3469 if (pl->ranged_ob == ob) 3242 if (pl->ranged_ob == ob)
3470 pl->ranged_ob = 0; 3243 pl->ranged_ob = 0;
3471} 3244}
3472 3245
3246//-GPL
3247
3473sint8 3248sint8
3474player::visibility_at (maptile *map, int x, int y) const 3249player::darkness_at (maptile *map, int x, int y) const
3475{ 3250{
3476 if (!ns) 3251 if (!ns)
3477 return 0; 3252 return LOS_BLOCKED;
3478 3253
3479 int dx, dy; 3254 int dx, dy;
3480 if (!adjacent_map (map, ns->current_map, &dx, &dy)) 3255 if (!adjacent_map (map, ns->current_map, &dx, &dy))
3481 return 0; 3256 return LOS_BLOCKED;
3482 3257
3483 x += dx - ns->current_x + ns->mapx / 2; 3258 x += dx - ns->current_x;
3484 y += dy - ns->current_y + ns->mapy / 2; 3259 y += dy - ns->current_y;
3485 3260
3486 if (!IN_RANGE_EXC (x, 0, ns->mapx) || !IN_RANGE_EXC (y, 0, ns->mapy))
3487 return 0;
3488
3489 return 100 - blocked_los [x][y]; 3261 return blocked_los (x, y);
3490} 3262}
3491 3263
3492void 3264void
3493player::infobox (const char *title, const char *msg, int color) 3265player::infobox (const char *title, const char *msg, int color)
3494{ 3266{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines