| Scripts/oolite-system-data-config.js | "use strict";
this.name        = "oolite-system-data-config";
this.author      = "phkb";
this.copyright   = "2017 phkb";
this.description = "Provides an interface for configuring the layout of the System Data (F7) screen.";
this.licence     = "CC BY-NC-SA 3.0";
// set up our configuration dictionary with the default settings
this._config = {
    1: {owner:"system", text:"[sysdata-eco]\t[economy_desc]"},
    2: {owner:"", text:""},
    3: {owner:"system", text:"[sysdata-govt]\t[government_desc]"},
    4: {owner:"", text:""},
    5: {owner:"system", text:"[sysdata-tl]\t[sysdata-tl-value]"},
    6: {owner:"", text:""},
    7: {owner:"system", text:"[sysdata-pop]\t[sysdata-pop-value]"},
    8: {owner:"system", text:"\t([inhabitants])"},
    9: {owner:"", text:""},
    10: {owner:"system", text:"[sysdata-prod]\t\t[sysdata-prod-value]"},
    11: {owner:"", text:""},
    12: {owner:"system", text:"[sysdata-radius]\t\t[sysdata-radius-value]"},
    13: {owner:"", text:""},
    14: {owner:"system", text:"[sysdata-distance]\t[distanceInfo]"},
    15: {owner:"", text:""},
    16: {owner:"", text:""},
};
this._governmentList = expandDescription("[sdc_governments]").split("|");
this._economyList = expandDescription("[sdc_economies]").split("|");
this._callbacks = [];
//-------------------------------------------------------------------------------------------------------------
this.startUp = function() {
    if (worldScripts["market_tweak.js"]) {
        this._economyList = expandDescription("[sdc_sw_economies]").split("|");
    }
    this.$updateData();
}
//-------------------------------------------------------------------------------------------------------------
this.guiScreenWillChange = function(to, from) {
    if (to === "GUI_SCREEN_SYSTEM_DATA") {
        this.$performCallbacks("guiScreenWillChange", to, from);
        this.$updateData();
    }
}
//-------------------------------------------------------------------------------------------------------------
this.infoSystemWillChange = function(to, from) {
    this.$performCallbacks("infoSystemWillChange", to, from);
    this.$updateData();
}
//-------------------------------------------------------------------------------------------------------------
// returns the current owner of a particular data line
this.systemDataLineOwner = function(ln) {
    if (isNaN(ln) || ln < 1 || ln > 16) {
        throw expandDescription("[sdc_error_01]", {line:ln});
    }
    return this._config[ln].owner;
}
//-------------------------------------------------------------------------------------------------------------
// returns the current text of a particular data line
this.systemDataLineText = function(ln) {
    if (isNaN(ln) || ln < 1 || ln > 16) {
        throw expandDescription("[sdc_error_01]", {line:ln});
    }
    return this._config[ln].text;
}
//-------------------------------------------------------------------------------------------------------------
// sets the text and optionally the owner of a particular data line
this.setSystemDataLine = function(ln, text, owner) {
    if (isNaN(ln) || ln < 1 || ln > 16) {
        throw expandDescription("[sdc_error_01]", {line:ln});
    }
    if (owner) {
        // if we've been passed an owner, update the whole entry
        this._config[ln] = {owner:owner, text:text};
    } else {
        // otherwise just update the text element
        this._config[ln].text = text;
    }
    this.$updateData(ln);
}
//-------------------------------------------------------------------------------------------------------------
// resets a particular data line to being blank with no owner
this.resetSystemDataLine = function(ln) {
    if (isNaN(ln) || ln < 1 || ln > 16) {
        throw expandDescription("[sdc_error_01]", {line:ln});
    }
    this._config[ln] = {owner:"", text:""};
    this.$updateData(ln);
}
//-------------------------------------------------------------------------------------------------------------
// adds a worldscript/callback function name to the list of callbacks to execute whenever the information
// on the F7 screen is about to change
this.addChangeCallback = function(ws, cb) {
    var found = false;
    if (this._callbacks.length > 0) {
        for (var i = 0; i < this._callbacks.length; i++) {
            if (this._callbacks[i].worldScript === ws && this._callbacks[i].callback === cb) {
                found = true;
                break;
            }
        }
    }
    if (found === false) {
        this._callbacks.push({worldScript:ws, callback:cb});
    }
}
//-------------------------------------------------------------------------------------------------------------
// removes a worldscript/callback function name from the list of callbacks
this.removeChangeCallback = function(ws, cb) {
    for (var i = this._callbacks.length - 1; i >= 0; i--) {
        if (this._callbacks[i].worldScript === ws && this._callbacks[i].callback === cb) {
            this._callbacks.splice(i, 1);
            break;
        }
    }
}
//-------------------------------------------------------------------------------------------------------------
// executes all the callbacks
this.$performCallbacks = function(c_type, to, from) {
    if (this._callbacks.length > 0) {
        for (var i = 0; i < this._callbacks.length; i++) {
            worldScripts[this._callbacks[i].worldScript][this._callbacks[i].callback](c_type, to, from);
        }
    }
}
//-------------------------------------------------------------------------------------------------------------
// updats the data for all lines (if no line number is passed) or for a specific line
this.$updateData = function(ln) {
    // if we've been passed a line number, just update that line
    var data = this.$getSystemData(player.ship.infoSystem);
    if (ln) {
        missionVariables["oolite_sysdata_line_" + ln] = expandDescription(this._config[ln].text, data);
        return;
    }
    // otherwise update all the lines with the latest config
    for (var i = 1; i <= 16; i++) {
        missionVariables["oolite_sysdata_line_" + i] = expandDescription(this._config[i].text, data);
    }
}
//-------------------------------------------------------------------------------------------------------------
// returns a dictionary object containing the details of a particular system
this.$getSystemData = function(sysID) {
    var sys = System.infoForSystem(galaxyNumber, sysID);
    var rt = system.info.routeToSystem(sys, player.ship.routeMode);
    var data = {};
    if (sys.sun_gone_nova) {
        data["economy_desc"] = expandDescription("[nova-system-economy]");
        data["government_desc"] = expandDescription("[nova-system-government]");
        data["sysdata-tl-value"] = 0;
        data["population"] = 0;
        data["inhabitants"] = expandDescription("[nova-system-inhabitants]");
        data["productivity"] = 0;
        data["radius"] = 0;
    } else {
        data["economy_desc"] = this._economyList[sys.economy];
        data["government_desc"] = this._governmentList[sys.government];
        data["sysdata-tl-value"] = sys.techlevel + 1;
        data["population"] = sys.population;
        data["inhabitants"] = sys.inhabitants;
        data["productivity"] = sys.productivity;
        data["radius"] = sys.radius;
    } 
    if (rt) {
        data["distanceInfo"] = rt.distance.toFixed(1) + " " + expandDescription("[sdc_ly]") + " / " + 
            rt.time.toFixed(1) + " " + (rt.time > 0.95 && rt.time < 1.05 ? expandDescription("[sysdata-route-hours%0]") : expandDescription("[sysdata-route-hours%1]"))  + " / " + 
            (rt.route.length - 1) + " " + (rt.route.length === 2 ? expandDescription("[sysdata-route-jumps%0]") : expandDescription("[sysdata-route-jumps%1]"));
    } else {
        data["distanceInfo"] = system.info.distanceToSystem(sys).toFixed(1) + " " + expandDescription("[sdc_ly]");
    }
    return data;
}
 |