| Scripts/thargoid-ironhide.js | "use strict";
this.name			= "IronHide Armour Script";
this.author			= "Thargoid";
this.copyright		= "Creative Commons: attribution, non-commercial, sharealike with clauses - see readme.txt";
this.description	= "Script for IronHide armour, for player ship only";
this.version		= "3.06";
this.startUp = function()
{
	if(!missionVariables.ironHide_percentage)
		missionVariables.ironHide_percentage = 0; // float from 0 to 100 representing percentage of armour remaining
	else if (player.ship.equipmentStatus("EQ_IRONHIDE") !== "EQUIPMENT_OK")
		missionVariables.ironHide_percentage = 0; // 3.06: resolve inconsistency caused by other OXPs removing IronHide without setting percentage to zero
	missionVariables.ironHide_milFlag = missionVariables.ironHide_milFlag ? 1 : 0; // boolean 0 or 1 representing whether the armour is military-grade
	// strength represents total damage the armour can absorb (does not regenerate unless repaired), and is configurable in equipment.plist, so always reload it
	missionVariables.ironHide_strength = parseInt(EquipmentInfo.infoForKey(missionVariables.ironHide_milFlag ? "EQ_IRONHIDE_MIL" : "EQ_IRONHIDE").scriptInfo.ironHideStrength);
	// as of IronHide 3.01, ironHide_strength is no longer used, so it is only here for compatibility with other OXPs that look at the value
	// below is how much to charge per percentage of armour to be repaired, proportional to the price of a new set of civilian grade armour
	this.ironHideFactor = this.updateEquipmentPrice("EQ_IRONHIDE", parseInt(EquipmentInfo.infoForKey("EQ_IRONHIDE").price)) / 1000;
}
this.updateEquipmentPrice = function(equipmentKey, currentPrice)
{
	if (equipmentKey === "EQ_IRONHIDE" || equipmentKey === "EQ_IRONHIDE_MIL")
	{
		// calculate price proportional to ship surface area
		var adjust = player.ship.boundingBox.magnitude() / 157.30239913964633; // baseline is from a stock cobra3-player
		return (adjust > 1) ? Math.ceil(adjust * 2 * currentPrice) : currentPrice; // only adjust price upwards, don't discount for smaller ships
	}
	return currentPrice;
}
this.shipDockedWithStation = function(station)
{
	// below is how much to charge per percentage of armour to be repaired; necessary to recalculate after startUp because player may have changed ships
	this.ironHideFactor = this.updateEquipmentPrice("EQ_IRONHIDE", parseInt(EquipmentInfo.infoForKey("EQ_IRONHIDE").price)) / 1000;
}
// this.equipmentDamaged = function(equipment) // unnecessary since 3.01 because damage_probability is set to zero in equipment.plist
// {
	// if(equipment === "EQ_IRONHIDE")
	// {
		// player.ship.setEquipmentStatus("EQ_IRONHIDE","EQUIPMENT_OK"); // as it doesn't make sense for the armour to be inoperable-damaged
	// }
// }
this.shipLaunchedFromStation = function() // 3.05: moved from startUp to every launch in case another OXP used replaceShip and awardEquipment("EQ_IRONHIDE") and set milFlag to a different value
{
	// below is the multiplier for damage taken to determine how much armour percentage is lost (e.g., if dmgMult is 0.2 and 10 damage is taken, ironHide_percentage drops by 10 * 0.2 = 2)
	this.ironHideDamageMult = parseFloat(EquipmentInfo.infoForKey(missionVariables.ironHide_milFlag ? "EQ_IRONHIDE_MIL" : "EQ_IRONHIDE").scriptInfo.ironHide_percentArmourLostPerDamagePointTaken);
	
	if (missionVariables.ironHide_percentage > 0 && player.ship.equipmentStatus("EQ_IRONHIDE") !== "EQUIPMENT_OK")
		missionVariables.ironHide_percentage = 0; // 3.06: resolve inconsistency caused by other OXPs removing IronHide without setting percentage to zero
}
this.shipTakingDamage = function(amount, fromEntity, damageType)
{
	if(amount < 1) return; // all damage was absorbed by shields
	var ps = player.ship, percLeft = missionVariables.ironHide_percentage, dmgMult = this.ironHideDamageMult; // cache for performance
	if(percLeft > 0 && damageType === "energy damage") // have armour, check damageType instead of energy level to improve compatibility with other OXPs
	{
		if(percLeft > amount * dmgMult) // armour is sufficient to absorb all the damage
		{
			ps.energy += amount; // formula change: restore the amount absorbed by armour instead of ps.energy = ps.maxEnergy
			missionVariables.ironHide_percentage = percLeft - amount * dmgMult;
		}
		else // damage fully consumes remaining armour
		{
			ps.energy += percLeft / dmgMult; // whatever armour was left absorbed as much as it could
			player.consoleMessage("IronHide armour has been destroyed!", 8);
			ps.removeEquipment("EQ_IRONHIDE");
			missionVariables.ironHide_percentage = 0;
			missionVariables.ironHide_milFlag = 0;
			mission.setInstructionsKey(null);
		}
	}
}
this.guiScreenWillChange = function(newScreen, oldScreen)
{
	if(newScreen === "GUI_SCREEN_MANIFEST")
	{
		//if (equipmentStatus("EQ_IRONHIDE") === "EQUIPMENT_OK")
		if(missionVariables.ironHide_percentage > 0) // 3.06 replaced condition above to ensure armour visibility even if EQ_IRONHIDE is missing (e.g., removed by other OXPs without adjusting missionVariables)
			mission.setInstructions([expandMissionText(missionVariables.ironHide_milFlag ? "ironHideMil_short" : "ironHideCiv_short")]);
		else
			mission.setInstructionsKey(null);
	}
}
this.allowAwardEquipment = function(eqKey, ship, context)
{
	// if (context === "newShip" || context === "purchase") // equipment pre-installed on a ship in a station shipyard (F3 F3), or equipment for purchase on the F3 screen
	if (ship === player.ship) // 3.04: replaced conditions above, to allow scripted awardEquipment but still restrict to player-only (e.g., by Ship Storage Helper)
	{
		if (eqKey === "EQ_IRONHIDE") {
			if (player.ship.mass < 30000 && worldScripts["shipversion"]) return false; // ShipVersion OXP does not allow IronHide for ships with mass below 30t
			if (missionVariables.ironHide_percentage > 0) return false; // have armour, but missing equipment (can be caused by other OXPs removing equipment or changing IronHide mission variables)
			return true; // no restrictions on civilian grade armour
		} else if (eqKey === "EQ_IRONHIDE_MIL" && missionVariables.ironHide_percentage === 100 && missionVariables.ironHide_milFlag === 0) {
			if (player.ship.mass < 130000 && worldScripts["shipversion"]) return false; // ShipVersion OXP does not allow military IronHide for ships with mass below 130t
			return true; // only if your civilian armour is fully repaired and you don't already have military grade armour
		} else if (eqKey === "EQ_IRONHIDE_REPAIR" && missionVariables.ironHide_percentage < 100) {
			return true; // only if your armour is damaged
		}
	} else if (context === "scripted" && eqKey === "EQ_IRONHIDE") { // equipment added to NPC ships by scripts from other OXPs
		return true; // allow this for NPC ships assuming that another OXP has a good reason to do this (e.g., Ship Storage Helper using it as a placeholder for player->npc->player transitions)
	}
	// default is no in all other cases (IronHide does nothing for NPC ships, so don't allow the game to spawn NPCs with IronHide, and don't allow awarding the military or repair equipment to non-player ships because they are placeholders for triggering playerBoughtEquipment and are removed immediately after)
	return false;
}
this.playerBoughtEquipment = function(equipment)
{
	switch(equipment)
	{
		case("EQ_IRONHIDE"): // always available
			missionVariables.ironHide_milFlag = 0;
			missionVariables.ironHide_percentage = 100;
			this.dmgMult = parseFloat(EquipmentInfo.infoForKey(equipment).scriptInfo.ironHide_percentArmourLostPerDamagePointTaken);
			break;
		case("EQ_IRONHIDE_MIL"): // only available when armour is fully repaired (therefore ironHide_percentage is already 100 and does not need to be set here)
			missionVariables.ironHide_milFlag = 1;
			this.dmgMult = parseFloat(EquipmentInfo.infoForKey(equipment).scriptInfo.ironHide_percentArmourLostPerDamagePointTaken);
			player.ship.removeEquipment("EQ_IRONHIDE_MIL"); // only used as a placeholder for purchase
			break;
		case("EQ_IRONHIDE_REPAIR"): // only available when armour is damaged
			missionVariables.ironHide_cost = (100 - missionVariables.ironHide_percentage) * this.ironHideFactor;
			if(missionVariables.ironHide_mil === 1)
				missionVariables.ironHide_cost *= 1.5; // cheaper to repair than buying armour new (price * 2 vs. price * 1.5)
			player.ship.removeEquipment("EQ_IRONHIDE_REPAIR");
			if(player.credits < missionVariables.ironHide_cost)
				mission.runScreen({title: "Too Expensive", messageKey:"ironHide_tooExpensive"});
			else
				mission.runScreen({title: "Armour Repair", messageKey:"ironHide_repair", choicesKey:"ironHide_choices"}, this.choice);
			break;
		case("EQ_RENOVATION"):
			if(missionVariables.ironHide_percentage === 100 || player.ship.equipmentStatus("EQ_IRONHIDE") === "EQUIPMENT_UNAVAILABLE")
				break; // don't have armour or it's full-strength, so no repair needed during renovations
			else if(player.ship.equipmentStatus("EQ_IRONHIDE") === "EQUIPMENT_OK")
			{
				missionVariables.ironHide_cost = (100 - missionVariables.ironHide_percentage) * this.ironHideFactor;
				if(missionVariables.ironHide_milFlag === 1)
					missionVariables.ironHide_cost *= 1.5; // cheaper to repair than buying armour new (price * 2 vs. price * 1.5)
				if(missionVariables.ironHide_cost > 0 && missionVariables.ironHide_cost < player.credits)
					mission.runScreen({title: "Armour Repair", messageKey:"ironHide_renovationRepair", choicesKey:"ironHide_renovationChoices"}, this.choice);
			}
			break;
	}
}
this.choice = function(choice)
{
	switch(choice)
	{
		case "IRONHIDE_1_ACCEPT":
			// repair time is proportional to installation time of 12 hours but 25% faster (0.75 * 12 * 60 * 60 = 32400 seconds)
			clock.addSeconds(32400 * (100 - missionVariables.ironHide_percentage) / 100);
			player.credits -= missionVariables.ironHide_cost;
			missionVariables.ironHide_percentage = 100;
			break;
		case "IRONHIDE_1_RENOVATION_ACCEPT":
			// repair time is included in renovation time (convenient) even though cost is not
			player.credits -= missionVariables.ironHide_cost;
			missionVariables.ironHide_percentage = 100;
			break;
		case "IRONHIDE_2_REJECT":
			break;
	}
}
this.playerBoughtNewShip = this.shipLaunchedEscapePod = function()
{ // reset everything for a new ship, as armour isn't transferable or covered by insurance
	player.ship.removeEquipment("EQ_IRONHIDE");
	missionVariables.ironHide_percentage = 0;
	missionVariables.ironHide_milFlag = 0;
	mission.setInstructionsKey(null);
}
 |