+ char chunk_name[CHUNK_ID_LEN + 1];
+ int chunk_size;
+ FILE *file;
+
+ /* always start with reliable default values */
+ setLevelInfoToDefaults();
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ level.no_level_file = TRUE;
+
+ Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
+ 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, "CAVE") != 0)
+ {
+ Error(ERR_WARN, "unknown format of level 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, LEVEL_COOKIE_TMPL))
+ {
+ Error(ERR_WARN, "unknown format of level file '%s'", filename);
+ fclose(file);
+ return;
+ }
+
+ if ((level.file_version = getFileVersionFromCookieString(cookie)) == -1)
+ {
+ Error(ERR_WARN, "unsupported version of level file '%s'", filename);
+ fclose(file);
+ return;
+ }
+
+ /* pre-2.0 level files have no game version, so use file version here */
+ level.game_version = level.file_version;
+ }
+
+ if (level.file_version < FILE_VERSION_1_2)
+ {
+ /* level files from versions before 1.2.0 without chunk structure */
+ LoadLevel_HEAD(file, LEVEL_HEADER_SIZE, &level);
+ LoadLevel_BODY(file, level.fieldx * level.fieldy, &level);
+ }
+ else
+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(FILE *, int, struct LevelInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", FILE_VERS_CHUNK_SIZE, LoadLevel_VERS },
+ { "HEAD", LEVEL_HEADER_SIZE, LoadLevel_HEAD },
+ { "AUTH", MAX_LEVEL_AUTHOR_LEN, LoadLevel_AUTH },
+ { "BODY", -1, LoadLevel_BODY },
+ { "CONT", -1, LoadLevel_CONT },
+ { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 },
+ { "CUS1", -1, LoadLevel_CUS1 },
+ { "CUS2", -1, LoadLevel_CUS2 },
+ { "CUS3", -1, LoadLevel_CUS3 },
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ 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 level 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 level file '%s'",
+ chunk_size, chunk_name, filename);
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ /* call function to load this level chunk */
+ int chunk_size_expected =
+ (chunk_info[i].loader)(file, chunk_size, &level);
+
+ /* 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 level file '%s'",
+ chunk_size, chunk_name, filename);
+ }
+ }
+ }
+ }
+
+ fclose(file);
+
+ if (leveldir_current == NULL) /* only when dumping level */
+ return;
+
+ if (IS_LEVELCLASS_CONTRIBUTION(leveldir_current) ||
+ IS_LEVELCLASS_USER(leveldir_current))
+ {
+ /* For user contributed and private levels, use the version of
+ the game engine the levels were created for.
+ Since 2.0.1, the game engine version is now directly stored
+ in the level file (chunk "VERS"), so there is no need anymore
+ to set the game version from the file version (except for old,
+ pre-2.0 levels, where the game version is still taken from the
+ file format version used to store the level -- see above). */
+
+ /* do some special adjustments to support older level versions */
+ if (level.file_version == FILE_VERSION_1_0)
+ {
+ Error(ERR_WARN, "level file '%s' has version number 1.0", filename);
+ Error(ERR_WARN, "using high speed movement for player");
+
+ /* player was faster than monsters in (pre-)1.0 levels */
+ level.double_speed = TRUE;
+ }
+
+ /* Default behaviour for EM style gems was "slippery" only in 2.0.1 */
+ if (level.game_version == VERSION_IDENT(2,0,1))
+ level.em_slippery_gems = TRUE;
+ }
+ else
+ {
+ /* Always use the latest version of the game engine for all but
+ user contributed and private levels; this allows for actual
+ corrections in the game engine to take effect for existing,
+ converted levels (from "classic" or other existing games) to
+ make the game emulation more accurate, while (hopefully) not
+ breaking existing levels created from other players. */
+
+ level.game_version = GAME_VERSION_ACTUAL;
+
+ /* Set special EM style gems behaviour: EM style gems slip down from
+ normal, steel and growing wall. As this is a more fundamental change,
+ it seems better to set the default behaviour to "off" (as it is more
+ natural) and make it configurable in the level editor (as a property
+ of gem style elements). Already existing converted levels (neither
+ private nor contributed levels) are changed to the new behaviour. */
+
+ if (level.file_version < FILE_VERSION_2_0)
+ level.em_slippery_gems = TRUE;
+ }
+
+ /* 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, j, 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);
+
+ for(j=0; j<MAX_ELEMENT_NAME_LEN; j++)
+ putFile8Bit(file, element_info[element].description[j]);
+
+ 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]);
+
+ putFile8Bit(file, element_info[element].slippery_type);
+
+ /* 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;