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

Comparing deliantra/server/common/object.C (file contents):
Revision 1.340 by root, Fri Jul 2 19:54:41 2010 UTC vs.
Revision 1.366 by root, Sat Nov 17 23:40:00 2018 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 9 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the 10 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your 11 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the Affero GNU General Public License 19 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see 20 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 21 * <http://www.gnu.org/licenses/>.
21 * 22 *
22 * The authors can be reached via e-mail to <support@deliantra.net> 23 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 24 */
24 25
25#include <global.h> 26#include <global.h>
26#include <stdio.h> 27#include <stdio.h>
45uint32_t object::create_count; 46uint32_t object::create_count;
46uint32_t object::destroy_count; 47uint32_t object::destroy_count;
47 48
48//+GPL 49//+GPL
49 50
50short freearr_x[SIZEOFFREE] = { 51int freearr_x[SIZEOFFREE] = {
51 0, 52 0,
52 0, 1, 1, 1, 0, -1, -1, -1, 53 0, 1, 1, 1, 0, -1, -1, -1,
53 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 54 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1,
54 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 55 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
55}; 56};
56short freearr_y[SIZEOFFREE] = { 57int freearr_y[SIZEOFFREE] = {
57 0, 58 0,
58 -1, -1, 0, 1, 1, 1, 0, -1, 59 -1, -1, 0, 1, 1, 1, 0, -1,
59 -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, 60 -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2,
60 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3 61 -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3
61}; 62};
72 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 48, 73 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45, 48,
73 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49 74 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
74}; 75};
75 76
76const char *wall_suffix[16] = { 77const char *wall_suffix[16] = {
77 "0", 78 "0",
78 "1_3", 79 "1_3",
79 "1_4", 80 "1_4",
80 "2_1_2", 81 "2_1_2",
81 "1_2", 82 "1_2",
82 "2_2_4", 83 "2_2_4",
83 "2_2_1", 84 "2_2_1",
84 "3_1", 85 "3_1",
85 "1_1", 86 "1_1",
86 "2_2_3", 87 "2_2_3",
87 "2_2_2", 88 "2_2_2",
88 "3_3", 89 "3_3",
89 "2_1_1", 90 "2_1_1",
90 "3_4", 91 "3_4",
91 "3_2", 92 "3_2",
92 "4" 93 "4"
93}; 94};
94 95
95static void 96static void
96write_uuid (uval64 skip, bool sync) 97write_uuid (uval64 skip, bool sync)
97{ 98{
233 234
234/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 235/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
235static bool 236static bool
236compare_ob_value_lists_one (const object *wants, const object *has) 237compare_ob_value_lists_one (const object *wants, const object *has)
237{ 238{
238 /* n-squared behaviour (see kv_get), but I'm hoping both 239 /* n-squared behaviour (see kv.get), but I'm hoping both
239 * objects with lists are rare, and lists stay short. If not, use a 240 * objects with lists are rare, and lists stay short. If not, use a
240 * different structure or at least keep the lists sorted... 241 * different structure or at least keep the lists sorted...
241 */ 242 */
242 243
243 /* For each field in wants, */ 244 /* For each field in wants, */
244 for (key_value *kv = wants->key_values; kv; kv = kv->next) 245 for (key_value *kv = wants->kv.first; kv; kv = kv->next)
245 if (has->kv_get (kv->key) != kv->value) 246 if (has->kv.get (kv->key) != kv->value)
246 return false; 247 return false;
247 248
248 /* If we get here, every field in wants has a matching field in has. */ 249 /* If we get here, every field in wants has a matching field in has. */
249 return true; 250 return true;
250} 251}
274 */ 275 */
275bool object::can_merge_slow (object *ob1, object *ob2) 276bool object::can_merge_slow (object *ob1, object *ob2)
276{ 277{
277 /* A couple quick sanity checks */ 278 /* A couple quick sanity checks */
278 if (ob1 == ob2 279 if (ob1 == ob2
279 || ob1->type != ob2->type 280 || ob1->type != ob2->type
280 || ob1->value != ob2->value 281 || ob1->value != ob2->value
281 || ob1->name != ob2->name 282 || ob1->name != ob2->name
283 || ob1->custom_name != ob2->custom_name
282 || fabs (ob1->speed - ob2->speed) >= MIN_ACTIVE_SPEED) 284 || fabs (ob1->speed - ob2->speed) >= MIN_ACTIVE_SPEED)
283 return 0; 285 return 0;
284 286
285 /* Do not merge objects if nrof would overflow, assume nrof 287 /* Do not merge objects if nrof would overflow, assume nrof
286 * is always 0 .. 2**31-1 */ 288 * is always 0 .. 2**31-1 */
350 /* inventory ok - still need to check rest of this object to see 352 /* inventory ok - still need to check rest of this object to see
351 * if it is valid. 353 * if it is valid.
352 */ 354 */
353 } 355 }
354 356
355 /* Don't merge objects that are applied. With the new 'body' code, 357 /* Don't merge objects that are applied. With the new 'body' code,
356 * it is possible for most any character to have more than one of 358 * it is possible for most any character to have more than one of
357 * some items equipped, and we don't want those to merge. 359 * some items equipped, and we don't want those to merge.
358 */ 360 */
359 if (ob1->flag [FLAG_APPLIED] || ob2->flag [FLAG_APPLIED]) 361 if (ob1->flag [FLAG_APPLIED] || ob2->flag [FLAG_APPLIED])
360 return 0; 362 return 0;
361 363
362 /* Note sure why the following is the case - either the object has to 364 /* Not sure why the following is the case - either the object has to
363 * be animated or have a very low speed. Is this an attempted monster 365 * be animated or have a very low speed. Is this an attempted monster
364 * check? 366 * check?
365 */ 367 */
366 if (!ob1->flag [FLAG_ANIMATE] && ob1->has_active_speed ()) 368 if (!ob1->flag [FLAG_ANIMATE] && ob1->has_active_speed ())
367 return 0; 369 return 0;
368 370
372 if (ob1->level != ob2->level) 374 if (ob1->level != ob2->level)
373 return 0; 375 return 0;
374 break; 376 break;
375 } 377 }
376 378
377 if (ob1->key_values || ob2->key_values) 379 if (!ob1->kv.empty () || !ob2->kv.empty ())
378 { 380 {
379 /* At least one of these has key_values. */ 381 /* At least one of these has key_values. */
380 if ((!ob1->key_values) != (!ob2->key_values)) 382 if (ob1->kv.empty () != ob2->kv.empty ())
381 return 0; /* One has fields, but the other one doesn't. */ 383 return 0; /* One has fields, but the other one doesn't. */
382 384
383 if (!compare_ob_value_lists (ob1, ob2)) 385 if (!compare_ob_value_lists (ob1, ob2))
384 return 0; 386 return 0;
385 } 387 }
445 447
446 return 0; 448 return 0;
447} 449}
448 450
449// adjust weight per container type ("of holding") 451// adjust weight per container type ("of holding")
450static uint32 452static weight_t
451weight_adjust_for (object *op, uint32 weight) 453weight_adjust_for (object *op, weight_t weight)
452{ 454{
453 return op->type == CONTAINER 455 if (op->type == CONTAINER)
454 ? weight - weight * op->stats.Str / 100 456 weight -= weight * op->stats.Str / 100;
455 : weight; 457
458 return weight;
456} 459}
457 460
458/* 461/*
459 * subtracts, then adds, the specified weight to an object, 462 * subtracts, then adds, the specified weight to an object,
460 * and also updates how much the environment(s) is/are carrying. 463 * and also updates how much the environment(s) is/are carrying.
461 */ 464 */
462static void 465static void
463adjust_weight (object *op, sint32 sub, sint32 add) 466adjust_weight (object *op, weight_t sub, weight_t add)
464{ 467{
465 while (op) 468 while (op)
466 { 469 {
467 sint32 ocarrying = op->carrying; 470 weight_t carrying = (weight_t)op->carrying
471 - weight_adjust_for (op, sub)
472 + weight_adjust_for (op, add);
468 473
469 op->carrying -= weight_adjust_for (op, sub); 474 sub = op->carrying;
470 op->carrying += weight_adjust_for (op, add); 475 op->carrying = carrying;
476 add = op->carrying;
471 477
472 if (object *pl = op->visible_to ()) 478 if (object *pl = op->visible_to ())
473 if (pl != op) // player is handled lazily 479 if (pl != op) // player is handled lazily
474 esrv_update_item (UPD_WEIGHT, pl, op); 480 esrv_update_item (UPD_WEIGHT, pl, op);
475 481
476 sub = ocarrying;
477 add = op->carrying;
478
479 op = op->env; 482 op = op->env;
480 } 483 }
481} 484}
482 485
483/* 486/*
486 * containers are carrying, and sums it up. 489 * containers are carrying, and sums it up.
487 */ 490 */
488void 491void
489object::update_weight () 492object::update_weight ()
490{ 493{
491 sint32 sum = 0; 494 weight_t sum = 0;
492 495
493 for (object *op = inv; op; op = op->below) 496 for (object *op = inv; op; op = op->below)
494 { 497 {
495 op->update_weight (); 498 op->update_weight ();
496 499
596 LOG (llevError | logBacktrace, "tried to set owner of %s to %s\n", debug_desc (), owner->debug_desc ()); 599 LOG (llevError | logBacktrace, "tried to set owner of %s to %s\n", debug_desc (), owner->debug_desc ());
597 return; 600 return;
598 } 601 }
599 602
600 this->owner = owner; 603 this->owner = owner;
601}
602
603/* Zero the key_values on op, decrementing the shared-string
604 * refcounts and freeing the links.
605 */
606static void
607free_key_values (object *op)
608{
609 for (key_value *i = op->key_values; i; )
610 {
611 key_value *next = i->next;
612 delete i;
613
614 i = next;
615 }
616
617 op->key_values = 0;
618} 604}
619 605
620/* 606/*
621 * copy_to first frees everything allocated by the dst object, 607 * copy_to first frees everything allocated by the dst object,
622 * and then copies the contents of itself into the second 608 * and then copies the contents of itself into the second
628void 614void
629object::copy_to (object *dst) 615object::copy_to (object *dst)
630{ 616{
631 dst->remove (); 617 dst->remove ();
632 *(object_copy *)dst = *this; 618 *(object_copy *)dst = *this;
619
620 // maybe move to object_copy?
621 dst->kv = kv;
622
633 dst->flag [FLAG_REMOVED] = true; 623 dst->flag [FLAG_REMOVED] = true;
634
635 /* Copy over key_values, if any. */
636 if (key_values)
637 {
638 key_value *tail = 0;
639 dst->key_values = 0;
640
641 for (key_value *i = key_values; i; i = i->next)
642 {
643 key_value *new_link = new key_value;
644
645 new_link->next = 0;
646 new_link->key = i->key;
647 new_link->value = i->value;
648
649 /* Try and be clever here, too. */
650 if (!dst->key_values)
651 {
652 dst->key_values = new_link;
653 tail = new_link;
654 }
655 else
656 {
657 tail->next = new_link;
658 tail = new_link;
659 }
660 }
661 }
662
663 dst->activate (); 624 dst->activate ();
664} 625}
665 626
666void 627void
667object::instantiate () 628object::instantiate ()
668{ 629{
669 if (!uuid.seq) // HACK 630 if (!uuid.seq) // HACK
670 uuid = UUID::gen (); 631 uuid = UUID::gen ();
671 632
672 // TODO: unclean state changes, should nt be done in copy_to AND instantiate 633 // TODO: unclean state changes, should not be done in copy_to AND instantiate
673 if (flag [FLAG_RANDOM_SPEED] && speed) 634 if (flag [FLAG_RANDOM_SPEED] && speed)
674 speed_left = - speed - rndm (); // TODO animation 635 speed_left = - speed - rndm (); // TODO animation
675 else 636 else
676 speed_left = -1.; 637 speed_left = -1.;
677 638
678 /* copy the body_info to the body_used - this is only really 639 /* copy the body_info to the body_used - this is only really
679 * need for monsters, but doesn't hurt to do it for everything. 640 * need for monsters, but doesn't hurt to do it for everything.
680 * by doing so, when a monster is created, it has good starting 641 * by doing so, when a monster is created, it has good starting
681 * values for the body_used info, so when items are created 642 * values for the body_used info, so when items are created
682 * for it, they can be properly equipped. 643 * for it, they can be properly equipped.
683 */ 644 */
684 for (int i = NUM_BODY_LOCATIONS; i--; ) 645 for (int i = 0; i < NUM_BODY_LOCATIONS; ++i)
685 slot[i].used = slot[i].info; 646 slot[i].used = slot[i].info;
686 647
687 attachable::instantiate (); 648 attachable::instantiate ();
688} 649}
689 650
778 } 739 }
779 740
780 mapspace &m = op->ms (); 741 mapspace &m = op->ms ();
781 742
782 if (!(m.flags_ & P_UPTODATE)) 743 if (!(m.flags_ & P_UPTODATE))
783 /* nop */; 744 m.update_up (); // nothing to do except copy up
784 else if (action == UP_OBJ_INSERT) 745 else if (action == UP_OBJ_INSERT)
785 { 746 {
786#if 0 747#if 0
787 // this is likely overkill, TODO: revisit (schmorp) 748 // this is likely overkill, TODO: revisit (schmorp)
788 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW)) 749 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW))
793 || (op->flag [FLAG_DAMNED] && !(m.flags_ & P_NO_CLERIC)) 754 || (op->flag [FLAG_DAMNED] && !(m.flags_ & P_NO_CLERIC))
794 || (m.move_on | op->move_on ) != m.move_on 755 || (m.move_on | op->move_on ) != m.move_on
795 || (m.move_off | op->move_off ) != m.move_off 756 || (m.move_off | op->move_off ) != m.move_off
796 || (m.move_slow | op->move_slow) != m.move_slow 757 || (m.move_slow | op->move_slow) != m.move_slow
797 /* This isn't perfect, but I don't expect a lot of objects to 758 /* This isn't perfect, but I don't expect a lot of objects to
798 * have move_allow right now. 759 * have move_allow right now.
799 */ 760 */
800 || ((m.move_block | op->move_block) & ~op->move_allow) != m.move_block 761 || ((m.move_block | op->move_block) & ~op->move_allow) != m.move_block
801 m.invalidate (); 762 m.invalidate ();
802#else 763#else
803 // the above is not strong enough a test to skip updating. los maybe? TODO (schmorp) 764 // the above is not strong enough a test to skip updating. los maybe? TODO (schmorp)
809 * that is being removed. 770 * that is being removed.
810 */ 771 */
811 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) 772 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE)
812 m.invalidate (); 773 m.invalidate ();
813 else if (action == UP_OBJ_FACE) 774 else if (action == UP_OBJ_FACE)
814 /* Nothing to do for that case */ ; 775 m.update_up (); // nothing to do for that case, except copy up
815 else 776 else
816 LOG (llevError, "update_object called with invalid action: %d\n", action); 777 LOG (llevError, "update_object called with invalid action: %d\n", action);
817 778
818 if (op->more) 779 if (op->more)
819 update_object (op->more, action); 780 update_object (op->more, action);
830 791
831object::~object () 792object::~object ()
832{ 793{
833 unlink (); 794 unlink ();
834 795
835 free_key_values (this); 796 kv.clear ();
836} 797}
837 798
838void object::link () 799void object::link ()
839{ 800{
840 assert (!index);//D 801 assert (!index);//D
883 op->activate_recursive (); 844 op->activate_recursive ();
884} 845}
885 846
886/* This function removes object 'op' from the list of active 847/* This function removes object 'op' from the list of active
887 * objects. 848 * objects.
888 * This should only be used for style maps or other such 849 * This should only be used for style maps or other such
889 * reference maps where you don't want an object that isn't 850 * reference maps where you don't want an object that isn't
890 * in play chewing up cpu time getting processed. 851 * in play chewing up cpu time getting processed.
891 * The reverse of this is to call update_ob_speed, which 852 * The reverse of this is to call update_ob_speed, which
892 * will do the right thing based on the speed of the object. 853 * will do the right thing based on the speed of the object.
893 */ 854 */
939 * if some form of movement is allowed, let objects 900 * if some form of movement is allowed, let objects
940 * drop on that space. 901 * drop on that space.
941 */ 902 */
942 if (!drop_to_ground 903 if (!drop_to_ground
943 || !map 904 || !map
944 || map->in_memory != MAP_ACTIVE 905 || !map->linkable ()
945 || map->no_drop 906 || map->no_drop
946 || ms ().move_block == MOVE_ALL) 907 || ms ().move_block == MOVE_ALL)
947 { 908 {
948 while (inv) 909 while (inv)
949 inv->destroy (); 910 inv->destroy ();
1046 1007
1047 freelist = li; 1008 freelist = li;
1048 ++free_count; 1009 ++free_count;
1049} 1010}
1050 1011
1051static struct freed_map : maptile
1052{
1053 freed_map ()
1054 : maptile (3, 3)
1055 {
1056 path = "<freed objects map>";
1057 name = "/internal/freed_objects_map";
1058 no_drop = 1;
1059 no_reset = 1;
1060
1061 in_memory = MAP_ACTIVE;
1062 }
1063
1064 ~freed_map ()
1065 {
1066 destroy ();
1067 }
1068} freed_map; // freed objects are moved here to avoid crashes
1069
1070void 1012void
1071object::do_destroy () 1013object::do_destroy ()
1072{ 1014{
1073 if (flag [FLAG_IS_LINKED]) 1015 if (flag [FLAG_IS_LINKED])
1074 remove_link (); 1016 remove_link ();
1147 flag [FLAG_REMOVED] = true; 1089 flag [FLAG_REMOVED] = true;
1148 1090
1149 if (more) 1091 if (more)
1150 more->remove (); 1092 more->remove ();
1151 1093
1152 /* 1094 /*
1153 * In this case, the object to be removed is in someones 1095 * In this case, the object to be removed is in someones
1154 * inventory. 1096 * inventory.
1155 */ 1097 */
1156 if (env) 1098 if (env)
1157 { 1099 {
1239 1181
1240 above = 0; 1182 above = 0;
1241 below = 0; 1183 below = 0;
1242 1184
1243 ms.invalidate (); 1185 ms.invalidate ();
1244
1245 if (map->in_memory == MAP_SAVING)
1246 return;
1247 1186
1248 int check_walk_off = !flag [FLAG_NO_APPLY]; 1187 int check_walk_off = !flag [FLAG_NO_APPLY];
1249 1188
1250 if (object *pl = ms.player ()) 1189 if (object *pl = ms.player ())
1251 { 1190 {
1386{ 1325{
1387 op->remove (); 1326 op->remove ();
1388 1327
1389 if (m == &freed_map)//D TODO: remove soon 1328 if (m == &freed_map)//D TODO: remove soon
1390 {//D 1329 {//D
1391 LOG (llevError | logBacktrace, "tries to insret object on freed objects map: %s", op->debug_desc ());//D 1330 LOG (llevError | logBacktrace, "tries to insert object on freed objects map: %s", op->debug_desc ());//D
1392 }//D 1331 }//D
1393 1332
1394 /* Ideally, the caller figures this out. However, it complicates a lot 1333 /* Ideally, the caller figures this out. However, it complicates a lot
1395 * of areas of callers (eg, anything that uses find_free_spot would now 1334 * of areas of callers (eg, anything that uses find_free_spot would now
1396 * need extra work 1335 * need extra work
1465 * We've already dealt with merging if appropriate. 1404 * We've already dealt with merging if appropriate.
1466 * Generally, we want to put the new object on top. But if 1405 * Generally, we want to put the new object on top. But if
1467 * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last 1406 * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last
1468 * floor, we want to insert above that and no further. 1407 * floor, we want to insert above that and no further.
1469 * Also, if there are spell objects on this space, we stop processing 1408 * Also, if there are spell objects on this space, we stop processing
1470 * once we get to them. This reduces the need to traverse over all of 1409 * once we get to them. This reduces the need to traverse over all of
1471 * them when adding another one - this saves quite a bit of cpu time 1410 * them when adding another one - this saves quite a bit of cpu time
1472 * when lots of spells are cast in one area. Currently, it is presumed 1411 * when lots of spells are cast in one area. Currently, it is presumed
1473 * that flying non pickable objects are spell objects. 1412 * that flying non pickable objects are spell objects.
1474 */ 1413 */
1475 for (object *tmp = ms.bot; tmp; tmp = tmp->above) 1414 for (object *tmp = ms.bot; tmp; tmp = tmp->above)
1601 1540
1602 return op; 1541 return op;
1603} 1542}
1604 1543
1605/* this function inserts an object in the map, but if it 1544/* this function inserts an object in the map, but if it
1606 * finds an object of its own type, it'll remove that one first. 1545 * finds an object of its own type, it'll remove that one first.
1607 * op is the object to insert it under: supplies x and the map. 1546 * op is the object to insert it under: supplies x and the map.
1608 */ 1547 */
1609void 1548void
1610replace_insert_ob_in_map (shstr_tmp archname, object *op) 1549replace_insert_ob_in_map (shstr_tmp archname, object *op)
1611{ 1550{
1643 if (!items // testing !items ensures we can drop at least one item 1582 if (!items // testing !items ensures we can drop at least one item
1644 || (items < m->max_items 1583 || (items < m->max_items
1645 && ms.volume () < m->max_volume)) 1584 && ms.volume () < m->max_volume))
1646 return true; 1585 return true;
1647 1586
1648 if (originator && originator->is_player ()) 1587 if (originator)
1649 originator->contr->failmsgf ( 1588 originator->failmsgf (
1650 "No matter how hard you try, you just cannot put the %s here H<Try to remove some items from the floor first.>", 1589 "No matter how hard you try, you just cannot put the %s here! H<Try to remove some items from the floor first.>",
1651 query_name () 1590 query_name ()
1652 ); 1591 );
1653 1592
1654 return false; 1593 return false;
1655} 1594}
1669 1608
1670 nr = min (nr, nrof); 1609 nr = min (nr, nrof);
1671 1610
1672 if (nrof > nr) 1611 if (nrof > nr)
1673 { 1612 {
1674 sint64 oweight = total_weight (); 1613 weight_t oweight = total_weight ();
1675 1614
1676 nrof -= nr; 1615 nrof -= nr;
1677 1616
1678 if (object *pl = visible_to ()) 1617 if (object *pl = visible_to ())
1679 esrv_update_item (UPD_NROF, pl, this); 1618 esrv_update_item (UPD_NROF, pl, this);
1740/* 1679/*
1741 * env->insert (op) 1680 * env->insert (op)
1742 * This function inserts the object op in the linked list 1681 * This function inserts the object op in the linked list
1743 * inside the object environment. 1682 * inside the object environment.
1744 * 1683 *
1745 * The function returns now pointer to inserted item, and return value can 1684 * The function returns now pointer to inserted item, and return value can
1746 * be != op, if items are merged. -Tero 1685 * be != op, if items are merged. -Tero
1747 */ 1686 */
1748object * 1687object *
1749object::insert (object *op) 1688object::insert (object *op)
1750{ 1689{
1763 if (object::can_merge (tmp, op)) 1702 if (object::can_merge (tmp, op))
1764 { 1703 {
1765 /* return the original object and remove inserted object 1704 /* return the original object and remove inserted object
1766 (client prefers the original object) */ 1705 (client prefers the original object) */
1767 1706
1768 // carring must be 0 for mergable objects 1707 // carrying must be 0 for mergable objects
1769 sint64 oweight = tmp->weight * tmp->nrof; 1708 weight_t oweight = weight_t (tmp->weight) * tmp->nrof;
1770 1709
1771 tmp->nrof += op->nrof; 1710 tmp->nrof += op->nrof;
1772 1711
1773 if (object *pl = tmp->visible_to ()) 1712 if (object *pl = tmp->visible_to ())
1774 esrv_update_item (UPD_NROF, pl, tmp); 1713 esrv_update_item (UPD_NROF, pl, tmp);
1775 1714
1776 adjust_weight (this, oweight, tmp->weight * tmp->nrof); 1715 adjust_weight (this, oweight, weight_t (tmp->weight) * tmp->nrof);
1777 1716
1778 op->destroy (); 1717 op->destroy ();
1779 op = tmp; 1718 op = tmp;
1780 goto inserted; 1719 goto inserted;
1781 } 1720 }
1870 */ 1809 */
1871 if ((op->move_type & ~move_on & ~move_block) != 0 && (op->move_type & ~move_slow & ~move_block) != 0) 1810 if ((op->move_type & ~move_on & ~move_block) != 0 && (op->move_type & ~move_slow & ~move_block) != 0)
1872 return 0; 1811 return 0;
1873 1812
1874 /* The objects have to be checked from top to bottom. 1813 /* The objects have to be checked from top to bottom.
1875 * Hence, we first go to the top: 1814 * Hence, we first go to the top:
1876 */ 1815 */
1877 for (object *next, *tmp = ms.top; tmp; tmp = next) 1816 for (object *next, *tmp = ms.top; tmp; tmp = next)
1878 { 1817 {
1879 next = tmp->below; 1818 next = tmp->below;
1880 1819
2064 * inform the caller. However, insert_ob_in_map will update as 2003 * inform the caller. However, insert_ob_in_map will update as
2065 * necessary, so the caller shouldn't need to do any special work. 2004 * necessary, so the caller shouldn't need to do any special work.
2066 * Note - updated to take an object instead of archetype - this is necessary 2005 * Note - updated to take an object instead of archetype - this is necessary
2067 * because arch_blocked (now ob_blocked) needs to know the movement type 2006 * because arch_blocked (now ob_blocked) needs to know the movement type
2068 * to know if the space in question will block the object. We can't use 2007 * to know if the space in question will block the object. We can't use
2069 * the archetype because that isn't correct if the monster has been 2008 * the archetype because that isn't correct if the monster has been
2070 * customized, changed states, etc. 2009 * customized, changed states, etc.
2071 */ 2010 */
2072int 2011int
2073find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop) 2012find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop)
2074{ 2013{
2075 int altern[SIZEOFFREE]; 2014 int altern[SIZEOFFREE];
2076 int index = 0, flag; 2015 int index = 0;
2077 2016
2078 for (int i = start; i < stop; i++) 2017 for (int i = start; i < stop; i++)
2079 { 2018 {
2080 mapxy pos (m, x, y); pos.move (i); 2019 mapxy pos (m, x, y); pos.move (i);
2081 2020
2097 continue; 2036 continue;
2098 } 2037 }
2099 2038
2100 /* Basically, if we find a wall on a space, we cut down the search size. 2039 /* Basically, if we find a wall on a space, we cut down the search size.
2101 * In this way, we won't return spaces that are on another side of a wall. 2040 * In this way, we won't return spaces that are on another side of a wall.
2102 * This mostly work, but it cuts down the search size in all directions - 2041 * This mostly work, but it cuts down the search size in all directions -
2103 * if the space being examined only has a wall to the north and empty 2042 * if the space being examined only has a wall to the north and empty
2104 * spaces in all the other directions, this will reduce the search space 2043 * spaces in all the other directions, this will reduce the search space
2105 * to only the spaces immediately surrounding the target area, and 2044 * to only the spaces immediately surrounding the target area, and
2106 * won't look 2 spaces south of the target space. 2045 * won't look 2 spaces south of the target space.
2107 */ 2046 */
2137 */ 2076 */
2138int 2077int
2139find_first_free_spot (const object *ob, maptile *m, int x, int y) 2078find_first_free_spot (const object *ob, maptile *m, int x, int y)
2140{ 2079{
2141 for (int i = 0; i < SIZEOFFREE; i++) 2080 for (int i = 0; i < SIZEOFFREE; i++)
2142 if (!ob->blocked (m, x + freearr_x[i], y + freearr_y[i])) 2081 if (!ob->blocked (m, x + DIRX (i), y + DIRY (i)))
2143 return i; 2082 return i;
2144 2083
2145 return -1; 2084 return -1;
2146} 2085}
2147 2086
2158 2097
2159 while (--end) 2098 while (--end)
2160 swap (arr [end], arr [rndm (end + 1)]); 2099 swap (arr [end], arr [rndm (end + 1)]);
2161} 2100}
2162 2101
2163/* new function to make monster searching more efficient, and effective! 2102/* new function to make monster searching more efficient, and effective!
2164 * This basically returns a randomized array (in the passed pointer) of 2103 * This basically returns a randomized array (in the passed pointer) of
2165 * the spaces to find monsters. In this way, it won't always look for 2104 * the spaces to find monsters. In this way, it won't always look for
2166 * monsters to the north first. However, the size of the array passed 2105 * monsters to the north first. However, the size of the array passed
2167 * covers all the spaces, so within that size, all the spaces within 2106 * covers all the spaces, so within that size, all the spaces within
2168 * the 3x3 area will be searched, just not in a predictable order. 2107 * the 3x3 area will be searched, just not in a predictable order.
2193 * there is capable of. 2132 * there is capable of.
2194 */ 2133 */
2195int 2134int
2196find_dir (maptile *m, int x, int y, object *exclude) 2135find_dir (maptile *m, int x, int y, object *exclude)
2197{ 2136{
2198 int max = SIZEOFFREE, mflags; 2137 int max = SIZEOFFREE;
2199 MoveType move_type; 2138 MoveType move_type;
2200 2139
2201 if (exclude && exclude->head_ () != exclude) 2140 if (exclude && exclude->head_ () != exclude)
2202 { 2141 {
2203 exclude = exclude->head; 2142 exclude = exclude->head;
2412 int mflags; 2351 int mflags;
2413 2352
2414 if (dir < 0) 2353 if (dir < 0)
2415 return 0; /* exit condition: invalid direction */ 2354 return 0; /* exit condition: invalid direction */
2416 2355
2417 dx = x + freearr_x[dir]; 2356 dx = x + DIRX (dir);
2418 dy = y + freearr_y[dir]; 2357 dy = y + DIRY (dir);
2419 2358
2420 mflags = get_map_flags (m, &m, dx, dy, &dx, &dy); 2359 mflags = get_map_flags (m, &m, dx, dy, &dx, &dy);
2421 2360
2422 /* This functional arguably was incorrect before - it was 2361 /* This functional arguably was incorrect before - it was
2423 * checking for P_WALL - that was basically seeing if 2362 * checking for P_WALL - that was basically seeing if
2454 return /*who->flag [FLAG_WIZ]|| */ 2393 return /*who->flag [FLAG_WIZ]|| */
2455 (item->weight > 0 && !item->flag [FLAG_NO_PICK] && 2394 (item->weight > 0 && !item->flag [FLAG_NO_PICK] &&
2456 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3)); 2395 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3));
2457} 2396}
2458 2397
2398//-GPL
2399
2459/* 2400/*
2460 * create clone from object to another 2401 * create clone from object to another
2461 */ 2402 */
2462object * 2403object *
2463object::deep_clone () 2404object::deep_clone ()
2493 return tmp; 2434 return tmp;
2494 2435
2495 return 0; 2436 return 0;
2496} 2437}
2497 2438
2439/* Zero the key_values on op, decrementing the shared-string
2440 * refcounts and freeing the links.
2441 */
2442void
2443key_values::clear ()
2444{
2445 for (key_value *kvp = first; kvp; )
2446 {
2447 key_value *next = kvp->next;
2448 delete kvp;
2449 kvp = next;
2450 }
2451
2452 first = 0;
2453}
2454
2498shstr_tmp 2455shstr_tmp
2499object::kv_get (shstr_tmp key) const 2456key_values::get (shstr_tmp key) const
2500{ 2457{
2501 for (key_value *kv = key_values; kv; kv = kv->next) 2458 for (key_value *kv = first; kv; kv = kv->next)
2502 if (kv->key == key) 2459 if (kv->key == key)
2503 return kv->value; 2460 return kv->value;
2504 2461
2505 return shstr (); 2462 return shstr ();
2506} 2463}
2507 2464
2508void 2465void
2466key_values::add (shstr_tmp key, shstr_tmp value)
2467{
2468 key_value *kv = new key_value;
2469
2470 kv->next = first;
2471 kv->key = key;
2472 kv->value = value;
2473
2474 first = kv;
2475}
2476
2477void
2509object::kv_set (shstr_tmp key, shstr_tmp value) 2478key_values::set (shstr_tmp key, shstr_tmp value)
2510{ 2479{
2511 for (key_value *kv = key_values; kv; kv = kv->next) 2480 for (key_value *kv = first; kv; kv = kv->next)
2512 if (kv->key == key) 2481 if (kv->key == key)
2513 { 2482 {
2514 kv->value = value; 2483 kv->value = value;
2515 return; 2484 return;
2516 } 2485 }
2517 2486
2518 key_value *kv = new key_value; 2487 add (key, value);
2519
2520 kv->next = key_values;
2521 kv->key = key;
2522 kv->value = value;
2523
2524 key_values = kv;
2525} 2488}
2526 2489
2527void 2490void
2528object::kv_del (shstr_tmp key) 2491key_values::del (shstr_tmp key)
2529{ 2492{
2530 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2493 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2531 if ((*kvp)->key == key) 2494 if ((*kvp)->key == key)
2532 { 2495 {
2533 key_value *kv = *kvp; 2496 key_value *kv = *kvp;
2534 *kvp = (*kvp)->next; 2497 *kvp = (*kvp)->next;
2535 delete kv; 2498 delete kv;
2536 return; 2499 return;
2537 } 2500 }
2501}
2502
2503void
2504key_values::reverse ()
2505{
2506 key_value *prev = 0;
2507 key_value *head = first;
2508
2509 while (head)
2510 {
2511 key_value *node = head;
2512 head = head->next;
2513 node->next = prev;
2514 prev = node;
2515 }
2516
2517 first = prev;
2518}
2519
2520key_values &
2521key_values::operator =(const key_values &kv)
2522{
2523 clear ();
2524
2525 for (key_value *kvp = kv.first; kvp; kvp = kvp->next)
2526 add (kvp->key, kvp->value);
2527
2528 reverse ();
2538} 2529}
2539 2530
2540object::depth_iterator::depth_iterator (object *container) 2531object::depth_iterator::depth_iterator (object *container)
2541: iterator_base (container) 2532: iterator_base (container)
2542{ 2533{
2625object::region () const 2616object::region () const
2626{ 2617{
2627 return map ? map->region (x, y) 2618 return map ? map->region (x, y)
2628 : region::default_region (); 2619 : region::default_region ();
2629} 2620}
2621
2622//+GPL
2630 2623
2631void 2624void
2632object::open_container (object *new_container) 2625object::open_container (object *new_container)
2633{ 2626{
2634 if (container == new_container) 2627 if (container == new_container)
2692 } 2685 }
2693// else if (!old_container->env && contr && contr->ns) 2686// else if (!old_container->env && contr && contr->ns)
2694// contr->ns->floorbox_reset (); 2687// contr->ns->floorbox_reset ();
2695} 2688}
2696 2689
2690//-GPL
2691
2692// prefetch some flat area around the player
2693static void
2694prefetch_surrounding_area (object *op, maptile *map, int range)
2695{
2696 for (maprect *rect = map->split_to_tiles (mapwalk_buf,
2697 op->x - range , op->y - range ,
2698 op->x + range + 1, op->y + range + 1);
2699 rect->m;
2700 ++rect)
2701 {
2702 rect->m->touch ();
2703 rect->m->activate ();
2704 }
2705}
2706
2707// prefetch a generous area around the player, also up and down
2708void
2709object::prefetch_surrounding_maps ()
2710{
2711 prefetch_surrounding_area (this, map, 40);
2712
2713 if (maptile *m = map->tile_available (TILE_DOWN))
2714 prefetch_surrounding_area (this, m, 20);
2715
2716 if (maptile *m = map->tile_available (TILE_UP))
2717 prefetch_surrounding_area (this, m, 20);
2718}
2719
2720//+GPL
2721
2697object * 2722object *
2698object::force_find (shstr_tmp name) 2723object::force_find (shstr_tmp name)
2699{ 2724{
2700 /* cycle through his inventory to look for the MARK we want to 2725 /* cycle through his inventory to look for the MARK we want to
2701 * place 2726 * place
2702 */ 2727 */
2703 for (object *tmp = inv; tmp; tmp = tmp->below) 2728 for (object *tmp = inv; tmp; tmp = tmp->below)
2704 if (tmp->type == FORCE && tmp->slaying == name) 2729 if (tmp->type == FORCE && tmp->slaying == name)
2705 return splay (tmp); 2730 return splay (tmp);
2706 2731
2707 return 0; 2732 return 0;
2708} 2733}
2709 2734
2710//-GPL
2711
2712void 2735void
2713object::force_set_timer (int duration) 2736object::force_set_timer (int duration)
2714{ 2737{
2715 this->duration = 1; 2738 this->duration = 1;
2716 this->speed_left = -1.f; 2739 this->speed_left = -1.f;
2722object::force_add (shstr_tmp name, int duration) 2745object::force_add (shstr_tmp name, int duration)
2723{ 2746{
2724 if (object *force = force_find (name)) 2747 if (object *force = force_find (name))
2725 force->destroy (); 2748 force->destroy ();
2726 2749
2727 object *force = get_archetype (FORCE_NAME); 2750 object *force = archetype::get (FORCE_NAME);
2728 2751
2729 force->slaying = name; 2752 force->slaying = name;
2730 force->force_set_timer (duration); 2753 force->force_set_timer (duration);
2731 force->flag [FLAG_APPLIED] = true; 2754 force->flag [FLAG_APPLIED] = true;
2732 2755

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines