1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
|
/*
* objs.cc - Game objects.
*
* Copyright (C) 1998-1999 Jeffrey S. Freedman
* Copyright (C) 2000-2001 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "objs.h"
#include "chunks.h"
#include "objiter.h"
#include "egg.h"
#include "gamewin.h"
#include "gamemap.h"
#include "actors.h"
#include "ucmachine.h"
#include "items.h"
#include "dir.h"
#include "ordinfo.h"
#include "game.h"
#include "Gump_manager.h"
#include "effects.h"
#ifndef ALPHA_LINUX_CXX
# include <cstring>
# include <cstdio>
#endif
#include <algorithm> // STL function things
#ifdef USE_EXULTSTUDIO
#include "cheat.h"
#include "server.h"
#include "objserial.h"
#include "servemsg.h"
#endif
using std::cerr;
using std::cout;
using std::endl;
using std::memcpy;
using std::memset;
using std::rand;
using std::ostream;
using std::snprintf;
using std::strchr;
using std::string;
// Offset to each neighbor, dir=0-7.
short Tile_coord::neighbors[16] = {0,-1, 1,-1, 1,0, 1,1, 0,1,
-1,1, -1,0, -1,-1 };
Game_object *Game_object::editing = 0;
// Bit 5=S, Bit6=reflect. on diag.
unsigned char Game_object::rotate[8] = { 0, 0, 48, 48, 16, 16, 32, 32};
/*
* Get direction to another object.
*/
int Game_object::get_direction
(
Game_object *o2
) const
{
Tile_coord t1 = get_tile();
Tile_coord t2 = o2->get_tile();
// Treat as cartesian coords.
return (int) Get_direction(t1.ty - t2.ty, t2.tx - t1.tx);
}
/*
* Get direction to a given tile.
*/
int Game_object::get_direction
(
Tile_coord t2
) const
{
Tile_coord t1 = get_tile();
// Treat as cartesian coords.
return (int) Get_direction(t1.ty - t2.ty, t2.tx - t1.tx);
}
/*
* Does a given shape come in quantity.
*/
static int Has_quantity
(
int shnum // Shape number.
)
{
Game_window *gwin = Game_window::get_game_window();
Shape_info& info = gwin->get_info(shnum);
return info.has_quantity();
}
static int Has_hitpoints(int shnum)
{
Game_window *gwin = Game_window::get_game_window();
Shape_info& info = gwin->get_info(shnum);
return ((info.get_shape_class() == Shape_info::has_hp) ||
(info.get_shape_class() == Shape_info::container));
// containers have hitpoints too ('resistance')
}
const int MAX_QUANTITY = 100; // Highest quantity possible.
/*
* Get the quantity.
*/
int Game_object::get_quantity
(
) const
{
int shnum = get_shapenum();
if (Has_quantity(shnum))
{
int qual = quality & 0x7f;
return qual ? qual : 1;
}
else
return 1;
}
int Game_object::get_obj_hp() const
{
int shnum = get_shapenum();
if (Has_hitpoints(shnum))
return quality;
else
return 0;
}
void Game_object::set_obj_hp(int hp)
{
int shnum = get_shapenum();
if (Has_hitpoints(shnum))
set_quality(hp);
}
/*
* Get the volume.
*/
int Game_object::get_volume
(
) const
{
int vol = Game_window::get_game_window()->get_info(this).get_volume();
return vol; // I think U7 ignores quantity!
}
/*
* Add or remove from object's 'quantity', and delete if it goes to 0.
* Also, this sets the correct frame, even if delta == 0.
*
* Output: Delta decremented/incremented by # added/removed.
* Container's volume_used field is updated.
*/
int Game_object::modify_quantity
(
int delta, // >=0 to add, <0 to remove.
bool *del // If !null, true ret'd if deleted.
)
{
if (del)
*del = false;
if (!Has_quantity(get_shapenum()))
{ // Can't do quantity here.
if (delta > 0)
return (delta);
remove_this(); // Remove from container (or world).
if (del)
*del = true;
return (delta + 1);
}
int quant = quality&0x7f; // Get current quality.
if (!quant)
quant = 1; // Might not be set.
int newquant = quant + delta;
if (delta >= 0) // Adding?
{ // Too much?
if (newquant > MAX_QUANTITY)
newquant = MAX_QUANTITY;
}
else if (newquant <= 0) // Subtracting.
{
remove_this(); // We're done for.
if (del)
*del = true;
return (delta + quant);
}
int oldvol = get_volume(); // Get old volume used.
quality = (char) newquant; // Store new value.
int shapenum = get_shapenum();
Game_window *gwin = Game_window::get_game_window();
// Set appropriate shape.
int num_frames = gwin->get_shapes().get_num_frames(shapenum);
int new_frame = newquant - 1;
if (new_frame > 7) // Range is 0-7.
new_frame = 7;
if (shapenum == 565) // Starbursts are special.
set_frame(0); // (Fixes messed-up games.)
else if (shapenum != 842) // Leave reagants alone.
// Guessing: Works for ammo, arrows.
set_frame(num_frames == 32 ? 24 + new_frame : new_frame);
Container_game_object *owner = get_owner();
if (owner) // Update owner's volume.
owner->modify_volume_used(get_volume() - oldvol);
return (delta - (newquant - quant));
}
/*
* Move to a new absolute location. This should work even if the old
* location is invalid (cx=cy=255).
*/
void Game_object::move
(
int newtx,
int newty,
int newlift
)
{
Game_window *gwin = Game_window::get_game_window();
Game_map *gmap = gwin->get_map();
// Figure new chunk.
int newcx = newtx/c_tiles_per_chunk, newcy = newty/c_tiles_per_chunk;
Map_chunk *newchunk = gmap->get_chunk_safely(newcx, newcy);
if (!newchunk)
return; // Bad loc.
// Remove from old.
Map_chunk *oldchunk = gmap->get_chunk_safely(cx, cy);
if (oldchunk)
{
gwin->add_dirty(this); // Want to repaint old area.
oldchunk->remove(this);
}
set_lift(newlift); // Set new values.
shape_pos = ((newtx%c_tiles_per_chunk) << 4) + newty%c_tiles_per_chunk;
newchunk->add(this); // Updates cx, cy.
gwin->add_dirty(this); // And repaint new area.
}
/*
* Swap positions with another object (of the same footprint).
*
* Output: 1 if successful, else 0.
*/
int Game_object::swap_positions
(
Game_object *obj2
)
{
Game_window *gwin = Game_window::get_game_window();
Shape_info& inf1 = gwin->get_info(this);
Shape_info& inf2 = gwin->get_info(obj2);
if (inf1.get_3d_xtiles() != inf2.get_3d_xtiles() ||
inf1.get_3d_ytiles() != inf2.get_3d_ytiles())
return 0; // Not the same size.
Tile_coord p1 = get_tile();
Tile_coord p2 = obj2->get_tile();
remove_this(1); // Remove (but don't delete) each.
set_invalid();
obj2->remove_this(1);
obj2->set_invalid();
move(p2.tx, p2.ty, p2.tz); // Move to new locations.
obj2->move(p1.tx, p1.ty, p1.tz);
return (1);
}
/*
* Remove all dependencies.
*/
void Game_object::clear_dependencies
(
)
{
Game_object_vector::const_iterator X;
// First do those we depend on.
for(X = dependencies.begin(); X != dependencies.end(); ++X )
(**X).dependors.remove(this);
dependencies.clear();
// Now those who depend on us.
for(X = dependors.begin(); X != dependors.end(); ++X )
(**X).dependencies.remove(this);
dependors.clear();
}
/*
* Check an object in find_nearby() against the mask.
*
* Output: 1 if it passes.
*/
static int Check_mask
(
Game_window *gwin,
Game_object *obj,
int mask
)
{
Shape_info& info = gwin->get_info(obj);
if ((mask&(4|8)) && // Both seem to be all NPC's.
!info.is_npc())
return 0;
Shape_info::Shape_class sclass = info.get_shape_class();
// Egg/barge?
if ((sclass == Shape_info::hatchable || sclass == Shape_info::barge) &&
!(mask&0x10)) // Only accept if bit 16 set.
return 0;
if (info.is_transparent() && // Transparent?
!(mask&0x80))
return 0;
// Invisible object?
if (obj->get_flag(Obj_flags::invisible))
if (!(mask&20)) // Guess: 0x20 == invisible.
{
if (!(mask&0x40)) // Guess: Inv. party member.
return 0;
if (obj != gwin->get_main_actor() &&
obj->get_party_id() < 0)
return 0;
}
return 1; // Passed all tests.
#if 0 /* +++++Old code. */
{
if (obj->is_monster())
return 1;
// Avatar? FALSE for SI. Unsure
// about BG, so return TRUE.
if (obj == gwin->get_main_actor())
return Game::get_game_type() != SERPENT_ISLE;
if (obj->get_npc_num() <= 0)
return 0; // Not an NPC & not the Avatar.
return 1;
}
if (mask == 16) // Egg or barge or path.
{
if (obj->get_shapenum() == 0x3c1 || obj->get_shapenum() == 607)
return 1;
if (!obj->is_egg())
return 0;
if (Game::get_game_type() != SERPENT_ISLE)
return 1;
// SI-SS wine cask problem:
Egg_object *egg = (Egg_object *)obj;
return egg->get_type() != (int) Egg_object::teleport;
}
if (mask == 32) // ??? Used in 'reveal' to find inv.
// objs, and to detect blocked gplank.
return 1; // For now.+++++
if (!mask) // Guessing a bit here.
return !obj->is_egg(); // Don't pass eggs if 0.
return 1;
#endif
}
/*
* Find objects near a given position.
*
* Output: # found, appended to vec.
*/
#ifndef MSVC_FIND_NEARBY_KLUDGE
template <class T>
#ifdef ALPHA_LINUX_CXX
int Game_object::find_nearby_static
#else
int Game_object::find_nearby
#endif
(
Exult_vector<T*>& vec, // Objects appended to this.
Tile_coord pos, // Look near this point.
int shapenum, // Shape to look for.
// -1=any (but always use mask?),
// c_any_shapenum=any.
int delta, // # tiles to look in each direction.
int mask, // See Check_mask() above.
int qual, // Quality, or c_any_qual for any.
int framenum // Frame #, or c_any_framenum for any.
)
{
if (delta < 0) // +++++Until we check all old callers.
delta = 24;
if (shapenum > 0 && mask == 4) // Ignore mask=4 if shape given!
mask = 0;
int vecsize = vec.size();
Game_window *gwin = Game_window::get_game_window();
Game_map *gmap = gwin->get_map();
Rectangle tiles(pos.tx - delta, pos.ty - delta, 1 + 2*delta, 1 +
2*delta);
// Stay within world.
Rectangle world(0, 0, c_num_chunks*c_tiles_per_chunk,
c_num_chunks*c_tiles_per_chunk);
tiles = tiles.intersect(world);
// Figure range of chunks.
int start_cx = tiles.x/c_tiles_per_chunk,
end_cx = (tiles.x + tiles.w - 1)/c_tiles_per_chunk;
int start_cy = tiles.y/c_tiles_per_chunk,
end_cy = (tiles.y + tiles.h - 1)/c_tiles_per_chunk;
// Go through all covered chunks.
for (int cy = start_cy; cy <= end_cy; cy++)
for (int cx = start_cx; cx <= end_cx; cx++)
{ // Go through objects.
Map_chunk *chunk = gmap->get_chunk(cx, cy);
Object_iterator next(chunk->get_objects());
Game_object *obj;
while ((obj = next.get_next()) != 0)
{ // Check shape.
if (shapenum >= 0)
{
if (obj->get_shapenum() != shapenum)
continue;
}
if (qual != c_any_qual && obj->get_quality()
!= qual)
continue;
if (framenum != c_any_framenum &&
obj->get_framenum() != framenum)
continue;
if (!Check_mask(gwin, obj, mask))
continue;
Tile_coord t = obj->get_tile();
if (tiles.has_point(t.tx, t.ty)) {
T* castobj = dynamic_cast<T*>(obj);
if (castobj)
vec.push_back(castobj);
}
}
}
// Return # added.
return (vec.size() - vecsize);
}
#ifdef ALPHA_LINUX_CXX
#define DEFINE_FIND_NEARBY(decl_type, decl_conttype) \
int Game_object::find_nearby(decl_type vec, Tile_coord pos, int shapenum, int delta, int mask, int qual, int framenum) \
{ \
return find_nearby_static(vec, pos, shapenum, delta, mask, qual, framenum); \
}
DEFINE_FIND_NEARBY(Egg_vector&);
DEFINE_FIND_NEARBY(Actor_vector&);
DEFINE_FIND_NEARBY(Game_object_vector&);
#endif //ALPHA_LINUX_CXX
#else //MSVC_FIND_NEARBY_KLUDGE
#define FN_VECTOR Egg_vector
#define FN_OBJECT Egg_object
#include "find_nearby.h"
#define FN_VECTOR Game_object_vector
#define FN_OBJECT Game_object
#include "find_nearby.h"
#define FN_VECTOR Actor_vector
#define FN_OBJECT Actor
#include "find_nearby.h"
#endif //MSVC_FIND_NEARBY_KLUDGE
int Game_object::find_nearby_eggs
(
Egg_vector& vec,
int shapenum,
int delta,
int qual,
int frnum
) const
{
return Game_object::find_nearby (vec, get_tile(), shapenum,
delta, 16, qual, frnum);
}
int Game_object::find_nearby_actors
(
Actor_vector& vec,
int shapenum,
int delta
) const
{
return Game_object::find_nearby(vec, get_tile(), shapenum,
delta, 8, c_any_qual, c_any_framenum);
}
int Game_object::find_nearby
(
Game_object_vector& vec,
int shapenum,
int delta,
int mask,
int qual,
int framenum
) const
{
return Game_object::find_nearby(vec, get_tile(), shapenum,
delta, mask, qual, framenum);
}
/*
* For sorting closest to a given spot.
*/
class Object_closest_sorter
{
Tile_coord pos; // Pos. to get closest to.
public:
Object_closest_sorter(Tile_coord p) : pos(p)
{ }
bool operator()(const Game_object *o1, const Game_object *o2)
{
Tile_coord t1 = o1->get_tile(),
t2 = o2->get_tile();
return t1.distance(pos) < t2.distance(pos);
}
};
/*
* Find the closest nearby objects with a shape in a given list.
*
* Output: ->closest object, or 0 if none found.
*/
Game_object *Game_object::find_closest
(
Game_object_vector& vec, // List returned here, closest 1st.
int *shapenums, // Shapes to look for.
// c_any_shapenum=any NPC.
int num_shapes, // Size of shapenums.
int dist // Distance to look (tiles).
)
{
int i;
for (i = 0; i < num_shapes; i++)
// 0xb0 mask finds anything.
find_nearby(vec, shapenums[i], dist, 0xb0);
int cnt = vec.size();
if (!cnt)
return (0);
if (cnt > 1)
std::sort(vec.begin(), vec.end(),
Object_closest_sorter(get_tile()));
return *(vec.begin());
}
/*
* Find the closest nearby object with a shape in a given list.
*
* Output: ->object, or 0 if none found.
*/
Game_object *Game_object::find_closest
(
int *shapenums, // Shapes to look for.
// c_any_shapenum=any NPC.
int num_shapes, // Size of shapenums.
int dist // Distance to look (tiles).
)
{
Game_object_vector vec; // Gets objects found.
int i;
for (i = 0; i < num_shapes; i++)
// 0xb0 mask finds anything.
find_nearby(vec, shapenums[i], dist, 0xb0);
int cnt = vec.size();
if (!cnt)
return (0);
Game_object *closest = 0; // Get closest.
int best_dist = 10000; // In tiles.
// Get our location.
Tile_coord loc = get_tile();
for (Game_object_vector::const_iterator it = vec.begin();
it != vec.end(); ++it)
{
Game_object *obj = *it;
int dist = obj->get_tile().distance(loc);
if (dist < best_dist)
{
closest = obj;
best_dist = dist;
}
}
return (closest);
}
#if 0 /* +++Going away. */
/*
* Find a free tile within given distance.
*
* Output: Tile, or (-1, -1, -1) if failed.
*/
Tile_coord Game_object::find_unblocked_tile
(
Tile_coord pos, // Position to look from.
int dist, // 1 means adjacent.
int height, // Height to check for unblocked.
const int move_flags // What sort of motion is allowed.
)
{
// Get box to go through.
Rectangle box(pos.tx - dist, pos.ty - dist, 2*dist + 1, 2*dist + 1);
Rectangle world(0, 0, c_num_tiles, c_num_tiles);
box = box.intersect(world);
int stopx = box.x + box.w, stopy = box.y + box.h;
for (int y = box.y; y < stopy; y++)
for (int x = box.x; x < stopx; x++)
{ // Check this spot.
Tile_coord spot(x, y, pos.tz);
if (!Map_chunk::is_blocked(spot, height,
move_flags))
return spot;
}
return Tile_coord(-1, -1, -1);
}
#endif
/*
* Get footprint in absolute tiles.
*/
Rectangle Game_object::get_footprint
(
)
{
Game_window *gwin = Game_window::get_game_window();
Shape_info& info = gwin->get_info(this);
// Get footprint.
int frame = get_framenum();
int xtiles = info.get_3d_xtiles(frame);
int ytiles = info.get_3d_ytiles(frame);
Tile_coord t = get_tile();
Rectangle foot((t.tx - xtiles + 1 + c_num_tiles)%c_num_tiles,
(t.ty - ytiles + 1 + c_num_tiles)%c_num_tiles,
xtiles, ytiles);
return foot;
}
/*
* Find the game object that's blocking a given tile.
*
* Output: ->object, or 0 if not found.
*/
Game_object *Game_object::find_blocking
(
Tile_coord tile // Tile to check.
)
{
Game_window *gwin = Game_window::get_game_window();
Map_chunk *chunk = gwin->get_chunk(tile.tx/c_tiles_per_chunk,
tile.ty/c_tiles_per_chunk);
Game_object *obj;
Object_iterator next(chunk->get_objects());
while ((obj = next.get_next()) != 0)
{
// Get object's coords.
Tile_coord t = obj->get_tile();
if (t.tx < tile.tx || t.ty < tile.ty || t.tz > tile.tz)
continue; // Out of range.
Shape_info& info = gwin->get_info(obj);
int ztiles = info.get_3d_height();
if (!ztiles || !info.is_solid())
continue; // Skip if not an obstacle.
// Occupies desired tile?
int frame = obj->get_framenum();
if (tile.tx > t.tx - info.get_3d_xtiles(frame) &&
tile.ty > t.ty - info.get_3d_ytiles(frame) &&
tile.tz < t.tz + ztiles)
return (obj); // Found it.
}
return (0);
}
/*
* Is this a closed door?
*/
int Game_object::is_closed_door
(
) const
{
Game_window *gwin = Game_window::get_game_window();
Shape_info& info = gwin->get_info(this);
if (!info.is_door())
return 0;
// Get door's footprint.
int xtiles = info.get_3d_xtiles(), ytiles = info.get_3d_ytiles();
// Get its location.
Tile_coord doortile = get_tile();
Tile_coord before, after; // Want tiles to both sides.
if (xtiles > ytiles) // Horizontal footprint?
{
before = doortile + Tile_coord(-xtiles, 0, 0);
after = doortile + Tile_coord(1, 0, 0);
}
else // Vertical footprint.
{
before = doortile + Tile_coord(0, -ytiles, 0);
after = doortile + Tile_coord(0, 1, 0);
}
// Should be blocked before/after.
return (Map_chunk::is_blocked(before) &&
Map_chunk::is_blocked(after));
}
/*
* Get the topmost owner of this object.
*
* Output: ->topmost owner, or the object itself.
*/
Game_object *Game_object::get_outermost
(
)
{
Game_object *top = this;
Game_object *above;
while ((above = top->get_owner()) != 0)
top = above;
return top;
}
/*
* Show text by the object on the screen.
*/
void Game_object::say
(
const char *text
)
{
Game_window *gwin = Game_window::get_game_window();
gwin->add_text(text, this);
}
/*
* Show a random string from 'text.flx' by the object.
*/
void Game_object::say
(
int from, int to // Range (inclusive).
)
{
if (from > to) return;
int offset = rand()%(to - from + 1);
if (from + offset < num_item_names)
say(item_names[from + offset]);
}
/*
* Paint at given spot in world.
*/
void Game_object::paint
(
Game_window *gwin
)
{
int x, y;
gwin->get_shape_location(this, x, y);
gwin->paint_shape(x, y, *this);
}
/*
* Run usecode when double-clicked.
*/
void Game_object::activate
(
Usecode_machine *umachine,
int event
)
{
if (edit())
return; // Map-editing.
int usefun = get_shapenum();
// Serpent Isle spell scrolls:
if (usefun == 0x2cb && Game::get_game_type() == SERPENT_ISLE)
{
Game_window *gwin = Game_window::get_game_window();
gwin->get_gump_man()->add_gump(this, 65);
return;
}
// !!!Special case: books
if (usefun == 0x282 && get_quality() >= 100 && get_quality() < 180)
usefun = 0x638;
else if (usefun == 0x282 && get_quality() >= 180 &&
Game::get_game_type() == SERPENT_ISLE )
usefun = 0x63b;
else if (usefun == 0x2c1 && get_quality() >= 213 &&
Game::get_game_type() == SERPENT_ISLE )
usefun = 0x62a;
umachine->call_usecode(usefun, this,
(Usecode_machine::Usecode_events) event);
}
/*
* Edit in ExultStudio.
*/
bool Game_object::edit
(
)
{
#ifdef USE_EXULTSTUDIO
if (client_socket >= 0 && // Talking to ExultStudio?
cheat.in_map_editor())
{
editing = 0;
Tile_coord t = get_tile();
unsigned long addr = (unsigned long) this;
std::string name = get_name();
if (Object_out(client_socket, addr, t.tx, t.ty, t.tz,
get_shapenum(), get_framenum(), get_quality(),
name) != -1)
{
cout << "Sent object data to ExultStudio" << endl;
editing = this;
}
else
cout << "Error sending object to ExultStudio" <<endl;
return true;
}
#endif
return false;
}
/*
* Message to update from ExultStudio.
*/
void Game_object::update_from_studio
(
unsigned char *data,
int datalen
)
{
#ifdef USE_EXULTSTUDIO
unsigned long addr;
int tx, ty, tz;
int shape, frame, quality;
std::string name;
if (!Object_in(data, datalen, addr, tx, ty, tz, shape, frame,
quality, name))
{
cout << "Error decoding object" << endl;
return;
}
Game_object *obj = (Game_object *) addr;
if (!editing || obj != editing)
{
cout << "Obj from ExultStudio is not being edited" << endl;
return;
}
editing = 0;
Game_window *gwin = Game_window::get_game_window();
gwin->add_dirty(obj);
obj->set_shape(shape, frame);
gwin->add_dirty(obj);
obj->set_quality(quality);
// See if it moved.
Tile_coord oldt = obj->get_tile();
if (oldt.tx != tx || oldt.ty != ty || oldt.tz != tz)
obj->move(tx, ty, tz);
cout << "Object updated" << endl;
#endif
}
/*
* For objects that can have a quantity, the name is in the format:
* %1/%2/%3/%4
* Where
* %1 : singular prefix (e.g. "a")
* %2 : main part of name
* %3 : singular suffix
* %4 : plural suffix (e.g. "s")
*/
/*
* Extracts the first, second and third parts of the name string
*/
static void get_singular_name
(
const char *name, // Raw name string from TEXT.FLX
string& output_name // Output string
)
{
if(*name != '/') // Output the first part
{
do
output_name += *name++;
while(*name != '/' && *name != '\0');
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
// If there is a first part it is followed by a space
output_name += ' ';
}
name++;
// Output the second part
while(*name != '/' && *name != '\0')
output_name += *name++;
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
name++;
// Output the third part
while(*name != '/' && *name != '\0')
output_name += *name++;
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
name++;
}
/*
* Extracts the second and fourth parts of the name string
*/
static void get_plural_name
(
const char *name,
int quantity,
string& output_name
)
{
char buf[20];
snprintf(buf, 20, "%d ", quantity); // Output the quantity
output_name = buf;
// Skip the first part
while(*name != '/' && *name != '\0')
name++;
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
name++;
// Output the second part
while(*name != '/' && *name != '\0')
output_name += *name++;
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
name++;
// Skip the third part
while(*name != '/' && *name != '\0')
name++;
if(*name == '\0') // should not happen
{
output_name = "?";
return;
}
name++;
while(*name != '\0') // Output the last part
output_name += *name++;
}
/*
* Returns the string to be displayed when the item is clicked on
*/
string Game_object::get_name
(
) const
{
const char *name = 0;
int quantity;
string display_name;
int shnum = get_shapenum();
int frnum = get_framenum();
int qual = get_quality();
if (Game::get_game_type() == BLACK_GATE) {
//TODO: yourself
switch (shnum) // Some special cases!
{
case 0x34a: // Reagents
name = item_names[0x500 + frnum];
break;
case 0x3bb: // Amulets
if (frnum < 3)
name = item_names[0x508 + frnum];
else
name = item_names[shnum];
break;
case 0x179: // Food items
name = item_names[0x50b + frnum];
break;
case 0x28a: // Sextants
name = item_names[0x52c - frnum];
break;
case 0x2a3: // Desk items
name = item_names[0x52d + frnum];
break;
default:
name = item_names[shnum];
break;
}
} else if (Game::get_game_type() == SERPENT_ISLE) {
//TODO: yourself, oilskin, broken glass, throne
//TODO: Dave, >= text.flx nr. 1540
switch (shnum) // More special cases!
{
case 0x34a: // Reagents
name = item_names[0x500 + frnum];
break;
case 0x32a: // Bucket
if (frnum == 1 && qual >= 10 && qual <= 15)
name = item_names[0x55b + qual];
else if (frnum == 2 && qual == 9)
name = item_names[0x55b + qual];
else
name = item_names[0x55b + frnum];
break;
case 0x179: // Food items
name = item_names[0x510 + frnum];
break;
case 0x2a3: // Desk items
name = item_names[0x532 + frnum];
break;
case 0x28a: // Sextants
name = item_names[0x531 - frnum];
break;
case 0x1bd: // Soul prisms
name = item_names[0x56b + frnum];
break;
case 0x289: // Artifacts
name = item_names[0x573 + frnum];
break;
case 0x1d3: // Magic plants
name = item_names[0x581 + frnum/2];
// not sure about 'catnip'
break;
case 0x1c2: // Orbs
name = item_names[0x585 + frnum];
break;
case 0x106: // Blackrock Serpents
if (frnum < 4)
name = item_names[0x592 + frnum];
else
name = item_names[shnum];
break;
case 0x0b2: // Cloth maps
name = item_names[0x596 + frnum];
break;
case 0x24b: // Boots
name = item_names[0x59c + frnum];
break;
case 0x21e: // Crested Helmets
name = item_names[0x5a3 + frnum];
break;
case 0x3ec: // Helmets
name = item_names[0x5a6 + frnum];
break;
case 0x241: // Nests
if (frnum < 6)
name = item_names[0x5ab + frnum/3];
else
name = item_names[shnum];
break;
case 0x3bb: // Amulets
name = item_names[0x5ae + frnum];
break;
case 0x128: // Rings
name = item_names[0x5b8 + frnum];
break;
case 0x377: // More rings
name = item_names[0x5bc + frnum];
break;
case 0x2b4: // Lute
if (frnum == 2)
name = item_names[0x5be];
else
name = item_names[shnum];
break;
case 0x347: // Hourglass
if (frnum == 1)
name = item_names[0x5bf];
else
name = item_names[shnum];
break;
case 0x0d1: // Artifacts
name = item_names[0x5c0 + frnum];
break;
case 0x0f4: // Large skulls
if (frnum < 2)
name = item_names[0x5d8 + frnum];
else
name = item_names[shnum];
break;
case 0x3f3: // Beds
if (frnum >= 1 && frnum <= 4)
name = item_names[0x5da + frnum - 1];
else if (frnum == 5)
name = item_names[0x5dd];
else if (frnum == 6)
name = item_names[0x5da];
else
name = item_names[shnum];
break;
case 0x390: // Acid
if (frnum == 24)
name = item_names[0x5de];
else
name = item_names[shnum];
break;
case 0x31F: // Body parts
name = item_names[0x5df + frnum];
break;
case 0x258: // Bottles
switch (frnum) {
case 1:
name = item_names[0x5f2]; // wine decanter
break;
case 9:
name = item_names[0x5f3]; // fawnish ale
break;
case 16:
name = item_names[0x5f4]; // ice wine
break;
case 17:
name = item_names[0x5f5]; // vintage wine
break;
case 18:
name = item_names[0x5f6]; // wineskin
break;
case 20:
name = item_names[0x5f7]; // everlasting goblet
break;
default:
name = item_names[shnum];
}
break;
case 0x1df: // claw + gwani amulet
name = item_names[0x5f8 + frnum/2];
break;
case 0x11d: // brush
if (frnum == 6)
name = item_names[0x5fa];
else
name = item_names[shnum];
break;
#if 0
// probably wrong
case 0x222: // broken dish
switch(frnum) {
case 4: case 6: case 7: case 8: case 9:
case 11: case 12: case 14: case 15:
name = item_names[0x5fb]; // broken glass
default:
name = item_names[shnum];
}
break;
#endif
#if 0
// not sure
case 0x124: // seat
if (frnum == 17 || frnum == 18 || frnum == 21 || frnum == 22)
name = item_names[0x5fc];
else
name = item_names[shnum];
#endif
case 0x320: // chest
if (frnum == 4 || frnum == 5)
name = item_names[0x5fd];
else if (frnum == 6 || frnum == 7)
name = item_names[0x5fe];
else
name = item_names[shnum];
break;
case 0x0e3: // cloak
if (frnum <= 4)
name = item_names[0x5ff + frnum];
else
name = item_names[shnum];
break;
default:
name = item_names[shnum];
break;
}
} else {
name = item_names[shnum];
}
if(name == 0)
return "?";
if (Has_quantity(shnum))
quantity = quality & 0x7f;
else
quantity = 1;
// If there are no slashes then it is simpler
if(strchr(name, '/') == 0)
{
if(quantity <= 1)
display_name = name;
else
{
char buf[50];
snprintf(buf, 50, "%d %s", quantity, name);
display_name = buf;
}
}
else if(quantity <= 1) // quantity might be zero?
get_singular_name(name, display_name);
else
get_plural_name(name, quantity, display_name);
return display_name;
}
/*
* Remove an object from the world.
* The object is deleted.
*/
void Game_object::remove_this
(
int nodel // 1 to not delete.
)
{
Map_chunk *chunk =
Game_window::get_game_window()->get_chunk_safely(
cx, cy);
if (chunk)
chunk->remove(this);
if (!nodel)
Game_window::get_game_window()->delete_object(this);
}
/*
* Can this be dragged?
*/
int Game_object::is_dragable
(
) const
{
return (0); // Default is 'no'.
}
/*
* Static method to get shape's weight in 1/10 stones. 0 means infinite.
*/
int Game_object::get_weight
(
int shnum, // Shape #,
int quant // Quantity.
)
{
int wt = quant *
Game_window::get_game_window()->get_info(shnum).get_weight();
// Special case: reagents, coins.
if (shnum == 842 || shnum == 644 ||
(Game::get_game_type() == SERPENT_ISLE &&
// Monetari/guilders/filari:
(shnum == 951 || shnum == 952 || shnum == 948)))
wt /= 10;
return wt > 0 ? wt : 1;
}
/*
* Get weight of object in 1/10 stones.
*/
int Game_object::get_weight
(
)
{
return get_weight(get_shapenum(), get_quantity());
}
/*
* Get maximum weight in stones that can be held.
*
* Output: Max. allowed, or 0 if no limit (i.e., not carried by an NPC).
*/
int Game_object::get_max_weight
(
)
{
// Looking outwards for NPC.
Container_game_object *own = get_owner();
return own ? own->get_max_weight() : 0;
}
/*
* Drop another onto this.
*
* Output: 0 to reject, 1 to accept.
*/
int Game_object::drop
(
Game_object *obj // This may be deleted.
)
{
int shapenum = get_shapenum(); // It's possible if shapes match.
if (obj->get_shapenum() != shapenum ||
!Has_quantity(shapenum) ||
// Reagents are a special case.
(shapenum == 842 && get_framenum() != obj->get_framenum()))
return (0);
int objq = obj->get_quantity();
int total_quant = get_quantity() + objq;
if (total_quant > MAX_QUANTITY) // Too much?
return (0);
modify_quantity(objq); // Add to our quantity.
obj->remove_this(); // It's been used up.
return (1);
}
//#define DEBUGLT
#ifdef DEBUGLT
static int rx1 = -1, ry1 = -1, rx2 = -1, ry2 = -1;
static void Debug_lt
(
int tx1, int ty1, // 1st coord.
int tx2, int ty2 // 2nd coord.
)
{
if (tx1 == rx1 && ty1 == ry1)
{
if (tx2 == rx2 && ty2 == ry2)
cout << "Debug_lt" << endl;
}
}
#endif
/*
* Should obj1 be rendered before obj2?
*
* +++++++++This should go away, replaced by compare().
*
* Output: 1 if so, 0 if not, -1 if cannot compare.
*/
int Game_object::lt
(
Ordering_info& inf1, // Info. for object 1.
Game_object *obj2
)
{
Game_window *gwin = Game_window::get_game_window();
// See if there's no overlap.
Rectangle r2 = gwin->get_shape_rect(obj2);
if (!inf1.area.intersects(r2))
return (-1); // No overlap on screen.
Ordering_info inf2(Game_window::get_game_window(), obj2, r2);
#ifdef DEBUGLT
Debug_lt(inf1.tx, inf1.ty, inf2.tx, inf2.ty);
#endif
int result = -1; // Watch for conflicts.
if (inf1.tz != inf2.tz) // Is one obj. on top of another?
{
if (inf1.tz + inf1.zs <= inf2.tz)
{ // It's above us.
if (inf1.zs >= 4) // Like roof/statue?
return 1;
result = 1;
}
else if (inf2.tz + inf2.zs <= inf1.tz)
{ // We're above.
if (inf2.zs >= 4) // Like roof/statue?
return 0;
result = 0;
}
}
if (inf1.ty != inf2.ty) // Is one obj. in front of the other?
{
if (inf1.ty <= inf2.ty - inf2.ys)
{ // Obj2 is in front.
if (result == 0)// Conflict, so return 'neither'.
return -1;
result = 1;
}
else if (inf2.ty <= inf1.ty - inf1.ys)
{ // We're in front.
if (result == 1)
return -1;
result = 0;
}
}
if (inf1.tx != inf2.tx) // Is one obj. to right of the other?
{
if (inf1.tx <= inf2.tx - inf2.xs)
{ // Obj2 is to right of us.
if (result == 0)
return -1;
result = 1;
}
if (inf2.tx <= inf1.tx - inf1.xs)
{ // We're to the right.
if (result == 1)
return -1;
result = 0;
}
}
if (result != -1) // Consistent result?
return result;
if (!inf1.zs && inf1.tz <= inf2.tz) // We're flat and below?
return (1);
if (!inf2.zs && inf2.tz <= inf1.tz) // It's below us?
return (0);
// Handle intersecting objects.
if (inf1.tx == inf2.tx && // Watch for paintings on NS walls.
// inf1.xs == inf2.xs)
inf1.xs >= inf2.xs) // Also pillar/button in SI.
if (inf1.ys < inf2.ys) // Take narrower 2nd.
return (0);
else if (inf2.ys > inf1.ys)
return (1);
else if (inf1.zs < inf2.zs)// The shorter one?
return (0);
else if (inf1.zs > inf2.zs)
return (1);
// Item on table?
if (inf1.zs > 1 && inf2.tz == inf1.tz + inf1.zs - 1 &&
inf2.tx - inf2.xs >= inf1.tx - inf1.xs && inf2.tx <= inf1.tx &&
inf2.ty - inf2.ys >= inf1.ty - inf1.ys && inf2.ty <= inf1.ty)
return 1;
if (inf2.zs > 1 && inf1.tz == inf2.tz + inf2.zs - 1 &&
inf1.tx - inf1.xs >= inf2.tx - inf2.xs && inf1.tx <= inf2.tx &&
inf1.ty - inf1.ys >= inf2.ty - inf2.ys && inf1.ty <= inf2.ty)
return 0;
// If x's overlap, see if in front.
if ((inf1.tx > inf2.tx - inf2.xs && inf1.tx <= inf2.tx) ||
(inf2.tx > inf1.tx - inf1.xs && inf2.tx <= inf1.tx))
{
if (inf1.ty < inf2.ty)
return (1);
else if (inf1.ty > inf2.ty)
return (0);
else if (inf1.zs < inf2.zs)// The shorter one?
return (0);
else if (inf1.zs > inf2.zs)
return (1);
else if (inf1.xs < inf2.xs)// Take the narrower one as greater.
return (0);
else if (inf1.xs > inf2.xs)
return (1);
}
// If y's overlap, see if to left.
if ((inf1.ty > inf2.ty - inf2.ys && inf1.ty <= inf2.ty) ||
(inf2.ty > inf1.ty - inf1.ys && inf2.ty <= inf1.ty))
{
if (inf1.tx < inf2.tx)
return (1);
else if (inf1.tx > inf2.tx)
return (0);
}
return (-1);
}
/*
* Compare ranges along a given dimension.
*/
inline void Compare_ranges
(
int from1, int to1, // First object's range.
int from2, int to2, // Second object's range.
// Returns:
int& cmp, // -1 if 1st < 2nd, 1 if 1st > 2nd,
// 0 if equal.
bool& overlap // true returned if they overlap.
)
{
if (to1 < from2)
{
overlap = false;
cmp = -1;
}
else if (to2 < from1)
{
overlap = false;
cmp = 1;
}
else // X's overlap.
{
overlap = true;
if (from1 < from2)
cmp = -1;
else if (from1 > from2)
cmp = 1;
else if (to1 - from1 < to2 - from2)
cmp = 1;
else if (to1 - from1 > to2 - from2)
cmp = -1;
else
cmp = 0;
}
}
/*
* Compare two objects.
*
* Output: -1 if 1st < 2nd, 0 if dont_care, 1 if 1st > 2nd.
*/
int Game_object::compare
(
Ordering_info& inf1, // Info. for object 1.
Game_object *obj2
)
{
Game_window *gwin = Game_window::get_game_window();
// See if there's no overlap.
Rectangle r2 = gwin->get_shape_rect(obj2);
if (!inf1.area.intersects(r2))
return (0); // No overlap on screen.
Ordering_info inf2(Game_window::get_game_window(), obj2, r2);
#ifdef DEBUGLT
Debug_lt(inf1.tx, inf1.ty, inf2.tx, inf2.ty);
#endif
int xcmp, ycmp, zcmp; // Comparisons for a given dimension:
// -1 if o1<o2, 0 if o1==o2,
// 1 if o1>o2.
bool xover, yover, zover; // True if dim's overlap.
Compare_ranges(inf1.xleft, inf1.xright, inf2.xleft, inf2.xright,
xcmp, xover);
Compare_ranges(inf1.yfar, inf1.ynear, inf2.yfar, inf2.ynear,
ycmp, yover);
Compare_ranges(inf1.zbot, inf1.ztop, inf2.zbot, inf2.ztop,
zcmp, zover);
if (!xcmp && !ycmp && !zcmp)
// Same space?
// Paint biggest area sec. (Fixes
// plaque at Penumbra's.)
return (inf1.area.w < inf2.area.w &&
inf1.area.h < inf2.area.h) ? -1 :
(inf1.area.w > inf2.area.w &&
inf1.area.h > inf2.area.h) ? 1 : 0;
// return 0; // Equal.
if (xover & yover & zover) // Complete overlap?
if (!inf1.zs) // Flat one is always drawn first.
return !inf2.zs ? 0 : -1;
else if (!inf2.zs)
return 1;
if (xcmp >= 0 && ycmp >= 0 && zcmp >= 0)
return 1; // GTE in all dimensions.
if (xcmp <= 0 && ycmp <= 0 && zcmp <= 0)
return -1; // LTE in all dimensions.
if (yover) // Y's overlap.
{
if (xover) // X's too?
return zcmp;
else if (zover) // Y's and Z's?
return xcmp;
// Just Y's overlap.
else if (!zcmp) // Z's equal?
return xcmp;
else // See if X and Z dirs. agree.
if (xcmp == zcmp)
return xcmp;
#if 1 /* Woohoo! Seems to work without messing up N. Trinsic gate. */
// Experiment: Fixes Trinsic mayor
// statue-through-roof.
else if (inf1.ztop/5 < inf2.zbot/5 && inf2.info.occludes())
return -1; // A floor above/below.
else if (inf2.ztop/5 < inf1.zbot/5 && inf1.info.occludes())
return 1;
#endif
else
return 0;
}
else if (xover) // X's overlap.
{
if (zover) // X's and Z's?
return ycmp;
else if (!zcmp) // Z's equal?
return ycmp;
else
return ycmp == zcmp ? ycmp : 0;
}
// Neither X nor Y overlap.
else if (xcmp == -1) // o1 X before o2 X?
{
if (ycmp == -1) // o1 Y before o2 Y?
// If Z agrees or overlaps, it's LT.
return (zover || zcmp <= 0) ? -1 : 0;
}
else if (ycmp == 1) // o1 Y after o2 Y?
if (zover || zcmp >= 0)
return 1;
#if 1 /* So far, this seems to work without causing problems: */
// Experiment: Fixes Brit. museum
// statue-through-roof.
else if (inf1.ztop/5 < inf2.zbot/5)
return -1; // A floor above.
else
#endif
return 0;
return 0;
}
/*
* Should this object be rendered before obj2?
* NOTE: This older interface isn't as efficient.
*
* Output: 1 if so, 0 if not, -1 if cannot compare.
*/
int Game_object::lt
(
Game_object& obj2
)
{
Game_window *gwin = Game_window::get_game_window();
Ordering_info ord(gwin, this);
#define TESTNEWCMP
#ifdef TESTNEWCMP /* Just #define it to use the new one. */
int cmp = compare(ord, &obj2);
return cmp == -1 ? 1 : cmp == 1 ? 0 : -1;
#else
return lt(ord, &obj2);
#endif
}
/*
* Get frame if rotated 1, 2, or 3 quadrants clockwise. This is to
* support barges (ship, cart, flying carpet).
*/
int Game_object::get_rotated_frame
(
int quads // 1=90, 2=180, 3=270.
)
{
Game_window *gwin = Game_window::get_game_window();
int curframe = get_framenum();
int shapenum = get_shapenum();
Shape_info& info = gwin->get_info(shapenum);
if (shapenum == 292) // Seat is a special case.
{
int dir = curframe%4; // Current dir (0-3).
return (curframe - dir) + (dir + quads)%4;
}
else if (info.is_barge_part()) // Piece of a barge?
switch (quads)
{
case 1:
return (curframe^32)^((curframe&32) ? 3 : 1);
case 2:
return curframe^2;
case 3:
return (curframe^32)^((curframe&32) ? 1 : 3);
default:
return curframe;
}
else
// Reflect. Bit 32==horizontal.
return curframe ^ ((quads%2)<<5);
}
/*
* Figure attack points against an object, and also run weapon's usecode.
*/
int Game_object::attack_object
(
Game_window *gwin,
Actor *attacker,
int weapon_shape, // Weapon shape, or 0 to use readied.
int ammo_shape
)
{
int wpoints = 0;
Weapon_info *winf;
if (weapon_shape > 0)
winf = gwin->get_info(weapon_shape).get_weapon_info();
else if (ammo_shape > 0) // Not sure about all this...
winf = gwin->get_info(ammo_shape).get_weapon_info();
else
winf = attacker->get_weapon(wpoints);
int usefun; // Run usecode if present.
if (winf && (usefun = winf->get_usecode()) != 0)
gwin->get_usecode()->call_usecode(usefun, this,
Usecode_machine::weapon);
if (!wpoints && winf)
wpoints = winf->get_damage();
if (attacker)
wpoints += attacker->get_level() +
attacker->get_property((int) Actor::strength);
return wpoints;
}
#if 0 /* ++++++++NOT USED */
/*
* Get all connected pieces of an object.
*/
static void Get_connected
(
Game_object *obj,
Game_object_vector& vec // All returned here.
)
{
vec.append(obj);
Game_object_vector newones;
// (Delta = 2 ok for glass counters.)
obj->find_nearby(newones, obj->get_shapenum(), 2, 0, c_any_qual, c_any_framenum);
bool found = false;
for (Game_object_vector::const_iterator it = newones.begin();
it != newones.end(); ++it)
{
Game_object *newobj = *it;
if (vec.find(newobj) == -1)
{ // Look recursively.
Get_connected(newobj, vec);
found = true;
}
}
}
#endif
/*
* Being attacked.
*
* Output: 0 if destroyed, else object itself.
*/
Game_object *Game_object::attacked
(
Actor *attacker,
int weapon_shape, // Weapon shape, or 0 to use readied.
int ammo_shape
)
{
Game_window *gwin = Game_window::get_game_window();
int wpoints = attack_object(gwin,
attacker, weapon_shape, ammo_shape);
int shnum = get_shapenum();
int frnum = get_framenum();
int hp = get_obj_hp(); // Returns 0 if doesn't have HP's or is
// indestructible,
if (!hp) { // with exceptions:
// some special cases
// guessing these don't have hitpoints by default because
// doors need their 'quality' field for something else
if (shnum == 432 || shnum == 433) // doors
{
if (get_quality()==0 || weapon_shape==704)
// only 'normal' doors (or powderkeg)
if (frnum != 3 && frnum < 7) // no magic-locked or steel doors
hp = 6;
}
else if (shnum == 270 || shnum == 376) // more doors
{
if (get_quality()==0 || weapon_shape==704)
// only 'normal' doors (or powderkeg)
if (frnum < 3 || (frnum >= 8 && frnum <= 10) ||
(frnum >= 16 && frnum <= 18)) // no magic or steel doors
hp = 6;
}
// Serpent statue at end of SI:
else if (shnum == 743 && Game::get_game_type() == SERPENT_ISLE)
hp = 1;
else if (shnum == 704 && weapon_shape == 704) { // Powder keg...
// cause chain reaction
// marked already detonating powderkegs with quality
if (get_quality()==0) {
Tile_coord pos = get_tile();
gwin->add_effect(new Explosion_effect(pos,
this));
}
}
// Arrow hitting practice targt?
else if (shnum == 735 && ammo_shape == 722) {
int newframe = !frnum ? (3*(rand()%8) + 1)
: ((frnum%3) != 0 ? frnum + 1 : frnum);
gwin->add_dirty(this);
set_frame(newframe);
gwin->add_dirty(this);
}
#if 0
else if (shnum == 522 && frnum < 2) { // locked normal chest
if (get_quality() == 0 || get_quality() == 255)
hp = 6;
}
#endif
}
string name = "<trap>";
if (attacker)
name = attacker->get_name();
if (hp == 0) { // indestructible
cout << name << " attacks " << get_name() <<
". No effect." << endl;
return this;
}
cout << name << " hits " << get_name() <<
" for " << wpoints << " hit points, leaving " <<
hp - wpoints << " remaining" << endl;
if (wpoints >= hp) {
// object destroyed
gwin->remove_text_effect(this); // Avoids crash.
gwin->get_usecode()->call_usecode(0x626, this,Usecode_machine::weapon);
return 0;
} else {
set_obj_hp(hp - wpoints);
return this;
}
}
/*
* Write the common IREG data for an entry.
*/
void Game_object::write_common_ireg
(
unsigned char *buf // 4-byte buffer to be filled.
)
{
// Coords:
buf[0] = ((get_cx()%16) << 4) | get_tx();
buf[1] = ((get_cy()%16) << 4) | get_ty();
int shapenum = get_shapenum(), framenum = get_framenum();
buf[2] = shapenum&0xff;
buf[3] = ((shapenum>>8)&3) | (framenum<<2);
}
/*
* Write out an IFIX object.
*/
void Ifix_game_object::write_ifix
(
ostream& ifix // Where to write.
)
{
unsigned char buf[4];
buf[0] = shape_pos;
buf[1] = lift;
int shapenum = get_shapenum(), framenum = get_framenum();
buf[2] = shapenum&0xff;
buf[3] = ((shapenum>>8)&3) | (framenum<<2);
ifix.write((char*)buf, sizeof(buf));
}
|