2012-07-14 16:37:52 +02:00
|
|
|
/*
|
2019-08-03 19:43:48 +02:00
|
|
|
* This software is licensed under the terms of the MIT License.
|
2017-02-11 04:52:08 +01:00
|
|
|
* See COPYING for further information.
|
2012-07-14 16:37:52 +02:00
|
|
|
* ---
|
2019-01-23 21:10:43 +01:00
|
|
|
* 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>.
|
2012-07-14 16:37:52 +02:00
|
|
|
*/
|
|
|
|
|
2019-01-23 21:10:43 +01:00
|
|
|
#ifndef IGUARD_replay_h
|
|
|
|
#define IGUARD_replay_h
|
|
|
|
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
2012-07-14 19:46:03 +02:00
|
|
|
|
|
|
|
#include "stage.h"
|
|
|
|
#include "player.h"
|
2017-09-26 21:50:22 +02:00
|
|
|
#include "version.h"
|
2019-05-01 06:03:22 +02:00
|
|
|
#include "util/systime.h"
|
2020-04-05 04:51:00 +02:00
|
|
|
#include "dynarray.h"
|
2012-07-14 19:46:03 +02:00
|
|
|
|
2017-02-09 05:06:46 +01:00
|
|
|
|
|
|
|
/*
|
2018-01-12 19:26:07 +01:00
|
|
|
* 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.
|
2017-02-09 05:06:46 +01:00
|
|
|
*
|
2018-01-12 19:26:07 +01:00
|
|
|
* Please maintain this convention, it makes it easier to grasp the replay file structure just by looking at this header.
|
2017-02-09 05:06:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-09-26 21:50:22 +02:00
|
|
|
// 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.
|
2017-02-09 05:06:46 +01:00
|
|
|
|
2017-09-26 21:50:22 +02:00
|
|
|
/* BEGIN supported struct versions */
|
|
|
|
// Taisei v1.1 legacy format
|
|
|
|
#define REPLAY_STRUCT_VERSION_TS101000 5
|
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
// Taisei v1.2 revision 0: adds game version information
|
2017-09-26 21:50:22 +02:00
|
|
|
#define REPLAY_STRUCT_VERSION_TS102000_REV0 6
|
2017-10-29 23:45:24 +01:00
|
|
|
|
|
|
|
// Taisei v1.2 revision 1: adds flags, stageflags and continues; reduces playername size to 255 bytes
|
|
|
|
#define REPLAY_STRUCT_VERSION_TS102000_REV1 7
|
2018-01-09 02:28:04 +01:00
|
|
|
|
|
|
|
// Taisei v1.2 revision 2: adds graze points
|
|
|
|
#define REPLAY_STRUCT_VERSION_TS102000_REV2 8
|
2019-02-22 00:56:03 +01:00
|
|
|
|
|
|
|
// 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
|
2019-03-09 17:19:42 +01:00
|
|
|
|
|
|
|
// Taisei v1.3 revision 2: RNG changed; seed separated from start time; time expanded to 64bit
|
|
|
|
#define REPLAY_STRUCT_VERSION_TS103000_REV2 11
|
2019-04-11 23:42:47 +02:00
|
|
|
|
|
|
|
// Taisei v1.3 revision 3: add final score at the end of each stage
|
|
|
|
#define REPLAY_STRUCT_VERSION_TS103000_REV3 12
|
2017-09-26 21:50:22 +02:00
|
|
|
/* END supported struct versions */
|
2017-02-09 05:06:46 +01:00
|
|
|
|
2017-02-22 21:52:25 +01:00
|
|
|
#define REPLAY_VERSION_COMPRESSION_BIT 0x8000
|
|
|
|
#define REPLAY_COMPRESSION_CHUNK_SIZE 4096
|
2017-09-26 21:50:22 +02:00
|
|
|
|
|
|
|
// What struct version to use when saving recorded replays
|
2019-04-11 23:42:47 +02:00
|
|
|
#define REPLAY_STRUCT_VERSION_WRITE (REPLAY_STRUCT_VERSION_TS103000_REV3 | REPLAY_VERSION_COMPRESSION_BIT)
|
2017-02-22 21:52:25 +01:00
|
|
|
|
2017-02-09 01:13:06 +01:00
|
|
|
#define REPLAY_ALLOC_INITIAL 256
|
2017-02-09 05:06:46 +01:00
|
|
|
|
|
|
|
#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"
|
2017-02-09 01:13:06 +01:00
|
|
|
#define REPLAY_USELESS_BYTE 0x69
|
2017-02-05 02:25:17 +01:00
|
|
|
|
2017-09-19 20:46:28 +02:00
|
|
|
#define REPLAY_WRITE_DESYNC_CHECKS
|
|
|
|
|
2017-02-05 03:58:27 +01:00
|
|
|
#ifdef DEBUG
|
2017-11-23 16:27:41 +01:00
|
|
|
// #define REPLAY_LOAD_DEBUG
|
2017-02-05 03:58:27 +01:00
|
|
|
#endif
|
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
typedef struct ReplayEvent {
|
2017-02-09 05:06:46 +01:00
|
|
|
/* BEGIN stored fields */
|
|
|
|
|
2017-02-05 02:25:17 +01:00
|
|
|
uint32_t frame;
|
|
|
|
uint8_t type;
|
2017-02-05 03:04:31 +01:00
|
|
|
uint16_t value;
|
2017-02-09 05:06:46 +01:00
|
|
|
|
|
|
|
/* END stored fields */
|
2012-07-14 19:46:03 +02:00
|
|
|
} ReplayEvent;
|
|
|
|
|
2012-08-07 05:28:41 +02:00
|
|
|
typedef struct ReplayStage {
|
2017-02-09 05:06:46 +01:00
|
|
|
/* BEGIN stored fields */
|
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
/* 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 */
|
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
// initial game settings
|
2017-09-27 10:50:56 +02:00
|
|
|
uint16_t stage; // must match type of StageInfo.id in stage.h
|
2019-03-09 17:19:42 +01:00
|
|
|
/* 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;
|
2017-02-11 04:52:08 +01:00
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
// initial player settings
|
2019-02-22 00:56:03 +01:00
|
|
|
uint64_t plr_points; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV0: uint32_t
|
2017-10-29 23:45:24 +01:00
|
|
|
/* 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;
|
2017-02-08 20:59:43 +01:00
|
|
|
uint16_t plr_pos_x;
|
|
|
|
uint16_t plr_pos_y;
|
2017-02-05 02:25:17 +01:00
|
|
|
uint8_t plr_focus;
|
2017-02-08 20:59:43 +01:00
|
|
|
uint16_t plr_power;
|
2017-03-21 11:09:32 +01:00
|
|
|
uint8_t plr_lives;
|
2019-02-22 00:56:03 +01:00
|
|
|
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;
|
2019-02-22 00:56:03 +01:00
|
|
|
uint16_t plr_bomb_fragments; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV1: uint8_t
|
2017-03-06 15:24:57 +01:00
|
|
|
uint8_t plr_inputflags;
|
2018-01-09 02:28:04 +01:00
|
|
|
/* BEGIN REPLAY_STRUCT_VERSION_TS102000_REV2 and above */
|
2019-02-22 00:56:03 +01:00
|
|
|
uint32_t plr_graze; // NOTE: before REPLAY_STRUCT_VERSION_TS103000_REV0: uint16_t
|
2018-01-09 02:28:04 +01:00
|
|
|
/* END REPLAY_STRUCT_VERSION_TS102000_REV2 and above */
|
2019-02-22 00:56:03 +01:00
|
|
|
/* BEGIN REPLAY_STRUCT_VERSION_TS103000_REV0 and above */
|
|
|
|
uint32_t plr_point_item_value;
|
|
|
|
/* END REPLAY_STRUCT_VERSION_TS103000_REV0 and above */
|
2017-02-09 01:55:01 +01:00
|
|
|
|
2019-04-11 23:42:47 +02:00
|
|
|
/* 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 */
|
|
|
|
|
2017-02-09 05:06:46 +01:00
|
|
|
// player input
|
2020-04-05 04:51:00 +02:00
|
|
|
// 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;
|
2017-02-09 01:55:01 +01:00
|
|
|
|
2017-02-09 05:06:46 +01:00
|
|
|
// checksum of all of the above -- 2's complement of value returned by replay_calc_stageinfo_checksum()
|
|
|
|
// uint32_t checksum;
|
|
|
|
|
|
|
|
/* END stored fields */
|
|
|
|
|
2018-05-28 10:10:41 +02:00
|
|
|
SystemTime init_time;
|
2020-04-05 04:51:00 +02:00
|
|
|
DYNAMIC_ARRAY(ReplayEvent) events;
|
2017-02-10 00:24:19 +01:00
|
|
|
|
2017-03-21 03:28:35 +01:00
|
|
|
// used during playback
|
2020-04-05 04:51:00 +02:00
|
|
|
// TODO separate this from the actual replay data storage
|
2012-07-29 15:54:49 +02:00
|
|
|
int playpos;
|
2017-03-21 03:28:35 +01:00
|
|
|
int fps;
|
2017-02-10 00:24:19 +01:00
|
|
|
uint16_t desync_check;
|
2017-09-19 20:46:28 +02:00
|
|
|
bool desynced;
|
2012-08-07 05:28:41 +02:00
|
|
|
} ReplayStage;
|
|
|
|
|
|
|
|
typedef struct Replay {
|
2017-02-09 05:06:46 +01:00
|
|
|
/* 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;
|
|
|
|
|
2017-09-26 21:50:22 +02:00
|
|
|
/* 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;
|
2017-02-09 05:06:46 +01:00
|
|
|
|
|
|
|
// 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
|
2017-10-29 23:45:24 +01:00
|
|
|
// REPLAY_STRUCT_VERSION_TS102000_REV1 and above:
|
2018-01-12 19:26:07 +01:00
|
|
|
// uint8_t playername_size;
|
2017-10-29 23:45:24 +01:00
|
|
|
// REPLAY_STRUCT_VERSION_TS102000_REV0 and below:
|
2018-01-12 19:26:07 +01:00
|
|
|
// uint16_t playername_size;
|
2017-02-09 05:06:46 +01:00
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
// The player UTF8-encoded player name
|
2012-08-07 05:28:41 +02:00
|
|
|
char *playername;
|
2017-10-29 23:45:24 +01:00
|
|
|
|
|
|
|
/* 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
|
2017-02-09 05:06:46 +01:00
|
|
|
uint16_t numstages;
|
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
// Array, contains {numstages} elements when not NULL
|
2017-02-10 00:24:19 +01:00
|
|
|
ReplayStage *stages;
|
2017-02-09 05:06:46 +01:00
|
|
|
|
|
|
|
// 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 */
|
2012-07-14 19:46:03 +02:00
|
|
|
} Replay;
|
|
|
|
|
2017-02-09 05:06:46 +01:00
|
|
|
typedef enum {
|
2012-07-14 19:46:03 +02:00
|
|
|
REPLAY_RECORD,
|
|
|
|
REPLAY_PLAY
|
2017-02-09 05:06:46 +01:00
|
|
|
} ReplayMode;
|
|
|
|
|
|
|
|
typedef enum ReplayReadMode {
|
|
|
|
// bitflags
|
|
|
|
REPLAY_READ_META = 1,
|
|
|
|
REPLAY_READ_EVENTS = 2,
|
|
|
|
REPLAY_READ_ALL = 3, // includes the other two
|
|
|
|
} ReplayReadMode;
|
2012-07-14 19:46:03 +02:00
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
typedef enum ReplayGlobalFlags {
|
|
|
|
_REPLAY_GFLAG_NULL,
|
2018-01-12 19:26:07 +01:00
|
|
|
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
|
2017-10-29 23:45:24 +01:00
|
|
|
} ReplayGlobalFlags;
|
|
|
|
|
|
|
|
typedef enum ReplayStageFlags {
|
|
|
|
_REPLAY_SFLAG_NULL,
|
2018-01-12 19:26:07 +01:00
|
|
|
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
|
2017-10-29 23:45:24 +01:00
|
|
|
} ReplayStageFlags;
|
|
|
|
|
2012-08-07 05:28:41 +02:00
|
|
|
void replay_init(Replay *rpy);
|
2019-03-09 17:19:42 +01:00
|
|
|
ReplayStage* replay_create_stage(Replay *rpy, StageInfo *stage, uint64_t start_time, uint64_t seed, Difficulty diff, Player *plr);
|
2017-02-10 00:24:19 +01:00
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
void replay_destroy(Replay *rpy);
|
2017-02-09 07:01:38 +01:00
|
|
|
void replay_destroy_events(Replay *rpy);
|
2017-02-10 00:24:19 +01:00
|
|
|
|
2017-09-19 20:46:28 +02:00
|
|
|
void replay_stage_event(ReplayStage *stg, uint32_t frame, uint8_t type, uint16_t value);
|
2017-02-10 00:24:19 +01:00
|
|
|
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);
|
2012-07-14 19:46:03 +02:00
|
|
|
|
2017-09-26 21:50:22 +02:00
|
|
|
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);
|
2017-02-10 00:24:19 +01:00
|
|
|
|
2017-02-11 04:52:08 +01:00
|
|
|
void replay_copy(Replay *dst, Replay *src, bool steal_events);
|
2012-07-16 17:47:06 +02:00
|
|
|
|
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);
|
2019-01-23 21:10:43 +01:00
|
|
|
|
|
|
|
#endif // IGUARD_replay_h
|