| Scripts/carriers-turret.js | "use strict";
this.name        = "carriers-turret";
this.author      = "Norby";
this.copyright   = "2015 Norbert Nagy";
this.description = "Place turret inside of the shields and send message if a turret gone.";
this.licence     = "CC BY-NC-SA 4.0";
this.$Carriersturret_HitTimer = null;
//this.shipBeingAttacked = function(whom) {
this.shipTakingDamage = function(amount, whom, type) {
	if(amount<=0) return;//player shield can take the whole damage
//if(this.ship.isPlayer) {var a=Math.round(amount); var e=Math.round(this.ship.energy);}//debug
	if(this.ship.scriptInfo.hardarmour > 0) { //Hard Armour support
		var d = Math.min(this.ship.scriptInfo.hardarmour, amount);
		if( this.ship.energy > 5 ) this.ship.energy += d; //hardly explode without if
		amount -= d;
	}
//if(this.ship.isPlayer) player.commsMessage(a+" "+Math.round(amount)+" "+e+" "+Math.round(this.ship.energy));//debug
	if(amount<=0) return;//extra check after armor
	var energydamage = amount;
	var owner = this.ship.owner;
	if(owner.isValid && !this.ship.scriptInfo.noturretshield
		&& !owner.scriptInfo.noturretshield) {//can disable turret shield in shipdata.plist
		var angle = 3.14;//unknown attacker assumed in front
		if(whom && whom.isValid)
			angle = owner.vectorForward.angleTo(owner.position.subtract(whom.position));
		if(owner.isPlayer) {
			if(angle > 1.57) {//forward
				if(energydamage > owner.forwardShield) { //hit the shield first
					this.ship.energy += owner.forwardShield;
					energydamage -= owner.forwardShield;
					owner.forwardShield = 0;
				} else {
					if(owner.isPlayer) var d = Math.round(this.ship.energy);//debug
					this.ship.energy += energydamage;
					if(this.ship.isPlayer) player.console(this.ship.name+" "+d+" "+Math.round(this.ship.energy)
						+" "+Math.round(amount));//debug					
					owner.forwardShield -= energydamage;
					energydamage = 0;
				}
			} else { //aft
				if(energydamage > owner.aftShield) {
					this.ship.energy += owner.aftShield;
					energydamage -= owner.aftShield;
					owner.aftShield = 0;
				} else {
					this.ship.energy += energydamage;
					owner.aftShield -= energydamage;
					energydamage = 0;
				}
			}
		} else if(worldScripts["customshields"]) { //NPC, check if CustomShields installed
			if(angle < 1.57) {
				if(energydamage > owner.script.customshieldsaftshieldlevel) {
					this.ship.energy += owner.script.customshieldsaftshieldlevel;
					energydamage -= owner.script.customshieldsaftshieldlevel;
					owner.script.customshieldsaftshieldlevel = 0;
				} else {
					this.ship.energy += energydamage;
					owner.script.customshieldsaftshieldlevel -= energydamage;
					energydamage = 0;
				}
			} else {
				if(energydamage > owner.script.customshieldsforwardshieldlevel) {
					this.ship.energy += owner.script.customshieldsforwardshieldlevel;
					energydamage -= owner.script.customshieldsforwardshieldlevel;
					owner.script.customshieldsforwardshieldlevel = 0;
				} else {
					this.ship.energy += energydamage;
					owner.script.customshieldsforwardshieldlevel -= energydamage;
					energydamage = 0;
				}
			}			
		}
	}
	if(energydamage > 0 && owner.isPlayer) {
		if(!this.$Carriersturret_HitTimer)  { 
			//delayed show the energy of the turret after hit to avoid too many messages
			this.$Carriersturret_HitTimer = new Timer(this, this.$Carriersturret_Hit, 1);
		}
	}
}
this.$Carriersturret_Pos = function() {
	var pos = "";
	if(this.ship.name === "Main Turret") pos+="Main ";//Main Turret always placed in Front
	else if(this.ship.position.z > 2)  pos+="Fore ";
	if(this.ship.position.z < -105)  pos+="Aft "; //side and top turrets are 104.85m behind the center
	if(this.ship.position.x > 2)  pos+="Port ";
	if(this.ship.position.x < -2)  pos+="Starboard ";
	if(this.ship.position.y > 2)  pos+="Up ";
	if(this.ship.position.y < -2)  pos+="Down ";
	return(pos);
}
this.$Carriersturret_Hit = function() {
	player.consoleMessage(this.$Carriersturret_Pos()+"Turret "+Math.ceil(this.ship.energy)+" armor left.");
	if(this.$Carriersturret_HitTimer) {
		this.$Carriersturret_HitTimer.stop();
		delete this.$Carriersturret_HitTimer;
	}
}
this.shipDied = function () {
	if(this.ship.owner === player.ship) {
		player.commsMessage(this.$Carriersturret_Pos()+"Turret gone.");
	}
};
 |