Board index FlightGear Development AI Traffic

Determine"Altitude" of various Fresh Water Lakes?  Topic is solved

Intelligent, computer controlled vehicles that drive/fly over the planet!

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Wed Feb 07, 2018 4:57 pm

wlbragg wrote in Wed Feb 07, 2018 3:48 pm:Something like this


Fantastic! Thank you so much for your gracious assistance, wlbragg :D

I've created the nasal script as you've suggested and, I've modified my xml file to include the variable "terrain_elev" in place of the previously-hard coded 'altitude' value. It's painfully obvious that I'm still missing something because the only time that I was able to see the ship was when I put my UFO into "submarine mode" and dove to 0' Sea Level. I'm certain that I need something else to indicate that "terrain_elev" is called by the nasal file. Could I push my luck and ask you how this must be done? And, one more thing (yeah, I'm really pushing my luck now...): How did you display the script log on your screen? This would be great for trying to muddle my way through a de-bugging case.

Here is my xml file:
Code: Select all
<?xml version="1.0"?>
<PropertyList>
<!-- enhansed Braunschweig-class corvette AI ship V1.0 -->
 <scenario>
  <name>FGS_Braunshweig_hevii</name>
  <description>
           Places a Braunschweig-class corvette (K130) environment -
   
         Notes:
   
         
         TACAN-channel-ID   Defines a TACAN channel for display
         purposes only
  </description>

   <entry>
      <type>carrier</type>
      <name>FGS_Braunshweig</name>
      <pennant-number>F260</pennant-number>
      <model>Models/Geometry/K130/F260.xml</model>
      <TACAN-channel-ID>026x</TACAN-channel-ID>
      
      <!-- Edit the following for initial start position, heading  + speed -->
      
      
      <!-- Adriatic Sea> -->
      <!--
         <latitude>45.066</latitude>
         <longitude>-12.980</longitude>
         <speed>10</speed>
         <heading>125</heading>
       -->   
      <!-- Bodensee -->
      
         <latitude>47.5149611</latitude>
         <longitude>9.6563101</longitude>
         <altitude>terrain_elev</altitude>
         <speed>10</speed>
         <heading>290</heading>
      
      <!--Lake Ontario -->
      <!--
         <latitude>44.181</latitude>
          <longitude>-76.566</longitude>
         <altitude>156</altitude>
         <speed>10</speed>
         <heading>200</heading>
      -->   
         
      <!-- St Lawrence Seaway (Québec) -->
      <!--
         <latitude>47.187</latitude>
         <longitude>-70.434</longitude>
         <altitude>17</altitude>
         <speed>10</speed>
         <heading>28</heading>
       -->   
      
      
      <parking-pos>
            <name>Heli1</name>
            <heading-offset-deg>0</heading-offset-deg>
            <x-offset-m>90.0</x-offset-m>
            <y-offset-m>0</y-offset-m>
            <z-offset-m>4</z-offset-m>
         </parking-pos>-->
   </entry>

 </scenario>

</PropertyList>

And this is my copy of your nasal code (saved as live_surface_elev.nas):
Code: Select all
setlistener("/sim/signals/fdm-initialized", func {
  #this needs to match what is designated in the XML
  var ship = "FGS_Braunshweig _hevii";

  foreach(var shipN; props.globals.getNode("/ai/models", 1).getChildren("aircraft")) {
    {
      if (string.match(shipN.getNode("callsign").getValue(), ship")) {

        aiParent = shipN.getNode("callsign").getParent().getName() ~ "[" ~ shipN.getNode("callsign").getParent().getIndex() ~ "]";

        var currentLat = props.globals.getNode("/ai/models/" ~ aiParent ~ "/latitude-deg").getValue();
        var currentLon = props.globals.getNode("/ai/models/" ~ aiParent ~ "/longitude-deg").getValue();
        var terrain_elev = geo.elevation(currentLat, currentLon);

        props.globals.getNode("ai/models/" ~ aiParent ~ "/altitude-ft").setDoubleValue(terrain_elev);
      }
   }
});
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Wed Feb 07, 2018 5:11 pm

Note: The setlistener is use to make sure everything is initialized before executing the script.
Also, you can get as fancy as you want with something like this and set another listener or put this in a loop to continuously check for the ships movement and keep changing the altitude as needed.


For one, I think your ship name in the AI list will be
var ship = "FGS_Braunshweig";
not
var ship = "FGS_Braunshweig _hevii";

"FGS_Braunshweig _hevii" I think is the name of the scenario not the AI.

You can look through the carrier AI entries and find the one your working with to confirm its callsign.

How did you display the script log on your screen?

This is called the "Property Browser"
In the GUI under Debug/Browse Internal Properties

There are quick dirty ways to debug and verify information is what you want such as...
setprop("A-my-ship-altitude-is", terrain_elev);

this will create a property in the property browser root (first level) at the top of the list that contains the current elevation your function is returning. Just don't forget to add it after the function has done all the work (at the end of the function). Also don't forget to delete that line when your finished debugging.

If this debug value matches the ship altitude value, you know your setting the variable.

Again, you may need to add, subtract or multiply the terrain_elev value to either convert from meter to feet or any offset the model might have.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Wed Feb 07, 2018 5:14 pm

If this debug value matches the ship altitude value, you know your setting the variable.

Also if you see this new property "A-my-ship-altitude-is" in the Property Browser, then you know your script is being executed and you know it is also getting the elevation (so long as there is a value attached to the property).

This one test line can tell you a lot at a glance.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Wed Feb 07, 2018 5:32 pm

For one, I think your ship name in the AI list will be
var ship = "FGS_Braunshweig";
not
var ship = "FGS_Braunshweig _hevii";

I think that I'm correct in this regard. "FGS_Braunshweig _hevii" is listed as one of the available AI choices in the list. "FGS_Braunshweig" is what I see on the Map view.

Is it actually correct of me to simply substitute <altitude>terrain_elev</altitude> for say, <altitude>1286</altitude> in the xml file? It doesn't seem code-worthy enough to me :?

you can get as fancy as you want with something like this and set another listener or put this in a loop to continuously check for the ships movement and keep changing the altitude as needed

Absolutely, this is a core functional requirement since elevations in moving bodies of water (ie rivers) are dynamic; they decrease when going downstream and increase when going upstream. I'm sure that I'm not capable of doing anything resembling "fancy" but, as long as I have even a Frankensteinian-hideous chunk o'code which does what I'm after, I'll be happy :P. Now it's time to see if I can scavenge some bits and pieces in attempt to create my monster...
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Wed Feb 07, 2018 5:54 pm

Is it actually correct of me to simply substitute <altitude>terrain_elev</altitude> for say, <altitude>1286</altitude> in the xml file? It doesn't seem code-worthy enough to me


I'm not sure you wrote that question correctly?

No, that is the "default" starting value for the ships elevation. That needs to be a number or value, not a variable name or a property. In this case it is actually immaterial what that value is. I think I would default it to 0 though. But if you want the ship to start at a certain position and that position is at 1286, then go ahead and use <altitude>1286</altitude>. Under no circumstance can you use <altitude>terrain_elev</altitude>.

The script is what is going to change that value dynamically, you can't dynamically change it in the XML.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Wed Feb 07, 2018 6:06 pm

I think that I'm correct in this regard. "FGS_Braunshweig _hevii" is listed as one of the available AI choices in the list.

No, I think your wrong.

your looking at these...

Image

The left dialog is the scenarios (not important for our discussion or the code).

The right dialog contains the list of carriers, one of which should be yours. It has the entries "callsign" and "name". I was wrong in using "callsign". What you should be using is "name". Whatever is in the "name" field or property is what you want to search for.

ie:

Code: Select all
if (string.match(shipN.getNode("name").getValue(), ship")) {

        aiParent = shipN.getNode("name").getParent().getName() ~ "[" ~ shipN.getNode("name").getParent().getIndex() ~ "]";
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Wed Feb 07, 2018 6:10 pm

I would default it to 0 though. But if you want the ship to start at a certain position and that position is at 1286, then go ahead and use <altitude>1286</altitude>.
Ah, I think that I see what you're saying. OK: I'll leave it at "0" because my intent is to be able to move this and similar scenarios to other bodies of water with unique surface elevations, either static (ie: "real" lakes) or dynamic (ie: big rivers).

Still stumbling through the darkness, trying to sort out how to loop the script...
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Wed Feb 07, 2018 6:28 pm

I would hate to see you in the darkness so here is a revised script with some corrections. Look closely for the differences. I took assumptions that weren't correct, such as aircraft instead of carrier and callsign instead of name.

This also includes the loop so it will continue to update the elevation.

Code: Select all
# main() ============================================================

#this needs to match what "name" is designated in the XML
#not the "scenario" name but the "entry" name
var ship = "FGS_Braunshweig";

var main_loop = func {

  foreach(var shipN; props.globals.getNode("/ai/models", 1).getChildren("carrier")) {
    {
      if (string.match(shipN.getNode("name").getValue(), ship")) {

        aiParent = shipN.getNode("name").getParent().getName() ~ "[" ~ shipN.getNode("name").getParent().getIndex() ~ "]";

        var currentLat = props.globals.getNode("/ai/models/" ~ aiParent ~ "/latitude-deg").getValue();
        var currentLon = props.globals.getNode("/ai/models/" ~ aiParent ~ "/longitude-deg").getValue();
        var terrain_elev = geo.elevation(currentLat, currentLon);

        props.globals.getNode("ai/models/" ~ aiParent ~ "/elevation-ft").setDoubleValue(terrain_elev);
      }
   }
  settimer(main_loop, 10);
}

setlistener("/sim/signals/fdm-initialized", func {
  main_loop();
});
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Wed Feb 07, 2018 6:37 pm

wlbragg, if one of your infamous tornadoes ever blows you and your dog over to my part of Oz, be sure to drop in and I'll buy you a beer!

I'm going to see if I will experience joy on the Bodensee now...
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Wed Feb 07, 2018 7:14 pm

wlbragg, if I knew how to add a screenshot to this message board I would have attached one which shows the Braunschweig cutting through the calm waters of the Bodensee. Thank you, very much! :mrgreen:

I'm now inspired to try to try to create other ships and perhaps even look into 'flight plans' so that they can fully navigate autonomously in the FlightGear world. But first, it's back to work in the "real world" so that I can eat...
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?  

Postby wlbragg » Wed Feb 07, 2018 7:35 pm

Your welcome, can't wait to see you progress to other endeavors now that you have the bug and can see how logical and easy it is to code fun stuff in FG. It simply takes time to learn the syntax. This bit of code has more lessons in it than you might think. For as simple as it is it covers quite a bit.

Good luck!
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hooray » Wed Feb 07, 2018 10:48 pm

To learn more about the built-in property browser (consider it a "variable viewer/inspector"), see:

http://wiki.flightgear.org/Property_browser
Image

For the sake of completeness, there are two more tools that are highly useful when tinkering with Nasal scripting:

http://wiki.flightgear.org/Nasal_Console
Image

http://wiki.flightgear.org/Interactive_Nasal_Console
Image

For debugging purposes, check out the debug module: http://wiki.flightgear.org/Nasal_library/debug

Referring to wlbragg's A-my-ship-altitude-is workaround, there is actually a dedicated "developer mode" available for these purposes in $FG_ROOT/Nasall/prop_key_handler.nas, the usage of which is documented at the top of the file: https://sourceforge.net/p/flightgear/fg ... andler.nas
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Thu Feb 08, 2018 1:20 am

I had thought of posting this in the Nasal forum but, since it's tied quite tightly to this particular thread, I felt that I had better continue here. I apologise for any decorum faux pas.

I've looked but quickly became overwhelmed and then lost when searching for code examples. So, I'm back here, grovelling... :oops:

Can a variable contain a Boolean operator? For example, the script which wlbragg so very kindly provided include this:
Code: Select all
var ship = "FGS_Braunshweig";

Since we currently have a fairly decent sized fleet of various ships available to us, I'd like to include some of these others in the growing flotilla upon the Bodensee. I'm now confident that to enable the same functionality for those ships I can create a unique nasal file for each named vessel. However, this seems very inefficient and likely (and rightly) to be scoffed at by the Coding Overlords. So, I was thinking that if I somehow had an "AND/OR" in the 'ship' variable, I could simply add the name of each craft to a single nas script. Having not found a suitable reference or example, I tried the following with unsuccessful results:
Code: Select all
var ship = "[FGS_Braunshweig", "Vinson"]";

Am I on the right track or have I gone completely off of the rails with this approach?

By the way: When I created a "live_surface_elevation.nas" file unique to the Vinson, the carrier was correctly floating upon the surface of the water as to be expected. However, the escorts referred to in the same xml scenario had all become submarines, probing the depths of the Bodensee at the equivalent of Mean Sea Level. I can understand why this happens since the escorts' actual names aren't referred to in the variable. Is it possible to create a global variable that can be applied to all ship models? After all, they all share the common attribute of floating upon the water's surface, regardless of local elevation. For now, I'll forget about the effect that fresh versus sea water densities has on buoyancy :wink:

*My pipe dream: It would be great if somehow all ships would have this functionality as a default in their xml files. This way, anybody could place any vessels anywhere in the simulated world without having to jump through the (granted, "highly interesting") hoops that I've had assistance in jumping through.
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby wlbragg » Thu Feb 08, 2018 3:46 am

Can a variable contain a Boolean operator?
[snip]
var ship = "[FGS_Braunshweig", "Vinson"]";

I think you mean an array. Yes a boolean is also possible. A boolean is true or false.
I can think of at least two possible ways to do this, there are probably more.

One way would be to use the first variable I tried to use "callsign". It doesn't appear to be used currently. In the XML files make all your AI ships use a unique "callsign", like "fleet" and use that field to be the criteria for executing the elevation change.
It might be as simple as changing the following lines.

#no longer needed
var ship = "FGS_Braunshweig";
Code: Select all
var main_loop = func {
  foreach(var shipN; props.globals.getNode("/ai/models", 1).getChildren("carrier")) {
    {
      if (string.match(shipN.getNode("callsign").getValue(), "fleet")) {

        aiParent = shipN.getNode("callsign").getParent().getName() ~ "[" ~ shipN.getNode("callsign").getParent().getIndex() ~ "]";

Everything else is the same.

Or using the array of names, something like this...
Code: Select all
#another method using an array of names
var ships = ["FGS_Braunshweig","Vinson"];

var main_loop = func {
  foreach(var carrierN; props.globals.getNode("/ai/models", 1).getChildren("carrier")) {
    foreach(var shipN; ships) {
      if (string.match(carrierN.getNode("name").getValue(), ships[shipN])) {

        aiParent = carrierN.getNode("name").getParent().getName() ~ "[" ~ shipN.getNode("name").getParent().getIndex() ~ "]";

        var currentLat = props.globals.getNode("/ai/models/" ~ aiParent ~ "/latitude-deg").getValue();
        var currentLon = props.globals.getNode("/ai/models/" ~ aiParent ~ "/longitude-deg").getValue();
        var terrain_elev = geo.elevation(currentLat, currentLon);

        props.globals.getNode("ai/models/" ~ aiParent ~ "/elevation-ft").setDoubleValue(terrain_elev);
      }
    }
  }
  settimer(main_loop, 10);
}
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7588
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Determine"Altitude" of various Fresh Water Lakes?

Postby Hevii Guy » Thu Feb 08, 2018 2:41 pm

wlbragg wrote in Thu Feb 08, 2018 3:46 am:It might be as simple as changing the following lines.

Two beers and a burger. And a chewy bone for Toto. Thank-you, so very much, wlbragg :!:

It's great to now be able to derive more usage from the available ships. However, it's even better learning about coding. Thank you also for these great examples. They are my Rosetta Stone to be used in further attempts to do other weird stuff in this great program :D
Hevii Guy
 
Posts: 42
Joined: Wed Mar 23, 2016 4:40 pm
Location: Global
Callsign: CH-HEVI
Version: 2017.3.1
OS: Mac OS 10.11.6

PreviousNext

Return to AI Traffic

Who is online

Users browsing this forum: No registered users and 6 guests