| Scripts/Deep_Horizon_Emergency_Witchspace_Initiator_World.js | "use strict";
this.name = "Deep_Horizon_Emer_Jump_Initiator";
this.author	= "Cmd. Cheyd (Blake Deakins)";
this.description = "Adds a prime-able piece of equipment that drastically reduces the countdown time required to initiate a hyperspace (or witchspace) jump.";
this.version = "1.0.0";
this.copyright = "� Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States license (Modified)";
//*************************************************************************************************************
//************************************         Variable Declaration        ************************************
//*************************************************************************************************************
this.$jumpDistance = 0;						// Distance to be jumped
this.$usedEWI = false;						// Flag for whether EWI was used on this jump
this.$repairMisjumpAnalyzer = false;		// Does the EWI need to repair the MisjumpAnalyzer after jump completion? 
this.$repairMisjumpInducer = false;			// Does the EWI need to repair the MisjumpInducer after jump completion?
this.$fuelLeak = false;						// Flag for determining if a fuel leak has transpired
this.$damageWitchspaceDriveUnit = false;	// Flag for determining if the engine was damaged due to shortened spin-up time
this.$notInKansasAnymore = false;			// Flag for determining if player should be sent to an alternative system.
//*************************************************************************************************************
//************************************           Event Functions           ************************************
//*************************************************************************************************************
this.shipWillLaunchFromStation = function () {
	this.gasGuzzler = false;		// Reset fuel waste flag
	this.$somethingWickedThisWayComes = false;
}
this.shipWillEnterWitchspace = function(cause) {
	if (cause == "wormhole" && !this.$notInKansasAnymore) {
		this.playerCancelledJumpCountdown();
		return;
	} else if (cause =="standard jump" && this.$usedEWI) {
		// Calculate distance about to be jumped.  Use this after the jump for fuel waste calculations
		this.$jumpDistance = Number(System.infoForSystem(galaxyNumber,system.ID).distanceToSystem(System.infoForSystem(galaxyNumber,player.ship.targetSystem)).toFixed(1));
	}
}
this.shipExitedWitchspace = function() {
	// If the EWI was used:
	if (!this.$usedEWI) return;
		// Reset EWI Usage Flag
	this.$usedEWI = false;
	// If flag for fuel waste is set:
	if (this.gasGuzzler) {
		// Calculate the amount of fuel that was wasted
		var fuelWasted = Number((this.$jumpDistance*0.1).toFixed(1));
		if (fuelWasted < 0.1) fuelWasted === 0.1;
		// Inform player of fuel expenditure
		if (fuelWasted > player.ship.fuel) {
			player.consoleMessage("All internal fuel reserves expended during emergency jump.", 4);
		} else {
			player.consoleMessage("Additional fuel expended due to inefficient routing: " + fuelWasted + " LY.", 4);
		}
		// Subtract wasted fuel from player
		player.ship.fuel -= fuelWasted;
		// Reset fuel-waste flag
		this.gasGuzzler = false;
	} else if (this.$fuelLeak) {
		// Initiate a fuel leak
		player.consoleMessage("WARNING!  Stress from short spin-time jump has caused a quirium fuel leak!", 4);
		player.ship.fuelLeakRate = 0.5;
		this.$fuelLeak = false;
	} else if (this.$damageWitchspaceDriveUnit) {
		// Simulate some sort of engine damage...?
		this._selectDamagedDriveComponent();
		this.$damageWitchspaceDriveUnit = false;
	} else if (this.$notInKansasAnymore) {
		player.commsMessage ("Navigation Failure - Witchspace jump initiated before navigation calculations were complete.",4);
		this.$notInKansasAnymore = false;
	}
	if (this.$repairMisjumpAnalyzer) {
		// Restore the Misjump Analyzer
		player.ship.setEquipmentStatus(EquipmentInfo.infoForKey("EQ_MISJUMP_ANALYSER"),"EQUIPMENT_OK");
		this.$repairMisjumpAnalyzer = false;
	}
	if (this.$repairMisjumpInducer) {
		// Restore the Misjump Inducer
		player.ship.setEquipmentStatus(EquipmentInfo.infoForKey("EQ_MISJUMPER"),"EQUIPMENT_OK");
		this.$repairMisjumpInducer = false;
	}
	if (this.$jumpTimer) {
		this.$jumpTimer.stop();
		delete this.$jumpTimer;
	}
	this.$somethingWickedThisWayComes = false;
}
this.playerStartedJumpCountdown = function(type, seconds) {
	if (type == "galactic" && this.$usedEWI) {
		player.consoleMessage ("Galactic drive startup detected.  Shutting down Emergency Witchspace Initiator.", 3);
		this.playerCancelledJumpCountdown();
	}
	if ((player.ship.equipmentStatus("EQ_DHI_WICCA") === "EQUIPMENT_DAMAGED" || player.ship.equipmentStatus("EQ_DHI_ASATRU") === "EQUIPMENT_DAMAGED" || player.ship.equipmentStatus("EQ_DHI_DRUID") === "EQUIPMENT_DAMAGED" || player.ship.equipmentStatus("EQ_DHI_SHAMAN") === "EQUIPMENT_DAMAGED" || player.ship.equipmentStatus("EQ_DHI_PAGAN") === "EQUIPMENT_DAMAGED") && type == "standard") {
		if (worldScripts["Deep_Horizon_Adv_Nav_Comp"]) {
			if (worldScripts["Deep_Horizon_Adv_Nav_Comp"].usedANC) {
				player.consoleMessage ("The Advance Navigation Computer has detected a failed drive component.  Calculations aborted.");
				worldScripts["Deep_Horizon_Adv_Nav_Comp"]._cancelANC (false);
				this.$usedEWI = false;
				return;
			}
		}
		if (this.$usedEWI) {
			player.consoleMessage ("Drive component is damaged.  Emergency jump initiation aborted.");
			player.ship.cancelHyperspaceCountdown();
			this.$usedEWI = false;
			return;
		} else {
			player.consoleMessage ("Witchspace drive component is damaged.  Jump initiation failed.");
			player.ship.cancelHyperspaceCountdown();
			this.$usedEWI = false;
			return;
		}
	}
	if (this.$somethingWickedThisWayComes) this._selectAccidentType();
	if (this.$notInKansasAnymore) {
		this._selectAlternateWormholeDestination();
		var spin_time = player.ship.hyperspaceSpinTime;
		if (spin_time < 5 || spin_time > 60) spin_time = 15; 
		var delay = (Math.floor(spin_time*0.5)-0.25);
		this.$jumpTimer = new Timer(this,this._spawnAltJumper,delay,0);
	}
}
this.playerCancelledJumpCountdown = function() {
	// Was EWI used to initiate the jump?
	if (this.$usedEWI) {
		// Reset EWI Usage Flag
		this.$usedEWI = false;
		this.gasGuzzler = false;
		this.$fuelLeak = false;
		this.$damageWitchspaceDriveUnit = false;
		this.$notInKansasAnymore = false;
		player.ship.scriptedMisjump = 0;
		if (this.$jumpTimer) {
			this.$jumpTimer.stop();
			delete this.$jumpTimer;
		}
	}
}
this.playerJumpFailed = function(reason) {
	// Reset EWI Usage Flag
	this.$usedEWI = false;
	this.gasGuzzler = false;
	this.$fuelLeak = false;
	this.$damageWitchspaceDriveUnit = false;
	this.$notInKansasAnymore = false;
	player.ship.scriptedMisjump = 0;
	if (this.$jumpTimer) {
		this.$jumpTimer.stop();
		delete this.$jumpTimer;
	}
}
this.equipmentRepaired = function(equipment) {
	if (equipment == "EQ_DHI_WICCA" || equipment == "EQ_DHI_ASATRU" || equipment == "EQ_DRUID" || equipment == "EQ_SHAMAN" || equipment == "EQ_PAGAN") {
		player.ship.removeEquipment(equipment);
		EquipmentInfo.infoForKey(equipment).effectiveTechLevel = 99;
	}
}
//*************************************************************************************************************
//************************************            OXP Functions            ************************************
//*************************************************************************************************************
this._ewiActivated = function() {
	if (!player.ship.targetSystem || player.ship.targetSystem == system.ID) {
		player.consoleMessage("Local System selected.  Emergency jump intiation impossible.  Shutting down.", 4);
		return;
	}
	if (System.infoForSystem(galaxyNumber,system.ID).distanceToSystem(System.infoForSystem(galaxyNumber,player.ship.targetSystem)) > player.ship.fuel ){
		player.consoleMessage("Insufficient fuel to reach destination system.  Navigation calculations unnecessary.  Shutting down.", 4);
		return;
	}
	if (player.ship.status == "STATUS_WITCHSPACE_COUNTDOWN") {
		//Determine if jump was EWI-initiated, ANC-initiated, or Standard
		if (this.$usedEWI) {
			//Player had prior EWI Countdown Running
			player.consoleMessage("Emergency Witchspace Initiation aborted by pilot.", 4);
			player.ship.cancelHyperspaceCountdown();
			this.$usedEWI = false;
			return;  //Return so as not to reinitiate the EWI jump
		} else if (worldScripts["Deep_Horizon_Adv_Nav_Comp"]) {
			// Does player have the ANC OXP?
			if (worldScripts["Deep_Horizon_Adv_Nav_Comp"].usedANC == true) {
				//Player had ANC script running
				worldScripts["Deep_Horizon_Adv_Nav_Comp"]._cancelANC (false);
				player.consoleMessage("Emergency witchspace jump initiated by pilot.", 4);
				// Do not RETURN so EWI initiates after current-jump is cancelled.
			} else {
				//Player had a standard jump or an otherwise-initiated jump downdown running
				player.ship.cancelHyperspaceCountdown();
				player.consoleMessage("Standard witchspace jump initiation procedure aborted.", 4);
				// Do not RETURN so EWI initiates after current-jump is cancelled.
			}
		} else {
			//Player had a standard jump or an otherwise-initiated jump downdown running
			player.ship.cancelHyperspaceCountdown();
			player.consoleMessage("Standard witchspace jump initiation procedure aborted.", 4);
			// Do not RETURN so EWI initiates after current-jump is cancelled.
		}
	} else if (worldScripts["Deep_Horizon_Adv_Nav_Comp"]) {
			// Does player have the ANC OXP?
			if (worldScripts["Deep_Horizon_Adv_Nav_Comp"].usedANC == true) {
				//Player had ANC script running
				worldScripts["Deep_Horizon_Adv_Nav_Comp"]._cancelANC (false);
				player.consoleMessage("Emergency witchspace Jump initiated by pilot.", 4);
				// Do not RETURN so EWI initiates after current-jump is cancelled.
			} 
		}
	player.consoleMessage("Initiating emergency jump to " + System.systemNameForID(player.ship.targetSystem) + ".", 4);
	//disable misjump analyzer
	if (player.ship.equipmentStatus("EQ_MISJUMP_ANALYSER") == "EQUIPMENT_OK") {
		player.ship.setEquipmentStatus(EquipmentInfo.infoForKey("EQ_MISJUMP_ANALYSER"),"EQUIPMENT_DAMAGED");
		this.$repairMisjumpAnalyzer = true;
	}
	
	//disable misjump inducer
	if (player.ship.equipmentStatus("EQ_MISJUMPER") == "EQUIPMENT_OK") {
		player.ship.setEquipmentStatus(EquipmentInfo.infoForKey("EQ_MISJUMPER"),"EQUIPMENT_DAMAGED");
		this.$repairMisjumpInducer = true;
	}
	//determine if any negative effects will happen from the jump
	this.$chance = Math.round(Math.random()*100);		// this is the chance that something bad happened
	this.$chanceOccurring = Math.ceil(100-player.ship.serviceLevel+(Math.ceil(player.ship.hyperspaceSpinTime*0.75)));		//this provides a variable ceiling-based value that something COULD have happened
	if (this.$chanceOccurring < this.$chance) {
		this.$somethingWickedThisWayComes = false;
	} else {
		this.$somethingWickedThisWayComes = true;
	}
	player.ship.beginHyperspaceCountdown(Math.floor(player.ship.hyperspaceSpinTime*0.5));
	this.$usedEWI = true;
}
this._selectAccidentType = function () {
	//determine WHICH negative effect will occur
	if (!this.$somethingWickedThisWayComes) return;
	this.$badThing = Math.floor(Math.random()*100);
	this.$badThing = 90;
	if ((this.$badThing >= 51) && (this.$badThing <= 70)) {
		// Between 51 and 70 == Fuel Waste
		if ((Number(Number(System.infoForSystem(galaxyNumber,system.ID).distanceToSystem(System.infoForSystem(galaxyNumber,player.ship.targetSystem)).toFixed(1))*1.1).toFixed(1)) > player.ship.fuel) {
			this._selectAccidentType();
			return;
		} else {
			this.gasGuzzler = true;
			player.ship.scriptedMisjump = 0;
		}
	} else if ((this.$badThing >= 71) && (this.$badThing <= 84)) {
		// Between 71 and 84 == Fuel Leak
		this.$fuelLeak = true;
		player.ship.scriptedMisjump = 0;
	} else if ((this.$badThing >= 85) && (this.$badThing <= 95)) {
		// Between 85 and 95 == Engine Damage
		this.$damageWitchspaceDriveUnit = true;
		player.ship.scriptedMisjump = 0;
	} else if ((this.$badThing >= 96) && (this.$badThing <= 100)) {
		// Between 96 and 100 == Witchspace to the wrong destination system
		this.$notInKansasAnymore = true;
	} else {
		// Between 0 and 50 == Misjump
		player.ship.scriptedMisjump = 1;
	}
}
this._selectDamagedDriveComponent = function() {
		//Award a 'broken' piece of engine equipment.
		var gear_damaged = Number(Math.floor(Math.random()*5).toFixed(0));
		switch (gear_damaged) {
			case 0: 				//Award EQ_DHI_WICCA
				this._awardDamagedDriveComponent("EQ_DHI_WICCA");
				break;
			case 1:					//Award EQ_DHI_ASATRU
				this._awardDamagedDriveComponent("EQ_DHI_ASATRU");
				break;
			case 2:					//Award EQ_DHI_DRUID
				this._awardDamagedDriveComponent("EQ_DHI_DRUID");
				break;
			case 3:					//Award EQ_DHI_SHAMAN
				this._awardDamagedDriveComponent("EQ_DHI_SHAMAN");
				break;
			case 4:					//Award EQ_DHI_PAGAN
				this._awardDamagedDriveComponent("EQ_DHI_PAGAN");
				break;
			default:				//Award EQ_DHI_PAGAN
				this._awardDamagedDriveComponent("EQ_DHI_PAGAN");
		}
}
this._awardDamagedDriveComponent = function (equipment) {
	if (player.ship.canAwardEquipment(equipment)) {
		player.ship.awardEquipment(equipment);
		player.ship.setEquipmentStatus(equipment,"EQUIPMENT_DAMAGED");
		EquipmentInfo.infoForKey(equipment).effectiveTechLevel = 0;
		player.commsMessage ("A component of the witchspace drive unit was damaged by short spin-time jump.",4);
	}
	else {
		this._selectDamagedDriveComponent();
	}
}
this._selectAlternateWormholeDestination = function () {
	if (!this.$notInKansasAnymore) return;
	// Select an alternative system within fuel-range and change player target to that system.
	this.$alternativeTargetSystems = new Array ();
	// Build array of systems within fuel range
	this.$alternativeTargetSystems = System.infoForSystem(galaxyNumber,system.ID).systemsInRange(player.ship.fuel);
	// If no other systems are available, select a new accident type
	if (this.$alternativeTargetSystems.length <= 2) return;
	else {
		//Delete the current player target system from the array, select a new random destination from the remaining choices, and assign it
		// Remove the player's current target system from the list of available options.
		this.$alternativeTargetSystems.splice(this.$alternativeTargetSystems.indexOf("SystemInfo galaxy "+ galaxyNumber +", system "+ player.ship.targetSystem),1);
		// Generate a random number between 0 and the end of the array
		this.$alternativeSelection = Math.floor(Math.random()*this.$alternativeTargetSystems.length);
		// Set jump-creator targetSystem to the systemID for the array member in position X
		this.$alternateDestination = this.$alternativeTargetSystems[this.$alternativeSelection].systemID
	}
}
this._spawnAltJumper = function () {
	this.$playerFuel = player.ship.fuel;
	this.$playerFuel -= System.infoForSystem(galaxyNumber,system.ID).distanceToSystem(System.infoForSystem(galaxyNumber,this.$alternateDestination));
	if (this.$playerFuel >= 0) {
		player.ship.cancelHyperspaceCountdown();
		var jumper = system.addShips("DHI_Alt_jumper",1,player.ship.position.add(player.ship.vectorForward.multiply(player.ship.collisionRadius+100)),0)[0];
		if (jumper.exitSystem(this.$alternateDestination)) {
			this._soundSource = new SoundSource;
			this._soundSource.sound = "[witchdrive-malfunction]";
			this._soundSource.play();
			this._soundSource.loop = false;
			player.ship.fuel = this.$playerFuel;
			player.ship.position = jumper.position;
			player.ship.velocity = [0,0,0];
		}
		jumper.remove(true);
	}
}
 |