2013-12-01 12:29:22 +00:00
|
|
|
# Class things:
|
|
|
|
var parents = [Map.Controller];
|
|
|
|
var __self__ = caller(0)[0];
|
|
|
|
Map.Controller.add("Aircraft position", __self__);
|
2014-02-02 18:00:32 +00:00
|
|
|
#Map.df_controller = __self__;
|
2014-04-29 02:26:32 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# encapsulate type of aircraft (main, ai, mp or ai+mp)
|
|
|
|
# This is so that we can use reuse the aircraft controller also
|
|
|
|
# for AI and/or MP traffic, which may use different properties
|
|
|
|
# and to fix up hard-coded property references in layers like TFC
|
|
|
|
var SOURCES = {};
|
|
|
|
SOURCES["main"] = {
|
|
|
|
getPosition: func subvec(geo.aircraft_position().latlon(), 0, 2),
|
|
|
|
getAltitude: func getprop('/position/altitude-ft'),
|
|
|
|
getHeading: func getprop('/orientation/heading-deg'),
|
|
|
|
};
|
|
|
|
|
|
|
|
# Layers which get updated every frame
|
|
|
|
var update_instant = [
|
|
|
|
"TFC",
|
|
|
|
];
|
|
|
|
|
|
|
|
var new = func(map, source='main') {
|
|
|
|
if (source != 'main')
|
|
|
|
die ("AI/MP traffic not yet supported (WIP)!");
|
|
|
|
|
2013-12-01 12:29:22 +00:00
|
|
|
var m = {
|
|
|
|
parents: [__self__],
|
|
|
|
map: map,
|
2014-04-29 02:26:32 +00:00
|
|
|
source: SOURCES[source], # main, ai, mp or ai+mp
|
|
|
|
_pos: nil, _time: nil, _range: nil,
|
2013-12-01 12:29:22 +00:00
|
|
|
};
|
2014-04-29 02:26:32 +00:00
|
|
|
m.timer1 = maketimer(0, m, update_pos);
|
|
|
|
m.timer2 = maketimer(0, m, update_layers);
|
|
|
|
m.timer1.start();
|
|
|
|
m.timer2.start();
|
|
|
|
m.update_pos();
|
2013-12-01 12:29:22 +00:00
|
|
|
return m;
|
|
|
|
};
|
|
|
|
var del = func(map) {
|
|
|
|
if (map != me.map) die();
|
2014-04-29 02:26:32 +00:00
|
|
|
me.timer1.stop();
|
|
|
|
me.timer2.stop();
|
2013-12-01 12:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Controller methods
|
|
|
|
var update_pos = func {
|
2014-04-29 02:26:32 +00:00
|
|
|
var (lat,lon) = me.source.getPosition();
|
|
|
|
me.map.setPos(lat:lat, lon:lon,
|
|
|
|
hdg:getprop("/orientation/heading-deg"),
|
|
|
|
alt:me.source.getAltitude());
|
|
|
|
foreach (var t; update_instant)
|
|
|
|
if ((var l=me.map.getLayer(t)) != nil)
|
|
|
|
l.update();
|
|
|
|
};
|
|
|
|
var update_layers = func {
|
|
|
|
var pos = me.map.getPosCoord();
|
2013-12-01 12:29:22 +00:00
|
|
|
var time = systime();
|
2014-04-29 02:26:32 +00:00
|
|
|
var range = me.map.getRange();
|
2013-12-01 12:29:22 +00:00
|
|
|
if (me._pos == nil)
|
|
|
|
me._pos = geo.Coord.new(pos);
|
2014-04-29 02:26:32 +00:00
|
|
|
# Always update if range changed
|
|
|
|
elsif (range == me._range) {
|
|
|
|
var dist_m = me._pos.direct_distance_to(pos);
|
2013-12-01 12:29:22 +00:00
|
|
|
# 2 NM until we update again
|
2014-04-29 02:26:32 +00:00
|
|
|
if (dist_m < 2 * NM2M) return;
|
|
|
|
# Update at most every 4 seconds to avoid excessive stutter:
|
2013-12-01 12:29:22 +00:00
|
|
|
elsif (time - me._time < 4) return;
|
|
|
|
}
|
2014-04-29 02:26:32 +00:00
|
|
|
printlog(_MP_dbg_lvl, "update aircraft position");
|
2013-12-01 12:29:22 +00:00
|
|
|
var (x,y,z) = pos.xyz();
|
|
|
|
me._pos.set_xyz(x,y,z);
|
|
|
|
me._time = time;
|
2014-04-29 02:26:32 +00:00
|
|
|
me._range = range;
|
2013-12-01 12:29:22 +00:00
|
|
|
me.map.update();
|
|
|
|
};
|
|
|
|
|
2014-04-29 02:26:32 +00:00
|
|
|
# predicate for the draw controller
|
|
|
|
var is_tuned = func(freq) {
|
|
|
|
var nav1=getprop("instrumentation/nav[0]/frequencies/selected-mhz");
|
|
|
|
var nav2=getprop("instrumentation/nav[1]/frequencies/selected-mhz");
|
|
|
|
if (freq == nav1 or freq == nav2) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var get_tuned_course = func(freq) {
|
|
|
|
if (freq == getprop("instrumentation/nav[0]/frequencies/selected-mhz"))
|
|
|
|
return getprop("instrumentation/nav[0]/radials/selected-deg");
|
|
|
|
else
|
|
|
|
return getprop("instrumentation/nav[1]/radials/selected-deg");
|
|
|
|
}
|
|
|
|
var get_position = func {
|
|
|
|
delete(caller(0)[0], "me"); # remove local me, inherit outer one
|
|
|
|
return [
|
|
|
|
me.aircraft_source.get_lat(), me.aircraft_source.get_lon()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|