Indicate these models want to use their own terrein handling code, until they switch...
[fg:toms-fgdata.git] / Aircraft / Cap10C / Nasal / cap10c.nas
1 #### Fichier crée par Flavien Blanc (Fly) ####
2 #### equipe-flightgear.forumactif.com ####
3
4 var config_dlg = gui.Dialog.new("/sim/gui/dialogs/config/dialog", getprop("/sim/aircraft-dir")~"/Dialogs/config.xml");
5
6 ##########################################
7 # Flaps overwrite
8 ##########################################
9
10 controls.flapsDown = func(step) {
11     if(step == 0) return;
12     if(props.globals.getNode("/sim/flaps") != nil) {
13         controls.stepProps("/controls/flight/flaps-cmd", "/sim/flaps", step);
14         return;
15     }
16     # Hard-coded flaps movement in 3 equal steps:
17     var val = 0.3333334 * step + getprop("/controls/flight/flaps-cmd");
18     setprop("/controls/flight/flaps-cmd", val > 1 ? 1 : val < 0 ? 0 : val);
19 }
20
21 ##########################################
22 # Horameter / Chrono
23 ##########################################
24
25 props.globals.initNode("/instrumentation/horameter/elapsed-time", 0, "INT");
26 props.globals.initNode("/instrumentation/chrono/elapsed-time", 0, "INT");
27
28 var chrono = aircraft.timer.new("/instrumentation/chrono/elapsed-time", 1);
29 var horameter = aircraft.timer.new("/instrumentation/horameter/elapsed-time", 1, 1);
30
31 setlistener("/instrumentation/horameter/running", func(running){
32   if(running.getBoolValue()){
33     horameter.start();
34   }else{
35     horameter.stop();
36   }
37 });
38
39
40 var floor = func(v) v < 0.0 ? -int(-v) - 1 : int(v);
41 var elapsedTime = 0;
42 var formatedTime = "";
43 var sec = 0;
44 var min = 0;
45 var hrs = 0;
46
47 var timeFormat = func{
48
49   elapsedTime = getprop("/instrumentation/chrono/elapsed-time");
50
51   hrs = floor(elapsedTime/3600);
52   min = floor(elapsedTime/60);
53   sec = elapsedTime;
54
55   formatedTime = sprintf("%02d:%02d:%02d", hrs, min-(60*hrs), sec-(60*min));
56   setprop("/instrumentation/chrono/formated-time", formatedTime);
57
58 }
59
60 ##########################################
61 # Mixture/Throttle controlled by mouse
62 ##########################################
63
64 var mousex =0;
65 var msx = 0;
66 var msxa = 0;
67 var mousey = 0;
68 var msy = 0;
69 var msya=0;
70
71 var mouse_accel=func{
72   msx=getprop("devices/status/mice/mouse/x") or 0;
73   mousex=msx-msxa;
74   mousex*=0.5;
75   msxa=msx;
76   msy=getprop("devices/status/mice/mouse/y") or 0;
77   mousey=msya-msy;
78   mousey*=0.5;
79   msya=msy;
80 #  settimer(mouse_accel, 0);
81 }
82
83 var set_levers = func(type,num,min,max){
84   var ctrl=[];
85   var cpld=-1;
86   if(type == "throttle"){
87     ctrl = ["controls/engines/engine[0]/throttle","controls/engines/engine[1]/throttle"];
88     cpld = "controls/throttle-coupled";
89   }elsif(type == "prop"){
90     ctrl = ["controls/engines/engine[0]/propeller-pitch","controls/engines/engine[1]/propeller-pitch"];
91     cpld = "controls/prop-coupled";
92   }elsif(type == "mixture"){
93     ctrl = ["controls/engines/engine[0]/mixture","controls/engines/engine[1]/mixture"];
94     cpld ="controls/mixture-coupled";
95   }
96
97   var amnt =mousey* getprop("controls/movement-scale");
98   var ttl = getprop(ctrl[num]) + amnt;
99   if(ttl > max) ttl = max;
100   if(ttl<min)ttl=min;
101   setprop(ctrl[num],ttl);
102   if(getprop(cpld))setprop(ctrl[1-num],ttl);
103 }
104
105 ##########################################
106 # Ground Detection
107 ##########################################
108
109 # Do terrain modelling ourselves.
110 setprop("sim/fdm/surface/override-level", 1);
111
112 var terrain_survol = func {
113   var lat = getprop("/position/latitude-deg");
114   var lon = getprop("/position/longitude-deg");
115
116   var info = geodinfo(lat, lon);
117   if (info != nil) {
118     if (info[1] != nil){
119       if (info[1].solid !=nil)
120         setprop("/environment/terrain-type",info[1].solid);
121       if (info[1].load_resistance !=nil)
122         setprop("/environment/terrain-load-resistance",info[1].load_resistance);
123       if (info[1].friction_factor !=nil)
124         setprop("/environment/terrain-friction-factor",info[1].friction_factor);
125       if (info[1].bumpiness !=nil)
126         setprop("/environment/terrain-bumpiness",info[1].bumpiness);
127       if (info[1].rolling_friction !=nil)
128         setprop("/environment/terrain-rolling-friction",info[1].rolling_friction);
129       if (info[1].names !=nil)
130         setprop("/environment/terrain-names",info[1].names[0]);
131     }         
132   }else{
133     setprop("/environment/terrain",1);
134     setprop("/environment/terrain-load-resistance",1e+30);
135     setprop("/environment/terrain-friction-factor",1.05);
136     setprop("/environment/terrain-bumpiness",0);
137     setprop("/environment/terrain-rolling-friction",0.02);
138   }
139
140   if(!getprop("sim/freeze/replay-state") and !getprop("/environment/terrain-type") and getprop("/position/altitude-agl-ft") < 3.0){
141     setprop("sim/messages/copilot", "You are on water !");
142     setprop("sim/freeze/clock", 1);
143     setprop("sim/freeze/master", 1);
144     setprop("sim/crashed", 1);
145   }
146 #  settimer(terrain_survol, 0);
147 }
148
149 ##############################################
150 #########       KY97A FORMAT       ###########
151 ##############################################
152
153 var formatCommSelectedFreq = func{
154   var selectedFreq = int(getprop("instrumentation/comm[0]/frequencies/selected-mhz")*100);
155   setprop("instrumentation/comm[0]/frequencies/selected-mhz-fmt", selectedFreq/100);
156 }
157
158 var formatCommStandbyFreq = func{
159   var standbyFreq = int(getprop("instrumentation/comm[0]/frequencies/standby-mhz")*100);
160   setprop("instrumentation/comm[0]/frequencies/standby-mhz-fmt", standbyFreq/100);
161 }
162
163 setlistener("instrumentation/comm[0]/frequencies/selected-mhz", func(){
164   formatCommSelectedFreq();
165 });
166
167 setlistener("instrumentation/comm[0]/frequencies/standby-mhz", func(){
168   formatCommStandbyFreq();
169 });
170
171 ##############################################
172 ######### AUTOSTART / AUTOSHUTDOWN ###########
173 ##############################################
174
175 setlistener("/sim/model/start-idling", func(idle){
176     var run= idle.getBoolValue();
177     if(run){
178     Startup();
179     }else{
180     Shutdown();
181     }
182 },0,0);
183
184 var Startup = func{
185   setprop("controls/fuel/selected-tank", 1);
186   setprop("controls/fuel/tank/boost-pump", 1);
187   setprop("controls/engines/engine[0]/master-alt",1);
188   setprop("/controls/engines/engine[0]/magnetos",3);
189   setprop("controls/engines/engine[0]/mixture",1);
190   setprop("/controls/gear/brake-parking",1);
191 #  setprop("/controls/lighting/instruments-norm",1);
192   setprop("/instrumentation/comm[0]/power-btn",1);
193   setprop("/instrumentation/comm[0]/volume",1);
194   setprop("/instrumentation/nav[0]/power-btn",1);  
195   setprop("/instrumentation/nav[0]/volume",1);
196   setprop("/instrumentation/adf[0]/power-btn",1);
197   setprop("/instrumentation/adf[0]/volume",1);
198   setprop("/instrumentation/adf[0]/volume-norm",1);
199   setprop("controls/electric/battery-switch",1);
200   setprop("controls/switches/ai-switch",1);
201   setprop("controls/switches/hi-switch",1);
202   setprop("controls/lighting/strobe-lights", 1);
203   setprop("controls/lighting/nav-lights", 1);
204   setprop("sim/messages/copilot", "Now press \"s\" to start engine");
205 }
206
207 var Shutdown = func{
208   setprop("controls/fuel/selected-tank", 0);
209   setprop("controls/engines/engine[0]/master-alt",0);
210   setprop("/controls/engines/engine[0]/magnetos",0);
211   setprop("controls/engines/engine[0]/mixture",1);
212   setprop("/engines/engine[0]/rpm",0);
213   setprop("/engines/engine[0]/running",0);
214   setprop("/controls/gear/brake-parking",1);
215 #  setprop("/controls/lighting/instruments-norm",0);
216   setprop("/instrumentation/comm[0]/power-btn",0);
217   setprop("/instrumentation/comm[0]/volume",0);
218   setprop("/instrumentation/nav[0]/power-btn",0);
219   setprop("/instrumentation/nav[0]/volume",0);
220   setprop("/instrumentation/adf[0]/power-btn",0);
221   setprop("/instrumentation/adf[0]/volume",0);
222   setprop("/instrumentation/adf[0]/volume-norm",0);
223   setprop("controls/electric/battery-switch",0);
224   setprop("controls/fuel/tank/boost-pump", 0);
225   setprop("controls/switches/ai-switch",0);
226   setprop("controls/switches/hi-switch",0);
227   setprop("controls/lighting/strobe-lights", 0);
228   setprop("controls/lighting/nav-lights", 0);
229   setprop("sim/messages/copilot", "Engine is stopped");
230 }
231
232 ############################################
233 # Global loop function
234 # If you need to run nasal as loop, add it in this function
235 ############################################
236 global_system = func{
237
238   if(getprop("/systems/electrical/outputs/starter") > 18){
239     setprop("/controls/engines/engine[0]/starter",1);
240   }else{
241     setprop("/controls/engines/engine[0]/starter",0);
242   }
243
244 #  if(getprop("fdm/jsbsim/propulsion/tank[0]/priority")){
245 #    setprop("fdm/jsbsim/propulsion/tank[1]/priority", 0);
246 #  }
247
248   if(getprop("/systems/electrical/volts") > 3){
249     setprop("/instrumentation/horameter/running", 1);
250     interpolate("consumables/fuel/tank[0]/level-gal_us-pos", getprop("consumables/fuel/tank[0]/level-gal_us"), 0.5);
251     interpolate("consumables/fuel/tank[1]/level-gal_us-pos", getprop("consumables/fuel/tank[1]/level-gal_us"), 0.5);
252     interpolate("fdm/jsbsim/propulsion/engine/oil-pressure-psi-pos", getprop("fdm/jsbsim/propulsion/engine/oil-pressure-psi"), 0.5);
253     interpolate("fdm/jsbsim/propulsion/engine/oil-temperature-degF-pos", getprop("fdm/jsbsim/propulsion/engine/oil-temperature-degF"), 0.5);
254   }else{
255     setprop("/instrumentation/horameter/running", 0);
256     interpolate("consumables/fuel/tank[0]/level-gal_us-pos", 0, 0.5);
257     interpolate("consumables/fuel/tank[1]/level-gal_us-pos", 0, 0.5);
258     interpolate("fdm/jsbsim/propulsion/engine/oil-pressure-psi-pos", 0, 0.5);
259     interpolate("fdm/jsbsim/propulsion/engine/oil-temperature-degF-pos", 0, 0.5);
260   }
261
262   if(getprop("/systems/electrical/outputs/ai") > 6){
263     setprop("/instrumentation/attitude-indicator/spin",10);
264   }else{
265     setprop("/instrumentation/attitude-indicator/spin",0);
266   }
267
268   if(getprop("/systems/electrical/outputs/hi") > 6){
269     setprop("/instrumentation/heading-indicator/spin",10);
270   }else{
271     setprop("/instrumentation/heading-indicator/spin",0);
272   }
273
274   if(getprop("/systems/electrical/volts") > 12){
275     controls.stepProps("/controls/flight/flaps", "/sim/flaps", getprop("/controls/flight/flaps-cmd"));
276   }
277
278
279   mouse_accel();
280   terrain_survol();
281   timeFormat();
282   cap10c.physics();
283
284   settimer(global_system, 0);
285
286 }
287
288 ############################################
289 # Upside down system
290 ############################################
291 var timeOfNegatifG = 0;
292 upsideDown_system = func{
293
294   if(getprop("fdm/jsbsim/accelerations/Nz") < -0.5){
295     timeOfNegatifG += 1;
296     if(timeOfNegatifG > 4){
297       if(getprop("fdm/jsbsim/propulsion/tank[1]/priority") > getprop("fdm/jsbsim/propulsion/tank[0]/priority")){
298         setprop("fdm/jsbsim/propulsion/tank[1]/priority", 0);
299       }
300       timeOfNegatifG = 0;
301     }
302   }else{
303     timeOfNegatifG = 0;
304   }
305   settimer(upsideDown_system, 1);
306 }
307
308 ##########################################
309 # SetListerner must be at the end of this file
310 ##########################################
311 setlistener("/sim/signals/fdm-initialized", func{
312   setprop("/environment/terrain-type",1);
313   setprop("/environment/terrain-load-resistance",1e+30);
314   setprop("/environment/terrain-friction-factor",1.05);
315   setprop("/environment/terrain-bumpiness",0);
316   setprop("/environment/terrain-rolling-friction",0.02);
317   setprop("/instrumentation/nav[0]/power-btn",0); #force OFF
318   setprop("/instrumentation/adf[0]/power-btn",0);
319   setprop("/instrumentation/adf[0]/volume",0);
320   setprop("/instrumentation/adf[0]/volume-norm",0);
321   setprop("/controls/lighting/nav-lights", 0);
322   setprop("/controls/lighting/landing-lights", 0);
323   setprop("/controls/electric/battery-switch", 0);
324   setprop("/instrumentation/ertical-speed-indicator/indicated-speed-fpm", 0);
325
326   if(getprop("sim/rendering/rembrandt/enabled") == nil){
327     props.globals.initNode("sim/rendering/rembrandt/enabled", 0, "BOOL");
328     print("Rembrandt no available");
329   }
330
331   formatCommSelectedFreq();
332   formatCommStandbyFreq();
333
334 });
335
336 var nasalInit = setlistener("/sim/signals/fdm-initialized", func{
337   settimer(global_system, 2);
338   settimer(upsideDown_system, 2);
339   removelistener(nasalInit);
340 });