WFMario / Universe Map Assistant

// ==UserScript==
// @name         Universe Map Assistant
// @version      2023-12-26
// @author       Mario
// @license MIT
// @match        https://*.atmoburn.com/extras/view_universe.php?x=*&y=*&z=*&fleet=*
// @grant        none
// ==/UserScript==

function getSystemsBeingExplored() {
    var missions = []
    for (var ID in mapMeta.f) {
        if (mapMeta.f[ID][13] !== '') {
            missions.push(mapMeta.f[ID][13])
        }
    }
    return missions
}

function isTargetInMissions(missions, target) {
    for (var mission in missions) {
        if (missions[mission][3] === target[3] && missions[mission][4] === target[4] && missions[mission][5] === target[5]) {
            return true
        }
    }
    return false
}

function isTargetBlacklisted(blacklist, target) {
   return isTargetInMissions(blacklist, target)
}

function getClosestSystem() {
    if (mapMeta && mapMeta.s) { // don't do anything if mapMeta isn't loaded
        // add system coords here if you want the system ignored. See example for format.
        var blacklist = [
            // [,,,x,y,z],
        ]

        var closestSys = 0, distance = Infinity, thisDistance, myFleet = mapMeta.f[selectedFleet];
        var missions = getSystemsBeingExplored()
        for (var ID in mapMeta.s) {
            try {
                thisDistance = getDistance(myFleet, mapMeta.s[ID]);
            } catch (error) {
                console.log(myFleet)
                console.log(mapMeta.s[ID])
            }
            if (thisDistance < distance && mapMeta.s[ID][8] != 1 && !isTargetInMissions(missions, mapMeta.s[ID]) && !isTargetBlacklisted(blacklist, mapMeta.s[ID])) {
                distance = thisDistance;
                closestSys = ID;
            }
        }
        showSystem(closestSys, myFleet[0])
    }
}

let closestButton = document.createElement('button');
closestButton.innerHTML = "Closest";
closestButton.id = "closestButton";
closestButton.className = "submit greenbutton darkbutton";
closestButton.title = "Explores next unexplored system.";
if (document.getElementsByClassName("starMapInfo alignleft")[0]) {
    closestButton.addEventListener("click", getClosestSystem);
    document.getElementsByClassName("starMapInfo alignleft")[0].append(closestButton);
}

document.onkeydown = function(e) {
    e = e || window.event;
    var keycode = e.which || e.keyCode;
    var ctrlPressed = e.ctrlKey || e.metaKey; //record if Ctrl key is pressed

    if( !ctrlPressed && document.activeElement.tagName == "BODY" ) {//skip if Ctrl key is pressed, activeElement used to skip if user has a text input active
        if( keycode == 88 ) {//'88' is the keycode for "x"
            e.preventDefault();
            getClosestSystem()
        }
    }
};