Page 30 of 117

Re: Scripters cove [hope I'm not hijaking this thread . .]

Posted: Fri Oct 07, 2011 3:00 pm
by Smivs
UK_Eliter wrote:
Hi everyone

Can anyone tell me, please, if/how I can detect whether a ship (specifically: the player) has initiated an ECM pulse?

Cheers.

If you pressed the right button, you probably did :wink:

Re: Scripters cove

Posted: Fri Oct 07, 2011 4:49 pm
by UK_Eliter
Smivs! :roll: This forum is not called 'player's cove'!

I'm trying to write a little OXP that fortifies the ECM system. For that I need to be able to detect by script whether the player's ECM is on (although, ideally, I'd allow some NPCs to have the equipment in question, too).

Re: Scripters cove

Posted: Fri Oct 07, 2011 5:50 pm
by Capt. Murphy
It doesn't appear to be documented in the wiki but in the source code I've seen a reference to a ship script event handler.. this.shipHitByECM(pulsesRemaining) .. which I think may be a starting point. It's noted in the release notes for 1.71 aswell.

Edit to add - and also used by Svengali in one of his Vector Scripts...vector_richman.js

Re: Scripters cove

Posted: Fri Oct 07, 2011 6:17 pm
by Eric Walch
Capt. Murphy wrote:
It doesn't appear to be documented in the wiki
Good spotted. Since today it is in the wiki as: shipHitByECM.
It was probably missed because it is in a whole different part of the code and not in ship- or player-entity. :?

Re: Scripters cove

Posted: Fri Oct 07, 2011 9:17 pm
by Svengali
Capt. Murphy wrote:
Edit to add - and also used by Svengali in one of his Vector Scripts...vector_richman.js
Yep. The Vector is using this handler.

But to decide if the ECM pulse comes from the player you'll have to combine it with other checks - e.g. checking players energy level, etc. If I remember right the calculated value of the energy drain is not a constant value and other stuff (like the cloak or hit by lasers) will change that level as well.

If your plan is just a communication you could also use Cabal_Common_Comms to have more control.
Eric Walch wrote:
Since today it is in the wiki as: shipHitByECM.
Thanks a lot Eric.

Re: Scripters cove

Posted: Tue Oct 11, 2011 8:07 pm
by Zieman
Is there any way an NPC could 'notice' player, using just JS (ship script?) ?

By 'notice' I mean to make it react (for example send comms message "hello") to player presence (player inside scanner range) or player action (player targetting said NPC for ID or missile).

Re: Scripters cove

Posted: Tue Oct 11, 2011 8:37 pm
by Thargoid
Yes, just use a periodic scan (driven by a timer for example) with a filter that the entity isPlayer and a range of 25600 (or whatever you want). The result of the scan will be an array of either zero length (if the player isn't in range) or 1 entity long (if they are).

You can adjust things by changing the filtering on the scan (e.g. entity.isPlayer && entity.target && entity.target === this.ship) for the player being in range and having the scanning ship under target lock.

If you want an example of such scanning, look in the probe missile script in Armoury, plus several other OXPs as well as it's quite a common technique.

Re: Scripters cove

Posted: Tue Oct 11, 2011 8:37 pm
by Eric Walch
Zieman wrote:
Is there any way an NPC could 'notice' player, using just JS (ship script?) ?
For that you need a timer that regularly does the check. Easiest is doing is with a script call from an AI because AI.plist already has a timer through UPDATE.

When you want to do it exclusively from JS you must create your own timer:

Code: Select all

this.shipSpawned = function ()
{
	this.checkPlayerTimer = new Timer(this, this.checkPlayer, 20, 10);
}

this.checkPlayer = function ()
{
	// do your checking here. e.g.:
	if (player.ship.position && player.ship.position.distanceTo(this.ship) < this.ship.scannerRange) this.foo();
}

this.entityDestroyed = function ()
{
    if (this.checkPlayerTimer) {this.checkPlayerTimer.stop(); delete this.checkPlayerTimer};
}
The last lines is to remove the timer again after the ship is removed from the system.

Re: Scripters cove

Posted: Wed Oct 12, 2011 7:06 pm
by Zieman
Wow, thanks guys!

Saves me countless hours of trying to figure out stuff from the wiki and endless try-think-retry-rethink -cycle...

Re: Scripters cove

Posted: Tue Nov 08, 2011 5:42 pm
by Zieman
Another question:
I have a code snippet in a ship script that is supposed to prevent said (pirate) ship attacking player if player has bounty:

Code: Select all

this.shipTargetAcquired = function(target)
	{
		if (target.isPlayer && player.bounty > 0) this.ship.target = null;
	}
It even seems to be working, but every now and the I get log errors conserning this code (line 37 is the if -line):

Code: Select all

19:17:54.031 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (Far Arm ships Dart script 3.0): TypeError: target is null
19:17:54.031 [script.javaScript.exception.unexpectedType]:       ../AddOns/Far_Arm_ships_v3.0_beta4.oxp/Scripts/fa-dart.js, line 37.
I presume my coding is again incomplete, even if the tiniest possibility of a possible Oolite bug might exist ?

Re: Scripters cove

Posted: Tue Nov 08, 2011 5:57 pm
by Thargoid
More likely the target is acquired and then immediately lost for some reason. Just change your code to

Code: Select all

if (target && target.isPlayer && player.bounty > 0) this.ship.target = null;
and you'll be covered in this case. It's good practice to do anyway (I should know, I keep falling over this one myself!)[/color]

Re: Scripters cove

Posted: Tue Nov 08, 2011 6:00 pm
by Eric Walch
Zieman wrote:
Another question:
I have a code snippet in a ship script that is supposed to prevent said (pirate) ship attacking player if player has bounty:

Code: Select all

this.shipTargetAcquired = function(target)
	{
		if (target.isPlayer && player.bounty > 0) this.ship.target = null;
	}
The problem is probably that the target is already invalid when the code triggers. Better to double check the target really exist by using

Code: Select all

this.shipTargetAcquired = function(target)
	{
		if (target && target.isPlayer && player.bounty > 0) this.ship.target = null;
	}
Edit: ninjad by Thargoid.

Re: Scripters cove

Posted: Tue Nov 08, 2011 6:04 pm
by Zieman
Wow, that was quick!

Thank you again, both of you!

Re: Scripters cove

Posted: Sun Nov 13, 2011 1:56 pm
by Okti
Hi all,

I am a bit confused about shipGroup. If I add a leader and more ships to the group, does the others use the escortAI for the leader, or can they have seperate AI's like a special escortAI?

Re: Scripters cove

Posted: Sun Nov 13, 2011 2:01 pm
by Thargoid
They can use whatever AI you want (the ship group won't make any changes to AI).

Have a look at the Thargoid armada group in TCAT for an example of it's use (it is a little convoluted).