zkv1000 / Nasal / map.nas /
Sébastien MARQUE commit initial
56c0030 7 years ago
1 contributor
128 lines | 4.405kb
var mapsRelativePath = '';
var mapsAbsolutePath = '';
var textureExtension = '.png';
var actual_map = '';
var mapRanges = [0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0];

var init_map = func {
    var maps = '/zkv1000/maps';
    var root_l = common_l = common_c = 0;

    var home = string.normpath(getprop('/sim/fg-home'));
    var home_s = size(home);
    
    var root = string.normpath(getprop('/sim/fg-root'));
    var root_s = size(root);
    
    for (var i = 0; i < root_s; i += 1) 
        if (root[i] == `/`)
            root_l += 1;
    
    for (var i = 0; i < root_s and i < home_s; i += 1) {
        (home[i] == root[i]) or break;
        common_c += 1;
        if (home[i] == `/`) common_l += 1;
    }
    
    mapsRelativePath = '../../../../';
    for (var i = 0; i < root_l - common_l; i += 1)
        mapsRelativePath ~= '../';
    
    mapsRelativePath ~= substr(home, common_c, home_s) ~ maps;
    mapsAbsolutePath = home ~ maps;
}

var moveMap = func (d) {
    atlas_map(d);
    terrain_map(d);
}

var atlas_map = func (d) {
    if (device[d].status != 2 and device[d].status != 3) return;

    var mapnode = '/instrumentation/zkv1000/device[' ~ d ~ ']/map/';
    var lat = getprop(mapnode ~ 'latitude-deg');
    var lon = getprop(mapnode ~ 'longitude-deg');
    var map = sprintf('%s%03i%s%02i',
        (lon > 0)? 'e' : 'w',
        (lon > 0)? lon : (abs(lon) + 1),
        (lat > 0)? 'n' : 's',
        (lat > 0)? lat : (abs(lat) + 1)
    );
    if (actual_map == map) {
        setprop(mapnode ~ 'moving-x', (lon > 0)? frac(lon) : 1 - abs(frac(lon)));
        setprop(mapnode ~ 'moving-y', (lat > 0)? frac(lat) : 1 - abs(frac(lat)));
    }
    elsif (io.stat(mapsAbsolutePath ~ '/terrain/' ~ map ~ textureExtension) != nil) {
        actual_map = map;
        setprop(mapnode ~ 'terrain-path', mapsRelativePath ~ '/terrain/' ~ map ~ textureExtension);
        setprop(mapnode ~ 'moving-x', (lon > 0)? frac(lon) : 1 - abs(frac(lon)));
        setprop(mapnode ~ 'moving-y', (lat > 0)? frac(lat) : 1 - abs(frac(lat)));
    }
    else {
        actual_map = '';
        setprop(mapnode ~ 'terrain-path', '');
        setprop(mapnode ~ 'moving-x', 0);
        setprop(mapnode ~ 'moving-y', 0);
    }
    setprop(mapnode ~ 'alt', computeCursorPosition(alt));
    setprop(mapnode ~ 'alt-selected', computeCursorPosition(getprop('/instrumentation/zkv1000/afcs/selected-alt-ft')));
    setprop(mapnode ~ 'alt-1-min', computeCursorPosition(alt + vs));
    setprop(mapnode ~ 'longitude-string', device[d].status == 3 ? 
            sprintf('%04.2f LON', lon) : DMS(lon, lon > 0 ? 'E' : 'W'));
    setprop(mapnode ~ 'latitude-string', device[d].status == 3 ? 
            sprintf('%04.2f LAT', lat) : DMS(lat, lat > 0 ? 'N' : 'S'));
}

var terrain_map_pos = nil;
var terrain_map_row = 16; # number of rows for terrain radar, equals to number of colums
var terrain_map_hdg = 0;
var terrain_map_alt = 0;

var terrain_map = func (d) {
# inspired from Ryan M's generic moving map
    if (device[d].status != 4) return;
    var path = '/instrumentation/zkv1000/device[' ~ d ~ ']/map/terrain-elevation/';
    var offset = 0;
    if (terrain_map_row < 15)
        terrain_map_row += 1;
    else {
        terrain_map_pos = geo.aircraft_position();
        terrain_map_hdg = hdg;
        terrain_map_alt = alt;
        terrain_map_row = 0;
    }
    terrain_map_pos.apply_course_distance(terrain_map_hdg - 90, range / 2 * 1852);
    for (var col = 0; col < 16; col += 1) {
        var info = geodinfo(terrain_map_pos.lat(), terrain_map_pos.lon());
        if (info != nil) {
            var d = terrain_map_alt - info[0] * 3.28;
            if (d > 1000) {
                offset = 0 ##black
            }
            elsif (d > -100) {
                offset = 0.25 ##yellow
            }
            else {
                offset = 1.5 ##red
            }
        }
        else {
            offset = 0.75 ##no available data
        }
        setprop(path ~ terrain_map_row ~ '-' ~ col, offset);
        terrain_map_pos.apply_course_distance(terrain_map_hdg + 90, range / 16 * 1852);
    }
    terrain_map_pos.apply_course_distance(terrain_map_hdg, range / 16 * 1852);
}

var computeCursorPosition = func (v) {
    var ref = round_bis(v, (v < 3281)? 820 : 1640);
    if (ref < 4921) ref /= 820;
    elsif (ref > 21325) ref = 15;
    else {
        ref /= 1640;
        ref += 2;
    }
    return ref;
}