Debian Codegraph @

Nasal/canvas/map/TFC.lcontroller

raw
1 # See: http://wiki.flightgear.org/MapStructure
2 # FIXME: this needs to be configurable via the ctor to optionally differentiate between MP and AI traffic, and exclude ground/sea traffic
3 # or at least use a different, non-TCAS symbol for those (looking pretty weird ATM)
4 #
5 # Class things:
6 var name = 'TFC';
7 var parents = [SymbolLayer.Controller];
8 var __self__ = caller(0)[0];
9 SymbolLayer.Controller.add(name, __self__);
10 SymbolLayer.add(name, {
11 parents: [MultiSymbolLayer],
12 type: name, # Symbol type
13 df_controller: __self__, # controller to use by default -- this one
14 df_style: {
15 },
16 df_options: { # default configuration options
17 floor_ft: 9900, # Display target from this height below the aircraft
18 ceiling_ft: 9900, # Display targets up to this height above the aircraft
19 display_id: 1, # Display aircraft ID
20 },
21 });
22
23 var model_root = props.globals.getNode("/ai/models/");
24
25 var new = func(layer) {
26 var m = {
27 parents: [__self__],
28 layer: layer,
29 map: layer.map,
30 listeners: [],
31 searchCmd: searchCmd_default,
32 };
33 layer.searcher._equals = func(l,r) l.equals(r);
34 m.addVisibilityListener();
35
36 return m;
37 };
38 var del = func() {
39 #print(name~".lcontroller.del()");
40 foreach (var l; me.listeners)
41 removelistener(l);
42 };
43
44 var TrafficModel = {
45 new: func(node, id=nil, layer=nil) {
46 if (id == nil) id = node.getValue("id");
47 var m = {
48 # Note: because this inherits from props.Node, Symbol.Controller.equals
49 # will call l.equals(r) -- the one defined below
50 parents: [TrafficModel, geo.Coord, node], # note we don't implement a full geo.Coord API
51 id: id,
52 node: node,
53 pos: node.getNode("position",1),
54 type: node.getName(),
55 };
56 return m;
57 },
58 equals: func(other) other.id == me.id,
59 latlon: func() { # this makes sure to look like a geo.Coord to MapStructure, but will internally use the AI/MP traffic properties instead
60 return [
61 me.pos.getValue("latitude-deg"),
62 me.pos.getValue("longitude-deg"),
63 me.pos.getValue("altitude-ft")
64 ];
65 },
66 # these are helper methods related to TCAS handling (TAs/RAs)
67 get_threat_lvl: func() me.getValue("tcas/threat-level"),
68 get_vspd: func() (me.getValue("velocities/vertical-speed-fps") or 0)*60,
69 get_alt: func() (me.getValue("position/altitude-ft") or 0),
70 };
71
72 var get_alt_diff = func(model) {
73 # debug.dump( keys(me) );
74 var model_alt = model.get_alt();
75 var alt = me.map.getAlt();
76 if (alt == nil or model_alt == nil) return 0;
77 return model_alt - alt;
78 };
79
80 ##
81 # dummy/placeholder (will be overridden in ctor and set to the default callback)
82 var searchCmd = func;
83
84 var searchCmd_default = func {
85 # TODO: this would be a good candidate for splitting across frames
86 logprint(_MP_dbg_lvl, "Doing query: "~name);
87
88 if (me.map.getRange() == nil) return;
89
90 var result = [];
91 var models = 0;
92
93 var alt = me.map.getAlt();
94 if (alt == nil) alt = 0;
95
96 var min_alt = alt - me.layer.options.floor_ft;
97 var max_alt = alt + me.layer.options.ceiling_ft;
98
99 # AI and Multiplayer traffic
100 foreach (var t; model_root.getChildren()) {
101 if (!t.getValue("valid")) continue;
102 var t_id = t.getNode("id");
103 if (t_id == nil or t_id.getValue() == -1) continue;
104 models += 1;
105 var (lat,lon) = (t.getValue("position/latitude-deg"),
106 t.getValue("position/longitude-deg"));
107 if (lat == nil or lon == nil) {
108 logprint(LOG_ALERT, "lat/lon was nil for AI node "~t.getPath());
109 continue;
110 }
111
112 var tm = TrafficModel.new(t, nil, me.layer);
113
114 if (((tm.get_threat_lvl() or -1) > -1) and
115 (min_alt < tm.get_alt()) and
116 (tm.get_alt() < max_alt) and
117 me.map.controller.in_range(lat, lon) ) {
118 append(result, tm);
119 }
120 }
121
122 #print("Found "~size(result)~" TrafficModel's in range out of "~models~" total.");
123 return result;
124 };