File: simutils.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (2927 lines) | stat: -rw-r--r-- 72,085 bytes parent folder | download | duplicates (3)
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
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "matrixmath.h"
#include "sfm.h"

#include "sarreality.h"
#include "obj.h"
#include "objutils.h"
#include "objsound.h"
#include "smoke.h"
#include "explosion.h"
#include "messages.h" 
#include "simmanage.h"
#include "simutils.h"
#include "sartime.h"
#include "sar.h"
#include "config.h"


float SARSimGetFlatContactRadius(sar_object_struct *obj_ptr);
sar_object_struct *SARSimMatchObjectFromFDM(
	sar_object_struct **list, int total,
	SFMModelStruct *fdm, int *index
);
float SARSimThrottleOutputCoeff(
	int flight_model_type,
	float throttle,
	float collective, float collective_range
);
float SARSimStallSpeed(
	const sar_object_aircraft_struct *obj_aircraft_ptr
);

void SARSimWarpObjectRelative(
	sar_scene_struct *scene, sar_object_struct *obj_ptr,
	sar_object_struct **ptr, int total, int ref_obj_num,
	sar_position_struct *offset_pos,
	sar_direction_struct *offset_dir
);	
void SARSimWarpObject(
	sar_scene_struct *scene, sar_object_struct *obj_ptr,
	sar_position_struct *new_pos,
	sar_direction_struct *new_dir
);

void SARSimUpdatePart(
	sar_scene_struct *scene,
	sar_object_struct *obj_ptr, sar_obj_part_struct *part_ptr,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit
);
void SARSimOpAirBrakes(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int air_brakes_state,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit

);
void SARSimOpLandingGear(  
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int gear_state,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit
);
void SARSimOpDoorRescue(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr,
	int state	/* 0 = close, 1 = open */
);

void SARSimRotorUpdateControls(
	sar_obj_rotor_struct *rotor_ptr,
	float pitch, float bank
);

sar_engine_state SARSimGetEngineState(sar_object_struct *obj_ptr);
void SARSimOpEngine(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, sar_engine_state engine_state,
	snd_recorder_struct *recorder, char play_sound, char ear_in_cockpit
);
void SARSimPitchEngine(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int pitch_state
);

void SARSimUpdateLights(sar_object_struct *obj_ptr);
int SARSimGetLightsState(sar_object_struct *obj_ptr);
void SARSimOpLights(
	sar_object_struct *obj_ptr, int state
);
int SARSimGetStrobesState(sar_object_struct *obj_ptr);
void SARSimOpStrobes(
	sar_object_struct *obj_ptr, int state
);
int SARSimGetAttenuateState(sar_object_struct *obj_ptr);
void SARSimOpAttenuate(
	sar_object_struct *obj_ptr, int state
);

int SARSimOpRepair(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
);
int SARSimOpRefuel(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
);

int SARSimOpPassengersSetLeave(
	sar_scene_struct *scene, sar_object_struct *obj_ptr,
	int passengers_leave_pending, int passengers_drop_pending
);
int SARSimOpPassengersUnloadAll(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
);

float SARSimOpTransferFuelFromTanks(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
);

int SARSimOpDropFuelTankNext(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num, sar_object_struct *obj_ptr
);

void SARSimSmokeSpawn(
	sar_core_struct *core_ptr, sar_object_struct *obj_ptr,
	sar_object_smoke_struct *obj_smoke_ptr
);

int SARSimIsSlew(sar_object_struct *obj_ptr);
void SARSimSetSlew(
	sar_object_struct *obj_ptr, int enter_slew
);
int SARSimDoPickUpHuman(
	sar_scene_struct *scene,
	sar_object_struct *obj_ptr,
	sar_object_human_struct *obj_human_ptr,
	int human_obj_num
);
int SARSimDoHoistIn(
	sar_core_struct *core_ptr, sar_object_struct *obj_ptr
);
int SARSimBoardObject(
	sar_core_struct *core_ptr,
	sar_object_struct *tar_obj_ptr, int src_obj_num
);
void SARSimSetFlyByPosition(
	sar_scene_struct *scene,
	sar_object_struct ***ptr,
	int *total,
	sar_object_struct *obj_ptr,
	sar_position_struct *pos_result
);


#define SQRT(x)		(((x) > 0.0f) ? sqrt(x) : 0.0f)

#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#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)


/*
 *      Returns the flat (XY plane) contact bounds radius for
 *      the given object.
 *
 *      Can return 0.0 if contact bounds are not availbale for
 *      the object.
 */
float SARSimGetFlatContactRadius(sar_object_struct *obj_ptr)
{
	float contact_radius = 0.0f;
	const sar_contact_bounds_struct *cb = (obj_ptr != NULL) ?
	    obj_ptr->contact_bounds : NULL;
	if(cb == NULL)
	    return(contact_radius);

	switch(cb->contact_shape)
	{
	  case SAR_CONTACT_SHAPE_SPHERICAL:
	  case SAR_CONTACT_SHAPE_CYLENDRICAL:
	    contact_radius = cb->contact_radius;
	    break;

	  case SAR_CONTACT_SHAPE_RECTANGULAR:
	    contact_radius = MAX(
		cb->contact_x_max - cb->contact_x_min,
		cb->contact_y_max - cb->contact_y_min
	    );
	    break;
	}

	return(MAX(contact_radius, 0.0f));
}

/*
 *	Returns the pointer to the object in the give nobject list that
 *	contains an FDM that matches the given fdm or NULL on error.
 *
 *      If index is not NULL then the index to the object number on
 *      the objects list will be set or -1 on failed match.
 */
sar_object_struct *SARSimMatchObjectFromFDM(
	sar_object_struct **list, int total,
	SFMModelStruct *fdm, int *index
)
{
	int i;
	sar_object_struct *obj_ptr;
	sar_object_aircraft_struct *obj_aircraft_ptr;

	if(index != NULL)
	    *index = -1;

	if((list == NULL) || (fdm == NULL))
	    return(NULL);

	/* Iterate through all objects */  
	for(i = 0; i < total; i++)
	{
	    obj_ptr = list[i]; 
	    if(obj_ptr == NULL)
		continue;

	    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:
		obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
		if(obj_aircraft_ptr != NULL)
		{
		    if(obj_aircraft_ptr->fdm == fdm)
		    {
			if(index != NULL)
			    *index = i;
			return(obj_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;
	    }
	}

	return(NULL);
}


/*
 *	Returns the actual throttle output based on the given
 *	values.
 */
float SARSimThrottleOutputCoeff(
	int flight_model_type,
	float throttle,
	float collective, float collective_range
)
{
	switch(flight_model_type)
	{
	  case SAR_FLIGHT_MODEL_AIRPLANE:
	    return(throttle);
	    break;

	  case SAR_FLIGHT_MODEL_HELICOPTER:
	    if(collective_range >= 0.0f)
		return(CLIP(
		    (throttle + (throttle * collective * collective_range))
			/ (1.0f + collective_range),
		    0.0f, 1.0f
		));
	    else
		return(throttle);
	    break;

	  default:	/* Slew, return throttle as given */
	    return(throttle);
	    break;
	}
}

/*
 *	Returns the speed_stall in meters per cycle on the given
 *	aircraft, applying any modifications by flaps.
 */
float SARSimStallSpeed(
	const sar_object_aircraft_struct *obj_aircraft_ptr
)
{
	if(obj_aircraft_ptr == NULL)
	    return(0.0f);

	/* Flaps fully retracted? */
	if(obj_aircraft_ptr->flaps_position <= 0.0f)
	{
	    return(obj_aircraft_ptr->speed_stall);
	}
	else
	{
	    /* Flaps are deployed, so reduce the stall speed by the
	     * amount the flaps are deployed times the effective
	     * stall speed modification of the flaps.
	     */
	    return(MAX(
		obj_aircraft_ptr->speed_stall - (
		    obj_aircraft_ptr->flaps_position *
		    obj_aircraft_ptr->flaps_stall_offset
		), 0.0f
	    ));
	}
}


/*
 *	Moves the given object obj_ptr relative to the object ref_obj_num
 *	if it is valid with respect to the given offset_pos and offset_dir
 *	(both relative to ref_obj_num).
 *
 *	If ref_obj_num is invalid then no operation will be performed.
 *
 *	This function automatically calls SARSimWarpObject() afterwards.
 */
void SARSimWarpObjectRelative(
	sar_scene_struct *scene, sar_object_struct *obj_ptr,
	sar_object_struct **ptr, int total, int ref_obj_num,
	sar_position_struct *offset_pos,
	sar_direction_struct *offset_dir
)
{
	double a[3 * 1], r[3 * 1];
	sar_position_struct *pos, *ref_pos;
	sar_direction_struct *dir, *ref_dir;
	sar_object_struct *ref_obj_ptr;

	if((scene == NULL) || (obj_ptr == NULL))
	    return;

	/* Check if reference object is valid */
	if((ref_obj_num >= 0) && (ref_obj_num < total))
	    ref_obj_ptr = ptr[ref_obj_num];
	else
	    ref_obj_ptr = NULL;

	/* Got a reference object? */
	if(ref_obj_ptr == NULL)
	    return;

	/* Get pointers to position of reference object */
	ref_pos = &ref_obj_ptr->pos;
	ref_dir = &ref_obj_ptr->dir;

	/* Set up and transform offset matrix a */
	if(offset_pos != NULL)
	{
	    a[0] = offset_pos->x;
	    a[1] = offset_pos->y;
	    a[2] = offset_pos->z;
	}
	else
	{
	    a[0] = 0.0;
	    a[1] = 0.0;
	    a[2] = 0.0;
	}

	/* Rotate the offset matrix a to get rotated offset matrix 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);

	/* Move the specified object to the new offset position relative
	 * to the reference object
	 */
	pos = &obj_ptr->pos;
	pos->x = (float)(ref_pos->x + r[0]);
	pos->y = (float)(ref_pos->y + r[1]);
	pos->z = (float)(ref_pos->z + r[2]);

	/* Rotate the given object by the relative object's direction
	 * added with the offset direction
	 */
	dir = &obj_ptr->dir;
	if(offset_dir != NULL)
	{
	    dir->heading = (float)SFMSanitizeRadians(
		ref_dir->heading + offset_dir->heading
	    );
	    dir->pitch = (float)SFMSanitizeRadians( 
		ref_dir->pitch + offset_dir->pitch
	    );
	    dir->bank = (float)SFMSanitizeRadians(
		ref_dir->bank + offset_dir->bank
	    );
	}
	else
	{
	    memcpy(dir, ref_dir, sizeof(sar_direction_struct));
	}

	/* Realize new position of the given object */
	SARSimWarpObject(scene, obj_ptr, pos, dir);
}



/*
 *	Updates (realizes) all position and attitude values for the
 *	given object to the specified new position and direction.
 *
 *	If any input is NULL then the values will be left unchanged.
 *
 *	If the given input pointers match the pointers of the structures
 *	on the object then the structure in question will be left
 *	unchanged.
 *
 *	Rotatation values for the contact bounds and ground object
 *	structure will be updated as needed.
 */
void SARSimWarpObject(
	sar_scene_struct *scene, sar_object_struct *obj_ptr,  
	sar_position_struct *new_pos,
	sar_direction_struct *new_dir
)
{
	sar_object_aircraft_struct *obj_aircraft_ptr = NULL;
	sar_object_ground_struct *obj_ground_ptr = NULL;
	sar_contact_bounds_struct *cb;
	SFMModelStruct *fdm = NULL;


	if((scene == NULL) || (obj_ptr == NULL))
	    return;

	if((new_pos != NULL) && (new_pos != &obj_ptr->pos))
	{
	    obj_ptr->pos.x = new_pos->x;
	    obj_ptr->pos.y = new_pos->y;
	    obj_ptr->pos.z = new_pos->z;
	}
	if((new_dir != NULL) && (new_dir != &obj_ptr->dir))
	{
	    obj_ptr->dir.heading = new_dir->heading;
	    obj_ptr->dir.pitch = new_dir->pitch;
	    obj_ptr->dir.bank = new_dir->bank;
	}

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		/* Get pointer to SFM flight dynamics structure */
		fdm = obj_aircraft_ptr->fdm;
	    }
	    break;
	  case SAR_OBJ_TYPE_GROUND:
	    obj_ground_ptr = SAR_OBJ_GET_GROUND(obj_ptr);
	    if(obj_ground_ptr != NULL)
	    {
		/* Update trig functions for ground's heightfield
		 * rotation
		 */
		if(new_dir != NULL)
		{
		    obj_ground_ptr->cos_heading = (float)cos(-new_dir->heading);
		    obj_ground_ptr->sin_heading = (float)sin(-new_dir->heading);
		}
	    }
	    break;
	  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;
	}

	/* Set values on FDM if exists */
	if(fdm != NULL)
	{
	    if(new_pos != NULL)
	    {
		fdm->position.x = new_pos->x;
		fdm->position.y = new_pos->y;
		fdm->position.z = new_pos->z;
	    }
	    if(new_dir != NULL)
	    {
		fdm->direction.heading = new_dir->heading;
		fdm->direction.pitch = new_dir->pitch;
		fdm->direction.bank = new_dir->bank;
	    }
	}

	/* Update values on contact bounds */
	cb = obj_ptr->contact_bounds;
	if(cb != NULL)
	{
	    /* Update values based on contact shape */
	    if(cb->contact_shape == SAR_CONTACT_SHAPE_RECTANGULAR)
	    {
		/* For rectangular contact bounds, the cos and sin
		 * values for the heading need to be updated based
		 * on the new rotation.
		 */
		if(new_dir != NULL)
		{
		    cb->cos_heading = (float)cos(-new_dir->heading);
		    cb->sin_heading = (float)sin(-new_dir->heading);
		}
	    }

	}
}

/*
 *	Updates the animated position of the given object part
 *	structure.  This does not update parts that respond to control
 *	position such as ailerons, rudders, and elevators.
 */
void SARSimUpdatePart(
	sar_scene_struct *scene,
	sar_object_struct *obj_ptr, sar_obj_part_struct *part_ptr,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit
)
{
	int type;
	sar_obj_flags_t flags;


	if((scene == NULL) || (obj_ptr == NULL) || (part_ptr == NULL))
	    return;

	type = part_ptr->type;
	flags = part_ptr->flags;

	/* Handle by object part type */
	switch(type)
	{
	  case SAR_OBJ_PART_TYPE_FLAP:
/* TODO */
	    break;

	  case SAR_OBJ_PART_TYPE_AIR_BRAKE:
	    /* Air brakes deploying/deployed? */
	    if(flags & SAR_OBJ_PART_FLAG_STATE)
	    {
		/* Going to max */
		sar_grad_anim_t prev_pos;

		/* Already deployed? */
		if(part_ptr->anim_pos == (sar_grad_anim_t)-1)
		    break;

		prev_pos = part_ptr->anim_pos;

		/* Increase animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MIN(
		    (int)part_ptr->anim_pos +
		    ((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    (sar_grad_anim_t)-1
		);
	    }
	    /* Air brakes closing/closed? */
	    else
	    {
		/* Going to min */
		sar_grad_anim_t prev_pos;

		/* Already closed? */
		if(part_ptr->anim_pos == 0)
		    break;

		prev_pos = part_ptr->anim_pos;

		/* Decrease animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MAX(
		    (int)part_ptr->anim_pos -
			((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    0
		);
	    }
	    break;

	  case SAR_OBJ_PART_TYPE_DOOR:
	  case SAR_OBJ_PART_TYPE_DOOR_RESCUE:
	  case SAR_OBJ_PART_TYPE_CANOPY:
	    /* Door opening? */
	    if(flags & SAR_OBJ_PART_FLAG_STATE)
	    {
		/* Opening */
		sar_grad_anim_t prev_pos;

		/* Already opened? */
		if(part_ptr->anim_pos == (sar_grad_anim_t)-1)
		    break;

		/* Door fixed and cannot be opened? */
		if(flags & SAR_OBJ_PART_FLAG_DOOR_FIXED)
		    break;

		prev_pos = part_ptr->anim_pos;

		/* Increase animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MIN(
		    (int)part_ptr->anim_pos +
		    ((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    (int)((sar_grad_anim_t)-1)
		);

		/* Play sound if just started to move? */
		if(play_sound && (prev_pos == 0))
		    SARSoundSourcePlayFromList(
			recorder,
			obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
			ear_in_cockpit ?
			    "door_open_inside" : "door_open",
			&obj_ptr->pos, &obj_ptr->dir,
			&scene->ear_pos
		    );
	    }
	    else
	    {
		/* Closing */
		sar_grad_anim_t prev_pos;

		/* Door already closed? */
		if(part_ptr->anim_pos == 0)
		    break;

		/* Door fixed and cannot be closed? */
		if(flags & SAR_OBJ_PART_FLAG_DOOR_FIXED)
		    break;

		prev_pos = part_ptr->anim_pos;

		/* Decrease animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MAX(
		    (int)part_ptr->anim_pos -
		    ((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    0
		);

		/* Play sound if just closed? */
		if(play_sound && (part_ptr->anim_pos == 0))
		    SARSoundSourcePlayFromList(
			recorder,
			obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
			ear_in_cockpit ?
			    "door_close_inside" : "door_close",
			&obj_ptr->pos, &obj_ptr->dir,
			&scene->ear_pos
		    );
	    }
	    break;

	  case SAR_OBJ_PART_TYPE_LANDING_GEAR:
	    /* When the state flag is set, it means the landing gear is
	     * deployed. However the deployed position is considered the
	     * minimum position (unlike other object part types).
	     */
	    if(flags & SAR_OBJ_PART_FLAG_STATE)
	    {
		/* Lowering landing gear */

		if(part_ptr->anim_pos == 0)
		    break;

		/* Do not change animation position if landing gear is
		 * fixed or damaged.
		 */
		if((flags & SAR_OBJ_PART_FLAG_LGEAR_FIXED) ||
		   (flags & SAR_OBJ_PART_FLAG_LGEAR_DAMAGED)
		)
		    break;

		/* Decrease animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MAX(
		    (int)part_ptr->anim_pos -
		    ((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    0
		);
	    }
	    else
	    {
		/* Retracting landing gears */

		if(part_ptr->anim_pos == (sar_grad_anim_t)-1)
		    break;

		/* Do not change animation position if landing gear is
		 * fixed or damaged.
		 */
		if((flags & SAR_OBJ_PART_FLAG_LGEAR_FIXED) ||
		   (flags & SAR_OBJ_PART_FLAG_LGEAR_DAMAGED)
		)
		    break;

		/* Increase animation position value */
		part_ptr->anim_pos = (sar_grad_anim_t)MIN(
		    (int)part_ptr->anim_pos +
		    ((int)part_ptr->anim_rate * time_compensation *
			time_compression),
		    (int)((sar_grad_anim_t)-1)
		);
	    }
	    break;

	}
}

/*
 *	Deploys or retracts air brakes on the object specified by
 *	obj_ptr.
 *
 *	This marks all air brake parts on the object to start deploying
 *	or retracting.
 */
void SARSimOpAirBrakes(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int air_brakes_state,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit
)
{
	int i, cur_state = -1, oped = 0;
	sar_obj_part_struct *air_brake;
	sar_object_aircraft_struct *obj_aircraft_ptr;


	if((obj_ptr == NULL) || (air_brakes_state < 0))
	    return;

	/* Check object type specific values to see if operation to the
	 * air brakes can be made
	 */
	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		/* Cannot deploy or retract air brakes if the
		 * aircraft is not flyable
		 */
		if(obj_aircraft_ptr->air_worthy_state == SAR_AIR_WORTHY_NOT_FLYABLE)
		    return;

		/* Already deployed or retracted? */
		if(obj_aircraft_ptr->air_brakes_state == air_brakes_state)
		    return;
		else
		    obj_aircraft_ptr->air_brakes_state = air_brakes_state;
	    }
	    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;
	}

	/* Iterate through all air brakes */
	for(i = 0; 1; i++)
	{
	    air_brake = SARObjGetPartPtr(
		obj_ptr, SAR_OBJ_PART_TYPE_AIR_BRAKE, i
	    );
	    if(air_brake == NULL)
		break;

	    /* Need to get initial state? */
	    if(cur_state == -1)
		cur_state = (air_brake->flags & SAR_OBJ_PART_FLAG_STATE) ?
		    1 : 0;

	    /* Is retracted and we want to deploy it? */
	    if(!cur_state && air_brakes_state)
	    {
		/* Currently retracted so deploy it */
		air_brake->flags |= SAR_OBJ_PART_FLAG_STATE;
		oped = 1;
	    }
	    /* Is deployed and we want to retract it? */
	    else if(cur_state && !air_brakes_state)
	    {
		/* Currently deployed so retract it */
		air_brake->flags &= ~SAR_OBJ_PART_FLAG_STATE;
		oped = 1;
	    }
	}

	/* Play air brakes sound? Note that we play the air brakes
	 * sound here since SARSimUpdatePart() that does the actual
	 * air brakes parts updating will play sounds for multiple
	 * air brakes and we don't want that.
	 */
	if(play_sound && oped)
	{
	    if(air_brakes_state)
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"air_brake_out_inside" : "air_brake_out",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	    else
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"air_brake_in_inside" : "air_brake_in",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	}
}

/*
 *	Raise or lower landing gear on the object specified by obj_ptr.
 *
 *	This marks all landing gear parts on the object to start
 *	lowering or raising.
 */
void SARSimOpLandingGear(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int gear_state,
	snd_recorder_struct *recorder, int play_sound, int ear_in_cockpit
)
{
	int i, cur_state = -1, oped = 0;
	sar_obj_part_struct *gear;
	sar_object_aircraft_struct *obj_aircraft_ptr;


	if((obj_ptr == NULL) || (gear_state < 0))
	    return;

	/* Input state can be 0 or 1, for landing gear state values
	 * have the following meanings:
	 * -1	non-existant
	 * 0	up/retracted/in
	 * 1	down/deployed/out
	 * 2	down/deployed/out and fixed
	 */

	/* Check object type specific values to see if operation to the
	 * landing gears can be made.
	 */
	obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	if(obj_aircraft_ptr != NULL)
	{
	    /* Cannot raise or lower landing gears while landed
	     * or if landing gears are fixed.
	     */
	    if(obj_aircraft_ptr->landed ||
	       (obj_aircraft_ptr->landing_gear_state < 0) ||
	       (obj_aircraft_ptr->landing_gear_state == 2)
	    )
		return;

	    /* No change in landing gear state? */
	    if(gear_state == obj_aircraft_ptr->landing_gear_state)
		return;

	    /* Set new gear state (must be 0 or 1) */
	    switch(gear_state)
	    {
	      case 1:
		obj_aircraft_ptr->landing_gear_state = 1;
		break;
	      default:
		obj_aircraft_ptr->landing_gear_state = 0;
		break;
	    }
	}


	/* Iterate through all landing gears */
	for(i = 0; 1; i++)
	{
	    gear = SARObjGetPartPtr(
		obj_ptr, SAR_OBJ_PART_TYPE_LANDING_GEAR, i
	    );
	    if(gear == NULL)
		break;

	    /* Need to get initial gear state? */
	    if(cur_state == -1)
		cur_state = (gear->flags & SAR_OBJ_PART_FLAG_STATE) ?
		    1 : 0;

	    /* Is gear up and we want to lower it? */
	    if(!cur_state && gear_state)
	    {
		/* Landing gear is currently up and we want to lower it */
		gear->flags |= SAR_OBJ_PART_FLAG_STATE;
		oped = 1;
	    }
	    /* Is gear down and we want to raise it? */
	    else if(cur_state && !gear_state)
	    {
		/* Landing gear is currently down and we want to raise it */
		if(!(gear->flags & SAR_OBJ_PART_FLAG_LGEAR_FIXED))
		{
		    gear->flags &= ~SAR_OBJ_PART_FLAG_STATE;
		    oped = 1;
		}
	    }
	}

	/* Play landing gear sound? Note that we play the landing
	 * gear sound here since SARSimUpdatePart() that does the
	 * actual landing gear parts updating will play sounds for
	 * multiple landing gears and we don't want that.
	 */
	if(play_sound && oped)
	{
	    if(gear_state)
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"gear_down_inside" : "gear_down",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	    else
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"gear_up_inside" : "gear_up",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	}
}

/*
 *	Open or close the first rescue door on the given object.
 *
 *	This marks the door to either start opening or closing.
 *
 *	If the door is to be closed and has the SAR_DOOR_FLAG_STAY_OPEN
 *	flag set, then this flag will be unset.
 */
void SARSimOpDoorRescue(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr,
	int state		/* 0 = close, 1 = open */
)
{
	int i;
	sar_obj_part_struct *door_ptr;


	if(obj_ptr == NULL)
	    return;

	/* Iterate through all rescue doors */
	for(i = 0; 1; i++)
	{
	    door_ptr = SARObjGetPartPtr(
		obj_ptr, SAR_OBJ_PART_TYPE_DOOR_RESCUE, i
	    );
	    if(door_ptr == NULL)
		break;

	    /* Door is opened, we want to close it? */
	    if((door_ptr->flags & SAR_OBJ_PART_FLAG_STATE) &&
	       !state
	    )
	    {
		if(!(door_ptr->flags & SAR_OBJ_PART_FLAG_DOOR_FIXED))
		{
		    door_ptr->flags &= ~SAR_OBJ_PART_FLAG_STATE;
		    door_ptr->flags &= ~SAR_OBJ_PART_FLAG_DOOR_STAY_OPEN;
		}
	    }
	    /* Door is closed, we want to open it? */
	    else if(!(door_ptr->flags & SAR_OBJ_PART_FLAG_STATE) &&
		    state
	    )
	    {
		if(!(door_ptr->flags & SAR_OBJ_PART_FLAG_DOOR_FIXED) &&
		   !(door_ptr->flags & SAR_OBJ_PART_FLAG_DOOR_LOCKED)
		)
		    door_ptr->flags |= SAR_OBJ_PART_FLAG_STATE;
	    }
	}

	/* Let SARSimUpdatePart() play door sound */

}

/*
 *	Updates control positions on the rotor in the given list as
 *	needed in accordance with the given control positions pitch
 *	and bank which must be in the range of -1.0 to 1.0.
 *
 *	All inputs assumed valid.
 */
void SARSimRotorUpdateControls(
	sar_obj_rotor_struct *rotor_ptr,
	float pitch, float bank
)
{
	if(rotor_ptr->flags & SAR_ROTOR_FLAG_FOLLOW_CONTROLS)
	{
	    rotor_ptr->control_coeff_pitch = pitch;
	    rotor_ptr->control_coeff_bank = bank;
	}
}

/*
 *	Returns the engine state of the object.
 *
 *	If object is not a type that has engines, then
 *	SAR_ENGINE_OFF is returned.
 */
sar_engine_state SARSimGetEngineState(sar_object_struct *obj_ptr)
{
	sar_object_aircraft_struct *obj_aircraft_ptr;

	if(obj_ptr == NULL)
	    return(SAR_ENGINE_OFF);

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
		return(obj_aircraft_ptr->engine_state);
	    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;
	}

	return(SAR_ENGINE_OFF);
}

/*
 *	Turns engine off or starts engine, depending on 
 *	engine_state which can be either SAR_ENGINE_ON or
 *	SAR_ENGINE_OFF.
 *
 *	When passing engine_state as SAR_ENGINE_ON, it does not
 *	actually turn the engine on but rather sets engine_state on
 *	the object to initialize (start) and sets the initialization
 *	time depending on the current throttle position.
 */
void SARSimOpEngine(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, sar_engine_state engine_state,
	snd_recorder_struct *recorder, char play_sound, char ear_in_cockpit
)
{
	char turned_engine_on = 0, turned_engine_off = 0;
	time_t engine_start_delay = 5000l;	/* In ms */
	sar_object_aircraft_struct *obj_aircraft_ptr;
  
 
	if(obj_ptr == NULL)
	    return;

	/* Request to start engine as init is the same as on */
	if(engine_state == SAR_ENGINE_INIT)
	    engine_state = SAR_ENGINE_ON;

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr == NULL)
		break;

	    /* Cannot turn engine on if not flyable */
	    if((obj_aircraft_ptr->air_worthy_state != SAR_AIR_WORTHY_FLYABLE) &&
	       (engine_state == SAR_ENGINE_ON)
	    )
		break;

	    /* Turn engine on and engine is currently off? */
	    if((engine_state == SAR_ENGINE_ON) &&
	       (obj_aircraft_ptr->engine_state == SAR_ENGINE_OFF)
	    )
	    {
		obj_aircraft_ptr->engine_state = SAR_ENGINE_INIT;

		/* Check if landed, if landed use normal engine start
	         * delay, otherwise start engine immediatly
		 */
		if(obj_aircraft_ptr->landed)
		    obj_aircraft_ptr->next_engine_on = cur_millitime +
			(time_t)(engine_start_delay *
			    CLIP(1.0 - obj_aircraft_ptr->throttle, 0.0, 1.0)
			);
		else
		    obj_aircraft_ptr->next_engine_on = cur_millitime;

		turned_engine_on = 1;
	    }
	    /* Turn engine off? */
	    else if(engine_state == SAR_ENGINE_OFF)
	    {
		obj_aircraft_ptr->engine_state = SAR_ENGINE_OFF;
		obj_aircraft_ptr->next_engine_on = 0l;
		turned_engine_off = 1;
	    }
	    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;
	}


	/* Play landing gear sound? */
	if(play_sound)
	{
	    if(turned_engine_on)
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"engine_start_inside" : "engine_start",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	    else if(turned_engine_off)
		SARSoundSourcePlayFromList(
		    recorder,
		    obj_ptr->sndsrc, obj_ptr->total_sndsrcs,
		    ear_in_cockpit ?
			"engine_shutdown_inside" : "engine_shutdown",
		    &obj_ptr->pos, &obj_ptr->dir,
		    &scene->ear_pos
		);
	}
}

/*
 *	Pitches engines up (if pitch_state is 0) or forwards (if 
 *	pitch_state is 1).
 *
 *	All rotors will be marked to begin pitching towards the newly
 *	specified pitch_state.
 */
void SARSimPitchEngine(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr, int pitch_state
)
{
	sar_obj_rotor_struct **rotor = NULL, *rotor_ptr;
	char can_pitch = 0;
	int total_rotors = 0;
	sar_object_aircraft_struct *obj_aircraft_ptr;


	if(obj_ptr == NULL)
	    return;

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr == NULL)
		break;

	    /* Engines cannot pitch? */
	    if(obj_aircraft_ptr->engine_can_pitch != 1)
		break;

	    /* Cannot pitch engine on if not flyable */
	    if(obj_aircraft_ptr->air_worthy_state == SAR_AIR_WORTHY_NOT_FLYABLE)
		break;

	    /* Get list of rotors */
	    rotor = obj_aircraft_ptr->rotor;
	    total_rotors = obj_aircraft_ptr->total_rotors;
	    can_pitch = 1;

	    /* Iterate through rotors, checking if one does not allow
	     * pitch when landed
	     */
	    if(obj_aircraft_ptr->landed)
	    {
		int i;

		for(i = 0; i < total_rotors; i++)
		{
		    rotor_ptr = rotor[i];
		    if(rotor_ptr == NULL)
			continue;

		    if(rotor_ptr->flags & SAR_ROTOR_FLAG_NO_PITCH_LANDED)
		    {
			/* Landed and this rotor does not allow pitch when
			 * landed
			 */
			total_rotors = 0;
			rotor = NULL;
			can_pitch = 0;
			break;
		    }
		}
	    }
	    if(!can_pitch)
		break;

	    /* Set new flight model */
	    if(pitch_state == 1)
	    {
		obj_aircraft_ptr->flight_model_type =
		    SAR_FLIGHT_MODEL_AIRPLANE;
	    }
	    else
	    {
		obj_aircraft_ptr->flight_model_type =
		    SAR_FLIGHT_MODEL_HELICOPTER;
	    }
	    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;
	}

	/* Iterate through each valid rotor found on the object and
	 * set the new pitch state
	 */
	if(can_pitch)
	{
	    int i;
	    for(i = 0; i < total_rotors; i++)
	    {
	        rotor_ptr = rotor[i];
	        if(rotor_ptr == NULL)
		    continue;
	        if(rotor_ptr->flags & SAR_ROTOR_FLAG_CAN_PITCH)
	        {
		    if(pitch_state)
		        rotor_ptr->flags |= SAR_ROTOR_FLAG_PITCH_STATE;
		    else
		        rotor_ptr->flags &= ~SAR_ROTOR_FLAG_PITCH_STATE;
	        }
	    }
	}
}


/*
 *	Updates all lights on the object.
 */
void SARSimUpdateLights(sar_object_struct *obj_ptr)
{
	int i;
	sar_light_struct *light;

	if(obj_ptr == NULL)
	    return;

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Strobe light? */
	    if(light->flags & SAR_LIGHT_FLAG_STROBE)
	    {
		if(light->next_on > 0)
		{
		    /* Waiting to be turned on, implies it's currently
		     * off.  Now check if it's time to turn it on.
		     */
		    if(light->next_on <= cur_millitime)
		    {
			light->next_on = 0;
			light->next_off = cur_millitime +
			    light->int_on;
		    }
		    else
		    {
			continue;
		    }
		}
		else
		{
		    /* Waiting to be turned off, implies it's currently
		     * on.  Now check if it's time to turn it off.
		     */
		    if(light->next_off <= cur_millitime)
		    {
			light->next_on = cur_millitime +
			    light->int_off;
			light->next_off = 0;
			continue;
		    }
		}
	    }
	}
}

/*
 *	Returns the state of the first light structure on the object.
 *
 *	Can return false if no lights exist or error.
 */
int SARSimGetLightsState(sar_object_struct *obj_ptr)
{
	int i;
	const sar_light_struct *light;


	if(obj_ptr == NULL)
	    return(0);

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Is it a strobe light? */
	    if(!(light->flags & SAR_LIGHT_FLAG_STROBE) &&
	       !(light->flags & SAR_LIGHT_FLAG_ATTENUATE)
	    )
		return(light->flags & SAR_LIGHT_FLAG_ON);
	}

	return(0);
}

/*
 *	Turns all regular lights on object on or off.
 */
void SARSimOpLights(
	sar_object_struct *obj_ptr, int state
)
{
	int i;
	sar_light_struct *light;

	if(obj_ptr == NULL)
	    return;

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Skip non-standard lights */
	    if((light->flags & SAR_LIGHT_FLAG_STROBE) ||
	       (light->flags & SAR_LIGHT_FLAG_ATTENUATE)
	    )
		continue;

	    if(state)
	    {
		/* Turn on */
		light->flags |= SAR_LIGHT_FLAG_ON;
	    }
	    else
	    {
		/* Turn off */
		light->flags &= ~SAR_LIGHT_FLAG_ON;
	    }
	}
}

/*
 *      Returns the state of the first strobe light structure on the
 *	object.
 *
 *      Can return false if no lights exist or error.
 */
int SARSimGetStrobesState(sar_object_struct *obj_ptr)
{
	int i;
	const sar_light_struct *light;


	if(obj_ptr == NULL)
	    return(0);

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Is it a strobe light? */
	    if(light->flags & SAR_LIGHT_FLAG_STROBE)
		return(light->flags & SAR_LIGHT_FLAG_ON);
	}

	return(0);
}

/*
 *	Turns all strobe lights on object on or off.
 */
void SARSimOpStrobes(
	sar_object_struct *obj_ptr, int state
)
{
	int i;
	sar_light_struct *light;

	if(obj_ptr == NULL)
	    return;

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Skip non-strobe lights */
	    if(!(light->flags & SAR_LIGHT_FLAG_STROBE))
		continue;

	    if(state)
	    {
		/* Turn on */
		light->flags |= SAR_LIGHT_FLAG_ON;
	    }
	    else
	    {   
		/* Turn off */
		light->flags &= ~SAR_LIGHT_FLAG_ON;
	    }
	}
}

/*
 *      Returns the state of the first attenuation light structure on the
 *      object.
 *
 *      Can return false if no lights exist or error.
 */
int SARSimGetAttenuateState(sar_object_struct *obj_ptr)
{
	int i;
	const sar_light_struct *light;


	if(obj_ptr == NULL)
	    return(0);

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)  
		continue;

	    /* Is it a attenuation light? */
	    if(light->flags & SAR_LIGHT_FLAG_ATTENUATE)
		return(light->flags & SAR_LIGHT_FLAG_ON);
	}

	return(0);
}

/*
 *      Turns all attenuation lights on object on or off.
 */
void SARSimOpAttenuate(
	sar_object_struct *obj_ptr, int state
)
{
	int i;
	sar_light_struct *light;

	if(obj_ptr == NULL)
	    return;

	for(i = 0; i < obj_ptr->total_lights; i++)
	{
	    light = obj_ptr->light[i];
	    if(light == NULL)
		continue;

	    /* Skip non-attenuation lights */
	    if(!(light->flags & SAR_LIGHT_FLAG_ATTENUATE))
		continue;

	    if(state)
	    {
		/* Turn on */
		light->flags |= SAR_LIGHT_FLAG_ON;
	    }
	    else
	    {
		/* Turn off */
		light->flags &= ~SAR_LIGHT_FLAG_ON;
	    }
	}
}

/*
 *	Repairs the object if possible.
 *
 *	Does not check if the object is near any repair faclity, the
 *	calling function must do that.
 *
 *      Returns -1 on error, -2 if the object cannot be repaired or
 *      0 on success.
 */
int SARSimOpRepair(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
)
{
	sar_contact_bounds_struct *cb;
	sar_object_aircraft_struct *obj_aircraft_ptr;

	if((scene == NULL) || (obj_ptr == NULL))
	    return(-1);

	switch(obj_ptr->type)
	{
	  case SAR_OBJ_TYPE_AIRCRAFT:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		obj_aircraft_ptr->air_worthy_state = SAR_AIR_WORTHY_FLYABLE;
		obj_ptr->hit_points = obj_ptr->hit_points_max;

		/* Reset crash flags */
		cb = obj_ptr->contact_bounds;
		if(cb != NULL)
		{
		    cb->crash_flags = SAR_CRASH_FLAG_CRASH_OTHER;
		}
		/* Return success */
		return(0);
	    }
	    break;

/* Add support for other objects that can be repaired here */
	  default:
	    break;
	}

	return(-2);
}

/*
 *	Refuels the object if possible.
 *
 *      Does not check if the object is near any refueling faclity, the
 *      calling function must do that.
 *
 *	Returns -1 on error, -2 if the object cannot be refueled or
 *	0 on success.
 */
int SARSimOpRefuel(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
)
{
	int eft_num;
	sar_object_aircraft_struct *obj_aircraft_ptr;
	sar_object_fueltank_struct *obj_fueltank_ptr;
	sar_external_fueltank_struct *eft_ptr;

	if((scene == NULL) || (obj_ptr == NULL))
	    return(-1);

	switch(obj_ptr->type)
	{
	  case SAR_OBJ_TYPE_AIRCRAFT:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		/* Set current fuel to amount of maximum fuel */
		obj_aircraft_ptr->fuel = obj_aircraft_ptr->fuel_max;
		/* Replace and refuel all external reserved tanks */
		for(eft_num = 0; eft_num < obj_aircraft_ptr->total_external_fueltanks; eft_num++)
		{
		    eft_ptr = obj_aircraft_ptr->external_fueltank[eft_num];
		    if(eft_ptr == NULL)
			continue;

		    eft_ptr->flags |= SAR_EXTERNAL_FUELTANK_FLAG_ONBOARD;
		    eft_ptr->fuel = eft_ptr->fuel_max;
		}
		return(0);
	    }
	    break;

	  case SAR_OBJ_TYPE_FUELTANK:
	    obj_fueltank_ptr = SAR_OBJ_GET_FUELTANK(obj_ptr);
	    if(obj_fueltank_ptr != NULL)
	    {
		/* Set current fuel to amount of maximum fuel */
		obj_fueltank_ptr->fuel = obj_fueltank_ptr->fuel_max;
		return(0);
	    }
	    break;

/* Add support for other objects that can be refueled here */
	  default:
	    break;
	}

	return(-2);
}


/*
 *	Marks the given number of passengers to leave or drop from the
 *	given object as pending. If either value is -1 then the value
 *	in question will be interprited as `all'.
 *
 *	Returns non-zero on error.
 */
int SARSimOpPassengersSetLeave(  
	sar_scene_struct *scene, sar_object_struct *obj_ptr,
	int passengers_leave_pending, int passengers_drop_pending
)
{
	int *crew, *passengers, *passengers_max;
	float *passengers_mass;
	int *cur_passengers_leave_pending, *cur_passengers_drop_pending;


	if((scene == NULL) || (obj_ptr == NULL))
	    return(-1);

	if(SARObjGetOnBoardPtr(
	    obj_ptr,
	    &crew, &passengers, &passengers_max,
	    &passengers_mass,
	    &cur_passengers_leave_pending,
	    &cur_passengers_drop_pending
	))
	    return(-2);

	if(passengers_max == NULL)
	    return(-2);

	/* Set passengers leave pending */
	if(cur_passengers_leave_pending != NULL)
	{
	  if(passengers_leave_pending < 0)
	  {
	    if((cur_passengers_leave_pending != NULL) &&
	       (passengers_max != NULL)
	    )
		*cur_passengers_leave_pending = *passengers_max;
	  }
	  else
	  {
	    if(passengers_max != NULL)
	    {
		if(passengers_leave_pending > *passengers_max)
		    passengers_leave_pending = *passengers_max;

		if(cur_passengers_leave_pending != NULL)
		    *cur_passengers_leave_pending = passengers_leave_pending;
	    }
	  }
	}

	/* Set passengers drop pending */
	if(cur_passengers_drop_pending != NULL)
	{
	    if(passengers_drop_pending < 0)
	    {
		if((cur_passengers_drop_pending != NULL) &&
		   (passengers_max != NULL)
		)
		    *cur_passengers_drop_pending = *passengers_max;
	    }
	    else
	    {
		if(passengers_max != NULL)
		{
		    if(passengers_drop_pending > *passengers_max)
			passengers_drop_pending = *passengers_max;

		    if(cur_passengers_drop_pending != NULL)
			*cur_passengers_drop_pending = passengers_drop_pending;
		}
	    }
	}

	return(0);
}

/*
 *	Unloads all passengers from the given object by checking if the
 *	object can have passengers and setting the object's passengers
 *	count to 0 and passengers mass to 0.
 *
 *	Does not check if unloading passengers is permitted, the calling
 *	function must do that.
 *
 *	Returns the number of passengers unloaded.
 */
int SARSimOpPassengersUnloadAll(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
)
{
	int passengers_unloaded = 0;
	int *crew, *passengers, *passengers_max;
	float *passengers_mass;
	int *passengers_leave_pending, *passengers_drop_pending;


	if((scene == NULL) || (obj_ptr == NULL))
	    return(passengers_unloaded);

	if(SARObjGetOnBoardPtr(
	    obj_ptr,
	    &crew, &passengers, &passengers_max,
	    &passengers_mass,
	    &passengers_leave_pending, &passengers_drop_pending
	))
	    return(passengers_unloaded);

	/* Got passengers? */
	if(passengers != NULL)
	{
	    /* Unload all passengers */
	    passengers_unloaded += *passengers;
	    *passengers = 0;

	    /* Reset passengers mass (in kg) if any */
	    if(passengers_mass != NULL)
		*passengers_mass = 0.0f;

	    /* Since all passengers gone, no pending to leave */
	    if(passengers_leave_pending != NULL)
		*passengers_leave_pending = 0;
	    if(passengers_drop_pending != NULL)
		*passengers_drop_pending = 0;
	}

	return(passengers_unloaded);
}


/*
 *	Transfers fuel from external reserve tanks on the object
 *	(if it has any) to the main fuel tank.
 *
 *	Returns the amount transfered or 0.0 on failure.
 */
float SARSimOpTransferFuelFromTanks(
	sar_scene_struct *scene, sar_object_struct *obj_ptr
)
{
	int i;
	sar_object_aircraft_struct *obj_aircraft_ptr;
	float *fuel = NULL, *fuel_max = NULL;
	sar_external_fueltank_struct **eft = NULL, *eft_ptr;
	int eft_total = 0;
	float fuel_transfered = 0.0f;

	if((scene == NULL) || (obj_ptr == NULL))
	    return(fuel_transfered);

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		fuel = &obj_aircraft_ptr->fuel;
		fuel_max = &obj_aircraft_ptr->fuel_max;
		eft = obj_aircraft_ptr->external_fueltank;
		eft_total = obj_aircraft_ptr->total_external_fueltanks;
	    }
	    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;
	}

	/* No fuel tanks to transfer to? */
	if((fuel == NULL) || (fuel_max == NULL))
	    return(fuel_transfered);

	/* No room for fuel on main tanks? */
	if(*fuel_max <= 0.0f)
	    return(fuel_transfered);

	/* Current fuel too much? */
	if(*fuel > *fuel_max)
	{
	    *fuel = *fuel_max;
	    return(fuel_transfered);
	}
	/* Sanitize current fuel */
	if(*fuel < 0.0f)
	    *fuel = 0.0f;

	/* No fuel tanks to transfer from? */
	if((eft == NULL) || (eft_total < 1))
	    return(fuel_transfered);

	/* Iterate through each external reserved fuel tank */
	for(i = 0; i < eft_total; i++)
	{
	    eft_ptr = eft[i];
	    if(eft_ptr == NULL)
		continue;

	    /* This external fuel tank not dropped? */
	    if(eft_ptr->flags & SAR_EXTERNAL_FUELTANK_FLAG_ONBOARD)
	    {
		float fuel_needed = MAX(*fuel_max - *fuel, 0.0f);
		float fuel_to_transfer;

		/* Check if external reserved tank has enough fuel,
		 * that we need to transfer. Then get the amount of
		 * fuel we can actually transfer.
		 */
		if(eft_ptr->fuel < fuel_needed)
		    fuel_to_transfer = eft_ptr->fuel;
		else
		    fuel_to_transfer = fuel_needed;

		/* Transfer fuel */
		*fuel += fuel_to_transfer;
		eft_ptr->fuel -= fuel_to_transfer;

		/* Update total amount of fuel transfered */
		fuel_transfered += fuel_to_transfer;
	    }
	}


	return(fuel_transfered);
}


/*
 *	Drops the `next' fuel tank that is on board the given object
 *	(if any).
 *
 *	Returns -1 on error, -2 if no tanks left to drop, or the
 *	index number of the newly created fuel tank object.
 *
 *	Inputs assumed valid except for obj_ptr and obj_num.
 */
int SARSimOpDropFuelTankNext(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num, sar_object_struct *obj_ptr
)
{
	int i, ft_obj_num;
	sar_object_aircraft_struct *obj_aircraft_ptr = NULL;
	sar_external_fueltank_struct **eft = NULL, *eft_ptr;
	int total_efts = 0;

	/* obj_ptr is the object dropping a fuel tank
	 *
	 * eft_ptr is the external fuel tank on obj_ptr
	 *
	 * ft_obj_ptr is the newly dropped fuel tank object
	 */

	if(!SARObjIsAllocated(*ptr, *total, obj_num))
	    return(-1);
	if(obj_ptr == NULL)
	    return(-1);

	/* Get pointer array to external fuel tanks on the given
	 * object
	 */
	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		eft = obj_aircraft_ptr->external_fueltank;
		total_efts = obj_aircraft_ptr->total_external_fueltanks;
	    }
	    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;
	}

	/* Iterate through existing fuel tanks */
	for(i = 0; i < total_efts; i++)
	{
	    eft_ptr = eft[i];
	    if(eft_ptr == NULL)
		continue;

	    /* Not droppable? */
	    if(eft_ptr->flags & SAR_EXTERNAL_FUELTANK_FLAG_FIXED)
		continue;

	    /* Already dropped? */
	    if(!(eft_ptr->flags & SAR_EXTERNAL_FUELTANK_FLAG_ONBOARD))
		continue;

	    /* Got valid external fuel tank that is on board and
	     * can be dropped.
	     */
	    break;
	}
	/* No more fuel tanks to drop? */
	if((i >= total_efts) || (eft_ptr == NULL))
	    return(-2);


	/* Got valid fuel tank i to drop, begin dropping it */

	/* Drop it by marking it as not on board */
	eft_ptr->flags &= ~SAR_EXTERNAL_FUELTANK_FLAG_ONBOARD;

	/* Create a new fuel tank object */
	ft_obj_num = SARObjNew(
	    scene, ptr, total, SAR_OBJ_TYPE_FUELTANK
	);
	if(ft_obj_num > -1)
	{
	    double a[3 * 1], r[3 * 1];
	    sar_direction_struct *dir;
	    sar_contact_bounds_struct *cb;
	    sar_object_struct *ft_obj_ptr = (*ptr)[ft_obj_num];
	    sar_object_fueltank_struct *obj_fueltank_ptr;

	    /* Newly created fuel tank object valid? */
	    if(ft_obj_ptr != NULL)
	    {
		/* Begin setting values on nwe fuel tank object */

		ft_obj_ptr->flags = 0;
		if(obj_ptr->flags & SAR_OBJ_FLAG_SHADE_MODEL_SMOOTH)
		    ft_obj_ptr->flags |= SAR_OBJ_FLAG_SHADE_MODEL_SMOOTH;

		free(ft_obj_ptr->name);
		ft_obj_ptr->name = STRDUP("Fueltank");

		ft_obj_ptr->range = 500.0f;	/* 500 meters */

		ft_obj_ptr->ground_elevation_msl = obj_ptr->ground_elevation_msl;

		/* Create contact bounds for new fuel tank object */
		cb = ft_obj_ptr->contact_bounds;
		if(cb == NULL)
		    ft_obj_ptr->contact_bounds = cb = SAR_CONTACT_BOUNDS(
			calloc(1, sizeof(sar_contact_bounds_struct))
		    );
		if(cb != NULL)
		{
		    cb->crash_flags = 0;
		    cb->crash_type = SAR_CRASH_TYPE_AIRCRAFT;

		    /* Set spherical contact bounds */
		    SARObjAddContactBoundsSpherical(
			ft_obj_ptr,
			cb->crash_flags,
			cb->crash_type,
			eft_ptr->radius
		    );
		} 

		ft_obj_ptr->life_span = 0l;
		ft_obj_ptr->hit_points = 1.0f;
		ft_obj_ptr->hit_points_max = 1.0f;
		ft_obj_ptr->temperature = eft_ptr->temperature;

		/* Remove existing visual model on new fueltank object
		 * (if it has one or more)
		 */
		SARVisualModelUnref(scene, ft_obj_ptr->visual_model);

		/* Transfer visual model from eft_ptr's visual model
		 * to the new fuel tank object and increment ref count
		 */
		ft_obj_ptr->visual_model = eft_ptr->visual_model;
		SARVisualModelRef(ft_obj_ptr->visual_model);
		ft_obj_ptr->visual_model_ir = eft_ptr->visual_model_ir;
		SARVisualModelRef(ft_obj_ptr->visual_model_ir);


		/* Get fueltank substructure from the object that is the
		 * fueltank
		 */
		obj_fueltank_ptr = SAR_OBJ_GET_FUELTANK(ft_obj_ptr);
		if(obj_fueltank_ptr != NULL)
		{
		    sar_position_struct *vel;

		    obj_fueltank_ptr->flags = 0;

		    obj_fueltank_ptr->belly_to_center_height =
			eft_ptr->belly_to_center_height;

		    obj_fueltank_ptr->dry_mass = eft_ptr->dry_mass;
		    obj_fueltank_ptr->fuel = eft_ptr->fuel;
		    obj_fueltank_ptr->fuel_max = eft_ptr->fuel_max;

		    obj_fueltank_ptr->ref_object = obj_num;


		    /* Maximum vertical velocity in meters per cycle */
		    obj_fueltank_ptr->vel_z_max = (float)(SFMFeetToMeters(-100.0) *
			SAR_SEC_TO_CYCLE_COEFF);

		    /* Get velocity from object */
		    vel = &obj_fueltank_ptr->vel;
		    if(obj_aircraft_ptr != NULL)
			memcpy(vel, &obj_aircraft_ptr->vel, sizeof(sar_position_struct));
		    else
			memset(vel, 0x00, sizeof(sar_position_struct));

		    obj_fueltank_ptr->speed = (float)SFMHypot3(
			vel->x, vel->y, vel->z
		    );
		}


		/* Move new fuel tank object to proper position */
		dir = &obj_ptr->dir;

		a[0] = eft_ptr->offset_pos.x;
		a[1] = eft_ptr->offset_pos.y;
		a[2] = eft_ptr->offset_pos.z;

		/* Rotate matrix a into r */
		MatrixRotateBank3(a, -dir->bank, r);	/* Our bank is negative so,
							 * so pass as flipped sign
							 */
		MatrixRotatePitch3(r, dir->pitch, a);
		MatrixRotateHeading3(a, dir->heading, r);

		ft_obj_ptr->pos.x = (float)(obj_ptr->pos.x + r[0]);
		ft_obj_ptr->pos.y = (float)(obj_ptr->pos.y + r[1]);
		ft_obj_ptr->pos.z = (float)(obj_ptr->pos.z + r[2]);

		memcpy(&ft_obj_ptr->dir, dir, sizeof(sar_direction_struct));

		SARSimWarpObject(
		    scene, ft_obj_ptr,
		    &ft_obj_ptr->pos, &ft_obj_ptr->dir
		);
	    }
	}

	return(i);
}


/*
 *	Generates a new smoke unit on the specified smoke object.
 */
void SARSimSmokeSpawn(
	sar_core_struct *core_ptr, sar_object_struct *obj_ptr,
	sar_object_smoke_struct *obj_smoke_ptr
)
{
	int i;
	sar_object_smoke_unit_struct *u;

	if((obj_ptr == NULL) || (obj_smoke_ptr == NULL))
	    return;

	/* No units */
	if(obj_smoke_ptr->unit == NULL)
	    return;

	/* Spawn unit by smoke type */
	switch(obj_smoke_ptr->type)
	{
	  case SAR_SMOKE_TYPE_SMOKE:
	    /* Find available unit */
	    u = NULL;
	    for(i = 0; i < obj_smoke_ptr->total_units; i++)
	    {
		u = &obj_smoke_ptr->unit[i];

		/* Unit available? (if it is no longer visible) */
		if(u->visibility <= 0.0f)
		    break;
	    }
	    /* Not able to find any available units? */
	    if(i >= obj_smoke_ptr->total_units)
	    {
		/* Overwrite oldest (first) unit and shift the rest of
		 * the units, leaving the last one available
		 */
		for(i = 0; i < (obj_smoke_ptr->total_units - 1); i++)
		    memcpy(
			&obj_smoke_ptr->unit[i],
			&obj_smoke_ptr->unit[i + 1],
			sizeof(sar_object_smoke_unit_struct)
		    );
		/* Get last unit */
		u = (obj_smoke_ptr->total_units > 0) ?
		    &obj_smoke_ptr->unit[
			obj_smoke_ptr->total_units - 1
		    ] : NULL;
	    }
	    /* Got available unit? */
	    if(u != NULL)
	    {
		sar_position_struct *pos;

		/* Set initial position */
		memcpy(&u->pos, &obj_ptr->pos, sizeof(sar_position_struct));

		/* Set initial velocity to drift upwards */
/* TODO need to apply wind and other variables to drift velocity */
		pos = &u->vel;
		pos->x = 0.0f;
		pos->y = 0.0f;
		pos->z = 4.0f;

		/* Apply offset to initial position */
		pos = &u->pos;
		pos->x += obj_smoke_ptr->respawn_offset.x;
		pos->y += obj_smoke_ptr->respawn_offset.y;
		pos->z += obj_smoke_ptr->respawn_offset.z;

		u->radius = obj_smoke_ptr->radius_start;
		u->visibility = 1.0f;
	    }
	    break;

	  case SAR_SMOKE_TYPE_SPARKS:
	    /* When sparks are respawned, all of them are respawned
	     * at once
	     */
	    for(i = 0; i < obj_smoke_ptr->total_units; i++)
	    {
		u = &obj_smoke_ptr->unit[i];

		/* Begin updating this unit's values */

		/* Set initial position */
		memcpy(&u->pos, &obj_ptr->pos, sizeof(sar_position_struct));

		/* Set random velocity */
		if(1)
		{
		    const time_t respawn_int = (obj_smoke_ptr->respawn_int > 0l) ?
			obj_smoke_ptr->respawn_int : 1000l;
		    float rand_theta = (float)(SARRandomCoeff(i + 0) * 2.0 * PI);
		    float rand_z = SARRandomCoeff(i + 1);
#if 0
		    float respawn_int_coeff = (float)obj_smoke_ptr->respawn_int /
			(float)CYCLE_LAPSE_MS;
		    float rand_speed = obj_smoke_ptr->radius_max /
			((respawn_int_coeff > 0.0f) ? respawn_int_coeff : 1.0f) *
			 ((SARRandomCoeff(i + 2) * 0.6f) + 0.3f);
#endif
		    float s = obj_smoke_ptr->radius_max *
			(float)CYCLE_LAPSE_MS / (float)respawn_int;
		    sar_position_struct *pos = &u->vel;
		    pos->x = (float)(cos(rand_theta) * s);
		    pos->y = (float)(sin(rand_theta) * s);
		    pos->z = (float)(s * rand_z);
		}

		/* Apply offset to initial position */
		if(1)
		{
		    sar_position_struct *pos = &u->pos;
		    pos->x += obj_smoke_ptr->respawn_offset.x;
		    pos->y += obj_smoke_ptr->respawn_offset.y;
		    pos->z += obj_smoke_ptr->respawn_offset.z;

		    u->radius = obj_smoke_ptr->radius_start;
		    u->visibility = 1.0f;
		}

		/* Set random spark color */
		if(1)
		{
		    float rand_green = SARRandomCoeff(i + 10);
		    float rand_blue = SARRandomCoeff(i + 11);
		    sar_color_struct *c = &u->color;
		    c->r = 1.0f;
		    c->g = (rand_green * 0.1f) + 0.9f;
		    c->b = (rand_green * rand_blue * 0.4f) + 0.6f;
		    c->a = 1.0f;
		}
	    }
	    break;

	  case SAR_SMOKE_TYPE_DEBRIS:
	    /* TODO */
	    break;
	}
}

/*
 *	Returns 1 if the object is in slew mode.
 *
 *	The object must be of type SAR_OBJ_TYPE_AIRCRAFT, otherwise
 *	it will always return False.
 */
int SARSimIsSlew(sar_object_struct *obj_ptr)
{
	sar_object_aircraft_struct *obj_aircraft_ptr;


	if(obj_ptr == NULL)
	    return(0);

	switch(obj_ptr->type)
	{
	  case SAR_OBJ_TYPE_AIRCRAFT:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
		return(
(obj_aircraft_ptr->flight_model_type == SAR_FLIGHT_MODEL_SLEW) ? 1 : 0
		);
	    break;

	  default:
	    break;
	}

	return(0);
}

/*
 *	Sets the object to enter slew mode if enter_slew is True.
 *	Otherwise it returns the object to its previous flight mode.
 *
 *	The object must be of type SAR_OBJ_TYPE_AIRCRAFT.
 */
void SARSimSetSlew(
	sar_object_struct *obj_ptr, int enter_slew
)
{
	sar_object_aircraft_struct *obj_aircraft_ptr;

	if(obj_ptr == NULL)
	    return;

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr == NULL)
		break;
	    /* Enter slew mode? */
	    if(enter_slew &&
	       (obj_aircraft_ptr->flight_model_type != SAR_FLIGHT_MODEL_SLEW)
	    )
	    {
		/* Record current flight model type */
		obj_aircraft_ptr->last_flight_model_type =
		    obj_aircraft_ptr->flight_model_type;

		/* Set flight model to slew mode */
		obj_aircraft_ptr->flight_model_type =
		    SAR_FLIGHT_MODEL_SLEW;
	    }
	    /* Leave slew mode? */
	    else if(!enter_slew &&
		    (obj_aircraft_ptr->flight_model_type == SAR_FLIGHT_MODEL_SLEW)
	    )
	    {
		/* Restore previous flight model type */
		obj_aircraft_ptr->flight_model_type =
		    obj_aircraft_ptr->last_flight_model_type;
	    }
	    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;
	}
}

/*
 *	Moves the human object specified by obj_human_ptr (the
 *	substructure) into the hoist deployment of the object specified
 *	by obj_ptr.
 *
 *	Note that the given obj_human_ptr may not be the substructure
 *	of the given obj_ptr.
 *
 *	Inputs assumed valid.
 *
 *	Returns the number of objects put into the hoist deployment.
 */
int SARSimDoPickUpHuman(
	sar_scene_struct *scene,
	sar_object_struct *obj_ptr,
	sar_object_human_struct *obj_human_ptr,	/* Not data structure of obj_ptr */
	int human_obj_num
)
{
	int n;
	int passengers = 0, passengers_max = 0;
	sar_object_aircraft_struct *obj_aircraft_ptr;
	sar_obj_hoist_struct *hoist = NULL;


	if(scene == NULL)
	    return(0);

	/* 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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		hoist = SARObjGetHoistPtr(obj_ptr, 0, NULL);
		passengers = obj_aircraft_ptr->passengers;
		passengers_max = obj_aircraft_ptr->passengers_max;
	    }
	    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;
	}

	/* Did we get a hoist structure? */
	if(hoist == NULL)
	    return(0);

	/* No more room left onboard for additional passengers? */
	if(passengers >= passengers_max)
	{
	    /* Warn if this is the player object and the scene is
	     * not currently displaying any messages
	     */
	    if((obj_ptr == scene->player_obj_ptr) &&
	       (scene->message_display_until <= 0)
	    )
	    {
		SARMessageAdd(
		    scene,
		    SAR_MESG_NO_ROOM_LEFT_FOR_PASSENGERS
		);
	    }
	    return(0);
	}

	/* Can hoist accomidate more occupants? (just limit to 1 for
	 * now)
	 */
	if(hoist->total_occupants >= 1)
	    return(0);

	/* Checks passed, begin adding human to hoist's list of
	 * occupants
	 */
	n = MAX(hoist->total_occupants, 0);
	hoist->total_occupants = n + 1;
	hoist->occupant = (int *)realloc(
	    hoist->occupant,
	    hoist->total_occupants * sizeof(int)
	);
	if(hoist->occupant == NULL)
	{
	    hoist->total_occupants = 0;
	    return(0);
	}
	else
	{
	    hoist->occupant[n] = human_obj_num;
	}

	/* Update hoist's occupants mass (in kg) */
	hoist->occupants_mass += obj_human_ptr->mass;

	/* Update nessesary flags on human object by what is on
	 * the end of the hoist rope
	 */
	switch(hoist->cur_deployment)
	{
	  case SAR_HOIST_DEPLOYMENT_BASKET:
	    obj_human_ptr->flags = ~SAR_HUMAN_FLAG_NEED_RESCUE;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT;
	    obj_human_ptr->flags |= SAR_HUMAN_FLAG_SIT_DOWN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT_UP;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_LYING;
	    /* Leave SAR_HUMAN_FLAG_ALERT and SAR_HUMAN_FLAG_AWARE as is */
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_IN_WATER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_ON_STREATCHER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_TOWARDS;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_AWAY;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_PUSHING;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_DIVER_CATCHER;
	    obj_human_ptr->flags |= SAR_HUMAN_FLAG_GRIPPED;
	    break;

	  case SAR_HOIST_DEPLOYMENT_DIVER:
	    obj_human_ptr->flags = ~SAR_HUMAN_FLAG_NEED_RESCUE;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT_DOWN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT_UP;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_LYING;
	    /* Leave SAR_HUMAN_FLAG_ALERT and SAR_HUMAN_FLAG_AWARE as is */
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_IN_WATER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_ON_STREATCHER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_TOWARDS;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_AWAY;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_PUSHING;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_DIVER_CATCHER;
	    obj_human_ptr->flags |= SAR_HUMAN_FLAG_GRIPPED;
	    break;

	  case SAR_HOIST_DEPLOYMENT_HOOK:
	    obj_human_ptr->flags = ~SAR_HUMAN_FLAG_NEED_RESCUE;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT_DOWN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_SIT_UP;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_LYING;
	    /* Leave SAR_HUMAN_FLAG_ALERT and SAR_HUMAN_FLAG_AWARE as is */
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_IN_WATER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_ON_STREATCHER;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_TOWARDS;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_RUN_AWAY;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_PUSHING;
	    obj_human_ptr->flags &= ~SAR_HUMAN_FLAG_DIVER_CATCHER;
	    obj_human_ptr->flags |= SAR_HUMAN_FLAG_GRIPPED;
	    break;
	}

 	return(1);
}


/*
 *	Called each time the hoist is pulled in, it will bring all
 *	objects in the hoist's deployment in. This will delete those
 *	object(s) and update the passenger count.
 *
 *	If a mission is active then the mission hoist in callback will
 *	be called.
 *
 *	Inputs assumed valid.
 *
 *	Returns the number of objects hoisted in.
 */
int SARSimDoHoistIn(
	sar_core_struct *core_ptr, sar_object_struct *obj_ptr
)
{
	int i, passenger_obj_num;
	int prev_passengers = 0;
	int *passengers = NULL, *passengers_max = NULL;
	int objects_hoisted_in = 0;
	sar_object_struct *passenger_obj_ptr;
	sar_object_human_struct *obj_human_ptr;
	sar_scene_struct *scene = core_ptr->scene;
	int is_player = (scene->player_obj_ptr == obj_ptr) ? 1 : 0;
	sar_obj_hoist_struct *hoist = SARObjGetHoistPtr(obj_ptr, 0, NULL);
	if((scene == NULL) || (hoist == NULL))
	    return(objects_hoisted_in);

	/* Get passenger counts */
	SARObjGetOnBoardPtr(
	    obj_ptr,
	    NULL, &passengers, &passengers_max,
	    NULL,
	    NULL, NULL
	);
	if(passengers != NULL)
	    prev_passengers = *passengers;

	/* Delete all objects in hoist's deployment */
	for(i = 0; i < hoist->total_occupants; i++)
	{
	    passenger_obj_num = hoist->occupant[i];
	    if(SARObjIsAllocated(
		core_ptr->object,
		core_ptr->total_objects,
		passenger_obj_num
	    ))
		passenger_obj_ptr = core_ptr->object[passenger_obj_num];
	    else
		continue;

	    /* Avoid deleting the given object */
	    if(passenger_obj_ptr == obj_ptr)
		continue;

	    /* Handle by occupant object type */
	    switch(passenger_obj_ptr->type)
	    {
	      case SAR_OBJ_TYPE_HUMAN:
		obj_human_ptr = SAR_OBJ_GET_HUMAN(passenger_obj_ptr);
		if(obj_human_ptr != NULL)
		{
		    /* If this is the player object, then print the
		     * hoist in message specified on the human object
		     */
		    if(is_player)
			SARMessageAdd(scene, obj_human_ptr->mesg_enter);
		}
		break;

	      default:
		/* Print hoist in message */
		if(is_player)
		{
/* TODO */
		}
		break;
	    }

	    /* Move the passenger object into the given object, the
	     * passenger object will be deleted if this call returns
	     * true
	     *
	     * Passenger count and related values will be updated on
	     * the given object
	     */
	    if(SARSimBoardObject(core_ptr, obj_ptr, passenger_obj_num))
	    {
		passenger_obj_num = -1;
		passenger_obj_ptr = NULL;
		objects_hoisted_in++;
	    }
	}

	/* Delete occupants list on hoist structure */
	free(hoist->occupant);
	hoist->occupant = NULL;
	hoist->total_occupants = 0;


	/* Print new number of passengers on the given object */
	if((passengers != NULL) && (passengers_max != NULL))
	{
	    /* Got more passengers? */
	    if(prev_passengers < *passengers)
	    {
		/* Is this the player object? */
	        if(is_player)
	        {
		    char text[256];

		    sprintf(
			text,
			"Passengers: %i(%i)",
			*passengers,
			*passengers_max
		    );
		    SARMessageAdd(scene, text);
		}
		else
		{
		    char name[SAR_OBJ_NAME_MAX];
		    char text[SAR_OBJ_NAME_MAX + 256];

		    if(obj_ptr->name == NULL)
		    {
			int obj_num = SARGetObjectNumberFromPointer(
			    scene, core_ptr->object, core_ptr->total_objects,
			    obj_ptr
			);
			if(SAR_OBJ_NAME_MAX > 80)
			    sprintf(name, "Object #%i\n", obj_num);
			else
			    *name = '\0';
		    }
		    else
		    {
			strncpy(name, obj_ptr->name, SAR_OBJ_NAME_MAX);
		    }
		    name[SAR_OBJ_NAME_MAX - 1] = '\0';

		    sprintf(text, "%s picked up %i passengers",
			name, (*passengers) - prev_passengers
		    );
		    SARMessageAdd(scene, text);
		}
	    }
	}

	/* If a mission is active then call the mission hoist in
	 * notify callback
	 */
	if(core_ptr->mission != NULL)
	    SARMissionHoistInNotify(core_ptr, obj_ptr, objects_hoisted_in);

	return(objects_hoisted_in);
}

/*
 *	Updates the source object to have taken the target object
 *	boarding it. The source object may be deleted by this function.
 *
 *	The target object must be able to hold the source object (must
 *	(be able to accept passengers and have room for more passengers).
 *
 *	For example if the target object is an aircraft and the source
 *	object is a human, then the human object will be deleted and
 *	the source object will have its passenger count increment (if
 *	it has room for more passengers).
 *
 *	Does not check if the source object is within range to board the
 *	target object.
 *
 *	Returns 0 on failure to board or 1 if the source object has
 *	boarded the target and the source object no longer exists.
 */
int SARSimBoardObject(
	sar_core_struct *core_ptr,
	sar_object_struct *tar_obj_ptr, int src_obj_num
)
{
	int *passengers, *passengers_max;
	float *passengers_mass;
	float src_mass = 0.0f;
	sar_object_struct *src_obj_ptr;
	sar_object_human_struct *obj_human_ptr;


	if(tar_obj_ptr == NULL)
	    return(0);

	/* Get source object pointer */
	src_obj_ptr = SARObjGetPtr(
	    core_ptr->object, core_ptr->total_objects, src_obj_num
	);
	/* Got source object pointer and it is not the same as the
	 * target object?
	 */
	if((src_obj_ptr == NULL) || (src_obj_ptr == tar_obj_ptr))
	    return(0);

	/* Check if the target object cannot hold passengers */
	if(SARObjGetOnBoardPtr(
	    tar_obj_ptr,
	    NULL, &passengers, &passengers_max,
	    &passengers_mass,
	    NULL, NULL
	))
	    return(0);

	if((passengers == NULL) || (passengers_max == NULL))
	    return(0);

	/* Target object has no more room for passengers? */
	if(*passengers >= *passengers_max)
	    return(0);

	/* Get some information about the source object by its type */
	switch(src_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:
	    obj_human_ptr = SAR_OBJ_GET_HUMAN(src_obj_ptr);
	    if(obj_human_ptr != NULL)
	    {
		src_mass = obj_human_ptr->mass;
	    }
	    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;
	}


	/* Begin moving source object into target object */

	/* Increment passengers count */
	*passengers = MAX(*passengers, 0) + 1;

	/* Increment passengers mass if possible */
	if(passengers_mass != NULL)
	    *passengers_mass = MAX(*passengers_mass, 0.0f) + src_mass;

	/* Delete the source object */
	SARObjDelete(
	    core_ptr,
	    &core_ptr->object, &core_ptr->total_objects,
	    src_obj_num
	);

	return(1);
}

/*
 *	Calculates the `fly by view' position with respect to
 *	the given object. The result of the calculation is stored
 *	in the pos_result structure.
 */
void SARSimSetFlyByPosition(
	sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	sar_object_struct *obj_ptr,
	sar_position_struct *pos_result
)
{
	float contact_radius;
	sar_object_aircraft_struct *obj_aircraft_ptr;

	if((obj_ptr == NULL) || (pos_result == NULL))
	    return;

	contact_radius = SARSimGetFlatContactRadius(obj_ptr);

	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:
	    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
	    if(obj_aircraft_ptr != NULL)
	    {
		sar_direction_struct vel_dir;
		sar_position_struct	*pos = &obj_ptr->pos,
					*vel = &obj_aircraft_ptr->vel;
		float	d = (float)MAX(
			    (5 * SAR_CYCLE_TO_SEC_COEFF) * obj_aircraft_ptr->speed,
			    contact_radius * 2
			),
			h = (float)(5 * SAR_CYCLE_TO_SEC_COEFF) * vel->z,
			r = (float)MAX(
			    SQRT((d * d) - (h * h)),
			    contact_radius * 2
			);

		vel_dir.heading = (float)SFMSanitizeRadians(
		    ((PI / 2) - atan2(vel->y, vel->x)) +
			obj_ptr->dir.heading
		);
		pos_result->x = (float)(r * sin(vel_dir.heading)) + pos->x -
		    (contact_radius * 2);
		pos_result->y = (float)(r * cos(vel_dir.heading)) + pos->y -
		    (contact_radius * 2);
		pos_result->z = h + pos->z;

		/* Don't position lower than object's center to ground */
		if(pos_result->z < (pos->z + obj_aircraft_ptr->center_to_ground_height))
		    pos_result->z = pos->z + obj_aircraft_ptr->center_to_ground_height;
	    }
	    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;
	}
}