2004-03-27 04:07:56 +00:00
|
|
|
# Properties under /consumables/fuel/tank[n]:
|
2007-01-31 15:53:01 +00:00
|
|
|
# + level-gal_us - Current fuel load. Can be set by user code.
|
|
|
|
# + level-lbs - OUTPUT ONLY property, do not try to set
|
2004-03-27 04:07:56 +00:00
|
|
|
# + selected - boolean indicating tank selection.
|
|
|
|
# + density-ppg - Fuel density, in lbs/gallon.
|
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) {
|
|
|
|
var cap = t.getNode("capacity-gal_us").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) {
|
|
|
|
var ppg = t.getNode("density-ppg").getValue();
|
|
|
|
var lbs = t.getNode("level-gal_us").getValue() * ppg;
|
|
|
|
lbs = lbs - fuel_per_tank;
|
|
|
|
if (lbs < 0) {
|
|
|
|
lbs = 0;
|
|
|
|
# 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);
|
|
|
|
}
|
|
|
|
var gals = lbs / ppg;
|
|
|
|
t.getNode("level-gal_us").setDoubleValue(gals);
|
|
|
|
t.getNode("level-lbs").setDoubleValue(lbs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Total fuel properties
|
|
|
|
var lbs = 0;
|
|
|
|
var gals = 0;
|
|
|
|
var cap = 0;
|
|
|
|
|
|
|
|
foreach (var t; tanks) {
|
|
|
|
lbs += t.getNode("level-lbs").getValue();
|
|
|
|
gals += t.getNode("level-gal_us").getValue();
|
|
|
|
cap += t.getNode("capacity-gal_us").getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
total_lbs.setDoubleValue(lbs);
|
|
|
|
total_gals.setDoubleValue(gals);
|
|
|
|
total_norm.setDoubleValue(gals / cap);
|
|
|
|
|
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
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
var init_double_prop = func(node, prop, val) {
|
|
|
|
if (node.getNode(prop) != nil)
|
2007-01-31 15:53:01 +00:00
|
|
|
val = num(node.getNode(prop).getValue());
|
2007-10-07 15:12:05 +00:00
|
|
|
|
2007-01-31 15:53:01 +00:00
|
|
|
node.getNode(prop, 1).setDoubleValue(val);
|
2004-03-27 04:07:56 +00:00
|
|
|
}
|
2007-01-31 15:53:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tanks = [];
|
|
|
|
var engines = [];
|
|
|
|
var fuel_freeze = nil;
|
|
|
|
var total_gals = nil;
|
|
|
|
var total_lbs = nil;
|
|
|
|
var total_norm = nil;
|
|
|
|
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
_setlistener("/sim/signals/fdm-initialized", func {
|
2007-01-31 15:53:01 +00:00
|
|
|
setlistener("/sim/freeze/fuel", func { fuel_freeze = cmdarg().getBoolValue() }, 1);
|
|
|
|
|
|
|
|
total_gals = props.globals.getNode("/consumables/fuel/total-fuel-gals", 1);
|
|
|
|
total_lbs = props.globals.getNode("/consumables/fuel/total-fuel-lbs", 1);
|
|
|
|
total_norm = props.globals.getNode("/consumables/fuel/total-fuel-norm", 1);
|
|
|
|
|
|
|
|
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")) {
|
2007-10-07 15:12:05 +00:00
|
|
|
if (!size(t.getChildren()))
|
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);
|
|
|
|
init_double_prop(t, "level-gal_us", 0.0);
|
|
|
|
init_double_prop(t, "level-lbs", 0.0);
|
|
|
|
init_double_prop(t, "capacity-gal_us", 0.01); # not zero (div/zero issue)
|
|
|
|
init_double_prop(t, "density-ppg", 6.0); # gasoline
|
|
|
|
|
2007-10-07 15:12:05 +00:00
|
|
|
if (t.getNode("selected") == nil)
|
2007-01-31 15:53:01 +00:00
|
|
|
t.getNode("selected", 1).setBoolValue(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
loop();
|
|
|
|
});
|
|
|
|
|
|
|
|
|