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.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}
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
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 ();
1239 1198
1240 above = 0; 1199 above = 0;
1241 below = 0; 1200 below = 0;
1242 1201
1243 ms.invalidate (); 1202 ms.invalidate ();
1244
1245 if (map->in_memory == MAP_SAVING)
1246 return;
1247 1203
1248 int check_walk_off = !flag [FLAG_NO_APPLY]; 1204 int check_walk_off = !flag [FLAG_NO_APPLY];
1249 1205
1250 if (object *pl = ms.player ()) 1206 if (object *pl = ms.player ())
1251 { 1207 {
1386{ 1342{
1387 op->remove (); 1343 op->remove ();
1388 1344
1389 if (m == &freed_map)//D TODO: remove soon 1345 if (m == &freed_map)//D TODO: remove soon
1390 {//D 1346 {//D
1391 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
1392 }//D 1348 }//D
1393 1349
1394 /* Ideally, the caller figures this out. However, it complicates a lot 1350 /* 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 1351 * of areas of callers (eg, anything that uses find_free_spot would now
1396 * need extra work 1352 * need extra work
2493 return tmp; 2449 return tmp;
2494 2450
2495 return 0; 2451 return 0;
2496} 2452}
2497 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
2498shstr_tmp 2470shstr_tmp
2499object::kv_get (shstr_tmp key) const 2471key_values::get (shstr_tmp key) const
2500{ 2472{
2501 for (key_value *kv = key_values; kv; kv = kv->next) 2473 for (key_value *kv = first; kv; kv = kv->next)
2502 if (kv->key == key) 2474 if (kv->key == key)
2503 return kv->value; 2475 return kv->value;
2504 2476
2505 return shstr (); 2477 return shstr ();
2506} 2478}
2507 2479
2508void 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
2509object::kv_set (shstr_tmp key, shstr_tmp value) 2493key_values::set (shstr_tmp key, shstr_tmp value)
2510{ 2494{
2511 for (key_value *kv = key_values; kv; kv = kv->next) 2495 for (key_value *kv = first; kv; kv = kv->next)
2512 if (kv->key == key) 2496 if (kv->key == key)
2513 { 2497 {
2514 kv->value = value; 2498 kv->value = value;
2515 return; 2499 return;
2516 } 2500 }
2517 2501
2518 key_value *kv = new key_value; 2502 add (key, value);
2519
2520 kv->next = key_values;
2521 kv->key = key;
2522 kv->value = value;
2523
2524 key_values = kv;
2525} 2503}
2526 2504
2527void 2505void
2528object::kv_del (shstr_tmp key) 2506key_values::del (shstr_tmp key)
2529{ 2507{
2530 for (key_value **kvp = &key_values; *kvp; kvp = &(*kvp)->next) 2508 for (key_value **kvp = &first; *kvp; kvp = &(*kvp)->next)
2531 if ((*kvp)->key == key) 2509 if ((*kvp)->key == key)
2532 { 2510 {
2533 key_value *kv = *kvp; 2511 key_value *kv = *kvp;
2534 *kvp = (*kvp)->next; 2512 *kvp = (*kvp)->next;
2535 delete kv; 2513 delete kv;
2536 return; 2514 return;
2537 } 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 ();
2538} 2544}
2539 2545
2540object::depth_iterator::depth_iterator (object *container) 2546object::depth_iterator::depth_iterator (object *container)
2541: iterator_base (container) 2547: iterator_base (container)
2542{ 2548{
2722object::force_add (shstr_tmp name, int duration) 2728object::force_add (shstr_tmp name, int duration)
2723{ 2729{
2724 if (object *force = force_find (name)) 2730 if (object *force = force_find (name))
2725 force->destroy (); 2731 force->destroy ();
2726 2732
2727 object *force = get_archetype (FORCE_NAME); 2733 object *force = archetype::get (FORCE_NAME);
2728 2734
2729 force->slaying = name; 2735 force->slaying = name;
2730 force->force_set_timer (duration); 2736 force->force_set_timer (duration);
2731 force->flag [FLAG_APPLIED] = true; 2737 force->flag [FLAG_APPLIED] = true;
2732 2738

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines