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.85 by root, Mon Dec 25 17:11:16 2006 UTC vs.
Revision 1.93 by root, Tue Dec 26 20:04:09 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);
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{
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
795 || !map
796 || map->in_memory != MAP_IN_MEMORY
797 || map->at (x, y).move_block == MOVE_ALL)
876 { 798 {
877 while (inv) 799 while (inv)
878 { 800 {
879 inv->destroy_inv (drop_to_ground); 801 inv->destroy_inv (drop_to_ground);
880 inv->destroy (); 802 inv->destroy ();
891 || op->type == RUNE 813 || op->type == RUNE
892 || op->type == TRAP 814 || op->type == TRAP
893 || op->flag [FLAG_IS_A_TEMPLATE]) 815 || op->flag [FLAG_IS_A_TEMPLATE])
894 op->destroy (); 816 op->destroy ();
895 else 817 else
896 { 818 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 } 819 }
903 } 820 }
821}
822
823object *object::create ()
824{
825 object *op = new object;
826 op->link ();
827 return op;
828}
829
830void
831object::do_destroy ()
832{
833 if (flag [FLAG_IS_LINKED])
834 remove_button_link (this);
835
836 if (flag [FLAG_FRIENDLY])
837 remove_friendly_object (this);
838
839 if (!flag [FLAG_REMOVED])
840 remove ();
841
842 if (flag [FLAG_FREED])
843 return;
844
845 set_speed (0);
846
847 flag [FLAG_FREED] = 1;
848
849 attachable::do_destroy ();
850
851 destroy_inv (true);
852 unlink ();
853
854 // hack to ensure that freed objects still have a valid map
855 {
856 static maptile *freed_map; // freed objects are moved here to avoid crashes
857
858 if (!freed_map)
859 {
860 freed_map = new maptile;
861
862 freed_map->name = "/internal/freed_objects_map";
863 freed_map->width = 3;
864 freed_map->height = 3;
865
866 freed_map->allocate ();
867 }
868
869 map = freed_map;
870 x = 1;
871 y = 1;
872 }
873
874 head = 0;
875
876 if (more)
877 {
878 more->destroy ();
879 more = 0;
880 }
881
882 // clear those pointers that likely might have circular references to us
883 owner = 0;
884 enemy = 0;
885 attacked_by = 0;
886
887 // only relevant for players(?), but make sure of it anyways
888 contr = 0;
904} 889}
905 890
906void 891void
907object::destroy (bool destroy_inventory) 892object::destroy (bool destroy_inventory)
908{ 893{
909 if (destroyed ()) 894 if (destroyed ())
910 return; 895 return;
911 896
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) 897 if (destroy_inventory)
920 destroy_inv (true); 898 destroy_inv (false);
921 899
922 attachable::destroy (); 900 attachable::destroy ();
923} 901}
924 902
925/* 903/*
1038 if (map->in_memory == MAP_SAVING) 1016 if (map->in_memory == MAP_SAVING)
1039 return; 1017 return;
1040 1018
1041 int check_walk_off = !flag [FLAG_NO_APPLY]; 1019 int check_walk_off = !flag [FLAG_NO_APPLY];
1042 1020
1043 for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) 1021 for (tmp = map->at (x, y).bot; tmp; tmp = tmp->above)
1044 { 1022 {
1045 /* No point updating the players look faces if he is the object 1023 /* No point updating the players look faces if he is the object
1046 * being removed. 1024 * being removed.
1047 */ 1025 */
1048 1026
1134 * job preparing multi-part monsters 1112 * job preparing multi-part monsters
1135 */ 1113 */
1136object * 1114object *
1137insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y) 1115insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y)
1138{ 1116{
1139 object *tmp;
1140
1141 if (op->head)
1142 op = op->head;
1143
1144 for (tmp = op; tmp; tmp = tmp->more) 1117 for (object *tmp = op->head_ (); tmp; tmp = tmp->more)
1145 { 1118 {
1146 tmp->x = x + tmp->arch->clone.x; 1119 tmp->x = x + tmp->arch->clone.x;
1147 tmp->y = y + tmp->arch->clone.y; 1120 tmp->y = y + tmp->arch->clone.y;
1148 } 1121 }
1149 1122
1463 tmp1->x = op->x; 1436 tmp1->x = op->x;
1464 tmp1->y = op->y; 1437 tmp1->y = op->y;
1465 insert_ob_in_map (tmp1, op->map, op, 0); 1438 insert_ob_in_map (tmp1, op->map, op, 0);
1466} 1439}
1467 1440
1441object *
1442object::insert_at (object *where, object *originator, int flags)
1443{
1444 where->map->insert (this, where->x, where->y, originator, flags);
1445}
1446
1468/* 1447/*
1469 * get_split_ob(ob,nr) splits up ob into two parts. The part which 1448 * 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 1449 * is returned contains nr objects, and the remaining parts contains
1471 * the rest (or is removed and freed if that number is 0). 1450 * the rest (or is removed and freed if that number is 0).
1472 * On failure, NULL is returned, and the reason put into the 1451 * On failure, NULL is returned, and the reason put into the
1473 * global static errmsg array. 1452 * global static errmsg array.
1474 */ 1453 */
1475
1476object * 1454object *
1477get_split_ob (object *orig_ob, uint32 nr) 1455get_split_ob (object *orig_ob, uint32 nr)
1478{ 1456{
1479 object *newob; 1457 object *newob;
1480 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1458 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1744 * 1722 *
1745 * MSW 2001-07-08: Check all objects on space, not just those below 1723 * 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 1724 * object being inserted. insert_ob_in_map may not put new objects
1747 * on top. 1725 * on top.
1748 */ 1726 */
1749
1750int 1727int
1751check_move_on (object *op, object *originator) 1728check_move_on (object *op, object *originator)
1752{ 1729{
1753 object *tmp; 1730 object *tmp;
1754 maptile *m = op->map; 1731 maptile *m = op->map;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines