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.350 by root, Sun May 8 12:40:41 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
1010// special "grave" map used to store all removed objects
1011// till they can be destroyed - saves a lot of checks in the rest
1012// of the code
1051static struct freed_map : maptile 1013static struct freed_map
1014: maptile
1052{ 1015{
1053 freed_map () 1016 freed_map ()
1054 : maptile (3, 3) 1017 : maptile (3, 3)
1055 { 1018 {
1056 path = "<freed objects map>"; 1019 path = "<freed objects map>";
1057 name = "/internal/freed_objects_map"; 1020 name = "/internal/freed_objects_map";
1058 no_drop = 1; 1021 no_drop = 1;
1059 no_reset = 1; 1022 no_reset = 1;
1060 1023
1061 in_memory = MAP_ACTIVE; 1024 state = MAP_ACTIVE;
1062 } 1025 }
1063 1026
1064 ~freed_map () 1027 ~freed_map ()
1065 { 1028 {
1066 destroy (); 1029 destroy ();
1239 1202
1240 above = 0; 1203 above = 0;
1241 below = 0; 1204 below = 0;
1242 1205
1243 ms.invalidate (); 1206 ms.invalidate ();
1244
1245 if (map->in_memory == MAP_SAVING)
1246 return;
1247 1207
1248 int check_walk_off = !flag [FLAG_NO_APPLY]; 1208 int check_walk_off = !flag [FLAG_NO_APPLY];
1249 1209
1250 if (object *pl = ms.player ()) 1210 if (object *pl = ms.player ())
1251 { 1211 {
1386{ 1346{
1387 op->remove (); 1347 op->remove ();
1388 1348
1389 if (m == &freed_map)//D TODO: remove soon 1349 if (m == &freed_map)//D TODO: remove soon
1390 {//D 1350 {//D
1391 LOG (llevError | logBacktrace, "tries to insret object on freed objects map: %s", op->debug_desc ());//D 1351 LOG (llevError | logBacktrace, "tries to insert object on freed objects map: %s", op->debug_desc ());//D
1392 }//D 1352 }//D
1393 1353
1394 /* Ideally, the caller figures this out. However, it complicates a lot 1354 /* 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 1355 * of areas of callers (eg, anything that uses find_free_spot would now
1396 * need extra work 1356 * need extra work
2454 return /*who->flag [FLAG_WIZ]|| */ 2414 return /*who->flag [FLAG_WIZ]|| */
2455 (item->weight > 0 && !item->flag [FLAG_NO_PICK] && 2415 (item->weight > 0 && !item->flag [FLAG_NO_PICK] &&
2456 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3)); 2416 !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3));
2457} 2417}
2458 2418
2419//-GPL
2420
2459/* 2421/*
2460 * create clone from object to another 2422 * create clone from object to another
2461 */ 2423 */
2462object * 2424object *
2463object::deep_clone () 2425object::deep_clone ()
2493 return tmp; 2455 return tmp;
2494 2456
2495 return 0; 2457 return 0;
2496} 2458}
2497 2459
2460/* Zero the key_values on op, decrementing the shared-string
2461 * refcounts and freeing the links.
2462 */
2463void
2464key_values::clear ()
2465{
2466 for (key_value *kvp = first; kvp; )
2467 {
2468 key_value *next = kvp->next;
2469 delete kvp;
2470 kvp = next;
2471 }
2472
2473 first = 0;
2474}
2475
2498shstr_tmp 2476shstr_tmp
2499object::kv_get (shstr_tmp key) const 2477key_values::get (shstr_tmp key) const
2500{ 2478{
2501 for (key_value *kv = key_values; kv; kv = kv->next) 2479 for (key_value *kv = first; kv; kv = kv->next)
2502 if (kv->key == key) 2480 if (kv->key == key)
2503 return kv->value; 2481 return kv->value;
2504 2482
2505 return shstr (); 2483 return shstr ();
2506} 2484}
2507 2485
2508void 2486void
2487key_values::add (shstr_tmp key, shstr_tmp value)
2488{
2489 key_value *kv = new key_value;
2490
2491 kv->next = first;
2492 kv->key = key;
2493 kv->value = value;
2494
2495 first = kv;
2496}
2497
2498void
2509object::kv_set (shstr_tmp key, shstr_tmp value) 2499key_values::set (shstr_tmp key, shstr_tmp value)
2510{ 2500{
2511 for (key_value *kv = key_values; kv; kv = kv->next) 2501 for (key_value *kv = first; kv; kv = kv->next)
2512 if (kv->key == key) 2502 if (kv->key == key)
2513 { 2503 {
2514 kv->value = value; 2504 kv->value = value;
2515 return; 2505 return;
2516 } 2506 }
2517 2507
2518 key_value *kv = new key_value; 2508 add (key, value);
2519
2520 kv->next = key_values;
2521 kv->key = key;
2522 kv->value = value;
2523
2524 key_values = kv;
2525} 2509}
2526 2510
2527void 2511void
2528object::kv_del (shstr_tmp key) 2512key_values::del (shstr_tmp key)
2529{ 2513{
2530 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2514 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2531 if ((*kvp)->key == key) 2515 if ((*kvp)->key == key)
2532 { 2516 {
2533 key_value *kv = *kvp; 2517 key_value *kv = *kvp;
2534 *kvp = (*kvp)->next; 2518 *kvp = (*kvp)->next;
2535 delete kv; 2519 delete kv;
2536 return; 2520 return;
2537 } 2521 }
2522}
2523
2524void
2525key_values::reverse ()
2526{
2527 key_value *prev = 0;
2528 key_value *head = first;
2529
2530 while (head)
2531 {
2532 key_value *node = head;
2533 head = head->next;
2534 node->next = prev;
2535 prev = node;
2536 }
2537
2538 first = prev;
2539}
2540
2541key_values &
2542key_values::operator =(const key_values &kv)
2543{
2544 clear ();
2545
2546 for (key_value *kvp = kv.first; kvp; kvp = kvp->next)
2547 add (kvp->key, kvp->value);
2548
2549 reverse ();
2538} 2550}
2539 2551
2540object::depth_iterator::depth_iterator (object *container) 2552object::depth_iterator::depth_iterator (object *container)
2541: iterator_base (container) 2553: iterator_base (container)
2542{ 2554{
2625object::region () const 2637object::region () const
2626{ 2638{
2627 return map ? map->region (x, y) 2639 return map ? map->region (x, y)
2628 : region::default_region (); 2640 : region::default_region ();
2629} 2641}
2642
2643//+GPL
2630 2644
2631void 2645void
2632object::open_container (object *new_container) 2646object::open_container (object *new_container)
2633{ 2647{
2634 if (container == new_container) 2648 if (container == new_container)
2692 } 2706 }
2693// else if (!old_container->env && contr && contr->ns) 2707// else if (!old_container->env && contr && contr->ns)
2694// contr->ns->floorbox_reset (); 2708// contr->ns->floorbox_reset ();
2695} 2709}
2696 2710
2711//-GPL
2712
2713// prefetch some flat area around the player
2714static void
2715prefetch_surrounding_area (object *op, maptile *map, int range)
2716{
2717 for (maprect *rect = map->split_to_tiles (mapwalk_buf,
2718 op->x - range , op->y - range ,
2719 op->x + range + 1, op->y + range + 1);
2720 rect->m;
2721 ++rect)
2722 {
2723 rect->m->touch ();
2724 rect->m->activate ();
2725 }
2726}
2727
2728// prefetch a generous area around the player, also up and down
2729void
2730object::prefetch_surrounding_maps ()
2731{
2732 prefetch_surrounding_area (this, map, 40);
2733
2734 if (maptile *m = map->tile_available (TILE_DOWN))
2735 prefetch_surrounding_area (this, m, 20);
2736
2737 if (maptile *m = map->tile_available (TILE_UP))
2738 prefetch_surrounding_area (this, m, 20);
2739}
2740
2741//+GPL
2742
2697object * 2743object *
2698object::force_find (shstr_tmp name) 2744object::force_find (shstr_tmp name)
2699{ 2745{
2700 /* cycle through his inventory to look for the MARK we want to 2746 /* cycle through his inventory to look for the MARK we want to
2701 * place 2747 * place
2705 return splay (tmp); 2751 return splay (tmp);
2706 2752
2707 return 0; 2753 return 0;
2708} 2754}
2709 2755
2710//-GPL
2711
2712void 2756void
2713object::force_set_timer (int duration) 2757object::force_set_timer (int duration)
2714{ 2758{
2715 this->duration = 1; 2759 this->duration = 1;
2716 this->speed_left = -1.f; 2760 this->speed_left = -1.f;
2722object::force_add (shstr_tmp name, int duration) 2766object::force_add (shstr_tmp name, int duration)
2723{ 2767{
2724 if (object *force = force_find (name)) 2768 if (object *force = force_find (name))
2725 force->destroy (); 2769 force->destroy ();
2726 2770
2727 object *force = get_archetype (FORCE_NAME); 2771 object *force = archetype::get (FORCE_NAME);
2728 2772
2729 force->slaying = name; 2773 force->slaying = name;
2730 force->force_set_timer (duration); 2774 force->force_set_timer (duration);
2731 force->flag [FLAG_APPLIED] = true; 2775 force->flag [FLAG_APPLIED] = true;
2732 2776

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines