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.83 by root, Mon Dec 25 14:43:22 2006 UTC vs.
Revision 1.95 by root, Wed Dec 27 05:42:08 2006 UTC

141 * objects with lists are rare, and lists stay short. If not, use a 141 * objects with lists are rare, and lists stay short. If not, use a
142 * different structure or at least keep the lists sorted... 142 * different structure or at least keep the lists sorted...
143 */ 143 */
144 144
145 /* For each field in wants, */ 145 /* For each field in wants, */
146 for (wants_field = wants->key_values; wants_field != NULL; wants_field = wants_field->next) 146 for (wants_field = wants->key_values; wants_field; wants_field = wants_field->next)
147 { 147 {
148 key_value *has_field; 148 key_value *has_field;
149 149
150 /* Look for a field in has with the same key. */ 150 /* Look for a field in has with the same key. */
151 has_field = get_ob_key_link (has, wants_field->key); 151 has_field = get_ob_key_link (has, wants_field->key);
364{ 364{
365 if (!op) 365 if (!op)
366 return strdup ("[NULLOBJ]"); 366 return strdup ("[NULLOBJ]");
367 367
368 object_freezer freezer; 368 object_freezer freezer;
369 save_object (freezer, op, 3); 369 save_object (freezer, op, 1);
370 return freezer.as_string (); 370 return freezer.as_string ();
371} 371}
372 372
373/* 373/*
374 * 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 tail = new_link; 522 tail = new_link;
523 } 523 }
524 } 524 }
525 } 525 }
526 526
527 update_ob_speed (dst); 527 dst->set_speed (dst->speed);
528} 528}
529 529
530object * 530object *
531object::clone () 531object::clone ()
532{ 532{
554 * Updates the speed of an object. If the speed changes from 0 to another 554 * Updates the speed of an object. If the speed changes from 0 to another
555 * value, or vice versa, then add/remove the object from the active list. 555 * value, or vice versa, then add/remove the object from the active list.
556 * This function needs to be called whenever the speed of an object changes. 556 * This function needs to be called whenever the speed of an object changes.
557 */ 557 */
558void 558void
559update_ob_speed (object *op) 559object::set_speed (float speed)
560{ 560{
561 extern int arch_init; 561 extern int arch_init;
562 562
563 /* No reason putting the archetypes objects on the speed list, 563 /* No reason putting the archetypes objects on the speed list,
564 * since they never really need to be updated. 564 * since they never really need to be updated.
565 */ 565 */
566 566 if (flag [FLAG_FREED] && speed)
567 if (QUERY_FLAG (op, FLAG_FREED) && op->speed)
568 { 567 {
569 LOG (llevError, "Object %s is freed but has speed.\n", &op->name); 568 LOG (llevError, "Object %s is freed but has speed.\n", &name);
570#ifdef MANY_CORES
571 abort ();
572#else
573 op->speed = 0; 569 speed = 0;
574#endif
575 } 570 }
571
572 this->speed = speed;
576 573
577 if (arch_init) 574 if (arch_init)
578 return; 575 return;
579 576
580 if (FABS (op->speed) > MIN_ACTIVE_SPEED) 577 if (FABS (speed) > MIN_ACTIVE_SPEED)
581 { 578 {
582 /* If already on active list, don't do anything */ 579 /* If already on active list, don't do anything */
583 if (op->active_next || op->active_prev || op == active_objects) 580 if (active_next || active_prev || this == active_objects)
584 return; 581 return;
585 582
586 /* process_events() expects us to insert the object at the beginning 583 /* process_events() expects us to insert the object at the beginning
587 * of the list. */ 584 * of the list. */
588 op->active_next = active_objects; 585 active_next = active_objects;
589 586
590 if (op->active_next != NULL) 587 if (active_next)
591 op->active_next->active_prev = op; 588 active_next->active_prev = this;
592 589
593 active_objects = op; 590 active_objects = this;
594 } 591 }
595 else 592 else
596 { 593 {
597 /* If not on the active list, nothing needs to be done */ 594 /* If not on the active list, nothing needs to be done */
598 if (!op->active_next && !op->active_prev && op != active_objects) 595 if (!active_next && !active_prev && this != active_objects)
599 return; 596 return;
600 597
601 if (op->active_prev == NULL) 598 if (!active_prev)
602 { 599 {
603 active_objects = op->active_next; 600 active_objects = active_next;
604 601
605 if (op->active_next != NULL) 602 if (active_next)
606 op->active_next->active_prev = NULL; 603 active_next->active_prev = 0;
607 } 604 }
608 else 605 else
609 { 606 {
610 op->active_prev->active_next = op->active_next; 607 active_prev->active_next = active_next;
611 608
612 if (op->active_next) 609 if (active_next)
613 op->active_next->active_prev = op->active_prev; 610 active_next->active_prev = active_prev;
614 } 611 }
615 612
616 op->active_next = NULL; 613 active_next = 0;
617 op->active_prev = NULL; 614 active_prev = 0;
618 } 615 }
619} 616}
620 617
621/* This function removes object 'op' from the list of active 618/* This function removes object 'op' from the list of active
622 * objects. 619 * objects.
781 778
782 prev = 0; 779 prev = 0;
783 next = 0; 780 next = 0;
784} 781}
785 782
786object *object::create ()
787{
788 object *op = new object;
789 op->link ();
790 return op;
791}
792
793/*
794 * free_object() frees everything allocated by an object, removes
795 * it from the list of used objects, and puts it on the list of
796 * free objects. The IS_FREED() flag is set in the object.
797 * The object must have been removed by remove_ob() first for
798 * this function to succeed.
799 *
800 * If destroy_inventory is set, free inventory as well. Else drop items in
801 * inventory to the ground.
802 */
803void
804object::do_destroy ()
805{
806 attachable::do_destroy ();
807
808 if (flag [FLAG_IS_LINKED])
809 remove_button_link (this);
810
811 if (flag [FLAG_FRIENDLY])
812 remove_friendly_object (this);
813
814 if (!flag [FLAG_REMOVED])
815 remove ();
816
817 if (flag [FLAG_FREED])
818 return;
819
820 flag [FLAG_FREED] = 1;
821
822 // hack to ensure that freed objects still have a valid map
823 {
824 static maptile *freed_map; // freed objects are moved here to avoid crashes
825
826 if (!freed_map)
827 {
828 freed_map = new maptile;
829
830 freed_map->name = "/internal/freed_objects_map";
831 freed_map->width = 3;
832 freed_map->height = 3;
833
834 freed_map->allocate ();
835 }
836
837 map = freed_map;
838 x = 1;
839 y = 1;
840 }
841
842 more = 0;
843 head = 0;
844 inv = 0;
845
846 // clear those pointers that likely might have circular references to us
847 owner = 0;
848 enemy = 0;
849 attacked_by = 0;
850
851 // only relevant for players(?), but make sure of it anyways
852 contr = 0;
853
854 /* Remove object from the active list */
855 speed = 0;
856 update_ob_speed (this);
857
858 unlink ();
859}
860
861/* 783/*
862 * Remove and free all objects in the inventory of the given object. 784 * Remove and free all objects in the inventory of the given object.
863 * object.c ? 785 * object.c ?
864 */ 786 */
865void 787void
866object::destroy_inv (bool drop_to_ground) 788object::destroy_inv (bool drop_to_ground)
867{ 789{
790 // need to check first, because the checks below might segfault
791 // as we might be on an invalid mapspace and crossfire code
792 // is too buggy to ensure that the inventory is empty.
793 // corollary: if you create arrows etc. with stuff in tis inventory,
794 // cf will crash below with off-map x and y
868 if (!inv) 795 if (!inv)
869 return; 796 return;
870 797
871 /* Only if the space blocks everything do we not process - 798 /* Only if the space blocks everything do we not process -
872 * if some form of movement is allowed, let objects 799 * if some form of movement is allowed, let objects
873 * drop on that space. 800 * drop on that space.
874 */ 801 */
875 if (!drop_to_ground || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) 802 if (!drop_to_ground
803 || !map
804 || map->in_memory != MAP_IN_MEMORY
805 || ms ().move_block == MOVE_ALL)
876 { 806 {
877 while (inv) 807 while (inv)
878 { 808 {
879 inv->destroy_inv (drop_to_ground); 809 inv->destroy_inv (drop_to_ground);
880 inv->destroy (); 810 inv->destroy ();
891 || op->type == RUNE 821 || op->type == RUNE
892 || op->type == TRAP 822 || op->type == TRAP
893 || op->flag [FLAG_IS_A_TEMPLATE]) 823 || op->flag [FLAG_IS_A_TEMPLATE])
894 op->destroy (); 824 op->destroy ();
895 else 825 else
896 { 826 map->insert (op, x, y);
897 op->remove ();
898 op->x = x;
899 op->y = y;
900 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */
901 }
902 } 827 }
903 } 828 }
829}
830
831object *object::create ()
832{
833 object *op = new object;
834 op->link ();
835 return op;
836}
837
838void
839object::do_destroy ()
840{
841 if (flag [FLAG_IS_LINKED])
842 remove_button_link (this);
843
844 if (flag [FLAG_FRIENDLY])
845 remove_friendly_object (this);
846
847 if (!flag [FLAG_REMOVED])
848 remove ();
849
850 if (flag [FLAG_FREED])
851 return;
852
853 set_speed (0);
854
855 flag [FLAG_FREED] = 1;
856
857 attachable::do_destroy ();
858
859 destroy_inv (true);
860 unlink ();
861
862 // hack to ensure that freed objects still have a valid map
863 {
864 static maptile *freed_map; // freed objects are moved here to avoid crashes
865
866 if (!freed_map)
867 {
868 freed_map = new maptile;
869
870 freed_map->name = "/internal/freed_objects_map";
871 freed_map->width = 3;
872 freed_map->height = 3;
873
874 freed_map->allocate ();
875 }
876
877 map = freed_map;
878 x = 1;
879 y = 1;
880 }
881
882 head = 0;
883
884 if (more)
885 {
886 more->destroy ();
887 more = 0;
888 }
889
890 // clear those pointers that likely might have circular references to us
891 owner = 0;
892 enemy = 0;
893 attacked_by = 0;
894
895 // only relevant for players(?), but make sure of it anyways
896 contr = 0;
904} 897}
905 898
906void 899void
907object::destroy (bool destroy_inventory) 900object::destroy (bool destroy_inventory)
908{ 901{
909 if (destroyed ()) 902 if (destroyed ())
910 return; 903 return;
911 904
912 if (more)
913 {
914 //TODO: non-head objects must not have inventory
915 more->destroy (destroy_inventory);
916 more = 0;
917 }
918
919 if (destroy_inventory) 905 if (destroy_inventory)
920 destroy_inv (true); 906 destroy_inv (false);
921 907
922 attachable::destroy (); 908 attachable::destroy ();
923} 909}
924 910
925/* 911/*
1027 dump = dump_object (GET_MAP_OB (map, x, y)); 1013 dump = dump_object (GET_MAP_OB (map, x, y));
1028 LOG (llevError, "%s\n", dump); 1014 LOG (llevError, "%s\n", dump);
1029 free (dump); 1015 free (dump);
1030 } 1016 }
1031 1017
1032 map->at (x, y).bottom = above; /* goes on above it. */ 1018 map->at (x, y).bot = above; /* goes on above it. */
1033 } 1019 }
1034 1020
1035 above = 0; 1021 above = 0;
1036 below = 0; 1022 below = 0;
1037 1023
1038 if (map->in_memory == MAP_SAVING) 1024 if (map->in_memory == MAP_SAVING)
1039 return; 1025 return;
1040 1026
1041 int check_walk_off = !flag [FLAG_NO_APPLY]; 1027 int check_walk_off = !flag [FLAG_NO_APPLY];
1042 1028
1043 for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) 1029 for (tmp = map->at (x, y).bot; tmp; tmp = tmp->above)
1044 { 1030 {
1045 /* No point updating the players look faces if he is the object 1031 /* No point updating the players look faces if he is the object
1046 * being removed. 1032 * being removed.
1047 */ 1033 */
1048 1034
1134 * job preparing multi-part monsters 1120 * job preparing multi-part monsters
1135 */ 1121 */
1136object * 1122object *
1137insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y) 1123insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y)
1138{ 1124{
1139 object *tmp;
1140
1141 if (op->head)
1142 op = op->head;
1143
1144 for (tmp = op; tmp; tmp = tmp->more) 1125 for (object *tmp = op->head_ (); tmp; tmp = tmp->more)
1145 { 1126 {
1146 tmp->x = x + tmp->arch->clone.x; 1127 tmp->x = x + tmp->arch->clone.x;
1147 tmp->y = y + tmp->arch->clone.y; 1128 tmp->y = y + tmp->arch->clone.y;
1148 } 1129 }
1149 1130
1281 op->below = originator->below; 1262 op->below = originator->below;
1282 1263
1283 if (op->below) 1264 if (op->below)
1284 op->below->above = op; 1265 op->below->above = op;
1285 else 1266 else
1286 op->ms ().bottom = op; 1267 op->ms ().bot = op;
1287 1268
1288 /* since *below* originator, no need to update top */ 1269 /* since *below* originator, no need to update top */
1289 originator->below = op; 1270 originator->below = op;
1290 } 1271 }
1291 else 1272 else
1370 1351
1371 if (op->above) 1352 if (op->above)
1372 op->above->below = op; 1353 op->above->below = op;
1373 1354
1374 op->below = NULL; 1355 op->below = NULL;
1375 op->ms ().bottom = op; 1356 op->ms ().bot = op;
1376 } 1357 }
1377 else 1358 else
1378 { /* get inserted into the stack above top */ 1359 { /* get inserted into the stack above top */
1379 op->above = top->above; 1360 op->above = top->above;
1380 1361
1407 * be far away from this change and not affected in any way - 1388 * be far away from this change and not affected in any way -
1408 * this should get redone to only look for players within range, 1389 * this should get redone to only look for players within range,
1409 * or just updating the P_NEED_UPDATE for spaces within this area 1390 * or just updating the P_NEED_UPDATE for spaces within this area
1410 * of effect may be sufficient. 1391 * of effect may be sufficient.
1411 */ 1392 */
1412 if (MAP_DARKNESS (op->map) && (op->glow_radius != 0)) 1393 if (op->map->darkness && (op->glow_radius != 0))
1413 update_all_los (op->map, op->x, op->y); 1394 update_all_los (op->map, op->x, op->y);
1414 1395
1415 /* updates flags (blocked, alive, no magic, etc) for this map space */ 1396 /* updates flags (blocked, alive, no magic, etc) for this map space */
1416 update_object (op, UP_OBJ_INSERT); 1397 update_object (op, UP_OBJ_INSERT);
1417 1398
1463 tmp1->x = op->x; 1444 tmp1->x = op->x;
1464 tmp1->y = op->y; 1445 tmp1->y = op->y;
1465 insert_ob_in_map (tmp1, op->map, op, 0); 1446 insert_ob_in_map (tmp1, op->map, op, 0);
1466} 1447}
1467 1448
1449object *
1450object::insert_at (object *where, object *originator, int flags)
1451{
1452 where->map->insert (this, where->x, where->y, originator, flags);
1453}
1454
1468/* 1455/*
1469 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1456 * get_split_ob(ob,nr) splits up ob into two parts. The part which
1470 * is returned contains nr objects, and the remaining parts contains 1457 * is returned contains nr objects, and the remaining parts contains
1471 * the rest (or is removed and freed if that number is 0). 1458 * the rest (or is removed and freed if that number is 0).
1472 * On failure, NULL is returned, and the reason put into the 1459 * On failure, NULL is returned, and the reason put into the
1473 * global static errmsg array. 1460 * global static errmsg array.
1474 */ 1461 */
1475
1476object * 1462object *
1477get_split_ob (object *orig_ob, uint32 nr) 1463get_split_ob (object *orig_ob, uint32 nr)
1478{ 1464{
1479 object *newob; 1465 object *newob;
1480 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1466 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1703 if ((op->glow_radius != 0) && map) 1689 if ((op->glow_radius != 0) && map)
1704 { 1690 {
1705#ifdef DEBUG_LIGHTS 1691#ifdef DEBUG_LIGHTS
1706 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name); 1692 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name);
1707#endif /* DEBUG_LIGHTS */ 1693#endif /* DEBUG_LIGHTS */
1708 if (MAP_DARKNESS (map)) 1694 if (map->darkness)
1709 update_all_los (map, x, y); 1695 update_all_los (map, x, y);
1710 } 1696 }
1711 1697
1712 /* Client has no idea of ordering so lets not bother ordering it here. 1698 /* Client has no idea of ordering so lets not bother ordering it here.
1713 * It sure simplifies this function... 1699 * It sure simplifies this function...
1744 * 1730 *
1745 * MSW 2001-07-08: Check all objects on space, not just those below 1731 * MSW 2001-07-08: Check all objects on space, not just those below
1746 * object being inserted. insert_ob_in_map may not put new objects 1732 * object being inserted. insert_ob_in_map may not put new objects
1747 * on top. 1733 * on top.
1748 */ 1734 */
1749
1750int 1735int
1751check_move_on (object *op, object *originator) 1736check_move_on (object *op, object *originator)
1752{ 1737{
1753 object *tmp; 1738 object *tmp;
1754 maptile *m = op->map; 1739 maptile *m = op->map;
1781 1766
1782 /* The objects have to be checked from top to bottom. 1767 /* The objects have to be checked from top to bottom.
1783 * Hence, we first go to the top: 1768 * Hence, we first go to the top:
1784 */ 1769 */
1785 1770
1786 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL && tmp->above != NULL; tmp = tmp->above) 1771 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp && tmp->above; tmp = tmp->above)
1787 { 1772 {
1788 /* Trim the search when we find the first other spell effect 1773 /* Trim the search when we find the first other spell effect
1789 * this helps performance so that if a space has 50 spell objects, 1774 * this helps performance so that if a space has 50 spell objects,
1790 * we don't need to check all of them. 1775 * we don't need to check all of them.
1791 */ 1776 */
1846/* 1831/*
1847 * present_arch(arch, map, x, y) searches for any objects with 1832 * present_arch(arch, map, x, y) searches for any objects with
1848 * a matching archetype at the given map and coordinates. 1833 * a matching archetype at the given map and coordinates.
1849 * The first matching object is returned, or NULL if none. 1834 * The first matching object is returned, or NULL if none.
1850 */ 1835 */
1851
1852object * 1836object *
1853present_arch (const archetype *at, maptile *m, int x, int y) 1837present_arch (const archetype *at, maptile *m, int x, int y)
1854{ 1838{
1855 object *
1856 tmp;
1857
1858 if (m == NULL || out_of_map (m, x, y)) 1839 if (m == NULL || out_of_map (m, x, y))
1859 { 1840 {
1860 LOG (llevError, "Present_arch called outside map.\n"); 1841 LOG (llevError, "Present_arch called outside map.\n");
1861 return NULL; 1842 return NULL;
1862 } 1843 }
1844
1863 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1845 for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
1864 if (tmp->arch == at) 1846 if (tmp->arch == at)
1865 return tmp; 1847 return tmp;
1848
1866 return NULL; 1849 return NULL;
1867} 1850}
1868 1851
1869/* 1852/*
1870 * present(type, map, x, y) searches for any objects with 1853 * present(type, map, x, y) searches for any objects with
1871 * a matching type variable at the given map and coordinates. 1854 * a matching type variable at the given map and coordinates.
1872 * The first matching object is returned, or NULL if none. 1855 * The first matching object is returned, or NULL if none.
1873 */ 1856 */
1874
1875object * 1857object *
1876present (unsigned char type, maptile *m, int x, int y) 1858present (unsigned char type, maptile *m, int x, int y)
1877{ 1859{
1878 object *
1879 tmp;
1880
1881 if (out_of_map (m, x, y)) 1860 if (out_of_map (m, x, y))
1882 { 1861 {
1883 LOG (llevError, "Present called outside map.\n"); 1862 LOG (llevError, "Present called outside map.\n");
1884 return NULL; 1863 return NULL;
1885 } 1864 }
1865
1886 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1866 for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
1887 if (tmp->type == type) 1867 if (tmp->type == type)
1888 return tmp; 1868 return tmp;
1869
1889 return NULL; 1870 return NULL;
1890} 1871}
1891 1872
1892/* 1873/*
1893 * present_in_ob(type, object) searches for any objects with 1874 * present_in_ob(type, object) searches for any objects with
1894 * a matching type variable in the inventory of the given object. 1875 * a matching type variable in the inventory of the given object.
1895 * The first matching object is returned, or NULL if none. 1876 * The first matching object is returned, or NULL if none.
1896 */ 1877 */
1897
1898object * 1878object *
1899present_in_ob (unsigned char type, const object *op) 1879present_in_ob (unsigned char type, const object *op)
1900{ 1880{
1901 object *
1902 tmp;
1903
1904 for (tmp = op->inv; tmp != NULL; tmp = tmp->below) 1881 for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below)
1905 if (tmp->type == type) 1882 if (tmp->type == type)
1906 return tmp; 1883 return tmp;
1884
1907 return NULL; 1885 return NULL;
1908} 1886}
1909 1887
1910/* 1888/*
1911 * present_in_ob (type, str, object) searches for any objects with 1889 * present_in_ob (type, str, object) searches for any objects with
1922 * to be unique. 1900 * to be unique.
1923 */ 1901 */
1924object * 1902object *
1925present_in_ob_by_name (int type, const char *str, const object *op) 1903present_in_ob_by_name (int type, const char *str, const object *op)
1926{ 1904{
1927 object *tmp;
1928
1929 for (tmp = op->inv; tmp; tmp = tmp->below) 1905 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1930 if ((type == -1 || tmp->type == type) && (!strcmp (str, tmp->name))) 1906 if ((type == -1 || tmp->type == type) && (!strcmp (str, tmp->name)))
1931 return tmp; 1907 return tmp;
1932 1908
1933 return 0; 1909 return 0;
1934} 1910}
2143 2119
2144 if ((move_type & blocked) == move_type) 2120 if ((move_type & blocked) == move_type)
2145 max = maxfree[i]; 2121 max = maxfree[i];
2146 else if (mflags & P_IS_ALIVE) 2122 else if (mflags & P_IS_ALIVE)
2147 { 2123 {
2148 for (tmp = ms.bottom; tmp; tmp = tmp->above) 2124 for (tmp = ms.bot; tmp; tmp = tmp->above)
2149 if ((tmp->flag [FLAG_MONSTER] || tmp->type == PLAYER) 2125 if ((tmp->flag [FLAG_MONSTER] || tmp->type == PLAYER)
2150 && (tmp != exclude || (tmp->head && tmp->head != exclude))) 2126 && (tmp != exclude || (tmp->head && tmp->head != exclude)))
2151 break; 2127 break;
2152 2128
2153 if (tmp) 2129 if (tmp)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines