| 1 |
var startEngine = func(v = 1, which...) { |
| 2 |
if (!v and !size(which)) |
| 3 |
return props.setAll("/controls/engines/engine", "starter", 0); |
| 4 |
if(size(which)) { |
| 5 |
foreach(var i; which) |
| 6 |
foreach(var e; engines) |
| 7 |
if(e.index == i) |
| 8 |
e.controls.getNode("starter").setBoolValue(v); |
| 9 |
} else { |
| 10 |
foreach(var e; engines) |
| 11 |
if(e.selected.getValue()) |
| 12 |
e.controls.getNode("starter").setBoolValue(v); |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
var selectEngine = func(which) { |
| 17 |
foreach(var e; engines) e.selected.setBoolValue(which == e.index); |
| 18 |
} |
| 19 |
|
| 20 |
# Selects (state=1) or deselects (state=0) a list of engines, or all |
| 21 |
# engines if no list is specified. Example: selectEngines(1, 1, 3, 5); |
| 22 |
# |
| 23 |
var selectEngines = func (state, which...) { |
| 24 |
if(size(which)) { |
| 25 |
foreach(var i; which) |
| 26 |
foreach(var e; engines) |
| 27 |
if(e.index == i) |
| 28 |
e.selected.setBoolValue(state); |
| 29 |
} else { |
| 30 |
foreach(var e; engines) |
| 31 |
e.selected.setBoolValue(state); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
var selectAllEngines = func { |
| 36 |
foreach(var e; engines) e.selected.setBoolValue(1); |
| 37 |
} |
| 38 |
|
| 39 |
var stepMagnetos = func(change) { |
| 40 |
if (!change) |
| 41 |
return; |
| 42 |
foreach(var e; engines) { |
| 43 |
if(e.selected.getValue()) { |
| 44 |
var mag = e.controls.getNode("magnetos", 1); |
| 45 |
mag.setIntValue(mag.getValue() + change); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
var centerFlightControls = func { |
| 51 |
setprop("/controls/flight/elevator", 0); |
| 52 |
setprop("/controls/flight/aileron", 0); |
| 53 |
setprop("/controls/flight/rudder", 0); |
| 54 |
} |
| 55 |
|
| 56 |
var throttleMouse = func { |
| 57 |
if(!getprop("/devices/status/mice/mouse[0]/button[1]")) return; |
| 58 |
var delta = cmdarg().getNode("offset").getValue() * -4; |
| 59 |
foreach(var e; engines) { |
| 60 |
if(!e.selected.getValue()) continue; |
| 61 |
var throttle = e.controls.getNode("throttle"); |
| 62 |
var val = throttle.getValue() + delta; |
| 63 |
if(size(arg) > 0) val = -val; |
| 64 |
throttle.setDoubleValue(val); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
setprop("/controls/engines/throttle-all",0); |
| 69 |
setprop("/controls/engines/mixture-all",0); |
| 70 |
setprop("/controls/engines/propeller-pitch-all",0); |
| 71 |
|
| 72 |
setForAllEnginesProperty= func(type){ |
| 73 |
var engineRoot = props.globals.getNode("/controls/engines"); |
| 74 |
var v = getprop("/controls/engines/"~type~"-all"); |
| 75 |
v = -v; |
| 76 |
v = (v+1)*0.5; # scale from -1..1 to 0..1 |
| 77 |
foreach( var c; engineRoot.getChildren() ){ |
| 78 |
if (c.getName() =="engine"){ |
| 79 |
var n = c.getNode(type); |
| 80 |
if(n!=nil) { |
| 81 |
n.setValue(v); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
_setlistener("/controls/engines/throttle-all", func{ |
| 88 |
setForAllEnginesProperty("throttle"); |
| 89 |
},0,0); |
| 90 |
_setlistener("/controls/engines/mixture-all", func{ |
| 91 |
setForAllEnginesProperty("mixture"); |
| 92 |
},0,0); |
| 93 |
_setlistener("/controls/engines/propeller-pitch-all", func{ |
| 94 |
setForAllEnginesProperty("propeller-pitch"); |
| 95 |
},0,0); |
| 96 |
_setlistener("/controls/engines/reverser-all", func{ |
| 97 |
setForAllEnginesProperty("reverser"); |
| 98 |
},0,0); |
| 99 |
|
| 100 |
var reverserTogglePosition = func { |
| 101 |
if (getprop("/controls/engines/reverser-all")) |
| 102 |
setprop("/controls/engines/reverser-all", 0); |
| 103 |
else |
| 104 |
setprop("/controls/engines/reverser-all", 1); |
| 105 |
} |
| 106 |
|
| 107 |
# Joystick axis handlers (use cmdarg). Shouldn't be called from |
| 108 |
# other contexts. A non-null argument reverses the axis direction. |
| 109 |
var axisHandler = func(pre, post) { |
| 110 |
func(invert = 0) { |
| 111 |
var val = cmdarg().getNode("setting").getValue(); |
| 112 |
if(invert) val = -val; |
| 113 |
foreach(var e; engines) |
| 114 |
if(e.selected.getValue()) |
| 115 |
setprop(pre ~ e.index ~ post, (1 - val) / 2); |
| 116 |
} |
| 117 |
} |
| 118 |
var throttleAxis = axisHandler("/controls/engines/engine[", "]/throttle"); |
| 119 |
var mixtureAxis = axisHandler("/controls/engines/engine[", "]/mixture"); |
| 120 |
var propellerAxis = axisHandler("/controls/engines/engine[", "]/propeller-pitch"); |
| 121 |
var carbHeatAxis = axisHandler("/controls/anti-ice/engine[", "]/carb-heat"); |
| 122 |
var conditionAxis = axisHandler("/controls/engines/engine[", "]/condition"); |
| 123 |
|
| 124 |
# Joystick axis handler for controlling subsets of similar properties. |
| 125 |
# Shouldn't be called from other contexts. |
| 126 |
# The argument engine can be either an index number or a list of |
| 127 |
# index numbers. |
| 128 |
# Use only when perEngineSelectedAxisHandler() below will not do. |
| 129 |
var perIndexAxisHandler = func(pre, post) { |
| 130 |
return |
| 131 |
func(index, invert = 0) { |
| 132 |
var val = cmdarg().getNode("setting").getValue(); |
| 133 |
if(invert) val = -val; |
| 134 |
if (typeof(index) == "scalar") { |
| 135 |
setprop(pre ~ index ~ post, (1 - val) / 2); |
| 136 |
} else { |
| 137 |
foreach (var e; index) { |
| 138 |
setprop(pre ~ e ~ post, (1 - val) / 2); |
| 139 |
} |
| 140 |
} |
| 141 |
}; |
| 142 |
} |
| 143 |
|
| 144 |
# Joystick axis handler for controlling a selected axis on specific engines. |
| 145 |
# Shouldn't be called from other contexts. |
| 146 |
# The argument mode can be |
| 147 |
# 0 - throttle |
| 148 |
# 1 - mixture |
| 149 |
# 2 - propeller-pitch |
| 150 |
# 3 - condition |
| 151 |
# The argument engine to the returned function can be either an |
| 152 |
# engine number or a list of engine numbers. |
| 153 |
# Usage example (controlling the mixture of engines 0 and 1): |
| 154 |
# <script> |
| 155 |
# controls.perEngineSelectedAxisHandler(1)([0,1]); |
| 156 |
# </script> |
| 157 |
var _axisMode = { |
| 158 |
0: perIndexAxisHandler("/controls/engines/engine[", |
| 159 |
"]/throttle"), |
| 160 |
1: perIndexAxisHandler("/controls/engines/engine[", |
| 161 |
"]/mixture"), |
| 162 |
2: perIndexAxisHandler("/controls/engines/engine[", |
| 163 |
"]/propeller-pitch"), |
| 164 |
3: perIndexAxisHandler("/controls/engines/engine[", |
| 165 |
"]/condition") |
| 166 |
}; |
| 167 |
var perEngineSelectedAxisHandler = func(mode) { |
| 168 |
return _axisMode[mode]; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
## |
| 173 |
# Wrapper around stepProps() which emulates the "old" flap behavior for |
| 174 |
# configurations that aren't using the new mechanism. |
| 175 |
# |
| 176 |
var flapsDown = func(step) { |
| 177 |
if(step == 0) return; |
| 178 |
if(props.globals.getNode("/sim/flaps") != nil) { |
| 179 |
stepProps("/controls/flight/flaps", "/sim/flaps", step); |
| 180 |
return; |
| 181 |
} |
| 182 |
# Hard-coded flaps movement in 3 equal steps: |
| 183 |
var val = 0.3333334 * step + getprop("/controls/flight/flaps"); |
| 184 |
setprop("/controls/flight/flaps", val > 1 ? 1 : val < 0 ? 0 : val); |
| 185 |
} |
| 186 |
|
| 187 |
var wingSweep = func(step) { |
| 188 |
if(step == 0) return; |
| 189 |
if(props.globals.getNode("/sim/wing-sweep") != nil) { |
| 190 |
stepProps("/controls/flight/wing-sweep", "/sim/wing-sweep", step); |
| 191 |
return; |
| 192 |
} |
| 193 |
# Hard-coded wing movement in 5 equal steps: |
| 194 |
var val = 0.20 * step + getprop("/controls/flight/wing-sweep"); |
| 195 |
setprop("/controls/flight/wing-sweep", val > 1 ? 1 : val < 0 ? 0 : val); |
| 196 |
} |
| 197 |
|
| 198 |
var wingsDown = func(v) { |
| 199 |
if(v) setprop("/controls/flight/wing-fold", v > 0); |
| 200 |
} |
| 201 |
|
| 202 |
var stepSpoilers = func(step) { |
| 203 |
if(props.globals.getNode("/sim/spoilers") != nil) { |
| 204 |
stepProps("/controls/flight/spoilers", "/sim/spoilers", step); |
| 205 |
return; |
| 206 |
} |
| 207 |
# Hard-coded spoilers movement in 4 equal steps: |
| 208 |
var val = 0.25 * step + getprop("/controls/flight/spoilers"); |
| 209 |
setprop("/controls/flight/spoilers", val > 1 ? 1 : val < 0 ? 0 : val); |
| 210 |
} |
| 211 |
|
| 212 |
var stepSlats = func(step) { |
| 213 |
if(props.globals.getNode("/sim/slats") != nil) { |
| 214 |
stepProps("/controls/flight/slats", "/sim/slats", step); |
| 215 |
return; |
| 216 |
} |
| 217 |
# Hard-coded slats movement in 4 equal steps: |
| 218 |
var val = 0.25 * step + getprop("/controls/flight/slats"); |
| 219 |
setprop("/controls/flight/slats", val > 1 ? 1 : val < 0 ? 0 : val); |
| 220 |
} |
| 221 |
|
| 222 |
## |
| 223 |
# Steps through an "array" of property settings. The first argument |
| 224 |
# specifies a destination property. The second is a string containing |
| 225 |
# a global property tree. This tree should contain an array of |
| 226 |
# indexed <setting> children. This function will maintain a |
| 227 |
# <current-setting> child, which contains the index of the currently |
| 228 |
# active setting. The third argument specifies an integer delta, |
| 229 |
# indicating how many steps to move through the setting array. |
| 230 |
# Note that because of the magic of the property system, this |
| 231 |
# mechanism works for all scalar property types (bool, int, double, |
| 232 |
# string). |
| 233 |
# |
| 234 |
# TODO: This interface could easily be extended to allow for wrapping, |
| 235 |
# in addition to clamping, allowing a "cycle" of settings to be |
| 236 |
# defined. It could also be hooked up with the interpolate() call, |
| 237 |
# which would allow the removal of the transition-time feature from |
| 238 |
# YASim. Finally, other pre-existing features (the views and engine |
| 239 |
# magnetos, for instance), work similarly but not compatibly, and |
| 240 |
# could be integrated. |
| 241 |
# |
| 242 |
var stepProps = func(dst, array, delta) { |
| 243 |
dst = props.globals.getNode(dst); |
| 244 |
array = props.globals.getNode(array); |
| 245 |
if(dst == nil or array == nil) { return; } |
| 246 |
|
| 247 |
var sets = array.getChildren("setting"); |
| 248 |
|
| 249 |
var curr = array.getNode("current-setting", 1).getValue(); |
| 250 |
if(curr == nil) { curr = 0; } |
| 251 |
curr = curr + delta; |
| 252 |
if (curr < 0) { curr = 0; } |
| 253 |
elsif(curr >= size(sets)) { curr = size(sets) - 1; } |
| 254 |
|
| 255 |
array.getNode("current-setting").setIntValue(curr); |
| 256 |
dst.setValue(sets[curr].getValue()); |
| 257 |
} |
| 258 |
|
| 259 |
## |
| 260 |
# "Slews" a property smoothly, without dependence on the simulator |
| 261 |
# frame rate. The first argument is the property name. The second is |
| 262 |
# a rate, in units per second. NOTE: this modifies the property for |
| 263 |
# the current frame only; it is intended to be called by bindings |
| 264 |
# which repeat each frame. If you want to cause motion over time, see |
| 265 |
# interpolate(). Returns new value. |
| 266 |
# |
| 267 |
var slewProp = func(prop, delta) { |
| 268 |
delta *= getprop("/sim/time/delta-realtime-sec"); |
| 269 |
setprop(prop, getprop(prop) + delta); |
| 270 |
return getprop(prop); # must read again because of clamping |
| 271 |
} |
| 272 |
|
| 273 |
# Standard trim rate, in units per second. Remember that the full |
| 274 |
# range of a trim axis is 2.0. Should probably read this out of a |
| 275 |
# property... |
| 276 |
var TRIM_RATE = 0.045; |
| 277 |
|
| 278 |
# |
| 279 |
# Trim elevator to current stick position (on release) |
| 280 |
var setElevatorTrimToPosition_listener = nil; |
| 281 |
setElevatorTrimToPosition = func() { |
| 282 |
if (setElevatorTrimToPosition_listener != nil) { |
| 283 |
removelistener(setElevatorTrimToPosition_listener); |
| 284 |
setElevatorTrimToPosition_listener = nil; |
| 285 |
} |
| 286 |
var nv = getprop("/controls/flight/elevator")+getprop("/controls/flight/elevator-trim"); |
| 287 |
nv = math.min(math.max(-1.0,nv),1.0); |
| 288 |
var lv = getprop("/controls/flight/elevator"); |
| 289 |
if (math.abs(lv) <= 0.001){ |
| 290 |
setprop("/controls/flight/elevator-trim", 0); |
| 291 |
} else { |
| 292 |
setElevatorTrimToPosition_listener = setlistener("/controls/flight/elevator", func(v){ |
| 293 |
if (math.abs(v.getValue()) <= 0.001) { |
| 294 |
setprop("controls/flight/elevator",0); |
| 295 |
removelistener(setElevatorTrimToPosition_listener); |
| 296 |
setElevatorTrimToPosition_listener = nil; |
| 297 |
setprop("/controls/flight/elevator-trim", nv); |
| 298 |
} |
| 299 |
}, 0, 0); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
# |
| 304 |
# Trim aileron to current stick position (on release) |
| 305 |
var setAileronTrimToPosition_listener = nil; |
| 306 |
setAileronTrimToPosition = func() { |
| 307 |
if (setAileronTrimToPosition_listener != nil) { |
| 308 |
removelistener(setAileronTrimToPosition_listener); |
| 309 |
setAileronTrimToPosition_listener = nil; |
| 310 |
} |
| 311 |
var nv = getprop("/controls/flight/aileron")+getprop("/controls/flight/aileron-trim"); |
| 312 |
nv = math.min(math.max(-1.0,nv),1.0); |
| 313 |
var lv = getprop("/controls/flight/aileron"); |
| 314 |
if (math.abs(lv) <= 0.001){ |
| 315 |
setprop("/controls/flight/aileron-trim", 0); |
| 316 |
} else { |
| 317 |
setAileronTrimToPosition_listener = setlistener("/controls/flight/aileron", func(v){ |
| 318 |
if (math.abs(v.getValue()) <= 0.001) { |
| 319 |
setprop("controls/flight/aileron",0); |
| 320 |
removelistener(setAileronTrimToPosition_listener); |
| 321 |
setAileronTrimToPosition_listener = nil; |
| 322 |
setprop("/controls/flight/aileron-trim", nv); |
| 323 |
} |
| 324 |
}, 0, 0); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
# |
| 329 |
# Trim rudder to current stick position (on release) |
| 330 |
var setRudderTrimToPosition_listener = nil; |
| 331 |
setRudderTrimToPosition = func() { |
| 332 |
if (setRudderTrimToPosition_listener != nil) { |
| 333 |
removelistener(setRudderTrimToPosition_listener); |
| 334 |
setRudderTrimToPosition_listener = nil; |
| 335 |
} |
| 336 |
var nv = getprop("/controls/flight/rudder")+getprop("/controls/flight/rudder-trim"); |
| 337 |
nv = math.min(math.max(-1.0,nv),1.0); |
| 338 |
var lv = getprop("/controls/flight/rudder"); |
| 339 |
if (math.abs(lv) <= 0.001){ |
| 340 |
setprop("/controls/flight/rudder-trim", 0); |
| 341 |
} else { |
| 342 |
setRudderTrimToPosition_listener = setlistener("/controls/flight/rudder", func(v){ |
| 343 |
if (math.abs(v.getValue()) <= 0.001) { |
| 344 |
setprop("controls/flight/rudder",0); |
| 345 |
removelistener(setRudderTrimToPosition_listener); |
| 346 |
setRudderTrimToPosition_listener = nil; |
| 347 |
setprop("/controls/flight/rudder-trim", nv); |
| 348 |
} |
| 349 |
}, 0, 0); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
# Adjust elevator-trim applying user defined factor |
| 354 |
# (intendet to be sued from mouse wheel binding) |
| 355 |
adjElevatorTrimWithFactor = func(step) { |
| 356 |
var factor = getprop("/sim/mouse/wheel-elevator-trim-factor"); |
| 357 |
var nextVal = getprop("/controls/flight/elevator-trim") + step*factor; |
| 358 |
setprop("/controls/flight/elevator-trim", nextVal); |
| 359 |
} |
| 360 |
|
| 361 |
## |
| 362 |
# Handlers. These are suitable for binding to repeatable button press |
| 363 |
# events. They are *not* good for binding to the keyboard, since (at |
| 364 |
# least) X11 synthesizes its own key repeats. |
| 365 |
# |
| 366 |
var elevatorTrim = func(speed) { |
| 367 |
slewProp("/controls/flight/elevator-trim", speed * TRIM_RATE); } |
| 368 |
var aileronTrim = func(speed) { |
| 369 |
slewProp("/controls/flight/aileron-trim", speed * TRIM_RATE); } |
| 370 |
var rudderTrim = func(speed) { |
| 371 |
slewProp("/controls/flight/rudder-trim", speed * TRIM_RATE); } |
| 372 |
|
| 373 |
var THROTTLE_RATE = 0.33; |
| 374 |
|
| 375 |
var adjThrottle = func(speed) { |
| 376 |
adjEngControl("throttle", speed); } |
| 377 |
var adjMixture = func(speed) { |
| 378 |
adjEngControl("mixture", speed); } |
| 379 |
var adjCondition = func(speed) { |
| 380 |
adjEngControl("condition", speed); } |
| 381 |
var adjPropeller = func(speed) { |
| 382 |
adjEngControl("propeller-pitch", speed); } |
| 383 |
|
| 384 |
var adjEngControl = func(prop, speed) { |
| 385 |
var delta = speed * THROTTLE_RATE * getprop("/sim/time/delta-realtime-sec"); |
| 386 |
var (value, count) = (0, 0); |
| 387 |
foreach(var e; engines) { |
| 388 |
if(e.selected.getValue()) { |
| 389 |
var node = e.controls.getNode(prop, 1); |
| 390 |
node.setValue(node.getValue() + delta); |
| 391 |
value += node.getValue(); # must read again because of clamping |
| 392 |
count += 1; |
| 393 |
} |
| 394 |
} |
| 395 |
return value / count; |
| 396 |
} |
| 397 |
|
| 398 |
## |
| 399 |
# arg[0] is the throttle increment |
| 400 |
# arg[1] is the auto-throttle target speed increment |
| 401 |
var incThrottle = func { |
| 402 |
var passive = getprop("/autopilot/locks/passive-mode"); |
| 403 |
var locked = getprop("/autopilot/locks/speed"); |
| 404 |
# Note: passive/locked may be nil on aircraft without A/P |
| 405 |
if (!passive and (locked)) |
| 406 |
{ |
| 407 |
var node = props.globals.getNode("/autopilot/settings/target-speed-kt", 1); |
| 408 |
if (node.getValue() == nil) { |
| 409 |
node.setValue(0.0); |
| 410 |
} |
| 411 |
node.setValue(node.getValue() + arg[1]); |
| 412 |
if (node.getValue() < 0.0) { |
| 413 |
node.setValue(0.0); |
| 414 |
} |
| 415 |
} |
| 416 |
else |
| 417 |
{ |
| 418 |
foreach(var e; engines) |
| 419 |
{ |
| 420 |
if(e.selected.getValue()) |
| 421 |
{ |
| 422 |
var node = e.controls.getNode("throttle", 1); |
| 423 |
var val = node.getValue() + arg[0]; |
| 424 |
node.setValue(val < -1.0 ? -1.0 : val > 1.0 ? 1.0 : val); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
## |
| 431 |
# arg[0] is the aileron increment |
| 432 |
# arg[1] is the autopilot target heading increment |
| 433 |
var incAileron = func { |
| 434 |
var passive = getprop("/autopilot/locks/passive-mode"); |
| 435 |
var locked = getprop("/autopilot/locks/heading"); |
| 436 |
# Note: passive/locked may be nil on aircraft without A/P |
| 437 |
if (!passive and (locked == "dg-heading-hold")) |
| 438 |
{ |
| 439 |
var node = props.globals.getNode("/autopilot/settings/heading-bug-deg", 1); |
| 440 |
if (node.getValue() == nil) { |
| 441 |
node.setValue(0.0); |
| 442 |
} |
| 443 |
node.setValue(node.getValue() + arg[1]); |
| 444 |
if (node.getValue() < 0.0) { |
| 445 |
node.setValue(node.getValue() + 360.0); |
| 446 |
} |
| 447 |
if (node.getValue() > 360.0) { |
| 448 |
node.setValue(node.getValue() - 360.0); |
| 449 |
} |
| 450 |
} |
| 451 |
else if (!passive and (locked == "true-heading-hold")) |
| 452 |
{ |
| 453 |
var node = props.globals.getNode("/autopilot/settings/true-heading-deg", 1); |
| 454 |
if (node.getValue() == nil) { |
| 455 |
node.setValue(0.0); |
| 456 |
} |
| 457 |
node.setValue(node.getValue() + arg[1]); |
| 458 |
if (node.getValue() < 0.0) { |
| 459 |
node.setValue(node.getValue() + 360.0); |
| 460 |
} |
| 461 |
if (node.getValue() > 360.0) { |
| 462 |
node.setValue(node.getValue() - 360.0); |
| 463 |
} |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
var aileron = props.globals.getNode("/controls/flight/aileron"); |
| 468 |
if (aileron.getValue() == nil) { |
| 469 |
aileron.setValue(0.0); |
| 470 |
} |
| 471 |
aileron.setValue(aileron.getValue() + arg[0]); |
| 472 |
if (aileron.getValue() < -1.0) { |
| 473 |
aileron.setValue(-1.0); |
| 474 |
} |
| 475 |
if (aileron.getValue() > 1.0) { |
| 476 |
aileron.setValue(1.0); |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
## |
| 482 |
# arg[0] is the elevator increment |
| 483 |
# arg[1] is the autopilot target altitude increment |
| 484 |
var incElevator = func { |
| 485 |
var passive = getprop("/autopilot/locks/passive-mode"); |
| 486 |
var locked = getprop("/autopilot/locks/altitude"); |
| 487 |
# Note: passive/locked may be nil on aircraft without A/P |
| 488 |
if (!passive and (locked =="altitude-hold")) |
| 489 |
{ |
| 490 |
var node = props.globals.getNode("/autopilot/settings/target-altitude-ft", 1); |
| 491 |
if (node.getValue() == nil) { |
| 492 |
node.setValue(0.0); |
| 493 |
} |
| 494 |
node.setValue(node.getValue() + arg[1]); |
| 495 |
if (node.getValue() < 0.0) { |
| 496 |
node.setValue(0.0); |
| 497 |
} |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
var elevator = props.globals.getNode("/controls/flight/elevator"); |
| 502 |
if (elevator.getValue() == nil) { |
| 503 |
elevator.setValue(0.0); |
| 504 |
} |
| 505 |
elevator.setValue(elevator.getValue() + arg[0]); |
| 506 |
if (elevator.getValue() < -1.0) { |
| 507 |
elevator.setValue(-1.0); |
| 508 |
} |
| 509 |
if (elevator.getValue() > 1.0) { |
| 510 |
elevator.setValue(1.0); |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
## |
| 516 |
# Joystick axis handlers. Don't call from other contexts. |
| 517 |
# |
| 518 |
var elevatorTrimAxis = func { elevatorTrim(cmdarg().getNode("setting").getValue()); } |
| 519 |
var aileronTrimAxis = func { aileronTrim(cmdarg().getNode("setting").getValue()); } |
| 520 |
var rudderTrimAxis = func { rudderTrim(cmdarg().getNode("setting").getValue()); } |
| 521 |
|
| 522 |
# |
| 523 |
# to avoid clash with "gearToggle"; this will only toggle when |
| 524 |
# given a parameter of 1; as the controls button binding will pass 0 when the button is pressed and |
| 525 |
# 1 when the button is released. |
| 526 |
var gearTogglePosition = func(v) { |
| 527 |
if (v){ |
| 528 |
if (getprop("/controls/gear/gear-down")) |
| 529 |
setprop("/controls/gear/gear-down", 0); |
| 530 |
else |
| 531 |
setprop("/controls/gear/gear-down", 1); |
| 532 |
} |
| 533 |
} |
| 534 |
## |
| 535 |
# Gear handling. |
| 536 |
# - parameter v is either 1 (down), -1 up (retracted) |
| 537 |
var gearDown = func(v) { |
| 538 |
if (v < 0) { |
| 539 |
setprop("/controls/gear/gear-down", 0); |
| 540 |
} elsif (v > 0) { |
| 541 |
setprop("/controls/gear/gear-down", 1); |
| 542 |
} |
| 543 |
} |
| 544 |
var gearToggle = func { gearDown(getprop("/controls/gear/gear-down") > 0 ? -1 : 1); } |
| 545 |
|
| 546 |
## |
| 547 |
# Brake handling. |
| 548 |
# |
| 549 |
var fullBrakeTime = 0.5; |
| 550 |
var applyBrakes = func(v, which = 0) { |
| 551 |
if (which <= 0) { interpolate("/controls/gear/brake-left", v, fullBrakeTime); } |
| 552 |
if (which >= 0) { interpolate("/controls/gear/brake-right", v, fullBrakeTime); } |
| 553 |
if (v and props.globals.getNode("/sim/controls/brake-cancels-parking-brake", 1).getBoolValue()) { |
| 554 |
setprop("/controls/gear/brake-parking", 0); |
| 555 |
} |
| 556 |
} |
| 557 |
var applyPickle = func(v) { |
| 558 |
setprop("/controls/armament/pickle", v); |
| 559 |
} |
| 560 |
|
| 561 |
var applyParkingBrake = func(v) { |
| 562 |
if (!v) { return; } |
| 563 |
var p = "/controls/gear/brake-parking"; |
| 564 |
setprop(p, var i = !getprop(p)); |
| 565 |
return i; |
| 566 |
} |
| 567 |
var parkingBrakeToggle = func(v) { |
| 568 |
if (v){ |
| 569 |
if (getprop("/controls/gear/brake-parking")) |
| 570 |
setprop("/controls/gear/brake-parking", 0); |
| 571 |
else |
| 572 |
setprop("/controls/gear/brake-parking", 1); |
| 573 |
} |
| 574 |
} |
| 575 |
var toggleNWS = func(v) { |
| 576 |
if (v){ |
| 577 |
if (getprop("/controls/gear/nose-wheel-steering")) |
| 578 |
setprop("/controls/gear/nose-wheel-steering", 0); |
| 579 |
else |
| 580 |
setprop("/controls/gear/nose-wheel-steering", 1); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
var weAppliedSpeedBrake = 99; |
| 585 |
var weAppliedWheelBrake = 99; |
| 586 |
|
| 587 |
# |
| 588 |
# allows one binding to do airbrakes and wheel brakes |
| 589 |
var applyApplicableBrakes = func(v, which = 0) { |
| 590 |
var wow = getprop ("/gear/gear[0]/wow") |
| 591 |
or getprop ("/gear/gear[1]/wow") |
| 592 |
or getprop ("/gear/gear[2]/wow") |
| 593 |
or getprop ("/gear/gear[3]/wow") |
| 594 |
or getprop ("/gear/gear[4]/wow") |
| 595 |
or getprop ("/gear/gear[5]/wow") |
| 596 |
or getprop ("/gear/gear[6]/wow") |
| 597 |
; |
| 598 |
|
| 599 |
if (wow) { |
| 600 |
if (!v and weAppliedSpeedBrake != 99) { |
| 601 |
setprop("controls/flight/speedbrake", 0); |
| 602 |
weAppliedSpeedBrake=0; |
| 603 |
} |
| 604 |
if (which <= 0) { |
| 605 |
interpolate("/controls/gear/brake-left", v, controls.fullBrakeTime); |
| 606 |
} |
| 607 |
if (which >= 0) { |
| 608 |
interpolate("/controls/gear/brake-right", v, controls.fullBrakeTime); |
| 609 |
} |
| 610 |
weAppliedWheelBrake = which; |
| 611 |
} |
| 612 |
else { |
| 613 |
if (!v and weAppliedWheelBrake != 99) { |
| 614 |
if (weAppliedWheelBrake <= 0) { interpolate("/controls/gear/brake-left", 0, controls.fullBrakeTime); } |
| 615 |
if (weAppliedWheelBrake >= 0) { interpolate("/controls/gear/brake-right", 0, controls.fullBrakeTime); } |
| 616 |
weAppliedWheelBrake=0; |
| 617 |
} |
| 618 |
|
| 619 |
weAppliedSpeedBrake=which; |
| 620 |
setprop("controls/flight/speedbrake", v); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
var autopilotDisconnect = func() { |
| 625 |
var aps = props.getNode("/controls/autoflight").getChildren("autopilot"); |
| 626 |
foreach (var ap; aps) { |
| 627 |
ap.getChild("engage").setBoolValue(0); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
# 1: Deploy, -1: Release |
| 632 |
var deployChute = func(v) setprop("/controls/flight/drag-chute", v); |
| 633 |
|
| 634 |
## |
| 635 |
# Weapon handling. |
| 636 |
# |
| 637 |
var trigger = func(b) setprop("/controls/armament/trigger", b); |
| 638 |
var weaponSelect = func(d) { |
| 639 |
var ws = props.globals.getNode("/controls/armament/selected", 1); |
| 640 |
var n = ws.getValue(); |
| 641 |
if (n == nil) { n = 0; } |
| 642 |
ws.setIntValue(n + d); |
| 643 |
} |
| 644 |
|
| 645 |
## |
| 646 |
# Communication. |
| 647 |
# |
| 648 |
var ptt = func(b){ |
| 649 |
if (b and b != getprop("/controls/radios/comm-radio-selected")) |
| 650 |
setprop("/controls/radios/comm-radio-selected", b); |
| 651 |
if (b > 0) { # activate comm-ptt only for operable radios |
| 652 |
if (getprop(sprintf("%s%d%s", '/instrumentation/comm[', (b-1), ']/operable'))) |
| 653 |
setprop("/controls/radios/comm-ptt", b); |
| 654 |
} else { # deactivate comm-ptt |
| 655 |
setprop("/controls/radios/comm-ptt", b); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
_setlistener("/instrumentation/comm[0]/ptt", func { |
| 660 |
var v=getprop("/instrumentation/comm[0]/ptt"); |
| 661 |
var o=getprop("/instrumentation/comm[0]/operable"); |
| 662 |
setprop("/controls/radios/comm-radio-selected", 1); |
| 663 |
if (v and (o == true)) |
| 664 |
setprop("/controls/radios/comm-ptt", 1); |
| 665 |
else |
| 666 |
setprop("/controls/radios/comm-ptt", 0); |
| 667 |
}); |
| 668 |
|
| 669 |
_setlistener("/instrumentation/comm[1]/ptt", func { |
| 670 |
var v=getprop("/instrumentation/comm[1]/ptt"); |
| 671 |
var o=getprop("/instrumentation/comm[1]/operable"); |
| 672 |
setprop("/controls/radios/comm-radio-selected", 2); |
| 673 |
if (v and (o == true)) |
| 674 |
setprop("/controls/radios/comm-ptt", 2); |
| 675 |
else |
| 676 |
setprop("/controls/radios/comm-ptt", 0); |
| 677 |
}); |
| 678 |
|
| 679 |
## |
| 680 |
# Lighting. |
| 681 |
# |
| 682 |
var toggleLights = func { |
| 683 |
if (getprop("/controls/switches/panel-lights")) { |
| 684 |
setprop("/controls/switches/panel-lights-factor", 0); |
| 685 |
setprop("/controls/switches/panel-lights", 0); |
| 686 |
setprop("/controls/switches/landing-light", 0); |
| 687 |
setprop("/controls/switches/flashing-beacon", 0); |
| 688 |
setprop("/controls/switches/strobe-lights", 0); |
| 689 |
setprop("/controls/switches/map-lights", 0); |
| 690 |
setprop("/controls/switches/cabin-lights", 0); |
| 691 |
setprop("/controls/switches/nav-lights", 0); |
| 692 |
} else { |
| 693 |
setprop("/controls/electric/battery-switch", 1); |
| 694 |
setprop("/controls/electric/alternator-switch", 1); |
| 695 |
setprop("/controls/switches/panel-lights-factor", 0.1); |
| 696 |
setprop("/controls/switches/panel-lights", 1); |
| 697 |
setprop("/controls/switches/landing-light", 1); |
| 698 |
setprop("/controls/switches/flashing-beacon", 1); |
| 699 |
setprop("/controls/switches/strobe-lights", 1); |
| 700 |
setprop("/controls/switches/map-lights", 1); |
| 701 |
setprop("/controls/switches/cabin-lights", 1); |
| 702 |
setprop("/controls/switches/nav-lights", 1); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
## |
| 707 |
# Initialization. |
| 708 |
# |
| 709 |
var engines = []; |
| 710 |
_setlistener("/sim/signals/fdm-initialized", func { |
| 711 |
var sel = props.globals.getNode("/sim/input/selected", 1); |
| 712 |
var engs = props.globals.getNode("/controls/engines").getChildren("engine"); |
| 713 |
|
| 714 |
# need to reset engine list on every FDM reset |
| 715 |
engines = []; |
| 716 |
# process all engines |
| 717 |
foreach(var e; engs) { |
| 718 |
var index = e.getIndex(); |
| 719 |
var s = sel.getChild("engine", index, 1); |
| 720 |
if(s.getType() == "NONE") s.setBoolValue(1); |
| 721 |
append(engines, { index: index, controls: e, selected: s }); |
| 722 |
} |
| 723 |
}); |
| 724 |
|
| 725 |
var replaySkip = func(skip_time) |
| 726 |
{ |
| 727 |
var t = getprop("/sim/replay/time"); |
| 728 |
if (t != "") |
| 729 |
{ |
| 730 |
t+=skip_time; |
| 731 |
if (t>getprop("/sim/replay/end-time")) |
| 732 |
t = getprop("/sim/replay/end-time"); |
| 733 |
if (t<getprop("/sim/replay/start-time")) |
| 734 |
t = getprop("/sim/replay/start-time"); |
| 735 |
setprop("/sim/replay/time", t); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
var speedup = func(speed_up) |
| 740 |
{ |
| 741 |
var t = getprop("/sim/speed-up"); |
| 742 |
if (speed_up < 0) |
| 743 |
{ |
| 744 |
t = (t > 1/32) ? t/2 : 1/32; |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
t = (t < 32) ? t*2 : 32; |
| 749 |
} |
| 750 |
setprop("/sim/speed-up", t); |
| 751 |
|
| 752 |
# reformat as a string, this is borrowed from replay.xml |
| 753 |
|
| 754 |
if (t<0.9) |
| 755 |
{ |
| 756 |
t=1/t; # invert the value |
| 757 |
t = "1/" ~ t; # convert to a string and show inverted |
| 758 |
} |
| 759 |
|
| 760 |
gui.popupTip("Time speed-up: " ~ t ~ "x"); |
| 761 |
} |
| 762 |
|
| 763 |
# mouse-mode handling |
| 764 |
|
| 765 |
var cycleMouseMode = func(node) |
| 766 |
{ |
| 767 |
var reason = node.getChild("reason").getValue(); |
| 768 |
if (reason == "right-click") { |
| 769 |
if (!getprop("/sim/mouse/right-button-mode-cycle-enabled")) { |
| 770 |
return; |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
var modeNode = props.globals.getNode('/devices/status/mice/mouse[0]/mode'); |
| 775 |
var mode = modeNode.getValue() + 1; |
| 776 |
|
| 777 |
if ((mode == 1) and getprop('/sim/mouse/skip-flight-controls-mode')) { |
| 778 |
mode +=1; |
| 779 |
} |
| 780 |
|
| 781 |
if (mode == 3) mode = 0; |
| 782 |
modeNode.setIntValue(mode); |
| 783 |
|
| 784 |
# this is really a 'show on-screen hints' control |
| 785 |
if (getprop('/sim/view-name-popup') == 0) |
| 786 |
return; |
| 787 |
|
| 788 |
# some people like popups but strongly object to this one. As you wish. |
| 789 |
if (getprop('/sim/mouse/cycle-mode-popup') == 0) |
| 790 |
return; |
| 791 |
|
| 792 |
if (mode == 0) { |
| 793 |
fgcommand("clear-message", props.Node.new({ "id":"mouse-mode" })); |
| 794 |
return; |
| 795 |
} |
| 796 |
|
| 797 |
var msg = ""; |
| 798 |
|
| 799 |
# give correct feedback for the UI action which prompted this |
| 800 |
var suffix = "Press TAB to change."; |
| 801 |
if (reason == "right-click") { |
| 802 |
suffix = "Right-click to change."; |
| 803 |
} |
| 804 |
|
| 805 |
if (mode == 1) { |
| 806 |
msg = "Mouse is controlling flight controls."; |
| 807 |
} else { |
| 808 |
msg = "Mouse is controlling view direction."; |
| 809 |
} |
| 810 |
|
| 811 |
msg = msg ~ " " ~ suffix; |
| 812 |
|
| 813 |
fgcommand("show-message", props.Node.new({ "label": msg, "id":"mouse-mode" })); |
| 814 |
} |
| 815 |
|
| 816 |
addcommand("cycle-mouse-mode", cycleMouseMode); |
| 817 |
|
| 818 |
var setMouseFlightControlsSensitivity = func(sensitivity) |
| 819 |
{ |
| 820 |
setprop("/input/mice/mouse/mode[1]/y-axis/binding[0]/factor", -sensitivity); |
| 821 |
setprop("/input/mice/mouse/mode[1]/x-axis/binding[0]/factor", sensitivity); |
| 822 |
setprop("/input/mice/mouse/mode[1]/x-axis/binding[1]/factor", sensitivity); |
| 823 |
} |
| 824 |
|
| 825 |
setlistener("/sim/mouse/flight-controls-sensitivity", func(prop){ |
| 826 |
setMouseFlightControlsSensitivity(prop.getValue()) |
| 827 |
}, 1, 0); |