#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
#ifdef MEDIA667_P31J
int16_t G0433_i_CommandQueueFirstIndex;
int16_t G0437_i_PendingClickX;
int16_t G0438_i_PendingClickY;
UD_COMMAND G2247_CommandQueue[6];
int16_t G0439_i_PendingClickButtonsStatus;
BOOLEAN G0436_B_PendingClickPresent;
INPUT_INFO* G7100_as_InputInfo[6];
MEMBLOCK G2245_apuc_AllocatedMemoryBlocks[200];
int16_t G2253_i_AllocatedBlockCount; /* G7101_ */
int16_t G7102_;
int16_t G7103_;
ON_DISK_PORTRAIT G2248_as_OnDiskPortraits[4];
int16_t G2271_ai_Box_EditText[4];
BOOLEAN G7104_TextCursorVisible;
int16_t G7105_EditChampionNameOrTitleCharacterIndex;
int16_t G7106_EditChampionNameOrTitle;
int16_t G7107_SelectedColorIndex;
int16_t G7108_BlinkingCursorTimer;
int16_t G7109_SelectedChampionIndex;
BACKUP_PORTRAIT G2329_s_UndoPortrait;
unsigned char* G2243_OnScreenPortraitBitmaps[4];
long G7110_RandomSeed;
GAME* G7111_Games; /* Pointer to array of 2 structs of size 902 */
unsigned char* G2244_apuc_DialogBitmaps[2];
BACKUP_PORTRAIT G2328_s_TempPortrait;
CMP G2246_CMPData;
int16_t G7112_HideMousePointerCount;
int16_t G7113_DialogBitmapIndex;
int16_t G7114_LoadedChampionCount;
long G7115_DialogByteCount;
unsigned char* G7116_Bitmap_FilePickerArrows;
unsigned char* G7117_Bitmap_Mirror;
char G4178_[120];
char G4179_[120];
long G7118_;
CEDT10STRUCT G4185_;
/* 1 byte padding included by compiler because next uninitialized global variable in another module */
#endif


void F7000_SavePortrait(
)
{
#ifdef MEDIA667_P31J
        register int16_t L3818_i_;
        register int16_t L3817_i_;
#endif
#ifdef MEDIA681_F31E_F31J_X31J_AU2E_AU3E
        int16_t L3818_i_;
        int16_t L3817_i_;
#endif
        unsigned char* L3811_puc_;
        long L6041_l_Unreferenced;
        unsigned char L3812_uc_;
        char L3813_ac_FileName[10];
#ifdef MEDIA695_X31J_P31J_AU2E_AU3E
        int16_t L3814_i_;
#endif
#ifdef MEDIA670_F31E_F31J
        volatile int16_t L3814_i_; /* ANOMALY_FMT Unwanted optimization in F7051_ if not volatile */
#endif
        PROGRAM_STATE L3815_s_;
        char L3816_c_;


        L3815_s_.Previous = G7128_LastProgramState;
        L3815_s_.Ordinal = ++G7129_ProgramStateOrdinal;
        G7128_LastProgramState = &L3815_s_;
        L3815_s_.ReturnValue = setjmp(L3815_s_.State);
        L3817_i_ = L3815_s_.ReturnValue;
        if (L3817_i_) {
                L3815_s_.ReturnValue = 0;
                if (L3817_i_ == 0x1810) {
                        L3818_i_ = 0;
                } else {
                        F7074_DrawDialogErrorMessage(G7077_ERROR_WHILE_SAVING_CHAMPION, C08_COLOR_RED);
                        L3818_i_ = 0;
                }
        } else {
                L3816_c_ = 0;
                L3811_puc_ = G2246_CMPData.Name;
                L3817_i_ = 0;
                while ((L3812_uc_ = *L3811_puc_) && (L3817_i_ < 8)) {
                        if ((L3812_uc_ >= 'A') && (L3812_uc_ <= 'Z')) {
                                L3813_ac_FileName[L3817_i_++] = L3812_uc_;
                        }
                        L3811_puc_++;
                }
                if (L3817_i_ == 0) {
                        F0088_strcpy(L3813_ac_FileName, G7020_PORTRAIT); /* Default file name */
                } else {
                        L3813_ac_FileName[L3817_i_] = '\0';
                }
                L3814_i_ = F7132_(M747_FILE_ID_SAVE_CMP);
                F7137_FileOperation(L3814_i_, 0xF00E, C17_FILEOP_SOURCE_FILENAME, (long)"#CHAMP_NAME#", C18_FILEOP_DESTINATION_FILENAME, (long)L3813_ac_FileName, 0);
                F7137_FileOperation(L3814_i_, 0xF004, C16_FILEOP_BYTE_COUNT, (long)1, 0);
                F7072_GetDialogChoiceOrdinal(G7073_PLEASE_WAIT_SAVING_CHAMPION, C15_COLOR_WHITE, NULL, NULL);
                L3816_c_ = 1;
#ifdef MEDIA671_F31E_F31J_P31J
                G2246_CMPData.Magic = (G2246_CMPData.Magic >> 8) | (G2246_CMPData.Magic << 8); /* Convert from little endian to big endian */
                G2246_CMPData.DungeonID = (G2246_CMPData.DungeonID >> 8) | (G2246_CMPData.DungeonID << 8);
                G2246_CMPData.Platform = (G2246_CMPData.Platform >> 8) | (G2246_CMPData.Platform << 8);
                G2246_CMPData.cmp_ui_6 = (G2246_CMPData.cmp_ui_6 >> 8) | (G2246_CMPData.cmp_ui_6 << 8);
                G2246_CMPData.cmp_ui_8 = (G2246_CMPData.cmp_ui_8 >> 8) | (G2246_CMPData.cmp_ui_8 << 8);
#endif
                F7137_FileOperation(L3814_i_, C0xF00B_OPERATION_WRITE, C16_FILEOP_BYTE_COUNT, (long)508, C15_FILEOP_SET_BUFFER, (long)&G2246_CMPData, 0); /* 508 size of CMP portrait file */
#ifdef MEDIA671_F31E_F31J_P31J
                G2246_CMPData.Magic = (G2246_CMPData.Magic >> 8) | (G2246_CMPData.Magic << 8); /* Convert from big endian to little endian */
                G2246_CMPData.DungeonID = (G2246_CMPData.DungeonID >> 8) | (G2246_CMPData.DungeonID << 8);
                G2246_CMPData.Platform = (G2246_CMPData.Platform >> 8) | (G2246_CMPData.Platform << 8);
                G2246_CMPData.cmp_ui_6 = (G2246_CMPData.cmp_ui_6 >> 8) | (G2246_CMPData.cmp_ui_6 << 8);
                G2246_CMPData.cmp_ui_8 = (G2246_CMPData.cmp_ui_8 >> 8) | (G2246_CMPData.cmp_ui_8 << 8);
#endif
                L3818_i_ = 0;
        }
        if (L3814_i_ != -1) {
                F7135_(L3814_i_);
                L3814_i_ = -1;
        }
        if (L3816_c_) {
                F7071_();
        }
        if (L3818_i_);
        F7111_RestorePreviousProgramState();
}

void F7001_SaveChampions(
void)
{
        int16_t L3821_i_;
        int16_t L3819_i_;
        unsigned int16_t L3820_ui_;


        if (!G7111_Games[C0_GAME_SOURCE].GameLoaded || (G7114_LoadedChampionCount == 0)) {
                L3821_i_ = 0;
        } else {
                if (G2246_CMPData.Name[0] == '\0') {
                        F7074_DrawDialogErrorMessage(G7081_EACH_CHAMPION_MUST_HAVE_A_UNIQUE_FIRST_NAME, C08_COLOR_RED);
                        L3821_i_ = 0;
                } else {
                        L3819_i_ = F7072_GetDialogChoiceOrdinal(G7069_YOU_MAY_SAVE_EITHER_THE_WHOLE_GAME_OR_THE_SELECTED_CHAMPIONS_PORTRAIT, C15_COLOR_WHITE | MASK0x8000_INVERT_BOX, G7064_GAME_PORTRAIT_CANCEL, &G2254_s_Input_Dialog3ChoicesB);
                        for (L3820_ui_ = 0; L3820_ui_ < 5; L3820_ui_++) {
                                F7323_WaitVerticalBlank();
                        }
                        F7071_();
                        if (L3819_i_ == 1) {
                                F7052_SaveGame(C0_GAME_SOURCE);
                        } else {
                                if (L3819_i_ == 2) {
                                        F7000_SavePortrait();
                                }
                        }
                        L3821_i_ = 0;
                }
        }
        if (L3821_i_);
}

#ifdef MEDIA699_X31J_AU2E_AU3E
unsigned char F2055_(unsigned char L5911_) {
        if (((L5911_ >= 'A') && (L5911_ <= 'Z')) || ((L5911_ >= 'a') && (L5911_ <= 'z'))) {
                L5911_ = ((L5911_ < 'a') || (L5911_ > 'z')) ? L5911_ : L5911_ - ('a' - 'A');
        } else {
                if ((L5911_ != '.') && (L5911_ != ',') && (L5911_ != '\0') && (L5911_ != ';') && (L5911_ != ':') && (L5911_ != ' ')
#ifdef MEDIA768_AU2E_AU3E
                   && (L5911_ != '\'') && (L5911_ != '-')
#endif
                   ) {
                        L5911_ = ' ';
                }
        }
        return L5911_;
}
#endif

void F7002_ReadCMP(int16_t P3550_i_)
{
        int16_t L3822_i_;


        F7137_FileOperation(P3550_i_, C0xF00A_OPERATION_READ, C16_FILEOP_BYTE_COUNT, (long)508, C15_FILEOP_SET_BUFFER, (long)&G2246_CMPData, 0);
#ifdef MEDIA671_F31E_F31J_P31J
        G2246_CMPData.Magic = (G2246_CMPData.Magic >> 8) | (G2246_CMPData.Magic << 8);
        G2246_CMPData.DungeonID = (G2246_CMPData.DungeonID >> 8) | (G2246_CMPData.DungeonID << 8);
        G2246_CMPData.Platform = (G2246_CMPData.Platform >> 8) | (G2246_CMPData.Platform << 8);
        G2246_CMPData.cmp_ui_6 = (G2246_CMPData.cmp_ui_6 >> 8) | (G2246_CMPData.cmp_ui_6 << 8);
        G2246_CMPData.cmp_ui_8 = (G2246_CMPData.cmp_ui_8 >> 8) | (G2246_CMPData.cmp_ui_8 << 8);
#endif
        if ((G2246_CMPData.cmp_ui_8 & 0x8000) || (G2246_CMPData.Magic != 0x91A7) || (G2246_CMPData.cmp_ui_6 != 1)) {
                F7074_DrawDialogErrorMessage(G7080_THIS_CHAMPION_CANNOT_BE_USED_WITH_THIS_ADVENTURE, C08_COLOR_RED);
                F7112_FallbackToLastProgramState(C0_FALSE, 23002, 0L);
        }
        if (!(((G2246_CMPData.Name[0] >= 'A') && (G2246_CMPData.Name[0] <= 'Z')) || ((G2246_CMPData.Name[0] >= 'a') && (G2246_CMPData.Name[0] <= 'z')))) {
                G2246_CMPData.Name[0] = '\0';
        }
        for (L3822_i_ = 0; L3822_i_ < 8; L3822_i_++) {
                if (G2246_CMPData.Name[L3822_i_] == '\0') {
                        break;
                }
#ifdef MEDIA699_X31J_AU2E_AU3E
                G2246_CMPData.Name[L3822_i_] = F2055_(G2246_CMPData.Name[L3822_i_]);
#endif
        }
        while (L3822_i_ < 8) {
                G2246_CMPData.Name[L3822_i_] = '\0';
                L3822_i_++;
        }
        if (L3822_i_ >= 8) {
                G2246_CMPData.Name[7] = '\0';
        }
        if (!(((G2246_CMPData.Title[0] >= 'A') && (G2246_CMPData.Title[0] <= 'Z')) || ((G2246_CMPData.Title[0] >= 'a') && (G2246_CMPData.Title[0] <= 'z')))) {
                G2246_CMPData.Title[0] = '\0';
        }
        for (L3822_i_ = 0; L3822_i_ < 20; L3822_i_++) {
                if (G2246_CMPData.Title[L3822_i_] == '\0') {
                        break;
                }
#ifdef MEDIA699_X31J_AU2E_AU3E
                G2246_CMPData.Title[L3822_i_] = F2055_(G2246_CMPData.Title[L3822_i_]);
#endif
        }
        while (L3822_i_ < 20) {
                G2246_CMPData.Title[L3822_i_] = '\0';
                L3822_i_++;
        }
        if (L3822_i_ >= 20) {
                G2246_CMPData.Title[19] = '\0';
        }
}

int16_t F7003_LoadPortrait(
)
{
        int16_t L3823_i_;
        int16_t L3824_i_;
#ifdef MEDIA695_X31J_P31J_AU2E_AU3E
        short L3825_i_FileHandle;
#endif
#ifdef MEDIA670_F31E_F31J
        volatile short L3825_i_FileHandle; /* ANOMALY_FMT Unwanted optimization in F7051_ if not volatile */
#endif
        PROGRAM_STATE L3826_s_;
        char L3827_c_;


        L3826_s_.Previous = G7128_LastProgramState;
        L3826_s_.Ordinal = ++G7129_ProgramStateOrdinal;
        G7128_LastProgramState = &L3826_s_;
        L3826_s_.ReturnValue = setjmp(L3826_s_.State);
        L3824_i_ = L3826_s_.ReturnValue;
        if (L3824_i_) {
                L3826_s_.ReturnValue = 0;
                if (L3824_i_ == 0x1810) {
                        L3823_i_ = -2;
                } else {
#ifdef MEDIA699_X31J_AU2E_AU3E
                        if (L3824_i_ != 0x59DA) {
#endif
                                F7074_DrawDialogErrorMessage(G7079_THIS_CHAMPION_IS_DAMAGED, C08_COLOR_RED);
#ifdef MEDIA699_X31J_AU2E_AU3E
                        }
#endif
                        L3823_i_ = -1;
                }
        } else {
                L3825_i_FileHandle = -1;
                L3827_c_ = 0;
                L3825_i_FileHandle = F7132_(M743_FILE_ID_LOAD_CMP);
                F7137_FileOperation(L3825_i_FileHandle, 0xF002, 0);
                F7072_GetDialogChoiceOrdinal(G7074_PLEASE_WAIT_LOADING_CHAMPION, C15_COLOR_WHITE, NULL, NULL);
                L3827_c_ = 1;
                F7002_ReadCMP(L3825_i_FileHandle);
                L3823_i_ = 2;
        }
        if (L3825_i_FileHandle != -1) {
                F7135_(L3825_i_FileHandle);
                L3825_i_FileHandle = -1;
        }
        if (L3827_c_) {
                F7071_();
        }
        F7111_RestorePreviousProgramState();
        return L3823_i_;
}

int16_t F7004_LoadChampions(
void)
{
        int16_t L6039i_i_Unreferenced;
        int16_t L3829_i_;
        int16_t L6040_i_Unreferenced;
        int16_t L3830_i_;
        unsigned int16_t L3828_ui_;


        L3829_i_ = F7072_GetDialogChoiceOrdinal(G7068_YOU_MAY_LOAD_EITHER_A_SAVED_GAME_OR_A_CHAMPION_PORTRAIT, C15_COLOR_WHITE | MASK0x8000_INVERT_BOX, G7064_GAME_PORTRAIT_CANCEL, &G2254_s_Input_Dialog3ChoicesB);
        for (L3828_ui_ = 0; L3828_ui_ < 5; L3828_ui_++) {
                F7323_WaitVerticalBlank();
        }
        F7071_();
        if (L3829_i_ == 3) { /* Cancel */
                L3830_i_ = -1;
        } else {
                if (L3829_i_ == 1) { /* Game */
                        L3830_i_ = F7051_LoadGame(C0_GAME_SOURCE);
                } else {
                        if (L3829_i_ == 2) { /* Portrait */
                                L3830_i_ = F7003_LoadPortrait();
                        }
                }
        }
        return L3830_i_;
}

int16_t F7005_Quit(int16_t P3551_i_)
{
        int16_t L3833_i_;
        PROGRAM_STATE L3831_s_;
        int16_t L3834_i_;
        short L3832_i_;


        L3832_i_ = -1;
        L3831_s_.Previous = G7128_LastProgramState;
        L3831_s_.Ordinal = ++G7129_ProgramStateOrdinal;
        G7128_LastProgramState = &L3831_s_;
        L3831_s_.ReturnValue = setjmp(L3831_s_.State);
        if ((L3834_i_ = L3831_s_.ReturnValue) != 0) {
                L3831_s_.ReturnValue = 0;
                if ((L3834_i_ != 0x1810) || !P3551_i_) {
                        goto loc_10AE2;
                }
                L3833_i_ = -1;
        } else {
                loc_10AE2:
                if (L3832_i_ == -1) {
                        L3832_i_ = F7132_(M748_FILE_ID_CURRENT_FOLDER);
                }
                F7137_FileOperation(L3832_i_, 0xF00F, 0);
                L3833_i_ = 0;
        }
        if (L3832_i_ != -1) {
                T7005001:
                F7135_(L3832_i_);
        }
        F7111_RestorePreviousProgramState();
        return L3833_i_;
}
#endif
