Page 1 of 1

Getting a ship to fire ECM on behalf of another ship

Posted: Mon Jul 10, 2017 10:03 pm
by UK_Eliter
I can't recall - or perhaps I never knew - how to do the following. I've got some ships in a group. Only one of them has an ECM. I want the one with an ECM to fire it when it, or one of its fellows, is attacked with a missile. How do do this? None of the ships has a script but only instead a (PLIST) AI. Thanks.

EDIT: I haven't set any of the ships as a group leader.

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 12:18 am
by Norby
Imho attach a ship script to all ships (either in shipata.plist or calling ship.setScript()) then within shipAttackedWithMissile handler you can call ship.fireECM() on the ECM-equipped ship.

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 1:08 am
by UK_Eliter
Thanks very much Norby. However, that approach seems a fair amount of work, both computationally (i.e. it will be a bit expensive CPU-wise) and for me, especially since I'm doing it only really for a gimic (with, er, interstellar versions of Witchspace-Worshipping Lobsters, and in an already rather over-extended OXP, namely my Interstellar Tweaks). Is there no easier way, involving an AI?

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 7:39 am
by Norby
This is not not CPU expensive because called only at the event of missile launch and the ECM-equipped ship should be stored into a variable when created.
The AI way is similar. For example:

Code: Select all

"INCOMING_MISSILE" = (fireECM, fightOrFleeMissile, setTargetToPrimaryAggressor, "sendScriptMessage: _requestECM");
Then you must make a _requestECM function in a ship_script_ecm.js:

Code: Select all

this._requestECM = function _requestECM() { 
    var ship = this.$shipWithECM;
    if( ship && ship.isValid) ship.fireECM();
}
When you create the ships you should set $shipWithECM in each ship script. If you create them using system.addShips then do this:

Code: Select all

var ships = system.addShips("unique-rule-or-datakey-of-ships", 6, player.ship.position, 20000);
    for(var i=0; i<ships.length; i++) {
        if(i == 0) { //give ECM to the first ship
            ships[i].awardEquipment("EQ_ECM");
        }
        ships[i].setScript("ship_script_ecm.js"); //will call for friendly ECM when attacked by missile
        ships[i].script.$shipWithECM = ships[0]; //save the first ship which hold ECM
    }
}

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 3:32 pm
by UK_Eliter
OK, I've implemented that and it works. Thank you very much, Norby. (You were in the acknowledgements of my OXP already.)

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 5:12 pm
by UK_Eliter
There's something else I'd like to do, but I fear it would be complicated. It's this: I have ships that attack the player if he is not a fugitive; but I'd like them to attack him, also, if he is a fugitive but has previously fired on one of them. (If he does fire on them, they will respond; but they can get distracted by picking up loot or fighting other ships; it's when they return to seeking victims that I'd like them to recall that he - the player - fired on them (if he did).

Any ideas? EDIT: OK, I've got it all working!

Perhaps I should do this: get the ships to store whether the player has fired on them, via the

Code: Select all

shipTakingDamage
event handler (or is there a better, more specific handler), and then check the stored variable when seeking targets. I'd use, in the targetting bit, something like this:

Code: Select all

this.$isPotentialTarget = function(e) {
	return
	e.isShip && !e.isCloaked && e.isPiloted 
	&&
	(
		(
			( e.isPirateVictim || e.hasRole("hiredGuns_escort") || e.isPlayer ) && ( e.bounty < 50 )
		)
		||
		(
			e.isPlayer && this.ship.script.$playerHasAttackedPirates
		)
	)
}
(though sadly I have to strip out the readable formatting for such a thing to work).

EDIT: OK, I've got it all working!

Re: Getting a ship to fire ECM on behalf of another ship

Posted: Tue Jul 11, 2017 8:05 pm
by Norby
I think this way is good, just set the HasAttacked variable in all ship scripts within the pirate group.