Scripts/pirateCove.js |
"use strict";
this.name = "Pirate_Coves";
this.author = "lazygun (JS adaption by EW)";
this.copyright = "lazygun";
this.description = "Adds pirate coves to the system.";
this.version = "1.4.2";
this.startUp = function ()
{
// restore the systems were a pirate cove is missing
if (missionVariables.pirate_cove_systems)
{
this.pirateCoveSystems = JSON.parse(missionVariables.pirate_cove_systems);
}
else
{
this.pirateCoveSystems = new Array(256);
}
this.resetTimer = new Timer(this, this.shipExitedWitchspace, 0.1); // already add station before first launch
this.basicDefenders = Math.round(Math.log(1 + player.score));
};
this.playerWillSaveGame = function()
{
// encode the systems were a pirate cove is missing
missionVariables.pirate_cove_systems = JSON.stringify(this.pirateCoveSystems);
}
this.playerEnteredNewGalaxy = function ()
{
// reset the systems
this.pirateCoveSystems = new Array(256);
}
this.shipExitedWitchspace = function ()
{
// Test for [System.countShipsWithRole("pirate-cove") == 0] because the populater might already have added them as rock hermit replacement.
if (!system.isInterstellarSpace && system.countShipsWithRole("pirate-cove") === 0 && !system.sun.hasGoneNova)
{
var isDead = this.pirateCoveSystems[system.ID];
if (isDead == undefined || (isDead && (isDead + 20 < clock.days)))
{
this.$initialiseSystem();
}
if (!this.pirateCoveSystems[system.ID])
{
this.$addRockHermit();
}
}
};
this.$initialiseSystem = function ()
{
// initialise
if (system.countShipsWithPrimaryRole("pirate") > 9)
{
this.pirateCoveSystems[system.ID] = false; // allow stations untill killed.
}
else
{
this.pirateCoveSystems[system.ID] = clock.days; // don't allow a station for the next 20 days.
}
}
this.$addRockHermit = function ()
{
var random = this.$random(); // use a 'fixed' random to make it predictable by system.
var coordinateSystem = (random< 0.1) ? "ps" : "wp";
var station = system.addShipsToRoute("rockhermit", 1, 0.10+random*0.3, coordinateSystem)[0];
station.orientation = [1,0,0,0]; // orientate it along the z-axis like oolite hermits.
var asteroidCount = Math.ceil(3 + random*8);
system.addShips("asteroid", asteroidCount, station.position, 15000);
if (station.scanClass == "CLASS_POLICE") return; // e.g. the astromine from commies.oxp, that should not be changed.
station.setAI("pirateCoveAI.plist");
station.setScript("pirateCoveRock.js");
station.hasNPCTraffic = false; // we don't want trader launches. Happens otherwise by default on rotating hermits.
station.script.defenderCount = this.basicDefenders + Math.floor(Math.random()*3);
if (station.scanClass == "CLASS_NEUTRAL")
{
// not all hermits have a scanclass defined.
if (0 >= oolite.compareVersion("1.77"))
{
station.scanClass = "CLASS_ROCK";
}
else
{
// scanClass is read-only in older Oolite versions.
station.scannerDisplayColor1 = "whiteColor";
}
}
};
this.$random = function ()
{
// return mostly the same value, but change over time.
var salt = (clock.days & 0xE0) + 15; // change once every 32 days
return system.scrambledPseudoRandomNumber(salt);
}; |
Scripts/pirateCoveRock.js |
"use strict";
this.name = "Pirate Cove Rock";
this.author = "eric walch";
this.description = "Script for the Pirate_Cove Rock by Lazy Gun";
this.version = "1.4.2";
this.defenderCount = 6; // defenderCount is redefined by the main script.
this.shipSpawned = function ()
{
this.ship.primaryRole = "pirate-cove"; // needed to set the market correct.
this.maxAirbornDefenders = 3 + Math.floor(Math.random() * 4);
if (player.score > 512) this.maxAirbornDefenders++;
if (player.score > 6400) this.maxAirbornDefenders++;
}
// Correct clean hits.
this.shipBeingAttacked = function (attacker)
{
if (attacker && attacker.owner === this.ship && attacker.target === this.ship)
{
attacker.reactToAIMessage("TARGET_LOST");
log(this.name, "Pirate cove was friendly hit by: " + attacker.displayName);
}
}
this.stationLaunchedShip = function (ship)
{
if (ship.primaryRole === "defense_ship") ship.switchAI("pirateCovePirateAI.plist");
if (ship.primaryRole === "scavenger" && !ship.bounty) ship.bounty = 5 + Math.random() * 25;
if (ship.primaryRole === "escort" && !ship.bounty) ship.bounty = 5 + Math.random() * 15;
}
this.otherShipDocked = function (ship)
{
if (ship.isPlayer) this.ship.displayName = "Pirate Cove"; // we are spotted!
if (ship.primaryRole == "pirate") this.defenderCount++;
}
/*
In addition to the below launched pirates, the AI can also launch pirates as defense ships
when directly attacked.
*/
this.$launchDefender = function ()
{
if (this.defenderCount > 0 && this.ship.target
&& this.ship.position.distanceTo(this.ship.target.position) < 15000
&& this.ship.group.count < this.maxAirbornDefenders)
{
var defender = this.ship.launchShipWithRole("pirate");
if (defender)
{
defender.switchAI("pirateCovePirateAI.plist");
defender.target = this.ship.target;
this.ship.group.addShip(defender);
defender.group = this.ship.group;
if (!this.ship.bounty && this.ship.target.isPlayer)
{
this.ship.bounty = (this.ship.scanClass == "CLASS_NEUTRAL") ? 100: 1000; // the cove is no longer clean now.
}
this.defenderCount--;
}
}
};
this.shipDied = function ()
{
// store the time of death
worldScripts.Pirate_Coves.pirateCoveSystems[system.ID] = clock.days;
}; |