2004-03-27 04:07:56 +00:00
|
|
|
# Properties under /consumables/fuel/tank[n]:
|
2011-02-14 22:13:12 +00:00
|
|
|
# + level-lbs - Current fuel load. Can be set by user code.
|
2004-03-27 04:07:56 +00:00
|
|
|
# + selected - boolean indicating tank selection.
|
2007-01-31 15:53:01 +00:00
|
|
|
# + capacity-gal_us - Tank capacity
|
2004-03-27 04:07:56 +00:00
|
|
|
#
|
|
|
|
# Properties under /engines/engine[n]:
|
|
|
|
# + fuel-consumed-lbs - Output from the FDM, zeroed by this script
|
|
|
|
# + out-of-fuel - boolean, set by this code.
|
|
|
|
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
var UPDATE_PERIOD = 0.3;
|
|
|
|
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
var update = func {
|
|
|
|
if (fuel_freeze)
|
2007-01-31 15:53:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var consumed_fuel = 0;
|
|
|
|
foreach (var e; engines) {
|
|
|
|
var fuel = e.getNode("fuel-consumed-lbs");
|
|
|
|
consumed_fuel += fuel.getValue();
|
|
|
|
fuel.setDoubleValue(0);
|
|
|
|
}
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
if (!consumed_fuel)
|
2007-01-31 15:53:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var selected_tanks = [];
|
|
|
|
foreach (var t; tanks) {
|
2011-04-02 09:29:40 +00:00
|
|
|
var cap = t.getNode("capacity-gal_us",0).getValue();
|
2007-10-07 15:12:05 +00:00
|
|
|
if (cap > 0.01 and t.getNode("selected").getBoolValue())
|
2007-01-31 15:53:01 +00:00
|
|
|
append(selected_tanks, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Subtract fuel from tanks, set auxilliary properties. Set out-of-fuel
|
|
|
|
# when any one tank is dry.
|
|
|
|
var out_of_fuel = 0;
|
|
|
|
if (size(selected_tanks) == 0) {
|
|
|
|
out_of_fuel = 1;
|
|
|
|
} else {
|
|
|
|
var fuel_per_tank = consumed_fuel / size(selected_tanks);
|
|
|
|
foreach (var t; selected_tanks) {
|
2011-02-14 22:13:12 +00:00
|
|
|
var lbs = t.getNode("level-lbs").getValue();
|
2007-01-31 15:53:01 +00:00
|
|
|
lbs = lbs - fuel_per_tank;
|
2011-02-14 22:13:12 +00:00
|
|
|
t.getNode("level-lbs").setDoubleValue(lbs);
|
|
|
|
if( t.getNode("empty").getBoolValue() ) {
|
2007-01-31 15:53:01 +00:00
|
|
|
# Kill the engines if we're told to, otherwise simply
|
|
|
|
# deselect the tank.
|
2007-10-07 15:12:05 +00:00
|
|
|
if (t.getNode("kill-when-empty", 1).getBoolValue())
|
2007-01-31 15:53:01 +00:00
|
|
|
out_of_fuel = 1;
|
2007-10-07 15:12:05 +00:00
|
|
|
else
|
2007-01-31 15:53:01 +00:00
|
|
|
t.getNode("selected").setBoolValue(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
foreach (var e; engines)
|
2007-01-31 15:53:01 +00:00
|
|
|
e.getNode("out-of-fuel").setBoolValue(out_of_fuel);
|
2004-03-27 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
var loop = func {
|
2007-01-31 15:53:01 +00:00
|
|
|
update();
|
|
|
|
settimer(loop, UPDATE_PERIOD);
|
2004-03-27 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
var tanks = [];
|
|
|
|
var engines = [];
|
|
|
|
var fuel_freeze = nil;
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
_setlistener("/sim/signals/fdm-initialized", func {
|
2007-10-15 18:27:42 +00:00
|
|
|
setlistener("/sim/freeze/fuel", func(n) { fuel_freeze = n.getBoolValue() }, 1);
|
2007-01-31 15:53:01 +00:00
|
|
|
|
|
|
|
engines = props.globals.getNode("engines", 1).getChildren("engine");
|
|
|
|
foreach (var e; engines) {
|
|
|
|
e.getNode("fuel-consumed-lbs", 1).setDoubleValue(0);
|
|
|
|
e.getNode("out-of-fuel", 1).setBoolValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var t; props.globals.getNode("/consumables/fuel", 1).getChildren("tank")) {
|
2008-08-10 17:53:41 +00:00
|
|
|
if (!t.getAttribute("children"))
|
2007-01-31 15:53:01 +00:00
|
|
|
continue; # skip native_fdm.cxx generated zombie tanks
|
2007-10-07 15:12:05 +00:00
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
append(tanks, t);
|
2008-11-20 19:45:40 +00:00
|
|
|
t.initNode("selected", 1, "BOOL");
|
2007-01-31 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop();
|
|
|
|
});
|
|
|
|
|
|
|
|
|