Page 5 of 7
Re: Scripters cove
Posted: Sat Aug 16, 2014 8:25 am
by cim
dertien wrote:I am trying to get ships that have this AIState to drop their gear, but it also has an influence on the playership, which I would like to avoid.
Code: Select all
if(this.ship.AIState = "GO_TO_STATION")
Typo - should be
Code: Select all
if(this.ship.AIState == "GO_TO_STATION")
"=" means set the thing on the left to be equal to the thing on the right. For reasons, this operation is treated as having the value of the thing on the right, so if you use it in an 'if', it's almost always true.
"==" means true if left is equivalent to right, and false otherwise.
"===" means true if left is exactly equal to right, and false otherwise.
In this case the difference between "==" and "===" isn't relevant. For an example of where it matters:
0 == "0"
is true
0 === "0"
is false
Some languages and compilers will warn you if you put the "=" form inside an 'if' without also putting a "no, I really meant to do that" marker with it.
Javascript doesn't, so you need to be extremely careful with them.
(ship.AIState is read/write, so the test will also be confusing a lot of the NPCs... and telling them to go to the station, which may not even be valid for their AI. With most ships in 1.80 using the new Javascript AIs, it won't be causing detectable problems, but if you'd done this in 1.77 it would have caused a big noticeable mess ... which is usually the way programmers spot they've left an "=" out in these things)
The best way to test in 1.80 if a ship is trying to dock is this:
Code: Select all
if (this.ship.dockingInstructions) {
This works whether it's using an old AI or a new AI, and even works for the player if they're using the docking computer. (Player without docking computer is not reliably testable)
Re: Scripters cove
Posted: Sat Aug 16, 2014 9:37 pm
by dertien
cim wrote:
The best way to test in 1.80 if a ship is trying to dock is this:
Code: Select all
if (this.ship.dockingInstructions) {
This works whether it's using an old AI or a new AI, and even works for the player if they're using the docking computer. (Player without docking computer is not reliably testable)
But testing
if (this.ship.dockingInstructions)
{
this.ship.commsMessage("Received dockinginstructions");
this.retractGear();
this.extendGear();
}
in either 1.79 or 1.80 doesn't seem to have any effect.
Playerwise, since I don't want to use a primable key with this one and complicating things gameplay wise, I thought a good and logical idea would simply be (if the player has no docking computer or it has been damaged) to test if
a) the player has targeted a station (the main station in this case)
and
b) if the player ship were closer than a certain value
to call the this.extendGear = function()
if not,
call the
this.retractGear = function()
I am still using 1.79 trunk
but this:
Code: Select all
if (this.ship == player && this.target.isStation)
{
this.ship.target.explode();
}
just for testing purposes of course, does not have the desired effect, the log is of no help either...
can somebody assist ?
Re: Scripters cove
Posted: Sat Aug 16, 2014 9:47 pm
by cim
The dockingInstructions test code looks like it should work. When do you actually run that test?
dertien wrote:if (this.ship == player && this.target.isStation)
{
this.ship.target.explode();
}
[/code]
A couple of typos.
Use
this.ship == player.ship
instead of
this.ship == player
. (Or, perhaps simpler, use
this.ship.isPlayer
)
Also
this.ship.target.isStation
instead of
this.target.isStation
dertien wrote:I am still using 1.79 trunk
You should probably upgrade to 1.80 - there were a lot of bugs fixed in the final weeks of 1.79. If it's a really old 1.79 it might not have the
dockingInstructions
property, either.
Re: Scripters cove
Posted: Sat Aug 16, 2014 9:53 pm
by dertien
I have tested the latest code in 1.8
I launch Oolite
I load a saved game
pressing F1
then turning the ship around and targeting the station (r) key on windows
It targets the station, but no explosion.
Used this code as you said now:
Code: Select all
if (this.ship.isPlayer && this.ship.target.isStation)
{
this.ship.target.explode();
}
or should I do this with a new start game, and not reload a savegame for this:
in the log I get this:
Code: Select all
23:49:45.218 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (landing_gear): TypeError: this.ship.target is null
23:49:45.218 [script.javaScript.exception.unexpectedType]: ../AddOns/ZZ_HPC_Cobra_mk3.oxp/Scripts/landing_gear_co3.js, line 183.
23:49:45.218 [script.javaScript.load.failed]: ***** Error loading JavaScript script ../AddOns/ZZ_HPC_Cobra_mk3.oxp/Scripts/landing_gear_co3.js -- could not run script
Re: Scripters cove
Posted: Sat Aug 16, 2014 9:56 pm
by dertien
also tested
Code: Select all
if (this.ship == player.ship && this.ship.target.isStation)
{
this.ship.target.explode();
}
no fireworks
Re: Scripters cove
Posted: Sat Aug 16, 2014 9:58 pm
by dertien
Oh, sorry about your earlier question:
Code: Select all
if (this.ship.dockingInstructions)
{
this.ship.commsMessage("Received dockinginstructions");
this.retractGear();
this.extendGear();
}
I run this, when switching on the autopilot, after launch. Pressing the C key with autopilot equipped of course.
Re: Scripters cove
Posted: Sat Aug 16, 2014 10:09 pm
by cim
dertien wrote:Code: Select all
23:49:45.218 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (landing_gear): TypeError: this.ship.target is null
Ah, yes.
Code: Select all
if (this.ship == player.ship && this.ship.target && this.ship.target.isStation)
You need to check if the ship has a target at all before you can check if that target is a station. Otherwise you're asking if "nothing" is a station, and that's not answerable.
dertien wrote:Code: Select all
23:49:45.218 [script.javaScript.exception.unexpectedType]: ../AddOns/ZZ_HPC_Cobra_mk3.oxp/Scripts/landing_gear_co3.js, line 183.
23:49:45.218 [script.javaScript.load.failed]: ***** Error loading JavaScript script ../AddOns/ZZ_HPC_Cobra_mk3.oxp/Scripts/landing_gear_co3.js -- could not run script
These bits, on the other hand ... it's not unexpected that you'd get an error from running that code with nothing targeted, but the exact text of the error seems odd. So - when do you run this test code?
dertien wrote:I run this, when switching on the autopilot, after launch. Pressing the C key with autopilot equipped of course.
Using the
playerStartedAutoPilot
function? That might not work - the docking instructions might not be sent from the station until the next frame, after your test has run.
Re: Scripters cove
Posted: Sat Aug 16, 2014 10:17 pm
by dertien
This it the complete code as I have tested on 1.8
Code: Select all
this.name = "landing_gear";
this.author = "griff";
this.copyright = "� 2008 griff.";
this.version = "1.01";
this._findSubEntity = function(key)
{
for (var i = this.ship.subEntities.length - 1 ; i >=0 ; --i)
{
if (this.ship.subEntities[i].dataKey == key)
{
return this.ship.subEntities[i];
}
}
return null; // couldn't find it.
}
this.retractGear = function()
{
this.ship.commsMessage("retracted");
var actuatorsR = this._findSubEntity("HPC_actuatorR");
if (actuatorsR)
{
actuatorsR.position = [0, 6.155206, 2.648078];
}
var actuatorsF = this._findSubEntity("HPC_actuatorF");
if (actuatorsF)
{
actuatorsF.position = [0, 3.80, -21.3];
}
var reargearextendPRT = this._findSubEntity("HPC_reargearPRT");
if (reargearextendPRT)
{
reargearextendPRT.position = [-17.392771, -8.961073, 2.796213];
reargearextendPRT.orientation = [0.9953, 0.0908, 0, 0];
}
var reargearextendSTB = this._findSubEntity("HPC_reargearSTB");
if (reargearextendSTB)
{
reargearextendSTB.position = [17.552752, -8.847222, 3.028129];
reargearextendPRT.orientation = [-0.9953, -0.0908, 0, 0];
}
var frontgearextendPRT = this._findSubEntity("HPC_frontgearPRT");
if (frontgearextendPRT)
{
frontgearextendPRT.position = [-13.038872, -5.954015, 25.049587];
reargearextendPRT.orientation = [0, 0, 0.9959, -0.0905];
}
var frontgearextendSTB = this._findSubEntity("HPC_frontgearSTB");
if (frontgearextendSTB)
{
frontgearextendSTB.position = [13.038872, -5.954015, 25.049587];
reargearextendPRT.orientation = [0.9959, 0.0905, 0, 0];
}
}
this.extendGear = function()
{
var actuatorsR = this._findSubEntity("HPC_actuatorR");
if (actuatorsR)
{
actuatorsR.position = [0,0,0];
}
var actuatorsF = this._findSubEntity("HPC_actuatorF");
if (actuatorsF)
{
actuatorsF.position = [0,0,0];
}
var reargearextendPRT = this._findSubEntity("HPC_reargearPRT");
if (reargearextendPRT)
{
reargearextendPRT.position = [-17.351349,-17.408951,-3.749173];
}
var reargearextendSTB = this._findSubEntity("HPC_reargearSTB");
if (reargearextendSTB)
{
reargearextendSTB.position = [17.435233,-17.338642,-3.50929];
}
var frontgearextendPRT = this._findSubEntity("HPC_frontgearPRT");
if (frontgearextendPRT)
{
frontgearextendPRT.position = [-12.765342,-11.194131,27.818142];
}
var frontgearextendSTB = this._findSubEntity("HPC_frontgearSTB");
if (frontgearextendSTB)
{
frontgearextendSTB.position = [12.765342,-11.194131,27.818142];
}
}
if (this.ship.dockingInstructions)
{
this.ship.commsMessage("Received dockinginstructions");
this.retractGear();
this.extendGear();
}
if (this.ship == player.ship && this.ship.target && this.ship.target.isStation)
{
this.ship.target.explode();
}
switching on the docking computer after launch ( no gear is down)
targeting the station gives me the "target locked on to Coriolis" but no explosion.
Re: Scripters cove
Posted: Sat Aug 16, 2014 10:35 pm
by dertien
The test file if it is of any help can be found here:
https://app.box.com/s/n6a61qarmglemfwce1ak
Re: Scripters cove
Posted: Sun Aug 17, 2014 2:36 am
by Neelix
Those if statements at the end would only be processed once, when script is initiated. If you want them to be continuously re-evaluated you would need to put them into a timer callback function.
- Neelix
Re: Scripters cove
Posted: Sun Aug 17, 2014 3:26 am
by dertien
Well, this is just for testing purposes, as the script is not complete yet;
The first script would have to be continuously checked since it will apply to players (with EQ_DOCK_COMP) and NPC's
The second one needs an extra if clause that should checking the player has EQ_DOCK_COMP. If the answer is null then the code should be run, based on a certain distance, and target met (in this case a station), and lowering the gear.
else the one above should run automatically with the EQ_DOCK_COMP installed.
I do understand what you mean now since they are not functions, and do not run like the rest of the code continuously. However, can someone point me to timer callback function in another oxp, I could try to paste someting together.
Re: Scripters cove
Posted: Sun Aug 17, 2014 8:54 am
by Zireael
My Engine Trails OXP uses timer callbacks... I'm not sure if they're well done, but it works...
Re: Scripters cove
Posted: Sun Aug 17, 2014 2:04 pm
by dertien
Okay, I've commented the autopilot code so it doesn't interfere with the two functions I would like to test:
I am running 1.81 now and have tested this also on 1.80, with the player ship.
I used a savegame.
I have put both "if" clauses (correct?) in the shipEnteredStationAegis function to make for easy testing.
Code: Select all
this.name = "HPC Cobra Mk3 ship scripts";
this.author = "Cim, Dertien";
this.copyright = "Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) http://creativecommons.org/licenses/by-nc-sa/3.0/";
this.version = "1.00";
// Cim: So, what you can do is search through the subentities list until you find the one you want.
this._findSubEntity = function(key)
{
for (var i = this.ship.subEntities.length - 1 ; i >=0 ; --i)
{
if (this.ship.subEntities[i].dataKey == key)
{
return this.ship.subEntities[i];
}
}
return null; // couldn't find it.
}
// reusable function that retracts the gear
this.retractGear = function()
{
var actuatorsR = this._findSubEntity("HPC_actuatorR");
if (actuatorsR)
{
actuatorsR.position = [0, 6.155206, 2.648078];
}
var actuatorsF = this._findSubEntity("HPC_actuatorF");
if (actuatorsF)
{
actuatorsF.position = [0, 3.80, -21.3];
}
var reargearextendPRT = this._findSubEntity("HPC_reargearPRT");
if (reargearextendPRT)
{
reargearextendPRT.position = [-17.392771, -8.961073, 2.796213];
reargearextendPRT.orientation = [0.9953, 0.0908, 0, 0];
}
var reargearextendSTB = this._findSubEntity("HPC_reargearSTB");
if (reargearextendSTB)
{
reargearextendSTB.position = [17.552752, -8.847222, 3.028129];
reargearextendPRT.orientation = [-0.9953, -0.0908, 0, 0];
}
var frontgearextendPRT = this._findSubEntity("HPC_frontgearPRT");
if (frontgearextendPRT)
{
frontgearextendPRT.position = [-13.038872, -5.954015, 25.049587];
reargearextendPRT.orientation = [0, 0, 0.9959, -0.0905];
}
var frontgearextendSTB = this._findSubEntity("HPC_frontgearSTB");
if (frontgearextendSTB)
{
frontgearextendSTB.position = [13.038872, -5.954015, 25.049587];
reargearextendPRT.orientation = [0.9959, 0.0905, 0, 0];
}
}
// reusable function that extends the gear
this.extendGear = function()
{
var actuatorsR = this._findSubEntity("HPC_actuatorR");
if (actuatorsR)
{
actuatorsR.position = [0,0,0];
}
var actuatorsF = this._findSubEntity("HPC_actuatorF");
if (actuatorsF)
{
actuatorsF.position = [0,0,0];
}
var reargearextendPRT = this._findSubEntity("HPC_reargearPRT");
if (reargearextendPRT)
{
reargearextendPRT.position = [-17.351349,-17.408951,-3.749173];
}
var reargearextendSTB = this._findSubEntity("HPC_reargearSTB");
if (reargearextendSTB)
{
reargearextendSTB.position = [17.435233,-17.338642,-3.50929];
}
var frontgearextendPRT = this._findSubEntity("HPC_frontgearPRT");
if (frontgearextendPRT)
{
frontgearextendPRT.position = [-12.765342,-11.194131,27.818142];
}
var frontgearextendSTB = this._findSubEntity("HPC_frontgearSTB");
if (frontgearextendSTB)
{
frontgearextendSTB.position = [12.765342,-11.194131,27.818142];
}
}
// functions to switch the navlights on and off
this.switchFlashersOff = function()
{
this.ship.lightsActive = false; // turns flashers off
}
this.switchFlashersOn = function()
{
this.ship.lightsActive = true; // turns flashers on
}
// when ship enters station Aegis, it will turn on its nav flashers:
this.shipEnteredStationAegis = function(station)
{
this.switchFlashersOn();
if (this.ship.dockingInstructions)
{
this.ship.commsMessage("Received dockinginstructions NOW");
this.retractGear();
this.extendGear();
}
if (this.ship == player.ship && this.ship.target && this.ship.target.isStation)
{
this.ship.target.explode();
}
}
// when ship exits station Aegis, it will turn off its nav flashers:
this.shipExitedStationAegis = function(station)
{
this.switchFlashersOff();
}
// if ship is docked, extend gear
this.shipDockedWithStation = function(station)
{
this.switchFlashersOn();
this.retractGear();
this.extendGear();
}
// if the ship launches, show gear deployed and automatically retract after 10 seconds
this.shipLaunchedFromStation = function(station)
{
this.extendGear();
this.tenSecTimer = new Timer(this, this.retractGear,10);
this.ship.commsMessage("GEAR UP");
this.switchFlashersOn();
}
// if player hits C key with docking computers active gear drops
/*
this.playerStartedAutoPilot = function()
{
this.ship.commsMessage("Autopilot ON");
this.ship.commsMessage("GEAR DOWN & LOCKED");
this.switchFlashersOn();
this.retractGear();
this.extendGear();
}
*/
// if player cancels docking computers gear retracts
this.playerCancelledAutoPilot = function()
{
this.extendGear();
this.retractGear();
this.ship.commsMessage("Autopilot OFF");
this.ship.commsMessage("GEAR UP");
}
// same as shipLaunchedFromStation function
this.shipLeavingPlanetSurface = function(planet)
{
this.extendGear();
this.tenSecTimer = new Timer(this, this.retractGear,10);
this.ship.commsMessage("GEAR UP");
}
this.shipApproachingPlanetSurface = function(planet)
{
this.retractGear();
this.extendGear();
this.ship.commsMessage("GEAR DOWN & LOCKED");
}
// still need to test this !
this.shipWillEnterWormhole = function()
{
this.switchFlashersOff();
}
// untested, but should work
this.stationWithdrewDockingClearance = function()
{
this.extendGear();
this.retractGear();
}
this.shipWillLaunchFromStation = function(station)
{
this.retractGear();
this.extendGear();
}
/*
if(this.ship.target = system.mainStation && this.ship.position.distanceTo(this.ship.target) < 11000)
{
this.retractGear();
this.extendGear();
}
*/
The log is mute, and I do not get the desired effect with either script... the only thing that works is the flashers when player ship enters the aegis.
Any help ?
Re: Scripters cove
Posted: Sun Aug 17, 2014 2:21 pm
by cim
Comments added throughout these code blocks. As a more general point, looking at your definition of what the
retractGear
and
extendGear
functions do, you don't need to call them both in succession -
retractGear
will set the gear to the retracted position regardless of what position it was previously in.
Code: Select all
this.shipEnteredStationAegis = function(station)
{
this.switchFlashersOn();
// CIM: This if() will always be false. The ship is at this point 51.2km from the station
// and incapable of having already received docking instructions. You'd have to
// activate the autopilot from outside the aegis to get this to work, which isn't possible.
if (this.ship.dockingInstructions)
{
this.ship.commsMessage("Received dockinginstructions NOW");
this.retractGear();
this.extendGear();
}
// CIM: Likewise, at 51.2km range, the player will not have the station targeted,
// so this is always false too.
if (this.ship == player.ship && this.ship.target && this.ship.target.isStation)
{
this.ship.target.explode();
}
}
// when ship exits station Aegis, it will turn off its nav flashers:
this.shipExitedStationAegis = function(station)
{
this.switchFlashersOff();
}
// if ship is docked, extend gear
this.shipDockedWithStation = function(station)
{
// CIM: this should work. You won't see it happen, of course, since an NPC will have disappeared
// and you won't be in a position to get an external view of your own ship either.
this.switchFlashersOn();
this.retractGear();
this.extendGear();
}
// if the ship launches, show gear deployed and automatically retract after 10 seconds
this.shipLaunchedFromStation = function(station)
{
// CIM: this should also be working properly - gear retracted 10 seconds after the player
// gets control of the ship, or an NPC starts manoeuvring on station exit
this.extendGear();
this.tenSecTimer = new Timer(this, this.retractGear,10);
this.ship.commsMessage("GEAR UP");
this.switchFlashersOn();
}
// if player hits C key with docking computers active gear drops
/*
this.playerStartedAutoPilot = function()
// CIM: this should work if it wasn't commented out
{
this.ship.commsMessage("Autopilot ON");
this.ship.commsMessage("GEAR DOWN & LOCKED");
this.switchFlashersOn();
this.retractGear();
this.extendGear();
}
*/
// if player cancels docking computers gear retracts
this.playerCancelledAutoPilot = function()
{
// CIM: this one should work
this.extendGear();
this.retractGear();
this.ship.commsMessage("Autopilot OFF");
this.ship.commsMessage("GEAR UP");
}
// same as shipLaunchedFromStation function
this.shipLeavingPlanetSurface = function(planet)
{
// CIM: this one should work, but note you need to be crossing the 500m-above-surface line
this.extendGear();
this.tenSecTimer = new Timer(this, this.retractGear,10);
this.ship.commsMessage("GEAR UP");
}
this.shipApproachingPlanetSurface = function(planet)
{
// CIM: this one should work, but note you need to be crossing the 500m-above-surface line
// which requires extreme caution to avoid a collision
this.retractGear();
this.extendGear();
this.ship.commsMessage("GEAR DOWN & LOCKED");
}
// still need to test this !
this.shipWillEnterWormhole = function()
{
// CIM: this will work, but [icode]shipWillEnterWitchspace[/icode] works better
this.switchFlashersOff();
}
// untested, but should work
this.stationWithdrewDockingClearance = function()
{
// CIM: should work.
this.extendGear();
this.retractGear();
}
this.shipWillLaunchFromStation = function(station)
{
// CIM: should work.
this.retractGear();
this.extendGear();
}
Re: Scripters cove
Posted: Sun Aug 17, 2014 2:33 pm
by dertien
Yes I did test the extend and retract with planetfall and both work.
Another little question I have is this:
I would like to switch the nav flashers off once a ship has entered a wormhole, or has exited one:
however:
Code: Select all
this.shipExitedWormhole = function()
{
this.switchFlashersOff();
}
or
Code: Select all
this.playerWillEnterWitchspace = function()
{
this.switchFlashersOff();
}
Does not have the desired effect since nav flashers are still active ?