Indicate these models want to use their own terrein handling code, until they switch...
[fg:toms-fgdata.git] / Aircraft / MiG-15 / Nasal / MiG-15bis.nas
1 autotakeoff = func {
2
3 # The ato_start function is only executed once but the ato_mode and
4 # ato_spddep functions will re-schedule themselves until
5 # /autopilot/locks/auto-take-off is disabled.
6
7 #  print("autotakeoff called");
8   if(getprop("autopilot/locks/auto-take-off") == "enabled") {
9     ato_start();      # Initialisation.
10     ato_main();       # Main loop.
11   }
12 }
13 #--------------------------------------------------------------------
14 ato_start = func {
15
16   if(getprop("autopilot/settings/target-gr-heading-deg") < -999) {
17
18     setprop("controls/flight/flaps", 0.0);
19     setprop("controls/flight/spoilers", 0.0);
20     setprop("controls/gear/brake-left", 0.0);
21     setprop("controls/gear/brake-right", 0.0);
22     setprop("controls/gear/brake-parking", 0.0);
23
24     hdgdeg = getprop("orientation/heading-deg");
25     tgt_gr_pitch_deg= getprop("autopilot/settings/target-gr-pitch-deg");
26     setprop("autopilot/settings/target-gr-heading-deg", hdgdeg);
27     setprop("autopilot/settings/true-heading-deg", hdgdeg);
28     setprop("autopilot/settings/target-speed-kt", 350);
29     setprop("autopilot/internal/target-pitch-deg-unfiltered", tgt_gr_pitch_deg);
30     setprop("autopilot/locks/speed", "speed-with-throttle");
31     setprop("autopilot/locks/rudder-control", "gr-rudder-hold");
32     setprop("autopilot/locks/altitude", "take-off");
33     setprop("autopilot/internal/target-roll-deg-unfiltered", 0);
34     setprop("autopilot/locks/auto-take-off", "engaged");
35   }
36 }
37 #--------------------------------------------------------------------
38 ato_main = func {
39
40   as_kt= getprop("velocities/airspeed-kt");
41   tgt_gr_rot_spd_kt= getprop("autopilot/settings/target-gr-rot-spd-kt");
42   tgt_to_p_deg= getprop("autopilot/settings/target-to-pitch-deg");
43
44   if(as_kt < tgt_gr_rot_spd_kt) {
45     # Do nothing
46   } else {
47     if(as_kt < 145) {
48       interpolate("controls/flight/elevator", -1.0, 2);
49       interpolate("autopilot/internal/target-pitch-deg-unfiltered", tgt_to_p_deg, 2);
50       setprop("autopilot/locks/heading", "wing-leveler");
51     } else {
52       if(as_kt < 160) {
53         interpolate("controls/flight/elevator", 0.0, 15);
54       } else {
55         if(as_kt < 170) {
56           setprop("controls/gear/gear-down", "false");
57           setprop("autopilot/locks/rudder-control", "");
58           setprop("controls/flight/flaps", 0.0);
59           interpolate("controls/flight/rudder", 0, 10);
60         } else {
61           if(as_kt > 200) {
62             setprop("controls/flight/flaps", 0.0);
63             setprop("autopilot/locks/heading", "true-heading-hold");
64             setprop("autopilot/locks/speed", "mach-with-throttle");
65             setprop("autopilot/locks/altitude", "mach-climb");
66             setprop("autopilot/locks/auto-take-off", "disabled");
67             setprop("autopilot/locks/auto-landing", "enabled");
68           }
69         }
70       }
71     }
72   }
73
74   # Re-schedule the next loop
75   if(getprop("autopilot/locks/auto-take-off") == "engaged") {
76     settimer(ato_main, 0.2);
77   }
78 }
79 #--------------------------------------------------------------------
80 autoland = func {
81   if(getprop("autopilot/locks/auto-landing") == "enabled") {
82     atl_start();      # Initialisation.
83     atl_main();       # Main loop.
84   }
85 }
86 #--------------------------------------------------------------------
87 atl_start = func {
88   setprop("autopilot/locks/auto-landing", "engaged");
89 }
90 #--------------------------------------------------------------------
91
92 atl_main = func {
93   # Get the agl, kias, vfps & heading.
94   agl = getprop("position/altitude-agl-ft");
95   hdgdeg = getprop("orientation/heading-deg");
96
97   if(agl > 200) {
98     # Glide Slope phase.
99     atl_heading();
100     atl_spddep();
101     atl_glideslope();
102   } else {
103     # Touch Down Phase
104     atl_touchdown();
105   }
106   # Re-schedule the next loop if the Landing function is enabled.
107   if(getprop("autopilot/locks/auto-landing") == "engaged") {
108     settimer(atl_main, 0.2);
109   }
110 }
111 #--------------------------------------------------------------------
112 atl_glideslope= func {
113   # This script handles the Glide Slope phase
114   ap_alt_lock= getprop("autopilot/locks/altitude");
115   gsvfps = getprop("instrumentation/nav[0]/gs-rate-of-climb");
116   curr_vfps = getprop("velocities/vertical-speed-fps");
117
118   if(ap_alt_lock != "vfps-hold") {
119     setprop("autopilot/settings/target-climb-rate-fps", curr_vfps);
120     interpolate("autopilot/settings/target-climb-rate-fps", gsvfps, 4);
121     setprop("autopilot/locks/altitude", "vfps-hold");
122   } else {
123     interpolate("autopilot/settings/target-climb-rate-fps", gsvfps, 1);
124   }
125 }
126 #--------------------------------------------------------------------
127 atl_spddep = func {
128   # This script handles the speed dependent actions.
129
130   # Set the target speed to 200 kt.
131   setprop("autopilot/locks/speed", "speed-with-throttle");
132   if(getprop("autopilot/settings/target-speed-kt") > 150) {
133     setprop("autopilot/settings/target-speed-kt", 150);
134   }
135
136   gsvfps = getprop("instrumentation/nav[0]/gs-rate-of-climb");
137   kias = getprop("velocities/airspeed-kt");
138   if(kias < 160) {
139     setprop("controls/flight/flaps", 1.0);
140     setprop("autopilot/locks/approach-aoa-hold", "engaged");
141     setprop("controls/flight/spoilers", 0.0);
142   } else {
143     if(kias < 170) {
144       setprop("controls/gear/gear-down", "true");
145     } else {
146       if(kias < 180) {
147         setprop("controls/flight/flaps", 0.36);
148       } else {
149         if(kias < 240) {
150 #          setprop("controls/gear/gear-down", "true");
151         } else {
152           if(getprop("velocities/vertical-speed-fps") < -10) {
153             if(gsvfps < 0) {
154               setprop("autopilot/settings/target-speed-kt", 150);
155             }
156           }
157         }
158       }
159     }
160   }
161 }
162 #--------------------------------------------------------------------
163 atl_touchdown = func {
164   # Touch Down Phase
165   agl = getprop("position/altitude-agl-ft");
166
167   setprop("autopilot/locks/heading", "");
168
169   if(agl > 80) {
170     # Do nothing.
171   } else {
172     if(agl > 20) {
173       interpolate("autopilot/settings/target-climb-rate-fps", -6, 4);
174     } else {
175       if(agl > 4) {
176         interpolate("autopilot/settings/target-climb-rate-fps", -3, 2);
177       } else {
178         if(agl > 1) {
179           interpolate("autopilot/settings/target-climb-rate-fps", -1, 1);
180           setprop("autopilot/locks/approach-aoa-hold", "off");
181         } else {
182           if(agl > 0.1) {
183             setprop("autopilot/locks/speed", "");
184             setprop("controls/engines/engine[0]/throttle", 0);
185           } else {
186             setprop("controls/gear/brake-left", 0.4);
187             setprop("controls/gear/brake-right", 0.4);
188             setprop("autopilot/settings/target-gr-heading-deg", -999.9);
189             setprop("autopilot/locks/auto-landing", "disabled");
190             setprop("autopilot/locks/auto-take-off", "enabled");
191             setprop("autopilot/locks/altitude", "off");
192             interpolate("controls/flight/elevator-trim", 0, 10.0);
193           }
194         }
195       }
196     }
197   }
198 }
199 #--------------------------------------------------------------------
200 atl_heading = func {
201   # This script handles heading dependent actions.
202   curr_kias = getprop("velocities/airspeed-kt");
203 #  hdnddf = getprop("autopilot/internal/heading-needle-deflection");
204   hdnddf = getprop("instrumentation/nav[0]/heading-needle-deflection");
205   if(curr_kias > 200) {
206     setprop("autopilot/locks/heading", "nav1-hold");
207   } else {
208     if(hdnddf < 4) {
209       if(hdnddf > -4) {
210         setprop("autopilot/locks/heading", "nav1-hold-fa");
211       } else {
212         setprop("autopilot/locks/heading", "nav1-hold");
213       }
214     }
215   }
216 }
217 #--------------------------------------------------------------------
218 toggle_traj_mkr = func {
219   if(getprop("ai/submodels/trajectory-markers") == nil) {
220     setprop("ai/submodels/trajectory-markers", 0);
221   }
222   if(getprop("ai/submodels/trajectory-markers") < 1) {
223     setprop("ai/submodels/trajectory-markers", 1);
224   } else {
225     setprop("ai/submodels/trajectory-markers", 0);
226   }
227 }
228 #--------------------------------------------------------------------
229 initialise_drop_view_pos = func {
230   eyelatdeg = getprop("position/latitude-deg");
231   eyelondeg = getprop("position/longitude-deg");
232   eyealtft = getprop("position/altitude-ft") + 20;
233   setprop("sim/view[100]/latitude-deg", eyelatdeg);
234   setprop("sim/view[100]/longitude-deg", eyelondeg);
235   setprop("sim/view[100]/altitude-ft", eyealtft);
236 }
237 #--------------------------------------------------------------------
238 update_drop_view_pos = func {
239   eyelatdeg = getprop("position/latitude-deg");
240   eyelondeg = getprop("position/longitude-deg");
241   eyealtft = getprop("position/altitude-ft") + 20;
242   interpolate("sim/view[100]/latitude-deg", eyelatdeg, 5);
243   interpolate("sim/view[100]/longitude-deg", eyelondeg, 5);
244   interpolate("sim/view[100]/altitude-ft", eyealtft, 5);
245 }
246 #--------------------------------------------------------------------
247 fire_cannon = func 
248         {
249                 n37_count = getprop("ai/submodels/submodel[1]/count");
250                 ns23_inner_count = getprop("ai/submodels/submodel[3]/count");
251                 ns23_outer_count = getprop("ai/submodels/submodel[5]/count");
252                 if (
253                         (n37_count==nil) 
254                         or (ns23_inner_count==nil)
255                         or (ns23_outer_count==nil)
256                 )
257                 {
258                         return (0);
259                 }
260                 if (n37_count>0) 
261                 {
262                         setprop("sounds/cannon/big-on", 1);
263                 }
264                 if (
265                         (ns23_inner_count>0) 
266                         or (ns23_outer_count>0)
267                 )
268                 {
269                         setprop("sounds/cannon/small-on", 1);
270                 }
271                 if (n37_count>0) 
272                 {
273                         n37_count=n37_count-1;
274                         setprop("ai/submodels/N-37", 1);
275                 }
276                 if (ns23_inner_count>0) 
277                 {
278                         ns23_inner_count=ns23_inner_count-1;
279                         setprop("ai/submodels/NS-23-I", 1);
280                 }
281                 if (ns23_outer_count>0) 
282                 {
283                         ns23_outer_count=ns23_outer_count-1;
284                         setprop("ai/submodels/NS-23-O", 1);
285                 }
286                 setprop("fdm/jsbsim/shells/n37", n37_count);
287                 setprop("fdm/jsbsim/shells/n23-inner", ns23_inner_count);
288                 setprop("fdm/jsbsim/shells/n23-outer", ns23_outer_count);
289                 return (1);
290         }
291
292 cfire_cannon = func 
293         {
294                 setprop("ai/submodels/N-37", 0);
295                 setprop("ai/submodels/NS-23-I", 0);
296                 setprop("ai/submodels/NS-23-O", 0);
297                 setprop("sounds/cannon/big-on", 0);
298                 setprop("sounds/cannon/small-on", 0);
299         }
300
301 cannon_shells_weght=func
302         {
303
304                 n37_count = getprop("ai/submodels/submodel[1]/count");
305                 ns23_inner_count = getprop("ai/submodels/submodel[3]/count");
306                 ns23_outer_count = getprop("ai/submodels/submodel[5]/count");
307                 if (
308                         (n37_count==nil) 
309                         or (ns23_inner_count==nil)
310                         or (ns23_outer_count==nil)
311                 )
312                 {
313                         return (settimer (cannon_shells_weght, 0.1));
314                 }
315                 shell_weight_as_fuel=(n37_count*0.735+(ns23_inner_count+ns23_outer_count)*0.2)/2.76;
316                 setprop("consumables/fuel/tank[5]/level-gal_us", shell_weight_as_fuel);
317                 settimer (cannon_shells_weght, 0.1);
318         }
319
320 init_cannon_shells_weght=func
321         {
322                 setprop("consumables/fuel/tank[5]/selected", 0);
323         }
324
325 init_cannon_shells_weght();
326
327 cannon_shells_weght();
328
329 #--------------------------------------------------------------------
330 controls.trigger = func(b) { b ? fire_cannon() : cfire_cannon() }
331 #--------------------------------------------------------------------
332 ap_common_elevator_monitor = func {
333   curr_ah_state = getprop("autopilot/locks/altitude");
334
335   if(curr_ah_state == "altitude-hold") {
336     setprop("autopilot/locks/common-elevator-control", "engaged");
337   } else {
338     if(curr_ah_state == "agl-hold") {
339       setprop("autopilot/locks/common-elevator-conctrol", "engaged");
340     } else {
341       if(curr_ah_state == "mach-climb") {
342         setprop("autopilot/locks/common-elevator-control", "engaged");
343       } else {
344         if(curr_ah_state == "vfps-hold") {
345           setprop("autopilot/locks/common-elevator-control", "engaged");
346         } else {
347           if(curr_ah_state == "take-off") {
348             setprop("autopilot/locks/common-elevator-control", "engaged");
349           } else {
350             setprop("autopilot/locks/common-elevator-control", "off");
351           }
352         }
353       }
354     }
355   } 
356   settimer(ap_common_elevator_monitor, 0.5);
357 }
358 #--------------------------------------------------------------------
359 ap_common_aileron_monitor = func {
360   curr_hd_state = getprop("autopilot/locks/heading");
361
362   if(curr_hd_state == "wing-leveler") {
363     setprop("autopilot/locks/common-aileron-control", "engaged");
364     setprop("autopilot/internal/target-roll-deg-unfiltered", 0);
365   } else {
366     if(curr_hd_state == "true-heading-hold") {
367       setprop("autopilot/locks/common-aileron-control", "engaged");
368     } else {
369       if(curr_hd_state == "dg-heading-hold") {
370         setprop("autopilot/locks/common-aileron-control", "engaged");
371       } else {
372         if(curr_hd_state == "nav1-hold") {
373           setprop("autopilot/locks/common-aileron-control", "engaged");
374         } else {
375           if(curr_hd_state == "nav1-hold-fa") {
376             setprop("autopilot/locks/common-aileron-control", "engaged");
377           } else {
378             setprop("autopilot/locks/common-aileron-control", "off");
379           }
380         }
381       }
382     }
383   } 
384   settimer(ap_common_aileron_monitor, 0.5);
385 }
386 #--------------------------------------------------------------------
387 #Set common one
388 setprop("one", 1);
389
390 #--------------------------------------------------------------------
391 start_up = func {
392   settimer(initialise_drop_view_pos, 5);
393   settimer(ap_common_elevator_monitor, 0.5);
394   settimer(ap_common_aileron_monitor, 0.5);
395 }
396
397 #---------------------------------------------------------------------------
398 #Common switch functions
399
400 #Common click sound to responce
401 clicksound = func
402         {
403                 setprop("sounds/click/on", 1);
404                 settimer(clickoff, 0.1);
405         }
406
407 clickoff = func
408         {
409                 setprop("sounds/click/on", 0);
410         }
411
412 setprop("sounds/click/on", 0);
413
414 #Common switch move function
415 switchmove = func (switch_name, property_name)
416         {
417                 switch_pos=getprop(switch_name~"/switch-pos-norm");
418                 interpolated_pos=getprop(switch_name~"/switch-pos-inter");
419                 set_pos=getprop(switch_name~"/set-pos");
420                 swap_pos=getprop(switch_name~"/swap-pos");
421                 move_count=getprop(switch_name~"/move-count");
422                 if (
423                         (switch_pos == nil)
424                         or (interpolated_pos == nil)
425                         or (set_pos == nil)
426                         or (swap_pos == nil)
427                         or (move_count==nil)
428                 )
429                 {
430                         return (0); 
431                 }
432                 if (set_pos==swap_pos)
433                 {
434                         swap_pos=abs(1-set_pos);
435                         setprop(switch_name~"/swap-pos", swap_pos);
436                 }
437                 if (switch_pos!=set_pos)
438                 {
439                         way_to=abs(set_pos-switch_pos)/(set_pos-switch_pos);
440                         switch_pos=switch_pos+0.3*way_to;
441                         if (((way_to>0) and (switch_pos>set_pos)) or ((way_to<0) and (switch_pos<set_pos)))
442                         {
443                                 setprop(switch_name~"/switch-pos-norm", set_pos);
444                                 interpolate(switch_name~"/switch-pos-inter", set_pos, 0);
445                         }
446                         else
447                         {
448                                 setprop(switch_name~"/switch-pos-norm", switch_pos);
449                                 interpolate(switch_name~"/switch-pos-inter", switch_pos, 0.09);
450                         }
451                         move_count=move_count+1;
452                         setprop(switch_name~"/move-count", move_count);
453                 }
454                 else
455                 {
456                         if (move_count>0)
457                         {
458                                 setprop(switch_name~"/move-count", 0);
459                                 clicksound();
460                                 if (switch_pos==0)
461                                 {
462                                         setprop(property_name, 0);
463                                 }
464                                 if (switch_pos==1)
465                                 {
466                                         setprop(property_name, 1);
467                                 }
468                                 if (interpolated_pos!=switch_pos)
469                                 {
470                                         interpolate(switch_name~"/switch-pos-inter", switch_pos, 0);
471                                 }                               
472                         }
473                 }
474                 return (1); 
475         }
476
477 #Timed switch move to move switch in time, call timer time must be 0.1
478 #Sound property to click or not to click
479 timedswitchmove = func (switch_name, switch_time, property_name, sound_on)
480         {
481                 switch_pos=getprop(switch_name~"/switch-pos-norm");
482                 prev_pos=getprop(switch_name~"/switch-pos-prev");
483                 set_pos=getprop(switch_name~"/set-pos");
484                 swap_pos=getprop(switch_name~"/swap-pos");
485                 move_count=getprop(switch_name~"/move-count");
486                 if (
487                         (switch_pos == nil)
488                         or (set_pos == nil)
489                         or (prev_pos == nil)
490                         or (swap_pos == nil)
491                         or (move_count==nil)
492                 )
493                 {
494                         return (0); 
495                 }
496                 if (set_pos==swap_pos)
497                 {
498                         swap_pos=abs(1-set_pos);
499                         setprop(switch_name~"/swap-pos", swap_pos);
500                 }
501                 if (switch_pos!=set_pos)
502                 {
503                         way_to=abs(set_pos-switch_pos)/(set_pos-switch_pos);
504                         switch_pos=switch_pos+0.1/switch_time*way_to;
505                         if (((way_to>0) and (switch_pos>set_pos)) or ((way_to<0) and (switch_pos<set_pos)))
506                         {
507                                 switch_pos=set_pos;
508                                 setprop(switch_name~"/switch-pos-norm", set_pos);
509                                 interpolate(switch_name~"/switch-pos-inter", set_pos, 0);
510                         }
511                         else
512                         {
513                                 setprop(switch_name~"/switch-pos-norm", switch_pos);
514                                 interpolate(switch_name~"/switch-pos-inter", switch_pos, 0.09);
515                         }
516                         move_count=move_count+1;
517                         setprop(switch_name~"/move-count", move_count);
518                 }
519                 if (switch_pos==set_pos)
520                 {
521                         if (move_count>0)
522                         {
523                                 setprop(switch_name~"/move-count", 0);
524                                 if (sound_on==1)
525                                 {
526                                         clicksound();
527                                 }
528                                 if (switch_pos==0)
529                                 {
530                                         setprop(property_name, 0);
531                                 }
532                                 if (switch_pos==1)
533                                 {
534                                         setprop(property_name, 1);
535                                 }
536                         }
537                 }
538                 return (1); 
539         }
540
541 #Common switch initialisation function
542 switchinit = func (switch_name, init_state, property_name)
543         {
544                 setprop(switch_name~"/switch-pos-prev", init_state);
545                 setprop(switch_name~"/switch-pos-norm", init_state);
546                 setprop(switch_name~"/switch-pos-inter", init_state);
547                 setprop(switch_name~"/set-pos", init_state);
548                 setprop(switch_name~"/swap-pos", abs(1-init_state));
549                 setprop(switch_name~"/move-count", 0);
550                 setprop(property_name, init_state);
551         }
552
553 #Common switch swap function
554 switchswap = func (switch_name)
555         {
556                 set_pos=getprop(switch_name~"/set-pos");
557                 swap_pos=getprop(switch_name~"/swap-pos");
558                 switch_pos=getprop(switch_name~"/switch-pos-norm");
559                 if ((set_pos == nil) or (swap_pos == nil) or (switch_pos==nil))
560                 {
561                         return (0); 
562                 }
563                 setprop(switch_name~"/switch-pos-norm", switch_pos);
564                 interpolate(switch_name~"/switch-pos-inter", switch_pos, 0);
565                 tempint=set_pos;
566                 set_pos=swap_pos;
567                 swap_pos=tempint;
568                 setprop(switch_name~"/set-pos", set_pos);
569                 setprop(switch_name~"/swap-pos", swap_pos);
570                 setprop(switch_name~"/move-count", 0);
571                 return (1); 
572         }
573
574 #Common switch back function
575 switchback = func (switch_name)
576         {
577                 set_pos=getprop(switch_name~"/set-pos");
578                 swap_pos=getprop(switch_name~"/swap-pos");
579                 switch_pos=getprop(switch_name~"/switch-pos-norm");
580                 if ((set_pos == nil) or (swap_pos == nil) or (switch_pos==nil))
581                 {
582                         return (0); 
583                 }
584                 if (abs(switch_pos-set_pos)>abs(switch_pos-swap_pos))
585                         switchswap(switch_name);
586                 return (1); 
587         }
588
589 #Common switch from propery dependance function
590 #needed then  property controlled elsethere
591 switchfeedback = func (switch_name, property_name)
592         {
593                 switch_pos=getprop(switch_name~"/switch-pos-norm");
594                 set_pos=getprop(switch_name~"/set-pos");
595                 swap_pos=getprop(switch_name~"/swap-pos");
596                 property=getprop(property_name);
597                 if ((switch_pos == nil) or (set_pos == nil) or (swap_pos == nil) or (property==nil))
598                 {
599                         return (0); 
600                 }
601                 if ((switch_pos==set_pos) and (swap_pos==(1-abs(set_pos))) and  (property==swap_pos))
602                 {
603                         switchswap(switch_name);
604                 }
605                 return (1); 
606         }
607
608 #smart interpolation, garantee property get to value in time
609 smartinterpolation = func (property_name, value, time, counts)
610         {
611                 current_counts=getprop(property_name~"/counts");
612                 if ((current_counts==0) or (current_counts==nil))
613                 {
614                         interpolate(property_name~"/value", value, time);
615                         setprop(property_name~"/counts", 1);
616                 }
617                 else
618                 {
619                         if (current_counts==counts)
620                         {
621                                 interpolate(property_name, value, 0);
622                                 setprop(property_name~"/counts", 0);
623                         }
624                         else
625                         {
626                                 setprop(property_name~"/counts", current_counts+1);
627                         }
628                 }
629                 return (1); 
630         }
631
632 #Common bit swap function
633 bitswap = func (bit_name)
634         {
635                 set_pos=getprop(bit_name);
636                 if (!((set_pos==1) or (set_pos==0)))
637                 {
638                         return (0); 
639                 }
640                 else
641                 {
642                         swap_pos=1-abs(set_pos);
643                         setprop(bit_name, swap_pos);
644                         return (1); 
645                 }
646         }
647
648 #--------------------------------------------------------------------
649 #Init Controls
650 init_controls  = func
651 {
652         setprop("controls/gear/brake-parking", 1);
653         setprop("controls/gear/gear-down", 1);
654         setprop("controls/flight/aileron", 0);
655         setprop("controls/flight/elevator", 0);
656         setprop("controls/flight/rudder", 0);
657         setprop("controls/flight/flaps", 0);
658         setprop("controls/flight/speedbrake", 0);
659 }
660
661 init_controls();
662
663 #--------------------------------------------------------------------
664 #Init FDM
665 init_fdm  = func
666 {
667         setprop("fdm/jsbsim/fcs/throttle-cmd-norm", 0);
668         setprop("fdm/jsbsim/fcs/throttle-cmd-norm-real", 0.3);
669         setprop("fdm/jsbsim/fcs/throttle-pos-norm", 0.3);
670
671         setprop("fdm/jsbsim/fcs/pitch-trim-norm-real", 0);
672         setprop("fdm/jsbsim/fcs/elevator-trim-norm-real", 0);
673         setprop("fdm/jsbsim/fcs/elevator-cmd-norm", 0);
674         setprop("fdm/jsbsim/fcs/elevator-cmd-norm-real", 0);
675         setprop("fdm/jsbsim/fcs/elevator-pos-norm", 0);
676
677         setprop("fdm/jsbsim/fcs/aileron-boost", 0);
678         setprop("fdm/jsbsim/fcs/roll-trim-norm-real", 0);
679         setprop("fdm/jsbsim/fcs/roll-pos-norm", 0);
680
681         setprop("fdm/jsbsim/fcs/aileron-cmd-norm", 0);
682         setprop("fdm/jsbsim/fcs/aileron-cmd-norm-real", 0);
683         setprop("fdm/jsbsim/fcs/aileron-pos-norm", 0);
684
685         setprop("fdm/jsbsim/fcs/rudder-cmd-norm", 0);
686         setprop("fdm/jsbsim/fcs/rudder-cmd-norm-real", 0);
687         setprop("fdm/jsbsim/fcs/rudder-pos-norm", 0);
688
689         setprop("fdm/jsbsim/fcs/flap-cmd-norm", 0);
690         setprop("fdm/jsbsim/fcs/flap-cmd-norm-real", 0);
691         setprop("fdm/jsbsim/fcs/flap-pos-norm", 0);
692
693         setprop("fdm/jsbsim/gear/gear-cmd-norm", 1);
694         setprop("fdm/jsbsim/gear/gear-cmd-norm-real", 1);
695
696         setprop("fdm/jsbsim/gear/unit[0]/pos-norm", 1);
697         setprop("fdm/jsbsim/gear/unit[1]/pos-norm", 1);
698         setprop("fdm/jsbsim/gear/unit[2]/pos-norm", 1);
699
700         setprop("fdm/jsbsim/gear/unit[0]/pos-norm-real", 1);
701         setprop("fdm/jsbsim/gear/unit[1]/pos-norm-real", 1);
702         setprop("fdm/jsbsim/gear/unit[2]/pos-norm-real", 1);
703
704         setprop("fdm/jsbsim/gear/unit[0]/z-position", -1.459/0.0254);
705         setprop("fdm/jsbsim/gear/unit[1]/z-position", -1.332/0.0254);
706         setprop("fdm/jsbsim/gear/unit[2]/z-position", -1.332/0.0254);
707
708         setprop("fdm/jsbsim/gear/unit[0]/tored", 0);
709         setprop("fdm/jsbsim/gear/unit[1]/tored", 0);
710         setprop("fdm/jsbsim/gear/unit[2]/tored", 0);
711
712         setprop("fdm/jsbsim/gear/unit[0]/stuck", 0);
713         setprop("fdm/jsbsim/gear/unit[1]/stuck", 0);
714         setprop("fdm/jsbsim/gear/unit[2]/stuck", 0);
715
716         setprop("fdm/jsbsim/gear/unit[0]/break-type", "");
717         setprop("fdm/jsbsim/gear/unit[1]/break-type", "");
718         setprop("fdm/jsbsim/gear/unit[2]/break-type", "");
719
720 #       setprop("ai/submodels/gear-middle-drop", 0);
721 #       setprop("ai/submodels/gear-left-drop", 0);
722 #       setprop("ai/submodels/gear-right-drop", 0);
723
724         setprop("fdm/jsbsim/fcs/speedbrake-cmd-norm", 0);
725         setprop("fdm/jsbsim/fcs/speedbrake-cmd-norm-real", 0);
726         setprop("fdm/jsbsim/fcs/speedbrake-pos-norm", 0);
727         setprop("fdm/jsbsim/tanks/fastened", 0);
728 }
729
730 init_fdm();
731
732 #Init positions
733 #--------------------------------------------------------------------
734 init_positions  = func
735 {
736         setprop("surface-positions/elevator-pos-norm", 0);
737         setprop("surface-positions/left-aileron-pos-norm", 0);
738         setprop("surface-positions/right-aileron-pos-norm", 0);
739         setprop("surface-positions/rudder-pos-norm", 0);
740         setprop("surface-positions/flap-pos-norm", 0);
741         setprop("surface-positions/speedbrake-pos-norm", 0);
742
743         setprop("gear/gear[0]/wow", 0);
744         setprop("gear/gear[1]/wow", 0);
745         setprop("gear/gear[2]/wow", 0);
746         setprop("gear/gear[3]/wow", 0);
747         setprop("gear/gear[4]/wow", 0);
748         setprop("gear/gear[5]/wow", 0);
749         setprop("gear/gear[6]/wow", 0);
750         setprop("gear/gear[7]/wow", 0);
751         setprop("gear/gear[8]/wow", 0);
752         setprop("gear/gear[9]/wow", 0);
753         setprop("gear/gear[10]/wow", 0);
754 }
755
756 init_positions();
757
758 #Init aircraft
759 #--------------------------------------------------------------------
760 start_init=func
761         {
762                 setprop("fdm/jsbsim/init/on", 1);
763                 setprop("fdm/jsbsim/init/finally-initialized", 0);
764                 setprop("gear/gear[0]/position-norm", 1);
765                 setprop("gear/gear[1]/position-norm", 1);
766                 setprop("gear/gear[2]/position-norm", 1);
767                 final_init();
768         }
769
770 final_init=func
771         {
772                 initialization=getprop("fdm/jsbsim/init/on");
773                 time_elapsed=getprop("fdm/jsbsim/simulation/sim-time-sec");
774                 if (
775                         (initialization!=nil)
776                         and
777                         (time_elapsed!=nil)
778                 )
779                 {
780                         if (time_elapsed>0)
781                         {
782                                 setprop("fdm/jsbsim/init/on", 0);
783                                 setprop("fdm/jsbsim/init/finally-initialized", 1);
784                         }
785                         else
786                         {
787                                 return ( settimer(final_init, 0.1) ); 
788                         }
789                 }
790         }
791
792 start_init();
793
794 #--------------------------------------------------------------------
795 # Chronometer
796
797 # helper 
798 stop_chron = func {
799 }
800
801 chron = func 
802         {
803                 # check power
804                 in_service = getprop("instrumentation/clock/serviceable" );
805                 if (in_service == nil)
806                 {
807                         stop_chron();
808                         return ( settimer(chron, 0.1) ); 
809                 }
810                 if( in_service != 1 )
811                 {
812                         stop_chron();
813                         return ( settimer(chron, 0.1) ); 
814                 }               
815                 # set secondomer
816                 sec_on = getprop("instrumentation/clock/sec_on");
817                 sec_set = getprop("instrumentation/clock/sec_set");
818                 sec_now=getprop("instrumentation/clock/indicated-sec");
819                 if ((sec_on == nil) or (sec_set == nil) or (sec_now == nil))
820                 {
821                         stop_chron();
822                         return ( settimer(chron, 0.1) ); 
823                 }
824                 if (sec_set>=1 )
825                 {
826                         setprop("instrumentation/clock/sec_set", 0);
827                         if (sec_on==0)
828                         {
829                                 sec_on=1;
830                                 setprop("instrumentation/clock/sec_on", sec_on);
831                                 sec_start=getprop("instrumentation/clock/indicated-sec");
832                                 setprop("instrumentation/clock/sec_start", sec_start);
833                         }
834                         else
835                         {
836                                 sec_on=0;
837                                 setprop("instrumentation/clock/sec_on", sec_on);
838                         }
839                 }
840                 if (sec_on==1)
841                 {
842                         sec_start=getprop("instrumentation/clock/sec_start");
843                         sec_show=sec_now-sec_start;
844                         setprop("instrumentation/clock/sec_show", sec_show);
845                 }
846
847                 # set inflight time
848                 inf_on = getprop("instrumentation/clock/inf_on");
849                 inf_set = getprop("instrumentation/clock/inf_set");
850                 inf_sec = getprop("instrumentation/clock/inf_sec");
851                 if ((inf_on == nil) or (inf_set == nil) or (inf_sec == nil))
852                 {
853                         stop_chron();
854                         return ( settimer(chron, 0.1) ); 
855                 }
856                 if (inf_set>=1 )
857                 {
858                         setprop("instrumentation/clock/inf_set", 0);
859                         if (inf_on==0)
860                         {
861                                 inf_on=1;
862                                 setprop("instrumentation/clock/inf_on", inf_on);
863                         }
864                         else
865                         {
866                                 inf_on=0;
867                                 setprop("instrumentation/clock/inf_on", inf_on);
868                         }
869                 }
870                 if (inf_on==1)
871                 {
872                         inf_sec=inf_sec+0.1;
873                         inf_days=int(inf_sec/(60*60*24));
874                         if (inf_sec<0)
875                         {
876                                 inf_sec=60*60*24*10+inf_sec;
877                                 inf_days=int(inf_sec/(60*60*24));
878                         }
879                         if (inf_sec>(60*60*24*10))
880                         {
881                                 inf_sec=inf_sec-60*60*24*10;
882                                 inf_days=int(inf_sec/(60*60*24));
883                         }
884                         setprop("instrumentation/clock/inf_sec", inf_sec);
885                         setprop("instrumentation/clock/inf_days", inf_days);
886                 }
887                 settimer(chron, 0.1);
888         }
889
890 # set startup configuration
891 init_chron = func
892 {
893         setprop("instrumentation/clock/serviceable", 1);
894         setprop("instrumentation/clock/sec_on", 0);
895         setprop("instrumentation/clock/sec_set", 0);
896         setprop("instrumentation/clock/sec_start", 0);
897         setprop("instrumentation/clock/inf_on", 1);
898         setprop("instrumentation/clock/inf_set", 0);
899         setprop("instrumentation/clock/inf_sec", 0);
900         setprop("instrumentation/clock/inf_days", 0);
901 }
902
903 init_chron();
904
905 # start cronometer process first time
906 chron ();
907
908 #---------------------------------------------------------------------
909 #Right panel
910
911 # set startup configuration
912 init_rightpanel = func 
913 {
914         setprop("fdm/jsbsim/systems/rightpanel/serviceable", 1);
915         setprop("fdm/jsbsim/systems/rightpanel/battery-input", 0);
916         setprop("fdm/jsbsim/systems/rightpanel/generator-input", 0);
917         setprop("fdm/jsbsim/systems/rightpanel/headlight-input", 0);
918         setprop("fdm/jsbsim/systems/rightpanel/trimmer-input", 0);
919         setprop("fdm/jsbsim/systems/rightpanel/horizon-input", 0);
920         setprop("fdm/jsbsim/systems/rightpanel/radio-input", 0);
921         setprop("fdm/jsbsim/systems/rightpanel/radioaltimeter-input", 0);
922         setprop("fdm/jsbsim/systems/rightpanel/radiocompass-input", 0);
923         setprop("fdm/jsbsim/systems/rightpanel/drop-tank-input", 0);
924         setprop("fdm/jsbsim/systems/rightpanel/bomb-input", 0);
925         setprop("fdm/jsbsim/systems/rightpanel/photo-input", 0);
926         setprop("fdm/jsbsim/systems/rightpanel/photo-machinegun-input", 0);
927         setprop("fdm/jsbsim/systems/rightpanel/headsight-input", 0);
928         setprop("fdm/jsbsim/systems/rightpanel/machinegun-input", 0);
929 }
930
931 init_rightpanel();
932
933 #---------------------------------------------------------------------
934 #Left panel
935
936 # set startup configuration
937 init_leftpanel = func 
938 {
939         setprop("fdm/jsbsim/systems/leftpanel/serviceable", 1);
940         setprop("fdm/jsbsim/systems/leftpanel/pump-input", 0);
941         setprop("fdm/jsbsim/systems/leftpanel/isolation-valve-input", 0);
942         setprop("fdm/jsbsim/systems/leftpanel/ignition-type-input", 0);
943         setprop("fdm/jsbsim/systems/leftpanel/ignition-input", 0);
944         setprop("fdm/jsbsim/systems/leftpanel/engine-control-input", 0);
945         setprop("fdm/jsbsim/systems/leftpanel/third-tank-pump-input", 0);
946 }
947
948 init_leftpanel();
949
950 #---------------------------------------------------------------------
951 #Stop control
952
953 # set startup configuration
954 init_stopcontrol = func 
955 {
956         setprop("fdm/jsbsim/systems/stopcontrol/serviceable", 1);
957         setprop("fdm/jsbsim/systems/stopcontrol/lever-input", 1);
958 }
959
960 init_stopcontrol();
961
962 #---------------------------------------------------------------------
963 #Fuel control
964
965 # set startup configuration
966 init_fuelcontrol = func 
967 {
968         setprop("fdm/jsbsim/systems/fuelcontrol/serviceable", 1);
969         setprop("fdm/jsbsim/systems/fuelcontrol/control-input", 0);
970 }
971
972 init_fuelcontrol();
973
974 #---------------------------------------------------------------------
975 #Headsight
976
977 # set startup configuration
978 init_headsight = func 
979 {
980         setprop("fdm/jsbsim/systems/headsight/serviceable", 1);
981         setprop("fdm/jsbsim/systems/headsight/on", 0);
982         setprop("fdm/jsbsim/systems/headsight/up-command", 1);
983         setprop("fdm/jsbsim/systems/headsight/gyro-command", 1);
984         setprop("fdm/jsbsim/systems/headsight/frame-command", 0);
985         setprop("fdm/jsbsim/systems/headsight/brightness", 1);
986         setprop("fdm/jsbsim/systems/headsight/target-size", 15);
987         setprop("fdm/jsbsim/systems/headsight/target-distance", 400);
988         setprop("fdm/jsbsim/systems/headsight/from-eye-to-sight", 0.4);
989 }
990
991 init_headsight();
992
993 #---------------------------------------------------------------------
994 #Gascontrol
995
996 # set startup configuration
997 init_gascontrol = func 
998 {
999         setprop("fdm/jsbsim/systems/gascontrol/serviceable", 0);
1000         setprop("fdm/jsbsim/systems/gascontrol/lock-command", 1);
1001         setprop("fdm/jsbsim/systems/gascontrol/lock-pos", 1);
1002         setprop("fdm/jsbsim/systems/gascontrol/fix-command", 0);
1003         setprop("fdm/jsbsim/systems/gascontrol/fix-pos", 0);
1004         setprop("fdm/jsbsim/systems/gascontrol/lever-command", 0);
1005         setprop("fdm/jsbsim/systems/gascontrol/lever-pos", 0);
1006         setprop("fdm/jsbsim/systems/gascontrol/shift-oscillator-command", 0);
1007         setprop("fdm/jsbsim/systems/gascontrol/shift-oscillator-pos", 0);
1008         setprop("fdm/jsbsim/systems/gascontrol/serviceable", 1);
1009 }
1010
1011 init_gascontrol();
1012
1013 #---------------------------------------------------------------------
1014 #Flapscontrol
1015
1016 # set startup configuration
1017 init_flapscontrol = func 
1018 {
1019         setprop("fdm/jsbsim/systems/flapscontrol/serviceable", 0);
1020         setprop("fdm/jsbsim/systems/flapscontrol/lever-command", 0);
1021         setprop("fdm/jsbsim/systems/flapscontrol/lever-pos", 0);
1022         setprop("fdm/jsbsim/systems/flapscontrol/fix-pos", 0);
1023         setprop("fdm/jsbsim/systems/flapscontrol/serviceable", 1);
1024 }
1025
1026 init_flapscontrol();
1027
1028 #--------------------------------------------------------------------
1029 # Speed brake control
1030
1031 # set startup configuration
1032 init_speedbrakecontrol = func 
1033 {
1034         setprop("fdm/jsbsim/systems/speedbrakescontrol/serviceable", 1);
1035         setprop("fdm/jsbsim/systems/speedbrakescontrol/control-input", 0);
1036         setprop("fdm/jsbsim/systems/speedbrakescontrol/control-command", 0);
1037         setprop("fdm/jsbsim/systems/speedbrakescontrol/control-pos", 0);
1038         setprop("fdm/jsbsim/systems/speedbrakescontrol/control-switch", 0);
1039         setprop("fdm/jsbsim/systems/speedbrakescontrol/light-pos", 0);
1040 }
1041
1042 init_speedbrakecontrol();
1043
1044 #---------------------------------------------------------------------
1045 #Gear control
1046
1047 # set startup configuration
1048 init_gearcontrol = func 
1049 {
1050         setprop("fdm/jsbsim/systems/gearcontrol/serviceable", 0);
1051         setprop("fdm/jsbsim/systems/gearcontrol/control-input", 1);
1052         setprop("fdm/jsbsim/systems/gearcontrol/control-command", 1);
1053         setprop("fdm/jsbsim/systems/gearcontrol/safer-command", 1);
1054         setprop("fdm/jsbsim/systems/gearcontrol/safer-pos", 1);
1055         setprop("fdm/jsbsim/systems/gearcontrol/lever-pos", 0);
1056         setprop("fdm/jsbsim/systems/gearcontrol/gear-one-end-move", 0);
1057         setprop("fdm/jsbsim/systems/gearcontrol/gear-two-end-move", 0);
1058         setprop("fdm/jsbsim/systems/gearcontrol/gear-three-end-move", 0);
1059         setprop("fdm/jsbsim/systems/gearcontrol/gear-end-move", 0);
1060         setprop("fdm/jsbsim/systems/gearcontrol/serviceable", 1);
1061 }
1062
1063 init_gearcontrol();
1064
1065 #--------------------------------------------------------------------
1066 # Cabin manometer
1067
1068 # helper 
1069 stop_manometer = func 
1070         {
1071                 setprop("instrumentation/manometer/lamp", 0);
1072         }
1073
1074 manometer = func 
1075         {
1076                 # check power
1077                 in_service = getprop("instrumentation/manometer/serviceable" );
1078                 if (in_service == nil)
1079                 {
1080                         stop_manometer();
1081                         return ( settimer(manometer, 5) ); 
1082                 }
1083                 if( in_service != 1 )
1084                 {
1085                         stop_manometer();
1086                         return ( settimer(manometer, 5) ); 
1087                 }
1088                 # get pressure values
1089                 pressure = getprop("environment/pressure-inhg");
1090                 sea_pressure = getprop("environment/pressure-sea-level-inhg");
1091                 cabin_pressure = getprop("instrumentation/manometer/cabin-pressure");
1092                 # get canopy value, 0 is closed
1093                 canopy = getprop("fdm/jsbsim/systems/canopy/pos");
1094                 if ((pressure == nil) or (sea_pressure == nil) or (cabin_pressure == nil) or (canopy == nil))
1095                 {
1096                         stop_manometer();
1097                         return ( settimer(manometer, 5) ); 
1098                 }
1099                 #There is two processes to cabin pressure
1100                 #first is pressure system that try to make it higher 
1101                 cabin_pressure=cabin_pressure+(((sea_pressure*9+pressure)/10)-cabin_pressure)*(0.05);
1102                 #second depends on canopy and try to make it on flight level with lesser speed
1103                 #but it really fast then canopy is open 
1104                 cabin_pressure=cabin_pressure+(pressure-cabin_pressure)*(canopy+0.005);
1105                 setprop("instrumentation/manometer/cabin-pressure", cabin_pressure);
1106                 cabin_delta=pressure/cabin_pressure;
1107                 setprop("instrumentation/manometer/cabin-delta", cabin_delta);
1108                 settimer(manometer, 5);
1109         }
1110
1111 # set startup configuration
1112 init_manometer = func
1113 {
1114         setprop("instrumentation/manometer/serviceable", 1);
1115         pressure = getprop("environment/pressure-inhg");
1116         setprop("instrumentation/manometer/cabin-pressure", pressure);
1117 }
1118
1119 init_manometer();
1120
1121 # start manometer process first time
1122 manometer ();
1123
1124 #--------------------------------------------------------------------
1125 # Magnetic compass
1126
1127 init_magnetic_compass = func
1128 {
1129         setprop("instrumentation/magnetic-compass/offset", 0);
1130 }
1131
1132 init_magnetic_compass();
1133
1134 #--------------------------------------------------------------------
1135 #Gear friction
1136
1137 # Do terrain modelling ourselves.
1138 setprop("sim/fdm/surface/override-level", 1);
1139
1140 terrain_under = func
1141         {
1142                 lat = getprop ("position/latitude-deg");
1143                 lon = getprop ("position/longitude-deg");
1144                 info = geodinfo (lat,lon);
1145
1146                 if (info != nil) 
1147                 {
1148                         if (info[1] != nil)
1149                         {
1150                                 if (info[1].solid != nil) 
1151                                 {
1152                                         setprop ("environment/terrain", info[1].solid);
1153                                 }
1154                                 if (info[1].load_resistance != nil)
1155                                 {
1156                                         setprop ("environment/terrain-load-resistance", info[1].load_resistance);
1157                                 }
1158                                 if (info[1].bumpiness != nil)
1159                                 {
1160                                         setprop ("environment/terrain-bumpiness", info[1].bumpiness);
1161                                 }
1162                                 if (info[1].friction_factor != nil)
1163                                 {
1164                                         setprop ("environment/terrain-friction-factor", info[1].friction_factor);
1165                                 }
1166                                 if (info[1].rolling_friction != nil)
1167                                 {
1168                                         setprop ("environment/terrain-rolling-friction", info[1].rolling_friction);
1169                                 }
1170                         }
1171                 }
1172                 else
1173                 {
1174                         setprop ("environment/terrain", 1);
1175                 }
1176                 settimer (terrain_under, 0.1);
1177         }
1178
1179 terrain_under();
1180
1181 set_friction = func
1182         {
1183                 friction = getprop ("environment/terrain-friction-factor");
1184                 rain = getprop ("environment/metar/rain-norm");
1185                 if (rain != nil)
1186                 {
1187                         friction_water = 0.4 * rain;
1188                 }
1189                 else
1190                 {
1191                         friction_water = 0;
1192                 }
1193                 if (friction != nil)
1194                 {
1195                         if (friction = 1)
1196                         {
1197                                 friction = 0.8-friction_water;
1198                         }
1199                         setprop("fdm/jsbsim/gear/unit[0]/static_friction_coeff", friction);
1200                         setprop("fdm/jsbsim/gear/unit[1]/static_friction_coeff", friction);
1201                         setprop("fdm/jsbsim/gear/unit[2]/static_friction_coeff", friction);
1202                         setprop("fdm/jsbsim/gear/unit[3]/static_friction_coeff", friction);
1203                         setprop("fdm/jsbsim/gear/unit[4]/static_friction_coeff", friction);
1204                         setprop("fdm/jsbsim/gear/unit[5]/static_friction_coeff", friction);
1205                         setprop("fdm/jsbsim/gear/unit[6]/static_friction_coeff", friction);
1206                         setprop("fdm/jsbsim/gear/unit[7]/static_friction_coeff", friction);
1207                         setprop("fdm/jsbsim/gear/unit[8]/static_friction_coeff", friction);
1208                         setprop("fdm/jsbsim/gear/unit[9]/static_friction_coeff", friction);
1209                         setprop("fdm/jsbsim/gear/unit[10]/static_friction_coeff", friction);
1210                 }
1211                 settimer (set_friction, 0.1);
1212         }
1213
1214 set_friction();
1215
1216 #--------------------------------------------------------------------
1217 # Gear breaks listener
1218
1219 teargear = func (gear_number, breaktype)
1220         {
1221                 gear_tored=getprop("fdm/jsbsim/gear/unit["~gear_number~"]/tored");
1222                 if (gear_tored==nil)
1223                 {
1224                         return (0);
1225                 }
1226                 if (gear_tored==1)
1227                 {
1228                         return (0);
1229                 }
1230                 wow=getprop("gear/gear["~gear_number~"]/wow");
1231                 setprop("fdm/jsbsim/gear/unit["~gear_number~"]/break-type", breaktype);
1232                 setprop("fdm/jsbsim/gear/unit["~gear_number~"]/tored", 1);
1233                 setprop("fdm/jsbsim/gear/unit["~gear_number~"]/pos-norm-real", 0);
1234                 setprop("fdm/jsbsim/gear/unit["~gear_number~"]/z-position", 0);
1235 #               setprop("gear/gear["~gear_number~"]/position-norm", 0);
1236                 gears_tored_sound=getprop("sounds/gears-tored/on");
1237                 if (gears_tored_sound!=nil)
1238                 {
1239                         if (gears_tored_sound==0)
1240                         {
1241                                 geartoredsound();
1242                         }
1243                 }
1244                 if (wow!=nil)
1245                 {
1246                         if (wow==0)
1247                         {
1248                                 if (gear_number==0)
1249                                 {
1250                                         setprop("ai/submodels/gear-middle-drop", 1);
1251                                 }
1252                                 if (gear_number==1)
1253                                 {
1254                                         setprop("ai/submodels/gear-left-drop", 1);
1255                                 }
1256                                 if (gear_number==2)
1257                                 {
1258                                         setprop("ai/submodels/gear-right-drop", 1);
1259                                 }
1260                         }
1261                 }
1262                 return (1);
1263         }
1264
1265 # helper 
1266 stop_gearbreakslistener = func 
1267         {
1268         }
1269
1270 gearbreakslistener = func 
1271         {
1272                 # check state
1273                 in_service = getprop("listneners/gear-break/enabled" );
1274                 if (in_service == nil)
1275                 {
1276                         return ( stop_gearbreakslistener );
1277                 }
1278                 if ( in_service != 1 )
1279                 {
1280                         return ( stop_gearbreakslistener );
1281                 }
1282                 # get gear values
1283                 gear_one_tored=getprop("fdm/jsbsim/gear/unit[0]/tored");
1284                 gear_two_tored=getprop("fdm/jsbsim/gear/unit[1]/tored");
1285                 gear_three_tored=getprop("fdm/jsbsim/gear/unit[2]/tored");
1286                 pilot_g=getprop("fdm/jsbsim/accelerations/Nz");
1287                 maximum_g=getprop("fdm/jsbsim/accelerations/Nz-max");
1288                 speed=getprop("velocities/airspeed-kt");
1289                 mach=getprop("velocities/mach");
1290                 pitch_degps=getprop("orientation/pitch-rate-degps");
1291                 roll_speed_one=getprop("gear/gear/rollspeed-ms");
1292                 roll_speed_two=getprop("gear/gear[1]/rollspeed-ms");
1293                 roll_speed_three=getprop("gear/gear[2]/rollspeed-ms");
1294                 compression_one=getprop("gear/gear/compression-ft");
1295                 compression_two=getprop("gear/gear[1]/compression-ft");
1296                 compression_three=getprop("gear/gear[2]/compression-ft");
1297                 wow_one=getprop("gear/gear/wow");
1298                 wow_two=getprop("gear/gear[1]/wow");
1299                 wow_three=getprop("gear/gear[2]/wow");
1300                 gear_started=getprop("fdm/jsbsim/init/finally-initialized");
1301                 if (
1302                         (gear_one_tored==nil)
1303                         or (gear_two_tored==nil)
1304                         or (gear_three_tored==nil)
1305                         or (pilot_g==nil)
1306                         or (maximum_g==nil)
1307                         or (speed==nil)
1308                         or (mach==nil)
1309                         or (pitch_degps==nil)
1310                         or (roll_speed_one==nil)
1311                         or (roll_speed_two==nil)
1312                         or (roll_speed_three==nil)
1313                         or (compression_one==nil)
1314                         or (compression_two==nil)
1315                         or (compression_three==nil)
1316                         or (wow_one==nil)
1317                         or (wow_two==nil)
1318                         or (wow_three==nil)
1319                         or (gear_started==nil)
1320                 )
1321                 {
1322                         return ( stop_gearbreakslistener ); 
1323                 }
1324                 if (gear_started==0)
1325                 {
1326                         return ( stop_gearbreakslistener ); 
1327                 }
1328                 #Hit breaks and ground type breaks checks here, speed breaks checks in process
1329                 if (
1330                         (wow_one>0)
1331                         and (abs(pilot_g)>2)
1332                         and (abs(pilot_g)>(maximum_g-(maximum_g*0.25)))
1333                 )
1334                 {
1335                         gear_one_tored=teargear(0, "hit overload "~pilot_g);
1336                 }
1337                 if (
1338                         (wow_two>0)
1339                         and (abs(pilot_g)>2)
1340                         and (abs(pilot_g)>(maximum_g-(maximum_g*0.25)))
1341                 )
1342                 {
1343                         gear_two_tored=teargear(1, "hit overload "~pilot_g);
1344                 }
1345                 if (
1346                         (wow_three>0)
1347                         and (abs(pilot_g)>2)
1348                         and (abs(pilot_g)>(maximum_g-(maximum_g*0.25)))
1349                 )
1350                 {
1351                         gear_three_tored=teargear(2, "hit overload "~pilot_g);
1352                 }
1353                 if (
1354                         (wow_one>0) 
1355                         and (
1356                                 (wow_two==0)
1357                                 and (wow_three==0)
1358                         )
1359                 )
1360                 {
1361                         gear_one_tored=teargear(0, "dig "~pilot_g);
1362                 }
1363                 if (
1364                         (wow_one>0)
1365                         and (pitch_degps<-10)
1366                 )
1367                 {
1368                         gear_one_tored=teargear(0, "peck "~pitch_degps);
1369                 }
1370         }
1371
1372 init_gearbreakslistener = func 
1373 {
1374         setprop("sounds/gears-tored/on", 0);
1375         setprop("listneners/gear-break/enabled", 1);
1376 }
1377
1378 init_gearbreakslistener();
1379
1380 setlistener("gear/gear/wow", gearbreakslistener);
1381 setlistener("gear/gear[1]/wow", gearbreakslistener);
1382 setlistener("gear/gear[2]/wow", gearbreakslistener);
1383
1384
1385 #--------------------------------------------------------------------
1386 # Gear breaks
1387
1388 # helper 
1389 stop_gearbreaksprocess = func 
1390         {
1391         }
1392
1393 gearbreaksprocess = func 
1394         {
1395                 # check state
1396                 in_service = getprop("processes/gear-break/enabled" );
1397                 if (in_service == nil)
1398                 {
1399                         stop_gearbreaksprocess();
1400                         return ( settimer(gearbreaksprocess, 0.1) ); 
1401                 }
1402                 if ( in_service != 1 )
1403                 {
1404                         stop_gearbreaksprocess();
1405                         return ( settimer(gearbreaksprocess, 0.1) ); 
1406                 }
1407                 # get gear values
1408                 gear_one_pos=getprop("fdm/jsbsim/gear/unit[0]/pos-norm-real");
1409                 gear_two_pos=getprop("fdm/jsbsim/gear/unit[1]/pos-norm-real");
1410                 gear_three_pos=getprop("fdm/jsbsim/gear/unit[2]/pos-norm-real");
1411                 gear_one_tored=getprop("fdm/jsbsim/gear/unit[0]/tored");
1412                 gear_two_tored=getprop("fdm/jsbsim/gear/unit[1]/tored");
1413                 gear_three_tored=getprop("fdm/jsbsim/gear/unit[2]/tored");
1414                 pilot_g=getprop("fdm/jsbsim/accelerations/Nz");
1415                 maximum_g=getprop("fdm/jsbsim/accelerations/Nz-max");
1416                 maximum_g_tenth=getprop("fdm/jsbsim/accelerations/Nz-max-tenth");
1417                 speed=getprop("velocities/airspeed-kt");
1418                 mach=getprop("velocities/mach");
1419                 pitch_degps=getprop("orientation/pitch-rate-degps");
1420                 roll_speed_one=getprop("gear/gear/rollspeed-ms");
1421                 roll_speed_two=getprop("gear/gear[1]/rollspeed-ms");
1422                 roll_speed_three=getprop("gear/gear[2]/rollspeed-ms");
1423                 wow_one=getprop("gear/gear/wow");
1424                 wow_two=getprop("gear/gear[1]/wow");
1425                 wow_three=getprop("gear/gear[2]/wow");
1426                 if (
1427                         (gear_one_pos == nil)
1428                         or (gear_two_pos == nil)
1429                         or (gear_three_pos == nil)
1430                         or (gear_one_tored==nil)
1431                         or (gear_two_tored==nil)
1432                         or (gear_three_tored==nil)
1433                         or (pilot_g==nil)
1434                         or (maximum_g==nil)
1435                         or (maximum_g_tenth=nil)
1436                         or (speed==nil)
1437                         or (mach==nil)
1438                         or (pitch_degps==nil)
1439                         or (roll_speed_one==nil)
1440                         or (roll_speed_two==nil)
1441                         or (roll_speed_three==nil)
1442                         or (wow_one==nil)
1443                         or (wow_two==nil)
1444                         or (wow_three==nil)
1445                 )
1446                 {
1447                         stop_gearbreaksprocess();
1448                         return ( settimer(gearbreaksprocess, 0.1) ); 
1449                 }
1450                 lat = getprop("position/latitude-deg");
1451                 lon = getprop("position/longitude-deg");
1452                 if (
1453                         (lat == nil)
1454                         or (lon==nil)
1455                 )
1456                 {
1457                         stop_gearbreaksprocess();
1458                         return ( settimer(gearbreaksprocess, 0.1) ); 
1459                 }
1460                 info = geodinfo(lat, lon);
1461                 if (info == nil)
1462                 {
1463                         stop_gearbreaksprocess();
1464                         return ( settimer(gearbreaksprocess, 0.1) ); 
1465                 }
1466                 if (
1467                         (info[0] == nil)
1468                         or (info[1] == nil)
1469                 )
1470                 {
1471                         stop_gearbreaksprocess();
1472                         return ( settimer(gearbreaksprocess, 0.1) ); 
1473                 }
1474                 setprop("gear/info/height", info[0]);
1475                 setprop("gear/info/light_coverage", info[1].light_coverage);
1476                 setprop("gear/info/bumpiness", info[1].bumpiness);
1477                 setprop("gear/info/load_resistance", info[1].load_resistance);
1478                 setprop("gear/info/solid", info[1].solid);
1479                 i=0;
1480                 foreach(terrain_name; info[1].names)
1481                 {
1482                         setprop("gear/info/names["~i~"]", terrain_name);
1483                         i=i+1;
1484                 }
1485                 setprop("gear/info/friction_factor", info[1].friction_factor);
1486                 setprop("gear/info/rolling_friction", info[1].rolling_friction);
1487                 if (
1488                         ((gear_one_pos>0) and (gear_one_tored==0))
1489                         or ((gear_two_pos>0) and (gear_two_tored==0))
1490                         or ((gear_three_pos>0) and (gear_three_tored==0))
1491                 )
1492                 {
1493                         speed_km=speed*1.852;
1494                         #Middle gear speed limit max 550 km\h on 0.5 of extraction, 520 on 1
1495                         speed_limit_middle=500-((gear_one_pos-0.5)/(1-0.5))*(550-525);
1496                         setprop("fdm/jsbsim/gear/unit[0]/speed-limit", speed_limit_middle);
1497                         if ((speed_km>speed_limit_middle) and (speed_limit_middle>0))
1498                         {
1499                                 if ((gear_one_tored==0) and (gear_one_pos>0))
1500                                 {
1501                                         gear_one_tored=teargear(0, "overspeeed "~speed_km);
1502                                 }
1503                         }
1504                         speed_limit_left=525-((gear_two_pos-0.5)/(1-0.5))*(525-505);
1505                         setprop("fdm/jsbsim/gear/unit[1]/speed-limit", speed_limit_left);
1506                         if ((speed_km>speed_limit_left) and (speed_limit_left>0))
1507                         {
1508                                 if ((gear_two_tored==0) and (gear_two_pos>0))
1509                                 {
1510                                         gear_two_tored=teargear(1, "overspeeed "~speed_km);
1511                                 }
1512                         }
1513                         speed_limit_right=610-((gear_three_pos-0.5)/(1-0.5))*(610-515);
1514                         setprop("fdm/jsbsim/gear/unit[2]/speed-limit", speed_limit_right);
1515                         if ((speed_km>speed_limit_right) and (speed_limit_right>0))
1516                         {
1517                                 if ((gear_three_tored==0) and (gear_three_pos>0))
1518                                 {
1519                                         gear_three_tored=teargear(2, "overspeeed "~speed_km);
1520                                 }
1521                         }
1522                         roll_speed_km_one=(roll_speed_one*(60*60)/1000);
1523                         roll_speed_km_two=(roll_speed_two*(60*60)/1000);
1524                         roll_speed_km_three=(roll_speed_three*(60*60)/1000);
1525                         if ((gear_one_tored==0) and (wow_one==1) and (roll_speed_km_one>300))
1526                         {
1527                                 gear_one_tored=teargear(0, "overroll "~speed_km);
1528                         }
1529                         if ((gear_two_tored==0) and (wow_two==1) and (roll_speed_km_two>325))
1530                         {
1531                                 gear_two_tored=teargear(1, "overroll "~speed_km);
1532                         }
1533                         if ((gear_three_tored==0) and (wow_three==1) and (roll_speed_km_three>320))
1534                         {
1535                                 gear_three_tored=teargear(2, "overroll "~speed_km);
1536                         }
1537
1538                         if (
1539                                 (gear_one_tored==0) 
1540                                 and (wow_one==0) 
1541                                 and (abs(pilot_g)>3)
1542                         )
1543                         {
1544                                 gear_one_tored=teargear(0, "flight overload "~pilot_g);
1545                         }
1546                         if (
1547                                 (gear_two_tored==0) 
1548                                 and (wow_two==0) 
1549                                 and (abs(pilot_g)>3)
1550                         )
1551                         {
1552                                 gear_two_tored=teargear(1, "flight overload "~pilot_g);
1553                         }
1554                         if (
1555                                 (gear_three_tored==0) 
1556                                 and (wow_three==0) 
1557                                 and (abs(pilot_g)>3)
1558                         )
1559                         {
1560                                 gear_three_tored=teargear(2, "flight overload "~pilot_g);
1561                         }
1562
1563                         if (info[1].solid!=1)
1564                         {
1565                                 if ((gear_one_tored==0) and (wow_one>0))
1566                                 {
1567                                         gear_one_tored=teargear(0, "water ");
1568                                 }
1569                                 if ((gear_two_tored==0) and (wow_two>0))
1570                                 {
1571                                         gear_two_tored=teargear(1, "water ");
1572                                 }
1573                                 if ((gear_three_tored==0) and (wow_three>0))
1574                                 {
1575                                         gear_three_tored=teargear(2, "water ");
1576                                 }
1577                         }
1578                         if (
1579                                 (info[1].bumpiness>0.1)
1580                                 or (info[1].rolling_friction>0.05)
1581                                 or (info[1].friction_factor<0.7)
1582                         )
1583                         {
1584                                 if ((gear_one_tored==0) and (wow_one==1) and (roll_speed_km_one>75))
1585                                 {
1586                                         gear_one_tored=teargear(0, "ground overroll "~speed_km);
1587                                 }
1588                                 if ((gear_two_tored==0) and (wow_two==1) and (roll_speed_km_two>80))
1589                                 {
1590                                         gear_two_tored=teargear(1, "ground overroll "~speed_km);
1591                                 }
1592                                 if ((gear_three_tored==0) and (wow_three==1) and (roll_speed_km_three>82))
1593                                 {
1594                                         gear_three_tored=teargear(2, "ground overroll "~speed_km);
1595                                 }
1596                         }
1597                         if (
1598                                 (info[1].bumpiness==1)
1599                                 or (info[1].rolling_friction==1)
1600                         )
1601                         {
1602                                 if ((gear_one_tored==0) and (wow_one==1))
1603                                 {
1604                                         gear_one_tored=teargear(0, "wrongground");
1605                                 }
1606                                 if ((gear_two_tored==0) and (wow_two==1))
1607                                 {
1608                                         gear_two_tored=teargear(1, "wrongground");
1609                                 }
1610                                 if ((gear_three_tored==0) and (wow_three==1))
1611                                 {
1612                                         gear_three_tored=teargear(2, "wrongground");
1613                                 }
1614                         }
1615
1616                 }
1617                 gear_impact=getprop("ai/submodels/gear-middle-impact");
1618                 if (gear_impact!=nil)
1619                 {
1620                         if (gear_impact!="")
1621                         {
1622                                 setprop("ai/submodels/gear-middle-impact", "");
1623                                 gear_touch_down();
1624                         }
1625                 }
1626                 gear_impact=getprop("ai/submodels/gear-middle-left-door-impact");
1627                 if (gear_impact!=nil)
1628                 {
1629                         if (gear_impact!="")
1630                         {
1631                                 setprop("ai/submodels/gear-middle-left-door-impact", "");
1632                         }
1633                 }
1634                 gear_impact=getprop("ai/submodels/gear-middle-right-door-impact");
1635                 if (gear_impact!=nil)
1636                 {
1637                         if (gear_impact!="")
1638                         {
1639                                 setprop("ai/submodels/gear-middle-right-door-impact", "");
1640                         }
1641                 }
1642                 gear_impact=getprop("ai/submodels/gear-left-impact");
1643                 if (gear_impact!=nil)
1644                 {
1645                         if (gear_impact!="")
1646                         {
1647                                 setprop("ai/submodels/gear-left-impact", "");
1648                                 gear_touch_down();
1649                         }
1650                 }
1651                 gear_impact=getprop("ai/submodels/gear-left-stow-impact");
1652                 if (gear_impact!=nil)
1653                 {
1654                         if (gear_impact!="")
1655                         {
1656                                 setprop("ai/submodels/gear-left-stow-impact", "");
1657                         }
1658                 }
1659                 gear_impact=getprop("ai/submodels/gear-left-door-inner-impact");
1660                 if (gear_impact!=nil)
1661                 {
1662                         if (gear_impact!="")
1663                         {
1664                                 setprop("ai/submodels/gear-left-door-inner-impact", "");
1665                         }
1666                 }
1667                 gear_impact=getprop("ai/submodels/gear-left-door-middle-impact");
1668                 if (gear_impact!=nil)
1669                 {
1670                         if (gear_impact!="")
1671                         {
1672                                 setprop("ai/submodels/gear-left-door-middle-impact", "");
1673                         }
1674                 }
1675                 gear_impact=getprop("ai/submodels/gear-left-door-outer-impact");
1676                 if (gear_impact!=nil)
1677                 {
1678                         if (gear_impact!="")
1679                         {
1680                                 setprop("ai/submodels/gear-left-door-outer-impact", "");
1681                         }
1682                 }
1683                 gear_impact=getprop("ai/submodels/gear-right-impact");
1684                 if (gear_impact!=nil)
1685                 {
1686                         if (gear_impact!="")
1687                         {
1688                                 setprop("ai/submodels/gear-right-impact", "");
1689                                 gear_touch_down();
1690                         }
1691                 }
1692                 gear_middle_drop=getprop("ai/submodels/gear-middle-drop");
1693                 if (gear_middle_drop!=nil)
1694                 {
1695                         if ((gear_one_tored==0) and (gear_middle_drop==1))
1696                         {
1697                                 setprop("ai/submodels/gear-middle-drop", 0);
1698                         }
1699                 }
1700                 gear_left_drop=getprop("ai/submodels/gear-left-drop");
1701                 if (gear_left_drop!=nil)
1702                 {
1703                         if ((gear_two_tored==0) and (gear_left_drop==1))
1704                         {
1705                                 setprop("ai/submodels/gear-left-drop", 0);
1706                         }
1707                 }
1708                 gear_right_drop=getprop("ai/submodels/gear-right-drop");
1709                 if (gear_right_drop!=nil)
1710                 {
1711                         if ((gear_three_tored==0) and (gear_right_drop==1))
1712                         {
1713                                 setprop("ai/submodels/gear-right-drop", 0);
1714                         }
1715                 }
1716                 settimer(gearbreaksprocess, 0.1)
1717         }
1718
1719 # set startup configuration
1720 init_gearbreaksprocess = func
1721 {
1722         setprop("processes/gear-break/enabled", 1);
1723 }
1724
1725 init_gearbreaksprocess();
1726
1727 geartoredsound = func
1728         {
1729                 setprop("sounds/gears-tored/on", 1);
1730                 settimer(geartoredsoundoff, 0.3);
1731         }
1732
1733 geartoredsoundoff = func
1734         {
1735                 setprop("sounds/gears-tored/on", 0);
1736         }
1737
1738 gear_touch_down = func
1739         {
1740                 setprop("sounds/gears-down/on", 1);
1741                 settimer(end_gear_touch_down, 3);
1742         }
1743
1744 end_gear_touch_down = func
1745         {
1746                 setprop("sounds/gears-down/on", 0);
1747         }
1748
1749 # start gear break process first time, give time to proper initialization
1750 gearbreaksprocess();
1751
1752 #Gear move process
1753 #--------------------------------------------------------------------
1754
1755 gear_control_up = func
1756         {
1757                 setprop("fdm/jsbsim/systems/gearcontrol/control-input", 0);
1758         }
1759
1760 gear_control_down = func
1761         {
1762                 setprop("fdm/jsbsim/systems/gearcontrol/control-input", 1);
1763         }
1764
1765 geartoredsound = func
1766         {
1767                 setprop("sounds/gears-tored/on", 1);
1768                 settimer(geartoredsoundoff, 0.3);
1769         }
1770
1771 geartoredsoundoff = func
1772         {
1773                 setprop("sounds/gears-tored/on", 0);
1774         }
1775
1776 gear_touch_down = func
1777         {
1778                 setprop("sounds/gears-down/on", 1);
1779                 settimer(end_gear_touch_down, 3);
1780         }
1781
1782 end_gear_touch_down = func
1783         {
1784                 setprop("sounds/gears-down/on", 0);
1785         }
1786
1787 timedhmove = func (property_name, control_name, move_time)
1788         {
1789                 pos=getprop(property_name);
1790                 set_pos=getprop(control_name);
1791                 if (
1792                         (pos == nil)
1793                         or (set_pos == nil)
1794                 )
1795                 {
1796                         return (0); 
1797                 }
1798                 if (pos!=set_pos)
1799                 {
1800                         way_to=abs(set_pos-pos)/(set_pos-pos);
1801                         pos=pos+0.1/move_time*way_to;
1802                         if (((way_to>0) and (pos>set_pos)) or ((way_to<0) and (pos<set_pos)))
1803                         {
1804                                 pos=set_pos;
1805                                 setprop(property_name, set_pos);
1806                                 interpolate(property_name~"-inter", set_pos, 0);
1807                         }
1808                         else
1809                         {
1810                                 setprop(property_name, pos);
1811                                 interpolate(property_name~"-inter", pos, 0.1);
1812                         }
1813                 }
1814                 return (1); 
1815         }
1816
1817 # helper 
1818 stop_gearmove = func 
1819         {
1820         }
1821
1822 one_gear_move=func(gear_num, gear_pos, gear_command, gear_stuck, move_time, z_max, max_angle, speed_km, speed_limit)
1823         {
1824                 if (
1825                         (gear_stuck==0)
1826                         or
1827                         (gear_command>gear_pos)
1828                 )
1829                 {
1830                         timedhmove("fdm/jsbsim/gear/unit["~gear_num~"]/pos-norm-real", "fdm/jsbsim/gear/gear-cmd-norm-real", move_time);
1831                         gear_pos=getprop("fdm/jsbsim/gear/unit["~gear_num~"]/pos-norm-real");
1832                         if (gear_pos==nil)
1833                         {
1834                                 return (0);
1835                         }
1836                         if  (gear_pos>=0.5)
1837                         {
1838                                 gear_angle=(1-(gear_pos-0.5)/0.5)*max_angle/180*math.pi;
1839                                 z_poz=z_max*(-1/0.0254)*math.cos(gear_angle);
1840                                 setprop("fdm/jsbsim/gear/unit["~gear_num~"]/z-position", z_poz);
1841                         }
1842                         if (
1843                                 (speed_km>speed_limit) 
1844                                 and (gear_pos>0)
1845                                 and (gear_command<gear_pos)
1846                         )
1847                         {
1848                                 if (gear_command<gear_pos)
1849                                 {
1850                                         gearstuck(gear_num, 1);
1851                                 }
1852                                 else
1853                                 {
1854                                         gearstuck(gear_num, 0);
1855                                 }
1856                         }
1857                         if (gear_pos==1)
1858                         {
1859                                 setprop("fdm/jsbsim/gear/unit["~gear_num~"]/stuck", 0);
1860                         }
1861                 }
1862         }
1863
1864 gearmove = func 
1865         {
1866                 # check state
1867                 in_service = getprop("processes/gear-move/enabled" );
1868                 if (in_service == nil)
1869                 {
1870                         stop_gearmove();
1871                         return ( settimer(gearmove, 0.1) ); 
1872                 }
1873                 if ( in_service != 1 )
1874                 {
1875                         stop_gearmove();
1876                         return ( settimer(gearmove, 0.1) ); 
1877                 }
1878
1879                 # get gear values
1880                 gear_one_pos=getprop("fdm/jsbsim/gear/unit[0]/pos-norm-real");
1881                 gear_two_pos=getprop("fdm/jsbsim/gear/unit[1]/pos-norm-real");
1882                 gear_three_pos=getprop("fdm/jsbsim/gear/unit[2]/pos-norm-real");
1883                 gear_one_tored=getprop("fdm/jsbsim/gear/unit[0]/tored");
1884                 gear_two_tored=getprop("fdm/jsbsim/gear/unit[1]/tored");
1885                 gear_three_tored=getprop("fdm/jsbsim/gear/unit[2]/tored");
1886                 gear_one_stuck=getprop("fdm/jsbsim/gear/unit[0]/stuck");
1887                 gear_two_stuck=getprop("fdm/jsbsim/gear/unit[1]/stuck");
1888                 gear_three_stuck=getprop("fdm/jsbsim/gear/unit[2]/stuck");
1889                 gear_command=getprop("fdm/jsbsim/systems/gearcontrol/control-switch");
1890                 gear_command_real=getprop("fdm/jsbsim/gear/gear-cmd-norm-real");
1891
1892                 speed=getprop("velocities/airspeed-kt");
1893
1894                 #get gear valve and handles values
1895                 valve_press=getprop("fdm/jsbsim/systems/gearvalve/pressure-pos");
1896                 left_handle_pos=getprop("fdm/jsbsim/systems/gearvalve/left-handle-switch");
1897                 right_handle_pos=getprop("fdm/jsbsim/systems/gearvalve/right-handle-switch");
1898                 #get power values
1899                 pump=getprop("systems/electrical-real/outputs/pump/volts-norm");
1900                 engine_running=getprop("engines/engine/running");
1901                 set_generator=getprop("fdm/jsbsim/systems/rightpanel/generator-switch");
1902
1903                 if (
1904                         (gear_one_pos==nil)
1905                         or (gear_two_pos==nil)
1906                         or (gear_three_pos==nil)
1907                         or (gear_one_tored==nil)
1908                         or (gear_two_tored==nil)
1909                         or (gear_three_tored==nil)
1910                         or (gear_one_stuck==nil)
1911                         or (gear_two_stuck==nil)
1912                         or (gear_three_stuck==nil)
1913                         or (gear_one_pos==nil)
1914                         or (gear_two_pos==nil)
1915                         or (gear_three_pos==nil)
1916                         or (gear_command==nil)
1917                         or (gear_command_real==nil)
1918                         or (speed==nil)
1919
1920                         or (valve_press==nil)
1921                         or (left_handle_pos==nil)
1922                         or (right_handle_pos==nil)
1923                         or (pump==nil)
1924                         or (engine_running==nil)
1925                         or (set_generator==nil)
1926
1927                 )
1928                 {
1929                         stop_gearmove();
1930                         return ( settimer(gearmove, 0.1) ); 
1931                 }
1932
1933                 if (
1934                         (
1935                                 (pump==1) 
1936                                 and (valve_press>=0.75)
1937                         )
1938                         and
1939                         (
1940                                 (engine_running!=1)
1941                                 or (set_generator!=1)
1942                         )
1943                         and (gear_command_real!=gear_command)
1944                 )
1945                 {
1946                         pump=0;
1947                         setprop("fdm/jsbsim/systems/leftpanel/pump-input", 0);
1948                 }
1949
1950                 if (
1951                         (pump==1)
1952                         and (valve_press>=0.75)
1953                         and (left_handle_pos==0)
1954                         and (right_handle_pos==0)
1955                 )
1956                 {
1957                         setprop("fdm/jsbsim/gear/gear-cmd-norm-real", gear_command);
1958                         gear_command_real=gear_command;
1959                 }
1960
1961                 if (
1962                         ((gear_one_tored==0) and (gear_one_stuck==0))
1963                         or ((gear_two_tored==0) and (gear_two_stuck==0))
1964                         or ((gear_three_tored==0) and (gear_three_stuck==0))
1965                 )
1966                 {
1967                         setprop("processes/gear-move/sound-enabled", 1);
1968                 }
1969                 else
1970                 {
1971                         setprop("processes/gear-move/sound-enabled", 0);
1972                 }
1973                 speed_km=speed*1.852;
1974                 if (gear_one_tored==0)
1975                 {
1976                         one_gear_move(0, gear_one_pos, gear_command_real, gear_one_stuck, 4.2, 1.459, 105, speed_km, 375);
1977                 }
1978                 if (gear_two_tored==0)
1979                 {
1980                         one_gear_move(1, gear_two_pos, gear_command_real, gear_two_stuck, 4.8, 1.332, 95, speed_km, 355);
1981                 }
1982                 if (gear_three_tored==0)
1983                 {
1984                         one_gear_move(2, gear_three_pos, gear_command_real, gear_three_stuck, 5, 1.332, 95, speed_km, 350);
1985                 }
1986                 settimer(gearmove, 0.0);
1987         }
1988
1989 gearstuck = func (gear_num, sounded)
1990         {
1991                 setprop("fdm/jsbsim/gear/unit["~gear_num~"]/stuck", 1);
1992                 if (sounded==1)
1993                 {
1994                         setprop("sounds/gears-stuck/on", 1);
1995                 }
1996                 settimer(gearstucksoundoff, 1);
1997         }
1998
1999 gearstucksoundoff = func
2000         {
2001                 setprop("sounds/gears-stuck/on", 0);
2002         }
2003
2004 # set startup configuration
2005 init_gearmove = func
2006 {
2007         setprop("processes/gear-move/enabled", 1);
2008         setprop("processes/gear-move/sound-enabled", 1);
2009         setprop("sounds/gears-stuck/on", 0);
2010         #Aditional gears control restart
2011         setprop("fdm/jsbsim/systems/gearcontrol/control-input", 1);
2012         setprop("fdm/jsbsim/systems/gearcontrol/control-command", 1);
2013         setprop("fdm/jsbsim/systems/gearcontrol/control-switch", 1);
2014         setprop("fdm/jsbsim/gear/gear-cmd-norm-real", 1);
2015 }
2016
2017 init_gearmove();
2018
2019 gearmove();
2020
2021 #--------------------------------------------------------------------
2022 # Gear indicator
2023
2024 # helper 
2025 stop_gearindicator = func 
2026         {
2027                 setprop("instrumentation/gear-indicator/green-light-norm", 0);
2028                 setprop("instrumentation/gear-indicator/red-light-norm", 0);
2029         }
2030
2031 gearindicator = func 
2032         {
2033                 # check state
2034                 in_service = getprop("instrumentation/gear-indicator/serviceable" );
2035                 if (in_service == nil)
2036                 {
2037                         stop_gearindicator();
2038                         return ( settimer(gearindicator, 0.1) ); 
2039                 }
2040                 if ( in_service != 1 )
2041                 {
2042                         stop_gearindicator();
2043                         return ( settimer(gearindicator, 0.1) ); 
2044                 }
2045                 #get gear value
2046                 gear_one_pos=getprop("fdm/jsbsim/gear/unit[0]/pos-norm-real");
2047                 gear_two_pos=getprop("fdm/jsbsim/gear/unit[1]/pos-norm-real");
2048                 gear_three_pos=getprop("fdm/jsbsim/gear/unit[2]/pos-norm-real");
2049                 gear_one_tored=getprop("fdm/jsbsim/gear/unit[0]/tored");
2050                 gear_two_tored=getprop("fdm/jsbsim/gear/unit[1]/tored");
2051                 gear_three_tored=getprop("fdm/jsbsim/gear/unit[2]/tored");
2052                 # get instrumentation values    
2053                 red_pos=getprop("instrumentation/gear-indicator/red-pos-norm");
2054                 green_pos=getprop("instrumentation/gear-indicator/green-pos-norm");
2055                 button_check_pos=getprop("instrumentation/gear-indicator/button-check/switch-pos-norm");
2056                 #get bus value
2057                 bus=getprop("systems/electrical-real/bus");
2058                 if (
2059                         (gear_one_pos==nil)
2060                         or (gear_two_pos==nil)
2061                         or (gear_three_pos==nil)
2062                         or (gear_one_tored==nil)
2063                         or (gear_two_tored==nil)
2064                         or (gear_three_tored==nil)
2065                         or (red_pos==nil)
2066                         or (green_pos==nil)
2067                         or (button_check_pos==nil)
2068                         or (bus==nil)
2069                 )
2070                 {
2071                         stop_gearindicator();
2072                         return ( settimer(gearindicator, 0.1) ); 
2073                 }
2074                 switchmove("instrumentation/gear-indicator", "dummy/dummy");
2075                 switchmove("instrumentation/gear-indicator/button-check", "dummy/dummy");
2076                 if (bus==0)
2077                 {
2078                         setprop("instrumentation/gear-indicator/middle-up", 0);
2079                         setprop("instrumentation/gear-indicator/left-up", 0);
2080                         setprop("instrumentation/gear-indicator/right-up", 0);
2081                         setprop("instrumentation/gear-indicator/middle-down", 0);
2082                         setprop("instrumentation/gear-indicator/left-down", 0);
2083                         setprop("instrumentation/gear-indicator/right-down", 0);
2084                 }
2085                 else
2086                 {
2087                         setprop("instrumentation/gear-indicator/middle-up", (((gear_one_pos==0) or (button_check_pos==1))) and (gear_one_tored==0));
2088                         setprop("instrumentation/gear-indicator/left-up", (((gear_two_pos==0) or (button_check_pos==1))) and (gear_two_tored==0));
2089                         setprop("instrumentation/gear-indicator/right-up", (((gear_three_pos==0) or (button_check_pos==1))) and (gear_three_tored==0));
2090                         setprop("instrumentation/gear-indicator/middle-down", (((gear_one_pos==1) or (button_check_pos==1))) and (gear_one_tored==0));
2091                         setprop("instrumentation/gear-indicator/left-down", (((gear_two_pos==1) or (button_check_pos==1))) and (gear_two_tored==0));
2092                         setprop("instrumentation/gear-indicator/right-down", (((gear_three_pos==1) or (button_check_pos==1))) and (gear_three_tored==0));
2093                 }
2094                 settimer(gearindicator, 0.1);
2095         }
2096
2097 # set startup configuration
2098 init_gearindicator = func
2099 {
2100         setprop("instrumentation/gear-indicator/serviceable", 1);
2101         switchinit("instrumentation/gear-indicator", 1, "dummy/dummy");
2102         switchinit("instrumentation/gear-indicator/button-check", 0, "dummy/dummy");
2103         setprop("instrumentation/gear-indicator/red-pos-norm", 0.5);
2104         setprop("instrumentation/gear-indicator/green-pos-norm", 0.5);
2105         setprop("instrumentation/gear-indicator/middle-up", 0);
2106         setprop("instrumentation/gear-indicator/left-up", 0);
2107         setprop("instrumentation/gear-indicator/right-up", 0);
2108         setprop("instrumentation/gear-indicator/middle-down", 0);
2109         setprop("instrumentation/gear-indicator/left-down", 0);
2110         setprop("instrumentation/gear-indicator/right-down", 0);
2111 }
2112
2113 init_gearindicator();
2114
2115 # start gear indicator process first time
2116 gearindicator ();
2117
2118 #-----------------------------------------------------------------------
2119
2120 init_gearvalve=func
2121 {
2122         setprop("fdm/jsbsim/systems/gearvalve/left-handle-input", 0);
2123         setprop("fdm/jsbsim/systems/gearvalve/left-handle-command", 0);
2124         setprop("fdm/jsbsim/systems/gearvalve/left-handle-pos", 0);
2125         setprop("fdm/jsbsim/systems/gearvalve/right-handle-input", 0);
2126         setprop("fdm/jsbsim/systems/gearvalve/right-handle-command", 0);
2127         setprop("fdm/jsbsim/systems/gearvalve/right-handle-pos", 0);
2128         setprop("fdm/jsbsim/systems/gearvalve/safer-input", 0);
2129         setprop("fdm/jsbsim/systems/gearvalve/safer-command", 0);
2130         setprop("fdm/jsbsim/systems/gearvalve/safer-pos", 0);
2131         setprop("fdm/jsbsim/systems/gearvalve/valve-input", 0);
2132         setprop("fdm/jsbsim/systems/gearvalve/valve-command", 0);
2133         setprop("fdm/jsbsim/systems/gearvalve/valve-pos", 0);
2134         setprop("fdm/jsbsim/systems/gearvalve/pressure-pos", 1);
2135 }
2136
2137 init_gearvalve();
2138
2139 #--------------------------------------------------------------------
2140 # Flaps lamp
2141
2142 # helper 
2143 stop_flapslamp = func 
2144         {
2145                 setprop("instrumentation/flaps-lamp/lamp-ligth-norm", 0);
2146         }
2147
2148 flapslamp = func 
2149         {
2150                 # check power
2151                 in_service = getprop("instrumentation/flaps-lamp/serviceable" );
2152                 if (in_service == nil)
2153                 {
2154                         stop_flapslamp();
2155                         return ( settimer(flapslamp, 0.1) ); 
2156                 }
2157                 if ( in_service != 1 )
2158                 {
2159                         stop_flapslamp();
2160                         return ( settimer(flapslamp, 0.1) ); 
2161                 }
2162                 # get flaps value
2163                 flaps_pos = getprop("surface-positions/flap-pos-norm");
2164                 # get bus value
2165                 bus=getprop("systems/electrical-real/bus");
2166                 tored = getprop("fdm/jsbsim/fcs/flap-tored");
2167                 if ((flaps_pos == nil) or (bus==nil) or (tored==nil))
2168                 {
2169                         stop_flapslamp();
2170                         return ( settimer(flapslamp, 0.1) ); 
2171                 }
2172                 if ((bus==0) or (tored==1))
2173                 {
2174                         stop_flapslamp();
2175                         return ( settimer(flapslamp, 0.1) ); 
2176                 }
2177                 lamp_light=0;
2178                 if (flaps_pos>0) 
2179                 {
2180                         lamp_light=0.5;
2181                 }
2182                 if (flaps_pos==1) 
2183                 {
2184                         lamp_light=1;
2185                 }
2186                 setprop("instrumentation/flaps-lamp/lamp-ligth-norm", lamp_light);
2187                 settimer(flapslamp, 0.1);
2188   }
2189
2190 # set startup configuration
2191 init_flapslamp = func
2192 {
2193         setprop("instrumentation/flaps-lamp/serviceable", 1);
2194         setprop("instrumentation/flaps-lamp/lamp-ligth-norm", 0);
2195 }
2196
2197 init_flapslamp();
2198
2199 # start flaps lamp process first time
2200 flapslamp ();
2201
2202 #--------------------------------------------------------------------
2203 # Fuelometer and fuelometer lamp
2204
2205 # helper 
2206 stop_fuelometer = func 
2207         {
2208                 setprop("instrumentation/fuelometer/lamp-ligth-norm", 0);
2209         }
2210
2211 fuelometer = func 
2212         {
2213                 # check power
2214                 in_service = getprop("instrumentation/fuelometer/serviceable" );
2215                 if (in_service == nil)
2216                 {
2217                         stop_fuelometer();
2218                         return ( settimer(fuelometer, 10) ); 
2219                 }
2220                 if ( in_service != 1 )
2221                 {
2222                         stop_fuelometer();
2223                         return ( settimer(fuelometer, 10) ); 
2224                 }
2225                 # get fuel values
2226                 fuel_pos_zero = getprop("consumables/fuel/tank[0]/level-lbs");
2227                 fuel_pos_one = getprop("consumables/fuel/tank[1]/level-lbs");
2228                 fuel_pos_two = getprop("consumables/fuel/tank[2]/level-lbs");
2229                 fuel_pos_three = getprop("consumables/fuel/tank[3]/level-lbs");
2230                 fuel_pos_four = getprop("consumables/fuel/tank[4]/level-lbs");
2231                 fuel_control_pos=getprop("fdm/jsbsim/systems/fuelcontrol/control-switch");
2232                 third_tank_pump=getprop("systems/electrical-real/outputs/third-tank-pump/volts-norm");
2233                 # get bus value
2234                 bus=getprop("systems/electrical-real/bus");
2235                 if (
2236                         (fuel_pos_zero == nil)
2237                         or (fuel_pos_one == nil)  
2238                         or (fuel_pos_two == nil)
2239                         or (fuel_pos_three == nil)
2240                         or (fuel_pos_four == nil)
2241                         or (fuel_control_pos == nil)
2242                         or (third_tank_pump == nil)  
2243                         or (bus==nil)
2244                 )
2245                 {
2246                         stop_fuelometer();
2247                         return ( settimer(fuelometer, 10) ); 
2248                 }
2249                 if (bus==0)
2250                 {
2251                         setprop("instrumentation/fuelometer/fuel-level-lbs", 0);
2252                         stop_fuelometer();
2253                         return ( settimer(fuelometer, 10) ); 
2254                 }
2255                 if (fuel_control_pos==0)
2256                 {
2257                         if (third_tank_pump==0)
2258                         {
2259                                 fuel_result=fuel_pos_one*0.453;
2260                         }
2261                         else
2262                         {
2263                                 fuel_result=(fuel_pos_three+fuel_pos_four)*0.453;
2264                         }
2265                 }
2266                 else
2267                 {
2268                         fuel_result=fuel_pos_two*0.453;
2269                 }
2270                 setprop("instrumentation/fuelometer/fuel-level-litres", fuel_result);
2271                 lamp_light=0;
2272                 if (fuel_pos_one<300) 
2273                 {
2274                         lamp_light=1;
2275                 }
2276                 setprop("instrumentation/fuelometer/lamp-ligth-norm", lamp_light);
2277                 settimer(fuelometer, 10);
2278   }
2279
2280 # set startup configuration
2281
2282 init_fuelometer = func 
2283 {
2284         setprop("instrumentation/fuelometer/serviceable", 1);
2285         setprop("instrumentation/fuelometer/lamp-ligth-norm", 0);
2286         setprop("instrumentation/fuelometer/fuel-level-lbs", 0);
2287 }
2288
2289 init_fuelometer();
2290
2291 # start fuel lamp process first time
2292 fuelometer ();
2293
2294 #--------------------------------------------------------------------
2295 # Altimeter
2296
2297 # set startup configuration
2298 init_altimeter = func 
2299 {
2300         setprop("fdm/jsbsim/systems/altimeter/serviceable", 1);
2301 }
2302
2303 init_altimeter();
2304
2305 #--------------------------------------------------------------------
2306 # Altimeter lamp
2307
2308 # helper 
2309 stop_altlamp = func 
2310         {
2311                 setprop("instrumentation/altimeter-lamp/lamp-ligth-norm", 0);
2312         }
2313
2314 altlamp = func 
2315         {
2316                 # check power
2317                 in_service = getprop("instrumentation/altimeter-lamp/serviceable" );
2318                 if (in_service == nil)
2319                 {
2320                         stop_altlamp();
2321                         return ( settimer(altlamp, 0.1) ); 
2322                 }
2323                 if ( in_service != 1 )
2324                 {
2325                         stop_altlamp();
2326                         return ( settimer(altlamp, 0.1) ); 
2327                 }
2328                 # get altitude and electrical bus values
2329                 alt_pos = getprop("fdm/jsbsim/position/h-sl-meters");           
2330                 bus=getprop("systems/electrical-real/bus");
2331                 if ((alt_pos == nil) or (bus==nil))
2332                 {
2333                         stop_altlamp();
2334                         return ( settimer(altlamp, 0.1) ); 
2335                 }
2336                 lamp_light=0;
2337                 if ((alt_pos<250) and (bus==1))
2338                 {
2339                         lamp_light=1;
2340                 }
2341                 setprop("instrumentation/altimeter-lamp/lamp-ligth-norm", lamp_light);
2342                 settimer(altlamp, 0.1);
2343         }
2344
2345 # set startup configuration
2346 init_altlamp = func 
2347 {
2348         setprop("instrumentation/altimeter-lamp/serviceable", 1);
2349         setprop("instrumentation/altimeter-lamp/lamp-ligth-norm", 0);
2350 }
2351
2352 init_altlamp();
2353
2354 # start altimeter lamp process first time
2355 altlamp ();
2356
2357 #--------------------------------------------------------------------
2358 # Gear caution lamp
2359
2360 # helper 
2361 stop_gearlamp = func 
2362         {
2363                 setprop("instrumentation/gear-lamp/lamp-ligth-norm", 0);
2364         }
2365
2366 gearlamp = func 
2367         {
2368                 # check power
2369                 in_service = getprop("instrumentation/gear-lamp/serviceable" );
2370                 if (in_service == nil)
2371                 {
2372                         stop_gearlamp();
2373                         return ( settimer(gearlamp, 0.1) ); 
2374                 }
2375                 if ( in_service != 1 )
2376                 {
2377                         stop_gearlamp();
2378                         return ( settimer(gearlamp, 0.1) ); 
2379                 }
2380                 # get altitude values
2381                 gear_one_pos=getprop("fdm/jsbsim/gear/unit[0]/pos-norm-real");
2382                 gear_two_pos=getprop("fdm/jsbsim/gear/unit[1]/pos-norm-real");
2383                 gear_three_pos=getprop("fdm/jsbsim/gear/unit[2]/pos-norm-real");
2384                 flaps_pos=getprop("fdm/jsbsim/fcs/flap-pos-norm");
2385                 #get bus value
2386                 bus=getprop("systems/electrical-real/bus");
2387                 if (
2388                         (gear_one_pos==nil)
2389                         or (gear_two_pos==nil)
2390                         or (gear_three_pos==nil)
2391                         or (flaps_pos==nil)
2392                         or (bus==nil)
2393                 )
2394                 {
2395                         stop_gearlamp();
2396                         return ( settimer(gearlamp, 0.1) ); 
2397                 }
2398                 if (bus==0)
2399                 {
2400                         stop_gearlamp();
2401                         return ( settimer(gearlamp, 0.1) ); 
2402                 }
2403                 lamp_light=0;
2404                 if (
2405                         (flaps_pos>0) 
2406                         and 
2407                         (
2408                                 (gear_one_pos!=1)
2409                                 or (gear_two_pos!=1)
2410                                 or (gear_three_pos!=1)
2411                         )
2412                 )
2413                 {
2414                         lamp_light=1;
2415                 }
2416                 setprop("instrumentation/gear-lamp/lamp-ligth-norm", lamp_light);
2417                 settimer(gearlamp, 0.1);
2418         }
2419
2420 # set startup configuration
2421 init_gearlamp = func 
2422 {
2423         setprop("instrumentation/gear-lamp/serviceable", 1);
2424         setprop("instrumentation/gear-lamp/lamp-ligth-norm", 0);
2425 };
2426
2427 init_gearlamp();
2428
2429 # start gear lamp process first time
2430 gearlamp ();
2431
2432 #--------------------------------------------------------------------
2433 # Cabin manometer
2434
2435 # helper 
2436 stop_oxypressmeter = func 
2437         {
2438                 setprop("instrumentation/oxygen-pressure-meter/lamp", 0);
2439         }
2440
2441 oxypressmeter = func 
2442         {
2443                 # check power
2444                 in_service = getprop("instrumentation/oxygen-pressure-meter/serviceable" );
2445                 if (in_service == nil)
2446                 {
2447                         stop_oxypressmeter();
2448                         return ( settimer(oxypressmeter, 6) ); 
2449                 }
2450                 if( in_service != 1 )
2451                 {
2452                         stop_oxypressmeter();
2453                         return ( settimer(oxypressmeter, 6) ); 
2454                 }
2455                 # get cabin pressure
2456                 pressure = getprop("instrumentation/manometer/cabin-pressure");
2457                 # get oxygen value
2458                 oxy_pos = getprop("consumables/oxygen/pressure-norm");
2459                 if ((pressure == nil) or (oxy_pos == nil))
2460                 {
2461                         stop_oxypressmeter();
2462                         return ( settimer(oxypressmeter, 6) ); 
2463                 }
2464                 oxy_on=0;
2465                 #If cabin altitude is highter than 1km automatically put oxy mask on
2466                 #and consume oxygen with speed ~1% per min
2467                 if (pressure<26.4)
2468                 {
2469                         if (oxy_pos>0)
2470                         {
2471                                 oxy_on=1;
2472                                 oxy_pos=oxy_pos-0.00033;
2473                         }
2474                 }
2475                 setprop("instrumentation/oxygen-pressure-meter/oxygen-on", oxy_on);
2476                 setprop("consumables/oxygen/pressure-norm", oxy_pos);
2477                 settimer(oxypressmeter, 6);
2478         }
2479
2480 # set startup configuration
2481 init_oxypressmeter = func 
2482 {
2483         setprop("instrumentation/oxygen-pressure-meter/serviceable", 1);
2484         setprop("instrumentation/oxygen-pressure-meter/oxygen-on", 0);
2485         setprop("consumables/oxygen/pressure-norm", 0.75);
2486 }
2487
2488 init_oxypressmeter();
2489
2490 # start oxygen pressure meter process first time
2491 oxypressmeter ();
2492
2493 #----------------------------------------------------------------------
2494 #Gyrocompass
2495
2496 init_gyrocompass = func 
2497 {
2498         setprop("fdm/jsbsim/systems/gyrocompass/serviceable", 1);
2499         setprop("fdm/jsbsim/systems/gyrocompass/on", 0);
2500         setprop("fdm/jsbsim/systems/gyrocompass/button-input", 0);
2501         setprop("fdm/jsbsim/systems/gyrocompass/button-command", 0);
2502         setprop("fdm/jsbsim/systems/gyrocompass/button-pos", 0);
2503         setprop("fdm/jsbsim/systems/gyrocompass/button-switch", 0);
2504         setprop("fdm/jsbsim/systems/gyrocompass/heading-great-drift", 0);
2505         setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-return-rad", 0);
2506         setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-drift-rad", 0);
2507         setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-stored-rad", 0);
2508         setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-input-rad", 0);
2509         var heading=getprop("fdm/jsbsim/systems/attitude/heading-true-rad");
2510         if (heading!=nil)
2511         {
2512                 setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-rad", -heading);
2513         }
2514         else
2515         {
2516                 setprop("fdm/jsbsim/systems/gyrocompass/heading-zero-rad", 0);
2517         }
2518         setprop("fdm/jsbsim/systems/gyrocompass/offset-deg", 0);
2519         setprop("fdm/jsbsim/systems/gyrocompass/offset-rad", 0);
2520         setprop("fdm/jsbsim/systems/gyrocompass/heading-resulted-rad", 0);
2521         setprop("fdm/jsbsim/systems/gyrocompass/heading-stored-rad", 0);
2522         setprop("fdm/jsbsim/systems/gyrocompass/heading-indicated-rad", 0);
2523         setprop("fdm/jsbsim/systems/gyrocompass/heading-indicated-deg", 0);
2524 }
2525
2526 init_gyrocompass();
2527 #--------------------------------------------------------------------
2528 # Flaps breaks process
2529
2530 # helper 
2531 stop_flapsbreaksprocess = func 
2532         {
2533         }
2534
2535 flapsbreaksprocess = func 
2536         {
2537                 # get flaps values
2538                 flaps_pos_deg = getprop("fdm/jsbsim/fcs/flap-pos-deg");
2539                 tored = getprop("fdm/jsbsim/fcs/flap-tored");
2540                 #get speed value
2541                 speed=getprop("velocities/airspeed-kt");
2542                 if (
2543                         (flaps_pos_deg == nil)
2544                         or (tored==nil)
2545                         or (speed==nil)
2546                 )
2547                 {
2548                         stop_flapsbreaksprocess();
2549                         return ( settimer(flapsbreaksprocess, 0.1) ); 
2550                 }
2551                 if ((tored==0) and (flaps_pos_deg>5))
2552                 {
2553                         speed_km=speed*1.852;
2554                         #max 450 km\h on 20 deg, 350 on 55 deg
2555                         speed_limit=450-((flaps_pos_deg-20)/(55-20))*(450-350);
2556                         setprop("fdm/jsbsim/fcs/flap-speed-limit", speed_limit);
2557                         if ((speed_km>speed_limit) and (speed_limit>0))
2558                         {
2559                                 tored=1;
2560                                 setprop("fdm/jsbsim/fcs/flap-tored", 1);
2561                                 setprop("fdm/jsbsim/fcs/flap-cmd-norm-real", 0);
2562                                 setprop("fdm/jsbsim/fcs/flap-pos-deg", 0);
2563                                 setprop("fdm/jsbsim/fcs/flap-pos-norm", 0);
2564                                 setprop("ai/submodels/left-flap-drop", 1);
2565                                 setprop("ai/submodels/right-flap-drop", 1);
2566                                 flapstoredsound();
2567                         }
2568                 }
2569                 flaps_impact=getprop("ai/submodels/left-flap-impact");
2570                 if (flaps_impact!=nil)
2571                 {
2572                         if (flaps_impact!="")
2573                         {
2574                                 setprop("ai/submodels/left-flap-impact", "");
2575                                 flaps_touch_down();
2576                         }
2577                 }
2578                 flaps_impact=getprop("ai/submodels/right-flap-impact");
2579                 if (flaps_impact!=nil)
2580                 {
2581                         if (flaps_impact!="")
2582                         {
2583                                 setprop("ai/submodels/right-flap-impact", "");
2584                                 flaps_touch_down();
2585                         }
2586                 }
2587                 left_flap_drop=getprop("ai/submodels/left-flap-drop");
2588                 right_flap_drop=getprop("ai/submodels/right-flap-drop");
2589                 if ((left_flap_drop!=nil) and (right_flap_drop!=nil))
2590                 {
2591                         if ((tored==0) and ((left_flap_drop==1) or (right_flap_drop==1)))
2592                         {
2593                                 setprop("ai/submodels/left-flap-drop", 0);
2594                                 setprop("ai/submodels/right-flap-drop", 0);
2595                         }
2596                 }
2597                 settimer(flapsbreaksprocess, 0.1);
2598         }
2599
2600 # set startup configuration
2601 init_flapsbreaksprocess = func 
2602 {
2603         setprop("fdm/jsbsim/fcs/flap-tored", 0);
2604 }
2605
2606 init_flapsbreaksprocess();
2607
2608
2609 flapstoredsound = func
2610         {
2611                 setprop("sounds/flaps-tored/on", 1);
2612                 settimer(flapstoredsoundoff, 0.3);
2613         }
2614
2615 flapstoredsoundoff = func
2616         {
2617                 setprop("sounds/flaps-tored/on", 0);
2618         }
2619
2620 flaps_touch_down = func
2621         {
2622                 setprop("sounds/flaps-down/on", 1);
2623                 settimer(end_flaps_touch_down, 3);
2624         }
2625
2626 end_flaps_touch_down = func
2627         {
2628                 setprop("sounds/flaps-down/on", 0);
2629         }
2630
2631 flapsbreaksprocess();
2632
2633 #--------------------------------------------------------------------
2634 # Flaps control
2635
2636 # helper 
2637 stop_flapsprocess = func 
2638         {
2639         }
2640
2641 flapsprocess = func 
2642         {
2643                 # check power
2644                 var in_service = getprop("fdm/jsbsim/systems/flapscontrol/serviceable" );
2645                 if (in_service == nil)
2646                 {
2647                         stop_flapsprocess();
2648                         return ( settimer(flapsprocess, 0.1) ); 
2649                 }
2650                 if ( in_service != 1 )
2651                 {
2652                         stop_flapsprocess();
2653                         return ( settimer(flapsprocess, 0.1) ); 
2654                 }
2655                 # get flaps values
2656                 var tored = getprop("fdm/jsbsim/fcs/flap-tored");
2657                 var flaps_command_pos = getprop("fdm/jsbsim/fcs/flap-cmd-norm");
2658                 # get instrumentation values
2659                 var flaps_go=getprop("fdm/jsbsim/systems/flapscontrol/flaps-go");
2660                 var valve_press=getprop("fdm/jsbsim/systems/flapsvalve/pressure-pos");
2661                 #get pump value
2662                 var pump=getprop("systems/electrical-real/outputs/pump/volts-norm");
2663                 var engine_running=getprop("engines/engine/running");
2664                 var set_generator=getprop("fdm/jsbsim/systems/rightpanel/generator-switch");
2665                 if (
2666                         (tored==nil)
2667                         or (flaps_command_pos == nil)
2668                         or (flaps_go==nil)
2669                         or (valve_press==nil)
2670                         or (pump==nil)
2671                         or (engine_running==nil)
2672                         or (set_generator==nil)
2673                 )
2674                 {
2675                         stop_flapsprocess();
2676                         return ( settimer(flapsprocess, 0.1) ); 
2677                 }
2678                 if ((pump==1) and (valve_press==1.0) and (tored==0) and (flaps_go==1))
2679                 {
2680                         if ((engine_running==1) and (set_generator==1))
2681                         {
2682                                 setprop("fdm/jsbsim/fcs/flap-cmd-norm-real", flaps_command_pos);
2683                         }
2684                         else
2685                         {
2686                                 setprop("fdm/jsbsim/systems/leftpanel/pump-input", 0);
2687                         }
2688                 }
2689                 settimer(flapsprocess, 0.1);
2690         }
2691
2692 # set startup configuration
2693 init_flapsprocess = func 
2694 {
2695         setprop("fdm/jsbsim/fcs/flap-cmd-norm", 0);
2696         setprop("fdm/jsbsim/fcs/flap-cmd-norm-real", 0);
2697         setprop("fdm/jsbsim/fcs/flap-pos-deg", 0);
2698         setprop("fdm/jsbsim/fcs/flap-pos-norm", 0);
2699 }
2700
2701 init_flapsprocess();
2702
2703 # start flaps control process first time
2704 flapsprocess ();
2705
2706 #--------------------------------------------------------------------
2707 # Ignition button
2708
2709 # helper 
2710 stop_ignitionbutton = func 
2711         {
2712         }
2713
2714 ignitionbutton = func 
2715         {
2716                 starter_key=getprop("controls/engines/engine/starter-key");
2717                 starter_command=getprop("controls/engines/engine/starter-command");
2718                 if (
2719                         (starter_key==nil)
2720                         or
2721                         (starter_command==nil)
2722                 )
2723                 {
2724                         stop_ignitionbutton();
2725                         return ( settimer(ignitionbutton, 0.1) ); 
2726                 }
2727                 if (
2728                         (starter_key==1) 
2729                         or (starter_command==1)
2730                 )
2731                 {
2732                         setprop("fdm/jsbsim/systems/ignitionbutton/button-input", 1);
2733                 }
2734                 else
2735                 {
2736                         setprop("fdm/jsbsim/systems/ignitionbutton/button-input", 0);
2737                 }
2738                 settimer(ignitionbutton, 0.1);
2739           }
2740
2741 # set startup configuration
2742 init_ignitionbutton = func 
2743 {
2744         setprop("controls/engines/engine/starter-command", 0);
2745         setprop("controls/engines/engine/starter-key", 0);
2746 }
2747
2748 init_ignitionbutton();
2749
2750 # start gas control process first time
2751 ignitionbutton ();
2752
2753
2754 #--------------------------------------------------------------------
2755 #Engine process
2756
2757 # helper 
2758 stop_engineprocess = func 
2759         {
2760         }
2761
2762 engineprocess=func
2763         {
2764                 var in_service = getprop("processes/engine/on");
2765                 if (in_service == nil)
2766                 {
2767                         stop_engineprocess();
2768                         return ( settimer(engineprocess, 0.1) ); 
2769                 }
2770                 if ( in_service != 1 )
2771                 {
2772                         stop_engineprocess();
2773                         return ( settimer(engineprocess, 0.1) ); 
2774                 }
2775                 var starter=getprop("controls/engines/engine/starter");
2776                 var starter_pressed=getprop("fdm/jsbsim/systems/ignitionbutton/button-switch");
2777                 var running=getprop("engines/engine/running");
2778                 var out_of_fuel=getprop("engines/engine/out-of-fuel");
2779                 var engine_n2=getprop("engines/engine/n2");
2780                 var engine_thrust=getprop("engines/engine/thrust_lb");
2781                 var engine_temperature=getprop("engines/engine/egt-degf");
2782                 #To compaility wth FG previous versions
2783                 if (engine_temperature==nil)
2784                 {
2785                         engine_temperature=getprop("engines/engine/egt_degf");
2786                 }
2787                 var pilot_g=getprop("fdm/jsbsim/accelerations/Nz");
2788                 var bus=getprop("systems/electrical-real/bus");
2789                 var gen_on=getprop("fdm/jsbsim/systems/rightpanel/generator-switch");
2790                 var pump=getprop("systems/electrical-real/outputs/pump/volts-norm");
2791                 var third_tank_pump=getprop("systems/electrical-real/outputs/third-tank-pump/volts-norm");
2792                 var fuel_control_pos=getprop("fdm/jsbsim/systems/fuelcontrol/control-input");
2793                 var tank=[0,0,0,0,0];
2794                 var tank_selected=[0,0,0,0,0];
2795                 tank[0]=getprop("consumables/fuel/tank[0]/level-gal_us");
2796                 tank[1]=getprop("consumables/fuel/tank[1]/level-gal_us");
2797                 tank[2]=getprop("consumables/fuel/tank[2]/level-gal_us");
2798                 tank[3]=getprop("consumables/fuel/tank[3]/level-gal_us");
2799                 tank[4]=getprop("consumables/fuel/tank[4]/level-gal_us");
2800                 var ignition_power=getprop("systems/electrical-real/outputs/ignition/on");
2801                 #Timers
2802                 var simulation_time=getprop("fdm/jsbsim/sim-time-sec");
2803                 var starter_begin_time=getprop("engines/engine/starter-begin-time");
2804                 var low_throttle_prev_time=getprop("engines/engine/low-throttle-prev-time");
2805                 var low_throttle_time=getprop("engines/engine/low-throttle-time");
2806                 var high_temperature_prev_time=getprop("engines/engine/high-temperature-prev-time");
2807                 var high_temperature_time=getprop("engines/engine/high-temperature-time");
2808                 var ignition_power_begin_time=getprop("engines/engine/ignition-power-begin-time");
2809                 if (
2810                         (starter==nil)
2811                         or (starter_pressed==nil)
2812                         or (running==nil)
2813                         or (out_of_fuel==nil)
2814                         or (engine_n2==nil)
2815                         or (engine_thrust==nil)
2816                         or (engine_temperature==nil)
2817                         or (pilot_g==nil)
2818                         or (bus==nil)
2819                         or (gen_on==nil)
2820                         or (pump==nil)
2821                         or (third_tank_pump==nil)
2822                         or (fuel_control_pos==nil)
2823                         or (tank[0]==nil)
2824                         or (tank[1]==nil)
2825                         or (tank[2]==nil)
2826                         or (tank[3]==nil)
2827                         or (tank[4]==nil)
2828                         or (ignition_power==nil)
2829                         or (simulation_time==nil)
2830                         or (starter_begin_time==nil)
2831                         or (low_throttle_prev_time==nil)
2832                         or (low_throttle_time==nil)
2833                         or (high_temperature_prev_time==nil)
2834                         or (high_temperature_time==nil)
2835                         or (ignition_power_begin_time==nil)
2836                 )
2837                 {
2838                         stop_engineprocess();
2839                         setprop("engines/engine/error", 1);
2840                         return ( settimer(engineprocess, 0.1) ); 
2841                 }
2842                 setprop("engines/engine/error", 0);
2843                 if (fuel_control_pos==1)
2844                 {
2845                         if ((tank[2]>0) and (pump>0))
2846                         {
2847                                 setprop("consumables/fuel/tank[2]/selected", 1);
2848                                 tank_selected[2]=1;
2849                                 setprop("consumables/fuel/tank[1]/selected", 0);
2850                                 setprop("consumables/fuel/tank[3]/selected", 0);
2851                                 setprop("consumables/fuel/tank[4]/selected", 0);
2852                                 tank_selected[1]=0;
2853                                 tank_selected[3]=0;
2854                                 tank_selected[4]=0;
2855                         }
2856                         else
2857                         {
2858                                 setprop("consumables/fuel/tank[2]/selected", 0);
2859                                 tank_selected[2]=0;
2860                         }
2861                 }
2862                 if ((fuel_control_pos==0) or (tank[2]<=0))
2863                 {
2864                         if (third_tank_pump!=0)
2865                         {
2866                                 if (tank[3]>0)
2867                                 {
2868                                         setprop("consumables/fuel/tank[3]/selected", 1);
2869                                         tank_selected[3]=1;
2870                                         setprop("consumables/fuel/tank[1]/selected", 0);
2871                                         setprop("consumables/fuel/tank[2]/selected", 0);
2872                                         tank_selected[1]=0;
2873                                         tank_selected[2]=0;
2874                                 }
2875                                 else
2876                                 {
2877                                         setprop("consumables/fuel/tank[3]/selected", 0);
2878                                         tank_selected[3]=0;
2879                                 }
2880                                 if (tank[4]>0)
2881                                 {
2882                                         setprop("consumables/fuel/tank[4]/selected", 1);
2883                                         tank_selected[4]=1;
2884                                         setprop("consumables/fuel/tank[1]/selected", 0);
2885                                         setprop("consumables/fuel/tank[2]/selected", 0);
2886                                         tank_selected[1]=0;
2887                                         tank_selected[2]=0;
2888                                 }
2889                                 else
2890                                 {
2891                                         setprop("consumables/fuel/tank[4]/selected", 0);
2892                                         tank_selected[4]=0;
2893                                 }
2894                         }
2895                         if ((third_tank_pump==0) or ((tank[3]<=0) and (tank[4]<=0)))
2896                         {
2897                                 if ((tank[1]>0) and (pump>0))
2898                                 {
2899                                         setprop("consumables/fuel/tank[1]/selected", 1);
2900