| Scripts/Reduce_Weapon_Damage.js | "use strict";
this.name           = "Reduce Weapon Damage";
this.author         = "Lone_Wolf";
this.copyright      = "(C) 2014 Lone_Wolf.";
this.licence        = "CC-by-SA 4.0";
this.description    = "Reduce laser/missile damage for ships";
this.version        = "0.10";
// Player ship and all npc-ships that qualify ( see this.shipSpawned handler) will have their own separate instance of this script
// using separate values for player / npc for flexibility .
this._rwd_player_reduction = 8;
this._rwd_npc_reduction = 8;
this.startUpComplete = function()
  {
    // setup handler for player.ship . npc-ships are dealt with by this.shipSpawned handler
    if ( !player.ship.script ) { player.ship.setScript("oolite-default-ship-script.js"); }; // just in case
    if ( player.ship.script.shipTakingDamage ) 
      {
	// there is already a shipTakingDamage handler, store it
	player.ship.script._rwd_player_oldShipTakingDamage = ship.script.shipTakingDamage;
      };
    player.ship.script._RWD = this; // setup pointer
    var new_std = function(amount, whom, type)
      {
	worldScripts["Reduce Weapon Damage"]._rwd_player_ShipTakingDamage(this.ship, amount, whom, type); 
	if ( player.ship.script._rwd_player_oldShipTakingDamage )
	  { 
	    player.ship.script._rwd_oldShipTakingDamage(amount, whom, type); //call the original function 
	   };
      };
    player.ship.script.shipTakingDamage = new_std;
  };
this._rwd_player_ShipTakingDamage = function (whoami, amount, whom, type)
  {
    if ( type != "energy damage" ) { return; }; // only act upon laser / missile damage
    if ( amount != 0 ) { return; }; // if hit penetrated shields, do nothing
    /* shields have absorbed hit, there is no known way to determine the strength of the hit
    * simplest solution to reduce damage is to increase shield strength
    */
    // try to determine whether forward or aft shield is hit
    if( whom && whom.isValid && ( player.ship.vectorForward.dot(player.ship.position.subtract(whom.position) ) > 0 ) ) 
      { 
	// forward shield hit
	player.ship.forwardShield += player.ship.script._RWD._rwd_player_reduction;
      }
    else
      {
	// aft hit
	player.ship.aftShield += player.ship.script._RWD._rwd_player_reduction;
      };
  };
  
this.shipSpawned = function(ship)
  {
    // called for non-player ships only
    if ( !ship || !ship.isPiloted || ship.isThargoid || ship.isStation ) { return; }; // only change npc-ships
    if ( !ship.autoWeapons ) {return; }; // ship properties are not supposed to be altered
    if ( !ship.script ) { ship.setScript("oolite-default-ship-script.js"); }; // just in case
    if ( ship.script.shipTakingDamage ) 
      {
	// there is already a shipTakingDamage handler, store it
	ship.script._rwd_npc_oldShipTakingDamage = ship.script.shipTakingDamage;
      };
    ship.script._RWD = this;
    var new_std = function(amount, whom, type)
      {
	amount = worldScripts["Reduce Weapon Damage"]._rwd_npc_ShipTakingDamage(this.ship, amount, whom, type); 
	if ( ship.script._rwd_npc_oldShipTakingDamage )
	  { 
	    ship.script._rwd_npc_oldShipTakingDamage(amount, whom, type); //call the original function 
	   };
      };
    ship.script.shipTakingDamage = new_std;
};
  
this._rwd_npc_ShipTakingDamage = function (whoami, amount, whom, type)
  {
    if ( type != "energy damage" ) { return amount; }; // only act upon laser / missile damage
    // npc ship, all that's needed is reducing damage amount
    amount -= whoami.script._RWD._rwd_npc_reduction;
    return amount;
  };
 |