| Scripts/customshields.js | 
"use strict";
this.name           = "customshields";
this.author         = "CommonSenseOTB";
this.copyright		= "Copyright Sept. 24, 2012 by CommonSenseOTB, licensed under Creative Commons: attribution, non-commercial, sharealike with clauses - see readme.txt";
this.description    = "Simulated shields, shields effects, damage effects";
this.version        = "0.83";
this.startUp = this.playerBoughtNewShip = function()
   {
   //
   //------------------YOU MAY CHANGE THESE DEFAULTS--------------
   //
   //----------------------------------------------------------------------------------------------------
   this.customshieldsshieldtype = "advanced";     //choices: "advanced" , "basic" , "off"
   //----------------------------------------------------------------------------------------------------
   this.customshieldsshieldtypethargoid = "basic";     //choices: "basic" , "off"
   //----------------------------------------------------------------------------------------------------
   this.customshieldsshieldcolor = "multicolor";     //choices: "multicolor" , "blue" , "pink"
   //----------------------------------------------------------------------------------------------------
   this.customshieldsshieldcolorthargoid = "green";     //choices: "green"
   //----------------------------------------------------------------------------------------------------
   this.customshieldsdamageeffects = "sparks";     //choices: "damage" , "blownpanels" , "sparks" , "off"
   //----------------------------------------------------------------------------------------------------
   this.customshieldsmissilemineexplosioneffects = "on";     //choices: "on" , "off" 
   //----------------------------------------------------------------------------------------------------
   //
   //-------------DO NOT CHANGE ANYTHING BELOW THIS POINT---------
   //
   //
   //
   //----------------------------------------------------------------
   //Note:draw distance maximum for effects is currently set at 51200
   //----------------------------------------------------------------
   //
   if(player.ship.mass > 0 && player.ship.mass <= 5000)
      {
      this.customshieldsplayershieldflashersize = 1;
      }
   if(player.ship.mass > 5000 && player.ship.mass <= 50000)
      {
      this.customshieldsplayershieldflashersize = 2;
      }    
   if(player.ship.mass > 50000 && player.ship.mass <= 150000)
      {
      this.customshieldsplayershieldflashersize = 3;
      }  
   if(player.ship.mass > 150000 && player.ship.mass <= 500000)
      {
      this.customshieldsplayershieldflashersize = 4;
      }
   if(player.ship.mass > 500000)
      {
      this.customshieldsplayershieldflashersize = 5;
      }
   this.customshieldshitrange = 50;//default if string different from below
   if(this.customshieldsdamageeffects === "damage")
      {
	  this.customshieldshitrange = 50;
	  }
   if(this.customshieldsdamageeffects === "blownpanels")
      {
	  this.customshieldshitrange = 45;
	  }
   if(this.customshieldsdamageeffects === "sparks")
      {
	  this.customshieldshitrange = 35;
	  }
   if(this.customshieldsdamageeffects === "off")
      {
	  this.customshieldshitrange = 1;
	  }
   this.customshieldsspeedcollisionradiusadjustment = (player.ship.maxSpeed / 350) * 8;     
   }
this.shipTakingDamage = function(amount, whom, type)
   {
   if(whom && whom.position !== undefined)//added for whom is null issue
      {
      if(player.ship.speed > player.ship.maxSpeed)//added for shield collision issue caused by using solid models which are needed to work correctly in 1.76, applies to advanced and basic npcs also
         {
         var speedcollisionradiusadjustment = (player.ship.speed / player.ship.maxSpeed) * this.customshieldsspeedcollisionradiusadjustment;
	     }
      else
         {
	     var speedcollisionradiusadjustment = 0;
	     }
      var impactradiusfactor = ((3 * (1500 / (player.ship.position.distanceTo(whom.position)))) + 16);	 
      var impactlocation = whom.position.add(whom.vectorForward.multiply((whom.position.distanceTo(player.ship.position)) - (player.ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible forward weapon hit location
      if((player.ship.position.distanceTo(impactlocation)) > (player.ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
         {
         impactlocation = whom.position.add(whom.vectorForward.multiply(-((whom.position.distanceTo(player.ship.position)) - (player.ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible aft weapon hit location
         if((player.ship.position.distanceTo(impactlocation)) > (player.ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
            {
            impactlocation = whom.position.add(whom.vectorRight.multiply((whom.position.distanceTo(player.ship.position)) - (player.ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible starboard weapon hit location
            if((player.ship.position.distanceTo(impactlocation)) > (player.ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
               {
               impactlocation = whom.position.add(whom.vectorRight.multiply(-((whom.position.distanceTo(player.ship.position)) - (player.ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible port weapon hit location
               if((player.ship.position.distanceTo(impactlocation)) > (player.ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                  {			   
                  var impactinterpolation = ((player.ship.collisionRadius + 1 + speedcollisionradiusadjustment) / (player.ship.position.distanceTo(whom.position)));  
                  impactlocation = Vector3D.interpolate(player.ship.position, whom.position, impactinterpolation);//interpolate weapon hit directly between ship and attacker as a fallback position
                  }
               }
            }
         }
      var attackerdirection = whom.position.subtract(impactlocation).direction();
      var playerdirection = player.ship.position.subtract(impactlocation).direction();
      var impactdirection = impactlocation.subtract(player.ship.position).direction();
      var impactrelativeposition = impactlocation.subtract(player.ship.position);
      var impactangle = player.ship.heading.angleTo(impactdirection);
      this.customshieldsattackerdirection = attackerdirection;
      this.customshieldsplayerdirection = playerdirection;
      this.customshieldsimpactdirection = impactdirection;
      this.customshieldsimpactrelativeposition = impactrelativeposition;
      this.customshieldsimpactlocation = impactlocation;
      if(impactangle <= 1.57)
         {
	     if(player.ship.forwardShield === 0)
            {
		    this.$customshieldsplayerunshieldedhits();	
		    }
         else
	        {
		    this.customshieldsplayershieldflashers = (player.ship.forwardShield / player.ship.maxForwardShield);
            if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))//might take closing speed into account in the future
		       {
			   if(player.ship.viewDirection === "VIEW_CUSTOM")
			      {
			      this.$customshieldsplayershieldflareupcustom();
			      }
               else
			      {
			      this.$customshieldsplayershieldflareup();
			      }
			   }
            else
		       {
			   if(player.ship.viewDirection === "VIEW_CUSTOM")
			      {
		          this.$customshieldsplayershieldflasherscustom();
			      }
               else
			      {
			      this.$customshieldsplayershieldflashers();
			      }
			   }
		    }
         }
      else
         {
	     if(player.ship.aftShield === 0)
            {
            this.$customshieldsplayerunshieldedhits();		 
		    }
         else
	        {
		    this.customshieldsplayershieldflashers = (player.ship.aftShield / player.ship.maxAftShield);
		    if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))
		       {
			   if(player.ship.viewDirection === "VIEW_CUSTOM")
			      {
			      this.$customshieldsplayershieldflareupcustom();
			      }
               else
			      {
			      this.$customshieldsplayershieldflareup();
			      }
			   }
            else
		       {
			   if(player.ship.viewDirection === "VIEW_CUSTOM")
			      {
		          this.$customshieldsplayershieldflasherscustom();
			      }
               else
			      {
			      this.$customshieldsplayershieldflashers();
			      }
			   }
		    }
         }
      }
   }
this.$customshieldsplayerunshieldedhits = function()
   {
   var hitrange = this.customshieldshitrange;
   var hitchance = Math.ceil(Math.random() * hitrange);
   switch(hitchance)
      {   
      case 1://10% nothing
	  case 2:
	  case 3:
	  case 4:
	  case 5:
	     {
		 break;
		 }
	  case 6://60% sparks
	  case 7:
	  case 8:
	  case 9:
	  case 10:
	  case 11:
	  case 12:
	  case 13:
	  case 14:
	  case 15:
	  case 16:
	  case 17:
	  case 18:
	  case 19:
	  case 20:
	  case 21:
	  case 22:
	  case 23:
	  case 24:
	  case 25:
	  case 26:
	  case 27:
	  case 28:
	  case 29:
	  case 30:
	  case 31:
	  case 32:
	  case 33:
	  case 34:
	  case 35:
         {
         var sparks = system.addShips("customshieldssparks", 1, this.customshieldsimpactlocation, 0);
         break;
         }
      case 36://20% blown panels
      case 37:
	  case 38:
	  case 39:
	  case 40:
	  case 41:
	  case 42:
	  case 43:
	  case 44:
	  case 45:
         {
		 var il = this.customshieldsimpactlocation;
         var id = this.customshieldsimpactdirection;
		 var sparksblownpanels = player.ship.spawnOne("customshieldssparksblownpanels");
         sparksblownpanels.position = il;
		 sparksblownpanels.orientation = id.rotationTo([0, 0, 1]);
		 sparksblownpanels.velocity = player.ship.velocity.add(sparksblownpanels.vectorForward.multiply((Math.ceil(Math.random() * 150)) + 50));
         break;
         }
      case 46://currently nothing, future expansion
	  case 47:
	  case 48:
	  case 49:
	     {
		 break;
		 }
      case 50://fuel leak caused by hull rupture
         {
		 if(this.customshieldsfuelleakflag === 1.0)
            {
            break;
            }
         this.customshieldsfuelleakflag = 1.0;
		 this.customshieldsfuelleakrepairTimer = new Timer(this, this.$customshieldsfuelleakrepair, 60, -1);	
		 var il = this.customshieldsimpactlocation;
         var id = this.customshieldsimpactdirection;
		 var sparksfuelleaks = player.ship.spawnOne("customshieldssparksfuelleaks");
         sparksfuelleaks.position = il;
		 sparksfuelleaks.orientation = id.rotationTo([0, 0, 1]);
         sparksfuelleaks.target = player.ship;
         break;
         }
      }
   }
this.$customshieldsfuelleakrepair = function()
   {
   this.customshieldsfuelleakflag = 0.0;
   player.consoleMessage("Fuel leak sealed", 4);
   if(this.customshieldsfuelleakrepairTimer && this.customshieldsfuelleakrepairTimer.isRunning)
      {
      this.customshieldsfuelleakrepairTimer.stop();
	  delete this.customshieldsfuelleakrepairTimer;
      }
   }
this.shipDied = function()
   {
   if(this.customshieldsfuelleakrepairTimer && this.customshieldsfuelleakrepairTimer.isRunning)
      {
      this.customshieldsfuelleakrepairTimer.stop();
	  delete this.customshieldsfuelleakrepairTimer;
      }
   }   
this.$customshieldsplayershieldflareupcustom = function()
   {
   var psf = this.customshieldsplayershieldflashers;
   var psfs = this.customshieldsplayershieldflashersize;
   var il = this.customshieldsimpactlocation;
   psf = Math.floor(psf * 10);
   psf = (psf / 10);
   switch(psf)
      {   
      case 0.9:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupA" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.8:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupB" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.7:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupC" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.6:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupD" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.5:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupE" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.4:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupF" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.3:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupG" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.2:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupH" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.1:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupI" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      case 0.0:
         {
		 var playershieldflareupcustom = player.ship.spawnOne("customshieldsshieldflasherflareupJ" + psfs);
         playershieldflareupcustom.position = il;
         playershieldflareupcustom.target = player.ship;
         break;
         }
      }
   }            
this.$customshieldsplayershieldflareup = function()
   {
   var psf = this.customshieldsplayershieldflashers;
   var il = this.customshieldsimpactlocation;
   var pd = this.customshieldsplayerdirection;
   psf = Math.floor(psf * 10);
   psf = (psf / 10);
   switch(psf)
      {   
      case 0.9:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupAplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.8:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupBplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.7:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupCplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.6:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupDplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.5:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupEplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.4:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupFplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.3:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupGplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.2:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupHplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.1:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupIplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      case 0.0:
         {
		 var playershieldflareup = player.ship.spawnOne("customshieldsshieldflasherflareupJplayer");
         playershieldflareup.position = il;
		 playershieldflareup.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflareup.target = player.ship;
         break;
         }
      }
   }      
this.$customshieldsplayershieldflasherscustom = function()
   {
   var customshieldsshieldcolor = this.customshieldsshieldcolor;
   var psf = this.customshieldsplayershieldflashers;
   var psfs = this.customshieldsplayershieldflashersize;
   var il = this.customshieldsimpactlocation;
   var ad = this.customshieldsattackerdirection;
   psf = Math.floor(psf * 10);
   psf = (psf / 10);
   switch(psf)
      {   
      case 0.9:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "A" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.8:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "B" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.7:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "C" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.6:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "D" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.5:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "E" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.4:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "F" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.3:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "G" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.2:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "H" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.1:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "I" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      case 0.0:
         {
         var playershieldflasherscustom = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "J" + psfs);
         playershieldflasherscustom.position = il;
		 playershieldflasherscustom.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflasherscustom.target = player.ship;
         break;
         }
      }
   }      
this.$customshieldsplayershieldflashers = function()//v0.81correction made to use a normal entity instead of a zero poly entity, spawn flash slightly farther out rather than at the same position as the tunneling effect
   {
   var customshieldsshieldcolor = this.customshieldsshieldcolor;
   var psf = this.customshieldsplayershieldflashers;
   var psfs = this.customshieldsplayershieldflashersize;
   var il = this.customshieldsimpactlocation;
   var ad = this.customshieldsattackerdirection;
   var pd = this.customshieldsplayerdirection;
   var interpolation = ((player.ship.collisionRadius + 2) / (player.ship.collisionRadius + 1));
   var ilinterpolation = Vector3D.interpolate(player.ship.position, il, interpolation);
   this.customshieldsimpactrelativepositioninterpolation = ilinterpolation.subtract(player.ship.position);
   psf = Math.floor(psf * 10);
   psf = (psf / 10);
   switch(psf)
      {   
      case 0.9:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "A" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Aplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.8:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "B" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Bplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.7:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "C" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Cplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.6:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "D" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Dplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.5:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "E" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Eplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.4:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "F" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Fplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.3:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "G" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Gplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.2:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "H" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Hplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.1:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "I" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Iplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      case 0.0:
         {
         var playershieldflashersmain = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "J" + psfs);
         playershieldflashersmain.position = il;
		 playershieldflashersmain.orientation = ad.rotationTo([0, 0, 1]);
         playershieldflashersmain.target = player.ship;
		 var playershieldflashersflash = player.ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "Jplayer");
         playershieldflashersflash.position = ilinterpolation;
		 playershieldflashersflash.orientation = pd.rotationTo([0, 0, 1]);
         playershieldflashersflash.target = player.ship;
         break;
         }
      }
   }   
this.shipSpawned = function(ship)
   {
   if((ship.isMissile || ship.isMine) && (this.customshieldsmissilemineexplosioneffects === "on"))
      {
	  if(ship.scriptInfo.npc_shields && ship.scriptInfo.npc_shields === "no")
         {
		 return;
		 }
      else
	     {
	     ship.script.$customshieldsmissilemineshipdied = ship.script.shipDied;
         ship.script.shipDied = function(whom, why)
            {
            if(why !== "removed" && why !== "hit a planet" && (player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200))//determine if effect should be drawn, use "isVisible" in 1.77
               {
               ship.script.customshieldsmissilemineexplosion = ship.spawnOne("customshieldsmissilemineflareupA");
			   ship.script.customshieldsmissilemineexplosion.position = ship.position;
		       ship.script.customshieldsmissilemineexplosion.orientation = ship.orientation;
			   ship.script.customshieldsmissilemineexplosion.velocity = ship.velocity;
               }
            if(ship.script.$customshieldsmissilemineshipdied)
	           {
               ship.script.$customshieldsmissilemineshipdied.apply(this, arguments);
		       }
            }
	     }
      }
   if((ship.isThargoid) && (this.customshieldsshieldtypethargoid === "basic"))//main thargoid shield effects routines using only energy level to control the shield and damage effects
      {
	  if(ship.scriptInfo.npc_shields && ship.scriptInfo.npc_shields === "no")
         {
		 return;
		 }
      else
	     {
         if(ship.mass > 0 && ship.mass <= 5000)
            {
            ship.script.customshieldsshieldflashersize = 1;
            }
         if(ship.mass > 5000 && ship.mass <= 50000)
            {
            ship.script.customshieldsshieldflashersize = 2;
            }    
         if(ship.mass > 50000 && ship.mass <= 150000)
            {
            ship.script.customshieldsshieldflashersize = 3;
            }  
         if(ship.mass > 150000 && ship.mass <= 500000)
            {
            ship.script.customshieldsshieldflashersize = 4;
            }
         if(ship.mass > 500000)
            {
            ship.script.customshieldsshieldflashersize = 5;
            }
         ship.script.customshieldsshieldcolorthargoid = worldScripts["customshields"].customshieldsshieldcolorthargoid;
	     //----------------------------------------------------begin cloaked attacker code
	     ship.script.$customshieldsthargoidsshipbeingattackedbycloaked = ship.script.shipBeingAttackedByCloaked;
	     ship.script.shipBeingAttackedByCloaked = function()
            {
	        if(player.ship.isCloaked === true)//if the player ship is cloaked it is likely to be the attacker
		       {
		       ship.script.customshieldscloakedattacker = player.ship;//tell the target who attacked
			   }
            if(ship.script.$customshieldsthargoidsshipbeingattackedbycloaked)
		       {
               ship.script.$customshieldsthargoidsshipbeingattackedbycloaked.apply(this, arguments);
               }
            }
	     ship.script.$customshieldsthargoidsshipattackedother = ship.script.shipAttackedOther;
	     ship.script.shipAttackedOther = function(other)
            {
	        if(ship.isCloaked === true)//a cloaked thargoid(is there such a thing?) attacking another npc will override the player as the cloaked attacker when the player is also cloaked
		       {
		       other.script.customshieldscloakedattacker = ship;//tell the target who attacked
			   }
            if(ship.script.$customshieldsthargoidsshipattackedother)
		       {
               ship.script.$customshieldsthargoidsshipattackedother.apply(this, arguments);
               }
           }
         ship.script.$customshieldsthargoidsshiptakingdamage = ship.script.shipTakingDamage;
         ship.script.shipTakingDamage = function(amount, whom, type)
            {
		    if(ship.script.customshieldscloakedattacker)//if this key exists the attacker is cloaked
               {
	           whom = ship.script.customshieldscloakedattacker;//whom must be the cloaked attacker, make it so
	           delete ship.script.customshieldscloakedattacker;//reset this key
	           }//---------------------------------------------end cloaked attacker code
			if(whom && whom.position !== undefined)
               {
	           var impactradiusfactor = ((3 * (1500 / (ship.position.distanceTo(whom.position)))) + 16);	 
               var impactlocation = whom.position.add(whom.vectorForward.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1)));//determine possible forward weapon hit location
               if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                  {
                  impactlocation = whom.position.add(whom.vectorForward.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1))));//determine possible aft weapon hit location
                  if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                     {
                     impactlocation = whom.position.add(whom.vectorRight.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1)));//determine possible starboard weapon hit location
                     if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                        {
                        impactlocation = whom.position.add(whom.vectorRight.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1))));//determine possible port weapon hit location
                        if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                           {			   
                           var impactinterpolation = ((ship.collisionRadius + 1) / (ship.position.distanceTo(whom.position)));  
                           impactlocation = Vector3D.interpolate(ship.position, whom.position, impactinterpolation);//interpolate weapon hit directly between ship and attacker as a fallback position
                           }
                        }
                     }
                  }
               var attackerdirection = whom.position.subtract(impactlocation).direction();
               var impactdirection = impactlocation.subtract(ship.position).direction();
               var impactrelativeposition = impactlocation.subtract(ship.position);
               var impactangle = ship.heading.angleTo(impactdirection);
	           ship.script.customshieldsattackerdirection = attackerdirection;
	           ship.script.customshieldsimpactdirection = impactdirection;
               ship.script.customshieldsimpactrelativeposition = impactrelativeposition;
               ship.script.customshieldsimpactlocation = impactlocation;			
               if(player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200)//determine if effect should be drawn, use "isVisible" in 1.77
                  {
			      ship.script.customshieldsdrawthargoidshieldflashers = (ship.energy / ship.maxEnergy);
		          if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))
		             {
			         ship.script.$customshieldsdrawthargoidshieldflareup();
			         }
                  else
			         {
			         ship.script.$customshieldsdrawthargoidshieldflashers();
			         }
			      }
               if(ship.script.$customshieldsthargoidsshiptakingdamage)
		          {
                  ship.script.$customshieldsthargoidsshiptakingdamage.apply(this, arguments);
                  }
               }
			}
         ship.script.$customshieldsdrawthargoidshieldflareup = function()
            {
            var dsf = ship.script.customshieldsdrawthargoidshieldflashers;
	        var sfs = ship.script.customshieldsshieldflashersize;
            var il = ship.script.customshieldsimpactlocation;
            dsf = Math.floor(dsf * 10);
            dsf = (dsf / 10);
            switch(dsf)
               {   
               case 0.9:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupA" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.8:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupB" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.7:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupC" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.6:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupD" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.5:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupE" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.4:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupF" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.3:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupG" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.2:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupH" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.1:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupI" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               case 0.0:
                  {
		          var thargoidshieldflareup = ship.spawnOne("customshieldsshieldflasherflareupJ" + sfs);
                  thargoidshieldflareup.position = il;
                  thargoidshieldflareup.target = ship;
                  break;
                  }
               }
            }        
         ship.script.$customshieldsdrawthargoidshieldflashers = function()
            {
	        var customshieldsshieldcolorthargoid = ship.script.customshieldsshieldcolorthargoid;
	        var dsf = ship.script.customshieldsdrawthargoidshieldflashers;
	        var sfs = ship.script.customshieldsshieldflashersize;
	        var il = ship.script.customshieldsimpactlocation;
	        var ad = ship.script.customshieldsattackerdirection;
	        dsf = Math.floor(dsf * 10);
            dsf = (dsf / 10);
	        switch(dsf)
               {   
               case 0.9:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "A" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.8:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "B" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.7:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "C" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.6:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "D" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.5:
                  {
		          var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "E" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.4:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "F" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.3:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "G" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.2:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "H" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.1:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "I" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               case 0.0:
                  {
			      var thargoidshieldflashers = ship.spawnOne("customshieldsshieldflasherthargoid" + customshieldsshieldcolorthargoid + "J" + sfs);
			      thargoidshieldflashers.position = il;
			      thargoidshieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			      thargoidshieldflashers.target = ship;
			      break;
			      }
               }
            }
		 }
      }
   if(ship.isPlayer || ship.isSubEntity || ship.isStation || ship.isThargoid || ship.isRock || ship.isCargo || ship.isWeapon || ship.isDerelict || ship.primaryRole === "wreckage" || ship.scanClass === "CLASS_BUOY" || (ship.scriptInfo.npc_shields && ship.scriptInfo.npc_shields === "no"))
      {
      return;
	  }	  
   if(this.customshieldsshieldtype === "basic")//main npc shielding routines using only energy level to control the shield and damage effects
      {
      if(ship.mass > 0 && ship.mass <= 5000)
         {
         ship.script.customshieldsshieldflashersize = 1;
         }
      if(ship.mass > 5000 && ship.mass <= 50000)
         {
         ship.script.customshieldsshieldflashersize = 2;
         }    
      if(ship.mass > 50000 && ship.mass <= 150000)
         {
         ship.script.customshieldsshieldflashersize = 3;
         }  
      if(ship.mass > 150000 && ship.mass <= 500000)
         {
         ship.script.customshieldsshieldflashersize = 4;
         }
      if(ship.mass > 500000)
         {
         ship.script.customshieldsshieldflashersize = 5;
         }
      ship.script.customshieldsspeedcollisionradiusadjustment = (ship.maxSpeed / 350) * 8;	
      ship.script.customshieldsshieldcolor = worldScripts["customshields"].customshieldsshieldcolor;
	  ship.script.customshieldshitrange = worldScripts["customshields"].customshieldshitrange;
	  //----------------------------------------------------begin cloaked attacker code
	  ship.script.$customshieldsshipbeingattackedbycloaked = ship.script.shipBeingAttackedByCloaked;
	  ship.script.shipBeingAttackedByCloaked = function()
         {
	     if(player.ship.isCloaked === true)//if the player ship is cloaked it is likely to be the attacker
		    {
		    ship.script.customshieldscloakedattacker = player.ship;//tell the target who attacked
			}
         if(ship.script.$customshieldsshipbeingattackedbycloaked)
		    {
            ship.script.$customshieldsshipbeingattackedbycloaked.apply(this, arguments);
            }
         }
	  ship.script.$customshieldsshipattackedother = ship.script.shipAttackedOther;
	  ship.script.shipAttackedOther = function(other)
         {
	     if(ship.isCloaked === true)//a cloaked npc attacking another npc will override the player as the cloaked attacker when the player is also cloaked
		    {
		    other.script.customshieldscloakedattacker = ship;//tell the target who attacked
			}
         if(ship.script.$customshieldsshipattackedother)
		    {
            ship.script.$customshieldsshipattackedother.apply(this, arguments);
            }
        }
      ship.script.$customshieldsshiptakingdamage = ship.script.shipTakingDamage;
      ship.script.shipTakingDamage = function(amount, whom, type)
         {
		 if(ship.script.customshieldscloakedattacker)//if this key exists the attacker is cloaked
            {
	        whom = ship.script.customshieldscloakedattacker;//whom must be the cloaked attacker, make it so
	        delete ship.script.customshieldscloakedattacker;//reset this key
	        }//---------------------------------------------end cloaked attacker code
		 if(whom && whom.position !== undefined)
            {
			if(ship.speed > ship.maxSpeed)//added for shield collision issue
               {
               var speedcollisionradiusadjustment = (ship.speed / ship.maxSpeed) * ship.script.customshieldsspeedcollisionradiusadjustment;
	           }
            else
               {
	           var speedcollisionradiusadjustment = 0;
	           }
	        var impactradiusfactor = ((3 * (1500 / (ship.position.distanceTo(whom.position)))) + 16);	 
            var impactlocation = whom.position.add(whom.vectorForward.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible forward weapon hit location
            if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
               {
               impactlocation = whom.position.add(whom.vectorForward.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible aft weapon hit location
               if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                  {
                  impactlocation = whom.position.add(whom.vectorRight.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible starboard weapon hit location
                  if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                     {
                     impactlocation = whom.position.add(whom.vectorRight.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible port weapon hit location
                     if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                        {			   
                        var impactinterpolation = ((ship.collisionRadius + 1 + speedcollisionradiusadjustment) / (ship.position.distanceTo(whom.position)));  
                        impactlocation = Vector3D.interpolate(ship.position, whom.position, impactinterpolation);//interpolate weapon hit directly between ship and attacker as a fallback position
                        }
                     }
                  }
               }
            var attackerdirection = whom.position.subtract(impactlocation).direction();
            var impactdirection = impactlocation.subtract(ship.position).direction();
            var impactrelativeposition = impactlocation.subtract(ship.position);
            var impactangle = ship.heading.angleTo(impactdirection);
	        ship.script.customshieldsattackerdirection = attackerdirection;
	        ship.script.customshieldsimpactdirection = impactdirection;
            ship.script.customshieldsimpactrelativeposition = impactrelativeposition;
            ship.script.customshieldsimpactlocation = impactlocation;
            if((ship.energy > (ship.maxEnergy * 0.2)) && (player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200))//determine if effect should be drawn, use "isVisible" in 1.77
               {
			   ship.script.customshieldsdrawshieldflashers = ((((ship.energy / ship.maxEnergy) - 0.2) / 4) * 5);
		       if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))
		          {
			      ship.script.$customshieldsdrawshieldflareup();
			      }
               else
			      {
			      ship.script.$customshieldsdrawshieldflashers();
			      }
			   }
            if((ship.energy <= (ship.maxEnergy * 0.2)) && (player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200))//determine if effect should be drawn, use "isVisible" in 1.77
               {
		       ship.script.$customshieldsdrawunshieldedhits();
		       }
            if(ship.script.$customshieldsshiptakingdamage)
		       {
               ship.script.$customshieldsshiptakingdamage.apply(this, arguments);
               }
            }
		 }
      ship.script.$customshieldsdrawunshieldedhits = function()
         {
		 var hitrange = ship.script.customshieldshitrange;
         var hitchance = Math.ceil(Math.random() * hitrange);
         switch(hitchance)
            {   
            case 1://10% nothing
	        case 2:
	        case 3:
	        case 4:
	        case 5:
	           {
		       break;
		       }
	        case 6://60% sparks
	        case 7:
	        case 8:
	        case 9:
	        case 10:
	        case 11:
	        case 12:
	        case 13:
	        case 14:
	        case 15:
	        case 16:
	        case 17:
	        case 18:
	        case 19:
	        case 20:
	        case 21:
	        case 22:
	        case 23:
	        case 24:
	        case 25:
	        case 26:
	        case 27:
	        case 28:
	        case 29:
	        case 30:
	        case 31:
	        case 32:
	        case 33:
	        case 34:
	        case 35:
               {
               var sparks = system.addShips("customshieldssparks", 1, ship.script.customshieldsimpactlocation, 0);
               break;
               }
            case 36://20% blown panels
            case 37:
	        case 38:
	        case 39:
	        case 40:
	        case 41:
	        case 42:
	        case 43:
	        case 44:
	        case 45:
               {
               var il = ship.script.customshieldsimpactlocation;
               var id = ship.script.customshieldsimpactdirection;
		       var sparksblownpanels = ship.spawnOne("customshieldssparksblownpanels");
               sparksblownpanels.position = il;
		       sparksblownpanels.orientation = id.rotationTo([0, 0, 1]);
		       sparksblownpanels.velocity = ship.velocity.add(sparksblownpanels.vectorForward.multiply((Math.ceil(Math.random() * 150)) + 50));
               break;
               }
            case 46://currently nothing, future expansion
            case 47:
	        case 48:
	        case 49:
	           {
		       break;
		       }
            case 50://fuel leak caused by hull rupture
               {
			   if(ship.script.customshieldsfuelleakflag === 1.0)
			      {
			      break;
			      }
               this.customshieldsfuelleakflag = 1.0;
			   var il = ship.script.customshieldsimpactlocation;
               var id = ship.script.customshieldsimpactdirection;
		       var sparksfuelleaks = ship.spawnOne("customshieldssparksfuelleaks");
               sparksfuelleaks.position = il;
		       sparksfuelleaks.orientation = id.rotationTo([0, 0, 1]);
               sparksfuelleaks.target = ship;
               break;
               }
            }
         }  
      ship.script.$customshieldsdrawshieldflareup = function()
         {
         var dsf = ship.script.customshieldsdrawshieldflashers;
	     var sfs = ship.script.customshieldsshieldflashersize;
         var il = ship.script.customshieldsimpactlocation;
         dsf = Math.floor(dsf * 10);
         dsf = (dsf / 10);
         switch(dsf)
            {   
            case 0.9:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupA" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.8:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupB" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.7:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupC" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.6:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupD" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.5:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupE" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.4:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupF" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.3:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupG" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.2:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupH" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.1:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupI" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.0:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupJ" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            }
         }        
      ship.script.$customshieldsdrawshieldflashers = function()
         {
	     var customshieldsshieldcolor = ship.script.customshieldsshieldcolor;
	     var dsf = ship.script.customshieldsdrawshieldflashers;
	     var sfs = ship.script.customshieldsshieldflashersize;
	     var il = ship.script.customshieldsimpactlocation;
	     var ad = ship.script.customshieldsattackerdirection;
	     dsf = Math.floor(dsf * 10);
         dsf = (dsf / 10);
	     switch(dsf)
            {   
            case 0.9:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "A" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.8:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "B" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.7:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "C" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.6:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "D" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.5:
               {
		       var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "E" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.4:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "F" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.3:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "G" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.2:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "H" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.1:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "I" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.0:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "J" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            }
         }
      }
   if(this.customshieldsshieldtype === "advanced")//main npc shielding routines using realistic forward and aft virtual shields to control the shield and damage effects and equipment
      {   	  
      ship.script.customshieldsmaxforwardshieldlevel = 128;
      ship.script.customshieldsmaxaftshieldlevel = 128;
      ship.script.customshieldsshieldrechargingrate = 1.0;
      ship.script.customshieldsshieldrechargingratemultiplier = 0.0;
      ship.script.customshieldslastenergylevel = ship.energy;
      if(ship.equipmentStatus("EQ_SHIELD_BOOSTER") === "EQUIPMENT_OK")
         {
	     ship.removeEquipment("EQ_SHIELD_BOOSTER");
         ship.script.customshieldsmaxforwardshieldlevel += 128;
         ship.script.customshieldsmaxaftshieldlevel += 128;
         }
      if(ship.equipmentStatus("EQ_SHIELD_ENHANCER") === "EQUIPMENT_OK")
         {
	     ship.removeEquipment("EQ_SHIELD_ENHANCER");
         ship.script.customshieldsmaxforwardshieldlevel += 128;
         ship.script.customshieldsmaxaftshieldlevel += 128;
	     ship.script.customshieldsshieldrechargingrate = 1.5;
	     ship.script.customshieldsshieldrechargingratemultiplier = 0.5;
         }
      if(worldScripts["shieldequalizercapacitors"])
         {
	     if(Math.random() <= 0.10)
	        {
		    if(Math.random() <= 0.50)
		       {
			   ship.awardEquipment("EQ_SHIELD_EQUALIZER");
			   }
            if(Math.random() <= 0.50)
		       {
			   ship.awardEquipment("EQ_FORWARD_SHIELD_CAPACITOR");
			   ship.script.customshieldsmaxforwardshieldlevel += 64;
			   }
            if(Math.random() <= 0.50)
		       {
			   ship.awardEquipment("EQ_AFT_SHIELD_CAPACITOR");
			   ship.script.customshieldsmaxaftshieldlevel += 64;
			   }
		    }
	     }
      if(worldScripts["Shield Cycler"])
         {
	     if(Math.random() <= 0.10)
	        {
		    if((Math.random() <= 0.50) && (ship.equipmentStatus("EQ_SHIELD_EQUALIZER") !== "EQUIPMENT_OK"))
		       {
			   ship.awardEquipment("EQ_SC_SHIELD_CYCLER_INTERNAL");
			   }
            }
         }
      if(ship.mass > 0 && ship.mass <= 5000)
         {
         ship.script.customshieldsshieldflashersize = 1;
         }
      if(ship.mass > 5000 && ship.mass <= 50000)
         {
         ship.script.customshieldsshieldflashersize = 2;
         }    
      if(ship.mass > 50000 && ship.mass <= 150000)
         {
         ship.script.customshieldsshieldflashersize = 3;
         }  
      if(ship.mass > 150000 && ship.mass <= 500000)
         {
         ship.script.customshieldsshieldflashersize = 4;
         }
      if(ship.mass > 500000)
         {
         ship.script.customshieldsshieldflashersize = 5;
         }
      ship.script.customshieldsspeedcollisionradiusadjustment = (ship.maxSpeed / 350) * 8;
      ship.script.customshieldsshieldcolor = worldScripts["customshields"].customshieldsshieldcolor;
	  ship.script.customshieldshitrange = worldScripts["customshields"].customshieldshitrange;
      ship.script.customshieldsforwardshieldlevel = ship.script.customshieldsmaxforwardshieldlevel;
      ship.script.customshieldsaftshieldlevel = ship.script.customshieldsmaxaftshieldlevel;
	  //----------------------------------------------------begin cloaked attacker code
	  ship.script.$customshieldsshipbeingattackedbycloaked = ship.script.shipBeingAttackedByCloaked;
	  ship.script.shipBeingAttackedByCloaked = function()
         {
	     if(player.ship.isCloaked === true)//if the player ship is cloaked it is likely to be the attacker
		    {
		    ship.script.customshieldscloakedattacker = player.ship;//tell the target who attacked
			}
         if(ship.script.$customshieldsshipbeingattackedbycloaked)
		    {
            ship.script.$customshieldsshipbeingattackedbycloaked.apply(this, arguments);
            }
         }
	  ship.script.$customshieldsshipattackedother = ship.script.shipAttackedOther;
	  ship.script.shipAttackedOther = function(other)
         {
	     if(ship.isCloaked === true)//a cloaked npc attacking another npc will override the player as the cloaked attacker when the player is also cloaked
		    {
		    other.script.customshieldscloakedattacker = ship;//tell the target who attacked
			}
         if(ship.script.$customshieldsshipattackedother)
		    {
            ship.script.$customshieldsshipattackedother.apply(this, arguments);
            }
        }
      ship.script.$customshieldsshiptakingdamage = ship.script.shipTakingDamage;
      ship.script.shipTakingDamage = function(amount, whom, type)
         {
		 if(ship.script.customshieldscloakedattacker)//if this key exists the attacker is cloaked
            {
	        whom = ship.script.customshieldscloakedattacker;//whom must be the cloaked attacker, make it so
	        delete ship.script.customshieldscloakedattacker;//reset this key
	        }//---------------------------------------------end cloaked attacker code
		 if(whom && whom.position !== undefined)
            {
			if(ship.speed > ship.maxSpeed)//added for shield collision issue
               {
               var speedcollisionradiusadjustment = (ship.speed / ship.maxSpeed) * ship.script.customshieldsspeedcollisionradiusadjustment;
	           }
            else
               {
	           var speedcollisionradiusadjustment = 0;
	           }
	        var impactradiusfactor = ((3 * (1500 / (ship.position.distanceTo(whom.position)))) + 16);	 
            var impactlocation = whom.position.add(whom.vectorForward.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible forward weapon hit location
            if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
               {
               impactlocation = whom.position.add(whom.vectorForward.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible aft weapon hit location
               if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                  {
                  impactlocation = whom.position.add(whom.vectorRight.multiply((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment)));//determine possible starboard weapon hit location
                  if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                     {
                     impactlocation = whom.position.add(whom.vectorRight.multiply(-((whom.position.distanceTo(ship.position)) - (ship.collisionRadius + 1 + speedcollisionradiusadjustment))));//determine possible port weapon hit location
                     if((ship.position.distanceTo(impactlocation)) > (ship.collisionRadius + speedcollisionradiusadjustment + impactradiusfactor))//too far from ship because of delay to hit from plasma weapon or off vector angle because of thargoid laser
                        {			   
                        var impactinterpolation = ((ship.collisionRadius + 1 + speedcollisionradiusadjustment) / (ship.position.distanceTo(whom.position)));  
                        impactlocation = Vector3D.interpolate(ship.position, whom.position, impactinterpolation);//interpolate weapon hit directly between ship and attacker as a fallback position
                        }
                     }
                  }
               }
            var attackerdirection = whom.position.subtract(impactlocation).direction();
            var impactdirection = impactlocation.subtract(ship.position).direction();
            var impactrelativeposition = impactlocation.subtract(ship.position);
            var impactangle = ship.heading.angleTo(impactdirection);
	        ship.script.customshieldsattackerdirection = attackerdirection;
	        ship.script.customshieldsimpactdirection = impactdirection;
            ship.script.customshieldsimpactrelativeposition = impactrelativeposition;
            ship.script.customshieldsimpactlocation = impactlocation;
            if((ship.script.customshieldsforwardshieldlevel > 0) && (impactangle <= 1.57))//choose the shield that was hit
               {
               ship.script.customshieldsforwardshieldlevel -= amount;//subtract from shield
               var forwardshieldrecovery = amount;
               if(ship.script.customshieldsforwardshieldlevel < 0)//shield minimum is 0, give overflow damage to energy
                  {
                  forwardshieldrecovery += ship.script.customshieldsforwardshieldlevel;
			      var transferredamount = (ship.script.customshieldsforwardshieldlevel * -1);
                  ship.script.customshieldsforwardshieldlevel = 0;
			      if(((ship.equipmentStatus("EQ_SHIELD_EQUALIZER") === "EQUIPMENT_OK") || (ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK")) && (ship.script.customshieldsaftshieldlevel > 0))//take from the opposite shield before energy adjustment
                     {
			         ship.script.customshieldsaftshieldlevel -= transferredamount;
                     var aftshieldrecovery = transferredamount;
                     if(ship.script.customshieldsaftshieldlevel < 0)//shield minimum is 0, give overflow damage to energy
                        {
                        aftshieldrecovery += ship.script.customshieldsaftshieldlevel;
                        ship.script.customshieldsaftshieldlevel = 0;
				        forwardshieldrecovery = aftshieldrecovery;
                        }
			         }
                  }
               if((ship.equipmentStatus("EQ_SHIELD_EQUALIZER") === "EQUIPMENT_OK") || (ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK"))//final division of shield energy between shields
                  {
			      if((ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK") && (ship.energy > 5))
			         {
			         ship.energy -= 4.0;//shield cycler transfer energy loss(minimum)
			         }
                  var shieldtransfer = ((ship.script.customshieldsforwardshieldlevel + ship.script.customshieldsaftshieldlevel) / 2);
                  ship.script.customshieldsforwardshieldlevel = shieldtransfer;
                  ship.script.customshieldsaftshieldlevel = shieldtransfer;
			      }
               ship.energy += forwardshieldrecovery;//restore energy	 
               if(player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200)//determine if effect should be drawn, use "isVisible" in 1.77
                  {
			      ship.script.customshieldsdrawshieldflashers = (ship.script.customshieldsforwardshieldlevel / ship.script.customshieldsmaxforwardshieldlevel);
		          if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))
		             {
			         ship.script.$customshieldsdrawshieldflareup();
			         }
                  else
			         {
			         ship.script.$customshieldsdrawshieldflashers();
			         }
			      }
               }
            if((ship.script.customshieldsforwardshieldlevel === 0) && (impactangle <= 1.57) && (player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200))//determine if effect should be drawn, use "isVisible" in 1.77
               {
		       ship.script.$customshieldsdrawunshieldedhits();
		       }
            if((ship.script.customshieldsaftshieldlevel > 0) && (impactangle > 1.57))//choose the shield that was hit
               {// the shield is depleted
               ship.script.customshieldsaftshieldlevel -= amount;//subtract from shield
               var aftshieldrecovery = amount;
               if(ship.script.customshieldsaftshieldlevel < 0)//shield minimum is 0, give overflow damage to energy
                  {
                  aftshieldrecovery += ship.script.customshieldsaftshieldlevel;
			      var transferredamount = (ship.script.customshieldsaftshieldlevel * -1);
                  ship.script.customshieldsaftshieldlevel = 0;
			      if(((ship.equipmentStatus("EQ_SHIELD_EQUALIZER") === "EQUIPMENT_OK") || (ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK")) && (ship.script.customshieldsforwardshieldlevel > 0))//take from the opposite shield before energy adjustment
                     {
			         ship.script.customshieldsforwardshieldlevel -= transferredamount;
                     var forwardshieldrecovery = transferredamount;
                     if(ship.script.customshieldsforwardshieldlevel < 0)//shield minimum is 0, give overflow damage to energy
                        {
                        forwardshieldrecovery += ship.script.customshieldsforwardshieldlevel;
                        ship.script.customshieldsforwardshieldlevel = 0;
				        aftshieldrecovery = forwardshieldrecovery;
                        }
			         }
                  }
               if((ship.equipmentStatus("EQ_SHIELD_EQUALIZER") === "EQUIPMENT_OK") || (ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK"))//final division of shield energy between shields
                  {
			      if((ship.equipmentStatus("EQ_SC_SHIELD_CYCLER_INTERNAL") === "EQUIPMENT_OK") && (ship.energy > 5))
			         {
			         ship.energy -= 4.0;//shield cycler transfer energy loss(minimum)
			         }
                  var shieldtransfer = ((ship.script.customshieldsforwardshieldlevel + ship.script.customshieldsaftshieldlevel) / 2);
                  ship.script.customshieldsforwardshieldlevel = shieldtransfer;
                  ship.script.customshieldsaftshieldlevel = shieldtransfer;
			      }
               ship.energy += aftshieldrecovery;//restore energy	 		 
               if(player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200)//determine if effect should be drawn, use "isVisible" in 1.77
		          {
			      ship.script.customshieldsdrawshieldflashers = (ship.script.customshieldsaftshieldlevel / ship.script.customshieldsmaxaftshieldlevel);
			      if((type === "scrape damage") && (whom.mass > 10000 || whom.velocity.magnitude > 500))
		             {
			         ship.script.$customshieldsdrawshieldflareup();
			         }
                  else
			         {
			         ship.script.$customshieldsdrawshieldflashers();
			         }
			      }
               }
            if((ship.script.customshieldsaftshieldlevel === 0) && (impactangle > 1.57) && (player.ship.isValid && player.ship.position.distanceTo(ship.position) < 51200))//determine if effect should be drawn, use "isVisible" in 1.77
               {
		       ship.script.$customshieldsdrawunshieldedhits();
		       }		 
            if(ship.script.customshieldsshieldrechargeTimer)//start shield recharge
		       {
		       ship.script.customshieldsshieldrechargeTimer.start();
               }
            else
		       {
		       ship.script.customshieldsshieldrechargeTimer = new Timer(ship.script, ship.script.$customshieldsshieldrecharge, 0.5, 0.5);
               }		 
            if(ship.script.$customshieldsshiptakingdamage)
		       {
               ship.script.$customshieldsshiptakingdamage.apply(this, arguments);
               }
            }
		 }
      ship.script.$customshieldsdrawunshieldedhits = function()
         {
		 var hitrange = ship.script.customshieldshitrange;
         var hitchance = Math.ceil(Math.random() * hitrange);
         switch(hitchance)
            {   
            case 1://10% nothing
	        case 2:
	        case 3:
	        case 4:
	        case 5:
	           {
		       break;
		       }
	        case 6://60% sparks
	        case 7:
	        case 8:
	        case 9:
	        case 10:
	        case 11:
	        case 12:
	        case 13:
	        case 14:
	        case 15:
	        case 16:
	        case 17:
	        case 18:
	        case 19:
	        case 20:
	        case 21:
	        case 22:
	        case 23:
	        case 24:
	        case 25:
	        case 26:
	        case 27:
	        case 28:
	        case 29:
	        case 30:
	        case 31:
	        case 32:
	        case 33:
	        case 34:
	        case 35:
               {
               var sparks = system.addShips("customshieldssparks", 1, ship.script.customshieldsimpactlocation, 0);
               break;
               }
            case 36://20% blown panels
            case 37:
	        case 38:
	        case 39:
	        case 40:
	        case 41:
	        case 42:
	        case 43:
	        case 44:
	        case 45:
               {
               var il = ship.script.customshieldsimpactlocation;
               var id = ship.script.customshieldsimpactdirection;
		       var sparksblownpanels = ship.spawnOne("customshieldssparksblownpanels");
               sparksblownpanels.position = il;
		       sparksblownpanels.orientation = id.rotationTo([0, 0, 1]);
		       sparksblownpanels.velocity = ship.velocity.add(sparksblownpanels.vectorForward.multiply((Math.ceil(Math.random() * 150)) + 50));
               break;
               }
            case 46://currently nothing, future expansion
            case 47:
	        case 48:
	        case 49:
	           {
		       break;
		       }
            case 50://fuel leak caused by hull rupture
               {
			   if(ship.script.customshieldsfuelleakflag === 1.0)
			      {
			      break;
			      }
               this.customshieldsfuelleakflag = 1.0;
			   var il = ship.script.customshieldsimpactlocation;
               var id = ship.script.customshieldsimpactdirection;
		       var sparksfuelleaks = ship.spawnOne("customshieldssparksfuelleaks");
               sparksfuelleaks.position = il;
		       sparksfuelleaks.orientation = id.rotationTo([0, 0, 1]);
               sparksfuelleaks.target = ship;
               break;
               }
            }
         }  	  
      ship.script.$customshieldsdrawshieldflareup = function()
         {
         var dsf = ship.script.customshieldsdrawshieldflashers;
	     var sfs = ship.script.customshieldsshieldflashersize;
         var il = ship.script.customshieldsimpactlocation;
         dsf = Math.floor(dsf * 10);
         dsf = (dsf / 10);
         switch(dsf)
            {   
            case 0.9:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupA" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.8:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupB" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.7:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupC" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.6:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupD" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.5:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupE" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.4:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupF" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.3:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupG" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.2:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupH" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.1:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupI" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            case 0.0:
               {
		       var shieldflareup = ship.spawnOne("customshieldsshieldflasherflareupJ" + sfs);
               shieldflareup.position = il;
               shieldflareup.target = ship;
               break;
               }
            }
         }   
      ship.script.$customshieldsdrawshieldflashers = function()
         {
	     var customshieldsshieldcolor = ship.script.customshieldsshieldcolor;
	     var dsf = ship.script.customshieldsdrawshieldflashers;
	     var sfs = ship.script.customshieldsshieldflashersize;
	     var il = ship.script.customshieldsimpactlocation;
	     var ad = ship.script.customshieldsattackerdirection;
	     dsf = Math.floor(dsf * 10);
         dsf = (dsf / 10);
	     switch(dsf)
            {   
            case 0.9:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "A" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.8:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "B" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.7:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "C" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.6:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "D" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.5:
               {
		       var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "E" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.4:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "F" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.3:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "G" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.2:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "H" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.1:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "I" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            case 0.0:
               {
			   var shieldflashers = ship.spawnOne("customshieldsshieldflasher" + customshieldsshieldcolor + "J" + sfs);
			   shieldflashers.position = il;
			   shieldflashers.orientation = ad.rotationTo([0, 0, 1]);
			   shieldflashers.target = ship;
			   break;
			   }
            }
         }
      ship.script.$customshieldsshieldrecharge = function()
         {
         if(ship.energy < ship.maxEnergy || ship.isDerelict)// recharge shields after energy has been recharged and derelicts have no shields to recharge
            {
		    if((ship.energy > ship.script.customshieldslastenergylevel) && ((ship.energy - ship.script.customshieldslastenergylevel) <= 10.0))//level went up but not much indicates charging rather than a one shot oxp energy replacement
		       {
			   ship.script.customshieldsenergyrecharged = ((ship.energy - ship.script.customshieldslastenergylevel) * ship.script.customshieldsshieldrechargingratemultiplier);
			   ship.energy += ship.script.customshieldsenergyrecharged;//add any multiplier of the ships built-in energy recharge rate to the ship's energy
			   ship.script.customshieldslastenergylevel = ship.energy;
			   }
            return;
            }
         ship.script.customshieldsforwardshieldlevel += ship.script.customshieldsshieldrechargingrate;
	     ship.script.customshieldsaftshieldlevel += ship.script.customshieldsshieldrechargingrate;
         if(ship.script.customshieldsforwardshieldlevel >= ship.script.customshieldsmaxforwardshieldlevel)
            {
            ship.script.customshieldsforwardshieldlevel = ship.script.customshieldsmaxforwardshieldlevel;//forward shield fully charged
            if(ship.script.customshieldsaftshieldlevel >= ship.script.customshieldsmaxaftshieldlevel)
               {
               ship.script.customshieldsaftshieldlevel = ship.script.customshieldsmaxaftshieldlevel;//aft shield fully charged
               ship.script.customshieldsshieldrechargeTimer.stop();//stop timer when both shields fully charged
			   return;
               }
            return;
            }
         if(ship.script.customshieldsaftshieldlevel >= ship.script.customshieldsmaxaftshieldlevel)
            {
            ship.script.customshieldsaftshieldlevel = ship.script.customshieldsmaxaftshieldlevel;//aft shield fully charged
            return;
		    }     
         }	  
      ship.script.$customshieldsshipdied = ship.script.shipDied;
      ship.script.shipDied = function(whom, why)
         {
         if(ship.script.customshieldsshieldrechargeTimer)// recharge timer is stopped and removed if the ship dies
            {
            ship.script.customshieldsshieldrechargeTimer.stop();
            delete ship.script.customshieldsshieldrechargeTimer;
            }
         if(ship.script.$customshieldsshipdied)
	        {
            ship.script.$customshieldsshipdied.apply(this, arguments);
		    }
         }
      }
   } |