Automating boats - BoatScript

VRTool software support forum

Automating boats - BoatScript

Postby omar » Mon Apr 18, 2011 2:41 pm

Auto-pilots are taboo in virtual regattas.
People feel the game will loose if too much automation
creeps in. Still there are times we wish boats had a
little intelligence of their own.

Going hours with the wrong sail?
Or getting stuck against the wind during the night?
Can a Captn get a decent sleep???

Enter Boat Script. BS is a script system designed to facilitate
monitoring and controlling boats in vrtool.
It uses JavaScript, a powerful and popular script language.

Image

Only vrtool's own sandbox boats can be controlled (course/sail).
Other boats can be monitored. Scripts always run in the client machine.
Running untrusted scripts (like web browsers do) is tricky.

The following illustrates a simple Boat Script:
Code: Select all
function OnNewPosition() {
   Boat.Course = Boat.BCD;         
   Boat.Sail   = Boat.BestSail;
   Boat.Commit();
}


Event OnNewPosition() is fired when a new point is
added to boat track (a 10 min tick)

What this script does?
1- Sets boat course to the best course to destination (best velocity made good)
2- Selects the best sail for that course
3- Uses Commit() to send the control action to the server (if course and/or sail changed)

Only 5 lines, yet pretty efficient. It will even enter zig-zag mode as needed (see image below)

Image

The full BoatScript interface is below:

Control Parameters (read/write):
Code: Select all
Boat.Course;            int
Boat.Sail;              int - Numeric sail ID
Boat.Commit();          Sends changed control parameters to server (if course or sail changed)


Boat data properties (read only):
Code: Select all
Boat.Boatspeed;         double     in knts
Boat.Latitude;          double
Boat.Longitude;         double
Boat.TWS;               double     Wind speed in knts
Boat.TWD;               int        Wind direction
Boat.TWA;               int        Wind angle
Boat.Classif;           int        Classification
Boat.DestDirection;     int        Direction to destination
Boat.DestDistance;      double     Distance to destination in NM
Boat.BCD;               int        Best course to destination
Boat.VMC;               double     Velocity made good to destinatio
Boat.BestSail;          int        Best sail for the course
Boat.PosTime;           datetime   Position time
Boat.Boatname;          String     Boat name
Boat.Sailname;          String     Sail name


Query other boats:
Code: Select all
Boat.BoatDistance(Boatname);    double  Distance to another boat in NM (returns 0 if boat not found)
Boat.BoatDirection(Boatname);   int     Direction to another boat (returns -1 if boat not found)
Boat.PointDistance(lat,lon);    double  Distance to a point in NM
Boat.PointDirection(lat,lon);   int     Direction to a point       
Boat.GetBoat(Boatname);         returns interface to another Boat (null if boat not found)
  You can use other boat data in your script, but cannot commit changes
  to control parameters in other boats (of course).


Script logging:
Code: Select all
Boat.ShowData();                Show boat data in script log
Boat.ShowMessage(Msg);          Show message in script log
Boat.ClearMessages();           Clear script log


Events:
Code: Select all
function OnNewPosition() {
  //fired every time a new point is received
}


// the texts after two slashes are comments (not necessary to run the script)
Notes: * All distances and directions calculated with great circle formulas
* South latitudes and East longitudes are negative

Script examples:

corkscrew route:
Code: Select all
// "corkscrew route" - boat script
// - increasing course regularly ( 2 degrees/10 min tick )
// - always use best sail (screw the crew)
// - skip pointing upwind

function OnNewPosition() {
   Boat.Course = Boat.Course+2;   // increase course each tick

   if (Boat.TWA<45) {             // skip upwind (wind angle<45)
     Boat.Course = Boat.Course+90;
     Boat.ShowMessage("Tack!");
     }

   Boat.Sail   = Boat.BestSail;     //change sail if needed

   //print information
   Boat.ShowData();
   Boat.ShowMessage("Hi Captn. Doing good at "+Boat.Boatspeed+" knts");
   Boat.Commit();         // sends control params, if changed
}


Follow a boat (either local or VR)
Code: Select all
//  "bs_followTitanic.js" - Follows a boat named "Titanic" with best sail

function OnNewPosition() {
  var c = Boat.BoatDirection("Titanic");    // BoatDirection() returns -1 if boat not found 
  if (c>=0) {
    Boat.Course = c;   
   Boat.Sail = Boat.BestSail;
   Boat.Commit();
  }
}



Print VMC table:
Code: Select all
// "print VMC table" - boat script
// - this example prints a table with course;vmc columns
// - example of how to use the script log
// - does not affect boat control ( since there is no commit() command )

function OnNewPosition() {
   var bestSpeed=0;
   var bestCourse=0;
   var vmc;

   for (i=0; i<360; i++) {
      Boat.Course = i;
      vmc = Boat.VMC;
      if (vmc>bestSpeed) {
        bestSpeed=vmc;
        bestCourse=i;
      }
      Boat.ShowMessage(i+";"+vmc);
   }
   Boat.ShowMessage("best course="+bestCourse+"/best vmc="+bestSpeed);
}


Installation

BoatScript is available in version 1.97c (zip)
link: http://www.tecepe.com.br/nav/vrtool/vrtool.zip
md5: 213583286F3351A5D95013493E4A2F13
Some sample scripts included (search for bs_*.js)
Users with versions older than 1.97b should use the full installer first,
then unzip the latest 1.97c

Usage
Use BoatScript panel in Remote Boat control window, as illustrated below.

Important: Remember to save the script.
I use files named bs_scriptname.js
Scripts are saved as external .js files, not embedded in the desktop.
So any change must be saved to the script file to remain persistent.

Image
User avatar
omar
Site Admin
 
Posts: 477
Joined: Thu Apr 30, 2009 11:00 am

Re: Automating boats - BoatScript

Postby Neptune » Mon Apr 18, 2011 6:03 pm

I tried to install it in the same folder as version "B" but I get an error and will not install - BTW version B works fine.
Neptune
 
Posts: 88
Joined: Sun Sep 20, 2009 8:39 pm

Re: Automating boats - BoatScript

Postby Neptune » Mon Apr 18, 2011 7:50 pm

This are the 2 messages I got :
WERBD64
Windows NT Version 6.0 Build: 6002 Service Pack 2
Product (0x3): Windows Vista (TM) Home Premium
Edition: HomePremium
BuildString: 6002.18327.x86fre.vistasp2_gdr.101014-0432
Flavor: Multiprocessor Free
Architecture: X86
LCID: 1033

WERCDE8
<?xml version="1.0" encoding="UTF-16"?>
<DATABASE>
<EXE NAME="VRTool.exe" FILTER="GRABMI_FILTER_PRIVACY">
<MATCHING_FILE NAME="VRTool.exe" SIZE="5662720" CHECKSUM="0x293E5246" MODULE_TYPE="WIN32" PE_CHECKSUM="0x0" LINKER_VERSION="0x0" LINK_DATE="06/19/1992 22:22:17" UPTO_LINK_DATE="06/19/1992 22:22:17" />
</EXE>
<EXE NAME="kernel32.dll" FILTER="GRABMI_FILTER_THISFILEONLY">
<MATCHING_FILE NAME="kernel32.dll" SIZE="891392" CHECKSUM="0xF772C2F2" BIN_FILE_VERSION="6.0.6002.18005" BIN_PRODUCT_VERSION="6.0.6002.18005" PRODUCT_VERSION="6.0.6001.18000" FILE_DESCRIPTION="Windows NT BASE API Client DLL" COMPANY_NAME="Microsoft Corporation" PRODUCT_NAME="Microsoft® Windows® Operating System" FILE_VERSION="6.0.6001.18000 (longhorn_rtm.080118-1840)" ORIGINAL_FILENAME="kernel32" INTERNAL_NAME="kernel32" LEGAL_COPYRIGHT="© Microsoft Corporation. All rights reserved." VERDATEHI="0x0" VERDATELO="0x0" VERFILEOS="0x40004" VERFILETYPE="0x2" MODULE_TYPE="WIN32" PE_CHECKSUM="0xDBE01" LINKER_VERSION="0x60000" UPTO_BIN_FILE_VERSION="6.0.6002.18005" UPTO_BIN_PRODUCT_VERSION="6.0.6002.18005" LINK_DATE="04/11/2009 06:25:33" UPTO_LINK_DATE="04/11/2009 06:25:33" EXPORT_NAME="KERNEL32.dll" VER_LANGUAGE="English (United States) [0x409]" />
</EXE>
Neptune
 
Posts: 88
Joined: Sun Sep 20, 2009 8:39 pm

Problem with Titanic script

Postby omar » Tue Apr 19, 2011 11:14 am

Image
"Corkscrew" script in action..

Obvious problem with the Follow boat script:
if you reach the target boat "Titanic", you start to
sail around it... :(

Hum.. Gotta add some other condition.
May be go direct to destination, if it
looks better then the Target boat ;)

p.s. About the message, i will investigate.
The only thing I found was that users of Skype
and a games called Oblivion and FIFA had the
same informative crash message...
User avatar
omar
Site Admin
 
Posts: 477
Joined: Thu Apr 30, 2009 11:00 am

Re: Automating boats - BoatScript

Postby Neptune » Tue Apr 19, 2011 1:15 pm

I removed Skype from my PC but it did not resolved the problem
Neptune
 
Posts: 88
Joined: Sun Sep 20, 2009 8:39 pm

Re: Automating boats - BoatScript

Postby omar » Wed Apr 20, 2011 12:01 pm

Neptune wrote:I removed Skype

I'm not saying Skype is causing the problem. It only gives that same error.
Internet Explorer is also failing with the same GRABMI message.
Unfortunately I found no explanation of what it means.
User avatar
omar
Site Admin
 
Posts: 477
Joined: Thu Apr 30, 2009 11:00 am

Improved "follow boat" script

Postby omar » Wed Apr 20, 2011 12:27 pm

The 1st version of this script has a problem:
boat circles around target boat if reached.
This version switches to destination course
if/when target boat is "passed".

Code: Select all
// bs_followTitanic.js - version 2
// Boat chases targetBoat.
// If 1)target boat data missing or
//    2)very close to target boat (<2 NM) or
//    3)boat course too different from destination course
// go to the destination with BCD 

function OnNewPosition() {
  var targetBoat = "Titanic";     //set target boatname
  var targetCap = Boat.BoatDirection(targetBoat);
  var targetDist = Boat.BoatDistance(targetBoat);
  var destCap = Boat.DestDirection;
  var destDist = Boat.DestDistance;
 
  // calc difference destination course - target course
  if (destCap>targetCap) delta=destCap-targetCap; else delta=targetCap-destCap; //cap difference
  if (delta>180) delta= delta-180;    //take the smaller angle
 
  // show calculations
  // Boat.ClearMessages(); //clear log (optional)
  Boat.ShowMessage("destination cap="+destCap+" dist="+destDist);
  Boat.ShowMessage("target boat cap="+targetCap+" dist="+targetDist);
  Boat.ShowMessage("cap dif="+delta);
  Boat.ShowMessage("");
 
  var cond1 = (targetCap == -1); // cond 1 - target course = -1 means "boat not found"   
  var cond2 = (targetDist<2);    // cond 2 - if less than 2 NM from Target
  var cond3 = (delta>60);        // cond 3 - TOO far from detination course. Probably we passed target
                       
  if  ( cond1 || cond2 || cond3)  {  //  cond1 OR cond2 OR cond3
    Boat.Course = Boat.BCD;          //use BCD instead of direct dest course (more efficient)
    Boat.ShowMessage("sailing best course to destination");
  } else {
    Boat.Course = targetCap;
    Boat.ShowMessage("following target boat");
  }
  Boat.ShowMessage("course set to "+Boat.Course);
  Boat.Sail = Boat.BestSail;     //select best sail
  Boat.ShowMessage("current sail "+Boat.Sailname);
   
  Boat.Commit();      //commit changes
}


Image
User avatar
omar
Site Admin
 
Posts: 477
Joined: Thu Apr 30, 2009 11:00 am

Re: Automating boats - BoatScript

Postby Neptune » Thu Apr 21, 2011 12:28 am

I was able to load V 1.97C on an old xp pc.
The Remote boat panel however looks very different than the one shown above. The Top is the same but the bottom half has info on server, regatta name etc.
I have not found the items to bring the section where the script would go.
The option panel setting ( to right hand corner ) switches from half to full length.
Neptune
 
Posts: 88
Joined: Sun Sep 20, 2009 8:39 pm

Re: Automating boats - BoatScript

Postby omar » Fri Apr 22, 2011 2:21 am

Regarding the crash problem when you start vrtool 1.97c
in Vista and Windows 7, please read the post below:
http://www.tecepe.com.br/phpbb3/viewtopic.php?f=2&t=199

The remote boat window has many panels.
Use the [.bs] button to open the script editor.
Use the [sliders] button to open the config panel.
If controlling a local boat, remember to activate
the embedded web server and point your remote
boat to localhost:8080.
User avatar
omar
Site Admin
 
Posts: 477
Joined: Thu Apr 30, 2009 11:00 am

Re: Automating boats - BoatScript

Postby jackblow33 » Sat Apr 23, 2011 4:07 pm

If controlling a local boat, remember to activate
the embedded web server and point your remote
boat to localhost:8080.

Can you PLZ explain how?
For now in the part named: regatta settings It said: downloading
But I didn't get my boat data. So script have no effect yet on the boat I'm trying to control on VirtualRegatta.
Thank you.
jackblow33
 
Posts: 1
Joined: Sat Apr 23, 2011 3:46 pm

Next

Return to VRTool Forum