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 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
|
/* 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 "asylum/views/scene.h"
#include "asylum/resources/actor.h"
#include "asylum/resources/encounters.h"
#include "asylum/resources/inventory.h"
#include "asylum/resources/object.h"
#include "asylum/resources/polygons.h"
#include "asylum/resources/script.h"
#include "asylum/resources/special.h"
#include "asylum/resources/worldstats.h"
#include "asylum/system/cursor.h"
#include "asylum/system/graphics.h"
#include "asylum/system/savegame.h"
#include "asylum/system/screen.h"
#include "asylum/system/speech.h"
#include "asylum/system/text.h"
#include "asylum/views/menu.h"
#include "asylum/views/scenetitle.h"
#include "asylum/asylum.h"
#include "asylum/respack.h"
#include "asylum/staticres.h"
namespace Asylum {
#define SCREEN_EDGES 40
#define SCROLL_STEP 10
int g_debugActors;
int g_debugObjects;
int g_debugPolygons;
int g_debugPolygonIndex;
int g_debugSceneRects;
int g_debugScrolling;
Scene::Scene(AsylumEngine *engine): _vm(engine),
_polygons(nullptr), _ws(nullptr) {
// Initialize data
_packId = kResourcePackInvalid;
_hitAreaChapter7Counter = 0;
_isCTRLPressed = false;
_chapter5RainFrameIndex = 0;
_musicVolume = 0;
_frameCounter = 0;
_savedScreen.create(640, 480, Graphics::PixelFormat::createFormatCLUT8());
_debugShowVersion = false;
g_debugActors = 0;
g_debugObjects = 0;
g_debugPolygons = 0;
g_debugPolygonIndex = 0;
g_debugSceneRects = 0;
g_debugScrolling = 0;
}
Scene::~Scene() {
// Unload the associated resources
getResource()->unload(_packId);
// Clear script queue
getScript()->reset();
_savedScreen.free();
delete _polygons;
delete _ws;
}
void Scene::enter(ResourcePackId packId) {
_vm->setGameFlag(kGameFlagScriptProcessing);
getCursor()->hide();
getSharedData()->setPlayerIndex(0);
// Load the scene data
load(packId);
// Set wheel indices
_ws->setWheelObjects();
// Adjust object priority
if (_ws->objects.size() > 0) {
int32 priority = 4091;
for (uint32 i = 0; i < _ws->objects.size(); i++) {
Object *object = _ws->objects[i];
object->setPriority(priority);
object->flags &= ~kObjectFlagC000;
priority -= 4;
}
}
// Set the cursor to magnifying glass
getCursor()->set(_ws->cursorResources[kCursorResourceMagnifyingGlass], 0, kCursorAnimationNone);
getCursor()->show();
// Clear the graphic queue
getScreen()->clearGraphicsInQueue();
_ws->sceneRectIdx = 0;
_ws->motionStatus = 1;
// Update current player bounding rectangle
Actor *player = getActor();
Common::Rect *boundingRect = player->getBoundingRect();
boundingRect->bottom = (int16)player->getPoint2()->y;
boundingRect->right = (int16)(player->getPoint2()->x * 2);
// Adjust scene bounding rect
_ws->boundingRect = Common::Rect(195, 115, 445 - boundingRect->right, 345 - boundingRect->bottom);
player->show(); // flag |= 1 (show actor)
player->enable();
// Update current player coordinates
player->getPoint1()->x -= player->getPoint2()->x;
player->getPoint1()->y -= player->getPoint2()->y;
// Update all other actors
if (_ws->actors.size() > 1) {
for (uint32 i = 1; i < _ws->actors.size(); i++) {
Actor *actor = _ws->actors[i];
actor->show();
actor->setDirection(kDirectionNW);
actor->enable();
actor->getPoint1()->x -= actor->getPoint2()->x;
actor->getPoint1()->y -= actor->getPoint2()->y;
actor->getBoundingRect()->bottom = (int16)actor->getPoint2()->y;
actor->getBoundingRect()->right = (int16)(2 * actor->getPoint2()->x);
}
}
// Queue scene script
if (_ws->scriptIndex)
getScript()->queueScript(_ws->scriptIndex, 0);
// Clear the graphic queue (FIXME: not sure why we need to do this again)
getScreen()->clearGraphicsInQueue();
// Load transparency tables
getScreen()->setupTransTables(3, _ws->cellShadeMask1, _ws->cellShadeMask2, _ws->cellShadeMask3);
getScreen()->selectTransTable(1);
// Setup font
getText()->loadFont(_ws->font1);
// Preload graphics (we are just showing the loading screen
preload();
// Play scene intro dialog
playIntroSpeech();
// Set actor type
_ws->actorType = actorType[_ws->chapter];
// Play intro music
if (_ws->musicCurrentResourceIndex != kMusicStopped && _ws->chapter != kChapter1)
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, _ws->musicCurrentResourceIndex));
else
getSound()->playMusic(kResourceNone, 0);
// Update global values
_vm->lastScreenUpdate = 1;
getSharedData()->setFlag(kFlagScene1, true);
player->setLastScreenUpdate(_vm->screenUpdateCount);
player->enable();
if (_ws->chapter == kChapter9) {
changePlayer(1);
_ws->nextPlayer = kActorInvalid;
}
}
void Scene::enterLoad() {
if (!_ws)
error("[Scene::enterLoad] WorldStats not initialized properly");
_vm->setGameFlag(kGameFlagScriptProcessing);
getScreen()->clearGraphicsInQueue();
// Setup scene bounding rect
_ws->boundingRect.left = 195;
_ws->boundingRect.top = 115;
_ws->boundingRect.right = 445 - getActor()->getBoundingRect()->right;
_ws->boundingRect.bottom = 345 - getActor()->getBoundingRect()->bottom;
// Setup transparency table
getScreen()->setupTransTables(3, _ws->cellShadeMask1, _ws->cellShadeMask2, _ws->cellShadeMask3);
getScreen()->selectTransTable(1);
getText()->loadFont(_ws->font1);
preload();
// Adjust object priority
if (_ws->objects.size() > 0) {
int32 priority = 4091;
for (uint32 i = 0; i < _ws->objects.size(); i++) {
Object *object = _ws->objects[i];
object->setPriority(priority);
object->flags &= ~kObjectFlagC000;
priority -= 4;
}
}
// Play intro music
if (_ws->musicCurrentResourceIndex != kMusicStopped)
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, _ws->musicCurrentResourceIndex));
else
getSound()->playMusic(kResourceNone, 0);
// Palette fade
getScreen()->paletteFade(0, 75, 8);
getScreen()->clear();
getSharedData()->setFlag(kFlagScene1, true);
_vm->lastScreenUpdate = 1;
getActor()->setLastScreenUpdate(_vm->screenUpdateCount);
}
void Scene::load(ResourcePackId packId) {
// Setup resource manager
_packId = packId;
getResource()->setMusicPackId(packId);
char filename[10];
snprintf(filename, 10, SCENE_FILE_MASK, _packId);
char sceneTag[6];
Common::File *fd = new Common::File;
if (!Common::File::exists(filename))
error("Scene file doesn't exist %s", filename);
fd->open(filename);
if (!fd->isOpen())
error("Failed to load scene file %s", filename);
fd->read(sceneTag, 6);
if (Common::String(sceneTag, 6) != "DFISCN")
error("The file isn't recognized as scene %s", filename);
_ws = new WorldStats(_vm);
_ws->load(fd);
if (_vm->checkGameVersion("Demo"))
fd->seek(0x1D72E, SEEK_SET);
_polygons = new Polygons(fd);
if (_vm->checkGameVersion("Demo"))
fd->seek(3 * 4, SEEK_CUR);
ScriptManager *script = getScript();
script->resetAll();
script->load(fd);
fd->close();
delete fd;
getSharedData()->resetAmbientFlags();
_ws->field_120 = -1;
int32 tick = _vm->getTick();
for (uint32 a = 0; a < _ws->actors.size(); a++)
_ws->actors[a]->setLastScreenUpdate(tick);
getCursor()->show();
}
//////////////////////////////////////////////////////////////////////////
// Event handling
//////////////////////////////////////////////////////////////////////////
bool Scene::handleEvent(const AsylumEvent &evt) {
switch ((int32)evt.type) {
default:
break;
case EVENT_ASYLUM_INIT:
return init();
case EVENT_ASYLUM_ACTIVATE:
activate();
break;
case EVENT_ASYLUM_UPDATE:
return update();
case Common::EVENT_CUSTOM_ENGINE_ACTION_START:
return actionDown((AsylumAction)evt.customType);
case Common::EVENT_CUSTOM_ENGINE_ACTION_END:
return actionUp((AsylumAction)evt.customType);
case Common::EVENT_KEYDOWN:
if (evt.kbd.flags & Common::KBD_CTRL)
_isCTRLPressed = true;
return key(evt);
case Common::EVENT_KEYUP:
if (!(evt.kbd.flags & Common::KBD_CTRL))
_isCTRLPressed = false;
break;
case Common::EVENT_LBUTTONDOWN:
case Common::EVENT_RBUTTONDOWN:
return getCursor()->isHidden() ? false : clickDown(evt);
case Common::EVENT_RBUTTONUP:
_rightButtonDown = false;
activate();
break;
}
return false;
}
void Scene::activate() {
Actor *player = getActor();
if (player->getStatus() == kActorStatusWalking)
player->changeStatus(kActorStatusEnabled);
if (player->getStatus() == kActorStatusWalking2)
player->changeStatus(kActorStatusEnabled2);
}
bool Scene::init() {
if (!_ws)
error("[Scene::init] WorldStats not initialized properly");
if (getSharedData()->getFlag((kFlag3))) { // this flag is set during an encounter
getSharedData()->setFlag(kFlag3, false);
// The original test for flag 1001 but doesn't use the result
return true;
}
getCursor()->set(_ws->cursorResources[kCursorResourceScrollUp], 0, kCursorAnimationNone);
_ws->coordinates[0] = -1;
getScreen()->clear();
getText()->loadFont(_ws->font1);
ResourceId paletteResource = _ws->actions[getActor()->getActionIndex3()]->paletteResourceId;
if (!paletteResource)
paletteResource = _ws->currentPaletteId;
getScreen()->setPalette(paletteResource);
getScreen()->setGammaLevel(paletteResource);
getScreen()->loadGrayPalette();
getScreen()->setupTransTables(3, _ws->cellShadeMask1, _ws->cellShadeMask2, _ws->cellShadeMask3);
getScreen()->selectTransTable(1);
getCursor()->show();
return true;
}
bool Scene::update() {
if (getEncounter()->shouldEnablePlayer()) {
getEncounter()->setShouldEnablePlayer(false);
// Enable player
getActor()->changeStatus(kActorStatusEnabled);
}
uint32 ticks = _vm->getTick();
if (!getSharedData()->getFlag(kFlagRedraw)) {
if (updateScreen())
return true;
getSharedData()->setFlag(kFlagRedraw, true);
}
if (ticks > getSharedData()->getNextScreenUpdate()) {
if (getSharedData()->getFlag(kFlagRedraw)) {
if (getSharedData()->getMatteBarHeight() <= 0)
getScreen()->copyBackBufferToScreen();
else
getEncounter()->drawScreen();
// Original also sets an unused value to 0
getSharedData()->setEventUpdate(getSharedData()->getEventUpdate() ^ 1);
getSharedData()->setFlag(kFlagRedraw, false);
getSharedData()->setNextScreenUpdate(ticks + 55 / Config.animationsSpeed);
++_vm->screenUpdateCount;
}
}
return true;
}
bool Scene::actionDown(AsylumAction a) {
Actor *player = getActor();
switch (a) {
case kAsylumActionShowVersion:
_debugShowVersion = !_debugShowVersion;
break;
case kAsylumActionQuickLoad:
if (!_vm->checkGameVersion("Demo"))
getSaveLoad()->quickLoad();
break;
case kAsylumActionQuickSave:
if (!_vm->checkGameVersion("Demo"))
getSaveLoad()->quickSave();
break;
case kAsylumActionSwitchToSarah:
case kAsylumActionSwitchToGrimwall:
case kAsylumActionSwitchToOlmec:
if (getCursor()->isHidden() || _ws->chapter != kChapter9)
return true;
getScript()->queueScript(_ws->actions[_ws->getActionAreaIndexById(2206 + a - kAsylumActionSwitchToSarah)]->scriptIndex,
getSharedData()->getPlayerIndex());
break;
case kAsylumActionOpenInventory:
if (getActor()->inventory[0] && getActor()->getStatus() == kActorStatusEnabled && !getActor()->inventory.getSelectedItem()) {
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 2));
getActor()->changeStatus(kActorStatusShowingInventory);
} else if (getActor()->getStatus() == kActorStatusShowingInventory || getActor()->getStatus() == kActorStatus10) {
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 5));
getActor()->changeStatus(kActorStatusEnabled);
}
break;
case kAsylumActionShowMenu:
if (getSpeech()->getSoundResourceId()) {
getScene()->stopSpeech();
} else {
if (getCursor()->isHidden())
break;
if (!_vm->checkGameVersion("Demo")) {
_savedScreen.copyFrom(*getScreen()->getSurface());
memcpy(_savedPalette, getScreen()->getPalette(), sizeof(_savedPalette));
_vm->switchEventHandler(_vm->menu());
}
}
break;
case kAsylumActionMoveUp:
if (player->getStatus() != kActorStatusDisabled) {
player->changeStatus(kActorStatusWalking);
}
_keyState |= kWalkUp;
break;
case kAsylumActionMoveDown:
if (player->getStatus() != kActorStatusDisabled) {
player->changeStatus(kActorStatusWalking);
}
_keyState |= kWalkDown;
break;
case kAsylumActionMoveLeft:
if (player->getStatus() != kActorStatusDisabled) {
player->changeStatus(kActorStatusWalking);
}
_keyState |= kWalkLeft;
break;
case kAsylumActionMoveRight:
if (player->getStatus() != kActorStatusDisabled) {
player->changeStatus(kActorStatusWalking);
}
_keyState |= kWalkRight;
break;
default:
break;
}
return true;
}
bool Scene::actionUp(AsylumAction a) {
byte lastKeyState = _keyState;
switch (a) {
case kAsylumActionMoveUp:
_keyState &= ~kWalkUp;
break;
case kAsylumActionMoveDown:
_keyState &= ~kWalkDown;
break;
case kAsylumActionMoveLeft:
_keyState &= ~kWalkLeft;
break;
case kAsylumActionMoveRight:
_keyState &= ~kWalkRight;
break;
default:
break;
}
if (lastKeyState && !_keyState)
activate();
return true;
}
bool Scene::key(const AsylumEvent &evt) {
if (!_ws)
error("[Scene::key] WorldStats not initialized properly");
switch (evt.kbd.keycode) {
default:
break;
case Common::KEYCODE_BACKSPACE:
// TODO add support for debug commands
warning("[Scene::key] debug command handling not implemented!");
break;
case Common::KEYCODE_RETURN:
// TODO add support for debug commands
warning("[Scene::key] debug command handling not implemented!");
break;
case Common::KEYCODE_LEFTBRACKET:
if (evt.kbd.ascii != 123)
break;
// fallthrough
case Common::KEYCODE_p:
case Common::KEYCODE_q:
case Common::KEYCODE_r:
case Common::KEYCODE_s:
case Common::KEYCODE_t:
case Common::KEYCODE_u:
case Common::KEYCODE_v:
case Common::KEYCODE_w:
case Common::KEYCODE_x:
case Common::KEYCODE_y:
case Common::KEYCODE_z:
if (speak(evt.kbd.keycode)) {
_vm->lastScreenUpdate = _vm->screenUpdateCount;
getActor()->setLastScreenUpdate(_vm->screenUpdateCount);
}
break;
}
return true;
}
bool Scene::clickDown(const AsylumEvent &evt) {
if (g_debugScrolling) {
g_debugScrolling = 0;
getActor()->setPosition(_ws->xLeft + evt.mouse.x, _ws->yTop + evt.mouse.y, getActor()->getDirection(), getActor()->getFrameIndex());
return true;
}
_vm->lastScreenUpdate = 0;
if (getSharedData()->getFlag(kFlag2)) {
stopSpeech();
return true;
}
Actor *player = getActor();
player->setLastScreenUpdate(_vm->screenUpdateCount);
switch (evt.type) {
default:
break;
case Common::EVENT_RBUTTONDOWN:
if (getSpeech()->getSoundResourceId())
stopSpeech();
if (player->getStatus() == kActorStatusShowingInventory || player->getStatus() == kActorStatus10) {
player->changeStatus(kActorStatusEnabled);
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 5));
} else if (player->getStatus() != kActorStatusDisabled) {
player->changeStatus(kActorStatusWalking);
}
_rightButtonDown = true;
break;
case Common::EVENT_LBUTTONDOWN:
if (_rightButtonDown || _keyState)
break;
if (getSpeech()->getSoundResourceId())
stopSpeech();
if (player->getStatus() == kActorStatusDisabled)
break;
if (player->inventory.getSelectedItem()) {
if (hitTestPlayer()) {
player->inventory.selectItem(0);
return true;
}
HitType type = kHitNone;
int32 res = hitTestScene(type);
if (res == -1)
getSpeech()->playIndexed(2);
else
handleHit(res, type);
return true;
}
if (!hitTestPlayer() || player->getStatus() >= kActorStatus11 || !player->inventory[0]) {
if (player->getStatus() == kActorStatusShowingInventory || player->getStatus() == kActorStatus10) {
clickInventory();
} else {
HitType type = kHitNone;
int32 res = hitTest(type);
if (res != -1)
handleHit(res, type);
}
return true;
}
if (player->getStatus() == kActorStatusShowingInventory || player->getStatus() == kActorStatus10) {
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 5));
player->changeStatus(kActorStatusEnabled);
} else {
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 2));
player->changeStatus(kActorStatusShowingInventory);
}
break;
}
return true;
}
//////////////////////////////////////////////////////////////////////////
// Scene update
//////////////////////////////////////////////////////////////////////////
bool Scene::updateScreen() {
_frameCounter++;
if (updateScene())
return true;
#if 0
if (Config.performance <= 4) {
// TODO: implement skip drawing frames to screen
} else {
#endif
if (drawScene())
return true;
//}
getActor()->drawNumber();
// Original handle all debug commands here (we do it as part of each update command)
if (_debugShowVersion) {
getText()->setPosition(Common::Point(0, 0));
getText()->loadFont(_ws->font1);
getText()->draw(Common::String::format("Version %s / Build %d",
getSaveLoad()->getVersion(),
getSaveLoad()->getBuild()).c_str());
}
if (getSharedData()->getFlag(kFlagScene1)) {
getScreen()->clear();
getScreen()->stopPaletteFade(0, 0, 0);
updateScene();
drawScene();
getScreen()->copyBackBufferToScreen();
getScreen()->stopPaletteFadeAndSet(getWorld()->currentPaletteId, 100, 10);
drawScene();
getScreen()->copyBackBufferToScreen();
getSharedData()->setFlag(kFlagScene1, false);
}
if (getSpeech()->getSoundResourceId() != 0) {
if (getSound()->isPlaying(getSpeech()->getSoundResourceId())) {
getSpeech()->prepareSpeech();
} else {
getSpeech()->resetResourceIds();
_vm->clearGameFlag(kGameFlag219);
}
}
if (getWorld()->chapter == kChapter5) {
if (_vm->isGameFlagSet(kGameFlag249))
drawRain();
}
return false;
}
bool Scene::updateScene() {
#ifdef DEBUG_SCENE_TIMES
#define MEASURE_TICKS(func) { \
int32 startTick =_vm->getTick(); \
func(); \
debugC(kDebugLevelScene, "[Scene] " #func " - Time: %d", _vm->getTick() - startTick); \
}
#else
#define MEASURE_TICKS(func) func();
#endif
// Update each part of the scene
if (getSharedData()->getMatteBarHeight() != 170 || getSharedData()->getMattePlaySound()) {
MEASURE_TICKS(updateMouse);
MEASURE_TICKS(updateActors);
MEASURE_TICKS(updateObjects);
MEASURE_TICKS(updateAmbientSounds);
MEASURE_TICKS(updateMusic);
MEASURE_TICKS(updateAdjustScreen);
}
return getScript()->process();
}
void Scene::updateMouse() {
Actor *player = getActor();
Common::Point mouse = getCursor()->position();
Common::Point pt;
player->adjustCoordinates(&pt);
Common::Rect actorRect;
if (_ws->chapter != kChapter2 || getSharedData()->getPlayerIndex() != 10) {
actorRect.left = pt.x + 20;
actorRect.top = pt.y;
actorRect.right = (int16)(pt.x + 2 * player->getPoint2()->x);
actorRect.bottom = (int16)(pt.y + player->getPoint2()->y);
} else {
actorRect.left = pt.x + 50;
actorRect.top = pt.y + 60;
actorRect.right = (int16)(pt.x + getActor(10)->getPoint2()->x + 10);
actorRect.bottom = (int16)(pt.y + getActor(10)->getPoint2()->y - 20);
}
ActorDirection newDirection = kDirectionInvalid;
if (_keyState) {
if (_keyState & kWalkLeft) {
if (_keyState & kWalkUp) {
newDirection = kDirectionNW;
} else if (_keyState & kWalkDown) {
newDirection = kDirectionSW;
} else {
newDirection = kDirectionW;
}
} else if (_keyState & kWalkRight) {
if (_keyState & kWalkUp) {
newDirection = kDirectionNE;
} else if (_keyState & kWalkDown) {
newDirection = kDirectionSE;
} else {
newDirection = kDirectionE;
}
} else if (_keyState & kWalkUp) {
newDirection = kDirectionN;
} else if (_keyState & kWalkDown) {
newDirection = kDirectionS;
}
updateCursor(newDirection, actorRect);
if (newDirection >= kDirectionN)
if (player->getStatus() == kActorStatusWalking || player->getStatus() == kActorStatusWalking2)
player->changeDirection(newDirection);
return;
}
if (mouse.x < actorRect.left) {
if (mouse.y >= actorRect.top) {
if (mouse.y > actorRect.bottom) {
if (player->getDirection() == kDirectionW) {
if ((mouse.y - actorRect.bottom) > 10)
newDirection = kDirectionSW;
} else {
if (player->getDirection() == kDirectionS) {
if ((actorRect.left - mouse.x) > 10)
newDirection = kDirectionSW;
} else {
newDirection = kDirectionSW;
}
}
} else {
if (player->getDirection() == kDirectionNW) {
if ((mouse.y - actorRect.top) > 10)
newDirection = kDirectionW;
} else {
if (player->getDirection() == kDirectionSW) {
if ((actorRect.bottom - mouse.y) > 10)
newDirection = kDirectionW;
} else {
newDirection = kDirectionW;
}
}
}
} else {
if (player->getDirection() != kDirectionN) {
if (player->getDirection() == kDirectionW) {
if ((actorRect.top - mouse.y) > 10)
newDirection = kDirectionNW;
} else {
newDirection = kDirectionNW;
}
} else {
if ((actorRect.left - mouse.x) > 10)
newDirection = kDirectionNW;
}
}
} else if (mouse.x <= actorRect.right) {
if (mouse.y >= actorRect.top) {
if (mouse.y > actorRect.bottom) {
if (player->getDirection() == kDirectionSW) {
if ((mouse.x - actorRect.left) > 10)
newDirection = kDirectionS;
} else {
if (player->getDirection() == kDirectionSE) {
if ((actorRect.right - mouse.x) > 10)
newDirection = kDirectionS;
} else {
newDirection = kDirectionS;
}
}
}
} else {
if (player->getDirection() == kDirectionNW) {
if ((mouse.x - actorRect.left) > 10)
newDirection = kDirectionN;
} else {
if (player->getDirection() == kDirectionNE) {
if ((actorRect.right - mouse.x) > 10)
newDirection = kDirectionN;
} else {
newDirection = kDirectionN;
}
}
}
} else if (mouse.y < actorRect.top) {
if (player->getDirection() != kDirectionN) {
if (player->getDirection() == kDirectionE) {
if ((actorRect.top - mouse.y) > 10)
newDirection = kDirectionNE;
} else {
newDirection = kDirectionNE;
}
} else {
if ((mouse.x - actorRect.right) > 10)
newDirection = kDirectionNE;
}
} else if (mouse.y <= actorRect.bottom) {
if (player->getDirection() == kDirectionSE) {
if ((actorRect.bottom - mouse.y) > 10)
newDirection = kDirectionE;
} else {
if (player->getDirection() == kDirectionNE) {
if ((mouse.y - actorRect.top) > 10)
newDirection = kDirectionE;
} else {
newDirection = kDirectionE;
}
}
} else if (player->getDirection() == kDirectionS) {
if ((mouse.x - actorRect.right) > 10)
newDirection = kDirectionSE;
} else if ((player->getDirection() != kDirectionE || (mouse.y - actorRect.bottom) > 10)) {
newDirection = kDirectionSE;
}
updateCursor(newDirection, actorRect);
if (newDirection >= kDirectionN)
if (player->getStatus() == kActorStatusWalking || player->getStatus() == kActorStatusWalking2)
player->changeDirection(newDirection);
}
void Scene::updateActors() {
if (!_ws)
error("[Scene::updateActors] WorldStats not initialized properly!");
for (uint32 i = 0; i < _ws->actors.size(); i++)
_ws->actors[i]->update();
}
void Scene::updateObjects() {
if (!_ws)
error("[Scene::updateObjects] WorldStats not initialized properly!");
for (uint32 i = 0; i < _ws->objects.size(); i++)
_ws->objects[i]->update();
}
void Scene::updateAmbientSounds() {
if (!_ws)
error("[Scene::updateAmbientSounds] WorldStats not initialized properly!");
if (Config.performance <= 3)
return;
// The original loops for each actor, but the volume calculation is always the same
for (uint32 i = 0; i < _ws->numAmbientSounds; i++) {
bool processSound = true;
AmbientSoundItem *snd = &_ws->ambientSounds[i];
uint32 ambientTick = getSharedData()->getAmbientTick(i);
for (int32 f = 0; f < 6; f++) {
int32 gameFlag = snd->flagNum[f];
if (!gameFlag)
break;
if (gameFlag >= 0) {
if (_vm->isGameFlagNotSet((GameFlag)gameFlag)) {
processSound = false;
break;
}
} else {
if (_vm->isGameFlagSet((GameFlag)-gameFlag)) {
processSound = false;
break;
}
}
}
if (processSound) {
if (_vm->sound()->isPlaying(snd->resourceId)) {
if (snd->field_0) {
int32 volume = Config.ambientVolume + getSound()->calculateVolumeAdjustement(snd->point, snd->attenuation, snd->delta);
if (volume <= 0) {
if (volume < -10000)
volume = -10000;
getSound()->setVolume(snd->resourceId, volume);
} else {
getSound()->setVolume(snd->resourceId, 0);
}
}
} else {
int32 panning = (snd->field_0) ? getSound()->calculatePanningAtPoint(snd->point) : 0;
int32 volume = 0;
if (snd->field_0)
volume = getSound()->calculateVolumeAdjustement(snd->point, snd->attenuation, snd->delta);
else
volume = -(int32)pow((double)snd->delta, 2);
volume += Config.ambientVolume;
if (LOBYTE(snd->flags) & 1) {
getSound()->playSound(snd->resourceId, true, volume, panning);
} else if (LOBYTE(snd->flags) & 2) {
if (_vm->getRandom(10000) < 10) {
if (snd->field_0) {
getSound()->playSound(snd->resourceId, false, volume, panning);
} else {
int32 tmpVol = volume + (int32)_vm->getRandom(500) * ((_vm->getRandom(100) >= 50) ? -1 : 1);
if (tmpVol >= 0)
tmpVol = 0;
else if (tmpVol <= -10000)
tmpVol = -10000;
getSound()->playSound(snd->resourceId, false, tmpVol, _vm->getRandom(20001) - 10000);
}
}
} else if (LOBYTE(snd->flags) & 4) {
if (ambientTick < _vm->getTick()) {
if (snd->nextTick >= 0)
getSharedData()->setAmbientTick(i, (uint32)((int32)_vm->getTick() + snd->nextTick * 60000));
else
getSharedData()->setAmbientTick(i, (uint32)((int32)_vm->getTick() - snd->nextTick * 1000));
getSound()->playSound(snd->resourceId, false, volume, panning);
}
} else if (LOBYTE(snd->flags) & 8) {
if (!getSharedData()->getAmbientFlag(i)) {
getSound()->playSound(snd->resourceId, false, volume, panning);
getSharedData()->setAmbientFlag(i, 1);
}
}
}
} else {
if (_vm->sound()->isPlaying(snd->resourceId))
_vm->sound()->stop(snd->resourceId);
}
}
}
void Scene::updateMusic() {
if (!getWorld()->musicFlag)
return;
if (getWorld()->musicCurrentResourceIndex != kMusicStopped) {
switch (getWorld()->musicStatus) {
default:
break;
case 1:
if (getWorld()->musicResourceIndex == kMusicStopped) {
getWorld()->musicCurrentResourceIndex = kMusicStopped;
getWorld()->musicStatus = 0;
getSound()->playMusic(kResourceNone, 0);
} else {
getWorld()->musicCurrentResourceIndex = getWorld()->musicResourceIndex;
getWorld()->musicStatus = getWorld()->musicStatusExt;
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, getWorld()->musicResourceIndex));
}
getWorld()->musicResourceIndex = kMusicStopped;
getWorld()->musicStatusExt = 0;
getWorld()->musicFlag = 0;
break;
case 2:
_musicVolume = getSound()->getMusicVolume();
getWorld()->musicStatus = 4;
break;
case 4:
_musicVolume -= 150;
if (_musicVolume > -2500) {
getSound()->setMusicVolume(_musicVolume);
break;
}
_musicVolume = -10000;
getWorld()->musicCurrentResourceIndex = kMusicStopped;
if (getWorld()->musicResourceIndex == kMusicStopped) {
getWorld()->musicStatus = 0;
getSound()->playMusic(kResourceNone, 0);
getWorld()->musicResourceIndex = kMusicStopped;
getWorld()->musicStatusExt = 0;
getWorld()->musicFlag = 0;
} else {
getWorld()->musicStatus = 8;
getSound()->playMusic(kResourceNone, 0);
getWorld()->musicCurrentResourceIndex = getWorld()->musicResourceIndex;
_musicVolume = -2500;
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, getWorld()->musicResourceIndex), _musicVolume);
}
break;
case 8:
_musicVolume += 150;
if (_musicVolume < Config.musicVolume) {
getSound()->setMusicVolume(_musicVolume);
break;
}
getSound()->setMusicVolume(Config.musicVolume);
getWorld()->musicStatus = getWorld()->musicStatusExt;
getWorld()->musicResourceIndex = kMusicStopped;
getWorld()->musicStatusExt = 0;
getWorld()->musicFlag = 0;
break;
}
} else if (getWorld()->musicResourceIndex != kMusicStopped) {
switch (getWorld()->musicStatusExt) {
default:
break;
case 1:
getWorld()->musicCurrentResourceIndex = getWorld()->musicResourceIndex;
getWorld()->musicStatus = 1;
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, getWorld()->musicResourceIndex));
getWorld()->musicResourceIndex = kMusicStopped;
getWorld()->musicStatusExt = 0;
getWorld()->musicFlag = 0;
break;
case 2:
_musicVolume = -10000;
getSound()->setMusicVolume(_musicVolume);
getWorld()->musicCurrentResourceIndex = getWorld()->musicResourceIndex;
getWorld()->musicStatus = 8;
getSound()->playMusic(MAKE_RESOURCE(kResourcePackMusic, getWorld()->musicResourceIndex), _musicVolume);
break;
}
} else {
getWorld()->musicFlag = 0;
}
}
void Scene::updateAdjustScreen() {
if (g_debugScrolling) {
debugScreenScrolling();
} else {
updateCoordinates();
}
}
void Scene::updateCoordinates() {
Actor *act = getActor();
int16 xLeft = _ws->xLeft, oxLeft = xLeft;
int16 yTop = _ws->yTop, oyTop = yTop;
int16 posX = act->getPoint1()->x - _ws->xLeft;
int16 posY = act->getPoint1()->y - _ws->yTop;
Common::Rect boundingRect = _ws->boundingRect;
switch (_ws->motionStatus) {
default:
break;
case 1:
if (posX < boundingRect.left) {
xLeft = (posX - boundingRect.left) + _ws->xLeft;
_ws->xLeft += posX - boundingRect.left;
} else if (posX > boundingRect.right) {
xLeft = (posX - boundingRect.right) + _ws->xLeft;
_ws->xLeft += posX - boundingRect.right;
}
if (posY < boundingRect.top) {
yTop = (posY - boundingRect.top) + _ws->yTop;
_ws->yTop += posY - boundingRect.top;
} else if (posY > boundingRect.bottom) {
yTop = (posY - boundingRect.bottom) + _ws->yTop;
_ws->yTop += posY - boundingRect.bottom;
}
if (xLeft < 0)
xLeft = _ws->xLeft = 0;
if (xLeft > (_ws->width - 640))
xLeft = _ws->xLeft = _ws->width - 640;
if (yTop < 0)
yTop = _ws->yTop = 0;
if (yTop > (_ws->height - 480))
yTop = _ws->yTop = _ws->height - 480;
break;
case 2:
case 5: {
getSharedData()->setSceneOffset(getSharedData()->getSceneOffset() + getSharedData()->getSceneOffsetAdd());
int32 coord1 = 0;
int32 coord2 = 0;
if (abs(getSharedData()->getSceneCoords().x - _ws->coordinates[0]) <= abs(getSharedData()->getSceneCoords().y - _ws->coordinates[1])) {
coord1 = _ws->coordinates[1];
coord2 = yTop;
if (_ws->coordinates[1] != _ws->yTop)
xLeft = _ws->xLeft = getSharedData()->getSceneOffset() + getSharedData()->getSceneCoords().x;
yTop = _ws->coordinates[2] + _ws->yTop;
_ws->yTop += _ws->coordinates[2];
} else {
coord1 = _ws->coordinates[0];
coord2 = xLeft;
if (_ws->coordinates[0] != _ws->xLeft)
yTop = _ws->yTop = getSharedData()->getSceneOffset() + getSharedData()->getSceneCoords().y;
xLeft = _ws->coordinates[2] + _ws->xLeft;
_ws->xLeft += _ws->coordinates[2];
}
if (abs(coord2 - coord1) <= abs(_ws->coordinates[2])) {
_ws->motionStatus = 3;
_ws->coordinates[0] = -1;
}
}
break;
}
// Update scene coordinates
Common::Rect sceneRect = _ws->sceneRects[_ws->sceneRectIdx];
if (xLeft < sceneRect.left)
xLeft =_ws->xLeft =sceneRect.left;
if (yTop < sceneRect.top)
yTop = _ws->yTop = sceneRect.top;
if ((xLeft + 639) > sceneRect.right)
xLeft = _ws->xLeft = sceneRect.right - 639;
if ((yTop + 479) > sceneRect.bottom)
yTop = _ws->yTop = sceneRect.bottom - 479;
// XXX dword_44E1EC is set to 2 at this point if the scene coordinates
// have changed, but that variable is never used anywhere else
if ((_ws->motionStatus == 2 || _ws->motionStatus == 5) && (oxLeft != _ws->xLeft || oyTop != _ws->yTop))
debugC(kDebugLevelScene, "[Scene::updateCoordinates] (%d, %d) ~> (%d, %d), motionStatus = %d",
_ws->xLeft, _ws->yTop, _ws->coordinates[0], _ws->coordinates[1], _ws->motionStatus);
}
void Scene::updateCursor(ActorDirection direction, const Common::Rect &rect) {
HitType type = kHitNone;
Actor *player = getActor();
int16 rightLimit = rect.right - 10;
Common::Point mouse = getCursor()->position();
if (getSharedData()->getFlag(kFlagIsEncounterRunning)) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceTalkNPC])
getCursor()->set(_ws->cursorResources[kCursorResourceTalkNPC]);
return;
}
if (_rightButtonDown || _keyState) {
if (player->getStatus() == kActorStatusWalking || player->getStatus() == kActorStatusWalking2) {
if (direction >= kDirectionN) {
ResourceId resourceId =_ws->cursorResources[direction];
if (getCursor()->getResourceId() != resourceId)
getCursor()->set(resourceId);
}
}
return;
}
if (player->getStatus() == kActorStatusShowingInventory || player->getStatus() == kActorStatus10) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceHand])
getCursor()->set(_ws->cursorResources[kCursorResourceHand]);
return;
}
if (player->inventory.getSelectedItem()) {
if (mouse.x >= rect.left && mouse.x <= rightLimit && mouse.y >= rect.top && mouse.y <= rect.bottom && hitTestPlayer()) {
ResourceId id = _ws->inventoryCursorsNormal[player->inventory.getSelectedItem() - 1];
if (getCursor()->getResourceId() != id)
getCursor()->set(id, 0, kCursorAnimationNone);
} else {
if (hitTestScene(type) == -1) {
ResourceId id = _ws->inventoryCursorsNormal[player->inventory.getSelectedItem() - 1];
if (getCursor()->getResourceId() != id)
getCursor()->set(id, 0, kCursorAnimationNone);
} else {
ResourceId id = _ws->inventoryCursorsBlinking[player->inventory.getSelectedItem() - 1];
uint32 frameCount = GraphicResource::getFrameCount(_vm, id);
if (getCursor()->getResourceId() != id)
getCursor()->set(id, 0, (frameCount <= 1) ? kCursorAnimationNone : kCursorAnimationMirror);
}
}
return;
}
if (mouse.x >= rect.left && mouse.x <= rightLimit && mouse.y >= rect.top && mouse.y <= rect.bottom && hitTestPlayer()) {
if (player->inventory[0]) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceGrabPointer])
getCursor()->set(_ws->cursorResources[kCursorResourceGrabPointer]);
return;
}
}
int32 index = hitTest(type);
if (index == -1) {
if (_ws->chapter != kChapter2 || getSharedData()->getPlayerIndex() != 10) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceMagnifyingGlass] || getCursor()->getAnimation())
getCursor()->set(_ws->cursorResources[kCursorResourceMagnifyingGlass]);
} else {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceTalkNPC2] || getCursor()->getAnimation())
getCursor()->set(_ws->cursorResources[kCursorResourceTalkNPC2]);
}
return;
}
int32 actionType = 0;
switch (type) {
default:
error("[Scene::updateCursor] Invalid hit type!");
break;
case kHitActionArea:
actionType = _ws->actions[index]->actionType;
break;
case kHitObject:
actionType = _ws->objects[index]->actionType;
break;
case kHitActor:
actionType = getActor(index)->actionType;
break;
}
if (actionType & kActionTypeFind) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceMagnifyingGlass] || getCursor()->getAnimation() != kCursorAnimationMirror)
getCursor()->set(_ws->cursorResources[kCursorResourceMagnifyingGlass]);
} else if (actionType & kActionTypeTalk) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceTalkNPC])
getCursor()->set(_ws->cursorResources[kCursorResourceTalkNPC]);
} else if (actionType & kActionTypeGrab) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceHand])
getCursor()->set(_ws->cursorResources[kCursorResourceHand]);
} else if (actionType & kActionType16) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceTalkNPC2] || getCursor()->getAnimation() != kCursorAnimationMirror)
getCursor()->set(_ws->cursorResources[kCursorResourceTalkNPC2]);
} else if (_ws->chapter != kChapter2 && getSharedData()->getPlayerIndex() != 10) {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceMagnifyingGlass] || getCursor()->getAnimation())
getCursor()->set(_ws->cursorResources[kCursorResourceMagnifyingGlass]);
} else {
if (getCursor()->getResourceId() != _ws->cursorResources[kCursorResourceTalkNPC2] || getCursor()->getAnimation())
getCursor()->set(_ws->cursorResources[kCursorResourceTalkNPC2]);
}
}
//////////////////////////////////////////////////////////////////////////
// HitTest
//////////////////////////////////////////////////////////////////////////
int32 Scene::hitTest(HitType &type) {
type = kHitNone;
int32 targetIdx = hitTestObject();
if (targetIdx == -1) {
targetIdx = hitTestActionArea();
if (targetIdx == -1) {
targetIdx = hitTestActor();
type = kHitActor;
} else {
type = kHitActionArea;
}
} else {
type = kHitObject;
}
return targetIdx;
}
int32 Scene::hitTestScene(HitType &type) {
if (!_ws)
error("[Scene::hitTestScene] WorldStats not initialized properly!");
const Common::Point pt = getCursor()->position();
int16 top = pt.x + _ws->xLeft;
int16 left = pt.y + _ws->yTop;
type = kHitNone;
int32 index = findActionArea(kActionAreaType2, Common::Point(top, left));
if (index != -1) {
if (_ws->actions[index]->actionType & kActionType8) {
type = kHitActionArea;
return index;
}
}
// Check objects
for (uint i = 0; i < _ws->objects.size(); i++) {
Object *object = _ws->objects[i];
if (object->isOnScreen() && (object->actionType & kActionType8)) {
if (hitTestPixel(object->getResourceId(),
object->getFrameIndex(),
top - object->x,
left - object->y,
(bool)(object->flags & kObjectFlag1000))) {
type = kHitObject;
return i;
}
}
}
// Check actors
for (uint i = 0; i < _ws->actors.size(); i++) {
Actor *actor = _ws->actors[i];
if (actor->actionType & kActionType8) {
uint32 frameIndex = (actor->getFrameIndex() >= actor->getFrameCount() ? 2 * actor->getFrameCount() - (actor->getFrameIndex() + 1) : actor->getFrameIndex());
if (hitTestPixel(actor->getResourceId(),
frameIndex,
top - (actor->getPoint()->x + actor->getPoint1()->x),
left - (actor->getPoint()->y + actor->getPoint1()->y),
actor->getDirection() >= kDirectionSE)) {
type = kHitActor;
return i;
}
}
}
return -1;
}
int32 Scene::hitTestActionArea() {
const Common::Point pt = getCursor()->position();
int32 targetIdx = findActionArea(kActionAreaType2, Common::Point(_ws->xLeft + pt.x, _ws->yTop + pt.y));
if (targetIdx == -1 || !(_ws->actions[targetIdx]->actionType & (kActionTypeFind | kActionTypeTalk | kActionTypeGrab | kActionType16)))
return -1;
return targetIdx;
}
ActorIndex Scene::hitTestActor() {
if (!_ws)
error("[Scene::hitTestActor] WorldStats not initialized properly!");
const Common::Point mouse = getCursor()->position();
if (_ws->actors.size() == 0)
return -1;
// Check actors 13 to 20
if (_ws->actors.size() >= 20) {
for (uint i = 13; i < 21; i++) {
Actor *actor = getActor(i);
if (!actor->isOnScreen() || !actor->actionType)
continue;
Common::Rect rect = GraphicResource::getFrameRect(_vm, getActor(12)->getResourceId(), 0);
int32 x = _ws->xLeft + mouse.x - (actor->getPoint1()->x + actor->getPoint()->x);
int32 y = _ws->yTop + mouse.y - (actor->getPoint1()->y + actor->getPoint()->y);
if (x > (rect.left - 20)
&& x < (rect.width() + rect.left + 20)
&& y > (rect.top - 20)
&& y < (rect.height() + rect.top + 20))
return i;
}
}
// Check Actor 11
if (_ws->actors.size() >= 11) {
Actor *actor11 = getActor(11);
if (actor11->isOnScreen() && actor11->actionType) {
Common::Point pt = mouse + Common::Point(_ws->xLeft, _ws->yTop) - *actor11->getPoint1();
if (actor11->getBoundingRect()->contains(pt))
return 11;
}
}
switch (_ws->chapter) {
default:
break;
case kChapter8:
if (_ws->actors.size() < 7)
error("[Scene::hitTestActor] Not enough actors to check (chapter 8 - checking actors 1-6)!");
for (uint i = 1; i < 7; i++) {
Actor *actor = getActor(i);
if (!actor->isVisible() || !actor->actionType)
continue;
int32 x = _ws->xLeft + mouse.x - (actor->getPoint1()->x + actor->getPoint()->x);
int32 y = _ws->yTop + mouse.y - (actor->getPoint1()->y + actor->getPoint()->y);
if (x > 300 && x < 340 && y > 220 && y < 260)
return i;
}
break;
case kChapter11:
if (_ws->actors.size() <= 1)
error("[Scene::hitTestActor] Not enough actors to check (chapter 11 - checking actor 1)!");
if (getActor(1)->isOnScreen() && getActor(1)->actionType) {
Actor *actor = getActor(1);
int32 x = _ws->xLeft + mouse.x - (actor->getPoint1()->x + actor->getPoint()->x);
int32 y = _ws->yTop + mouse.y - (actor->getPoint1()->y + actor->getPoint()->y);
Common::Rect rect = GraphicResource::getFrameRect(_vm, actor->getResourceId(), 0);
if (x > (rect.left - 10)
&& x < (rect.width() + rect.left + 10)
&& y > (rect.top - 10)
&& y < (rect.height() + rect.top + 10))
return 1;
}
if (_ws->actors.size() <= 15)
error("[Scene::hitTestActor] Not enough actors to check (chapter 11 - checking actors 10-15)!");
for (uint i = 10; i < 15; i++) {
Actor *actor = getActor(i);
if (!actor->isOnScreen() || !actor->actionType)
continue;
Common::Rect rect = GraphicResource::getFrameRect(_vm, actor->getResourceId(), 0);
int32 x = _ws->xLeft + mouse.x - (actor->getPoint1()->x + actor->getPoint()->x);
int32 y = _ws->yTop + mouse.y - (actor->getPoint1()->y + actor->getPoint()->y);
if (x > (rect.left - 10)
&& x < (rect.width() + rect.left + 10)
&& y > (rect.top - 10)
&& y < (rect.height() + rect.top + 10))
return i;
}
break;
}
//////////////////////////////////////////////////////////////////////////
// Default check
for (int i = _ws->actors.size() - 1; i >= 0 ; i--) {
Actor *actor = getActor(i);
if (!actor->isOnScreen() || !actor->actionType)
continue;
uint32 hitFrame;
if (actor->getFrameIndex() >= actor->getFrameCount())
hitFrame = 2 * actor->getFrameIndex() - (actor->getFrameCount() + 1);
else
hitFrame = actor->getFrameIndex();
if (hitTestPixel(actor->getResourceId(),
hitFrame,
_ws->xLeft + mouse.x - (actor->getPoint1()->x + actor->getPoint()->x),
_ws->yTop + mouse.y - (actor->getPoint1()->y + actor->getPoint()->y),
actor->getDirection() >= kDirectionSE))
return i;
}
return -1;
}
bool Scene::hitTestPlayer() {
const Common::Point pt = getCursor()->position();
Actor *player = getActor();
Common::Point point;
player->adjustCoordinates(&point);
uint32 frameIndex = (player->getFrameIndex() >= player->getFrameCount() ? 2 * player->getFrameCount() - (player->getFrameIndex() + 1) : player->getFrameIndex());
return hitTestPixel(player->getResourceId(),
frameIndex,
pt.x - (player->getPoint()->x + point.x),
pt.y - (player->getPoint()->y + point.y),
player->getDirection() >= kDirectionSE);
}
int32 Scene::hitTestObject() {
if (!_ws)
error("[Scene::hitTestObject] WorldStats not initialized properly!");
const Common::Point pt = getCursor()->position();
for (int32 i = _ws->objects.size() - 1; i >= 0; i--) {
Object *object = _ws->objects[i];
if (object->isOnScreen() && object->actionType)
if (hitTestPixel(object->getResourceId(),
object->getFrameIndex(),
_ws->xLeft + pt.x - object->x,
_ws->yTop + pt.y - object->y,
(bool)(object->flags & kObjectFlag1000)))
return i;
}
return -1;
}
bool Scene::hitTestPixel(ResourceId resourceId, uint32 frameIndex, int16 x, int16 y, bool flipped) {
if (x < 0 || y < 0)
return false;
GraphicResource *resource = new GraphicResource(_vm, resourceId);
GraphicFrame *frame = resource->getFrame(frameIndex);
Common::Rect frameRect = frame->getRect();
// Check y coordinates
if (y < frameRect.top || y >= frameRect.bottom)
goto cleanup;
// Compute left/right x coordinates, using flipped value
int32 left, right;
if (flipped) {
if (getScreen()->getFlag() == -1) {
left = resource->getData().maxWidth - frameRect.right;
right = resource->getData().maxWidth - frameRect.left;
} else {
left = frameRect.left + 2 * (getScreen()->getFlag() - (frameRect.right / 2));
right = x;
}
} else {
left = frameRect.left;
right = frameRect.right;
}
// Check x coordinates
if (x < left || x >= right)
goto cleanup;
// Check pixel value
byte *pixel;
if (flipped) {
pixel = (byte *)frame->surface.getBasePtr((left - x) + frame->getWidth() - 1, y - frame->y);
} else {
pixel = (byte *)frame->surface.getBasePtr(x - left, y - frame->y);
}
if (*pixel == 0)
goto cleanup;
delete resource;
return true;
cleanup:
delete resource;
return false;
}
//////////////////////////////////////////////////////////////////////////
// Hit actions
//////////////////////////////////////////////////////////////////////////
void Scene::handleHit(int32 index, HitType type) {
if (!_ws)
error("[Scene::handleHit] WorldStats not initialized properly!");
switch (type) {
default:
break;
case kHitActionArea:
if (!getScript()->isInQueue(_ws->actions[index]->scriptIndex)) {
debugC(kDebugLevelScripts, "[Script] Queuing Script idx: %d from kHitActionArea (idx: %d, name: '%s')",
_ws->actions[index]->scriptIndex, index, _ws->actions[index]->name);
getScript()->queueScript(_ws->actions[index]->scriptIndex, getSharedData()->getPlayerIndex());
}
switch (_ws->chapter) {
default:
break;
case kChapter2:
hitAreaChapter2(_ws->actions[index]->id);
break;
case kChapter7:
hitAreaChapter7(_ws->actions[index]->id);
break;
case kChapter11:
hitAreaChapter11(_ws->actions[index]->id);
break;
}
break;
case kHitObject: {
Object *object = _ws->objects[index];
if (object->getSoundResourceId()) {
if (getSound()->isPlaying(object->getSoundResourceId())) {
getSound()->stop(object->getSoundResourceId());
object->setSoundResourceId(kResourceNone);
}
}
if (!getScript()->isInQueue(object->getScriptIndex())) {
debugC(kDebugLevelScripts, "[Script] Queuing Script idx: %d from kHitObject (id: %d, name: '%s')",
object->getScriptIndex(), object->getId(), object->getName());
getScript()->queueScript(object->getScriptIndex(), getSharedData()->getPlayerIndex());
}
// Original executes special script hit functions, but since there is none defined, we can skip this part
}
break;
case kHitActor: {
Actor *actor = _ws->actors[index];
if (actor->actionType & (kActionTypeFind | kActionType16)) {
if (!getScript()->isInQueue(actor->getScriptIndex())) {
debugC(kDebugLevelScripts, "[Script] Queuing Script idx: %d from kHitActor (id: %d, name: '%s')",
actor->getScriptIndex(), index, actor->getName());
getScript()->queueScript(actor->getScriptIndex(), getSharedData()->getPlayerIndex());
}
} else if (actor->actionType & kActionTypeTalk) {
if (getSound()->isPlaying(actor->getSoundResourceId())) {
if (actor->getStatus() != kActorStatusEnabled)
actor->changeStatus(kActorStatusEnabled);
getSound()->stop(actor->getSoundResourceId());
actor->setSoundResourceId(kResourceNone);
}
if (!getScript()->isInQueue(actor->getScriptIndex())) {
debugC(kDebugLevelScripts, "[Script] Queuing Script idx: %d from kActionTypeTalk (actor idx: %d)",
actor->getScriptIndex(), getSharedData()->getPlayerIndex());
getScript()->queueScript(actor->getScriptIndex(), getSharedData()->getPlayerIndex());
}
}
switch (_ws->chapter) {
default:
break;
case kChapter2:
hitActorChapter2(index);
break;
case kChapter11:
hitActorChapter11(index);
break;
}
}
break;
}
}
void Scene::clickInventory() {
const Common::Point mouse = getCursor()->position();
Common::Point point;
Actor *player = getActor();
player->adjustCoordinates(&point);
uint count = player->inventory.find();
player->inventory.selectItem(0);
if (count > 0) {
for (uint32 i = 0; i < count; i++) {
Common::Point ringPoint = Inventory::getInventoryRingPoint(_vm, count, i);
int32 x = point.x + player->getPoint2()->x + ringPoint.x;
int32 y = point.y + player->getPoint2()->y / 2 - ringPoint.y;
if (mouse.x >= x && mouse.x <= (x + 40) && mouse.y >= y && mouse.y <= (y + 40)) {
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 4));
if (_ws->chapter == kChapter9) {
switch (i) {
default:
player->inventory.selectItem(player->inventory[i]);
break;
case 0:
getScript()->queueScript(_ws->actions[_ws->getActionAreaIndexById(2206)]->scriptIndex, getSharedData()->getPlayerIndex());
break;
case 1:
getScript()->queueScript(_ws->actions[_ws->getActionAreaIndexById(2207)]->scriptIndex, getSharedData()->getPlayerIndex());
break;
case 2:
getScript()->queueScript(_ws->actions[_ws->getActionAreaIndexById(2208)]->scriptIndex, getSharedData()->getPlayerIndex());
break;
}
} else {
player->inventory.selectItem(player->inventory[i]);
}
break;
}
}
}
player->changeStatus(kActorStatusEnabled);
getSound()->playSound(MAKE_RESOURCE(kResourcePackSound, 5));
}
void Scene::hitAreaChapter2(int32 id) {
if (id == 783)
getActor()->inventory.selectItem(6);
}
void Scene::hitAreaChapter7(int32 id) {
switch (id) {
default:
break;
case 1088:
if (_isCTRLPressed)
_vm->setGameFlag(kGameFlag1144);
break;
case 2504:
if (++_hitAreaChapter7Counter > 20) {
_vm->setGameFlag(kGameFlag1108);
getActor(1)->setPosition(570, 225, kDirectionN, 0);
getActor(1)->show();
}
break;
}
}
void Scene::hitAreaChapter11(int32 id) {
if (!_ws)
error("[Scene::hitAreaChapter11] WorldStats not initialized properly!");
if (id == 1670)
_ws->field_E849C = 666;
}
void Scene::hitActorChapter2(ActorIndex index) {
if (!_ws)
error("[Scene::hitActorChapter2] WorldStats not initialized properly!");
Actor *player = getActor();
if (player->getStatus() != kActorStatusEnabled2 && player->getStatus() != kActorStatusWalking2)
return;
if (index == 11) {
player->faceTarget((uint32)index, kDirectionFromActor);
player->changeStatus(kActorStatusAttacking);
Actor *actor11 = getActor(index);
Common::Point pointPlayer(player->getPoint1()->x + player->getPoint2()->x, player->getPoint1()->y + player->getPoint2()->y);
Common::Point pointActor11(actor11->getPoint1()->x + actor11->getPoint2()->x, actor11->getPoint1()->y + actor11->getPoint2()->y);
if (Actor::euclidianDistance(pointPlayer, pointActor11) < 150) {
if (actor11->getStatus() == kActorStatusWalking2)
actor11->changeStatus(kActorStatus18);
if (actor11->getStatus() == kActorStatusEnabled)
actor11->changeStatus(kActorStatusEnabled2);
}
getSharedData()->setChapter2ActorIndex(index);
} else if (index > 12) {
player->faceTarget((uint32)(index + 9), kDirectionFromActor);
player->changeStatus(kActorStatusAttacking);
getSharedData()->setChapter2ActorIndex(index);
}
}
void Scene::hitActorChapter11(ActorIndex index) {
if (!_ws)
error("[Scene::hitActorChapter11] WorldStats not initialized properly!");
if (_ws->field_E848C < 3)
_ws->field_E849C = index;
}
//////////////////////////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////////////////////////
void Scene::playIntroSpeech() {
ResourceId resourceId;
switch (_packId) {
default:
resourceId = (ResourceId)_packId;
break;
case kResourcePackCourtyardAndChapel:
resourceId = getSpeech()->playScene(4, 3);
break;
case kResourcePackCave:
resourceId = getSpeech()->playScene(4, 6);
break;
case kResourcePackLaboratory:
resourceId = getSpeech()->playScene(4, 7);
break;
}
getScreen()->clear();
getScreen()->stopPaletteFade(0, 0, 0);
do {
// Poll events (this ensure we don't freeze the screen)
Common::Event ev;
_vm->getEventManager()->pollEvent(ev);
g_system->updateScreen();
g_system->delayMillis(100);
} while (getSound()->isPlaying(resourceId));
}
void Scene::stopSpeech() {
if (_vm->isGameFlagNotSet(kGameFlag219)) {
if (getSpeech()->getSoundResourceId() != kResourceNone && getSound()->isPlaying(getSpeech()->getSoundResourceId()))
getSound()->stopAll(getSpeech()->getSoundResourceId());
else if (getSpeech()->getTick())
getSpeech()->setTick(_vm->getTick());
}
}
bool Scene::speak(Common::KeyCode code) {
if (!_ws)
error("[Scene::speak] WorldStats not initialized properly!");
#define GET_INDEX() ((int)abs((double)_vm->getRandom(RAND_MAX)) & 1)
int32 index = -1;
switch (code) {
default:
break;
case Common::KEYCODE_p:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = GET_INDEX();
break;
case kActorSarah:
case kActorCyclops:
case kActorAztec:
index = 1;
break;
}
break;
case Common::KEYCODE_q:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 3 - GET_INDEX();
break;
case kActorSarah:
case kActorCyclops:
case kActorAztec:
index = 2;
break;
}
break;
case Common::KEYCODE_r:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 2;
break;
case kActorSarah:
case kActorCyclops:
case kActorAztec:
index = 4;
break;
}
break;
case Common::KEYCODE_s:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 5;
break;
case kActorSarah:
case kActorCyclops:
case kActorAztec:
index = 3;
break;
}
break;
case Common::KEYCODE_t:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 6;
break;
case kActorSarah:
case kActorCyclops:
case kActorAztec:
index = 4;
break;
}
break;
case Common::KEYCODE_u:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 7;
break;
case kActorSarah:
case kActorCyclops:
index = 5;
break;
}
break;
case Common::KEYCODE_v:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 8;
break;
case kActorSarah:
case kActorCyclops:
index = 6;
break;
}
break;
case Common::KEYCODE_w:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 9;
break;
case kActorSarah:
case kActorCyclops:
index = 7;
break;
}
break;
case Common::KEYCODE_x:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 10;
break;
case kActorSarah:
case kActorCyclops:
index = 8;
break;
}
break;
case Common::KEYCODE_y:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 11;
break;
case kActorSarah:
case kActorCyclops:
index = 9;
break;
}
break;
case Common::KEYCODE_z:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 13 - GET_INDEX();
break;
case kActorSarah:
case kActorCyclops:
index = 10;
break;
}
break;
case Common::KEYCODE_LEFTBRACKET:
switch (_ws->actorType) {
default:
break;
case kActorMax:
index = 15 - GET_INDEX();
break;
case kActorCyclops:
index = 12 - GET_INDEX();
break;
}
break;
}
if (getSpeech()->getSoundResourceId() && getSound()->isPlaying(getSpeech()->getSoundResourceId()))
return false;
if (index == -1)
return false;
getSpeech()->playPlayer(index);
return true;
#undef GET_INDEX
}
bool Scene::pointBelowLine(const Common::Point &point, const Common::Rect &rect) const {
if (rect.top || rect.left || rect.bottom || rect.right) {
Common::Rational res(rect.height() * (point.x - rect.left), rect.width());
return (bool)(point.y > (rect.top + res.toInt()));
}
return true;
}
bool Scene::rectIntersect(int32 x, int32 y, int32 x1, int32 y1, int32 x2, int32 y2, int32 x3, int32 y3) const {
return (x <= x3 && x1 >= x2) && (y <= y3 && y1 >= y2);
}
void Scene::adjustCoordinates(Common::Point *point) {
if (!_ws)
error("[Scene::adjustCoordinates] WorldStats not initialized properly!");
point->x = _ws->xLeft + getCursor()->position().x;
point->y = _ws->yTop + getCursor()->position().y;
}
Actor *Scene::getActor(ActorIndex index) {
if (!_ws)
error("[Scene::getActor] WorldStats not initialized properly!");
ActorIndex computedIndex = (index != kActorInvalid) ? index : getSharedData()->getPlayerIndex();
if (computedIndex < 0 || computedIndex >= (int16)_ws->actors.size())
error("[Scene::getActor] Invalid actor index: %d ([0-%d] allowed)", computedIndex, _ws->actors.size() - 1);
return _ws->actors[computedIndex];
}
bool Scene::updateSceneCoordinates(int32 tX, int32 tY, int32 A0, bool checkSceneCoords, int32 *param) {
if (!_ws)
error("[Scene::updateSceneCoordinates] WorldStats not initialized properly!");
Common::Rect *sr = &_ws->sceneRects[_ws->sceneRectIdx];
int16 *targetX = &_ws->coordinates[0];
int16 *targetY = &_ws->coordinates[1];
int16 *coord3 = &_ws->coordinates[2];
*targetX = (int16)tX;
*targetY = (int16)tY;
*coord3 = (int16)A0;
// Adjust coordinates
if (checkSceneCoords)
if (*targetX + 640 > _ws->width)
*targetX = _ws->width - 640;
if (*targetX < sr->left)
*targetX = sr->left;
if (*targetY < sr->top)
*targetY = sr->top;
if (*targetX + 640 > sr->right)
*targetX = sr->right - 640;
if (*targetY + 480 > sr->bottom)
*targetY = sr->bottom - 480;
if (checkSceneCoords)
if (*targetY + 480 > _ws->height)
*targetY = _ws->height - 480;
// Adjust scene offsets & coordinates
getSharedData()->setSceneOffset(0);
getSharedData()->setSceneCoords(Common::Point(_ws->xLeft, _ws->yTop));
int32 diffX = *targetX - _ws->xLeft;
int32 diffY = *targetY - _ws->yTop;
if (abs(diffX) <= abs(diffY)) {
if (_ws->yTop > *targetY)
*coord3 = -*coord3;
if (diffY)
getSharedData()->setSceneOffsetAdd((int16)Common::Rational(*coord3 * diffX, diffY).toInt());
if (param != nullptr && abs(diffY) <= abs(*coord3)) {
*targetX = -1;
*param = 0;
return true;
}
} else {
if (_ws->xLeft > *targetX)
*coord3 = -*coord3;
getSharedData()->setSceneOffsetAdd((int16)Common::Rational(*coord3 * diffY, diffX).toInt());
if (param != nullptr && abs(diffX) <= abs(*coord3)) {
*targetX = -1;
return true;
}
}
return false;
}
int32 Scene::findActionArea(ActionAreaType type, const Common::Point &pt, bool highlight) {
if (!_ws)
error("[Scene::findActionArea] WorldStats not initialized properly!");
if (!_polygons)
error("[Scene::findActionArea] Polygons not initialized properly!");
switch (type) {
default:
return type - 2;
case kActionAreaType1:
if (_ws->actions.size() < 1)
return -1;
for (int32 i = _ws->actions.size() - 1; i >= 0; i--) {
ActionArea *area = _ws->actions[i];
if (!(area->flags & 1))
continue;
if (g_debugPolygons && highlight) {
// Highlight each polygon as it gets checked for action
debugHighlightPolygon(area->polygonIndex);
}
bool found = false;
// Iterate over flagNum
for (uint32 j = 0; j < 10; j++) {
if (!area->flagNums[j])
break; // We stop as soon as a flag is 0
bool flagSet = false;
if (area->flagNums[j] <= 0)
flagSet = _vm->isGameFlagNotSet((GameFlag)-area->flagNums[j]);
else
flagSet = _vm->isGameFlagSet((GameFlag)area->flagNums[j]);
if (!flagSet) {
found = true;
break;
}
}
if (!found && _polygons->get(area->polygonIndex).contains(pt))
return i;
}
break;
case kActionAreaType2:
if (_ws->actions.size() < 1)
return -1;
for (int32 i = _ws->actions.size() - 1; i >= 0; i--) {
ActionArea *area = _ws->actions[i];
bool found = false;
// Iterate over flagNum
for (uint32 j = 0; j < 10; j++) {
if (!area->flagNums[j])
continue; // We skip over null flags
bool flagSet = false;
if (area->flagNums[j] <= 0)
flagSet = _vm->isGameFlagNotSet((GameFlag)-area->flagNums[j]);
else
flagSet = _vm->isGameFlagSet((GameFlag)area->flagNums[j]);
if (!flagSet) {
found = true;
break;
}
}
if (!found && _polygons->get(area->polygonIndex).contains(pt))
return i;
}
break;
}
return -1;
}
void Scene::changePlayer(ActorIndex index) {
if (!_ws)
error("[Scene::changePlayer] WorldStats not initialized properly!");
switch (index) {
default:
if (_ws->chapter == kChapter9) {
changePlayerUpdate(index);
getActor(index)->show();
}
getSharedData()->setPlayerIndex(index);
break;
case 1:
if (_ws->chapter == kChapter9) {
changePlayerUpdate(index);
getScreen()->setPalette(_ws->graphicResourceIds[0]);
_ws->currentPaletteId = _ws->graphicResourceIds[0];
getScreen()->setGammaLevel(_ws->graphicResourceIds[0]);
_vm->setGameFlag(kGameFlag635);
_vm->clearGameFlag(kGameFlag636);
_vm->clearGameFlag(kGameFlag637);
getActor(index)->show();
}
getSharedData()->setPlayerIndex(index);
break;
case 2:
if (_ws->chapter == kChapter9) {
changePlayerUpdate(index);
getScreen()->setPalette(_ws->graphicResourceIds[1]);
_ws->currentPaletteId = _ws->graphicResourceIds[1];
getScreen()->setGammaLevel(_ws->graphicResourceIds[1]);
_vm->setGameFlag(kGameFlag636);
_vm->clearGameFlag(kGameFlag635);
_vm->clearGameFlag(kGameFlag637);
getActor(index)->show();
}
getSharedData()->setPlayerIndex(index);
break;
case 3:
if (_ws->chapter == kChapter9) {
changePlayerUpdate(index);
getScreen()->setPalette(_ws->graphicResourceIds[2]);
_ws->currentPaletteId = _ws->graphicResourceIds[2];
getScreen()->setGammaLevel(_ws->graphicResourceIds[2]);
_vm->setGameFlag(kGameFlag637);
_vm->clearGameFlag(kGameFlag635);
_vm->clearGameFlag(kGameFlag636);
getActor(index)->show();
}
getActor(index)->show();
getSharedData()->setPlayerIndex(index);
break;
case 666:
getScreen()->setupTransTables(3, _ws->graphicResourceIds[50], _ws->graphicResourceIds[49], _ws->graphicResourceIds[48]);
// Save scene data
getSharedData()->saveCursorResources((ResourceId *)&_ws->cursorResources, sizeof(_ws->cursorResources));
getSharedData()->saveSceneFonts(_ws->font1, _ws->font2, _ws->font3);
getSharedData()->saveSmallCursor(_ws->smallCurUp, _ws->smallCurDown);
getSharedData()->saveEncounterFrameBackground(_ws->encounterFrameBg);
// Setup new values
for (uint32 i = 0; i < 11; i++)
_ws->cursorResources[i] = _ws->graphicResourceIds[20 + i];
_ws->font1 = _ws->graphicResourceIds[35];
_ws->font2 = _ws->graphicResourceIds[37];
_ws->font3 = _ws->graphicResourceIds[36];
_ws->smallCurUp = _ws->graphicResourceIds[33];
_ws->smallCurDown = _ws->graphicResourceIds[34];
_ws->encounterFrameBg = _ws->graphicResourceIds[32];
break;
case 667:
getScreen()->setupTransTables(3, _ws->cellShadeMask1, _ws->cellShadeMask2, _ws->cellShadeMask3);
// Load scene data
getSharedData()->loadCursorResources((ResourceId *)&_ws->cursorResources, sizeof(_ws->cursorResources));
getSharedData()->loadSceneFonts(&_ws->font1, &_ws->font2, &_ws->font3);
getSharedData()->loadSmallCursor(&_ws->smallCurUp, &_ws->smallCurDown);
getSharedData()->loadEncounterFrameBackground(&_ws->encounterFrameBg);
// Reset cursor
getCursor()->set(_ws->cursorResources[kCursorResourceMagnifyingGlass], 0, kCursorAnimationNone);
break;
case 668:
getActor(11)->setPosition(2300, 100, kDirectionN, 0);
getSharedData()->setChapter2Counter(6, 0);
getSharedData()->setFlag(kFlag1, false);
break;
}
}
void Scene::changePlayerUpdate(ActorIndex index) {
Actor *actor = getActor(index);
Actor *player = getActor();
actor->setPosition(player->getPoint1()->x + player->getPoint2()->x, player->getPoint1()->y + player->getPoint2()->y, player->getDirection(), 0);
player->hide();
actor->inventory.copyFrom(player->inventory);
}
//////////////////////////////////////////////////////////////////////////
// Scene drawing
//////////////////////////////////////////////////////////////////////////
void Scene::preload() {
if (!Config.showSceneLoading || _vm->checkGameVersion("Demo"))
return;
SceneTitle *title = new SceneTitle(_vm);
getCursor()->hide();
title->load();
do {
title->update(_vm->getTick());
g_system->updateScreen();
g_system->delayMillis(10);
// Poll events (this ensure we don't freeze the screen)
Common::Event ev;
_vm->getEventManager()->pollEvent(ev);
} while (!title->loadingComplete());
delete title;
}
bool Scene::drawScene() {
if (!_ws)
error("[Scene::drawScene] WorldStats not initialized properly!");
_vm->screen()->clearGraphicsInQueue();
if (getSharedData()->getFlag(kFlagSkipDrawScene)) {
_vm->screen()->fillRect(0, 0, 640, 480, 0);
getCursor()->hide();
return false;
}
// Draw scene background
getScreen()->draw(_ws->backgroundImage, 0, Common::Point(-_ws->xLeft, -_ws->yTop), kDrawFlagNone, false);
// Draw actors on the update list
buildUpdateList();
processUpdateList();
if (_ws->chapter == kChapter11)
checkVisibleActorsPriority();
// Queue updates
for (uint32 i = 0; i < _ws->actors.size(); i++)
_ws->actors[i]->draw();
for (uint32 i = 0; i < _ws->objects.size(); i++)
_ws->objects[i]->draw();
Actor *player = getActor();
if (player->getStatus() == kActorStatusShowingInventory || player->getStatus() == kActorStatus10)
player->drawInventory();
else
player->setNumberFlag01(0);
_vm->screen()->drawGraphicsInQueue();
// Show debug information
if (g_debugScrolling)
debugScreenScrolling();
if (g_debugActors)
debugShowActors();
if (g_debugPolygons)
debugShowPolygons();
if (g_debugPolygonIndex)
debugHighlightPolygon(g_debugPolygonIndex);
if (g_debugObjects)
debugShowObjects();
if (g_debugSceneRects)
debugShowSceneRects();
return false;
}
bool Scene::updateListCompare(const UpdateItem &item1, const UpdateItem &item2) {
return (item1.priority < item2.priority);
}
void Scene::buildUpdateList() {
if (!_ws)
error("[Scene::buildUpdateList] WorldStats not initialized properly!");
_updateList.clear();
for (uint32 i = 0; i < _ws->actors.size(); i++) {
Actor *actor = _ws->actors[i];
if (actor->isVisible()) {
UpdateItem item;
item.index = i;
item.priority = actor->getPoint1()->y + actor->getPoint2()->y;
_updateList.push_back(item);
}
}
// Sort the list (the original uses qsort, so we may have to revert to that if our sort isn't behaving the same)
Common::sort(_updateList.begin(), _updateList.end(), &Scene::updateListCompare);
}
void Scene::processUpdateList() {
if (!_ws)
error("[Scene::processUpdateList] WorldStats not initialized properly!");
for (uint32 i = 0; i < _updateList.size(); i++) {
Actor *actor = getActor(_updateList[i].index);
int32 priority = _updateList[i].priority;
// Check priority
if (priority < 0) {
actor->setPriority(abs(priority));
continue;
}
actor->setPriority(3);
if (actor->getField944() == 1 || actor->getField944() == 4) {
actor->setPriority(1);
} else {
actor->setField938(1);
actor->setField934(0);
Common::Point sum = *actor->getPoint1() + *actor->getPoint2();
int16 bottomRight = actor->getPoint1()->y + actor->getBoundingRect()->bottom + 4;
if (_ws->chapter == kChapter11 && _updateList[i].index != getSharedData()->getPlayerIndex())
bottomRight += 20;
// Our actor rect
Common::Rect actorRect(actor->getPoint1()->x, actor->getPoint1()->y, actor->getPoint1()->x + actor->getBoundingRect()->right, bottomRight);
// Process objects
for (uint32 j = 0; j < _ws->objects.size(); j++) {
Object *object = _ws->objects[j];
// Skip hidden objects
if (!object->isOnScreen())
continue;
// Check that the rects are contained
if (!rectIntersect(object->x,
object->y,
object->x + object->getBoundingRect()->right,
object->y + object->getBoundingRect()->bottom,
actor->getPoint1()->x,
actor->getPoint1()->y,
actor->getPoint1()->x + actor->getBoundingRect()->right,
bottomRight)) {
if ((BYTE1(object->flags) & kObjectFlag20) && !(BYTE1(object->flags) & kObjectFlag80))
BYTE1(object->flags) = BYTE1(object->flags) | kObjectFlag40;
continue;
}
// Check if it intersects with either the object rect or the related polygon
bool isMasked = false;
if (object->flags & kObjectFlag2) {
isMasked = !pointBelowLine(sum, *object->getRect());
} else if (object->flags & kObjectFlag40) {
Polygon poly = _polygons->get(object->getPolygonIndex());
isMasked = poly.contains(sum);
}
// Adjust object flags
if ((BYTE1(object->flags) & kObjectFlag80) || isMasked) {
if (BYTE1(object->flags) & kObjectFlag20)
BYTE1(object->flags) = (BYTE1(object->flags) & kObjectFlagBF) | kObjectFlag80;
} else {
if (BYTE1(object->flags) & kObjectFlag20) {
BYTE1(object->flags) = BYTE1(object->flags) | kObjectFlag40;
}
}
if (LOBYTE(object->flags) & kObjectFlag4) {
if (isMasked) {
if (actor->flags & kActorFlagMasked)
error("[Scene::processUpdateList] Assigning mask to masked character (%s)", actor->getName());
// We are masked by the object!
actor->setObjectIndex(j);
actor->flags |= kActorFlagMasked;
}
} else {
if (isMasked) {
if (actor->getPriority() < object->getPriority()) {
actor->setField934(1);
actor->setPriority(object->getPriority() + 3);
if (i > 0) {
// Update actor priority up to current index
for (uint32 k = 0; k < i; k++) {
Actor *previousActor = getWorld()->actors[_updateList[k].index];
if (getWorld()->chapter != kChapter2 || previousActor->getField944() != 1) {
if (previousActor->getPriority() == actor->getPriority())
actor->setPriority(actor->getPriority() - 1);
}
}
}
}
} else {
if (actor->getPriority() > object->getPriority() || actor->getPriority() == 1) {
actor->setField934(1);
actor->setPriority(object->getPriority() - 1);
if (i > 0) {
// Update actor priority up to current index
for (uint32 k = 0; k < i; k++) {
Actor *previousActor = getWorld()->actors[_updateList[k].index];
if (previousActor->getField944() != 1 && previousActor->getField944() != 4) {
Actor *actorCheck = getWorld()->actors[k];
if (rectIntersect(actorCheck->getPoint1()->x,
actorCheck->getPoint1()->y,
actorCheck->getPoint1()->x + actorCheck->getBoundingRect()->bottom,
actorCheck->getPoint1()->y + actorCheck->getBoundingRect()->right,
actor->getPoint1()->x,
actor->getPoint1()->y,
actor->getPoint1()->x + actor->getBoundingRect()->right,
bottomRight)) {
if (previousActor->getPriority() == actor->getPriority())
actor->setPriority(actor->getPriority() - 1);
}
}
}
}
}
}
}
} // end processing objects
// Update all other actors
for (uint32 k = 0; k < _updateList.size(); k++) {
Actor *actor2 = getActor(_updateList[k].index);
if (actor2->isVisible() && actor2->getField944() != 1 && actor2->getField944() != 4 && _updateList[k].index != _updateList[i].index) {
Common::Rect actor2Rect(actor2->getPoint1()->x, actor2->getPoint1()->y, actor2->getPoint1()->x + actor2->getBoundingRect()->right, actor2->getPoint1()->y + actor2->getBoundingRect()->bottom);
if (actor2Rect.contains(actorRect)) {
// Inferior
if ((actor2->getPoint1()->y + actor2->getPoint2()->y) > (actor->getPoint1()->y + actor->getPoint2()->y)) {
if (actor->getPriority() <= actor2->getPriority()) {
if (actor->getField934() || actor2->getNumberValue01()) {
if (!actor2->getNumberValue01())
actor2->setPriority(actor->getPriority() - 1);
} else {
actor->setPriority(actor2->getPriority() + 1);
}
}
}
// Superior
if ((actor2->getPoint1()->y + actor2->getPoint2()->y) < (actor->getPoint1()->y + actor->getPoint2()->y)) {
if (actor->getPriority() >= actor2->getPriority()) {
if (actor->getField934() || actor2->getNumberValue01()) {
if (!actor2->getNumberValue01())
actor2->setPriority(actor->getPriority() + 1);
} else {
actor->setPriority(actor2->getPriority() - 1);
}
}
}
}
}
}
if (actor->shouldInvertPriority())
getActor(actor->getNextActorIndex())->setPriority(-actor->getPriority());
}
} // end processing actors
// Go through the list from the end
if (_updateList.size() > 1) {
for (int i = _updateList.size() - 1; i >= 0; --i) {
// Get the actor
Actor *actor1 = getActor(_updateList[i].index);
// Skip hidden actors
if (!actor1->isVisible())
continue;
if (actor1->getField944() != 1 && actor1->getField944() != 4) {
// Process all previous actors
if (i > 0) {
for (int j = i - 1; j >= 0; --j) {
Actor *actor2 = getActor(_updateList[j].index);
if (actor2->getField944() != 1 && actor2->getField944() != 4) {
// Process priorities
if (actor2->getPriority() <= actor1->getPriority()) {
if (rectIntersect(actor1->getPoint1()->x,
actor1->getPoint1()->y,
actor1->getPoint1()->x + actor1->getBoundingRect()->right,
actor1->getPoint1()->y + actor1->getBoundingRect()->bottom,
actor2->getPoint1()->x,
actor2->getPoint1()->y,
actor2->getPoint1()->x + actor2->getBoundingRect()->right,
actor2->getPoint1()->y + actor2->getBoundingRect()->bottom)) {
actor2->setPriority(actor1->getPriority() + 1);
}
}
}
}
}
}
}
}
}
void Scene::checkVisibleActorsPriority() {
for (uint i = 2; i < 9; i++)
if (getActor(i)->isVisible())
adjustActorPriority(i);
for (uint i = 16; i < 18; i++)
if (getActor(i)->isVisible())
adjustActorPriority(i);
}
void Scene::adjustActorPriority(ActorIndex index) {
Actor *actor0 = getActor(0);
Actor *actor = getActor(index);
if (rectIntersect(actor0->getPoint1()->x,
actor0->getPoint1()->y,
actor0->getPoint1()->x + actor0->getBoundingRect()->right,
actor0->getPoint1()->y + actor0->getBoundingRect()->bottom + 4,
actor->getPoint1()->x,
actor->getPoint1()->y,
actor->getPoint1()->x + actor0->getBoundingRect()->right,
actor->getPoint1()->y + actor0->getBoundingRect()->bottom)) {
if (actor->getPriority() < actor0->getPriority())
actor0->setPriority(actor->getPriority());
}
}
void Scene::drawRain() {
if (!_ws)
error("[Scene::drawRain] WorldStats not initialized properly!");
if (getSharedData()->getFlag(kFlagSkipDrawScene))
return;
for (int16 y = 0; y < 512; y = y + 64) {
for (int16 x = 0; x < 704; x = x + 64) {
getScreen()->draw(MAKE_RESOURCE(kResourcePackShared, 58), (uint32)_chapter5RainFrameIndex, Common::Point(x + (_ws->xLeft % 64) / 8, y + (_ws->yTop % 64) / 8));
}
}
_chapter5RainFrameIndex = (_chapter5RainFrameIndex + 1) % (int32)GraphicResource::getFrameCount(_vm, MAKE_RESOURCE(kResourcePackShared, 58));
}
//////////////////////////////////////////////////////////////////////////
// Debug
//////////////////////////////////////////////////////////////////////////
void Scene::debugScreenScrolling() {
if (!_ws)
error("[Scene::debugScreenScrolling] WorldStats not initialized properly!");
Common::Rect rect = GraphicResource::getFrameRect(_vm, _ws->backgroundImage, 0);
// Horizontal scrolling
if (getCursor()->position().x < SCREEN_EDGES && _ws->xLeft >= SCROLL_STEP)
_ws->xLeft -= SCROLL_STEP;
else if (getCursor()->position().x > (640 - SCREEN_EDGES) && _ws->xLeft <= (rect.width() - (640 + SCROLL_STEP)))
_ws->xLeft += SCROLL_STEP;
// Vertical scrolling
if (getCursor()->position().y < SCREEN_EDGES && _ws->yTop >= SCROLL_STEP)
_ws->yTop -= SCROLL_STEP;
else if (getCursor()->position().y > (480 - SCREEN_EDGES) && _ws->yTop <= (rect.height() - (480 + SCROLL_STEP)))
_ws->yTop += SCROLL_STEP;
}
// WALK REGION DEBUG
void Scene::debugShowWalkRegion(Polygon *poly) {
Graphics::Surface surface;
surface.create((uint16)poly->boundingRect.width() + 1,
(uint16)poly->boundingRect.height() + 1,
Graphics::PixelFormat::createFormatCLUT8());
// Draw all lines in Polygon
for (uint32 i = 0; i < poly->count(); i++) {
surface.drawLine(
poly->points[i].x - poly->boundingRect.left,
poly->points[i].y - poly->boundingRect.top,
poly->points[(i+1) % poly->count()].x - poly->boundingRect.left,
poly->points[(i+1) % poly->count()].y - poly->boundingRect.top, 0x3A);
}
getScreen()->copyToBackBufferClipped(&surface, poly->boundingRect.left, poly->boundingRect.top);
surface.free();
}
// POLYGONS DEBUG
void Scene::debugShowPolygons() {
if (!_polygons)
error("[Scene::debugShowPolygons] Polygons not initialized properly!");
for (uint32 p = 0; p < _polygons->size(); p++)
debugShowPolygon(p);
}
void Scene::debugShowPolygon(uint32 index, uint32 color) {
if (!_polygons)
error("[Scene::debugShowPolygon] Polygons not initialized properly");
if (index >= _polygons->size() - 1)
return;
Graphics::Surface surface;
Polygon poly = _polygons->get(index);
surface.create((uint16)poly.boundingRect.width() + 1,
(uint16)poly.boundingRect.height() + 1,
Graphics::PixelFormat::createFormatCLUT8());
// Draw all lines in Polygon
for (uint32 i = 0; i < poly.count(); i++) {
surface.drawLine(poly.points[i].x - poly.boundingRect.left,
poly.points[i].y - poly.boundingRect.top,
poly.points[(i+1) % poly.count()].x - poly.boundingRect.left,
poly.points[(i+1) % poly.count()].y - poly.boundingRect.top,
color);
}
getScreen()->copyToBackBufferClipped(&surface, poly.boundingRect.left, poly.boundingRect.top);
surface.free();
}
void Scene::debugHighlightPolygon(uint32 index) {
debugShowPolygon(index, 0x12);
getScreen()->copyBackBufferToScreen();
g_system->updateScreen();
}
// SCENE RECTS DEBUG
void Scene::debugShowSceneRects() {
if (!_ws)
error("[Scene::debugShowObjects] WorldStats not initialized properly!");
for (uint32 i = 0; i < ARRAYSIZE(_ws->sceneRects); i++)
getScreen()->drawRect(_ws->sceneRects[i]);
}
// OBJECT DEBUGGING
void Scene::debugShowObjects() {
if (!_ws)
error("[Scene::debugShowObjects] WorldStats not initialized properly!");
for (uint32 p = 0; p < _ws->objects.size(); p++) {
Graphics::Surface surface;
Object *object = _ws->objects[p];
if (object->isOnScreen()) {
surface.create((uint16)object->getBoundingRect()->width() + 1,
(uint16)object->getBoundingRect()->height() + 1,
Graphics::PixelFormat::createFormatCLUT8());
surface.frameRect(*object->getBoundingRect(), 0x22);
getScreen()->copyToBackBufferClipped(&surface, object->x, object->y);
}
surface.free();
}
}
// ACTOR DEBUGGING
void Scene::debugShowActors() {
if (!_ws)
error("[Scene::debugShowActors] WorldStats not initialized properly!");
for (uint32 p = 0; p < _ws->actors.size(); p++) {
Graphics::Surface surface;
Actor *a = _ws->actors[p];
if (a->isOnScreen()) {
surface.create((uint16)a->getBoundingRect()->width() + 1,
(uint16)a->getBoundingRect()->height() + 1,
Graphics::PixelFormat::createFormatCLUT8());
surface.frameRect(*a->getBoundingRect(), 0x128);
getScreen()->copyToBackBufferClipped(&surface, a->getPoint1()->x, a->getPoint1()->y);
}
surface.free();
}
}
} // end of namespace Asylum
|