#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA234_S12E
#include <OSBIND.H>

#define C0L_FILLER                     0L
#define C0_FILE_ATTRIBUTES              0
#define C0_READ_ONLY                    0
#define C0_SEEK_FROM_BEGINNING_OF_FILE  0
#define C2_SEEK_FROM_END_OF_FILE        2

unsigned int16_t G0715_aui_MiniDungeon[54] = {
        0x0005, /* Dungeon ID */
        0x0023, /* Map data size */
        0x0100, /* Map count (most significant byte only) */
        0x0000, /* Text data size */
        0x0803, /* Party starting location at x=3, y = 0, south direction */
        0x0000, /* Thing list size */
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* Thing counts */
        0x0000, 0x0000, 0x0000, 0x0000, 0x3100, 0x0000, 0x1000, 0x1000, /* Map definition width=5, height=7 */
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* Index of squares with things on them */
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0000, 0x0020, 0x2020, 0x2020, 0x2020, 0x0000, 0x0000, 0x0020, 0x2001, /* Map data and first byte of the checksum */
        0xC53C /* Second byte of the checksum (checksum = 0x01C5) and an unused last byte */
};

/* Map: W=Wall, F=Floor */
/* WWWFW */
/* WWWFW */
/* WWWFW */
/* WWFFW */
/* WWWFW */
/* WWWFF */
/* WWWFF */


int16_t main(
)
{
        int16_t L1841_i_FileHandle;
        long L1842_l_InputCharacter;
        char* L1843_pc_ErrorMessage;
        long L1844_l_FileSize;
        unsigned long L1845_ul_DungeonOffsetInSavedGame;
        unsigned char* L1846_puc_Buffer;
        unsigned char* L1847_puc_DungeonDataInSavedGame;
        unsigned char* L1848_puc_NextMiniDungeonByte;
        unsigned char* L1849_puc_ModifiedSavedGame;
        char L1850_c_FileOpened;


        L1843_pc_ErrorMessage = NULL;
        L1846_puc_Buffer = NULL;
        L1850_c_FileOpened = C0_FALSE;
        Cconws("DM Champion Extraction Utility  v1.0\n\r");
        Cconws("           Copyright (C) 1990\n\r");
        Cconws("     FTL Games/Software Heaven, Inc.\n\n\r");
        Cconws("This utility compresses a Dungeon Master\n\r");
        Cconws("saved game into a shortened format which\n\r");
        Cconws("can be read by the Chaos Strikes Back\n\r");
        Cconws("Utility Disk.  It removes all data from\n\r");
        Cconws("the saved game which is copyrighted by\n\r");
        Cconws("FTL Games/Software Heaven, Inc.\n\r");
        Cconws("The shortened saved game is intended\n\r");
        Cconws("solely to enable CSB owners to exchange\n\r");
        Cconws("advanced Dungeon Master Champions for\n\r");
        Cconws("import into Chaos Strikes Back.\n\n\r");
        Cconws("Insert a disk which contains a Dungeon\n\r");
        Cconws("Master saved game into drive A:\n\r");
        Cconws("Press 'Y' when ready: \n\r");
        while (Cconis())
                Crawcin();
        L1842_l_InputCharacter = Crawcin() & 0xFF;
        if ((L1842_l_InputCharacter != 'y') && (L1842_l_InputCharacter != 'Y')) {
                L1843_pc_ErrorMessage = "User exit";
                goto T0949000_ExitProgram;
        }
        L1841_i_FileHandle = Fopen("A:\\DMGAME.DAT", C0_READ_ONLY);
        if (L1841_i_FileHandle < 0) {
                L1843_pc_ErrorMessage = "Can't find DM saved game 'DMGAME.DAT'";
                goto T0949000_ExitProgram;
        }
        L1850_c_FileOpened = C1_TRUE;
        L1844_l_FileSize = Fseek(0L, L1841_i_FileHandle, C2_SEEK_FROM_END_OF_FILE);
        if (Fseek(0L, L1841_i_FileHandle, C0_SEEK_FROM_BEGINNING_OF_FILE) < 0) {
                L1843_pc_ErrorMessage = "File access 1";
                goto T0949000_ExitProgram;
        }
        if ((L1844_l_FileSize == 47710) || (L1844_l_FileSize == 47712)) { /* Size of a Dungeon Master 1.0 EN saved game without or with checksum */
                L1845_ul_DungeonOffsetInSavedGame = 0x000028F4;
        } else {
                if (L1844_l_FileSize == 47914) { /* Size of a Dungeon Master 1.1 EN saved game */
                        L1845_ul_DungeonOffsetInSavedGame = 0x00002924;
                } else {
                        if (L1844_l_FileSize == 47916) { /* Size of a Dungeon Master 1.2 EN saved game */
                                L1845_ul_DungeonOffsetInSavedGame = 0x00002924;
                        } else {
                                L1843_pc_ErrorMessage = "Incompatible DMGAME.DAT file";
                                goto T0949000_ExitProgram;
                        }
                }
        }
        L1846_puc_Buffer = (unsigned char*)Malloc(L1844_l_FileSize);
        if (!L1846_puc_Buffer) {
                L1843_pc_ErrorMessage = "not enough memory!";
                goto T0949000_ExitProgram;
        }
        L1849_puc_ModifiedSavedGame = L1847_puc_DungeonDataInSavedGame = L1846_puc_Buffer;
        if (Fread(L1841_i_FileHandle, (long)L1845_ul_DungeonOffsetInSavedGame, L1847_puc_DungeonDataInSavedGame) != L1845_ul_DungeonOffsetInSavedGame) {
                L1843_pc_ErrorMessage = "File access 2";
                goto T0949000_ExitProgram;
        }
        Fclose(L1841_i_FileHandle);
        L1850_c_FileOpened = C0_FALSE;
        L1847_puc_DungeonDataInSavedGame = L1847_puc_DungeonDataInSavedGame + L1845_ul_DungeonOffsetInSavedGame;
        L1848_puc_NextMiniDungeonByte = (unsigned char*)&G0715_aui_MiniDungeon;
        for (L1844_l_FileSize = 0; L1844_l_FileSize < sizeof(G0715_aui_MiniDungeon); L1844_l_FileSize++) {
                *L1847_puc_DungeonDataInSavedGame++ = *L1848_puc_NextMiniDungeonByte++;
        }
        Cconws("Insert a blank formatted disk which will\n\r");
        Cconws("receive the newly compressed saved game\n\r");
        Cconws("into drive A:\n\r");
        Cconws("Press 'Y' when ready:\n\r");
        while (Cconis())
                Crawcin();
        L1842_l_InputCharacter = Crawcin() & 0xFF;
        if ((L1842_l_InputCharacter != 'y') && (L1842_l_InputCharacter != 'Y')) {
                L1843_pc_ErrorMessage = "User exit";
                goto T0949000_ExitProgram;
        }
        L1841_i_FileHandle = Fopen("A:\\DMGAME.DAT", C0_READ_ONLY);
        if (L1841_i_FileHandle >= 0) {
                L1843_pc_ErrorMessage = "This disk alreay has a DMGAME.DAT file";
                goto T0949000_ExitProgram;
        }
        L1841_i_FileHandle = Fcreate("A:\\DMGAME.DAT", C0_FILE_ATTRIBUTES);
        if (L1841_i_FileHandle < 0) {
                L1843_pc_ErrorMessage = "Trouble writing the output file";
                goto T0949000_ExitProgram;
        }
        L1850_c_FileOpened = C1_TRUE;
        if (Fwrite(L1841_i_FileHandle, (long)L1845_ul_DungeonOffsetInSavedGame + sizeof(G0715_aui_MiniDungeon), L1849_puc_ModifiedSavedGame) != L1845_ul_DungeonOffsetInSavedGame + sizeof(G0715_aui_MiniDungeon)) {
                L1843_pc_ErrorMessage = "File access 3";
                goto T0949000_ExitProgram;
        }
        T0949000_ExitProgram:
        if (L1846_puc_Buffer)
                Mfree(L1846_puc_Buffer);
        if (L1850_c_FileOpened)
                Fclose(L1841_i_FileHandle);
        if (L1843_pc_ErrorMessage) {
                Cconws("ERROR:\n\r");
                Cconws(L1843_pc_ErrorMessage);
                Cconws("\n\rAborted.\n\r");
        } else {
                Cconws("Success.\n\r");
        }
        Cconws("Press any key to exit.\n\r");
        while (Cconis())
                Crawcin();
        Crawcin();
}
#endif
