+ /* map some elements which have changed in newer versions */
+ if (level.game_version <= VERSION_IDENT(2,2,0))
+ {
+ int x, y;
+
+ /* map game font elements */
+ for(y=0; y<level.fieldy; y++)
+ {
+ for(x=0; x<level.fieldx; x++)
+ {
+ int element = Ur[x][y];
+
+ if (element == EL_CHAR('['))
+ element = EL_CHAR_AUMLAUT;
+ else if (element == EL_CHAR('\\'))
+ element = EL_CHAR_OUMLAUT;
+ else if (element == EL_CHAR(']'))
+ element = EL_CHAR_UUMLAUT;
+ else if (element == EL_CHAR('^'))
+ element = EL_CHAR_COPYRIGHT;
+
+ Feld[x][y] = Ur[x][y] = element;
+ }
+ }
+ }
+
+ /* determine border element for this level */
+ SetBorderElement();
+}
+
+void LoadLevel(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ LoadLevelFromFilename(filename);
+ InitElementPropertiesEngine(level.game_version);
+}
+
+static void SaveLevel_VERS(FILE *file, struct LevelInfo *level)
+{
+ putFileVersion(file, level->file_version);
+ putFileVersion(file, level->game_version);
+}
+
+static void SaveLevel_HEAD(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ fputc(level->fieldx, file);
+ fputc(level->fieldy, file);
+
+ putFile16BitBE(file, level->time);
+ putFile16BitBE(file, level->gems_needed);
+
+ for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
+ fputc(level->name[i], file);
+
+ for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
+ fputc(level->score[i], file);
+
+ for(i=0; i<STD_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ fputc((level->encoding_16bit_yamyam ? EL_EMPTY :
+ level->yamyam_content[i][x][y]),
+ file);
+ fputc(level->amoeba_speed, file);
+ fputc(level->time_magic_wall, file);
+ fputc(level->time_wheel, file);
+ fputc((level->encoding_16bit_amoeba ? EL_EMPTY : level->amoeba_content),
+ file);
+ fputc((level->double_speed ? 1 : 0), file);
+ fputc((level->gravity ? 1 : 0), file);
+ fputc((level->encoding_16bit_field ? 1 : 0), file);
+ fputc((level->em_slippery_gems ? 1 : 0), file);
+
+ fputc((level->use_custom_template ? 1 : 0), file);
+
+ WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
+}
+
+static void SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
+{
+ int i;
+
+ for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
+ fputc(level->author[i], file);
+}
+
+static void SaveLevel_BODY(FILE *file, struct LevelInfo *level)
+{
+ int x, y;
+
+ for(y=0; y<level->fieldy; y++)
+ for(x=0; x<level->fieldx; x++)
+ if (level->encoding_16bit_field)
+ putFile16BitBE(file, Ur[x][y]);
+ else
+ fputc(Ur[x][y], file);
+}
+
+#if 0
+static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ fputc(EL_YAMYAM, file);
+ fputc(level->num_yamyam_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->yamyam_content[i][x][y]);
+ else
+ fputc(level->yamyam_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_yamyam_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->yamyam_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]);
+}
+
+#if 0
+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");
+}
+#endif
+
+#if 0
+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 (element_info[element].change.target_element != EL_EMPTY_SPACE)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile16BitBE(file, element_info[element].change.target_element);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) /* should not happen */
+ Error(ERR_WARN, "inconsistent number of custom target elements");
+}
+#endif
+
+static void SaveLevel_CUS3(FILE *file, struct LevelInfo *level,
+ int num_changed_custom_elements)
+{
+ int i, x, y, 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]);
+
+ /* some free bytes for future properties and padding */
+ WriteUnusedBytesToFile(file, 7);
+
+ putFile8Bit(file, element_info[element].use_gfx_element);
+ putFile16BitBE(file, element_info[element].gfx_element);
+
+ putFile8Bit(file, element_info[element].score);
+ putFile8Bit(file, element_info[element].gem_count);
+
+ putFile16BitBE(file, element_info[element].push_delay_fixed);
+ putFile16BitBE(file, element_info[element].push_delay_random);
+ putFile16BitBE(file, element_info[element].move_delay_fixed);
+ putFile16BitBE(file, element_info[element].move_delay_random);
+
+ putFile16BitBE(file, element_info[element].move_pattern);
+ putFile8Bit(file, element_info[element].move_direction_initial);
+ putFile8Bit(file, element_info[element].move_stepsize);
+
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ putFile16BitBE(file, element_info[element].content[x][y]);
+
+ putFile32BitBE(file, element_info[element].change.events);
+
+ putFile16BitBE(file, element_info[element].change.target_element);
+
+ putFile16BitBE(file, element_info[element].change.delay_fixed);
+ putFile16BitBE(file, element_info[element].change.delay_random);
+ putFile16BitBE(file, element_info[element].change.delay_frames);
+
+ putFile16BitBE(file, element_info[element].change.trigger_element);
+
+ putFile8Bit(file, element_info[element].change.explode);
+ putFile8Bit(file, element_info[element].change.use_content);
+ putFile8Bit(file, element_info[element].change.only_complete);
+ putFile8Bit(file, element_info[element].change.use_random_change);
+
+ putFile8Bit(file, element_info[element].change.random);
+ putFile8Bit(file, element_info[element].change.power);
+
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ putFile16BitBE(file, element_info[element].change.content[x][y]);
+
+ /* some free bytes for future properties and padding */
+ WriteUnusedBytesToFile(file, LEVEL_CPART_CUS3_UNUSED);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) /* should not happen */
+ Error(ERR_WARN, "inconsistent number of custom element properties");
+}
+
+void SaveLevel(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+ int body_chunk_size;
+ int num_changed_custom_elements = 0;
+ int level_chunk_CUS3_size;
+ 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_yamyam_contents; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ if (level.yamyam_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 "CUS3" 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_elements++;
+ level_chunk_CUS3_size = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
+
+ 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_yamyam_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_elements > 0)
+ {
+ putFileChunkBE(file, "CUS3", level_chunk_CUS3_size);
+ SaveLevel_CUS3(file, &level, num_changed_custom_elements);
+ }
+
+ fclose(file);
+
+ SetFilePermissions(filename, PERMS_PRIVATE);
+}
+
+void DumpLevel(struct LevelInfo *level)
+{
+ printf_line("-", 79);
+ printf("Level xxx (file version %08d, game version %08d)\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);