Downloads containing STVcustomhud.mut

Downloads
Name Author Game Mode Rating
TSF with JJ2+ Only: Custom HUD Spaz Electro Mutator N/A Download file

File preview

// TODO: make teamscore work with both teams, also add support for
// 3/4 team CTF

#pragma name "Custom HUD"
#pragma require "STVutil.asc"

#include "STVutil.asc"

// for intellisense
#include "../../scripts/STVutil.asc"

bool toggled_on = true;

bool locatingItem = false;
string itemBeingLocated = "";

string settingsVersion = "0.1P";

array<int> upgradedIds;
array<int> unupgradedIds;

int gridSize = 10;

// the magic starts here

int ammoX = 700;
int ammoY = 585;
STRING::Size ammoSize = STRING::MEDIUM;
bool ammoEnabled = true;

int healthX       = 785;
int healthY       = 10;
int healthSize    = 1;
int healthPadding = 15;
bool healthEnabled = true;

int teamScoreX    = 4;
int teamScoreY    = 12;
STRING::Size teamScoreSize = STRING::MEDIUM;
bool teamScoreEnabled = false;

// the magic ends here

AnimatedSprite@ gun = AnimatedSprite(ANIM::PICKUPS, 0, 0, 0, 0.25, false);

int snap(int spot) {
    return (spot + gridSize / 2) / gridSize * gridSize;
}

bool upgraded(jjPLAYER @player, int id) { return player.powerup[id]; }

int idToSprite(jjPLAYER@ player, int id)
{
    if(id == 1) {
        if(
            gun.animSet == ANIM::PLUS_AMMO && player.charCurr != CHAR::LORI
        ) gun.setAnimSet(ANIM::PICKUPS);

        if(player.charCurr == CHAR::JAZZ) {
            if(upgraded(player, 1)) {
                gun.setAnimSet(ANIM::AMMO);
                return 18;
            }

            gun.setAnimSet(ANIM::PICKUPS);

            return 29;
        } else if(player.charCurr == CHAR::SPAZ) {
            if(upgraded(player, 1)) {
                gun.setAnimSet(ANIM::AMMO);
                return 19;
            }

            gun.setAnimSet(ANIM::PICKUPS);
            return 30;
        } else if(player.charCurr == CHAR::LORI) {
            gun.setAnimSet(ANIM::PLUS_AMMO);
            
            return 5;
        } else if(player.charCurr == CHAR::BIRD2) {
            if(player.charOrig == CHAR::LORI) {
                gun.setAnimSet(ANIM::PLUS_AMMO);

                if(upgraded(player, 1)) {
                    return 6;
                } else {
                    return 5;
                }
            }

            if(upgraded(player, 1)) {
                gun.setAnimSet(ANIM::AMMO);
                return player.charOrig == CHAR::JAZZ ? 18 : 19;
            }

            gun.setAnimSet(ANIM::PICKUPS);

            return player.charOrig == CHAR::JAZZ ? 29 : 30;
        }

        gun.setVisible(false);
        return 0;
    }

    gun.setAnimSet(ANIM::AMMO);

    return upgraded(player, id) ? upgradedIds[id - 2] : unupgradedIds[id - 2];
}

jjSTREAM saveSettings() {
    jjSTREAM settingsStream = jjSTREAM();
    settingsStream.push(settingsVersion);
    settingsStream.push(toggled_on);
    
    // the magic starts here
    settingsStream.push(formatInt(ammoX));
    settingsStream.push(formatInt(ammoY));
    settingsStream.push(formatSize(ammoSize));
    settingsStream.push(ammoEnabled);
    
    settingsStream.push(formatInt(healthX));
    settingsStream.push(formatInt(healthY));
    settingsStream.push(formatInt(healthSize));
    settingsStream.push(formatInt(healthPadding));
    settingsStream.push(healthEnabled);
    
    settingsStream.push(formatInt(teamScoreX));
    settingsStream.push(formatInt(teamScoreY));
    settingsStream.push(formatSize(teamScoreSize));
    settingsStream.push(teamScoreEnabled);
    
    settingsStream.push(formatInt(gridSize));

    // the magic ends here

    settingsStream.save("STVcustomhud-" + p.nameUnformatted + ".asdat");
    
    return settingsStream;
}

void backupData() {
    jjSTREAM("STVcustomhud-" + p.nameUnformatted + ".asdat").save("STVcustomhud-restore-" + p.nameUnformatted + ".asdat");
}

void loadSettings(jjSTREAM settingsStream) {
    string version;
    if(!settingsStream.pop(version)) {
        jjConsole("[CH] Version not found!");
        return;
    }

    if(version != settingsVersion) {
        backupData();
        jjConsole("[CH] Version mismatch found! Your old settings have been saved");
        jjConsole("... into STVcustomhud-restore-" + p.nameUnformatted + ".asdat");
        saveSettings();
        return;
    }

    settingsStream.pop(toggled_on);

    string ammoXStr, ammoYStr, ammoSizeStr;
    settingsStream.pop(ammoXStr);
    settingsStream.pop(ammoYStr);
    settingsStream.pop(ammoSizeStr);
    settingsStream.pop(ammoEnabled);

    ammoX = parseInt(ammoXStr);
    ammoY = parseInt(ammoYStr);
    ammoSize = parseSize(ammoSizeStr);

    string healthXStr, healthYStr, healthSizeStr, healthPaddingStr;
    
    settingsStream.pop(healthXStr);
    settingsStream.pop(healthYStr);
    settingsStream.pop(healthSizeStr);
    settingsStream.pop(healthPaddingStr);
    settingsStream.pop(healthEnabled);
    
    healthX = parseInt(healthXStr);
    healthY = parseInt(healthYStr);
    healthSize = parseInt(healthSizeStr);
    healthPadding = parseInt(healthPaddingStr);

    string teamScoreXStr, teamScoreYStr, teamScoreSizeStr;
    settingsStream.pop(teamScoreXStr);
    settingsStream.pop(teamScoreYStr);
    settingsStream.pop(teamScoreSizeStr);
    settingsStream.pop(teamScoreEnabled);

    teamScoreX = parseInt(teamScoreXStr);
    teamScoreY = parseInt(teamScoreYStr);
    teamScoreSize = parseSize(teamScoreSizeStr);

    string snappingStr;
    settingsStream.pop(snappingStr);

    gridSize = parseInt(snappingStr);
}

void onLevelBegin() {
    // jjConsole("[CH] Started!");
    
    jjSTREAM settingsStream("STVcustomhud-" + p.nameUnformatted + ".asdat");
    
    if(settingsStream.isEmpty()) {
        settingsStream = saveSettings();
    }

    loadSettings(jjSTREAM("STVcustomhud-" + p.nameUnformatted + ".asdat"));

    // jjConsole("[CH] Loaded settings!");

    unupgradedIds.insertLast(25); // bouncer
    unupgradedIds.insertLast(29); // ice
    unupgradedIds.insertLast(34); // seeker
    unupgradedIds.insertLast(49); // rf
    unupgradedIds.insertLast(57); // toaster
    unupgradedIds.insertLast(59); // tnt
    unupgradedIds.insertLast(62); // fireball
    unupgradedIds.insertLast(68); // electroblaster

    upgradedIds.insertLast(24); // bouncer
    upgradedIds.insertLast(28); // ice
    upgradedIds.insertLast(33); // seeker
    upgradedIds.insertLast(48); // rf
    upgradedIds.insertLast(56); // toaster
    upgradedIds.insertLast(59); // tnt
    upgradedIds.insertLast(61); // fireball
    upgradedIds.insertLast(67); // electroblaster

    // jjConsole("[CH] Loaded ids!");
}

void onPlayer(jjPLAYER@ player) {
    if(locatingItem) {
        int x = snap(jjMouseX);
        int y = snap(jjMouseY);
        
        if(itemBeingLocated == "ammo") {
            ammoX = x;
            ammoY = y;
        }
        
        if(itemBeingLocated == "health") {
            healthX = x;
            healthY = y;
        }
        
        if(itemBeingLocated == "teamscore") {
            teamScoreX = x;
            teamScoreY = y;
        }

        if(jjKey[0x1]) {
            locatingItem = false;
            itemBeingLocated = "";
            saveSettings();
        }
    }

    gun.setId(idToSprite(player, player.currWeapon));
    
    gun.update();

    if(!(
        player.charCurr == CHAR::JAZZ ||
        player.charCurr == CHAR::SPAZ ||
        player.charCurr == CHAR::LORI ||
        player.charCurr == CHAR::BIRD2
    )) {
        gun.visible = false;
    } else {
        if(!ammoEnabled && itemBeingLocated == "ammo") { gun.visible = true; } else { gun.visible = ammoEnabled; }
    }
}

bool onLocalChat(string &in stringReceived, CHAT::Type chatType) {
    if(stringReceived.findFirst("!locate") == 0) {
        if(stringReceived.split(" ").length() == 1) {
            jjConsole("[CH] Provide something to locate, options: ammo, health!");
            return true;
        }

        string element = stringReceived.split(" ")[1];

        if(element != "ammo" && element != "health" && element != "teamscore") {
            jjConsole("[CH] " + element + " does not exist, options: ammo, health!");
            return true;
        }

        itemBeingLocated = element;
        locatingItem = true;

        return true;
    }

    if(stringReceived == "!hudenable") {
        toggled_on = true;

        saveSettings();

        return true;
    }

    if(stringReceived == "!huddisable") {
        toggled_on = false;

        saveSettings();
        
        return true;
    }

    if(stringReceived.findFirst("!gridsize") == 0) {
        if(stringReceived.split(" ").length() == 1) {
            jjConsole("[CH] Provide a number to set the grid size to!");
            return true;
        }

        int snapping = parseInt(stringReceived.split(" ")[1]);

        gridSize = snapping;

        jjConsole("[CH] Grid size has been set to " + snapping + "!");
        
        saveSettings();

        return true;
    }

    if(stringReceived.findFirst("!hudtoggle") == 0) {
        if(stringReceived.split(" ").length() == 1) {
            jjConsole("[CH] Provide something to toggle, options: ammo, health!");
            return true;
        }

        string element = stringReceived.split(" ")[1];

        if(element == "ammo") {
            ammoEnabled = !ammoEnabled;
        } else if(element == "health") {
            healthEnabled = !healthEnabled;
        } else if(element == "teamscore") {
            teamScoreEnabled = !teamScoreEnabled;
        } else {
            jjConsole("[CH] " + element + " does not exist, options: ammo, health!");
        }

        return true;
    }

    if(stringReceived == "!hudhelp") {
        jjConsole("----------------");
        jjConsole("!locate - Locate a custom HUD element from the parameter");
        jjConsole("!gridsize - Sets the locating grid size to the parameter");
        jjConsole("!hudenable - Enables the custom HUD");
        jjConsole("!huddisable - Disables the custom HUD");
        jjConsole("!hudtoggle - Toggle a custom HUD element");
        jjConsole("----------------");

        return true;
    }

    return false;
}

string determineTeamColor(TEAM::Color color) {
    if(color == TEAM::RED) return "||";
    if(color == TEAM::BLUE) return "|||";
    if(color == TEAM::YELLOW) return "||||";
    
    // green
    return "|";
}

bool onDrawAmmo(jjPLAYER@ player, jjCANVAS@ canvas) {
    // utils
    if(locatingItem) {
        int x = ammoX;
        int y = ammoY;

        if(itemBeingLocated == "health") {
            x = healthX;
            y = healthY;
        } else if(itemBeingLocated == "teamscore") {
            x = teamScoreX;
            y = teamScoreY;
        }

        x += 10;
        int xRatio = (jjResolutionWidth / 2) - snap(jjMouseX);
        int yRatio = jjResolutionHeight - snap(jjMouseY);

        if(x + jjGetStringWidth("Bottom center ratio: " + xRatio + "x" + yRatio, STRING::SMALL, normalTextAppearance) >= jjResolutionWidth) {
            x -= 200;
        }

        if(y <= 50) {
            y += 80;
        }

        canvas.drawString(x, y - 10, "" + snap(jjMouseX) + " - " + snap(jjMouseY), STRING::SMALL);
        canvas.drawString(x, y - 20, "Ratio: " + (jjResolutionWidth - snap(jjMouseX)) + "x" + (jjResolutionHeight - snap(jjMouseY)), STRING::SMALL);
        canvas.drawString(x, y - 30, "Center ratio: " + xRatio + "x" + ((jjResolutionHeight / 2) - snap(jjMouseY)), STRING::SMALL);
        canvas.drawString(x, y - 40, "Bottom center ratio: " + xRatio + "x" + yRatio, STRING::SMALL);
    }

    if(toggled_on) {
        if(ammoEnabled || itemBeingLocated == "ammo") {
            string ammo = player.currWeapon == 1 ? "^" : "" + player.ammo[player.currWeapon];

            if((
                player.charCurr == CHAR::JAZZ ||
                player.charCurr == CHAR::SPAZ ||
                player.charCurr == CHAR::LORI ||
                player.charCurr == CHAR::BIRD2
            )) {
                canvas.drawString(ammoX, ammoY, "x" + ammo, ammoSize);
            }
            
            gun.x = ammoX - 10;
            gun.y = ammoY;
            
            gun.spriteMode = (player.currWeapon == 1) ? SPRITE::PLAYER : SPRITE::NORMAL;
            gun.spriteModeParam = player.clientID;

            gun.draw(canvas);
        }

        return true;
    } else { return false; }
}

bool onDrawHealth(jjPLAYER@ player, jjCANVAS@ canvas) {
    if(toggled_on) {
        if(healthEnabled || itemBeingLocated == "health") {
            int maxHealth = jjShowMaxHealth ? jjMaxHealth : player.health;

            for(int i = 0; i < maxHealth; i++) {
                bool isDamaged = i >= player.health;
                int sprParam = isDamaged ? 127 : 0;

                canvas.drawResizedSprite(
                    healthX - (i * (healthPadding * healthSize)),
                    healthY,
                    ANIM::PICKUPS,
                    41,
                    0,
                    healthSize,
                    healthSize,
                    isDamaged ? SPRITE::TINTED : SPRITE::NORMAL,
                    sprParam
                );
            }
        }

        if((jjGameMode == GAME::CTF && teamScoreEnabled) || itemBeingLocated == "teamscore") canvas.drawString(teamScoreX, teamScoreY, "" + determineTeamColor(player.team) + jjTeamScore[player.team] + "/" + jjMaxScore, teamScoreSize);

        return true;
    } else { return false; }
}

// bool onDrawLives(jjPLAYER@ player, jjCANVAS@ canvas) {return true;}
// bool onDrawPlayerTimer(jjPLAYER@ player, jjCANVAS@ canvas) {return true;}
// bool onDrawScore(jjPLAYER@ player, jjCANVAS@ canvas) {return true;}
// bool onDrawGameModeHUD(jjPLAYER@ player, jjCANVAS@ canvas) { return teamScoreEnabled; }