Indicate these models want to use their own terrein handling code, until they switch...
[fg:toms-fgdata.git] / Aircraft / PC-6 / Nasal / pc6-b2h4.nas
1 var fuel_used_value = 0;\r
2 var last_fuel_read = 0;\r
3 var g_dt = 0;\r
4 \r
5 var init = func {\r
6         fuel_at_start = getprop("/consumables/fuel/total-fuel-lbs");\r
7         #print("Initialising urban effect and sone things else");\r
8         #setprop("/sim/rendering/quality-level",2);\r
9         #setprop("/sim/rendering/urban-shader",1);\r
10         #setprop("/sim/rendering/transition-shader",1);\r
11         #setprop("/sim/rendering/random-vegetation",1);\r
12         \r
13         last_fuel_read = getprop("/consumables/fuel/total-fuel-lbs");\r
14         print("Init Nasal PC6-B2H4 ...done");\r
15         \r
16         \r
17         main_loop();\r
18 }\r
19 \r
20 #activation des timers pour les lumieres clignotantes\r
21 var beacon = aircraft.light.new( "/sim/model/lights/beacon", [0.05, 0.05, 0.05, 1 ], "/controls/lighting/beacon" );\r
22 var strobe = aircraft.light.new( "/sim/model/lights/strobe", [0.05, 0.05, 0.05, 0.05, 0.05, 0.35 ], "/controls/lighting/strobe" );\r
23 \r
24 # Setup listener call to start update loop once the fdm is initialized\r
25 setlistener("sim/signals/fdm-initialized", init);\r
26 \r
27 #main loop\r
28 var main_loop = func {\r
29 \r
30         fuel_used();\r
31         \r
32         stall_horn();\r
33         \r
34         ##failures\r
35         check_g_load();\r
36         check_vne_flaps();\r
37         check_vne_structure();\r
38         \r
39         settimer(main_loop, 0);\r
40 }\r
41 \r
42 #fuel used, for the fuel used instrument\r
43 var fuel_used = func{\r
44 \r
45         var fuel_density = 6.0;\r
46         setprop("/consumables/fuel/tank[0]/level-lbs",fuel_density * getprop("/consumables/fuel/tank[0]/level-gal_us"));\r
47         setprop("/consumables/fuel/tank[1]/level-lbs",fuel_density * getprop("/consumables/fuel/tank[1]/level-gal_us"));\r
48         setprop("/consumables/fuel/total-fuel-gals",getprop("/consumables/fuel/tank[0]/level-gal_us") +  getprop("/consumables/fuel/tank[1]/level-gal_us"));\r
49         setprop("/consumables/fuel/total-fuel-lbs",getprop("/consumables/fuel/tank[0]/level-lbs") +  getprop("/consumables/fuel/tank[1]/level-lbs"));\r
50         \r
51         if(last_fuel_read==nil){\r
52                 last_fuel_read = getprop("/consumables/fuel/total-fuel-lbs");\r
53         }\r
54         var fuel_read = getprop("/consumables/fuel/total-fuel-lbs");\r
55         if(fuel_read!=nil and last_fuel_read!=nil){\r
56                 var fuel_used_delta = last_fuel_read - fuel_read;\r
57                 if(fuel_used_delta<0){\r
58                         fuel_used_delta = 0;\r
59                 }\r
60                 if(getprop("/instrumentation/fuel-used-indicator/serviceable")){\r
61                         fuel_used_value = fuel_used_value + fuel_used_delta;\r
62                 }\r
63                 last_fuel_read = fuel_read;\r
64                 \r
65                 if(fuel_used_value>999.99){\r
66                         fuel_used_value = 999.99;\r
67                 }\r
68                 setprop("/consumables/fuel/total-fuel-used-lbs",fuel_used_value);\r
69         }\r
70 }\r
71 \r
72 #fuel used reset, for the fuel used instrument\r
73 var reset_fuel_used = func{\r
74         fuel_used_value = 0;\r
75 }\r
76 \r
77 #activation de la stall horn\r
78 var stall_horn = func{\r
79         var alert = 0;\r
80         var kias = getprop("velocities/airspeed-kt");\r
81         var wow1 = getprop("gear/gear[1]/wow");\r
82         var wow2 = getprop("gear/gear[2]/wow");\r
83         var button_click = getprop("/controls/switches/stall_annunciator-click");\r
84         if(getprop("/instrumentation/annunciators/stall-warning/serviceable")){\r
85                 var stall_speed = 58 - (getprop("/controls/flight/flaps")*6);\r
86                 if((kias<stall_speed and !wow1 and !wow2) or button_click==0){\r
87                         alert=1;\r
88                 }\r
89         }\r
90         setprop("/sim/alarms/stall-warning",alert);\r
91 }\r
92 \r
93 #changer les positions des phares d'atterrissage\r
94 var adjust_left_landing_light = func{\r
95         var pos = getprop("/controls/lighting/landing-lights-pos-L");\r
96         var pos_switch = getprop("/controls/switches/pos_landing_L-click");\r
97         \r
98         if(getprop("/controls/lighting/pos-landing-lights-L-serviceable")==1){\r
99                 if(pos_switch == 1 and pos<1){\r
100                         pos = pos + 0.1;\r
101                         setprop("/controls/lighting/landing-lights-pos-L",pos);\r
102                 }else if(pos_switch == -1 and pos>0){\r
103                         pos = pos - 0.1;\r
104                         setprop("/controls/lighting/landing-lights-pos-L",pos);\r
105                 }\r
106         }\r
107 }\r
108 \r
109 var adjust_right_landing_light = func{\r
110         var pos = getprop("/controls/lighting/landing-lights-pos-R");\r
111         var pos_switch = getprop("/controls/switches/pos_landing_R-click");\r
112         \r
113         if(getprop("/controls/lighting/pos-landing-lights-R-serviceable")==1){\r
114                 if(pos_switch == 1 and pos<1){\r
115                         pos = pos + 0.1;\r
116                         setprop("/controls/lighting/landing-lights-pos-R",pos);\r
117                 }else if(pos_switch == -1 and pos>0){\r
118                         pos = pos - 0.1;\r
119                         setprop("/controls/lighting/landing-lights-pos-R",pos);\r
120                 }\r
121         }\r
122 }\r
123 \r
124 setlistener("/controls/switches/pos_landing_L-click",adjust_left_landing_light);\r
125 setlistener("/controls/switches/pos_landing_R-click",adjust_right_landing_light);\r
126 \r
127 # Terrain lookup function, for testing (et peut etre test de crash, etc ...)\r
128 var terrain_loockup = func {\r
129 \r
130    var lat = getprop("/position/latitude-deg");\r
131    var lon = getprop("/position/longitude-deg");\r
132    setprop("/environment/terrain_lookup/latitude",lat);\r
133    setprop("/environment/terrain_lookup/longitude",lon);\r
134    \r
135    if(lat != nil and lon != nil)\r
136    {\r
137       # Get Geo-Info\r
138       var info = geodinfo(lat, lon);\r
139 \r
140       if(info != nil){\r
141          if (info[1] != nil){\r
142                         var names = props.globals.getNode("/environment/terrain_lookup/names");\r
143                         if(names!=nil){\r
144                                 names.removeChildren();\r
145                         }\r
146                  \r
147             forindex(i; info[1].names){\r
148                                 setprop("/environment/terrain_lookup/names/names["~i~"]",info[1].names[i]);\r
149             }\r
150                         if(getprop("/environment/terrain_lookup/names/names")!=nil and substr(getprop("/environment/terrain_lookup/names/names"),0,1)=="p"){\r
151                                 setprop("/environment/terrain_lookup/dust_color",1);\r
152                         }else{\r
153                                 setprop("/environment/terrain_lookup/dust_color",0);\r
154                         }\r
155                         setprop("/environment/terrain_lookup/light_coverage",info[1].light_coverage);\r
156                         setprop("/environment/terrain_lookup/bumpiness",info[1].bumpiness);\r
157                         setprop("/environment/terrain_lookup/load_resistance",info[1].load_resistance);\r
158                         setprop("/environment/terrain_lookup/solid",info[1].solid);\r
159                         setprop("/environment/terrain_lookup/friction_factor",info[1].friction_factor);\r
160                         setprop("/environment/terrain_lookup/rolling_friction",info[1].rolling_friction);\r
161                         setprop("/environment/terrain_lookup/bumpiness",info[1].bumpiness);\r
162          }\r
163       }\r
164    }\r
165 \r
166    settimer(terrain_loockup, 0.1);\r
167 }\r
168 \r
169 setlistener("/sim/signals/fdm-initialized", terrain_loockup);\r
170 \r
171 var check_g_load = func{\r
172         var g_load = getprop("/accelerations/pilot-g");\r
173         if(g_load!=nil and (g_load>3.58 or g_load<-1.43)){\r
174                 g_dt = g_dt + 1;\r
175         }else{\r
176                 g_dt = 0;\r
177         }\r
178 \r
179         if(g_dt>5){\r
180                 setprop("/controls/flight/wing_destroyed",1);\r
181                 setprop("/sim/sound/crash",1);\r
182                 setprop("/sim/messages/copilot","Too much G load !!!!!!!!!!!!!");\r
183         }\r
184 }\r
185 \r
186 var check_vne_flaps = func{\r
187         var kias = getprop("velocities/airspeed-kt");\r
188         var flaps = getprop("/controls/flight/flaps");\r
189         if(kias!=nil and kias>95 and flaps!=nil and flaps>0){\r
190                 setprop("/sim/failure-manager/controls/flight/flaps/serviceable",0);\r
191 #               setprop("/sim/sound/crash",1);\r
192                 setprop("/sim/messages/copilot","VNE for flaps exceed !!!!!!!!!!!!!");\r
193         }\r
194 }\r
195 \r
196 var     check_vne_structure = func{\r
197         var kias = getprop("velocities/airspeed-kt");\r
198         if(kias!=nil and kias>151){\r
199                 setprop("/controls/flight/wing_destroyed",1);\r
200                 setprop("/sim/sound/crash",1);\r
201                 setprop("/sim/messages/copilot","VNE exceed !!!!!!!!!!!!!");\r
202         }\r
203 }\r