taisei/src/replay.h

260 lines
8.5 KiB
C
Raw Normal View History

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
2019-07-03 20:00:56 +02:00
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#ifndef IGUARD_replay_h
#define IGUARD_replay_h
#include "taisei.h"
#include "stage.h"
#include "player.h"
#include "version.h"
#include "util/systime.h"
#include "dynarray.h"
/*
* All stored fields in the Replay* structures are in the order in which they appear in the file.
* If a field is commented out, that means it *IS* stored in the file, just not used outside of the loading routine.
*
* Please maintain this convention, it makes it easier to grasp the replay file structure just by looking at this header.
*/
// The struct version is a numeric designation given to the replay file format.
// Always bump it when making incompatible changes to the layout.
// If dropping support for a version, comment out its #define and remove all related code.
/* BEGIN supported struct versions */
// Taisei v1.1 legacy format
#define REPLAY_STRUCT_VERSION_TS101000 5
// Taisei v1.2 revision 0: adds game version information
#define REPLAY_STRUCT_VERSION_TS102000_REV0 6
// Taisei v1.2 revision 1: adds flags, stageflags and continues; reduces playername size to 255 bytes
#define REPLAY_STRUCT_VERSION_TS102000_REV1 7
// Taisei v1.2 revision 2: adds graze points
#define REPLAY_STRUCT_VERSION_TS102000_REV2 8
// Taisei v1.3 revision 0: adds piv; expands points to 64bit, graze to 32bit
#define REPLAY_STRUCT_VERSION_TS103000_REV0 9
// Taisei v1.3 revision 1: expands life and bomb fragments to 16bit
#define REPLAY_STRUCT_VERSION_TS103000_REV1 10
// Taisei v1.3 revision 2: RNG changed; seed separated from start time; time expanded to 64bit
#define REPLAY_STRUCT_VERSION_TS103000_REV2 11
// Taisei v1.3 revision 3: add final score at the end of each stage
#define REPLAY_STRUCT_VERSION_TS103000_REV3 12
/* END supported struct versions */
2017-02-22 21:52:25 +01:00
#define REPLAY_VERSION_COMPRESSION_BIT 0x8000
#define REPLAY_COMPRESSION_CHUNK_SIZE 4096
// What struct version to use when saving recorded replays
#define REPLAY_STRUCT_VERSION_WRITE (REPLAY_STRUCT_VERSION_TS103000_REV3 | REPLAY_VERSION_COMPRESSION_BIT)
2017-02-22 21:52:25 +01:00
#define REPLAY_ALLOC_INITIAL 256
#define REPLAY_MAGIC_HEADER { 0x68, 0x6f, 0x6e, 0x6f, 0xe2, 0x9d, 0xa4, 0x75, 0x6d, 0x69 }
2017-02-05 02:25:17 +01:00
#define REPLAY_EXTENSION "tsr"
#define REPLAY_USELESS_BYTE 0x69
2017-02-05 02:25:17 +01:00
#define REPLAY_WRITE_DESYNC_CHECKS
2017-02-05 03:58:27 +01:00
#ifdef DEBUG
// #define REPLAY_LOAD_DEBUG
2017-02-05 03:58:27 +01:00
#endif
typedef struct ReplayEvent {
/* BEGIN stored fields */
2017-02-05 02:25:17 +01:00
uint32_t frame;
uint8_t type;
uint16_t value;
/* END stored fields */
} ReplayEvent;
2012-08-07 05:28:41 +02:00
typedef struct ReplayStage {
/* BEGIN stored fields */
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
// Bitfield, various parameters that apply to this specific stage
uint32_t flags;
/* END REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
// initial game settings
2017-09-27 10:50:56 +02:00
uint16_t stage; // must match type of StageInfo.id in stage.h
/* BEGIN REPLAY_STRUCT_VERSION_TS103000_REV2 and above */
uint64_t start_time;
/* END REPLAY_STRUCT_VERSION_TS103000_REV2 and above */
uint64_t rng_seed; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV2: uint32_t, also specifies start_time
2017-02-05 02:25:17 +01:00
uint8_t diff;
// initial player settings
uint64_t plr_points; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV0: uint32_t
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
uint8_t plr_continues_used;
/* END REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
2017-02-05 02:25:17 +01:00
uint8_t plr_char;
uint8_t plr_shot;
uint16_t plr_pos_x;
uint16_t plr_pos_y;
2017-02-05 02:25:17 +01:00
uint8_t plr_focus;
uint16_t plr_power;
uint8_t plr_lives;
uint16_t plr_life_fragments; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV1: uint8_t
2017-02-05 02:25:17 +01:00
uint8_t plr_bombs;
uint16_t plr_bomb_fragments; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV1: uint8_t
uint8_t plr_inputflags;
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV2 and above */
uint32_t plr_graze; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV0: uint16_t
/* END REPLAY_STRUCT_VERSION_TS102000_REV2 and above */
/* BEGIN REPLAY_STRUCT_VERSION_TS103000_REV0 and above */
uint32_t plr_point_item_value;
/* END REPLAY_STRUCT_VERSION_TS103000_REV0 and above */
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV3 and above */
// player score at the end of the stage
uint64_t plr_points_final;
/* END REPLAY_STRUCT_VERSION_TS102000_REV3 and above */
// player input
// NOTE: only used to read the number of events from file.
// Actual numbers of events stored/allocated are tracked by the .events dynamic array.
uint16_t num_events;
// checksum of all of the above -- 2's complement of value returned by replay_calc_stageinfo_checksum()
// uint32_t checksum;
/* END stored fields */
SystemTime init_time;
DYNAMIC_ARRAY(ReplayEvent) events;
// used during playback
// TODO separate this from the actual replay data storage
2012-07-29 15:54:49 +02:00
int playpos;
int fps;
uint16_t desync_check;
bool desynced;
2012-08-07 05:28:41 +02:00
} ReplayStage;
typedef struct Replay {
/* BEGIN stored fields */
// each byte must be equal to the corresponding byte in replay_magic_header.
// uint8_t[sizeof(replay_magic_header)];
// must be equal to REPLAY_STRUCT_VERSION
2017-02-22 21:52:25 +01:00
uint16_t version;
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV0 and above */
// Game version this replay was recorded on
TaiseiVersion game_version;
/* END REPLAY_STRUCT_VERSION_TS102000_REV0 and above */
2017-02-22 21:52:25 +01:00
// Where the events begin
// NOTE: this is not present in uncompressed replays!
uint32_t fileoffset;
// How many bytes is {playername} long. The string is not null terminated in the file, but is null terminated after it's been loaded into this struct
// REPLAY_STRUCT_VERSION_TS102000_REV1 and above:
// uint8_t playername_size;
// REPLAY_STRUCT_VERSION_TS102000_REV0 and below:
// uint16_t playername_size;
// The player UTF8-encoded player name
2012-08-07 05:28:41 +02:00
char *playername;
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
// Bitfield, various parameters that apply to the whole replay
uint32_t flags;
/* END REPLAY_STRUCT_VERSION_TS102000_REV1 and above */
// Number of stages in this replay
uint16_t numstages;
// Array, contains {numstages} elements when not NULL
ReplayStage *stages;
// ALL input events from ALL of the stages
// This is actually loaded into separate sub-arrays for every stage, see ReplayStage.events
//
// All input events are stored at the very end of the replay so that we can save some time and memory
// by only loading them when necessary without seeking around the file too much.
//
// ReplayStage input_events[];
// at least one trailing byte, value doesn't matter
// uint8_t useless;
/* END stored fields */
} Replay;
typedef enum {
REPLAY_RECORD,
REPLAY_PLAY
} ReplayMode;
typedef enum ReplayReadMode {
// bitflags
REPLAY_READ_META = 1,
REPLAY_READ_EVENTS = 2,
REPLAY_READ_ALL = 3, // includes the other two
} ReplayReadMode;
typedef enum ReplayGlobalFlags {
_REPLAY_GFLAG_NULL,
REPLAY_GFLAG_CONTINUES = (1 << 0), // a continue was used in any stage
REPLAY_GFLAG_CHEATS = (1 << 1), // a cheat was used in any stage
REPLAY_GFLAG_CLEAR = (1 << 2), // all stages in the replay were cleared
} ReplayGlobalFlags;
typedef enum ReplayStageFlags {
_REPLAY_SFLAG_NULL,
REPLAY_SFLAG_CONTINUES = (1 << 0), // a continue was used in this stage
REPLAY_SFLAG_CHEATS = (1 << 1), // a cheat was used in this stage
REPLAY_SFLAG_CLEAR = (1 << 2), // this stage was cleared
} ReplayStageFlags;
2012-08-07 05:28:41 +02:00
void replay_init(Replay *rpy);
ReplayStage* replay_create_stage(Replay *rpy, StageInfo *stage, uint64_t start_time, uint64_t seed, Difficulty diff, Player *plr);
void replay_destroy(Replay *rpy);
void replay_destroy_events(Replay *rpy);
void replay_stage_event(ReplayStage *stg, uint32_t frame, uint8_t type, uint16_t value);
void replay_stage_check_desync(ReplayStage *stg, int time, uint16_t check, ReplayMode mode);
2017-02-15 18:34:47 +01:00
void replay_stage_sync_player_state(ReplayStage *stg, Player *plr);
bool replay_write(Replay *rpy, SDL_RWops *file, uint16_t version);
2017-09-26 01:59:33 +02:00
bool replay_read(Replay *rpy, SDL_RWops *file, ReplayReadMode mode, const char *source);
2012-07-15 12:16:27 +02:00
2017-04-18 21:48:18 +02:00
bool replay_save(Replay *rpy, const char *name);
bool replay_load(Replay *rpy, const char *name, ReplayReadMode mode);
bool replay_load_syspath(Replay *rpy, const char *path, ReplayReadMode mode);
void replay_copy(Replay *dst, Replay *src, bool steal_events);
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
void replay_play(Replay *rpy, int firstidx, CallChain next);
2017-04-04 11:10:54 +02:00
int replay_find_stage_idx(Replay *rpy, uint8_t stageid);
#endif // IGUARD_replay_h