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.62 by root, Mon Dec 11 23:35:37 2006 UTC vs.
Revision 1.66 by root, Wed Dec 13 03:28:42 2006 UTC

183 * 183 *
184 * Note that this function appears a lot longer than the macro it 184 * Note that this function appears a lot longer than the macro it
185 * replaces - this is mostly for clarity - a decent compiler should hopefully 185 * replaces - this is mostly for clarity - a decent compiler should hopefully
186 * reduce this to the same efficiency. 186 * reduce this to the same efficiency.
187 * 187 *
188 * Check nrof variable *before* calling CAN_MERGE() 188 * Check nrof variable *before* calling can_merge()
189 * 189 *
190 * Improvements made with merge: Better checking on potion, and also 190 * Improvements made with merge: Better checking on potion, and also
191 * check weight 191 * check weight
192 */ 192 */
193 193
194bool object::can_merge (object *ob1, object *ob2) 194bool object::can_merge_slow (object *ob1, object *ob2)
195{ 195{
196 /* A couple quicksanity checks */ 196 /* A couple quicksanity checks */
197 if ((ob1 == ob2) || (ob1->type != ob2->type)) 197 if (ob1 == ob2
198 || ob1->type != ob2->type
199 || ob1->speed != ob2->speed
200 || ob1->value != ob2->value
201 || ob1->name != ob2->name)
198 return 0; 202 return 0;
199 203
200 if (ob1->speed != ob2->speed) 204 //TODO: this ain't working well, use nicer and correct overflow check
201 return 0;
202
203 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 205 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
204 * value could not be stored in a sint32 (which unfortunately sometimes is 206 * value could not be stored in a sint32 (which unfortunately sometimes is
205 * used to store nrof). 207 * used to store nrof).
206 */ 208 */
207 if (ob1->nrof + ob2->nrof >= 1UL << 31) 209 if (ob1->nrof + ob2->nrof >= 1UL << 31)
260 /* if one object has inventory but the other doesn't, not equiv */ 262 /* if one object has inventory but the other doesn't, not equiv */
261 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) 263 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
262 return 0; 264 return 0;
263 265
264 /* Now check to see if the two inventory objects could merge */ 266 /* Now check to see if the two inventory objects could merge */
265 if (!CAN_MERGE (ob1->inv, ob2->inv)) 267 if (!object::can_merge (ob1->inv, ob2->inv))
266 return 0; 268 return 0;
267 269
268 /* inventory ok - still need to check rest of this object to see 270 /* inventory ok - still need to check rest of this object to see
269 * if it is valid. 271 * if it is valid.
270 */ 272 */
526 528
527 if (settings.casting_time) 529 if (settings.casting_time)
528 casting_time = -1; 530 casting_time = -1;
529} 531}
530 532
531void object::clone (object *destination)
532{
533 *(object_copy *)destination = *this;
534 *(object_pod *)destination = *this;
535
536 if (self || cb)
537 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
538}
539
540/* 533/*
541 * copy object first frees everything allocated by the second object, 534 * copy_to first frees everything allocated by the dst object,
542 * and then copies the contends of the first object into the second 535 * and then copies the contents of itself into the second
543 * object, allocating what needs to be allocated. Basically, any 536 * object, allocating what needs to be allocated. Basically, any
544 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 537 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
545 * if the first object is freed, the pointers in the new object 538 * if the first object is freed, the pointers in the new object
546 * will point at garbage. 539 * will point at garbage.
547 */ 540 */
548void 541void
549copy_object (object *op2, object *op) 542object::copy_to (object *dst)
550{ 543{
551 bool is_freed = QUERY_FLAG (op, FLAG_FREED); 544 bool is_freed = QUERY_FLAG (dst, FLAG_FREED);
552 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); 545 bool is_removed = QUERY_FLAG (dst, FLAG_REMOVED);
553 546
554 op2->clone (op); 547 *(object_copy *)dst = *this;
548 *(object_pod *)dst = *this;
549
550 if (self || cb)
551 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (dst));
555 552
556 if (is_freed) 553 if (is_freed)
557 SET_FLAG (op, FLAG_FREED); 554 SET_FLAG (dst, FLAG_FREED);
558 555
559 if (is_removed) 556 if (is_removed)
560 SET_FLAG (op, FLAG_REMOVED); 557 SET_FLAG (dst, FLAG_REMOVED);
561 558
562 if (op2->speed < 0) 559 if (speed < 0)
563 op->speed_left = op2->speed_left - RANDOM () % 200 / 100.0; 560 dst->speed_left = speed_left - RANDOM () % 200 / 100.0;
564 561
565 /* Copy over key_values, if any. */ 562 /* Copy over key_values, if any. */
566 if (op2->key_values) 563 if (key_values)
567 { 564 {
568 key_value *tail = 0; 565 key_value *tail = 0;
569 key_value *i; 566 key_value *i;
570 567
571 op->key_values = 0; 568 dst->key_values = 0;
572 569
573 for (i = op2->key_values; i; i = i->next) 570 for (i = key_values; i; i = i->next)
574 { 571 {
575 key_value *new_link = new key_value; 572 key_value *new_link = new key_value;
576 573
577 new_link->next = 0; 574 new_link->next = 0;
578 new_link->key = i->key; 575 new_link->key = i->key;
579 new_link->value = i->value; 576 new_link->value = i->value;
580 577
581 /* Try and be clever here, too. */ 578 /* Try and be clever here, too. */
582 if (!op->key_values) 579 if (!dst->key_values)
583 { 580 {
584 op->key_values = new_link; 581 dst->key_values = new_link;
585 tail = new_link; 582 tail = new_link;
586 } 583 }
587 else 584 else
588 { 585 {
589 tail->next = new_link; 586 tail->next = new_link;
590 tail = new_link; 587 tail = new_link;
591 } 588 }
592 } 589 }
593 } 590 }
594 591
595 update_ob_speed (op); 592 update_ob_speed (dst);
593}
594
595object *
596object::clone ()
597{
598 object *neu = create ();
599 copy_to (neu);
600 return neu;
596} 601}
597 602
598/* 603/*
599 * If an object with the IS_TURNABLE() flag needs to be turned due 604 * If an object with the IS_TURNABLE() flag needs to be turned due
600 * to the closest player being on the other side, this function can 605 * to the closest player being on the other side, this function can
909 914
910 if (QUERY_FLAG (this, FLAG_FRIENDLY)) 915 if (QUERY_FLAG (this, FLAG_FRIENDLY))
911 remove_friendly_object (this); 916 remove_friendly_object (this);
912 917
913 if (!QUERY_FLAG (this, FLAG_REMOVED)) 918 if (!QUERY_FLAG (this, FLAG_REMOVED))
914 remove_ob (this); 919 remove ();
915 920
916 SET_FLAG (this, FLAG_FREED); 921 SET_FLAG (this, FLAG_FREED);
917 922
918 if (more) 923 if (more)
919 { 924 {
944 949
945 while (op) 950 while (op)
946 { 951 {
947 object *tmp = op->below; 952 object *tmp = op->below;
948 953
949 remove_ob (op); 954 op->remove ();
950 955
951 if (QUERY_FLAG (op, FLAG_STARTEQUIP) 956 if (QUERY_FLAG (op, FLAG_STARTEQUIP)
952 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE)) 957 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
953 free_object (op); 958 op->destroy ();
954 else 959 else
955 { 960 {
956 op->x = x; 961 op->x = x;
957 op->y = y; 962 op->y = y;
958 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */ 963 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */
1015 op->carrying -= weight; 1020 op->carrying -= weight;
1016 op = op->env; 1021 op = op->env;
1017 } 1022 }
1018} 1023}
1019 1024
1020/* remove_ob(op): 1025/* op->remove ():
1021 * This function removes the object op from the linked list of objects 1026 * This function removes the object op from the linked list of objects
1022 * which it is currently tied to. When this function is done, the 1027 * which it is currently tied to. When this function is done, the
1023 * object will have no environment. If the object previously had an 1028 * object will have no environment. If the object previously had an
1024 * environment, the x and y coordinates will be updated to 1029 * environment, the x and y coordinates will be updated to
1025 * the previous environment. 1030 * the previous environment.
1197 1202
1198 for (; top != NULL; top = top->below) 1203 for (; top != NULL; top = top->below)
1199 { 1204 {
1200 if (top == op) 1205 if (top == op)
1201 continue; 1206 continue;
1202 if (CAN_MERGE (op, top)) 1207
1208 if (object::can_merge (op, top))
1203 { 1209 {
1204 top->nrof += op->nrof; 1210 top->nrof += op->nrof;
1205 1211
1206/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ 1212/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/
1207 op->weight = 0; /* Don't want any adjustements now */ 1213 op->weight = 0; /* Don't want any adjustements now */
1208 remove_ob (op); 1214 op->destroy ();
1209 free_object (op);
1210 return top; 1215 return top;
1211 } 1216 }
1212 } 1217 }
1213 1218
1214 return 0; 1219 return 0;
1341 1346
1342 /* this has to be done after we translate the coordinates. 1347 /* this has to be done after we translate the coordinates.
1343 */ 1348 */
1344 if (op->nrof && !(flag & INS_NO_MERGE)) 1349 if (op->nrof && !(flag & INS_NO_MERGE))
1345 for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above) 1350 for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above)
1346 if (CAN_MERGE (op, tmp)) 1351 if (object::can_merge (op, tmp))
1347 { 1352 {
1348 op->nrof += tmp->nrof; 1353 op->nrof += tmp->nrof;
1349 remove_ob (tmp); 1354 tmp->destroy ();
1350 free_object (tmp);
1351 } 1355 }
1352 1356
1353 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ 1357 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */
1354 CLEAR_FLAG (op, FLAG_INV_LOCKED); 1358 CLEAR_FLAG (op, FLAG_INV_LOCKED);
1355 1359
1541 tmp1; 1545 tmp1;
1542 1546
1543 /* first search for itself and remove any old instances */ 1547 /* first search for itself and remove any old instances */
1544 1548
1545 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) 1549 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above)
1546 {
1547 if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */ 1550 if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */
1548 { 1551 tmp->destroy ();
1549 remove_ob (tmp);
1550 free_object (tmp);
1551 }
1552 }
1553 1552
1554 tmp1 = arch_to_object (archetype::find (arch_string)); 1553 tmp1 = arch_to_object (archetype::find (arch_string));
1555 1554
1556 tmp1->x = op->x; 1555 tmp1->x = op->x;
1557 tmp1->y = op->y; 1556 tmp1->y = op->y;
1567 */ 1566 */
1568 1567
1569object * 1568object *
1570get_split_ob (object *orig_ob, uint32 nr) 1569get_split_ob (object *orig_ob, uint32 nr)
1571{ 1570{
1572 object * 1571 object *newob;
1573 newob;
1574 int
1575 is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1572 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1576 1573
1577 if (orig_ob->nrof < nr) 1574 if (orig_ob->nrof < nr)
1578 { 1575 {
1579 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name); 1576 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name);
1580 return NULL; 1577 return NULL;
1581 } 1578 }
1582 1579
1583 newob = object_create_clone (orig_ob); 1580 newob = object_create_clone (orig_ob);
1584 1581
1585 if ((orig_ob->nrof -= nr) < 1) 1582 if ((orig_ob->nrof -= nr) < 1)
1586 { 1583 orig_ob->destroy (1);
1587 if (!is_removed)
1588 remove_ob (orig_ob);
1589 free_object2 (orig_ob, 1);
1590 }
1591 else if (!is_removed) 1584 else if (!is_removed)
1592 { 1585 {
1593 if (orig_ob->env != NULL) 1586 if (orig_ob->env != NULL)
1594 sub_weight (orig_ob->env, orig_ob->weight * nr); 1587 sub_weight (orig_ob->env, orig_ob->weight * nr);
1595 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY) 1588 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY)
1659 esrv_send_item (tmp, op); 1652 esrv_send_item (tmp, op);
1660 } 1653 }
1661 } 1654 }
1662 else 1655 else
1663 { 1656 {
1664 remove_ob (op); 1657 op->remove ();
1665 op->nrof = 0; 1658 op->nrof = 0;
1666 if (tmp) 1659 if (tmp)
1667 { 1660 {
1668 esrv_del_item (tmp->contr, op->count); 1661 esrv_del_item (tmp->contr, op->count);
1669 } 1662 }
1675 1668
1676 if (i < op->nrof) 1669 if (i < op->nrof)
1677 op->nrof -= i; 1670 op->nrof -= i;
1678 else 1671 else
1679 { 1672 {
1680 remove_ob (op); 1673 op->remove ();
1681 op->nrof = 0; 1674 op->nrof = 0;
1682 } 1675 }
1683 1676
1684 /* Since we just removed op, op->above is null */ 1677 /* Since we just removed op, op->above is null */
1685 for (tmp = above; tmp != NULL; tmp = tmp->above) 1678 for (tmp = above; tmp != NULL; tmp = tmp->above)
1694 1687
1695 if (op->nrof) 1688 if (op->nrof)
1696 return op; 1689 return op;
1697 else 1690 else
1698 { 1691 {
1699 free_object (op); 1692 op->destroy ();
1700 return NULL; 1693 return NULL;
1701 } 1694 }
1702} 1695}
1703 1696
1704/* 1697/*
1765 CLEAR_FLAG (op, FLAG_OBJ_ORIGINAL); 1758 CLEAR_FLAG (op, FLAG_OBJ_ORIGINAL);
1766 CLEAR_FLAG (op, FLAG_REMOVED); 1759 CLEAR_FLAG (op, FLAG_REMOVED);
1767 if (op->nrof) 1760 if (op->nrof)
1768 { 1761 {
1769 for (tmp = inv; tmp != NULL; tmp = tmp->below) 1762 for (tmp = inv; tmp != NULL; tmp = tmp->below)
1770 if (CAN_MERGE (tmp, op)) 1763 if (object::can_merge (tmp, op))
1771 { 1764 {
1772 /* return the original object and remove inserted object 1765 /* return the original object and remove inserted object
1773 (client needs the original object) */ 1766 (client needs the original object) */
1774 tmp->nrof += op->nrof; 1767 tmp->nrof += op->nrof;
1775 /* Weight handling gets pretty funky. Since we are adding to 1768 /* Weight handling gets pretty funky. Since we are adding to
2536 src = src->head; 2529 src = src->head;
2537 2530
2538 prev = 0; 2531 prev = 0;
2539 for (part = src; part; part = part->more) 2532 for (part = src; part; part = part->more)
2540 { 2533 {
2541 tmp = get_object (); 2534 tmp = part->clone ();
2542 copy_object (part, tmp);
2543 tmp->x -= src->x; 2535 tmp->x -= src->x;
2544 tmp->y -= src->y; 2536 tmp->y -= src->y;
2545 2537
2546 if (!part->head) 2538 if (!part->head)
2547 { 2539 {
2591 } 2583 }
2592 2584
2593 fprintf (tempfile, obstr); 2585 fprintf (tempfile, obstr);
2594 fclose (tempfile); 2586 fclose (tempfile);
2595 2587
2596 op = get_object (); 2588 op = object::create ();
2597 2589
2598 object_thawer thawer (filename); 2590 object_thawer thawer (filename);
2599 2591
2600 if (thawer) 2592 if (thawer)
2601 load_object (thawer, op, 0); 2593 load_object (thawer, op, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines