+}
+
+void LoadTape(int level_nr)
+{
+ char *filename = getTapeFilename(level_nr);
+
+ LoadTapeFromFilename(filename);
+}
+
+static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
+{
+ putFileVersion(file, tape->file_version);
+ putFileVersion(file, tape->game_version);
+}
+
+static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
+{
+ int i;
+ byte store_participating_players = 0;
+
+ /* set bits for participating players for compact storage */
+ for(i=0; i<MAX_PLAYERS; i++)
+ if (tape->player_participates[i])
+ store_participating_players |= (1 << i);
+
+ putFile32BitBE(file, tape->random_seed);
+ putFile32BitBE(file, tape->date);
+ putFile32BitBE(file, tape->length);
+
+ putFile8Bit(file, store_participating_players);
+
+ /* unused bytes not at the end here for 4-byte alignment of engine_version */
+ WriteUnusedBytesToFile(file, TAPE_HEADER_UNUSED);
+
+ putFileVersion(file, tape->engine_version);
+}
+
+static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
+{
+ int level_identifier_size = strlen(tape->level_identifier) + 1;
+ int i;
+
+ putFile16BitBE(file, level_identifier_size);
+
+ for(i=0; i < level_identifier_size; i++)
+ putFile8Bit(file, tape->level_identifier[i]);
+
+ putFile16BitBE(file, tape->level_nr);
+}
+
+static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
+{
+ int i, j;
+
+ for(i=0; i<tape->length; i++)
+ {
+ for(j=0; j<MAX_PLAYERS; j++)
+ if (tape->player_participates[j])
+ putFile8Bit(file, tape->pos[i].action[j]);
+
+ putFile8Bit(file, tape->pos[i].delay);
+ }
+}
+
+void SaveTape(int level_nr)
+{
+ char *filename = getTapeFilename(level_nr);
+ FILE *file;
+ boolean new_tape = TRUE;
+ int num_participating_players = 0;
+ int info_chunk_size;
+ int body_chunk_size;
+ int i;
+
+ InitTapeDirectory(leveldir_current->filename);
+
+ /* if a tape still exists, ask to overwrite it */
+ if (access(filename, F_OK) == 0)
+ {
+ new_tape = FALSE;
+ if (!Request("Replace old tape ?", REQ_ASK))
+ return;
+ }
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Error(ERR_WARN, "cannot save level recording file '%s'", filename);
+ return;
+ }
+
+ tape.file_version = FILE_VERSION_ACTUAL;
+ tape.game_version = GAME_VERSION_ACTUAL;
+
+ /* count number of participating players */
+ for(i=0; i<MAX_PLAYERS; i++)
+ if (tape.player_participates[i])
+ num_participating_players++;
+
+ info_chunk_size = 2 + (strlen(tape.level_identifier) + 1) + 2;
+ body_chunk_size = (num_participating_players + 1) * tape.length;
+
+ putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
+ putFileChunkBE(file, "TAPE", CHUNK_SIZE_NONE);
+
+ putFileChunkBE(file, "VERS", FILE_VERS_CHUNK_SIZE);
+ SaveTape_VERS(file, &tape);
+
+ putFileChunkBE(file, "HEAD", TAPE_HEADER_SIZE);
+ SaveTape_HEAD(file, &tape);
+
+ putFileChunkBE(file, "INFO", info_chunk_size);
+ SaveTape_INFO(file, &tape);
+
+ putFileChunkBE(file, "BODY", body_chunk_size);
+ SaveTape_BODY(file, &tape);
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+
+ tape.changed = FALSE;
+
+ if (new_tape)
+ Request("tape saved !", REQ_CONFIRM);
+}
+
+void DumpTape(struct TapeInfo *tape)
+{
+ int i, j;
+
+ if (TAPE_IS_EMPTY(*tape))
+ {
+ Error(ERR_WARN, "no tape available for level %d", tape->level_nr);
+ return;
+ }
+
+ printf_line("-", 79);
+ printf("Tape of Level %03d (file version %08d, game version %08d)\n",
+ tape->level_nr, tape->file_version, tape->game_version);
+ printf("Level series identifier: '%s'\n", tape->level_identifier);
+ printf_line("-", 79);
+
+ for(i=0; i<tape->length; i++)
+ {
+ if (i >= MAX_TAPELEN)
+ break;
+
+ printf("%03d: ", i);
+
+ for(j=0; j<MAX_PLAYERS; j++)
+ {
+ if (tape->player_participates[j])
+ {
+ int action = tape->pos[i].action[j];
+
+ printf("%d:%02x ", j, action);
+ printf("[%c%c%c%c|%c%c] - ",
+ (action & JOY_LEFT ? '<' : ' '),
+ (action & JOY_RIGHT ? '>' : ' '),
+ (action & JOY_UP ? '^' : ' '),
+ (action & JOY_DOWN ? 'v' : ' '),
+ (action & JOY_BUTTON_1 ? '1' : ' '),
+ (action & JOY_BUTTON_2 ? '2' : ' '));
+ }
+ }
+
+ printf("(%03d)\n", tape->pos[i].delay);
+ }
+
+ printf_line("-", 79);
+}
+
+
+/* ========================================================================= */
+/* score file functions */
+/* ========================================================================= */
+
+void LoadScore(int level_nr)
+{
+ int i;
+ char *filename = getScoreFilename(level_nr);
+ char cookie[MAX_LINE_LEN];
+ char line[MAX_LINE_LEN];
+ char *line_ptr;
+ FILE *file;
+
+ /* always start with reliable default values */
+ for(i=0; i<MAX_SCORE_ENTRIES; i++)
+ {
+ strcpy(highscore[i].Name, EMPTY_PLAYER_NAME);
+ highscore[i].Score = 0;
+ }
+
+ if (!(file = fopen(filename, MODE_READ)))
+ return;
+
+ /* check file identifier */
+ fgets(cookie, MAX_LINE_LEN, file);
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, SCORE_COOKIE))
+ {
+ Error(ERR_WARN, "unknown format of score file '%s'", filename);
+ fclose(file);
+ return;
+ }
+
+ for(i=0; i<MAX_SCORE_ENTRIES; i++)
+ {
+ fscanf(file, "%d", &highscore[i].Score);
+ fgets(line, MAX_LINE_LEN, file);
+
+ if (line[strlen(line) - 1] == '\n')
+ line[strlen(line) - 1] = '\0';
+
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
+ {
+ strncpy(highscore[i].Name, line_ptr, MAX_PLAYER_NAME_LEN);
+ highscore[i].Name[MAX_PLAYER_NAME_LEN] = '\0';
+ break;
+ }
+ }
+ }
+
+ fclose(file);
+}
+
+void SaveScore(int level_nr)
+{
+ int i;
+ char *filename = getScoreFilename(level_nr);
+ FILE *file;
+
+ InitScoreDirectory(leveldir_current->filename);
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Error(ERR_WARN, "cannot save score for level %d", level_nr);
+ return;
+ }
+
+ fprintf(file, "%s\n\n", SCORE_COOKIE);
+
+ for(i=0; i<MAX_SCORE_ENTRIES; i++)
+ fprintf(file, "%d %s\n", highscore[i].Score, highscore[i].Name);
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PUBLIC);
+}