// Thanks to Glain and the FFTPatcher project for some of the data in the lists.
#include "unit.h"
#include "filesystem.h"
#include "util.h"
#include <string.h>

#define EVENTS_PER_FILE (128)
#define EVENT_BYTE_SIZE (640)
#define UNIT_BYTE_SIZE  (40)

// find the ENTD file based on the entd_id
static file_entry_e find_unit_file(int entd_id) {
    if (entd_id < 0x80) {
        return F_BATTLE__ENTD1_ENT;
    } else if (entd_id < 0x100) {
        return F_BATTLE__ENTD2_ENT;
    } else if (entd_id < 0x180) {
        return F_BATTLE__ENTD3_ENT;
    } else {
        return F_BATTLE__ENTD4_ENT;
    }
}

unit_t read_unit(span_t* span) {
    unit_t unit = { 0 };

    unit.sprite_set = span_read_u8(span);              // 1
    unit.flags_a = (unit_flags_a_e)span_read_u8(span); // 2
    unit.name = span_read_u8(span);                    // 3
    unit.level = span_read_u8(span);                   // 4
    unit.birthday = span_read_u16(span);               // 6
    unit.bravery = span_read_u8(span);                 // 7
    unit.faith = span_read_u8(span);                   // 8
    unit.job_unlock = span_read_u8(span);              // 9
    unit.job_level = span_read_u8(span);               // 10
    unit.job = span_read_u8(span);                     // 11
    unit.secondary_job = span_read_u8(span);           // 12
    unit.reaction = span_read_u16(span);               // 14
    unit.support = span_read_u16(span);                // 16
    unit.movement = span_read_u16(span);               // 18
    unit.head = span_read_u8(span);                    // 19
    unit.body = span_read_u8(span);                    // 20
    unit.accessory = span_read_u8(span);               // 21
    unit.right_hand = span_read_u8(span);              // 22
    unit.left_hand = span_read_u8(span);               // 23
    unit.palette = span_read_u8(span);                 // 24
    unit.flags_b = (unit_flags_b_e)span_read_u8(span); // 25
    unit.pos_x = span_read_i8(span);                   // 26
    unit.pos_y = span_read_i8(span);                   // 27
    unit.direction = span_read_u8(span);               // 28
    unit.experience = span_read_u8(span);              // 29
    unit.unknown_1D = span_read_u8(span);              // 30, always 0xFF?
    unit.unknown_1E = span_read_u16(span);             // 32, always zero? spoilers?
    unit.unit_id = span_read_u8(span);                 // 33
    unit.unknown_21 = span_read_u16(span);             // 35
    unit.flags_c = (unit_flags_c_e)span_read_u8(span); // 36
    unit.target_unit_id = span_read_u8(span);          // 37
    u8 byte1 = span_read_u8(span);                     // 38
    u8 byte2 = span_read_u8(span);                     // 39
    u8 byte3 = span_read_u8(span);                     // 40
    unit.unknown_25 = (byte1 << 16) | (byte2 << 8) | byte3;

    return unit;
}

units_t unit_get_units(int entd_id) {
    file_entry_e entry = find_unit_file(entd_id);
    span_t span = filesystem_read_file(entry);

    usize intra_file_offset = (entd_id % EVENTS_PER_FILE) * EVENT_BYTE_SIZE;
    span.offset = intra_file_offset;

    units_t units = {0};

    for (int i = 0; i < UNITS_PER_EVENT; i++) {
        unit_t unit = read_unit(&span);
        if (unit.job == 0) {
            continue; // skip units without jobs. They may still have names of 255/254
        }

        units.units[units.count++] = unit;
    }
    return units;
}

void unit_flags_a_str(unit_flags_a_e flags, char buffer[static 64]) {
    buffer[0] = '\0';

    if (flags & FLAGA_MALE)
        strcat(buffer, "Male, ");
    if (flags & FLAGA_FEMALE)
        strcat(buffer, "Female, ");
    if (flags & FLAGA_MONSTER)
        strcat(buffer, "Monster, ");
    if (flags & FLAGA_JOIN_AFTER)
        strcat(buffer, "JoinAfter, ");
    if (flags & FLAGA_LOAD_FORMATION)
        strcat(buffer, "Load, ");
    if (flags & FLAGA_HIDE_STATS)
        strcat(buffer, "HideStats, ");
    if (flags & FLAGA_UNKNOWN)
        strcat(buffer, "XX, ");
    if (flags & FLAGA_SAVE_FORMATION)
        strcat(buffer, "Save, ");

    strcat(buffer, "\0"); // Ensure null-termination
}

void unit_flags_b_str(unit_flags_b_e flags, char buffer[static 64]) {
    buffer[0] = '\0';

    if (flags & FLAGB_ALWAYS_P)
        strcat(buffer, "AlwaysP, ");
    if (flags & FLAGB_RANDOMLY_P)
        strcat(buffer, "RandomlyP, ");
    if (flags & FLAGB_TEAM_C)
        strcat(buffer, "TeamC, ");
    if (flags & FLAGB_CONTROL)
        strcat(buffer, "Control, ");
    if (flags & FLAGB_IMMORTAL)
        strcat(buffer, "Immortal, ");
    if (flags & FLAGB_UNKNOWN_1)
        strcat(buffer, "XXA, ");
    if (flags & FLAGB_UNKNOWN_2)
        strcat(buffer, "XXB, ");

    strcat(buffer, "\0"); // Ensure null-termination
}

void unit_flags_c_str(unit_flags_c_e flags, char buffer[static 64]) {
    buffer[0] = '\0';

    if (flags & FLAGC_FOCUS_U)
        strcat(buffer, "Focus, ");
    if (flags & FLAGC_STAY_NEAR_XY)
        strcat(buffer, "Near, ");
    if (flags & FLAGC_AGGRESSIVE)
        strcat(buffer, "Agg, ");
    if (flags & FLAGC_DEFENSIVE)
        strcat(buffer, "Def, ");
    if (flags & FLAGC_UNKNOWN_1)
        strcat(buffer, "XXA, ");
    if (flags & FLAGC_UNKNOWN_2)
        strcat(buffer, "XXB, ");
    if (flags & FLAGC_UNKNOWN_3)
        strcat(buffer, "XXC, ");
    if (flags & FLAGC_UNKNOWN_4)
        strcat(buffer, "XXD, ");

    strcat(buffer, "\0"); // Ensure null-termination
}

static const char* month_str(u8 month) {
    switch (month) {
    case 1:
        return "January";
    case 2:
        return "February";
    case 3:
        return "March";
    case 4:
        return "April";
    case 5:
        return "May";
    case 6:
        return "June";
    case 7:
        return "July";
    case 8:
        return "August";
    case 9:
        return "September";
    case 10:
        return "October";
    case 11:
        return "November";
    case 12:
        return "December";
    default:
        return "Unknown";
    }
}

const char* unit_direction_str(direction_e direction) {
    switch (direction) {
    case DIRECTION_SOUTH:
        return "South";
    case DIRECTION_EAST:
        return "East";
    case DIRECTION_NORTH:
        return "North";
    case DIRECTION_WEST:
        return "West";
    default:
        return "Unknown";
    }
}

// Birthday. Month (first byte) - Day (second byte).
void unit_bday_str(u16 value, char buffer[static 64]) {
    buffer[0] = '\0';

    u8 day = (value >> 8) & 0xFF;
    u8 month = value & 0xFF;

    if (month == 0 && day == 0) {
        strcpy(buffer, "Unknown");
    } else if (month == 254 && day == 254) {
        strcpy(buffer, "-");
    } else {
        snprintf(buffer, 64, "%s %02d", month_str(month), day);
    }
}

void unit_level_str(u8 level, char buffer[static 64]) {
    buffer[0] = '\0';

    if (level == 0xFF) {
        strcpy(buffer, "-");
    } else {
        snprintf(buffer, 64, "%d", level);
    }
}

void unit_brave_faith_str(u8 bravery, u8 faith, char buffer[static 64]) {
    const char* brave_rnd = (bravery == 0xFE) ? "-" : NULL;
    const char* faith_rnd = (faith == 0xFE) ? "-" : NULL;

    if (brave_rnd && faith_rnd) {
        snprintf(buffer, 64, "-");
    } else if (brave_rnd) {
        snprintf(buffer, 64, "- / %d", faith);
    } else if (faith_rnd) {
        snprintf(buffer, 64, "%d / -", bravery);
    } else {
        snprintf(buffer, 64, "%d / %d", bravery, faith);
    }
}

const char* unit_jobs[256] = {
    [0x00] = "",
    [0x01] = "Squire",
    [0x02] = "Squire",
    [0x03] = "Squire",
    [0x04] = "Squire",
    [0x05] = "Holy Knight",
    [0x06] = "Arc Knight",
    [0x07] = "Squire",
    [0x08] = "Arc Knight",
    [0x09] = "Lune Knight",
    [0x0A] = "Duke",
    [0x0B] = "Duke",
    [0x0C] = "Princess",
    [0x0D] = "Holy Swordsman",
    [0x0E] = "High Priest",
    [0x0F] = "Dragoner",
    [0x10] = "Holy Priest",
    [0x11] = "Dark Knight",
    [0x12] = "Hell Knight",
    [0x13] = "Bishop",
    [0x14] = "Cleric",
    [0x15] = "Astrologist",
    [0x16] = "Engineer",
    [0x17] = "Dark Knight",
    [0x18] = "Cardinal",
    [0x19] = "Heaven Knight",
    [0x1A] = "Hell Knight",
    [0x1B] = "Arc Knight",
    [0x1C] = "Delita's Sis",
    [0x1D] = "Arc Duke",
    [0x1E] = "Holy Knight",
    [0x1F] = "Temple Knight",
    [0x20] = "White Knight",
    [0x21] = "Arc Witch",
    [0x22] = "Engineer",
    [0x23] = "Bi-Count",
    [0x24] = "Divine Knight",
    [0x25] = "Divine Knight",
    [0x26] = "Knight Blade",
    [0x27] = "Sorceror",
    [0x28] = "White Knight",
    [0x29] = "Heaven Knight",
    [0x2A] = "Divine Knight",
    [0x2B] = "Engineer",
    [0x2C] = "Cleric",
    [0x2D] = "Assassin",
    [0x2E] = "Assassin",
    [0x2F] = "Divine Knight",
    [0x30] = "Cleric",
    [0x31] = "Phony Saint",
    [0x32] = "Soldier",
    [0x33] = "Arc Knight",
    [0x34] = "Holy Knight",
    [0x35] = "Chemist",
    [0x36] = "Priest",
    [0x37] = "Wizard",
    [0x38] = "Oracle",

    [0x3C] = "Warlock",
    [0x3D] = "Knight",
    [0x3E] = "Angel of Death",
    [0x3F] = "Archer",
    [0x40] = "Regulator",
    [0x41] = "Holy Angel",
    [0x42] = "Wizard",
    [0x43] = "Impure King",
    [0x44] = "Time Mage",
    [0x45] = "Ghost of Fury",
    [0x46] = "Oracle",
    [0x47] = "Summoner",
    [0x48] = "Holy Dragon",
    [0x49] = "Arch Angel",
    [0x4A] = "Squire",
    [0x4B] = "Chemist",
    [0x4C] = "Knight",
    [0x4D] = "Archer",
    [0x4E] = "Monk",
    [0x4F] = "Priest",
    [0x50] = "Wizard",
    [0x51] = "Time Mage",
    [0x52] = "Summoner",
    [0x53] = "Thief",
    [0x54] = "Mediator",
    [0x55] = "Oracle",
    [0x56] = "Geomancer",
    [0x57] = "Lancer",
    [0x58] = "Samurai",
    [0x59] = "Ninja",
    [0x5A] = "Calculator",
    [0x5B] = "Bard",
    [0x5C] = "Dancer",
    [0x5D] = "Mime",
    [0x5E] = "Chocobo",
    [0x5F] = "Black Chocobo",
    [0x60] = "Red Chocobo",
    [0x61] = "Goblin",
    [0x62] = "Black Goblin",
    [0x63] = "Gobbledeguck",
    [0x64] = "Bomb",
    [0x65] = "Grenade",
    [0x66] = "Explosive",
    [0x67] = "Red Panther",
    [0x68] = "Cuar",
    [0x69] = "Vampire",
    [0x6A] = "Pisco Demon",
    [0x6B] = "Squidlarkin",
    [0x6C] = "Mindflare",
    [0x6D] = "Skeleton",
    [0x6E] = "Bone Snatch",
    [0x6F] = "Living Bone",
    [0x70] = "Ghoul",
    [0x71] = "Gust",
    [0x72] = "Revnant",
    [0x73] = "Flotiball",
    [0x74] = "Ahriman",
    [0x75] = "Plague",
    [0x76] = "Juravis",
    [0x77] = "Steel Hawk",
    [0x78] = "Cocatoris",
    [0x79] = "Uribo",
    [0x7A] = "Porky",
    [0x7B] = "Wildbow",
    [0x7C] = "Woodman",
    [0x7D] = "Trent",
    [0x7E] = "Taiju",
    [0x7F] = "Bull Demon",
    [0x80] = "Minitaurus",
    [0x81] = "Sacred",
    [0x82] = "Morbol",
    [0x83] = "Ochu",
    [0x84] = "Great Morbol",
    [0x85] = "Behemoth",
    [0x86] = "King Behemoth",
    [0x87] = "Dark Behemoth",
    [0x88] = "Dragon",
    [0x89] = "Blue Dragon",
    [0x8A] = "Red Dragon",
    [0x8B] = "Hyudra",
    [0x8C] = "Hydra",
    [0x8D] = "Tiamat",

    [0x90] = "Byblos",
    [0x91] = "Steel Giant",

    [0x96] = "Apanda",
    [0x97] = "Serpentarius",
    [0x98] = "Holy Dragon",
    [0x99] = "Archaic Demon",
    [0x9A] = "Ultima Demon",
};

const char* unit_skills[256] = {
    [0x00] = "",
    [0x01] = "Attack",
    [0x02] = "Defend",
    [0x03] = "Equip Change",

    [0x05] = "Basic Skill",
    [0x06] = "Item",
    [0x07] = "Battle Skill",
    [0x08] = "Charge",
    [0x09] = "Punch Art",
    [0x0A] = "White Magic",
    [0x0B] = "Black Magic",
    [0x0C] = "Time Magic",
    [0x0D] = "Summon Magic",
    [0x0E] = "Steal",
    [0x0F] = "Talk Skill",
    [0x10] = "Yin Yang Magic",
    [0x11] = "Elemental",
    [0x12] = "Jump",
    [0x13] = "Draw Out",
    [0x14] = "Throw",
    [0x15] = "Math Skill",
    [0x16] = "Sing",
    [0x17] = "Dance",
    [0x18] = "Mimic",
    [0x19] = "Guts",
    [0x1A] = "Guts",
    [0x1B] = "Guts",
    [0x1C] = "Guts",
    [0x1D] = "Holy Sword",
    [0x1E] = "Mighty Sword",
    [0x1F] = "Basic Skill",
    [0x20] = "Dark Sword",
    [0x21] = "Holy Sword",
    [0x22] = "Holy Sword",
    [0x23] = "Magic",
    [0x24] = "Holy Magic",
    [0x25] = "Snipe",
    [0x26] = "Snipe",
    [0x27] = "Dark Sword",
    [0x28] = "Holy Sword",
    [0x29] = "Limit",
    [0x2A] = "White-aid",
    [0x2B] = "Dragon",
    [0x2C] = "Breath",
    [0x2D] = "Truth",
    [0x2E] = "Un-truth",
    [0x2F] = "Starry Heaven",
    [0x30] = "Holy Sword",
    [0x31] = "Holy Magic",
    [0x32] = "Truth",
    [0x33] = "Battle Skill",
    [0x34] = "Jump",
    [0x35] = "Punch Skill",
    [0x36] = "Use Hand",
    [0x37] = "Use Hand",
    [0x38] = "Throw",
    [0x39] = "Throw",
    [0x3A] = "Holy Sword",
    [0x3B] = "Sword Spirit",
    [0x3C] = "Mighty Sword",
    [0x3D] = "All Magic",
    [0x3E] = "Sword Spirit",
    [0x3F] = "Blood Suck",
    [0x40] = "Mighty Sword",
    [0x41] = "All Magic",
    [0x42] = "Mighty Sword",
    [0x43] = "Mighty Sword",
    [0x44] = "Snipe",
    [0x45] = "Magic Sword",
    [0x46] = "Sword Skill",
    [0x47] = "All Magic",
    [0x48] = "All Magic",
    [0x49] = "Phantom",
    [0x4A] = "All Swordskill",
    [0x4B] = "Destroy Sword",
    [0x4C] = "Holy Magic",

    [0x67] = "Fear",
    [0x68] = "Warlock Summon",

    [0x6B] = "Fear",
    [0x6C] = "Ja Magic",

    [0x6F] = "Fear",
    [0x70] = "Dimension Magic",

    [0x73] = "Fear",
    [0x74] = "Impure",

    [0x77] = "Fear",
    [0x78] = "All Magic",

    [0x7B] = "Ultimate Magic",
    [0x7C] = "Chaos",
    [0x7D] = "Complete Magic",
    [0x7E] = "Saturation",

    [0x9B] = "Sword Skill",
    [0x9C] = "Charge",
    [0x9D] = "Black Magic",
    [0x9E] = "Time Magic",
    [0x9F] = "Yin Yang Magic",
    [0xA0] = "Summon Magic",
    [0xA1] = "Item",
    [0xA2] = "White Magic",
    [0xA3] = "Black Magic",
    [0xA4] = "Yin Yang Magic",

    [0xAA] = "Byblos",
    [0xAB] = "Work",
    [0xAC] = "Bio",
    [0xAD] = "Dark Cloud",
    [0xAE] = "Dark Magic",
    [0xAF] = "Night Magic",
    [0xB0] = "Chocobo",
    [0xB1] = "Black Chocobo",
    [0xB2] = "Red Chocobo",
    [0xB3] = "Goblin",
    [0xB4] = "Black Goblin",
    [0xB5] = "Gobbledeguck",
    [0xB6] = "Bomb",
    [0xB7] = "Grenade",
    [0xB8] = "Explosive",
    [0xB9] = "Red Panther",
    [0xBA] = "Cuar",
    [0xBB] = "Vampire",
    [0xBC] = "Pisco Demon",
    [0xBD] = "Squidlarkin",
    [0xBE] = "Mindflare",
    [0xBF] = "Skeleton",
    [0xC0] = "Bone Snatch",
    [0xC1] = "Living Bone",
    [0xC2] = "Ghoul",
    [0xC3] = "Ghost",
    [0xC4] = "Revenant",
    [0xC5] = "Flotiball",
    [0xC6] = "Ahriman",
    [0xC7] = "Plague",
    [0xC8] = "Juravis",
    [0xC9] = "Steel Hawk",
    [0xCA] = "Cockatoris",
    [0xCB] = "Uribo",
    [0xCC] = "Porky",
    [0xCD] = "Wildbow",
    [0xCE] = "Woodman",
    [0xCF] = "Trent",
    [0xD0] = "Taiju",
    [0xD1] = "Bull Demon",
    [0xD2] = "Minitaurus",
    [0xD3] = "Sacred",
    [0xD4] = "Morbol",
    [0xD5] = "Ochu",
    [0xD6] = "Great Morbol",
    [0xD7] = "Behemoth",
    [0xD8] = "King Behemoth",
    [0xD9] = "Dark Behemoth",
    [0xDA] = "Dragon",
    [0xDB] = "Blue Dragon",
    [0xDC] = "Red Dragon",
    [0xDD] = "Hyudra",
    [0xDE] = "Hydra",
    [0xDF] = "Tiamat",

    [0xFE] = "-"
};

const char* unit_names[256] = {
    [1] = "RamzaA",
    [2] = "RamzaB",
    [3] = "RamzaC",
    [4] = "DelitaA",
    [5] = "DelitaB",
    [6] = "DelitaC",
    [7] = "Algus",
    [8] = "Zalbag",
    [9] = "Dycedarg",
    [10] = "Larg",
    [11] = "Goltana",
    [12] = "Ovelia",
    [13] = "Orlandu",
    [14] = "Funeral",
    [15] = "Reis",
    [16] = "Zalmo",
    [17] = "Gafgarion",
    [18] = "Malak",
    [19] = "Simon",
    [20] = "Alma",
    [21] = "Olan",
    [22] = "Mustadio",
    [23] = "Gafgarion",
    [24] = "Draclau",
    [25] = "Rafa",
    [26] = "Malak",
    [27] = "Elmdor",
    [28] = "Teta",
    [29] = "Barinten",
    [30] = "Agrias",
    [31] = "Beowulf",
    [32] = "Wiegraf",
    [33] = "Balmafula",
    [34] = "Mustadio",
    [35] = "Rudvich",
    [36] = "Vormav",
    [37] = "Rofel",
    [38] = "Izlude",
    [39] = "Kletian",
    [40] = "Wiegraf",
    [41] = "Rafa",
    [42] = "Meliadoul",
    [43] = "Balk",
    [44] = "Alma",
    [45] = "Celia",
    [46] = "Lede",
    [47] = "Meliadoul",
    [48] = "Alma",
    [49] = "Ajora",
    [50] = "Cloud",
    [51] = "Zalbag",
    [52] = "Agrias",
    [60] = "Velius",
    [62] = "Zalera",
    [64] = "Hashmalum",
    [65] = "Altima",
    [67] = "Queklain",
    [68] = "Rofokare",
    [69] = "Adramelk",
    [72] = "Reis",
    [73] = "Altima",
    [74] = "Gustav",
    [75] = "Miluda",
    [76] = "Golagros",
    [115] = "Worker 7",
    [116] = "Sinogue",
    [117] = "Worker 8",
    [118] = "Boco",
    [119] = "Elidibs",
    [120] = "Lavian",
    [121] = "Alicia",
    [122] = "Fukes",
    [123] = "Dish",
    [124] = "Viggs",
    [125] = "Wezaleff",
    [126] = "Lezales",
    [127] = "Rad",
    [254] = "Ramza Beoulve",
    [255] = "-",
};

const char* unit_abilities[] = {
    [0] = "",
    [1] = "Cure",
    [2] = "Cure 2",
    [3] = "Cure 3",
    [4] = "Cure 4",
    [5] = "Raise",
    [6] = "Raise 2",
    [7] = "Reraise",
    [8] = "Regen",
    [9] = "Protect",
    [10] = "Protect 2",
    [11] = "Shell",
    [12] = "Shell 2",
    [13] = "Wall",
    [14] = "Esuna",
    [15] = "Holy",
    [16] = "Fire",
    [17] = "Fire 2",
    [18] = "Fire 3",
    [19] = "Fire 4",
    [20] = "Bolt",
    [21] = "Bolt 2",
    [22] = "Bolt 3",
    [23] = "Bolt 4",
    [24] = "Ice",
    [25] = "Ice 2",
    [26] = "Ice 3",
    [27] = "Ice 4",
    [28] = "Poison",
    [29] = "Frog",
    [30] = "Death",
    [31] = "Flare",
    [32] = "Haste",
    [33] = "Haste 2",
    [34] = "Slow",
    [35] = "Slow 2",
    [36] = "Stop",
    [37] = "Don't Move",
    [38] = "Float",
    [39] = "Reflect",
    [40] = "(Summon Demon)",
    [41] = "Quick",
    [42] = "Demi",
    [43] = "Demi 2",
    [44] = "Meteor",
    [45] = "",
    [46] = "Blind",
    [47] = "Spell Absorb",
    [48] = "Life Drain",
    [49] = "Pray Faith",
    [50] = "Doubt Faith",
    [51] = "Zombie",
    [52] = "Silence Song",
    [53] = "Blind Rage",
    [54] = "Foxbird",
    [55] = "Confusion Song",
    [56] = "Dispel Magic",
    [57] = "Paralyze",
    [58] = "Sleep",
    [59] = "Petrify",
    [60] = "Moogle",
    [61] = "Shiva",
    [62] = "Ramuh",
    [63] = "Ifrit",
    [64] = "Titan",
    [65] = "Golem",
    [66] = "Carbunkle",
    [67] = "Bahamut",
    [68] = "Odin",
    [69] = "Leviathan",
    [70] = "Salamander",
    [71] = "Silf",
    [72] = "Fairy",
    [73] = "Lich",
    [74] = "Cyclops",
    [75] = "Zodiac",
    [76] = "Asura",
    [77] = "Koutetsu",
    [78] = "Bizen Boat",
    [79] = "Murasame",
    [80] = "Heaven's Cloud",
    [81] = "Kiyomori",
    [82] = "Muramasa",
    [83] = "Kikuichimoji",
    [84] = "Masamune",
    [85] = "Chirijiraden",
    [86] = "Angel Song",
    [87] = "Life Song",
    [88] = "Cheer Song",
    [89] = "Battle Song",
    [90] = "Magic Song",
    [91] = "Nameless Song",
    [92] = "Last Song",
    [93] = "Witch Hunt",
    [94] = "Wiznaibus",
    [95] = "Slow Dance",
    [96] = "Polka Polka",
    [97] = "Disillusion",
    [98] = "Nameless Dance",
    [99] = "Last Dance",
    [100] = "Spin Fist",
    [101] = "Repeating Fist",
    [102] = "Wave Fist",
    [103] = "Earth Slash",
    [104] = "Secret Fist",
    [105] = "Stigma Magic",
    [106] = "Chakra",
    [107] = "Revive",
    [108] = "Gil Taking",
    [109] = "Steal Heart",
    [110] = "Steal Helmet",
    [111] = "Steal Armor",
    [112] = "Steal Shield",
    [113] = "Steal Weapon",
    [114] = "Steal Accessory",
    [115] = "Steal Exp",
    [116] = "Invitation",
    [117] = "Persuade",
    [118] = "Praise",
    [119] = "Threaten",
    [120] = "Preach",
    [121] = "Solution",
    [122] = "Death Sentence",
    [123] = "Negotiate",
    [124] = "Insult",
    [125] = "Mimic Daravon",
    [126] = "Pitfall",
    [127] = "Water Ball",
    [128] = "Hell Ivy",
    [129] = "Carve Model",
    [130] = "Local Quake",
    [131] = "Kamaitachi",
    [132] = "Demon Fire",
    [133] = "Quicksand",
    [134] = "Sand Storm",
    [135] = "Blizzard",
    [136] = "Gusty Wind",
    [137] = "Lava Ball",
    [138] = "Head Break",
    [139] = "Armor Break",
    [140] = "Shield Break",
    [141] = "Weapon Break",
    [142] = "Magic Break",
    [143] = "Speed Break",
    [144] = "Power Break",
    [145] = "Mind Break",
    [146] = "Accumulate",
    [147] = "Dash",
    [148] = "Throw Stone",
    [149] = "Heal",
    [150] = "Yell",
    [151] = "Cheer Up",
    [152] = "Wish",
    [153] = "Scream",
    [154] = "Ultima",
    [155] = "Stasis Sword",
    [156] = "Split Punch",
    [157] = "Crush Punch",
    [158] = "Lightning Stab",
    [159] = "Holy Explosion",
    [160] = "Shellbust Stab",
    [161] = "Blastar Punch",
    [162] = "Hellcry Punch",
    [163] = "Icewolf Bite",
    [164] = "Dark Sword",
    [165] = "Night Sword",
    [166] = "Dark Holy",
    [167] = "Deathspell 2",
    [168] = "Galaxy Stop",
    [169] = "Heaven Thunder",
    [170] = "Asura*",
    [171] = "Diamond Sword",
    [172] = "Hydragon Pit",
    [173] = "Space Storage",
    [174] = "Sky Demon",
    [175] = "Heaven Bolt Back",
    [176] = "Asura Back",
    [177] = "Diamond Sword Back",
    [178] = "Hydragon Pit Back",
    [179] = "Space Storage Back",
    [180] = "Sky Demon Back",
    [181] = "Seal",
    [182] = "Shadow Stitch",
    [183] = "Stop Bracelet",
    [184] = "(Summon Angel)",
    [185] = "Shock",
    [186] = "Difference",
    [187] = "Seal*",
    [188] = "Chicken Race",
    [189] = "Hold Tight",
    [190] = "Darkness",
    [191] = "Lose Voice",
    [192] = "Loss",
    [193] = "Spell",
    [194] = "Nightmare",
    [195] = "Death Cold",
    [196] = "Magic Ruin",
    [197] = "Speed Ruin",
    [198] = "Power Ruin",
    [199] = "Mind Ruin",
    [200] = "Blood Suck",
    [201] = "Allure",
    [202] = "Bio",
    [203] = "Bio*",
    [204] = "Bio**",
    [205] = "Bio 2",
    [206] = "Bio 2*",
    [207] = "Bio 2**",
    [208] = "Bio 2***",
    [209] = "Bio 3",
    [210] = "Bio 3*",
    [211] = "Bio 3*",
    [212] = "Magic Barrier",
    [213] = "Leg Aim",
    [214] = "Arm Aim",
    [215] = "Seal Evil",
    [216] = "Melt",
    [217] = "Tornado",
    [218] = "Quake",
    [219] = "(Teleport 3: Send)",
    [220] = "(Teleport 3: Arrive)",
    [221] = "Toad 2",
    [222] = "Gravi 2",
    [223] = "Flare 2",
    [224] = "Blind 2",
    [225] = "Small Bomb",
    [226] = "Small Bomb*",
    [227] = "Confuse 2",
    [228] = "Sleep 2",
    [229] = "Ultima*",
    [230] = "All-ultima",
    [231] = "Mute",
    [232] = "Despair 2",
    [233] = "Return 2",
    [234] = "Blind*",
    [235] = "Aspel",
    [236] = "Drain",
    [237] = "Faith",
    [238] = "Innocent",
    [239] = "Zombie*",
    [240] = "Silence",
    [241] = "Berserk",
    [242] = "Chicken",
    [243] = "Confuse",
    [244] = "Despair",
    [245] = "Don't Act",
    [246] = "Sleep*",
    [247] = "Break",
    [248] = "Ice Bracelet",
    [249] = "Fire Bracelet",
    [250] = "Thunder Bracelet",
    [251] = "Dragon Tame",
    [252] = "Dragon Care",
    [253] = "Dragon Power Up",
    [254] = "Dragon Level Up",
    [255] = "Holy Bracelet",
    [256] = "Shock!",
    [257] = "Braver",
    [258] = "Cross-slash",
    [259] = "Blade Beam",
    [260] = "Climhazzard",
    [261] = "Meteorain",
    [262] = "Finish Touch",
    [263] = "Omnislash",
    [264] = "Cherry Blossom",
    [265] = "Choco Attack",
    [266] = "Choco Ball",
    [267] = "Choco Meteor",
    [268] = "Choco Esuna",
    [269] = "Choco Cure",
    [270] = "Tackle",
    [271] = "Goblin Punch",
    [272] = "Turn Punch",
    [273] = "Eye Gouge",
    [274] = "Mutilate",
    [275] = "Bite",
    [276] = "Small Bomb*",
    [277] = "Self Destruct",
    [278] = "Flame Attack",
    [279] = "Spark",
    [280] = "Scratch",
    [281] = "Cat Kick",
    [282] = "Blaster",
    [283] = "Poison Nail",
    [284] = "Blood Suck*",
    [285] = "Tentacle",
    [286] = "Black Ink",
    [287] = "Odd Soundwave",
    [288] = "Mind Blast",
    [289] = "Level Blast",
    [290] = "Knife Hand",
    [291] = "Thunder Soul",
    [292] = "Aqua Soul",
    [293] = "Ice Soul",
    [294] = "Wind Soul",
    [295] = "Throw Spirit",
    [296] = "Zombie Touch",
    [297] = "Sleep Touch",
    [298] = "Drain Touch",
    [299] = "Grease Touch",
    [300] = "Wing Attack",
    [301] = "Look of Devil",
    [302] = "Look of Fright",
    [303] = "Circle",
    [304] = "Death Sentence*",
    [305] = "Scratch Up",
    [306] = "Beak",
    [307] = "Shine Lover",
    [308] = "Feather Bomb",
    [309] = "Beaking",
    [310] = "Straight Dash",
    [311] = "Nose Bracelet",
    [312] = "Oink",
    [313] = "Pooh-",
    [314] = "Please Eat",
    [315] = "Leaf Dance",
    [316] = "Protect Spirit",
    [317] = "Clam Spirit",
    [318] = "Spirit of Life",
    [319] = "Magic Spirit",
    [320] = "Shake Off",
    [321] = "Wave Around",
    [322] = "Mimic Titan",
    [323] = "Gather Power",
    [324] = "Blow Fire",
    [325] = "Tentacle*",
    [326] = "Lick",
    [327] = "Goo",
    [328] = "Bad Bracelet",
    [329] = "Moldball Virus",
    [330] = "Stab Up",
    [331] = "Sudden Cry",
    [332] = "Hurricane",
    [333] = "Ulmaguest",
    [334] = "Giga Flare",
    [335] = "Dash*",
    [336] = "Tail Swing",
    [337] = "Ice Bracelet*",
    [338] = "Fire Bracelet*",
    [339] = "Thunder Bracelet*",
    [340] = "Triple Attack",
    [341] = "Triple Bracelet",
    [342] = "Triple Thunder",
    [343] = "Triple Flame",
    [344] = "Dark Whisper",
    [345] = "Snake Carrier",
    [346] = "Poison Frog",
    [347] = "Midgar Swarm",
    [348] = "Lifebreak",
    [349] = "Nanoflare",
    [350] = "Grand Cross",
    [351] = "Destroy",
    [352] = "Compress",
    [353] = "Dispose",
    [354] = "Crush",
    [355] = "Energy",
    [356] = "Parasite",
    [357] = "",
    [358] = "",
    [359] = "",
    [360] = "",
    [361] = "",
    [362] = "",
    [363] = "",
    [364] = "",
    [365] = "",
    [366] = "",
    [367] = "Frog Attack",
    [368] = "Potion",
    [369] = "Hi-Potion",
    [370] = "X-Potion",
    [371] = "Ether",
    [372] = "Hi-Ether",
    [373] = "Elixir",
    [374] = "Antidote",
    [375] = "Eye Drop",
    [376] = "Echo Grass",
    [377] = "Maiden's Kiss",
    [378] = "Soft",
    [379] = "Holy Water",
    [380] = "Remedy",
    [381] = "Phoenix Down",
    [382] = "Shuriken",
    [383] = "Knife",
    [384] = "Sword",
    [385] = "Hammer",
    [386] = "Katana",
    [387] = "Ninja Sword",
    [388] = "Axe",
    [389] = "Spear",
    [390] = "Stick",
    [391] = "Knight Sword",
    [392] = "Dictionary",
    [393] = "Ball",
    [394] = "Level Jump2",
    [395] = "Level Jump3",
    [396] = "Level Jump4",
    [397] = "Level Jump5",
    [398] = "Level Jump8",
    [399] = "Vertical Jump2",
    [400] = "Vertical Jump3",
    [401] = "Vertical Jump4",
    [402] = "Vertical Jump5",
    [403] = "Vertical Jump6",
    [404] = "Vertical Jump7",
    [405] = "Vertical Jump8",
    [406] = "Charge+1",
    [407] = "Charge+2",
    [408] = "Charge+3",
    [409] = "Charge+4",
    [410] = "Charge+5",
    [411] = "Charge+7",
    [412] = "Charge+10",
    [413] = "Charge+20",
    [414] = "CT",
    [415] = "Level",
    [416] = "Exp",
    [417] = "Height",
    [418] = "Prime Number",
    [419] = "5",
    [420] = "4",
    [421] = "3",
    [422] = "A Save",
    [423] = "MA Save",
    [424] = "Speed Save",
    [425] = "Sunken State",
    [426] = "Caution",
    [427] = "Dragon Spirit",
    [428] = "Regenerator",
    [429] = "Brave Up",
    [430] = "Face Up",
    [431] = "HP Restore",
    [432] = "MP Restore",
    [433] = "Critical Quick",
    [434] = "Meatbone Slash",
    [435] = "Counter Magic",
    [436] = "Counter Tackle",
    [437] = "Counter Flood",
    [438] = "Absorb Used MP",
    [439] = "Gilgame Heart",
    [440] = "Reflect*",
    [441] = "Auto Potion",
    [442] = "Counter",
    [443] = "",
    [444] = "Distribute",
    [445] = "MP Switch",
    [446] = "Damage Split",
    [447] = "Weapon Guard",
    [448] = "Finger Guard",
    [449] = "Abandon",
    [450] = "Catch",
    [451] = "Blade Grasp",
    [452] = "Arrow Guard",
    [453] = "Hamedo",
    [454] = "Equip Armor",
    [455] = "Equip Shield",
    [456] = "Equip Sword",
    [457] = "Equip Knife",
    [458] = "Equip Crossbow",
    [459] = "Equip Spear",
    [460] = "Equip Axe",
    [461] = "Equip Gun",
    [462] = "Half of MP",
    [463] = "Gained Jp UP",
    [464] = "Gained Exp UP",
    [465] = "Attack UP",
    [466] = "Defense UP",
    [467] = "Magic AttackUP",
    [468] = "Magic DefendUP",
    [469] = "Concentrate",
    [470] = "Train",
    [471] = "Secret Hunt",
    [472] = "Martial Arts",
    [473] = "Monster Talk",
    [474] = "Throw Item",
    [475] = "Maintenance",
    [476] = "Two Hands",
    [477] = "Two Swords",
    [478] = "Monster Skill",
    [479] = "Defend",
    [480] = "Equip Change",
    [481] = "",
    [482] = "Short Charge",
    [483] = "Non-charge",
    [484] = "",
    [485] = "",
    [486] = "Move+1",
    [487] = "Move+2",
    [488] = "Move+3",
    [489] = "Jump+1",
    [490] = "Jump+2",
    [491] = "Jump+3",
    [492] = "Ignore Height",
    [493] = "Move-HP Up",
    [494] = "Move-MP Up",
    [495] = "Move-Get Exp",
    [496] = "Move-Get Jp",
    [497] = "(Cannot enter water)",
    [498] = "Teleport",
    [499] = "Teleport 2",
    [500] = "Any Weather",
    [501] = "Any Ground",
    [502] = "Move in Water (Walk*)",
    [503] = "Walk on Water (Move*)",
    [504] = "Move on Lava",
    [505] = "Move Underwater",
    [506] = "Float*",
    [507] = "Fly",
    [508] = "Silent Walk",
    [509] = "Move-Find Item",
    [510] = "-",
    [511] = "",
    [512] = "",
};

const char* unit_items[256] = {
    [0] = "",
    [1] = "Dagger",
    [2] = "Mythril Knife",
    [3] = "Blind Knife",
    [4] = "Mage Masher",
    [5] = "Platina Dagger",
    [6] = "Main Gauche",
    [7] = "Orichalcum",
    [8] = "Assassin Dagger",
    [9] = "Air Knife",
    [10] = "Zorlin Shape",
    [11] = "Hidden Knife",
    [12] = "Ninja Knife",
    [13] = "Short Edge",
    [14] = "Ninja Edge",
    [15] = "Spell Edge",
    [16] = "Sasuke Knife",
    [17] = "Iga Knife",
    [18] = "Koga Knife",
    [19] = "Broad Sword",
    [20] = "Long Sword",
    [21] = "Iron Sword",
    [22] = "Mythril Sword",
    [23] = "Blood Sword",
    [24] = "Coral Sword",
    [25] = "Ancient Sword",
    [26] = "Sleep Sword",
    [27] = "Platinum Sword",
    [28] = "Diamond Sword",
    [29] = "Ice Brand",
    [30] = "Rune Blade",
    [31] = "Nagrarock",
    [32] = "Materia Blade",
    [33] = "Defender",
    [34] = "Save the Queen",
    [35] = "Excalibur",
    [36] = "Ragnarok",
    [37] = "Chaos Blade",
    [38] = "Asura Knife",
    [39] = "Koutetsu Knife",
    [40] = "Bizen Boat",
    [41] = "Murasame",
    [42] = "Heaven's Cloud",
    [43] = "Kiyomori",
    [44] = "Muramasa",
    [45] = "Kikuichimoji",
    [46] = "Masamune",
    [47] = "Chirijiraden",
    [48] = "Battle Axe",
    [49] = "Giant Axe",
    [50] = "Slasher",
    [51] = "Rod",
    [52] = "Thunder Rod",
    [53] = "Flame Rod",
    [54] = "Ice Rod",
    [55] = "Poison Rod",
    [56] = "Wizard Rod",
    [57] = "Dragon Rod",
    [58] = "Faith Rod",
    [59] = "Oak Staff",
    [60] = "White Staff",
    [61] = "Healing Staff",
    [62] = "Rainbow Staff",
    [63] = "Wizard Staff",
    [64] = "Gold Staff",
    [65] = "Mace of Zeus",
    [66] = "Sage Staff",
    [67] = "Flail",
    [68] = "Flame Whip",
    [69] = "Morning Star",
    [70] = "Scorpion Tail",
    [71] = "Romanda Gun",
    [72] = "Mythril Gun",
    [73] = "Stone Gun",
    [74] = "Blaze Gun",
    [75] = "Glacier Gun",
    [76] = "Blast Gun",
    [77] = "Bow Gun",
    [78] = "Night Killer",
    [79] = "Cross Bow",
    [80] = "Poison Bow",
    [81] = "Hunting Bow",
    [82] = "Gastrafitis",
    [83] = "Long Bow",
    [84] = "Silver Bow",
    [85] = "Ice Bow",
    [86] = "Lightning Bow",
    [87] = "Windslash Bow",
    [88] = "Mythril Bow",
    [89] = "Ultimus Bow",
    [90] = "Yoichi Bow",
    [91] = "Perseus Bow",
    [92] = "Ramia Harp",
    [93] = "Bloody Strings",
    [94] = "Fairy Harp",
    [95] = "Battle Dict",
    [96] = "Monster Dict",
    [97] = "Papyrus Plate",
    [98] = "Madlemgen",
    [99] = "Javelin",
    [100] = "Spear",
    [101] = "Mythril Spear",
    [102] = "Partisan",
    [103] = "Oberisk",
    [104] = "Holy Lance",
    [105] = "Dragon Whisker",
    [106] = "Javelin*",
    [107] = "Cypress Rod",
    [108] = "Battle Bamboo",
    [109] = "Musk Rod",
    [110] = "Iron Fan",
    [111] = "Gokuu Rod",
    [112] = "Ivory Rod",
    [113] = "Octagon Rod",
    [114] = "Whale Whisker",
    [115] = "C Bag",
    [116] = "FS Bag",
    [117] = "P Bag",
    [118] = "H Bag",
    [119] = "Persia",
    [120] = "Cashmere",
    [121] = "Ryozan Silk",
    [122] = "Shuriken",
    [123] = "Magic Shuriken",
    [124] = "Yagyu Darkness",
    [125] = "Fire Ball",
    [126] = "Water Ball",
    [127] = "Lightning Ball",
    [128] = "Escutcheon",
    [129] = "Buckler",
    [130] = "Bronze Shield",
    [131] = "Round Shield",
    [132] = "Mythril Shield",
    [133] = "Gold Shield",
    [134] = "Ice Shield",
    [135] = "Flame Shield",
    [136] = "Aegis Shield",
    [137] = "Diamond Shield",
    [138] = "Platina Shield",
    [139] = "Crystal Shield",
    [140] = "Genji Shield",
    [141] = "Kaiser Plate",
    [142] = "Venetian Shield",
    [143] = "Escutcheon*",
    [144] = "Leather Helmet",
    [145] = "Bronze Helmet",
    [146] = "Iron Helmet",
    [147] = "Barbuta",
    [148] = "Mythril Helmet",
    [149] = "Gold Helmet",
    [150] = "Cross Helmet",
    [151] = "Diamond Helmet",
    [152] = "Platina Helmet",
    [153] = "Circlet",
    [154] = "Crystal Helmet",
    [155] = "Genji Helmet",
    [156] = "Grand Helmet",
    [157] = "Leather Hat",
    [158] = "Feather Hat",
    [159] = "Red Hood",
    [160] = "Headgear",
    [161] = "Triangle Hat",
    [162] = "Green Beret",
    [163] = "Twist Headband",
    [164] = "Holy Miter",
    [165] = "Black Hood",
    [166] = "Golden Hairpin",
    [167] = "Flash Hat",
    [168] = "Thief Hat",
    [169] = "Cachusha",
    [170] = "Barette",
    [171] = "Ribbon",
    [172] = "Leather Armor",
    [173] = "Linen Cuirass",
    [174] = "Bronze Armor",
    [175] = "Chain Mail",
    [176] = "Mythril Armor",
    [177] = "Plate Mail",
    [178] = "Gold Armor",
    [179] = "Diamond Armor",
    [180] = "Platina Armor",
    [181] = "Carabini Mail",
    [182] = "Crystal Mail",
    [183] = "Genji Armor",
    [184] = "Reflect Mail",
    [185] = "Maximillian",
    [186] = "Clothes",
    [187] = "Leather Outfit",
    [188] = "Leather Vest",
    [189] = "Chain Vest",
    [190] = "Mythril Vest",
    [191] = "Adaman Vest",
    [192] = "Wizard Outfit",
    [193] = "Brigandine",
    [194] = "Judo Outfit",
    [195] = "Power Sleeve",
    [196] = "Earth Clothes",
    [197] = "Secret Clothes",
    [198] = "Black Costume",
    [199] = "Rubber Costume",
    [200] = "Linen Robe",
    [201] = "Silk Robe",
    [202] = "Wizard Robe",
    [203] = "Chameleon Robe",
    [204] = "White Robe",
    [205] = "Black Robe",
    [206] = "Light Robe",
    [207] = "Robe of Lords",
    [208] = "Battle Boots",
    [209] = "Spike Shoes",
    [210] = "Germinas Boots",
    [211] = "Rubber Shoes",
    [212] = "Feather Boots",
    [213] = "Sprint Shoes",
    [214] = "Red Shoes",
    [215] = "Power Wrist",
    [216] = "Genji Gauntlet",
    [217] = "Magic Gauntlet",
    [218] = "Bracer",
    [219] = "Reflect Ring",
    [220] = "Defense Ring",
    [221] = "Magic Ring",
    [222] = "Cursed Ring",
    [223] = "Angel Ring",
    [224] = "Diamond Armlet",
    [225] = "Jade Armlet",
    [226] = "108 Gems",
    [227] = "N-Kai Armlet",
    [228] = "Defense Armlet",
    [229] = "Small Mantle",
    [230] = "Leather Mantle",
    [231] = "Wizard Mantle",
    [232] = "Elf Mantle",
    [233] = "Dracula Mantle",
    [234] = "Feather Mantle",
    [235] = "Vanish Mantle",
    [236] = "Chantage",
    [237] = "Cherche",
    [238] = "Setiemson",
    [239] = "Salty Rage",
    [240] = "Potion",
    [241] = "Hi-Potion",
    [242] = "X-Potion",
    [243] = "Ether",
    [244] = "Hi-Ether",
    [245] = "Elixir",
    [246] = "Antidote",
    [247] = "Eye Drop",
    [248] = "Echo Grass",
    [249] = "Maiden's Kiss",
    [250] = "Soft",
    [251] = "Holy Water",
    [252] = "Remedy",
    [253] = "Phoenix Down",
    [254] = "-",
    [255] = "",

};

const char* unit_name_str(u8 id) { return unit_names[id]; }
const char* unit_job_str(u8 id) { return unit_jobs[id]; }
const char* unit_item_str(u8 id) { return unit_items[id]; }
const char* unit_skill_str(u8 id) { return unit_skills[id]; }
const char* unit_ability_str(u16 id) { return unit_abilities[id]; }
