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.82 by root, Mon Dec 25 11:25:49 2006 UTC vs.
Revision 1.91 by elmex, Tue Dec 26 10:51:52 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.
691 */ 688 */
692 if (!op->map || op->map->in_memory == MAP_SAVING) 689 if (!op->map || op->map->in_memory == MAP_SAVING)
693 return; 690 return;
694 691
695 /* make sure the object is within map boundaries */ 692 /* make sure the object is within map boundaries */
696 if (op->x < 0 || op->x >= MAP_WIDTH (op->map) || op->y < 0 || op->y >= MAP_HEIGHT (op->map)) 693 if (op->x < 0 || op->x >= op->map->width || op->y < 0 || op->y >= op->map->height)
697 { 694 {
698 LOG (llevError, "update_object() called for object out of map!\n"); 695 LOG (llevError, "update_object() called for object out of map!\n");
699#ifdef MANY_CORES 696#ifdef MANY_CORES
700 abort (); 697 abort ();
701#endif 698#endif
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{
868 if (!inv)
869 return;
870
871 /* Only if the space blocks everything do we not process - 790 /* Only if the space blocks everything do we not process -
872 * if some form of movement is allowed, let objects 791 * if some form of movement is allowed, let objects
873 * drop on that space. 792 * drop on that space.
874 */ 793 */
875 if (!drop_to_ground || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) 794 if (!drop_to_ground || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)
876 { 795 {
877 while (inv) 796 while (inv)
878 {
879 inv->destroy_inv (drop_to_ground);
880 inv->destroy (); 797 inv->destroy ();
881 }
882 } 798 }
883 else 799 else
884 { /* Put objects in inventory onto this space */ 800 { /* Put objects in inventory onto this space */
885 while (inv) 801 while (inv)
886 { 802 {
901 } 817 }
902 } 818 }
903 } 819 }
904} 820}
905 821
822object *object::create ()
823{
824 object *op = new object;
825 op->link ();
826 return op;
827}
828
829void
830object::do_destroy ()
831{
832 if (flag [FLAG_IS_LINKED])
833 remove_button_link (this);
834
835 if (flag [FLAG_FRIENDLY])
836 remove_friendly_object (this);
837
838 if (!flag [FLAG_REMOVED])
839 remove ();
840
841 if (flag [FLAG_FREED])
842 return;
843
844 flag [FLAG_FREED] = 1;
845
846 attachable::do_destroy ();
847
848 destroy_inv (true);
849 set_speed (0);
850 unlink ();
851
852 // hack to ensure that freed objects still have a valid map
853 {
854 static maptile *freed_map; // freed objects are moved here to avoid crashes
855
856 if (!freed_map)
857 {
858 freed_map = new maptile;
859
860 freed_map->name = "/internal/freed_objects_map";
861 freed_map->width = 3;
862 freed_map->height = 3;
863
864 freed_map->allocate ();
865 }
866
867 map = freed_map;
868 x = 1;
869 y = 1;
870 }
871
872 head = 0;
873
874 if (more)
875 {
876 more->destroy ();
877 more = 0;
878 }
879
880 // clear those pointers that likely might have circular references to us
881 owner = 0;
882 enemy = 0;
883 attacked_by = 0;
884
885 // only relevant for players(?), but make sure of it anyways
886 contr = 0;
887}
888
906void 889void
907object::destroy (bool destroy_inventory) 890object::destroy (bool destroy_inventory)
908{ 891{
909 if (destroyed ()) 892 if (destroyed ())
910 return; 893 return;
911 894
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) 895 if (destroy_inventory)
920 destroy_inv (true); 896 destroy_inv (false);
921 897
922 attachable::destroy (); 898 attachable::destroy ();
923} 899}
924 900
925/* 901/*
1027 dump = dump_object (GET_MAP_OB (map, x, y)); 1003 dump = dump_object (GET_MAP_OB (map, x, y));
1028 LOG (llevError, "%s\n", dump); 1004 LOG (llevError, "%s\n", dump);
1029 free (dump); 1005 free (dump);
1030 } 1006 }
1031 1007
1032 map->at (x, y).bottom = above; /* goes on above it. */ 1008 map->at (x, y).bot = above; /* goes on above it. */
1033 } 1009 }
1034 1010
1035 above = 0; 1011 above = 0;
1036 below = 0; 1012 below = 0;
1037 1013
1038 if (map->in_memory == MAP_SAVING) 1014 if (map->in_memory == MAP_SAVING)
1039 return; 1015 return;
1040 1016
1041 int check_walk_off = !flag [FLAG_NO_APPLY]; 1017 int check_walk_off = !flag [FLAG_NO_APPLY];
1042 1018
1043 for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) 1019 for (tmp = map->at (x, y).bot; tmp; tmp = tmp->above)
1044 { 1020 {
1045 /* No point updating the players look faces if he is the object 1021 /* No point updating the players look faces if he is the object
1046 * being removed. 1022 * being removed.
1047 */ 1023 */
1048 1024
1281 op->below = originator->below; 1257 op->below = originator->below;
1282 1258
1283 if (op->below) 1259 if (op->below)
1284 op->below->above = op; 1260 op->below->above = op;
1285 else 1261 else
1286 op->ms ().bottom = op; 1262 op->ms ().bot = op;
1287 1263
1288 /* since *below* originator, no need to update top */ 1264 /* since *below* originator, no need to update top */
1289 originator->below = op; 1265 originator->below = op;
1290 } 1266 }
1291 else 1267 else
1370 1346
1371 if (op->above) 1347 if (op->above)
1372 op->above->below = op; 1348 op->above->below = op;
1373 1349
1374 op->below = NULL; 1350 op->below = NULL;
1375 op->ms ().bottom = op; 1351 op->ms ().bot = op;
1376 } 1352 }
1377 else 1353 else
1378 { /* get inserted into the stack above top */ 1354 { /* get inserted into the stack above top */
1379 op->above = top->above; 1355 op->above = top->above;
1380 1356
1407 * be far away from this change and not affected in any way - 1383 * be far away from this change and not affected in any way -
1408 * this should get redone to only look for players within range, 1384 * this should get redone to only look for players within range,
1409 * or just updating the P_NEED_UPDATE for spaces within this area 1385 * or just updating the P_NEED_UPDATE for spaces within this area
1410 * of effect may be sufficient. 1386 * of effect may be sufficient.
1411 */ 1387 */
1412 if (MAP_DARKNESS (op->map) && (op->glow_radius != 0)) 1388 if (op->map->darkness && (op->glow_radius != 0))
1413 update_all_los (op->map, op->x, op->y); 1389 update_all_los (op->map, op->x, op->y);
1414 1390
1415 /* updates flags (blocked, alive, no magic, etc) for this map space */ 1391 /* updates flags (blocked, alive, no magic, etc) for this map space */
1416 update_object (op, UP_OBJ_INSERT); 1392 update_object (op, UP_OBJ_INSERT);
1417 1393
1703 if ((op->glow_radius != 0) && map) 1679 if ((op->glow_radius != 0) && map)
1704 { 1680 {
1705#ifdef DEBUG_LIGHTS 1681#ifdef DEBUG_LIGHTS
1706 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name); 1682 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name);
1707#endif /* DEBUG_LIGHTS */ 1683#endif /* DEBUG_LIGHTS */
1708 if (MAP_DARKNESS (map)) 1684 if (map->darkness)
1709 update_all_los (map, x, y); 1685 update_all_los (map, x, y);
1710 } 1686 }
1711 1687
1712 /* Client has no idea of ordering so lets not bother ordering it here. 1688 /* Client has no idea of ordering so lets not bother ordering it here.
1713 * It sure simplifies this function... 1689 * It sure simplifies this function...
1781 1757
1782 /* The objects have to be checked from top to bottom. 1758 /* The objects have to be checked from top to bottom.
1783 * Hence, we first go to the top: 1759 * Hence, we first go to the top:
1784 */ 1760 */
1785 1761
1786 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL && tmp->above != NULL; tmp = tmp->above) 1762 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp && tmp->above; tmp = tmp->above)
1787 { 1763 {
1788 /* Trim the search when we find the first other spell effect 1764 /* Trim the search when we find the first other spell effect
1789 * this helps performance so that if a space has 50 spell objects, 1765 * this helps performance so that if a space has 50 spell objects,
1790 * we don't need to check all of them. 1766 * we don't need to check all of them.
1791 */ 1767 */
1846/* 1822/*
1847 * present_arch(arch, map, x, y) searches for any objects with 1823 * present_arch(arch, map, x, y) searches for any objects with
1848 * a matching archetype at the given map and coordinates. 1824 * a matching archetype at the given map and coordinates.
1849 * The first matching object is returned, or NULL if none. 1825 * The first matching object is returned, or NULL if none.
1850 */ 1826 */
1851
1852object * 1827object *
1853present_arch (const archetype *at, maptile *m, int x, int y) 1828present_arch (const archetype *at, maptile *m, int x, int y)
1854{ 1829{
1855 object *
1856 tmp;
1857
1858 if (m == NULL || out_of_map (m, x, y)) 1830 if (m == NULL || out_of_map (m, x, y))
1859 { 1831 {
1860 LOG (llevError, "Present_arch called outside map.\n"); 1832 LOG (llevError, "Present_arch called outside map.\n");
1861 return NULL; 1833 return NULL;
1862 } 1834 }
1835
1863 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1836 for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
1864 if (tmp->arch == at) 1837 if (tmp->arch == at)
1865 return tmp; 1838 return tmp;
1839
1866 return NULL; 1840 return NULL;
1867} 1841}
1868 1842
1869/* 1843/*
1870 * present(type, map, x, y) searches for any objects with 1844 * present(type, map, x, y) searches for any objects with
1871 * a matching type variable at the given map and coordinates. 1845 * a matching type variable at the given map and coordinates.
1872 * The first matching object is returned, or NULL if none. 1846 * The first matching object is returned, or NULL if none.
1873 */ 1847 */
1874
1875object * 1848object *
1876present (unsigned char type, maptile *m, int x, int y) 1849present (unsigned char type, maptile *m, int x, int y)
1877{ 1850{
1878 object *
1879 tmp;
1880
1881 if (out_of_map (m, x, y)) 1851 if (out_of_map (m, x, y))
1882 { 1852 {
1883 LOG (llevError, "Present called outside map.\n"); 1853 LOG (llevError, "Present called outside map.\n");
1884 return NULL; 1854 return NULL;
1885 } 1855 }
1856
1886 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) 1857 for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
1887 if (tmp->type == type) 1858 if (tmp->type == type)
1888 return tmp; 1859 return tmp;
1860
1889 return NULL; 1861 return NULL;
1890} 1862}
1891 1863
1892/* 1864/*
1893 * present_in_ob(type, object) searches for any objects with 1865 * present_in_ob(type, object) searches for any objects with
1894 * a matching type variable in the inventory of the given object. 1866 * a matching type variable in the inventory of the given object.
1895 * The first matching object is returned, or NULL if none. 1867 * The first matching object is returned, or NULL if none.
1896 */ 1868 */
1897
1898object * 1869object *
1899present_in_ob (unsigned char type, const object *op) 1870present_in_ob (unsigned char type, const object *op)
1900{ 1871{
1901 object *
1902 tmp;
1903
1904 for (tmp = op->inv; tmp != NULL; tmp = tmp->below) 1872 for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below)
1905 if (tmp->type == type) 1873 if (tmp->type == type)
1906 return tmp; 1874 return tmp;
1875
1907 return NULL; 1876 return NULL;
1908} 1877}
1909 1878
1910/* 1879/*
1911 * present_in_ob (type, str, object) searches for any objects with 1880 * present_in_ob (type, str, object) searches for any objects with
1922 * to be unique. 1891 * to be unique.
1923 */ 1892 */
1924object * 1893object *
1925present_in_ob_by_name (int type, const char *str, const object *op) 1894present_in_ob_by_name (int type, const char *str, const object *op)
1926{ 1895{
1927 object *tmp;
1928
1929 for (tmp = op->inv; tmp; tmp = tmp->below) 1896 for (object *tmp = op->inv; tmp; tmp = tmp->below)
1930 if ((type == -1 || tmp->type == type) && (!strcmp (str, tmp->name))) 1897 if ((type == -1 || tmp->type == type) && (!strcmp (str, tmp->name)))
1931 return tmp; 1898 return tmp;
1932 1899
1933 return 0; 1900 return 0;
1934} 1901}
2143 2110
2144 if ((move_type & blocked) == move_type) 2111 if ((move_type & blocked) == move_type)
2145 max = maxfree[i]; 2112 max = maxfree[i];
2146 else if (mflags & P_IS_ALIVE) 2113 else if (mflags & P_IS_ALIVE)
2147 { 2114 {
2148 for (tmp = ms.bottom; tmp; tmp = tmp->above) 2115 for (tmp = ms.bot; tmp; tmp = tmp->above)
2149 if ((tmp->flag [FLAG_MONSTER] || tmp->type == PLAYER) 2116 if ((tmp->flag [FLAG_MONSTER] || tmp->type == PLAYER)
2150 && (tmp != exclude || (tmp->head && tmp->head != exclude))) 2117 && (tmp != exclude || (tmp->head && tmp->head != exclude)))
2151 break; 2118 break;
2152 2119
2153 if (tmp) 2120 if (tmp)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines