… | |
… | |
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 | |
… | |
… | |
1054 | esrv_del_item (pl->contr, count); |
1057 | esrv_del_item (pl->contr, count); |
1055 | flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed |
1058 | flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed |
1056 | |
1059 | |
1057 | adjust_weight (env, -total_weight ()); |
1060 | adjust_weight (env, -total_weight ()); |
1058 | |
1061 | |
1059 | *(above ? &above->below : &env->inv) = below; |
|
|
1060 | |
|
|
1061 | if (below) |
|
|
1062 | below->above = above; |
|
|
1063 | |
|
|
1064 | /* we set up values so that it could be inserted into |
1062 | /* we set up values so that it could be inserted into |
1065 | * the map, but we don't actually do that - it is up |
1063 | * the map, but we don't actually do that - it is up |
1066 | * to the caller to decide what we want to do. |
1064 | * to the caller to decide what we want to do. |
1067 | */ |
1065 | */ |
1068 | map = env->map; |
1066 | map = env->map; |
1069 | x = env->x; |
1067 | x = env->x; |
1070 | y = env->y; |
1068 | y = env->y; |
|
|
1069 | |
|
|
1070 | // make sure cmov optimisation is applicable |
|
|
1071 | *(above ? &above->below : &env->inv) = below; |
|
|
1072 | *(below ? &below->above : &above ) = above; // &above is just a dummy |
|
|
1073 | |
1071 | above = 0; |
1074 | above = 0; |
1072 | below = 0; |
1075 | below = 0; |
1073 | env = 0; |
1076 | env = 0; |
1074 | |
1077 | |
1075 | /* NO_FIX_PLAYER is set when a great many changes are being |
1078 | /* NO_FIX_PLAYER is set when a great many changes are being |
… | |
… | |
1105 | |
1108 | |
1106 | esrv_del_item (pl->contr, count); |
1109 | esrv_del_item (pl->contr, count); |
1107 | } |
1110 | } |
1108 | |
1111 | |
1109 | /* link the object above us */ |
1112 | /* link the object above us */ |
1110 | if (above) |
1113 | // re-link, make sure compiler can easily use cmove |
1111 | above->below = below; |
1114 | *(above ? &above->below : &ms.top) = below; |
1112 | else |
1115 | *(below ? &below->above : &ms.bot) = above; |
1113 | ms.top = below; /* we were top, set new top */ |
|
|
1114 | |
|
|
1115 | /* Relink the object below us, if there is one */ |
|
|
1116 | if (below) |
|
|
1117 | below->above = above; |
|
|
1118 | else |
|
|
1119 | { |
|
|
1120 | /* Nothing below, which means we need to relink map object for this space |
|
|
1121 | * use translated coordinates in case some oddness with map tiling is |
|
|
1122 | * evident |
|
|
1123 | */ |
|
|
1124 | if (GET_MAP_OB (map, x, y) != this) |
|
|
1125 | 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 ()); |
|
|
1126 | |
|
|
1127 | ms.bot = above; /* goes on above it. */ |
|
|
1128 | } |
|
|
1129 | |
1116 | |
1130 | above = 0; |
1117 | above = 0; |
1131 | below = 0; |
1118 | below = 0; |
1132 | |
1119 | |
1133 | if (map->in_memory == MAP_SAVING) |
1120 | if (map->in_memory == MAP_SAVING) |
… | |
… | |
1280 | * just 'op' otherwise |
1267 | * just 'op' otherwise |
1281 | */ |
1268 | */ |
1282 | object * |
1269 | object * |
1283 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1270 | insert_ob_in_map (object *op, maptile *m, object *originator, int flag) |
1284 | { |
1271 | { |
1285 | assert (!op->flag [FLAG_FREED]); |
|
|
1286 | |
|
|
1287 | op->remove (); |
1272 | op->remove (); |
1288 | |
1273 | |
1289 | /* Ideally, the caller figures this out. However, it complicates a lot |
1274 | /* Ideally, the caller figures this out. However, it complicates a lot |
1290 | * of areas of callers (eg, anything that uses find_free_spot would now |
1275 | * of areas of callers (eg, anything that uses find_free_spot would now |
1291 | * need extra work |
1276 | * need extra work |
1292 | */ |
1277 | */ |
1293 | if (!xy_normalise (m, op->x, op->y)) |
1278 | if (!xy_normalise (m, op->x, op->y)) |
1294 | { |
1279 | { |
1295 | op->destroy (1); |
1280 | op->head_ ()->destroy (1);// remove head_ once all tail object destroyers found |
1296 | return 0; |
1281 | return 0; |
1297 | } |
1282 | } |
1298 | |
1283 | |
1299 | if (object *more = op->more) |
1284 | if (object *more = op->more) |
1300 | if (!insert_ob_in_map (more, m, originator, flag)) |
1285 | if (!insert_ob_in_map (more, m, originator, flag)) |
… | |
… | |
1309 | */ |
1294 | */ |
1310 | if (op->nrof && !(flag & INS_NO_MERGE)) |
1295 | if (op->nrof && !(flag & INS_NO_MERGE)) |
1311 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1296 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1312 | if (object::can_merge (op, tmp)) |
1297 | if (object::can_merge (op, tmp)) |
1313 | { |
1298 | { |
1314 | // TODO: we atcually want to update tmp, not op, |
1299 | // TODO: we actually want to update tmp, not op, |
1315 | // but some caller surely breaks when we return tmp |
1300 | // but some caller surely breaks when we return tmp |
1316 | // from here :/ |
1301 | // from here :/ |
1317 | op->nrof += tmp->nrof; |
1302 | op->nrof += tmp->nrof; |
1318 | tmp->destroy (1); |
1303 | tmp->destroy (1); |
1319 | } |
1304 | } |
… | |
… | |
1324 | if (!QUERY_FLAG (op, FLAG_ALIVE)) |
1309 | if (!QUERY_FLAG (op, FLAG_ALIVE)) |
1325 | CLEAR_FLAG (op, FLAG_NO_STEAL); |
1310 | CLEAR_FLAG (op, FLAG_NO_STEAL); |
1326 | |
1311 | |
1327 | if (flag & INS_BELOW_ORIGINATOR) |
1312 | if (flag & INS_BELOW_ORIGINATOR) |
1328 | { |
1313 | { |
1329 | if (originator->map != op->map || originator->x != op->x || originator->y != op->y) |
1314 | if (originator->map != op->map || originator->x != op->x || originator->y != op->y || !originator->is_on_map ()) |
1330 | { |
1315 | { |
1331 | LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); |
1316 | LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); |
1332 | abort (); |
1317 | abort (); |
1333 | } |
1318 | } |
1334 | |
1319 | |
1335 | op->above = originator; |
1320 | op->above = originator; |
1336 | op->below = originator->below; |
1321 | op->below = originator->below; |
1337 | |
|
|
1338 | if (op->below) |
|
|
1339 | op->below->above = op; |
|
|
1340 | else |
|
|
1341 | ms.bot = op; |
|
|
1342 | |
|
|
1343 | /* since *below* originator, no need to update top */ |
|
|
1344 | originator->below = op; |
1322 | originator->below = op; |
|
|
1323 | |
|
|
1324 | *(op->below ? &op->below->above : &ms.bot) = op; |
1345 | } |
1325 | } |
1346 | else |
1326 | else |
1347 | { |
1327 | { |
1348 | object *top, *floor = NULL; |
1328 | object *floor = 0; |
1349 | |
1329 | object *top = ms.top; |
1350 | top = ms.bot; |
|
|
1351 | |
1330 | |
1352 | /* If there are other objects, then */ |
1331 | /* If there are other objects, then */ |
1353 | if (top) |
1332 | if (top) |
1354 | { |
1333 | { |
1355 | object *last = 0; |
|
|
1356 | |
|
|
1357 | /* |
1334 | /* |
1358 | * If there are multiple objects on this space, we do some trickier handling. |
1335 | * If there are multiple objects on this space, we do some trickier handling. |
1359 | * We've already dealt with merging if appropriate. |
1336 | * We've already dealt with merging if appropriate. |
1360 | * Generally, we want to put the new object on top. But if |
1337 | * Generally, we want to put the new object on top. But if |
1361 | * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last |
1338 | * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last |
… | |
… | |
1364 | * once we get to them. This reduces the need to traverse over all of |
1341 | * once we get to them. This reduces the need to traverse over all of |
1365 | * them when adding another one - this saves quite a bit of cpu time |
1342 | * them when adding another one - this saves quite a bit of cpu time |
1366 | * when lots of spells are cast in one area. Currently, it is presumed |
1343 | * when lots of spells are cast in one area. Currently, it is presumed |
1367 | * that flying non pickable objects are spell objects. |
1344 | * that flying non pickable objects are spell objects. |
1368 | */ |
1345 | */ |
1369 | for (top = ms.bot; top; top = top->above) |
1346 | for (object *tmp = ms.bot; tmp; tmp = tmp->above) |
1370 | { |
1347 | { |
1371 | if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) |
1348 | if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) || QUERY_FLAG (tmp, FLAG_OVERLAY_FLOOR)) |
1372 | floor = top; |
1349 | floor = tmp; |
1373 | |
1350 | |
1374 | if (QUERY_FLAG (top, FLAG_NO_PICK) && (top->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (top, FLAG_IS_FLOOR)) |
1351 | if (QUERY_FLAG (tmp, FLAG_NO_PICK) && (tmp->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (tmp, FLAG_IS_FLOOR)) |
1375 | { |
1352 | { |
1376 | /* We insert above top, so we want this object below this */ |
1353 | /* We insert above top, so we want this object below this */ |
1377 | top = top->below; |
1354 | top = tmp->below; |
1378 | break; |
1355 | break; |
1379 | } |
1356 | } |
1380 | |
1357 | |
1381 | last = top; |
1358 | top = tmp; |
1382 | } |
1359 | } |
1383 | |
|
|
1384 | /* Don't want top to be NULL, so set it to the last valid object */ |
|
|
1385 | top = last; |
|
|
1386 | |
1360 | |
1387 | /* We let update_position deal with figuring out what the space |
1361 | /* We let update_position deal with figuring out what the space |
1388 | * looks like instead of lots of conditions here. |
1362 | * looks like instead of lots of conditions here. |
1389 | * makes things faster, and effectively the same result. |
1363 | * makes things faster, and effectively the same result. |
1390 | */ |
1364 | */ |
… | |
… | |
1397 | */ |
1371 | */ |
1398 | if (!(flag & INS_ON_TOP) |
1372 | if (!(flag & INS_ON_TOP) |
1399 | && ms.flags () & P_BLOCKSVIEW |
1373 | && ms.flags () & P_BLOCKSVIEW |
1400 | && (op->face && !faces [op->face].visibility)) |
1374 | && (op->face && !faces [op->face].visibility)) |
1401 | { |
1375 | { |
|
|
1376 | object *last; |
|
|
1377 | |
1402 | for (last = top; last != floor; last = last->below) |
1378 | for (last = top; last != floor; last = last->below) |
1403 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1379 | if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) |
1404 | break; |
1380 | break; |
1405 | |
1381 | |
1406 | /* Check to see if we found the object that blocks view, |
1382 | /* Check to see if we found the object that blocks view, |
… | |
… | |
1417 | top = floor; |
1393 | top = floor; |
1418 | |
1394 | |
1419 | /* Top is the object that our object (op) is going to get inserted above. |
1395 | /* Top is the object that our object (op) is going to get inserted above. |
1420 | */ |
1396 | */ |
1421 | |
1397 | |
1422 | /* First object on this space */ |
1398 | /* no top found, insert on top */ |
1423 | if (!top) |
1399 | if (!top) |
1424 | { |
1400 | { |
1425 | op->above = ms.bot; |
|
|
1426 | |
|
|
1427 | if (op->above) |
|
|
1428 | op->above->below = op; |
|
|
1429 | |
|
|
1430 | op->below = 0; |
1401 | op->above = 0; |
|
|
1402 | op->below = ms.top; |
1431 | ms.bot = op; |
1403 | ms.top = op; |
|
|
1404 | |
|
|
1405 | *(op->below ? &op->below->above : &ms.bot) = op; |
1432 | } |
1406 | } |
1433 | else |
1407 | else |
1434 | { /* get inserted into the stack above top */ |
1408 | { /* get inserted into the stack above top */ |
1435 | op->above = top->above; |
1409 | op->above = top->above; |
1436 | |
|
|
1437 | if (op->above) |
|
|
1438 | op->above->below = op; |
1410 | top->above = op; |
1439 | |
1411 | |
1440 | op->below = top; |
1412 | op->below = top; |
1441 | top->above = op; |
1413 | *(op->above ? &op->above->below : &ms.top) = op; |
1442 | } |
1414 | } |
1443 | |
|
|
1444 | if (!op->above) |
|
|
1445 | ms.top = op; |
|
|
1446 | } /* else not INS_BELOW_ORIGINATOR */ |
1415 | } /* else not INS_BELOW_ORIGINATOR */ |
1447 | |
1416 | |
1448 | if (op->type == PLAYER) |
1417 | if (op->type == PLAYER) |
1449 | { |
1418 | { |
1450 | op->contr->do_los = 1; |
1419 | op->contr->do_los = 1; |
… | |
… | |
1587 | } |
1556 | } |
1588 | else |
1557 | else |
1589 | { |
1558 | { |
1590 | decrease (nr); |
1559 | decrease (nr); |
1591 | |
1560 | |
1592 | object *op = object_create_clone (this); |
1561 | object *op = deep_clone (); |
1593 | op->nrof = nr; |
1562 | op->nrof = nr; |
1594 | return op; |
1563 | return op; |
1595 | } |
1564 | } |
1596 | } |
1565 | } |
1597 | |
1566 | |
… | |
… | |
1818 | LOG (llevError, "Present_arch called outside map.\n"); |
1787 | LOG (llevError, "Present_arch called outside map.\n"); |
1819 | return NULL; |
1788 | return NULL; |
1820 | } |
1789 | } |
1821 | |
1790 | |
1822 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1791 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1823 | if (tmp->arch == at) |
1792 | if (tmp->arch->archname == at->archname) |
1824 | return tmp; |
1793 | return tmp; |
1825 | |
1794 | |
1826 | return NULL; |
1795 | return NULL; |
1827 | } |
1796 | } |
1828 | |
1797 | |
… | |
… | |
1892 | * The first matching object is returned, or NULL if none. |
1861 | * The first matching object is returned, or NULL if none. |
1893 | */ |
1862 | */ |
1894 | object * |
1863 | object * |
1895 | present_arch_in_ob (const archetype *at, const object *op) |
1864 | present_arch_in_ob (const archetype *at, const object *op) |
1896 | { |
1865 | { |
1897 | for (object *tmp = op->inv; tmp != NULL; tmp = tmp->below) |
1866 | for (object *tmp = op->inv; tmp; tmp = tmp->below) |
1898 | if (tmp->arch == at) |
1867 | if (tmp->arch->archname == at->archname) |
1899 | return tmp; |
1868 | return tmp; |
1900 | |
1869 | |
1901 | return NULL; |
1870 | return NULL; |
1902 | } |
1871 | } |
1903 | |
1872 | |
… | |
… | |
2312 | |
2281 | |
2313 | /* |
2282 | /* |
2314 | * create clone from object to another |
2283 | * create clone from object to another |
2315 | */ |
2284 | */ |
2316 | object * |
2285 | object * |
2317 | object_create_clone (object *asrc) |
2286 | object::deep_clone () |
2318 | { |
2287 | { |
|
|
2288 | assert (("deep_clone called on non-head object", is_head ())); |
|
|
2289 | |
2319 | object *dst = 0; |
2290 | object *dst = clone (); |
2320 | |
2291 | |
2321 | if (!asrc) |
|
|
2322 | return 0; |
|
|
2323 | |
|
|
2324 | object *src = asrc->head_ (); |
|
|
2325 | |
|
|
2326 | object *prev = 0; |
2292 | object *prev = dst; |
2327 | for (object *part = src; part; part = part->more) |
2293 | for (object *part = this->more; part; part = part->more) |
2328 | { |
2294 | { |
2329 | object *tmp = part->clone (); |
2295 | object *tmp = part->clone (); |
2330 | |
|
|
2331 | tmp->x -= src->x; |
|
|
2332 | tmp->y -= src->y; |
|
|
2333 | |
|
|
2334 | if (!part->head) |
|
|
2335 | { |
|
|
2336 | dst = tmp; |
|
|
2337 | tmp->head = 0; |
|
|
2338 | } |
|
|
2339 | else |
|
|
2340 | tmp->head = dst; |
2296 | tmp->head = dst; |
2341 | |
|
|
2342 | tmp->more = 0; |
|
|
2343 | |
|
|
2344 | if (prev) |
|
|
2345 | prev->more = tmp; |
2297 | prev->more = tmp; |
2346 | |
|
|
2347 | prev = tmp; |
2298 | prev = tmp; |
2348 | } |
2299 | } |
2349 | |
2300 | |
2350 | for (object *item = src->inv; item; item = item->below) |
2301 | for (object *item = inv; item; item = item->below) |
2351 | insert_ob_in_ob (object_create_clone (item), dst); |
2302 | insert_ob_in_ob (item->deep_clone (), dst); |
2352 | |
2303 | |
2353 | return dst; |
2304 | return dst; |
2354 | } |
2305 | } |
2355 | |
2306 | |
2356 | /* This returns the first object in who's inventory that |
2307 | /* This returns the first object in who's inventory that |