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 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/config-manager.h"
#include "hdb/hdb.h"
#include "hdb/ai.h"
#include "hdb/ai-player.h"
#include "hdb/file-manager.h"
#include "hdb/gfx.h"
#include "hdb/input.h"
#include "hdb/lua-script.h"
#include "hdb/map.h"
#include "hdb/mpc.h"
#include "hdb/sound.h"
#include "hdb/window.h"
namespace HDB {
AIEntity *AI::spawn(AIType type, AIDir dir, int x, int y, const char *funcInit, const char *funcAction, const char *funcUse, AIDir dir2, int level, int value1, int value2, int callInit) {
AIEntity *e = new AIEntity;
e->type = type;
e->dir = dir;
// Set Co-ordinates & Speed
e->x = x * kTileWidth;
e->tileX = x;
e->y = y * kTileHeight;
e->tileY = y;
e->moveSpeed = kPlayerMoveSpeed; // Default Speed
if (!g_hdb->getActionMode())
e->moveSpeed /= 2;
// Other variables
e->dir2 = dir2;
if (!level)
level = 1;
e->level = level;
e->value1 = value1;
e->value2 = value2;
e->animCycle = 2; // Game frames to wait before animating graphic frames
e->animDelay = e->animCycle;
e->animFrame = 0;
if (funcInit)
Common::strlcpy(e->luaFuncInit, funcInit, 32);
if (funcAction)
Common::strlcpy(e->luaFuncAction, funcAction, 32);
if (funcUse)
Common::strlcpy(e->luaFuncUse, funcUse, 32);
if (e->luaFuncInit[0] == '*')
e->luaFuncInit[0] = 0;
if (e->luaFuncAction[0] == '*')
e->luaFuncAction[0] = 0;
if (e->luaFuncUse[0] == '*')
e->luaFuncUse[0] = 0;
e->standdownFrames = e->standupFrames = e->standleftFrames = e->standrightFrames = 0;
e->movedownFrames = e->moveupFrames = e->moveleftFrames = e->moverightFrames = 0;
e->blinkFrames = 0;
if (!cacheEntGfx(e, (bool)callInit))
return nullptr;
else
_ents->push_back(e);
return e;
}
bool AI::cacheEntGfx(AIEntity *e, bool initFlag) {
int i = 0;
while (true) {
if (aiEntList[i].type == END_AI_TYPES)
return false;
// Load Gfx for corresponding Entity
if (aiEntList[i].type == e->type) {
int j = 0;
AIStateDef *list = aiEntList[i].stateDef;
while (list[j].state != STATE_ENDSTATES) {
Common::Array<const char *> *gfxFiles = g_hdb->_fileMan->findFiles(list[j].name, TYPE_TILE32);
uint32 size;
if (gfxFiles->size() == 0)
warning("AI::cacheEntGfx: no files for %s", list[j].name);
for (Common::Array<const char *>::iterator it = gfxFiles->begin(); it != gfxFiles->end(); ++it) {
size = g_hdb->_fileMan->getLength((*it), TYPE_TILE32);
if (g_hdb->_gfx->selectGfxType((*it))) {
Tile *gfx = g_hdb->_gfx->getTileGfx((*it), size);
switch (list[j].state) {
case STATE_STANDDOWN:
e->standdownGfx[e->standdownFrames] = gfx;
e->standdownFrames++;
break;
case STATE_STANDUP:
e->standupGfx[e->standupFrames] = gfx;
e->standupFrames++;
break;
case STATE_STANDLEFT:
e->standleftGfx[e->standleftFrames] = gfx;
e->standleftFrames++;
break;
case STATE_STANDRIGHT:
e->standrightGfx[e->standrightFrames] = gfx;
e->standrightFrames++;
break;
case STATE_BLINK:
e->blinkGfx[e->blinkFrames] = gfx;
e->blinkFrames++;
break;
case STATE_MOVEDOWN:
e->movedownGfx[e->movedownFrames] = gfx;
e->movedownFrames++;
break;
case STATE_MOVEUP:
e->moveupGfx[e->moveupFrames] = gfx;
e->moveupFrames++;
break;
case STATE_MOVELEFT:
e->moveleftGfx[e->moveleftFrames] = gfx;
e->moveleftFrames++;
break;
case STATE_MOVERIGHT:
e->moverightGfx[e->moverightFrames] = gfx;
e->moverightFrames++;
break;
// Special Player Frames
case STATE_PUSHDOWN:
_pushdownGfx[_pushdownFrames] = gfx;
_pushdownFrames++;
break;
case STATE_PUSHUP:
_pushupGfx[_pushupFrames] = gfx;
_pushupFrames++;
break;
case STATE_PUSHLEFT:
_pushleftGfx[_pushleftFrames] = gfx;
_pushleftFrames++;
break;
case STATE_PUSHRIGHT:
_pushrightGfx[_pushrightFrames] = gfx;
_pushrightFrames++;
break;
case STATE_GRABUP:
_getGfx[DIR_UP] = gfx;
break;
case STATE_GRABDOWN:
_getGfx[DIR_DOWN] = gfx;
break;
case STATE_GRABLEFT:
_getGfx[DIR_LEFT] = gfx;
break;
case STATE_GRABRIGHT:
_getGfx[DIR_RIGHT] = gfx;
break;
case STATE_ATK_STUN_UP:
_stunUpGfx[_stunUpFrames] = gfx;
_stunUpFrames++;
break;
case STATE_ATK_STUN_DOWN:
_stunDownGfx[_stunDownFrames] = gfx;
_stunDownFrames++;
break;
case STATE_ATK_STUN_LEFT:
_stunLeftGfx[_stunLeftFrames] = gfx;
_stunLeftFrames++;
break;
case STATE_ATK_STUN_RIGHT:
_stunRightGfx[_stunRightFrames] = gfx;
_stunRightFrames++;
break;
case STATE_ATK_SLUG_UP:
_slugUpGfx[_slugUpFrames] = gfx;
_slugUpFrames++;
break;
case STATE_ATK_SLUG_DOWN:
_slugDownGfx[_slugDownFrames] = gfx;
_slugDownFrames++;
break;
case STATE_ATK_SLUG_LEFT:
_slugLeftGfx[_slugLeftFrames] = gfx;
_slugLeftFrames++;
break;
case STATE_ATK_SLUG_RIGHT:
_slugRightGfx[_slugRightFrames] = gfx;
_slugRightFrames++;
break;
// Maintenance Bot
case STATE_USEUP:
e->standupGfx[4] = gfx;
break;
case STATE_USEDOWN:
e->standdownGfx[4] = gfx;
break;
case STATE_USELEFT:
e->standleftGfx[4] = gfx;
break;
case STATE_USERIGHT:
e->standrightGfx[4] = gfx;
break;
// Death & Dying for Player
case STATE_DYING:
_dyingGfx[_dyingFrames] = gfx;
_dyingFrames++;
break;
case STATE_GOODJOB:
_goodjobGfx = gfx;
break;
case STATE_HORRIBLE1:
_horrible1Gfx[_horrible1Frames] = gfx;
_horrible1Frames++;
break;
case STATE_HORRIBLE2:
_horrible2Gfx[_horrible2Frames] = gfx;
_horrible2Frames++;
break;
case STATE_HORRIBLE3:
_horrible3Gfx[_horrible3Frames] = gfx;
_horrible3Frames++;
break;
case STATE_HORRIBLE4:
_horrible4Gfx[_horrible4Frames] = gfx;
_horrible4Frames++;
break;
case STATE_PLUMMET:
_plummetGfx[_plummetFrames] = gfx;
_plummetFrames++;
break;
// floating frames - overwrite "standup" info
case STATE_FLOATING:
e->blinkGfx[e->blinkFrames] = gfx;
e->blinkFrames++;
break;
// melted frames - go in the special area (lightbarrels)
// shocking frames - go in the special1 area (shockbots)
// exploding frames, same
case STATE_MELTED:
case STATE_SHOCKING:
case STATE_EXPLODING:
e->special1Gfx[e->special1Frames] = gfx;
e->special1Frames++;
break;
// ICEPUFF spawning states
case STATE_ICEP_PEEK:
e->blinkGfx[e->blinkFrames] = gfx;
e->blinkFrames++;
break;
case STATE_ICEP_APPEAR:
e->standupGfx[e->standupFrames] = gfx;
e->standupFrames++;
break;
case STATE_ICEP_THROWDOWN:
e->standdownGfx[e->standdownFrames] = gfx;
e->standdownFrames++;
break;
case STATE_ICEP_THROWRIGHT:
e->standrightGfx[e->standrightFrames] = gfx;
e->standrightFrames++;
break;
case STATE_ICEP_THROWLEFT:
e->standleftGfx[e->standleftFrames] = gfx;
e->standleftFrames++;
break;
case STATE_ICEP_DISAPPEAR:
e->special1Gfx[e->special1Frames] = gfx;
e->special1Frames++;
break;
// FATFROG spawning states
case STATE_LICKDOWN:
e->movedownGfx[e->movedownFrames] = gfx;
e->movedownFrames++;
break;
case STATE_LICKLEFT:
e->moveleftGfx[e->moveleftFrames] = gfx;
e->moveleftFrames++;
break;
case STATE_LICKRIGHT:
e->moverightGfx[e->moverightFrames] = gfx;
e->moverightFrames++;
break;
// MEERKAT spawning states
case STATE_MEER_MOVE:
e->standdownGfx[e->standdownFrames] = gfx;
e->standdownFrames++;
break;
case STATE_MEER_APPEAR:
e->standleftGfx[e->standleftFrames] = gfx;
e->standleftFrames++;
break;
case STATE_MEER_BITE:
e->standrightGfx[e->standrightFrames] = gfx;
e->standrightFrames++;
break;
case STATE_MEER_DISAPPEAR:
e->standupGfx[e->standupFrames] = gfx;
e->standupFrames++;
break;
case STATE_MEER_LOOK:
e->movedownGfx[e->movedownFrames] = gfx;
e->movedownFrames++;
break;
// DIVERTER spawning states
case STATE_DIVERTER_BL:
e->standdownGfx[e->standdownFrames] = gfx;
e->standdownFrames++;
break;
case STATE_DIVERTER_BR:
e->standupGfx[e->standupFrames] = gfx;
e->standupFrames++;
break;
case STATE_DIVERTER_TL:
e->standleftGfx[e->standleftFrames] = gfx;
e->standleftFrames++;
break;
case STATE_DIVERTER_TR:
e->standrightGfx[e->standrightFrames] = gfx;
e->standrightFrames++;
break;
// DOLLY states
// angry[4] = standright[4]
// kissright[4]/kissleft[4] = standleft[8]
// panic[4]/laugh[4] = standdown[8]
// dollyuseright[5] = special1[5]
case STATE_ANGRY:
e->standrightGfx[e->standrightFrames] = gfx;
e->standrightFrames++;
break;
case STATE_KISSRIGHT:
e->standleftGfx[e->standleftFrames] = gfx;
e->standleftFrames++;
break;
case STATE_KISSLEFT:
e->standleftGfx[4 + e->int1] = gfx;
e->int1++;
break;
case STATE_PANIC:
e->standdownGfx[e->standdownFrames] = gfx;
e->standdownFrames++;
break;
case STATE_LAUGH:
e->standdownGfx[4 + e->value1] = gfx;
e->value1++;
break;
case STATE_DOLLYUSERIGHT:
e->special1Gfx[e->special1Frames] = gfx;
e->special1Frames++;
break;
// SARGE yelling
case STATE_YELL:
e->special1Gfx[e->special1Frames] = gfx;
e->special1Frames++;
break;
default:
// no op
break;
}
} else {
Picture *gfx = g_hdb->_gfx->getPicGfx((*it), size);
switch (list[j].state) {
case STATE_ATK_CLUB_UP:
_clubUpGfx[_clubUpFrames] = gfx;
_clubUpFrames++;
break;
case STATE_ATK_CLUB_DOWN:
_clubDownGfx[_clubDownFrames] = gfx;
_clubDownFrames++;
break;
case STATE_ATK_CLUB_LEFT:
_clubLeftGfx[_clubLeftFrames] = gfx;
_clubLeftFrames++;
break;
case STATE_ATK_CLUB_RIGHT:
_clubRightGfx[_clubRightFrames] = gfx;
_clubRightFrames++;
break;
default:
break;
}
}
}
j++;
delete gfxFiles;
}
e->aiInit = aiEntList[i].initFunc;
e->aiInit2 = aiEntList[i].initFunc2;
if (initFlag) {
e->aiInit(e, 0, 0);
if (e->aiInit2)
e->aiInit2(e, 0, 0);
if (e->luaFuncInit[0]) {
g_hdb->_lua->callFunction(e->luaFuncInit, 2);
const char *str1 = g_hdb->_lua->getStringOffStack();
const char *str2 = g_hdb->_lua->getStringOffStack();
if (str1)
Common::strlcpy(e->entityName, str1, 32);
if (str2)
Common::strlcpy(e->printedName, str2, 32);
}
} else if (e->aiInit2)
e->aiInit2(e, 0, 0);
break; // Entity Initiated
}
i++;
}
return true;
}
// Stops the movement of an entity
void AI::stopEntity(AIEntity *e) {
if (e == _player) {
clearWaypoints();
// Reset Player speed
e->moveSpeed = kPlayerMoveSpeed;
}
// Reset animation
e->animFrame = 0;
// Align with tile boundaries
e->x = e->tileX * kTileWidth;
e->y = e->tileY * kTileHeight;
// TODO: Check in the original if also present. Removed as it's useless
// e->goalX = e->tileX;
// e->goalY = e->tileY;
e->drawXOff = e->drawYOff = 0;
e->goalX = e->goalY = e->xVel = e->yVel = 0;
// Don't change the state of Diverters or Floating entities
switch (e->state) {
case STATE_FLOATLEFT:
case STATE_FLOATRIGHT:
case STATE_FLOATUP:
case STATE_FLOATDOWN:
e->state = STATE_FLOATING;
return;
default:
break;
}
if (e->type != AI_DIVERTER) {
switch (e->dir) {
case DIR_UP:
if (e->standupFrames)
e->state = STATE_STANDUP;
else
e->state = STATE_NONE;
break;
case DIR_DOWN:
if (e->standdownFrames)
e->state = STATE_STANDDOWN;
else
e->state = STATE_NONE;
break;
case DIR_LEFT:
if (e->standleftFrames)
e->state = STATE_STANDLEFT;
else
e->state = STATE_NONE;
break;
case DIR_RIGHT:
if (e->standrightFrames)
e->state = STATE_STANDRIGHT;
else
e->state = STATE_NONE;
break;
default:
break;
}
}
}
AIEntity *AI::locateEntity(const char *luaName) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
if (Common::matchString((*it)->entityName, luaName))
return *it;
}
return nullptr;
}
AIEntity *AI::findEntity(int x, int y) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y)
return *it;
}
for (Common::Array<AIEntity *>::iterator it = _floats->begin(); it != _floats->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y)
return *it;
}
if (g_hdb->_map->laserBeamExist(x, y))
return &_dummyLaser;
return nullptr;
}
AIEntity *AI::findEntityIgnore(int x, int y, AIEntity *ignore) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y && (*it) != ignore)
return *it;
}
for (Common::Array<AIEntity *>::iterator it = _floats->begin(); it != _floats->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y && (*it) != ignore)
return *it;
}
if (g_hdb->_map->laserBeamExist(x, y) && ignore->type != AI_LASERBEAM)
return &_dummyLaser;
return nullptr;
}
AIEntity *AI::findEntityType(AIType type, int x, int y) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y && (*it)->type == type)
return *it;
}
for (Common::Array<AIEntity *>::iterator it = _floats->begin(); it != _floats->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y && (*it)->type == type)
return *it;
}
if (g_hdb->_map->laserBeamExist(x, y) && type == AI_LASERBEAM)
return &_dummyLaser;
return nullptr;
}
void AI::getEntityXY(const char *entName, int *x, int *y) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
AIEntity *e = *it;
if (!scumm_stricmp(entName, e->entityName)) {
*x = e->tileX;
*y = e->tileY;
return;
}
}
for (Common::Array<AIEntity *>::iterator jt = _floats->begin(); jt != _floats->end(); ++jt) {
AIEntity *e = *jt;
if (!scumm_stricmp(entName, e->entityName)) {
*x = e->tileX;
*y = e->tileY;
return;
}
}
for (Common::Array<HereT *>::iterator kt = _hereList->begin(); kt != _hereList->end(); ++kt) {
HereT *h = *kt;
if (!scumm_stricmp(entName, h->entName)) {
*x = h->x;
*y = h->y;
return;
}
}
}
bool AI::useLuaEntity(const char *initName) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
AIEntity *e = *it;
if (!scumm_stricmp(initName, e->entityName)) {
e->aiUse(e, 0, 0);
checkActionList(e, e->tileX, e->tileY, true);
if (e->luaFuncUse[0])
g_hdb->_lua->callFunction(e->luaFuncUse, 0);
return true;
}
}
// Check _actions list for activation as well
for (int i = 0; i < kMaxActions; i++) {
if (!scumm_stricmp(initName, _actions[i].entityName)) {
checkActionList(&_dummyPlayer, _actions[i].x1, _actions[i].y1, false);
checkActionList(&_dummyPlayer, _actions[i].x2, _actions[i].y2, false);
}
}
return false;
}
void AI::removeLuaEntity(const char *initName) {
for (uint i = 0; i < _ents->size(); i++) {
AIEntity *e = _ents->operator[](i);
if (!scumm_stricmp(initName, e->entityName)) {
removeEntity(e);
i--;
}
}
}
void AI::animLuaEntity(const char *initName, AIState st) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
AIEntity *e = *it;
if (!scumm_stricmp(initName, e->entityName)) {
e->state = st;
e->animFrame = 0;
e->animDelay = e->animCycle;
}
}
}
void AI::setLuaAnimFrame(const char *initName, AIState st, int frame) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
AIEntity *e = *it;
if (!scumm_stricmp(initName, e->entityName)) {
e->state = st;
e->animFrame = frame;
e->animDelay = e->animCycle;
animEntFrames(e);
e->state = STATE_NONE;
}
}
}
int AI::checkForTouchplate(int x, int y) {
int tileIndex = g_hdb->_map->getMapBGTileIndex(x, y);
if (tileIndex == _touchplateOff || tileIndex == _templeTouchpOff)
return tileIndex;
return 0;
}
void AI::removeEntity(AIEntity *e) {
for (uint i = 0; i < _ents->size(); i++) {
if (_ents->operator[](i) == e) {
delete _ents->operator[](i);
_ents->remove_at(i);
return;
}
}
}
void AI::setEntityGoal(AIEntity *e, int x, int y) {
e->xVel = e->yVel = 0;
int xv = x - e->tileX;
if (xv < 0) {
e->xVel = -e->moveSpeed;
e->state = STATE_MOVELEFT;
e->dir = DIR_LEFT;
} else if (xv > 0) {
e->xVel = e->moveSpeed;
e->state = STATE_MOVERIGHT;
e->dir = DIR_RIGHT;
}
int yv = y - e->tileY;
if (yv < 0) {
e->yVel = -e->moveSpeed;
e->state = STATE_MOVEUP;
e->dir = DIR_UP;
} else if (yv > 0) {
e->yVel = e->moveSpeed;
e->state = STATE_MOVEDOWN;
e->dir = DIR_DOWN;
}
if (e->type == AI_GUY && _playerRunning) {
e->xVel = e->xVel << 1;
e->yVel = e->yVel << 1;
}
e->goalX = x;
e->goalY = y;
e->animFrame = 0;
e->drawXOff = e->drawYOff = 0;
}
// Initializes each entity after map is loaded
void AI::initAllEnts() {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
(*it)->aiInit((*it), 0, 0);
if ((*it)->luaFuncInit[0]) {
if (g_hdb->_lua->callFunction((*it)->luaFuncInit, 2)) {
Common::strlcpy((*it)->entityName, g_hdb->_lua->getStringOffStack(), 32);
Common::strlcpy((*it)->printedName, g_hdb->_lua->getStringOffStack(), 32);
} else
warning("'%s' doesn't exists", (*it)->luaFuncInit);
}
}
for (int i = 0; i < _numInventory; i++) {
AIEntity *temp = &_inventory[i].ent;
// Clear out all ptrs in entity before writing
for (int j = 0; j < kMaxAnimFrames; j++) {
temp->blinkGfx[j] = nullptr;
temp->movedownGfx[j] = nullptr;
temp->moveupGfx[j] = nullptr;
temp->moveleftGfx[j] = nullptr;
temp->moverightGfx[j] = nullptr;
temp->standdownGfx[j] = nullptr;
temp->standupGfx[j] = nullptr;
temp->standleftGfx[j] = nullptr;
temp->standrightGfx[j] = nullptr;
temp->special1Gfx[j] = nullptr;
}
temp->blinkFrames = 0;
temp->movedownFrames = 0;
temp->moveupFrames = 0;
temp->moveleftFrames = 0;
temp->moverightFrames = 0;
temp->standdownFrames = 0;
temp->standupFrames = 0;
temp->standleftFrames = 0;
temp->standrightFrames = 0;
temp->draw = nullptr;
temp->aiDraw = nullptr;
temp->aiAction = temp->aiInit = temp->aiUse = nullptr;
cacheEntGfx(temp, false);
}
for (int i = 0; i < _numDeliveries; i++) {
_deliveries[i].itemGfx = g_hdb->_gfx->getTileGfx(_deliveries[i].itemGfxName, -1);
_deliveries[i].destGfx = g_hdb->_gfx->getTileGfx(_deliveries[i].destGfxName, -1);
}
// do a quick LaserScan to fill the laserbeam matrix!
laserScan();
}
void AI::killPlayer(Death method) {
int x = _player->x, y = _player->y;
stopEntity(_player);
_player->x = x;
_player->y = y;
_playerInvisible = false;
_playerDead = true;
g_hdb->_window->closeDialog();
g_hdb->_window->closeDialogChoice();
g_hdb->_window->stopPanicZone();
if (g_hdb->isPPC()) {
g_hdb->_window->closeDlvs();
g_hdb->_window->closeInv();
}
switch (method) {
case DEATH_NORMAL:
_player->state = STATE_DYING;
g_hdb->_sound->playSound(SND_GUY_DYING);
break;
case DEATH_FRIED:
_player->state = STATE_HORRIBLE1;
g_hdb->_sound->playSound(SND_GUY_FRIED);
break;
case DEATH_SHOCKED:
_player->state = STATE_HORRIBLE2;
g_hdb->_sound->playSound(SND_GUY_DYING);
g_hdb->_sound->playSound(SND_SHOCKBOT_SHOCK);
break;
case DEATH_GRABBED:
_player->state = STATE_HORRIBLE3;
g_hdb->_sound->playSound(SND_GUY_GRABBED);
break;
case DEATH_DROWNED:
_player->state = STATE_HORRIBLE4;
g_hdb->_sound->playSound(SND_GUY_DROWN);
break;
case DEATH_PANICZONE:
_player->state = STATE_DYING;
g_hdb->_sound->playSound(SND_PANIC_DEATH);
break;
case DEATH_PLUMMET:
if (!g_hdb->isDemo()) {
_player->state = STATE_PLUMMET;
g_hdb->_sound->playSound(SND_GUY_PLUMMET);
}
break;
default:
break;
}
// sound.StopMusic();
if (ConfMan.getInt(CONFIG_MUSICVOL) == 0)
g_hdb->_sound->playSound(SND_TRY_AGAIN);
}
void AI::stunEnemy(AIEntity *e, int time) {
bool ns = (e->stunnedWait != 0);
e->stunnedWait = g_hdb->getTimeSlice() + 1000 * time;
// Already stunned? If not, play sound
if (!ns)
switch (e->type) {
case AI_BUZZFLY:
g_hdb->_sound->playSound(SND_BUZZFLY_STUNNED);
break;
case AI_PUSHBOT:
g_hdb->_sound->playSound(SND_PUSHBOT_STUNNED);
break;
case AI_MEERKAT:
g_hdb->_sound->playSound(SND_MEERKAT_STUNNED);
break;
case AI_FATFROG:
g_hdb->_sound->playSound(SND_FATFROG_STUNNED);
break;
case AI_OMNIBOT:
case AI_SHOCKBOT:
case AI_LISTENBOT:
g_hdb->_sound->playSound(SND_ROBOT_STUNNED);
break;
case AI_GOODFAIRY:
g_hdb->_sound->playSound(SND_GOOD_FAERIE_STUNNED);
break;
case AI_BADFAIRY:
g_hdb->_sound->playSound(SND_BADFAIRY_STUNNED);
break;
case AI_ICEPUFF:
g_hdb->_sound->playSound(SND_ICEPUFF_STUNNED);
break;
case AI_RIGHTBOT:
g_hdb->_sound->playSound(SND_RIGHTBOT_STUNNED);
break;
case AI_BOOMBARREL:
g_hdb->_sound->playSound(SND_CLUB_HIT_METAL);
break;
case AI_CHICKEN:
g_hdb->_sound->playSound(SND_CHICKEN_DEATH);
// fallthrough
default:
g_hdb->_sound->playSound(g_hdb->_ai->metalOrFleshSND(e));
break;
}
}
int AI::metalOrFleshSND(AIEntity *e) {
switch (e->type) {
case AI_OMNIBOT:
case AI_TURNBOT:
case AI_SHOCKBOT:
case AI_RIGHTBOT:
case AI_PUSHBOT:
case AI_LISTENBOT:
case AI_MAINTBOT:
return SND_CLUB_HIT_METAL;
case AI_DEADEYE:
case AI_MEERKAT:
case AI_FATFROG:
case AI_GOODFAIRY:
case AI_BADFAIRY:
case AI_ICEPUFF:
case AI_BUZZFLY:
default:
return SND_CLUB_HIT_FLESH;
}
}
/*
Note from original:
Moves the entity along toward its goal, sets current frame to draw
depending on its current state. Special checking is done for the
player in here to move him along his waypoints.
*/
void AI::animateEntity(AIEntity *e) {
static const int xva[5] = {9, 0, 0, -1, 1};
static const int yva[5] = {9, -1, 1, 0, 0};
// Move entity if player is not dead
debug(9, "Before animateEntity, e->x: %d, e->y: %d", e->x, e->y);
debug(9, "Before animateEntity, e->tileX: %d, e->tileY: %d", e->tileX, e->tileY);
if (!_playerDead) {
e->x += e->xVel;
e->y += e->yVel;
e->tileX = e->x / kTileWidth;
e->tileY = e->y / kTileHeight;
debug(9, "After animateEntity, e->x: %d, e->y: %d", e->x, e->y);
debug(9, "After animateEntity, e->tileX: %d, e->tileY: %d", e->tileX, e->tileY);
}
// For non-players, check for trigger being hit
if (onEvenTile(e->x, e->y)) {
// Check if a trigger is hit
checkTriggerList(e->entityName, e->tileX, e->tileY);
/*
For Non-Players only
are we on a touchplate?
Barrels, Crates, Magic Egg & Ice Block ONLY
standing on a Touchplate will activate
something WHILE standing on it
*/
switch (e->type) {
case AI_CRATE:
case AI_BOOMBARREL:
case AI_HEAVYBARREL:
case AI_LIGHTBARREL:
case AI_MAGIC_EGG:
case AI_ICE_BLOCK:
case AI_FROGSTATUE:
{
int bgtile = g_hdb->_ai->checkForTouchplate(e->tileX, e->tileY);
if (bgtile && !e->touchpWait && e->touchpX != e->tileX && e->touchpY != e->tileY) {
if (g_hdb->_ai->checkActionList(e, e->tileX, e->tileY, false)) {
e->touchpTile = bgtile;
e->touchpX = e->tileX;
e->touchpY = e->tileY;
e->touchpWait = kPlayerTouchPWait;
}
}
_laserRescan = true;
}
break;
default:
break;
}
// Are we on ice?
int bgTileFlags = g_hdb->_map->getMapBGTileFlags(e->tileX, e->tileY);
int fgTileFlags = g_hdb->_map->getMapFGTileFlags(e->tileX, e->tileY);
if (e->level == 1 ? ((bgTileFlags & kFlagIce) == kFlagIce) : (((bgTileFlags & kFlagIce) == kFlagIce) && !(fgTileFlags & kFlagGrating))) {
int nx, ny, moveOK = 0;
AIEntity *hit;
// Types allowed to slide on ice...
switch (e->type) {
case AI_GUY:
case AI_CHICKEN:
case AI_TURNBOT:
case AI_RIGHTBOT:
case AI_PUSHBOT:
case AI_CRATE:
case AI_LIGHTBARREL:
case AI_HEAVYBARREL:
case AI_BOOMBARREL:
case AI_MAGIC_EGG:
case AI_ICE_BLOCK:
case AI_DIVERTER:
e->moveSpeed = kPlayerMoveSpeed << 1;
nx = e->tileX + xva[e->dir];
ny = e->tileY + yva[e->dir];
hit = legalMove(nx, ny, e->level, &moveOK);
bgTileFlags = g_hdb->_map->getMapBGTileFlags(nx, ny);
if (hit)
switch (hit->type) {
case ITEM_GEM_WHITE:
case ITEM_GEM_BLUE:
case ITEM_GEM_GREEN:
case ITEM_GEM_RED:
case AI_GOODFAIRY:
case AI_BADFAIRY:
hit = nullptr;
break;
default:
break;
}
if ((!hit && moveOK) || (bgTileFlags & kFlagPlayerDie))
setEntityGoal(e, nx, ny);
if (e == _player) {
_playerOnIce = true;
clearWaypoints();
}
break;
default:
break;
}
} else if (e == _player)
_playerOnIce = false;
/*
Player only
are we trying to walk into a solid tile?
first, let's make sure we're perfectly aligned on
a tile boundary before the check so we don't snap
the player back into position...
if we're on a waypoint, nevermind!
*/
if (e == _player) {
bool result = e->x == (e->goalX * kTileWidth) && e->y == (e->goalY * kTileWidth);
if (!result) {
int xv = 0, yv = 0;
switch (e->dir) {
case DIR_UP:
yv = -1;
break;
case DIR_DOWN:
yv = 1;
break;
case DIR_LEFT:
xv = -1;
break;
case DIR_RIGHT:
xv = 1;
break;
case DIR_NONE:
default:
break;
}
bgTileFlags = g_hdb->_map->getMapBGTileFlags(e->tileX + xv, e->tileY + yv);
fgTileFlags = g_hdb->_map->getMapFGTileFlags(e->tileX + xv, e->tileY + yv);
if ((bgTileFlags & kFlagSolid) && !(fgTileFlags & kFlagGrating))
stopEntity(e);
}
}
}
// If player, then scroll the screen with the player
if (e == _player && !_playerDead) {
if (!_cameraLock)
g_hdb->_map->centerMapXY(e->x + 16, e->y + 16);
// Check if player walked into teleporter
checkTeleportList(e, e->tileX, e->tileY);
// Check for bad tiles (DEATH)
int cx = (e->x + 16) / kTileWidth;
int cy = (e->y + 16) / kTileHeight;
int bgTileFlags = g_hdb->_map->getMapBGTileFlags(cx, cy);
int fgTileFlags = g_hdb->_map->getMapFGTileFlags(cx, cy);
if ((bgTileFlags & kFlagPlayerDie) && !(checkFloating(cx, cy)) && !(fgTileFlags & kFlagGrating)) {
if ((bgTileFlags & kFlagEnergyFloor) == kFlagEnergyFloor)
killPlayer(DEATH_SHOCKED);
else if (((bgTileFlags & kFlagPlasmaFloor) == kFlagPlasmaFloor) || ((bgTileFlags & kFlagRadFloor) == kFlagRadFloor))
killPlayer(DEATH_FRIED);
else
killPlayer(DEATH_NORMAL);
return;
}
// Check if player wants to stop
// If yes, sets last waypoint right in front of player
if (_numWaypoints > 1) {
int xOff = 0;
int yOff = 0;
switch (e->dir) {
case DIR_UP:
xOff = 0;
yOff = -1;
break;
case DIR_DOWN:
xOff = 0;
yOff = 1;
break;
case DIR_LEFT:
xOff = -1;
yOff = 0;
break;
case DIR_RIGHT:
xOff = 1;
yOff = 0;
break;
case DIR_NONE:
default:
break;
}
if ((e->tileX + xOff == _waypoints[_numWaypoints - 1].x &&
e->tileY + yOff == _waypoints[_numWaypoints - 1].y) &&
e->level == _waypoints[_numWaypoints - 1].level) {
clearWaypoints();
_numWaypoints = 1;
_waypoints[0].x = e->tileX + xOff;
_waypoints[0].y = e->tileY + yOff;
_waypoints[0].level = e->level;
e->goalX = e->tileX + xOff;
e->goalY = e->tileY + yOff;
}
}
}
// Check for moving up/down stair levels
// int bgTileIndex = g_hdb->_map->getMapBGTileIndex(e->tileX, e->tileY); // CHECKME: unused?
int bgTileFlags = g_hdb->_map->getMapBGTileFlags(e->tileX, e->tileY);
// fgTileFlags = g_hdb->_map->getMapFGTileFlags(e->tileX, e->tileY); // CHECKME: unused?
if (bgTileFlags & kFlagStairTop)
e->level = 2;
else if (bgTileFlags & kFlagStairBot)
e->level = 1;
// Reached goal?
// Cinematic require less accuracy for NPCs
bool result;
if (_cineActive && e != _player)
result = (abs(e->x - (e->goalX * kTileWidth)) <= abs(e->xVel)) && (abs(e->y - (e->goalY * kTileHeight)) <= abs(e->yVel));
else
result = (e->x == e->goalX * kTileWidth) && (e->y == e->goalY * kTileHeight);
if (result) {
// If player, this is a waypoint goal.
// Drop one waypoint from list
if (e == _player) {
removeFirstWaypoint();
_playerEmerging = false;
}
// If entity not player, stop it here
// If entity is player and no waypoints are left, stop it here
if (e != _player || (!_numWaypoints && e == _player)) {
e->tileX = e->goalX;
e->tileY = e->goalY;
uint16 buttons = g_hdb->_input->getButtons();
if (e == _player && (buttons & (kButtonUp | kButtonDown | kButtonLeft | kButtonRight))) {
if (e->state != STATE_PUSHRIGHT && e->state != STATE_PUSHLEFT && e->state != STATE_PUSHUP && e->state != STATE_PUSHDOWN) {
if (buttons & kButtonUp)
e->dir = DIR_UP;
else if (buttons & kButtonDown)
e->dir = DIR_DOWN;
else if (buttons & kButtonLeft)
e->dir = DIR_LEFT;
else if (buttons & kButtonRight)
e->dir = DIR_RIGHT;
int nx = e->tileX + xva[e->dir];
int ny = e->tileY + yva[e->dir];
int result2;
AIEntity *hit = legalMove(nx, ny, e->level, &result2);
if (!hit && result2) {
switch (e->dir) {
case DIR_UP:
e->goalY = ny;
e->xVel = 0;
e->yVel = -kPlayerMoveSpeed;
e->state = STATE_MOVEUP;
break;
case DIR_DOWN:
e->goalY = ny;
e->xVel = 0;
e->yVel = kPlayerMoveSpeed;
e->state = STATE_MOVEDOWN;
break;
case DIR_LEFT:
e->goalX = nx;
e->yVel = 0;
e->xVel = -kPlayerMoveSpeed;
e->state = STATE_MOVELEFT;
break;
case DIR_RIGHT:
e->goalX = nx;
e->yVel = 0;
e->xVel = kPlayerMoveSpeed;
e->state = STATE_MOVERIGHT;
break;
case DIR_NONE:
default:
break;
}
if (_playerRunning) {
e->xVel = e->xVel << 1;
e->yVel = e->yVel << 1;
}
} else
stopEntity(e);
} else
stopEntity(e);
} else
stopEntity(e);
// Handle lasers after entity has stopped
switch (e->type) {
case AI_GUY:
case AI_CRATE:
case AI_LIGHTBARREL:
case AI_HEAVYBARREL:
case AI_BOOMBARREL:
case AI_MAGIC_EGG:
case AI_ICE_BLOCK:
case AI_DIVERTER:
if (g_hdb->_map->laserBeamExist(e->tileX, e->tileY))
_laserRescan = true;
break;
default:
break;
}
// Checking at the Destination
uint64 flags = g_hdb->_map->getMapBGTileFlags(e->tileX, e->tileY);
// Can this entity float and it is over-water
if ((flags & kFlagWater) && (e->type == AI_CRATE || e->type == AI_LIGHTBARREL || e->type == AI_BOOMBARREL || e->type == AI_HEAVYBARREL || e->type == AI_FROGSTATUE || e->type == AI_DIVERTER)) {
// On a grating and level2?
if ((g_hdb->_map->getMapFGTileFlags(e->tileX, e->tileY) & kFlagGrating) && e->level == 2) {
animEntFrames(e);
return;
}
// If it fell in slime
// If it is a light barrel on a melting floor
// If it is supposed to slide across the floor
// If it is being pushed on a floating entity, don't float it
if ((flags & kFlagSlime) == kFlagSlime) {
// unless its a Heavy Barrel in which case it floats in slime
if ((e->type == AI_CRATE || e->type == AI_HEAVYBARREL) && !checkFloating(e->tileX, e->tileY)) {
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_SLIME_SPLASH_SIT);
floatEntity(e, STATE_FLOATING);
g_hdb->_sound->playSound(SND_SPLASH);
} else if (!checkFloating(e->tileX, e->tileY)) {
if (e->type == AI_BOOMBARREL) {
aiBarrelExplode(e, 0, 0);
aiBarrelBlowup(e, e->tileX, e->tileY);
return;
} else {
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_STEAM_PUFF_SIT);
removeEntity(e);
if (!g_hdb->isDemo())
g_hdb->_sound->playSound(SND_BARREL_MELTING);
return;
}
}
} else if ((flags & kFlagLightMelt) && e->type == AI_LIGHTBARREL) {
if (!checkFloating(e->tileX, e->tileY)) {
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_STEAM_PUFF_SIT);
floatEntity(e, STATE_MELTED);
if (!g_hdb->isDemo())
g_hdb->_sound->playSound(SND_BARREL_MELTING);
}
} else if (flags & kFlagSlide) {
int xv = 0, yv = 0;
switch (e->dir) {
case DIR_UP:
yv = -1;
break;
case DIR_DOWN:
yv = 1;
break;
case DIR_LEFT:
xv = -1;
break;
case DIR_RIGHT:
xv = 1;
break;
case DIR_NONE:
default:
break;
}
AIEntity *hit = findEntityIgnore(e->tileX + xv, e->tileY + yv, &_dummyLaser);
if (!hit) {
e->state = STATE_SLIDING;
if ((flags & kFlagAnimFast) == kFlagAnimFast)
e->moveSpeed = kPlayerMoveSpeed << 1;
else if (flags & kFlagAnimSlow)
e->moveSpeed = kPlayerMoveSpeed >> 1;
setEntityGoal(e, e->tileX + xv, e->tileY + yv);
g_hdb->_sound->playSound(SND_LIGHT_SLIDE);
}
} else if (!checkFloating(e->tileX, e->tileY)) {
if (e->type == AI_BOOMBARREL || e->type == AI_HEAVYBARREL || e->type == AI_FROGSTATUE || e->type == AI_DIVERTER) {
// Make it disappear in the water
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_WATER_SPLASH_SIT);
removeEntity(e);
g_hdb->_sound->playSound(SND_SPLASH);
return;
} else {
// Make it float and splash in water
e->state = STATE_FLOATING;
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_WATER_SPLASH_SIT);
floatEntity(e, STATE_FLOATING);
g_hdb->_sound->playSound(SND_SPLASH);
}
}
// If it is floating downstream, keep moving it
if (flags & (kFlagPushRight | kFlagPushLeft | kFlagPushUp | kFlagPushDown)) {
int xv = 0, yv = 0;
AIState state = STATE_NONE;
if (flags & kFlagPushRight) {
e->dir = DIR_RIGHT;
xv = 1;
state = STATE_FLOATRIGHT;
} else if (flags & kFlagPushLeft) {
e->dir = DIR_LEFT;
xv = -1;
state = STATE_FLOATLEFT;
} else if (flags & kFlagPushUp) {
e->dir = DIR_UP;
yv = -1;
state = STATE_FLOATUP;
} else if (flags & kFlagPushDown) {
e->dir = DIR_DOWN;
yv = 1;
state = STATE_FLOATDOWN;
}
if (!checkFloating(e->tileX + xv, e->tileY + yv)) {
if ((flags & kFlagAnimFast) == kFlagAnimFast)
e->moveSpeed = kPlayerMoveSpeed << 1;
else if (flags & kFlagAnimMedium)
e->moveSpeed = kPlayerMoveSpeed;
else
e->moveSpeed = kPushMoveSpeed;
setEntityGoal(e, e->tileX + xv, e->tileY + yv);
e->state = state;
} else {
// Landed on a floatmove entity. Make it float really slow and then it'll speed up
uint32 flags2 = g_hdb->_map->getMapBGTileFlags(e->tileX + xv, e->tileY + yv);
if (!(flags2 & (kFlagPushRight | kFlagPushLeft | kFlagPushUp | kFlagPushDown))) {
floatEntity(e, STATE_FLOATING);
e->value1 = 0x666; // Don't move me ever again
return;
}
if (flags & kFlagPushRight) {
e->dir = DIR_RIGHT;
xv = 1;
state = STATE_FLOATRIGHT;
} else if (flags & kFlagPushLeft) {
e->dir = DIR_LEFT;
xv = -1;
state = STATE_FLOATLEFT;
} else if (flags & kFlagPushUp) {
e->dir = DIR_UP;
yv = -1;
state = STATE_FLOATUP;
} else if (flags & kFlagPushDown) {
e->dir = DIR_DOWN;
yv = 1;
state = STATE_FLOATDOWN;
}
e->moveSpeed = kPushMoveSpeed >> 1;
setEntityGoal(e, e->tileX + xv, e->tileY + yv);
e->state = state;
}
}
} else if ((flags & kFlagWater) && (e->type == AI_MAGIC_EGG || e->type == AI_ICE_BLOCK)) {
// And no foreground tile is there
if (g_hdb->_map->getMapFGTileIndex(e->tileX, e->tileY) < 0 && !checkFloating(e->tileX, e->tileY)) {
if ((flags & kFlagSlime) == kFlagSlime) {
// Evaporates in Slime
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_STEAM_PUFF_SIT);
removeEntity(e);
g_hdb->_sound->playSound(SND_SPLASH);
return;
} else {
// Drowns in water
addAnimateTarget(e->x, e->y, 0, 3, ANIM_NORMAL, false, false, GROUP_WATER_SPLASH_SIT);
removeEntity(e);
g_hdb->_sound->playSound(SND_SPLASH);
return;
}
}
}
} else if (onEvenTile(e->x, e->y))
setEntityGoal(e, _waypoints[0].x, _waypoints[0].y);
}
animEntFrames(e);
}
void AI::animEntFrames(AIEntity *e) {
static int click = 0;
int max = 1;
// Set current graphic to draw
switch (e->state) {
case STATE_STANDDOWN:
e->draw = e->standdownGfx[0];
max = 1;
break;
case STATE_STANDUP:
e->draw = e->standupGfx[0];
max = 1;
break;
case STATE_STANDLEFT:
e->draw = e->standleftGfx[0];
max = 1;
break;
case STATE_STANDRIGHT:
e->draw = e->standrightGfx[0];
max = 1;
break;
case STATE_BLINK:
e->draw = e->blinkGfx[e->animFrame];
max = e->blinkFrames;
break;
case STATE_MOVEUP:
e->draw = e->moveupGfx[e->animFrame];
max = e->moveupFrames;
break;
case STATE_MOVEDOWN:
e->draw = e->movedownGfx[e->animFrame];
max = e->movedownFrames;
break;
case STATE_MOVELEFT:
e->draw = e->moveleftGfx[e->animFrame];
max = e->moveleftFrames;
break;
case STATE_MOVERIGHT:
e->draw = e->moverightGfx[e->animFrame];
max = e->moverightFrames;
break;
case STATE_PUSHDOWN:
e->draw = _pushdownGfx[e->animFrame];
max = _pushdownFrames;
break;
case STATE_PUSHUP:
e->draw = _pushupGfx[e->animFrame];
max = _pushupFrames;
break;
case STATE_PUSHLEFT:
e->draw = _pushleftGfx[e->animFrame];
max = _pushleftFrames;
break;
case STATE_PUSHRIGHT:
e->draw = _pushrightGfx[e->animFrame];
max = _pushrightFrames;
break;
case STATE_GRABUP:
e->draw = _getGfx[DIR_UP];
max = 1;
break;
case STATE_GRABDOWN:
e->draw = _getGfx[DIR_DOWN];
max = 1;
break;
case STATE_GRABLEFT:
e->draw = _getGfx[DIR_LEFT];
max = 1;
break;
case STATE_GRABRIGHT:
e->draw = _getGfx[DIR_RIGHT];
max = 1;
break;
case STATE_HORRIBLE1:
e->draw = _horrible1Gfx[e->animFrame];
max = _horrible1Frames;
if (e->animFrame == max - 1)
e->state = STATE_DEAD;
break;
case STATE_HORRIBLE2:
{
e->draw = _horrible2Gfx[e->animFrame];
max = _horrible2Frames;
click++;
if (click == 16) {
g_hdb->_sound->playSound(SND_SHOCKBOT_SHOCK);
click = 0;
}
break;
}
case STATE_HORRIBLE3:
{
e->draw = _horrible3Gfx[e->animFrame];
max = _horrible3Frames;
click++;
if (click == 32) {
g_hdb->_sound->playSound(SND_GUY_GRABBED);
click = 0;
}
break;
}
case STATE_HORRIBLE4:
e->draw = _horrible4Gfx[e->animFrame];
max = _horrible4Frames;
if (e->animFrame == max - 1)
e->state = STATE_DEAD;
break;
case STATE_PLUMMET:
e->draw = _plummetGfx[e->animFrame];
max = _plummetFrames;
if (e->animFrame == max - 1) {
e->state = STATE_NONE;
setPlayerInvisible(true);
}
break;
//
// maintenance bot uses stuff
//
case STATE_USEDOWN:
e->draw = e->standdownGfx[4];
return;
case STATE_USEUP:
e->draw = e->standupGfx[4];
return;
case STATE_USELEFT:
e->draw = e->standleftGfx[4];
return;
case STATE_USERIGHT:
e->draw = e->standrightGfx[4];
return;
//
// death!
//
case STATE_DYING:
e->draw = _dyingGfx[e->animFrame];
max = _dyingFrames;
if (e->animFrame == max - 1)
e->state = STATE_DEAD;
break;
case STATE_DEAD:
e->draw = _dyingGfx[_dyingFrames - 1];
max = _dyingFrames;
break;
case STATE_GOODJOB:
e->draw = _goodjobGfx;
max = 1;
break;
//
// floating stuff uses the "standup" frames for animating the float
//
case STATE_FLOATING:
case STATE_FLOATDOWN:
case STATE_FLOATUP:
case STATE_FLOATLEFT:
case STATE_FLOATRIGHT:
e->draw = e->blinkGfx[e->animFrame];
max = e->blinkFrames;
break;
case STATE_MELTED:
case STATE_EXPLODING:
e->draw = e->special1Gfx[e->animFrame];
max = e->special1Frames;
if (e->type == AI_BOOMBARREL) {
// while exploding, call this function
aiBarrelExplodeSpread(e, 0, 0);
if (e->animFrame == max - 1) {
removeEntity(e);
return;
}
}
break;
//
// ICEPUFF states
//
case STATE_ICEP_PEEK:
e->draw = e->blinkGfx[e->animFrame];
max = e->blinkFrames;
break;
case STATE_ICEP_APPEAR:
e->draw = e->standupGfx[e->animFrame];
max = e->standupFrames;
break;
case STATE_ICEP_THROWDOWN:
e->draw = e->standdownGfx[e->animFrame];
max = e->standdownFrames;
break;
case STATE_ICEP_THROWRIGHT:
e->draw = e->standrightGfx[e->animFrame];
max = e->standrightFrames;
break;
case STATE_ICEP_THROWLEFT:
e->draw = e->standleftGfx[e->animFrame];
max = e->standleftFrames;
break;
case STATE_ICEP_DISAPPEAR:
e->draw = e->special1Gfx[e->animFrame];
max = e->special1Frames;
break;
//
// MEERKAT states
//
case STATE_MEER_MOVE:
e->draw = e->standdownGfx[e->animFrame];
max = e->standdownFrames;
break;
case STATE_MEER_APPEAR:
e->draw = e->standleftGfx[e->animFrame];
max = e->standleftFrames;
break;
case STATE_MEER_BITE:
e->draw = e->standrightGfx[e->animFrame];
max = e->standrightFrames;
break;
case STATE_MEER_DISAPPEAR:
e->draw = e->standupGfx[e->animFrame];
max = e->standupFrames;
break;
case STATE_MEER_LOOK:
e->draw = e->movedownGfx[e->animFrame];
max = e->movedownFrames;
break;
//
// DIVERTER spawning states
//
case STATE_DIVERTER_BL:
e->draw = e->standdownGfx[e->animFrame];
max = e->standdownFrames;
break;
case STATE_DIVERTER_BR:
e->draw = e->standupGfx[e->animFrame];
max = e->standupFrames;
break;
case STATE_DIVERTER_TL:
e->draw = e->standleftGfx[e->animFrame];
max = e->standleftFrames;
break;
case STATE_DIVERTER_TR:
e->draw = e->standrightGfx[e->animFrame];
max = e->standrightFrames;
break;
//
// DOLLY states
// angry[4] = standright[4]
// kissright[4]/kissleft[4] = standleft[8]
// panic[4]/laugh[4] = standdown[8]
// dollyuseright[5] = special1[5]
//
case STATE_ANGRY:
e->draw = e->standrightGfx[e->animFrame];
max = 2;
break;
case STATE_KISSRIGHT:
e->draw = e->standleftGfx[e->animFrame];
max = 4;
break;
case STATE_KISSLEFT:
e->draw = e->standleftGfx[e->animFrame + 4];
max = 4;
break;
case STATE_PANIC:
e->draw = e->standdownGfx[e->animFrame];
max = 2;
break;
case STATE_LAUGH:
e->draw = e->standdownGfx[e->animFrame + 4];
max = 2;
break;
case STATE_DOLLYUSERIGHT:
e->draw = e->special1Gfx[e->animFrame];
max = e->special1Frames;
break;
// SARGE yelling
case STATE_YELL:
e->draw = e->special1Gfx[e->animFrame];
max = e->special1Frames;
break;
default:
debug(9, "AI-FUNCS: animEntFrames: Unintended State for entity %s", AIType2Str(e->type));
break;
}
// Cycle animation frames
if (e->animDelay-- > 0)
return;
e->animDelay = e->animCycle;
e->animFrame++;
if (e->animFrame == max)
e->animFrame = 0;
}
void AI::drawEnts(int x, int y, int w, int h) {
static int stunAnim = 0;
static uint32 stunTimer = g_hdb->getTimeSlice();
int debugFlag = g_hdb->getDebug();
// Draw Floating Entities
for (uint i = 0; i < _floats->size(); i++) {
AIEntity *e = _floats->operator[](i);
if (e->aiDraw)
e->aiDraw(e, x, y);
if ((e->x > x - kTileWidth) && (e->x < x + w) && (e->y > y - kTileHeight) && (e->y < y + h)) {
e->draw->drawMasked(e->x - x + e->drawXOff, e->y - y + e->drawYOff);
e->onScreen = 1;
} else
e->onScreen = 0;
}
// Draw all other Ents
_numLevel2Ents = 0;
for (uint i = 0; i < _ents->size(); i++) {
AIEntity *e = _ents->operator[](i);
debugN(5, "AI::drawEnts: enity %s(%d) state %s(%d)...", AIType2Str(e->type), e->type, AIState2Str(e->state), e->state);
if (e->type == AI_LASER || e->type == AI_DIVERTER) {
if (e->aiDraw) {
if (e->level == 2 && _numLevel2Ents < kMaxLevel2Ents) {
_entsLevel2[_numLevel2Ents].aiDraw = e->aiDraw;
_entsLevel2[_numLevel2Ents].x = x;
_entsLevel2[_numLevel2Ents].y = y;
_entsLevel2[_numLevel2Ents].e = e;
_entsLevel2[_numLevel2Ents].stunnedWait = 0;
_numLevel2Ents++;
debugN(5, "not drawing1...");
} else {
e->aiDraw(e, x, y);
debugN(5, "drawing1...");
}
}
}
if ((e->x > x - kTileWidth) && (e->x < x + w) && (e->y > y - kTileHeight) && (e->y < y + h)) {
// If extra drawing func is present, call it
if (e->aiDraw && e->type != AI_LASER && e->type != AI_DIVERTER) {
if (e->level == 2 && _numLevel2Ents < kMaxLevel2Ents) {
_entsLevel2[_numLevel2Ents].aiDraw = e->aiDraw;
_entsLevel2[_numLevel2Ents].draw = e->draw;
_entsLevel2[_numLevel2Ents].x = x;
_entsLevel2[_numLevel2Ents].y = y;
_entsLevel2[_numLevel2Ents].e = e;
_entsLevel2[_numLevel2Ents].stunnedWait = 0;
_numLevel2Ents++;
debugN(5, "not drawing2...");
} else {
e->aiDraw(e, x, y);
debugN(5, "drawing2...");
}
}
switch (e->type) {
case AI_VORTEXIAN:
if (e->draw)
e->draw->drawMasked(e->x - x + e->drawXOff, e->y - y + e->drawYOff, e->value2 & 0xff);
break;
case AI_GUY: // Draw Player Last
break;
default:
if (e->level == 2 && _numLevel2Ents < kMaxLevel2Ents) {
_entsLevel2[_numLevel2Ents].aiDraw = nullptr;
_entsLevel2[_numLevel2Ents].draw = e->draw;
_entsLevel2[_numLevel2Ents].x = e->x - x + e->drawXOff;
_entsLevel2[_numLevel2Ents].y = e->y - y + e->drawYOff;
_entsLevel2[_numLevel2Ents].e = nullptr;
_entsLevel2[_numLevel2Ents].stunnedWait = e->stunnedWait;
_numLevel2Ents++;
debugN(5, "not trying to draw...");
} else {
debugN(5, "trying to draw...");
if (e->draw) {
debugN(5, "at %d %d", e->x, e->y);
e->draw->drawMasked(e->x - x + e->drawXOff, e->y - y + e->drawYOff);
} else if (debugFlag)
_debugQMark->drawMasked(e->x - x, e->y - y);
else
debugN(5, "no draw function");
if (e->stunnedWait)
g_hdb->_ai->_stunnedGfx[stunAnim]->drawMasked(e->x - x, e->y - y);
}
break;
}
e->onScreen = 1;
} else {
e->onScreen = 0;
debugN(5, "not on screen");
}
debug(5, "%s", ""); // newline
}
if (stunTimer < g_hdb->getTimeSlice()) {
stunAnim = (stunAnim + 1) & 3;
stunTimer = g_hdb->getTimeSlice();
}
// Draw player last
if (_player && _player->level < 2 && !_playerInvisible && _player->draw)
_player->draw->drawMasked(_player->x - x + _player->drawXOff, _player->y - y + _player->drawYOff);
}
void AI::drawLevel2Ents() {
int debugFlag = g_hdb->getDebug();
for (int i = 0; i < _numLevel2Ents; i++) {
// call custom drawing code?
if (_entsLevel2[i].aiDraw) {
debug(5, "AI::drawLevel2Ents: entity %s(%d) at %d,%d", AIType2Str(_entsLevel2[i].e->type), _entsLevel2[i].e->type, _entsLevel2[i].x, _entsLevel2[i].y);
_entsLevel2[i].aiDraw(_entsLevel2[i].e, _entsLevel2[i].x, _entsLevel2[i].y);
} else if (_entsLevel2[i].draw) {
debug(5, "AI::drawLevel2Ents: tile '%s' at %d,%d", _entsLevel2[i].draw->getName(), _entsLevel2[i].x, _entsLevel2[i].y);
_entsLevel2[i].draw->drawMasked(_entsLevel2[i].x, _entsLevel2[i].y);
} else if (debugFlag)
_debugQMark->drawMasked(_entsLevel2[i].x, _entsLevel2[i].y );
if (_entsLevel2[i].stunnedWait)
g_hdb->_ai->_stunnedGfx[_stunAnim]->drawMasked(_entsLevel2[i].x , _entsLevel2[i].y);
}
// always draw the player last
if (_player && _player->level == 2 && !_playerInvisible) {
int x, y;
g_hdb->_map->getMapXY(&x, &y);
if (_player->draw)
_player->draw->drawMasked((_player->x - x) + _player->drawXOff, (_player->y - y) + _player->drawYOff);
}
if (_stunTimer < g_system->getMillis()) {
_stunAnim = (_stunAnim + 1) & 3;
_stunTimer = g_system->getMillis() + 100;
}
}
void AI::animGrabbing() {
if (_player->state == STATE_GRABUP ||
_player->state == STATE_GRABDOWN ||
_player->state == STATE_GRABLEFT ||
_player->state == STATE_GRABRIGHT)
return;
AIState s = STATE_NONE;
switch (_player->dir) {
case DIR_UP:
s = STATE_GRABUP;
_player->draw = _getGfx[DIR_UP];
break;
case DIR_DOWN:
s = STATE_GRABDOWN;
_player->draw = _getGfx[DIR_DOWN];
break;
case DIR_LEFT:
s = STATE_GRABLEFT;
_player->draw = _getGfx[DIR_LEFT];
break;
case DIR_RIGHT:
s = STATE_GRABRIGHT;
_player->draw = _getGfx[DIR_RIGHT];
break;
default:
break;
}
_player->state = s;
_player->animFrame = 5;
}
void AI::entityFace(const char *luaName, int dir) {
AIEntity *e = locateEntity(luaName);
e->dir = (AIDir)dir;
switch (e->dir) {
case DIR_UP:
e->state = STATE_STANDUP;
break;
case DIR_DOWN:
e->state = STATE_STANDDOWN;
break;
case DIR_LEFT:
e->state = STATE_STANDLEFT;
break;
case DIR_RIGHT:
e->state = STATE_STANDRIGHT;
break;
case DIR_NONE:
default:
break;
}
}
void AI::moveEnts() {
static int frameDelay = kAnimFrameDelay;
static bool startLaserSound = false;
if (frameDelay-- > 0)
return;
frameDelay = kAnimFrameDelay;
// Call aiAction for Floating Entities
for (Common::Array<AIEntity *>::iterator it = _floats->begin(); it != _floats->end(); ++it) {
if ((*it)->aiAction)
(*it)->aiAction((*it), 0, 0);
}
// Call aiAction for all other Entities
for (uint i = 0; i < _ents->size(); i++) {
AIEntity *e = _ents->operator[](i);
if (e->aiAction) {
// NPC Touchplate Counter
if (e != _player && e->touchpWait) {
e->touchpWait--;
if (!e->touchpWait) {
if (e->tileX == e->touchpX && e->tileY == e->touchpY && onEvenTile(e->x, e->y))
e->touchpWait = 1;
else {
checkActionList(e, e->touchpX, e->touchpY, false);
g_hdb->_map->setMapBGTileIndex(e->touchpX, e->touchpY, e->touchpTile);
e->touchpX = e->touchpY = e->touchpTile = 0;
}
}
}
// Stunned Entity Timer
if (!e->stunnedWait)
e->aiAction(e, 0, 0);
else if (e->stunnedWait < (int32)g_hdb->getTimeSlice())
e->stunnedWait = 0;
}
}
// if lasers need to rescan, do it here only
if (_laserRescan) {
_laserRescan = false;
laserScan();
}
// handle the constant laser looping sound channel
if (_laserOnScreen)
startLaserSound = true;
if (!_laserOnScreen && startLaserSound) {
startLaserSound = false;
g_hdb->_sound->stopChannel(kLaserChannel);
}
}
bool AI::findPath(AIEntity *e) {
// Initial Pointing Direction to search in
int x = e->tileX;
int y = e->tileY;
ArrowPath *here = findArrowPath(x, y);
// Only look for GO arrows at this first location
if (here && here->type == 1)
e->dir = here->dir;
int xv = 0, yv = 0;
switch (e->dir) {
case DIR_UP:
yv = -1;
break;
case DIR_DOWN:
yv = 1;
break;
case DIR_LEFT:
xv = -1;
break;
case DIR_RIGHT:
xv = 1;
break;
case DIR_NONE:
default:
break;
}
int max;
if (xv)
max = g_hdb->_map->_width;
else
max = g_hdb->_map->_height;
while (max--) {
ArrowPath *arrowPath = findArrowPath(x + xv, y + yv);
if (arrowPath) {
setEntityGoal(e, arrowPath->tileX, arrowPath->tileY);
return true;
} else {
uint32 flags = g_hdb->_map->getMapBGTileFlags(x + xv, y + yv);
if (flags & kFlagSolid)
return false;
}
x += xv;
y += yv;
}
return false;
}
AIEntity *AI::legalMove(int tileX, int tileY, int level, int *result) {
uint32 bgFlags = g_hdb->_map->getMapBGTileFlags(tileX, tileY);
uint32 fgFlags = g_hdb->_map->getMapFGTileFlags(tileX, tileY);
AIEntity *hit = findEntity(tileX, tileY);
if (hit && hit->state != STATE_FLOATING) {
// If player and entity are not at the same level, are they on stairs?
if (hit->level != level) {
if (level == 1 && !(bgFlags & kFlagStairTop))
hit = nullptr;
else if (level == 2 && !(bgFlags & kFlagStairBot))
hit = nullptr;
}
}
if (level == 1) {
if (bgFlags & kFlagSolid) {
*result = 0;
return hit;
}
if (bgFlags & (kFlagWater | kFlagSlime)) {
if (hit && hit->state == STATE_FLOATING) {
*result = 1;
return nullptr;
} else
*result = 0;
return hit;
} else
*result = 1;
} else {
if (fgFlags & kFlagSolid) {
*result = 0;
return hit;
} else if (fgFlags & kFlagGrating) {
*result = 1;
return hit;
} else if (bgFlags & kFlagSolid) {
*result = 0;
return hit;
}
if (bgFlags & (kFlagWater | kFlagSlime | kFlagPlummet)) {
if (hit && hit->state == STATE_FLOATING) {
*result = 1;
return nullptr;
} else
*result = 0;
return hit;
} else
*result = 1;
}
return hit;
}
AIEntity *AI::legalMoveOverWater(int tileX, int tileY, int level, int *result) {
uint32 bgFlags = g_hdb->_map->getMapBGTileFlags(tileX, tileY);
uint32 fgFlags = g_hdb->_map->getMapFGTileFlags(tileX, tileY);
AIEntity *hit = findEntity(tileX, tileY);
if (level == 1 ? (bgFlags & kFlagMonsterBlock) : (!(fgFlags &kFlagGrating) && ((fgFlags & kFlagSolid) || (bgFlags & kFlagMonsterBlock))))
*result = 0;
else
*result = 1;
return hit;
}
AIEntity *AI::legalMoveOverWaterIgnore(int tileX, int tileY, int level, int *result, AIEntity *ignore) {
uint32 bgFlags = g_hdb->_map->getMapBGTileFlags(tileX, tileY);
uint32 fgFlags = g_hdb->_map->getMapFGTileFlags(tileX, tileY);
AIEntity *hit = findEntityIgnore(tileX, tileY, ignore);
if (level == 1 ? (bgFlags & kFlagMonsterBlock) : (!(fgFlags &kFlagGrating) && ((fgFlags & kFlagSolid) || (bgFlags & kFlagMonsterBlock))))
*result = 0;
else
*result = 1;
return hit;
}
AIEntity *AI::playerCollision(int topBorder, int bottomBorder, int leftBorder, int rightBorder) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
AIEntity *e = *it;
if (e == _player || !e->onScreen)
continue;
if (e->x > (_player->x - 32 - leftBorder) && e->x < (_player->x + 32 + rightBorder) && e->y >(_player->y - 32 - topBorder) && e->y < (_player->y + 32 + bottomBorder))
return e;
}
return nullptr;
}
bool AI::checkPlayerTileCollision(int x, int y) {
if (g_hdb->getDebug() == 2)
return false;
return (_player->tileX == x && _player->tileY == y);
}
bool AI::checkPlayerCollision(int x, int y, int border) {
if (g_hdb->getDebug() == 2 || !_player)
return false;
return (x > (_player->x - 32 + border) && x < (_player->x + 32 - border) &&
y > (_player->y - 32 + border) && y < (_player->y + 32 - border));
}
void AI::clearDiverters() {
for (uint i = 0; i < _ents->size(); i++) {
AIEntity *e = _ents->operator[](i);
if (e->type == AI_DIVERTER)
e->value1 = e->value2 = 0;
}
}
void AI::laserScan() {
clearDiverters();
g_hdb->_map->clearLaserBeams();
for (uint i = 0; i < _ents->size(); i++) {
AIEntity *e = _ents->operator[](i);
if (e->type == AI_LASER)
aiLaserAction(e, 0, 0);
}
}
void AI::floatEntity(AIEntity *e, AIState state) {
for (Common::Array<AIEntity *>::iterator it = _ents->begin(); it != _ents->end(); ++it) {
if (e == *it) {
_floats->push_back(*it);
_ents->erase(it);
break;
}
}
e->state = state;
e->level = 1;
}
bool AI::checkFloating(int x, int y) {
for (Common::Array<AIEntity *>::iterator it = _floats->begin(); it != _floats->end(); ++it) {
if ((*it)->tileX == x && (*it)->tileY == y)
return true;
}
return false;
}
// Check to see if we can get this entity
bool AI::getTableEnt(AIType type) {
switch (type) {
case ITEM_CELL:
case ITEM_ENV_WHITE:
case ITEM_ENV_RED:
case ITEM_ENV_BLUE:
case ITEM_ENV_GREEN:
case ITEM_TRANSCEIVER:
case ITEM_CLUB:
case ITEM_ROBOSTUNNER:
case ITEM_SLUGSLINGER:
case ITEM_MONKEYSTONE:
case ITEM_GOO_CUP:
case ITEM_TEACUP:
case ITEM_BURGER:
case ITEM_PDA:
case ITEM_BOOK:
case ITEM_CLIPBOARD:
case ITEM_NOTE:
case ITEM_KEYCARD_WHITE:
case ITEM_KEYCARD_BLUE:
case ITEM_KEYCARD_RED:
case ITEM_KEYCARD_GREEN:
case ITEM_KEYCARD_PURPLE:
case ITEM_KEYCARD_BLACK:
case ITEM_SEED:
case ITEM_SODA:
case ITEM_SLICER:
case ITEM_DOLLYTOOL1:
case ITEM_DOLLYTOOL2:
case ITEM_DOLLYTOOL3:
case ITEM_DOLLYTOOL4:
return true;
default:
return false;
}
}
// Check to see if it's okay to move through this entity
bool AI::walkThroughEnt(AIType type) {
switch (type) {
case AI_VORTEXIAN:
case AI_MEERKAT:
case AI_GOODFAIRY:
case AI_BADFAIRY:
case AI_GATEPUDDLE:
case AI_BUZZFLY:
case AI_OMNIBOT:
case AI_PUSHBOT:
case AI_TURNBOT:
case AI_RIGHTBOT:
case ITEM_GEM_WHITE:
case ITEM_GEM_BLUE:
case ITEM_GEM_RED:
case ITEM_GEM_GREEN:
return true;
default:
return getTableEnt(type);
}
}
// Play special sound for every item you get
void AI::getItemSound(AIType type) {
switch (type) {
case ITEM_GOO_CUP:
g_hdb->_sound->playSound(SND_GET_GOO);
break;
case ITEM_GEM_WHITE:
case ITEM_GEM_BLUE:
case ITEM_GEM_RED:
case ITEM_GEM_GREEN:
g_hdb->_sound->playSound(SND_GET_GEM);
break;
case ITEM_CLUB:
g_hdb->_sound->playSound(SND_GET_CLUB);
break;
case ITEM_SLUGSLINGER:
g_hdb->_sound->playSound(SND_GET_SLUG);
break;
case ITEM_ROBOSTUNNER:
g_hdb->_sound->playSound(SND_GET_STUNNER);
break;
case ITEM_CELL:
case ITEM_TRANSCEIVER:
case ITEM_TEACUP:
case ITEM_COOKIE:
case ITEM_BURGER:
case ITEM_PDA:
case ITEM_BOOK:
case ITEM_CLIPBOARD:
case ITEM_NOTE:
case ITEM_CABKEY:
case ITEM_DOLLYTOOL1:
case ITEM_DOLLYTOOL2:
case ITEM_DOLLYTOOL3:
case ITEM_DOLLYTOOL4:
case ITEM_SEED:
case ITEM_SODA:
case ITEM_ROUTER:
case ITEM_SLICER:
case ITEM_CHICKEN:
case ITEM_PACKAGE:
case ITEM_ENV_RED:
case ITEM_ENV_BLUE:
case ITEM_ENV_GREEN:
if (!g_hdb->isPPC()) {
if (g_hdb->_sound->getVoiceStatus())
g_hdb->_sound->playVoice(GUY_GOT_SOMETHING, 1);
else
g_hdb->_sound->playSound(SND_GET_THING);
break;
}
// fall through
default:
g_hdb->_sound->playSound(SND_GET_THING);
}
}
void AI::lookAtEntity(AIEntity *e) {
lookAtXY(e->tileX, e->tileY);
}
// Change player direction to XY
void AI::lookAtXY(int x, int y) {
int distX = abs(_player->tileX - x);
int distY = abs(_player->tileY - y);
if (distX > distY) {
// X takes precedence
if (x < _player->tileX)
_player->dir = DIR_LEFT;
else if (x > _player->tileX)
_player->dir = DIR_RIGHT;
else if (y < _player->tileY)
_player->dir = DIR_UP;
else
_player->dir = DIR_DOWN;
} else {
// Y takes precedence
if (y < _player->tileY)
_player->dir = DIR_UP;
else if (y > _player->tileY)
_player->dir = DIR_DOWN;
else if (x < _player->tileX)
_player->dir = DIR_LEFT;
else
_player->dir = DIR_RIGHT;
}
switch (_player->dir) {
case DIR_UP:
_player->state = STATE_STANDUP;
_player->draw = _getGfx[DIR_UP];
break;
case DIR_DOWN:
_player->state = STATE_STANDDOWN;
_player->draw = _getGfx[DIR_DOWN];
break;
case DIR_LEFT:
_player->state = STATE_STANDLEFT;
_player->draw = _getGfx[DIR_LEFT];
break;
case DIR_RIGHT:
_player->state = STATE_STANDRIGHT;
_player->draw = _getGfx[DIR_RIGHT];
break;
default:
break;
}
}
void AI::movePlayer(uint16 buttons) {
static const AIState stateList[] = {
STATE_ATK_CLUB_UP, STATE_ATK_CLUB_DOWN, STATE_ATK_CLUB_LEFT, STATE_ATK_CLUB_RIGHT,
STATE_ATK_STUN_UP, STATE_ATK_STUN_DOWN, STATE_ATK_STUN_LEFT, STATE_ATK_STUN_RIGHT,
STATE_ATK_SLUG_UP, STATE_ATK_SLUG_DOWN, STATE_ATK_SLUG_LEFT, STATE_ATK_SLUG_RIGHT,
STATE_PUSHUP, STATE_PUSHDOWN, STATE_PUSHLEFT, STATE_PUSHRIGHT,
STATE_GRABUP, STATE_GRABDOWN, STATE_GRABLEFT, STATE_GRABRIGHT
};
static const int xva[5] = {9, 0, 0,-1, 1};
static const int yva[5] = {9,-1, 1, 0, 0};
if (!_player)
return;
// If we're already attacking, don't do anything else
for (int i = 0; i < 20; i++) {
if (_player->state == stateList[i])
return;
}
// Just trying to put away a dialog?
if (buttons & kButtonB) {
if (g_hdb->isPPC()) {
if (g_hdb->_window->deliveriesActive()) {
g_hdb->_window->closeDlvs();
return;
} else if (g_hdb->_window->inventoryActive()) {
g_hdb->_window->closeInv();
return;
}
}
if (g_hdb->_window->dialogActive()) {
g_hdb->_window->closeDialog();
return;
} else if (g_hdb->_window->dialogChoiceActive()) {
g_hdb->_window->closeDialogChoice();
return;
} else if (g_hdb->_window->msgBarActive()) {
g_hdb->_window->closeMsg();
return;
}
if (cinematicsActive() || _playerLock)
return;
// Are we trying to use something? An ACTION, AUTO, LUA?
int nx = _player->tileX + xva[_player->dir];
int ny = _player->tileY + yva[_player->dir];
AIEntity *hit = findEntity(nx, ny);
if (hit && hit->type == AI_NONE) {
AIEntity *temp = g_hdb->_ai->findEntityIgnore(nx, ny, hit);
if (temp)
hit = temp;
}
// the reason to check for no entity or an AI_NONE is because
// there's a possibility that an actual entity and a LUA entity
// can share the same spot, so we need to be able to deal with
// the real entity first, then the LUA entity.
if (!hit || hit->type == AI_NONE) {
switch (_player->state) {
case STATE_STANDUP:
case STATE_STANDDOWN:
case STATE_STANDLEFT:
case STATE_STANDRIGHT:
if (checkForTouchplate(nx, ny))
break;
if (checkActionList(_player, nx, ny, true))
return;
if (checkAutoList(_player, nx, ny))
return;
if (checkLuaList(_player, nx, ny))
return;
default:
break;
}
}
// Attackable Entity? (we're right up on it)
int amt = getGemAmount();
bool attackable = false;
if (hit)
switch (hit->type) {
case AI_OMNIBOT:
case AI_TURNBOT:
case AI_SHOCKBOT:
case AI_RIGHTBOT:
case AI_PUSHBOT:
case AI_LISTENBOT:
case AI_MAINTBOT:
case AI_DEADEYE:
case AI_MEERKAT:
case AI_FATFROG:
case AI_GOODFAIRY:
case AI_BADFAIRY:
case AI_ICEPUFF:
case AI_BUZZFLY:
case AI_DRAGON:
case AI_NONE:
attackable = true;
break;
default:
break;
}
if (g_hdb->getActionMode() && ((hit && attackable) || !hit)) {
// Attack
if (_weaponSelected != AI_NONE && onEvenTile(_player->x, _player->y)) {
switch (_weaponSelected) {
case ITEM_CLUB: {
AIState club[5] = {STATE_NONE, STATE_ATK_CLUB_UP, STATE_ATK_CLUB_DOWN, STATE_ATK_CLUB_LEFT, STATE_ATK_CLUB_RIGHT};
_player->state = club[_player->dir];
_player->animFrame = 0;
_player->animDelay = _player->animCycle;
g_hdb->_sound->playSound(SND_CLUB_MISS);
}
break;
case ITEM_ROBOSTUNNER: {
// it costs 1 gem to attack!
if (!amt) {
g_hdb->_sound->playSound(SND_CELLHOLDER_USE_REJECT);
g_hdb->_window->openMessageBar("Recharging...", 1);
setGemAmount(1);
return;
}
setGemAmount(amt - 1);
AIState stun[5] = {STATE_NONE, STATE_ATK_STUN_UP, STATE_ATK_STUN_DOWN, STATE_ATK_STUN_LEFT, STATE_ATK_STUN_RIGHT};
_player->state = stun[_player->dir];
_player->animFrame = 0;
_player->animDelay = _player->animCycle;
_player->sequence = 1;
}
break;
case ITEM_SLUGSLINGER: {
// it costs 1 gem to attack!
if (!amt) {
g_hdb->_sound->playSound(SND_CELLHOLDER_USE_REJECT);
g_hdb->_window->openMessageBar("Recharging...", 1);
setGemAmount(1);
return;
}
setGemAmount(amt - 1);
AIState slug[5] = {STATE_NONE, STATE_ATK_SLUG_UP, STATE_ATK_SLUG_DOWN, STATE_ATK_SLUG_LEFT, STATE_ATK_SLUG_RIGHT};
_player->state = slug[_player->dir];
_player->animFrame = 0;
_player->animDelay = _player->animCycle;
spawn(AI_SLUG_ATTACK, _player->dir, _player->tileX, _player->tileY,
nullptr, nullptr, nullptr, DIR_NONE, _player->level, 0, 0, 1);
}
break;
default:
break;
} // switch
}
return;
}
// Puzzle Mode - throw a gem
// If this is the last gem, throw it and signal that it should come back
if (amt && (attackable || !hit)) {
int xv = xva[_player->dir];
int yv = yva[_player->dir];
nx = _player->tileX + xv;
ny = _player->tileY + yv;
spawn(AI_GEM_ATTACK, _player->dir, nx, ny, nullptr, nullptr, nullptr, DIR_NONE, _player->level, amt == 1, 0, 1);
setGemAmount(amt - 1);
animGrabbing();
return;
}
// Are we trying to use this entity?
if (hit) {
g_hdb->useEntity(hit);
return;
}
} // if kButtonB
if (!onEvenTile(_player->x, _player->y))
return;
// Is the player dead or move-locked?
if (_player->state == STATE_DEAD || _playerLock || _playerEmerging)
return;
// Are we on a touchplate and trying to move within the waiting period
if (_player->touchpWait > kPlayerTouchPWait / 4)
return;
if (g_hdb->isPPC()) {
// Are the Deliveries active?
if (g_hdb->_window->deliveriesActive())
if (!g_hdb->_ai->cinematicsActive())
return;
}
// Is a dialog active?
if (g_hdb->_window->dialogActive()) {
if (!cinematicsActive())
return;
}
// is a choice dialog active?
if (g_hdb->_window->dialogChoiceActive()) {
if (!cinematicsActive())
return;
}
if (g_hdb->isPPC()) {
// Is the Inventory active?
if (g_hdb->_window->inventoryActive())
if (!g_hdb->_ai->cinematicsActive())
return;
}
// In a cinematic?
if (_playerLock || _numWaypoints)
return;
int xv = 0, yv = 0;
if (buttons & kButtonUp)
yv = -1;
else if (buttons & kButtonDown)
yv = 1;
else if (buttons & kButtonLeft)
xv = -1;
else if (buttons & kButtonRight)
xv = 1;
else if (buttons & kButtonB) {
playerUse();
return;
}
// Check if we can move there
int nx = _player->tileX + xv;
if (!nx) // Don't allow moving to X-coordinate 0
return;
int ny = _player->tileY + yv;
int moveOK;
AIEntity *hit = legalMove(nx, ny, _player->level, &moveOK);
if (hit && walkThroughEnt(hit->type))
hit = nullptr;
if (hit || !moveOK) {
lookAtXY(nx, ny);
stopEntity(_player);
return;
}
// Walk into Lua Entity?
if (checkLuaList(_player, nx, ny))
return;
if (buttons & (kButtonUp | kButtonDown | kButtonLeft | kButtonRight)) {
int temp = _player->animFrame;
if (_player->state != STATE_MOVELEFT && _player->state != STATE_MOVERIGHT && _player->state != STATE_MOVEUP && _player->state != STATE_MOVEDOWN)
temp = 0;
setEntityGoal(_player, nx, ny);
_player->animFrame = temp;
} else
setEntityGoal(_player, nx, ny);
}
void AI::playerUse() {
static const int xv[5] = {9, 0, 0,-1, 1};
static const int yv[5] = {9,-1, 1, 0, 0};
g_hdb->setTargetXY(kTileWidth * (_player->tileX + xv[_player->dir]), kTileWidth * (_player->tileY + yv[_player->dir]));
}
} // End of Namespace
|