#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA678_F31E_F31J_X31J_P31J_SU1E_AU1E_AU2E_AU3E
unsigned long G0349_ul_LastRandomNumber = 0;

#ifdef MEDIA667_P31J
int16_t (*G7125_)();
unsigned int16_t G7126_;
unsigned int16_t G7127_CleanupHandlerCount;
CLEANUP_HANDLER G2252_CleanupHandlers[5];
PROGRAM_STATE* G7128_LastProgramState;
int16_t G7129_ProgramStateOrdinal;
int16_t (*G7130_)(UDSTRUCT44* P3552_ps_);

int16_t G7131_Date;
int16_t G7132_;
char G7133_;
char G7134_;
char G7135_;
unsigned char G7136_;
int16_t G7137_;
int16_t G7138_;
char G7139_;
char G7140_;

unsigned int16_t G7141_SegmentCount;
UDSTRUCTA G4188_;
int16_t G7147_;
int16_t G7148_;
int16_t G7149_;

char G2300_FileNamesList[200][32];
unsigned int16_t G7150_;
FILE_LIST G7151_;
char G2322_Unreferenced[4];
unsigned int16_t G7152_;
int16_t G2332_Unreferenced; /* To be moved to UTMEMORY.C to be included in UDSTRUCTB G4189_; but beware of PC98.H adjustment as this variable's segment will change, reordering will be necessary */
#endif


int16_t F0023_MAIN_GetAbsoluteValue(
int16_t P0028_i_Value
)
{
#ifdef MEDIA773_SU1E_AU1E
        asm {
                        move.w  P0028_i_Value(A6),D0
                        bpl.s   @T0023002_Return         /* Branch if D0 >= 0 */
                        neg.w   D0
                @T0023002_Return:
        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        if (P0028_i_Value < 0) {
                return -P0028_i_Value;
        }
        return P0028_i_Value;
#endif
}

int16_t F0024_MAIN_GetMinimumValue(
int16_t P0029_i_Value1,
int16_t P0030_i_Value2
)
{
#ifdef MEDIA773_SU1E_AU1E
        asm {
                        move.w  P0029_i_Value1(A6),D0
                        cmp.w   P0030_i_Value2(A6),D0
                        blt.s   @T0024002_Return         /* Branch if D0 < P0030_i_Value2 (signed comparison) */
                        move.w  P0030_i_Value2(A6),D0
                @T0024002_Return:
        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        if (P0029_i_Value1 < P0030_i_Value2) {
                return P0029_i_Value1;
        }
        return P0030_i_Value2;
#endif
}

int16_t F0025_MAIN_GetMaximumValue(
int16_t P0031_i_Value1,
int16_t P0032_i_Value2
)
{
#ifdef MEDIA773_SU1E_AU1E
        asm {
                        move.w  P0031_i_Value1(A6),D0
                        cmp.w   P0032_i_Value2(A6),D0
                        bgt.s   @T0025002_Return         /* Branch if D0 > P0032_i_Value2 (signed comparison) */
                        move.w  P0032_i_Value2(A6),D0
                @T0025002_Return:
        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        if (P0031_i_Value1 > P0032_i_Value2) {
                return P0031_i_Value1;
        }
        return P0032_i_Value2;
#endif
}

int16_t F0026_MAIN_GetBoundedValue(
int16_t P0033_i_MinimumAllowedValue,
int16_t P0034_i_Value,
int16_t P0035_i_MaximumAllowedValue
)
{
#ifdef MEDIA773_SU1E_AU1E
        asm {
                        move.w  P0034_i_Value(A6),D0
                        cmp.w   P0033_i_MinimumAllowedValue(A6),D0
                        bgt.s   @T0026001                                /* Branch if D0 > P0033_i_MinimumAllowedValue (signed comparison) */
                        move.w  P0033_i_MinimumAllowedValue(A6),D0
                        bra.s   @T0026003
                @T0026001:
                        cmp.w   P0035_i_MaximumAllowedValue(A6),D0
                        ble.s   @T0026003                                /* Branch if D0 <= P0035_i_MaximumAllowedValue (signed comparison) */
                        move.w  P0035_i_MaximumAllowedValue(A6),D0
                @T0026003:
        }
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        int16_t L3835_i_;


        if (P0034_i_Value > P0033_i_MinimumAllowedValue) {
                if (P0034_i_Value <= P0035_i_MaximumAllowedValue) {
                        L3835_i_ = P0034_i_Value;
                } else {
                        L3835_i_ = P0035_i_MaximumAllowedValue;
                }
        } else {
                L3835_i_ = P0033_i_MinimumAllowedValue;
        }
        return L3835_i_;
#endif
}

unsigned int16_t F0027_MAIN_Get16bitRandomNumber(
)
{
        if (G0349_ul_LastRandomNumber == 0) {
#ifdef MEDIA773_SU1E_AU1E
                G0349_ul_LastRandomNumber = G3601_l_VerticalBlankCount + 1023013;
#endif
#ifdef MEDIA699_X31J_AU2E_AU3E
                G0349_ul_LastRandomNumber = F2093_GetVerticalBlankCount() + 1023013;
#endif
#ifdef MEDIA671_F31E_F31J_P31J
                G0349_ul_LastRandomNumber = G7110_RandomSeed + 1023013;
#endif
        }
        G0349_ul_LastRandomNumber = (314159 * G0349_ul_LastRandomNumber) + 11; /* Pseudorandom number generator */
        return (unsigned int16_t)(G0349_ul_LastRandomNumber >> 8);
}

unsigned int16_t F0028_MAIN_Get1BitRandomNumber(
)
{
        return F0027_MAIN_Get16bitRandomNumber() & 0x0001; /* Pseudorandom number generator */
}

unsigned int16_t F0029_MAIN_Get2BitRandomNumber(
)
{
        return F0027_MAIN_Get16bitRandomNumber() & 0x0003; /* Pseudorandom number generator */
}

#ifdef MEDIA773_SU1E_AU1E
void F1989_InitializeRandomNumber(
)
{
        G0349_ul_LastRandomNumber = Random(); /* BUG0_01 Coding error without consequence. On Atari ST this calls an XBIOS routine which is fine but on Amiga this calls a Macintosh routine that would fail. No consequence because this function is never called on Amiga */
}
#endif

#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
BOOLEAN F7006_IsPointInBox(
#ifdef MEDIA699_X31J_AU2E_AU3E
register int16_t* P3575_pi_Box,
register int16_t P3576_i_X,
register int16_t P3577_i_Y
#endif
#ifdef MEDIA671_F31E_F31J_P31J
REGISTER int16_t* P3575_pi_Box,
int16_t P3576_i_X,
int16_t P3577_i_Y
#endif
)
{
        return ((P3576_i_X <= M769_BOX_RIGHT(P3575_pi_Box)) && (P3576_i_X >= M768_BOX_LEFT(P3575_pi_Box)) && (P3577_i_Y <= M771_BOX_BOTTOM(P3575_pi_Box)) && (P3577_i_Y >= M770_BOX_TOP(P3575_pi_Box)));
}
#endif
#endif
