2007-07-08 18:15:02 +00:00
|
|
|
#### Aerotro's Flightgear Cam ####
|
|
|
|
|
|
|
|
var CAM = props.globals.getNode("/sim/cam");
|
|
|
|
|
|
|
|
var MPS = 0.514444444;
|
|
|
|
var D2R = math.pi / 180;
|
|
|
|
var R2D = 180 / math.pi;
|
|
|
|
var sin = func(v) math.sin(v * D2R);
|
|
|
|
var cos = func(v) math.cos(v * D2R);
|
|
|
|
var atan2 = func(v, w) math.atan2(v, w) * R2D;
|
2007-08-21 05:35:40 +00:00
|
|
|
|
2007-07-09 22:32:58 +00:00
|
|
|
var panel_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/panel/dialog",
|
|
|
|
"Aircraft/ufo/Dialogs/cam.xml");
|
|
|
|
var callsign_dialog = gui.Dialog.new("/sim/gui/dialogs/cam/select/dialog",
|
|
|
|
"Aircraft/ufo/Dialogs/callsign.xml");
|
|
|
|
|
2007-07-08 18:15:02 +00:00
|
|
|
var maxspeedN = props.globals.getNode("engines/engine/speed-max-mps");
|
|
|
|
var speed = [10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000];
|
2007-08-21 05:35:40 +00:00
|
|
|
var current = 5;
|
2007-07-08 18:15:02 +00:00
|
|
|
var maxspeed = speed[current];
|
|
|
|
var cam_view = nil;
|
2007-07-09 22:32:58 +00:00
|
|
|
var view_number = 0;
|
2007-07-27 17:41:59 +00:00
|
|
|
var min_alt_agl = 5.0; # meter
|
2007-07-08 18:15:02 +00:00
|
|
|
|
|
|
|
var targetN = nil;
|
|
|
|
var target = geo.Coord.new();
|
|
|
|
var self = nil;
|
2007-07-09 22:32:58 +00:00
|
|
|
var aircraft_list = [];
|
2007-07-08 18:15:02 +00:00
|
|
|
|
|
|
|
|
2007-07-27 17:41:59 +00:00
|
|
|
var goal_headingN = props.globals.getNode("sim/current-view/goal-heading-offset-deg");
|
|
|
|
var goal_pitchN = props.globals.getNode("sim/current-view/goal-pitch-offset-deg");
|
|
|
|
var goal_rollN = props.globals.getNode("sim/current-view/goal-roll-offset-deg");
|
|
|
|
|
|
|
|
|
|
|
|
var mode = {
|
2007-08-20 06:22:06 +00:00
|
|
|
chase : props.globals.getNode("/sim/cam/chase"),
|
|
|
|
focus : props.globals.getNode("/sim/cam/focus"),
|
|
|
|
speed : props.globals.getNode("/sim/cam/speed"),
|
|
|
|
alt : props.globals.getNode("/sim/cam/alt"),
|
|
|
|
lock : props.globals.getNode("/sim/cam/lock"),
|
2007-07-27 17:41:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var lowpass = {
|
2007-08-20 06:22:06 +00:00
|
|
|
viewheading : aircraft.angular_lowpass.new(0.2),
|
|
|
|
viewpitch : aircraft.lowpass.new(0.1),
|
|
|
|
viewroll : aircraft.lowpass.new(0.1),
|
|
|
|
|
|
|
|
hdg : aircraft.angular_lowpass.new(0.6),
|
|
|
|
alt : aircraft.lowpass.new(3),
|
|
|
|
speed : aircraft.lowpass.new(2),
|
|
|
|
throttle : aircraft.lowpass.new(0.3),
|
2007-07-27 17:41:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-07-08 18:15:02 +00:00
|
|
|
controls.flapsDown = func(x) {
|
2007-08-20 06:22:06 +00:00
|
|
|
if (!x)
|
|
|
|
return;
|
|
|
|
elsif (x < 0 and current > 0)
|
|
|
|
current -= 1;
|
|
|
|
elsif (x > 0 and current < size(speed) - 1)
|
|
|
|
current += 1;
|
|
|
|
|
|
|
|
maxspeed = speed[current];
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 22:32:58 +00:00
|
|
|
#var throttle = 0;
|
2007-10-15 18:59:34 +00:00
|
|
|
#controls.throttleAxis = func(n) {
|
|
|
|
# val = n.getNode("setting").getValue();
|
2007-07-09 22:32:58 +00:00
|
|
|
# if (size(arg) > 0)
|
|
|
|
# val = -val;
|
|
|
|
# throttle = (1 - val) * 0.5;
|
2007-07-27 17:41:59 +00:00
|
|
|
# props.setAll("/controls/engines/engine", "throttle", lowpass.throttle.filter(throttle));
|
2007-07-09 22:32:58 +00:00
|
|
|
#}
|
2007-07-08 18:15:02 +00:00
|
|
|
|
|
|
|
|
2007-07-27 17:41:59 +00:00
|
|
|
# sort function
|
2007-07-09 22:32:58 +00:00
|
|
|
var by_callsign = func(a, b) {
|
2007-08-20 06:22:06 +00:00
|
|
|
cmp(a.getNode("callsign").getValue(), b.getNode("callsign").getValue());
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 22:32:58 +00:00
|
|
|
var update_aircraft_list = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
var ac = [];
|
|
|
|
var n = props.globals.getNode("/ai/models");
|
2007-07-09 22:32:58 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
if (getprop("/sim/cam/target-ai"))
|
|
|
|
ac ~= n.getChildren("aircraft") ~ n.getChildren("tanker");
|
|
|
|
if (getprop("/sim/cam/target-mp"))
|
|
|
|
ac ~= n.getChildren("multiplayer");
|
2007-07-08 18:15:02 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
aircraft_list = [];
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
foreach (var model; ac) {
|
|
|
|
var model_node = model.getNode("valid");
|
|
|
|
if (model_node != nil and model_node.getBoolValue()) {
|
|
|
|
append(aircraft_list, model);
|
|
|
|
}
|
2007-08-20 04:22:36 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
aircraft_list = sort(aircraft_list, by_callsign);
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var update = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
# data acquisition
|
|
|
|
target.set_latlon(
|
|
|
|
targetN.getNode("position/latitude-deg").getValue(),
|
|
|
|
targetN.getNode("position/longitude-deg").getValue(),
|
|
|
|
targetN.getNode("position/altitude-ft").getValue() * geo.FT2M);
|
2007-07-08 18:15:02 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
self = geo.aircraft_position();
|
2007-07-27 17:41:59 +00:00
|
|
|
|
2007-07-08 18:15:02 +00:00
|
|
|
if (0) {
|
2007-08-20 06:22:06 +00:00
|
|
|
if (mode.lock.getValue()) {
|
|
|
|
self.set(target);
|
|
|
|
self.apply_course_distance(rel.course, rel.distance);
|
|
|
|
self.set_alt(target.alt() + rel.alt);
|
|
|
|
setprop("/position/latitude-deg", self.lat());
|
|
|
|
setprop("/position/longitude-deg", self.lon());
|
|
|
|
setprop("/position/altitude-ft", self.alt() * geo.M2FT);
|
|
|
|
}
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
var self_heading = getprop("/orientation/heading-deg");
|
|
|
|
var self_pitch = getprop("/orientation/pitch-deg");
|
|
|
|
var self_roll = getprop("/orientation/roll-deg");
|
|
|
|
|
|
|
|
# check whether to unlock altitude/heading
|
|
|
|
if (abs(self_pitch) > 2)
|
|
|
|
mode.alt.setValue(0);
|
|
|
|
if (abs(self_roll) > 2)
|
|
|
|
mode.chase.setValue(0);
|
|
|
|
|
|
|
|
# calculate own altitude
|
|
|
|
var min_alt_asl = target.alt() - getprop("/position/altitude-agl-ft") * geo.FT2M + min_alt_agl;
|
|
|
|
if (self.alt() <= min_alt_asl)
|
|
|
|
setprop("/position/altitude-ft", lowpass.alt.filter(min_alt_asl) * geo.M2FT);
|
|
|
|
elsif (mode.alt.getValue())
|
|
|
|
setprop("/position/altitude-ft", lowpass.alt.filter(target.alt()) * geo.M2FT);
|
|
|
|
else
|
|
|
|
lowpass.alt.filter(self.alt());
|
|
|
|
|
|
|
|
# calculate position relative to target
|
|
|
|
var distance = self.direct_distance_to(target);
|
|
|
|
if (distance < 1) {
|
|
|
|
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
|
|
|
mode.focus.setValue(0);
|
|
|
|
mode.chase.setValue(0);
|
|
|
|
} else {
|
|
|
|
var course = self.course_to(target);
|
|
|
|
}
|
|
|
|
var elevation = atan2(target.alt() - lowpass.alt.get(), distance);
|
|
|
|
|
|
|
|
# set own heading
|
2007-08-21 05:35:40 +00:00
|
|
|
if (mode.chase.getValue()){
|
|
|
|
var voff=targetN.getNode("radar/v-offset").getValue();
|
|
|
|
if(voff >60 or voff < -60){mode.chase.setValue(0);}
|
|
|
|
var dist=targetN.getNode("radar/range-nm").getValue();
|
|
|
|
if(dist < 0.01){mode.chase.setValue(0);}
|
|
|
|
setprop("/orientation/heading-deg", self_heading = lowpass.hdg.filter(course));
|
|
|
|
}
|
2007-08-20 06:22:06 +00:00
|
|
|
# calculate focus view direction
|
|
|
|
if (mode.focus.getValue()) {
|
|
|
|
var h = lowpass.viewheading.filter(self_heading - course);
|
|
|
|
var p = elevation - self_pitch * cos(h);
|
|
|
|
var r = -self_roll * sin(h);
|
|
|
|
goal_headingN.setValue(h);
|
|
|
|
goal_pitchN.setValue(lowpass.viewpitch.filter(p));
|
|
|
|
goal_rollN.setValue(lowpass.viewroll.filter(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
# calculate own speed
|
2007-08-21 05:35:40 +00:00
|
|
|
if (mode.speed.getValue())
|
|
|
|
maxspeed = targetN.getNode("velocities/true-airspeed-kt").getValue() * MPS * 2;
|
|
|
|
else
|
2007-08-20 06:22:06 +00:00
|
|
|
maxspeed = speed[current];
|
|
|
|
maxspeedN.setDoubleValue(lowpass.speed.filter(maxspeed));
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var loop = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
if (view_number == cam_view and targetN != nil)
|
|
|
|
update();
|
2007-08-21 05:35:40 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
settimer(loop, 0);
|
2007-07-08 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 22:32:58 +00:00
|
|
|
var select_aircraft = func(index) {
|
2007-08-20 06:22:06 +00:00
|
|
|
update_aircraft_list();
|
|
|
|
|
|
|
|
var number = size(aircraft_list);
|
|
|
|
var name = "";
|
|
|
|
targetN = nil;
|
|
|
|
|
|
|
|
if (number) {
|
|
|
|
if (index < 0)
|
|
|
|
index = number - 1;
|
|
|
|
elsif (index >= number)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
targetN = aircraft_list[index];
|
|
|
|
name = targetN.getNode("callsign").getValue();
|
|
|
|
}
|
|
|
|
setprop("/sim/cam/target-number", index);
|
|
|
|
setprop("/sim/cam/target-name", name);
|
2007-07-09 22:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-20 04:22:36 +00:00
|
|
|
var target_name_changed = func() {
|
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
var target_name = getprop("/sim/cam/target-name");
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
if (target_name == nil or cmp(target_name, "") == 0)
|
|
|
|
return;
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
forindex (var i; aircraft_list) {
|
|
|
|
if (cmp(target_name,
|
|
|
|
aircraft_list[i].getNode("callsign").getValue()) == 0) {
|
|
|
|
if (i != getprop("/sim/cam/target-number")) {
|
|
|
|
# found the index for the new target-name
|
|
|
|
select_aircraft(i);
|
|
|
|
}
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
# no matches, select the current index again
|
|
|
|
update_aircraft();
|
2007-08-20 04:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 22:32:58 +00:00
|
|
|
# called from the dialog
|
|
|
|
var goto_target = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
targetN != nil or return;
|
|
|
|
self != nil or return;
|
|
|
|
var lat = targetN.getNode("position/latitude-deg").getValue();
|
|
|
|
var lon = targetN.getNode("position/longitude-deg").getValue();
|
|
|
|
var alt = targetN.getNode("position/altitude-ft").getValue() * geo.FT2M;
|
|
|
|
var speed = targetN.getNode("velocities/true-airspeed-kt").getValue() * MPS * 2;
|
|
|
|
var course = targetN.getNode("orientation/true-heading-deg").getValue();
|
|
|
|
self.set_latlon(lat, lon, alt).apply_course_distance(course + 180, 100);
|
|
|
|
setprop("/position/latitude-deg", self.lat());
|
|
|
|
setprop("/position/longitude-deg", self.lon());
|
|
|
|
lowpass.alt.set(self.alt());
|
|
|
|
lowpass.speed.set(speed);
|
|
|
|
mode.chase.setValue(1);
|
|
|
|
mode.focus.setValue(1);
|
|
|
|
mode.alt.setValue(1);
|
|
|
|
mode.speed.setValue(1);
|
|
|
|
maxspeed = speed * 2;
|
2007-07-27 17:41:59 +00:00
|
|
|
# props.setAll("/controls/engines/engine", "throttle", lowpass.throttle.set(0.5));
|
2007-07-09 22:32:58 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 04:22:36 +00:00
|
|
|
var update_goto = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
if (!getprop("/sim/cam/goto"))
|
|
|
|
return;
|
2007-08-20 04:22:36 +00:00
|
|
|
|
2007-08-20 06:22:06 +00:00
|
|
|
goto_target();
|
|
|
|
setprop("/sim/cam/goto", 0);
|
2007-08-20 04:22:36 +00:00
|
|
|
}
|
2007-07-09 22:32:58 +00:00
|
|
|
|
|
|
|
var update_aircraft = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
select_aircraft(getprop("/sim/cam/target-number"));
|
2007-07-09 22:32:58 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 04:22:36 +00:00
|
|
|
var ai_removed = func {
|
2007-08-20 06:22:06 +00:00
|
|
|
var node_str = getprop("/ai/models/model-removed") ~ "/callsign";
|
|
|
|
var target_name = getprop(node_str);
|
|
|
|
if (cmp(target_name, getprop("/sim/cam/target-name")) == 0) {
|
|
|
|
update_aircraft();
|
|
|
|
}
|
2007-08-20 04:22:36 +00:00
|
|
|
}
|
2007-07-09 22:32:58 +00:00
|
|
|
|
|
|
|
setlistener("/sim/cam/target-number", update_aircraft);
|
2007-08-20 04:22:36 +00:00
|
|
|
setlistener("/sim/cam/target-name", target_name_changed);
|
2007-07-09 22:32:58 +00:00
|
|
|
setlistener("/sim/cam/target-ai", update_aircraft);
|
|
|
|
setlistener("/sim/cam/target-mp", update_aircraft);
|
2007-08-20 04:22:36 +00:00
|
|
|
setlistener("/sim/cam/goto", update_goto);
|
|
|
|
setlistener("/ai/models/model-removed", ai_removed);
|
2007-07-09 22:32:58 +00:00
|
|
|
|
2007-10-15 18:59:34 +00:00
|
|
|
setlistener("/sim/current-view/view-number", func(n) {
|
|
|
|
view_number = n.getValue();
|
2007-08-20 06:22:06 +00:00
|
|
|
if (view_number == cam_view)
|
|
|
|
panel_dialog.open();
|
|
|
|
else
|
|
|
|
panel_dialog.close();
|
2007-07-08 18:15:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (0) {
|
|
|
|
var rel = { course: 0, distance: 0, alt: 0 };
|
2007-10-15 18:59:34 +00:00
|
|
|
setlistener("/sim/cam/lock", func(n) {
|
|
|
|
if (n.getValue()) {
|
2007-08-20 06:22:06 +00:00
|
|
|
rel.course = target.course_to(self);
|
|
|
|
rel.distance = target.distance_to(self);
|
|
|
|
rel.alt = self.alt() - target.alt();
|
|
|
|
}
|
2007-07-08 18:15:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setlistener("/sim/signals/fdm-initialized", func {
|
2007-08-20 06:22:06 +00:00
|
|
|
var views = props.globals.getNode("/sim").getChildren("view");
|
|
|
|
forindex (var i; views)
|
|
|
|
if (views[i].getNode("name").getValue() == "Cam View")
|
|
|
|
cam_view = i;
|
|
|
|
|
|
|
|
setprop("/sim/current-view/view-number", cam_view);
|
|
|
|
setprop("/engines/engine/speed-max-mps", 500);
|
|
|
|
update_aircraft();
|
|
|
|
loop();
|
2007-07-08 18:15:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|