| Scripts/gsagostinho_cobra_mk4_externalMissiles.js | "use strict";
this.name				= "gsagostinho_cobra_mk4_externalMissiles.js";
this.author				= "Thargoid";
this.copyright			= "Creative Commons: attribution, non-commercial, sharealike.";
this.description		= "Generic external missile launching script";
this.version			= "1.01";
this.shipSpawned = function()
	{ // just to ensure ship is fully loaded with selected missile type and nothing else
	if(this.ship.scriptInfo.missileRole) // missileRole should be defined in shipdata.plist
		{ this.missileRole = this.ship.scriptInfo.missileRole; }
	else
		{ this.missileRole = "EQ_MISSILE"; } // default to standard missile if not
    
    if (this.ship.scriptInfo.initialMissiles)
        {this.initialMissiles = parseInt(this.ship.scriptInfo.initialMissiles);}
    else
        {this.initialMissiles = this.ship.missileCapacity;};
    
	if (this.ship.missiles.length > 0) this.ship.awardEquipment("EQ_MISSILE_REMOVAL"); // remove all spawning missiles and restock with selected ones.
	var addCounter = 0; 
	for(addCounter = 0; addCounter < this.initialMissiles;addCounter++)
		{ this.ship.awardEquipment(this.missileRole); }
	}
this.shipFiredMissile = function(missile, target)
	{
	if(!this.ship.subEntities || this.ship.subEntities.length === 0) {return}; // if we've run out of sub-ents before we run out of missiles
	
	var subCounter = this.ship.subEntities.length - 1; // Set counter to number of sub-ents minus 1 (as entity array goes up from zero)
	for(subCounter = this.ship.subEntities.length - 1;subCounter>=0;subCounter--)
		{
		if(this.ship.subEntities[subCounter].hasRole(missile.primaryRole)) // if the sub-ent is the same as the missile being fired
			{
			missile.position = this.localToGlobal(this.ship.subEntities[subCounter].position); // move the fired missile to the sub-ent position
			missile.orientation = this.ship.subEntities[subCounter].orientation.multiply(this.ship.orientation); // point the missile in the right direction
			missile.desiredSpeed = missile.maxSpeed;
			this.ship.subEntities[subCounter].remove(); // remove the sub-ent version of the missile
			break; // come out of the loop, as we've done our swap
			}
		}
	}
this.localToGlobal = function(position)
	{ // sub-ent position is relative to mother, but for swapping we need the absolute global position
	let orientation = this.ship.orientation;
	return this.ship.position.add(position.rotateBy(orientation));
	}
this.shipTakingDamage = function(amount, fromEntity, damageType)
	{
	if(this.ship.missiles.length === 0 && (!this.ship.subEntities || this.ship.subEntities.length === 0)) // if we're all out of missiles and any sub-entities, bail out.
		{ return; }
	
	this.missileSubs = 0;
	var subCounter = this.ship.subEntities.length - 1; // Set counter to number of sub-ents minus 1 (as entity array goes up from zero)
	for(subCounter = this.ship.subEntities.length - 1;subCounter>=0;subCounter--)
		{
		if(this.ship.subEntities[subCounter].hasRole(this.missileRole)) // if the sub-ent is a missile, count it
			{ this.missileSubs++; }
		}
	
	if(this.missileSubs === 0 && this.ship.subEntities.length === 0) // if we're all out of missiles and missile sub-entities, bail out.
		{ return; }
	
	if(this.missileSubs < this.ship.missiles.length) // if we've got more missiles than sub-entity missiles
		{
		this.ship.awardEquipment("EQ_MISSILE_REMOVAL"); // get rid of all missiles
		if(this.missileSubs > 0)
			{
			var missileCounter = 0; 
			for(missileCounter = 0;missileCounter<this.missileSubs;missileCounter++) // restock with the correct number of selected missile
				{ this.ship.awardEquipment(this.missileRole); }
			}
		return;	
		}
		
	if(this.missileSubs > this.ship.missiles.length) // if we've got less missiles than sub-entity missiles
		{
		this.difference = this.missileSubs - this.ship.missiles.length;
		var removeCounter = 0; 
		for(removeCounter = 0;removeCounter<this.difference;removeCounter++) // loop through however many subs we need to remove
				{
				var subCounter = this.ship.subEntities.length - 1; // Set counter to number of sub-ents minus 1 (as entity array goes up from zero)
				for(subCounter = this.ship.subEntities.length - 1;subCounter>=0;subCounter--)
					{
					if(this.ship.subEntities[subCounter].hasRole(this.missileRole)) // if the sub-ent is a missile, remove it
						{ 
						this.ship.subEntities[subCounter].remove(); 
						break;
						}
					}
				}
		return;		
		}	
	}
	
this.shipDied = function() // event that occurs when the entity (ship) whose script this is dies
	{
	if(!this.ship || (player.ship.isValid && this.ship.position.distanceTo(player.ship.position) > 50000)) return; // exit the script if the explosion happens far from the player
 		this.largeCount = (Math.ceil(Math.random() * 4)); // between 0-4 pieces of wreckage
		this.tinyCount =  (Math.ceil(Math.random() * 10) + 5); // between 5-10 tiny spark fragments
		this.sparkCount =  (Math.ceil(Math.random() * 20) + 10); // between 10-20 spin spark fragments
		this.ship.spawn("griffspark", this.tinyCount); // spawn the tiny spark fragments
	if(this.largeCount > 0) // if there is large wreckage
		{
		this.ship.spawn("griffwreckage", this.largeCount); // spawn the larger wreckage
		this.ship.spawn("spinspark", this.sparkCount); // spawn the spin sparks
		}
	}	
 |