Re: Station Defense Minigame
Posted: Sun Jun 03, 2012 4:54 am
Missed a bracket:
Capt. Murphy wrote:if(station.hasRole("coriolisStationStationDefense")
)
For information and discussion about Oolite.
https://bb.oolite.space/
Capt. Murphy wrote:if(station.hasRole("coriolisStationStationDefense")
)
Code: Select all
if(!station)
{
spawnStation();
}
I don't think I understand at all what your question is.Commander Xvyto wrote:How do I ensure that a station is spawned even if the player quits the game and logs back out?
Code: Select all
this.shipWillExitWitchspace = function()
{
// your spawning code
}
shipWillExitWitchspace
is for all cases where the ship is supposed to be already there when the player comes in. If the NPC's entry shall be visible to the player, shipExitedWitchspace
is the better choice. Although this matters only for NPCs spawned at the witchpoint, therefore it doesn't apply to your station anyway.shipWillLaunchFromStation
and shipLaunchedFromStation
are for. Just make sure that those are only executed after restarting the game, but not if shipWillExitWitchspace
or shipWillExitWitchspace
have been executed previously. Checking for the presence of your ship first in your spawning code is a good way to do that.startUp
is a good place for doing things that need to be done immediately after loading the game, like getting missionVariables from the save-game and storing them in JS-variables, or checking for the presence of other world scripts and interacting with them. It's not a good place for spawning ships for two reasons: (a) Do you really need your ships to be spawned immediately? The player is still docked and can't watch them anyway. He won't notice a difference to anything spawned when he launches. And (b) what sense does it make to spawn your ship only in the system in which the player happens to be when (re-)loading a game, but nowhere else? For most situations this makes no sense at all. For instance, extra stations are in most cases supposed to be, well, stationary. They are supposed to be reliably present in the same system at the same place, regardless whether the player happened to load in that system or just to pass by. Just imagine: you jump into a system. Your station isn't there. You dock with the main station, save your game, and call it a day. In your next session (which means that in-game no time has passed at all) there is suddenly a huge station in the system that wasn't there even a second (in-game time) ago, just because the player outside the game world decided to save the game and to continue it on the next evening. This makes no sense at all from an in-game perspective.This is a strong case for usingCommander McLane wrote:Checking for the presence of your ship first in your spawning code is a good way to do that.
startUp
is a good place for doing things that need to be done immediately after loading the game, like getting missionVariables from the save-game and storing them in JS-variables, or checking for the presence of other world scripts and interacting with them. It's not a good place for spawning ships for two reasons:
startUp
instead of the launch functions - you can use startUp
and shipWillExitWitchspace
to place your ships without worrying if they're already there (because one or the other function will fire exactly one per system entry). Since 1.76 the system info is correctly in place when startUp
runs, so you can do the same checks for "is this the right system" that you would do on launch before deciding to add the station.startUp
will add the station before the player can possibly interact with it, so shipWillLaunchFromStation
is more efficient, but the efficiency gain is extremely marginal, so I think it's better to follow the easier coding practice, now that it can be done at all. I have seen bugs in a few OXPs with "addition on launch", even from experienced OXP authors.shipLaunchedFromStation
only fires when the player is granted flight control on exit from the docking tunnel, which is slightly too late to add ships, though if the ship is nowhere near the main station probably no-one will notice...What I meant was, if the player goes into the system with the special station, docks at the main station, saves, and exits oolite, the station won't still exist. This is because the station is spawned in this.shipExitedWitchspace. I was just wondering how I should add the code. I'm tryingI don't believe that this is what you wanted to ask. Can you try to rephrase?
Code: Select all
if(!system.shipsWithRole("coriolisStationStationDefense"))
{
system.addShips("coriolisStationStationDefense", 1, Vector3D(0, 0, 1.5).fromCoordinateSystem("psu"), 0);
}
countShipsWithRole()
instead which returns an integer, or check for system.shipsWithRole("coriolisStationStationDefense").length===0
Code: Select all
this.shipWillLaunchFromStation = function()
{
this.shipWillExitWitchspace();
delete this.shipWillLaunchFromStation;
}
Code: Select all
var killArray = system.shipsWithRole("coriolisStationStationDefense");
var i=0
var specKills = 0
for(i, i<=7, i++)
{
if( ! killArray[i].isValid() )
{
specKills++;
}
}
if(specKills==7)
{
// Do stuff
}
this.shipKilledOther = function(whom,damageType)
in a worldScript is the event handler that fires when the player makes a kill (except with some OXP weapons). http://wiki.alioth.net/index.php/Oolite ... illedOthervar
within a function limits the scope to the function) so each time the player kills something specKills will be reset to 0. If you need a variable to persist through a session and/or be accessible to several functions within the same script you should create it as an Object attached to your script (which is simple done by use of this.
). Some example code below.Code: Select all
this.shipKilledOther = function(whom,damageType)
{
if (whom.primaryRole !== "coriolisStationStationDefense"){return;}
if (!this.specKills) {this.specKills = 1;} else {this.specKills++;}
if (this.specKills >= 7)
{//do something}
}
shipDied
handler.shipDied
handler in one of the ship scripts is triggered, it can manipulate a property of your world script. It can even trigger a self-defined function in your world script. So you can do this:Code: Select all
this.startUp = function()
{
this.specKills = 0;
}
this.$checkSpecKills = function()
{
if(this.specKills >= 7)
{
// do stuff
}
}
this.$checkSpecKills
is not an official event handler, but a self-created function that you can use like an event handler by triggering it yourself. There's a convention to distinguish such self-created functions from "official" handlers by preceding their names with an "$". (This way you avoid the confusion that may arise if some day the developers create a new event handler that happens to be named exactly like a function you created yourself. This would result in your function being triggered each time the new handler fires, and not only if your code calls it, with predictably undesired results. However, the developers will never create a new handler that begins with "$", so you're safe if you use this "name space" for your own functions. Even if it's highly unlikely that there will ever be an event handler named "checkSpecKills", you should make this a habit from the get-go, because one day you may come up with a more generic sounding function name in one of your scripts.)script
key in shipdata, which takes the file name of the ship script as a value (including the ".js"). Your ship script would look like this:
Code: Select all
this.shipDied = function()
{
worldScripts["yourWorldScriptName"].specKills ++;
worldScripts["yourWorldScriptName"].$checkSpecKills();
}
this.name
property at the beginning of your world script. If it doesn't contain special characters like "-", ".", or " ", you can simplify it to worldScripts.yourWorldScriptName.specKills
.worldScripts["yourWorldScriptName"]
is what the this
inside the world script is actually standing for. So there is no difference between using this
inside a world script and worldScripts["worldScriptName"]
outside that script. This way each one of the running scripts can access and manipulate every other script.specKills
property in your world script, and to run the function in your world script that checks if it has already reached 7 or more. Now your stuff is done, independent of who killed the ships.Did this ever see the light of day?Commander Xvyto wrote: ↑Tue Jun 05, 2012 1:03 pmOk, thanks. That seems like it would work.
Edit: Tested, it does indeed work. Thanks again.