| 1 |
var reset_all_damage = func |
| 2 |
{ |
| 3 |
setprop("/engines/active-engine/crash-engine", 0); |
| 4 |
setprop("/engines/active-engine/kill-engine", 0); |
| 5 |
|
| 6 |
# Landing gear |
| 7 |
setprop("/fdm/jsbsim/gear/unit[0]/broken", 0); |
| 8 |
setprop("/fdm/jsbsim/gear/unit[1]/broken", 0); |
| 9 |
setprop("/fdm/jsbsim/gear/unit[2]/broken", 0); |
| 10 |
|
| 11 |
setprop("/fdm/jsbsim/gear/unit[19]/broken", 0); |
| 12 |
setprop("/fdm/jsbsim/gear/unit[20]/broken", 0); |
| 13 |
setprop("/fdm/jsbsim/gear/unit[21]/broken", 0); |
| 14 |
setprop("/fdm/jsbsim/gear/unit[22]/broken", 0); |
| 15 |
|
| 16 |
if (getprop("/fdm/jsbsim/bushkit")==3) { |
| 17 |
if (getprop("/fdm/jsbsim/hydro/active-norm")) { |
| 18 |
setprop("controls/gear/gear-down-command", 0); |
| 19 |
setprop("/fdm/jsbsim/gear/gear-pos-norm", 0); |
| 20 |
} else { |
| 21 |
setprop("controls/gear/gear-down-command", 1); |
| 22 |
setprop("/fdm/jsbsim/gear/gear-pos-norm", 1); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
# Wings |
| 27 |
setprop("/fdm/jsbsim/wing-damage/left-wing", 0); |
| 28 |
setprop("/fdm/jsbsim/wing-damage/right-wing", 0); |
| 29 |
|
| 30 |
# Collapsed wings |
| 31 |
setprop("/fdm/jsbsim/crash", 0); |
| 32 |
|
| 33 |
# Pontoons |
| 34 |
setprop("/fdm/jsbsim/pontoon-damage/left-pontoon", 0); |
| 35 |
setprop("/fdm/jsbsim/pontoon-damage/right-pontoon", 0); |
| 36 |
|
| 37 |
if (getprop("fdm/jsbsim/orientation/upside-down")) { |
| 38 |
setprop("/orientation/pitch-deg", 0); |
| 39 |
setprop("/orientation/roll-deg", 0); |
| 40 |
} |
| 41 |
|
| 42 |
# Repair engine damage |
| 43 |
setprop("/fdm/jsbsim/engine/damage-level", 0); |
| 44 |
} |
| 45 |
|
| 46 |
var repair_damage = func { |
| 47 |
set_bushkit(getprop("/fdm/jsbsim/bushkit")); |
| 48 |
}; |
| 49 |
|
| 50 |
var killengine = func |
| 51 |
{ |
| 52 |
if (getprop("/fdm/jsbsim/settings/damage")) |
| 53 |
setprop("/engines/active-engine/crash-engine", 1); |
| 54 |
} |
| 55 |
|
| 56 |
# Hydro system loop |
| 57 |
|
| 58 |
var anchor_pos = geo.Coord.new(); |
| 59 |
var aircraft_pos = geo.Coord.new(); |
| 60 |
var anchor_dist = 0.0; |
| 61 |
|
| 62 |
var heading_diff = 0.0; |
| 63 |
var wind_from_heading = 0.0; |
| 64 |
var aircraft_heading = 0.0; |
| 65 |
var wind_speed = 0.0; |
| 66 |
var forward_speed = 0.0; |
| 67 |
|
| 68 |
var poll_hydro = func |
| 69 |
{ |
| 70 |
var engine_rpm = getprop("fdm/jsbsim/propulsion/engine/engine-rpm"); |
| 71 |
var hydro_active_norm = getprop("/fdm/jsbsim/hydro/active-norm"); |
| 72 |
var ground_splash_norm = getprop("/environment/aircraft-effects/ground-splash-norm"); |
| 73 |
|
| 74 |
# Use engine RPM and speed to control ground splash if on the water |
| 75 |
# and below 2 meter AGL |
| 76 |
if (getprop("position/altitude-agl-m") < 2 and hydro_active_norm > 0) { |
| 77 |
var groundspeed_half_pc = 0.005 * getprop("/velocities/groundspeed-kt"); |
| 78 |
var engine_rpm_almost_nothing = 0.005 * 0.065 * engine_rpm; |
| 79 |
|
| 80 |
var splash_norm = std.max(engine_rpm_almost_nothing, groundspeed_half_pc); |
| 81 |
setprop("/environment/aircraft-effects/ground-splash-norm", splash_norm); |
| 82 |
} |
| 83 |
elsif (ground_splash_norm > 0) |
| 84 |
setprop("/environment/aircraft-effects/ground-splash-norm", ground_splash_norm - 0.005); |
| 85 |
|
| 86 |
if (getprop("/controls/mooring/anchor") and !getprop("fdm/jsbsim/mooring/mooring-connected")) { |
| 87 |
if (getprop("/fdm/jsbsim/mooring/anchor-length") == 0) { |
| 88 |
wind_from_heading = getprop("/environment/wind-from-heading-deg"); |
| 89 |
aircraft_heading = getprop("/orientation/heading-deg"); |
| 90 |
heading_diff = math.min(math.abs(aircraft_heading-wind_from_heading),math.abs(360-aircraft_heading+wind_from_heading),math.abs(360-wind_from_heading+aircraft_heading)); |
| 91 |
wind_speed = getprop("/environment/windsock/wind-speed-kt"); |
| 92 |
forward_speed = getprop("/fdm/jsbsim/hydro/vbx-fps"); |
| 93 |
if (heading_diff > 85) { |
| 94 |
gui.popupTip("Face into the wind to set anchor", 5); |
| 95 |
setprop("/controls/mooring/anchor", 0); |
| 96 |
return; |
| 97 |
} elsif (wind_speed > 40) { |
| 98 |
gui.popupTip("Wind too high to anchor", 5); |
| 99 |
setprop("/controls/mooring/anchor", 0); |
| 100 |
return; |
| 101 |
} elsif (forward_speed > 7) { |
| 102 |
gui.popupTip("Can't anchor while moving forward", 5); |
| 103 |
setprop("/controls/mooring/anchor", 0); |
| 104 |
return; |
| 105 |
} elsif (getprop("/engines/active-engine/running")) { |
| 106 |
gui.popupTip("Can't anchor with engine running", 5); |
| 107 |
setprop("/controls/mooring/anchor", 0); |
| 108 |
return; |
| 109 |
} else { |
| 110 |
anchor_pos.set_latlon(getprop("/fdm/jsbsim/mooring/anchor-lat"), getprop("/fdm/jsbsim/mooring/anchor-lon")); |
| 111 |
setprop("fdm/jsbsim/mooring/latitude-deg", anchor_pos.lat()); |
| 112 |
setprop("fdm/jsbsim/mooring/longitude-deg", anchor_pos.lon()); |
| 113 |
setprop("fdm/jsbsim/mooring/altitude-ft", getprop("/position/ground-elev-ft")); |
| 114 |
anchor_dist = 0.0; |
| 115 |
setprop("/fdm/jsbsim/mooring/anchor-length", 1); |
| 116 |
setprop("/sim/anchorbuoy/enable", 1); |
| 117 |
setprop("fdm/jsbsim/mooring/mooring-connected", 1); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (getprop("fdm/jsbsim/mooring/mooring-connected")) { |
| 123 |
aircraft_pos = geo.aircraft_position(); |
| 124 |
anchor_dist = aircraft_pos.distance_to(anchor_pos); |
| 125 |
aircraft_heading = getprop("/orientation/heading-deg"); |
| 126 |
var bearing = aircraft_pos.course_to(anchor_pos); |
| 127 |
var rel_bearing = (aircraft_heading - 180.0) - bearing; |
| 128 |
setprop("fdm/jsbsim/mooring/rope-yaw", rel_bearing); |
| 129 |
|
| 130 |
if (anchor_dist < (getprop("/fdm/jsbsim/mooring/rope-length-ft")*0.3048)-1) { |
| 131 |
setprop("fdm/jsbsim/mooring/rope-visible", 0); |
| 132 |
} else { |
| 133 |
setprop("fdm/jsbsim/mooring/rope-visible", 1); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
# Duration in which no damage will occur. Assumes the aircraft has |
| 139 |
# stabilized within this duration. |
| 140 |
var bushkit_change_timeout = 8.0; |
| 141 |
|
| 142 |
var physics_loop = func |
| 143 |
{ |
| 144 |
if (getprop("/sim/freeze/replay-state")) { |
| 145 |
return; |
| 146 |
} |
| 147 |
c172p.particle_effects_loop(); |
| 148 |
if (getprop("/fdm/jsbsim/bushkit") == 3 or getprop("/fdm/jsbsim/bushkit") == 4) |
| 149 |
poll_hydro(); |
| 150 |
if (getprop("/fdm/jsbsim/contact/unit[9]/WOW") or getprop("/fdm/jsbsim/contact/unit[10]/WOW")) |
| 151 |
killengine(); |
| 152 |
} |
| 153 |
|
| 154 |
var set_bushkit = func (bushkit) { |
| 155 |
setprop("/fdm/jsbsim/damage/repairing", 1); |
| 156 |
reset_all_damage(); |
| 157 |
bushkit_changed_timer.restart(bushkit_change_timeout); |
| 158 |
}; |
| 159 |
|
| 160 |
# This timer object is used to enable damage again a short time after |
| 161 |
# changing to the last bush kit option. |
| 162 |
var bushkit_changed_timer = maketimer(bushkit_change_timeout, func { |
| 163 |
setprop("/fdm/jsbsim/damage/repairing", 0); |
| 164 |
setprop("/fdm/jsbsim/damage/traversing", 0); |
| 165 |
}); |
| 166 |
bushkit_changed_timer.singleShot = 1; |
| 167 |
|
| 168 |
# Update the 3D model when changing bush kit |
| 169 |
setlistener("/fdm/jsbsim/bushkit", func (n) { |
| 170 |
set_bushkit(n.getValue()); |
| 171 |
}, 1, 0); |
| 172 |
|
| 173 |
setlistener("/fdm/jsbsim/crash", func (n) { |
| 174 |
if (n.getBoolValue() and getprop("position/altitude-agl-m") < 10) { |
| 175 |
killengine(); |
| 176 |
} |
| 177 |
}, 0, 0); |
| 178 |
|
| 179 |
setlistener("/fdm/jsbsim/wing-damage/left-wing", func (n) { |
| 180 |
var damage = n.getValue(); |
| 181 |
var altitude = getprop("position/altitude-agl-m"); |
| 182 |
|
| 183 |
if (damage < 1.0 and damage > 0.0 and altitude < 10.0) |
| 184 |
killengine(); |
| 185 |
}, 0, 0); |
| 186 |
|
| 187 |
setlistener("/fdm/jsbsim/wing-damage/right-wing", func (n) { |
| 188 |
var damage = n.getValue(); |
| 189 |
var altitude = getprop("position/altitude-agl-m"); |
| 190 |
|
| 191 |
if (damage < 1.0 and damage > 0.0 and altitude < 10.0) |
| 192 |
killengine(); |
| 193 |
}, 0, 0); |
| 194 |
|
| 195 |
setlistener("controls/gear/gear-down-command", func (n) { |
| 196 |
if (getprop("/fdm/jsbsim/pontoon-damage/left-pontoon")==0 and getprop("/fdm/jsbsim/pontoon-damage/right-pontoon")==0) { |
| 197 |
setprop("/fdm/jsbsim/damage/traversing", 1); |
| 198 |
bushkit_changed_timer.restart(bushkit_change_timeout); |
| 199 |
} |
| 200 |
}, 0, 0); |
| 201 |
|
| 202 |
# Float water disipation time |
| 203 |
var float_water_dissipation =0.5; |
| 204 |
|
| 205 |
# This timer object is used to display water particle effect falling from |
| 206 |
# floats for a short time after leaving the water. |
| 207 |
|
| 208 |
var float_water_dissipation_timer = maketimer(float_water_dissipation, func { |
| 209 |
setprop("/fdm/jsbsim/float/water-dissipation", 0); |
| 210 |
}); |
| 211 |
float_water_dissipation_timer.singleShot = 1; |
| 212 |
|
| 213 |
setlistener("fdm/jsbsim/hydro/active-norm", func (n) { |
| 214 |
if (!n.getBoolValue()) { |
| 215 |
setprop("/fdm/jsbsim/float/water-dissipation", 1); |
| 216 |
float_water_dissipation_timer.restart(float_water_dissipation); |
| 217 |
} |
| 218 |
}, 0, 0); |