+ // all engine modifications also valid for levels which use latest engine
+ if (level->game_version < VERSION_IDENT(3,2,0,5))
+ {
+ // time bonus score was given for 10 s instead of 1 s before 3.2.0-5
+ level->score[SC_TIME_BONUS] /= 10;
+ }
+
+ if (leveldir_current->latest_engine)
+ {
+ // ---------- use latest game engine --------------------------------------
+
+ /* For all levels which are forced to use the latest game engine version
+ (normally all but user contributed, private and undefined levels), set
+ the game engine version to the actual version; 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 emulation
+ of the corresponding game 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;
+
+ return;
+ }
+
+ // ---------- use game engine the level was created with --------------------
+
+ /* For all levels which are not forced to use the latest game engine
+ version (normally user contributed, private and undefined 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). */
+
+ // player was faster than enemies in 1.0.0 and before
+ if (level->file_version == FILE_VERSION_1_0)
+ for (i = 0; i < MAX_PLAYERS; i++)
+ level->initial_player_stepsize[i] = STEPSIZE_FAST;
+
+ // default behaviour for EM style gems was "slippery" only in 2.0.1
+ if (level->game_version == VERSION_IDENT(2,0,1,0))
+ level->em_slippery_gems = TRUE;
+
+ // springs could be pushed over pits before (pre-release version) 2.2.0
+ if (level->game_version < VERSION_IDENT(2,2,0,0))
+ level->use_spring_bug = TRUE;
+
+ if (level->game_version < VERSION_IDENT(3,2,0,5))
+ {
+ // time orb caused limited time in endless time levels before 3.2.0-5
+ level->use_time_orb_bug = TRUE;
+
+ // default behaviour for snapping was "no snap delay" before 3.2.0-5
+ level->block_snap_field = FALSE;
+
+ // extra time score was same value as time left score before 3.2.0-5
+ level->extra_time_score = level->score[SC_TIME_BONUS];
+ }
+
+ if (level->game_version < VERSION_IDENT(3,2,0,7))
+ {
+ // default behaviour for snapping was "not continuous" before 3.2.0-7
+ level->continuous_snapping = FALSE;
+ }
+
+ // only few elements were able to actively move into acid before 3.1.0
+ // trigger settings did not exist before 3.1.0; set to default "any"
+ if (level->game_version < VERSION_IDENT(3,1,0,0))
+ {
+ // correct "can move into acid" settings (all zero in old levels)
+
+ level->can_move_into_acid_bits = 0; // nothing can move into acid
+ level->dont_collide_with_bits = 0; // nothing is deadly when colliding
+
+ setMoveIntoAcidProperty(level, EL_ROBOT, TRUE);
+ setMoveIntoAcidProperty(level, EL_SATELLITE, TRUE);
+ setMoveIntoAcidProperty(level, EL_PENGUIN, TRUE);
+ setMoveIntoAcidProperty(level, EL_BALLOON, TRUE);
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ SET_PROPERTY(EL_CUSTOM_START + i, EP_CAN_MOVE_INTO_ACID, TRUE);
+
+ // correct trigger settings (stored as zero == "none" in old levels)
+
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ for (j = 0; j < ei->num_change_pages; j++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[j];
+
+ change->trigger_player = CH_PLAYER_ANY;
+ change->trigger_page = CH_PAGE_ANY;
+ }
+ }
+ }
+
+ // try to detect and fix "Snake Bite" levels, which are broken with 3.2.0
+ {
+ int element = EL_CUSTOM_256;
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementChangeInfo *change = &ei->change_page[0];
+
+ /* This is needed to fix a problem that was caused by a bugfix in function
+ game.c/CreateFieldExt() introduced with 3.2.0 that corrects the behaviour
+ when a custom element changes to EL_SOKOBAN_FIELD_PLAYER (before, it did
+ not replace walkable elements, but instead just placed the player on it,
+ without placing the Sokoban field under the player). Unfortunately, this
+ breaks "Snake Bite" style levels when the snake is halfway through a door
+ that just closes (the snake head is still alive and can be moved in this
+ case). This can be fixed by replacing the EL_SOKOBAN_FIELD_PLAYER by the
+ player (without Sokoban element) which then gets killed as designed). */
+
+ if ((strncmp(leveldir_current->identifier, "snake_bite", 10) == 0 ||
+ strncmp(ei->description, "pause b4 death", 14) == 0) &&
+ change->target_element == EL_SOKOBAN_FIELD_PLAYER)
+ change->target_element = EL_PLAYER_1;
+ }
+
+ // try to detect and fix "Zelda" style levels, which are broken with 3.2.5
+ if (level->game_version < VERSION_IDENT(3,2,5,0))
+ {
+ /* This is needed to fix a problem that was caused by a bugfix in function
+ game.c/CheckTriggeredElementChangeExt() introduced with 3.2.5 that
+ corrects the behaviour when a custom element changes to another custom
+ element with a higher element number that has change actions defined.
+ Normally, only one change per frame is allowed for custom elements.
+ Therefore, it is checked if a custom element already changed in the
+ current frame; if it did, subsequent changes are suppressed.
+ Unfortunately, this is only checked for element changes, but not for
+ change actions, which are still executed. As the function above loops
+ through all custom elements from lower to higher, an element change
+ resulting in a lower CE number won't be checked again, while a target
+ element with a higher number will also be checked, and potential change
+ actions will get executed for this CE, too (which is wrong), while
+ further changes are ignored (which is correct). As this bugfix breaks
+ Zelda II (and introduces graphical bugs to Zelda I, and also breaks a
+ few other levels like Alan Bond's "FMV"), allow the previous, incorrect
+ behaviour for existing levels and tapes that make use of this bug */
+
+ level->use_action_after_change_bug = TRUE;
+ }
+
+ // not centering level after relocating player was default only in 3.2.3
+ if (level->game_version == VERSION_IDENT(3,2,3,0)) // (no pre-releases)
+ level->shifted_relocation = TRUE;
+
+ // EM style elements always chain-exploded in R'n'D engine before 3.2.6
+ if (level->game_version < VERSION_IDENT(3,2,6,0))
+ level->em_explodes_by_fire = TRUE;
+
+ // levels were solved by the first player entering an exit up to 4.1.0.0
+ if (level->game_version <= VERSION_IDENT(4,1,0,0))
+ level->solved_by_one_player = TRUE;
+
+ // game logic of "game of life" and "biomaze" was buggy before 4.1.1.1
+ if (level->game_version < VERSION_IDENT(4,1,1,1))
+ level->use_life_bugs = TRUE;
+
+ // only Sokoban fields (but not objects) had to be solved before 4.1.1.1
+ if (level->game_version < VERSION_IDENT(4,1,1,1))
+ level->sb_objects_needed = FALSE;
+}
+
+static void LoadLevel_InitStandardElements(struct LevelInfo *level)
+{
+ int i, x, y;
+
+ // map elements that have changed in newer versions
+ level->amoeba_content = getMappedElementByVersion(level->amoeba_content,
+ level->game_version);
+ for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
+ for (x = 0; x < 3; x++)
+ for (y = 0; y < 3; y++)
+ level->yamyam_content[i].e[x][y] =
+ getMappedElementByVersion(level->yamyam_content[i].e[x][y],
+ level->game_version);
+
+}
+
+static void LoadLevel_InitCustomElements(struct LevelInfo *level)
+{
+ int i, j;
+
+ // map custom element change events that have changed in newer versions
+ // (these following values were accidentally changed in version 3.0.1)
+ // (this seems to be needed only for 'ab_levelset3' and 'ab_levelset4')
+ if (level->game_version <= VERSION_IDENT(3,0,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ // order of checking and copying events to be mapped is important
+ // (do not change the start and end value -- they are constant)
+ for (j = CE_BY_OTHER_ACTION; j >= CE_VALUE_GETS_ZERO; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 2))
+ {
+ SET_CHANGE_EVENT(element, j - 2, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+
+ // order of checking and copying events to be mapped is important
+ // (do not change the start and end value -- they are constant)
+ for (j = CE_PLAYER_COLLECTS_X; j >= CE_HITTING_SOMETHING; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 1))
+ {
+ SET_CHANGE_EVENT(element, j - 1, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+ }
+ }
+
+ // initialize "can_change" field for old levels with only one change page
+ if (level->game_version <= VERSION_IDENT(3,0,2,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (CAN_CHANGE(element))
+ element_info[element].change->can_change = TRUE;
+ }
+ }
+
+ // correct custom element values (for old levels without these options)
+ if (level->game_version < VERSION_IDENT(3,1,1,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->access_direction == MV_NO_DIRECTION)
+ ei->access_direction = MV_ALL_DIRECTIONS;
+ }
+ }
+
+ // correct custom element values (fix invalid values for all versions)
+ if (1)
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+ struct ElementInfo *ei = &element_info[element];
+
+ for (j = 0; j < ei->num_change_pages; j++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[j];
+
+ if (change->trigger_player == CH_PLAYER_NONE)
+ change->trigger_player = CH_PLAYER_ANY;
+
+ if (change->trigger_side == CH_SIDE_NONE)
+ change->trigger_side = CH_SIDE_ANY;
+ }
+ }
+ }
+
+ // initialize "can_explode" field for old levels which did not store this
+ // !!! CHECK THIS -- "<= 3,1,0,0" IS PROBABLY WRONG !!!
+ if (level->game_version <= VERSION_IDENT(3,1,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (EXPLODES_1X1_OLD(element))
+ element_info[element].explosion_type = EXPLODES_1X1;
+
+ SET_PROPERTY(element, EP_CAN_EXPLODE, (EXPLODES_BY_FIRE(element) ||
+ EXPLODES_SMASHED(element) ||
+ EXPLODES_IMPACT(element)));
+ }
+ }
+
+ // correct previously hard-coded move delay values for maze runner style
+ if (level->game_version < VERSION_IDENT(3,1,1,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ if (element_info[element].move_pattern & MV_MAZE_RUNNER_STYLE)
+ {
+ // previously hard-coded and therefore ignored
+ element_info[element].move_delay_fixed = 9;
+ element_info[element].move_delay_random = 0;
+ }
+ }
+ }
+
+ // set some other uninitialized values of custom elements in older levels
+ if (level->game_version < VERSION_IDENT(3,1,0,0))
+ {
+ for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ element_info[element].access_direction = MV_ALL_DIRECTIONS;
+
+ element_info[element].explosion_delay = 17;
+ element_info[element].ignition_delay = 8;
+ }
+ }
+}
+
+static void LoadLevel_InitElements(struct LevelInfo *level)
+{
+ LoadLevel_InitStandardElements(level);
+
+ if (level->file_has_custom_elements)
+ LoadLevel_InitCustomElements(level);
+
+ // initialize element properties for level editor etc.
+ InitElementPropertiesEngine(level->game_version);
+ InitElementPropertiesGfxElement();
+}
+
+static void LoadLevel_InitPlayfield(struct LevelInfo *level)
+{
+ int x, y;
+
+ // map elements that have changed in newer versions
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ level->field[x][y] = getMappedElementByVersion(level->field[x][y],
+ level->game_version);
+
+ // clear unused playfield data (nicer if level gets resized in editor)
+ for (x = 0; x < MAX_LEV_FIELDX; x++)
+ for (y = 0; y < MAX_LEV_FIELDY; y++)
+ if (x >= level->fieldx || y >= level->fieldy)
+ level->field[x][y] = EL_EMPTY;
+
+ // 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
+ if (level->file_info.type == LEVEL_FILE_TYPE_DC)
+ BorderElement = EL_EMPTY; // (in editor, SetBorderElement() is used)
+ else
+ SetBorderElement();
+}
+
+static void LoadLevel_InitNativeEngines(struct LevelInfo *level)
+{
+ struct LevelFileInfo *level_file_info = &level->file_info;
+
+ if (level_file_info->type == LEVEL_FILE_TYPE_RND)
+ CopyNativeLevel_RND_to_Native(level);
+}
+
+static void LoadLevelTemplate_LoadAndInit(void)
+{
+ LoadLevelFromFileInfo(&level_template, &level_template.file_info, FALSE);
+
+ LoadLevel_InitVersion(&level_template);
+ LoadLevel_InitElements(&level_template);
+
+ ActivateLevelTemplate();
+}
+
+void LoadLevelTemplate(int nr)
+{
+ if (!fileExists(getGlobalLevelTemplateFilename()))
+ {
+ Error(ERR_WARN, "no level template found for this level");
+
+ return;
+ }
+
+ setLevelFileInfo(&level_template.file_info, nr);
+
+ LoadLevelTemplate_LoadAndInit();
+}
+
+static void LoadNetworkLevelTemplate(struct NetworkLevelInfo *network_level)
+{
+ copyLevelFileInfo(&network_level->tmpl_info, &level_template.file_info);
+
+ LoadLevelTemplate_LoadAndInit();
+}
+
+static void LoadLevel_LoadAndInit(struct NetworkLevelInfo *network_level)
+{
+ LoadLevelFromFileInfo(&level, &level.file_info, FALSE);
+
+ if (level.use_custom_template)
+ {
+ if (network_level != NULL)
+ LoadNetworkLevelTemplate(network_level);
+ else
+ LoadLevelTemplate(-1);
+ }
+
+ LoadLevel_InitVersion(&level);
+ LoadLevel_InitElements(&level);
+ LoadLevel_InitPlayfield(&level);
+
+ LoadLevel_InitNativeEngines(&level);
+}
+
+void LoadLevel(int nr)
+{
+ SetLevelSetInfo(leveldir_current->identifier, nr);
+
+ setLevelFileInfo(&level.file_info, nr);
+
+ LoadLevel_LoadAndInit(NULL);
+}
+
+void LoadLevelInfoOnly(int nr)
+{
+ setLevelFileInfo(&level.file_info, nr);
+
+ LoadLevelFromFileInfo(&level, &level.file_info, TRUE);
+}
+
+void LoadNetworkLevel(struct NetworkLevelInfo *network_level)
+{
+ SetLevelSetInfo(network_level->leveldir_identifier,
+ network_level->file_info.nr);
+
+ copyLevelFileInfo(&network_level->file_info, &level.file_info);
+
+ LoadLevel_LoadAndInit(network_level);
+}
+
+static int SaveLevel_VERS(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+
+ chunk_size += putFileVersion(file, level->file_version);
+ chunk_size += putFileVersion(file, level->game_version);
+
+ return chunk_size;
+}
+
+static int SaveLevel_DATE(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+
+ chunk_size += putFile16BitBE(file, level->creation_date.year);
+ chunk_size += putFile8Bit(file, level->creation_date.month);
+ chunk_size += putFile8Bit(file, level->creation_date.day);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+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].e[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->initial_player_stepsize == STEPSIZE_FAST ? 1 : 0));
+ putFile8Bit(file, (level->initial_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));
+
+ putFile8Bit(file, (level->block_last_field ? 1 : 0));
+ putFile8Bit(file, (level->sp_block_last_field ? 1 : 0));
+ putFile32BitBE(file, level->can_move_into_acid_bits);
+ putFile8Bit(file, level->dont_collide_with_bits);
+
+ putFile8Bit(file, (level->use_spring_bug ? 1 : 0));
+ putFile8Bit(file, (level->use_step_counter ? 1 : 0));
+
+ putFile8Bit(file, (level->instant_relocation ? 1 : 0));
+ putFile8Bit(file, (level->can_pass_to_walkable ? 1 : 0));
+ putFile8Bit(file, (level->grow_into_diggable ? 1 : 0));
+
+ putFile8Bit(file, level->game_engine_type);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_HEAD_UNUSED);
+}
+#endif
+
+static int SaveLevel_NAME(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
+ chunk_size += putFile8Bit(file, level->name[i]);
+
+ return chunk_size;
+}
+
+static int SaveLevel_AUTH(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int i;
+
+ for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
+ chunk_size += putFile8Bit(file, level->author[i]);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int x, y;
+
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ if (level->encoding_16bit_field)
+ chunk_size += putFile16BitBE(file, level->field[x][y]);
+ else
+ chunk_size += putFile8Bit(file, level->field[x][y]);
+
+ return chunk_size;
+}
+#endif
+
+static int SaveLevel_BODY(FILE *file, struct LevelInfo *level)
+{
+ int chunk_size = 0;
+ int x, y;
+
+ for (y = 0; y < level->fieldy; y++)
+ for (x = 0; x < level->fieldx; x++)
+ chunk_size += putFile16BitBE(file, level->field[x][y]);
+
+ return chunk_size;
+}
+
+#if ENABLE_HISTORIC_CHUNKS
+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].e[x][y]);
+ else
+ putFile8Bit(file, level->yamyam_content[i].e[x][y]);
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+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].e[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]);
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static int SaveLevel_CNT3(FILE *file, struct LevelInfo *level, int element)
+{
+ int envelope_nr = element - EL_ENVELOPE_1;
+ int envelope_len = strlen(level->envelope_text[envelope_nr]) + 1;
+ int chunk_size = 0;
+ int i;
+
+ chunk_size += putFile16BitBE(file, element);
+ chunk_size += putFile16BitBE(file, envelope_len);
+ chunk_size += putFile8Bit(file, level->envelope_xsize[envelope_nr]);
+ chunk_size += putFile8Bit(file, level->envelope_ysize[envelope_nr]);
+
+ WriteUnusedBytesToFile(file, LEVEL_CHUNK_CNT3_UNUSED);
+ chunk_size += LEVEL_CHUNK_CNT3_UNUSED;
+
+ for (i = 0; i < envelope_len; i++)
+ chunk_size += putFile8Bit(file, level->envelope_text[envelope_nr][i]);
+
+ return chunk_size;
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+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;
+
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->properties[EP_BITFIELD_BASE_NR] != EP_BITMASK_DEFAULT)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+ }
+
+ check++;
+ }
+ }
+
+ if (check != num_changed_custom_elements) // should not happen
+ Error(ERR_WARN, "inconsistent number of custom element properties");
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+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
+
+#if ENABLE_HISTORIC_CHUNKS
+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;
+ struct ElementInfo *ei = &element_info[element];
+
+ if (ei->modified_settings)
+ {
+ if (check < num_changed_custom_elements)
+ {
+ putFile16BitBE(file, element);
+
+ for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++)
+ putFile8Bit(file, ei->description[j]);
+
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+
+ // some free bytes for future properties and padding
+ WriteUnusedBytesToFile(file, 7);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, ei->collect_score_initial);
+ putFile8Bit(file, ei->collect_count_initial);
+
+ putFile16BitBE(file, ei->push_delay_fixed);
+ putFile16BitBE(file, ei->push_delay_random);
+ putFile16BitBE(file, ei->move_delay_fixed);
+ putFile16BitBE(file, ei->move_delay_random);
+
+ putFile16BitBE(file, ei->move_pattern);
+ putFile8Bit(file, ei->move_direction_initial);
+ putFile8Bit(file, ei->move_stepsize);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->content.e[x][y]);
+
+ putFile32BitBE(file, ei->change->events);
+
+ putFile16BitBE(file, ei->change->target_element);
+
+ putFile16BitBE(file, ei->change->delay_fixed);
+ putFile16BitBE(file, ei->change->delay_random);
+ putFile16BitBE(file, ei->change->delay_frames);
+
+ putFile16BitBE(file, ei->change->initial_trigger_element);
+
+ putFile8Bit(file, ei->change->explode);
+ putFile8Bit(file, ei->change->use_target_content);
+ putFile8Bit(file, ei->change->only_if_complete);
+ putFile8Bit(file, ei->change->use_random_replace);
+
+ putFile8Bit(file, ei->change->random_percentage);
+ putFile8Bit(file, ei->change->replace_when);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->change->content.e[x][y]);
+
+ putFile8Bit(file, ei->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");
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ int i, j, x, y;
+
+ // ---------- custom element base property values (96 bytes) ----------------
+
+ putFile16BitBE(file, element);
+
+ for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
+ putFile8Bit(file, ei->description[i]);
+
+ putFile32BitBE(file, ei->properties[EP_BITFIELD_BASE_NR]);
+
+ WriteUnusedBytesToFile(file, 4); // reserved for more base properties
+
+ putFile8Bit(file, ei->num_change_pages);
+
+ putFile16BitBE(file, ei->ce_value_fixed_initial);
+ putFile16BitBE(file, ei->ce_value_random_initial);
+ putFile8Bit(file, ei->use_last_ce_value);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, ei->collect_score_initial);
+ putFile8Bit(file, ei->collect_count_initial);
+
+ putFile8Bit(file, ei->drop_delay_fixed);
+ putFile8Bit(file, ei->push_delay_fixed);
+ putFile8Bit(file, ei->drop_delay_random);
+ putFile8Bit(file, ei->push_delay_random);
+ putFile16BitBE(file, ei->move_delay_fixed);
+ putFile16BitBE(file, ei->move_delay_random);
+
+ // bits 0 - 15 of "move_pattern" ...
+ putFile16BitBE(file, ei->move_pattern & 0xffff);
+ putFile8Bit(file, ei->move_direction_initial);
+ putFile8Bit(file, ei->move_stepsize);
+
+ putFile8Bit(file, ei->slippery_type);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, ei->content.e[x][y]);
+
+ putFile16BitBE(file, ei->move_enter_element);
+ putFile16BitBE(file, ei->move_leave_element);
+ putFile8Bit(file, ei->move_leave_type);
+
+ // ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible)
+ putFile16BitBE(file, (ei->move_pattern >> 16) & 0xffff);
+
+ putFile8Bit(file, ei->access_direction);
+
+ putFile8Bit(file, ei->explosion_delay);
+ putFile8Bit(file, ei->ignition_delay);
+ putFile8Bit(file, ei->explosion_type);
+
+ // some free bytes for future custom property values and padding
+ WriteUnusedBytesToFile(file, 1);
+
+ // ---------- change page property values (48 bytes) ------------------------
+
+ for (i = 0; i < ei->num_change_pages; i++)
+ {
+ struct ElementChangeInfo *change = &ei->change_page[i];
+ unsigned int event_bits;
+
+ // bits 0 - 31 of "has_event[]" ...
+ event_bits = 0;
+ for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++)
+ if (change->has_event[j])
+ event_bits |= (1 << j);
+ putFile32BitBE(file, event_bits);
+
+ putFile16BitBE(file, change->target_element);
+
+ putFile16BitBE(file, change->delay_fixed);
+ putFile16BitBE(file, change->delay_random);
+ putFile16BitBE(file, change->delay_frames);
+
+ putFile16BitBE(file, change->initial_trigger_element);
+
+ putFile8Bit(file, change->explode);
+ putFile8Bit(file, change->use_target_content);
+ putFile8Bit(file, change->only_if_complete);
+ putFile8Bit(file, change->use_random_replace);
+
+ putFile8Bit(file, change->random_percentage);
+ putFile8Bit(file, change->replace_when);
+
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ putFile16BitBE(file, change->target_content.e[x][y]);
+
+ putFile8Bit(file, change->can_change);
+
+ putFile8Bit(file, change->trigger_side);
+
+ putFile8Bit(file, change->trigger_player);
+ putFile8Bit(file, (change->trigger_page == CH_PAGE_ANY ? CH_PAGE_ANY_FILE :
+ log_2(change->trigger_page)));
+
+ putFile8Bit(file, change->has_action);
+ putFile8Bit(file, change->action_type);
+ putFile8Bit(file, change->action_mode);
+ putFile16BitBE(file, change->action_arg);
+
+ // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible)
+ event_bits = 0;
+ for (j = 32; j < NUM_CHANGE_EVENTS; j++)
+ if (change->has_event[j])
+ event_bits |= (1 << (j - 32));
+ putFile8Bit(file, event_bits);
+ }
+}
+#endif
+
+#if ENABLE_HISTORIC_CHUNKS
+static void SaveLevel_GRP1(FILE *file, struct LevelInfo *level, int element)
+{
+ struct ElementInfo *ei = &element_info[element];
+ struct ElementGroupInfo *group = ei->group;
+ int i;
+
+ putFile16BitBE(file, element);
+
+ for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++)
+ putFile8Bit(file, ei->description[i]);
+
+ putFile8Bit(file, group->num_elements);
+
+ putFile8Bit(file, ei->use_gfx_element);
+ putFile16BitBE(file, ei->gfx_element_initial);
+
+ putFile8Bit(file, group->choice_mode);
+
+ // some free bytes for future values and padding
+ WriteUnusedBytesToFile(file, 3);
+
+ for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++)
+ putFile16BitBE(file, group->element[i]);
+}
+#endif
+
+static int SaveLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *entry,
+ boolean write_element)
+{
+ int save_type = entry->save_type;
+ int data_type = entry->data_type;
+ int conf_type = entry->conf_type;
+ int byte_mask = conf_type & CONF_MASK_BYTES;
+ int element = entry->element;
+ int default_value = entry->default_value;
+ int num_bytes = 0;
+ boolean modified = FALSE;
+
+ if (byte_mask != CONF_MASK_MULTI_BYTES)
+ {
+ void *value_ptr = entry->value;
+ int value = (data_type == TYPE_BOOLEAN ? *(boolean *)value_ptr :
+ *(int *)value_ptr);
+
+ // check if any settings have been modified before saving them
+ if (value != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += (byte_mask == CONF_MASK_1_BYTE ? putFile8Bit (file, value) :
+ byte_mask == CONF_MASK_2_BYTE ? putFile16BitBE(file, value) :
+ byte_mask == CONF_MASK_4_BYTE ? putFile32BitBE(file, value) :
+ 0);
+ }
+ else if (data_type == TYPE_STRING)
+ {
+ char *default_string = entry->default_string;
+ char *string = (char *)(entry->value);
+ int string_length = strlen(string);
+ int i;
+
+ // check if any settings have been modified before saving them
+ if (!strEqual(string, default_string))
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, string_length);
+
+ for (i = 0; i < string_length; i++)
+ num_bytes += putFile8Bit(file, string[i]);
+ }
+ else if (data_type == TYPE_ELEMENT_LIST)
+ {
+ int *element_array = (int *)(entry->value);
+ int num_elements = *(int *)(entry->num_entities);
+ int i;
+
+ // check if any settings have been modified before saving them
+ for (i = 0; i < num_elements; i++)
+ if (element_array[i] != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, num_elements * CONF_ELEMENT_NUM_BYTES);
+
+ for (i = 0; i < num_elements; i++)
+ num_bytes += putFile16BitBE(file, element_array[i]);
+ }
+ else if (data_type == TYPE_CONTENT_LIST)
+ {
+ struct Content *content = (struct Content *)(entry->value);
+ int num_contents = *(int *)(entry->num_entities);
+ int i, x, y;
+
+ // check if any settings have been modified before saving them
+ for (i = 0; i < num_contents; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ if (content[i].e[x][y] != default_value)
+ modified = TRUE;
+
+ // do not save if explicitly told or if unmodified default settings
+ if ((save_type == SAVE_CONF_NEVER) ||
+ (save_type == SAVE_CONF_WHEN_CHANGED && !modified))
+ return 0;
+
+ if (write_element)
+ num_bytes += putFile16BitBE(file, element);
+
+ num_bytes += putFile8Bit(file, conf_type);
+ num_bytes += putFile16BitBE(file, num_contents * CONF_CONTENT_NUM_BYTES);
+
+ for (i = 0; i < num_contents; i++)
+ for (y = 0; y < 3; y++)
+ for (x = 0; x < 3; x++)
+ num_bytes += putFile16BitBE(file, content[i].e[x][y]);
+ }
+
+ return num_bytes;
+}
+
+static int SaveLevel_INFO(FILE *file, struct LevelInfo *level)
+{