// msc.h
// Generated by Mojon Script Compiler v3.97 20191202 from MT Engine MK1 v5.0+
// Copyleft 2015 The Mojon Twins

#ifdef CLEAR_FLAGS
void msc_init_all (void) {
    for (sc_c = 0; sc_c < MAX_FLAGS; ++ sc_c)
        flags [sc_c] = 0;
}
#endif

unsigned char read_byte (void) {
    #asm
        #ifdef MODE_128K
            di
            ld b, SCRIPT_PAGE
            call SetRAMBank
        #endif

            ld  hl, (_script)
            ld  a, (hl)
            ld  (_safe_byte), a
            inc hl
            ld  (_script), hl

        #ifdef MODE_128K
            ld b, 0
            call SetRAMBank
            ei
        #endif
    #endasm
    return safe_byte;
}

unsigned char read_vbyte (void) {
    #asm
        call _read_byte
        ld  a, l
        and 128
        ret z
        ld  a, l
        and 127
        ld  c, a
        ld  b, 0
        ld  hl, _flags
        add hl, bc
        ld  l, (hl)
        ld  h, 0
        ret
    #endasm
}

void readxy (void) {
    sc_x = read_vbyte ();
    sc_y = read_vbyte ();
}

void stop_player (void) {
    p_vx = p_vy = 0;
}

void reloc_player (void) {
    p_x = read_vbyte () << (4+FIXBITS);
    p_y = read_vbyte () << (4+FIXBITS);
    stop_player ();
}

void read_two_bytes_D_E (void) {
    #asm
            // Read two bytes: flag #, number

            #ifdef MODE_128K
                di
                ld  b, SCRIPT_PAGE
                call SetRAMBank
            #endif

                ld  hl, (_script)
                ld  d, (hl)         // flag #
                inc hl
                ld  e, (hl)         // number
                inc hl
                ld  (_script), hl

            #ifdef MODE_128K
                ld  b, 0
                call SetRAMBank
                ei
            #endif
    #endasm
}
unsigned char *next_script;
void run_script (unsigned char whichs) {

    // main_script_offset contains the address of scripts for current level
    asm_int = main_script_offset + whichs + whichs;
#ifdef DEBUG
    debug_print_16bits (0, 23, asm_int);
#endif

#ifdef MODE_128K
    #asm
        di
        ld b, SCRIPT_PAGE
        call SetRAMBank
    #endasm
#endif

    #asm
        ld hl, (_asm_int)
        ld a, (hl)
        inc hl
        ld h, (hl)
        ld l, a
        ld  (_script), hl
    #endasm

#ifdef MODE_128K
    #asm
        ld b, 0
        call SetRAMBank
        ei
    #endasm
#endif

#ifdef DEBUG
    debug_print_16bits (5, 23, (unsigned int) script);
#endif

    if (script == 0)
        return;

    script += main_script_offset;
#ifdef DEBUG
    debug_print_16bits (10, 23, (unsigned int) script);
#endif


    while ((sc_c = read_byte ()) != 0xFF) {
        next_script = script + sc_c;
        sc_terminado = sc_continuar = 0;
        while (!sc_terminado) {
            switch (read_byte ()) {
                case 0xFF:
                    // THEN
                    // Opcode: FF
                    sc_terminado = 1;
                    sc_continuar = 1;
                    break;
            }
        }
        if (sc_continuar) {
            sc_terminado = 0;
            while (!sc_terminado) {
                switch (read_byte ()) {
                    case 0xFF:
                        sc_terminado = 1;
                        break;
                }
            }
        }
        script = next_script;
    }
}
