| Config/script.js | "use strict";
this.name = "MilitaryShields_Ships";
this.author = "Rustem";
this.copyright = "2018";
this.description = "Script for spawn of the military ships without N-Shields OXP";
this.licence = "CC-BY-SA 4.0";
this.version = "0.2";
//---- Logging ----
this.logging = false;
this.debugging = null; // used in check for special event. 
//---- Start Setting ----
this.NEU_multiplier = parseFloat(EquipmentInfo.infoForKey("EQ_MMS_NPC_NAVAL_ENERGY_UNIT").scriptInfo.oolite_energy_recharge_multiplier);
this.EEU_multiplier = parseFloat(EquipmentInfo.infoForKey("EQ_MMS_NPC_NAVAL_ENERGY_UNIT").scriptInfo.oolite_energy_recharge_multiplier);
// Shield Boosters
this.SB_shields = 2 * parseInt(EquipmentInfo.infoForKey("EQ_SHIELD_BOOSTER").scriptInfo.oolite_shield_increase);
this.SB_multiplier = parseFloat(EquipmentInfo.infoForKey("EQ_SHIELD_BOOSTER").scriptInfo.oolite_shield_recharge_multiplier);
// Military Shield Enhancement, Shield Enhancers
this.MSE_shields = 2 * parseInt(EquipmentInfo.infoForKey("EQ_MMS_NPC_SHIELD_ENHANCER").scriptInfo.oolite_shield_increase);
this.MSE_multiplier = parseFloat(EquipmentInfo.infoForKey("EQ_MMS_NPC_SHIELD_ENHANCER").scriptInfo.oolite_shield_recharge_multiplier);
this.NEG_shields = 2 * parseInt(EquipmentInfo.infoForKey("EQ_MMS_NPC_NAVAL_ENERGY_GRID").scriptInfo.oolite_shield_increase);
this.NEG_multiplier = parseFloat(EquipmentInfo.infoForKey("EQ_MMS_NPC_NAVAL_ENERGY_GRID").scriptInfo.oolite_shield_recharge_multiplier);
// or read from ship script_info
//---- WorldScript Event Handlers ----
this.startUp = function () {
	if (worldScripts.NShields) this.$npc_shields = true;
	if (worldScripts.NPC_Energy_Units) this.$npc_units = true;
	// if (worldScripts.ShipConfiguration_NPCShips) { // TODO: if the Naval Energy Unit will damaged
	// 	this.NPC_equipmentDamagedHandlers.push(this._shipconf_equipmentDamaged);
	// }
	if (worldScripts.NPC_Equipment_Damage) { // for NPC_Equipment_Damage v0.2.2
		worldScripts.NPC_Equipment_Damage.NPC_equipmentDamagedHandlers.push(this.NPC_energy_equipmentDamaged);
	}
	if (this.$npc_shields) {
		delete this.NPC_shield_equipmentDamaged;
	} else if (worldScripts.NPC_Equipment_Damage) { // for NPC_Equipment_Damage v0.2.2
		worldScripts.NPC_Equipment_Damage.NPC_equipmentDamagedHandlers.push(this.NPC_shield_equipmentDamaged)
	}
}
this.shipSpawned = function (ship) {
	if (!this.$shipHasShields(ship)) {
		return;
	}
	if (ship.energyRechargeRate < 7) return; // ship has opportunity for enchancements: is simulated NEU? // rate for v0.1: 10 // v0.2: 7
	//if (!ship.isPiloted || ship.isStation || ship.isThargoid || ship.energyRechargeRate > 5 || !ship.autoWeapons) return;
	//log(this.name, ship.name + " has recharge: " + ship.energyRechargeRate);
	// Setup of the Energy Units:
	this.$configureNPCUnits(ship); // if not intalled NPC Energy Units OXP
	// Setup of the Shields Equipments:
	this.$configureNPCShields(ship);
}
//---- Ship Setup ----
this.$shipHasShields = function (ship) {
	if (ship.scriptInfo && ship.scriptInfo.npc_shields == "no") return false;
	return (ship.isPiloted && !(ship.primaryRole == "escape-capsule") && !ship.isStation) // isPiloted rules out buoys, rocks, weapons and debris
}
this.$configureNPCUnits = function (ship) {
	if ((this.$npc_units && (ship.equipmentStatus("EQ_NPC_ENERGY_UNIT") === "EQUIPMENT_OK" || // EQ_ENERGY_UNIT ?
			ship.equipmentStatus("EQ_NPC_NAVAL_ENERGY_UNIT") === "EQUIPMENT_OK" || // EQ_NAVAL_ENERGY_UNIT ?
			worldScripts.NPC_Energy_Units._alreadyGotOne[ship.dataKey]))) return;
	//||
	//ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_GRID") === "EQUIPMENT_OK" ||
	//ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_UNIT") === "EQUIPMENT_OK") return;
	// Get info about initial equipment status
	var sc = ship.script;
	if (!sc.$enhrs) sc.$enhrs = 0; // TODO : may be will deleted, used in damage state
	if (!sc.$grids) sc.$grids = 0; // TODO : may be will deleted, used in damage state	
	var grids_boost = 0;
	var recharge = ship.energyRechargeRate;	
	var sci = ship.scriptInfo;	
	if (sci) {
		if (sci.recharge) recharge = parseFloat(sci.recharge);
		if (sci.max_shield_forward) grids_boost += parseInt(sci.max_shield_forward);
		if (sci.max_shield_aft) grids_boost += parseInt(sci.max_shield_aft);
		if (grids_boost > 0) sc.$grids = grids_boost
	}
	// initial recharge rate without MSE
	var ench = 0;
	if (ship.equipmentStatus("EQ_SHIELD_ENHANCER") === "EQUIPMENT_OK") { // MSE
		ench = 1;
		recharge = ship.energyRechargeRate / this.MSE_multiplier;
		//log(this.name, "MSE will be replace: " + ship.energyRechargeRate.toFixed(2) + " to " + recharge.toFixed(2));
	} else if (ship.equipmentStatus("EQ_NSHIELDS_NPC_NAVAL_SHIELD_BOOSTER") === "EQUIPMENT_OK") {
		ench = 2;
		recharge = ship.energyRechargeRate;
		//log(this.name, "NPC MSE will be upgrade: " + recharge.toFixed(2));
	}
	if (ench) {
		sc.$enhrs = this.MSE_multiplier; // : may be will deleted
		// Info for upgrading of the OXP: EEU or NEU simulated?		
		if (!grids_boost && recharge > 6.99) { //special event
			if (!this.logging) {
				this.logging = true;
				this.debugging = true;
			}
			log(this.name, "check shipdata to correctly sets a recharge rate[" + ship.energyRechargeRate.toFixed(2) + "] by MSE to " + ship.dataKey);
		}
	}
	// ship has NEU simulated or has MSE?
	if (recharge > 6.99) {
		if (ship.awardEquipment("EQ_MMS_NPC_NAVAL_ENERGY_UNIT")) {
			if (!this.$npc_shields) {
				if (grids_boost) ship.energyRechargeRate /= this.NEG_multiplier; // Adjustment for NEG
				if (recharge > 9.99) ship.energyRechargeRate /= this.MSE_multiplier; // Adjustment for dummy MSE				
			}
			if (this.logging) log(this.name, "Dummy NEU awarded to " + ship.name + ", has recharge: " + ship.energyRechargeRate.toFixed(2))
		} else log(this.name, "Fail NEU awarding to " + ship.name);
	} else return;
	// Addition to NPC Shield Enhancers for NEU simulated, avoiding an overfitting of the recharge rate
	if (!this.$npc_shields && ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_UNIT") === "EQUIPMENT_OK") {
		ship.removeEquipment("EQ_SHIELD_ENHANCER");
		if (ship.awardEquipment("EQ_MMS_NPC_SHIELD_ENHANCER")) {
			this.$shieldBoosterAdjustment(ship, "EQ_NAVAL_SHIELD_BOOSTER");
			if (this.logging) log(this.name, "MSE[NEU] was replaced: " + ship.energyRechargeRate.toFixed(2))
		}
	}
}
this.$configureNPCShields = function (ship) {
	//var recharge_rt = 0;
	var grids_boost = 0;
	var sc = ship.script;
	var sci = ship.scriptInfo;
	if (sci) { // Possible non-standard adjustments:
		//if (sci.recharge) recharge_rt = parseFloat(sci.recharge);
		if (sci.max_shield_forward) grids_boost += parseInt(sci.max_shield_forward);
		if (sci.max_shield_aft) grids_boost += parseInt(sci.max_shield_aft);
		// Replacement to NPC Shield Boosters for simulate correctly
		if (!this.$npc_shields && ship.equipmentStatus("EQ_SHIELD_BOOSTER") === "EQUIPMENT_OK") {
			ship.removeEquipment("EQ_SHIELD_BOOSTER");
			if (ship.awardEquipment("EQ_MMS_NPC_SHIELD_BOOSTER")) {
				this.$shieldBoosterAdjustment(ship, "EQ_SHIELD_BOOSTER");
				if (this.logging) log(this.name, "SB was replaced: " + ship.energyRechargeRate.toFixed(2))
			}
		}
		// Replacement to NPC Shield Enhancers for simulate correctly
		if (!this.$npc_shields && ship.equipmentStatus("EQ_SHIELD_ENHANCER") === "EQUIPMENT_OK") {
			ship.removeEquipment("EQ_SHIELD_ENHANCER");
			if (ship.awardEquipment("EQ_MMS_NPC_SHIELD_ENHANCER")) {
				this.$shieldBoosterAdjustment(ship, "EQ_NAVAL_SHIELD_BOOSTER");
				if (this.logging) log(this.name, "MSE was replaced: " + ship.energyRechargeRate.toFixed(2))
			}
		}
		// Addition a Naval Grids
		if (grids_boost > 0) {
			//log(this.name, "NEG will be awarded to " + ship.name);
			if (ship.awardEquipment("EQ_MMS_NPC_NAVAL_ENERGY_GRID")) { // ~ Naval Shield Boosters (EQ_NPC_SHIELD_BOOSTER, EQ_NPC_NAVAL_SHIELD_BOOSTER, EQ_NPC_SHIELD_ENHANCER)
				sc.$grids = grids_boost; // : may be will deleted			
				if (!this.$npc_shields) {
					this.$energyGridAdjustment(ship, "EQ_MMS_NPC_NAVAL_ENERGY_GRID");
				}
				if (this.logging) log(this.name, "NEG was awarded to " + ship.name + ", has recharge: " + ship.energyRechargeRate.toFixed(2));
				return
			} else log(this.name, "failed to award NEG to " + ship.name)
		}
	}
	// Info for upgrading of the OXP
	if (!sc.$enhrs) {		
		log(this.name, "no shields script info to award NEG to " + ship.name)
	} else if (this.debugging) {
		this.logging = false;
		this.debugging = null;
	}
}
this.$energyUnitAdjustment = function (ship, type, reverse) {
	let eq = EquipmentInfo.infoForKey(type);
	let recharge_boost = parseFloat(eq.scriptInfo.oolite_energy_recharge_multiplier);
	if (reverse) { // damaging/removing energy unit, not awarding
		if (ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_GRID") === "EQUIPMENT_OK") {
			if (this.$npc_shields && ship.equipmentStatus("EQ_NSHIELDS_NPC_NAVAL_SHIELD_BOOSTER") !== "EQUIPMENT_OK" || ship.equipmentStatus("EQ_MMS_NPC_SHIELD_ENHANCER") !== "EQUIPMENT_OK") {
				worldScripts.MilitaryShields_Ships.$energyGridAdjustment(ship, "EQ_MMS_NPC_NAVAL_ENERGY_GRID", true);
				ship.setEquipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_GRID", "EQUIPMENT_DAMAGED");
				//log(this.name, "NEU fail, damaging EQ_MMS_NPC_NAVAL_ENERGY_GRID on "+ship.displayName)				
			}
		}
		ship.energyRechargeRate /= recharge_boost
	} else {
		ship.energyRechargeRate *= recharge_boost
	}
}
this.$shieldBoosterAdjustment = function (ship, type, reverse) {
	//var shield_boost = this.shieldBoosters[type].shield_boost;
	//var recharge_boost = this.shieldBoosters[type].recharge_boost;
	let eq = EquipmentInfo.infoForKey(type);
	let shield_boost = parseInt(eq.scriptInfo.oolite_shield_increase);
	let recharge_boost = parseFloat(eq.scriptInfo.oolite_shield_recharge_multiplier);
	if (!recharge_boost) recharge_boost = 1; //just in case, to avoid NaN and divide-by-zero errors
	if (reverse) // damaging/removing shield booster, not awarding
	{
		if (type === "EQ_NAVAL_SHIELD_BOOSTER" && ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_GRID") === "EQUIPMENT_OK") {
			if (this.$npc_shields && ship.equipmentStatus("EQ_NSHIELDS_NPC_NAVAL_SHIELD_BOOSTER") !== "EQUIPMENT_OK" || ship.equipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_UNIT") !== "EQUIPMENT_OK") {
				worldScripts.MilitaryShields_Ships.$energyGridAdjustment(ship, "EQ_MMS_NPC_NAVAL_ENERGY_GRID", true);
				ship.setEquipmentStatus("EQ_MMS_NPC_NAVAL_ENERGY_GRID", "EQUIPMENT_DAMAGED");
				//log(this.name, "MSE fail, damaging EQ_MMS_NPC_NAVAL_ENERGY_GRID on "+ship.displayName)				
			}
		}
		if (this.$npc_shields) {
			ship.maxForwardShield -= shield_boost;
			ship.maxAftShield -= shield_boost;
			ship.forwardShieldRechargeRate /= recharge_boost;
			ship.aftShieldRechargeRate /= recharge_boost;
			if (ship.forwardShield > ship.maxForwardShield) ship.forwardShield = ship.maxForwardShield;
			if (ship.aftShield > ship.maxAftShield) ship.aftShield = ship.maxAftShield;
		} else {
			ship.maxEnergy -= 2 * shield_boost;
			ship.energyRechargeRate /= recharge_boost;
			if (ship.energy > ship.maxEnergy) ship.energy = ship.maxEnergy;
		}
	} else {
		if (this.$npc_shields) {
			ship.maxForwardShield += shield_boost;
			ship.maxAftShield += shield_boost;
			ship.forwardShieldRechargeRate *= recharge_boost;
			ship.aftShieldRechargeRate *= recharge_boost;
			ship.forwardShield = ship.maxForwardShield; // in case this was called after another script awarded equipment
			ship.aftShield = ship.maxAftShield;
		} else {
			ship.energy = ship.maxEnergy += 2 * shield_boost;
			ship.energyRechargeRate *= recharge_boost;
		}
	}
}
this.$energyGridAdjustment = function (ship, type, reverse) {
	let eq = EquipmentInfo.infoForKey(type);
	let shield_boost = parseInt(eq.scriptInfo.oolite_shield_increase); // 0.0
	let recharge_boost = parseFloat(eq.scriptInfo.oolite_shield_recharge_multiplier); // 1.5
	if (ship.script.$grids) { // setup by script info
		shield_boost = 0.5 * ship.script.$grids
	}
	if (!recharge_boost) recharge_boost = 1; //just in case, to avoid NaN and divide-by-zero errors
	if (reverse) // damaging/removing energy grid, not awarding
	{
		if (this.$npc_shields) {
			ship.energyRechargeRate /= recharge_boost;
			ship.forwardShieldRechargeRate /= recharge_boost;
			ship.aftShieldRechargeRate /= recharge_boost;
			if (ship.forwardShield > ship.maxForwardShield) ship.forwardShield = ship.maxForwardShield;
			if (ship.aftShield > ship.maxAftShield) ship.aftShield = ship.maxAftShield;
		} else {
			ship.maxEnergy -= 2 * shield_boost;
			ship.energyRechargeRate /= recharge_boost;
			if (ship.energy > ship.maxEnergy) ship.energy = ship.maxEnergy;
		}
	} else {
		if (this.$npc_shields) {
			ship.energyRechargeRate *= recharge_boost;
			ship.forwardShieldRechargeRate *= recharge_boost;
			ship.aftShieldRechargeRate *= recharge_boost;
			ship.forwardShield = ship.maxForwardShield; // in case this was called after another script awarded equipment
			ship.aftShield = ship.maxAftShield;
		} else {
			ship.energy = ship.maxEnergy += 2 * shield_boost;
			ship.energyRechargeRate *= recharge_boost;
		}
	}
}
//---- Handler for NPC Shields Damage ----
this.NPC_energy_equipmentDamaged = function (equipment) {
	if (equipment == "EQ_MMS_NPC_NAVAL_ENERGY_UNIT") { // TODO: compartibility with: equipment == "EQ_NPC_NAVAL_ENERGY_UNIT"
		worldScripts.MilitaryShields_Ships.$energyUnitAdjustment(this.ship, "EQ_MMS_NPC_NAVAL_ENERGY_UNIT", true); // for NPC_Equipment_Damage v0.2.2
		//return;
	}
	if (equipment == "EQ_MMS_NPC_NAVAL_ENERGY_GRID") {
		worldScripts.MilitaryShields_Ships.$energyGridAdjustment(this.ship, "EQ_MMS_NPC_NAVAL_ENERGY_GRID", true); // for NPC_Equipment_Damage v0.2.2
	}
}
this.NPC_shield_equipmentDamaged = function (equipment) {
	if (equipment == "EQ_MMS_NPC_SHIELD_BOOSTER") {
		worldScripts.MilitaryShields_Ships.$shieldBoosterAdjustment(this.ship, "EQ_SHIELD_BOOSTER", true); // for NPC_Equipment_Damage v0.2.2
	}
	if (equipment == "EQ_MMS_NPC_SHIELD_ENHANCER") {
		worldScripts.MilitaryShields_Ships.$shieldBoosterAdjustment(this.ship, "EQ_NAVAL_SHIELD_BOOSTER", true); // for NPC_Equipment_Damage v0.2.2
	}
} |