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 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
|
/*
* Seven Kingdoms: Ancient Adversaries
*
* Copyright 1997,1998 Enlight Software Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU 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/>.
*
*/
//Filename : OUNITM.CPP
//Description : Object Unit movement
//Owner : Alex
#include <ALL.h>
#include <OWORLD.h>
#include <OF_HARB.h>
#include <OGAME.h>
#include <ONATION.h>
#include <OU_MARI.h>
#include <OSPATH.h>
#include <OSPREUSE.h>
#include <OSERES.h>
#include <OLOG.h>
#include <OEFFECT.h>
#include <ConfigAdv.h>
#ifdef NO_DEBUG_UNIT
#undef err_when
#undef err_here
#undef err_if
#undef err_else
#undef err_now
#define err_when(cond)
#define err_here()
#define err_if(cond)
#define err_else
#define err_now(msg)
#undef DEBUG
#endif
//-*********** simulate aat ************-//
#ifdef DEBUG
#include <OSYS.h>
#endif
//-*********** simulate aat ************-//
//--- Define no. of pixels per direction move (N, NE, E, SE, S, SW, W, NW) ---//
static short move_x_pixel_array[] = { 0, ZOOM_LOC_WIDTH, ZOOM_LOC_WIDTH, ZOOM_LOC_WIDTH, 0, -ZOOM_LOC_WIDTH, -ZOOM_LOC_WIDTH, -ZOOM_LOC_WIDTH };
static short move_y_pixel_array[] = { -ZOOM_LOC_HEIGHT, -ZOOM_LOC_HEIGHT, 0, ZOOM_LOC_HEIGHT, ZOOM_LOC_HEIGHT, ZOOM_LOC_HEIGHT, 0, -ZOOM_LOC_HEIGHT };
static short cycle_wait_unit_index;
static short *cycle_wait_unit_array;
static short cycle_wait_unit_array_def_size;
static short cycle_wait_unit_array_multipler;
static char move_action_call_flag=0; // avoid calling move_to_my_loc() if this function is called from move_to() chain
//--------- Begin of function Unit::reset_action_para ---------//
// reset action parameters when action is finished or cancelled
// for action_mode
//
void Unit::reset_action_para()
{
action_mode = ACTION_STOP;
action_x_loc = action_y_loc = -1;
action_para = 0;
}
//----------- End of function Unit::reset_action_para -----------//
//--------- Begin of function Unit::reset_action_para2 ---------//
// reset action parameters when action is finished or cancelled
//
// <int> keepMode - use to keep action
//
void Unit::reset_action_para2(int keepMode)
{
if(keepMode!=KEEP_DEFENSE_MODE || !in_any_defense_mode())
{
action_mode2 = ACTION_STOP;
action_para2 = 0;
action_x_loc2 = action_y_loc2 = -1;
}
else
{
switch(unit_mode)
{
case UNIT_MODE_DEFEND_TOWN:
if(action_mode2!=ACTION_AUTO_DEFENSE_DETECT_TARGET)
defend_town_detect_target();
break;
case UNIT_MODE_REBEL:
if(action_mode2!=ACTION_DEFEND_TOWN_DETECT_TARGET)
defense_detect_target();
break;
case UNIT_MODE_MONSTER:
if(action_mode2!=ACTION_MONSTER_DEFEND_DETECT_TARGET)
monster_defend_detect_target();
break;
}
}
}
//----------- End of function Unit::reset_action_para2 -----------//
//--------- Begin of function Unit::reset_action_misc_para ---------//
// reset miscellaneous action parameters
//
void Unit::reset_action_misc_para()
{
action_misc = ACTION_MISC_STOP;
action_misc_para = 0;
}
//----------- End of function Unit::reset_action_misc_para -----------//
//--------- Begin of function Unit::stop ---------//
// stop unit action
//
// [int] preserveAction - preserve the current action or not.
// (default: 0)
//
void Unit::stop(int preserveAction)
{
//------------- reset vars ------------//
if(action_mode2!=ACTION_MOVE)
reset_way_point_array();
reset_path();
err_when(result_node_array!=NULL);
//-------------- keep action or not --------------//
switch(preserveAction)
{
case 0: case KEEP_DEFENSE_MODE:
reset_action_para();
range_attack_x_loc = range_attack_y_loc = -1; // should set here or reset for attack
break;
case KEEP_PRESERVE_ACTION:
break;
/*case 3: err_when(action_mode!=ACTION_ATTACK_UNIT);
go_x = next_x; // combine move_to_attack() into move_to()
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
return;*/
}
waiting_term = 0; // for idle_detect_attack(), oscillate between 0 and 1
err_when(cur_dir<0 || cur_dir>MAX_SPRITE_DIR_TYPE);
err_when(final_dir<0 || final_dir>MAX_SPRITE_DIR_TYPE);
UnitMarine *shipPtr;
//----------------- update parameters ----------------//
switch( cur_action )
{
//----- if the unit is moving right now, ask it to stop as soon as possible -----//
case SPRITE_READY_TO_MOVE:
set_idle();
break;
case SPRITE_TURN:
case SPRITE_WAIT:
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
final_dir = cur_dir;
turn_delay = 0;
set_idle();
break;
case SPRITE_SHIP_EXTRA_MOVE:
shipPtr = (UnitMarine*) this;
switch(shipPtr->extra_move_in_beach)
{
case NO_EXTRA_MOVE:
if(cur_x==next_x && cur_y==next_y)
{
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
set_idle();
return;
}
break;
case EXTRA_MOVING_IN:
if(cur_x==next_x && cur_y==next_y && (cur_x!=go_x || cur_y!=go_y))
{
shipPtr->extra_move_in_beach = NO_EXTRA_MOVE; // not yet move although location is chosed
err_when(next_x_loc()%2 || next_y_loc()%2); // in even location
}
else
err_when(next_x_loc()%2==0 && next_y_loc()%2==0); // in even location
break;
case EXTRA_MOVING_OUT:
if(cur_x==next_x && cur_y==next_y && (cur_x!=go_x || cur_y!=go_y))
{
shipPtr->extra_move_in_beach = EXTRA_MOVE_FINISH; // not yet move although location is chosed
err_when(next_x_loc()%2==0 && next_y_loc()%2==0); // not in even location
}
else
err_when(next_x_loc()%2 || next_y_loc()%2); // in even location
break;
case EXTRA_MOVE_FINISH:
break;
}
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
break;
case SPRITE_MOVE:
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
if(cur_x==next_x && cur_y==next_y)
set_idle();
break;
//--- if its current action is SPRITE_ATTACK, stop immediately ---//
case SPRITE_ATTACK:
set_next(cur_x, cur_y, 0, 1); //********** BUGHERE
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
set_idle();
#ifdef DEBUG
char h, w, blocked=0;
short x, y;
for(h=0, y=next_y_loc(); h<sprite_info->loc_height&&!blocked; h++, y++)
{
for(w=0, x=next_x_loc(); w<sprite_info->loc_width&&!blocked; w++, x++)
err_when(world.get_unit_recno(x, y, mobile_type) != sprite_recno);
}
#endif
cur_frame = 1;
break;
}
}
//----------- End of function Unit::stop -----------//
//--------- Begin of function Unit::stop2 ---------//
// stop unit action
//
// preserverAction can be
// 0, KEEP_PRESERVE_ACTION, KEEP_DEFENSE_MODE (default 0)
//
void Unit::stop2(int preserveAction)
{
stop(preserveAction);
reset_action_para2(preserveAction);
//----------------------------------------------------------//
// set the original location of the attacking target when
// the attack() function is called, action_x_loc2 & action_y_loc2
// will change when the unit move, but these two will not.
//----------------------------------------------------------//
//##### begin trevor 13/10 #####//
force_move_flag = 0;
ai_no_suitable_action = 0;
if( preserveAction==0 )
{
original_action_mode = 0;
ai_original_target_x_loc = -1;
if( ai_action_id )
nation_array[nation_recno]->action_failure(ai_action_id, sprite_recno);
}
//##### end trevor 13/10 #####//
}
//----------- End of function Unit::stop2 -----------//
//######## begin trevor 26/4 ###########//
//--------- Begin of function Unit::set_search_tries ---------//
// used to limit the number of nodes in searching
//
// <int> tries - number of nodes used in searhcing
//
void Unit::set_search_tries(int tries)
{
unit_search_tries = tries;
unit_search_tries_flag++;
}
//----------- End of function Unit::set_search_tries -----------//
//--------- Begin of function Unit::reset_search_tries ---------//
// reset the number of node to default value
//
void Unit::reset_search_tries()
{
unit_search_tries = 0; // 0 for reset
unit_search_tries_flag = 0;
}
//----------- End of function Unit::reset_search_tries -----------//
//######## end trevor 26/4 ###########//
//-------- Begin of function Unit::abort_searching --------//
//
// <int> reuseSetNext - condition flag for updating parameters
// of path_reuse
//
void Unit::abort_searching(int reuseSetNext)
{
if(reuseSetNext) // to avoid error in path-reuse
seek_path_reuse.set_next_cur_path_num();
if(unit_search_tries_flag)
reset_search_tries();
}
//-------- End of function Unit::abort_searching ---------//
//--------- Begin of function Unit::enable_force_move ---------//
void Unit::enable_force_move()
{
force_move_flag = 1;
}
//-------- End of function Unit::enable_force_move ---------//
//--------- Begin of function Unit::disable_force_move ---------//
void Unit::disable_force_move()
{
force_move_flag = 0;
}
//-------- End of function Unit::disable_force_move ---------//
//--------- Begin of function Unit::move_to ---------//
// Main function for action_mode = ACTION_MOVE
//
// Order the unit to move to a specific location following the shortest path.
//
// <int> destX - x location of the destination
// <int> destY - y location of the destination
// [int] preserveAction - preserve the current action or not (default: 0)
// [short] searchMode - the search mode used (default: SEARCH_MODE_IN_A_GROUP)
// [short] miscNo - = target record no if search_mode=SEARCH_MODE_TO_ATTACK
// = firm ID if search_mode=SEARCH_MODE_TO_FIRM (default: 0)
// [short] numOfPath - num of path, used in path reuse, (default: 1)
// [short] reuseMode - path reuse mode (default: GENERAL_GROUP_MOVEMENT)
// [short] pathReuseStatus - path reuse status (default: 0)
//
void Unit::move_to(int destX, int destY, int preserveAction, short searchMode, short miscNo, short numOfPath, short reuseMode, short pathReuseStatus)
{
err_when(destX<0 || destX>=MAX_WORLD_X_LOC || destY<0 || destY>=MAX_WORLD_Y_LOC);
if(!seek_path.total_node_avail)
{
//-------- insufficient nodes for searching, return now ----------//
stop(KEEP_PRESERVE_ACTION);
action_mode = action_mode2 = ACTION_MOVE;
action_para = action_para2 = 0;
action_x_loc = action_x_loc2 = destX;
action_y_loc = action_y_loc2 = destY;
return; // for later searching
}
//int useClosestNode = (seek_path.total_node_avail>=MIN_BACKGROUND_NODE_USED_UP);
//---------- reset way point array since new action is assigned --------//
if(way_point_count)
{
ResultNode *nodePtr = way_point_array;
if(nodePtr->node_x!=destX || nodePtr->node_y!=destY)
reset_way_point_array();
}
//----------------------------------------------------------------//
// calculate new destination if trying to move to different territory
//----------------------------------------------------------------//
int curXLoc = next_x_loc();
int curYLoc = next_y_loc();
Location *locPtr = world.get_loc(curXLoc, curYLoc);
Location *destLocPtr = world.get_loc(destX, destY);
int destXLoc = destX;
int destYLoc = destY;
if(locPtr->region_id!=destLocPtr->region_id && mobile_type!=UNIT_AIR) // different territory
different_territory_destination(destXLoc, destYLoc);
//----------------------------------------------------------------//
// for path_reuse initialization
//----------------------------------------------------------------//
if(numOfPath!=1 && pathReuseStatus==REUSE_PATH_INITIAL) // for path-reuse only
{
search(destXLoc, destYLoc, preserveAction, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
return;
}
//----------------------------------------------------------------//
// return if the unit is dead
//----------------------------------------------------------------//
if(is_unit_dead())
{
abort_searching(searchMode==SEARCH_MODE_REUSE && numOfPath>1);
return;
}
//-----------------------------------------------------------------------------------//
// The codes here is used to check for equal action in movement.
//
// mainly checked by action_mode2. If previous action is ACTION_MOVE, action_mode2,
// action_para2, action_x_loc2 and action_x_loc2 need to be kept for this checking.
//
// If calling from unit_array.move_to(), action_mode is set to ACTION_MOVE, action_para
// is set to 0 while action_x_loc and action_y_loc are kept as original value for checking.
// Meanwhile, action_mode2, action_para2, action_x_loc2 and action_y_loc2 are kept if
// the condition is fulfilled (action_mode2==ACTION_MOVE)
//-----------------------------------------------------------------------------------//
if(action_mode2==ACTION_MOVE && action_mode==ACTION_MOVE)
{
//------ previous action is ACTION_MOVE -------//
err_when(action_para2 || action_para);
if(action_x_loc2==destXLoc && action_y_loc2==destYLoc)
{
//-------- equal order --------//
action_x_loc = action_x_loc2;
action_y_loc = action_y_loc2;
if(cur_action!=SPRITE_IDLE)
{
//-------- the old order is processing --------//
abort_searching(searchMode==SEARCH_MODE_REUSE && numOfPath>1);
if(result_node_array==NULL) // cannot move
{
err_when(result_path_dist);
if(unit_res[unit_id]->unit_class==UNIT_CLASS_SHIP)
{
if(cur_action!=SPRITE_SHIP_EXTRA_MOVE)
{
err_when(result_node_count || result_node_recno);
if(cur_x!=next_x || cur_y!=next_y)
set_move();
else
set_idle();
}
//else keep extra_moving
}
else
{
err_when(result_node_count || result_node_recno);
if(cur_x!=next_x || cur_y!=next_y)
set_move();
else
set_idle();
}
}
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
return;
}//else action is hold due to some problems, re-activiate again
}
}//else, new order or searching is required
move_action_call_flag++; // set flag to avoid calling move_to_my_loc()
seek_path_reuse.set_status(PATH_WAIT);
err_when(seek_path_reuse.get_reuse_path_status()==REUSE_PATH_INCOMPLETE_SEARCH);
action_mode2 = ACTION_MOVE;
action_para2 = 0;
int enoughNode = search(destXLoc, destYLoc, preserveAction, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
move_action_call_flag = 0; // clear the flag
//----------------------------------------------------------------//
// store new order in action parameters
//----------------------------------------------------------------//
action_mode = ACTION_MOVE;
action_para = 0;
if(!enoughNode || (searchMode==SEARCH_MODE_REUSE && seek_path_reuse.get_reuse_path_status()==REUSE_PATH_INCOMPLETE_SEARCH))
{
action_x_loc = action_x_loc2 = destXLoc;
action_y_loc = action_y_loc2 = destYLoc;
}
else // enough node for search
{
action_x_loc = action_x_loc2 = move_to_x_loc;
action_y_loc = action_y_loc2 = move_to_y_loc;
}
#ifdef DEBUG
if(result_node_array && result_node_count)
{
ResultNode *debugPtr = result_node_array + result_node_count - 1;
err_when(debugPtr->node_x != move_to_x_loc || debugPtr->node_y != move_to_y_loc);
}
else
{
UnitInfo* unitInfo = unit_res[unit_id];
if( unitInfo->unit_class == UNIT_CLASS_SHIP )
{
UnitMarine *shipPtr = (UnitMarine*) this;
if(shipPtr->extra_move_in_beach==NO_EXTRA_MOVE)
err_when(curXLoc!=move_to_x_loc || curYLoc!=move_to_y_loc);
}
else
{
err_when(curXLoc!=move_to_x_loc || curYLoc!=move_to_y_loc);
}
}
#endif
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
err_when(action_mode2==ACTION_MOVE && (action_x_loc2==-1 || action_y_loc2==-1));
}
//----------- End of function Unit::move_to -----------//
//--------- Begin of function Unit::search ---------//
// This function is only used to find the shorest path for
// searching. Action parameters should not be changed in
// this function.
//
// <int> destX - x coordinate of destination
// <int> destY - y coordinate of destination
// <int> preserveAction - used to keep action in calling stop()
// <short> searchMode - search mode being used
// <short> miscNo - see move_to()
// <short> numOfPath - num of path
// <short> reuseMode - path reuse mode being used
// <short> pathReuseStatus - path reuse status
//
// return 1 for normal operation process
// return 0 otherwise or there is not enough node for searching
//
int Unit::search(int destXLoc, int destYLoc, int preserveAction, short searchMode, short miscNo, short numOfPath, short reuseMode, short pathReuseStatus)
{
#ifdef DEBUG
err_when(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC);
err_when(searchMode==SEARCH_MODE_TO_FIRM && miscNo!=FIRM_HARBOR && mobile_type!=UNIT_AIR &&
world.get_loc(next_x_loc(), next_y_loc())->region_id!=world.get_loc(destXLoc, destYLoc)->region_id);
err_when(hit_points<=0 || action_mode==ACTION_DIE || cur_action==SPRITE_DIE);
#else
if(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC || hit_points<=0 ||
action_mode==ACTION_DIE || cur_action==SPRITE_DIE || searchMode<=0 || searchMode>MAX_SEARCH_MODE_TYPE)
{
stop2(KEEP_DEFENSE_MODE); //-********** BUGHERE, err_handling for retailed version
return 1;
}
#endif
//----------------------------------------------------------------//
// for path_reuse initialization
//----------------------------------------------------------------//
if(numOfPath!=1 && pathReuseStatus==REUSE_PATH_INITIAL) // for path-reuse only
{
seek_path_reuse.seek(next_x_loc(), next_y_loc(), move_to_x_loc, move_to_y_loc, sprite_info->loc_width,
unit_group_id, mobile_type, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
return 1;
}
int result=0;
if(unit_res[unit_id]->unit_class==UNIT_CLASS_SHIP)
{
UnitMarine *shipPtr = (UnitMarine*) this;
switch(shipPtr->extra_move_in_beach)
{
case NO_EXTRA_MOVE:
result = searching(destXLoc, destYLoc, preserveAction, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
break;
case EXTRA_MOVING_IN:
err_when(next_x_loc()%2==0 && next_y_loc()%2==0);
if(pathReuseStatus==REUSE_PATH_SEARCH || pathReuseStatus==REUSE_PATH_FIRST_SEEK)
seek_path_reuse.set_next_cur_path_num();
return 0;
case EXTRA_MOVING_OUT:
err_when(next_x_loc()%2 || next_y_loc()%2);
if(pathReuseStatus==REUSE_PATH_SEARCH || pathReuseStatus==REUSE_PATH_FIRST_SEEK)
seek_path_reuse.set_next_cur_path_num();
return 0;
case EXTRA_MOVE_FINISH:
err_when(next_x_loc()%2==0 && next_y_loc()%2==0);
if(pathReuseStatus==REUSE_PATH_SEARCH || pathReuseStatus==REUSE_PATH_FIRST_SEEK)
seek_path_reuse.set_next_cur_path_num();
ship_leave_beach(next_x_loc(), next_y_loc());
break;
default: err_here();
break;
}
}
else
result = searching(destXLoc, destYLoc, preserveAction, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
if(way_point_count && !result_node_array) // can move no more
reset_way_point_array();
if(!result)
return 0; // not enough node or extra_move_in_beach!=NO_EXTRA_MOVE
else
return 1;
}
//----------- End of function Unit::search -----------//
//--------- Begin of function Unit::select_search_sub_mode ---------//
// select searching sub_mode
//
// <int> sx - start location x
// <int> sy - start location y
// <int> dx - x coordinate of destination
// <int> dy - y coordinate of destination
// <short> nationRecno - nation recno of the unit
// <short> searchMode - search mode being used
//
void Unit::select_search_sub_mode(int sx, int sy, int dx, int dy, short nationRecno, short searchMode)
{
if( !config_adv.unit_allow_path_power_mode )
{
// cancel the selection
seek_path.set_sub_mode();
seek_path_reuse.set_sub_mode();
return;
}
err_when(mobile_type!=UNIT_LAND);
if(!nation_recno || ignore_power_nation)
{
seek_path.set_sub_mode(); // always using normal mode for independent unit
seek_path_reuse.set_sub_mode();
return;
}
//--------------------------------------------------------------------------------//
// Checking for starting location and destination to determine sub_mode used
// N - not hostile, H - hostile
// 1) N -> N, using normal mode
// 2) N -> H, H -> N, H -> H, using sub_mode SEARCH_SUB_MODE_PASSABLE
//--------------------------------------------------------------------------------//
Location *startLocPtr = world.get_loc(sx, sy);
Location *destLocPtr = world.get_loc(dx, dy);
Nation *nationPtr = nation_array[nationRecno];
int subModeOn = 1;
if((startLocPtr->power_nation_recno && !nationPtr->get_relation_passable(startLocPtr->power_nation_recno)) ||
(destLocPtr->power_nation_recno && !nationPtr->get_relation_passable(destLocPtr->power_nation_recno)))
subModeOn = 0;
if(subModeOn) // true only when both start and end locations are passable for this nation
{
seek_path.set_nation_passable(nationPtr->relation_passable_array);
seek_path.set_sub_mode(SEARCH_SUB_MODE_PASSABLE);
}
else
seek_path.set_sub_mode(); //----- normal sub mode, normal searching
//----------- set sub_mode of path-reuse if more than one unit are selected -----------//
if(subModeOn && searchMode==SEARCH_MODE_REUSE)
{
seek_path_reuse.set_nation_passable(nationPtr->relation_passable_array);
seek_path_reuse.set_sub_mode(SEARCH_SUB_MODE_PASSABLE);
}
else
seek_path_reuse.set_sub_mode();
}
//----------- End of function Unit::select_search_sub_mode -----------//
//--------- Begin of function Unit::searching ---------//
int Unit::searching(int destXLoc, int destYLoc, int preserveAction, short searchMode, short miscNo, short numOfPath, short reuseMode, short pathReuseStatus)
{
stop(preserveAction); // stop the unit as soon as possible
int startXLocLoc=next_x_loc(); // next location the sprite is moving towards
int startYLocLoc=next_y_loc();
int totalAvailableNode = seek_path.total_node_avail;
if(!avail_node_enough_for_search(startXLocLoc, startYLocLoc, destXLoc, destYLoc))
{
abort_searching(searchMode==4 && numOfPath>1);
return 0; // not enough node for searching
}
//stop(preserveAction); // stop the unit as soon as possible
//---------------------------------------------------------------------------//
// adjust the destination for unit size
//---------------------------------------------------------------------------//
/*err_when(sprite_info->loc_width!=sprite_info->loc_height);
if(sprite_info->loc_width>1) // not size 1x1
{
destXLoc = move_to_x_loc = MIN(destXLoc, MAX_WORLD_X_LOC-sprite_info->loc_width);
destYLoc = move_to_y_loc = MIN(destYLoc, MAX_WORLD_Y_LOC-sprite_info->loc_height);
}
else
{*/
move_to_x_loc = destXLoc;
move_to_y_loc = destYLoc;
//}
//------------------------------------------------------------//
// fast checking for destination == current location
//------------------------------------------------------------//
//if(startXLocLoc==move_to_x_loc && startYLocLoc==move_to_y_loc) // already here
if(startXLocLoc==destXLoc && startYLocLoc==destYLoc) // already here
{
if(cur_x!=next_x || cur_y!=next_y)
set_move();
else
set_idle();
err_when(move_to_x_loc!=startXLocLoc || move_to_y_loc!=startYLocLoc);
err_when(result_node_array!=NULL);
abort_searching(searchMode==SEARCH_MODE_REUSE && numOfPath>1);
return 1;
}
//------------------------ find the shortest path --------------------------//
//
// Note: seek() will never return PATH_SEEKING as the maxTries==max_node in
// calling seek()
//
// decide the searching to use according to the unit size
// assume the unit size is always 1x1, 2x2, 3x3 and so on
// i.e. sprite_info->loc_width == sprite_info->loc_height
//--------------------------------------------------------------------------//
result_node_recno = result_node_count = 0;
err_when(result_node_array!=NULL);
seek_path.set_nation_recno(nation_recno);
int seekResult;
#ifdef DEBUG
unsigned long seekPathStartTime = misc.get_time();
#endif
/*switch(sprite_info->loc_width)
{
case 1:*/
if(searchMode!=SEARCH_MODE_REUSE || numOfPath==1) // no need to call path_reuse
{
if(mobile_type==UNIT_LAND)
select_search_sub_mode(startXLocLoc, startYLocLoc, destXLoc, destYLoc, nation_recno, searchMode);
seekResult = seek_path.seek(startXLocLoc, startYLocLoc, destXLoc, destYLoc, unit_group_id,
mobile_type, searchMode, miscNo, numOfPath, unit_search_tries);
result_node_array = seek_path.get_result(result_node_count, result_path_dist);
seek_path.set_sub_mode(); // reset sub_mode searching
}
else // use path_reuse
{
err_when(reuseMode!=GENERAL_GROUP_MOVEMENT);
seekResult = seek_path_reuse.seek(startXLocLoc, startYLocLoc, destXLoc, destYLoc, 1, unit_group_id,
mobile_type, searchMode, miscNo, numOfPath, reuseMode, pathReuseStatus);
result_node_array = seek_path_reuse.get_result(result_node_count, result_path_dist);
}
#ifdef DEBUG
seek_path_profile_time = misc.get_time() - seekPathStartTime;
#endif
/* break;
default: err_here();
break;
}*/
if(seekResult==PATH_IMPOSSIBLE)
{
reset_path();
err_when(result_node_array || result_node_count);
}
//####### begin trevor 15/10 ########//
//-----------------------------------------------------------------------//
// update ignore_power_nation,seek_path_fail_count
//-----------------------------------------------------------------------//
if(ai_unit)
{
//----- set seek_path_fail_count ------//
if( seekResult==PATH_IMPOSSIBLE ||
(seekResult==PATH_NODE_USED_UP && // if all the nodes have been used up and the number of nodes original available is >= VALID_BACKGROUND_SEARCH_NODE
totalAvailableNode >= VALID_BACKGROUND_SEARCH_NODE) )
{
if( seek_path_fail_count < 100 ) // prevent numeric overflow
seek_path_fail_count++;
}
else
seek_path_fail_count=0;
//------- set ignore_power_nation -------//
if( seekResult==PATH_IMPOSSIBLE )
{
switch(ignore_power_nation)
{
case 0: ignore_power_nation = 1;
break;
case 1: ignore_power_nation = 2;
break;
case 2: break;
default: err_here();
break;
}
}
else
{
if( ignore_power_nation==1 )
ignore_power_nation = 0;
}
}
//######## end trevor 15/10 ########//
//-----------------------------------------------------------------------//
// if closest node is returned, the destination should not be the real
// location to go to. Thus, move_to_?_loc should be adjusted
//-----------------------------------------------------------------------//
if(result_node_array && result_node_count)
{
ResultNode* lastNode = result_node_array + result_node_count - 1;
move_to_x_loc = lastNode->node_x; // adjust move_to_?_loc
move_to_y_loc = lastNode->node_y;
result_node_recno = 1; // skip the first node which is the current location
if(cur_action != SPRITE_MOVE) // check if the unit is moving right now, wait until it reaches the nearest complete tile.
{
err_when(cur_action!=SPRITE_SHIP_EXTRA_MOVE && (cur_x!=next_x || cur_y!=next_y));
#ifdef DEBUG
int moveToXLoc = move_to_x_loc;
int moveToYLoc = move_to_y_loc;
#endif
ResultNode *nextNode = result_node_array + 1;
//set_dir(next_x_loc(), next_y_loc(), nextNode->node_x, nextNode->node_y);
set_dir(startXLocLoc, startYLocLoc, nextNode->node_x, nextNode->node_y);
next_move();
#ifdef DEBUG
err_when(move_action_call_flag && (moveToXLoc!=move_to_x_loc || moveToYLoc!=move_to_y_loc));
#endif
}
}
else // stay in the current location
{
err_when(result_node_array!=NULL);
move_to_x_loc = startXLocLoc; // adjust move_to_?_loc
move_to_y_loc = startYLocLoc;
err_when(move_to_x_loc!=startXLocLoc || move_to_y_loc!=startYLocLoc);
if(cur_x!=next_x || cur_y!=next_y)
set_move();
else
set_idle();
}
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
//-------------------------------------------------------//
// PATH_NODE_USED_UP happens when:
// Exceed the object's MAX's node limitation, the closest path
// is returned. Get to the closest path first and continue
// to seek the path in the background.
//-------------------------------------------------------//
return 1;
}
//----------- End of function Unit::searching -----------//
//--------- Begin of function Unit::move_to_firm_surround ---------//
// order unit to move to firm surrounding
//
// <int> destXLoc, destYLoc - destination
// <int> width - unit width
// <int> height - unit height
// <int> miscNo - the firm id (default 0)
// <int> readyDist - similar to that in set_move_to_surround()
// (default 0)
//
// Note: the firm should exist in the location (destXloc, destYLoc)
// this function can be called by unit with size 1x1, 2x2,
//
void Unit::move_to_firm_surround(int destXLoc, int destYLoc, int width, int height, int miscNo, int readyDist)
{
err_when(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC);
//----------------------------------------------------------------//
// calculate new destination if trying to move to different territory
//----------------------------------------------------------------//
Location *locPtr = world.get_loc(destXLoc, destYLoc);
if(unit_res[unit_id]->unit_class==UNIT_CLASS_SHIP && miscNo==FIRM_HARBOR)
{
err_when(!locPtr->is_firm());
Firm *firmPtr = firm_array[locPtr->firm_recno()];
FirmHarbor *harborPtr = (FirmHarbor*) firmPtr;
if(world.get_loc(next_x_loc(), next_y_loc())->region_id!=harborPtr->sea_region_id)
{
move_to(destXLoc, destYLoc);
return;
}
}
else
{
if(world.get_loc(next_x_loc(), next_y_loc())->region_id!=locPtr->region_id)
{
move_to(destXLoc, destYLoc);
return;
}
}
//----------------------------------------------------------------//
// return if the unit is dead
//----------------------------------------------------------------//
if(is_unit_dead())
return;
//----------------------------------------------------------------//
// check for equal actions
//----------------------------------------------------------------//
if(action_mode2==ACTION_MOVE && action_mode==ACTION_MOVE)
{
//------ previous action is ACTION_MOVE -------//
err_when(action_para2 || action_para);
if(action_x_loc2==destXLoc && action_y_loc2==destYLoc)
{
//-------- equal order --------//
action_x_loc = action_x_loc2;
action_y_loc = action_y_loc2;
if(cur_action!=SPRITE_IDLE)
{
//-------- the old order is processing --------//
if(result_node_array==NULL) // cannot move
{
err_when(result_node_count || result_node_recno);
set_idle();
}
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
return;
}//else action is hold due to some problems, re-activiate again
}
}//else, new order or searching is required
int destX = MAX(0, ((width>1) ? destXLoc : destXLoc - width + 1));
int destY = MAX(0, ((height>1) ? destYLoc : destYLoc - height + 1));
FirmInfo *firmInfo = firm_res[miscNo];
stop();
set_move_to_surround(destX, destY, firmInfo->loc_width, firmInfo->loc_height, BUILDING_TYPE_FIRM_MOVE_TO, miscNo);
//----------------------------------------------------------------//
// store new order in action parameters
//----------------------------------------------------------------//
action_mode = action_mode2 = ACTION_MOVE;
action_para = action_para2 = 0;
action_x_loc = action_x_loc2 = move_to_x_loc;
action_y_loc = action_y_loc2 = move_to_y_loc;
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
#ifdef DEBUG
if(unit_res[unit_id]->unit_class!=UNIT_CLASS_SHIP || ((UnitMarine*)this)->extra_move_in_beach==NO_EXTRA_MOVE)
err_when(result_node_array==NULL && (next_x!=go_x || next_y!=go_y));
#endif
}
//----------- End of function Unit::move_to_firm_surround -----------//
//--------- Begin of function Unit::move_to_town_surround ---------//
// move to town surrounding
//
// <int> destXLoc, destYLoc - destination
// <int> width - unit width
// <int> height - unit height
// <int> miscNo - reserved (default 0)
// <int> readyDist - similar to that in set_move_to_surround()
// (default 0)
//
// Note: assume the town exists
// this function can be called by unit with size 1x1, 2x2,
//
void Unit::move_to_town_surround(int destXLoc, int destYLoc, int width, int height, int miscNo, int readyDist)
{
err_when(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC);
//----------------------------------------------------------------//
// calculate new destination if trying to move to different territory
//----------------------------------------------------------------//
Location *locPtr = world.get_loc(destXLoc, destYLoc);
if(world.get_loc(next_x_loc(), next_y_loc())->region_id!=locPtr->region_id)
{
move_to(destXLoc, destYLoc);
return;
}
//----------------------------------------------------------------//
// return if the unit is dead
//----------------------------------------------------------------//
if(is_unit_dead())
return;
//----------------------------------------------------------------//
// check for equal actions
//----------------------------------------------------------------//
if(action_mode2==ACTION_MOVE && action_mode==ACTION_MOVE)
{
//------ previous action is ACTION_MOVE -------//
err_when(action_para2 || action_para);
if(action_x_loc2==destXLoc && action_y_loc2==destYLoc)
{
//-------- equal order --------//
action_x_loc = action_x_loc2;
action_y_loc = action_y_loc2;
if(cur_action!=SPRITE_IDLE)
{
//-------- the old order is processing --------//
if(result_node_array==NULL) // cannot move
{
err_when(result_node_count || result_node_recno);
set_idle();
}
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
return;
}//else action is hold due to some problems, re-activiate again
}
}//else, new order or searching is required
int destX = MAX(0, ((width>1) ? destXLoc : destXLoc - width + 1));
int destY = MAX(0, ((height>1) ? destYLoc : destYLoc - height + 1));
stop();
set_move_to_surround(destX, destY, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, BUILDING_TYPE_TOWN_MOVE_TO);
//----------------------------------------------------------------//
// store new order in action parameters
//----------------------------------------------------------------//
action_mode = action_mode2 = ACTION_MOVE;
action_para = action_para2 = 0;
action_x_loc = action_x_loc2 = move_to_x_loc;
action_y_loc = action_y_loc2 = move_to_y_loc;
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
err_when(result_node_array==NULL && (next_x!=go_x || next_y!=go_y));
}
//----------- End of function Unit::move_to_town_surround -----------//
//--------- Begin of function Unit::move_to_wall_surround ---------//
// move to wall surrounding
//
// <int> destXLoc, destYLoc - destination
// <int> width - unit width
// <int> height - unit height
// <int> miscNo - reserved (default 0)
// <int> readyDist - similar to that in set_move_to_surround()
// (default 0)
//
// Note: assume the wall exists
// this function can be called by unit with size 1x1, 2x2,
//
void Unit::move_to_wall_surround(int destXLoc, int destYLoc, int width, int height, int miscNo, int readyDist)
{
err_when(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC);
//----------------------------------------------------------------//
// calculate new destination if trying to move to different territory
//----------------------------------------------------------------//
Location *locPtr = world.get_loc(destXLoc, destYLoc);
if(world.get_loc(next_x_loc(), next_y_loc())->region_id!=locPtr->region_id)
{
move_to(destXLoc, destYLoc);
return;
}
//----------------------------------------------------------------//
// return if the unit is dead
//----------------------------------------------------------------//
if(is_unit_dead())
return;
//----------------------------------------------------------------//
// check for equal actions
//----------------------------------------------------------------//
if(action_mode2==ACTION_MOVE && action_mode==ACTION_MOVE)
{
//------ previous action is ACTION_MOVE -------//
err_when(action_para2 || action_para);
if(action_x_loc2==destXLoc && action_y_loc2==destYLoc)
{
//-------- equal order --------//
action_x_loc = action_x_loc2;
action_y_loc = action_y_loc2;
if(cur_action!=SPRITE_IDLE)
{
//-------- the old order is processing --------//
if(result_node_array==NULL) // cannot move
{
err_when(result_node_count || result_node_recno);
set_idle();
}
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
return;
}//else action is hold due to some problems, re-activiate again
}
}//else, new order or searching is required
int destX = MAX(0, ((width>1) ? destXLoc : destXLoc - width + 1));
int destY = MAX(0, ((height>1) ? destYLoc : destYLoc - height + 1));
stop();
set_move_to_surround(destX, destY, 1, 1, BUILDING_TYPE_WALL);
//----------------------------------------------------------------//
// store new order in action parameters
//----------------------------------------------------------------//
action_mode = action_mode2 = ACTION_MOVE;
action_para = action_para2 = 0;
action_x_loc = action_x_loc2 = move_to_x_loc;
action_y_loc = action_y_loc2 = move_to_y_loc;
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
err_when(result_node_array==NULL && (next_x!=go_x || next_y!=go_y));
}
//----------- End of function Unit::move_to_wall_surround -----------//
//--------- Begin of function Unit::move_to_unit_surround ---------//
// move to unit surrounding
//
// <int> destXLoc, destYLoc - destination
// <int> width - unit width
// <int> height - unit height
// <int> miscNo - vehicle unit_recno (default 0)
// <int> readyDist - similar to that in set_move_to_surround()
// (default 0)
//
// Note: assume the vehicle exists
// this function can be called by unit with size 1x1, 2x2,
//
void Unit::move_to_unit_surround(int destXLoc, int destYLoc, int width, int height, int miscNo, int readyDist)
{
err_when(destXLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc<0 || destYLoc>=MAX_WORLD_Y_LOC);
//----------------------------------------------------------------//
// calculate new destination if trying to move to different territory
//----------------------------------------------------------------//
Location *locPtr = world.get_loc(destXLoc, destYLoc);
if(world.get_loc(next_x_loc(), next_y_loc())->region_id!=locPtr->region_id)
{
move_to(destXLoc, destYLoc);
return;
}
//----------------------------------------------------------------//
// return if the unit is dead
//----------------------------------------------------------------//
if(is_unit_dead())
return;
//----------------------------------------------------------------//
// check for equal actions
//----------------------------------------------------------------//
if(action_mode2==ACTION_MOVE && action_mode==ACTION_MOVE)
{
//------ previous action is ACTION_MOVE -------//
err_when(action_para2 || action_para);
if(action_x_loc2==destXLoc && action_y_loc2==destYLoc)
{
//-------- equal order --------//
action_x_loc = action_x_loc2;
action_y_loc = action_y_loc2;
if(cur_action!=SPRITE_IDLE)
{
//-------- the old order is processing --------//
if(result_node_array==NULL) // cannot move
{
err_when(result_node_count || result_node_recno);
set_idle();
}
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
return;
}//else action is hold due to some problems, re-activiate again
}
}//else, new order or searching is required
int destX = MAX(0, ((width>1) ? destXLoc : destXLoc - width + 1));
int destY = MAX(0, ((height>1) ? destYLoc : destYLoc - height + 1));
err_when(unit_array.is_deleted(miscNo));
Unit *unitPtr = unit_array[miscNo];
SpriteInfo *spriteInfo = unitPtr->sprite_info;
stop();
set_move_to_surround(destX, destY, spriteInfo->loc_width, spriteInfo->loc_height, BUILDING_TYPE_VEHICLE);
//----------------------------------------------------------------//
// store new order in action parameters
//----------------------------------------------------------------//
action_mode = action_mode2 = ACTION_MOVE;
action_para = action_para2 = 0;
action_x_loc = action_x_loc2 = move_to_x_loc;
action_y_loc = action_y_loc2 = move_to_y_loc;
err_when(action_mode==ACTION_MOVE && (action_x_loc==-1 || action_y_loc==-1));
err_when(move_to_x_loc<0 || move_to_x_loc>=MAX_WORLD_X_LOC || move_to_y_loc<0 || move_to_y_loc>=MAX_WORLD_Y_LOC);
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
err_when(result_node_array==NULL && (next_x!=go_x || next_y!=go_y));
}
//----------- End of function Unit::move_to_unit_surround -----------//
//--------- Begin of function Unit::different_territory_destination ---------//
// calculate a reachable destination if the unit is ordered to move to unreachable
// location on different territory
//
// <int&> destX, destY - reference to return destination
//
void Unit::different_territory_destination(int& destX, int& destY)
{
int curXLoc = next_x_loc();
int curYLoc = next_y_loc();
Location *locPtr = world.get_loc(curXLoc, curYLoc);
int regionId = locPtr->region_id;
int xStep = destX-curXLoc;
int yStep = destY-curYLoc;
int absXStep = abs(xStep);
int absYStep = abs(yStep);
int count = (absXStep>=absYStep) ? absXStep : absYStep;
int x, y;
long int sameTerr = 0;
//------------------------------------------------------------------------------//
// draw a line from the unit location to the destination, find the last location
// with the same region id.
//------------------------------------------------------------------------------//
for(long int i=1; i<=count; i++)
{
x = curXLoc + int((i*xStep)/count);
y = curYLoc + int((i*yStep)/count);
locPtr = world.get_loc(x, y);
if(locPtr->region_id==regionId)
sameTerr = i;
}
if(sameTerr)
{
destX = curXLoc + int((sameTerr*xStep)/count);
destY = curYLoc + int((sameTerr*yStep)/count);
}
else
{
destX = curXLoc;
destY = curYLoc;
}
}
//----------- End of function Unit::different_territory_destination -----------//
//--------- Begin of function Unit::next_move ---------//
//
// If there is unprocessed node(s) in the result_node_array,
// then next unprocessed node will be set to be the next location
// to move to. (i.e. go_? = location of the unprocessed node)
//
void Unit::next_move()
{
if(result_node_array == NULL || !result_node_count || !result_node_recno)
return;
if( ++result_node_recno > result_node_count )
{
//------------ all nodes are visited --------------//
err_when(cur_x!=next_x || cur_y!=next_y);
err_when(next_x_loc()!=move_to_x_loc || next_y_loc()!=move_to_y_loc);
mem_del(result_node_array);
result_node_array = NULL;
set_idle();
if(action_mode2==ACTION_MOVE) //--------- used to terminate action_mode==ACTION_MOVE
{
force_move_flag = 0;
//------- reset ACTION_MOVE parameters ------//
reset_action_para();
if(move_to_x_loc==action_x_loc2 && move_to_y_loc==action_y_loc2)
reset_action_para2();
}
return;
}
//---- order the unit to move to the next checkpoint following the path ----//
ResultNode* resultNode = result_node_array+result_node_recno-1;
#ifdef DEBUG
err_when(cur_x==move_to_x_loc*ZOOM_LOC_WIDTH && cur_y==move_to_y_loc*ZOOM_LOC_HEIGHT);
err_when(!resultNode);
#endif
sprite_move( resultNode->node_x*ZOOM_LOC_WIDTH, resultNode->node_y*ZOOM_LOC_HEIGHT );
err_when(cur_x==go_x && cur_y==go_y && (cur_x!=next_x || cur_y!=next_y));
}
//----------- End of function Unit::next_move -----------//
//--------- Begin of function Unit::reset_path ---------//
// Cancel all movement.
//
void Unit::reset_path()
{
if( result_node_array )
{
mem_del(result_node_array);
result_node_array = NULL;
}
result_path_dist = result_node_count = result_node_recno = 0;
}
//----------- End of function Unit::reset_path -----------//
//--------- Begin of function Unit::pre_process ---------//
// process unit's action
//
void Unit::pre_process()
{
//-*********** simulate aat ************-//
#ifdef DEBUG
if(debug_sim_game_type==2)
{
if(hit_points!=max_hit_points)
hit_points = max_hit_points;
Nation *nationPtr = nation_array[nation_recno];
if(nationPtr->cash<4000)
nationPtr->cash += 10000;
if(nationPtr->food<4000)
nationPtr->food += 10000;
}
#endif
//-*********** simulate aat ************-//
#if defined(DEBUG) && defined(ENABLE_LOG)
String logStr;
logStr = " begin unit ";
logStr += sprite_recno;
// ########## begin Gilbert 6/9 #######//
logStr += " nation=";
logStr += nation_recno;
logStr += "(";
logStr += true_nation_recno();
logStr += ")";
// ########## end Gilbert 6/9 #######//
logStr += " pre_process(), action_mode=";
logStr += action_mode;
logStr += " action_mode2=";
logStr += action_mode2;
LOG_MSG(logStr);
logStr = "action_para=";
logStr += action_para;
logStr += " action_para2=";
logStr += action_para2;
logStr += " cur_action=";
logStr += cur_action;
logStr += " cur_x/y=";
logStr += cur_x;
logStr += "/";
logStr += cur_y;
logStr += " next_x/y=";
logStr += next_x;
logStr += "/";
logStr += next_y;
LOG_MSG(logStr);
#endif
//------ if all the hit points are lost, die now ------//
if(hit_points <= 0 && action_mode != ACTION_DIE)
{
set_die();
if(ai_action_id)
nation_array[nation_recno]->action_failure(ai_action_id, sprite_recno);
return;
}
#ifdef DEBUG
int debugActionMode = action_mode;
int debugActionPatra = action_para;
int debugCurAction = cur_action;
#endif
if( config.fog_of_war )
{
if( is_own() ||
(nation_recno && nation_array[nation_recno]->is_allied_with_player) )
{
world.visit(next_x_loc(), next_y_loc(), next_x_loc()+sprite_info->loc_width-1,
next_y_loc()+sprite_info->loc_height-1, unit_res[unit_id]->visual_range,
unit_res[unit_id]->visual_extend);
}
}
//--------- process action corresponding to action_mode ----------//
#ifdef DEBUG
unsigned long startTime;
#endif
switch(action_mode)
{
case ACTION_ATTACK_UNIT:
#ifdef DEBUG
startTime = misc.get_time();
#endif
//------------------------------------------------------------------//
// if unit is in defense mode, check situation to follow the target
// or return back to camp
//------------------------------------------------------------------//
if(action_mode!=action_mode2)
{
if(action_mode2==ACTION_AUTO_DEFENSE_ATTACK_TARGET)
{
if(!defense_follow_target()) // false if abort attacking
break; // cancel attack and go back to military camp
}
else if(action_mode2==ACTION_DEFEND_TOWN_ATTACK_TARGET)
{
if(!defend_town_follow_target())
break;
}
else if(action_mode2==ACTION_MONSTER_DEFEND_ATTACK_TARGET)
{
if(!monster_defend_follow_target())
break;
}
else
err_here();
}
process_attack_unit();
#ifdef DEBUG
unit_attack_profile_time += misc.get_time() - startTime;
#endif
break;
case ACTION_ATTACK_FIRM:
process_attack_firm();
break;
case ACTION_ATTACK_TOWN:
process_attack_town();
break;
case ACTION_ATTACK_WALL:
process_attack_wall();
break;
case ACTION_ASSIGN_TO_FIRM:
case ACTION_ASSIGN_TO_TOWN:
case ACTION_ASSIGN_TO_VEHICLE:
#ifdef DEBUG
startTime = misc.get_time();
#endif
process_assign();
#ifdef DEBUG
unit_assign_profile_time += misc.get_time() - startTime;
#endif
break;
case ACTION_ASSIGN_TO_SHIP:
process_assign_to_ship();
break;
case ACTION_BUILD_FIRM:
process_build_firm();
break;
case ACTION_BURN:
process_burn();
break;
case ACTION_SETTLE:
process_settle();
break;
case ACTION_SHIP_TO_BEACH:
process_ship_to_beach();
break;
case ACTION_GO_CAST_POWER:
process_go_cast_power();
break;
}
//-****** don't add code here, the unit may be removed after the above function call*******-//
// ###### begin Gilbert 20/6 ########//
#ifdef DEBUG
// do not read data member
LOG_MSG( " end Unit::pre_process()" );
LOG_MSG( misc.get_random_seed() );
#endif
// ###### end Gilbert 20/6 ########//
}
//----------- End of function Unit::pre_process -----------//
//--------- Begin of function Unit::process_die ---------//
// process unit die
//
// return 1 if die frame is counting
// return 0 otherwise
//
int Unit::process_die()
{
//-*********** simulate aat ************-//
#ifdef DEBUG
if(debug_sim_game_type)
{
cur_action = SPRITE_IDLE;
hit_points = max_hit_points;
stop2();
return 0;
}
#endif
//-*********** simulate aat ************-//
//--------- voice ------------//
se_res.sound(cur_x_loc(), cur_y_loc(), cur_frame, 'S',sprite_id,"DIE");
// ####### begin Gilbert 14/7 ###########//
//------------- add die effect on first frame --------- //
if( cur_frame == 1 && unit_res[unit_id]->die_effect_id)
{
Effect::create(unit_res[unit_id]->die_effect_id, cur_x, cur_y,
SPRITE_DIE, cur_dir, mobile_type == UNIT_AIR ? 8 : 2, 0);
}
// ####### end Gilbert 14/7 ###########//
//--------- next frame ---------//
if( ++cur_frame > sprite_info->die.frame_count )
return 1;
return 0;
}
//----------- End of function Unit::process_die -----------//
//--------- Begin of function Unit::process_rebel ---------//
//
// Unit::process_rebel() is in OREBEL.CPP
//
//----------- End of function Unit::process_rebel ---------//
//--------- Begin of function Unit::avail_node_enough_for_search --------//
// decide whether the available number of nodes enough for a valid path searching
//
// <short> x1, y1 - start location
// <short> x2, y2 - end location
//
// return 1 if num of nodes is enough
// return 0 otherwise
//
int Unit::avail_node_enough_for_search(short x1, short y1, short x2, short y2)
{
short dispX = abs(x1-x2);
short dispY = abs(y1-y2);
short majDist = dispX>dispY ? dispX : dispY;
short minDist = abs(dispX-dispY);
int nodeRequire = MIN(VALID_BACKGROUND_SEARCH_NODE, majDist<<5); // *32
int totalNode = seek_path.total_node_avail;
if(totalNode < nodeRequire)
{
if(totalNode>=MIN_BACKGROUND_NODE_USED_UP)
seek_path.total_node_avail = MIN_BACKGROUND_NODE_USED_UP-1;
return 0;
}
return 1;
}
//----------- End of function Unit::avail_node_enough_for_search -----------//
//--------- Begin of function Unit::process_move --------//
// process unit movement
//
void Unit::process_move()
{
//----- if the sprite has reach the destintion ----//
//--------------------------------------------------------//
// if the unit reach its destination, then
// cur_? == next_? == go_?
//--------------------------------------------------------//
err_when(cur_x==go_x && cur_y==go_y && (cur_x!=next_x || cur_y!=next_y));
if(cur_x==go_x && cur_y==go_y)
{
if(result_node_array)
{
next_move();
if( cur_action != SPRITE_MOVE ) // if next_move() is not successful, the movement has been stopped
return;
//---------------------------------------------------------------------------//
// If (1) the unit is blocked at cur_? == go_? and go_? != destination and
// (2) a new path is generated if calling the previous next_move(),
// then cur_? still equal to go_?.
//
// The following Sprite::process_move() call will set the unit to SPRITE_IDLE
// since cur_? == go_?. Thus, the unit terminates its move although it has not
// reached its destination.
//
// (note: if it has reached its destination, cur_? == go_? and cur_action =
// SPRITE_IDLE)
//
// if the unit is still moving and cur_? == go_?, call next_move() again to reset
// the go_?.
//---------------------------------------------------------------------------//
if(cur_action==SPRITE_MOVE && cur_x==go_x && cur_y==go_y)
next_move();
}
}
err_when(result_node_array && result_node_count==result_node_recno &&
(result_node_array[result_node_count-1].node_x!=go_x>>ZOOM_X_SHIFT_COUNT ||
result_node_array[result_node_count-1].node_y!=go_y>>ZOOM_Y_SHIFT_COUNT));
//--------- process the move, update sprite position ---------//
//--------------------------------------------------------//
// if the unit is moving, cur_?!=go_? and
// if next_? != cur_?, the direction from cur_? to next_?
// should equal to that from cur_? to go_?
//--------------------------------------------------------//
err_when((cur_x%ZOOM_LOC_WIDTH==0 && cur_y%ZOOM_LOC_HEIGHT==0) && (cur_x!=next_x || cur_y!=next_y) &&
((check_unit_dir1=get_dir(cur_x, cur_y, go_x, go_y))!=(check_unit_dir2=get_dir(cur_x, cur_y, next_x, next_y))));
#ifdef DEBUG
int debugCurX = cur_x;
int debugCurY = cur_y;
int debugNextX = next_x;
int debugNextY = next_y;
int debugGoX = go_x;
int debugGoY = go_y;
#endif
err_when(cur_x-next_x!=0 && cur_y-next_y!=0 && abs(next_x-cur_x)!=abs(next_y-cur_y));
err_when(result_path_dist && result_node_array==NULL);
Sprite::process_move();
err_when( cur_x < 0 || cur_y < 0 || cur_x >= ZOOM_X_PIXELS || cur_y >= ZOOM_Y_PIXELS );
if(cur_x==go_x && cur_y==go_y && cur_action==SPRITE_IDLE) // the sprite has reached its destination
{
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
}
//--------------------------------------------------------//
// after Sprite::process_move(), if the unit is blocked, its
// cur_action is set to SPRITE_WAIT. Otherwise, its cur_action
// is still SPRITE_MOVE. Then cur_? != next_? if the unit
// has not reached its destination.
//--------------------------------------------------------//
err_when(cur_action==SPRITE_MOVE && (cur_x!=go_x || cur_y!=go_y) && (cur_x==next_x && cur_y==next_y));
}
//---------- End of function Unit::process_move ----------//
//--------- Begin of function Unit::process_wait ---------//
// process unit's waiting
//
void Unit::process_wait()
{
err_when((check_unit_dir1=get_dir(cur_x, cur_y, go_x, go_y))!=final_dir);
if(!match_dir())
return;
//-----------------------------------------------------//
// If the unit is moving to the destination and was
// blocked by something. If it is now no longer blocked,
// continue the movement.
//-----------------------------------------------------//
//
// When this funciton is called:
//
// (next_x, next_y)==(cur_x, cur_y), it's the location of the sprite.
//
//-----------------------------------------------------//
//--- find out the next location which the sprite should be moving towards ---//
//-----------------------------------------------------//
// If the unit is waiting,
// go_? != cur_?
// go_? != next_?
//
// If the unit is not under swapping, the next_?_loc()
// is always the move_to_?_loc. Thus, the unit is ordered
// to stop.
//-----------------------------------------------------//
err_when( (go_x>>ZOOM_X_SHIFT_COUNT!=move_to_x_loc || go_y>>ZOOM_Y_SHIFT_COUNT!=move_to_y_loc) &&
( (cur_x==go_x && cur_y==go_y) || (cur_x!=next_x || cur_y!=next_y) ) );
if(next_x_loc()==move_to_x_loc && next_y_loc()==move_to_y_loc && !swapping)
{
terminate_move();
return; // terminate since already in destination
}
int stepMagn = move_step_magn();
int nextX = cur_x+stepMagn*move_x_pixel_array[final_dir];
int nextY = cur_y+stepMagn*move_y_pixel_array[final_dir];
/*short w, h, blocked=0;
short x, y, blockedX, blockedY;
Location* locPtr;
//---------- check whether the unit is blocked -----------//
for(h=0, y=nextY>>ZOOM_Y_SHIFT_COUNT; h<sprite_info->loc_height && !blocked; h++, y++)
{
for(w=0, x=nextX>>ZOOM_X_SHIFT_COUNT; w<sprite_info->loc_width && !blocked; w++, x++)
{
locPtr = world.get_loc(x, y);
blocked = ( (!locPtr->is_accessible(mobile_type)) || (locPtr->has_unit(mobile_type) &&
locPtr->unit_recno(mobile_type)!=sprite_recno) );
if(blocked)
{
blockedX = x;
blockedY = y;
}
}
}*/
short x = nextX>>ZOOM_X_SHIFT_COUNT;
short y = nextY>>ZOOM_Y_SHIFT_COUNT;
Location *locPtr = world.get_loc(x, y);
short blocked = ( (!locPtr->is_accessible(mobile_type)) || (locPtr->has_unit(mobile_type) &&
locPtr->unit_recno(mobile_type)!=sprite_recno) );
if(!blocked || move_action_call_flag)
{
//--------- not blocked, continue to move --------//
waiting_term = 0;
set_move();
cur_frame = 1;
set_next(nextX, nextY, -stepMagn, 1);
}
else
{
//------- blocked, call handle_blocked_move() ------//
//locPtr = world.get_loc(blockedX, blockedY);
err_when(cur_x!=next_x || cur_y!=next_y);
handle_blocked_move(locPtr);
}
err_when(cur_action==SPRITE_MOVE && (cur_x!=go_x || cur_y!=go_y) && (cur_x==next_x && cur_y==next_y));
err_when(cur_x==go_x && cur_y==go_y && (cur_x!=next_x || cur_y!=next_y));
}
//----------- End of function Unit::process_wait -----------//
//--------- Begin of function Unit::set_next --------//
// set the next coordinates to move to
//
// <int> newNextX, newNextY - next coordinate to move to
// <int> para - used to count the result_path_dist
// <int> blockedChecked - whether the next location specified is checked to be
// non-blocked. 1 checked for non-blocked, 0 for not checked
//
void Unit::set_next(int newNextX, int newNextY, int para, int blockedChecked)
{
err_when(!is_visible());
int curNextXLoc = next_x_loc();
int curNextYLoc = next_y_loc();
int newNextXLoc = newNextX >> ZOOM_X_SHIFT_COUNT;
int newNextYLoc = newNextY >> ZOOM_Y_SHIFT_COUNT;
#ifdef DEBUG
int debugStepMagn = move_step_magn();
err_when(abs(curNextXLoc-newNextXLoc)>debugStepMagn || abs(curNextYLoc-newNextYLoc)>debugStepMagn);
#endif
if(curNextXLoc!=newNextXLoc || curNextYLoc!=newNextYLoc)
{
if(!match_dir())
{
set_wait();
return;
}
}
//short w, h, blocked=0;
//short x, y, blockedX, blockedY;
short w, h, blocked=0;
short x, y;
#ifdef DEBUG
for(h=0, y=curNextYLoc; h<sprite_info->loc_height; h++, y++)
for(w=0, x=curNextXLoc; w<sprite_info->loc_width; w++, x++)
err_when( world.get_unit_recno(x, y, mobile_type) != sprite_recno ); // it must be 0 to put the sprite in this location
#endif
if( curNextXLoc != newNextXLoc || curNextYLoc != newNextYLoc )
{
//------- if the next location is blocked ----------//
Location* locPtr;
if(!blockedChecked)
{
/*for(h=0, y=newNextYLoc; h<sprite_info->loc_height && !blocked; h++, y++)
{
for(w=0, x=newNextXLoc; w<sprite_info->loc_width && !blocked; w++, x++)
{
locPtr = world.get_loc(x, y);
blocked = ( (!locPtr->is_accessible(mobile_type)) || (locPtr->has_unit(mobile_type) &&
locPtr->unit_recno(mobile_type)!=sprite_recno) );
if(blocked)
{
blockedX = x;
blockedY = y;
}
}
}*/
x = newNextXLoc;
y = newNextYLoc;
locPtr = world.get_loc(x, y);
blocked = ( (!locPtr->is_accessible(mobile_type)) || (locPtr->has_unit(mobile_type) &&
locPtr->unit_recno(mobile_type)!=sprite_recno) );
}//else, then blockedChecked = 0
//--- no change to next_x & next_y if the new next location is blocked ---//
if(blocked)
{
set_cur(next_x, next_y); // align the sprite to 32x32 location when it stops
//------ avoid infinitely looping in calling handle_blocked_move() ------//
if(blocked_by_member || move_action_call_flag)
{
set_wait();
blocked_by_member = 0;
}
else
{
locPtr = world.get_loc(x, y);
handle_blocked_move(locPtr);
}
#ifdef DEBUG
for(h=0, y=next_y_loc(); h<sprite_info->loc_height; h++, y++)
{
for(w=0, x=next_x_loc(); w<sprite_info->loc_width; w++, x++)
err_when( world.get_unit_recno(x, y, mobile_type) != sprite_recno ); // it must be 0 to put the sprite in this location
}
#endif
}
else
{
err_when(mobile_type!=UNIT_LAND && (abs(para)!=2 || result_path_dist%2));
if(para)
{
#ifdef DEBUG
int count=0, ii;
int dist;
int curXLoc = next_x_loc();
int curYLoc = next_y_loc();
ResultNode *curNode = result_node_array + result_node_recno -1;
ResultNode *nextNode;
dist = misc.points_distance(curXLoc, curYLoc, curNode->node_x, curNode->node_y);
if(result_node_recno>1)
count += dist;
else
count -= dist;
for(ii=result_node_recno, nextNode=curNode+1; ii<result_node_count; ii++, curNode++, nextNode++)
{
dist = misc.points_distance(nextNode->node_x, nextNode->node_y, curNode->node_x, curNode->node_y);
count += dist;
}
err_when(result_path_dist!=count);
#endif
//----------------------------------------------------------------------------//
// calculate the result_path_dist as the unit move from one tile to another
//----------------------------------------------------------------------------//
result_path_dist += para;
}
next_x = newNextX;
next_y = newNextY;
swapping = blocked_by_member = 0;
//---- move sprite_recno to the next location ------//
for(h=0, y=curNextYLoc; h<sprite_info->loc_height; h++, y++)
{
for(w=0, x=curNextXLoc; w<sprite_info->loc_width; w++, x++)
world.set_unit_recno(x, y, mobile_type, 0);
}
for(h=0, y=next_y_loc(); h<sprite_info->loc_height; h++, y++)
{
for(w=0, x=next_x_loc(); w<sprite_info->loc_width; w++, x++)
world.set_unit_recno(x, y, mobile_type, sprite_recno);
}
//--------- explore land ----------//
// ###### begin Gilbert 24/5 ######//
if( !config.explore_whole_map && is_own() )
// ###### end Gilbert 24/5 ######//
{
int xLoc1 = MAX(0,newNextXLoc-EXPLORE_RANGE);
int yLoc1 = MAX(0,newNextYLoc-EXPLORE_RANGE);
int xLoc2 = MIN(MAX_WORLD_X_LOC-1, newNextXLoc+EXPLORE_RANGE);
int yLoc2 = MIN(MAX_WORLD_Y_LOC-1, newNextYLoc+EXPLORE_RANGE);
int exploreWidth = move_step_magn()-1;
if( newNextYLoc < curNextYLoc ) // if move upwards, explore upper area
world.explore(xLoc1, yLoc1, xLoc2, yLoc1+exploreWidth);
else if( newNextYLoc > curNextYLoc ) // if move downwards, explore lower area
world.explore(xLoc1, yLoc2-exploreWidth, xLoc2, yLoc2);
if( newNextXLoc < curNextXLoc ) // if move towards left, explore left area
world.explore(xLoc1, yLoc1, xLoc1+exploreWidth, yLoc2);
else if( newNextXLoc > curNextXLoc ) // if move towards right, explore right area
world.explore(xLoc2-exploreWidth, yLoc1, xLoc2, yLoc2);
}
}
}
err_when(cur_x!=next_x && cur_y!=next_y && // is not blocked
(check_unit_dir1=get_dir(cur_x, cur_y, next_x, next_y))!=(check_unit_dir2=get_dir(cur_x, cur_y, go_x, go_y)));
}
//---------- End of function Unit::set_next ----------//
//------ Begin of function Unit::blocked_move_new_handle -------//
/*int Unit::blocked_move_new_handle()
{
//------------------------------------------------------------------//
// new handling for blocked move
//------------------------------------------------------------------//
static int counter = 0;
int checkXLoc1, checkYLoc1, checkXLoc2, checkYLoc2;
int curXLoc = next_x_loc(), curYLoc = next_y_loc();
Location *locPtr;
counter++;
switch(final_dir)
{
case DIR_N:
checkXLoc1 = MIN(curXLoc+1, MAX_WORLD_X_LOC-1);
checkYLoc1 = checkYLoc2 = MAX(curYLoc-1, 0);
checkXLoc2 = MAX(curXLoc-1, 0);
break;
case DIR_NE:
checkXLoc1 = MIN(curXLoc+1, MAX_WORLD_X_LOC-1);
checkYLoc1 = curYLoc;
checkXLoc2 = curXLoc;
checkYLoc2 = MAX(curYLoc-1, 0);
break;
case DIR_E:
checkXLoc1 = checkXLoc2 = MIN(curXLoc+1, MAX_WORLD_X_LOC-1);
checkYLoc1 = MIN(curYLoc+1, MAX_WORLD_Y_LOC-1);
checkYLoc2 = MAX(curYLoc-1, 0);
break;
case DIR_SE:
checkXLoc1 = curXLoc;
checkYLoc1 = MIN(curYLoc+1, MAX_WORLD_Y_LOC-1);
checkXLoc2 = MIN(curXLoc+1, MAX_WORLD_X_LOC-1);
checkYLoc2 = curYLoc;
break;
case DIR_S:
checkXLoc1 = MAX(curXLoc-1, 0);
checkYLoc1 = checkYLoc2 = MIN(curYLoc+1, MAX_WORLD_Y_LOC-1);
checkXLoc2 = MIN(curXLoc+1, MAX_WORLD_X_LOC-1);
break;
case DIR_SW:
checkXLoc1 = MAX(curXLoc-1, 0);
checkYLoc1 = curYLoc;
checkXLoc2 = curXLoc;
checkYLoc2 = MIN(curYLoc, MAX_WORLD_Y_LOC-1);
break;
case DIR_W:
checkXLoc1 = checkXLoc2 = MAX(curXLoc-1, 0);
checkYLoc1 = MAX(curYLoc-1, 0);
checkYLoc2 = MIN(curYLoc+1, MAX_WORLD_Y_LOC-1);
break;
case DIR_NW:
checkXLoc1 = curXLoc;
checkYLoc1 = MAX(curYLoc-1, 0);
checkXLoc2 = MAX(curXLoc-1, 0);
checkYLoc2 = curYLoc;
break;
}
locPtr = world.get_loc(checkXLoc1, checkYLoc1);
if(locPtr->can_move(mobile_type))
set_path_to(checkXLoc1, checkYLoc1);
else
{
locPtr = world.get_loc(checkXLoc2, checkYLoc2);
if(locPtr->can_move(mobile_type))
set_path_to(checkXLoc2, checkYLoc2);
else
return 0;
}
return 1;
}*/
//------- End of function Unit::blocked_move_new_handle --------//
//------ Begin of function Unit::set_path_to -------//
/*void Unit::set_path_to(int destXLoc, int destYLoc)
{
//reset_path();
terminate_move();
result_node_array = (ResultNode*) mem_add(2*sizeof(ResultNode));
ResultNode *curNodePtr = result_node_array;
curNodePtr->node_x = next_x_loc();
curNodePtr->node_y = next_y_loc();
curNodePtr++;
curNodePtr->node_x = destXLoc;
curNodePtr->node_y = destYLoc;
result_node_count = 2;
result_node_recno = 1;
result_path_dist = 1;
move_to_x_loc = destXLoc;
move_to_y_loc = destYLoc;
go_x = destXLoc*ZOOM_LOC_WIDTH;
go_y = destYLoc*ZOOM_LOC_HEIGHT;
//set_dir(cur_x, cur_y, go_x, go_y);
//set_wait();
next_move();
}*/
//------- End of function Unit::set_path_to --------//
//------ Begin of function Unit::handle_blocked_move -------//
// Note: it assumes the given location is blocked, it makes no
// further attempt to verify it.
//
// <Location*> blockedLoc - blocked location
//
// return : <int> 1 - handled successfully
// 0 - cannot be handled, the movement must be terminated
//
void Unit::handle_blocked_move(Location* blockedLoc)
{
//--- check if the tile we are moving at is blocked by a building ---//
if(blockedLoc->is_firm() || blockedLoc->is_town() || blockedLoc->is_wall())
{
//------------------------------------------------//
// firm/town/wall is on the blocked location
//------------------------------------------------//
reset_path();
search_or_stop(move_to_x_loc, move_to_y_loc, 1);
//search(move_to_x_loc, move_to_y_loc, 1);
return;
}
if(next_x_loc()==move_to_x_loc && next_y_loc()==move_to_y_loc && !swapping)
{
terminate_move(); // terminate since already reaching destination
return;
}
if(!blockedLoc->is_accessible(mobile_type))
{
terminate_move(); // the location is not accessible
err_when(cur_x==go_x && cur_y==go_y && (cur_x!=next_x || cur_y!=next_y));
return;
}
//-----------------------------------------------------------------------------------//
// there is another sprite on the move_to location, check the combination of both sizes
//-----------------------------------------------------------------------------------//
blocked_by_member = 1;
err_when(!blockedLoc->unit_recno(mobile_type));
Unit* unitPtr = unit_array[blockedLoc->unit_recno(mobile_type)];
//if(unitPtr->sprite_info->loc_width>1 || sprite_info->loc_width>1)
//{
// set_wait();
// return;
//}
//else
handle_blocked_move_s11(unitPtr); //------ both units size 1x1
err_when(cur_x==go_x && cur_y==go_y && (cur_x!=next_x || cur_y!=next_y));
return;
}
//------- End of function Unit::handle_blocked_move --------//
//------ Begin of function Unit::handle_blocked_by_idle_unit ---------//
// handle the case blocked by idle unit
//
// <Unit*> unitPtr - the unit blocking this unit
//
void Unit::handle_blocked_by_idle_unit(Unit *unitPtr)
{
#define TEST_DIMENSION 10
#define TEST_LIMIT TEST_DIMENSION*TEST_DIMENSION
char notLandUnit = (mobile_type!=UNIT_LAND);
int unitXLoc = unitPtr->next_x_loc();
int unitYLoc = unitPtr->next_y_loc();
int xShift, yShift;
int checkXLoc, checkYLoc;
Location *locPtr;
int xSign = misc.random(2) ? 1 : -1;
int ySign = misc.random(2) ? 1 : -1;
for(int i=2; i<=TEST_LIMIT; i++)
{
misc.cal_move_around_a_point(i, TEST_DIMENSION, TEST_DIMENSION, xShift, yShift);
xShift *= xSign;
yShift *= ySign;
if(notLandUnit)
{
checkXLoc = unitXLoc + xShift*2;
checkYLoc = unitYLoc + yShift*2;
}
else
{
checkXLoc = unitXLoc + xShift;
checkYLoc = unitYLoc + yShift;
}
if(checkXLoc<0 || checkXLoc>=MAX_WORLD_X_LOC || checkYLoc<0 || checkYLoc>=MAX_WORLD_Y_LOC)
continue;
locPtr = world.get_loc(checkXLoc , checkYLoc);
if(!locPtr->can_move(unitPtr->mobile_type))
continue;
if(on_my_path(checkXLoc, checkYLoc))
continue;
unitPtr->move_to(checkXLoc, checkYLoc);
set_wait();
return;
}
if( config_adv.fix_path_blocked_by_team )
stop(KEEP_PRESERVE_ACTION);
else
stop(KEEP_DEFENSE_MODE);
//--------------------------------------------------------------------------------//
// improved version!!!
//--------------------------------------------------------------------------------//
/*int testResult = 0, worstCase=0;
int worstXLoc=-1, worstYLoc=-1;
int startCount, endCount;
int i, j;
for(j=0; j<2; j++)
{
//----------- set the startCount and endCount ------------//
if(j==0)
{
startCount = 2;
endCount = 9;
}
else
{
startCount = 10;
endCount = TEST_LIMIT;
}
for(i=startCount; i<=endCount; i++)
{
misc.cal_move_around_a_point(i, TEST_DIMENSION, TEST_DIMENSION, xShift, yShift);
if(notLandUnit)
{
checkXLoc = unitXLoc + xShift*2;
checkYLoc = unitYLoc + yShift*2;
}
else
{
checkXLoc = unitXLoc + xShift;
checkYLoc = unitYLoc + yShift;
}
if(checkXLoc<0 || checkXLoc>=MAX_WORLD_X_LOC || checkYLoc<0 || checkYLoc>=MAX_WORLD_Y_LOC)
continue;
locPtr = world.get_loc(checkXLoc , checkYLoc);
if(!locPtr->can_move(unitPtr->mobile_type))
continue;
//-------------------------------------------------------------------//
// a possible location
//-------------------------------------------------------------------//
testResult = on_my_path(checkXLoc, checkYLoc);
if(testResult)
{
if(j==0 && !worstCase)
{
worstCase++;
worstXLoc = checkXLoc;
worstYLoc = checkYLoc;
}
continue;
}
unitPtr->move_to(checkXLoc, checkYLoc):
set_wait();
return;
}
}
//-------------------------------------------------------------------//
if(worstCase)
{
unitPtr->move_to(worstXLoc, worstYLoc);
set_wait();
}
else
stop(KEEP_DEFENSE_MODE);*/
}
//------- End of function Unit::handle_blocked_by_idle_unit --------//
//------ Begin of function Unit::on_my_path ---------//
// This function is used to check whether a location
// (checkXLoc, checkYLoc) is on the unit path, result_node_array.
//
// return 1 if true
// return 0 otherwise
//
int Unit::on_my_path(short checkXLoc, short checkYLoc)
{
err_when(result_node_count<2);
ResultNode* curNodePtr = result_node_array+result_node_recno-2;
ResultNode* nextNodePtr = curNodePtr+1;
for(int i=result_node_recno-1; i<result_node_count; i++, curNodePtr++, nextNodePtr++)
{
if((curNodePtr->node_x-checkXLoc)*(checkYLoc-nextNodePtr->node_y)==
(curNodePtr->node_y-checkYLoc)*(checkXLoc-nextNodePtr->node_x)) // point of division
return 1;
}
return 0;
}
//------- End of function Unit::on_my_path --------//
//------ Begin of function Unit::handle_blocked_wait ---------//
//
// this function is worked for unit size 1x1 only to handle case that
// blocked by waiting unit
//
// <Unit*> unitPtr - unit blocking this unit
//
void Unit::handle_blocked_wait(Unit* unitPtr)
{
err_when(sprite_info->loc_width>1 || unitPtr->sprite_info->loc_width>1);
int stepMagn = move_step_magn();
short cycleWait = 0;
Location *locPtr;
if(is_dir_correct())
{
Unit *blockedUnitPtr = unitPtr;
SpriteInfo *unitSpriteInfo = unitPtr->sprite_info;
int nextX, nextY, loop = 0, i;
short blocked=0;
//---------------------------------------------------------------//
// construct a cycle_waiting array to store the sprite_recno of
// those units in cycle_waiting in order to prevent forever looping
// in the checking
//---------------------------------------------------------------//
int arraySize = 20;
cycle_wait_unit_array_def_size = arraySize;
cycle_wait_unit_index = 0;
cycle_wait_unit_array_multipler = 1;
cycle_wait_unit_array = (short*)mem_add(sizeof(short)*cycle_wait_unit_array_def_size);
memset(cycle_wait_unit_array, 0, sizeof(short)*cycle_wait_unit_array_def_size);
//---------------------------------------------------------------//
// don't handle the case blocked by size 2x2 unit in this moment
//---------------------------------------------------------------//
while(!cycleWait && blockedUnitPtr->cur_action==SPRITE_WAIT)
{
if(unitSpriteInfo->loc_width>1)
break; // don't handle unit size > 1
if(!blockedUnitPtr->is_dir_correct())
break;
//----------------------------------------------------------------------------------------//
// cur_x, cur_y of unit pointed by blockedUnitPtr should be exactly inside a tile
//----------------------------------------------------------------------------------------//
nextX = blockedUnitPtr->cur_x+stepMagn*move_x_pixel_array[blockedUnitPtr->final_dir];
nextY = blockedUnitPtr->cur_y+stepMagn*move_y_pixel_array[blockedUnitPtr->final_dir];
//---------- calculate location blocked unit attempts to move to ---------//
nextX >>= ZOOM_X_SHIFT_COUNT;
nextY >>= ZOOM_Y_SHIFT_COUNT;
locPtr = world.get_loc(nextX, nextY);
blocked = locPtr->has_unit(mobile_type);
//---------------- the unit is also waiting ---------------//
if(blocked && (blockedUnitPtr->move_to_x_loc!=blockedUnitPtr->cur_x_loc() ||
blockedUnitPtr->move_to_y_loc!=blockedUnitPtr->cur_y_loc()))
{
if(locPtr->unit_recno(mobile_type) == sprite_recno)
cycleWait = 1;
else
{
for(i=0; i<cycle_wait_unit_index; i++)
{
//---------- checking for forever loop ----------------//
if(cycle_wait_unit_array[i] == blockedUnitPtr->sprite_recno)
{
loop = 1;
break;
}
}
if(loop)
break;
//------------------------------------------------------//
// resize array if required size is larger than arraySize
//------------------------------------------------------//
if(cycle_wait_unit_index >= arraySize)
{
cycle_wait_unit_array_multipler++;
arraySize = cycle_wait_unit_array_def_size*cycle_wait_unit_array_multipler;
cycle_wait_unit_array = (short*) mem_resize(cycle_wait_unit_array, sizeof(short)*arraySize);
}
else
{
//-------- store recno of next blocked unit ----------//
cycle_wait_unit_array[cycle_wait_unit_index++] = blockedUnitPtr->sprite_recno;
locPtr = world.get_loc(nextX, nextY);
err_when(blockedUnitPtr->mobile_type!=mobile_type);
blockedUnitPtr = unit_array[locPtr->unit_recno(mobile_type)];
unitSpriteInfo = blockedUnitPtr->sprite_info;
}
}
}
else
break;
}
//---------- deinit data structure -------//
mem_del(cycle_wait_unit_array);
cycle_wait_unit_array = NULL;
}
if(cycleWait)
{
//----------------------------------------------------------------------//
// shift the recno of all the unit in the cycle
//----------------------------------------------------------------------//
err_when(!is_dir_correct());
short backupSpriteRecno;
world.set_unit_recno(cur_x_loc(), cur_y_loc(), mobile_type, 0); // empty the firt node in the cycle
cycle_wait_shift_recno(this, unitPtr); // shift all the unit in the cycle
backupSpriteRecno = world.get_unit_recno(cur_x_loc(), cur_y_loc(), mobile_type);
world.set_unit_recno(cur_x_loc(), cur_y_loc(), mobile_type, sprite_recno);
set_next(unitPtr->cur_x, unitPtr->cur_y, -stepMagn, 1);
set_move();
world.set_unit_recno(unitPtr->cur_x_loc(), unitPtr->cur_y_loc(), mobile_type, sprite_recno);
world.set_unit_recno(cur_x_loc(), cur_y_loc(), mobile_type, backupSpriteRecno);
swapping = 1;
}
else // not in a cycle
{
set_wait();
//if(waiting_term>=MAX_WAITING_TERM_SAME)
if(waiting_term>=MAX_WAITING_TERM_SAME*move_step_magn())
{
//-----------------------------------------------------------------//
// codes used to speed up frame rate
//-----------------------------------------------------------------//
locPtr = world.get_loc(move_to_x_loc, move_to_y_loc);
if(!locPtr->can_move(mobile_type) && action_mode2!=ACTION_MOVE)
stop(KEEP_PRESERVE_ACTION); // let reactivate..() call searching later
else
search_or_wait();
waiting_term = 0;
}
}
}
//-------- End of function Unit::handle_blocked_wait ---------//
//------ Begin of function Unit::cycle_wait_shift_recno ---------//
// copy recno of curUnit to location where nextUnit on
//
// <Unit*> curUnit -
// <Unit*> nextUnit -
//
void Unit::cycle_wait_shift_recno(Unit* curUnit, Unit* nextUnit)
{
err_when(!curUnit->is_dir_correct() || !nextUnit->is_dir_correct());
int stepMagn = move_step_magn();
Unit *blockedUnitPtr;
Location *locPtr;
//----------- find the next location ------------//
int nextX = nextUnit->cur_x+stepMagn*move_x_pixel_array[nextUnit->final_dir];
int nextY = nextUnit->cur_y+stepMagn*move_y_pixel_array[nextUnit->final_dir];
nextX >>= ZOOM_X_SHIFT_COUNT;
nextY >>= ZOOM_Y_SHIFT_COUNT;
if(nextX != cur_x_loc() || nextY != cur_y_loc())
{
locPtr = world.get_loc(nextX, nextY);
blockedUnitPtr = unit_array[locPtr->unit_recno(nextUnit->mobile_type)];
}
else
blockedUnitPtr = this;
err_when(!blockedUnitPtr);
if(blockedUnitPtr != this)
{
cycle_wait_shift_recno(nextUnit, blockedUnitPtr);
nextUnit->set_next(blockedUnitPtr->cur_x, blockedUnitPtr->cur_y, -stepMagn, 1);
nextUnit->set_move();
world.set_unit_recno(blockedUnitPtr->cur_x_loc(), blockedUnitPtr->cur_y_loc(), nextUnit->mobile_type, nextUnit->sprite_recno);
world.set_unit_recno(nextUnit->cur_x_loc(), nextUnit->cur_y_loc(), nextUnit->mobile_type, 0);
nextUnit->swapping = 1;
}
else // the cycle shift is ended
{
err_when(blockedUnitPtr != this);
err_when(nextUnit->cur_x!=nextUnit->next_x || nextUnit->cur_y!=nextUnit->next_y);
err_when(nextUnit->cur_action != SPRITE_WAIT);
nextUnit->set_next(cur_x, cur_y, -stepMagn, 1);
nextUnit->set_move();
world.set_unit_recno(cur_x_loc(), cur_y_loc(), nextUnit->mobile_type, nextUnit->sprite_recno);
world.set_unit_recno(nextUnit->cur_x_loc(), nextUnit->cur_y_loc(), nextUnit->mobile_type, 0);
nextUnit->swapping = 1;
}
}
//-------- End of function Unit::cycle_wait_shift_recno ---------//
//------ Begin of function Unit::opposite_direction_blocked ---------//
// the two units are oppositely blocked, handle swapping
//
void Unit::opposite_direction_blocked(short vecX, short vecY, short unitPtrVecX, short unitPtrVecY, Unit* unitPtr)
{
//---------------------------------------------------------------------------//
// processing swapping only when both units are exactly in the tiles
//---------------------------------------------------------------------------//
if(unitPtr->cur_action!=SPRITE_IDLE)
{
if(unitPtr->move_to_x_loc!=move_to_x_loc || unitPtr->move_to_y_loc!=move_to_y_loc)
{
int stepMagn = move_step_magn();
world.set_unit_recno(unitPtr->cur_x_loc(), unitPtr->cur_y_loc(), mobile_type, 0);
err_when(!is_dir_correct());
set_next(unitPtr->cur_x, unitPtr->cur_y, -stepMagn, -1);
world.set_unit_recno(unitPtr->cur_x_loc(), unitPtr->cur_y_loc(), mobile_type, unitPtr->sprite_recno);
world.set_unit_recno(cur_x_loc(), cur_y_loc(), unitPtr->mobile_type, 0);
err_when(!unitPtr->is_dir_correct());
unitPtr->set_next(cur_x, cur_y, -stepMagn, 1);
world.set_unit_recno(unitPtr->cur_x_loc(), unitPtr->cur_y_loc(), mobile_type, sprite_recno);
world.set_unit_recno(cur_x_loc(), cur_y_loc(), unitPtr->mobile_type, unitPtr->sprite_recno);
set_move();
unitPtr->set_move();
swapping = 1;
unitPtr->swapping = 1;
#ifdef DEBUG
ResultNode* curNode;
curNode = result_node_array + result_node_count - 1;
err_when(curNode->node_x!=move_to_x_loc || curNode->node_y!=move_to_y_loc);
curNode = unitPtr->result_node_array + unitPtr->result_node_count - 1;
err_when(curNode->node_x!=unitPtr->move_to_x_loc || curNode->node_y!=unitPtr->move_to_y_loc);
#endif
}
else
terminate_move();
}
else
{
//----------------------------------------------------------------------//
// If the unit pointed by unitPtr (unit B) has the same unit_id, rank_id
// and both are in the same group, this unit will order the other unit to
// move to its location and this unit will occupy the location of the unit B.
//
// If the above condition is not fulfilled, swapping is processed.
//----------------------------------------------------------------------//
if(unit_id!=unitPtr->unit_id || rank_id!=unitPtr->rank_id || unit_group_id!=unitPtr->unit_group_id)
{
if(unitPtr->move_to_x_loc!=move_to_x_loc || unitPtr->move_to_y_loc!=move_to_y_loc)
{
//----------------- process swapping ---------------//
set_wait();
unitPtr->set_dir(unitPtr->next_x, unitPtr->next_y, next_x, next_y);
set_dir(next_x, next_y, unitPtr->next_x, unitPtr->next_y);
err_when(unitPtr->result_node_array!=NULL);
unitPtr->result_node_array = (ResultNode*)mem_add(sizeof(ResultNode)*2);
ResultNode* nodePtr = unitPtr->result_node_array;
err_when(next_x_loc()!= cur_x_loc() || next_y_loc()!=cur_y_loc());
nodePtr->node_x = next_x_loc();
nodePtr->node_y = next_y_loc();
nodePtr++;
nodePtr->node_x = unitPtr->next_x_loc();
nodePtr->node_y = unitPtr->next_y_loc();
unitPtr->result_node_count = 2;
unitPtr->result_node_recno = 1;
unitPtr->set_wait();
unitPtr->go_x = next_x;
unitPtr->go_y = next_y;
unitPtr->result_path_dist = 2;
err_when((check_unit_dir1=get_dir(cur_x, cur_y, go_x, go_y))!=final_dir);
swapping = 1;
unitPtr->swapping = 1;
}
else
terminate_move();
}
else
{
//------------ process move_to_my_loc or terminate the movement -----------//
if(unitPtr->move_to_x_loc!=move_to_x_loc || unitPtr->move_to_y_loc!=move_to_y_loc)
move_to_my_loc(unitPtr);
else
terminate_move();
}
#ifdef DEBUG
ResultNode* curNode;
if(result_node_array!=NULL)
{
curNode = result_node_array + result_node_count - 1;
err_when(curNode->node_x!=move_to_x_loc || curNode->node_y!=move_to_y_loc);
}
if(unitPtr->result_node_array!=NULL)
{
curNode = unitPtr->result_node_array + unitPtr->result_node_count - 1;
err_when(curNode->node_x!=unitPtr->move_to_x_loc || curNode->node_y!=unitPtr->move_to_y_loc);
}
#endif
}
}
//-------- End of function Unit::opposite_direction_blocked ---------//
//------ Begin of function Unit::terminate_move -------//
//
// When the sprite has finished moving the next tile in
// the path. If the following tile is blocked and the whole
// movement need to be terminated, this function is called.
//
// When SPRITE_IDLE:
//
// (next_x, next_y) must be == (cur_x, cur_y), it's the location of the sprite.
//
void Unit::terminate_move()
{
#ifdef DEBUG
err_when( next_x != cur_x || next_y != cur_y );
short h, y;
for(h=0, y=cur_y_loc(); h<sprite_info->loc_height; h++, y++)
for(short w=0, x=cur_x_loc(); w<sprite_info->loc_width; w++, x++)
err_when( world.get_unit_recno(x, y, mobile_type) != sprite_recno );
#endif
go_x = next_x;
go_y = next_y;
move_to_x_loc = next_x_loc();
move_to_y_loc = next_y_loc();
#ifdef DEBUG
char blocked=0;
for(h=0, y=next_y_loc(); h<sprite_info->loc_height&&!blocked; h++, y++)
for(short w=0, x=next_x_loc(); w<sprite_info->loc_width&&!blocked; w++, x++)
blocked = world.get_unit_recno(x,y,mobile_type) != sprite_recno;
err_when(blocked);
#endif
cur_frame = 1;
reset_path();
set_idle();
err_when(result_node_array!=NULL);
}
//------- End of function Unit::terminate_move --------//
//------------- Begin of function Unit::move_to_my_loc --------------//
//
// This function is used as this unit Unit A is blocked by another
// IDLE unit Unit B. Then Unit A will move to the location of Unit B
// and Unit B will move to the location of Unit A want to move to.
//
// Note: action_?_loc2 of both units can be different from their
// move_to_?_loc
//
void Unit::move_to_my_loc(Unit* unitPtr)
{
int unitDestX, unitDestY;
if(unitPtr->action_mode2==ACTION_MOVE)
{
unitDestX = unitPtr->action_x_loc2;
unitDestY = unitPtr->action_y_loc2;
err_when(unitDestX==-1 || unitDestY==-1);
}
else
{
unitDestX = unitPtr->move_to_x_loc;
unitDestY = unitPtr->move_to_y_loc;
}
//--------------- init parameters ---------------//
int unitCurX = unitPtr->next_x_loc();
int unitCurY = unitPtr->next_y_loc();
int destX = action_x_loc2;
int destY = action_y_loc2;
int curX = next_x_loc();
int curY = next_y_loc();
int moveScale = move_step_magn();
err_when(curX != cur_x_loc() || curY != cur_y_loc());
err_when(mobile_type!=unitPtr->mobile_type);
//------------------------------------------------------------------//
// setting for unit pointed by unitPtr
//------------------------------------------------------------------//
if(result_node_array==NULL) //************BUGHERE
{
err_when(unitPtr->cur_action!=SPRITE_IDLE);
unitPtr->move_to(destX, destY, 1); // unit pointed by unitPtr is idle before calling searching
err_when(unitPtr->cur_action!=SPRITE_DIE && unitPtr->action_mode2!=ACTION_MOVE);
}
else
{
err_when(result_node_recno<1 || unitPtr->result_node_array!=NULL);
ResultNode* resultNode = result_node_array+result_node_recno-1;
if(go_x == unitPtr->next_x && go_y == unitPtr->next_y)
{
//------ Unit B is in one of the node of the result_node_array ---//
unitPtr->result_node_count = result_node_count-result_node_recno+1; // at least there are 2 nodes
unitPtr->result_node_array = (ResultNode*)mem_add(sizeof(ResultNode)*(unitPtr->result_node_count));
memcpy(unitPtr->result_node_array, resultNode, sizeof(ResultNode)*(unitPtr->result_node_count));
}
else
{
//----- Unit B is in the middle of two nodes in the result_node_array -----//
unitPtr->result_node_count = result_node_count-result_node_recno+2;
unitPtr->result_node_array = (ResultNode*)mem_add(sizeof(ResultNode)*(unitPtr->result_node_count));
ResultNode* curNode = unitPtr->result_node_array;
curNode->node_x = unitCurX;
curNode->node_y = unitCurY;
curNode++;
memcpy(curNode, resultNode, sizeof(ResultNode)*(unitPtr->result_node_count-1));
}
err_when(unitPtr->result_node_count<2);
//--------------- set unit action ---------------//
if(unitPtr->action_mode2==ACTION_STOP || unitPtr->action_mode2==ACTION_MOVE) // unitPtr is idle now
{
//---------- activate unit pointed by unitPtr now ------------//
unitPtr->action_mode = unitPtr->action_mode2 = ACTION_MOVE;
unitPtr->action_para = unitPtr->action_para2 = 0;
if(destX!=-1 && destY!=-1)
{
unitPtr->action_x_loc = unitPtr->action_x_loc2 = destX;
unitPtr->action_y_loc = unitPtr->action_y_loc2 = destY;
}
else
{
ResultNode *lastNodePtr = unitPtr->result_node_array + unitPtr->result_node_count - 1;
unitPtr->action_x_loc = unitPtr->action_x_loc2 = lastNodePtr->node_x;
unitPtr->action_y_loc = unitPtr->action_y_loc2 = lastNodePtr->node_y;
}
}
//----------------- set unit movement parameters -----------------//
unitPtr->result_node_recno = 1;
unitPtr->result_path_dist = result_path_dist-moveScale;
unitPtr->move_to_x_loc = move_to_x_loc;
unitPtr->move_to_y_loc = move_to_y_loc;
err_when( next_x != cur_x || next_y != cur_y );
unitPtr->next_move();
#ifdef DEBUG
if(unitPtr->result_node_array!=NULL)
{
ResultNode* curNode1 = unitPtr->result_node_array + unitPtr->result_node_recno - 1;
err_when(curNode1->node_x!=unitPtr->go_x>>ZOOM_X_SHIFT_COUNT || curNode1->node_y!=unitPtr->go_y>>ZOOM_Y_SHIFT_COUNT);
}
#endif
}
//------------------------------------------------------------------//
// setting for this unit
//------------------------------------------------------------------//
int shouldWait = 0;
if(next_x==unitPtr->cur_x && next_y==unitPtr->cur_y)
{
reset_path();
result_path_dist = 0;
}
else
{
terminate_move();
shouldWait++;
result_path_dist = moveScale;
}
go_x = unitPtr->cur_x;
go_y = unitPtr->cur_y;
move_to_x_loc = unitCurX;
move_to_y_loc = unitCurY;
if(action_mode2==ACTION_MOVE)
{
action_x_loc = action_x_loc2 = unitDestX;
action_y_loc = action_y_loc2 = unitDestY;
}
//---------- note: the cur_dir is already the correct direction ---------------//
err_when(result_node_array!=NULL);
result_node_array = (ResultNode*)mem_add(sizeof(ResultNode)*2);
ResultNode* nodePtr = result_node_array;
nodePtr->node_x = curX;
nodePtr->node_y = curY;
nodePtr++;
nodePtr->node_x = unitCurX;
nodePtr->node_y = unitCurY;
result_node_count = 2;
result_node_recno = 2;
if(shouldWait)
set_wait(); // wait for the blocking unit to move first
#ifdef DEBUG
if(result_node_array!=NULL)
{
ResultNode* curNode2 = result_node_array + result_node_recno - 1;
err_when(curNode2->node_x!=go_x>>ZOOM_X_SHIFT_COUNT || curNode2->node_y!=go_y>>ZOOM_Y_SHIFT_COUNT);
}
#endif
err_when(cur_action==SPRITE_IDLE && (move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc()));
err_when(cur_action==SPRITE_IDLE && (cur_x!=next_x || cur_y!=next_y));
err_when(unitPtr->cur_action==SPRITE_IDLE && (unitPtr->move_to_x_loc!=unitPtr->next_x_loc() ||
unitPtr->move_to_y_loc!=unitPtr->next_y_loc()));
}
//----------------- End of function Unit::move_to_my_loc ----------------//
//###### begin trevor 25/6 #######//
/*
//------------- Begin of function Unit::change_relation --------------//
void Unit::change_relation(short nation1, short nation2, int relationType)
{
if(!nation1 || !nation2 || nation1==nation2)
return; // return if either is neutral nation or same nation
nation_array[nation1]->set_relation_status(nation2, relationType);
}
//----------------- End of function Unit::change_relation ----------------//
*/
//####### end trevor 25/6 ########//
//------------- Begin of function Unit::set_idle --------------//
// set parameters for unit idle
//
void Unit::set_idle()
{
/*err_when(unit_res[unit_id]->unit_class==UNIT_CLASS_SHIP &&
(((UnitMarine*)this)->extra_move_in_beach==EXTRA_MOVING_IN ||
((UnitMarine*)this)->extra_move_in_beach==EXTRA_MOVING_OUT));*/
err_when(cur_x<0);
err_when(move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc());
err_when(cur_x!=next_x || cur_y!=next_y || next_x!=go_x || next_y!=go_y);
err_when(cur_x%ZOOM_LOC_WIDTH || cur_y%ZOOM_LOC_HEIGHT);
err_when(result_path_dist || result_node_array);
err_when(cur_dir<0 || cur_dir>MAX_SPRITE_DIR_TYPE);
final_dir = cur_dir;
turn_delay = 0;
cur_action = SPRITE_IDLE;
}
//----------------- End of function Unit::set_idle ----------------//
//------------- Begin of function Unit::set_ready --------------//
// set parameters for unit ready to move
//
void Unit::set_ready()
{
err_when(cur_x<0);
err_when(move_to_x_loc!=next_x_loc() || move_to_y_loc!=next_y_loc());
err_when(cur_x!=next_x || cur_y!=next_y || next_x!=go_x || next_y!=go_y);
err_when(cur_x%ZOOM_LOC_WIDTH || cur_y%ZOOM_LOC_HEIGHT);
err_when(cur_dir<0 || cur_dir>MAX_SPRITE_DIR_TYPE);
final_dir = cur_dir;
turn_delay = 0;
cur_action = SPRITE_READY_TO_MOVE;
}
//----------------- End of function Unit::set_ready ----------------//
//------------- Begin of function Unit::set_move --------------//
// set parameters for unit movement
//
void Unit::set_move()
{
err_when(cur_x<0);
err_when(cur_dir!=final_dir);
cur_action = SPRITE_MOVE;
}
//----------------- End of function Unit::set_move ----------------//
//------------- Begin of function Unit::set_wait --------------//
// Set the unit to waiting status.
// When SPRITE_WAIT:
// (next_x, next_y) must be == (cur_x, cur_y), it's the location of the sprite.
//
void Unit::set_wait()
{
err_when(cur_x<0);
#ifdef DEBUG
err_when(go_x==cur_x && go_y==cur_y);
err_when( next_x != cur_x || next_y != cur_y );
short w, h, x, y;
for(h=0, y=cur_y_loc(); h<sprite_info->loc_height; h++, y++)
for(w=0, x=cur_x_loc(); w<sprite_info->loc_width; w++, x++)
err_when( world.get_unit_recno(x, y, mobile_type) != sprite_recno );
err_when((check_unit_dir1=get_dir(cur_x, cur_y, go_x, go_y))!=final_dir);
#endif
cur_action = SPRITE_WAIT;
cur_frame = 1;
waiting_term++;
}
//----------------- End of function Unit::set_wait ----------------//
//------------- Begin of function Unit::set_attack --------------//
// set parameters for unit attack
//
void Unit::set_attack()
{
err_when(cur_x<0);
err_when(next_x!=cur_x || next_y!=cur_y);
err_when(cur_dir<0 || cur_dir>=MAX_SPRITE_DIR_TYPE || turn_delay);
final_dir = cur_dir;
turn_delay = 0;
cur_action = SPRITE_ATTACK;
}
//----------------- End of function Unit::set_attack ----------------//
//------------- Begin of function Unit::set_turn --------------//
// set parameters for unit turning
//
void Unit::set_turn()
{
err_when(cur_x<0);
err_when(next_x!=cur_x || next_y!=cur_y);
cur_action = SPRITE_TURN;
}
//----------------- End of function Unit::set_turn ----------------//
//------------- Begin of function Unit::set_ship_extra_move --------------//
// set parameters for ship extra moving in inlet
//
void Unit::set_ship_extra_move()
{
err_when(cur_x<0);
cur_action = SPRITE_SHIP_EXTRA_MOVE;
}
//----------------- End of function Unit::set_ship_extra_move ----------------//
//------------- Begin of function Unit::set_die --------------//
// set parameters for unit die
//
void Unit::set_die()
{
if( action_mode == ACTION_DIE )
return;
err_when(hit_points>0);
action_mode = ACTION_DIE;
cur_action = SPRITE_DIE;
cur_frame = 1;
//##### begin trevor 19/7 #####//
//---- if this unit is led by a leader, only mobile units has leader_unit_recno assigned to a leader -----//
if( leader_unit_recno && !unit_array.is_deleted(leader_unit_recno) ) // the leader unit may have been killed at the same time
{
unit_array[leader_unit_recno]->del_team_member(sprite_recno);
leader_unit_recno = 0;
}
//##### end trevor 19/7 #####//
}
//----------------- End of function Unit::set_die ----------------//
|