| Config/script.js | "use strict";
this.author = "phkb";
this.copyright = "© 2021 phkb.";
this.licence = "CC-BY-NC-SA 4.0";
this.name = "RemoveIndividualPylon";
this.description = "Script for removing an individual pylon-mounted weapon";
this._lastChoice = "";
//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function() {
    if (worldScripts.EmailSystem) {
        worldScripts.GalCopAdminServices._purchase_ignore_equip.push("EQ_IND_MISSILE_REMOVAL");
    }
}
//-------------------------------------------------------------------------------------------------------------
this.playerBoughtEquipment = function (equipmentKey) {
    if (equipmentKey == "EQ_IND_MISSILE_REMOVAL") {
        player.ship.removeEquipment("EQ_IND_MISSILE_REMOVAL");
        this.$showMissileRemovalScreen();
    }
}
//-------------------------------------------------------------------------------------------------------------
this.$showMissileRemovalScreen = function $showMissileRemovalScreen() {
    var curChoices = {};
	var itemcount = 0;
    var p = player.ship;
    var text = "Select the pylon mounted weapon you want to sell and remove:";
	var pagesize = 17;
	if (this.$isBigGuiActive() === true) pagesize = 23;
    // any missiles?
    if (p.missileCapacity > 0) {
        var flag = false;
        try {
            var miss = p.missiles;
            for (var i = 0; i < miss.length; i++) {
                curChoices["01_MISSILE_" + (i < 10 ? "0" : "") + i + "~" + i] = {text:" -- " + EquipmentInfo.infoForKey(miss[i].equipmentKey).name, alignment:"LEFT"};
                itemcount += 1;
            }
            if (itemcount == 0) text += "\n\nNo pylon mounted weapons found.";
            flag = true;
        } catch (err) {
            text += "\n\nComputer system malfunction - unable to access pylons!";
            if (this._debug) log(this.name, "!!ERROR: " + err);
        }
        if (flag === true) {
        }
    } else {
        text += "\n\nShip has no pylons.";
    }
    for (var i = 0; i < ((pagesize + 1) - itemcount); i++) {
        curChoices["90_SPACER_" + i] = "";
    }
    var def = "99_EXIT";
    if (this._lastChoice != "") def = this._lastChoice;
    curChoices["99_EXIT"] = {text: "Return"};
    
    var opts = {
        screenID: "oolite-sellpylon-map",
        title: "Sell Pylon Mounted Weapon",
        exitScreen: "GUI_SCREEN_EQUIP_SHIP",
        choices: curChoices,
        initialChoicesKey: def,
        message: text
    };
    mission.runScreen(opts, this.$screenHandler, this);
}
//-------------------------------------------------------------------------------------------------------------
this.$screenHandler = function $screenHandler(choice) {
    if (!choice) return;
    if (choice.indexOf("01_MISSILE") >= 0) {
        var idx = parseInt(choice.substring(choice.indexOf("~") + 1));
        var eq = player.ship.missiles[idx].equipmentKey;
        var info = EquipmentInfo.infoForKey(eq);
        player.ship.removeEquipment(eq);
        player.credits += parseInt(info.price / 10);
        player.consoleMessage("You have been refunded " + formatCredits(parseInt(info.price / 10), false, true));
        // reset the last choice so we auto-select the next missile, if there is one to select
        if (player.ship.missiles.length = idx) idx -= 1;
        if (player.ship.missiles.length = 0) {
            this._lastChoice = "";
        } else {
            this._lastChoice = "01_MISSILE_" + (idx < 10 ? "0" : "") + idx + "~" + idx;
        }
    }
    if (choice != "99_EXIT") this.$showMissileRemovalScreen();
}
//-------------------------------------------------------------------------------------------------------------
// returns true if a HUD with allowBigGUI is enabled, otherwise false
this.$isBigGuiActive = function $isBigGuiActive() {
	if (oolite.compareVersion("1.83") <= 0) {
		return player.ship.hudAllowsBigGui;
	} else {
		var bigGuiHUD = ["XenonHUD.plist", "coluber_hud_ch01-dock.plist"]; // until there is a property we can check, I'll be listing HUD's that have the allow_big_gui property set here
		if (bigGuiHUD.indexOf(player.ship.hud) >= 0) {
			return true;
		} else {
			return false;
		}
	}
}
 |