#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA774_SU1E_AU1E_AU2E
/*
Segment #2
F1882_PrintTextString                  F1970_Debug_Unreferenced          same segment            both sets in same segment?
F1617_VDI_vsin_mode     F1607_CallVDI   same segment
*/
extern unsigned int16_t F0090_strlen();
extern long F1886_Memory();
extern void F1889_Screen_CPSX();
extern long F1955_Input_CPSX();
extern void F1956_EndProgram();
extern long F1965_();
extern void F1966_DebugEnter();
extern void F1967_DebugExit();

unsigned char* G3613_puc_Bitmap_Font;

#ifdef MEDIA772_SU1E
unsigned char* G3612_; /* to HINTSCR.C */
unsigned char* G3611_puc_Bitmap_Oracle; /* to HINTSCR.C */
HINT_GRAPHICS G3615_s_HintGraphics; /* to HINTSCR.C */
long G3601_l_VerticalBlankCount; /* to HINTSCR.C */
int16_t G3724_ai_XYZ[4] = { 0, 320, 0, 164 }; /* to HINTSCR.C */
long G3631_; /* to HINTSCR.C */
unsigned int16_t G3609_aui_GraphicIntegers[100]; /* 50 ints loaded from graphic #0 in HCSB.DAT */ /* to HINTSCR.C */
unsigned int16_t G3682_aui_Palette_BeforeHintOracle[16]; /* to HINTPAL.C */
unsigned int16_t G3617_aui_Palette[16]; /* to HINTPAL.C */
int16_t G3603_aui_Palette_HintOracle[16]; /* to HINTPAL.C */
int16_t G3874_[16]; /* to HINTPAL.C */
unsigned char G3657_auc_PaletteChanges_Font[16] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150 }; /* to HINTPAL.C */
unsigned char G3655_auc_PaletteChanges[16] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 90, 130, 140, 150 }; /* to HINTPAL.C */

char* G3683_pc_NextFTLModuleToRun = "A:\\FTLCODE"; /* to HINT002.C */
int16_t G3622_;
char* G3618_apuc_Screens[2]; /* to HINTSCR.C */
/*char* G3619_;*/
int16_t G3620_i_ScreenIndex; /* to HINTSCR.C */
BOOLEAN G3624_ = C1_TRUE; /* to HINTSCR.C */
int16_t G3721_ai_ColorComponentMasks[3] = { 0x700, 0x070, 0x007 }; /* to HINT003.C */
int16_t G3722_ai_ColorComponentIncrements[3] = { 0x100, 0x010, 0x001 }; /* to HINT003.C */
/* strings, border between two .C source files */
#endif

#ifdef MEDIA763_AU1E_AU2E
unsigned char* G3612_; /* to HINTSCR.C */
unsigned char* G3611_puc_Bitmap_Oracle; /* to HINTSCR.C */
long G3852_;
long G3601_l_VerticalBlankCount; /* to HINTSCR.C */
unsigned int16_t G3609_aui_GraphicIntegers[100]; /* 50 ints loaded from graphic #0 in HCSB.DAT */ /* to HINTSCR.C */
int16_t G3622_;
char G3864_[8]; /* Most probably G3618_apuc_Screens that is unreferenced in AU1E and AU2E */
int16_t G3620_i_ScreenIndex; /* to HINTSCR.C */
BOOLEAN G3624_ = C0_FALSE;
unsigned char G3657_auc_PaletteChanges_Font[16] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 15 };
unsigned char G3655_auc_PaletteChanges[16] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 90, 130, 140, 150 };
char G3665_ac_TextLine[80];
int16_t G3658_ = -1;
int16_t G3659_i_CharacterIndex;
int16_t G3663_i_SpaceCharacterIndex;
int16_t G3660_i_CharacterCount;
int16_t G3661_i_Width;
int16_t G3662_i_HintTextLineY;
int16_t G3666_i_HintTextLineX;
char G3664_;
#endif



void F1882_PrintTextString(int16_t P5679_, int16_t P5680_, register unsigned char* P5681_puc_String) {
        int16_t L5545_ai_XYZ[4];
#ifdef MEDIA773_SU1E_AU1E
        char L5546_c_CharacterIndex;
#endif
#ifdef MEDIA767_AU2E
        unsigned char L5546_c_CharacterIndex;
#endif
        int16_t L5547_i_X;
        int16_t L5548_i_Y;


        L5545_ai_XYZ[0] = P5679_;
        L5545_ai_XYZ[3] = P5680_;
        L5545_ai_XYZ[2] = P5680_ - 8;
        while (*P5681_puc_String) {
#ifdef MEDIA773_SU1E_AU1E
                L5546_c_CharacterIndex = *P5681_puc_String++ - ' ';
#endif
#ifdef MEDIA767_AU2E
                L5546_c_CharacterIndex = *P5681_puc_String++;
#endif
                if (L5546_c_CharacterIndex) {
                        L5545_ai_XYZ[1] = L5545_ai_XYZ[0] + 7;
                        L5547_i_X = (L5546_c_CharacterIndex & 0x001F) << 3;
                        L5548_i_Y = (L5546_c_CharacterIndex >> 5) * 9;
#ifdef MEDIA772_SU1E
                        F0132_VIDEO_Blit(G3613_puc_Bitmap_Font, G0348_Bitmap_Screen, L5545_ai_XYZ, L5547_i_X, L5548_i_Y, C128_BYTE_WIDTH, C160_BYTE_WIDTH_SCREEN, C12_COLOR_DARKEST_GRAY);
#endif
#ifdef MEDIA763_AU1E_AU2E
                        F0132_VIDEO_Blit(G3613_puc_Bitmap_Font, G0348_Bitmap_Screen, L5545_ai_XYZ, L5547_i_X, L5548_i_Y, C256_HINT_FONT_WIDTH, 320, C12_COLOR_DARKEST_GRAY, M739_HINT_FONT_HEIGHT, 200);
#endif
                }
                L5545_ai_XYZ[0] += 9;
        }
}

int16_t F1883_(STRUCT22B* P5682_, int16_t P5683_, int16_t P5684_) {
        return (P5684_ < P5683_) ? 9 : (P5684_ - P5683_ + 1) * 9;
}

STRUCT22B* F1884_Text(int16_t P5685_, int16_t P5686_, ...) {
        STRUCT22B* L5549_;
        int16_t L5550_;


#ifdef MEDIA773_SU1E_AU1E
        F1966_DebugEnter(9, P5686_);
#endif
        switch (P5686_) {
                case C07_TO_LOWERCASE:
                        for (L5550_ = 0; L5550_ < (int16_t)F0090_strlen(*((char**)(&P5686_ + 1))); L5550_++) {
#ifdef MEDIA772_SU1E
                                (*((char**)(&P5686_ + 1)))[L5550_] = F1984_ConvertCharacterToLowerCase((*((char**)(&P5686_ + 1)))[L5550_]);
#endif
#ifdef MEDIA763_AU1E_AU2E
                                (*((char**)(&P5686_ + 1)))[L5550_] = F2014_ConvertStringToLowerCase((*((char**)(&P5686_ + 1)))[L5550_]);
#endif
                        }
                        break;
                case C16_ALLOCATE_AND_INITIALIZE:
                        L5549_ = F1884_Text(P5685_, C14_ALLOCATE);
                        if (*((long*)(&P5686_ + 1))) {
#ifdef MEDIA772_SU1E
                                L5549_->_pc_0x02_String = (char*)F1886_Memory(P5685_, C14_ALLOCATE, (*((long*)(&P5686_ + 1))));
#endif
#ifdef MEDIA763_AU1E_AU2E
                                L5549_->_pc_0x02_String = (char*)F1886_Memory(-1, C14_ALLOCATE, (*((long*)(&P5686_ + 1))));
#endif
                        }
                        L5549_->_ai_XYZ_0x06[0] = G3621_ai_XYZ_Screen[0];
                        L5549_->_ai_XYZ_0x06[1] = G3621_ai_XYZ_Screen[1];
                        L5549_->_ai_XYZ_0x06[2] = G3621_ai_XYZ_Screen[2];
                        L5549_->_ai_XYZ_0x06[3] = G3621_ai_XYZ_Screen[3];
                        L5549_->_i_0x12 = 0;
                        L5549_->_i_0x14 = 0;
                        L5549_->_i_0x0E = 1;
                        break;
                case C14_ALLOCATE:
#ifdef MEDIA772_SU1E
                        L5549_ = (STRUCT22B*)F1886_Memory(P5685_, C14_ALLOCATE, (long)sizeof(STRUCT22B));
#endif
#ifdef MEDIA763_AU1E_AU2E
                        L5549_ = (STRUCT22B*)F1886_Memory(-1, C14_ALLOCATE, (long)sizeof(STRUCT22B));
#endif
                        L5549_->_i_0x00 = 3;
                        break;
                case C20_FREE:
                        if ((*((STRUCT22B**)(&P5686_ + 1)))->_i_0x00  != 3) {
                                F1956_EndProgram(P5685_, 18, 2L);
                        }
#ifdef MEDIA772_SU1E
                        F1886_Memory(P5685_, C20_FREE, (*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String);
                        F1886_Memory(P5685_, C20_FREE, (*((STRUCT22B**)(&P5686_ + 1))));
#endif
#ifdef MEDIA763_AU1E_AU2E
                        F1886_Memory(-1, C20_FREE, (*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String);
                        F1886_Memory(-1, C20_FREE, (*((STRUCT22B**)(&P5686_ + 1))));
#endif
                        break;
                case C26_SET_FONT_COLOR:
                        if (G3658_ == -1) {
                                G3657_auc_PaletteChanges_Font[*((int16_t*)(&P5686_ + 2))] = (*((char*)((char*)&P5686_ + 9))) * 10;
                                G3658_ = *((int16_t*)(&P5686_ + 4));
                        } else {
                                G3657_auc_PaletteChanges_Font[G3658_] = *((char*)((char*)&P5686_ + 5)) * 10;
                                G3658_ = *((int16_t*)(&P5686_ + 2));
                        }
                        F0129_VIDEO_BlitShrinkWithPaletteChanges(G3613_puc_Bitmap_Font, G3613_puc_Bitmap_Font, C256_HINT_FONT_WIDTH, M739_HINT_FONT_HEIGHT, C256_HINT_FONT_WIDTH, M739_HINT_FONT_HEIGHT, G3657_auc_PaletteChanges_Font);
                        break;
                case C06_PRINT:
                        if ((*((STRUCT22B**)(&P5686_ + 1)))->_i_0x00  != 3) {
                                F1956_EndProgram(P5685_, 18, 1L);
                        }
                        G3659_i_CharacterIndex = 0;
                        G3660_i_CharacterCount = F0090_strlen((*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String);
                        G3661_i_Width = (*((STRUCT22B**)(&P5686_ + 1)))->_ai_XYZ_0x06[1] - (*((STRUCT22B**)(&P5686_ + 1)))->_ai_XYZ_0x06[0];
                        G3662_i_HintTextLineY = (*((STRUCT22B**)(&P5686_ + 1)))->_ai_XYZ_0x06[2] + 11;
                        while (G3659_i_CharacterIndex < G3660_i_CharacterCount) {
                                do {
                                        if ((*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String[G3659_i_CharacterIndex] != ' ') {
                                                break;
                                        }
                                        G3659_i_CharacterIndex++;
                                } while (G3659_i_CharacterIndex < G3660_i_CharacterCount);
                                G3663_i_SpaceCharacterIndex = G3659_i_CharacterIndex;
                                for (;;) {
                                        if (G3659_i_CharacterIndex >= G3660_i_CharacterCount) {
                                                G3664_ = ' ';
                                        } else {
                                                G3664_ = (*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String[G3659_i_CharacterIndex];
                                        }
                                        G3665_ac_TextLine[G3659_i_CharacterIndex - G3663_i_SpaceCharacterIndex] = G3664_;
                                        if (G3664_ == ' ') {
                                                if (G3661_i_Width < F1883_(*((STRUCT22B**)(&P5686_ + 1)), G3663_i_SpaceCharacterIndex, G3659_i_CharacterIndex - 1)) {
                                                        G3659_i_CharacterIndex--;
                                                        while (G3659_i_CharacterIndex > G3663_i_SpaceCharacterIndex) {
                                                                if (G3665_ac_TextLine[G3659_i_CharacterIndex - G3663_i_SpaceCharacterIndex] == ' ') {
                                                                        break;
                                                                }
                                                                G3659_i_CharacterIndex--;
                                                        }
                                                        if (G3659_i_CharacterIndex != G3663_i_SpaceCharacterIndex) {
                                                                break;
                                                        }
                                                        do {
                                                                G3659_i_CharacterIndex++;
                                                        } while (G3661_i_Width > F1883_(*((STRUCT22B**)(&P5686_ + 1)), G3663_i_SpaceCharacterIndex, G3659_i_CharacterIndex));
                                                        G3659_i_CharacterIndex--;
                                                        break;
                                                } else {
                                                        if (G3659_i_CharacterIndex >= G3660_i_CharacterCount) {
                                                                break;
                                                        }
                                                        G3659_i_CharacterIndex++;
                                                        continue;
                                                }
                                        }
                                        if (G3664_ == '/') {
                                                G3665_ac_TextLine[G3659_i_CharacterIndex - G3663_i_SpaceCharacterIndex] = '\0';
                                                break;
                                        }
                                        G3659_i_CharacterIndex++;
                                }
                                G3665_ac_TextLine[G3659_i_CharacterIndex - G3663_i_SpaceCharacterIndex] = '\0';
                                G3666_i_HintTextLineX = (*((STRUCT22B**)(&P5686_ + 1)))->_ai_XYZ_0x06[0];
                                if ((*((STRUCT22B**)(&P5686_ + 1)))->_i_0x0E & 1) {
                                        G3666_i_HintTextLineX += (G3661_i_Width - F1883_(*((STRUCT22B**)(&P5686_ + 1)), G3663_i_SpaceCharacterIndex, G3659_i_CharacterIndex - 1)) / 2;
                                }
                                if (G3662_i_HintTextLineY <= (*((STRUCT22B**)(&P5686_ + 1)))->_ai_XYZ_0x06[3]) {
#ifdef MEDIA773_SU1E_AU1E
                                        for (L5550_ = 0; G3665_ac_TextLine[L5550_]; L5550_++) { /* Replace non printable characters by underscore characters */
                                                if ((G3665_ac_TextLine[L5550_] < ' ') || (G3665_ac_TextLine[L5550_] > 0x7F)) {
                                                        G3665_ac_TextLine[L5550_] = '_';
                                                }
                                        }
#endif
#ifdef MEDIA767_AU2E
                                        F2027_(&G3665_ac_TextLine, 80L);
#endif
                                        F1882_PrintTextString(G3666_i_HintTextLineX, G3662_i_HintTextLineY, &G3665_ac_TextLine);
                                }
                                G3662_i_HintTextLineY += 11;
                                if ((*((STRUCT22B**)(&P5686_ + 1)))->_pc_0x02_String[G3659_i_CharacterIndex] == '/') {
                                        G3659_i_CharacterIndex++;
                                }
                        }
                        break;
                default:
                        F1956_EndProgram(P5685_, 18, 3L);
        }
#ifdef MEDIA773_SU1E_AU1E
        F1967_DebugExit(9, P5686_);
#endif
        return L5549_;
}

void F1885_(int16_t P5689_, int16_t P5690_, STRUCT22B* P5691_) {
        long L5732_l_Unreferenced;
        int16_t L5552_;


#ifdef MEDIA773_SU1E_AU1E
        F1966_DebugEnter(21, P5690_);
#endif
        switch (P5690_) {
                case 6:
                        F1965_(9, 27, 34L, P5691_, 6L);
                        F1889_Screen_CPSX(9, 31);
                        break;
                case 8:
                        F1955_Input_CPSX(-1, C02_UNINITIALIZE);
                        F1889_Screen_CPSX(P5689_, 35);
                        F1955_Input_CPSX(P5689_, 3, 6L, 0L);
                        F1885_(P5689_, 6, P5691_);
                        for (;;) {
                                L5552_ = F1955_Input_CPSX(P5689_, 5, 0L);
                                if (F1955_Input_CPSX(P5689_, 8, (long)L5552_) == 6) {
                                        break;
                                }
                        }
                        break;
                default:
                        F1956_EndProgram(-1, 18, 4L);
                        break;
        }
#ifdef MEDIA773_SU1E_AU1E
        F1967_DebugExit(21, P5690_);
#endif
}
#endif
