Scripts/gh_script.js |
"use strict";
this.name = "GETter_HUD";
this.author = "Reval";
this.licence = "CC BY-NC-SA 4.0";
this.version = "1.5";
this._operationalFunctionNames = [ // list of functions to enable/disable at HUDSelector callback
"playerBoughtNewShip",
"playerBoughtEquipment",
"equipmentAdded",
"shipWillLaunchFromStation",
"shipExitedWitchspace",
"guiScreenChanged",
"shipWillDockWithStation"
];
this.startUp = function() {
this.$hudSelector = worldScripts.hudselector;
if(this.$hudSelector) {
this.$hudSelector.$HUDSelectorAddHUD("GETter HUD", this.name);
this.$hudSelector.$HUDSelectorAddHUD("GETter HUD Small", this.name + "-small");
}
}
this.startUpComplete = function() {
if(!this.$hudSelector && (player.ship.hud === "hud.plist"))
player.ship.hud = this.name + ".plist";
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
this.playerBoughtNewShip = function(ship, price) {
if(!this.$hudSelector && (player.ship.hud === "hud.plist"))
player.ship.hud = this.name + ".plist";
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
this.playerBoughtEquipment = function(equipment, paid) {
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
this.equipmentAdded = function(equipmentKey) {
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
this.shipWillLaunchFromStation = function() {
if(!this.$Timer) this.$Timer = new Timer(this,this._ghupdateSpeed.bind(this),0,0.25);
if(!this.$Timer1) this.$Timer1 = new Timer(this,this._ghshowWeps.bind(this),0,0.25);
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
this.shipWillDockWithStation = function() {
if (this.$Timer && this.$Timer.isRunning) { this.$Timer.stop(); this.$Timer = null; }
if (this.$Timer1 && this.$Timer1.isRunning) { this.$Timer1.stop(); this.$Timer1 = null; }
}
this.shipExitedWitchspace = function() {
this._ghlistSystem();
}
this.guiScreenChanged = function(to, from) {
if ((from=="GUI_SCREEN_MARKET")||(from=="GUI_SCREEN_MANIFEST")) {
this._ghlistGoods();
this._ghlistOwner();
this._ghlistCap();
this._ghlistSystem();
}
}
this._ghcenteredText = function(txt, width) {
var t = ""+txt; // force to string, else txt.length is undefined for numbers
var w = width / 2;
while( t && t.length < w ) { // for fixed width font in setCustomHUDDial
t = " " + t;
}
return(t);
}
this._ghupdateSpeed = function () {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var sp = p.speed;
sp = this._ghcenteredText(sp.toFixed(1), 28); // default width
setd("shipSpeed", sp);
}
}
// output System name to HUD
this._ghlistSystem = function() {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var s = this._ghsystemList();
setd("systemList", s);
}
}
// show System name
this._ghsystemList = function() {
var result = "";
result += System.systemNameForID(system.ID);
return result;
}
// output capList to HUD
this._ghlistCap = function() {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var cap = this._ghcapList();
setd("capList", cap);
}
}
// show ship's TC capacity
this._ghcapList = function() {
var result = "";
result += "Capacity: ";
result += player.ship.cargoSpaceCapacity+" TC";
return result;
}
// output ownerList to HUD
this._ghlistOwner = function() {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var owner = this._ghownerList();
setd("ownerList", owner);
}
}
// show ship, class, name, owner
this._ghownerList = function() {
var result = "";
result += "Cdr. ";
result += player.name + ", ";
result += player.ship.displayName;
return result;
}
// output goods list to HUD
this._ghlistGoods = function() {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var goods = this._ghgoodsList();
setd("goodsList", goods);
}
}
// list commodities in ship's hold
this._ghgoodsList = function() {
var result = "";
var m = manifest.list;
for (var i=0; i<m.length; i++) {
var q = m[i].commodity;
if (q) result += q + " ("+m[i].quantity+") ";
}
if (result=="") result = "EMPTY";
return result;
}
// weapons online indicator
this._ghshowWeps = function() {
var p = player.ship;
if((!p) || (!p.isValid)) return; // player died
var setd = p.setCustomHUDDial;
if (setd) {
var weps = "";
if (p.weaponsOnline) weps="W";
setd("showWeapons", weps);
}
}
this.$HUDSelectorCallBack = function(off) {
var ws = worldScripts.GETter_HUD;
var funcNames = this._operationalFunctionNames;
var i = funcNames.length;
if (off) {
// disable
while (i--)
if (ws[funcNames[i]]) {
ws["$save_" + funcNames[i]] = ws[funcNames[i]];
delete ws[funcNames[i]];
}
} else {
// enable
while (i--)
if (!ws[funcNames[i]]) {
ws[funcNames[i]] = ws["$save_" + funcNames[i]];
}
ws.playerBoughtEquipment();
}
}
|