#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA662_A31M_A35M
/* If the ALT key is pressed while this program starts or if no language file can be found on the game disk then the program displays the language selection screen else the program automatically starts the game in the language associated with the language file name */
char* G3312_pc_GameDiskVolumeName = "DungeonMaster:";
char* G3313_apc_LanguageChoiceFileNames[3] = {"CSBGAMEF.DAT", "CSBGAME.DAT", "CSBGAMEG.DAT"};
char G3314_ac_FullFileName[64];
unsigned char G3308_auc_InfoData[sizeof(struct InfoData) + 4]; /* Space to store a long aligned structure */
struct IOStdReq* G3310_ps_IOStdReq;
struct MsgPort* G3309_ps_MsgPort;
struct Device* G3311_ps_Device;
unsigned char G3315_auc_KeyboardMatrix[13];


char* F1289_GetFullFileName(P4388_pc_VolumeName, P4389_pc_FileName)
char* P4388_pc_VolumeName;
char* P4389_pc_FileName;
{
        register char* L4785_pc_Character;


        L4785_pc_Character = G3314_ac_FullFileName;
        while (*L4785_pc_Character++ = *P4388_pc_VolumeName++);
        L4785_pc_Character--;
        while (*L4785_pc_Character++ = *P4389_pc_FileName++);
        return G3314_ac_FullFileName;
}

int16_t F1290_GetVolumeState(P4387_pc_VolumeName)
char* P4387_pc_VolumeName;
{
        register struct FileLock* L4781_ps_FileLock;
        register struct InfoData* L4782_ps_InfoData;
        register int16_t L4783_i_RetryCount;
        register int16_t L4784_i_Result;


        L4782_ps_InfoData = (struct InfoData*)(((long)G3308_auc_InfoData + 3) & 0xFFFFFFFC); /* structure must be long aligned */
        L4783_i_RetryCount = 0;
        while (((L4781_ps_FileLock = Lock(P4387_pc_VolumeName, ACCESS_READ)) == NULL) && (L4783_i_RetryCount++ < 16)) {
                Delay(7L);
        }
        if (L4781_ps_FileLock == NULL) {
                L4784_i_Result = 0; /* Volume is absent */
        } else {
                L4783_i_RetryCount = 0;
                while (((L4784_i_Result = Info(L4781_ps_FileLock, L4782_ps_InfoData)) == 0) && (L4783_i_RetryCount++ < 16)) {
                        Delay(7L);
                }
                UnLock(L4781_ps_FileLock);
                if (L4784_i_Result == 0) {
                        L4784_i_Result = 0; /* Volume is absent */
                } else {
                        if (L4782_ps_InfoData->id_DiskState == ID_WRITE_PROTECTED) {
                                L4784_i_Result = 1; /* Volume is present and write protected */
                        } else {
                                L4784_i_Result = 2; /* Volume is present and not write protected */
                        }
                }
        }
        return L4784_i_Result;
}

int16_t F1291_GetLanguageIndexFromLanguageFileName(P4386_pc_VolumeName)
char* P4386_pc_VolumeName;
{
        register int16_t L4779_i_LanguageChoiceFileNameIndex;
        FILE_HANDLE L4780_FileHandle;


        if (F1290_GetVolumeState(G3312_pc_GameDiskVolumeName) == 0) { /* Volume is absent */
                return -1;
        }
        for (L4779_i_LanguageChoiceFileNameIndex = 0; L4779_i_LanguageChoiceFileNameIndex < 3; L4779_i_LanguageChoiceFileNameIndex++) {
                if (L4780_FileHandle = F0770_FILE_Open(F1289_GetFullFileName(P4386_pc_VolumeName, G3313_apc_LanguageChoiceFileNames[L4779_i_LanguageChoiceFileNameIndex]))) {
                        F0771_FILE_Close(L4780_FileHandle);
                        return L4779_i_LanguageChoiceFileNameIndex;
                }
        }
        return -1;
}

void F1292_DeleteLanguageFilesFromGameDisk(
)
{
        register int16_t L4786_i_LanguageFileNameIndex;


        if (F1290_GetVolumeState(G3312_pc_GameDiskVolumeName) == 2) { /* Volume is present and not write protected */
                for (L4786_i_LanguageFileNameIndex = 0; L4786_i_LanguageFileNameIndex < 3; L4786_i_LanguageFileNameIndex++) {
                        F0777_FILE_Delete(F1289_GetFullFileName(G3312_pc_GameDiskVolumeName, G3313_apc_LanguageChoiceFileNames[L4786_i_LanguageFileNameIndex]));
                }
        }
}

BOOLEAN F1293_CreateLanguageFile(P4390_LanguageFileNameIndex)
int16_t P4390_LanguageFileNameIndex;
{
        FILE_HANDLE L4787_FileHandle;


        if (F1290_GetVolumeState(G3312_pc_GameDiskVolumeName) != 2) { /* Volume is not present or write protected */
                return C0_FALSE;
        }
        F1292_DeleteLanguageFilesFromGameDisk();
        if (L4787_FileHandle = F0776_FILE_Create(F1289_GetFullFileName(G3312_pc_GameDiskVolumeName, G3313_apc_LanguageChoiceFileNames[P4390_LanguageFileNameIndex]))) {
                F0771_FILE_Close(L4787_FileHandle);
        }
        return (L4787_FileHandle != NULL);
}

void F1294_GetKeyboardMatrix(
)
{
        G3310_ps_IOStdReq->io_Command = KBD_READMATRIX;
        G3310_ps_IOStdReq->io_Flags = IOF_QUICK;
        G3310_ps_IOStdReq->io_Data = &G3315_auc_KeyboardMatrix;
        G3310_ps_IOStdReq->io_Length = 13;
        DoIO(G3310_ps_IOStdReq);
}

BOOLEAN F1295_IsAltKeyPressed(
)
{
        F1294_GetKeyboardMatrix();
        return ((G3315_auc_KeyboardMatrix[12] & 0x10) || (G3315_auc_KeyboardMatrix[12] & 0x20)); /* Keycode 100 = Left Alt; Keycode 101 = Right Alt */
}

void F1296_(
)
{
        if ((G3309_ps_MsgPort = CreatePort(0L, 16L)) == NULL) {
                F9073_DisplayError(0x8F10014);
        }
        if ((G3310_ps_IOStdReq = CreateStdIO(G3309_ps_MsgPort)) == NULL) {
                F9073_DisplayError(0x8F10015);
        }
        if (OpenDevice("keyboard.device", 0L, G3310_ps_IOStdReq, 0L)) {
                F9073_DisplayError(0x8F10016);
        }
        G3311_ps_Device = G3310_ps_IOStdReq->io_Device;
}

void F1297_(
)
{
        if (G3311_ps_Device) {
                CloseDevice(G3310_ps_IOStdReq);
        }
        G3311_ps_Device = NULL;
        if (G3310_ps_IOStdReq) {
                DeleteStdIO(G3310_ps_IOStdReq);
        }
        G3310_ps_IOStdReq = NULL;
        if (G3309_ps_MsgPort) {
                DeletePort(G3309_ps_MsgPort);
        }
        G3309_ps_MsgPort = NULL;
}

int16_t F1298_GetAutomaticStartupLanguage(
)
{
        int16_t L4777_LanguageIndex;


        if (F1295_IsAltKeyPressed()) { /* Bypass automatic language detection if ALT key is pressed */
                return -1;
        }
        L4777_LanguageIndex = F1291_GetLanguageIndexFromLanguageFileName(G3312_pc_GameDiskVolumeName);
        return L4777_LanguageIndex;
}

BOOLEAN F1299_SaveLanguageChoice(P4385_i_LanguageFileIndex)
int16_t P4385_i_LanguageFileIndex;
{
        BOOLEAN L4778_i_Result;


        L4778_i_Result = F1293_CreateLanguageFile(P4385_i_LanguageFileIndex);
        return L4778_i_Result;
}

void F1300_Initialize(
)
{
        F1088_OpenAmigaStuff();
        F1296_();
}

void F1301_Cleanup(
)
{
        F1297_();
        F1089_CloseAmigaStuff();
}
#endif
