… | |
… | |
24 | #include <global.h> |
24 | #include <global.h> |
25 | #include <stdio.h> |
25 | #include <stdio.h> |
26 | #include <sys/types.h> |
26 | #include <sys/types.h> |
27 | #include <sys/uio.h> |
27 | #include <sys/uio.h> |
28 | #include <object.h> |
28 | #include <object.h> |
29 | #include <funcpoint.h> |
|
|
30 | #include <sproto.h> |
29 | #include <sproto.h> |
31 | #include <loader.h> |
30 | #include <loader.h> |
32 | |
31 | |
33 | #include <bitset> |
32 | #include <bitset> |
34 | |
33 | |
… | |
… | |
331 | |
330 | |
332 | /* Everything passes, must be OK. */ |
331 | /* Everything passes, must be OK. */ |
333 | return 1; |
332 | return 1; |
334 | } |
333 | } |
335 | |
334 | |
|
|
335 | // find player who can see this object |
|
|
336 | object * |
|
|
337 | object::visible_to () const |
|
|
338 | { |
|
|
339 | if (!flag [FLAG_REMOVED]) |
|
|
340 | { |
|
|
341 | // see if we are in a container of sorts |
|
|
342 | if (env) |
|
|
343 | { |
|
|
344 | // the player inventory itself is always visible |
|
|
345 | if (env->type == PLAYER) |
|
|
346 | return env; |
|
|
347 | |
|
|
348 | // else a player could have our env open |
|
|
349 | object *envest = env->outer_env (); |
|
|
350 | |
|
|
351 | // the player itself is always on a map, so we will find him here |
|
|
352 | // even if our inv is in a player. |
|
|
353 | if (envest->is_on_map ()) |
|
|
354 | if (object *pl = envest->ms ().player ()) |
|
|
355 | if (pl->container == env) |
|
|
356 | return pl; |
|
|
357 | } |
|
|
358 | else |
|
|
359 | { |
|
|
360 | // maybe there is a player standing on the same mapspace |
|
|
361 | // this will catch the case where "this" is a player |
|
|
362 | if (object *pl = ms ().player ()) |
|
|
363 | return pl; |
|
|
364 | } |
|
|
365 | } |
|
|
366 | |
|
|
367 | return 0; |
|
|
368 | } |
|
|
369 | |
336 | // adjust weight per container type ("of holding") |
370 | // adjust weight per container type ("of holding") |
337 | static sint32 |
371 | static sint32 |
338 | weight_adjust (object *op, sint32 weight) |
372 | weight_adjust (object *op, sint32 weight) |
339 | { |
373 | { |
340 | return op->type == CONTAINER |
374 | return op->type == CONTAINER |
… | |
… | |
351 | { |
385 | { |
352 | while (op) |
386 | while (op) |
353 | { |
387 | { |
354 | weight = weight_adjust (op, weight); |
388 | weight = weight_adjust (op, weight); |
355 | |
389 | |
|
|
390 | if (!weight) |
|
|
391 | return; |
|
|
392 | |
356 | op->carrying += weight; |
393 | op->carrying += weight; |
|
|
394 | |
|
|
395 | if (object *pl = op->visible_to ()) |
|
|
396 | if (pl != op) // player is handled lazily |
|
|
397 | esrv_update_item (UPD_WEIGHT, pl, op); |
|
|
398 | |
357 | op = op->env; |
399 | op = op->env; |
358 | } |
400 | } |
359 | } |
401 | } |
360 | |
402 | |
361 | /* |
403 | /* |
… | |
… | |
374 | op->update_weight (); |
416 | op->update_weight (); |
375 | |
417 | |
376 | sum += op->total_weight (); |
418 | sum += op->total_weight (); |
377 | } |
419 | } |
378 | |
420 | |
379 | carrying = weight_adjust (this, sum); |
421 | sum = weight_adjust (this, sum); |
|
|
422 | |
|
|
423 | if (sum != carrying) |
|
|
424 | { |
|
|
425 | carrying = sum; |
|
|
426 | |
|
|
427 | if (object *pl = visible_to ()) |
|
|
428 | if (pl != this) // player is handled lazily |
|
|
429 | esrv_update_item (UPD_WEIGHT, pl, this); |
|
|
430 | } |
380 | } |
431 | } |
381 | |
432 | |
382 | /* |
433 | /* |
383 | * Used by: Server DM commands: dumpbelow, dump. Some error messages. |
434 | * Used by: Server DM commands: dumpbelow, dump. Some error messages. |
384 | */ |
435 | */ |
… | |
… | |
916 | } |
967 | } |
917 | |
968 | |
918 | void |
969 | void |
919 | object::do_destroy () |
970 | object::do_destroy () |
920 | { |
971 | { |
921 | attachable::do_destroy (); |
|
|
922 | |
|
|
923 | if (flag [FLAG_IS_LINKED]) |
972 | if (flag [FLAG_IS_LINKED]) |
924 | remove_button_link (this); |
973 | remove_button_link (this); |
925 | |
974 | |
926 | if (flag [FLAG_FRIENDLY]) |
975 | if (flag [FLAG_FRIENDLY]) |
927 | remove_friendly_object (this); |
976 | remove_friendly_object (this); |
928 | |
977 | |
929 | if (!flag [FLAG_REMOVED]) |
|
|
930 | remove (); |
978 | remove (); |
|
|
979 | |
|
|
980 | attachable::do_destroy (); |
931 | |
981 | |
932 | destroy_inv (true); |
982 | destroy_inv (true); |
933 | |
983 | |
934 | deactivate (); |
984 | deactivate (); |
935 | unlink (); |
985 | unlink (); |
… | |
… | |
1003 | object::do_remove () |
1053 | object::do_remove () |
1004 | { |
1054 | { |
1005 | object *tmp, *last = 0; |
1055 | object *tmp, *last = 0; |
1006 | object *otmp; |
1056 | object *otmp; |
1007 | |
1057 | |
1008 | if (QUERY_FLAG (this, FLAG_REMOVED)) |
1058 | if (flag [FLAG_REMOVED]) |
1009 | return; |
1059 | return; |
1010 | |
1060 | |
1011 | SET_FLAG (this, FLAG_REMOVED); |
|
|
1012 | INVOKE_OBJECT (REMOVE, this); |
1061 | INVOKE_OBJECT (REMOVE, this); |
|
|
1062 | |
|
|
1063 | if (object *pl = visible_to ()) |
|
|
1064 | esrv_del_item (pl->contr, count); |
|
|
1065 | |
|
|
1066 | flag [FLAG_REMOVED] = true; |
1013 | |
1067 | |
1014 | if (more) |
1068 | if (more) |
1015 | more->remove (); |
1069 | more->remove (); |
1016 | |
1070 | |
1017 | /* |
1071 | /* |
… | |
… | |
1152 | if (!top) |
1206 | if (!top) |
1153 | for (top = op; top && top->above; top = top->above) |
1207 | for (top = op; top && top->above; top = top->above) |
1154 | ; |
1208 | ; |
1155 | |
1209 | |
1156 | for (; top; top = top->below) |
1210 | for (; top; top = top->below) |
1157 | { |
|
|
1158 | if (top == op) |
|
|
1159 | continue; |
|
|
1160 | |
|
|
1161 | if (object::can_merge (op, top)) |
1211 | if (object::can_merge (op, top)) |
1162 | { |
1212 | { |
1163 | top->nrof += op->nrof; |
1213 | top->nrof += op->nrof; |
1164 | |
1214 | |
1165 | /* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ |
1215 | if (object *pl = top->visible_to ()) |
1166 | op->weight = 0; /* Don't want any adjustements now */ |
1216 | esrv_update_item (UPD_NROF, pl, top); |
|
|
1217 | |
|
|
1218 | op->weight = 0; // cancel the addition above |
|
|
1219 | op->carrying = 0; // must be 0 already |
|
|
1220 | |
1167 | op->destroy (); |
1221 | op->destroy (1); |
|
|
1222 | |
1168 | return top; |
1223 | return top; |
1169 | } |
1224 | } |
1170 | } |
|
|
1171 | |
1225 | |
1172 | return 0; |
1226 | return 0; |
1173 | } |
1227 | } |
1174 | |
1228 | |
1175 | void |
1229 | void |
… | |
… | |
1478 | else |
1532 | else |
1479 | return where->map->insert (this, where->x, where->y, originator, flags); |
1533 | return where->map->insert (this, where->x, where->y, originator, flags); |
1480 | } |
1534 | } |
1481 | |
1535 | |
1482 | /* |
1536 | /* |
1483 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
|
|
1484 | * is returned contains nr objects, and the remaining parts contains |
|
|
1485 | * the rest (or is removed and freed if that number is 0). |
|
|
1486 | * On failure, NULL is returned, and the reason put into the |
|
|
1487 | * global static errmsg array. |
|
|
1488 | */ |
|
|
1489 | object * |
|
|
1490 | object::split_nr (sint32 nr) |
|
|
1491 | { |
|
|
1492 | if (nrof < nr) |
|
|
1493 | return 0; |
|
|
1494 | else if (nrof == nr) |
|
|
1495 | { |
|
|
1496 | remove (); |
|
|
1497 | return this; |
|
|
1498 | } |
|
|
1499 | else |
|
|
1500 | { |
|
|
1501 | decrease_nr (nr); |
|
|
1502 | |
|
|
1503 | object *op = object_create_clone (this); |
|
|
1504 | op->nrof = nr; |
|
|
1505 | return op; |
|
|
1506 | } |
|
|
1507 | } |
|
|
1508 | |
|
|
1509 | //TODO: remove, but semantics differs from split_nr |
|
|
1510 | object * |
|
|
1511 | get_split_ob (object *orig_ob, uint32 nr) |
|
|
1512 | { |
|
|
1513 | if (orig_ob->nrof < nr) |
|
|
1514 | { |
|
|
1515 | sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name); |
|
|
1516 | return 0; |
|
|
1517 | } |
|
|
1518 | |
|
|
1519 | return orig_ob->split_nr (nr); |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | // find player who can see this object |
|
|
1523 | static object * |
|
|
1524 | visible_to (object *op) |
|
|
1525 | { |
|
|
1526 | if (!op->flag [FLAG_REMOVED]) |
|
|
1527 | { |
|
|
1528 | // see if we are in a container of sorts |
|
|
1529 | if (object *env = op->env) |
|
|
1530 | { |
|
|
1531 | // the player inventory itself is always visible |
|
|
1532 | if (env->type == PLAYER) |
|
|
1533 | return env; |
|
|
1534 | |
|
|
1535 | // else a player could have our env open |
|
|
1536 | env = env->outer_env (); |
|
|
1537 | |
|
|
1538 | // the player itself is always on a map, so we will find him here |
|
|
1539 | // even if our inv is in a player. |
|
|
1540 | if (env->is_on_map ()) |
|
|
1541 | if (object *pl = env->ms ().player ()) |
|
|
1542 | if (pl->container == op->env) |
|
|
1543 | return pl; |
|
|
1544 | } |
|
|
1545 | else |
|
|
1546 | { |
|
|
1547 | // maybe there is a player standing on the same mapspace |
|
|
1548 | if (object *pl = env->ms ().player ()) |
|
|
1549 | return pl; |
|
|
1550 | } |
|
|
1551 | } |
|
|
1552 | |
|
|
1553 | return 0; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | /* |
|
|
1557 | * decrease_ob_nr(object, number) decreases a specified number from |
1537 | * decrease(object, number) decreases a specified number from |
1558 | * the amount of an object. If the amount reaches 0, the object |
1538 | * the amount of an object. If the amount reaches 0, the object |
1559 | * is subsequently removed and freed. |
1539 | * is subsequently removed and freed. |
1560 | * |
1540 | * |
1561 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1541 | * Return value: 'op' if something is left, NULL if the amount reached 0 |
1562 | */ |
1542 | */ |
1563 | bool |
1543 | bool |
1564 | object::decrease_nr (sint32 nr) |
1544 | object::decrease (sint32 nr) |
1565 | { |
1545 | { |
|
|
1546 | if (!nr) |
|
|
1547 | return true; |
|
|
1548 | |
1566 | nr = min (nr, nrof); |
1549 | nr = min (nr, nrof); |
1567 | |
1550 | |
1568 | if (!nr) |
|
|
1569 | return 1; |
|
|
1570 | |
|
|
1571 | nrof -= nr; |
1551 | nrof -= nr; |
1572 | |
1552 | |
1573 | object *visible = visible_to (this); |
|
|
1574 | |
|
|
1575 | if (nrof) |
1553 | if (nrof) |
1576 | { |
1554 | { |
1577 | adjust_weight (env, -weight * nr); // carrying == 0 |
1555 | adjust_weight (env, -weight * nr); // carrying == 0 |
1578 | |
1556 | |
1579 | if (visible) |
1557 | if (object *pl = visible_to ()) |
1580 | esrv_send_item (visible, this); |
1558 | esrv_update_item (UPD_NROF, pl, this); |
1581 | |
1559 | |
1582 | return 1; |
1560 | return true; |
1583 | } |
1561 | } |
1584 | else |
1562 | else |
1585 | { |
1563 | { |
1586 | if (visible) |
|
|
1587 | esrv_del_item (visible->contr, count); |
|
|
1588 | |
|
|
1589 | this->destroy (1); |
1564 | destroy (1); |
1590 | return 0; |
1565 | return false; |
1591 | } |
1566 | } |
1592 | } |
1567 | } |
1593 | |
1568 | |
1594 | //TODO: remove |
1569 | /* |
|
|
1570 | * split(ob,nr) splits up ob into two parts. The part which |
|
|
1571 | * is returned contains nr objects, and the remaining parts contains |
|
|
1572 | * the rest (or is removed and returned if that number is 0). |
|
|
1573 | * On failure, NULL is returned. |
|
|
1574 | */ |
1595 | object * |
1575 | object * |
1596 | decrease_ob_nr (object *op, uint32 i) |
1576 | object::split (sint32 nr) |
1597 | { |
1577 | { |
1598 | return op->decrease_nr (i) ? op : 0; |
1578 | int have = number_of (); |
|
|
1579 | |
|
|
1580 | if (have < nr) |
|
|
1581 | return 0; |
|
|
1582 | else if (have == nr) |
|
|
1583 | { |
|
|
1584 | remove (); |
|
|
1585 | return this; |
|
|
1586 | } |
|
|
1587 | else |
|
|
1588 | { |
|
|
1589 | decrease (nr); |
|
|
1590 | |
|
|
1591 | object *op = object_create_clone (this); |
|
|
1592 | op->nrof = nr; |
|
|
1593 | return op; |
|
|
1594 | } |
1599 | } |
1595 | } |
1600 | |
1596 | |
1601 | object * |
1597 | object * |
1602 | insert_ob_in_ob (object *op, object *where) |
1598 | insert_ob_in_ob (object *op, object *where) |
1603 | { |
1599 | { |
… | |
… | |
1644 | if (object::can_merge (tmp, op)) |
1640 | if (object::can_merge (tmp, op)) |
1645 | { |
1641 | { |
1646 | /* return the original object and remove inserted object |
1642 | /* return the original object and remove inserted object |
1647 | (client needs the original object) */ |
1643 | (client needs the original object) */ |
1648 | tmp->nrof += op->nrof; |
1644 | tmp->nrof += op->nrof; |
|
|
1645 | |
|
|
1646 | if (object *pl = tmp->visible_to ()) |
|
|
1647 | esrv_update_item (UPD_NROF, pl, tmp); |
|
|
1648 | |
|
|
1649 | adjust_weight (this, op->total_weight ()); |
|
|
1650 | |
1649 | op->destroy (1); |
1651 | op->destroy (1); |
1650 | op = tmp; |
1652 | op = tmp; |
1651 | goto inserted; |
1653 | goto inserted; |
1652 | } |
1654 | } |
1653 | |
1655 | |
… | |
… | |
1665 | |
1667 | |
1666 | inv = op; |
1668 | inv = op; |
1667 | |
1669 | |
1668 | op->flag [FLAG_REMOVED] = 0; |
1670 | op->flag [FLAG_REMOVED] = 0; |
1669 | |
1671 | |
|
|
1672 | if (object *pl = op->visible_to ()) |
|
|
1673 | esrv_send_item (pl, op); |
|
|
1674 | |
1670 | adjust_weight (this, op->total_weight ()); |
1675 | adjust_weight (this, op->total_weight ()); |
1671 | |
1676 | |
1672 | inserted: |
1677 | inserted: |
1673 | /* reset the light list and los of the players on the map */ |
1678 | /* reset the light list and los of the players on the map */ |
1674 | if (op->glow_radius && map && map->darkness) |
1679 | if (op->glow_radius && map && map->darkness) |
1675 | update_all_los (map, x, y); |
1680 | update_all_los (map, x, y); |
1676 | |
1681 | |
1677 | if (object *otmp = in_player ()) |
1682 | // if this is a player's inventory, update stats |
1678 | if (otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) |
1683 | if (type == PLAYER && !flag [FLAG_NO_FIX_PLAYER]) |
1679 | otmp->update_stats (); |
1684 | update_stats (); |
1680 | |
1685 | |
1681 | INVOKE_OBJECT (INSERT, this); |
1686 | INVOKE_OBJECT (INSERT, this); |
1682 | |
1687 | |
1683 | return op; |
1688 | return op; |
1684 | } |
1689 | } |