1
0

map.nas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. print("*** LOADING MAP.nas ... ***");
  2. var zoom = 10;
  3. var width = 768;
  4. var height = 576;
  5. #------------------------------------------------------------------------------- thanks to Harbal1
  6. #===============================================================================
  7. # FUNCTIONS
  8. #-------------------------------------------------------------------------------
  9. # draw_arc
  10. # this function draws an arc
  11. # params :
  12. # - element : canvas object created by createChild()
  13. # - center_x : coord x of the center of the arc in px
  14. # - center_y : coord y of the center of the arc in px
  15. # - radius : radius
  16. # - start_angle : start angle in deg ()
  17. # - end_angle : end angle in deg ()
  18. # - color : color
  19. # - line_width : line_width
  20. #
  21. var draw_arc = func(element, center_x, center_y, radius, start_angle, end_angle, color, line_width)
  22. {
  23. var coord_start_x = center_x + (radius * math.cos(start_angle * D2R));
  24. var coord_start_y = center_y - (radius * math.sin(start_angle * D2R));
  25. var to_x = -(radius * math.cos(start_angle * D2R)) + (radius * math.cos(end_angle * D2R));
  26. var to_y = (radius * math.sin(start_angle * D2R)) - (radius * math.sin(end_angle * D2R));
  27. element.setStrokeLineWidth(line_width)
  28. .set("stroke", color)
  29. .moveTo(coord_start_x, coord_start_y)
  30. .arcSmallCCW(radius, radius, 0, to_x, to_y);
  31. print("coord_start_x:"~coord_start_x~"| coord_start_y:"~coord_start_y~"| radius:"~ radius ~"| to_x:"~to_x~"| to_y:"~ to_y);
  32. }
  33. #-------------------------------------------------------------------------------
  34. # draw_piste
  35. # this function creates a piste
  36. #
  37. # params :
  38. # - element : canvas object created by createChild()
  39. #
  40. var draw_piste = func(element)
  41. {
  42. element.setStrokeLineWidth(5)
  43. .set("stroke", "rgba(40, 240, 40, 1)")
  44. .moveTo(-13, 13)
  45. .lineTo(-13, -13)
  46. .moveTo(13, 13)
  47. .lineTo(13, -13);
  48. # creation du vecteur vitesse+cap de la cible
  49. element.setStrokeLineWidth(4)
  50. .set("stroke", "rgba(40, 240, 40, 1)")
  51. .moveTo(0, 0)
  52. .lineTo(0, 0);
  53. element.setStrokeLineWidth(4)
  54. .set("stroke", "rgba(40, 240, 40, 1)")
  55. .moveTo(-12, 11)
  56. .lineTo(12, 11)
  57. .moveTo(-12, -11)
  58. .lineTo(12, -11);
  59. }
  60. #-------------------------------------------------------------------------------
  61. # update_piste
  62. # this function updates piste - length of vector = distance in 15s
  63. #
  64. # params :
  65. # - element : canvas object created by createChild()
  66. # - FIXME ...
  67. #
  68. var update_piste = func(element, my_heading, my_alt, target_heading, target_alt, target_speed, pixel_range, radar_range)
  69. {
  70. var vector_x = ((target_speed * pixel_range / radar_range) / 240) * math.sin((target_heading - my_heading) * D2R);
  71. var vector_y = ((target_speed * pixel_range / radar_range) / 240) * math.cos((target_heading - my_heading) * D2R);
  72. if((target_alt - 1000 ) < my_alt)
  73. {
  74. element._node.getNode("coord[12]", 1).setValue(-12);
  75. }
  76. else
  77. {
  78. element._node.getNode("coord[12]", 1).setValue(12);
  79. }
  80. if((target_alt + 1000 ) > my_alt)
  81. {
  82. element._node.getNode("coord[16]", 1).setValue(-12);
  83. }
  84. else
  85. {
  86. element._node.getNode("coord[16]", 1).setValue(12);
  87. }
  88. element._node.getNode("coord[8]", 1).setValue(vector_x);
  89. element._node.getNode("coord[9]", 1).setValue(-vector_y);
  90. }
  91. #-------------------------------------------------------------------------------
  92. # update_piste_fl
  93. # this function updates flight level piste label position
  94. #
  95. # params :
  96. # - element : canvas object created by createChild()
  97. # - FIXME ...
  98. #
  99. var update_piste_fl = func(element, my_heading, target_heading)
  100. {
  101. var vector_x = -30 * math.sin((target_heading - my_heading) * D2R);
  102. var vector_y = 30 * math.cos((target_heading - my_heading) * D2R);
  103. element.setTranslation(vector_x, vector_y);
  104. }
  105. var MAPCanvas = {
  106. canvas_settings: {
  107. "name": "PFD-Test", # The name is optional but allow for easier identification
  108. "size": [1024, 1024], # Size of the underlying texture (should be a power of 2, required) [Resolution]
  109. "view": [width, height], # Virtual resolution (Defines the coordinate system of the canvas [Dimensions]
  110. # which will be stretched the size of the texture, required)
  111. "mipmapping": 1 # Enable mipmapping (optional)
  112. },
  113. new: func(placement)
  114. {
  115. var m = {
  116. parents: [MAPCanvas],
  117. canvas: canvas.new(MAPCanvas.canvas_settings)
  118. };
  119. ## Base for the canvas
  120. m.canvas.addPlacement(placement);
  121. m.root = m.canvas.createGroup();
  122. m.mapStuff = m.root.createChild("group");
  123. m.radarStuff = m.root.createChild("group"); #Should be replaced by rwr
  124. #MAP stuff
  125. m.g_front = m.mapStuff.createChild("group");
  126. m.g_back = m.mapStuff.createChild("group");
  127. #Aircraft orientation/position stuff
  128. m.myHeadingProp = props.globals.getNode("orientation/heading-deg");
  129. m.myCoord = geo.aircraft_position();
  130. #Center of the canvas
  131. m.root.setCenter(384,256);
  132. ##MAP stuff : Set up of the tiles
  133. m.tile_size = 256;
  134. m.num_tiles = [4, 3];
  135. m.type = "map";
  136. m.home = props.globals.getNode("/sim/fg-home");
  137. m.maps_base = m.home.getValue() ~ '/cache/maps';
  138. #---------------- Make the url where to take the tiles ------------
  139. #Some alternative can exist
  140. # http://otile1.mqcdn.com/tiles/1.0.0/map
  141. # http://otile1.mqcdn.com/tiles/1.0.0/sat
  142. # (also see http://wiki.openstreetmap.org/wiki/Tile_usage_policy)
  143. #var makeUrl = string.compileTemplate('http://{server}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg');
  144. #var servers = ["otile1", "otile2", "otile3", "otile4"];
  145. m.makeUrl = string.compileTemplate('http://{server}.tile.osm.org/{z}/{x}/{y}.png');
  146. m.servers = ["a", "b", "c"];
  147. m.makePath = string.compileTemplate(m.maps_base ~ '/osm-{type}/{z}/{x}/{y}.png');
  148. #Setting up red little aircraft
  149. m.center_tile_offset = [
  150. (m.num_tiles[0] - 1) / 2,
  151. (m.num_tiles[1] - 1) / 2
  152. ];
  153. # simple aircraft icon at current position/center of the map
  154. m.filename = "Nasal/littleaircraftRed.svg";
  155. m.svg_symbol = m.root.createChild("group");
  156. canvas.parsesvg(m.svg_symbol, m.filename);
  157. m.svg_symbol.setScale(0.05);
  158. m.svg_symbol.setTranslation((width/2)-20,height/2-45);
  159. m.myVector = m.svg_symbol.getBoundingBox();
  160. #svg_symbol.setCenter(width/2,height/2);
  161. m.svg_symbol.updateCenter();
  162. m.svg_symbol.set("z-index", 1);
  163. var make_tiles = func (canvas_group) {
  164. var tiles = setsize([], m.num_tiles[0]);
  165. for (var x = 0; x < m.num_tiles[0]; x += 1) {
  166. tiles[x] = setsize([], m.num_tiles[1]);
  167. for (var y = 0; y < m.num_tiles[1]; y += 1) {
  168. tiles[x][y] = canvas_group.createChild("image", "map-tile");
  169. }
  170. }
  171. return tiles;
  172. }
  173. #MAP Stuff
  174. m.tiles_front = make_tiles(m.g_front);
  175. m.tiles_back = make_tiles(m.g_back);
  176. m.use_front = 1;
  177. m.last_tile = [-1,-1];
  178. m.last_type = m.type;
  179. ##ETC all needed for MAP and RWR canvas
  180. m.zoom = 10;
  181. m.update_timer = nil;
  182. ## RADAR STUFF ##
  183. m.MapToggle = 1;
  184. ## creation des arcs "range"
  185. #m.arc_range1 = m.radarStuff.createChild("path", "arc_range1");
  186. ##m.arc_range1.moveTo(334,256).arcSmallCCW(50, 50, 0, 434, 256);
  187. #m.arc_range1.setStrokeLineWidth(3)
  188. #.moveTo(484, 256)
  189. #.set("stroke", "rgba(100, 100, 100, 1)")
  190. #.arcSmallCCW(100, 100, 0, -200, 0)
  191. #.arcSmallCCW(100, 100, 0, 200, 0);
  192. # draw_arc(m.arc_range1, 384,256 , 100 , 0, 180, "rgba(100, 100, 100, 1)", 3);
  193. return m;
  194. },
  195. changeZoomMap: func(d) {
  196. new_zoom = math.max(2, math.min(15, me.zoom + d));
  197. if (new_zoom != me.zoom) {
  198. me.zoom = new_zoom;
  199. #debug.dump(zoom);
  200. #updateTiles();
  201. }
  202. },
  203. updateTiles: func() {
  204. #print("updateTiles is working");
  205. me.svg_symbol.setRotation(me.myHeadingProp.getValue()*D2R);
  206. #g.setRotation(myHeadingProp.getValue()*D2R);
  207. me.myCoord = geo.aircraft_position();
  208. lat = me.myCoord.lat();
  209. lon = me.myCoord.lon();
  210. var n = math.pow(2, me.zoom);
  211. var offset = [
  212. n * ((lon + 180) / 360) - me.center_tile_offset[0],
  213. (1 - math.ln(math.tan(lat * math.pi/180) + 1 / math.cos(lat * math.pi/180)) / math.pi) / 2 * n - me.center_tile_offset[1]
  214. ];
  215. var tile_index = [int(offset[0]), int(offset[1])];
  216. var ox = tile_index[0] - offset[0];
  217. var oy = tile_index[1] - offset[1];
  218. me.g_front.setVisible(me.use_front);
  219. me.g_back.setVisible(!me.use_front);
  220. me.use_front = math.mod(me.use_front + 1, 2);
  221. for (var x = 0; x < me.num_tiles[0]; x += 1) {
  222. for (var y = 0; y < me.num_tiles[1]; y += 1) {
  223. if (me.use_front) {
  224. me.tiles_back[x][y].setTranslation(int((ox + x) * me.tile_size + 0.5), int((oy + y) * me.tile_size + 0.5));
  225. #debug.dump("updating back");
  226. }
  227. else {
  228. me.tiles_front[x][y].setTranslation(int((ox + x) * me.tile_size + 0.5), int((oy + y) * me.tile_size + 0.5));
  229. #debug.dump("updating front");
  230. }
  231. }
  232. }
  233. if (tile_index[0] != me.last_tile[0] or tile_index[1] != me.last_tile[1] or me.type != me.last_type) {
  234. for (var x = 0; x < me.num_tiles[0]; x += 1) {
  235. for (var y = 0; y < me.num_tiles[1]; y += 1) {
  236. var server_index = math.round(rand() * (size(me.servers) - 1));
  237. var server_name = me.servers[server_index];
  238. var pos = {
  239. z: me.zoom,
  240. x: int(offset[0] + x),
  241. y: int(offset[1] + y),
  242. type: me.type,
  243. server: server_name
  244. };
  245. (func {
  246. var img_path = me.makePath(pos);
  247. if (io.stat(img_path) == nil) {
  248. var img_url = me.makeUrl(pos);
  249. var message = "Requesting %s...";
  250. #printf(message, img_url);
  251. http.save(img_url, img_path)
  252. .done(func {
  253. var message = "Received image %s";
  254. #printf(message, img_path);
  255. # if (pos.z == zoom) {
  256. # tile.setFile(img_path);
  257. # }
  258. })
  259. .fail(func (r) {
  260. var message = "Failed to get image %s %s: %s";
  261. #printf(message, img_path, r.status, r.reason);
  262. me.tiles_back[x-1][y-1].setFile("");
  263. me.tiles_front[x-1][y-1].setFile("");
  264. });
  265. }
  266. else {
  267. if (pos.z == me.zoom) {
  268. var message = "Loading %s";
  269. #printf(message, img_path);
  270. me.tiles_back[x][y].setFile(img_path);
  271. me.tiles_front[x][y].setFile(img_path);
  272. }
  273. }
  274. })();
  275. }
  276. }
  277. me.last_tile = tile_index;
  278. me.last_type = me.type;
  279. }
  280. },
  281. updateRadar:func(){
  282. #Rotating aircraft to the front.
  283. me.svg_symbol.setRotation(0*D2R);
  284. },
  285. changeMfD_Displaying:func(){
  286. #Temporary function : we change the displaying called : mirage2000.changeMfD_Displaying()
  287. if(me.MapToggle){
  288. me.mapStuff.hide();
  289. me.radarStuff.show();
  290. me.MapToggle = 0;
  291. }else{
  292. me.mapStuff.show();
  293. me.radarStuff.hide();
  294. me.MapToggle = 1;
  295. }
  296. },
  297. update: func()
  298. {
  299. #Whatever need to be updated
  300. var update_timer = maketimer(0, func(){
  301. #print("Hello World");
  302. if(me.MapToggle){
  303. me.updateTiles();
  304. }else{
  305. me.updateRadar()
  306. }
  307. });
  308. update_timer.start();
  309. },
  310. #Other function like zoom in/out changing tile index, etc
  311. };
  312. var MapCanvas = bae125.MAPCanvas.new({"node": "canvasCadre", "texture": "canvasTex.png"});
  313. MapCanvas.update();