… | |
… | |
354 | return 0; |
354 | return 0; |
355 | } |
355 | } |
356 | |
356 | |
357 | // adjust weight per container type ("of holding") |
357 | // adjust weight per container type ("of holding") |
358 | static sint32 |
358 | static sint32 |
359 | weight_adjust (object *op, sint32 weight) |
359 | weight_adjust_for (object *op, sint32 weight) |
360 | { |
360 | { |
361 | return op->type == CONTAINER |
361 | return op->type == CONTAINER |
362 | ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) |
362 | ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) |
363 | : weight; |
363 | : weight; |
364 | } |
364 | } |
… | |
… | |
370 | static void |
370 | static void |
371 | adjust_weight (object *op, sint32 weight) |
371 | adjust_weight (object *op, sint32 weight) |
372 | { |
372 | { |
373 | while (op) |
373 | while (op) |
374 | { |
374 | { |
|
|
375 | // adjust by actual difference to account for rounding errors |
|
|
376 | // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct |
375 | weight = weight_adjust (op, weight); |
377 | weight = weight_adjust_for (op, op->carrying) |
|
|
378 | - weight_adjust_for (op, op->carrying - weight); |
376 | |
379 | |
377 | if (!weight) |
380 | if (!weight) |
378 | return; |
381 | return; |
379 | |
382 | |
380 | op->carrying += weight; |
383 | op->carrying += weight; |
… | |
… | |
403 | op->update_weight (); |
406 | op->update_weight (); |
404 | |
407 | |
405 | sum += op->total_weight (); |
408 | sum += op->total_weight (); |
406 | } |
409 | } |
407 | |
410 | |
408 | sum = weight_adjust (this, sum); |
411 | sum = weight_adjust_for (this, sum); |
409 | |
412 | |
410 | if (sum != carrying) |
413 | if (sum != carrying) |
411 | { |
414 | { |
412 | carrying = sum; |
415 | carrying = sum; |
413 | |
416 | |
… | |
… | |
901 | * drop on that space. |
904 | * drop on that space. |
902 | */ |
905 | */ |
903 | if (!drop_to_ground |
906 | if (!drop_to_ground |
904 | || !map |
907 | || !map |
905 | || map->in_memory != MAP_ACTIVE |
908 | || map->in_memory != MAP_ACTIVE |
906 | || map->nodrop |
909 | || map->no_drop |
907 | || ms ().move_block == MOVE_ALL) |
910 | || ms ().move_block == MOVE_ALL) |
908 | { |
911 | { |
909 | while (inv) |
912 | while (inv) |
910 | { |
913 | { |
911 | inv->destroy_inv (false); |
914 | inv->destroy_inv (false); |
… | |
… | |
940 | |
943 | |
941 | static struct freed_map : maptile |
944 | static struct freed_map : maptile |
942 | { |
945 | { |
943 | freed_map () |
946 | freed_map () |
944 | { |
947 | { |
945 | path = "<freed objects map>"; |
948 | path = "<freed objects map>"; |
946 | name = "/internal/freed_objects_map"; |
949 | name = "/internal/freed_objects_map"; |
947 | width = 3; |
950 | width = 3; |
948 | height = 3; |
951 | height = 3; |
949 | nodrop = 1; |
952 | no_drop = 1; |
|
|
953 | no_reset = 1; |
950 | |
954 | |
951 | alloc (); |
955 | alloc (); |
952 | in_memory = MAP_ACTIVE; |
956 | in_memory = MAP_ACTIVE; |
|
|
957 | } |
|
|
958 | |
|
|
959 | ~freed_map () |
|
|
960 | { |
|
|
961 | destroy (); |
953 | } |
962 | } |
954 | } freed_map; // freed objects are moved here to avoid crashes |
963 | } freed_map; // freed objects are moved here to avoid crashes |
955 | |
964 | |
956 | void |
965 | void |
957 | object::do_destroy () |
966 | object::do_destroy () |
… | |
… | |
1048 | if (object *pl = visible_to ()) |
1057 | if (object *pl = visible_to ()) |
1049 | esrv_del_item (pl->contr, count); |
1058 | esrv_del_item (pl->contr, count); |
1050 | flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed |
1059 | flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed |
1051 | |
1060 | |
1052 | adjust_weight (env, -total_weight ()); |
1061 | adjust_weight (env, -total_weight ()); |
1053 | |
|
|
1054 | *(above ? &above->below : &env->inv) = below; |
|
|
1055 | |
|
|
1056 | if (below) |
|
|
1057 | below->above = above; |
|
|
1058 | |
1062 | |
1059 | /* we set up values so that it could be inserted into |
1063 | /* we set up values so that it could be inserted into |
1060 | * the map, but we don't actually do that - it is up |
1064 | * the map, but we don't actually do that - it is up |
1061 | * to the caller to decide what we want to do. |
1065 | * to the caller to decide what we want to do. |
1062 | */ |
1066 | */ |
1063 | map = env->map; |
1067 | map = env->map; |
1064 | x = env->x; |
1068 | x = env->x; |
1065 | y = env->y; |
1069 | y = env->y; |
|
|
1070 | |
|
|
1071 | // make sure cmov optimisation is applicable |
|
|
1072 | *(above ? &above->below : &env->inv) = below; |
|
|
1073 | *(below ? &below->above : &above ) = above; // &above is just a dummy |
|
|
1074 | |
1066 | above = 0; |
1075 | above = 0; |
1067 | below = 0; |
1076 | below = 0; |
1068 | env = 0; |
1077 | env = 0; |
1069 | |
1078 | |
1070 | /* NO_FIX_PLAYER is set when a great many changes are being |
1079 | /* NO_FIX_PLAYER is set when a great many changes are being |
… | |
… | |
1100 | |
1109 | |
1101 | esrv_del_item (pl->contr, count); |
1110 | esrv_del_item (pl->contr, count); |
1102 | } |
1111 | } |
1103 | |
1112 | |
1104 | /* link the object above us */ |
1113 | /* link the object above us */ |
1105 | if (above) |
1114 | // re-link, make sure compiler can easily use cmove |
1106 | above->below = below; |
1115 | *(above ? &above->below : &ms.top) = below; |
1107 | else |
1116 | *(below ? &below->above : &ms.bot) = above; |
1108 | ms.top = below; /* we were top, set new top */ |
|
|
1109 | |
|
|
1110 | /* Relink the object below us, if there is one */ |
|
|
1111 | if (below) |
|
|
1112 | below->above = above; |
|
|
1113 | else |
|
|
1114 | { |
|
|
1115 | /* Nothing below, which means we need to relink map object for this space |
|
|
1116 | * use translated coordinates in case some oddness with map tiling is |
|
|
1117 | * evident |
|
|
1118 | */ |
|
|
1119 | if (GET_MAP_OB (map, x, y) != this) |
|
|
1120 | LOG (llevError, "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom? %s\n", debug_desc ()); |
|
|
1121 | |
|
|
1122 | ms.bot = above; /* goes on above it. */ |
|
|
1123 | } |
|
|
1124 | |
1117 | |
1125 | above = 0; |
1118 | above = 0; |
1126 | below = 0; |
1119 | below = 0; |
1127 | |
1120 | |
1128 | if (map->in_memory == MAP_SAVING) |
1121 | if (map->in_memory == MAP_SAVING) |
… | |
… | |
1275 | * just 'op' otherwise |
1268 | * just 'op' otherwise |
1276 | */ |
1269 | */ |
1277 | object * |
1270 | object * |
1278 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1271 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1279 | { |
1272 | { |
1280 | assert (!op->flag [FLAG_FREED]); |
|
|
1281 | |
|
|
1282 | op->remove (); |
1273 | op->remove (); |
1283 | |
1274 | |
1284 | /* Ideally, the caller figures this out. However, it complicates a lot |
1275 | /* Ideally, the caller figures this out. However, it complicates a lot |
1285 | * of areas of callers (eg, anything that uses find_free_spot would now |
1276 | * of areas of callers (eg, anything that uses find_free_spot would now |
1286 | * need extra work |
1277 | * need extra work |
1287 | */ |
1278 | */ |
1288 | if (!xy_normalise (m, op->x, op->y)) |
1279 | if (!xy_normalise (m, op->x, op->y)) |
1289 | { |
1280 | { |
1290 | op->destroy (1); |
1281 | op->head_ ()->destroy (1);// remove head_ once all tail object destroyers found |
1291 | return 0; |
1282 | return 0; |
1292 | } |
1283 | } |
1293 | |
1284 | |
1294 | if (object *more = op->more) |
1285 | if (object *more = op->more) |
1295 | if (!insert_ob_in_map (more, m, originator, flag)) |
1286 | if (!insert_ob_in_map (more, m, originator, flag)) |
… | |
… | |
1304 | */ |
1295 | */ |
1305 | if (op->nrof && !(flag & INS_NO_MERGE)) |
1296 | if (op->nrof && !(flag & INS_NO_MERGE)) |
1306 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1297 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1307 | if (object::can_merge (op, tmp)) |
1298 | if (object::can_merge (op, tmp)) |
1308 | { |
1299 | { |
1309 | // TODO: we atcually want to update tmp, not op, |
1300 | // TODO: we actually want to update tmp, not op, |
1310 | // but some caller surely breaks when we return tmp |
1301 | // but some caller surely breaks when we return tmp |
1311 | // from here :/ |
1302 | // from here :/ |
1312 | op->nrof += tmp->nrof; |
1303 | op->nrof += tmp->nrof; |
1313 | tmp->destroy (1); |
1304 | tmp->destroy (1); |
1314 | } |
1305 | } |
… | |
… | |
1319 | if (!QUERY_FLAG (op, FLAG_ALIVE)) |
1310 | if (!QUERY_FLAG (op, FLAG_ALIVE)) |
1320 | CLEAR_FLAG (op, FLAG_NO_STEAL); |
1311 | CLEAR_FLAG (op, FLAG_NO_STEAL); |
1321 | |
1312 | |
1322 | if (flag & INS_BELOW_ORIGINATOR) |
1313 | if (flag & INS_BELOW_ORIGINATOR) |
1323 | { |
1314 | { |
1324 | if (originator->map != op->map || originator->x != op->x || originator->y != op->y) |
1315 | if (originator->map != op->map || originator->x != op->x || originator->y != op->y || !originator->is_on_map ()) |
1325 | { |
1316 | { |
1326 | LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); |
1317 | LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); |
1327 | abort (); |
1318 | abort (); |
1328 | } |
1319 | } |
1329 | |
1320 | |
1330 | op->above = originator; |
1321 | op->above = originator; |
1331 | op->below = originator->below; |
1322 | op->below = originator->below; |
1332 | |
|
|
1333 | if (op->below) |
|
|
1334 | op->below->above = op; |
|
|
1335 | else |
|
|
1336 | ms.bot = op; |
|
|
1337 | |
|
|
1338 | /* since *below* originator, no need to update top */ |
|
|
1339 | originator->below = op; |
1323 | originator->below = op; |
|
|
1324 | |
|
|
1325 | *(op->below ? &op->below->above : &ms.bot) = op; |
1340 | } |
1326 | } |
1341 | else |
1327 | else |
1342 | { |
1328 | { |
1343 | object *top, *floor = NULL; |
1329 | object *floor = 0; |
1344 | |
1330 | object *top = ms.top; |
1345 | top = ms.bot; |
|
|
1346 | |
1331 | |
1347 | /* If there are other objects, then */ |
1332 | /* If there are other objects, then */ |
1348 | if (top) |
1333 | if (top) |
1349 | { |
1334 | { |
1350 | object *last = 0; |
|
|
1351 | |
|
|
1352 | /* |
1335 | /* |
1353 | * If there are multiple objects on this space, we do some trickier handling. |
1336 | * If there are multiple objects on this space, we do some trickier handling. |
1354 | * We've already dealt with merging if appropriate. |
1337 | * We've already dealt with merging if appropriate. |
1355 | * Generally, we want to put the new object on top. But if |
1338 | * Generally, we want to put the new object on top. But if |
1356 | * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last |
1339 | * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last |
… | |
… | |
1359 | * once we get to them. This reduces the need to traverse over all of |
1342 | * once we get to them. This reduces the need to traverse over all of |
1360 | * them when adding another one - this saves quite a bit of cpu time |
1343 | * them when adding another one - this saves quite a bit of cpu time |
1361 | * when lots of spells are cast in one area. Currently, it is presumed |
1344 | * when lots of spells are cast in one area. Currently, it is presumed |
1362 | * that flying non pickable objects are spell objects. |
1345 | * that flying non pickable objects are spell objects. |
1363 | */ |
1346 | */ |
1364 | for (top = ms.bot; top; top = top->above) |
1347 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1365 | { |
1348 | { |
1366 | if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) |
1349 | if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) || QUERY_FLAG (tmp, FLAG_OVERLAY_FLOOR)) |
1367 | floor = top; |
1350 | floor = tmp; |
1368 | |
1351 | |
1369 | if (QUERY_FLAG (top, FLAG_NO_PICK) && (top->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (top, FLAG_IS_FLOOR)) |
1352 | if (QUERY_FLAG (tmp, FLAG_NO_PICK) && (tmp->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (tmp, FLAG_IS_FLOOR)) |
1370 | { |
1353 | { |
1371 | /* We insert above top, so we want this object below this */ |
1354 | /* We insert above top, so we want this object below this */ |
1372 | top = top->below; |
1355 | top = tmp->below; |
1373 | break; |
1356 | break; |
1374 | } |
1357 | } |
1375 | |
1358 | |
1376 | last = top; |
1359 | top = tmp; |
1377 | } |
1360 | } |
1378 | |
|
|
1379 | /* Don't want top to be NULL, so set it to the last valid object */ |
|
|
1380 | top = last; |
|
|
1381 | |
1361 | |
1382 | /* We let update_position deal with figuring out what the space |
1362 | /* We let update_position deal with figuring out what the space |
1383 | * looks like instead of lots of conditions here. |
1363 | * looks like instead of lots of conditions here. |
1384 | * makes things faster, and effectively the same result. |
1364 | * makes things faster, and effectively the same result. |
1385 | */ |
1365 | */ |
… | |
… | |
1392 | */ |
1372 | */ |
1393 | if (!(flag & INS_ON_TOP) |
1373 | if (!(flag & INS_ON_TOP) |
1394 | && ms.flags () & P_BLOCKSVIEW |
1374 | && ms.flags () & P_BLOCKSVIEW |
1395 | && (op->face && !faces [op->face].visibility)) |
1375 | && (op->face && !faces [op->face].visibility)) |
1396 | { |
1376 | { |
|
|
1377 | object *last; |
|
|
1378 | |
1397 | for (last = top; last != floor; last = last->below) |
1379 | for (last = top; last != floor; last = last->below) |
1398 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1380 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1399 | break; |
1381 | break; |
1400 | |
1382 | |
1401 | /* Check to see if we found the object that blocks view, |
1383 | /* Check to see if we found the object that blocks view, |
… | |
… | |
1409 | } /* If objects on this space */ |
1391 | } /* If objects on this space */ |
1410 | |
1392 | |
1411 | if (flag & INS_ABOVE_FLOOR_ONLY) |
1393 | if (flag & INS_ABOVE_FLOOR_ONLY) |
1412 | top = floor; |
1394 | top = floor; |
1413 | |
1395 | |
1414 | /* Top is the object that our object (op) is going to get inserted above. |
1396 | // insert object above top, or bottom-most if top = 0 |
1415 | */ |
|
|
1416 | |
|
|
1417 | /* First object on this space */ |
|
|
1418 | if (!top) |
1397 | if (!top) |
1419 | { |
1398 | { |
|
|
1399 | op->below = 0; |
1420 | op->above = ms.bot; |
1400 | op->above = ms.bot; |
1421 | |
|
|
1422 | if (op->above) |
|
|
1423 | op->above->below = op; |
|
|
1424 | |
|
|
1425 | op->below = 0; |
|
|
1426 | ms.bot = op; |
1401 | ms.bot = op; |
|
|
1402 | |
|
|
1403 | *(op->above ? &op->above->below : &ms.top) = op; |
1427 | } |
1404 | } |
1428 | else |
1405 | else |
1429 | { /* get inserted into the stack above top */ |
1406 | { |
1430 | op->above = top->above; |
1407 | op->above = top->above; |
1431 | |
|
|
1432 | if (op->above) |
|
|
1433 | op->above->below = op; |
1408 | top->above = op; |
1434 | |
1409 | |
1435 | op->below = top; |
1410 | op->below = top; |
1436 | top->above = op; |
1411 | *(op->above ? &op->above->below : &ms.top) = op; |
1437 | } |
1412 | } |
1438 | |
1413 | } |
1439 | if (!op->above) |
|
|
1440 | ms.top = op; |
|
|
1441 | } /* else not INS_BELOW_ORIGINATOR */ |
|
|
1442 | |
1414 | |
1443 | if (op->type == PLAYER) |
1415 | if (op->type == PLAYER) |
1444 | { |
1416 | { |
1445 | op->contr->do_los = 1; |
1417 | op->contr->do_los = 1; |
1446 | ++op->map->players; |
1418 | ++op->map->players; |
… | |
… | |
1582 | } |
1554 | } |
1583 | else |
1555 | else |
1584 | { |
1556 | { |
1585 | decrease (nr); |
1557 | decrease (nr); |
1586 | |
1558 | |
1587 | object *op = object_create_clone (this); |
1559 | object *op = deep_clone (); |
1588 | op->nrof = nr; |
1560 | op->nrof = nr; |
1589 | return op; |
1561 | return op; |
1590 | } |
1562 | } |
1591 | } |
1563 | } |
1592 | |
1564 | |
… | |
… | |
1813 | LOG (llevError, "Present_arch called outside map.\n"); |
1785 | LOG (llevError, "Present_arch called outside map.\n"); |
1814 | return NULL; |
1786 | return NULL; |
1815 | } |
1787 | } |
1816 | |
1788 | |
1817 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1789 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1818 | if (tmp->arch == at) |
1790 | if (tmp->arch->archname == at->archname) |
1819 | return tmp; |
1791 | return tmp; |
1820 | |
1792 | |
1821 | return NULL; |
1793 | return NULL; |
1822 | } |
1794 | } |
1823 | |
1795 | |
… | |
… | |
1887 | * The first matching object is returned, or NULL if none. |
1859 | * The first matching object is returned, or NULL if none. |
1888 | */ |
1860 | */ |
1889 | object * |
1861 | object * |
1890 | present_arch_in_ob (const archetype *at, const object *op) |
1862 | present_arch_in_ob (const archetype *at, const object *op) |
1891 | { |
1863 | { |
1892 | for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below) |
1864 | for (object *tmp = op->inv; tmp; tmp = tmp->below) |
1893 | if (tmp->arch == at) |
1865 | if (tmp->arch->archname == at->archname) |
1894 | return tmp; |
1866 | return tmp; |
1895 | |
1867 | |
1896 | return NULL; |
1868 | return NULL; |
1897 | } |
1869 | } |
1898 | |
1870 | |
… | |
… | |
2307 | |
2279 | |
2308 | /* |
2280 | /* |
2309 | * create clone from object to another |
2281 | * create clone from object to another |
2310 | */ |
2282 | */ |
2311 | object * |
2283 | object * |
2312 | object_create_clone (object *asrc) |
2284 | object::deep_clone () |
2313 | { |
2285 | { |
|
|
2286 | assert (("deep_clone called on non-head object", is_head ())); |
|
|
2287 | |
2314 | object *dst = 0; |
2288 | object *dst = clone (); |
2315 | |
2289 | |
2316 | if (!asrc) |
|
|
2317 | return 0; |
|
|
2318 | |
|
|
2319 | object *src = asrc->head_ (); |
|
|
2320 | |
|
|
2321 | object *prev = 0; |
2290 | object *prev = dst; |
2322 | for (object *part = src; part; part = part->more) |
2291 | for (object *part = this->more; part; part = part->more) |
2323 | { |
2292 | { |
2324 | object *tmp = part->clone (); |
2293 | object *tmp = part->clone (); |
2325 | |
|
|
2326 | tmp->x -= src->x; |
|
|
2327 | tmp->y -= src->y; |
|
|
2328 | |
|
|
2329 | if (!part->head) |
|
|
2330 | { |
|
|
2331 | dst = tmp; |
|
|
2332 | tmp->head = 0; |
|
|
2333 | } |
|
|
2334 | else |
|
|
2335 | tmp->head = dst; |
2294 | tmp->head = dst; |
2336 | |
|
|
2337 | tmp->more = 0; |
|
|
2338 | |
|
|
2339 | if (prev) |
|
|
2340 | prev->more = tmp; |
2295 | prev->more = tmp; |
2341 | |
|
|
2342 | prev = tmp; |
2296 | prev = tmp; |
2343 | } |
2297 | } |
2344 | |
2298 | |
2345 | for (object *item = src->inv; item; item = item->below) |
2299 | for (object *item = inv; item; item = item->below) |
2346 | insert_ob_in_ob (object_create_clone (item), dst); |
2300 | insert_ob_in_ob (item->deep_clone (), dst); |
2347 | |
2301 | |
2348 | return dst; |
2302 | return dst; |
2349 | } |
2303 | } |
2350 | |
2304 | |
2351 | /* This returns the first object in who's inventory that |
2305 | /* This returns the first object in who's inventory that |