+static int LoadTape_HEAD(struct TapeInfo *tape, FILE *file, int chunk_size)
+{
+ int i;
+
+ tape->random_seed = getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
+ tape->date = getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
+ tape->length = getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
+
+ /* read header fields that are new since version 1.2 */
+ if (tape->file_version >= FILE_VERSION_1_2)
+ {
+ byte store_participating_players = fgetc(file);
+
+ ReadUnusedBytesFromFile(file, TAPE_HEADER_UNUSED);
+
+ /* since version 1.2, tapes store which players participate in the tape */
+ tape->num_participating_players = 0;
+ for(i=0; i<MAX_PLAYERS; i++)
+ {
+ tape->player_participates[i] = FALSE;
+
+ if (store_participating_players & (1 << i))
+ {
+ tape->player_participates[i] = TRUE;
+ tape->num_participating_players++;
+ }
+ }
+ }
+
+ tape->level_nr = level_nr;
+ tape->counter = 0;
+ tape->changed = FALSE;
+
+ tape->recording = FALSE;
+ tape->playing = FALSE;
+ tape->pausing = FALSE;
+
+ return chunk_size;
+}
+
+static int LoadTape_BODY(struct TapeInfo *tape, FILE *file, int chunk_size)
+{
+ int i, j;
+ int chunk_size_expected =
+ (tape->num_participating_players + 1) * tape->length;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size);
+ return chunk_size_expected;
+ }
+
+#if DEBUG
+ printf("\nTAPE OF LEVEL %d\n", level_nr);
+#endif
+
+ for(i=0; i<tape->length; i++)
+ {
+ if (i >= MAX_TAPELEN)
+ break;
+
+ for(j=0; j<MAX_PLAYERS; j++)
+ {
+ tape->pos[i].action[j] = MV_NO_MOVING;
+
+ if (tape->player_participates[j])
+ tape->pos[i].action[j] = fgetc(file);
+
+#if DEBUG
+ {
+ int x = tape->pos[i].action[j];
+
+ printf("%d:%02x ", j, x);
+ printf("[%c%c%c%c|%c%c] - ",
+ (x & JOY_LEFT ? '<' : ' '),
+ (x & JOY_RIGHT ? '>' : ' '),
+ (x & JOY_UP ? '^' : ' '),
+ (x & JOY_DOWN ? 'v' : ' '),
+ (x & JOY_BUTTON_1 ? '1' : ' '),
+ (x & JOY_BUTTON_2 ? '2' : ' '));
+ }
+#endif
+
+ }
+
+ tape->pos[i].delay = fgetc(file);
+
+#if DEBUG
+ printf("[%03d]\n", tape->pos[i].delay);
+#endif
+
+ if (tape->file_version == FILE_VERSION_1_0)
+ {
+ /* eliminate possible diagonal moves in old tapes */
+ /* this is only for backward compatibility */
+
+ byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
+ byte action = tape->pos[i].action[0];
+ int k, num_moves = 0;
+
+ for (k=0; k<4; k++)
+ {
+ if (action & joy_dir[k])
+ {
+ tape->pos[i + num_moves].action[0] = joy_dir[k];
+ if (num_moves > 0)
+ tape->pos[i + num_moves].delay = 0;
+ num_moves++;
+ }
+ }
+
+ if (num_moves > 1)
+ {
+ num_moves--;
+ i += num_moves;
+ tape->length += num_moves;
+ }
+ }
+ else if (tape->file_version < FILE_VERSION_2_0)
+ {
+ if (tape->pos[i].delay > 1)
+ {
+ /* action part */
+ tape->pos[i + 1] = tape->pos[i];
+ tape->pos[i + 1].delay = 1;
+
+ /* delay part */
+ for(j=0; j<MAX_PLAYERS; j++)
+ tape->pos[i].action[j] = MV_NO_MOVING;
+ tape->pos[i].delay--;
+
+ i++;
+ tape->length++;
+ }
+ }
+
+ if (feof(file))
+ break;
+ }
+
+ if (i != tape->length)
+ chunk_size = (tape->num_participating_players + 1) * i;
+
+ return chunk_size;
+}
+
+void LoadTape(int level_nr)
+{
+ char *filename = getTapeFilename(level_nr);
+ char cookie[MAX_LINE_LEN];
+ char chunk_name[CHUNK_ID_LEN + 1];
+ FILE *file;
+ int chunk_size;
+
+ /* always start with reliable default values */
+ setTapeInfoToDefaults();
+
+ 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, TAPE_COOKIE)) /* unknown file format */
+ {
+ Error(ERR_WARN, "unknown format of tape file '%s'", filename);
+ fclose(file);
+ return;
+ }
+
+ if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1)
+ {
+ Error(ERR_WARN, "unsupported version of tape file '%s'", filename);
+ fclose(file);
+ return;
+ }
+
+ tape.game_version = tape.file_version;
+
+ if (tape.file_version < FILE_VERSION_1_2)
+ {
+ /* tape files from versions before 1.2.0 without chunk structure */
+ LoadTape_HEAD(&tape, file, TAPE_HEADER_SIZE);
+ LoadTape_BODY(&tape, file, 2 * tape.length);
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(struct TapeInfo *, FILE *, int);
+ }
+ chunk_info[] =
+ {
+ { "HEAD", TAPE_HEADER_SIZE, LoadTape_HEAD },
+ { "BODY", -1, LoadTape_BODY },
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_BIG_ENDIAN))
+ {
+ int i = 0;
+
+ while (chunk_info[i].name != NULL &&
+ strcmp(chunk_name, chunk_info[i].name) != 0)
+ i++;
+
+ if (chunk_info[i].name == NULL)
+ {
+ Error(ERR_WARN, "unknown chunk '%s' in tape file '%s'",
+ chunk_name, filename);
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else if (chunk_info[i].size != -1 &&
+ chunk_info[i].size != chunk_size)
+ {
+ Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ /* call function to load this tape chunk */
+ int chunk_size_expected =
+ (chunk_info[i].loader)(&tape, file, chunk_size);
+
+ /* the size of some chunks cannot be checked before reading other
+ chunks first (like "HEAD" and "BODY") that contain some header
+ information, so check them here */
+ if (chunk_size_expected != chunk_size)
+ {
+ Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+ }
+ }
+ }
+ }
+
+ fclose(file);
+
+ tape.length_seconds = GetTapeLength();
+}
+
+void OLD_SaveTape(int level_nr)