Debian Codegraph @

Nasal/local_weather/compat_layer.nas

raw
1
2 ########################################################
3 # compatibility layer for local weather package
4 # Thorsten Renk, March 2011
5 ########################################################
6
7 # function purpose
8 #
9 # setDefaultCloudsOff to remove the standard Flightgear 3d clouds
10 # setVisibility to set the visibility to a given value
11 # setLift to set lift to given value
12 # setRain to set rain to a given value
13 # setSnow to set snow to a given value
14 # setTurbulence to set turbulence to a given value
15 # setTemperature to set temperature to a given value
16 # setPressure to set pressure to a given value
17 # setDewpoint to set the dewpoint to a given value
18 # setLight to set light saturation to given value
19 # setWind to set wind
20 # setWindSmoothly to set the wind gradually across a second
21 # smooth_wind_loop (helper function for setWindSmoothly)
22 # create_cloud to place a single cloud into the scenery
23 # create_impostor to place an impostor sheet mimicking far clouds into the scene
24 # create_cloud_array to place clouds from storage arrays into the scenery
25 # get_elevation to get the terrain elevation at given coordinates
26 # get_elevation_vector to get terrain elevation at given coordinate vector
27 # set_wxradarecho_storm to provide info about a storm to the wxradar
28
29
30 # This file contains portability wrappers for the local weather system:
31 # http://wiki.flightgear.org/index.php/A_local_weather_system
32 #
33 # This module is intended to provide a certain degree of backward compatibility for past
34 # FlightGear releases, while sketching out the low level APIs used and required by the
35 # local weather system, as these
36 # are being added to FlightGear.
37 #
38 # This file contains various workarounds for doing things that are currently not yet directly
39 # supported by the core FlightGear/Nasal APIs (fgfs 2.0).
40 #
41 # Some of these workarounds are purely implemented in Nasal space, and may thus not provide sufficient
42 # performance in some situations.
43 #
44 # The goal is to move all such workarounds eventually into this module, so that the high level weather modules
45 # only refer to this "compatibility layer" (using an "ideal API"), while this module handles
46 # implementation details
47 # and differences among different versions of FlightGear, so that key APIs can be ported to C++ space
48 # for the sake
49 # of improving runtime performance and efficiency.
50 #
51 # This provides an abstraction layer that isolates the rest of the local weather system from low
52 # level implementation details.
53 #
54 # C++ developers who want to help improve the local weather system (or the FlightGear/Nasal
55 # interface in general) should
56 # check out this file (as well as the wiki page) for APIs or features that shall eventually be
57 # re/implemented in C++ space for
58 # improving the local weather system.
59 #
60 #
61 # This module provides a handful of helpers for dynamically querying the Nasal API of the running fgfs binary,
62 # so that it can make use of new APIs (where available), while still working with older fgfs versions.
63 #
64 # Note: The point of these helpers is that they should really only be used
65 # by this module, and not in other parts/files of the
66 # local weather system. Any hard coded special cases should be moved into this module.
67 #
68 # The compatibility layer is currently work in progress and will be extended as new Nasal
69 # APIs are being added to FlightGear.
70
71 var weather_dynamics = nil;
72 var weather_tile_management = nil;
73 var compat_layer = nil;
74 var weather_tiles = nil;
75
76
77 _setlistener("/nasal/local_weather/loaded", func {
78
79 compat_layer = local_weather;
80 weather_dynamics = local_weather;
81 weather_tile_management = local_weather;
82 weather_tiles = local_weather;
83
84
85 var result = "yes";
86
87 if (1==0) # no compatibility tests for 2.4 binary, it has the required features
88 {
89 print("Compatibility layer: testing for hard coded support");
90
91 if (props.globals.getNode("/rendering/scene/saturation", 0) == nil)
92 {result = "no"; features.can_set_light = 0;}
93 else
94 {result = "yes"; features.can_set_light = 1;}
95 print("* can set light saturation: "~result);
96
97
98 if (props.globals.getNode("/rendering/scene/scattering", 0) == nil)
99 {result = "no"; features.can_set_scattering = 0;}
100 else
101 {result = "yes"; features.can_set_scattering = 1;}
102 print("* can set horizon scattering: "~result);
103
104 if (props.globals.getNode("/environment/terrain", 0) == nil)
105 {result = "no"; features.terrain_presampling = 0;}
106 else
107 {result = "yes"; features.terrain_presampling = 1;setprop("/environment/terrain/area[0]/enabled",1);}
108 print("* hard coded terrain presampling: "~result);
109
110 if ((props.globals.getNode("/environment/terrain/area[0]/enabled",1).getBoolValue() == 1) and (features.terrain_presampling ==1))
111 {result = "yes"; features.terrain_presampling_active = 1;}
112 else
113 {result = "no"; features.terrain_presampling_active = 0;}
114 print("* terrain presampling initialized: "~result);
115
116
117 if (props.globals.getNode("/environment/config/enabled", 0) == nil)
118 {result = "no"; features.can_disable_environment = 0;}
119 else
120 {result = "yes"; features.can_disable_environment = 1;}
121 print("* can disable global weather: "~result);
122
123
124 print("Compatibility layer: tests done.");
125 }
126
127
128 # features of a 2.4 binary
129
130 # switch terrainsampler to active, should be initialized
131
132
133 features.can_set_light = 1;
134 features.can_set_scattering = 1;
135 features.terrain_presampling = 1;
136 features.terrain_presampling_active = 1;
137 features.can_disable_environment = 1;
138
139
140
141 # features of a current GIT binary
142
143 features.fast_geodinfo = 1;
144
145
146 # do actual startup()
147 local_weather.updateMenu();
148 local_weather.startup();
149
150 });
151
152
153
154
155
156 var setDefaultCloudsOff = func {
157
158 var layers = props.globals.getNode("/environment/clouds").getChildren("layer");
159
160 foreach (var l; layers)
161 {
162 l.getNode("coverage-type").setValue(5);
163 }
164
165
166
167 # we store that information ourselves, so this should be zero, but rain forces us to go for an offset
168 setprop("/environment/clouds/layer[0]/elevation-ft",0.0);
169
170 # layer wrapping off
171 setprop("/sim/rendering/clouds3d-wrap",0);
172
173 # rain altitude limit off, detailed precipitation control on
174
175 props.globals.getNode("/environment/params/use-external-precipitation-level").setBoolValue("true");
176 props.globals.getNode("/environment/precipitation-control/detailed-precipitation").setBoolValue("true");
177
178
179 # set skydome unloading off
180
181 setprop("/sim/rendering/minimum-sky-visibility", 0.0);
182
183 # just to be sure, set other parameters off
184
185 compat_layer.setRain(0.0);
186 compat_layer.setSnow(0.0);
187 compat_layer.setLight(1.0);
188
189 }
190
191
192 ####################################
193 # set visibility to given value
194 ####################################
195
196 var setVisibility = func (vis) {
197
198 setprop("/environment/visibility-m",vis);
199
200 }
201
202
203 var setVisibilitySmoothly = func (vis) {
204
205
206 visibility_target = vis;
207 visibility_current = getprop("/environment/visibility-m");
208
209 if (smooth_visibility_loop_flag == 0)
210 {
211 smooth_visibility_loop_flag = 1;
212 visibility_loop();
213 }
214 }
215
216 var visibility_loop = func {
217
218 if (local_weather.local_weather_running_flag == 0) {return;}
219
220 if (visibility_target == visibility_current)
221 {smooth_visibility_loop_flag = 0; return;}
222
223 if (visibility_target < visibility_current)
224 {
225 var vis_goal = visibility_target;
226 if (vis_goal < 0.97 * visibility_current) {vis_goal = 0.97 * visibility_current;}
227 }
228 else
229 {
230 var vis_goal = visibility_target;
231 if (vis_goal > 1.03 * visibility_current) {vis_goal = 1.03 * visibility_current;}
232 }
233 # print(vis_goal, " ",local_weather.interpolated_conditions.visibility_m );
234 if (local_weather.interpolated_conditions.visibility_m > vis_goal)
235 {setprop("/environment/visibility-m",vis_goal);}
236 visibility_current = vis_goal;
237
238 settimer( func {visibility_loop(); },0);
239 }
240
241
242 ####################################
243 # set thermal lift to given value
244 ####################################
245
246 var setLift = func (lift) {
247
248 setprop("/environment/local-weather-lift-fps",lift);
249
250 }
251
252 ####################################
253 # set rain properties
254 ####################################
255
256 var setRain = func (rain) {
257
258 setprop("/environment/rain-norm", rain);
259 }
260
261
262 var setRainDropletSize = func (size) {
263
264 setprop("/environment/precipitation-control/rain-droplet-size", size);
265 }
266
267 ####################################
268 # set snow properties
269 ####################################
270
271 var setSnow = func (snow) {
272
273 setprop("/environment/snow-norm", snow);
274 }
275
276 var setSnowFlakeSize = func (size) {
277
278 setprop("/environment/precipitation-control/snow-flake-size", size);
279 }
280
281
282
283 ####################################
284 # set turbulence to given value
285 ####################################
286
287 var setTurbulence = func (turbulence) {
288
289 var turbulence_scale = getprop("/local-weather/config/turbulence-scale");
290
291 setprop("/environment/turbulence/magnitude-norm",turbulence * turbulence_scale);
292 setprop("/environment/turbulence/rate-hz",3.0);
293 }
294
295
296 ####################################
297 # set temperature to given value
298 ####################################
299
300 var setTemperature = func (T) {
301
302 setprop("/environment/temperature-sea-level-degc",T);
303 }
304
305 ####################################
306 # set pressure to given value
307 ####################################
308
309 var setPressure = func (p) {
310
311 setprop("/environment/pressure-sea-level-inhg",p);
312 }
313
314 ####################################
315 # set dewpoint to given value
316 ####################################
317
318 var setDewpoint = func (D) {
319
320 setprop("/environment/dewpoint-sea-level-degc",D);
321 }
322
323 ####################################
324 # set light saturation to given value
325 ####################################
326
327 var setLight = func (s) {
328
329 setprop("/rendering/scene/saturation",s);
330 }
331
332 var setLightSmoothly = func (s) {
333
334 light_target = s;
335 light_current = getprop("/rendering/scene/saturation");
336
337 if (smooth_light_loop_flag == 0)
338 {
339 smooth_light_loop_flag = 1;
340 light_loop();
341 }
342 }
343
344 var light_loop = func {
345
346 if (local_weather.local_weather_running_flag == 0) {return;}
347
348 if (light_target == light_current)
349 {smooth_light_loop_flag = 0; return;}
350
351 if (light_target < light_current)
352 {
353 var light_goal = light_target;
354 if (light_goal < 0.97 * light_current) {light_goal = 0.97 * light_current;}
355 }
356 else
357 {
358 var light_goal = light_target;
359 if (light_goal > 1.03 * light_current) {light_goal = 1.03 * light_current;}
360 }
361
362 setprop("/rendering/scene/saturation",light_goal);
363 light_current = light_goal;
364
365 settimer( func {light_loop(); },0);
366 }
367
368
369 ####################################
370 # set horizon scattering
371 ####################################
372
373 var setScattering = func (s) {
374
375 setprop("/rendering/scene/scattering",s);
376 }
377
378 ####################################
379 # set overcast haze
380 ####################################
381
382 var setOvercast = func (o) {
383
384 setprop("/rendering/scene/overcast",o);
385 }
386
387
388 ####################################
389 # set skydome scattering parameters
390 ####################################
391
392 var setSkydomeShader = func (r, m, d) {
393
394 setprop("/sim/rendering/rayleigh", r);
395 setprop("/sim/rendering/mie", m);
396 setprop("/sim/rendering/dome-density",d);
397 }
398
399 ###########################################################
400 # set wind to given direction and speed
401 ###########################################################
402
403
404 var setWind = func (dir, speed) {
405
406 setprop("/environment/wind-from-heading-deg",dir);
407 setprop("/environment/wind-speed-kt",speed);
408
409
410 # this is needed to trigger the cloud drift to pick up the new wind setting
411 setprop("/environment/clouds/layer[0]/elevation-ft",0.0);
412
413 }
414
415 ###########################################################
416 # set wind smoothly to given direction and speed
417 # interpolating across several frames
418 ###########################################################
419
420 var smoothDirection = func (dir0, dir1, factor) {
421
422 var diff = ( math.mod( dir0 - dir1 + 180 + 360, 360 ) - 180 );
423 diff *= factor;
424
425 return math.mod( 360 + dir1 + ( diff / 2), 360);
426 }
427
428
429 var setWindSmoothly = func (dir, speed) {
430
431 var curDir = getprop("/environment/wind-from-heading-deg");
432 var curSpeed = getprop("/environment/wind-speed-kt");
433
434 dir = math.mod(dir, 360);
435
436 var newSpeed = (curSpeed * 9 + speed) / 10;
437 var newDir = smoothDirection(dir, curDir, 0.2);
438
439 setWind(newDir, newSpeed);
440
441 #setWind(dir, speed);
442 }
443
444
445 ###########################################################
446 # place a single cloud
447 ###########################################################
448
449 var create_cloud = func(path, lat, long, alt, heading) {
450
451 var tile_counter = getprop(lw~"tiles/tile-counter");
452 var buffer_flag = getprop(lw~"config/buffer-flag");
453 var d_max = weather_tile_management.cloud_view_distance + 1000.0;
454
455 # noctilucent clouds should not be deleted with the tile, hence they're assigned to tile zero
456 if (find("noctilucent",path) != -1)
457 {tile_counter=0;}
458
459 # check if we deal with a convective cloud - no need to do this any more, convective clouds go via a different system
460
461 var convective_flag = 0;
462
463 #if (find("cumulus",path) != -1)
464 # {
465 # if ((find("alto",path) != -1) or (find("cirro", path) != -1) or (find("strato", path) != -1))
466 # {convective_flag = 0;}
467 # else if ((find("small",path) != -1) or (find("whisp",path) != -1))
468 # {convective_flag = 1;}
469 # else if (find("bottom",path) != -1)
470 # {convective_flag = 4;}
471 # else
472 # {convective_flag = 2;}
473 #
474 # }
475 #else if (find("congestus",path) != -1)
476 # {
477 # if (find("bottom",path) != -1)
478 # {convective_flag = 5;}
479 # else
480 # {convective_flag = 3;}
481 # }
482
483 #print("path: ", path, " flag: ", convective_flag);
484
485 # first check if the cloud should be stored in the buffer
486 # we keep it if it is in visual range or at high altitude (where visual range is different)
487
488
489
490 # now check if we are writing from the buffer, in this case change tile index
491 # to buffered one
492
493 if (getprop(lw~"tmp/buffer-status") == "placing")
494 {
495 tile_counter = buffered_tile_index;
496 }
497
498
499
500 # if the cloud is not buffered, get property tree nodes and write it
501 # into the scenery
502
503 var n = props.globals.getNode("local-weather/clouds", 1);
504 var c = n.getChild("tile",tile_counter,1);
505
506
507 var cloud_number = n.getNode("placement-index").getValue();
508 for (var i = cloud_number; 1; i += 1)
509 if (c.getChild("cloud", i, 0) == nil)
510 break;
511 var cl = c.getChild("cloud", i, 1);
512 n.getNode("placement-index").setValue(i);
513
514 var placement_index = i;
515
516 var model_number = n.getNode("model-placement-index").getValue();
517 var m = props.globals.getNode("models", 1);
518 for (var i = model_number; 1; i += 1)
519 if (m.getChild("model", i, 0) == nil)
520 break;
521 var model = m.getChild("model", i, 1);
522 n.getNode("model-placement-index").setValue(i);
523
524
525
526 var latN = cl.getNode("position/latitude-deg", 1); latN.setValue(lat);
527 var lonN = cl.getNode("position/longitude-deg", 1); lonN.setValue(long);
528 var altN = cl.getNode("position/altitude-ft", 1); altN.setValue(alt);
529 var hdgN = cl.getNode("orientation/true-heading-deg", 1); hdgN.setValue(heading);
530
531 cl.getNode("tile-index",1).setValue(tile_counter);
532
533 model.getNode("path", 1).setValue(path);
534 model.getNode("latitude-deg", 1).setValue(lat);
535 model.getNode("longitude-deg", 1).setValue(long);
536 model.getNode("elevation-ft", 1).setValue(alt);
537 model.getNode("heading-deg", 1).setValue(local_weather.wind.cloudlayer[0]+180.0);
538 model.getNode("tile-index",1).setValue(tile_counter);
539 model.getNode("speed-kt",1).setValue(local_weather.wind.cloudlayer[1]);
540 model.getNode("load", 1).remove();
541
542
543 #model.getNode("latitude-deg-prop", 1).setValue(latN.getPath());
544 #model.getNode("longitude-deg-prop", 1).setValue(lonN.getPath());
545 #model.getNode("elevation-ft-prop", 1).setValue(altN.getPath());
546 #model.getNode("heading-deg-prop", 1).setValue(hdgN.getPath());
547
548 # sort the cloud into the cloud hash array
549
550 if (buffer_flag == 1)
551 {
552 var cs = weather_tile_management.cloudScenery.new(tile_counter, convective_flag, cl, model);
553 append(weather_tile_management.cloudSceneryArray,cs);
554 }
555
556 # if weather dynamics is on, also create a timestamp property and sort the cloud hash into quadtree
557
558 if (local_weather.dynamics_flag == 1)
559 {
560 cs.timestamp = weather_dynamics.time_lw;
561 cs.write_index = placement_index;
562
563
564 if (getprop(lw~"tmp/buffer-status") == "placing")
565 {
566 var blat = buffered_tile_latitude;
567 var blon = buffered_tile_longitude;
568 var alpha = buffered_tile_alpha;
569 }
570 else
571 {
572 var blat = getprop(lw~"tiles/tmp/latitude-deg");
573 var blon = getprop(lw~"tiles/tmp/longitude-deg");
574 var alpha = getprop(lw~"tmp/tile-orientation-deg");
575 }
576 weather_dynamics.sort_into_quadtree(blat, blon, alpha, lat, long, weather_dynamics.cloudQuadtrees[tile_counter-1], cs);
577 }
578
579 }
580
581
582 ###########################################################
583 # place an impostor sheet
584 ###########################################################
585
586 var create_impostor = func(path, lat, long, alt, heading) {
587
588 var n = props.globals.getNode("local-weather/clouds", 1);
589 var model_number = n.getNode("model-placement-index").getValue();
590 var m = props.globals.getNode("models", 1);
591 for (var i = model_number; 1; i += 1)
592 if (m.getChild("model", i, 0) == nil)
593 break;
594 var model = m.getChild("model", i, 1);
595 n.getNode("model-placement-index").setValue(i);
596
597
598 model.getNode("path", 1).setValue(path);
599 model.getNode("latitude-deg", 1).setValue(lat);
600 model.getNode("longitude-deg", 1).setValue(long);
601 model.getNode("elevation-ft", 1).setValue(alt);
602 model.getNode("heading-deg", 1).setValue(local_weather.wind.cloudlayer[0]+180.0);
603 model.getNode("speed-kt",1).setValue(local_weather.wind.cloudlayer[1]);
604 model.getNode("load", 1).remove();
605
606
607 var imp = weather_tile_management.cloudImpostor.new(model);
608 append(weather_tile_management.cloudImpostorArray,imp);
609
610
611 }
612
613
614 ###########################################################
615 # place a model
616 ###########################################################
617
618 var place_model = func(path, lat, lon, alt, heading, pitch, yaw) {
619
620
621
622 var m = props.globals.getNode("models", 1);
623 for (var i = 0; 1; i += 1)
624 if (m.getChild("model", i, 0) == nil)
625 break;
626 var model = m.getChild("model", i, 1);
627
628
629 model.getNode("path", 1).setValue(path);
630 model.getNode("latitude-deg", 1).setValue(lat);
631 model.getNode("longitude-deg", 1).setValue(lon);
632 model.getNode("elevation-ft", 1).setValue(alt);
633 model.getNode("heading-deg", 1).setValue(heading);
634 model.getNode("pitch-deg", 1).setValue(pitch);
635 model.getNode("roll-deg", 1).setValue(yaw);
636 model.getNode("load", 1).remove();
637
638
639 }
640
641 ###########################################################
642 # place a model with control properties
643 ###########################################################
644
645 var place_model_controlled = func(string, path, lat, lon, alt, heading, pitch, roll) {
646
647
648
649 var m = props.globals.getNode("models", 1);
650 for (var i = 0; 1; i += 1)
651 if (m.getChild("model", i, 0) == nil)
652 break;
653 var model = m.getChild("model", i, 1);
654
655
656 setprop("/local-weather/"~string~"/latitude-deg", lat);
657 setprop("/local-weather/"~string~"/longitude-deg", lon);
658 setprop("/local-weather/"~string~"/elevation-ft", alt);
659 setprop("/local-weather/"~string~"/heading-deg", heading);
660 setprop("/local-weather/"~string~"/pitch-deg", pitch);
661 setprop("/local-weather/"~string~"/roll-deg", roll);
662
663
664
665 var cmodel = props.globals.getNode("/local-weather/"~string, 1);
666 var latN = cmodel.getNode("latitude-deg",1);
667 var lonN = cmodel.getNode("longitude-deg",1);
668 var altN = cmodel.getNode("elevation-ft",1);
669 var headN = cmodel.getNode("heading-deg",1);
670 var pitchN = cmodel.getNode("pitch-deg",1);
671 var rollN = cmodel.getNode("roll-deg",1);
672
673
674
675 model.getNode("path", 1).setValue(path);
676 model.getNode("latitude-deg-prop", 1).setValue(latN.getPath());
677 model.getNode("longitude-deg-prop", 1).setValue(lonN.getPath());
678 model.getNode("elevation-ft-prop", 1).setValue(altN.getPath());
679 model.getNode("heading-deg-prop", 1).setValue(headN.getPath());
680 model.getNode("pitch-deg-prop", 1).setValue(pitchN.getPath());
681 model.getNode("roll-deg-prop", 1).setValue(rollN.getPath());
682 model.getNode("tile-index",1).setValue(0);
683 model.getNode("load", 1).remove();
684
685
686 #return model;
687 }
688
689
690
691
692 ###########################################################
693 # place a single cloud using hard-coded system
694 ###########################################################
695
696 var create_cloud_new = func(c) {
697
698
699
700 var tile_counter = getprop(lw~"tiles/tile-counter");
701 cloud_index = cloud_index + 1;
702
703 c.index = tile_counter;
704 c.cloud_index = cloud_index;
705
706 # light must be such that the top of a cloud cannot be darker than the bottom
707
708 if (c.bottom_shade > c.top_shade) {c.bottom_shade = c.top_shade;}
709 c.middle_shade = c.top_shade;
710
711 # write the actual cloud into the scenery
712
713
714 var p = props.Node.new({ "layer" : 0,
715 "index": cloud_index,
716 "lat-deg": c.lat,
717 "lon-deg": c.lon,
718 "min-sprite-width-m": c.min_width,
719 "max-sprite-width-m": c.max_width,
720 "min-sprite-height-m": c.min_height,
721 "max-sprite-height-m": c.max_height,
722 "num-sprites": c.n_sprites,
723 "min-bottom-lighting-factor": c.bottom_shade,
724 "max-bottom-lighting-factor": c.bottom_shade,
725 "min-middle-lighting-factor": c.middle_shade,
726 "min-top-lighting-factor": c.top_shade,
727 "max-top-lighting-factor": c.top_shade,
728 "alpha-factor": c.alpha_factor,
729 "min-shade-lighting-factor": c.bottom_shade,
730 "texture": c.texture_sheet,
731 "num-textures-x": c.num_tex_x,
732 "num-textures-y": c.num_tex_y,
733 "min-cloud-width-m": c.min_cloud_width,
734 "max-cloud-width-m": c.min_cloud_width,
735 "min-cloud-height-m": c.min_cloud_height + c.min_cloud_height * 0.2 * local_weather.height_bias,
736 "max-cloud-height-m": c.min_cloud_height + c.min_cloud_height * 0.2 * local_weather.height_bias,
737 "z-scale": c.z_scale,
738 "height-map-texture": 0,
739 "alt-ft" : c.alt });
740 fgcommand("add-cloud", p);
741
742 #print("alt: ", c.alt);
743
744 # add other management properties to the hash if dynamics is on
745
746 if (local_weather.dynamics_flag == 1)
747 {
748 c.timestamp = weather_dynamics.time_lw;
749 }
750
751
752 # add cloud to array
753
754 append(weather_tile_management.cloudArray,c);
755
756
757 }
758
759
760
761
762
763
764
765 ###########################################################
766 # place a cloud layer from arrays, split across frames
767 ###########################################################
768
769 var create_cloud_array = func (i, clouds_path, clouds_lat, clouds_lon, clouds_alt, clouds_orientation) {
770
771 if (getprop(lw~"tmp/thread-status") != "placing") {return;}
772 if (getprop(lw~"tmp/convective-status") != "idle") {return;}
773 if ((i < 0) or (i==0))
774 {
775 if (local_weather.debug_output_flag == 1)
776 {print("Cloud placement from array finished!"); }
777
778 # then place all clouds using the new rendering system
779 if (local_weather.hardcoded_clouds_flag == 1)
780 {
781 var s = size(local_weather.cloudAssemblyArray);
782 create_new_cloud_array(s,cloudAssemblyArray);
783 }
784
785 setprop(lw~"tmp/thread-status", "idle");
786
787 # now set flag that tile has been completely processed
788 var dir_index = props.globals.getNode(lw~"tiles/tmp/dir-index").getValue();
789
790 setprop(lw~"tiles/tile["~dir_index~"]/generated-flag",2);
791
792 return;
793 }
794
795
796 var k_max = 30;
797 var s = size(clouds_path);
798
799 if (s < k_max) {k_max = s;}
800
801 for (var k = 0; k < k_max; k = k+1)
802 {
803 if (getprop(lw~"config/dynamics-flag") ==1)
804 {
805 cloud_mean_altitude = local_weather.clouds_mean_alt[s-k-1];
806 cloud_flt = local_weather.clouds_flt[s-k-1];
807 cloud_evolution_timestamp = local_weather.clouds_evolution_timestamp[s-k-1];
808 }
809 create_cloud(clouds_path[s-k-1], clouds_lat[s-k-1], clouds_lon[s-k-1], clouds_alt[s-k-1], clouds_orientation[s-k-1]);
810 #create_cloud_new(clouds_path[s-k-1], clouds_lat[s-k-1], clouds_lon[s-k-1], clouds_alt[s-k-1], clouds_orientation[s-k-1]);
811 }
812
813 setsize(clouds_path,s-k_max);
814 setsize(clouds_lat,s-k_max);
815 setsize(clouds_lon,s-k_max);
816 setsize(clouds_alt,s-k_max);
817 setsize(clouds_orientation,s-k_max);
818
819 if (getprop(lw~"config/dynamics-flag") ==1)
820 {
821 setsize(local_weather.clouds_mean_alt,s-k_max);
822 setsize(local_weather.clouds_flt,s-k_max);
823 setsize(local_weather.clouds_evolution_timestamp,s-k_max);
824 }
825
826 settimer( func {create_cloud_array(i - k, clouds_path, clouds_lat, clouds_lon, clouds_alt, clouds_orientation ) }, 0 );
827 };
828
829
830 var create_new_cloud_array = func (i, cloudArray)
831 {
832
833
834
835
836 if ((i < 0) or (i==0))
837 {
838 if (local_weather.debug_output_flag == 1)
839 {print("Processing add-cloud calls finished!"); }
840 return;
841 }
842
843
844 var k_max = 20;
845 var s = size(cloudArray);
846
847 if (s < k_max) {k_max = s;}
848
849 for (var k = 0; k < k_max; k = k+1)
850 {
851 local_weather.create_cloud_new(cloudArray[s-k-1]);
852 #print(cloudArray[s-k-1].alt);
853 }
854
855 setsize(cloudArray,s-k_max);
856
857
858
859 settimer( func {create_new_cloud_array(i - k, cloudArray) }, 0 );
860 }
861
862
863
864
865
866 ###########################################################
867 # get terrain elevation
868 ###########################################################
869
870 var get_elevation = func (lat, lon) {
871
872 var info = geodinfo(lat, lon);
873 if (info != nil) {var elevation = info[0] * local_weather.m_to_ft;}
874 else {var elevation = -1.0; }
875
876
877 return elevation;
878 }
879
880 ###########################################################
881 # get terrain elevation vector
882 ###########################################################
883
884 var get_elevation_array = func (lat, lon) {
885
886 var elevation = [];
887 var n = size(lat);
888
889
890 for(var i = 0; i < n; i=i+1)
891 {
892 append(elevation, get_elevation(lat[i], lon[i]));
893 }
894
895
896 return elevation;
897 }
898
899 ###########################################################
900 # set the wxradar echo of a storm
901 ###########################################################
902
903 var set_wxradarecho_storm = func (lat, lon, base, top, radius, ref, turb, type) {
904
905 # look for the next free index in the wxradar property tree entries
906
907 var n = props.globals.getNode("/instrumentation/wxradar", 1);
908 for (var i = 0; 1; i += 1)
909 if (n.getChild("storm", i, 0) == nil)
910 break;
911 var s = n.getChild("storm", i, 1);
912
913
914 s.getNode("latitude-deg",1).setValue(lat);
915 s.getNode("longitude-deg",1).setValue(lon);
916 s.getNode("heading-deg",1).setValue(0.0);
917 s.getNode("base-altitude-ft",1).setValue(base);
918 s.getNode("top-altitude-ft",1).setValue(top);
919 s.getNode("radius-nm",1).setValue(radius * m_to_nm);
920 s.getNode("reflectivity-norm",1).setValue(ref);
921 s.getNode("turbulence-norm",1).setValue(turb);
922 s.getNode("type",1).setValue(type);
923 s.getNode("show",1).setValue(1);
924 }
925
926 ###########################################################
927 # remove unused echos
928 ###########################################################
929
930 var remove_wxradar_echos = func {
931
932 var distance_to_remove = 70000.0;
933
934 var storms = props.globals.getNode("/instrumentation/wxradar", 1).getChildren("storm");
935
936 var pos = geo.aircraft_position();
937
938 foreach (s; storms)
939 {
940 var d_sq = local_weather.calc_d_sq(pos.lat(), pos.lon(), s.getNode("latitude-deg").getValue(), s.getNode("longitude-deg").getValue());
941 if (d_sq > distance_to_remove * distance_to_remove)
942 {
943 s.remove();
944 }
945 }
946
947 }
948
949 ############################################################
950 # global variables
951 ############################################################
952
953 # conversions
954
955 var nm_to_m = 1852.00;
956 var m_to_nm = 1.0/nm_to_m;
957
958 # some common abbreviations
959
960 var lw = "/local-weather/";
961 var ec = "/environment/config/";
962
963 # storage arrays for model vector
964
965 var mvec = [];
966 var msize = 0;
967
968 # loop flags and variables
969
970 var smooth_visibility_loop_flag = 0;
971
972 var visibility_target = 0.0;
973 var visibility_current = 0.0;
974
975 var smooth_light_loop_flag = 0;
976
977 var light_target = 0.0;
978 var light_current = 0.0;
979
980 # available hard-coded support
981
982 var features = {};
983
984 # globals to transmit info if clouds are written from buffer
985
986 var buffered_tile_latitude = 0.0;
987 var buffered_tile_longitude = 0.0;
988 var buffered_tile_alpha = 0.0;
989 var buffered_tile_index = 0;
990
991 # globals to handle additional info for Cumulus cloud dynamics
992
993 var cloud_mean_altitude = 0.0;
994 var cloud_flt = 0.0;
995 var cloud_evolution_timestamp = 0.0;
996
997 # globals to handle new cloud indexing
998
999 var cloud_index = 0;