"use strict";
this.name = "smCarrier";
this.author = "Eric Walch";
this.copyright = "Feb2008";
this.description = "Captured Leviathan";
this.version = "1.02";

//-------------------------------------------------------------------------------------------------------------
this.shipSpawned = function () {
    this.spareEnergy = (missionVariables.military_fiasco_killcount > 40) ? missionVariables.military_fiasco_killcount * 10 : 0;
    this.oldBounty = this.ship.bounty;
    delete this.shipSpawned;
}

//-------------------------------------------------------------------------------------------------------------
this.shipDied = function () {
    missionVariables.military_fiasco = "MIL_LEVI_DESTROYED";
    if (worldScripts.GalCopBB_Reputation) {
        var rep = worldScripts.GalCopBB_Reputation;
        rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_galcop]"), system.ID, "Success", 1, 1);
        rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_localgov]"), system.ID, "Success", 1, 1);
        rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_navy]"), system.ID, "Success", 1, 1);
    }
    mission.setInstructionsKey("mission_military_desc4", "military_fiasco");
}

//-------------------------------------------------------------------------------------------------------------
this.otherShipDocked = function (ship) {
    if (ship.isPlayer) player.consoleMessage(expandMissionText("fiasco_dead_in_minutes"), 3);
}

//-------------------------------------------------------------------------------------------------------------
this.stationLaunchedShip = function (ship) {
    if (ship.isPlayer) ship.fuel = 0;
}

//-------------------------------------------------------------------------------------------------------------
this.clearBounty = function () {
    // don't attract police, only player.
    if (this.ship.bounty !== 0) {
        this.oldBounty = this.ship.bounty;
        this.ship.bounty = 0;
    }
}

//-------------------------------------------------------------------------------------------------------------
this.setBounty = function () {
    if (this.ship.bounty === 0) this.ship.bounty = this.oldBounty;
    if (!this.alreadyMet && !player.ship.isCloaked) {
        this.ship.commsMessage(expandMissionText("fiasco_revenge"));
        this.alreadyMet = true;
    }
    this.callTroops();
}

//-------------------------------------------------------------------------------------------------------------
this.callTroops = function () {
    var smships = system.shipsWithPrimaryRole("smship");
    for (var i = 0; i < smships.length; i++)
        if (smships[i].isValid) //could have already been destroyed
        {
            if (!(smships[i].target && smships[i].target === this.ship.target)) {
                smships[i].target = this.ship.target;
                smships[i].reactToAIMessage("ATTACK_MAIN_TARGET");
            }
        }
    smships = system.shipsWithRole("smship-defender");
    for (var i = 0; i < smships.length; i++)
        if (smships[i].isValid) //could have already been destroyed
        {
            smships[i].target = this.ship.target;
            smships[i].AIState = "ATTACK_SHIP";
        }
}

//-------------------------------------------------------------------------------------------------------------
this.shipBeingAttacked = function () {
    if (this.ship.energy < this.ship.maxEnergy * 0.5 && this.spareEnergy > 0) {
        this.ship.commsMessage(expandMissionText("fiasco_energy_banks"))
        if (this.spareEnergy > 500) {
            this.spareEnergy -= 500;
            this.ship.energy += 500;
        }
        else {
            this.ship.energy += this.spareEnergy;
            this.spareEnergy = 0;
        }
    }
}

//-------------------------------------------------------------------------------------------------------------
this.witchpoint = function () {
    this.direction = "witchpoint";
    this.clearBounty();
}

//-------------------------------------------------------------------------------------------------------------
this.planet = function () {
    this.direction = "planet";
    this.clearBounty();
}

//-------------------------------------------------------------------------------------------------------------
this.whichDirection = function () {
    this.ship.commsMessage(expandMissionText("fiasco_exhaust_trails"));
    if (this.direction === "witchpoint") this.ship.reactToAIMessage("GO_TO_WITCHPOINT")
    else this.ship.reactToAIMessage("GO_TO_PLANET");
}