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.323 by root, Sun Apr 11 23:56:09 2010 UTC vs.
Revision 1.337 by root, Thu May 6 21:45:49 2010 UTC

36static const uint64 UUID_GAP = 1<<19; 36static const uint64 UUID_GAP = 1<<19;
37uint32_t mapspace::smellcount = 10000; 37uint32_t mapspace::smellcount = 10000;
38 38
39objectvec objects; 39objectvec objects;
40activevec actives; 40activevec actives;
41
42freelist_item *object::freelist;
43uint32_t object::object_count;
44uint32_t object::free_count;
45uint32_t object::create_count;
46uint32_t object::destroy_count;
41 47
42//+GPL 48//+GPL
43 49
44short freearr_x[SIZEOFFREE] = { 50short freearr_x[SIZEOFFREE] = {
45 0, 51 0,
420 426
421 return 0; 427 return 0;
422} 428}
423 429
424// adjust weight per container type ("of holding") 430// adjust weight per container type ("of holding")
425static sint32 431static uint32
426weight_adjust_for (object *op, sint32 weight) 432weight_adjust_for (object *op, uint32 weight)
427{ 433{
428 return op->type == CONTAINER 434 return op->type == CONTAINER
429 ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) 435 ? weight - weight * op->stats.Str / 100
430 : weight; 436 : weight;
431} 437}
432 438
433/* 439/*
434 * adjust_weight(object, weight) adds the specified weight to an object, 440 * subtracts, then adds, the specified weight to an object,
435 * and also updates how much the environment(s) is/are carrying. 441 * and also updates how much the environment(s) is/are carrying.
436 */ 442 */
437static void 443static void
438adjust_weight (object *op, sint32 weight) 444adjust_weight (object *op, sint32 sub, sint32 add)
439{ 445{
440 while (op) 446 while (op)
441 { 447 {
442 // adjust by actual difference to account for rounding errors 448 sint32 ocarrying = op->carrying;
443 // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct
444 weight = weight_adjust_for (op, op->carrying)
445 - weight_adjust_for (op, op->carrying - weight);
446 449
447 if (!weight) 450 op->carrying -= weight_adjust_for (op, sub);
448 return; 451 op->carrying += weight_adjust_for (op, add);
449
450 op->carrying += weight;
451 452
452 if (object *pl = op->visible_to ()) 453 if (object *pl = op->visible_to ())
453 if (pl != op) // player is handled lazily 454 if (pl != op) // player is handled lazily
454 esrv_update_item (UPD_WEIGHT, pl, op); 455 esrv_update_item (UPD_WEIGHT, pl, op);
455 456
457 sub = ocarrying;
458 add = op->carrying;
459
456 op = op->env; 460 op = op->env;
457 } 461 }
458} 462}
459 463
460/* 464/*
467{ 471{
468 sint32 sum = 0; 472 sint32 sum = 0;
469 473
470 for (object *op = inv; op; op = op->below) 474 for (object *op = inv; op; op = op->below)
471 { 475 {
472 if (op->inv)
473 op->update_weight (); 476 op->update_weight ();
474 477
475 sum += op->total_weight (); 478 sum += weight_adjust_for (this, op->total_weight ());
476 } 479 }
477
478 sum = weight_adjust_for (this, sum);
479 480
480 if (sum != carrying) 481 if (sum != carrying)
481 { 482 {
483 if (carrying != sum && carrying)//D
484 LOG (llevDebug, "updating carrying got %ld, expected %ld (%s)\n",
485 (long long)sum, (long long)carrying, debug_desc ());
486
482 carrying = sum; 487 carrying = sum;
483 488
484 if (object *pl = visible_to ()) 489 if (object *pl = visible_to ())
485 if (pl != this) // player is handled lazily 490 if (pl != this) // player is handled lazily
486 esrv_update_item (UPD_WEIGHT, pl, this); 491 esrv_update_item (UPD_WEIGHT, pl, this);
809 unlink (); 814 unlink ();
810 815
811 free_key_values (this); 816 free_key_values (this);
812} 817}
813 818
814static int object_count;
815
816void object::link () 819void object::link ()
817{ 820{
818 assert (!index);//D 821 assert (!index);//D
819 uuid = UUID::gen (); 822 uuid = UUID::gen ();
820 count = ++object_count;
821 823
822 refcnt_inc (); 824 refcnt_inc ();
823 objects.insert (this); 825 objects.insert (this);
826
827 ++create_count;
828
824} 829}
825 830
826void object::unlink () 831void object::unlink ()
827{ 832{
828 if (!index) 833 if (!index)
829 return; 834 return;
835
836 ++destroy_count;
830 837
831 objects.erase (this); 838 objects.erase (this);
832 refcnt_dec (); 839 refcnt_dec ();
833} 840}
834 841
960 // then destroy 967 // then destroy
961 op->destroy (); 968 op->destroy ();
962 } 969 }
963} 970}
964 971
972void
973object::freelist_free (int count)
974{
975 while (count-- && freelist)
976 {
977 freelist_item *next = freelist->next;
978 // count is being "destroyed"
979
980 sfree ((char *)freelist, sizeof (object));
981
982 freelist = next;
983 --free_count;
984 }
985}
986
987object *
965object *object::create () 988object::create ()
966{ 989{
967 object *op = new object; 990 object *op;
991
992 if (freelist)
993 {
994 freelist_item li = *freelist;
995 memset (freelist, 0, sizeof (object));
996
997 op = new (freelist) object;
998 op->count = li.count;
999
1000 freelist = li.next;
1001 --free_count;
1002 }
1003 else
1004 {
1005 void *ni = salloc0<char> (sizeof (object));
1006
1007 op = new(ni) object;
1008
1009 op->count = ++object_count;
1010 }
1011
968 op->link (); 1012 op->link ();
1013
969 return op; 1014 return op;
970} 1015}
971 1016
1017void
1018object::do_delete ()
1019{
1020 uint32_t count = this->count;
1021
1022 this->~object ();
1023
1024 freelist_item *li = (freelist_item *)this;
1025 li->next = freelist;
1026 li->count = count;
1027
1028 freelist = li;
1029 ++free_count;
1030}
1031
972static struct freed_map : maptile 1032static struct freed_map : maptile
973{ 1033{
974 freed_map () 1034 freed_map ()
1035 : maptile (3, 3)
975 { 1036 {
976 path = "<freed objects map>"; 1037 path = "<freed objects map>";
977 name = "/internal/freed_objects_map"; 1038 name = "/internal/freed_objects_map";
978 width = 3;
979 height = 3;
980 no_drop = 1; 1039 no_drop = 1;
981 no_reset = 1; 1040 no_reset = 1;
982 1041
983 alloc ();
984 in_memory = MAP_ACTIVE; 1042 in_memory = MAP_ACTIVE;
985 } 1043 }
986 1044
987 ~freed_map () 1045 ~freed_map ()
988 { 1046 {
1081 flag [FLAG_REMOVED] = false; // hack around the issue of visible_to checking flag_removed 1139 flag [FLAG_REMOVED] = false; // hack around the issue of visible_to checking flag_removed
1082 if (object *pl = visible_to ()) 1140 if (object *pl = visible_to ())
1083 esrv_del_item (pl->contr, count); 1141 esrv_del_item (pl->contr, count);
1084 flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed 1142 flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed
1085 1143
1086 adjust_weight (env, -total_weight ()); 1144 adjust_weight (env, total_weight (), 0);
1087 1145
1088 object *pl = in_player (); 1146 object *pl = in_player ();
1089 1147
1090 /* we set up values so that it could be inserted into 1148 /* we set up values so that it could be inserted into
1091 * the map, but we don't actually do that - it is up 1149 * the map, but we don't actually do that - it is up
1509 */ 1567 */
1510 1568
1511 /* if this is not the head or flag has been passed, don't check walk on status */ 1569 /* if this is not the head or flag has been passed, don't check walk on status */
1512 if (!(flag & INS_NO_WALK_ON) && op->is_head ()) 1570 if (!(flag & INS_NO_WALK_ON) && op->is_head ())
1513 { 1571 {
1514 if (check_move_on (op, originator)) 1572 if (check_move_on (op, originator, flag))
1515 return 0; 1573 return 0;
1516 1574
1517 /* If we are a multi part object, lets work our way through the check 1575 /* If we are a multi part object, let's work our way through the check
1518 * walk on's. 1576 * walk on's.
1519 */ 1577 */
1520 for (object *tmp = op->more; tmp; tmp = tmp->more) 1578 for (object *tmp = op->more; tmp; tmp = tmp->more)
1521 if (check_move_on (tmp, originator)) 1579 if (check_move_on (tmp, originator, flag))
1522 return 0; 1580 return 0;
1523 } 1581 }
1524 1582
1525 return op; 1583 return op;
1526} 1584}
1592 1650
1593 nr = min (nr, nrof); 1651 nr = min (nr, nrof);
1594 1652
1595 if (nrof > nr) 1653 if (nrof > nr)
1596 { 1654 {
1655 sint64 oweight = total_weight ();
1656
1597 nrof -= nr; 1657 nrof -= nr;
1598 adjust_weight (env, -weight * max (1, nr)); // carrying == 0
1599 1658
1600 if (object *pl = visible_to ()) 1659 if (object *pl = visible_to ())
1601 esrv_update_item (UPD_NROF, pl, this); 1660 esrv_update_item (UPD_NROF, pl, this);
1661
1662 adjust_weight (env, oweight, total_weight ());
1602 1663
1603 return true; 1664 return true;
1604 } 1665 }
1605 else 1666 else
1606 { 1667 {
1681 if (op->nrof) 1742 if (op->nrof)
1682 for (object *tmp = inv; tmp; tmp = tmp->below) 1743 for (object *tmp = inv; tmp; tmp = tmp->below)
1683 if (object::can_merge (tmp, op)) 1744 if (object::can_merge (tmp, op))
1684 { 1745 {
1685 /* return the original object and remove inserted object 1746 /* return the original object and remove inserted object
1686 (client needs the original object) */ 1747 (client prefers the original object) */
1748
1749 // carring must be 0 for mergable objects
1750 sint64 oweight = tmp->weight * tmp->nrof;
1751
1687 tmp->nrof += op->nrof; 1752 tmp->nrof += op->nrof;
1688 1753
1689 if (object *pl = tmp->visible_to ()) 1754 if (object *pl = tmp->visible_to ())
1690 esrv_update_item (UPD_NROF, pl, tmp); 1755 esrv_update_item (UPD_NROF, pl, tmp);
1691 1756
1692 adjust_weight (this, op->total_weight ()); 1757 adjust_weight (this, oweight, tmp->weight * tmp->nrof);
1693 1758
1694 op->destroy (); 1759 op->destroy ();
1695 op = tmp; 1760 op = tmp;
1696 goto inserted; 1761 goto inserted;
1697 } 1762 }
1713 op->flag [FLAG_REMOVED] = 0; 1778 op->flag [FLAG_REMOVED] = 0;
1714 1779
1715 if (object *pl = op->visible_to ()) 1780 if (object *pl = op->visible_to ())
1716 esrv_send_item (pl, op); 1781 esrv_send_item (pl, op);
1717 1782
1718 adjust_weight (this, op->total_weight ()); 1783 adjust_weight (this, 0, op->total_weight ());
1719 1784
1720inserted: 1785inserted:
1721 /* reset the light list and los of the players on the map */ 1786 /* reset the light list and los of the players on the map */
1722 if (op->glow_radius && is_on_map ()) 1787 if (op->glow_radius && is_on_map ())
1723 { 1788 {
1752 * MSW 2001-07-08: Check all objects on space, not just those below 1817 * MSW 2001-07-08: Check all objects on space, not just those below
1753 * object being inserted. insert_ob_in_map may not put new objects 1818 * object being inserted. insert_ob_in_map may not put new objects
1754 * on top. 1819 * on top.
1755 */ 1820 */
1756int 1821int
1757check_move_on (object *op, object *originator) 1822check_move_on (object *op, object *originator, int flags)
1758{ 1823{
1759 if (op->flag [FLAG_NO_APPLY]) 1824 if (op->flag [FLAG_NO_APPLY])
1760 return 0; 1825 return 0;
1761 1826
1762 object *tmp; 1827 object *tmp;
1821 1886
1822 /* Basically same logic as above, except now for actual apply. */ 1887 /* Basically same logic as above, except now for actual apply. */
1823 if ((!op->move_type && tmp->move_on & MOVE_WALK) || 1888 if ((!op->move_type && tmp->move_on & MOVE_WALK) ||
1824 ((op->move_type & tmp->move_on) && (op->move_type & ~tmp->move_on & ~tmp->move_block) == 0)) 1889 ((op->move_type & tmp->move_on) && (op->move_type & ~tmp->move_on & ~tmp->move_block) == 0))
1825 { 1890 {
1891 if ((flags & INS_NO_AUTO_EXIT)
1892 && (tmp->type == EXIT || tmp->type == TELEPORTER
1893 || tmp->type == HOLE || tmp->type == TRAPDOOR)) //TODO: temporary, fix exits instead
1894 continue;
1895
1826 move_apply (tmp, op, originator); 1896 move_apply (tmp, op, originator);
1827 1897
1828 if (op->destroyed ()) 1898 if (op->destroyed ())
1829 return 1; 1899 return 1;
1830 1900
2155{ 2225{
2156 return (ob1->x - ob2->x) * (ob1->x - ob2->x) + (ob1->y - ob2->y) * (ob1->y - ob2->y); 2226 return (ob1->x - ob2->x) * (ob1->x - ob2->x) + (ob1->y - ob2->y) * (ob1->y - ob2->y);
2157} 2227}
2158 2228
2159/* 2229/*
2160 * find_dir_2(delta-x,delta-y) will return a direction in which 2230 * find_dir_2(delta-x,delta-y) will return a direction value
2161 * an object which has subtracted the x and y coordinates of another 2231 * for running into direct [dx, dy].
2162 * object, needs to travel toward it. 2232 * (the opposite of crossfire's find_dir_2!)
2163 */ 2233 */
2164int 2234int
2165find_dir_2 (int x, int y) 2235find_dir_2 (int x, int y)
2166{ 2236{
2237#if 1 // new algorithm
2238 // this works by putting x, y into 16 sectors, which
2239 // are not equal sized, but are a better approximation
2240 // then the old algorithm, and then using a mapping
2241 // table to map it into a direction value.
2242 // basically, it maps these comparisons to each bit
2243 // bit #3: x < 0
2244 // bit #2: y < 0
2245 // bit #1: x > y
2246 // bit #0: x > 2y
2247
2248 static const uint8 dir[16] = {
2249 4, 5, 4, 3,
2250 2, 1, 2, 3,
2251 6, 5, 6, 7,
2252 8, 1, 8, 7,
2253 };
2254 int sector = 0;
2255
2256 // this is a bit ugly, but more likely to result in branchless code
2257 sector |= x < 0 ? 8 : 0;
2258 x = x < 0 ? -x : x; // abs
2259
2260 sector |= y < 0 ? 4 : 0;
2261 y = y < 0 ? -y : y; // abs
2262
2263 if (x > y)
2264 {
2265 sector |= 2;
2266
2267 if (x > y * 2)
2268 sector |= 1;
2269 }
2270 else
2271 {
2272 if (y > x * 2)
2273 sector |= 1;
2274 else if (!y)
2275 return 0; // x == 0 here
2276 }
2277
2278 return dir [sector];
2279#else // old algorithm
2167 int q; 2280 int q;
2168 2281
2169 if (y) 2282 if (y)
2170 q = x * 100 / y; 2283 q = 128 * x / y;
2171 else if (x) 2284 else if (x)
2172 q = -300 * x; 2285 q = -512 * x; // to make it > 309
2173 else 2286 else
2174 return 0; 2287 return 0;
2175 2288
2176 if (y > 0) 2289 if (y > 0)
2177 { 2290 {
2178 if (q < -242) 2291 if (q < -309) return 7;
2292 if (q < -52) return 6;
2293 if (q < 52) return 5;
2294 if (q < 309) return 4;
2295
2179 return 3; 2296 return 3;
2180 if (q < -41) 2297 }
2181 return 2; 2298 else
2182 if (q < 41) 2299 {
2183 return 1; 2300 if (q < -309) return 3;
2184 if (q < 242) 2301 if (q < -52) return 2;
2185 return 8; 2302 if (q < 52) return 1;
2303 if (q < 309) return 8;
2304
2186 return 7; 2305 return 7;
2187 } 2306 }
2188 2307#endif
2189 if (q < -242)
2190 return 7;
2191 if (q < -41)
2192 return 6;
2193 if (q < 41)
2194 return 5;
2195 if (q < 242)
2196 return 4;
2197
2198 return 3;
2199} 2308}
2200 2309
2201/* 2310/*
2202 * dirdiff(dir1, dir2) returns how many 45-degrees differences there is 2311 * dirdiff(dir1, dir2) returns how many 45-degrees differences there is
2203 * between two directions (which are expected to be absolute (see absdir()) 2312 * between two directions (which are expected to be absolute (see absdir())
2204 */ 2313 */
2205int 2314int
2206dirdiff (int dir1, int dir2) 2315dirdiff (int dir1, int dir2)
2207{ 2316{
2208 int d;
2209
2210 d = abs (dir1 - dir2); 2317 int d = abs (dir1 - dir2);
2211 if (d > 4)
2212 d = 8 - d;
2213 2318
2214 return d; 2319 return d > 4 ? 8 - d : d;
2215} 2320}
2216 2321
2217/* peterm: 2322/* peterm:
2218 * do LOS stuff for ball lightning. Go after the closest VISIBLE monster. 2323 * do LOS stuff for ball lightning. Go after the closest VISIBLE monster.
2219 * Basically, this is a table of directions, and what directions 2324 * Basically, this is a table of directions, and what directions
2677 } 2782 }
2678 else 2783 else
2679 move_type = mt; 2784 move_type = mt;
2680} 2785}
2681 2786
2787/* object should be a player.
2788 * we return the object the player has marked with the 'mark' command
2789 * below. If no match is found (or object has changed), we return
2790 * NULL. We leave it up to the calling function to print messages if
2791 * nothing is found.
2792 */
2793object *
2794object::mark () const
2795{
2796 if (contr && contr->mark && contr->mark->env == this)
2797 return contr->mark;
2798 else
2799 return 0;
2800}
2801

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines