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.338 by root, Fri Jul 2 16:24:24 2010 UTC vs.
Revision 1.351 by elmex, Fri Jun 3 08:36:45 2011 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,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 8 * 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 9 * the terms of the Affero GNU General Public License as published by the
233 233
234/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 234/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
235static bool 235static bool
236compare_ob_value_lists_one (const object *wants, const object *has) 236compare_ob_value_lists_one (const object *wants, const object *has)
237{ 237{
238 /* n-squared behaviour (see kv_get), but I'm hoping both 238 /* 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 239 * objects with lists are rare, and lists stay short. If not, use a
240 * different structure or at least keep the lists sorted... 240 * different structure or at least keep the lists sorted...
241 */ 241 */
242 242
243 /* For each field in wants, */ 243 /* For each field in wants, */
244 for (key_value *kv = wants->key_values; kv; kv = kv->next) 244 for (key_value *kv = wants->kv.first; kv; kv = kv->next)
245 if (has->kv_get (kv->key) != kv->value) 245 if (has->kv.get (kv->key) != kv->value)
246 return false; 246 return false;
247 247
248 /* If we get here, every field in wants has a matching field in has. */ 248 /* If we get here, every field in wants has a matching field in has. */
249 return true; 249 return true;
250} 250}
274 */ 274 */
275bool object::can_merge_slow (object *ob1, object *ob2) 275bool object::can_merge_slow (object *ob1, object *ob2)
276{ 276{
277 /* A couple quick sanity checks */ 277 /* A couple quick sanity checks */
278 if (ob1 == ob2 278 if (ob1 == ob2
279 || ob1->type != ob2->type 279 || ob1->type != ob2->type
280 || ob1->value != ob2->value 280 || ob1->value != ob2->value
281 || ob1->name != ob2->name 281 || ob1->name != ob2->name
282 || ob1->custom_name != ob2->custom_name
282 || fabs (ob1->speed - ob2->speed) >= MIN_ACTIVE_SPEED) 283 || fabs (ob1->speed - ob2->speed) >= MIN_ACTIVE_SPEED)
283 return 0; 284 return 0;
284 285
285 /* Do not merge objects if nrof would overflow, assume nrof 286 /* Do not merge objects if nrof would overflow, assume nrof
286 * is always 0 .. 2**31-1 */ 287 * is always 0 .. 2**31-1 */
372 if (ob1->level != ob2->level) 373 if (ob1->level != ob2->level)
373 return 0; 374 return 0;
374 break; 375 break;
375 } 376 }
376 377
377 if (ob1->key_values || ob2->key_values) 378 if (!ob1->kv.empty () || !ob2->kv.empty ())
378 { 379 {
379 /* At least one of these has key_values. */ 380 /* At least one of these has key_values. */
380 if ((!ob1->key_values) != (!ob2->key_values)) 381 if (ob1->kv.empty () != ob2->kv.empty ())
381 return 0; /* One has fields, but the other one doesn't. */ 382 return 0; /* One has fields, but the other one doesn't. */
382 383
383 if (!compare_ob_value_lists (ob1, ob2)) 384 if (!compare_ob_value_lists (ob1, ob2))
384 return 0; 385 return 0;
385 } 386 }
598 } 599 }
599 600
600 this->owner = owner; 601 this->owner = owner;
601} 602}
602 603
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}
619
620/* 604/*
621 * copy_to first frees everything allocated by the dst object, 605 * copy_to first frees everything allocated by the dst object,
622 * and then copies the contents of itself into the second 606 * and then copies the contents of itself into the second
623 * object, allocating what needs to be allocated. Basically, any 607 * object, allocating what needs to be allocated. Basically, any
624 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 608 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
628void 612void
629object::copy_to (object *dst) 613object::copy_to (object *dst)
630{ 614{
631 dst->remove (); 615 dst->remove ();
632 *(object_copy *)dst = *this; 616 *(object_copy *)dst = *this;
617
618 // maybe move to object_copy?
619 dst->kv = kv;
620
633 dst->flag [FLAG_REMOVED] = true; 621 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 (); 622 dst->activate ();
664} 623}
665 624
666void 625void
667object::instantiate () 626object::instantiate ()
668{ 627{
669 if (!uuid.seq) // HACK 628 if (!uuid.seq) // HACK
670 uuid = UUID::gen (); 629 uuid = UUID::gen ();
671 630
672 // TODO: unclean state changes, should nt be done in copy_to AND instantiate 631 // TODO: unclean state changes, should not be done in copy_to AND instantiate
673 if (flag [FLAG_RANDOM_SPEED] && speed) 632 if (flag [FLAG_RANDOM_SPEED] && speed)
674 speed_left = - speed - rndm (); // TODO animation 633 speed_left = - speed - rndm (); // TODO animation
675 else 634 else
676 speed_left = -1.; 635 speed_left = -1.;
677 636
778 } 737 }
779 738
780 mapspace &m = op->ms (); 739 mapspace &m = op->ms ();
781 740
782 if (!(m.flags_ & P_UPTODATE)) 741 if (!(m.flags_ & P_UPTODATE))
783 /* nop */; 742 m.update_up (); // nothing to do except copy up
784 else if (action == UP_OBJ_INSERT) 743 else if (action == UP_OBJ_INSERT)
785 { 744 {
786#if 0 745#if 0
787 // this is likely overkill, TODO: revisit (schmorp) 746 // this is likely overkill, TODO: revisit (schmorp)
788 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW)) 747 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW))
809 * that is being removed. 768 * that is being removed.
810 */ 769 */
811 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) 770 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE)
812 m.invalidate (); 771 m.invalidate ();
813 else if (action == UP_OBJ_FACE) 772 else if (action == UP_OBJ_FACE)
814 /* Nothing to do for that case */ ; 773 m.update_up (); // nothing to do for that case, except copy up
815 else 774 else
816 LOG (llevError, "update_object called with invalid action: %d\n", action); 775 LOG (llevError, "update_object called with invalid action: %d\n", action);
817 776
818 if (op->more) 777 if (op->more)
819 update_object (op->more, action); 778 update_object (op->more, action);
830 789
831object::~object () 790object::~object ()
832{ 791{
833 unlink (); 792 unlink ();
834 793
835 free_key_values (this); 794 kv.clear ();
836} 795}
837 796
838void object::link () 797void object::link ()
839{ 798{
840 assert (!index);//D 799 assert (!index);//D
939 * if some form of movement is allowed, let objects 898 * if some form of movement is allowed, let objects
940 * drop on that space. 899 * drop on that space.
941 */ 900 */
942 if (!drop_to_ground 901 if (!drop_to_ground
943 || !map 902 || !map
944 || map->in_memory != MAP_ACTIVE 903 || !map->linkable ()
945 || map->no_drop 904 || map->no_drop
946 || ms ().move_block == MOVE_ALL) 905 || ms ().move_block == MOVE_ALL)
947 { 906 {
948 while (inv) 907 while (inv)
949 inv->destroy (); 908 inv->destroy ();
1046 1005
1047 freelist = li; 1006 freelist = li;
1048 ++free_count; 1007 ++free_count;
1049} 1008}
1050 1009
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 1010void
1071object::do_destroy () 1011object::do_destroy ()
1072{ 1012{
1073 if (flag [FLAG_IS_LINKED]) 1013 if (flag [FLAG_IS_LINKED])
1074 remove_link (); 1014 remove_link ();
1182 1122
1183 if (pl && pl->is_player ()) 1123 if (pl && pl->is_player ())
1184 { 1124 {
1185 if (expect_false (pl->contr->combat_ob == this)) 1125 if (expect_false (pl->contr->combat_ob == this))
1186 { 1126 {
1187 pl->apply (pl->contr->combat_ob, AP_UNAPPLY); 1127 pl->apply (pl->contr->combat_ob, AP_UNAPPLY | AP_IGNORE_CURSE);
1188 pl->contr->combat_ob = 0; 1128 pl->contr->combat_ob = 0;
1189 if (pl->contr->ranged_ob) pl->apply (pl->contr->ranged_ob); 1129 if (pl->contr->ranged_ob) pl->apply (pl->contr->ranged_ob);
1190 } 1130 }
1191 1131
1192 if (expect_false (pl->contr->ranged_ob == this)) 1132 if (expect_false (pl->contr->ranged_ob == this))
1193 { 1133 {
1194 pl->apply (pl->contr->ranged_ob, AP_UNAPPLY); 1134 pl->apply (pl->contr->ranged_ob, AP_UNAPPLY | AP_IGNORE_CURSE);
1195 pl->contr->ranged_ob = 0; 1135 pl->contr->ranged_ob = 0;
1196 if (pl->contr->combat_ob) pl->apply (pl->contr->combat_ob); 1136 if (pl->contr->combat_ob) pl->apply (pl->contr->combat_ob);
1197 } 1137 }
1198 1138
1199 pl->contr->queue_stats_update (); 1139 pl->contr->queue_stats_update ();
1239 1179
1240 above = 0; 1180 above = 0;
1241 below = 0; 1181 below = 0;
1242 1182
1243 ms.invalidate (); 1183 ms.invalidate ();
1244
1245 if (map->in_memory == MAP_SAVING)
1246 return;
1247 1184
1248 int check_walk_off = !flag [FLAG_NO_APPLY]; 1185 int check_walk_off = !flag [FLAG_NO_APPLY];
1249 1186
1250 if (object *pl = ms.player ()) 1187 if (object *pl = ms.player ())
1251 { 1188 {
1386{ 1323{
1387 op->remove (); 1324 op->remove ();
1388 1325
1389 if (m == &freed_map)//D TODO: remove soon 1326 if (m == &freed_map)//D TODO: remove soon
1390 {//D 1327 {//D
1391 LOG (llevError | logBacktrace, "tries to insret object on freed objects map: %s", op->debug_desc ());//D 1328 LOG (llevError | logBacktrace, "tries to insert object on freed objects map: %s", op->debug_desc ());//D
1392 }//D 1329 }//D
1393 1330
1394 /* Ideally, the caller figures this out. However, it complicates a lot 1331 /* 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 1332 * of areas of callers (eg, anything that uses find_free_spot would now
1396 * need extra work 1333 * need extra work
2454 return /*who->flag [FLAG_WIZ]|| */ 2391 return /*who->flag [FLAG_WIZ]|| */
2455 (item->weight > 0 && !item->flag [FLAG_NO_PICK] && 2392 (item->weight > 0 && !item->flag [FLAG_NO_PICK] &&
2456 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3)); 2393 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3));
2457} 2394}
2458 2395
2396//-GPL
2397
2459/* 2398/*
2460 * create clone from object to another 2399 * create clone from object to another
2461 */ 2400 */
2462object * 2401object *
2463object::deep_clone () 2402object::deep_clone ()
2493 return tmp; 2432 return tmp;
2494 2433
2495 return 0; 2434 return 0;
2496} 2435}
2497 2436
2437/* Zero the key_values on op, decrementing the shared-string
2438 * refcounts and freeing the links.
2439 */
2440void
2441key_values::clear ()
2442{
2443 for (key_value *kvp = first; kvp; )
2444 {
2445 key_value *next = kvp->next;
2446 delete kvp;
2447 kvp = next;
2448 }
2449
2450 first = 0;
2451}
2452
2498shstr_tmp 2453shstr_tmp
2499object::kv_get (shstr_tmp key) const 2454key_values::get (shstr_tmp key) const
2500{ 2455{
2501 for (key_value *kv = key_values; kv; kv = kv->next) 2456 for (key_value *kv = first; kv; kv = kv->next)
2502 if (kv->key == key) 2457 if (kv->key == key)
2503 return kv->value; 2458 return kv->value;
2504 2459
2505 return shstr (); 2460 return shstr ();
2506} 2461}
2507 2462
2508void 2463void
2464key_values::add (shstr_tmp key, shstr_tmp value)
2465{
2466 key_value *kv = new key_value;
2467
2468 kv->next = first;
2469 kv->key = key;
2470 kv->value = value;
2471
2472 first = kv;
2473}
2474
2475void
2509object::kv_set (shstr_tmp key, shstr_tmp value) 2476key_values::set (shstr_tmp key, shstr_tmp value)
2510{ 2477{
2511 for (key_value *kv = key_values; kv; kv = kv->next) 2478 for (key_value *kv = first; kv; kv = kv->next)
2512 if (kv->key == key) 2479 if (kv->key == key)
2513 { 2480 {
2514 kv->value = value; 2481 kv->value = value;
2515 return; 2482 return;
2516 } 2483 }
2517 2484
2518 key_value *kv = new key_value; 2485 add (key, value);
2519
2520 kv->next = key_values;
2521 kv->key = key;
2522 kv->value = value;
2523
2524 key_values = kv;
2525} 2486}
2526 2487
2527void 2488void
2528object::kv_del (shstr_tmp key) 2489key_values::del (shstr_tmp key)
2529{ 2490{
2530 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2491 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2531 if ((*kvp)->key == key) 2492 if ((*kvp)->key == key)
2532 { 2493 {
2533 key_value *kv = *kvp; 2494 key_value *kv = *kvp;
2534 *kvp = (*kvp)->next; 2495 *kvp = (*kvp)->next;
2535 delete kv; 2496 delete kv;
2536 return; 2497 return;
2537 } 2498 }
2499}
2500
2501void
2502key_values::reverse ()
2503{
2504 key_value *prev = 0;
2505 key_value *head = first;
2506
2507 while (head)
2508 {
2509 key_value *node = head;
2510 head = head->next;
2511 node->next = prev;
2512 prev = node;
2513 }
2514
2515 first = prev;
2516}
2517
2518key_values &
2519key_values::operator =(const key_values &kv)
2520{
2521 clear ();
2522
2523 for (key_value *kvp = kv.first; kvp; kvp = kvp->next)
2524 add (kvp->key, kvp->value);
2525
2526 reverse ();
2538} 2527}
2539 2528
2540object::depth_iterator::depth_iterator (object *container) 2529object::depth_iterator::depth_iterator (object *container)
2541: iterator_base (container) 2530: iterator_base (container)
2542{ 2531{
2625object::region () const 2614object::region () const
2626{ 2615{
2627 return map ? map->region (x, y) 2616 return map ? map->region (x, y)
2628 : region::default_region (); 2617 : region::default_region ();
2629} 2618}
2619
2620//+GPL
2630 2621
2631void 2622void
2632object::open_container (object *new_container) 2623object::open_container (object *new_container)
2633{ 2624{
2634 if (container == new_container) 2625 if (container == new_container)
2692 } 2683 }
2693// else if (!old_container->env && contr && contr->ns) 2684// else if (!old_container->env && contr && contr->ns)
2694// contr->ns->floorbox_reset (); 2685// contr->ns->floorbox_reset ();
2695} 2686}
2696 2687
2688//-GPL
2689
2690// prefetch some flat area around the player
2691static void
2692prefetch_surrounding_area (object *op, maptile *map, int range)
2693{
2694 for (maprect *rect = map->split_to_tiles (mapwalk_buf,
2695 op->x - range , op->y - range ,
2696 op->x + range + 1, op->y + range + 1);
2697 rect->m;
2698 ++rect)
2699 {
2700 rect->m->touch ();
2701 rect->m->activate ();
2702 }
2703}
2704
2705// prefetch a generous area around the player, also up and down
2706void
2707object::prefetch_surrounding_maps ()
2708{
2709 prefetch_surrounding_area (this, map, 40);
2710
2711 if (maptile *m = map->tile_available (TILE_DOWN))
2712 prefetch_surrounding_area (this, m, 20);
2713
2714 if (maptile *m = map->tile_available (TILE_UP))
2715 prefetch_surrounding_area (this, m, 20);
2716}
2717
2718//+GPL
2719
2697object * 2720object *
2698object::force_find (shstr_tmp name) 2721object::force_find (shstr_tmp name)
2699{ 2722{
2700 /* cycle through his inventory to look for the MARK we want to 2723 /* cycle through his inventory to look for the MARK we want to
2701 * place 2724 * place
2705 return splay (tmp); 2728 return splay (tmp);
2706 2729
2707 return 0; 2730 return 0;
2708} 2731}
2709 2732
2710//-GPL
2711
2712void 2733void
2713object::force_set_timer (int duration) 2734object::force_set_timer (int duration)
2714{ 2735{
2715 this->duration = 1; 2736 this->duration = 1;
2716 this->speed_left = -1.f; 2737 this->speed_left = -1.f;
2722object::force_add (shstr_tmp name, int duration) 2743object::force_add (shstr_tmp name, int duration)
2723{ 2744{
2724 if (object *force = force_find (name)) 2745 if (object *force = force_find (name))
2725 force->destroy (); 2746 force->destroy ();
2726 2747
2727 object *force = get_archetype (FORCE_NAME); 2748 object *force = archetype::get (FORCE_NAME);
2728 2749
2729 force->slaying = name; 2750 force->slaying = name;
2730 force->force_set_timer (duration); 2751 force->force_set_timer (duration);
2731 force->flag [FLAG_APPLIED] = true; 2752 force->flag [FLAG_APPLIED] = true;
2732 2753
2816 return contr->mark; 2837 return contr->mark;
2817 else 2838 else
2818 return 0; 2839 return 0;
2819} 2840}
2820 2841
2842// put marked object first in the inventory
2843// this is used by identify-like spells so players can influence
2844// the order a bit.
2845void
2846object::splay_marked ()
2847{
2848 if (object *marked = mark ())
2849 splay (marked);
2850}
2851

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines