"use strict";
this.name = "smShip";
this.author = "Eric Walch";
this.copyright = "may2008";
this.description = "Stolen Military ships";
this.version = "1.02";

this.isHelper = false; // set true by WorldScript in end mission.

//-------------------------------------------------------------------------------------------------------------
this.spawnedAsEscort = function () {
    this.isHelper = true;
    this.mom = system.shipsWithPrimaryRole("leviathan-pirate")[0] // define mother ship on spawning
}

//-------------------------------------------------------------------------------------------------------------
this.shipSpawned = function () {
    if (this.isHelper) this.ship.switchAI("smShipDefenceAI.plist")

    if (this.ship.primaryRole == "defense_ship") {
        this.ship.AIState = "CLEAR_LAUNCHING_BAY" // Missed "LAUNCHED OKAY" message on AI switching
        this.mom = system.shipsWithPrimaryRole("leviathan-pirate")[0] // define mother ship on launch
    }
    if (missionVariables.military_fiasco_killcount > 150 && Math.random() < 0.5) this.ship.fuel = 70
    delete this.shipSpawned;
}

//-------------------------------------------------------------------------------------------------------------
this.shipDied = function (who) {
    if (who && who.isPlayer) {
        missionVariables.military_fiasco_killcount++
        missionVariables.military_fiasco_progresscount++
        if (this.isHelper) missionVariables.military_fiasco_helpers--
        if (worldScripts.GalCopBB_Reputation) {
            var rep = worldScripts.GalCopBB_Reputation;
            rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_galcop]"), system.ID, "Success", 0.1, 1);
            rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_localgov]"), system.ID, "Success", 0.1, 1);
            rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_navy]"), system.ID, "Success", 1, 1);
        }
    }
}

//-------------------------------------------------------------------------------------------------------------
this.shipEnergyIsLow = function () {
    if (this.ship.subEntities.length == 0 && Math.random() < 0.05 && !this.mom) this.ship.reactToAIMessage("JUMP_AWAY")
}

//-------------------------------------------------------------------------------------------------------------
this.checkTarget = function () {
    // prefent targetting his own kind when player hits the Leviathan from outside his scanner range or cloacked hit.
    if (this.ship.target
        && (this.ship.target.primaryRole == "smship" || this.ship.target.primaryRole == "leviathan-pirate"
            || this.ship.target.primaryRole == "defense_ship")) {
        if (this.mom && this.mom.target) this.ship.target = this.mom.target
        else this.ship.AIState = "TRAVEL_TO_LURK_AREA"
    }
}

//-------------------------------------------------------------------------------------------------------------
this.checkCloakedTarget = function () {
    if (this.ship.target && !this.ship.target.isCloaked) this.ship.reactToAIMessage("TARGET_DECLOAKED")
}

//-------------------------------------------------------------------------------------------------------------
// check mother to prevent it forcing in stop mode for docking.
this.checkMother = function () {
    if (this.mom) {
        if (this.mom.AIState == "ATTACK_TARGET") this.ship.reactToAIMessage("MOM_BUSY")
        else this.ship.reactToAIMessage("MOM_IDLE")
    }
}

//-------------------------------------------------------------------------------------------------------------
this.findMom = function () {
    if (this.mom) {
        this.ship.target = this.mom
        this.ship.reactToAIMessage("FOUND_MOM")
    }
}
