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.343 by root, Sat Apr 23 04:56:46 2011 UTC vs.
Revision 1.353 by root, Fri Jan 27 22:00:39 2012 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,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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}
373 if (ob1->level != ob2->level) 373 if (ob1->level != ob2->level)
374 return 0; 374 return 0;
375 break; 375 break;
376 } 376 }
377 377
378 if (ob1->key_values || ob2->key_values) 378 if (!ob1->kv.empty () || !ob2->kv.empty ())
379 { 379 {
380 /* At least one of these has key_values. */ 380 /* At least one of these has key_values. */
381 if ((!ob1->key_values) != (!ob2->key_values)) 381 if (ob1->kv.empty () != ob2->kv.empty ())
382 return 0; /* One has fields, but the other one doesn't. */ 382 return 0; /* One has fields, but the other one doesn't. */
383 383
384 if (!compare_ob_value_lists (ob1, ob2)) 384 if (!compare_ob_value_lists (ob1, ob2))
385 return 0; 385 return 0;
386 } 386 }
599 } 599 }
600 600
601 this->owner = owner; 601 this->owner = owner;
602} 602}
603 603
604/* Zero the key_values on op, decrementing the shared-string
605 * refcounts and freeing the links.
606 */
607static void
608free_key_values (object *op)
609{
610 for (key_value *i = op->key_values; i; )
611 {
612 key_value *next = i->next;
613 delete i;
614
615 i = next;
616 }
617
618 op->key_values = 0;
619}
620
621/* 604/*
622 * copy_to first frees everything allocated by the dst object, 605 * copy_to first frees everything allocated by the dst object,
623 * and then copies the contents of itself into the second 606 * and then copies the contents of itself into the second
624 * object, allocating what needs to be allocated. Basically, any 607 * object, allocating what needs to be allocated. Basically, any
625 * 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,
629void 612void
630object::copy_to (object *dst) 613object::copy_to (object *dst)
631{ 614{
632 dst->remove (); 615 dst->remove ();
633 *(object_copy *)dst = *this; 616 *(object_copy *)dst = *this;
617
618 // maybe move to object_copy?
619 dst->kv = kv;
620
634 dst->flag [FLAG_REMOVED] = true; 621 dst->flag [FLAG_REMOVED] = true;
635
636 /* Copy over key_values, if any. */
637 if (key_values)
638 {
639 key_value *tail = 0;
640 dst->key_values = 0;
641
642 for (key_value *i = key_values; i; i = i->next)
643 {
644 key_value *new_link = new key_value;
645
646 new_link->next = 0;
647 new_link->key = i->key;
648 new_link->value = i->value;
649
650 /* Try and be clever here, too. */
651 if (!dst->key_values)
652 {
653 dst->key_values = new_link;
654 tail = new_link;
655 }
656 else
657 {
658 tail->next = new_link;
659 tail = new_link;
660 }
661 }
662 }
663
664 dst->activate (); 622 dst->activate ();
665} 623}
666 624
667void 625void
668object::instantiate () 626object::instantiate ()
779 } 737 }
780 738
781 mapspace &m = op->ms (); 739 mapspace &m = op->ms ();
782 740
783 if (!(m.flags_ & P_UPTODATE)) 741 if (!(m.flags_ & P_UPTODATE))
784 /* nop */; 742 m.update_up (); // nothing to do except copy up
785 else if (action == UP_OBJ_INSERT) 743 else if (action == UP_OBJ_INSERT)
786 { 744 {
787#if 0 745#if 0
788 // this is likely overkill, TODO: revisit (schmorp) 746 // this is likely overkill, TODO: revisit (schmorp)
789 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW)) 747 if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW))
810 * that is being removed. 768 * that is being removed.
811 */ 769 */
812 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) 770 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE)
813 m.invalidate (); 771 m.invalidate ();
814 else if (action == UP_OBJ_FACE) 772 else if (action == UP_OBJ_FACE)
815 /* Nothing to do for that case */ ; 773 m.update_up (); // nothing to do for that case, except copy up
816 else 774 else
817 LOG (llevError, "update_object called with invalid action: %d\n", action); 775 LOG (llevError, "update_object called with invalid action: %d\n", action);
818 776
819 if (op->more) 777 if (op->more)
820 update_object (op->more, action); 778 update_object (op->more, action);
831 789
832object::~object () 790object::~object ()
833{ 791{
834 unlink (); 792 unlink ();
835 793
836 free_key_values (this); 794 kv.clear ();
837} 795}
838 796
839void object::link () 797void object::link ()
840{ 798{
841 assert (!index);//D 799 assert (!index);//D
940 * if some form of movement is allowed, let objects 898 * if some form of movement is allowed, let objects
941 * drop on that space. 899 * drop on that space.
942 */ 900 */
943 if (!drop_to_ground 901 if (!drop_to_ground
944 || !map 902 || !map
945 || map->in_memory != MAP_ACTIVE 903 || !map->linkable ()
946 || map->no_drop 904 || map->no_drop
947 || ms ().move_block == MOVE_ALL) 905 || ms ().move_block == MOVE_ALL)
948 { 906 {
949 while (inv) 907 while (inv)
950 inv->destroy (); 908 inv->destroy ();
1046 li->count = count; 1004 li->count = count;
1047 1005
1048 freelist = li; 1006 freelist = li;
1049 ++free_count; 1007 ++free_count;
1050} 1008}
1051
1052static struct freed_map : maptile
1053{
1054 freed_map ()
1055 : maptile (3, 3)
1056 {
1057 path = "<freed objects map>";
1058 name = "/internal/freed_objects_map";
1059 no_drop = 1;
1060 no_reset = 1;
1061
1062 in_memory = MAP_ACTIVE;
1063 }
1064
1065 ~freed_map ()
1066 {
1067 destroy ();
1068 }
1069} freed_map; // freed objects are moved here to avoid crashes
1070 1009
1071void 1010void
1072object::do_destroy () 1011object::do_destroy ()
1073{ 1012{
1074 if (flag [FLAG_IS_LINKED]) 1013 if (flag [FLAG_IS_LINKED])
1240 1179
1241 above = 0; 1180 above = 0;
1242 below = 0; 1181 below = 0;
1243 1182
1244 ms.invalidate (); 1183 ms.invalidate ();
1245
1246 if (map->in_memory == MAP_SAVING)
1247 return;
1248 1184
1249 int check_walk_off = !flag [FLAG_NO_APPLY]; 1185 int check_walk_off = !flag [FLAG_NO_APPLY];
1250 1186
1251 if (object *pl = ms.player ()) 1187 if (object *pl = ms.player ())
1252 { 1188 {
1387{ 1323{
1388 op->remove (); 1324 op->remove ();
1389 1325
1390 if (m == &freed_map)//D TODO: remove soon 1326 if (m == &freed_map)//D TODO: remove soon
1391 {//D 1327 {//D
1392 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
1393 }//D 1329 }//D
1394 1330
1395 /* Ideally, the caller figures this out. However, it complicates a lot 1331 /* Ideally, the caller figures this out. However, it complicates a lot
1396 * 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
1397 * need extra work 1333 * need extra work
2072 */ 2008 */
2073int 2009int
2074find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop) 2010find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop)
2075{ 2011{
2076 int altern[SIZEOFFREE]; 2012 int altern[SIZEOFFREE];
2077 int index = 0, flag; 2013 int index = 0;
2078 2014
2079 for (int i = start; i < stop; i++) 2015 for (int i = start; i < stop; i++)
2080 { 2016 {
2081 mapxy pos (m, x, y); pos.move (i); 2017 mapxy pos (m, x, y); pos.move (i);
2082 2018
2194 * there is capable of. 2130 * there is capable of.
2195 */ 2131 */
2196int 2132int
2197find_dir (maptile *m, int x, int y, object *exclude) 2133find_dir (maptile *m, int x, int y, object *exclude)
2198{ 2134{
2199 int max = SIZEOFFREE, mflags; 2135 int max = SIZEOFFREE;
2200 MoveType move_type; 2136 MoveType move_type;
2201 2137
2202 if (exclude && exclude->head_ () != exclude) 2138 if (exclude && exclude->head_ () != exclude)
2203 { 2139 {
2204 exclude = exclude->head; 2140 exclude = exclude->head;
2455 return /*who->flag [FLAG_WIZ]|| */ 2391 return /*who->flag [FLAG_WIZ]|| */
2456 (item->weight > 0 && !item->flag [FLAG_NO_PICK] && 2392 (item->weight > 0 && !item->flag [FLAG_NO_PICK] &&
2457 !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));
2458} 2394}
2459 2395
2396//-GPL
2397
2460/* 2398/*
2461 * create clone from object to another 2399 * create clone from object to another
2462 */ 2400 */
2463object * 2401object *
2464object::deep_clone () 2402object::deep_clone ()
2494 return tmp; 2432 return tmp;
2495 2433
2496 return 0; 2434 return 0;
2497} 2435}
2498 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
2499shstr_tmp 2453shstr_tmp
2500object::kv_get (shstr_tmp key) const 2454key_values::get (shstr_tmp key) const
2501{ 2455{
2502 for (key_value *kv = key_values; kv; kv = kv->next) 2456 for (key_value *kv = first; kv; kv = kv->next)
2503 if (kv->key == key) 2457 if (kv->key == key)
2504 return kv->value; 2458 return kv->value;
2505 2459
2506 return shstr (); 2460 return shstr ();
2507} 2461}
2508 2462
2509void 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
2510object::kv_set (shstr_tmp key, shstr_tmp value) 2476key_values::set (shstr_tmp key, shstr_tmp value)
2511{ 2477{
2512 for (key_value *kv = key_values; kv; kv = kv->next) 2478 for (key_value *kv = first; kv; kv = kv->next)
2513 if (kv->key == key) 2479 if (kv->key == key)
2514 { 2480 {
2515 kv->value = value; 2481 kv->value = value;
2516 return; 2482 return;
2517 } 2483 }
2518 2484
2519 key_value *kv = new key_value; 2485 add (key, value);
2520
2521 kv->next = key_values;
2522 kv->key = key;
2523 kv->value = value;
2524
2525 key_values = kv;
2526} 2486}
2527 2487
2528void 2488void
2529object::kv_del (shstr_tmp key) 2489key_values::del (shstr_tmp key)
2530{ 2490{
2531 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2491 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2532 if ((*kvp)->key == key) 2492 if ((*kvp)->key == key)
2533 { 2493 {
2534 key_value *kv = *kvp; 2494 key_value *kv = *kvp;
2535 *kvp = (*kvp)->next; 2495 *kvp = (*kvp)->next;
2536 delete kv; 2496 delete kv;
2537 return; 2497 return;
2538 } 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 ();
2539} 2527}
2540 2528
2541object::depth_iterator::depth_iterator (object *container) 2529object::depth_iterator::depth_iterator (object *container)
2542: iterator_base (container) 2530: iterator_base (container)
2543{ 2531{
2626object::region () const 2614object::region () const
2627{ 2615{
2628 return map ? map->region (x, y) 2616 return map ? map->region (x, y)
2629 : region::default_region (); 2617 : region::default_region ();
2630} 2618}
2619
2620//+GPL
2631 2621
2632void 2622void
2633object::open_container (object *new_container) 2623object::open_container (object *new_container)
2634{ 2624{
2635 if (container == new_container) 2625 if (container == new_container)
2693 } 2683 }
2694// else if (!old_container->env && contr && contr->ns) 2684// else if (!old_container->env && contr && contr->ns)
2695// contr->ns->floorbox_reset (); 2685// contr->ns->floorbox_reset ();
2696} 2686}
2697 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
2698object * 2720object *
2699object::force_find (shstr_tmp name) 2721object::force_find (shstr_tmp name)
2700{ 2722{
2701 /* 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
2702 * place 2724 * place
2706 return splay (tmp); 2728 return splay (tmp);
2707 2729
2708 return 0; 2730 return 0;
2709} 2731}
2710 2732
2711//-GPL
2712
2713void 2733void
2714object::force_set_timer (int duration) 2734object::force_set_timer (int duration)
2715{ 2735{
2716 this->duration = 1; 2736 this->duration = 1;
2717 this->speed_left = -1.f; 2737 this->speed_left = -1.f;
2723object::force_add (shstr_tmp name, int duration) 2743object::force_add (shstr_tmp name, int duration)
2724{ 2744{
2725 if (object *force = force_find (name)) 2745 if (object *force = force_find (name))
2726 force->destroy (); 2746 force->destroy ();
2727 2747
2728 object *force = get_archetype (FORCE_NAME); 2748 object *force = archetype::get (FORCE_NAME);
2729 2749
2730 force->slaying = name; 2750 force->slaying = name;
2731 force->force_set_timer (duration); 2751 force->force_set_timer (duration);
2732 force->flag [FLAG_APPLIED] = true; 2752 force->flag [FLAG_APPLIED] = true;
2733 2753

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines