zkv1000 / Nasal / routes.nas /
Sébastien MARQUE commit initial
56c0030 8 years ago
1 contributor
154 lines | 6.38kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155var selectNextWayPoint = func (_me_, command) {
    var w = geo.Coord.new();
    var r = geo.Coord.new();
    var c = [];
    var scratch = '/instrumentation/gps/scratch/';
    w.set_latlon(getprop(_me_.map_path ~ '/latitude-deg'),
                 getprop(_me_.map_path ~ '/longitude-deg'));
    lockSearches();
    foreach (var t; ['fix', 'airport', 'vor', 'ndb']) {
        searchNearestNavaid(t, 3, _me_.map_path);
        while (getprop(scratch ~ '/has-next')) {
            r.set_latlon(getprop(scratch ~ 'latitude-deg'),
                         getprop(scratch ~ 'longitude-deg'));
            append(c, [ w.distance_to(r),
                        getprop(scratch ~ 'name'),
                        getprop(scratch ~ 'ident'),
                        getprop(scratch ~ 'mag-bearing-deg'),
                        t
                       ]
             );
             setprop('/instrumentation/gps/command', 'next');
        }
    }
    unlockSearches();
    c = sort(c, func (a,b) { return a[0] - b[0] });
    _me_.mud.wipe();
    var action = '';
    if (command == 'dto') {
        _me_.mud.setTitle('DIRECT TO...');
        action = 'direct to '
    }
    else {
        _me_.mud.setTitle('SELECT A WAYPOINT');
        action = 'appended to route ';
    }
    _me_.mud.add(['CANCEL'], func { _me_.mud.close() });
    _me_.mud.add(['HERE'], func {
                               var n = 'instrumentation/gps/scratch/';
                               lockSearches();
                               setprop(n ~ 'longitude-deg', _me_.map_path ~ '/longitude-deg');
                               setprop(n ~ 'latitude-deg', _me_.map_path ~ '/latitude-deg');
                               setprop(n ~ 'type', 'USER');
                               setprop(n ~ 'name', 'USER WAYPOINT');
                               setprop(n ~ 'ident', '---');
                               setprop(n ~ 'index', -1);
                               setprop(n ~ '../command', command);
                               unlockSearches();
                               msg(action ~ 'unnamed waypoint');
                           });
    for (var i = 0; i < size(c) and c[i][0] < 18520 and i < 10; i += 1) { # only the 10 nearest and distance < 10NM
        _me_.mud.add(
            [c[i][1], sprintf('%s -- DST %.1fNM BRG %03i', c[i][2], c[i][0] / 1852, c[i][3])], 
            func { 
                var n = '/instrumentation/gps/scratch/';
                lockSearches();
                setprop(n ~ 'longitude-deg', getprop(_me_.map_path ~ '/longitude-deg'));
                setprop(n ~ 'latitude-deg', getprop(_me_.map_path ~ '/latitude-deg'));
                setprop(n ~ 'type', arg[0][0]);
                setprop(n ~ 'query', arg[0][1]);
                setprop(n ~ 'order-by-range', 1);
                setprop(n ~ 'exact', 1);
                setprop(n ~ '../command', 'search');
                setprop(n ~ 'index', -1);
                setprop(n ~ '../command', command);
                unlockSearches();
                msg(action ~ arg[0][0] ~ ' ' ~ arg[0][1]);
            },
            [ c[i][4], c[i][2] ]
        );
    }
    c = nil;
    _me_.mud.open(1); # allow wrapping
    _me_.mud.select(); # select the first entry
    _me_.mud.select(1); # select HERE;
    if (size(_me_.mud.content) > 2) _me_.mud.select(1); # select nearest navaid if available
    _me_.largeFMSknob = func (dir) { _me_.mud.select(dir) };
}

showSearchResults = func (mud, title, freq, callback) {
    var scratch = '/instrumentation/gps/scratch/';
    mud.wipe();
    mud.setTitle(title);
    while (getprop(scratch ~ 'has-next')) {
        mud.add([getprop(scratch ~ 'name'),
                 sprintf('%s -- DST %.1fNM BRG %03i', 
                    getprop(scratch ~ 'ident'),
                    getprop(scratch ~ 'distance-nm'), 
                    getprop(scratch ~ 'mag-bearing-deg')
                 )],
                 callback,
                 [ getprop(scratch ~ freq), getprop(scratch ~ 'ident'), getprop(scratch ~ 'type') ]);
        setprop('/instrumentation/gps/command', 'next');
    }
    mud.open();
    mud.select();
}

var selectSearchType = func (mud, dir) {
    lockSearches();
    var title = mud.node.getNode('title').getValue();
    if (title == 'NEAREST AIRPORTS') {
        searchNearestNavaid('vor',     5);
        showSearchResults(mud, 'NEAREST VOR', 'frequency-mhz',
                func { msg('set NAV1 standby freq to: ' ~ arg[0][0] ~ 'MHz');
                    setprop('/instrumentation/nav/frequencies/standby-mhz', arg[0][0]) 
                }
        );
    }
    elsif (title == 'NEAREST VOR') {
        searchNearestNavaid('ndb',     5);
        showSearchResults(mud, 'NEAREST NDB', 'frequency-khz', 
                func { msg('set ADF standby freq to ' ~ arg[0][0] ~ 'kHz');
                    setprop('/instrumentation/adf/frequencies/standby-khz', arg[0][0]) 
                }
        );
    }
    elsif (title == 'NEAREST NDB') {
        searchNearestNavaid('airport', 10);
        showSearchResults(mud, 'NEAREST AIRPORTS', 'mag-bearing-deg', 
                func { msg('set HDG to ' ~ arg[0][0]); 
                    setprop('/instrumentation/zkv1000/afcs/heading-bug-deg', arg[0][0]) 
                }
        );
    }
    unlockSearches();
}

var showAvailableFPL = func (_me_) {
    var files = [];
    var mud = _me_.mud;
    foreach (var subdir; ['/zkv1000/routes/', '/Routes/']) { # compatibility with zkv500 route store path
        var path = getprop('/sim/fg-home') ~ subdir;
        var s = io.stat(path);
        (s != nil and s[11] == 'dir') or continue;
        foreach (var f; directory(path)) {
            if (f[0] != `.` and substr(f, -4) == '.xml') append(files, path ~ f);
        }
    }
    if (size(files) == 0) {
        msg('unable to find a directory containing routes');
        return;
    }
    files = sort(files, func (a,b) { a[0] - b[0] });
    mud.wipe();
    mud.setTitle('STORED FLIGHTPLANS');
    mud.add(['CANCEL'], func { mud.close(mud) });
    for (var i = 0; i < size(files); i += 1) {
        mud.add([string.uc(split('.', split('/', files[i])[-1])[0])], loadFPLfromFile, files[i]);
    }
    mud.open(1); # allow wrapping
    mud.select(); # select CANCEL
    if (size(mud.content) > 1) mud.select(1); # select first entry if exists
    _me_.largeFMSknob = func (dir) { mud.select(dir) }
}