+
+#define CHUNK_ID_LEN 4 /* IFF style chunk id length */
+#define CHUNK_SIZE_UNDEFINED 0 /* undefined chunk size == 0 */
+#define CHUNK_SIZE_NONE -1 /* do not write chunk size */
+#define FILE_VERS_CHUNK_SIZE 8 /* size of file version chunk */
+#define LEVEL_HEADER_SIZE 80 /* size of level file header */
+#define LEVEL_HEADER_UNUSED 13 /* unused level header bytes */
+#define LEVEL_CHUNK_CNT2_SIZE 160 /* size of level CNT2 chunk */
+#define LEVEL_CHUNK_CNT2_UNUSED 11 /* unused CNT2 chunk bytes */
+#define LEVEL_CPART_CUS3_SIZE 134 /* size of CUS3 chunk part */
+#define LEVEL_CPART_CUS3_UNUSED 15 /* unused CUS3 bytes / part */
+#define TAPE_HEADER_SIZE 20 /* size of tape file header */
+#define TAPE_HEADER_UNUSED 3 /* unused tape header bytes */
+
+#define LEVEL_CHUNK_CUS3_SIZE(x) (2 + x * LEVEL_CPART_CUS3_SIZE)
+
+/* file identifier strings */
+#define LEVEL_COOKIE_TMPL "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_x.x"
+#define TAPE_COOKIE_TMPL "ROCKSNDIAMONDS_TAPE_FILE_VERSION_x.x"
+#define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2"
+
+
+/* ========================================================================= */
+/* level file functions */
+/* ========================================================================= */
+
+void setElementChangePages(struct ElementInfo *ei, int change_pages)
+{
+ int change_page_size = sizeof(struct ElementChangeInfo);
+
+ ei->num_change_pages = MAX(1, change_pages);
+
+ ei->change_page =
+ checked_realloc(ei->change_page, ei->num_change_pages * change_page_size);
+
+ if (ei->current_change_page >= ei->num_change_pages)
+ ei->current_change_page = ei->num_change_pages - 1;
+
+ ei->change = &ei->change_page[ei->current_change_page];
+}
+
+void setElementChangeInfoToDefaults(struct ElementChangeInfo *change)
+{
+ int x, y;
+
+ change->can_change = FALSE;
+
+ change->events = CE_BITMASK_DEFAULT;
+ change->target_element = EL_EMPTY_SPACE;
+
+ change->delay_fixed = 0;
+ change->delay_random = 0;
+ change->delay_frames = -1; /* later set to reliable default value */
+
+ change->trigger_element = EL_EMPTY_SPACE;
+
+ change->explode = FALSE;
+ change->use_content = FALSE;
+ change->only_complete = FALSE;
+ change->use_random_change = FALSE;
+ change->random = 0;
+ change->power = CP_NON_DESTRUCTIVE;
+
+ for(x=0; x<3; x++)
+ for(y=0; y<3; y++)
+ change->content[x][y] = EL_EMPTY_SPACE;
+
+ change->player_action = 0;
+ change->collide_action = 0;
+ change->other_action = 0;
+
+ change->pre_change_function = NULL;
+ change->change_function = NULL;
+ change->post_change_function = NULL;
+}
+
+static void setLevelInfoToDefaults(struct LevelInfo *level)
+{
+ int i, j, x, y;
+
+ level->file_version = FILE_VERSION_ACTUAL;
+ level->game_version = GAME_VERSION_ACTUAL;
+
+ level->encoding_16bit_field = FALSE; /* default: only 8-bit elements */
+ level->encoding_16bit_yamyam = FALSE; /* default: only 8-bit elements */
+ level->encoding_16bit_amoeba = FALSE; /* default: only 8-bit elements */
+
+ level->fieldx = STD_LEV_FIELDX;
+ level->fieldy = STD_LEV_FIELDY;
+
+ for(x=0; x<MAX_LEV_FIELDX; x++)
+ for(y=0; y<MAX_LEV_FIELDY; y++)
+ level->field[x][y] = EL_SAND;
+
+ level->time = 100;
+ level->gems_needed = 0;
+ level->amoeba_speed = 10;
+ level->time_magic_wall = 10;
+ level->time_wheel = 10;
+ level->time_light = 10;
+ level->time_timegate = 10;
+ level->amoeba_content = EL_DIAMOND;
+ level->double_speed = FALSE;
+ level->gravity = FALSE;
+ level->em_slippery_gems = FALSE;
+
+ level->use_custom_template = FALSE;
+
+ for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
+ level->name[i] = '\0';
+ for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
+ level->author[i] = '\0';
+
+ strcpy(level->name, NAMELESS_LEVEL_NAME);
+ strcpy(level->author, ANONYMOUS_NAME);
+
+ level->envelope[0] = '\0';
+ level->envelope_xsize = MAX_ENVELOPE_XSIZE;
+ level->envelope_ysize = MAX_ENVELOPE_YSIZE;
+
+ for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
+ level->score[i] = 10;
+
+ level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(x=0; x<3; x++)
+ for(y=0; y<3; y++)
+ level->yamyam_content[i][x][y] =
+ (i < STD_ELEMENT_CONTENTS ? EL_ROCK : EL_EMPTY);
+
+ level->field[0][0] = EL_PLAYER_1;
+ level->field[STD_LEV_FIELDX - 1][STD_LEV_FIELDY - 1] = EL_EXIT_CLOSED;
+
+ for (i=0; i < MAX_NUM_ELEMENTS; i++)
+ {
+ setElementChangePages(&element_info[i], 1);
+ setElementChangeInfoToDefaults(element_info[i].change);
+ }
+
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ for(j=0; j < MAX_ELEMENT_NAME_LEN + 1; j++)
+ element_info[element].description[j] = '\0';
+ if (element_info[element].custom_description != NULL)
+ strncpy(element_info[element].description,
+ element_info[element].custom_description, MAX_ELEMENT_NAME_LEN);
+ else
+ strcpy(element_info[element].description,
+ element_info[element].editor_description);
+
+ element_info[element].use_gfx_element = FALSE;
+ element_info[element].gfx_element = EL_EMPTY_SPACE;
+
+ element_info[element].collect_score = 10; /* special default */
+ element_info[element].collect_count = 1; /* special default */
+
+ element_info[element].push_delay_fixed = 2; /* special default */
+ element_info[element].push_delay_random = 8; /* special default */
+ element_info[element].move_delay_fixed = 0;
+ element_info[element].move_delay_random = 0;
+
+ element_info[element].move_pattern = MV_ALL_DIRECTIONS;
+ element_info[element].move_direction_initial = MV_NO_MOVING;
+ element_info[element].move_stepsize = TILEX / 8;
+
+ element_info[element].slippery_type = SLIPPERY_ANY_RANDOM;
+
+ for(x=0; x<3; x++)
+ for(y=0; y<3; y++)
+ element_info[element].content[x][y] = EL_EMPTY_SPACE;
+
+ element_info[element].access_type = 0;
+ element_info[element].access_layer = 0;
+ element_info[element].walk_to_action = 0;
+ element_info[element].smash_targets = 0;
+ element_info[element].deadliness = 0;
+ element_info[element].consistency = 0;
+
+ element_info[element].can_explode_by_fire = FALSE;
+ element_info[element].can_explode_smashed = FALSE;
+ element_info[element].can_explode_impact = FALSE;
+
+ element_info[element].current_change_page = 0;
+
+ /* start with no properties at all */
+ for (j=0; j < NUM_EP_BITFIELDS; j++)
+ Properties[element][j] = EP_BITMASK_DEFAULT;
+
+ element_info[element].modified_settings = FALSE;
+ }
+
+ BorderElement = EL_STEELWALL;
+
+ level->no_level_file = FALSE;
+
+ if (leveldir_current == NULL) /* only when dumping level */
+ return;
+
+ /* try to determine better author name than 'anonymous' */
+ if (strcmp(leveldir_current->author, ANONYMOUS_NAME) != 0)
+ {
+ strncpy(level->author, leveldir_current->author, MAX_LEVEL_AUTHOR_LEN);
+ level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
+ }
+ else
+ {
+ switch (LEVELCLASS(leveldir_current))
+ {
+ case LEVELCLASS_TUTORIAL:
+ strcpy(level->author, PROGRAM_AUTHOR_STRING);
+ break;
+
+ case LEVELCLASS_CONTRIBUTION:
+ strncpy(level->author, leveldir_current->name,MAX_LEVEL_AUTHOR_LEN);
+ level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
+ break;
+
+ case LEVELCLASS_USER:
+ strncpy(level->author, getRealName(), MAX_LEVEL_AUTHOR_LEN);
+ level->author[MAX_LEVEL_AUTHOR_LEN] = '\0';
+ break;
+
+ default:
+ /* keep default value */
+ break;
+ }
+ }
+}
+
+static void ActivateLevelTemplate()
+{
+ /* Currently there is no special action needed to activate the template
+ data, because 'element_info' and 'Properties' overwrite the original
+ level data, while all other variables do not change. */
+}
+
+boolean LevelFileExists(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ return (access(filename, F_OK) == 0);
+}
+
+static int checkLevelElement(int element)
+{
+ if (element >= NUM_FILE_ELEMENTS)
+ {
+ Error(ERR_WARN, "invalid level element %d", element);
+ element = EL_CHAR_QUESTION;
+ }
+ else if (element == EL_PLAYER_OBSOLETE)
+ element = EL_PLAYER_1;
+ else if (element == EL_KEY_OBSOLETE)
+ element = EL_KEY_1;
+
+ return element;
+}
+
+static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ level->file_version = getFileVersion(file);
+ level->game_version = getFileVersion(file);
+
+ return chunk_size;
+}
+
+static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int i, x, y;
+
+ level->fieldx = getFile8Bit(file);
+ level->fieldy = getFile8Bit(file);
+
+ level->time = getFile16BitBE(file);
+ level->gems_needed = getFile16BitBE(file);
+
+ for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
+ level->name[i] = getFile8Bit(file);
+ level->name[MAX_LEVEL_NAME_LEN] = 0;
+
+ for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
+ level->score[i] = getFile8Bit(file);
+
+ level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
+ for(i=0; i<STD_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ level->yamyam_content[i][x][y] = checkLevelElement(getFile8Bit(file));
+
+ level->amoeba_speed = getFile8Bit(file);
+ level->time_magic_wall = getFile8Bit(file);
+ level->time_wheel = getFile8Bit(file);
+ level->amoeba_content = checkLevelElement(getFile8Bit(file));
+ level->double_speed = (getFile8Bit(file) == 1 ? TRUE : FALSE);
+ level->gravity = (getFile8Bit(file) == 1 ? TRUE : FALSE);
+ level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
+ level->em_slippery_gems = (getFile8Bit(file) == 1 ? TRUE : FALSE);
+
+ level->use_custom_template = (getFile8Bit(file) == 1 ? TRUE : FALSE);
+
+ ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
+
+ return chunk_size;
+}
+
+static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int i;
+
+ for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
+ level->author[i] = getFile8Bit(file);
+ level->author[MAX_LEVEL_NAME_LEN] = 0;
+
+ return chunk_size;
+}
+
+static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int x, y;
+ int chunk_size_expected = level->fieldx * level->fieldy;
+
+ /* Note: "chunk_size" was wrong before version 2.0 when elements are
+ stored with 16-bit encoding (and should be twice as big then).
+ Even worse, playfield data was stored 16-bit when only yamyam content
+ contained 16-bit elements and vice versa. */
+
+ if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
+ chunk_size_expected *= 2;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size);
+ return chunk_size_expected;
+ }
+
+ for(y=0; y<level->fieldy; y++)
+ for(x=0; x<level->fieldx; x++)
+ level->field[x][y] =
+ checkLevelElement(level->encoding_16bit_field ? getFile16BitBE(file) :
+ getFile8Bit(file));
+ return chunk_size;
+}
+
+static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int i, x, y;
+ int header_size = 4;
+ int content_size = MAX_ELEMENT_CONTENTS * 3 * 3;
+ int chunk_size_expected = header_size + content_size;
+
+ /* Note: "chunk_size" was wrong before version 2.0 when elements are
+ stored with 16-bit encoding (and should be twice as big then).
+ Even worse, playfield data was stored 16-bit when only yamyam content
+ contained 16-bit elements and vice versa. */
+
+ if (level->encoding_16bit_field && level->file_version >= FILE_VERSION_2_0)
+ chunk_size_expected += content_size;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size);
+ return chunk_size_expected;
+ }
+
+ getFile8Bit(file);
+ level->num_yamyam_contents = getFile8Bit(file);
+ getFile8Bit(file);
+ getFile8Bit(file);
+
+ /* correct invalid number of content fields -- should never happen */
+ if (level->num_yamyam_contents < 1 ||
+ level->num_yamyam_contents > MAX_ELEMENT_CONTENTS)
+ level->num_yamyam_contents = STD_ELEMENT_CONTENTS;
+
+ for(i=0; i<MAX_ELEMENT_CONTENTS; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ level->yamyam_content[i][x][y] =
+ checkLevelElement(level->encoding_16bit_field ?
+ getFile16BitBE(file) : getFile8Bit(file));
+ return chunk_size;
+}
+
+static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int i, x, y;
+ int element;
+ int num_contents, content_xsize, content_ysize;
+ int content_array[MAX_ELEMENT_CONTENTS][3][3];
+
+ element = checkLevelElement(getFile16BitBE(file));
+ num_contents = getFile8Bit(file);
+ content_xsize = getFile8Bit(file);
+ content_ysize = getFile8Bit(file);
+ ReadUnusedBytesFromFile(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++)
+ content_array[i][x][y] = checkLevelElement(getFile16BitBE(file));
+
+ /* correct invalid number of content fields -- should never happen */
+ if (num_contents < 1 || num_contents > MAX_ELEMENT_CONTENTS)
+ num_contents = STD_ELEMENT_CONTENTS;
+
+ if (element == EL_YAMYAM)
+ {
+ level->num_yamyam_contents = num_contents;
+
+ for(i=0; i<num_contents; i++)
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ level->yamyam_content[i][x][y] = content_array[i][x][y];
+ }
+ else if (element == EL_BD_AMOEBA)
+ {
+ level->amoeba_content = content_array[0][0][0];
+ }
+ else
+ {
+ Error(ERR_WARN, "cannot load content for element '%d'", element);
+ }
+
+ return chunk_size;
+}
+
+static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int num_changed_custom_elements = getFile16BitBE(file);
+ int chunk_size_expected = 2 + num_changed_custom_elements * 6;
+ int i;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size - 2);
+ return chunk_size_expected;
+ }
+
+ for (i=0; i < num_changed_custom_elements; i++)
+ {
+ int element = getFile16BitBE(file);
+ int properties = getFile32BitBE(file);
+
+ if (IS_CUSTOM_ELEMENT(element))
+ Properties[element][EP_BITFIELD_BASE] = properties;
+ else
+ Error(ERR_WARN, "invalid custom element number %d", element);
+ }
+
+ return chunk_size;
+}
+
+static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int num_changed_custom_elements = getFile16BitBE(file);
+ int chunk_size_expected = 2 + num_changed_custom_elements * 4;
+ int i;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size - 2);
+ return chunk_size_expected;
+ }
+
+ for (i=0; i < num_changed_custom_elements; i++)
+ {
+ int element = getFile16BitBE(file);
+ int custom_target_element = getFile16BitBE(file);
+
+ if (IS_CUSTOM_ELEMENT(element))
+ element_info[element].change->target_element = custom_target_element;
+ else
+ Error(ERR_WARN, "invalid custom element number %d", element);
+ }
+
+ return chunk_size;
+}
+
+static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level)
+{
+ int num_changed_custom_elements = getFile16BitBE(file);
+ int chunk_size_expected = LEVEL_CHUNK_CUS3_SIZE(num_changed_custom_elements);
+ int i, j, x, y;
+
+ if (chunk_size_expected != chunk_size)
+ {
+ ReadUnusedBytesFromFile(file, chunk_size - 2);
+ return chunk_size_expected;
+ }
+
+ for (i=0; i < num_changed_custom_elements; i++)
+ {
+ int element = getFile16BitBE(file);
+
+ if (!IS_CUSTOM_ELEMENT(element))
+ {
+ Error(ERR_WARN, "invalid custom element number %d", element);
+
+ element = EL_DEFAULT; /* dummy element used for artwork config */
+ }
+
+ for(j=0; j<MAX_ELEMENT_NAME_LEN; j++)
+ element_info[element].description[j] = getFile8Bit(file);
+ element_info[element].description[MAX_ELEMENT_NAME_LEN] = 0;
+
+ Properties[element][EP_BITFIELD_BASE] = getFile32BitBE(file);
+
+ /* some free bytes for future properties and padding */
+ ReadUnusedBytesFromFile(file, 7);
+
+ element_info[element].use_gfx_element = getFile8Bit(file);
+ element_info[element].gfx_element =
+ checkLevelElement(getFile16BitBE(file));
+
+ element_info[element].collect_score = getFile8Bit(file);
+ element_info[element].collect_count = getFile8Bit(file);
+
+ element_info[element].push_delay_fixed = getFile16BitBE(file);
+ element_info[element].push_delay_random = getFile16BitBE(file);
+ element_info[element].move_delay_fixed = getFile16BitBE(file);
+ element_info[element].move_delay_random = getFile16BitBE(file);
+
+ element_info[element].move_pattern = getFile16BitBE(file);
+ element_info[element].move_direction_initial = getFile8Bit(file);
+ element_info[element].move_stepsize = getFile8Bit(file);
+
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ element_info[element].content[x][y] =
+ checkLevelElement(getFile16BitBE(file));
+
+ element_info[element].change->events = getFile32BitBE(file);
+
+ element_info[element].change->target_element =
+ checkLevelElement(getFile16BitBE(file));
+
+ element_info[element].change->delay_fixed = getFile16BitBE(file);
+ element_info[element].change->delay_random = getFile16BitBE(file);
+ element_info[element].change->delay_frames = getFile16BitBE(file);
+
+ element_info[element].change->trigger_element =
+ checkLevelElement(getFile16BitBE(file));
+
+ element_info[element].change->explode = getFile8Bit(file);
+ element_info[element].change->use_content = getFile8Bit(file);
+ element_info[element].change->only_complete = getFile8Bit(file);
+ element_info[element].change->use_random_change = getFile8Bit(file);
+
+ element_info[element].change->random = getFile8Bit(file);
+ element_info[element].change->power = getFile8Bit(file);
+
+ for(y=0; y<3; y++)
+ for(x=0; x<3; x++)
+ element_info[element].change->content[x][y] =
+ checkLevelElement(getFile16BitBE(file));
+
+ element_info[element].slippery_type = getFile8Bit(file);
+
+ /* some free bytes for future properties and padding */
+ ReadUnusedBytesFromFile(file, LEVEL_CPART_CUS3_UNUSED);
+
+ /* mark that this custom element has been modified */
+ element_info[element].modified_settings = TRUE;
+ }
+
+ return chunk_size;
+}
+
+void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
+{
+ char cookie[MAX_LINE_LEN];
+ char chunk_name[CHUNK_ID_LEN + 1];
+ int chunk_size;
+ FILE *file;
+
+ /* always start with reliable default values */
+ setLevelInfoToDefaults(level);
+
+ if (!(file = fopen(filename, MODE_READ)))
+ {
+ level->no_level_file = TRUE;
+
+ if (level != &level_template)
+ 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 1
+
+static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
+{
+ if (leveldir_current == NULL) /* only when dumping level */
+ return;
+
+ /* determine correct game engine version of current level */
+ if (IS_LEVELCLASS_CONTRIBUTION(leveldir_current) ||
+ IS_LEVELCLASS_USER(leveldir_current))
+ {
+#if 0
+ printf("\n::: This level is private or contributed: '%s'\n", filename);
+#endif
+
+ /* 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
+ {
+#if 0
+ printf("\n::: ALWAYS USE LATEST ENGINE FOR THIS LEVEL: [%d] '%s'\n",
+ leveldir_current->sort_priority, filename);
+#endif
+
+ /* 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;
+ }
+}
+
+static void LoadLevel_InitElements(struct LevelInfo *level, char *filename)
+{
+ int i, j;
+
+ /* map custom element change events that have changed in newer versions
+ (these following values have accidentally changed in version 3.0.1) */
+ if (level->game_version <= VERSION_IDENT(3,0,0))
+ {
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ /* order of checking events to be mapped is important */
+ for (j=CE_BY_OTHER; j >= CE_BY_PLAYER; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 2))
+ {
+ SET_CHANGE_EVENT(element, j - 2, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+
+ /* order of checking events to be mapped is important */
+ for (j=CE_OTHER_GETS_COLLECTED; j >= CE_COLLISION; 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))
+ {
+ 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;
+ }
+ }
+
+ /* initialize element properties for level editor etc. */
+ InitElementPropertiesEngine(level->game_version);
+}
+
+static void LoadLevel_InitPlayfield(struct LevelInfo *level, char *filename)
+{
+ 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++)
+ {
+ int element = level->field[x][y];
+
+ if (level->game_version <= VERSION_IDENT(2,2,0))
+ {
+ /* map game font elements */
+ element = (element == EL_CHAR('[') ? EL_CHAR_AUMLAUT :
+ element == EL_CHAR('\\') ? EL_CHAR_OUMLAUT :
+ element == EL_CHAR(']') ? EL_CHAR_UUMLAUT :
+ element == EL_CHAR('^') ? EL_CHAR_COPYRIGHT : element);
+ }
+
+ if (level->game_version < VERSION_IDENT(3,0,0))
+ {
+ /* map Supaplex gravity tube elements */
+ element = (element == EL_SP_GRAVITY_PORT_LEFT ? EL_SP_PORT_LEFT :
+ element == EL_SP_GRAVITY_PORT_RIGHT ? EL_SP_PORT_RIGHT :
+ element == EL_SP_GRAVITY_PORT_UP ? EL_SP_PORT_UP :
+ element == EL_SP_GRAVITY_PORT_DOWN ? EL_SP_PORT_DOWN :
+ element);
+ }
+
+ 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();
+}
+
+#else
+
+static void LoadLevel_InitLevel(struct LevelInfo *level, char *filename)
+{
+ int i, j, x, y;
+
+ if (leveldir_current == NULL) /* only when dumping level */
+ return;
+
+ /* determine correct game engine version of current level */
+ if (IS_LEVELCLASS_CONTRIBUTION(leveldir_current) ||
+ IS_LEVELCLASS_USER(leveldir_current))
+ {
+#if 0
+ printf("\n::: This level is private or contributed: '%s'\n", filename);
+#endif
+
+ /* 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
+ {
+#if 0
+ printf("\n::: ALWAYS USE LATEST ENGINE FOR THIS LEVEL: [%d] '%s'\n",
+ leveldir_current->sort_priority, filename);
+#endif
+
+ /* 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 that have changed in newer versions */
+ for(y=0; y<level->fieldy; y++)
+ {
+ for(x=0; x<level->fieldx; x++)
+ {
+ int element = level->field[x][y];
+
+ if (level->game_version <= VERSION_IDENT(2,2,0))
+ {
+ /* map game font elements */
+ element = (element == EL_CHAR('[') ? EL_CHAR_AUMLAUT :
+ element == EL_CHAR('\\') ? EL_CHAR_OUMLAUT :
+ element == EL_CHAR(']') ? EL_CHAR_UUMLAUT :
+ element == EL_CHAR('^') ? EL_CHAR_COPYRIGHT : element);
+ }
+
+ if (level->game_version < VERSION_IDENT(3,0,0))
+ {
+ /* map Supaplex gravity tube elements */
+ element = (element == EL_SP_GRAVITY_PORT_LEFT ? EL_SP_PORT_LEFT :
+ element == EL_SP_GRAVITY_PORT_RIGHT ? EL_SP_PORT_RIGHT :
+ element == EL_SP_GRAVITY_PORT_UP ? EL_SP_PORT_UP :
+ element == EL_SP_GRAVITY_PORT_DOWN ? EL_SP_PORT_DOWN :
+ element);
+ }
+
+ level->field[x][y] = element;
+ }
+ }
+
+ /* map custom element change events that have changed in newer versions
+ (these following values have accidentally changed in version 3.0.1) */
+ if (level->game_version <= VERSION_IDENT(3,0,0))
+ {
+ for (i=0; i < NUM_CUSTOM_ELEMENTS; i++)
+ {
+ int element = EL_CUSTOM_START + i;
+
+ /* order of checking events to be mapped is important */
+ for (j=CE_BY_OTHER; j >= CE_BY_PLAYER; j--)
+ {
+ if (HAS_CHANGE_EVENT(element, j - 2))
+ {
+ SET_CHANGE_EVENT(element, j - 2, FALSE);
+ SET_CHANGE_EVENT(element, j, TRUE);
+ }
+ }
+
+ /* order of checking events to be mapped is important */
+ for (j=CE_OTHER_GETS_COLLECTED; j >= CE_COLLISION; 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))
+ {
+ 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;
+ }
+ }
+
+ /* 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);
+}
+
+#endif
+
+void LoadLevelTemplate(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ LoadLevelFromFilename(&level_template, filename);
+
+ LoadLevel_InitVersion(&level, filename);
+ LoadLevel_InitElements(&level, filename);
+
+ ActivateLevelTemplate();
+}
+
+void LoadLevel(int level_nr)
+{
+ char *filename = getLevelFilename(level_nr);
+
+ LoadLevelFromFilename(&level, filename);
+
+ if (level.use_custom_template)
+ LoadLevelTemplate(-1);
+
+#if 1
+ LoadLevel_InitVersion(&level, filename);
+ LoadLevel_InitElements(&level, filename);
+ LoadLevel_InitPlayfield(&level, filename);
+#else
+ LoadLevel_InitLevel(&level, filename);
+#endif
+}
+
+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)