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 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "matrixmath.h"
#include "sfm.h"
#include "v3dtex.h"
#include "gctl.h"
#include "sarreality.h"
#include "obj.h"
#include "objsound.h"
#include "objutils.h"
#include "sar.h"
#include "messages.h"
#include "sardrawselect.h"
#include "simcb.h"
#include "simutils.h"
#include "simsurface.h"
#include "simop.h"
#include "explosion.h"
#include "smoke.h"
#include "config.h"
static float SARSimLandingGearDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_obj_part_struct *lgear_ptr /* First landing gear */
);
static float SARSimFlapDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
);
static float SARSimHoistDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
const sar_obj_hoist_struct *hoist
);
static float SARSimAirBrakeCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
);
float SARSimFindGround(
sar_scene_struct *scene,
sar_object_struct **ptr, int total,
const sar_position_struct *pos
);
int SARSimDoMortality(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
);
int SARSimRestart(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
int obj_num, sar_object_struct *obj_ptr
);
int SARSimDeleteEffects(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
int obj_num,
unsigned int filter_flags /* Any of SARSIM_DELETE_EFFECTS_* */
);
void SARSimSetAircraftCrashed(
sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
);
void SARSimSetSFMValues(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct *obj_ptr
);
void SARSimGetSFMValues(
sar_scene_struct *scene, sar_object_struct *obj_ptr
);
int SARSimApplyNaturalForce(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
);
int SARSimApplyArtificialForce(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
);
static void SARSimApplyGCTLAutoPilot(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr,
gctl_struct *gc,
float *h_con_coeff,
float *p_con_coeff,
float *b_con_coeff,
float *elevator_trim,
float *throttle_coeff,
float *collective
);
void SARSimApplyGCTL(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
);
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h) (MIN(MAX((a),(l)),(h)))
#define RADTODEG(r) ((r) * 180.0 / PI)
#define DEGTORAD(d) ((d) * PI / 180.0)
#define SAR_ABSOLUTE(x) (((x) < 0.0) ? ((x) * -1.0) : (x))
/*
* Returns the drag coeff caused by the state of the first
* landing gear on the given object.
*
* Return value is from 0.0 to 0.5 where 0.0 is no additional
* drag and 0.5 is full drag.
*/
static float SARSimLandingGearDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_obj_part_struct *lgear_ptr /* First landing gear */
)
{
if(lgear_ptr == NULL)
return(0.0f);
/* Is landing gear retracted or not retractable? If so then
* no additional drag should be added.
*/
if(!(lgear_ptr->flags & SAR_OBJ_PART_FLAG_STATE) ||
(lgear_ptr->flags & SAR_OBJ_PART_FLAG_LGEAR_FIXED)
)
return(0.0f);
/* Return the position of the gear by its animated position,
* where 0 is up and (sar_grad_anim_t)-1 is down.
* This will produce a coefficient in the range of 0.0 to 0.5.
*/
return((float)(
(1.0 - ((float)lgear_ptr->anim_pos /
(float)((sar_grad_anim_t)-1))) * 0.5
));
}
/*
* Returns the drag coeff caused by the state of the flaps.
*
* Return value is from 0.0 to 0.3 where 0.0 is no additional
* drag and 0.3 is full drag.
*/
static float SARSimFlapDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
)
{
if(aircraft == NULL)
return(0.0f);
return((float)(aircraft->flaps_position * 0.3));
}
/*
* Returns the drag coeff caused by the position of the hoist
* (how far the rescue basket is from the hoist).
*
* Return value is from 0.0 to 1.0 where 0.0 is no additional
* drag and 1.0 is full drag.
*/
static float SARSimHoistDragCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
const sar_obj_hoist_struct *hoist
)
{
float rope_cur, rope_max;
if(hoist == NULL)
return(0.0f);
/* Get rope lengths which imply position of rescue basket.
* Remember to subtract the upper z bounds off the lengths.
*/
rope_cur = (float)MAX(
hoist->rope_cur - hoist->contact_z_min, 0.0
);
rope_max = (float)MAX(
hoist->rope_max - hoist->contact_z_min, 0.0
);
if(rope_max > 0.0f)
return((float)(MIN(rope_cur / rope_max, 1.0) * 0.9));
else
return(0.0f);
}
/*
* Returns the air brake effectiveness coefficient based on the
* calculating of the object's speed.
*
* Return value is from 0.0 to 1.0 where 0.0 is no air brake
* effectiveness and 1.0 is full effectiveness.
*/
static float SARSimAirBrakeCoeff(
sar_scene_struct *scene, sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
)
{
if(aircraft == NULL)
return(0.0f);
/* Slower than stall speed threshold (do not account for
* flaps)?
*/
if(aircraft->speed < aircraft->speed_stall)
{
/* Return value from 0.0 to 0.1 */
return((float)((aircraft->speed_stall > 0.0) ?
MAX(
(aircraft->speed / aircraft->speed_stall * 0.1), 0.0) :
0.0
));
}
else
{
/* Return value from 0.1 to 1.0 */
return((float)CLIP(
((aircraft->overspeed_expected > 0.0f) ?
(aircraft->speed / aircraft->overspeed_expected) : 0.1
),
0.1, 1.0
));
}
}
/*
* Returns the elevation of (not to) the solid ground with respect
* to the given position pos.
*
* Only objects of type ground will be checked, becareful not
* to check the given pos if it came from a ground object or
* else it would return its own elevation.
*
* So this function should only be used to check landable/walkable
* ground for objects that need it (ie aircraft and humans).
* Also note that pos passed to this function should not come from
* an object who's contact bounds specify the crash flag
* SAR_CRASH_FLAG_SUPPORT_SURFACE or else they will keep being
* supported on themselves.
*/
float SARSimFindGround(
sar_scene_struct *scene,
sar_object_struct **ptr, int total,
const sar_position_struct *pos
)
{
int i;
sar_object_struct *tar_obj_ptr;
float new_height, cur_height = 0.0f;
const sar_contact_bounds_struct *cb_tar;
const sar_position_struct *pos_src = pos;
if((scene == NULL) || (pos_src == NULL))
return(cur_height);
/* Iterate from last object to first */
for(i = total - 1; i >= 0; i--)
{
tar_obj_ptr = ptr[i];
if(tar_obj_ptr == NULL)
continue;
/* Check if target object specifies a landable surface */
cb_tar = tar_obj_ptr->contact_bounds;
if(cb_tar != NULL)
{
if(cb_tar->crash_flags & SAR_CRASH_FLAG_SUPPORT_SURFACE)
{
new_height = SARSimSupportSurfaceHeight(
tar_obj_ptr, cb_tar, pos_src,
(float)SAR_DEF_SURFACE_CONTACT_Z_TOLORANCE
);
if(new_height > cur_height)
cur_height = new_height;
}
}
/* Get ground object height (function will check
* if object really is a ground object), on error
* it will return 0.0 so accepting its value unconditionally
* is safe.
*
* Both ground object cylendrical contact and heightfield
* contact will be checked.
*/
new_height = (float)SARSimHFGetGroundHeight(
tar_obj_ptr, /* Ground object */
pos_src
);
/* New height higher? */
if(new_height > cur_height)
cur_height = new_height;
}
return(cur_height);
}
/*
* Checks if the life span of the given object has been exceeded,
* if so then that object will be deleted and returns non-zero.
* Calling function may need to consider the pointer array base for
* the objects changed if non-zero is returned.
*
* Any object who's life_span is 0 or less will be ignored.
*/
int SARSimDoMortality(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
)
{
if(obj_ptr == NULL)
return(0);
/* Ignore objects who's life_span is 0 or less */
if(obj_ptr->life_span <= 0)
return(0);
/* Check if object's life_span has exceeded the current
* time. Note that the life_span value was calculated from
* when it was created plus its life span (cur_millitime +
* life_expectancy) so when the current time exceeds the
* object's life_span it indicates the object must be deleted.
* All units are in milliseconds.
*/
if(obj_ptr->life_span <= cur_millitime)
{
int i;
for(i = 0; i < core_ptr->total_objects; i++)
{
if(core_ptr->object[i] == obj_ptr)
SARObjDelete(
core_ptr,
&core_ptr->object,
&core_ptr->total_objects,
i
);
}
return(1);
}
else
{
return(0);
}
}
/*
* Moves the given object to the nearest helipad mark restartable
* and refuels/repairs the object.
*
* This function is intended to be called when the player has
* crashed and needs to be restarted.
*
* Does not check if restarting is valid, all permission checks must
* be done by the calling function before calling this function.
*
* Returns the index to the helipad object the object is restarting
* at or -1 on failure.
*/
int SARSimRestart(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
int obj_num, sar_object_struct *obj_ptr
)
{
int i, closest_obj_num = -1;
float closest_distance = 0.0;
sar_position_struct *pos;
sar_object_struct *start_obj_ptr, *closest_obj_ptr = NULL;
sar_object_helipad_struct *obj_helipad_ptr = NULL;
sar_object_aircraft_struct *aircraft;
sar_obj_hoist_struct *hoist_ptr;
SFMModelStruct *fdm = NULL;
if((scene == NULL) || (ptr == NULL) || (total == NULL) ||
(obj_ptr == NULL)
)
return(-1);
pos = &obj_ptr->pos;
/* Check if this is the player object on the scene structure */
if((obj_num == scene->player_obj_num) ||
(obj_ptr == scene->player_obj_ptr)
)
{
/* Reset player has crashed mark on scene structure */
scene->player_has_crashed = False;
/* Also reset time compression */
time_compression = 1.0f;
/* Move camera reference back to cockpit */
scene->camera_ref = SAR_CAMERA_REF_COCKPIT;
}
/* Repair and refuel the object */
SARSimOpRepair(scene, obj_ptr);
SARSimOpRefuel(scene, obj_ptr);
/* Unload any passengers since we're re-starting */
SARSimOpPassengersUnloadAll(scene, obj_ptr);
/* Search for the closest helipad to restart at */
for(i = 0; i < *total; i++)
{
start_obj_ptr = (*ptr)[i];
if(start_obj_ptr == NULL)
continue;
if(start_obj_ptr == obj_ptr)
continue;
obj_helipad_ptr = SAR_OBJ_GET_HELIPAD(start_obj_ptr);
if(obj_helipad_ptr == NULL)
continue;
/* Skip if helipad is referencing this object */
if(obj_helipad_ptr->flags & SAR_HELIPAD_FLAG_REF_OBJECT)
{
if((obj_helipad_ptr->ref_object == obj_num) &&
(obj_num > -1)
)
continue;
}
/* Does this helipad allow restarts? */
if(obj_helipad_ptr->flags & SAR_HELIPAD_FLAG_RESTART_POINT)
{
/* Calculate 2d distance from object to potential starting
* point object
*/
float dlen = (float)SFMHypot2(
start_obj_ptr->pos.x - pos->x, start_obj_ptr->pos.y - pos->y
);
/* No previous closest object recorded? */
if(closest_obj_ptr == NULL)
{
closest_distance = dlen;
closest_obj_ptr = start_obj_ptr;
closest_obj_num = i;
}
else
{
/* Closer? */
if(dlen < closest_distance)
{
closest_distance = dlen;
closest_obj_ptr = start_obj_ptr;
closest_obj_num = i;
}
}
}
}
/* Got closest helipad? */
if(closest_obj_ptr != NULL)
{
/* Move object to closest helipad */
memcpy(pos, &closest_obj_ptr->pos, sizeof(sar_position_struct));
obj_ptr->ground_elevation_msl = pos->z;
/* Set direction to match helipad */
memcpy(&obj_ptr->dir, &closest_obj_ptr->dir, sizeof(sar_direction_struct));
/* Reset values by object type */
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(aircraft != NULL)
{
fdm = aircraft->fdm;
if(fdm != NULL)
{
#define TAR_PTR fdm
TAR_PTR->heading_control_coeff = 0.0f;
TAR_PTR->pitch_control_coeff = 0.0f;
TAR_PTR->bank_control_coeff = 0.0f;
TAR_PTR->elevator_trim_coeff = 0.0f;
TAR_PTR->throttle_coeff = 0.0f;
TAR_PTR->velocity_vector.x = 0.0f;
TAR_PTR->velocity_vector.y = 0.0f;
TAR_PTR->velocity_vector.z = 0.0f;
TAR_PTR->speed = 0.0f;
TAR_PTR->landed_state = True;
#undef TAR_PTR
}
#define TAR_PTR aircraft
/* Reset current speed and velocity */
memset(&TAR_PTR->vel, 0x00, sizeof(sar_position_struct));
TAR_PTR->speed = 0.0f;
TAR_PTR->z_accel = 0.0f;
/* Control positions */
TAR_PTR->control_heading = 0.0f;
TAR_PTR->control_pitch = 0.0f;
TAR_PTR->control_bank = 0.0f;
TAR_PTR->control_effective_heading = 0.0f;
TAR_PTR->control_effective_pitch = 0.0f;
TAR_PTR->control_effective_bank = 0.0f;
TAR_PTR->elevator_trim = 0.0f;
TAR_PTR->flaps_position = 0.0f;
TAR_PTR->throttle = 0.0f;
TAR_PTR->collective = 0.0f;
/* Reset air brakes state, note that visual models
* for air brakes will be reset during regular
* simulation when they detect the new state we
* set here
*/
if(TAR_PTR->air_brakes_state > -1)
{
TAR_PTR->air_brakes_state = 1;
SARSimOpAirBrakes(
scene, ptr, total, obj_ptr, 0,
NULL, 0, 0
);
}
/* Lower landing gears, first mark as not landed in
* order to trick it to lower landing gears
*/
TAR_PTR->landed = 0;
SARSimOpLandingGear(
scene, ptr, total, obj_ptr, 1,
NULL, 0, 0
);
/* Pitch rotors up if the rotors can pitch (landed
* state also needs to be 0 for this)
*/
if(TAR_PTR->engine_can_pitch)
{
SARSimPitchEngine(
scene, ptr, total, obj_ptr, 0
);
}
/* Spot light direction */
if(True)
{
sar_direction_struct *dir = &TAR_PTR->spotlight_dir;
dir->heading = (float)SFMDegreesToRadians(
SAR_DEF_SPOTLIGHT_HEADING
);
dir->pitch = (float)SFMDegreesToRadians(
SAR_DEF_SPOTLIGHT_PITCH
);
dir->bank = (float)SFMDegreesToRadians(
SAR_DEF_SPOTLIGHT_BANK
);
}
/* Switch off autopilot */
TAR_PTR->autopilot_state = SAR_AUTOPILOT_OFF;
/* Reset hoist */
hoist_ptr = SARObjGetHoistPtr(obj_ptr, 0, NULL);
if(hoist_ptr != NULL)
{
hoist_ptr->rope_cur = 0.0f;
}
/* Close the door */
SARSimOpDoorRescue(
scene, ptr, total, obj_ptr, 0
);
/* Turn engine on */
SARSimOpEngine(
scene, ptr, total,
obj_ptr, SAR_ENGINE_ON,
NULL, 0, 0
);
/* Move aircraft up just a bit to offset for its
* belly and gear heights.
*/
pos->z += (TAR_PTR->belly_height +
TAR_PTR->gear_height);
/* Implicitly mark as landed and not on water */
TAR_PTR->landed = 1;
TAR_PTR->on_water = 0;
#undef TAR_PTR
}
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
case SAR_OBJ_TYPE_HUMAN:
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Realize new position */
SARSimWarpObject(
scene, obj_ptr, pos, &obj_ptr->dir
);
}
/* Destroy all effects objects that reference this object */
SARSimDeleteEffects(
core_ptr, scene, ptr, total,
obj_num,
0
);
return(closest_obj_num);
}
/*
* Deletes all explosion, smoke, and other `effects objects'
* with respect to the given reference object number.
*
* If the given object is -1 then all of the above will be deleted
* if they are non-errant (have a reference object).
*
* filter_flags can be any of the following:
*
* SARSIM_DELETE_EFFECTS_SMOKE_STOP_RESPAWN
* Stop smoke trail objects from respawning but do not delete
*
* Returns the number of objects deleted.
*/
int SARSimDeleteEffects(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
int obj_num,
unsigned int filter_flags /* Any of SARSIM_DELETE_EFFECTS_* */
)
{
int i, objects_deleted = 0;
Boolean delete_this_object;
sar_object_struct *effects_obj_ptr;
sar_object_smoke_struct *smoke;
sar_object_fire_struct *fire;
sar_object_explosion_struct *explosion;
sar_object_chemical_spray_struct *spray;
if((scene == NULL) || (ptr == NULL) || (total == NULL))
return(objects_deleted);
for(i = 0; i < *total; i++)
{
effects_obj_ptr = (*ptr)[i];
if(effects_obj_ptr == NULL)
continue;
/* Skip given reference object */
if(obj_num == i)
continue;
/* Reset delete_this_object marker to False so if the below
* check determines that this object should be deleted,
* delete_this_object will be set to True.
*/
delete_this_object = False;
/* Handle by object type */
switch(effects_obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
case SAR_OBJ_TYPE_AIRCRAFT:
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
case SAR_OBJ_TYPE_HUMAN:
break;
case SAR_OBJ_TYPE_SMOKE:
smoke = SAR_OBJ_GET_SMOKE(effects_obj_ptr);
if(smoke != NULL)
{
if(obj_num > -1)
{
/* Does the effect object reference the given
* object?
*/
if(smoke->ref_object == obj_num)
delete_this_object = True;
}
else
{
/* No object given as input, so treat criteria
* as delete all non-errant effects objects
*/
if(smoke->ref_object > -1)
delete_this_object = True;
}
}
break;
case SAR_OBJ_TYPE_FIRE:
fire = SAR_OBJ_GET_FIRE(effects_obj_ptr);
if(fire != NULL)
{
if(obj_num > -1)
{
/* Does the effect object reference the given
* object?
*/
if(fire->ref_object == obj_num)
delete_this_object = True;
}
else
{
/* No object given as input, so treat criteria
* as delete all non-errant effects objects
*/
if(fire->ref_object > -1)
delete_this_object = True;
}
}
break;
case SAR_OBJ_TYPE_EXPLOSION:
explosion = SAR_OBJ_GET_EXPLOSION(effects_obj_ptr);
if(explosion != NULL)
{
if(obj_num > -1)
{
/* Does the effect object reference the given
* object?
*/
if(explosion->ref_object == obj_num)
delete_this_object = True;
}
else
{
/* No object given as input, so treat criteria
* as delete all non-errant effects objects
*/
if(explosion->ref_object > -1)
delete_this_object = True;
}
}
break;
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
spray = SAR_OBJ_GET_CHEMICAL_SPRAY(effects_obj_ptr);
if(spray != NULL)
{
#if 0
/* TODO */
if(obj_num > -1)
{
/* Does the effect object reference the given
* object?
*/
if(spray->owner == obj_num)
delete_this_object = True;
}
else
{
/* No object given as input, so treat criteria
* as delete all non-errant effects objects
*/
if(spray->owner > -1)
delete_this_object = True;
}
#endif
}
break;
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Delete this effect object? */
if(delete_this_object)
{
/* Delete effect object by type, this is so we can
* handle type-specific filters
*/
switch(effects_obj_ptr->type)
{
case SAR_OBJ_TYPE_SMOKE:
/* Stop smoke trail from respawning instead of
* deleting it?
*/
if(filter_flags & SARSIM_DELETE_EFFECTS_SMOKE_STOP_RESPAWN)
{
smoke = SAR_OBJ_GET_SMOKE(effects_obj_ptr);
if(smoke != NULL)
{
smoke->respawn_int = 0l;
/* Mark that smoke trail should be deleted
* when all of its units are no longer
* visible
*/
smoke->delete_when_no_units = 1;
}
}
else
{
SARObjDelete(
core_ptr, ptr, total, i
);
objects_deleted++;
}
break;
default:
/* We know this is an effects object, but of a type
* that is not filtered. So we can safely delete it
* without checking filter_flags
*/
SARObjDelete(
core_ptr, ptr, total, i
);
objects_deleted++;
break;
}
/* Do not go further since this effects object is now
* now deleted
*/
continue;
} /* Delete this effect object? */
}
return(objects_deleted);
}
/*
* Sets up values on the given aircraft object to be crashed.
*
* Does not create any explosions, play sounds, or updates related
* resources, it is up to the calling function to do that about this
* crash.
*/
void SARSimSetAircraftCrashed(
sar_scene_struct *scene,
sar_object_struct ***ptr, int *total,
sar_object_struct *obj_ptr,
sar_object_aircraft_struct *aircraft
)
{
int i, occupant_obj_num;
sar_direction_struct *dir, new_dir;
sar_obj_hoist_struct *hoist_ptr;
sar_object_struct *occupant_obj_ptr;
sar_object_human_struct *human;
if((scene == NULL) || (obj_ptr == NULL) ||
(aircraft == NULL) || (ptr == NULL) || (total == NULL)
)
return;
/* Get direction of object */
dir = &obj_ptr->dir;
/* Get pointer to aircraft's hoist */
hoist_ptr = SARObjGetHoistPtr(obj_ptr, 0, NULL);
if(hoist_ptr != NULL)
{
/* Iterate through occupants in hoist */
for(i = 0; i < hoist_ptr->total_occupants; i++)
{
occupant_obj_num = hoist_ptr->occupant[i];
if(SARObjIsAllocated(*ptr, *total, occupant_obj_num))
occupant_obj_ptr = (*ptr)[occupant_obj_num];
else
continue;
/* Skip our object itself just in case */
if(occupant_obj_ptr == obj_ptr)
continue;
/* Handle by occupant object type */
switch(occupant_obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
case SAR_OBJ_TYPE_AIRCRAFT:
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
break;
case SAR_OBJ_TYPE_HUMAN:
human = SAR_OBJ_GET_HUMAN(occupant_obj_ptr);
if(human != NULL)
{
/* Mark human as no longer gripped in the rescue
* basket.
*/
human->flags &= ~SAR_HUMAN_FLAG_GRIPPED;
/* Mark as lying down */
human->flags &= ~SAR_HUMAN_FLAG_SIT;
human->flags &= ~SAR_HUMAN_FLAG_SIT_DOWN;
human->flags &= ~SAR_HUMAN_FLAG_SIT_UP;
human->flags |= SAR_HUMAN_FLAG_LYING;
/* Mark human as not alert and not aware */
human->flags &= ~SAR_HUMAN_FLAG_ALERT;
human->flags &= ~SAR_HUMAN_FLAG_AWARE;
human->flags &= ~SAR_HUMAN_FLAG_RUN_TOWARDS;
human->flags &= ~SAR_HUMAN_FLAG_RUN_AWAY;
human->flags &= ~SAR_HUMAN_FLAG_PUSHING;
human->flags &= ~SAR_HUMAN_FLAG_DIVER_CATCHER;
/* Not in water (need to work on this) */
human->flags &= ~SAR_HUMAN_FLAG_IN_WATER;
human->flags &= ~SAR_HUMAN_FLAG_ON_STREATCHER;
}
break;
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Set attitude to make it `upright' or lie flat */
occupant_obj_ptr->dir.pitch = (float)(0.0 * PI);
occupant_obj_ptr->dir.bank = (float)(0.0 * PI);
/* Mark this occupant as no longer in the list */
hoist_ptr->occupant[i] = -1;
}
/* Clear hoist list */
free(hoist_ptr->occupant);
hoist_ptr->occupant = NULL;
hoist_ptr->total_occupants = 0;
/* Retract rope */
hoist_ptr->rope_cur = 0.0f;
hoist_ptr->rope_cur_vis = 0.0f;
hoist_ptr->on_ground = 0;
hoist_ptr->occupants_mass = 0;
}
/* Begin setting values on aircraft */
/* Mark as not flyable */
aircraft->air_worthy_state = SAR_AIR_WORTHY_NOT_FLYABLE;
/* Turn engine off */
aircraft->engine_state = SAR_ENGINE_OFF;
/* Set new direction */
new_dir.heading = dir->heading;
new_dir.pitch = (float)(0.0 * PI);
new_dir.bank = (float)(1.7 * PI); /* Tilt it */
SARSimWarpObject(scene, obj_ptr, NULL, &new_dir);
/* Turn off lights */
SARSimOpLights(obj_ptr, False);
/* Turn off spot lights */
SARSimOpAttenuate(obj_ptr, False);
/* Turn off strobes */
SARSimOpStrobes(obj_ptr, False);
}
/*
* Sets the flyable object's values to the SFM's values.
*/
void SARSimSetSFMValues(
sar_core_struct *core_ptr, sar_scene_struct *scene,
sar_object_struct *obj_ptr
)
{
int i;
SFMModelStruct *fdm = NULL;
sar_object_aircraft_struct *aircraft;
sar_air_worthy_state air_worthy_state;
sar_obj_part_struct *lgear_ptr = NULL;
sar_obj_hoist_struct *hoist_ptr;
sar_contact_bounds_struct *cb;
const sar_option_struct *opt = &core_ptr->option;
if((scene == NULL) || (obj_ptr == NULL))
return;
cb = obj_ptr->contact_bounds;
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(aircraft == NULL)
break;
fdm = aircraft->fdm;
if(fdm == NULL)
break;
air_worthy_state = aircraft->air_worthy_state;
hoist_ptr = SARObjGetHoistPtr(obj_ptr, 0, NULL);
/* Get pointer to first landing gear */
lgear_ptr = SARObjGetPartPtr(
obj_ptr, SAR_OBJ_PART_TYPE_LANDING_GEAR, 0
);
/* No landing gears if not flyable */
if(air_worthy_state == SAR_AIR_WORTHY_NOT_FLYABLE)
lgear_ptr = NULL;
/* Begin setting SFM values from object */
#define TAR_PTR fdm
#define SRC_PTR aircraft
TAR_PTR->flags = (SFMFlagFlightModelType |
SFMFlagPosition | SFMFlagDirection |
SFMFlagVelocityVector | SFMFlagSpeed |
SFMFlagSpeedStall | SFMFlagDragMin |
SFMFlagSpeedMax | SFMFlagAccelResponsiveness |
SFMFlagGroundElevation | SFMFlagServiceCeiling |
SFMFlagBellyHeight | SFMFlagGearState |
SFMFlagGearType | SFMFlagGearHeight |
SFMFlagGearBrakesState | SFMFlagGearTurnVelocityOptimul |
SFMFlagGearTurnVelocityMax | SFMFlagGearTurnRate |
SFMFlagLandedState | SFMFlagGroundContactType |
SFMFlagHeadingControlCoeff | SFMFlagBankControlCoeff |
SFMFlagPitchControlCoeff | SFMFlagThrottleCoeff |
SFMFlagAfterBurnerState | SFMFlagAfterBurnerPowerCoeff |
SFMFlagEnginePower | SFMFlagTotalMass |
SFMFlagAttitudeChangeRate | SFMFlagAttitudeLevelingRate |
SFMFlagAirBrakesState | SFMFlagAirBrakesRate |
SFMFlagCanCrashIntoOther | SFMFlagCanCauseCrash |
SFMFlagCrashContactShape | SFMFlagCrashableSizeRadius |
SFMFlagCrashableSizeZMin | SFMFlagCrashableSizeZMax |
SFMFlagTouchDownCrashResistance | SFMFlagCollisionCrashResistance
);
/* Update flight model type only if SFM not in slew mode */
switch(SRC_PTR->flight_model_type)
{
case SAR_FLIGHT_MODEL_SLEW:
TAR_PTR->type = SFMFlightModelSlew;
break;
case SAR_FLIGHT_MODEL_AIRPLANE:
TAR_PTR->type = SFMFlightModelAirplane;
break;
default:
TAR_PTR->type = SFMFlightModelHelicopter;
break;
}
TAR_PTR->speed_stall = SARSimStallSpeed(SRC_PTR);
TAR_PTR->drag_min = SRC_PTR->min_drag;
/* Calculate maximum speed, take drag into account */
TAR_PTR->speed_max =
SRC_PTR->speed_max *
(1.0 - SARSimHoistDragCoeff(scene, obj_ptr, hoist_ptr)) *
(1.0 - SARSimLandingGearDragCoeff(scene, obj_ptr, lgear_ptr)) *
(1.0 - SARSimFlapDragCoeff(scene, obj_ptr, SRC_PTR));
TAR_PTR->overspeed_expected = SRC_PTR->overspeed_expected;
TAR_PTR->overspeed = SRC_PTR->overspeed;
/* If not flyable, then reduce speed bounds */
if(air_worthy_state == SAR_AIR_WORTHY_NOT_FLYABLE)
{
TAR_PTR->drag_min = TAR_PTR->drag_min * 5.0;
TAR_PTR->speed_max = TAR_PTR->speed_max / 15.0;
}
/* Set acceleration responsiveness by flight model type */
switch(TAR_PTR->type)
{
case SFMFlightModelAirplane:
TAR_PTR->accel_responsiveness.x =
SRC_PTR->airplane_accel_responsiveness.x;
TAR_PTR->accel_responsiveness.y =
SRC_PTR->airplane_accel_responsiveness.y;
TAR_PTR->accel_responsiveness.z =
SRC_PTR->airplane_accel_responsiveness.z;
break;
default: /* Assume SFMFlightModelHelicopter */
TAR_PTR->accel_responsiveness.x =
SRC_PTR->accel_responsiveness.x;
TAR_PTR->accel_responsiveness.y =
SRC_PTR->accel_responsiveness.y;
TAR_PTR->accel_responsiveness.z =
SRC_PTR->accel_responsiveness.z;
break;
}
TAR_PTR->service_ceiling = SRC_PTR->service_ceiling;
TAR_PTR->belly_height = SRC_PTR->belly_height;
TAR_PTR->ground_elevation_msl = obj_ptr->ground_elevation_msl;
TAR_PTR->gear_state = (SFMBoolean)((lgear_ptr != NULL) ?
(lgear_ptr->flags & SAR_OBJ_PART_FLAG_STATE) : False
);
if(lgear_ptr == NULL)
{
/* Assume wheels */
TAR_PTR->gear_type = SFMGearTypeWheels;
}
else
{
if(lgear_ptr->flags & SAR_OBJ_PART_FLAG_LGEAR_SKI)
TAR_PTR->gear_type = SFMGearTypeSkis;
else
TAR_PTR->gear_type = SFMGearTypeWheels;
}
TAR_PTR->gear_height = SRC_PTR->gear_height;
TAR_PTR->gear_brakes_state = ((SRC_PTR->wheel_brakes_state > 0) ?
True : False
);
TAR_PTR->gear_turn_velocity_optimul = SRC_PTR->gturn_vel_opt;
TAR_PTR->gear_turn_velocity_max = SRC_PTR->gturn_vel_max;
/* TAR_PTR->gear_turn_rate = SRC_PTR->; */
/* TAR_PTR->ground_contact_type = SRC_PTR->; */
TAR_PTR->heading_control_coeff = 0.0;
TAR_PTR->bank_control_coeff = 0.0;
TAR_PTR->pitch_control_coeff = 0.0;
TAR_PTR->elevator_trim_coeff = 0.0;
TAR_PTR->throttle_coeff = 0.0;
TAR_PTR->after_burner_state = False;
TAR_PTR->after_burner_power_coeff = 0.0;
TAR_PTR->engine_power = SRC_PTR->engine_power;
TAR_PTR->total_mass = SRC_PTR->dry_mass + SRC_PTR->fuel;
/* Add up mass in existing external fuel tanks */
for(i = 0; i < SRC_PTR->total_external_fueltanks; i++)
{
const sar_external_fueltank_struct *eft_ptr = SRC_PTR->external_fueltank[i];
if((eft_ptr != NULL) ?
(eft_ptr->flags & SAR_EXTERNAL_FUELTANK_FLAG_ONBOARD) : 0
)
TAR_PTR->total_mass += eft_ptr->dry_mass + eft_ptr->fuel;
}
/* Add up mass in the contents of the hoist */
if((hoist_ptr != NULL) ?
(!hoist_ptr->on_ground && (hoist_ptr->rope_cur > 0.0f)) : 0
)
{
TAR_PTR->total_mass += hoist_ptr->occupants_mass;
}
/* Add up mass of passengers (if any) */
TAR_PTR->total_mass += SRC_PTR->passengers_mass;
TAR_PTR->attitude_change_rate.heading =
SRC_PTR->attitude_change_rate.heading;
TAR_PTR->attitude_change_rate.pitch =
SRC_PTR->attitude_change_rate.pitch;
TAR_PTR->attitude_change_rate.bank =
SRC_PTR->attitude_change_rate.bank;
TAR_PTR->attitude_leveling_rate.heading = 0.0;
TAR_PTR->attitude_leveling_rate.pitch = SRC_PTR->pitch_leveling;
TAR_PTR->attitude_leveling_rate.bank = SRC_PTR->bank_leveling;
TAR_PTR->air_brakes_state = (SRC_PTR->air_brakes_state > 0) ?
True : False;
TAR_PTR->air_brakes_rate = SRC_PTR->air_brakes_rate *
SARSimAirBrakeCoeff(scene, obj_ptr, SRC_PTR);
/* We'll do all collision crash checks, so set
* can_crash_into_other and can_cause_crash to False so
* FDM does not check for collisions.
*/
TAR_PTR->can_crash_into_other = False;
TAR_PTR->can_cause_crash = False;
if(cb == NULL)
{
/* Assume values for unavailable contact bounds values */
TAR_PTR->crash_contact_shape =
SFMCrashContactShapeSpherical;
TAR_PTR->crashable_size_radius = 1.0;
TAR_PTR->crashable_size_z_min = -0.5;
TAR_PTR->crashable_size_z_max = 0.5;
}
else
{
switch(cb->contact_shape)
{
case SAR_CONTACT_SHAPE_CYLENDRICAL:
TAR_PTR->crash_contact_shape =
SFMCrashContactShapeCylendrical;
break;
/* All else assume spherical */
case SAR_CONTACT_SHAPE_SPHERICAL:
case SAR_CONTACT_SHAPE_RECTANGULAR:
TAR_PTR->crash_contact_shape =
SFMCrashContactShapeSpherical;
break;
}
TAR_PTR->crashable_size_radius = cb->contact_radius;
TAR_PTR->crashable_size_z_min = cb->contact_h_min;
TAR_PTR->crashable_size_z_max = cb->contact_h_max;
}
TAR_PTR->touch_down_crash_resistance = 7.0 *
MAX(opt->damage_resistance_coeff, 1.0f);
/* Need to work on this */
TAR_PTR->collision_crash_resistance = 2.0;
/* Do not allow attitude leveling if not flyable */
if(SRC_PTR->air_worthy_state != SAR_AIR_WORTHY_FLYABLE)
TAR_PTR->flags &= ~(SFMFlagAttitudeLevelingRate);
#undef TAR_PTR
#undef SRC_PTR
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
case SAR_OBJ_TYPE_HUMAN:
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Was the SFM obtained from the above checks? */
if(fdm != NULL)
{
/* Set other common SFM values from the object's general
* structure here
*/
#define SRC_PTR fdm
#define TAR_PTR obj_ptr
#undef TAR_PTR
#undef SRC_PTR
}
}
/*
* Gets the flyable object's SFM values to the object
*/
void SARSimGetSFMValues(sar_scene_struct *scene, sar_object_struct *obj_ptr)
{
SFMModelStruct *fdm = NULL;
sar_object_aircraft_struct *aircraft = NULL;
if((scene == NULL) || (obj_ptr == NULL))
return;
/* Get FDM based on object type */
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
#define TAR_PTR aircraft
#define SRC_PTR fdm
TAR_PTR = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(TAR_PTR == NULL)
break;
SRC_PTR = TAR_PTR->fdm;
if(SRC_PTR == NULL)
break;
TAR_PTR->center_to_ground_height =
(float)SRC_PTR->center_to_ground_height;
/* Get z acceleration before updating velocity */
TAR_PTR->z_accel = (float)SRC_PTR->velocity_vector.z -
TAR_PTR->vel.z;
TAR_PTR->vel.x = (float)SRC_PTR->velocity_vector.x;
TAR_PTR->vel.y = (float)SRC_PTR->velocity_vector.y;
TAR_PTR->vel.z = (float)SRC_PTR->velocity_vector.z;
TAR_PTR->speed = (float)SRC_PTR->speed;
TAR_PTR->landed = SRC_PTR->landed_state;
#undef TAR_PTR
#undef SRC_PTR
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
case SAR_OBJ_TYPE_HUMAN:
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Got FDM? */
if(fdm != NULL)
{
#define SRC_PTR fdm
#define TAR_PTR obj_ptr
/* Get common FDM values */
sar_contact_bounds_struct *cb = TAR_PTR->contact_bounds;
TAR_PTR->pos.x = (float)SRC_PTR->position.x;
TAR_PTR->pos.y = (float)SRC_PTR->position.y;
TAR_PTR->pos.z = (float)SRC_PTR->position.z;
TAR_PTR->dir.heading = (float)SRC_PTR->direction.heading;
TAR_PTR->dir.pitch = (float)SRC_PTR->direction.pitch;
TAR_PTR->dir.bank = (float)SRC_PTR->direction.bank;
/* If object is not flyable and on water (implying it has
* crashed into the water) we need to lower the position
* so that the object appears submersed in the water
*/
if((aircraft != NULL) ?
aircraft->on_water : False
)
{
if(aircraft->air_worthy_state == SAR_AIR_WORTHY_NOT_FLYABLE)
{
TAR_PTR->pos.z = TAR_PTR->ground_elevation_msl;
}
}
/* Update contact bounds rotation if rectangular */
if((cb != NULL) ?
(cb->contact_shape == SAR_CONTACT_SHAPE_RECTANGULAR) : False
)
{
cb->cos_heading = (float)cos(-TAR_PTR->dir.heading);
cb->sin_heading = (float)sin(-TAR_PTR->dir.heading);
}
#undef TAR_PTR
#undef SRC_PTR
}
}
/*
* Applies natural forces to object.
*
* Note that natural forces applied to flyable objects will be
* applied by the SFM and not here.
*
* Returns 1 to indicate object has been deleted or memory change.
*
* Other non-zero return indicates error.
*/
int SARSimApplyNaturalForce(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
)
{
int tar_obj_num;
sar_direction_struct *dir;
sar_position_struct *pos, *vel;
sar_object_helipad_struct *helipad;
sar_object_smoke_struct *smoke;
sar_object_fire_struct *fire;
sar_object_explosion_struct *explosion;
sar_object_human_struct *human;
sar_object_fueltank_struct *fueltank;
sar_scene_struct *scene = core_ptr->scene;
const sar_option_struct *opt = &core_ptr->option;
if((scene == NULL) || (obj_ptr == NULL))
return(-1);
/* Handle by object type */
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
case SAR_OBJ_TYPE_AIRCRAFT:
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
break;
case SAR_OBJ_TYPE_HELIPAD:
helipad = SAR_OBJ_GET_HELIPAD(obj_ptr);
if(helipad == NULL)
break;
/* Helipad follows a reference object? */
if(helipad->flags & SAR_HELIPAD_FLAG_FOLLOW_REF_OBJECT)
{
sar_object_struct *ref_obj_ptr = SARObjGetPtr(
core_ptr->object, core_ptr->total_objects,
helipad->ref_object
);
if(ref_obj_ptr != NULL)
{
/* Check the type of the reference object, only
* move to the reference object if it is a type of
* object that moves around (since the helipad is
* already moved to the correct reference position
* when it is loaded)
*/
if((ref_obj_ptr->type == SAR_OBJ_TYPE_AUTOMOBILE) ||
(ref_obj_ptr->type == SAR_OBJ_TYPE_WATERCRAFT) ||
(ref_obj_ptr->type == SAR_OBJ_TYPE_AIRCRAFT) ||
(ref_obj_ptr->type == SAR_OBJ_TYPE_HUMAN)
)
{
SARSimWarpObjectRelative(
scene, obj_ptr,
core_ptr->object, core_ptr->total_objects,
helipad->ref_object,
&helipad->ref_offset,
&helipad->ref_dir
);
}
}
}
break;
case SAR_OBJ_TYPE_SMOKE:
smoke = SAR_OBJ_GET_SMOKE(obj_ptr);
if(smoke == NULL)
break;
pos = &obj_ptr->pos;
/* Any units on this smoke trails object? */
if(smoke->unit != NULL)
{
int n, units_visible = 0, smoke_puff_hidden = 0;
sar_object_smoke_unit_struct *u;
/* Iterate through all smoke trail units, updating
* each one to reflect on the smoke trail type's
* behavour
*/
for(n = 0; n < smoke->total_units; n++)
{
u = &smoke->unit[n];
/* Skip this unit if it is not visible */
if(u->visibility <= 0.0f)
continue;
units_visible++; /* Count this unit visible */
/* Change position and size of this unit */
u->pos.x += (u->vel.x *
time_compensation * time_compression
);
u->pos.y += (u->vel.y *
time_compensation * time_compression
);
u->pos.z += (u->vel.z *
time_compensation * time_compression
);
u->radius += smoke->radius_rate * time_compensation * time_compression;
/* Has the smoke unit's radius (size) grown too big? */
if(u->radius > smoke->radius_max)
{
u->radius = smoke->radius_max;
/* Should smoke puff disappear when it gets
* too big?
*/
if(smoke->hide_at_max)
{
u->visibility = 0.0f;
smoke_puff_hidden++;
continue;
}
}
}
#if 0
/* If one or more smoke units were hidden then the units
* need to be shifted so that the first contiguous units
* in the list are not hidden
*/
if(smoke_puff_hidden > 0)
{
/* TODO This won't really affect anything if we don't have it, it just
* makes smoke puffs draw in possible random order (which is still
* unlikly since we time things well)
*/
}
#endif
/* If no units are visible and this smoke trail should
* be deleted when that happens then mark the life span
* to be 1 ms
*/
if((units_visible <= 0) &&
smoke->delete_when_no_units
)
{
obj_ptr->life_span = 1l;
}
}
/* Get smoke trail's reference object, that's the object
* it is following
*/
tar_obj_num = smoke->ref_object;
if(SARObjIsAllocated(
core_ptr->object, core_ptr->total_objects, tar_obj_num
))
{
sar_object_struct *tar_obj_ptr = core_ptr->object[tar_obj_num];
/* Move smoke trail object to position of reference object */
memcpy(pos, &tar_obj_ptr->pos, sizeof(sar_position_struct));
/* Realize changes in position */
SARSimWarpObject(
scene, obj_ptr,
pos, NULL
);
}
/* Time to spawn a new smoke unit and should we respawn? */
if((smoke->respawn_next <= cur_millitime) &&
(smoke->respawn_int > 0l)
)
{
SARSimSmokeSpawn(core_ptr, obj_ptr, smoke);
/* Schedual next respawn */
smoke->respawn_next = cur_millitime +
smoke->respawn_int;
}
break;
case SAR_OBJ_TYPE_FIRE:
fire = SAR_OBJ_GET_FIRE(obj_ptr);
if(fire == NULL)
break;
pos = &obj_ptr->pos;
/* Time to increment explosion animation frame? */
if(fire->next_frame_inc <= cur_millitime)
{
int tex_num = fire->tex_num;
/* Set next time to increment animation frame */
fire->next_frame_inc = cur_millitime +
fire->frame_inc_int;
/* Increment current animation frame */
fire->cur_frame++;
/* Is fire object's texture structure available? */
if(SARIsTextureAllocated(scene, tex_num))
{
v3d_texture_ref_struct *t = scene->texture_ref[tex_num];
/* Animation frames cycled? */
if(fire->cur_frame >= t->total_frames)
{
fire->cur_frame = 0;
fire->frame_repeats++;
/* Total animation frame cycle repeats exceeded? */
if((fire->frame_repeats >=
fire->total_frame_repeats) &&
(fire->total_frame_repeats > 0)
)
{
/* Mark this object to be deleted by setting
* its life span to 1 ms which will cause it to
* be deleted
*/
obj_ptr->life_span = 1l;
/* Set current frame to be the last frame */
fire->cur_frame = t->total_frames - 1;
}
}
/* Sanitize current animation frame */
if(fire->cur_frame < 0)
fire->cur_frame = 0;
}
}
break;
case SAR_OBJ_TYPE_EXPLOSION:
explosion = SAR_OBJ_GET_EXPLOSION(obj_ptr);
if(explosion == NULL)
break;
pos = &obj_ptr->pos;
/* Time to increment explosion animation frame? */
if(explosion->next_frame_inc <= cur_millitime)
{
int tex_num = explosion->tex_num;
/* Set next time to increment animation frame */
explosion->next_frame_inc = cur_millitime +
explosion->frame_inc_int;
/* Increment current animation frame */
explosion->cur_frame++;
/* Is explosion object's texture structure available? */
if(SARIsTextureAllocated(scene, tex_num))
{
v3d_texture_ref_struct *t = scene->texture_ref[tex_num];
/* Animation frames cycled? */
if(explosion->cur_frame >= t->total_frames)
{
explosion->cur_frame = 0;
explosion->frame_repeats++;
/* Total animation frame cycle repeats exceeded? */
if((explosion->frame_repeats >=
explosion->total_frame_repeats) &&
(explosion->total_frame_repeats > 0)
)
{
/* Mark this object to be deleted by setting
* its life span to 1 ms which will cause it to
* be deleted
*/
obj_ptr->life_span = 1l;
/* Set current frame to be the last frame */
explosion->cur_frame = t->total_frames - 1;
}
}
/* Sanitize current animation frame */
if(explosion->cur_frame < 0)
explosion->cur_frame = 0;
}
}
/* Get explosion's reference object, that's the object it
* is following
*/
tar_obj_num = explosion->ref_object;
if(SARObjIsAllocated(
core_ptr->object, core_ptr->total_objects,
tar_obj_num
))
{
float ground_elevation_msl;
sar_object_struct *tar_obj_ptr = core_ptr->object[tar_obj_num];
/* Move explosion object's position to the position of
* the referenced object.
*/
memcpy(pos, &tar_obj_ptr->pos, sizeof(sar_position_struct));
/* Get ref object's ground elevation or its z position
* (whichever is greater).
*/
ground_elevation_msl = MAX(
tar_obj_ptr->ground_elevation_msl,
tar_obj_ptr->pos.z
);
/* Keep above ground level by gravity offset */
switch(explosion->center_offset)
{
case SAR_EXPLOSION_CENTER_OFFSET_BASE:
/* Need to move the position up */
pos->z += explosion->radius;
/* Sanitize z position to base */
if(pos->z < (ground_elevation_msl +
explosion->radius)
)
pos->z = ground_elevation_msl +
explosion->radius;
break;
case SAR_EXPLOSION_CENTER_OFFSET_NONE:
/* Sanitize z position to center */
if(pos->z < ground_elevation_msl)
pos->z = ground_elevation_msl;
break;
}
/* Realize changes in position */
SARSimWarpObject(
scene, obj_ptr,
pos, NULL
);
}
break;
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
break;
case SAR_OBJ_TYPE_HUMAN:
human = SAR_OBJ_GET_HUMAN(obj_ptr);
if(human == NULL)
break;
pos = &obj_ptr->pos;
/* Move human only if it is not being moved by something else
* we check the human's SAR_HUMAN_FLAG_GRIPPED flag. If it
* is set then we do not apply any forces to the human.
*/
if(human->flags & SAR_HUMAN_FLAG_GRIPPED)
{
/* Human is being moved by another object, possibly the
* it is in a rescue basket. In any case we do not apply
* any forces to the human object.
*/
}
else
{
/* Pull human down with gravity (use constant 3 meters per
* second velocity for now).
*
* We must keep the rate less than 3.0 since contact
* checks to keep human above objects like buildings
* (because buildings have hollow contact bounds, but
* heightfields are solid contact bounds).
*/
pos->z += (float)MAX(
(SAR_DEF_HUMAN_FALL_RATE * time_compensation * time_compression),
SAR_DEF_HUMAN_FALL_RATE
);
/* Keep human above ground level */
if(pos->z < obj_ptr->ground_elevation_msl)
pos->z = obj_ptr->ground_elevation_msl;
/* Realize changes in position */
SARSimWarpObject(
scene, obj_ptr,
pos, NULL
);
}
break;
case SAR_OBJ_TYPE_FUELTANK:
fueltank = SAR_OBJ_GET_FUELTANK(obj_ptr);
if(fueltank == NULL)
break;
dir = &obj_ptr->dir;
pos = &obj_ptr->pos;
/* Landed on ground yet? */
if(fueltank->flags & SAR_FUELTANK_FLAG_ON_GROUND)
{
}
else
{
float cos_heading = (float)cos(dir->heading),
sin_heading = (float)sin(dir->heading);
vel = &fueltank->vel;
vel->z += (float)-(
SAR_GRAVITY * time_compensation * time_compression
);
if(vel->z < fueltank->vel_z_max)
vel->z = fueltank->vel_z_max;
pos->x += (((sin_heading * vel->y) +
(cos_heading * vel->x)) *
time_compensation * time_compression
);
pos->y += (((cos_heading * vel->y) -
(sin_heading * vel->x)) *
time_compensation * time_compression
);
pos->z += vel->z * time_compensation * time_compression;
/* Landed? */
if((pos->z - fueltank->belly_to_center_height) <
obj_ptr->ground_elevation_msl
)
{
Boolean over_water;
int obj_num;
float contact_radius = MAX(
SARSimGetFlatContactRadius(obj_ptr), 1.0f
);
/* Get object number */
for(obj_num = 0; obj_num < core_ptr->total_objects; obj_num++)
{
if(obj_ptr == core_ptr->object[obj_num])
break;
}
if(obj_num >= core_ptr->total_objects)
obj_num = -1;
/* Keep position above ground level */
pos->z = obj_ptr->ground_elevation_msl +
fueltank->belly_to_center_height;
/* Mark as landed */
fueltank->flags |= SAR_FUELTANK_FLAG_ON_GROUND;
/* Set lifespan so it gets removed after a while */
obj_ptr->life_span = cur_millitime +
opt->fuel_tank_life_span;
/* Landed on water? */
SARGetGHCOverWater(
core_ptr, scene,
&core_ptr->object, &core_ptr->total_objects,
obj_num,
NULL, &over_water
);
if(over_water)
{
/* Sink down a little so that center is at the
* water level
*/
pos->z = obj_ptr->ground_elevation_msl;
/* Landed on water, create splash */
SplashCreate(
core_ptr, scene,
&core_ptr->object, &core_ptr->total_objects,
pos,
1.5f * contact_radius, /* Radius size in meters */
obj_num, /* Reference object */
SAR_STD_TEXNAME_SPLASH, SAR_STD_TEXNAME_SPLASH
);
if(opt->event_sounds)
SARSoundSourcePlayFromList(
core_ptr->recorder,
scene->sndsrc, scene->total_sndsrcs,
"splash_human",
pos, dir, &scene->ear_pos
);
}
else
{
/* Landed on ground, create a smoke puff */
SmokeCreate(
scene, &core_ptr->object, &core_ptr->total_objects,
SAR_SMOKE_TYPE_SMOKE,
pos, NULL,
0.5f * contact_radius, /* Radius min */
2.0f * contact_radius, /* Radius max */
-1.0f, /* Autocalc growth */
1, /* Hide at max */
1, /* Total units */
3000, /* Respawn interval in ms */
SAR_STD_TEXNAME_SMOKE_LIGHT,
-1, /* No reference object */
cur_millitime + 3000
);
if(opt->event_sounds)
SARSoundSourcePlayFromList(
core_ptr->recorder,
scene->sndsrc, scene->total_sndsrcs,
"thud_medium",
pos, dir, &scene->ear_pos
);
}
}
/* Realize changes in position */
SARSimWarpObject(
scene, obj_ptr,
pos, NULL
);
}
break;
case SAR_OBJ_TYPE_PREMODELED:
break;
}
return(0);
}
/*
* Called once per cycle for each object to apply artificial
* forces to the object.
*
* Can return 1 to indicate object has been deleted or memory
* change.
*
* Other non-zero return indicates error.
*/
int SARSimApplyArtificialForce(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr
)
{
int i, air_worthy_state;
float thrust_output = 0.0f, throttle_output = 0.0f;
float ground_elevation, cen_to_gear_height;
char ear_in_cockpit, is_player;
sar_direction_struct *dir;
sar_position_struct *pos, *vel;
sar_object_aircraft_struct *aircraft;
sar_object_human_struct *human;
sar_obj_rotor_struct *rotor_ptr;
sar_obj_part_struct *gear;
sar_obj_hoist_struct *hoist;
sar_contact_bounds_struct *cb;
sar_scene_struct *scene = core_ptr->scene;
const sar_option_struct *opt = &core_ptr->option;
if((scene == NULL) || (obj_ptr == NULL))
return(-1);
/* Check if the given object is the player object */
if(scene->player_obj_ptr == obj_ptr)
{
is_player = 1;
ear_in_cockpit = SAR_IS_EAR_IN_COCKPIT(scene);
}
else
{
is_player = 0;
ear_in_cockpit = 0;
}
pos = &obj_ptr->pos;
dir = &obj_ptr->dir;
/* Get contact bounds (may be NULL) */
cb = obj_ptr->contact_bounds;
/* Begin calculating vertical distance from 0.0 to object for
* object types that need to land or walk on surfaces (humans,
* aircrafts, and fueltanks)
*
* Set starting elevation to 0.0 (not scene's msl elevation,
* that is only added when displaying current MSL altitudes)
*/
ground_elevation = 0.0f;
/* Check objects of types that need to know about ground
* elevation.
*/
if((obj_ptr->type == SAR_OBJ_TYPE_AIRCRAFT) ||
(obj_ptr->type == SAR_OBJ_TYPE_HUMAN) ||
(obj_ptr->type == SAR_OBJ_TYPE_FUELTANK)
)
{
/* We need to check if this object has contact bounds that
* specify SAR_CRASH_FLAG_SUPPORT_SURFACE, because if it
* does then checking for surface at the position of this
* object may hit itself as a surface and cause recursion
*/
if(!(((cb != NULL) ? cb->crash_flags : 0) &
SAR_CRASH_FLAG_SUPPORT_SURFACE)
)
{
/* This object does not specify countact bounds for
* SAR_CRASH_FLAG_SUPPORT_SURFACE
*/
/* Check for other objects that are "under" this object
* for increased ground elevation, this checks for all
* all objects that have either a contact bounds set for
* SAR_CRASH_FLAG_SUPPORT_SURFACE and objects of type
* SAR_OBJ_TYPE_GROUND
*
* Only SAR_OBJ_TYPE_GROUND objects will be treated as
* "solid" ground while objects with contact bounds will
* be "hollow".
*/
ground_elevation += SARSimFindGround(
scene,
core_ptr->object, core_ptr->total_objects,
pos /* Position of our object */
);
}
}
/* Set new ground elevation value on the object */
obj_ptr->ground_elevation_msl = ground_elevation;
/* Handle by object type */
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(aircraft == NULL)
break;
vel = &aircraft->vel;
air_worthy_state = aircraft->air_worthy_state;
/* Get pointer to first landing gear */
gear = SARObjGetPartPtr(
obj_ptr, SAR_OBJ_PART_TYPE_LANDING_GEAR, 0
);
/* Calculate center of object to lower of landing gear height */
cen_to_gear_height = aircraft->belly_height;
if(gear != NULL)
cen_to_gear_height += (float)((gear->flags & SAR_OBJ_PART_FLAG_STATE) ?
aircraft->gear_height : 0.0
);
/* Calculate throttle output */
throttle_output = SARSimThrottleOutputCoeff(
(aircraft->flight_model_type != SAR_FLIGHT_MODEL_SLEW) ?
aircraft->flight_model_type :
aircraft->last_flight_model_type
,
aircraft->throttle,
aircraft->collective,
aircraft->collective_range
);
/* Any fuel left? */
if(aircraft->fuel > 0.0f)
{
/* Handle engine initialization */
if(aircraft->engine_state == SAR_ENGINE_INIT)
{
/* Time to initialize? */
if(aircraft->next_engine_on <= cur_millitime)
{
aircraft->engine_state = SAR_ENGINE_ON;
aircraft->next_engine_on = 0l;
}
}
/* Calculate thrust output and consume fuel if engine
* is on
*/
if((aircraft->service_ceiling > 0.0f) &&
(aircraft->engine_state == SAR_ENGINE_ON)
)
{
/* Has fuel left, consume based on the fuel rate
* and the actual throttle output coeff
*/
aircraft->fuel -= aircraft->fuel_rate *
throttle_output * time_compensation * time_compression;
if(aircraft->fuel <= 0.0f)
{
aircraft->fuel = 0.0f;
aircraft->engine_state = SAR_ENGINE_OFF;
}
else
{
/* Calculate thrust output, the closer the
* object is to its service ceiling the less
* thrust output will be
*/
thrust_output = (float)(throttle_output *
aircraft->engine_power *
MAX(1.0 - (pos->z /
aircraft->service_ceiling),
0.0
)
);
if(thrust_output < 0.0f)
thrust_output = 0.0f;
}
}
}
else if(aircraft->engine_state == SAR_ENGINE_ON)
{
/* No fuel and engine state still on, in which case turn
* engine off
*/
aircraft->engine_state = SAR_ENGINE_OFF;
}
/* Spin rotor(s) based on actual throttle and engine state,
* and update rotor pitch animation values
*/
for(i = 0; i < aircraft->total_rotors; i++)
{
rotor_ptr = aircraft->rotor[i];
if(rotor_ptr == NULL)
continue;
/* Spin rotor only if engine or throttle is on */
if((aircraft->engine_state == SAR_ENGINE_ON) ||
(aircraft->throttle > 0.0f)
)
{
rotor_ptr->anim_pos += (int)MAX(
(float)(aircraft->throttle + 0.2) *
(float)((int)((sar_grad_anim_t)-1) * 1.40) *
time_compensation * time_compression,
1
);
rotor_ptr->rotor_wash_anim_pos += (int)MAX(
(float)(aircraft->throttle + 0.2) *
(float)((int)((sar_grad_anim_t)-1) * 0.50) *
time_compensation * time_compression,
1
);
}
if(rotor_ptr->flags & SAR_ROTOR_FLAG_CAN_PITCH)
{
/* Pitched down? */
if((rotor_ptr->flags & SAR_ROTOR_FLAG_PITCH_STATE) &&
(rotor_ptr->pitch_anim_pos < (sar_grad_anim_t)-1)
)
{
rotor_ptr->pitch_anim_pos = (sar_grad_anim_t)MIN(
rotor_ptr->pitch_anim_pos +
(10000 * time_compensation * time_compression),
(sar_grad_anim_t)-1
);
}
/* Pitched up? */
else if(!(rotor_ptr->flags & SAR_ROTOR_FLAG_PITCH_STATE) &&
(rotor_ptr->pitch_anim_pos > 0)
)
{
rotor_ptr->pitch_anim_pos = (sar_grad_anim_t)MAX(
rotor_ptr->pitch_anim_pos -
(10000 * time_compensation * time_compression),
0
);
}
}
}
/* Update animation position for all object parts */
for(i = 0; i < aircraft->total_parts; i++)
SARSimUpdatePart(
scene,
obj_ptr, aircraft->part[i],
core_ptr->recorder, opt->event_sounds, ear_in_cockpit
);
/* Update hoist and its occupants (if rope is out) */
hoist = SARObjGetHoistPtr(obj_ptr, 0, NULL);
if((hoist != NULL) ? (hoist->rope_cur > 0.0f) : 0)
{
double a[3 * 1], r[3 * 1];
sar_position_struct *basket_pos = &hoist->pos;
int tar_obj_num;
sar_object_struct *tar_obj_ptr;
/* Get matrix a as hoist offset translation */
a[0] = hoist->offset.x;
a[1] = hoist->offset.y;
a[2] = hoist->offset.z;
/* Rotate matrix a into r */
MatrixRotateBank3(a, -dir->bank, r); /* Our bank is negative,
* so pass as flipped sign
*/
MatrixRotatePitch3(r, dir->pitch, a);
MatrixRotateHeading3(a, dir->heading, r);
/* Calculate position of hoist's rescue basket in world
* coordinates by adding rorated offset relative to
* object's center
*/
basket_pos->x = (float)(pos->x + r[0]);
basket_pos->y = (float)(pos->y + r[1]);
basket_pos->z = (float)(pos->z + r[2]);
/* Set basket position to be at end of rope length */
basket_pos->z -= hoist->rope_cur;
/* Update visual length of rope */
hoist->rope_cur_vis = hoist->rope_cur;
/* Check if basket is `close enough' to be contacting
* the ground
*/
if(basket_pos->z <= (ground_elevation + 0.01))
hoist->on_ground = 1;
else
hoist->on_ground = 0;
/* Is rope out so much that basket is embedded into
* the ground?
*/
if(basket_pos->z < ground_elevation)
{
/* Reduce current visual extension of rope */
hoist->rope_cur_vis = (float)MAX(
hoist->rope_cur -
ground_elevation + basket_pos->z,
0.0
);
/* Sanitize basket z position */
basket_pos->z = ground_elevation;
}
/* Adjust heading of hoist basket to match that of
* the object it is connected to
*/
memcpy(
&hoist->dir,
dir,
sizeof(sar_direction_struct)
);
/* Move occupants in rescue basket to the new position
* of the rescue basket
*/
for(i = 0; i < hoist->total_occupants; i++)
{
tar_obj_num = hoist->occupant[i];
if(SARObjIsAllocated(
core_ptr->object, core_ptr->total_objects,
tar_obj_num
))
tar_obj_ptr = core_ptr->object[tar_obj_num];
else
continue;
/* Move passenger along with rescue basket */
memcpy(
&tar_obj_ptr->pos,
basket_pos,
sizeof(sar_position_struct)
);
/* Offset passenger position by current deployment */
switch(hoist->cur_deployment)
{
case SAR_HOIST_DEPLOYMENT_BASKET:
/* Move passenger up since basket is above the
* water
*/
tar_obj_ptr->pos.z += 0.2f;
break;
case SAR_HOIST_DEPLOYMENT_DIVER:
/* Move passenger down a bit since diver is
* waist deep offset into the water
*/
tar_obj_ptr->pos.z -= 1.0;
break;
case SAR_HOIST_DEPLOYMENT_HOOK:
tar_obj_ptr->pos.z -= 1.0;
break;
}
/* Direction of occupant is to match direction of
* hoist rescue basket
*/
memcpy(
&tar_obj_ptr->dir,
&hoist->dir,
sizeof(sar_direction_struct)
);
/* Formally realize changes to occupant object */
SARSimWarpObject(
scene, tar_obj_ptr,
&tar_obj_ptr->pos, &tar_obj_ptr->dir
);
}
/* Update animation position */
hoist->anim_pos += (sar_grad_anim_t)(
hoist->anim_rate * time_compensation * time_compression
);
} /* Update hoist */
/* Check if any passengers are pending to leave */
if(aircraft->passengers_leave_pending > 0)
{
sar_obj_part_struct *door_ptr = SARObjGetPartPtr(
obj_ptr, SAR_OBJ_PART_TYPE_DOOR_RESCUE, 0
);
/* Check if door is opened all the way and the
* aircraft is safely landed?
*/
if(aircraft->landed &&
(door_ptr == NULL) ?
1 : ((door_ptr->anim_pos == (sar_grad_anim_t)-1) &&
(door_ptr->flags & SAR_OBJ_PART_FLAG_STATE))
)
{
/* Landed and door fully opened */
/* TODO
*
* The door needs to be checked if its fully opened and then
* remove the passengers by creating human objects at the
* door position
*
* The mission callback for humans leaving aircraft needs to
* be called as well
*/
}
}
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
break;
case SAR_OBJ_TYPE_HUMAN:
human = SAR_OBJ_GET_HUMAN(obj_ptr);
if(human == NULL)
break;
/* Update animation */
human->anim_pos += (sar_grad_anim_t)(
(float)human->anim_rate *
time_compensation * time_compression
);
/* Move human only if it is not being moved by something else
* we check the human's SAR_HUMAN_FLAG_GRIPPED flag
*
* If it is set then we do not apply any forces to the
* human
*/
if(human->flags & SAR_HUMAN_FLAG_GRIPPED)
{
/* Human is being moved by another object, possibly
* the it is in a rescue basket
*
* In any case we do not apply any forces to the human
* object
*/
}
else
{
/* Check if human is running towards or away from an
* object
*/
int ref_obj_num = human->intercepting_object;
sar_object_struct *ref_obj_ptr = NULL;
/* Check cases where intercepting object number would be
* a special value
*/
switch(ref_obj_num)
{
case -2: /* Player object */
ref_obj_num = scene->player_obj_num;
break;
}
/* Match intercept object */
ref_obj_ptr = SARObjGetPtr(
core_ptr->object, core_ptr->total_objects,
ref_obj_num
);
/* Got an intercepting object and it is not the same as
* the given object?
*/
if((ref_obj_ptr != NULL) &&
(ref_obj_ptr != obj_ptr)
)
{
sar_position_struct *ref_pos = &ref_obj_ptr->pos, delta;
sar_direction_struct *ref_dir = &ref_obj_ptr->dir;
float ref_distance2d, ref_distance3d;
/* Calculate general deltas from human to reference
* object, a more specific deltas calculation will
* be performed later if possible otherwise it falls
* back to this calculation
*/
delta.x = ref_pos->x - pos->x;
delta.y = ref_pos->y - pos->y;
delta.z = ref_pos->z - pos->z;
/* Run towards? */
if(human->flags & SAR_HUMAN_FLAG_RUN_TOWARDS)
{
sar_obj_part_struct *door_ptr = NULL;
int passengers = 0, passengers_max = 0;
/* Reset human run flag (it will be updated below) */
human->flags &= ~SAR_HUMAN_FLAG_RUN;
/* Handle movement by the reference object type */
switch(ref_obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
aircraft = SAR_OBJ_GET_AIRCRAFT(ref_obj_ptr);
/* Aircraft landed and is airworthy? */
if((aircraft != NULL) ?
(aircraft->landed &&
(aircraft->air_worthy_state == SAR_AIR_WORTHY_FLYABLE)) : 0
)
{
door_ptr = SARObjGetPartPtr(
ref_obj_ptr, SAR_OBJ_PART_TYPE_DOOR_RESCUE, 0
);
passengers = aircraft->passengers;
passengers_max = aircraft->passengers_max;
}
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
break;
case SAR_OBJ_TYPE_HUMAN:
/* TODO: Another human */
break;
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Door exists on the reference object to
* calculate accurate delta position?
*/
if(door_ptr != NULL)
{
/* Calculate door's world position */
sar_position_struct *offset = &door_ptr->pos_cen;
sar_position_struct thres_pos;
double a[3 * 1], r[3 * 1];
/* Taking the reference object's rotation
* and offseting from the reference object's
* world position
*/
a[0] = offset->x;
a[1] = offset->y;
a[2] = offset->z;
/* Rotate matrix a into r */
MatrixRotateBank3(a, -ref_dir->bank, r); /* Our bank is negative,
* so pass as flipped sign.
*/
MatrixRotatePitch3(r, ref_dir->pitch, a);
MatrixRotateHeading3(a, ref_dir->heading, r);
/* Calculate door's threshold world position */
thres_pos.x = (float)(ref_pos->x + r[0]);
thres_pos.y = (float)(ref_pos->y + r[1]);
thres_pos.z = (float)(ref_pos->z + r[2]);
/* Update delta from human to reference object */
delta.x = thres_pos.x - pos->x;
delta.y = thres_pos.y - pos->y;
delta.z = thres_pos.z - pos->z;
}
/* Calculate 2d and 3d distances to reference object */
human->intercepting_object_distance2d =
ref_distance2d = (float)SFMHypot2(delta.x, delta.y);
human->intercepting_object_distance3d =
ref_distance3d = (float)SFMHypot2(ref_distance2d, delta.z);
/* Close enough for human to enter reference
* object and is there enough room for one more
* passenger
*/
if((ref_distance2d < 0.5f) && (passengers < passengers_max))
{
/* Door exists and close enough in 3d distance? */
if((door_ptr != NULL) && (ref_distance3d < 4.0f))
{
/* Door not fully opened? */
if(door_ptr->anim_pos != (sar_grad_anim_t)(-1))
{
/* Door marked opened? */
if(door_ptr->flags & SAR_OBJ_PART_FLAG_STATE)
{
/* Wait patiently for door to open */
}
else
{
/* Open the door then */
SARSimOpDoorRescue(
scene, &core_ptr->object, &core_ptr->total_objects,
ref_obj_ptr, 1
);
}
}
else
{
/* Door fully opened and in range, enter! */
int this_obj_num = SARGetObjectNumberFromPointer(
scene, core_ptr->object, core_ptr->total_objects,
obj_ptr
);
/* Begin closing door on reference object
* if the door is not to remain opened.
*/
if(!(door_ptr->flags & SAR_OBJ_PART_FLAG_DOOR_STAY_OPEN))
SARSimOpDoorRescue(
scene, &core_ptr->object, &core_ptr->total_objects,
ref_obj_ptr, 0
);
/* Print enter message */
if((scene->player_obj_ptr == ref_obj_ptr) &&
(human->mesg_enter != NULL)
)
SARMessageAdd(scene, human->mesg_enter);
/* Move our object into the reference object if
* possible and update the reference object's
* passengers count.
*/
SARSimBoardObject(
core_ptr, ref_obj_ptr, this_obj_num
);
/* Given human object no longer exists, so
* return marking that the object is gone.
*/
return(1);
}
}
}
/* Close enough to run towards, there is a
* door (implying reference object is landed),
* and room for one more passenger?
*/
else if((ref_distance2d < 15.0f) &&
(door_ptr != NULL) &&
(passengers < passengers_max)
)
{
/* Set run flag on human */
human->flags |= SAR_HUMAN_FLAG_RUN;
pos->x += (float)(
sin(dir->heading) * 1.0 *
time_compensation * time_compression
);
pos->y += (float)(
cos(dir->heading) * 1.0 *
time_compensation * time_compression
);
}
/* Set new direction of human object */
dir->heading = (float)SFMSanitizeRadians(
(0.5 * PI) - atan2(delta.y, delta.x)
);
dir->pitch = (float)(0.0 * PI);
dir->bank = (float)(0.0 * PI);
/* Realize new position and direction of human */
SARSimWarpObject(scene, obj_ptr, pos, dir);
}
/* Run away? */
else if(human->flags & SAR_HUMAN_FLAG_RUN_AWAY)
{
/* Reset human run flag (it will be updated below) */
human->flags &= ~SAR_HUMAN_FLAG_RUN;
/* Need to work on this */
if(0)
{
human->flags |= SAR_HUMAN_FLAG_RUN;
pos->x += (float)(
sin(dir->heading) * 1.0 *
time_compensation * time_compression
);
pos->y += (float)(
cos(dir->heading) * 1.0 *
time_compensation * time_compression
);
}
/* Set new direction of human object */
dir->heading = (float)SFMSanitizeRadians(
(0.5 * PI) - atan2(delta.y, delta.x)
);
dir->pitch = (float)(0.0 * PI);
dir->bank = (float)(0.0 * PI);
/* Realize new position and direction of human */
SARSimWarpObject(scene, obj_ptr, pos, dir);
}
}
}
break;
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Update values common to all object types here */
/* Lights */
SARSimUpdateLights(obj_ptr);
return(0);
}
/*
* Called by SARSimApplyGCTL() to modify the given control position
* inputs by autopilot if autopilot is activated on the
* given object.
*
* If autopilot is not on then no modifications will be made.
*
* If autopilot is on then the game controller will be checked
* against the given control position values to see if the
* autopilot should be automatically turned off. If not then
* the given control positions (not those in the gctl structure)
* will be modified to simulate corrections made by the autopilot.
*/
static void SARSimApplyGCTLAutoPilot(
sar_core_struct *core_ptr, sar_object_struct *obj_ptr,
gctl_struct *gc,
float *h_con_coeff,
float *p_con_coeff,
float *b_con_coeff,
float *elevator_trim, /* Not the effective elevator trim */
float *throttle_coeff,
float *collective
)
{
sar_object_aircraft_struct *aircraft;
if((obj_ptr == NULL) || (gc == NULL))
return;
if((h_con_coeff == NULL) || (p_con_coeff == NULL) ||
(b_con_coeff == NULL) || (elevator_trim == NULL) ||
(throttle_coeff == NULL) || (collective == NULL)
)
return;
/* Begin handling by object type */
aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(aircraft != NULL)
{
char need_autopilot_off = 0;
float tmp_delta;
float new_collective;
float tar_z, cur_z;
/* Check if autopilot is off */
if(aircraft->autopilot_state == SAR_AUTOPILOT_OFF)
return;
/* Begin calculating new controller positions based on
* flight model type
*/
switch(aircraft->flight_model_type)
{
case SAR_FLIGHT_MODEL_HELICOPTER:
/* Begin adjusting the collective, calculate the new
* collective value.
*/
new_collective = *collective;
cur_z = obj_ptr->pos.z;
tar_z = aircraft->autopilot_altitude;
tmp_delta = tar_z - cur_z;
/* Need to go up? */
if(tmp_delta > 0.1)
{
/* Increase or decrease collective to achieve the
* target velocity.
*/
float tar_vel_z = (float)MIN(tmp_delta / 2.0, 1.0);
if(aircraft->vel.z < tar_vel_z)
new_collective += (float)(0.01 * time_compensation);
else
new_collective -= (float)(0.01 * time_compensation);
}
/* Need to go down? */
else if(tmp_delta < -0.1)
{
/* Increase or decrease collective to achieve the
* target velocity.
*/
float tar_vel_z = (float)MAX(tmp_delta / 2.0, -1.0);
if(aircraft->vel.z < tar_vel_z)
new_collective += (float)(0.01 * time_compensation);
else
new_collective -= (float)(0.01 * time_compensation);
}
/* Check if new controller positions differ too great
* from current controller positions that autopilot needs
* to be turned off.
*/
tmp_delta = new_collective - gc->throttle;
if((tmp_delta > 0.1) || (tmp_delta < -0.1))
need_autopilot_off = 1;
/* Does autopilot need to be turned off? */
if(need_autopilot_off)
{
aircraft->autopilot_state = SAR_AUTOPILOT_OFF;
SARMessageAdd(core_ptr->scene, "Autopilot: Off");
}
else
{
/* Autopilot should remain on, so set new calculated
* control positions
*/
*collective = new_collective;
}
break;
case SAR_FLIGHT_MODEL_AIRPLANE:
/* Autopilot for airplane flight models is poor right
* now, instead of modifying the controls the actual
* pitch and bank is modified. This should be improved
* later however in order to adjust only the controls
* the pitch and bank *rates* need to be known and they
* are not known so far.
*/
if(1)
{
SFMModelStruct *fdm = aircraft->fdm;
/* Adjust pitch */
if(obj_ptr->dir.pitch < (1.0 * PI))
{
obj_ptr->dir.pitch = (float)MAX(
obj_ptr->dir.pitch - 0.005, 0.0 * PI
);
}
else
{
obj_ptr->dir.pitch = (float)MIN(
obj_ptr->dir.pitch + 0.005, 2.0 * PI
);
if(obj_ptr->dir.pitch >= (2.0 * PI))
obj_ptr->dir.pitch = (float)(0.0 * PI);
}
/* Adjust bank */
if(obj_ptr->dir.bank < (1.0 * PI))
{
obj_ptr->dir.bank = (float)MAX(
obj_ptr->dir.bank - 0.005, 0.0 * PI
);
}
else
{
obj_ptr->dir.bank = (float)MIN(
obj_ptr->dir.bank + 0.005, 2.0 * PI
);
if(obj_ptr->dir.bank >= (2.0 * PI))
obj_ptr->dir.bank = (float)(0.0 * PI);
}
if(fdm != NULL)
{
fdm->direction.pitch = obj_ptr->dir.pitch;
fdm->direction.bank = obj_ptr->dir.bank;
}
}
/* Check if new controller positions differ too great
* from current controller positions that autopilot needs
* to be turned off.
*/
tmp_delta = gc->pitch - 0.0f;
if((tmp_delta > 0.2) || (tmp_delta < -0.2f))
need_autopilot_off = 1;
tmp_delta = gc->bank - 0.0f;
if((tmp_delta > 0.2) || (tmp_delta < -0.2f))
need_autopilot_off = 1;
/* Does autopilot need to be turned off? */
if(need_autopilot_off)
{
aircraft->autopilot_state = SAR_AUTOPILOT_OFF;
SARMessageAdd(core_ptr->scene, "Autopilot: Off");
}
else
{
/* Autopilot should remain on, so set new calculated
* control positions
*/
}
break;
/* Other flight model, possibly slew, ignore */
case SAR_FLIGHT_MODEL_SLEW:
break;
}
/* Check if game controller positions would cause the
* autopilot to turn off automatically (that is, if they
* are moved far beyond the autopilot calculated control
* range
*/
}
}
/*
* Apply game controller (gctl) values to the given player object
* and camera on the scene structure found on the given core
* structure.
*
* The gctl values will be applied to both the object and the
* object's FDM (if it has one).
*/
void SARSimApplyGCTL(sar_core_struct *core_ptr, sar_object_struct *obj_ptr)
{
const float camera_turn_rate = (float)(0.30 * PI * SAR_SEC_TO_CYCLE_COEFF);
int rotor_num;
sar_air_worthy_state air_worthy_state;
sar_engine_state engine_state;
sar_flight_model_type flight_model_type;
sar_autopilot_state autopilot_state = SAR_AUTOPILOT_OFF;
sar_direction_struct *dir;
sar_position_struct *pos;
sar_object_aircraft_struct *aircraft = NULL;
const sar_contact_bounds_struct *cb;
float contact_radius = 0.0f;
sar_obj_hoist_struct *hoist = NULL;
sar_obj_part_struct *door_ptr = NULL;
sar_obj_rotor_struct *rotor_ptr;
SFMModelStruct *fdm = NULL;
float h_con_coeff = 0.0f,
p_con_coeff = 0.0f,
b_con_coeff = 0.0f,
elevator_trim = 0.0f,
elevator_trim_effective = 0.0f,
throttle_coeff = 0.0f,
throttle_output = 0.0f,
collective = 0.0f;
sar_scene_struct *scene = core_ptr->scene;
gctl_struct *gc = core_ptr->gctl;
const sar_option_struct *opt = &core_ptr->option;
if((scene == NULL) || (gc == NULL) || (obj_ptr == NULL))
return;
cb = obj_ptr->contact_bounds; /* Get contact bounds */
/* Get flat contact radius */
contact_radius = SARSimGetFlatContactRadius(obj_ptr);
/* Apply game controller values based on object type */
switch(obj_ptr->type)
{
case SAR_OBJ_TYPE_GARBAGE:
case SAR_OBJ_TYPE_STATIC:
case SAR_OBJ_TYPE_AUTOMOBILE:
case SAR_OBJ_TYPE_WATERCRAFT:
break;
case SAR_OBJ_TYPE_AIRCRAFT:
aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
if(aircraft == NULL)
break;
dir = &obj_ptr->dir;
pos = &obj_ptr->pos;
air_worthy_state = aircraft->air_worthy_state;
engine_state = aircraft->engine_state;
flight_model_type = aircraft->flight_model_type;
autopilot_state = aircraft->autopilot_state;
elevator_trim = aircraft->elevator_trim;
door_ptr = SARObjGetPartPtr(obj_ptr, SAR_OBJ_PART_TYPE_DOOR_RESCUE, 0);
hoist = SARObjGetHoistPtr(obj_ptr, 0, NULL);
fdm = aircraft->fdm;
/* If flight model is slew then use previous flight model type */
if(flight_model_type == SAR_FLIGHT_MODEL_SLEW)
flight_model_type = aircraft->last_flight_model_type;
/* Begin fetching control position values based on landed
* state and airworthyness
*/
/* Check if landed */
if(aircraft->landed)
{
/* Is landed */
/* Handle flight control by airworthy states */
if(air_worthy_state == SAR_AIR_WORTHY_FLYABLE)
{
if(engine_state == SAR_ENGINE_ON)
{
h_con_coeff = (float)CLIP(gc->heading, -1.0, 1.0);
p_con_coeff = (float)CLIP(gc->pitch, -1.0, 1.0);
b_con_coeff = (float)CLIP(gc->bank, -1.0, 1.0);
elevator_trim_effective = (float)CLIP(
aircraft->elevator_trim, -1.0, 1.0
);
switch(flight_model_type)
{
case SAR_FLIGHT_MODEL_AIRPLANE:
throttle_output = throttle_coeff = (float)CLIP(gc->throttle, 0.0, 1.0);
collective = 1.0f;
break;
case SAR_FLIGHT_MODEL_HELICOPTER:
throttle_coeff = 1.0f;
if(autopilot_state == SAR_AUTOPILOT_ON)
collective = (float)CLIP(aircraft->collective, 0.0, 1.0);
else
collective = (float)CLIP(gc->throttle, 0.0, 1.0);
throttle_output = (float)CLIP(throttle_coeff -
aircraft->collective_range + (
collective * aircraft->collective_range),
0.0, 1.0
);
break;
case SAR_FLIGHT_MODEL_SLEW:
break;
}
}
else
{
/* Engine off while on ground, only allow ground turning */
h_con_coeff = (float)CLIP(gc->heading, -1.0, 1.0);
}
}
else if(air_worthy_state == SAR_AIR_WORTHY_OUT_OF_CONTROL)
{
/* Out of control and landed */
/* Check if engines are on, if so give it just
* half power at most
*/
if(engine_state == SAR_ENGINE_ON)
{
switch(flight_model_type)
{
case SAR_FLIGHT_MODEL_AIRPLANE:
throttle_output = throttle_coeff = (float)CLIP(gc->throttle * 0.5, 0.0, 1.0);
collective = 1.0f;
break;
case SAR_FLIGHT_MODEL_HELICOPTER:
throttle_coeff = 0.5f;
if(autopilot_state == SAR_AUTOPILOT_ON)
collective = (float)CLIP(aircraft->collective, 0.0, 1.0);
else
collective = (float)CLIP(gc->throttle, 0.0, 1.0);
throttle_output = (float)CLIP(throttle_coeff -
aircraft->collective_range + (
collective * aircraft->collective_range),
0.0, 1.0
);
break;
case SAR_FLIGHT_MODEL_SLEW:
break;
}
}
}
else
{
/* Not flyable and landed */
}
}
else
{
/* In flight */
/* Handle flight control by airworthy states */
if(air_worthy_state == SAR_AIR_WORTHY_FLYABLE)
{
h_con_coeff = (float)CLIP(gc->heading, -1.0, 1.0);
p_con_coeff = (float)CLIP(gc->pitch, -1.0, 1.0);
b_con_coeff = (float)CLIP(gc->bank, -1.0, 1.0);
elevator_trim_effective = (float)CLIP(
aircraft->elevator_trim, -1.0, 1.0
);
/* Check if engines are on */
if(engine_state == SAR_ENGINE_ON)
{
switch(flight_model_type)
{
case SAR_FLIGHT_MODEL_AIRPLANE:
throttle_output = throttle_coeff = (float)CLIP(gc->throttle, 0.0, 1.0);
collective = 1.0f;
break;
case SAR_FLIGHT_MODEL_HELICOPTER:
throttle_coeff = 1.0f;
if(autopilot_state == SAR_AUTOPILOT_ON)
collective = (float)CLIP(aircraft->collective, 0.0, 1.0);
else
collective = (float)CLIP(gc->throttle, 0.0, 1.0);
throttle_output = (float)CLIP(throttle_coeff -
aircraft->collective_range + (
collective * aircraft->collective_range),
0.0, 1.0
);
break;
case SAR_FLIGHT_MODEL_SLEW:
break;
}
}
}
else if(air_worthy_state == SAR_AIR_WORTHY_OUT_OF_CONTROL)
{
/* In flight, but out of control */
elevator_trim_effective = (float)CLIP(
aircraft->elevator_trim, -1.0, 1.0
);
switch(flight_model_type)
{
case SAR_FLIGHT_MODEL_AIRPLANE:
/* Aircraft, roll when out of control */
h_con_coeff = (float)CLIP(gc->heading, -0.1, 0.1);
p_con_coeff = (float)CLIP(gc->pitch, -0.1, 0.1);
b_con_coeff = (float)CLIP(gc->bank + 1.1, -1.0, 1.0);
break;
case SAR_FLIGHT_MODEL_HELICOPTER:
/* Helicopter, spin when out of control */
h_con_coeff = (float)CLIP(gc->heading + 1.1, -1.0, 1.0);
p_con_coeff = (float)CLIP(gc->pitch, -0.1, 0.1);
b_con_coeff = (float)CLIP(gc->bank, -0.1, 0.1);
break;
case SAR_FLIGHT_MODEL_SLEW:
break;
}
/* Check if engines are on, if so give it less power
* (75% power)
*/
if(engine_state == SAR_ENGINE_ON)
{
switch(flight_model_type)
{
case SAR_FLIGHT_MODEL_AIRPLANE:
throttle_output = throttle_coeff = (float)CLIP(gc->throttle * 0.75, 0.0, 1.0);
collective = 1.0f;
break;
case SAR_FLIGHT_MODEL_HELICOPTER:
throttle_coeff = 0.75f; /* 75% power */
if(autopilot_state == SAR_AUTOPILOT_ON)
collective = (float)CLIP(aircraft->collective, 0.0, 1.0);
else
collective = (float)CLIP(gc->throttle, 0.0, 1.0);
throttle_output = (float)CLIP(throttle_coeff -
aircraft->collective_range + (
collective * aircraft->collective_range),
0.0, 1.0
);
break;
case SAR_FLIGHT_MODEL_SLEW:
break;
}
}
}
else
{
/* In flight and not flyable */
}
}
/* At this point the new control position values have
* been fetched
*/
/* Apply autopilot (if on) to the newly fetched control
* position values, modifying them as needed
*/
SARSimApplyGCTLAutoPilot(
core_ptr, obj_ptr, gc,
&h_con_coeff, &p_con_coeff, &b_con_coeff,
&elevator_trim, /* Pass not effective elevator trim */
&throttle_coeff, &collective
);
/* Begin updating the actual throttle value from the newly
* calculated one, check if there is a change in the
* throttle value from the one calculated above
*/
if(aircraft->throttle != throttle_coeff)
{
/* Calculate maximum allowed change in throttle on this
* cycle, this is to simulate slow throttle
* responsiveness
*/
/* Should get throttle change coeff from object. But for now we use
* constants dependant on the global opt->flight_physics_level
*/
float throttle_dc = 0.0f;
switch(opt->flight_physics_level)
{
case FLIGHT_PHYSICS_REALISTIC:
throttle_dc = (float)(
0.035 * time_compensation *
time_compression
);
break;
case FLIGHT_PHYSICS_MODERATE:
throttle_dc = (float)(
0.06 * time_compensation *
time_compression
);
break;
case FLIGHT_PHYSICS_EASY:
throttle_dc = (float)(
0.1 * time_compensation *
time_compression
);
break;
}
/* Decrease in throttle? */
if(aircraft->throttle > throttle_coeff)
{
aircraft->throttle = (float)MAX(MAX(
throttle_coeff,
aircraft->throttle - throttle_dc
), 0.0);
}
/* Increase in throttle? */
else if(aircraft->throttle < throttle_coeff)
{
aircraft->throttle = (float)MIN(MIN(
throttle_coeff,
aircraft->throttle + throttle_dc
), 1.0);
}
}
/* Begin updating new control positions on aircraft */
/* Displayed attitude control positions */
aircraft->control_heading = gc->heading;
aircraft->control_pitch = gc->pitch;
aircraft->control_bank = gc->bank;
/* Effective attitude control positions */
aircraft->control_effective_heading = (float)CLIP(
h_con_coeff, -1.0, 1.0
);
aircraft->control_effective_pitch = (float)CLIP(
p_con_coeff, -1.0, 1.0
);
aircraft->control_effective_bank = (float)CLIP(
b_con_coeff, -1.0, 1.0
);
/* Collective */
aircraft->collective = collective;
/* If air brakes exist then turn air brakes on/off */
if(aircraft->air_brakes_state > -1)
SARSimOpAirBrakes(
scene, &core_ptr->object, &core_ptr->total_objects,
obj_ptr,
(gc->air_brakes_state) ? 1 : 0,
core_ptr->recorder, opt->event_sounds,
SAR_IS_EAR_IN_COCKPIT(scene)
);
/* Wheel brakes on/off */
if(aircraft->wheel_brakes_state > -1)
{
if(gc->wheel_brakes_state == 2)
aircraft->wheel_brakes_state = 2;
else if(gc->wheel_brakes_coeff > 0.0f)
aircraft->wheel_brakes_state = 1;
else
aircraft->wheel_brakes_state = 0;
}
/* Update SAR FDM control positions */
if(fdm != NULL)
{
fdm->heading_control_coeff = h_con_coeff;
fdm->pitch_control_coeff = p_con_coeff;
fdm->bank_control_coeff = b_con_coeff;
fdm->elevator_trim_coeff = elevator_trim_effective;
fdm->throttle_coeff = SARSimThrottleOutputCoeff(
flight_model_type, /* Flight model never slew */
aircraft->throttle,
aircraft->collective,
aircraft->collective_range
);
fdm->gear_brakes_state = (aircraft->wheel_brakes_state > 0) ?
True : False;
fdm->air_brakes_state = (aircraft->air_brakes_state > 0) ?
True : False;
}
/* Update rotor control positions, this is to update the
* pitch and bank angles of the rotor according to the
* game controller positions
*/
for(rotor_num = 0; rotor_num < aircraft->total_rotors; rotor_num++)
{
rotor_ptr = aircraft->rotor[rotor_num];
if(rotor_ptr == NULL)
continue;
/* Report values from gc since *_con_coeff reflects those
* dependant on engine state and other physics values
*/
SARSimRotorUpdateControls(
rotor_ptr,
(float)CLIP(gc->pitch, -1.0, 1.0),
(float)CLIP(gc->bank, -1.0, 1.0)
);
}
break;
case SAR_OBJ_TYPE_GROUND:
case SAR_OBJ_TYPE_RUNWAY:
case SAR_OBJ_TYPE_HELIPAD:
case SAR_OBJ_TYPE_HUMAN:
case SAR_OBJ_TYPE_SMOKE:
case SAR_OBJ_TYPE_FIRE:
case SAR_OBJ_TYPE_EXPLOSION:
case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
case SAR_OBJ_TYPE_FUELTANK:
case SAR_OBJ_TYPE_PREMODELED:
break;
}
/* Operate hoist */
if(hoist != NULL)
{
float rope_rate = hoist->rope_rate,
hoist_up_coeff = (float)((gc->ctrl_state) ?
gc->zoom_in_coeff : gc->hoist_up_coeff
),
hoist_down_coeff = (float)((gc->ctrl_state) ?
gc->zoom_out_coeff : gc->hoist_down_coeff
);
/* Rope movable? */
if(rope_rate > 0.0f)
{
/* Hoist up? */
if(hoist_up_coeff > 0.0f)
{
float prev_rope = hoist->rope_cur;
/* Pull in/up */
hoist->rope_cur -= (rope_rate * time_compensation *
time_compression * hoist_up_coeff);
/* Do not update rope_cur_vis, it will be updated
* in the other simulation calls
*/
/* Rope hoisted in? */
if(hoist->rope_cur < hoist->contact_z_max)
{
hoist->rope_cur = 0.0f;
hoist->rope_cur_vis = 0.0f;
hoist->on_ground = 0;
/* Was rope previously out? */
if(prev_rope > hoist->contact_z_max)
{
/* Rope has just been hoisted in, reset some
* hoist values
*/
hoist->occupants_mass = 0.0f;
/* Call hoist in procedure to remove the
* occupants from the hoist and process them
*/
SARSimDoHoistIn(core_ptr, obj_ptr);
/* Door exists and is currently opened? */
if((door_ptr != NULL) ?
(door_ptr->flags & SAR_OBJ_PART_FLAG_STATE) : 0
)
{
/* Door is opened, try to close it? */
if(!(door_ptr->flags & SAR_OBJ_PART_FLAG_DOOR_STAY_OPEN))
SARSimOpDoorRescue(
scene,
&core_ptr->object, &core_ptr->total_objects,
obj_ptr,
0 /* Close */
);
}
} /* Was rope previously out? */
}
}
/* Hoist down? */
else if(hoist_down_coeff > 0.0f)
{
/* Open rescue door as needed */
/* TODO Check if rescue door needs to be opened */
if((door_ptr != NULL) ?
!(door_ptr->flags & SAR_OBJ_PART_FLAG_STATE) : 0
)
{
/* Rescue door exists but is closed, so we need
* to open it
*/
SARSimOpDoorRescue(
scene,
&core_ptr->object, &core_ptr->total_objects,
obj_ptr,
1 /* Open */
);
/* Here is also a good point to check if we have
* enough room for additional passengers
*/
if((aircraft != NULL) ?
(aircraft->passengers >=
aircraft->passengers_max) : 0
)
{
/* Is this the player object? */
if(scene->player_obj_ptr == obj_ptr)
SARMessageAdd(
scene,
SAR_MESG_NO_ROOM_LEFT_FOR_PASSENGERS
);
}
}
/* Was rope previously in? */
if(hoist->rope_cur < hoist->contact_z_max)
{
/* Hoist deployment was in so now it needs to
* be put out and its values initialized
*/
sar_position_struct *hoist_pos = &hoist->pos,
*hoist_offset = &hoist->offset;
double tx = hoist_offset->x,
ty = hoist_offset->y,
tz = hoist_offset->z;
/* Move to initial position at hoist center */
SFMOrthoRotate2D(
obj_ptr->dir.heading, &tx, &ty
);
hoist_pos->x = (float)(
obj_ptr->pos.x + tx
);
hoist_pos->y = (float)(
obj_ptr->pos.y + ty
);
hoist_pos->z = (float)(
obj_ptr->pos.z + tz -
hoist->rope_cur
);
/* Reset hoist values */
hoist->rope_cur = hoist->contact_z_max;
hoist->rope_cur_vis = hoist->rope_cur;
hoist->on_ground = 0;
}
else
{
/* Lower rope normally */
hoist->rope_cur += (rope_rate *
time_compensation * time_compression *
hoist_down_coeff);
if(hoist->rope_cur > hoist->rope_max)
hoist->rope_cur = hoist->rope_max;
/* Do not update rope_cur_vis, it will be updated
* in the other simulation calls
*/
}
}
}
}
/* Begin updating camera distance/direction/position */
/* Updates the spot light on the aircraft if the ctrl key is
* down and the hat is moved
*/
#define ROTATE_SPOTLIGHT_BY_HAT(_hat_x_,_hat_y_) { \
\
/* Rotate spotlight heading */ \
if(((_hat_x_) != 0.0f) && (aircraft != NULL)) { \
sar_direction_struct *dir = &aircraft->spotlight_dir; \
/* Calculate rate (rad/sec) based on the pitch */ \
const float rate = (float)(0.6 * PI * \
((dir->pitch + (0.05 * PI)) / (0.55 * PI)) \
); \
/* Rotate heading */ \
dir->heading = (float)SFMSanitizeRadians( \
dir->heading + \
((_hat_x_) * rate * time_compensation) \
); \
} \
\
/* Rotate spotlight pitch */ \
if(((_hat_y_) != 0.0f) && (aircraft != NULL)) { \
sar_direction_struct *dir = &aircraft->spotlight_dir; \
/* Calculate rate (rad/sec) based on the pitch */ \
const float rate = (float)(0.3 * PI * \
((dir->pitch + (0.05 * PI)) / (0.55 * PI)) \
); \
/* Rotate pitch */ \
dir->pitch = (float)( \
dir->pitch + \
(-(_hat_y_) * rate * time_compensation) \
); \
if(dir->pitch <= (0.02 * PI)) \
dir->pitch = (float)(0.02 * PI); \
else if(dir->pitch > (0.5 * PI)) \
dir->pitch = (float)(0.5 * PI); \
} \
}
/* Update camera based on current camera reference */
switch(scene->camera_ref)
{
case SAR_CAMERA_REF_HOIST:
dir = &scene->camera_hoist_dir;
/* Use hat position to rotate spot light? */
if(gc->ctrl_state)
{
ROTATE_SPOTLIGHT_BY_HAT(gc->hat_x, gc->hat_y);
}
else
{
/* Hat moves camera around target (basket or aircraft)
* as if camera was set to SAR_CAMERA_REF_SPOT
*/
if(gc->hat_x != 0.0f)
{
dir->heading = (float)SFMSanitizeRadians(
dir->heading - (gc->hat_x * camera_turn_rate *
time_compensation) /* No time acceleration */
);
}
if(gc->hat_y != 0.0f)
{
float prev_pitch = dir->pitch;
dir->pitch = (float)SFMSanitizeRadians(
dir->pitch - (gc->hat_y * camera_turn_rate *
time_compensation) /* No time acceleration */
);
/* Clip pitch */
if((dir->pitch < (float)(1.51 * PI)) &&
(dir->pitch > (float)(0.49 * PI))
)
dir->pitch = (float)((prev_pitch < (float)(1.0 * PI)) ?
(0.49 * PI) : (1.51 * PI)
);
}
}
/* Zoom in and out */
if(!gc->ctrl_state)
{
if(gc->zoom_in_coeff > 0.0f)
{
scene->camera_hoist_dist -= (float)(
((gc->shift_state) ? 50.0 : 10.0) *
time_compensation * SAR_SEC_TO_CYCLE_COEFF *
gc->zoom_in_coeff
);
/* Do not zoom closer than contact radius of hoist */
if(scene->camera_hoist_dist < contact_radius)
scene->camera_hoist_dist = contact_radius;
}
if(gc->zoom_out_coeff > 0.0f)
{
scene->camera_hoist_dist += (float)(
((gc->shift_state) ? 50.0 : 10.0) *
time_compensation * SAR_SEC_TO_CYCLE_COEFF *
gc->zoom_out_coeff
);
}
}
break;
case SAR_CAMERA_REF_MAP:
pos = &scene->camera_map_pos;
/* Use hat position to rotate spot light? */
if(gc->ctrl_state)
{
ROTATE_SPOTLIGHT_BY_HAT(gc->hat_x, gc->hat_y);
}
else
{
/* Hat moves map location */
if(gc->hat_x != 0.0f)
pos->x += (float)(gc->hat_x *
(gc->shift_state ? 32500.0 : 5000.0)
* time_compensation
);
if(gc->hat_y != 0.0f)
pos->y += (float)(gc->hat_y *
(gc->shift_state ? 32500.0 : 5000.0)
* time_compensation
);
}
/* Zoom map in and out */
if(!gc->ctrl_state)
{
if(gc->zoom_in_coeff > 0.0f)
{
pos->z -= (float)(
(gc->shift_state ? 30000.0 : 1200.0) *
time_compensation * SAR_SEC_TO_CYCLE_COEFF *
gc->zoom_in_coeff
);
/* Stay atleast 10 meters above ground */
if(pos->z < 10.0f)
pos->z = 10.0f;
}
if(gc->zoom_out_coeff > 0.0f)
{
pos->z += (float)(
(gc->shift_state ? 30000.0 : 1200.0) *
time_compensation * SAR_SEC_TO_CYCLE_COEFF *
gc->zoom_out_coeff
);
/* Keep below `map ceiling' */
if(pos->z > (float)SFMFeetToMeters(SAR_MAX_MAP_VIEW_HEIGHT))
pos->z = (float)SFMFeetToMeters(SAR_MAX_MAP_VIEW_HEIGHT);
}
}
break;
case SAR_CAMERA_REF_TOWER:
pos = &scene->camera_tower_pos;
/* Use hat position to rotate spot light? */
if(gc->ctrl_state)
{
ROTATE_SPOTLIGHT_BY_HAT(gc->hat_x, gc->hat_y);
}
else
{
/* Hat x orbits camera position around camera_target
* object.
*/
if(gc->hat_x != 0.0)
{
sar_object_struct *tar_obj_ptr = SARObjGetPtr(
core_ptr->object, core_ptr->total_objects,
scene->camera_target
);
if(tar_obj_ptr != NULL)
{
sar_position_struct *tar_pos = &tar_obj_ptr->pos;
double a[3], r[3];
/* Calculate deltas from target to camera */
a[0] = pos->x - tar_pos->x;
a[1] = pos->y - tar_pos->y;
a[2] = 0.0;
/* Rotate delta, thus orbiting the tower offset */
MatrixRotateHeading3(
a,
-gc->hat_x * camera_turn_rate * time_compensation,
r
);
/* Set new tower position based on offset from target */
pos->x = (float)(tar_pos->x + r[0]);
pos->y = (float)(tar_pos->y + r[1]);
}
}
/* Hat y moves camera up and down */
if(gc->hat_y != 0.0)
{
sar_object_struct *tar_obj_ptr = SARObjGetPtr(
core_ptr->object, core_ptr->total_objects,
scene->camera_target
);
if(tar_obj_ptr != NULL)
{
sar_position_struct *tar_pos = &tar_obj_ptr->pos;
float distance, ddist;
/* Calculate distance from target to camera */
distance = (float)SAR_ABSOLUTE(pos->z - tar_pos->z);
/* Interprite distance as always 5 meters or more */
if(distance < 5.0)
distance = 5.0;
/* Calculate distance change */
ddist = (float)(
distance *
((gc->shift_state) ? 2.5 : 0.9) *
gc->hat_y *
time_compensation * SAR_SEC_TO_CYCLE_COEFF
);
/* Set new camera position */
pos->z += ddist;
if(pos->z < 0.0f)
pos->z = 0.0f;
}
}
}
/* Move closer or away when ctrl key is not held */
if(!gc->ctrl_state)
{
if((gc->zoom_in_coeff > 0.0f) || (gc->zoom_out_coeff > 0.0f))
{
sar_object_struct *tar_obj_ptr = SARObjGetPtr(
core_ptr->object, core_ptr->total_objects,
scene->camera_target
);
if(tar_obj_ptr != NULL)
{
sar_position_struct *tar_pos = &tar_obj_ptr->pos;
float dx, dy, distance_xy, ddist, dcoeff;
float zoom_coeff = gc->zoom_out_coeff -
gc->zoom_in_coeff;
/* Calculate deltas from camera to target */
dx = tar_pos->x - pos->x;
dy = tar_pos->y - pos->y;
/* Calculate xy distance between camera and target */
distance_xy = (float)SFMHypot2(dx, dy);
/* Do this only if the current distance away is
* greater than a meter.
*/
if(distance_xy > 1.0f)
{
/* Calculate delta distance change based on *1.5
* (or *3.0 if shift is held) of current xy
* distance relative to 1 second.
*/
ddist = (float)(
distance_xy *
((gc->shift_state) ? 2.5 : 0.9) *
zoom_coeff *
time_compensation * SAR_SEC_TO_CYCLE_COEFF
);
/* Calculate coefficient change from old distance to
* new distance (note we know distance_xy is > 1.0).
*/
dcoeff = (ddist + distance_xy) / distance_xy;
/* Use similar triangles to calculate new tower
* camera position.
*/
pos->x = tar_pos->x - (dx * dcoeff);
pos->y = tar_pos->y - (dy * dcoeff);
}
}
}
}
break;
case SAR_CAMERA_REF_SPOT:
dir = &scene->camera_spot_dir;
/* Use hat position to rotate spot light? */
if(gc->ctrl_state)
{
ROTATE_SPOTLIGHT_BY_HAT(gc->hat_x, gc->hat_y);
}
else
{
/* Hat moves camera around target in spot reference */
if(gc->hat_x != 0.0f)
dir->heading = (float)SFMSanitizeRadians(
dir->heading - (gc->hat_x * camera_turn_rate *
time_compensation) /* No time acceleration */
);
if(gc->hat_y != 0.0f)
{
float prev_pitch = dir->pitch;
dir->pitch = (float)SFMSanitizeRadians(
dir->pitch - (gc->hat_y * camera_turn_rate *
time_compensation) /* No time acceleration */
);
/* Clip pitch */
if((dir->pitch < (float)(1.51 * PI)) &&
(dir->pitch > (float)(0.49 * PI))
)
dir->pitch = (float)((prev_pitch < (float)(1.0 * PI)) ?
(0.49 * PI) : (1.51 * PI)
);
}
/*
dir->bank = 0.0f;
*/
}
/* Zoom in and out when ctrl key is not held */
if(!gc->ctrl_state)
{
if(gc->zoom_in_coeff > 0.0f)
{
float ddist = (float)(scene->camera_spot_dist *
((gc->shift_state) ? 2.0 : 0.8) *
-gc->zoom_in_coeff * time_compensation *
SAR_SEC_TO_CYCLE_COEFF
);
scene->camera_spot_dist += ddist;
if(scene->camera_spot_dist < contact_radius)
scene->camera_spot_dist = contact_radius;
}
if(gc->zoom_out_coeff > 0.0)
{
float ddist = (float)(scene->camera_spot_dist *
((gc->shift_state) ? 2.0 : 0.8) *
gc->zoom_out_coeff * time_compensation *
SAR_SEC_TO_CYCLE_COEFF
);
scene->camera_spot_dist += ddist;
}
}
break;
default: /* Default to cockpit */
dir = &scene->camera_cockpit_dir;
/* Use hat position to rotate spot light? */
if(gc->ctrl_state)
{
ROTATE_SPOTLIGHT_BY_HAT(gc->hat_x, gc->hat_y);
}
else
{
/* If shift key is held, then rotate camera instantly
* to right angle directions. Otherwise rotate normally.
*/
if(gc->shift_state)
{
if(gc->hat_x > 0.5)
dir->heading = (float)(0.5 * PI);
else if(gc->hat_x < -0.5)
dir->heading = (float)(1.5 * PI);
if(gc->hat_y > 0.5)
dir->heading = (float)(0.0 * PI);
else if(gc->hat_y < -0.5)
dir->heading = (float)(1.0 * PI);
dir->pitch = (float)(0.0 * PI);
}
else
{
if(gc->hat_x != 0.0f)
{
dir->heading = (float)SFMSanitizeRadians(
dir->heading + (gc->hat_x * camera_turn_rate *
time_compensation)
);
}
if(gc->hat_y != 0.0f)
{
float prev_pitch = dir->pitch;
dir->pitch = (float)SFMSanitizeRadians(
dir->pitch - (gc->hat_y * camera_turn_rate *
time_compensation)
);
/* Clip pitch */
if((dir->pitch < (float)(1.51 * PI)) &&
(dir->pitch > (float)(0.49 * PI))
)
dir->pitch = (float)((prev_pitch < (float)(1.0 * PI)) ?
(0.49 * PI) : (1.51 * PI)
);
}
/* dir->bank = 0.0f; Camera bank is unaffected */
}
}
break;
}
#undef ROTATE_SPOTLIGHT_BY_HAT
}
|