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.342 by sf-kernelpanic, Sun Apr 10 20:30:53 2011 UTC vs.
Revision 1.346 by root, Wed May 4 07:36:40 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}
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 ()
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 ();
1240 1198
1241 above = 0; 1199 above = 0;
1242 below = 0; 1200 below = 0;
1243 1201
1244 ms.invalidate (); 1202 ms.invalidate ();
1245
1246 if (map->in_memory == MAP_SAVING)
1247 return;
1248 1203
1249 int check_walk_off = !flag [FLAG_NO_APPLY]; 1204 int check_walk_off = !flag [FLAG_NO_APPLY];
1250 1205
1251 if (object *pl = ms.player ()) 1206 if (object *pl = ms.player ())
1252 { 1207 {
1387{ 1342{
1388 op->remove (); 1343 op->remove ();
1389 1344
1390 if (m == &freed_map)//D TODO: remove soon 1345 if (m == &freed_map)//D TODO: remove soon
1391 {//D 1346 {//D
1392 LOG (llevError | logBacktrace, "tries to insret object on freed objects map: %s", op->debug_desc ());//D 1347 LOG (llevError | logBacktrace, "tries to insert object on freed objects map: %s", op->debug_desc ());//D
1393 }//D 1348 }//D
1394 1349
1395 /* Ideally, the caller figures this out. However, it complicates a lot 1350 /* 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 1351 * of areas of callers (eg, anything that uses find_free_spot would now
1397 * need extra work 1352 * need extra work
2494 return tmp; 2449 return tmp;
2495 2450
2496 return 0; 2451 return 0;
2497} 2452}
2498 2453
2454/* Zero the key_values on op, decrementing the shared-string
2455 * refcounts and freeing the links.
2456 */
2457void
2458key_values::clear ()
2459{
2460 for (key_value *kvp = first; kvp; )
2461 {
2462 key_value *next = kvp->next;
2463 delete kvp;
2464 kvp = next;
2465 }
2466
2467 first = 0;
2468}
2469
2499shstr_tmp 2470shstr_tmp
2500object::kv_get (shstr_tmp key) const 2471key_values::get (shstr_tmp key) const
2501{ 2472{
2502 for (key_value *kv = key_values; kv; kv = kv->next) 2473 for (key_value *kv = first; kv; kv = kv->next)
2503 if (kv->key == key) 2474 if (kv->key == key)
2504 return kv->value; 2475 return kv->value;
2505 2476
2506 return shstr (); 2477 return shstr ();
2507} 2478}
2508 2479
2509void 2480void
2481key_values::add (shstr_tmp key, shstr_tmp value)
2482{
2483 key_value *kv = new key_value;
2484
2485 kv->next = first;
2486 kv->key = key;
2487 kv->value = value;
2488
2489 first = kv;
2490}
2491
2492void
2510object::kv_set (shstr_tmp key, shstr_tmp value) 2493key_values::set (shstr_tmp key, shstr_tmp value)
2511{ 2494{
2512 for (key_value *kv = key_values; kv; kv = kv->next) 2495 for (key_value *kv = first; kv; kv = kv->next)
2513 if (kv->key == key) 2496 if (kv->key == key)
2514 { 2497 {
2515 kv->value = value; 2498 kv->value = value;
2516 return; 2499 return;
2517 } 2500 }
2518 2501
2519 key_value *kv = new key_value; 2502 add (key, value);
2520
2521 kv->next = key_values;
2522 kv->key = key;
2523 kv->value = value;
2524
2525 key_values = kv;
2526} 2503}
2527 2504
2528void 2505void
2529object::kv_del (shstr_tmp key) 2506key_values::del (shstr_tmp key)
2530{ 2507{
2531 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2508 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2532 if ((*kvp)->key == key) 2509 if ((*kvp)->key == key)
2533 { 2510 {
2534 key_value *kv = *kvp; 2511 key_value *kv = *kvp;
2535 *kvp = (*kvp)->next; 2512 *kvp = (*kvp)->next;
2536 delete kv; 2513 delete kv;
2537 return; 2514 return;
2538 } 2515 }
2516}
2517
2518void
2519key_values::reverse ()
2520{
2521 key_value *prev = 0;
2522 key_value *head = first;
2523
2524 while (head)
2525 {
2526 key_value *node = head;
2527 head = head->next;
2528 node->next = prev;
2529 prev = node;
2530 }
2531
2532 first = prev;
2533}
2534
2535key_values &
2536key_values::operator =(const key_values &kv)
2537{
2538 clear ();
2539
2540 for (key_value *kvp = kv.first; kvp; kvp = kvp->next)
2541 add (kvp->key, kvp->value);
2542
2543 reverse ();
2539} 2544}
2540 2545
2541object::depth_iterator::depth_iterator (object *container) 2546object::depth_iterator::depth_iterator (object *container)
2542: iterator_base (container) 2547: iterator_base (container)
2543{ 2548{
2723object::force_add (shstr_tmp name, int duration) 2728object::force_add (shstr_tmp name, int duration)
2724{ 2729{
2725 if (object *force = force_find (name)) 2730 if (object *force = force_find (name))
2726 force->destroy (); 2731 force->destroy ();
2727 2732
2728 object *force = get_archetype (FORCE_NAME); 2733 object *force = archetype::get (FORCE_NAME);
2729 2734
2730 force->slaying = name; 2735 force->slaying = name;
2731 force->force_set_timer (duration); 2736 force->force_set_timer (duration);
2732 force->flag [FLAG_APPLIED] = true; 2737 force->flag [FLAG_APPLIED] = true;
2733 2738

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines