Scripts/mfdfastconfiguration.js |
"use strict";
this.name = "MFDFastConfiguration";
this.author = "phkb";
this.copyright = "2016 phkb";
this.description = "Primable equipment that allows different sets of MFD's to be quickly shown.";
this.licence = "CC BY-NC-SA 3.0";
// need to test without HUD Selector installed
this._sets = []; // our data array of different MFD sets
this._startpoint = 0; // starting point for the mode, which will either be 0 or the altstart point
this._curr_mode = -1; // current mode of the primable equipment
this._altstart = 21; // alternate starting point when "Hide all setting functions" is enabled
this._default = ""; // default MFD set, to be activated when docking so HUD Selector picks up the right MFD's
this._hudSelectorInstalled = false;
this._currentMFDSet = [];
this._activeMode = true;
this._currentSetName = "";
this._lastSetName = "";
this._playerIsDocking = false; // flag to indicate the player has requested docking clearance
this._dockTimer = null; // timer used when docking to determine if the player has exceeded the alotted time
this._trueValues = ["yes", "1", 1, "true", true];
this._changing = [];
// list of different modes
this._modes = [
"Set current config as set 1",
"Set current config as set 2",
"Set current config as set 3",
"Set current config as Condition Green set",
"Set current config as Condition Yellow set",
"Set current config as Condition Red set",
"Set current config as docking set",
"Make set 1 default",
"Make set 2 default",
"Make set 3 default",
"Make Condition Green set default",
"Make Condition Yellow set default",
"Make Condition Red set default",
"Delete set 1",
"Delete set 2",
"Delete set 3",
"Delete Condition Green set",
"Delete Condition Yellow set",
"Delete Condition Red set",
"Delete docking set",
"Hide all setting functions from this list",
"Activate default set",
"Activate set 1",
"Activate set 2",
"Activate set 3",
"Activate Condition Green set",
"Activate Condition Yellow set",
"Activate Condition Red set",
"Activate docking set",
"Disable auto-update mode",
"Restore all setting functions to this list"
];
//-------------------------------------------------------------------------------------------------------------
this.activated = function() {
var w = worldScripts.MFDFastConfiguration;
switch (w._curr_mode) {
case 0:
w.$storeMFDSet("set1", false);
w._currentSetName = "set1";
player.consoleMessage("Current config saved as set 1");
break;
case 1:
w.$storeMFDSet("set2", false);
w._currentSetName = "set2";
player.consoleMessage("Current config saved as set 2");
break;
case 2:
w.$storeMFDSet("set3", false);
w._currentSetName = "set3";
player.consoleMessage("Current config saved as set 3");
break;
case 3:
w.$storeMFDSet("setGreen", false);
w._currentSetName = "setGreen";
player.consoleMessage("Current config saved as Condition Green set");
break;
case 4:
w.$storeMFDSet("setYellow", false);
w._currentSetName = "setYellow";
player.consoleMessage("Current config saved as Condition Yellow set");
break;
case 5:
w.$storeMFDSet("setRed", false);
w._currentSetName = "setRed";
player.consoleMessage("Current config saved as Condition Red set");
break;
case 6:
w.$storeMFDSet("setDocking", false);
w._currentSetName = "setDocking";
player.consoleMessage("Current config saved as Docking set");
break;
case 7:
w._default = "set1";
player.consoleMessage("Default now Set 1");
break;
case 8:
w._default = "set2";
player.consoleMessage("Default now Set 2");
break;
case 9:
w._default = "set3";
player.consoleMessage("Default now Set 3");
break;
case 10:
w._default = "setGreen";
player.consoleMessage("Default now Condition Green Set");
break;
case 11:
w._default = "setYellow";
player.consoleMessage("Default now Condition Yellow Set");
break;
case 12:
w._default = "setRed";
player.consoleMessage("Default now Condition Red Set");
break;
case 13:
w.$deleteMFDSet("set1");
break;
case 14:
w.$deleteMFDSet("set2");
break;
case 15:
w.$deleteMFDSet("set3");
break;
case 16:
w.$deleteMFDSet("setGreen");
break;
case 17:
w.$deleteMFDSet("setYellow");
break;
case 18:
w.$deleteMFDSet("setRed");
break;
case 19:
w.$deleteMFDSet("setDocking");
break;
case 20:
w._startpoint = this._altstart;
player.consoleMessage("Setting functions now hidden");
break;
case 21:
if (w._default != "") w.$restoreMFDSet(w._default, false);
break;
case 22:
//this.$updateCurrentSet();
w.$restoreMFDSet("set1", false);
break;
case 23:
this.$updateCurrentSet();
w.$restoreMFDSet("set2", false);
break;
case 24:
this.$updateCurrentSet();
w.$restoreMFDSet("set3", false);
break;
case 25:
this.$updateCurrentSet();
w.$restoreMFDSet("setGreen", false);
break;
case 26:
this.$updateCurrentSet();
w.$restoreMFDSet("setYellow", false);
break;
case 27:
this.$updateCurrentSet();
w.$restoreMFDSet("setRed", false);
break;
case 28:
this.$updateCurrentSet();
w.$restoreMFDSet("setDocking", false);
break;
case 29:
w._activeMode = !w._activeMode;
if (w._activeMode === true) {
w._modes[29] = "Disable auto-update mode";
player.consoleMessage("Auto-update mode enabled");
}
if (w._activeMode === false) {
w._modes[29] = "Enable auto-update mode";
player.consoleMessage("Auto-update mode disabled");
}
break;
case 30:
w._startpoint = 0;
w._curr_mode = -1;
player.consoleMessage("Setting functions restored");
break;
}
}
//-------------------------------------------------------------------------------------------------------------
this.mode = function() {
var w = worldScripts.MFDFastConfiguration;
w._curr_mode += 1;
// skip the last one (restore functions) if they're in the list already.
if (w._curr_mode == w._modes.length - 1 && w._startpoint === 0) w._curr_mode += 1;
if (w._curr_mode == w._modes.length) w._curr_mode = w._startpoint;
// tell the player what mode they're in
player.consoleMessage(w._modes[w._curr_mode]);
}
//-------------------------------------------------------------------------------------------------------------
this.$deleteMFDSet = function(setName) {
// see if the set is stored
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
// found it! delete it and notify the player
this._sets.splice(idx, 1);
player.consoleMessage("MFD set deleted");
} else {
// notify the player that the set wasn't there to be deleted
player.consoleMessage("MFD set not found");
}
if (this._currentSetName === setName) this._currentSetName = "";
}
//-------------------------------------------------------------------------------------------------------------
this.$storeMFDSet = function(setName, suppressMessages, autoSave) {
var p = player.ship;
var config = {};
// put all the settings in a dictionary object
if (p && p.multiFunctionDisplayList) {
for (var i = 0; i < p.multiFunctionDisplayList.length; i++) {
var noadd = false;
if (autoSave === true) {
// these two MFDs autohide themselves, so don't add them to the config
if (p.multiFunctionDisplayList[i] === "DamageReportMFD" && worldScripts.DamageReportMFD && worldScripts.DamageReportMFD._mode === 1) noadd = true;
if (p.multiFunctionDisplayList[i] === "BountySystem_WarrantScanner" && worldScripts.BountySystem_WarrantScanner) noadd = true;
}
if (noadd === false) config["MFD_" + (i < 10 ? "0" : "") + i] = p.multiFunctionDisplayList[i];
}
}
// see if this set is already stored (meaning we're doing an update)
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
// ok, found it! reset the data here
this._sets[idx].data = JSON.stringify(config);
} else {
// this must be a new one, so store a new set
this._sets.push({name:setName, data:JSON.stringify(config)});
}
// notify the player
if (suppressMessages == false) player.consoleMessage("MFD set stored");
}
//-------------------------------------------------------------------------------------------------------------
this.$restoreMFDSet = function(setName, suppressMessages) {
var config = {};
// see if we've got this set stored
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
config = JSON.parse(this._sets[idx].data);
} else {
// report back if not
if (suppressMessages == false) {
player.consoleMessage("Selected MFD set not defined");
}
return;
}
// restore the set
var p = player.ship;
if (p && p.isValid && p.multiFunctionDisplayList) {
// check for the autohiding SDC MFD
var bSDC = false;
if (worldScripts.StationDockControl_MFD) {
if (worldScripts.StationDockControl_MFD._mfdID >= 0) {
bSDC = true;
worldScripts.StationDockControl_MFD._mfdID = -1;
}
}
for (var i = 0; i < p.multiFunctionDisplayList.length; i++) {
var itm = config["MFD_" + (i < 10 ? "0" : "") + i];
try {
p.setMultiFunctionDisplay(i, itm);
if (itm === "LaunchQueueMFD" && bSDC === true) {
log(this.name, "setting SDC " + i);
worldScripts.StationDockControl_MFD._mfdID = i;
}
}
catch (err) {
log(this.name, "!!ERROR: Unable to set MFD " + (i + 1) + " to " + itm);
}
}
// notify the player
if (suppressMessages == false) {
player.consoleMessage("MFD set activated");
}
this._currentSetName = setName;
// if the player has made changes outside of active mode, make sure they stay
// only applies to automatic changes based on condition
if (this._activeMode === false) {
for (var i = 0; i < this._changing.length; i++) {
if (this._changing[i] != "" && this._changing[i] != undefined) {
p.setMultiFunctionDisplay(i, this._changing[i]);
}
}
}
}
p = null;
}
//-------------------------------------------------------------------------------------------------------------
this.$setNameIndex = function(setName) {
for (var i = 0; i < this._sets.length; i++) {
if (this._sets[i].name == setName) return i;
}
return -1;
}
//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function() {
if (missionVariables.MFDFastConfig_Data) {
this._sets = JSON.parse(missionVariables.MFDFastConfig_Data);
delete missionVariables.MFDFastConfig_Data;
}
if (missionVariables.MFDFastConfig_Default) {
this._default = missionVariables.MFDFastConfig_Default;
delete missionVariables.MFDFastConfig_Default;
}
if (missionVariables.MFDFastConfig_HideSettings) {
if (missionVariables.MFDFastConfig_HideSettings == false) {
this._startpoint = 0;
} else {
this._startpoint = this._altstart;
this._curr_mode = this._startpoint;
}
delete missionVariables.MFDFastConfig_HideSettings;
}
if (missionVariables.MFDFastConfig_AutoUpdate) {
this._activeMode = (this._trueValues.indexOf(missionVariables.MFDFastConfig_AutoUpdate) === -1 ? false : true);
if (this._activeMode === true) this._modes[29] = "Disable auto-update mode";
if (this._activeMode === false) this._modes[29] = "Enable auto-update mode";
}
if (worldScripts.hudselector) this._hudSelectorInstalled = true;
}
//-------------------------------------------------------------------------------------------------------------
this.alertConditionChanged = function(newCondition, oldCondition) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// note: I'm spoofing an alert condition of "4", meaning "player is docking"
if (newCondition === 0) return;
var idx = 0;
var idxGr = this.$setNameIndex("setGreen");
var idxYw = this.$setNameIndex("setYellow");
var idxRd = this.$setNameIndex("setRed");
var idxDk = this.$setNameIndex("setDocking");
if (["set1","set2","set3","setGreen","setYellow","setRed","setDocking"].indexOf(this._currentSetName) >= 0) {
this._lastSetName = this._currentSetName;
}
// auto store the old state before we switch to any alert-condition set (if there are any)
// but only if we have an alert condition set to switch to
if (this._activeMode === true && ((newCondition === 1 && idxGr >= 0) || (newCondition === 2 && idxYw >= 0) || (newCondition === 3 && idxRd >= 0) || (newCondition === 4 && idxDk >= 0))) {
this.$updateCurrentSet();
}
if (newCondition === 1) {
if (idxGr >= 0) this.$restoreMFDSet("setGreen", false);
}
if (newCondition === 2) {
if (idxYw >= 0) this.$restoreMFDSet("setYellow", false);
}
if (newCondition === 3) {
if (idxRd >= 0) this.$restoreMFDSet("setRed", false);
}
if (newCondition === 4) {
if (idxDk >= 0) this.$restoreMFDSet("setDocking", false);
}
}
//-------------------------------------------------------------------------------------------------------------
this.$updateCurrentSet = function() {
// auto store the old state before we switch to any alert-condition set (if there are any)
if (this._activeMode === true) {
// check for currently active set and update
if (["set1","set2","set3","setGreen","setYellow","setRed","setDocking"].indexOf(this._currentSetName) >= 0) {
var idx = this.$setNameIndex(this._currentSetName);
if (idx >= 0) this.$storeMFDSet(this._currentSetName, true, true);
}
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function() {
// store all our data
if (this._sets.length > 0) {
missionVariables.MFDFastConfig_Data = JSON.stringify(this._sets);
}
missionVariables.MFDFastConfig_Default = this._default;
if (this._startpoint != 0) {
missionVariables.MFDFastConfig_HideSettings = true;
} else {
missionVariables.MFDFastConfig_HideSettings = false;
}
missionVariables.MFDFastConfig_AutoUpdate = this._activeMode;
}
//-------------------------------------------------------------------------------------------------------------
this.shipWillDockWithStation = function(station) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// auto store the old state before we switch to any alert-condition set (if there are any)
this.$updateCurrentSet();
this._changing.length = 0;
// if we have a docking set, and there's a previous set in play, and we don't have a default
// switch to the previous set we had so it can be made the default
// this is to avoid having the docking set pulled up on launch
if (this._currentSetName === "setDocking" && this._lastSetName !== "" && this._default === "") {
this.$restoreMFDSet(this._lastSetName, false);
}
if (this._hudSelectorInstalled == false && this._default == "") {
// save the current config so we can put it back on launch
this.$storeMFDSet("current", true, true);
}
if (this._default !== "") {
// restore the default so HUD Selector picks up the correct MFD's
this.$restoreMFDSet(this._default, true);
}
this.$stopTimers();
}
//-------------------------------------------------------------------------------------------------------------
this.shipLaunchedFromStation = function(station) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
this._playerIsDocking = false;
// if HUD selector isn't installed, lets restore either the default set (if defined) or what we had when we docked.
// if HUD selector is installed, it will handle restoring the MFD config for us.
if (this._hudSelectorInstalled == false) {
if (this._default != "") {
// restore the default MFD set
this.$restoreMFDSet(this._default, true);
} else {
this.$restoreMFDSet("current", true);
}
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerBoughtEquipment = function(equipmentKey) {
if (equipmentKey == "EQ_MFD_FAST_CONFIG_REMOVAL") {
// give the player their money back
player.credits += 600;
var p = player.ship;
p.removeEquipment(equipmentKey);
p.removeEquipment("EQ_MFD_FAST_CONFIG");
this._sets = [];
this._default = "";
this.startpoint = 0;
player.consoleMessage("MFD Fast Configuration device removed.");
}
}
//-------------------------------------------------------------------------------------------------------------
this.mfdKeyChanged = function(index, key) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// are we in auto mode? don't do anything if we are
if (this._activeMode === true) return;
// if not, make a note of what we're changing
if (this._changing.length === 0) this._changing = new Array(player.ship.multiFunctionDisplayList.length);
this._changing[index] = player.ship.multiFunctionDisplayList[index];
}
//-------------------------------------------------------------------------------------------------------------
this.shipDied = function(whom, why) {
this.$stopTimers();
}
//-------------------------------------------------------------------------------------------------------------
this.playerRequestedDockingClearance = function(message) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
if (message === "DOCKING_CLEARANCE_GRANTED" || message === "DOCKING_CLEARANCE_EXTENDED" || message === "DOCKING_CLEARANCE_DENIED_TRAFFIC_INBOUND" || message === "DOCKING_CLEARANCE_DENIED_TRAFFIC_OUTBOUND") {
this._playerIsDocking = true;
this.alertConditionChanged(4, player.alertCondition);
if (message === "DOCKING_CLEARANCE_GRANTED" || message === "DOCKING_CLEARANCE_EXTENDED") {
// start a timer so we can track when the clearance expires
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
this._dockTimer = new Timer(this, this.$dockingExpired, 121, 0);
}
} else {
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerDockingClearanceCancelled = function() {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
//-------------------------------------------------------------------------------------------------------------
// stops the dock timer
this.$stopTimers = function() {
// stop the dock timer check timer if it's running
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
//-------------------------------------------------------------------------------------------------------------
// checks for an expired docking request
this.$dockingExpired = function $dockingExpired() {
if (this._playerIsDocking === true) {
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
}
}
|