#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA042_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
#ifdef MEDIA006_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB /* CHANGE7_03_IMPROVEMENT Support for two floppy disk drives */
char* G0519_pc_ADMGAMEDAT = "A:\\DMGAME.DAT";
char* G0520_pc_ADMGAMEBAK = "A:\\DMGAME.BAK";
#endif
#ifdef MEDIA098_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
FILE_HANDLE G0521_i_GameFileHandle; /* Dungeon file or saved game file */
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
FILE_HANDLE G1023_ps_GameFileHandle; /* Dungeon file or saved game file */
#endif
#ifdef MEDIA628_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J
char G0534_ac_SaveHeaderAdditionalData[132];
#endif
#ifdef MEDIA545_F20E_F20J_X30J_A36M
char G0534_ac_SaveHeaderAdditionalData[134];
#endif
#ifdef MEDIA096_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_F20E_F20J_X30J_P20JA_P20JB
long G0522_l_Unreferenced; /* BUG0_00 Useless code */
#endif
#ifdef MEDIA008_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E
BOOLEAN G0523_B_RestartGameRequested;
BOOLEAN G0524_B_RestartGameAllowed;
#endif
#ifdef MEDIA097_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_P31J
long G0525_l_GameID;
#endif
#ifdef MEDIA309_S20E_S21E_G20E_G21E_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* CHANGE7_29_IMPROVEMENT */
unsigned int16_t G0526_ui_DungeonID;
#ifdef MEDIA747_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J
long G0525_l_GameID;
#endif
int16_t G0527_i_Platform;
int16_t G0528_i_Format;
int16_t G0529_i_Useless; /* BUG0_00 Useless code */
#endif
#ifdef MEDIA388_G20E_G21E
BOOLEAN G1147_B_LoadBonusDungeon;
#endif
#ifdef MEDIA309_S20E_S21E_G20E_G21E_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* CHANGE7_30_IMPROVEMENT */
BOOLEAN G0530_B_LoadingCompressedDungeon;
unsigned char* G0531_puc_DecompressedDungeonCurrentPosition;
long G0532_l_DecompressedDungeonRemainingByteCount;
#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
#ifndef NOCOPYPROTECTION
int16_t G0533_ai_CodePatch2_CPSE[34];
#endif
#endif
#ifdef MEDIA333_S20E_S21E_P31J /* CHANGE7_29_IMPROVEMENT */
char G0534_ac_SaveHeaderAdditionalData[132];
#endif
#ifdef MEDIA462_P20JA_P20JB_I34E_I34M
char G0534_ac_SaveHeaderAdditionalData[134];
#endif
#ifdef MEDIA707_I34E_I34M
int16_t G2010_; /* G0589_i_MousePointerHotspotX ? */
int16_t G2011_; /* G0590_i_MousePointerHotspotY ? */
char G2204_Unreferenced[12];
#endif


#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
overlay "start"
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
segment "start"
#endif

#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
void F1061_Pre_Unreferenced(
void
)
{
#ifdef MEDIA618_A31E
#include "FAKE3.C"
#endif
#ifdef MEDIA657_A31M_A33M_A35E
#include "FAKE2.C"
#endif
#ifdef MEDIA742_A35M
#include "FAKE4.C"
#endif
}

void F1063_ChecksumEor_CPSX(
void (*P2849_pfv_StartFunction)() SEPARATOR
void (*P2850_pfv_EndFunction)()   FINAL_SEPARATOR
{
        asm {
                        movea.l P2849_pfv_StartFunction(a6),a0
                        movea.l P2850_pfv_EndFunction(a6),a1
                        moveq   #0,d0
                        moveq   #1,d1
                        bra.s   @Tloc_227B0
                @Tloc_227AA:
                        add.w   (a0)+,d0
                        eor.w   d1,d0
                        addq.w  #1,d1
                @Tloc_227B0:
                        cmpa.l  a1,a0
                        bcs.s   @Tloc_227AA
        }
}
#endif
#endif

BOOLEAN F0415_SAVEUTIL_IsReadBytesSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0800_puc_Buffer, P0801_ui_ByteCount)
#endif
unsigned char*   P0800_puc_Buffer   SEPARATOR
unsigned int16_t P0801_ui_ByteCount FINAL_SEPARATOR
{
        REGISTER long L1280_l_ByteCount;


        if ((L1280_l_ByteCount = P0801_ui_ByteCount) == 0) {
                return C1_TRUE;
        }
#ifdef MEDIA291_S20E_S21E_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* CHANGE7_30_IMPROVEMENT */
        if (G0530_B_LoadingCompressedDungeon) {
                if ((long)L1280_l_ByteCount > G0532_l_DecompressedDungeonRemainingByteCount) {
                        return C0_FALSE;
                }
                F0007_MAIN_CopyBytes(M772_CAST_PC(G0531_puc_DecompressedDungeonCurrentPosition), M772_CAST_PC(P0800_puc_Buffer), M573_BYTE_COUNT_LONG(L1280_l_ByteCount));
                G0531_puc_DecompressedDungeonCurrentPosition += L1280_l_ByteCount;
                G0532_l_DecompressedDungeonRemainingByteCount -= L1280_l_ByteCount;
                return C1_TRUE;
        }
#endif
#ifdef MEDIA391_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* ANOMALY_ST_BRANCH */
        else {
#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
                return (Fread(G0521_i_GameFileHandle, (long)L1280_l_ByteCount, P0800_puc_Buffer) == (long)L1280_l_ByteCount);
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
                return (Read(G1023_ps_GameFileHandle, P0800_puc_Buffer, (long)L1280_l_ByteCount) == L1280_l_ByteCount);
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
                return F0772_FILE_Read(G0521_i_GameFileHandle, L1280_l_ByteCount, P0800_puc_Buffer);
#endif
#ifdef MEDIA525_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
                return F0772_FILE_Read(G0521_i_GameFileHandle, (unsigned long)L1280_l_ByteCount, P0800_puc_Buffer);
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
                return F0772_FILE_Read(G1023_ps_GameFileHandle, (unsigned long)L1280_l_ByteCount, P0800_puc_Buffer);
#endif
#ifdef MEDIA391_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
        }
#endif
}

BOOLEAN F0416_SAVEUTIL_IsWriteBytesSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0802_puc_Buffer, P0803_ui_ByteCount)
#endif
unsigned char*   P0802_puc_Buffer   SEPARATOR
unsigned int16_t P0803_ui_ByteCount FINAL_SEPARATOR
{
        REGISTER unsigned long L1281_ul_ByteCount;


        if ((L1281_ul_ByteCount = P0803_ui_ByteCount) == 0) {
                return C1_TRUE;
        }
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
        return (Fwrite(G0521_i_GameFileHandle, (long)L1281_ul_ByteCount, P0802_puc_Buffer) == L1281_ul_ByteCount);
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        return (Write(G1023_ps_GameFileHandle, P0802_puc_Buffer, (long)L1281_ul_ByteCount) == L1281_ul_ByteCount);
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        return F0773_FILE_Write(G0521_i_GameFileHandle, L1281_ul_ByteCount, P0802_puc_Buffer);
#endif
#ifdef MEDIA525_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
        return F0773_FILE_Write(G0521_i_GameFileHandle, (unsigned long)L1281_ul_ByteCount, P0802_puc_Buffer);
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        return F0773_FILE_Write(G1023_ps_GameFileHandle, (unsigned long)L1281_ul_ByteCount, P0802_puc_Buffer);
#endif
}

#ifdef MEDIA128_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G20E_G21E_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
/* Obfuscation and deobfuscation are the same reversible operation */
unsigned int16_t F0417_SAVEUTIL_GetChecksumAndObfuscate(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0804_pui_Buffer, P0805_ui_Key, P0806_ui_WordCount)
#endif
REGISTER unsigned int16_t* P0804_pui_Buffer   SEPARATOR
REGISTER unsigned int16_t  P0805_ui_Key       SEPARATOR
REGISTER unsigned int16_t  P0806_ui_WordCount FINAL_SEPARATOR
{
        REGISTER unsigned int16_t L1282_ui_Checksum;


        L1282_ui_Checksum = P0805_ui_Key;
        do {
                L1282_ui_Checksum += *P0804_pui_Buffer;
                *P0804_pui_Buffer ^= P0805_ui_Key;
                L1282_ui_Checksum += *P0804_pui_Buffer++;
                P0805_ui_Key += P0806_ui_WordCount;
        } while (--P0806_ui_WordCount);
        return L1282_ui_Checksum;
}

unsigned int16_t F0418_SAVEUTIL_GetChecksum(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0807_pui_Buffer, P0808_ui_Key, P0809_ui_WordCount)
#endif
REGISTER unsigned int16_t* P0807_pui_Buffer   SEPARATOR
REGISTER unsigned int16_t  P0808_ui_Key       SEPARATOR
REGISTER unsigned int16_t  P0809_ui_WordCount FINAL_SEPARATOR
{
        REGISTER unsigned int16_t L1283_ui_Checksum;


        L1283_ui_Checksum = P0808_ui_Key;
        do {
                L1283_ui_Checksum += *P0807_pui_Buffer;
                L1283_ui_Checksum += *P0807_pui_Buffer++ ^ P0808_ui_Key;
                P0808_ui_Key += P0809_ui_WordCount;
        } while (--P0809_ui_WordCount);
        return L1283_ui_Checksum;
}

BOOLEAN F0419_SAVEUTIL_IsReadObfuscatedBytesAndValidateChecksumSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0810_pc_Buffer, P0811_ui_ByteCount, P0812_ui_Key, P0813_ui_ExpectedChecksum)
#endif
char*            P0810_pc_Buffer           SEPARATOR
unsigned int16_t P0811_ui_ByteCount        SEPARATOR
unsigned int16_t P0812_ui_Key              SEPARATOR
unsigned int16_t P0813_ui_ExpectedChecksum FINAL_SEPARATOR
{
        return (F0415_SAVEUTIL_IsReadBytesSuccessful(M774_CAST_PUC(P0810_pc_Buffer), P0811_ui_ByteCount) && (P0813_ui_ExpectedChecksum == F0417_SAVEUTIL_GetChecksumAndObfuscate(M776_CAST_PUI(P0810_pc_Buffer), P0812_ui_Key, P0811_ui_ByteCount >> 1)));
}

BOOLEAN F0420_SAVEUTIL_IsWriteObfuscatedSavePartSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0814_pc_Buffer, P0815_ui_ByteCount, P0816_i_Key, P0817_pi_Checksum)
#endif
REGISTER char*   P0814_pc_Buffer    SEPARATOR
unsigned int16_t P0815_ui_ByteCount SEPARATOR
int16_t          P0816_i_Key        SEPARATOR
int16_t*         P0817_pi_Checksum  FINAL_SEPARATOR
{
        REGISTER BOOLEAN L1284_B_IsWriteObfuscatedBytesSuccessful;
        REGISTER int16_t L1285_i_WordCount;


        *P0817_pi_Checksum = F0417_SAVEUTIL_GetChecksumAndObfuscate(M776_CAST_PUI(P0814_pc_Buffer), P0816_i_Key, L1285_i_WordCount = P0815_ui_ByteCount >> 1); /* Obfuscate before writing */
        L1284_B_IsWriteObfuscatedBytesSuccessful = F0416_SAVEUTIL_IsWriteBytesSuccessful(M774_CAST_PUC(P0814_pc_Buffer), P0815_ui_ByteCount);
        F0417_SAVEUTIL_GetChecksumAndObfuscate(M776_CAST_PUI(P0814_pc_Buffer), P0816_i_Key, L1285_i_WordCount); /* Deobfuscate after writing */
        return L1284_B_IsWriteObfuscatedBytesSuccessful;
}
#endif

#ifdef MEDIA240_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* CHANGE3_15_IMPROVEMENT */
BOOLEAN F0421_SAVEUTIL_IsReadBytesWithChecksumSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0818_puc_Buffer, P0819_pi_RunningChecksum, P0820_i_ByteCount)
#endif
REGISTER unsigned char* P0818_puc_Buffer         SEPARATOR
#ifdef MEDIA238_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
REGISTER int16_t*       P0819_pi_RunningChecksum SEPARATOR
#endif
#ifdef MEDIA529_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
int16_t*                P0819_pi_RunningChecksum SEPARATOR
#endif
REGISTER int16_t        P0820_i_ByteCount        FINAL_SEPARATOR
{
        REGISTER int16_t L1286_i_Checksum;


        L1286_i_Checksum = 0;
        if (!F0415_SAVEUTIL_IsReadBytesSuccessful(P0818_puc_Buffer, P0820_i_ByteCount)) {
                return C0_FALSE;
        }
        while (P0820_i_ByteCount--) {
                L1286_i_Checksum += *P0818_puc_Buffer++;
        }
        *P0819_pi_RunningChecksum += L1286_i_Checksum;
        return C1_TRUE;
}

BOOLEAN F0422_SAVEUTIL_IsWriteBytesWithChecksumSuccessful(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0821_puc_Buffer, P0822_pi_RunningChecksum, P0823_i_ByteCount)
#endif
REGISTER unsigned char* P0821_puc_Buffer         SEPARATOR
#ifdef MEDIA238_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
REGISTER int16_t*       P0822_pi_RunningChecksum SEPARATOR
#endif
#ifdef MEDIA529_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
int16_t*                P0822_pi_RunningChecksum SEPARATOR
#endif
REGISTER int16_t        P0823_i_ByteCount        FINAL_SEPARATOR
{
        REGISTER int16_t L1287_i_Checksum;


        L1287_i_Checksum = 0;
        if (!F0416_SAVEUTIL_IsWriteBytesSuccessful(P0821_puc_Buffer, P0823_i_ByteCount)) {
                return C0_FALSE;
        }
        while (P0823_i_ByteCount--) {
                L1287_i_Checksum += *P0821_puc_Buffer++;
        }
        *P0822_pi_RunningChecksum += L1287_i_Checksum;
        return C1_TRUE;
}
#endif

#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
#include "SAVEHEAD.C"
#include "LOADSAVE.C"
#endif

#ifdef MEDIA340_S21E /* CHANGE8_12_FIX New function called right after loading a saved game to clean up cloned things caused by BUG0_12. It contains several loops to flag things that are referenced as square first things, things possessed by groups, things in chests (things are removed from chest if they are already referenced elsewhere), scroll texts and projectile associated things. It also removes things from champion slots and from the leader hand if they are already flagged as referenced elsewhere */
void F0423_SAVEUTIL_FixClonedThings(
)
{
        REGISTER int16_t L1288_i_Multiple;
#define AL1288_i_Counter    L1288_i_Multiple
#define AL1288_i_ThingIndex L1288_i_Multiple
#define AL1288_i_BitMask    L1288_i_Multiple
        REGISTER int16_t L1289_i_Multiple;
#define AL1289_i_ThingIndex    L1289_i_Multiple
#define AL1289_i_BitMask       L1289_i_Multiple
#define AL1289_i_ByteIndex     L1289_i_Multiple
#define AL1289_i_ChampionIndex L1289_i_Multiple
        REGISTER THING L1290_T_Thing;
        REGISTER int16_t L1291_i_Multiple;
#define AL1291_i_ByteIndex  L1291_i_Multiple
#define AL1291_i_ThingIndex L1291_i_Multiple
#define AL1291_i_BitMask    L1291_i_Multiple
        REGISTER unsigned char* L1292_puc_Buffer;
        REGISTER char* L1293_pc_Address;
        int16_t L1294_i_ByteOffset;
        long L1295_l_Unreferenced; /* BUG0_00 Useless code */
        THING L1296_T_PreviousThing;
        GENERIC* L1297_ps_Generic;
        static unsigned char G0535_auc_BitIndexToMaskConversionTable[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };


        L1292_puc_Buffer = F0468_MEMORY_Allocate(2048L, C0_ALLOCATION_TEMPORARY_ON_TOP_OF_HEAP); /* 2048 bytes = 16384 bits = 1024 * 16 (one bit for each thing of each type)
        In the following loops, each bit is set if the corresponding thing is referenced in order to detect things that are referenced multiple times (clones) */
        F0008_MAIN_ClearBytes(L1292_puc_Buffer, 2048);
        for (AL1288_i_Counter = 0; AL1288_i_Counter < G0278_ps_DungeonHeader->SquareFirstThingCount; AL1288_i_Counter++) {
                if ((L1290_T_Thing = G0283_pT_SquareFirstThings[AL1288_i_Counter]) != C0xFFFF_THING_NONE) {
                        do {
                                AL1289_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                                AL1291_i_ByteIndex = (M012_TYPE(L1290_T_Thing) << 7) + (AL1289_i_ThingIndex >> 3);
                                AL1289_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1289_i_ThingIndex & 0x0007];
                                if (L1292_puc_Buffer[AL1291_i_ByteIndex] & AL1289_i_BitMask) {
                                        continue;
                                }
                                L1292_puc_Buffer[AL1291_i_ByteIndex] |= AL1289_i_BitMask;
                        } while ((L1290_T_Thing = F0159_DUNGEON_GetNextThing(L1290_T_Thing)) != C0xFFFE_THING_ENDOFLIST);
                }
        }
        L1293_pc_Address = (char*)G0284_apuc_ThingData[C04_THING_TYPE_GROUP];
        AL1288_i_Counter = 0;
        while (AL1288_i_Counter < G0278_ps_DungeonHeader->ThingCount[C04_THING_TYPE_GROUP]) {
                if ((((GROUP*)L1293_pc_Address)->Next != C0xFFFF_THING_NONE) && ((L1290_T_Thing = ((GROUP*)L1293_pc_Address)->Slot) != C0xFFFE_THING_ENDOFLIST)) { /* If group thing data is used and has possessions */
                        do {
                                AL1289_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                                AL1291_i_ByteIndex = (M012_TYPE(L1290_T_Thing) << 7) + (AL1289_i_ThingIndex >> 3);
                                AL1289_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1289_i_ThingIndex & 0x0007];
                                if (L1292_puc_Buffer[AL1291_i_ByteIndex] & AL1289_i_BitMask) {
                                        continue;
                                }
                                L1292_puc_Buffer[AL1291_i_ByteIndex] |= AL1289_i_BitMask;
                        } while ((L1290_T_Thing = F0159_DUNGEON_GetNextThing(L1290_T_Thing)) != C0xFFFE_THING_ENDOFLIST);
                }
                L1293_pc_Address += sizeof(GROUP);
                AL1288_i_Counter++;
        }
        L1293_pc_Address = (char*)G0284_apuc_ThingData[C09_THING_TYPE_CONTAINER];
        AL1288_i_Counter = 0;
        while (AL1288_i_Counter < G0278_ps_DungeonHeader->ThingCount[C09_THING_TYPE_CONTAINER]) {
                if ((((CONTAINER*)L1293_pc_Address)->Next != C0xFFFF_THING_NONE) && ((L1290_T_Thing = ((CONTAINER*)L1293_pc_Address)->Slot) != C0xFFFE_THING_ENDOFLIST)) { /* If container thing data is used and has possessions */
                        L1296_T_PreviousThing = C0xFFFF_THING_NONE;
                        do {
                                AL1289_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                                AL1291_i_ByteIndex = (M012_TYPE(L1290_T_Thing) << 7) + (AL1289_i_ThingIndex >> 3);
                                AL1289_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1289_i_ThingIndex & 0x0007];
                                if (L1292_puc_Buffer[AL1291_i_ByteIndex] & AL1289_i_BitMask) { /* If the thing is a clone (already marked in L1292_puc_Buffer) then remove it from the chest */
                                        if (L1296_T_PreviousThing == C0xFFFF_THING_NONE) {
                                                ((CONTAINER*)L1293_pc_Address)->Slot = C0xFFFE_THING_ENDOFLIST;
                                        } else {
                                                L1297_ps_Generic = (GENERIC*)F0156_DUNGEON_GetThingData(L1296_T_PreviousThing);
                                                L1297_ps_Generic->Next = C0xFFFE_THING_ENDOFLIST;
                                        }
                                        break;
                                }
                                L1292_puc_Buffer[AL1291_i_ByteIndex] |= AL1289_i_BitMask;
                                L1296_T_PreviousThing = L1290_T_Thing;
                        } while ((L1290_T_Thing = F0159_DUNGEON_GetNextThing(L1290_T_Thing)) != C0xFFFE_THING_ENDOFLIST);
                }
                L1293_pc_Address += sizeof(CONTAINER);
                AL1288_i_Counter++;
        }
        L1293_pc_Address = (char*)G0284_apuc_ThingData[C07_THING_TYPE_SCROLL];
        AL1288_i_Counter = 0;
        while (AL1288_i_Counter < G0278_ps_DungeonHeader->ThingCount[C07_THING_TYPE_SCROLL]) {
                if (((SCROLL*)L1293_pc_Address)->Next != C0xFFFF_THING_NONE) {
                        AL1289_i_ThingIndex = ((SCROLL*)L1293_pc_Address)->TextStringThingIndex;
                        AL1291_i_ByteIndex = (C02_THING_TYPE_TEXTSTRING << 7) + (AL1289_i_ThingIndex >> 3);
                        AL1289_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1289_i_ThingIndex & 0x0007];
                        if (L1292_puc_Buffer[AL1291_i_ByteIndex] & AL1289_i_BitMask) {
                                goto T0423022;
                        }
                        L1292_puc_Buffer[AL1291_i_ByteIndex] |= AL1289_i_BitMask;
                }
                T0423022:
                L1293_pc_Address += sizeof(SCROLL);
                AL1288_i_Counter++;
        }
        L1293_pc_Address = (char*)G0284_apuc_ThingData[C14_THING_TYPE_PROJECTILE];
        AL1288_i_Counter = 0;
        while (AL1288_i_Counter < G0278_ps_DungeonHeader->ThingCount[C14_THING_TYPE_PROJECTILE]) {
                if (((PROJECTILE*)L1293_pc_Address)->Next != C0xFFFF_THING_NONE) {
                        if ((L1290_T_Thing = ((PROJECTILE*)L1293_pc_Address)->Slot) == C0xFFFE_THING_ENDOFLIST) {
                                goto T0423028;
                        }
                        if (L1290_T_Thing == C0xFFFF_THING_NONE) {
                                goto T0423028;
                        }
                        if (M012_TYPE(L1290_T_Thing) != C15_THING_TYPE_EXPLOSION) {
                                AL1289_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                                AL1291_i_ByteIndex = (M012_TYPE(L1290_T_Thing) << 7) + (AL1289_i_ThingIndex >> 3);
                                AL1289_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1289_i_ThingIndex & 0x0007];
                                if (L1292_puc_Buffer[AL1291_i_ByteIndex] & AL1289_i_BitMask) {
                                        goto T0423028;
                                }
                                L1292_puc_Buffer[AL1291_i_ByteIndex] |= AL1289_i_BitMask;
                        }
                }
                T0423028:
                L1293_pc_Address += sizeof(PROJECTILE);
                AL1288_i_Counter++;
        }
        for (AL1289_i_ChampionIndex = C00_CHAMPION_FIRST; AL1289_i_ChampionIndex < G0305_ui_PartyChampionCount; AL1289_i_ChampionIndex++) {
                L1293_pc_Address = (char*)M516_CHAMPIONS[AL1289_i_ChampionIndex].Slots;
                for (AL1288_i_Counter = C00_SLOT_READY_HAND; AL1288_i_Counter < C30_SLOT_CHEST_1; AL1288_i_Counter++) {
                        L1290_T_Thing = ((THING*)L1293_pc_Address)[AL1288_i_Counter];
                        if (L1290_T_Thing != C0xFFFF_THING_NONE) {
                                AL1291_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                                L1294_i_ByteOffset = (M012_TYPE(L1290_T_Thing) << 7) + (AL1291_i_ThingIndex >> 3);
                                AL1291_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1291_i_ThingIndex & 0x0007];
                                if (L1292_puc_Buffer[L1294_i_ByteOffset] & AL1291_i_BitMask) { /* If the thing is a clone (already marked in L1292_puc_Buffer) then remove it from the champion slot */
                                        ((THING*)L1293_pc_Address)[AL1288_i_Counter] = C0xFFFF_THING_NONE;
                                        continue;
                                }
                                L1292_puc_Buffer[L1294_i_ByteOffset] |= AL1291_i_BitMask;
                        }
                }
        }
        if ((L1290_T_Thing = G4055_s_LeaderHandObject.Thing) != C0xFFFF_THING_NONE) {
                AL1288_i_ThingIndex = M013_INDEX(L1290_T_Thing);
                AL1289_i_ByteIndex = (M012_TYPE(L1290_T_Thing) << 7) + (AL1288_i_ThingIndex >> 3);
                AL1288_i_BitMask = G0535_auc_BitIndexToMaskConversionTable[AL1288_i_ThingIndex & 0x0007];
                if (L1292_puc_Buffer[AL1289_i_ByteIndex] & AL1288_i_BitMask) { /* If the thing is a clone (already marked in L1292_puc_Buffer) then remove it from the leader hand */
                        G4055_s_LeaderHandObject.Thing = C0xFFFF_THING_NONE;
                } else {
                        L1292_puc_Buffer[AL1289_i_ByteIndex] |= AL1288_i_BitMask;
                }
        }
        F0469_MEMORY_FreeAtHeapTop(2048L);
}
#endif
#endif
