… | |
… | |
426 | |
426 | |
427 | return 0; |
427 | return 0; |
428 | } |
428 | } |
429 | |
429 | |
430 | // adjust weight per container type ("of holding") |
430 | // adjust weight per container type ("of holding") |
431 | static sint32 |
431 | static uint32 |
432 | weight_adjust_for (object *op, sint32 weight) |
432 | weight_adjust_for (object *op, uint32 weight) |
433 | { |
433 | { |
434 | return op->type == CONTAINER |
434 | return op->type == CONTAINER |
435 | ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) |
435 | ? weight - weight * op->stats.Str / 100 |
436 | : weight; |
436 | : weight; |
437 | } |
437 | } |
438 | |
438 | |
439 | /* |
439 | /* |
440 | * adjust_weight(object, weight) adds the specified weight to an object, |
440 | * subtracts, then adds, the specified weight to an object, |
441 | * and also updates how much the environment(s) is/are carrying. |
441 | * and also updates how much the environment(s) is/are carrying. |
442 | */ |
442 | */ |
443 | static void |
443 | static void |
444 | adjust_weight (object *op, sint32 weight) |
444 | adjust_weight (object *op, sint32 sub, sint32 add) |
445 | { |
445 | { |
446 | while (op) |
446 | while (op) |
447 | { |
447 | { |
448 | // adjust by actual difference to account for rounding errors |
448 | sint32 ocarrying = op->carrying; |
449 | // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct |
|
|
450 | weight = weight_adjust_for (op, op->carrying) |
|
|
451 | - weight_adjust_for (op, op->carrying - weight); |
|
|
452 | |
449 | |
453 | if (!weight) |
450 | op->carrying -= weight_adjust_for (op, sub); |
454 | return; |
451 | op->carrying += weight_adjust_for (op, add); |
455 | |
|
|
456 | op->carrying += weight; |
|
|
457 | |
452 | |
458 | if (object *pl = op->visible_to ()) |
453 | if (object *pl = op->visible_to ()) |
459 | if (pl != op) // player is handled lazily |
454 | if (pl != op) // player is handled lazily |
460 | esrv_update_item (UPD_WEIGHT, pl, op); |
455 | esrv_update_item (UPD_WEIGHT, pl, op); |
461 | |
456 | |
|
|
457 | sub = ocarrying; |
|
|
458 | add = op->carrying; |
|
|
459 | |
462 | op = op->env; |
460 | op = op->env; |
463 | } |
461 | } |
464 | } |
462 | } |
465 | |
463 | |
466 | /* |
464 | /* |
… | |
… | |
473 | { |
471 | { |
474 | sint32 sum = 0; |
472 | sint32 sum = 0; |
475 | |
473 | |
476 | for (object *op = inv; op; op = op->below) |
474 | for (object *op = inv; op; op = op->below) |
477 | { |
475 | { |
478 | if (op->inv) |
|
|
479 | op->update_weight (); |
476 | op->update_weight (); |
480 | |
477 | |
481 | sum += op->total_weight (); |
478 | sum += weight_adjust_for (this, op->total_weight ()); |
482 | } |
479 | } |
483 | |
|
|
484 | sum = weight_adjust_for (this, sum); |
|
|
485 | |
480 | |
486 | if (sum != carrying) |
481 | if (sum != carrying) |
487 | { |
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 | |
488 | carrying = sum; |
487 | carrying = sum; |
489 | |
488 | |
490 | if (object *pl = visible_to ()) |
489 | if (object *pl = visible_to ()) |
491 | if (pl != this) // player is handled lazily |
490 | if (pl != this) // player is handled lazily |
492 | esrv_update_item (UPD_WEIGHT, pl, this); |
491 | esrv_update_item (UPD_WEIGHT, pl, this); |
… | |
… | |
1031 | } |
1030 | } |
1032 | |
1031 | |
1033 | static struct freed_map : maptile |
1032 | static struct freed_map : maptile |
1034 | { |
1033 | { |
1035 | freed_map () |
1034 | freed_map () |
|
|
1035 | : maptile (3, 3) |
1036 | { |
1036 | { |
1037 | path = "<freed objects map>"; |
1037 | path = "<freed objects map>"; |
1038 | name = "/internal/freed_objects_map"; |
1038 | name = "/internal/freed_objects_map"; |
1039 | width = 3; |
|
|
1040 | height = 3; |
|
|
1041 | no_drop = 1; |
1039 | no_drop = 1; |
1042 | no_reset = 1; |
1040 | no_reset = 1; |
1043 | |
1041 | |
1044 | alloc (); |
|
|
1045 | in_memory = MAP_ACTIVE; |
1042 | in_memory = MAP_ACTIVE; |
1046 | } |
1043 | } |
1047 | |
1044 | |
1048 | ~freed_map () |
1045 | ~freed_map () |
1049 | { |
1046 | { |
… | |
… | |
1142 | 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 |
1143 | if (object *pl = visible_to ()) |
1140 | if (object *pl = visible_to ()) |
1144 | esrv_del_item (pl->contr, count); |
1141 | esrv_del_item (pl->contr, count); |
1145 | 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 |
1146 | |
1143 | |
1147 | adjust_weight (env, -total_weight ()); |
1144 | adjust_weight (env, total_weight (), 0); |
1148 | |
1145 | |
1149 | object *pl = in_player (); |
1146 | object *pl = in_player (); |
1150 | |
1147 | |
1151 | /* we set up values so that it could be inserted into |
1148 | /* we set up values so that it could be inserted into |
1152 | * 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 |
… | |
… | |
1570 | */ |
1567 | */ |
1571 | |
1568 | |
1572 | /* 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 */ |
1573 | if (!(flag & INS_NO_WALK_ON) && op->is_head ()) |
1570 | if (!(flag & INS_NO_WALK_ON) && op->is_head ()) |
1574 | { |
1571 | { |
1575 | if (check_move_on (op, originator)) |
1572 | if (check_move_on (op, originator, flag)) |
1576 | return 0; |
1573 | return 0; |
1577 | |
1574 | |
1578 | /* 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 |
1579 | * walk on's. |
1576 | * walk on's. |
1580 | */ |
1577 | */ |
1581 | for (object *tmp = op->more; tmp; tmp = tmp->more) |
1578 | for (object *tmp = op->more; tmp; tmp = tmp->more) |
1582 | if (check_move_on (tmp, originator)) |
1579 | if (check_move_on (tmp, originator, flag)) |
1583 | return 0; |
1580 | return 0; |
1584 | } |
1581 | } |
1585 | |
1582 | |
1586 | return op; |
1583 | return op; |
1587 | } |
1584 | } |
… | |
… | |
1653 | |
1650 | |
1654 | nr = min (nr, nrof); |
1651 | nr = min (nr, nrof); |
1655 | |
1652 | |
1656 | if (nrof > nr) |
1653 | if (nrof > nr) |
1657 | { |
1654 | { |
|
|
1655 | sint64 oweight = total_weight (); |
|
|
1656 | |
1658 | nrof -= nr; |
1657 | nrof -= nr; |
1659 | adjust_weight (env, -weight * max (1, nr)); // carrying == 0 |
|
|
1660 | |
1658 | |
1661 | if (object *pl = visible_to ()) |
1659 | if (object *pl = visible_to ()) |
1662 | esrv_update_item (UPD_NROF, pl, this); |
1660 | esrv_update_item (UPD_NROF, pl, this); |
|
|
1661 | |
|
|
1662 | adjust_weight (env, oweight, total_weight ()); |
1663 | |
1663 | |
1664 | return true; |
1664 | return true; |
1665 | } |
1665 | } |
1666 | else |
1666 | else |
1667 | { |
1667 | { |
… | |
… | |
1742 | if (op->nrof) |
1742 | if (op->nrof) |
1743 | for (object *tmp = inv; tmp; tmp = tmp->below) |
1743 | for (object *tmp = inv; tmp; tmp = tmp->below) |
1744 | if (object::can_merge (tmp, op)) |
1744 | if (object::can_merge (tmp, op)) |
1745 | { |
1745 | { |
1746 | /* return the original object and remove inserted object |
1746 | /* return the original object and remove inserted object |
1747 | (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 | |
1748 | tmp->nrof += op->nrof; |
1752 | tmp->nrof += op->nrof; |
1749 | |
1753 | |
1750 | if (object *pl = tmp->visible_to ()) |
1754 | if (object *pl = tmp->visible_to ()) |
1751 | esrv_update_item (UPD_NROF, pl, tmp); |
1755 | esrv_update_item (UPD_NROF, pl, tmp); |
1752 | |
1756 | |
1753 | adjust_weight (this, op->total_weight ()); |
1757 | adjust_weight (this, oweight, tmp->weight * tmp->nrof); |
1754 | |
1758 | |
1755 | op->destroy (); |
1759 | op->destroy (); |
1756 | op = tmp; |
1760 | op = tmp; |
1757 | goto inserted; |
1761 | goto inserted; |
1758 | } |
1762 | } |
… | |
… | |
1774 | op->flag [FLAG_REMOVED] = 0; |
1778 | op->flag [FLAG_REMOVED] = 0; |
1775 | |
1779 | |
1776 | if (object *pl = op->visible_to ()) |
1780 | if (object *pl = op->visible_to ()) |
1777 | esrv_send_item (pl, op); |
1781 | esrv_send_item (pl, op); |
1778 | |
1782 | |
1779 | adjust_weight (this, op->total_weight ()); |
1783 | adjust_weight (this, 0, op->total_weight ()); |
1780 | |
1784 | |
1781 | inserted: |
1785 | inserted: |
1782 | /* 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 */ |
1783 | if (op->glow_radius && is_on_map ()) |
1787 | if (op->glow_radius && is_on_map ()) |
1784 | { |
1788 | { |
… | |
… | |
1813 | * 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 |
1814 | * 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 |
1815 | * on top. |
1819 | * on top. |
1816 | */ |
1820 | */ |
1817 | int |
1821 | int |
1818 | check_move_on (object *op, object *originator) |
1822 | check_move_on (object *op, object *originator, int flags) |
1819 | { |
1823 | { |
1820 | if (op->flag [FLAG_NO_APPLY]) |
1824 | if (op->flag [FLAG_NO_APPLY]) |
1821 | return 0; |
1825 | return 0; |
1822 | |
1826 | |
1823 | object *tmp; |
1827 | object *tmp; |
… | |
… | |
1882 | |
1886 | |
1883 | /* Basically same logic as above, except now for actual apply. */ |
1887 | /* Basically same logic as above, except now for actual apply. */ |
1884 | if ((!op->move_type && tmp->move_on & MOVE_WALK) || |
1888 | if ((!op->move_type && tmp->move_on & MOVE_WALK) || |
1885 | ((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)) |
1886 | { |
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 | |
1887 | move_apply (tmp, op, originator); |
1896 | move_apply (tmp, op, originator); |
1888 | |
1897 | |
1889 | if (op->destroyed ()) |
1898 | if (op->destroyed ()) |
1890 | return 1; |
1899 | return 1; |
1891 | |
1900 | |
… | |
… | |
2216 | { |
2225 | { |
2217 | 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); |
2218 | } |
2227 | } |
2219 | |
2228 | |
2220 | /* |
2229 | /* |
2221 | * 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 |
2222 | * an object which has subtracted the x and y coordinates of another |
2231 | * for running into direct [dx, dy]. |
2223 | * object, needs to travel toward it. |
2232 | * (the opposite of crossfire's find_dir_2!) |
2224 | */ |
2233 | */ |
2225 | int |
2234 | int |
2226 | find_dir_2 (int x, int y) |
2235 | find_dir_2 (int x, int y) |
2227 | { |
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 |
2228 | int q; |
2280 | int q; |
2229 | |
2281 | |
2230 | if (y) |
2282 | if (y) |
2231 | q = x * 100 / y; |
2283 | q = 128 * x / y; |
2232 | else if (x) |
2284 | else if (x) |
2233 | q = -300 * x; |
2285 | q = -512 * x; // to make it > 309 |
2234 | else |
2286 | else |
2235 | return 0; |
2287 | return 0; |
2236 | |
2288 | |
2237 | if (y > 0) |
2289 | if (y > 0) |
2238 | { |
2290 | { |
2239 | 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 | |
2240 | return 3; |
2296 | return 3; |
2241 | if (q < -41) |
2297 | } |
2242 | return 2; |
2298 | else |
2243 | if (q < 41) |
2299 | { |
2244 | return 1; |
2300 | if (q < -309) return 3; |
2245 | if (q < 242) |
2301 | if (q < -52) return 2; |
2246 | return 8; |
2302 | if (q < 52) return 1; |
|
|
2303 | if (q < 309) return 8; |
|
|
2304 | |
2247 | return 7; |
2305 | return 7; |
2248 | } |
2306 | } |
2249 | |
2307 | #endif |
2250 | if (q < -242) |
|
|
2251 | return 7; |
|
|
2252 | if (q < -41) |
|
|
2253 | return 6; |
|
|
2254 | if (q < 41) |
|
|
2255 | return 5; |
|
|
2256 | if (q < 242) |
|
|
2257 | return 4; |
|
|
2258 | |
|
|
2259 | return 3; |
|
|
2260 | } |
2308 | } |
2261 | |
2309 | |
2262 | /* |
2310 | /* |
2263 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2311 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2264 | * between two directions (which are expected to be absolute (see absdir()) |
2312 | * between two directions (which are expected to be absolute (see absdir()) |
2265 | */ |
2313 | */ |
2266 | int |
2314 | int |
2267 | dirdiff (int dir1, int dir2) |
2315 | dirdiff (int dir1, int dir2) |
2268 | { |
2316 | { |
2269 | int d; |
|
|
2270 | |
|
|
2271 | d = abs (dir1 - dir2); |
2317 | int d = abs (dir1 - dir2); |
2272 | if (d > 4) |
|
|
2273 | d = 8 - d; |
|
|
2274 | |
2318 | |
2275 | return d; |
2319 | return d > 4 ? 8 - d : d; |
2276 | } |
2320 | } |
2277 | |
2321 | |
2278 | /* peterm: |
2322 | /* peterm: |
2279 | * 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. |
2280 | * Basically, this is a table of directions, and what directions |
2324 | * Basically, this is a table of directions, and what directions |
… | |
… | |
2738 | } |
2782 | } |
2739 | else |
2783 | else |
2740 | move_type = mt; |
2784 | move_type = mt; |
2741 | } |
2785 | } |
2742 | |
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 | */ |
|
|
2793 | object * |
|
|
2794 | object::mark () const |
|
|
2795 | { |
|
|
2796 | if (contr && contr->mark && contr->mark->env == this) |
|
|
2797 | return contr->mark; |
|
|
2798 | else |
|
|
2799 | return 0; |
|
|
2800 | } |
|
|
2801 | |