zkv1000 / Nasal / map.nas /
Newer Older
128 lines | 4.405kb
commit initial
Sébastien MARQUE authored on 2017-03-07
1
var mapsRelativePath = '';
2
var mapsAbsolutePath = '';
3
var textureExtension = '.png';
4
var actual_map = '';
5
var mapRanges = [0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0];
6

            
7
var init_map = func {
8
    var maps = '/zkv1000/maps';
9
    var root_l = common_l = common_c = 0;
10

            
11
    var home = string.normpath(getprop('/sim/fg-home'));
12
    var home_s = size(home);
13
    
14
    var root = string.normpath(getprop('/sim/fg-root'));
15
    var root_s = size(root);
16
    
17
    for (var i = 0; i < root_s; i += 1) 
18
        if (root[i] == `/`)
19
            root_l += 1;
20
    
21
    for (var i = 0; i < root_s and i < home_s; i += 1) {
22
        (home[i] == root[i]) or break;
23
        common_c += 1;
24
        if (home[i] == `/`) common_l += 1;
25
    }
26
    
27
    mapsRelativePath = '../../../../';
28
    for (var i = 0; i < root_l - common_l; i += 1)
29
        mapsRelativePath ~= '../';
30
    
31
    mapsRelativePath ~= substr(home, common_c, home_s) ~ maps;
32
    mapsAbsolutePath = home ~ maps;
33
}
34

            
35
var moveMap = func (d) {
36
    atlas_map(d);
37
    terrain_map(d);
38
}
39

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

            
43
    var mapnode = '/instrumentation/zkv1000/device[' ~ d ~ ']/map/';
44
    var lat = getprop(mapnode ~ 'latitude-deg');
45
    var lon = getprop(mapnode ~ 'longitude-deg');
46
    var map = sprintf('%s%03i%s%02i',
47
        (lon > 0)? 'e' : 'w',
48
        (lon > 0)? lon : (abs(lon) + 1),
49
        (lat > 0)? 'n' : 's',
50
        (lat > 0)? lat : (abs(lat) + 1)
51
    );
52
    if (actual_map == map) {
53
        setprop(mapnode ~ 'moving-x', (lon > 0)? frac(lon) : 1 - abs(frac(lon)));
54
        setprop(mapnode ~ 'moving-y', (lat > 0)? frac(lat) : 1 - abs(frac(lat)));
55
    }
56
    elsif (io.stat(mapsAbsolutePath ~ '/terrain/' ~ map ~ textureExtension) != nil) {
57
        actual_map = map;
58
        setprop(mapnode ~ 'terrain-path', mapsRelativePath ~ '/terrain/' ~ map ~ textureExtension);
59
        setprop(mapnode ~ 'moving-x', (lon > 0)? frac(lon) : 1 - abs(frac(lon)));
60
        setprop(mapnode ~ 'moving-y', (lat > 0)? frac(lat) : 1 - abs(frac(lat)));
61
    }
62
    else {
63
        actual_map = '';
64
        setprop(mapnode ~ 'terrain-path', '');
65
        setprop(mapnode ~ 'moving-x', 0);
66
        setprop(mapnode ~ 'moving-y', 0);
67
    }
68
    setprop(mapnode ~ 'alt', computeCursorPosition(alt));
69
    setprop(mapnode ~ 'alt-selected', computeCursorPosition(getprop('/instrumentation/zkv1000/afcs/selected-alt-ft')));
70
    setprop(mapnode ~ 'alt-1-min', computeCursorPosition(alt + vs));
71
    setprop(mapnode ~ 'longitude-string', device[d].status == 3 ? 
72
            sprintf('%04.2f LON', lon) : DMS(lon, lon > 0 ? 'E' : 'W'));
73
    setprop(mapnode ~ 'latitude-string', device[d].status == 3 ? 
74
            sprintf('%04.2f LAT', lat) : DMS(lat, lat > 0 ? 'N' : 'S'));
75
}
76

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

            
82
var terrain_map = func (d) {
83
# inspired from Ryan M's generic moving map
84
    if (device[d].status != 4) return;
85
    var path = '/instrumentation/zkv1000/device[' ~ d ~ ']/map/terrain-elevation/';
86
    var offset = 0;
87
    if (terrain_map_row < 15)
88
        terrain_map_row += 1;
89
    else {
90
        terrain_map_pos = geo.aircraft_position();
91
        terrain_map_hdg = hdg;
92
        terrain_map_alt = alt;
93
        terrain_map_row = 0;
94
    }
95
    terrain_map_pos.apply_course_distance(terrain_map_hdg - 90, range / 2 * 1852);
96
    for (var col = 0; col < 16; col += 1) {
97
        var info = geodinfo(terrain_map_pos.lat(), terrain_map_pos.lon());
98
        if (info != nil) {
99
            var d = terrain_map_alt - info[0] * 3.28;
100
            if (d > 1000) {
101
                offset = 0 ##black
102
            }
103
            elsif (d > -100) {
104
                offset = 0.25 ##yellow
105
            }
106
            else {
107
                offset = 1.5 ##red
108
            }
109
        }
110
        else {
111
            offset = 0.75 ##no available data
112
        }
113
        setprop(path ~ terrain_map_row ~ '-' ~ col, offset);
114
        terrain_map_pos.apply_course_distance(terrain_map_hdg + 90, range / 16 * 1852);
115
    }
116
    terrain_map_pos.apply_course_distance(terrain_map_hdg, range / 16 * 1852);
117
}
118

            
119
var computeCursorPosition = func (v) {
120
    var ref = round_bis(v, (v < 3281)? 820 : 1640);
121
    if (ref < 4921) ref /= 820;
122
    elsif (ref > 21325) ref = 15;
123
    else {
124
        ref /= 1640;
125
        ref += 2;
126
    }
127
    return ref;
128
}