+ 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);
+}
+
+static void LoadLevel_InitLevel(struct LevelInfo *level, char *filename)
+{
+ int x, y;
+
+ 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 elements which have changed in newer versions */
+ if (level->game_version <= VERSION_IDENT(2,2,0))
+ {
+ /* map game font elements */
+ for(y=0; y<level->fieldy; y++)
+ {
+ for(x=0; x<level->fieldx; x++)
+ {
+ int element = level->field[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;
+
+ level->field[x][y] = element;
+ }
+ }
+ }
+
+ /* copy elements to runtime playfield array */
+ for(x=0; x<MAX_LEV_FIELDX; x++)
+ for(y=0; y<MAX_LEV_FIELDY; y++)
+ Feld[x][y] = level->field[x][y];
+
+ /* initialize level size variables for faster access */
+ lev_fieldx = level->fieldx;
+ lev_fieldy = level->fieldy;
+
+ /* determine border element for this level */
+ SetBorderElement();
+
+ /* initialize element properties for level editor etc. */
+ InitElementPropertiesEngine(level->game_version);
+}
+
+void LoadLevelTemplate(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ LoadLevelFromFilename(&level_template, filename);
+
+ ActivateLevelTemplate();
+}
+
+void LoadLevel(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ LoadLevelFromFilename(&level, filename);
+
+ if (level.use_custom_template)
+ LoadLevelTemplate(-1);
+
+ LoadLevel_InitLevel(&level, filename);
+}
+
+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;
+
+ putFile8Bit(file, level->fieldx);
+ putFile8Bit(file, level->fieldy);
+
+ putFile16BitBE(file, level->time);
+ putFile16BitBE(file, level->gems_needed);
+
+ for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
+ putFile8Bit(file, level->name[i]);
+
+ for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
+ putFile8Bit(file, level->score[i]);
+
+ for(i=0; i<STD_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ putFile8Bit(file, (level->encoding_16bit_yamyam ? EL_EMPTY :
+ level->yamyam_content[i][x][y]));
+ putFile8Bit(file, level->amoeba_speed);
+ putFile8Bit(file, level->time_magic_wall);
+ putFile8Bit(file, level->time_wheel);
+ putFile8Bit(file, (level->encoding_16bit_amoeba ? EL_EMPTY :
+ level->amoeba_content));
+ putFile8Bit(file, (level->double_speed ? 1 : 0));
+ putFile8Bit(file, (level->gravity ? 1 : 0));
+ putFile8Bit(file, (level->encoding_16bit_field ? 1 : 0));
+ putFile8Bit(file, (level->em_slippery_gems ? 1 : 0));
+
+ putFile8Bit(file, (level->use_custom_template ? 1 : 0));
+
+ 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++)
+ putFile8Bit(file, level->author[i]);
+}
+
+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, level->field[x][y]);
+ else
+ putFile8Bit(file, level->field[x][y]);
+}
+
+#if 0
+static void SaveLevel_CONT(FILE *file, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ putFile8Bit(file, EL_YAMYAM);
+ putFile8Bit(file, level->num_yamyam_contents);
+ putFile8Bit(file, 0);
+ putFile8Bit(file, 0);
+
+ 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
+ putFile8Bit(file, level->yamyam_content[i][x][y]);
+}
+#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);
+ putFile8Bit(file, num_contents);
+ putFile8Bit(file, content_xsize);
+ putFile8Bit(file, content_ysize);
+
+ 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");
+}
+
+static void SaveLevelFromFilename(struct LevelInfo *level, char *filename)
+{
+ 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 (level->field[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)