Remove old references to /sim/auto-coordination.
[fg:toms-fgdata.git] / Aircraft / pa22 / Nasal / pa22.nas
1 ######
2 #
3 # Fuel system
4 #
5 ######
6
7
8 # Fuel valve selector
9
10 var fuel_valve = props.globals.getNode("/controls/fuel/fuel-valve-pos", 1);
11
12 var handle_up = func {
13     var handle = fuel_valve.getValue() + 1;
14     if (handle == 2) {handle = -1};
15     fuel_valve.setValue(handle);
16     set_tanks(handle);
17 }
18
19 var handle_down = func {
20     var handle = fuel_valve.getValue() - 1;
21     if (handle == -2) {handle = 1};
22     fuel_valve.setValue(handle);
23     set_tanks(handle);
24 }
25
26 var set_tanks = func(position) {
27     var valve_left = props.globals.getNode("/consumables/fuel/tank[0]/selected");
28     var valve_right = props.globals.getNode("/consumables/fuel/tank[1]/selected");
29     var out_of_fuel = props.globals.getNode("/engines/engine/out-of-fuel");
30     if (position == -1) {
31         valve_left.setValue(0);
32         valve_right.setValue(0);
33     } elsif (position == 0) {
34         valve_left.setValue(1);
35         valve_right.setValue(0);
36         out_of_fuel.setValue(0);
37     } elsif (position == 1) {
38         valve_left.setValue(0);
39         valve_right.setValue(1);
40         out_of_fuel.setValue(0);
41     }
42 }
43
44
45 # Primer
46
47 var primer = props.globals.getNode("/controls/engines/engine[0]/primer-pump",1);
48 var out_of_fuel = props.globals.getNode("/engines/engine/out-of-fuel", 1);
49
50 var primer_check = func {
51         if(primer.getValue() < 6) {
52                 out_of_fuel.setValue(1);
53                 out_of_fuel.setAttribute("writable", 0);
54         } else {out_of_fuel.setAttribute("writable", 1);}
55             if(fuel_valve.getValue() != -1) {
56                 out_of_fuel.setValue(0);
57         }
58 }
59
60 var primer_pump_switch = func (v = 1) {
61     if(fuel_valve.getValue() == -1) {
62         return;
63     } else {primer.setValue(primer.getValue() + v);}
64 }
65
66
67 # Load saved fuel level on sim initialization
68
69 var tank_0 = props.globals.getNode("/consumables/fuel/tank[0]/level-gal_us", 1);
70 var tank_1 = props.globals.getNode("/consumables/fuel/tank[1]/level-gal_us", 1);
71 var copy_0 = props.globals.getNode("/sim/model/fuel/tank[0]/level-gal_us", 1);
72 var copy_1 = props.globals.getNode("/sim/model/fuel/tank[1]/level-gal_us", 1);
73
74 var update_fuel = func {
75
76     if (copy_0.getValue() != nil) {
77         tank_0.setValue(copy_0.getValue());
78     }
79     if (copy_1.getValue() != nil) {
80         tank_1.setValue(copy_1.getValue());
81     }
82
83     setlistener("/consumables/fuel/tank[0]/level-gal_us", func {
84         copy_0.setValue(tank_0.getValue());
85     });
86
87     setlistener("/consumables/fuel/tank[1]/level-gal_us", func {
88         copy_1.setValue(tank_1.getValue());
89     });
90 }
91
92 ################################################################################
93
94 # Record engine's running time
95
96 var hours = props.globals.getNode("/engines/engine[0]/hours-running", 1);
97 var rpm = props.globals.getNode("/engines/engine[0]/rpm", 1);
98 var magnetos = props.globals.getNode("/controls/engines/engine[0]/magnetos", 1);
99 var cruise_rpm = 2600;
100
101 if (hours.getValue() == nil) {hours.setValue(0);}
102 if (hours.getValue() >= 10000) {hours.setValue(hours.getValue() - 10000);}
103
104 var add_hours = func {
105     var rpm = rpm.getValue();
106     if (rpm != nil and magnetos.getValue()) {
107         var add_time = rpm / cruise_rpm / 3600;
108         hours.setValue(hours.getValue() + add_time);
109     }
110     settimer(add_hours, 1);
111 }
112
113
114 # Set landing and taxi lights spots intensity according to time of day
115
116 var landing_light = props.globals.getNode("systems/electrical/outputs/landing-light", 1);
117 var taxi_light = props.globals.getNode("systems/electrical/outputs/taxi-lights", 1);
118 var sun = props.globals.getNode("/sim/time/sun-angle-rad", 1);
119 var altitude_AGL = props.globals.getNode("/position/altitude-agl-ft", 1);
120 var landing_lights_intensity = props.globals.getNode("/sim/model/landing_lights-intensity", 1);
121
122 landing_lights_intensity.setValue(1.0);
123
124 var update_lights_intensity = func {
125     if (landing_light.getValue() > 10 or taxi_light.getValue() > 10){
126         intensity = -(sun.getValue()) + 1.7;
127         if (intensity < 0) {intensity = 0;}
128         if (intensity > 1) {intensity = 1;}
129         landing_lights_intensity.setValue(intensity + (altitude_AGL.getValue() * 0.003));
130     }
131     settimer(update_lights_intensity, 0.2);
132 }
133
134 # Autostart
135
136 var autostart = func {
137     tripacer.master_switch(1);
138     set_tanks(0);
139     setprop("/controls/fuel/fuel-valve-pos", 0);
140     setprop("/controls/engines/engine[0]/mixture", 1);
141     setprop("/controls/engines/engine[0]/magnetos", 3);
142         setprop("/controls/engines/engine[0]/primer-pump", 6);
143         tripacer.start_switch(1);
144         settimer(func {tripacer.start_switch(0)}, 2);
145 }
146
147
148 # Auto Coordination
149
150 var auto_coordination_switch = func (v = -1) {
151         toggle=getprop("controls/flight/auto-coordination");
152   
153         if(v == -1)
154         {
155                 toggle=1-toggle;
156         }
157         else
158         {
159                 toggle = v;
160         }
161         
162         setprop("controls/flight/auto-coordination",toggle);
163   
164 #       if(toggle == 1)
165 #       {
166 #               gui.popupTip("Auto Coordination on");
167 #       }
168 #       else
169 #       {
170 #               gui.popupTip("Auto Coordination off");
171 #       }
172 }
173
174
175 # Tyre smoke and rain
176
177 # Overwrite default behavior of aircraft.rain class and make rain visible from cockpit.
178 var  precipitation_enable= props.globals.getNode("sim/rendering/precipitation-aircraft-enable", 1);
179 precipitation_enable.setBoolValue(1);
180 precipitation_enable.setAttribute("writable", 0);
181
182
183 var tyresmoke_0 = aircraft.tyresmoke.new(0);
184 var tyresmoke_1 = aircraft.tyresmoke.new(1);
185 var tyresmoke_2 = aircraft.tyresmoke.new(2);
186 aircraft.rain.init();
187
188 var smoke_rain_update = func {
189     aircraft.rain.update();
190     tyresmoke_0.update();
191     tyresmoke_1.update();
192     tyresmoke_2.update();
193     settimer(smoke_rain_update, 0);
194 }
195
196 # Detect precipitation to hide canopy reflections.
197
198 var raining = props.globals.getNode("environment/metar/rain-norm", 1);
199 var snowing = props.globals.getNode("environment/metar/snow-norm", 1);
200 var precipitation_level = props.globals.getNode("environment/params/precipitation-level-ft", 1);
201 var altitude_MSL = props.globals.getNode("/position/altitude-ft", 1);
202 var show_canopy = props.globals.getNode("sim/model/show-canopy", 1);
203
204 show_canopy.setBoolValue(0);
205
206 var update_canopy_visibility = func {
207     if (altitude_MSL.getValue() <= precipitation_level.getValue()) {
208         if (raining.getValue() or snowing.getValue()){
209             show_canopy.setValue(0);
210         }else {show_canopy.setValue(1);}
211     }else {show_canopy.setValue(1);}
212     settimer(update_canopy_visibility, 0);
213 }
214
215 # Update MP animations
216
217 var nav_lights_local = "systems/electrical/outputs/nav-lights";
218 var landing_light_local = "systems/electrical/outputs/landing-light";
219 var taxi_light_local = "systems/electrical/outputs/taxi-lights";
220
221 var nav_lights_multi = "sim/multiplay/generic/float[0]";
222 var landing_light_multi = "sim/multiplay/generic/float[1]";
223 var taxi_light_multi = "sim/multiplay/generic/float[2]";
224
225 var MP_update = func {
226     setprop(nav_lights_multi, getprop(nav_lights_local));
227     setprop(landing_light_multi, getprop(landing_light_local));
228     setprop(taxi_light_multi, getprop(taxi_light_local));
229     settimer(MP_update, 0.5);
230 }
231
232
233 # Save configuration
234
235 var save_list = ["/engines/engine[0]/hours-running",
236                  "/sim/model/fuel/tank[0]/level-gal_us",
237                  "/sim/model/fuel/tank[1]/level-gal_us",
238                  "/sim/model/move-compass",
239                  "/sim/model/yokes-alpha",
240                  "/sim/model/show-wheel-pants",
241                  "/sim/weight[0]/weight-lb",
242                  "/sim/weight[1]/weight-lb",
243                  "/sim/weight[2]/weight-lb",
244                  "/sim/weight[3]/weight-lb",
245                  "/sim/weight[4]/weight-lb",
246                  "/sim/model/livery-cabin/name",
247                  "/sim/model/livery-fuel-valve/name",
248                  "/sim/model/livery-fuselage/name",
249                  "/sim/model/livery-wheel-pants/name",
250                  "/sim/model/livery-wings/name"];
251
252 aircraft.data.add(save_list);
253
254 ################################################################################
255 # Initialize systems
256
257 var initialize = setlistener("/sim/signals/fdm-initialized", func {
258     set_tanks(fuel_valve.getValue());
259     update_fuel();
260     add_hours();
261     update_lights_intensity();
262     smoke_rain_update();
263     update_canopy_visibility();
264     MP_update();
265     removelistener(initialize);
266 });