zkv1000 / Nasal / routes.nas /
Sébastien MARQUE commit initial
56c0030 7 years ago
1 contributor
154 lines | 6.38kb
var 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) }
}