replay file saving/loading (WIP)

This commit is contained in:
Andrew "Akari" Alexeyew 2012-07-15 13:16:27 +03:00
parent 4adf4f995b
commit 5112dffd80
4 changed files with 129 additions and 4 deletions

View file

@ -15,6 +15,7 @@
#include "global.h"
#include "stage.h"
#include "paths/native.h"
void quit_menu(void *arg) {
MenuData *m = arg;
@ -47,6 +48,13 @@ troll:
}
void start_replay(void *arg) {
char p[1337];
snprintf(p, 1337, "%s/test.%s", get_config_path(), REPLAY_EXTENSION);
FILE *fp = fopen(p, "rb");
printf("%s\n", p);
replay_read(&global.replay, fp);
fclose(fp);
StageInfo *s = stage_get(global.replay.stage);
if(!s) {
@ -54,6 +62,8 @@ void start_replay(void *arg) {
return;
}
init_player(&global.plr);
// XXX: workaround, doesn't even always work. DEBUG THIS.
global.fps.show_fps = 0;

View file

@ -13,6 +13,7 @@
#include <stdio.h>
#include "global.h"
#include "paths/native.h"
void replay_init(Replay *rpy, StageInfo *stage, int seed, Player *plr) {
memset(rpy, 0, sizeof(Replay));
@ -50,9 +51,6 @@ void replay_event(Replay *rpy, int type, int key) {
printf("[%d] Replay event #%d: %d, %d\n", global.frames, rpy->ecount, type, key);
if(type == EV_OVER)
printf("The replay is OVER\n");
ReplayEvent *e = &(rpy->events[rpy->ecount]);
e->frame = global.frames;
e->type = (char)type;
@ -65,4 +63,112 @@ void replay_event(Replay *rpy, int type, int key) {
rpy->events = (ReplayEvent*)realloc(rpy->events, sizeof(ReplayEvent) * rpy->capacity);
printf("The new capacity is %d\n", rpy->capacity);
}
if(type == EV_OVER) {
printf("The replay is OVER\n");
char p[1337];
snprintf(p, 1337, "%s/test.%s", get_config_path(), REPLAY_EXTENSION);
FILE *fp = fopen(p, "wb");
replay_write(rpy, fp);
fflush(fp);
fclose(fp);
}
}
#define WRITE(type, value, count) if(fwrite(value, sizeof(type), count, file) != (count)) { \
printf("replay_write(): fwrite failed at (%s)(%s)\n", #type, #value); \
return False; \
}
int replay_write(Replay *rpy, FILE* file) {
char *head = "Taisei replay file";
short headlen = strlen(head);
short magic = REPLAY_MAGICNUMBER;
char replays = 1;
// replay header
WRITE(short, &magic, 1)
WRITE(short, &headlen, 1)
WRITE(char, head, headlen)
// amount of replays (stages) stored in this file (may be used later)
WRITE(char, &replays, 1)
// initial game settigs
WRITE(int, &rpy->stage, 1)
WRITE(int, &rpy->seed, 1)
WRITE(int, &rpy->stage, 1)
WRITE(int, &rpy->diff, 1)
WRITE(int, &rpy->points, 1)
// initial player settings
WRITE(Character, &rpy->plr_char, 1)
WRITE(ShotMode, &rpy->plr_shot, 1)
WRITE(complex, &rpy->plr_pos, 1)
WRITE(float, &rpy->plr_power, 1)
WRITE(int, &rpy->plr_lifes, 1)
WRITE(int, &rpy->plr_bombs, 1)
// events
WRITE(int, &rpy->ecount, 1)
WRITE(ReplayEvent, rpy->events, rpy->ecount)
return True;
}
#define READ(type, ptr, count) if(fread(ptr, sizeof(type), count, file) != (count)) { \
printf("replay_read(): fread failed at (%s)(%s)\n", #type, #ptr); \
return False; \
} else printf("replay_read(): (%s)(%s)\n", #type, #ptr);
int replay_read(Replay *rpy, FILE* file) {
char *head;
short headlen;
short magic;
char replays;
printf("replay_read()\n");
// replay header
READ(short, &magic, 1)
if(magic != REPLAY_MAGICNUMBER) {
printf("replay_read(): invalid magic number (got %d, expected %d).\n", magic, REPLAY_MAGICNUMBER);
return False;
}
READ(short, &headlen, 1)
head = (char*)malloc(headlen+1);
READ(char, head, headlen)
printf("replay_read(): %s\n", head);
// amount of replays (stages) stored in this file (may be used later)
READ(char, &replays, 1)
// initial game settigs
READ(int, &rpy->stage, 1)
READ(int, &rpy->seed, 1)
READ(int, &rpy->stage, 1)
READ(int, &rpy->diff, 1)
READ(int, &rpy->points, 1)
// initial player settings
READ(Character, &rpy->plr_char, 1)
READ(ShotMode, &rpy->plr_shot, 1)
READ(complex, &rpy->plr_pos, 1)
READ(float, &rpy->plr_power, 1)
READ(int, &rpy->plr_lifes, 1)
READ(int, &rpy->plr_bombs, 1)
// events
READ(int, &rpy->ecount, 1)
rpy->capacity = rpy->ecount;
rpy->events = (ReplayEvent*)malloc(sizeof(ReplayEvent) * rpy->capacity);
READ(ReplayEvent, rpy->events, rpy->ecount)
return True;
}
#undef WRITE
#undef READ

View file

@ -35,9 +35,9 @@ typedef struct Replay {
ReplayEvent *events;
int ecount;
int capacity;
// The fields below should not be stored
int capacity;
int active;
} Replay;
@ -50,7 +50,12 @@ void replay_init(Replay *rpy, StageInfo *stage, int seed, Player *plr);
void replay_destroy(Replay *rpy);
void replay_event(Replay *rpy, int type, int key);
int replay_write(Replay *rpy, FILE *file);
int replay_read(Replay *rpy, FILE *file);
#define REPLAY_ALLOC_INITIAL 100
#define REPLAY_ALLOC_ADDITIONAL 100
#define REPLAY_MAGICNUMBER 1337
#define REPLAY_EXTENSION "tsr"
#endif

View file

@ -80,6 +80,7 @@ void replay_input() {
ReplayEvent *e = &(global.replay.events[i]);
if(e->frame == global.frames) switch(e->type) {
case EV_OVER:
global.game_over = GAMEOVER_ABORT;
break;
@ -400,10 +401,13 @@ void stage_loop(StageInfo* info, StageRule start, StageRule end, StageRule draw,
if(global.replaymode == REPLAY_RECORD) {
replay_destroy(&global.replay);
replay_init(&global.replay, info, seed, &global.plr);
printf("Random seed: %d\n", seed);
} else {
printf("REPLAY_PLAY mode: %d events\n", global.replay.ecount);
srand(global.replay.seed);
printf("Random seed: %d\n", global.replay.seed);
global.diff = global.replay.diff;
global.points = global.replay.points;