Page 1 of 4
OXP idea: Escape capsule locator
Posted: Fri Sep 10, 2010 8:51 pm
by alocritani
Hi,
I found really simple to change the color the escape capsule is drawn in scanner, by changing the shipdata.plist.
But I'd like to create an additional equipment providing the same option, using Javascript.
I know how to create the equipment and that I can use the scannerdisplaycolor1(2) properties to set the colors, but I have no idea about how to say to Oolite to assign that color to the EscapeCapsule.
In other words, how should I write the javascript?
My idea is something like (pseudo-code)
"if equipment is installed and working, when new ship is added, if its name/type/role is EscapeCapsule, then scannerdisplaycolor1=... else retain normal (white) color" but I'm not sure this is the right way and which function to call.
Can someone suggest me something or implement this so I can read how to do that? I've no interest in this idea's credits or something similar, but I'm really interested in understanding the way to do this thing.
Thanks a lot
Re: OXP idea: Escape capsule locator
Posted: Fri Sep 10, 2010 9:55 pm
by another_commander
alocritani wrote:Hi,
I found really simple to change the color the escape capsule is drawn in scanner, by changing the shipdata.plist.
But I'd like to create an additional equipment providing the same option, using Javascript.
I know how to create the equipment and that I can use the scannerdisplaycolor1(2) properties to set the colors, but I have no idea about how to say to Oolite to assign that color to the EscapeCapsule.
In other words, how should I write the javascript?
My idea is something like (pseudo-code)
"if equipment is installed and working, when new ship is added, if its name/type/role is EscapeCapsule, then scannerdisplaycolor1=... else retain normal (white) color" but I'm not sure this is the right way and which function to call.
Can someone suggest me something or implement this so I can read how to do that? I've no interest in this idea's credits or something similar, but I'm really interested in understanding the way to do this thing.
Thanks a lot
Let's assume that the equipment you have is called "EQ_ESCAPE_CAPSULE_LOCATOR" and that your OXP is called EscapePodLocator.oxp. You need a world script. Let's call it escapePodLocator.js. Inside that, you put:
Code: Select all
this.shipSpawned = function(ship)
{
if (ship.primaryRole == "escape-capsule" && player.ship.equipmentStatus("EQ_ESCAPE_CAPSULE_LOCATOR") == "EQUIPMENT_OK")
{
ship.scannerDisplayColor1 = "grayColor"; // or any [r, g, b, a]
ship.scannerDisplayColor2 = "orangeColor"; // or any [r, g, b, a]
}
}
This escapePodLocator.js file, containing the above function, goes inside EscapePodLocator.oxp/Scripts.
Now you need to tell Oolite that this is a world script, i.e. it is applicable to player and all ships that are spawned. To do this, save a file called world-scripts.plist inside EscapePodLocator.oxp/Config, containing the following:
this.shipSpawned is an event handler called in the worldscript for any and all ships at the moment they are spawned. Inside this handler is the code that handles the scanner colors of the escape pod. In order to get Oolite to recognize it as a world script, you must declare it in the second file (world-script.plist).
I hope this is clear enough to get you started. If not, just ask. Quick and dirty version of the OXP is here (link will be valid for 15 days):
http://terrastorage.ath.cx/Marmagka/01e ... or.oxp.zip
Posted: Sat Sep 11, 2010 7:23 am
by alocritani
Thanks a lot, I've seen your OXP and it's exactly what I was looking for.
if I understand correctly, if I want to apply my script only to my ship I should create a script and refer to it in shipdata.plist while if the script should apply to all ships it should be referred in word-scripts.plist
But now I've another question: if the equipment become damaged, how can I change back the color? I know I can use ship.scannerDisplayColor1=null to bring back the original color, but how can I change the value for every ship?
My idea is, always in escapePodLocator.js:
Code: Select all
this.shipBeingAttacked = this.shipBeingAttackedByCloaked = function()
{
if(player.ship.equipmentStatus("EQ_ESCAPE_CAPSULE_LOCATOR") == "EQUIPMENT_DAMAGED")
{
for (escape_capsule in system.shipsWithRole("escape-capsule")){
escape_capsule.scannerDisplayColor1=null;
escape_capsule.scannerDisplayColor2 = null;
}
}
}
what do you think? Do I have to define escape_capsule in some way in order to let the system know it's a ship entity?
thanks again!
Posted: Sat Sep 11, 2010 8:21 am
by Thargoid
Have a look at my OXP Target Autolock Plus (via the links in my sig below). That recolours the scanner lollipop of your current target, so is along similar lines.
You can refer to the script in the shipdata.plist script of the players ship, but that will then only work for that ship (and for an OXP you'd have to supply the ship). By using the world-scripts.plist file that A_C mentioned it will work for any player ship. World scripts are essentially scripts run on the current player, regardless of what ship they are flying.
And for your code below, that will only trigger if a ship (cloaked or otherwise) attacks you (the player), and in that case would reset the escape capsules lollipops to default. That said for (escape_capsule in system.shipsWithRole("escape-capsule")) isn't valid javascript, you need something like:
Code: Select all
this.capsules = system.shipsWithRole("escape-capsule");
if(this.capsules.length > 0)
{
let i=0;
for(i=0;i<this.capsules.length;i++)
{
this.capsules[i].scannerDisplayColor1 = null;
this.capsules[i].scannerDisplayColor2 = null;
}
}
or something along those lines (above code is untested but along the right lines). this.capsules is an array formed of all ships in the system that have the role "escape-capsule". If there are any (ie the array has non-zero length) then the loop steps through them in turn and makes the change. There are other ways to do it that act on all items in the array, but personally I prefer loops as they are clearer.
Look over my OXP, that should give you a framework on doing things, or examine A_C's.
Posted: Sat Sep 11, 2010 8:24 am
by another_commander
I think only the shipSpawned event is recognized in world scripts. Unless something changed recently and I missed it, acting on all ships is possible only with shipSpawned. Basically, world scripts are performed on events happening from the player side, while ship specific scripts are, well, specific to the ships they refer to. In your case, shipBeingAttacked will be executed only when the player is attacked, because there is no generic handling for all ships for this event.
However, there is the
equipmentDamaged handler, which can be inserted in escapePodLocator.js. Once you detect that your pod locator is damaged, you can use your code to reset colors. Haven't checked it, but it should be
Code: Select all
this.equipmentDamaged = function (eqKey)
{
if (eqKey == "EQ_ESCAPE_CAPSULE_LOCATOR")
{
... rest of your code ...
}
}
Posted: Sat Sep 11, 2010 8:30 am
by Thargoid
Personally I'd either use a timer that's started on launch and stopped on docking/equipment damage and the sort of scan I gave previously. But that's a little more complex than the code above.
Posted: Sat Sep 11, 2010 8:37 am
by another_commander
I agree, the timer is probably the best solution for things like this, also because a script could repair this equipment while in flight. In such case, maybe the timer should be stopped only when docked.
Posted: Sat Sep 11, 2010 9:18 am
by Commander McLane
Oh! I had no idea that shipSpawned in a worldscript is triggered for NPCs as well. The
documentation isn't very clear about it. Could one of you improve it according to what the handler actually does?
Also, IMO the parameter shouldn't be named "whom", because that sounds like it would contain the entity by whom the ship was spawned, which is misleading and often wouldn't make sense anyway. I think it should simply be renamed to "ship".
Posted: Sat Sep 11, 2010 9:43 am
by Thargoid
It's listed in ship script events too, which technically would be the NPC version (although it's the same function).
For me the converse was true, that shipSpawned actually fires for the player ship in place of this.startUp if the script is linked to the player ship via shipdata.plist. If you are using the script for a specific player ship then you have to use shipSpawned instead (as I had to do last night for a new WIP player ship).
That said I'd never noticed the whom parameter either until you mentioned it. But for me it doesn't seem to do anything at all - I would agree it should return the entity that spawned the ship.
Posted: Sat Sep 11, 2010 10:14 am
by Eric Walch
Commander McLane wrote:Oh! I had no idea that shipSpawned in a worldscript is triggered for NPCs as well. The
documentation isn't very clear about it. Could one of you improve it according to what the handler actually does?
Also, IMO the parameter shouldn't be named "whom", because that sounds like it would contain the entity by whom the ship was spawned, which is misleading and often wouldn't make sense anyway. I think it should simply be renamed to "ship".
Its a new feature added to 1.74. Before, the shipSpawned event was only send to the shipScript but since 1.74 it is in addition also send to the worldScript.
I think that this fact has never put in the wiki. And reading the history of that page is a bit confusing because it shows that the handler was there for ages. Truth is that it used to be a page for both world & ship scripts. At some point it was split in two versions but the info was never updated accordingly.
I just edited the text a bit to make clear its a new feature in 1.74 and changed the "whom" to "ship" as suggested.
Re: OXP idea: Escape capsule locator
Posted: Thu Jul 21, 2011 8:51 am
by Zireael
Would it be possible to have this OXP available again?
Re: OXP idea: Escape capsule locator
Posted: Thu Jul 21, 2011 9:47 am
by another_commander
The OXP in preliminary, barely tested form (actually, not tested at all with 1.75.3), has been uploaded here:
http://www.box.net/shared/hngzh2jmx67royok2npo
I do not intend to maintain it, anyone who is willing to take it and develop it further, feel free to do so.
Re: OXP idea: Escape capsule locator
Posted: Thu Jul 21, 2011 11:17 am
by Wildeblood
I just added beacon="e";
to the escape capsules' shipdata when I wanted to make them easier to find.
Re: OXP idea: Escape capsule locator
Posted: Thu Jul 21, 2011 12:54 pm
by Capt. Murphy
You could very easily adapt the code from Police IFF Scanner to do this.
It's basically doing exactly the same thing but looking at ships with a bounty.
Link in my signature.
Re: OXP idea: Escape capsule locator
Posted: Thu Jul 21, 2011 1:01 pm
by Gimi
Capt. Murphy wrote:You could very easily adapt the code from Police IFF Scanner to do this.
It's basically doing exactly the same thing but looking at ships with a bounty.
Link in my signature.
I think that a Police Scanner should have this functionality in there by default. The Police have a duty to protect as well, and identifying an assisting people in distress falls within their realm.
Didn't there use to be an OXP with a ship that ran around doing that. Would make a good role for Staer9's Chopped Cobra. Oolite ambulance and rescue service running around with Chopped Cobras (Red Cross on each wing) with Role Police but scoping escape pods and bringing them back to the main station. Would need a separate AI. I'm going to dive into the Wiki to have a look.
Edit: Found it, Cobra Clipper SAR
here