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.118 by root, Thu Jan 18 19:42:09 2007 UTC vs.
Revision 1.136 by root, Thu Mar 15 14:23:02 2007 UTC

357/* 357/*
358 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 358 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
359 * Some error messages. 359 * Some error messages.
360 * The result of the dump is stored in the static global errmsg array. 360 * The result of the dump is stored in the static global errmsg array.
361 */ 361 */
362
363char * 362char *
364dump_object (object *op) 363dump_object (object *op)
365{ 364{
366 if (!op) 365 if (!op)
367 return strdup ("[NULLOBJ]"); 366 return strdup ("[NULLOBJ]");
368 367
369 object_freezer freezer; 368 object_freezer freezer;
370 save_object (freezer, op, 1); 369 op->write (freezer);
371 return freezer.as_string (); 370 return freezer.as_string ();
372} 371}
373 372
374/* 373/*
375 * get_nearest_part(multi-object, object 2) returns the part of the 374 * get_nearest_part(multi-object, object 2) returns the part of the
522 } 521 }
523 } 522 }
524 } 523 }
525 524
526 dst->set_speed (dst->speed); 525 dst->set_speed (dst->speed);
526}
527
528void
529object::instantiate ()
530{
531 if (!uuid.seq) // HACK
532 uuid = gen_uuid ();
533
534 speed_left = -0.1f;
535 /* copy the body_info to the body_used - this is only really
536 * need for monsters, but doesn't hurt to do it for everything.
537 * by doing so, when a monster is created, it has good starting
538 * values for the body_used info, so when items are created
539 * for it, they can be properly equipped.
540 */
541 memcpy (body_used, body_info, sizeof (body_used));
542
543 attachable::instantiate ();
527} 544}
528 545
529object * 546object *
530object::clone () 547object::clone ()
531{ 548{
589 * UP_OBJ_FACE: only the objects face has changed. 606 * UP_OBJ_FACE: only the objects face has changed.
590 */ 607 */
591void 608void
592update_object (object *op, int action) 609update_object (object *op, int action)
593{ 610{
594 MoveType move_on, move_off, move_block, move_slow;
595
596 if (op == NULL) 611 if (op == NULL)
597 { 612 {
598 /* this should never happen */ 613 /* this should never happen */
599 LOG (llevDebug, "update_object() called for NULL object.\n"); 614 LOG (llevDebug, "update_object() called for NULL object.\n");
600 return; 615 return;
670 face = blank_face; 685 face = blank_face;
671} 686}
672 687
673object::~object () 688object::~object ()
674{ 689{
690 unlink ();
691
675 free_key_values (this); 692 free_key_values (this);
676} 693}
677 694
678static int object_count; 695static int object_count;
679 696
687 objects.insert (this); 704 objects.insert (this);
688} 705}
689 706
690void object::unlink () 707void object::unlink ()
691{ 708{
692 assert (index);//D 709 if (!index)
710 return;
711
693 objects.erase (this); 712 objects.erase (this);
694 refcnt_dec (); 713 refcnt_dec ();
695} 714}
696 715
697void 716void
771 * drop on that space. 790 * drop on that space.
772 */ 791 */
773 if (!drop_to_ground 792 if (!drop_to_ground
774 || !map 793 || !map
775 || map->in_memory != MAP_IN_MEMORY 794 || map->in_memory != MAP_IN_MEMORY
795 || map->nodrop
776 || ms ().move_block == MOVE_ALL) 796 || ms ().move_block == MOVE_ALL)
777 { 797 {
778 while (inv) 798 while (inv)
779 { 799 {
780 inv->destroy_inv (drop_to_ground); 800 inv->destroy_inv (drop_to_ground);
814 834
815 if (flag [FLAG_IS_LINKED]) 835 if (flag [FLAG_IS_LINKED])
816 remove_button_link (this); 836 remove_button_link (this);
817 837
818 if (flag [FLAG_FRIENDLY]) 838 if (flag [FLAG_FRIENDLY])
839 {
819 remove_friendly_object (this); 840 remove_friendly_object (this);
841
842 if (type == GOLEM
843 && owner
844 && owner->type == PLAYER
845 && owner->contr->ranges[range_golem] == this)
846 owner->contr->ranges[range_golem] = 0;
847 }
820 848
821 if (!flag [FLAG_REMOVED]) 849 if (!flag [FLAG_REMOVED])
822 remove (); 850 remove ();
823 851
824 destroy_inv (true); 852 destroy_inv (true);
896 * This function removes the object op from the linked list of objects 924 * This function removes the object op from the linked list of objects
897 * which it is currently tied to. When this function is done, the 925 * which it is currently tied to. When this function is done, the
898 * object will have no environment. If the object previously had an 926 * object will have no environment. If the object previously had an
899 * environment, the x and y coordinates will be updated to 927 * environment, the x and y coordinates will be updated to
900 * the previous environment. 928 * the previous environment.
901 * Beware: This function is called from the editor as well!
902 */ 929 */
903void 930void
904object::remove_slow () 931object::do_remove ()
905{ 932{
906 object *tmp, *last = 0; 933 object *tmp, *last = 0;
907 object *otmp; 934 object *otmp;
908 935
909 if (QUERY_FLAG (this, FLAG_REMOVED)) 936 if (QUERY_FLAG (this, FLAG_REMOVED))
952 } 979 }
953 else if (map) 980 else if (map)
954 { 981 {
955 if (type == PLAYER) 982 if (type == PLAYER)
956 { 983 {
984 // leaving a spot always closes any open container on the ground
985 if (container && !container->env)
986 // this causes spurious floorbox updates, but it ensures
987 // that the CLOSE event is being sent.
988 close_container ();
989
957 --map->players; 990 --map->players;
958 map->touch (); 991 map->touch ();
959 } 992 }
960 993
961 map->dirty = true; 994 map->dirty = true;
1256 * looks like instead of lots of conditions here. 1289 * looks like instead of lots of conditions here.
1257 * makes things faster, and effectively the same result. 1290 * makes things faster, and effectively the same result.
1258 */ 1291 */
1259 1292
1260 /* Have object 'fall below' other objects that block view. 1293 /* Have object 'fall below' other objects that block view.
1261 * Unless those objects are exits, type 66 1294 * Unless those objects are exits.
1262 * If INS_ON_TOP is used, don't do this processing 1295 * If INS_ON_TOP is used, don't do this processing
1263 * Need to find the object that in fact blocks view, otherwise 1296 * Need to find the object that in fact blocks view, otherwise
1264 * stacking is a bit odd. 1297 * stacking is a bit odd.
1265 */ 1298 */
1266 if (!(flag & INS_ON_TOP) 1299 if (!(flag & INS_ON_TOP)
1267 && ms.flags () & P_BLOCKSVIEW 1300 && ms.flags () & P_BLOCKSVIEW
1268 && (op->face && !op->face->visibility)) 1301 && (op->face && !faces [op->face].visibility))
1269 { 1302 {
1270 for (last = top; last != floor; last = last->below) 1303 for (last = top; last != floor; last = last->below)
1271 if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) 1304 if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT))
1272 break; 1305 break;
1273 1306
1396 tmp1->x = op->x; 1429 tmp1->x = op->x;
1397 tmp1->y = op->y; 1430 tmp1->y = op->y;
1398 insert_ob_in_map (tmp1, op->map, op, 0); 1431 insert_ob_in_map (tmp1, op->map, op, 0);
1399} 1432}
1400 1433
1434// XXX: function not returning object*
1401object * 1435object *
1402object::insert_at (object *where, object *originator, int flags) 1436object::insert_at (object *where, object *originator, int flags)
1403{ 1437{
1404 where->map->insert (this, where->x, where->y, originator, flags); 1438 where->map->insert (this, where->x, where->y, originator, flags);
1405} 1439}
1746 if ((!op->move_type && tmp->move_slow & MOVE_WALK) || 1780 if ((!op->move_type && tmp->move_slow & MOVE_WALK) ||
1747 ((op->move_type & tmp->move_slow) && (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0)) 1781 ((op->move_type & tmp->move_slow) && (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0))
1748 { 1782 {
1749 1783
1750 float 1784 float
1751 diff = tmp->move_slow_penalty * FABS (op->speed); 1785 diff = tmp->move_slow_penalty * fabs (op->speed);
1752 1786
1753 if (op->type == PLAYER) 1787 if (op->type == PLAYER)
1754 if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) || 1788 if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) ||
1755 (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN))) 1789 (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN)))
1756 diff /= 4.0; 1790 diff /= 4.0;
1963 } 1997 }
1964 1998
1965 if (!index) 1999 if (!index)
1966 return -1; 2000 return -1;
1967 2001
1968 return altern[RANDOM () % index]; 2002 return altern [rndm (index)];
1969} 2003}
1970 2004
1971/* 2005/*
1972 * find_first_free_spot(archetype, maptile, x, y) works like 2006 * find_first_free_spot(archetype, maptile, x, y) works like
1973 * find_free_spot(), but it will search max number of squares. 2007 * find_free_spot(), but it will search max number of squares.
1994{ 2028{
1995 arr += begin; 2029 arr += begin;
1996 end -= begin; 2030 end -= begin;
1997 2031
1998 while (--end) 2032 while (--end)
1999 swap (arr [end], arr [RANDOM () % (end + 1)]); 2033 swap (arr [end], arr [rndm (end + 1)]);
2000} 2034}
2001 2035
2002/* new function to make monster searching more efficient, and effective! 2036/* new function to make monster searching more efficient, and effective!
2003 * This basically returns a randomized array (in the passed pointer) of 2037 * This basically returns a randomized array (in the passed pointer) of
2004 * the spaces to find monsters. In this way, it won't always look for 2038 * the spaces to find monsters. In this way, it won't always look for
2316 insert_ob_in_ob (object_create_clone (item), dst); 2350 insert_ob_in_ob (object_create_clone (item), dst);
2317 2351
2318 return dst; 2352 return dst;
2319} 2353}
2320 2354
2321/* GROS - Creates an object using a string representing its content. */
2322/* Basically, we save the content of the string to a temp file, then call */
2323/* load_object on it. I admit it is a highly inefficient way to make things, */
2324/* but it was simple to make and allows reusing the load_object function. */
2325/* Remember not to use load_object_str in a time-critical situation. */
2326/* Also remember that multiparts objects are not supported for now. */
2327object *
2328load_object_str (const char *obstr)
2329{
2330 object *op;
2331 char filename[MAX_BUF];
2332
2333 sprintf (filename, "%s/cfloadobstr2044", settings.tmpdir);
2334
2335 FILE *tempfile = fopen (filename, "w");
2336
2337 if (tempfile == NULL)
2338 {
2339 LOG (llevError, "Error - Unable to access load object temp file\n");
2340 return NULL;
2341 }
2342
2343 fprintf (tempfile, obstr);
2344 fclose (tempfile);
2345
2346 op = object::create ();
2347
2348 object_thawer thawer (filename);
2349
2350 if (thawer)
2351 load_object (thawer, op, 0);
2352
2353 LOG (llevDebug, " load str completed, object=%s\n", &op->name);
2354 CLEAR_FLAG (op, FLAG_REMOVED);
2355
2356 return op;
2357}
2358
2359/* This returns the first object in who's inventory that 2355/* This returns the first object in who's inventory that
2360 * has the same type and subtype match. 2356 * has the same type and subtype match.
2361 * returns NULL if no match. 2357 * returns NULL if no match.
2362 */ 2358 */
2363object * 2359object *
2416 if (link->key == canonical_key) 2412 if (link->key == canonical_key)
2417 return link->value; 2413 return link->value;
2418 2414
2419 return 0; 2415 return 0;
2420} 2416}
2421
2422 2417
2423/* 2418/*
2424 * Updates the canonical_key in op to value. 2419 * Updates the canonical_key in op to value.
2425 * 2420 *
2426 * canonical_key is a shared string (value doesn't have to be). 2421 * canonical_key is a shared string (value doesn't have to be).
2566{ 2561{
2567 char flagdesc[512]; 2562 char flagdesc[512];
2568 char info2[256 * 4]; 2563 char info2[256 * 4];
2569 char *p = info; 2564 char *p = info;
2570 2565
2571 p += snprintf (p, 512, "{cnt:%d,uuid:<1,%" PRIx64 ">,name:\"%s\"%s%s,flags:[%s],type:%d}", 2566 p += snprintf (p, 512, "{cnt:%d,uuid:<1.%" PRIx64 ">,name:\"%s\"%s%s,flags:[%s],type:%d}",
2572 count, uuid.seq, 2567 count, uuid.seq,
2573 &name, 2568 &name,
2574 title ? "\",title:\"" : "", 2569 title ? "\",title:\"" : "",
2575 title ? (const char *)title : "", 2570 title ? (const char *)title : "",
2576 flag_desc (flagdesc, 512), type); 2571 flag_desc (flagdesc, 512), type);
2596{ 2591{
2597 static char info[256 * 4]; 2592 static char info[256 * 4];
2598 return debug_desc (info); 2593 return debug_desc (info);
2599} 2594}
2600 2595
2596struct region *
2597object::region () const
2598{
2599 return map ? map->region (x, y)
2600 : region::default_region ();
2601}
2602
2603const materialtype_t *
2604object::dominant_material () const
2605{
2606 if (materialtype_t *mat = name_to_material (materialname))
2607 return mat;
2608
2609 // omfg this is slow, this has to be temporary :)
2610 shstr unknown ("unknown");
2611
2612 return name_to_material (unknown);
2613}
2614
2615void
2616object::open_container (object *new_container)
2617{
2618 if (container == new_container)
2619 return;
2620
2621 if (object *old_container = container)
2622 {
2623 if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this)))
2624 return;
2625
2626#if 0
2627 // remove the "Close old_container" object.
2628 if (object *closer = old_container->inv)
2629 if (closer->type == CLOSE_CON)
2630 closer->destroy ();
2631#endif
2632
2633 old_container->flag [FLAG_APPLIED] = 0;
2634 container = 0;
2635
2636 esrv_update_item (UPD_FLAGS, this, old_container);
2637 new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container));
2638 }
2639
2640 if (new_container)
2641 {
2642 if (INVOKE_OBJECT (OPEN, new_container, ARG_OBJECT (this)))
2643 return;
2644
2645 // TODO: this does not seem to serve any purpose anymore?
2646#if 0
2647 // insert the "Close Container" object.
2648 if (archetype *closer = new_container->other_arch)
2649 {
2650 object *closer = arch_to_object (new_container->other_arch);
2651 closer->flag [FLAG_NO_MAP_SAVE] = 1;
2652 new_container->insert (closer);
2653 }
2654#endif
2655
2656 new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container));
2657
2658 new_container->flag [FLAG_APPLIED] = 1;
2659 container = new_container;
2660
2661 esrv_update_item (UPD_FLAGS, this, new_container);
2662 esrv_send_inventory (this, new_container);
2663 }
2664}
2665
2666

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines