#include #include #include #include "state.h" #include "script.h" #include "emulator.h" #include "graph.h" script_t script_load(char *path) { script_t sc; sc.seq_len = 0; sc.sequence = NULL; sc.index = 0; sc.last_edge = NULL; FILE *f = fopen(path, "r"); if ( ! f ) { printf("Unable to open scenario file !\n"); exit(1); } char buf[255]; while ( fgets(buf, 254, f) ) { command_t c; if ( strncmp("l", buf, 1) == 0 ) c = GO_LEFT; else if ( strncmp("r", buf, 1) == 0 ) c = GO_RIGHT; else if ( strncmp("u", buf, 1) == 0 ) c = GO_UP; else if ( strncmp("d", buf, 1) == 0 ) c = GO_DOWN; else { printf("Error reading script!\n"); exit(1); } sc.seq_len++; sc.sequence = (command_t*)realloc(sc.sequence, sizeof(command_t)*sc.seq_len); sc.sequence[sc.seq_len-1] = c; } printf("SCRIPT: %d commands loaded.\n", sc.seq_len); fclose(f); return sc; } int script_advance(script_t *sc, game_state_t *gs) { /* If we still are on the same edge, don't do anything */ if ( gs->actors.pacman.edge == sc->last_edge || gs->actors.pacman.percent < 0.5 ) return 1; switch( sc->sequence[sc->index] ) { case GO_LEFT: printf("Script : Left\n"); set_joystick_direction(DIR_LEFT); break; case GO_RIGHT: printf("Script : Right\n"); set_joystick_direction(DIR_RIGHT); break; case GO_UP: printf("Script : Up\n"); set_joystick_direction(DIR_UP); break; case GO_DOWN: printf("Script : Down\n"); set_joystick_direction(DIR_DOWN); break; default: printf("WARN: (Script) Unhandled command\n"); break; } sc->index++; sc->last_edge = gs->actors.pacman.edge; if ( sc->index >= sc->seq_len ) return 1; else return 1; }