| 1 |
# Compatibility failure modes |
| 2 |
# |
| 3 |
# Loads FailureMgr with the failure modes that where previously hardcoded, |
| 4 |
# emulating former behavior and allowing backward compatibility. |
| 5 |
# |
| 6 |
# Copyright (C) 2014 Anton Gomez Alvedro |
| 7 |
# Based on previous work by Stuart Buchanan, Erobo & John Denker |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or |
| 10 |
# modify it under the terms of the GNU General Public License as |
| 11 |
# published by the Free Software Foundation; either version 2 of the |
| 12 |
# License, or (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with this program; if not, write to the Free Software |
| 21 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
|
| 23 |
io.include("Aircraft/Generic/Systems/failures.nas"); |
| 24 |
|
| 25 |
|
| 26 |
MTBF = 0; |
| 27 |
MCBF = 1; |
| 28 |
|
| 29 |
SERV = 0; |
| 30 |
JAM = 1; |
| 31 |
ENG = 2; |
| 32 |
|
| 33 |
|
| 34 |
var compat_modes = [ |
| 35 |
# Instruments |
| 36 |
{ id: "instrumentation/adf", type: MTBF, failure: SERV, desc: "ADF" }, |
| 37 |
{ id: "instrumentation/dme", type: MTBF, failure: SERV, desc: "DME" }, |
| 38 |
{ id: "instrumentation/airspeed-indicator", type: MTBF, failure: SERV, desc: "ASI" }, |
| 39 |
{ id: "instrumentation/altimeter", type: MTBF, failure: SERV, desc: "Altimeter" }, |
| 40 |
{ id: "instrumentation/attitude-indicator", type: MTBF, failure: SERV, desc: "Attitude Indicator" }, |
| 41 |
{ id: "instrumentation/heading-indicator", type: MTBF, failure: SERV, desc: "Heading Indicator" }, |
| 42 |
{ id: "instrumentation/magnetic-compass", type: MTBF, failure: SERV, desc: "Magnetic Compass" }, |
| 43 |
{ id: "instrumentation/nav/gs", type: MTBF, failure: SERV, desc: "Nav 1 Glideslope" }, |
| 44 |
{ id: "instrumentation/nav/cdi", type: MTBF, failure: SERV, desc: "Nav 1 CDI" }, |
| 45 |
{ id: "instrumentation/nav[1]/gs", type: MTBF, failure: SERV, desc: "Nav 2 Glideslope" }, |
| 46 |
{ id: "instrumentation/nav[1]/cdi", type: MTBF, failure: SERV, desc: "Nav 2 CDI" }, |
| 47 |
{ id: "instrumentation/slip-skid-ball", type: MTBF, failure: SERV, desc: "Slip/Skid Ball" }, |
| 48 |
{ id: "instrumentation/turn-indicator", type: MTBF, failure: SERV, desc: "Turn Indicator" }, |
| 49 |
{ id: "instrumentation/vertical-speed-indicator", type: MTBF, failure: SERV, desc: "VSI" }, |
| 50 |
|
| 51 |
# Systems |
| 52 |
{ id: "systems/electrical", type: MTBF, failure: SERV, desc: "Electrical system" }, |
| 53 |
{ id: "systems/pitot", type: MTBF, failure: SERV, desc: "Pitot system" }, |
| 54 |
{ id: "systems/static", type: MTBF, failure: SERV, desc: "Static system" }, |
| 55 |
{ id: "systems/vacuum", type: MTBF, failure: SERV, desc: "Vacuum system" }, |
| 56 |
|
| 57 |
# Controls |
| 58 |
{ id: "controls/flight/aileron", type: MTBF, failure: JAM, desc: "Aileron" }, |
| 59 |
{ id: "controls/flight/elevator", type: MTBF, failure: JAM, desc: "Elevator" }, |
| 60 |
{ id: "controls/flight/rudder", type: MTBF, failure: JAM, desc: "Rudder" }, |
| 61 |
{ id: "controls/flight/flaps", type: MCBF, failure: JAM, desc: "Flaps" }, |
| 62 |
{ id: "controls/flight/speedbrake", type: MCBF, failure: JAM, desc: "Speed Brake" }, |
| 63 |
{ id: "controls/gear", type: MCBF, failure: SERV, desc: "Gear", prop: "/gear", mcbf_prop: "/controls/gear/gear-down" } |
| 64 |
]; |
| 65 |
|
| 66 |
|
| 67 |
## |
| 68 |
# Handles the old failures.nas property tree interface, |
| 69 |
# sending the appropriate commands to the new FailureMgr. |
| 70 |
|
| 71 |
var compat_listener = func(prop) { |
| 72 |
|
| 73 |
var name = prop.getName(); |
| 74 |
var value = prop.getValue(); |
| 75 |
var id = string.replace(io.dirname(prop.getPath()), FailureMgr.proproot, ""); |
| 76 |
id = string.trim(id, 0, func(c) c == `/`); |
| 77 |
|
| 78 |
if (name == "serviceable") { |
| 79 |
FailureMgr.set_failure_level(id, value ? 0 : 1); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (name == "failure-level") { |
| 84 |
setprop(io.dirname(prop.getPath()) ~ "/serviceable", value ? 0 : 1); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
# mtbf and mcbf parameter handling |
| 89 |
var trigger = FailureMgr.get_trigger(id); |
| 90 |
|
| 91 |
if (trigger == nil or (trigger.type != "mcbf" and trigger.type != "mtbf")) |
| 92 |
return; |
| 93 |
|
| 94 |
if (value != 0) |
| 95 |
trigger.set_param(name, value) and trigger.arm(); |
| 96 |
else |
| 97 |
trigger.disarm(); |
| 98 |
} |
| 99 |
|
| 100 |
## |
| 101 |
# Listens to FailureMgr events. Resets mcbf/mtbf params to zero so they can |
| 102 |
# be rearmed from the GUI. |
| 103 |
|
| 104 |
var trigger_listener = func(event) { |
| 105 |
var trigger = event.trigger; |
| 106 |
|
| 107 |
# Only control modes in our compat list, i.e. do not interfere |
| 108 |
# with custom scripts. |
| 109 |
|
| 110 |
if (trigger.type != "mtbf" and trigger.type != "mcbf") |
| 111 |
return; |
| 112 |
|
| 113 |
foreach (var m; compat_modes) { |
| 114 |
if (m.id == event.mode_id) { |
| 115 |
trigger.set_param(trigger.type, 0); |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
## |
| 122 |
# Called from the ramdom-failures dialog to set the global MCBF parameter |
| 123 |
|
| 124 |
var apply_global_mcbf = func(value) { |
| 125 |
foreach (var mode; compat_modes) { |
| 126 |
mode.type != MCBF and continue; |
| 127 |
setprop(FailureMgr.proproot ~ mode.id ~ "/mcbf", value); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
## |
| 132 |
# Called from the ramdom-failures dialog to set the global MTBF parameter |
| 133 |
|
| 134 |
var apply_global_mtbf = func(value) { |
| 135 |
foreach (var mode; compat_modes) { |
| 136 |
mode.type != MTBF and continue; |
| 137 |
setprop(FailureMgr.proproot ~ mode.id ~ "/mtbf", value); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
## |
| 142 |
# Discover aircraft engines dynamically and add a failure mode to the |
| 143 |
# compat_modes table for each engine. |
| 144 |
|
| 145 |
var populate_engine_data = func { |
| 146 |
|
| 147 |
var engines = props.globals.getNode("/engines"); |
| 148 |
var engine_id = 0; |
| 149 |
|
| 150 |
foreach (var e; engines.getChildren("engine")) { |
| 151 |
var starter = e.getChild("starter"); |
| 152 |
var running = e.getChild("running"); |
| 153 |
|
| 154 |
(starter != nil and starter != "" and starter.getType() != "NONE") |
| 155 |
or (running != nil and running != "" and running.getType() != "NONE") |
| 156 |
or continue; |
| 157 |
|
| 158 |
var id = "engines/engine"; |
| 159 |
if (engine_id > 0) |
| 160 |
id = id ~ "[" ~ engine_id ~ "]"; |
| 161 |
|
| 162 |
var entry = { |
| 163 |
id: id, |
| 164 |
desc: "Engine " ~ (engine_id + 1), |
| 165 |
type: MTBF, |
| 166 |
failure: ENG |
| 167 |
}; |
| 168 |
|
| 169 |
append(compat_modes, entry); |
| 170 |
engine_id += 1; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
## |
| 175 |
# Subscribes all failure modes that the old failures.nas module did, |
| 176 |
# and recreates the same property tree interface (more or less). |
| 177 |
|
| 178 |
var compat_setup = func { |
| 179 |
|
| 180 |
removelistener(lsnr); |
| 181 |
populate_engine_data(); |
| 182 |
|
| 183 |
foreach (var m; compat_modes) { |
| 184 |
var control_prop = contains(m, "prop") ? m.prop : m.id; |
| 185 |
|
| 186 |
FailureMgr.add_failure_mode( |
| 187 |
id: m.id, |
| 188 |
description: m.desc, |
| 189 |
actuator: if (m.failure == SERV) set_unserviceable(control_prop) |
| 190 |
elsif (m.failure == JAM) set_readonly(control_prop) |
| 191 |
else fail_engine(io.basename(control_prop))); |
| 192 |
|
| 193 |
# Recreate the prop tree interface |
| 194 |
var prop = FailureMgr.proproot ~ m.id; |
| 195 |
var n = props.globals.initNode(prop ~ "/serviceable", 1, "BOOL"); |
| 196 |
|
| 197 |
setlistener(n, compat_listener, 0, 0); |
| 198 |
setlistener(prop ~ "/failure-level", compat_listener, 0, 0); |
| 199 |
|
| 200 |
if (m.type == MTBF) { |
| 201 |
var trigger_type = "/mtbf"; |
| 202 |
FailureMgr.set_trigger(m.id, MtbfTrigger.new(0)); |
| 203 |
} |
| 204 |
else { |
| 205 |
var trigger_type = "/mcbf"; |
| 206 |
var control = contains(m, "mcbf_prop")? m.mcbf_prop : m.id; |
| 207 |
FailureMgr.set_trigger(m.id, McbfTrigger.new(control, 0)); |
| 208 |
} |
| 209 |
|
| 210 |
setprop(prop ~ trigger_type, 0); |
| 211 |
setlistener(prop ~ trigger_type, compat_listener, 0, 0); |
| 212 |
} |
| 213 |
|
| 214 |
FailureMgr.events["trigger-fired"].subscribe(trigger_listener); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
var lsnr = setlistener("sim/signals/fdm-initialized", compat_setup); |