| Config/script.js | "use strict";
this.name        = "AutoPrimeEquipment";
this.author      = "phkb";
this.copyright   = "2017 phkb";
this.description = "Automatic priming of equipment when current MFD changes.";
this.licence     = "CC BY-NC-SA 4.0";
// preloaded with some examples
this._config = {
    "CommsLogMFD":"EQ_COMMSLOGMFD",
    "DamageReportMFD":"EQ_DAMAGE_REPORT_MFD",
    "BroadcastCommsMFD":"EQ_BROADCASTCOMMSMFD",
    "RangeFinderMFD":"EQ_GCM_RANGE_FINDER_MFD",
    "ManifestScanner":"EQ_MANIF_SCANNER",
    "inquirer_mfd":"EQ_MARKET_INQUIRER_MFD",
    "Waypoint_Here_MFD":"EQ_WPH_ASC_UPGRADE",
    "escortdeck":"EQ_ESCORTDECK",
    "telescope":"EQ_TELESCOPE"
};
this._unprime = "";
//-------------------------------------------------------------------------------------------------------------
// public function to add extra config items
// mfdID = the ID of the MFD (required)
// eqKey = the equipment key for the equipment item to be primed when the MFD is selected
this.$addConfig = function(mfdID, eqKey) {
    if (mfdID === "") return;
    if (eqKey === "" && mfdID !== "") {
        delete this._config[mfdID];
    } else {
        this._config[mfdID] = eqKey;
    }
}
//-------------------------------------------------------------------------------------------------------------
this.shipWillLaunchFromStation = function() {
    // special case for escort deck, which has two different equipment items, depending on ship size
    if (player.ship.equipmentStatus("EQ_ESCORTDECKXL") === "EQUIPMENT_OK") {
        this.$addConfig("escortdeck", "EQ_ESCORTDECKXL");
    } else {
        this.$addConfig("escortdeck", "EQ_ESCORTDECK");
    }
}
//-------------------------------------------------------------------------------------------------------------
this.selectedMFDChanged = function(index) {
    var equipkey = this._config[player.ship.multiFunctionDisplayList[index]];
    if (equipkey) this.$primeEquipment(equipkey); 
    else this.$unprimeEquipment();
}
//-------------------------------------------------------------------------------------------------------------
this.mfdKeyChanged = function(index, key) {
    var equipkey = this._config[key];
    if (equipkey) this.$primeEquipment(equipkey);
    else this.$unprimeEquipment();
}
//-------------------------------------------------------------------------------------------------------------
this.playerChangedPrimedEquipment = function(equipmentKey) {
    // if the player manually changes the primed equipment, but we have an unprime value set, update it
    if (equipmentKey !== "" && this._unprime !== "" && player.ship.equipmentStatus(equipmentKey) === "EQUIPMENT_OK") this._unprime = equipmentKey;
}
//-------------------------------------------------------------------------------------------------------------
this.$primeEquipment = function(equipmentKey) {
    var p = player.ship;
    // only prime equipment if it's installed and undamaged
    if (p.equipmentStatus(equipmentKey) !== "EQUIPMENT_OK") return;
    // check the currently primed equipment item
    if (p.hasOwnProperty("primedEquipment")) {
        var check = p.primedEquipment;
        // if it's not one of the ones in our config, make a note of it so we can switch back to it later
        if (!this.$equipmentInConfig(check)) this._unprime = p.primedEquipment;
    }
    p.setPrimedEquipment(equipmentKey);
}
//-------------------------------------------------------------------------------------------------------------
this.$unprimeEquipment = function() {
    if (this._unprime !== "") {
        player.ship.setPrimedEquipment(this._unprime);
        this._unprime = "";
    }
}
//-------------------------------------------------------------------------------------------------------------
this.$equipmentInConfig = function(equipmentKey) {
    var k = Object.keys(this._config);
    for (var i = 0; i < k.length; i++) {
        if (this._config[k[i]] === equipmentKey) return true;
    }
    return false;
} |