// msc.h
// Generado por Mojon Script Compiler de la Churrera
// Copyleft 2011 The Mojon Twins
 
// Script data & pointers
extern unsigned char mscce_0 [];
extern unsigned char mscce_1 [];
extern unsigned char mscce_2 [];
extern unsigned char mscce_3 [];
extern unsigned char mscce_4 [];
extern unsigned char mscce_5 [];
extern unsigned char mscce_6 [];
extern unsigned char mscce_7 [];
extern unsigned char mscce_8 [];
extern unsigned char mscce_9 [];
extern unsigned char msccf_0 [];
extern unsigned char msccf_1 [];
extern unsigned char msccf_2 [];
extern unsigned char msccf_3 [];
extern unsigned char msccf_4 [];
extern unsigned char msccf_5 [];
 
unsigned char *e_scripts [] = {
    mscce_7, mscce_1, 0, mscce_5, 0, mscce_6, mscce_8, mscce_2, 0, mscce_0, 0, mscce_3, 0, 0, mscce_9, mscce_4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
 
unsigned char *f_scripts [] = {
    0, msccf_1, 0, 0, 0, msccf_4, 0, 0, 0, msccf_0, 0, msccf_2, 0, 0, msccf_5, msccf_3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
 
#asm
._mscce_0

    defb 0x0A, 0x10, 0x00, 0x00, 0xFF, 0x51, 0x00, 0x00, 0x40, 0x40, 0xFF, 0xFF

._mscce_1

    defb 0x09, 0x10, 0x01, 0x01, 0xFF, 0x20, 0x01, 0x09, 0x2D, 0xFF, 0x0A, 0x10, 0x01, 0x00, 0xFF, 0x51
    defb 0x64, 0x46, 0x84, 0x66, 0xFF, 0x09, 0x10, 0x06, 0x01, 0xFF, 0x20, 0x00, 0x03, 0x2D, 0xFF, 0xFF

._mscce_2

    defb 0x09, 0x12, 0x01, 0x00, 0xFF, 0x20, 0x00, 0x04, 0x2D, 0xFF, 0x11, 0x10, 0x03, 0x01, 0xFF, 0x20
    defb 0x06, 0x09, 0x2D, 0x20, 0x07, 0x09, 0x2D, 0x20, 0x08, 0x09, 0x2D, 0xFF, 0xFF

._mscce_3

    defb 0x0D, 0x10, 0x03, 0x01, 0xFF, 0x20, 0x0E, 0x07, 0x2D, 0x20, 0x0E, 0x08, 0x2D, 0xFF, 0x0A, 0x10
    defb 0x02, 0x00, 0xFF, 0x51, 0x4F, 0x2F, 0x7F, 0x5F, 0xFF, 0x0D, 0x10, 0x02, 0x01, 0xFF, 0x20, 0x06
    defb 0x09, 0x2D, 0x20, 0x07, 0x09, 0x2D, 0xFF, 0xFF

._mscce_4

    defb 0x0A, 0x10, 0x03, 0x00, 0xFF, 0x51, 0xAF, 0x3F, 0xC7, 0x5F, 0xFF, 0x0D, 0x10, 0x03, 0x01, 0xFF
    defb 0x20, 0x01, 0x02, 0x24, 0x20, 0x0C, 0x04, 0x1D, 0xFF, 0x0D, 0x10, 0x03, 0x01, 0x10, 0x04, 0x00
    defb 0xFF, 0x51, 0x00, 0x00, 0x3F, 0x2F, 0xFF, 0xFF

._mscce_5

    defb 0x09, 0x10, 0x03, 0x01, 0xFF, 0x20, 0x03, 0x04, 0x2D, 0xFF, 0xFF

._mscce_6

    defb 0x0A, 0x10, 0x06, 0x00, 0xFF, 0x51, 0x10, 0x10, 0x5F, 0x5F, 0xFF, 0xFF

._mscce_7

    defb 0x09, 0x10, 0x04, 0x01, 0xFF, 0x20, 0x04, 0x05, 0x05, 0xFF, 0xFF

._mscce_8

    defb 0x09, 0x10, 0x04, 0x01, 0xFF, 0x20, 0x00, 0x01, 0x05, 0xFF, 0xFF

._mscce_9

    defb 0x18, 0x41, 0x14, 0x10, 0x07, 0x00, 0xFF, 0x20, 0x07, 0x04, 0x05, 0x20, 0x08, 0x04, 0x25, 0x20
    defb 0x0B, 0x04, 0x05, 0x51, 0x70, 0x40, 0x90, 0x50, 0xFF, 0x05, 0x10, 0x07, 0x01, 0xFF, 0xFF, 0xFF

._msccf_0

    defb 0x0B, 0x10, 0x00, 0x00, 0xFF, 0x01, 0x00, 0x01, 0xE4, 0x04, 0x6E, 0xFF, 0xFF

._msccf_1

    defb 0x0F, 0x10, 0x01, 0x00, 0xFF, 0x01, 0x01, 0x01, 0xE4, 0x01, 0x6E, 0x20, 0x01, 0x09, 0x2D, 0xFF
    defb 0xFF

._msccf_2

    defb 0x13, 0x10, 0x02, 0x00, 0xFF, 0x01, 0x02, 0x01, 0xE4, 0x02, 0x6E, 0x20, 0x06, 0x09, 0x2D, 0x20
    defb 0x07, 0x09, 0x2D, 0xFF, 0xFF

._msccf_3

    defb 0x0F, 0x10, 0x03, 0x01, 0x10, 0x04, 0x00, 0x40, 0xFF, 0x01, 0x04, 0x01, 0xE4, 0x06, 0x6E, 0xFF
    defb 0x0B, 0x10, 0x03, 0x00, 0xFF, 0x01, 0x03, 0x01, 0xE4, 0x03, 0x6E, 0xFF, 0xFF

._msccf_4

    defb 0x0B, 0x10, 0x06, 0x00, 0xFF, 0x01, 0x06, 0x01, 0xE4, 0x07, 0x6E, 0xFF, 0xFF

._msccf_5

    defb 0x11, 0x41, 0x14, 0x10, 0x07, 0x00, 0xFF, 0x01, 0x07, 0x01, 0xE4, 0x08, 0x20, 0x08, 0x04, 0x05
    defb 0x6E, 0xFF, 0xFF

#endasm
 
unsigned char *script;
 
void msc_init_all (void) {
    unsigned char i;
    for (i = 0; i < MAX_FLAGS; i ++)
        flags [i] = 0;
}
 
unsigned char read_byte (void) {
    unsigned char c;
    c = script [0];
    script ++;
    return c;
}
 
unsigned char read_vbyte (void) {
    unsigned char c;
    c = read_byte ();
    if (c & 128) return flags [c & 127];
    return c;
}
 
// Ejecutamos el script apuntado por *script:
void run_script (void) {
    unsigned char terminado = 0;
    unsigned char continuar = 0;
    unsigned char x, y, n, m, c;
    unsigned char *next_script;
 
    if (script == 0)
        return; 
 
    script_something_done = 0;
 
    while (1) {
        c = read_byte ();
        if (c == 0xFF) break;
        next_script = script + c;
        terminado = continuar = 0;
        while (!terminado) {
            c = read_byte ();
            switch (c) {
                case 0x10:
                    // IF FLAG x = n
                    // Opcode: 10 x n
                    x = read_vbyte ();
                    n = read_vbyte ();
                    terminado = (flags [x] != n);
                    break;
                case 0x12:
                    // IF FLAG x > n
                    // Opcode: 12 x n
                    x = read_vbyte ();
                    n = read_vbyte ();
                    terminado = (flags [x] <= n);
                    break;
                case 0x40:
                     // IF PLAYER_HAS_OBJECTS
                     // Opcode: 40
                     terminado = (player.objs == 0);
                     break;
                case 0x41:
                     // IF OBJECT_COUNT = n
                     // Opcode: 41 n
                     n = read_vbyte ();
                     terminado = (player.objs != n);
                     break;
                case 0xFF:
                    // THEN
                    // Opcode: FF
                    terminado = 1;
                    continuar = 1;
                    script_something_done = 1;
                    break;
            }
        }
        if (continuar) {
            terminado = 0;
            while (!terminado) {
                c = read_byte ();
                switch (c) {
                    case 0x01:
                        // SET FLAG x = n
                        // Opcode: 01 x n
                        x = read_vbyte ();
                        n = read_vbyte ();
                        flags [x] = n;
                        break;
                    case 0x20:
                        // SET TILE (x, y) = n
                        // Opcode: 20 x y n
                        x = read_vbyte ();
                        y = read_vbyte ();
                        n = read_vbyte ();
                        map_buff [x + (y << 4) - y] = n;
                        map_attr [x + (y << 4) - y] = comportamiento_tiles [n];
                        draw_coloured_tile (VIEWPORT_X + x + x, VIEWPORT_Y + y + y, n);
                        break;
                    case 0x51:
                        // SET_FIRE_ZONE x1, y1, x2, y2
                        // Opcode: 51 x1 y1 x2 y2
                        fzx1 = read_byte ();
                        fzy1 = read_byte ();
                        fzx2 = read_byte ();
                        fzy2 = read_byte ();
                        f_zone_ac = 1;
                        break;
                    case 0x6E:
                        // REDRAW
                        // Opcode: 6E
                        draw_scr_background ();
                        break;
                    case 0xE4:
                        // EXTERN n
                        // Opcode: 0xE4 n
                        n = read_vbyte ();
                        do_extern_action (n);
                        break;
                    case 0xFF:
                        terminado = 1;
                        break;
                }
            }
        }
        script = next_script;
    }
    if (script_result == 3) {
        script_result = 0;
        draw_scr ();
    }
}
