+#if 0
+static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ fputc(EL_YAMYAM, file);
+ fputc(level->num_yam_contents, file);
+ fputc(0, file);
+ fputc(0, file);
+
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ if (level->encoding_16bit_field)
+ putFile16BitBE(file, level->yam_content[i][x][y]);
+ else
+ fputc(level->yam_content[i][x][y], file);
+}
+#endif
+
+static void SaveLevel_CNT2(FILE *file, struct LevelInfo *level, int element)
+{
+ int i, x, y;
+ int num_contents, content_xsize, content_ysize;
+ int content_array[MAX_ELEMENT_CONTENTS][3][3];
+
+ if (element == EL_YAMYAM)
+ {
+ num_contents = level->num_yam_contents;
+ content_xsize = 3;
+ content_ysize = 3;
+
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ content_array[i][x][y] = level->yam_content[i][x][y];
+ }
+ else if (element == EL_BD_AMOEBA)
+ {
+ num_contents = 1;
+ content_xsize = 1;
+ content_ysize = 1;
+
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ content_array[i][x][y] = EL_EMPTY;
+ content_array[0][0][0] = level->amoeba_content;
+ }
+ else
+ {
+ /* chunk header already written -- write empty chunk data */
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_SIZE);
+
+ Error(ERR_WARN, "cannot save content for element '%d'", element);
+ return;
+ }
+
+ putFile16BitBE(file, element);
+ fputc(num_contents, file);
+ fputc(content_xsize, file);
+ fputc(content_ysize, file);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT2_UNUSED);
+
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ putFile16BitBE(file, content_array[i][x][y]);
+}
+
+static void SaveLevel_CUS1(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, check = 0;
+
+ putFile16BitBE(file, num_changed_custom_elements);
+
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (Properties[element][EP_BITFIELD_BASE] != EP_BITMASK_DEFAULT)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile32BitBE(file, Properties[element][EP_BITFIELD_BASE]);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) /* should not happen */
+ Error(ERR_WARN, "inconsistent number of custom element properties");
+}
+
+static void SaveLevel_CUS2(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, check = 0;
+
+ putFile16BitBE(file, num_changed_custom_elements);
+
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (level->custom_element_successor[i] != EL_EMPTY_SPACE)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile16BitBE(file, level->custom_element_successor[i]);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) /* should not happen */
+ Error(ERR_WARN, "inconsistent number of custom element successors");
+}
+
+void SaveLevel(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+ int body_chunk_size;
+ int num_changed_custom_elements1 = 0;
+ int num_changed_custom_elements2 = 0;
+ int i, x, y;
+ FILE *file;
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Error(ERR_WARN, "cannot save level file '%s'", filename);
+ return;
+ }
+
+ level.file_version = FILE_VERSION_ACTUAL;
+ level.game_version = GAME_VERSION_ACTUAL;
+
+ /* check level field for 16-bit elements */
+ level.encoding_16bit_field = FALSE;
+ for(y=0; y<level.fieldy; y++)
+ for(x=0; x<level.fieldx; x++)
+ if (Ur[x][y] > 255)
+ level.encoding_16bit_field = TRUE;
+
+ /* check yamyam content for 16-bit elements */
+ level.encoding_16bit_yamyam = FALSE;
+ for(i=0; i<level.num_yam_contents; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ if (level.yam_content[i][x][y] > 255)
+ level.encoding_16bit_yamyam = TRUE;
+
+ /* check amoeba content for 16-bit elements */
+ level.encoding_16bit_amoeba = FALSE;
+ if (level.amoeba_content > 255)
+ level.encoding_16bit_amoeba = TRUE;
+
+ /* calculate size of "BODY" chunk */
+ body_chunk_size =
+ level.fieldx * level.fieldy * (level.encoding_16bit_field ? 2 : 1);
+
+ /* check for non-standard custom elements and calculate "CUS1" chunk size */
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ if (Properties[EL_CUSTOM_START +i][EP_BITFIELD_BASE] != EP_BITMASK_DEFAULT)
+ num_changed_custom_elements1++;
+
+ /* check for non-standard custom elements and calculate "CUS2" chunk size */
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ if (level.custom_element_successor[i] != EL_EMPTY_SPACE)
+ num_changed_custom_elements2++;
+
+ putFileChunkBE(file, "RND1", CHUNK_SIZE_UNDEFINED);
+ putFileChunkBE(file, "CAVE", CHUNK_SIZE_NONE);
+
+ putFileChunkBE(file, "VERS", FILE_VERS_CHUNK_SIZE);
+ SaveLevel_VERS(file, &level);
+
+ putFileChunkBE(file, "HEAD", LEVEL_HEADER_SIZE);
+ SaveLevel_HEAD(file, &level);
+
+ putFileChunkBE(file, "AUTH", MAX_LEVEL_AUTHOR_LEN);
+ SaveLevel_AUTH(file, &level);
+
+ putFileChunkBE(file, "BODY", body_chunk_size);
+ SaveLevel_BODY(file, &level);
+
+ if (level.encoding_16bit_yamyam ||
+ level.num_yam_contents != STD_ELEMENT_CONTENTS)
+ {
+ putFileChunkBE(file, "CNT2", LEVEL_CHUNK_CNT2_SIZE);
+ SaveLevel_CNT2(file, &level, EL_YAMYAM);
+ }
+
+ if (level.encoding_16bit_amoeba)
+ {
+ putFileChunkBE(file, "CNT2", LEVEL_CHUNK_CNT2_SIZE);
+ SaveLevel_CNT2(file, &level, EL_BD_AMOEBA);
+ }
+
+ if (num_changed_custom_elements1 > 0)
+ {
+ putFileChunkBE(file, "CUS1", 2 + num_changed_custom_elements1 * 6);
+ SaveLevel_CUS1(file, &level, num_changed_custom_elements1);
+ }
+
+ if (num_changed_custom_elements2 > 0)
+ {
+ putFileChunkBE(file, "CUS2", 2 + num_changed_custom_elements2 * 4);
+ SaveLevel_CUS2(file, &level, num_changed_custom_elements2);
+ }
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+
+void DumpLevel(struct LevelInfo *level)
+{
+ printf_line("-", 79);
+ printf("Level xxx (file version %06d, game version %06d)\n",
+ level->file_version, level->game_version);
+ printf_line("-", 79);
+
+ printf("Level Author: '%s'\n", level->author);
+ printf("Level Title: '%s'\n", level->name);
+ printf("\n");
+ printf("Playfield Size: %d x %d\n", level->fieldx, level->fieldy);
+ printf("\n");
+ printf("Level Time: %d seconds\n", level->time);
+ printf("Gems needed: %d\n", level->gems_needed);
+ printf("\n");
+ printf("Time for Magic Wall: %d seconds\n", level->time_magic_wall);
+ printf("Time for Wheel: %d seconds\n", level->time_wheel);
+ printf("Time for Light: %d seconds\n", level->time_light);
+ printf("Time for Timegate: %d seconds\n", level->time_timegate);
+ printf("\n");
+ printf("Amoeba Speed: %d\n", level->amoeba_speed);
+ printf("\n");
+ printf("Gravity: %s\n", (level->gravity ? "yes" : "no"));
+ printf("Double Speed Movement: %s\n", (level->double_speed ? "yes" : "no"));
+ printf("EM style slippery gems: %s\n", (level->em_slippery_gems ? "yes" : "no"));
+
+ printf_line("-", 79);
+}
+
+
+/* ========================================================================= */
+/* tape file functions */
+/* ========================================================================= */
+
+static void setTapeInfoToDefaults()
+{
+ int i;
+
+ /* always start with reliable default values (empty tape) */
+ TapeErase();
+
+ /* default values (also for pre-1.2 tapes) with only the first player */
+ tape.player_participates[0] = TRUE;
+ for(i=1; i<MAX_PLAYERS; i++)
+ tape.player_participates[i] = FALSE;
+
+ /* at least one (default: the first) player participates in every tape */
+ tape.num_participating_players = 1;
+
+ tape.level_nr = level_nr;
+ tape.counter = 0;
+ tape.changed = FALSE;
+
+ tape.recording = FALSE;
+ tape.playing = FALSE;
+ tape.pausing = FALSE;
+}
+
+static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape)
+{
+ tape->file_version = getFileVersion(file);
+ tape->game_version = getFileVersion(file);
+
+ return chunk_size;
+}
+
+static int LoadTape_HEAD(FILE *file, int chunk_size, struct TapeInfo *tape)
+{
+ int i;
+
+ tape->random_seed = getFile32BitBE(file);
+ tape->date = getFile32BitBE(file);
+ tape->length = getFile32BitBE(file);
+
+ /* read header fields that are new since version 1.2 */
+ if (tape->file_version >= FILE_VERSION_1_2)
+ {
+ byte store_participating_players = fgetc(file);
+ int engine_version;
+
+ /* 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++;
+ }
+ }
+
+ ReadUnusedBytesFromFile(file, TAPE_HEADER_UNUSED);
+
+ engine_version = getFileVersion(file);
+ if (engine_version > 0)
+ tape->engine_version = engine_version;
+ }
+
+ return chunk_size;
+}
+
+static int LoadTape_INFO(FILE *file, int chunk_size, struct TapeInfo *tape)
+{
+ int level_identifier_size;
+ int i;
+
+ level_identifier_size = getFile16BitBE(file);
+
+ tape->level_identifier =
+ checked_realloc(tape->level_identifier, level_identifier_size);
+
+ for(i=0; i < level_identifier_size; i++)
+ tape->level_identifier[i] = fgetc(file);
+
+ tape->level_nr = getFile16BitBE(file);
+
+ chunk_size = 2 + level_identifier_size + 2;
+
+ return chunk_size;
+}
+
+static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape)
+{
+ 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;
+ }
+
+ 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);
+ }
+
+ tape->pos[i].delay = fgetc(file);
+
+ 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)
+ {
+ /* convert pre-2.0 tapes to new tape format */
+
+ 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 LoadTapeFromFilename(char *filename)
+{
+ 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;
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (strcmp(chunk_name, "RND1") == 0)
+ {
+ getFile32BitBE(file); /* not used */
+
+ getFileChunkBE(file, chunk_name, NULL);
+ if (strcmp(chunk_name, "TAPE") != 0)
+ {
+ Error(ERR_WARN, "unknown format of tape file '%s'", filename);
+ fclose(file);
+ return;
+ }
+ }
+ else /* check for pre-2.0 file format with cookie string */
+ {
+ strcpy(cookie, chunk_name);
+ fgets(&cookie[4], MAX_LINE_LEN - 4, file);
+ if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
+ cookie[strlen(cookie) - 1] = '\0';
+
+ if (!checkCookieString(cookie, TAPE_COOKIE_TMPL))
+ {
+ 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;
+ }
+
+ /* pre-2.0 tape files have no game version, so use file version here */
+ 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(file, TAPE_HEADER_SIZE, &tape);
+ LoadTape_BODY(file, 2 * tape.length, &tape);
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(FILE *, int, struct TapeInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", FILE_VERS_CHUNK_SIZE, LoadTape_VERS },
+ { "HEAD", TAPE_HEADER_SIZE, LoadTape_HEAD },
+ { "INFO", -1, LoadTape_INFO },
+ { "BODY", -1, LoadTape_BODY },
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ int i = 0;