From: Holger Schemel Date: Mon, 16 Dec 2013 23:01:32 +0000 (+0100) Subject: rnd-20131217-1-src X-Git-Tag: 4.0.0.0-rc1~425 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=6e44f03074b9b879f97c4ce873962649f9699d2e;p=rocksndiamonds.git rnd-20131217-1-src --- diff --git a/src/conftime.h b/src/conftime.h index e6c5ef71..21ff7bdc 100644 --- a/src/conftime.h +++ b/src/conftime.h @@ -1 +1 @@ -#define COMPILE_DATE_STRING "2013-12-16 21:45" +#define COMPILE_DATE_STRING "2013-12-16 23:56" diff --git a/src/files.c b/src/files.c index 7237f577..cde49741 100644 --- a/src/files.c +++ b/src/files.c @@ -2451,7 +2451,9 @@ int getMappedElementByVersion(int element, int game_version) return element; } -static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level) +#if 1 + +static int LoadLevel_VERS(File *file, int chunk_size, struct LevelInfo *level) { level->file_version = getFileVersion(file); level->game_version = getFileVersion(file); @@ -2459,7 +2461,7 @@ static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_DATE(File *file, int chunk_size, struct LevelInfo *level) { level->creation_date.year = getFile16BitBE(file); level->creation_date.month = getFile8Bit(file); @@ -2470,7 +2472,7 @@ static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_HEAD(File *file, int chunk_size, struct LevelInfo *level) { int initial_player_stepsize; int initial_player_gravity; @@ -2535,7 +2537,7 @@ static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_NAME(File *file, int chunk_size, struct LevelInfo *level) { int i; @@ -2546,7 +2548,7 @@ static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_AUTH(File *file, int chunk_size, struct LevelInfo *level) { int i; @@ -2557,7 +2559,7 @@ static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_BODY(File *file, int chunk_size, struct LevelInfo *level) { int x, y; int chunk_size_expected = level->fieldx * level->fieldy; @@ -2584,7 +2586,7 @@ static int LoadLevel_BODY(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CONT(File *file, int chunk_size, struct LevelInfo *level) { int i, x, y; int header_size = 4; @@ -2624,7 +2626,7 @@ static int LoadLevel_CONT(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CNT2(File *file, int chunk_size, struct LevelInfo *level) { int i, x, y; int element; @@ -2676,7 +2678,7 @@ static int LoadLevel_CNT2(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CNT3(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CNT3(File *file, int chunk_size, struct LevelInfo *level) { int i; int element; @@ -2710,7 +2712,7 @@ static int LoadLevel_CNT3(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level) +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; @@ -2741,7 +2743,7 @@ static int LoadLevel_CUS1(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level) +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; @@ -2767,7 +2769,7 @@ static int LoadLevel_CUS2(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level) +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); @@ -2858,7 +2860,7 @@ static int LoadLevel_CUS3(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level) { struct ElementInfo *ei; int chunk_size_expected; @@ -3006,7 +3008,7 @@ static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_GRP1(File *file, int chunk_size, struct LevelInfo *level) { struct ElementInfo *ei; struct ElementGroupInfo *group; @@ -3050,7 +3052,7 @@ static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level) return chunk_size; } -static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf, +static int LoadLevel_MicroChunk(File *file, struct LevelFileConfigInfo *conf, int element, int real_element) { int micro_chunk_size = 0; @@ -3190,13 +3192,13 @@ static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf, return micro_chunk_size; } -static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_INFO(File *file, int chunk_size, struct LevelInfo *level) { int real_chunk_size = 0; li = *level; /* copy level data into temporary buffer */ - while (!feof(file)) + while (!checkEndOfFile(file)) { real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1); @@ -3209,13 +3211,13 @@ static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CONF(File *file, int chunk_size, struct LevelInfo *level) { int real_chunk_size = 0; li = *level; /* copy level data into temporary buffer */ - while (!feof(file)) + while (!checkEndOfFile(file)) { int element = getMappedElement(getFile16BitBE(file)); @@ -3231,13 +3233,13 @@ static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_ELEM(File *file, int chunk_size, struct LevelInfo *level) { int real_chunk_size = 0; li = *level; /* copy level data into temporary buffer */ - while (!feof(file)) + while (!checkEndOfFile(file)) { int element = getMappedElement(getFile16BitBE(file)); @@ -3253,13 +3255,13 @@ static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_NOTE(File *file, int chunk_size, struct LevelInfo *level) { int element = getMappedElement(getFile16BitBE(file)); int envelope_nr = element - EL_ENVELOPE_1; int real_chunk_size = 2; - while (!feof(file)) + while (!checkEndOfFile(file)) { real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE, -1, element); @@ -3273,7 +3275,7 @@ static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_CUSX(File *file, int chunk_size, struct LevelInfo *level) { int element = getMappedElement(getFile16BitBE(file)); int real_chunk_size = 2; @@ -3284,7 +3286,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level) xx_ei.num_change_pages = -1; - while (!feof(file)) + while (!checkEndOfFile(file)) { real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base, -1, element); @@ -3318,7 +3320,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level) /* start with reading properties for the first change page */ xx_current_change_page = 0; - while (!feof(file)) + while (!checkEndOfFile(file)) { struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page]; @@ -3340,7 +3342,7 @@ static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level) +static int LoadLevel_GRPX(File *file, int chunk_size, struct LevelInfo *level) { int element = getMappedElement(getFile16BitBE(file)); int real_chunk_size = 2; @@ -3350,7 +3352,7 @@ static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level) xx_ei = *ei; /* copy element data into temporary buffer */ xx_group = *group; /* copy group data into temporary buffer */ - while (!feof(file)) + while (!checkEndOfFile(file)) { real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX, -1, element); @@ -3365,181 +3367,1264 @@ static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level) return real_chunk_size; } -static void LoadLevelFromFileInfo_RND(struct LevelInfo *level, - struct LevelFileInfo *level_file_info, - boolean level_info_only) +#else + +static int LoadLevel_VERS(FILE *file, int chunk_size, struct LevelInfo *level) { - char *filename = level_file_info->filename; - char cookie[MAX_LINE_LEN]; - char chunk_name[CHUNK_ID_LEN + 1]; - int chunk_size; - FILE *file; + level->file_version = getFileVersion(file); + level->game_version = getFileVersion(file); - if (!(file = fopen(filename, MODE_READ))) - { - level->no_valid_file = TRUE; + return chunk_size; +} -#if 1 - if (!level_info_only) - Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); -#else - if (level != &level_template) - Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); -#endif +static int LoadLevel_DATE(FILE *file, int chunk_size, struct LevelInfo *level) +{ + level->creation_date.year = getFile16BitBE(file); + level->creation_date.month = getFile8Bit(file); + level->creation_date.day = getFile8Bit(file); - return; - } + level->creation_date.src = DATE_SRC_LEVELFILE; - getFileChunkBE(file, chunk_name, NULL); - if (strEqual(chunk_name, "RND1")) - { - getFile32BitBE(file); /* not used */ + return chunk_size; +} - getFileChunkBE(file, chunk_name, NULL); - if (!strEqual(chunk_name, "CAVE")) - { - level->no_valid_file = TRUE; +static int LoadLevel_HEAD(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int initial_player_stepsize; + int initial_player_gravity; + int i, x, y; - 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); - if (fgets(&cookie[4], MAX_LINE_LEN - 4, file) == NULL) - cookie[4] = '\0'; - if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n') - cookie[strlen(cookie) - 1] = '\0'; + level->fieldx = getFile8Bit(file); + level->fieldy = getFile8Bit(file); - if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL)) - { - level->no_valid_file = TRUE; + level->time = getFile16BitBE(file); + level->gems_needed = getFile16BitBE(file); - Error(ERR_WARN, "unknown format of level file '%s'", filename); - fclose(file); - return; - } + for (i = 0; i < MAX_LEVEL_NAME_LEN; i++) + level->name[i] = getFile8Bit(file); + level->name[MAX_LEVEL_NAME_LEN] = 0; - if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1) - { - level->no_valid_file = TRUE; + for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++) + level->score[i] = getFile8Bit(file); - Error(ERR_WARN, "unsupported version of level file '%s'", filename); - fclose(file); - return; - } + 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].e[x][y] = getMappedElement(getFile8Bit(file)); - /* pre-2.0 level files have no game version, so use file version here */ - level->game_version = level->file_version; - } + level->amoeba_speed = getFile8Bit(file); + level->time_magic_wall = getFile8Bit(file); + level->time_wheel = getFile8Bit(file); + level->amoeba_content = getMappedElement(getFile8Bit(file)); - if (level->file_version < FILE_VERSION_1_2) - { - /* level files from versions before 1.2.0 without chunk structure */ - LoadLevel_HEAD(file, LEVEL_CHUNK_HEAD_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", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS }, - { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE }, - { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD }, - { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME }, - { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH }, - { "INFO", -1, LoadLevel_INFO }, - { "BODY", -1, LoadLevel_BODY }, - { "CONT", -1, LoadLevel_CONT }, - { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 }, - { "CNT3", -1, LoadLevel_CNT3 }, - { "CUS1", -1, LoadLevel_CUS1 }, - { "CUS2", -1, LoadLevel_CUS2 }, - { "CUS3", -1, LoadLevel_CUS3 }, - { "CUS4", -1, LoadLevel_CUS4 }, - { "GRP1", -1, LoadLevel_GRP1 }, - { "CONF", -1, LoadLevel_CONF }, - { "ELEM", -1, LoadLevel_ELEM }, - { "NOTE", -1, LoadLevel_NOTE }, - { "CUSX", -1, LoadLevel_CUSX }, - { "GRPX", -1, LoadLevel_GRPX }, + initial_player_stepsize = (getFile8Bit(file) == 1 ? STEPSIZE_FAST : + STEPSIZE_NORMAL); - { NULL, 0, NULL } - }; + for (i = 0; i < MAX_PLAYERS; i++) + level->initial_player_stepsize[i] = initial_player_stepsize; - while (getFileChunkBE(file, chunk_name, &chunk_size)) - { - int i = 0; + initial_player_gravity = (getFile8Bit(file) == 1 ? TRUE : FALSE); - while (chunk_info[i].name != NULL && - !strEqual(chunk_name, chunk_info[i].name)) - i++; + for (i = 0; i < MAX_PLAYERS; i++) + level->initial_player_gravity[i] = initial_player_gravity; - 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); + level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->em_slippery_gems = (getFile8Bit(file) == 1 ? TRUE : FALSE); - /* 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); - } - } - } - } + level->use_custom_template = (getFile8Bit(file) == 1 ? TRUE : FALSE); - fclose(file); + level->block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->sp_block_last_field = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->can_move_into_acid_bits = getFile32BitBE(file); + level->dont_collide_with_bits = getFile8Bit(file); + + level->use_spring_bug = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->use_step_counter = (getFile8Bit(file) == 1 ? TRUE : FALSE); + + level->instant_relocation = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->can_pass_to_walkable = (getFile8Bit(file) == 1 ? TRUE : FALSE); + level->grow_into_diggable = (getFile8Bit(file) == 1 ? TRUE : FALSE); + + level->game_engine_type = getFile8Bit(file); + + ReadUnusedBytesFromFile(file, LEVEL_CHUNK_HEAD_UNUSED); + + return chunk_size; } -/* ------------------------------------------------------------------------- */ -/* functions for loading EM level */ -/* ------------------------------------------------------------------------- */ +static int LoadLevel_NAME(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int i; -#if 0 + for (i = 0; i < MAX_LEVEL_NAME_LEN; i++) + level->name[i] = getFile8Bit(file); + level->name[MAX_LEVEL_NAME_LEN] = 0; -static int map_em_element_yam(int element) + return chunk_size; +} + +static int LoadLevel_AUTH(FILE *file, int chunk_size, struct LevelInfo *level) { - switch (element) - { - case 0x00: return EL_EMPTY; - case 0x01: return EL_EMERALD; - case 0x02: return EL_DIAMOND; - case 0x03: return EL_ROCK; - case 0x04: return EL_ROBOT; - case 0x05: return EL_SPACESHIP_UP; - case 0x06: return EL_BOMB; - case 0x07: return EL_BUG_UP; - case 0x08: return EL_AMOEBA_DROP; - case 0x09: return EL_NUT; - case 0x0a: return EL_YAMYAM; - case 0x0b: return EL_QUICKSAND_FULL; - case 0x0c: return EL_SAND; + int i; + + for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++) + level->author[i] = getFile8Bit(file); + level->author[MAX_LEVEL_AUTHOR_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] = + getMappedElement(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].e[x][y] = + getMappedElement(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; +#if 0 + int content_xsize, content_ysize; +#endif + int content_array[MAX_ELEMENT_CONTENTS][3][3]; + + element = getMappedElement(getFile16BitBE(file)); + num_contents = getFile8Bit(file); +#if 1 + getFile8Bit(file); /* content x size (unused) */ + getFile8Bit(file); /* content y size (unused) */ +#else + content_xsize = getFile8Bit(file); + content_ysize = getFile8Bit(file); +#endif + + 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] = getMappedElement(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].e[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_CNT3(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int i; + int element; + int envelope_nr; + int envelope_len; + int chunk_size_expected; + + element = getMappedElement(getFile16BitBE(file)); + if (!IS_ENVELOPE(element)) + element = EL_ENVELOPE_1; + + envelope_nr = element - EL_ENVELOPE_1; + + envelope_len = getFile16BitBE(file); + + level->envelope[envelope_nr].xsize = getFile8Bit(file); + level->envelope[envelope_nr].ysize = getFile8Bit(file); + + ReadUnusedBytesFromFile(file, LEVEL_CHUNK_CNT3_UNUSED); + + chunk_size_expected = LEVEL_CHUNK_CNT3_SIZE(envelope_len); + if (chunk_size_expected != chunk_size) + { + ReadUnusedBytesFromFile(file, chunk_size - LEVEL_CHUNK_CNT3_HEADER); + return chunk_size_expected; + } + + for (i = 0; i < envelope_len; i++) + level->envelope[envelope_nr].text[i] = getFile8Bit(file); + + 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 = getMappedElement(getFile16BitBE(file)); + int properties = getFile32BitBE(file); + + if (IS_CUSTOM_ELEMENT(element)) + element_info[element].properties[EP_BITFIELD_BASE_NR] = properties; + else + Error(ERR_WARN, "invalid custom element number %d", element); + + /* older game versions that wrote level files with CUS1 chunks used + different default push delay values (not yet stored in level file) */ + element_info[element].push_delay_fixed = 2; + element_info[element].push_delay_random = 8; + } + + 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 = getMappedElement(getFile16BitBE(file)); + int custom_target_element = getMappedElement(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 = getMappedElement(getFile16BitBE(file)); + struct ElementInfo *ei = &element_info[element]; + unsigned int event_bits; + + if (!IS_CUSTOM_ELEMENT(element)) + { + Error(ERR_WARN, "invalid custom element number %d", element); + + element = EL_INTERNAL_DUMMY; + } + + for (j = 0; j < MAX_ELEMENT_NAME_LEN; j++) + ei->description[j] = getFile8Bit(file); + ei->description[MAX_ELEMENT_NAME_LEN] = 0; + + ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file); + + /* some free bytes for future properties and padding */ + ReadUnusedBytesFromFile(file, 7); + + ei->use_gfx_element = getFile8Bit(file); + ei->gfx_element_initial = getMappedElement(getFile16BitBE(file)); + + ei->collect_score_initial = getFile8Bit(file); + ei->collect_count_initial = getFile8Bit(file); + + ei->push_delay_fixed = getFile16BitBE(file); + ei->push_delay_random = getFile16BitBE(file); + ei->move_delay_fixed = getFile16BitBE(file); + ei->move_delay_random = getFile16BitBE(file); + + ei->move_pattern = getFile16BitBE(file); + ei->move_direction_initial = getFile8Bit(file); + ei->move_stepsize = getFile8Bit(file); + + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + ei->content.e[x][y] = getMappedElement(getFile16BitBE(file)); + + event_bits = getFile32BitBE(file); + for (j = 0; j < NUM_CHANGE_EVENTS; j++) + if (event_bits & (1 << j)) + ei->change->has_event[j] = TRUE; + + ei->change->target_element = getMappedElement(getFile16BitBE(file)); + + ei->change->delay_fixed = getFile16BitBE(file); + ei->change->delay_random = getFile16BitBE(file); + ei->change->delay_frames = getFile16BitBE(file); + + ei->change->initial_trigger_element= getMappedElement(getFile16BitBE(file)); + + ei->change->explode = getFile8Bit(file); + ei->change->use_target_content = getFile8Bit(file); + ei->change->only_if_complete = getFile8Bit(file); + ei->change->use_random_replace = getFile8Bit(file); + + ei->change->random_percentage = getFile8Bit(file); + ei->change->replace_when = getFile8Bit(file); + + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + ei->change->target_content.e[x][y] = + getMappedElement(getFile16BitBE(file)); + + ei->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 */ + ei->modified_settings = TRUE; + } + + return chunk_size; +} + +static int LoadLevel_CUS4(FILE *file, int chunk_size, struct LevelInfo *level) +{ + struct ElementInfo *ei; + int chunk_size_expected; + int element; + int i, j, x, y; + + /* ---------- custom element base property values (96 bytes) ------------- */ + + element = getMappedElement(getFile16BitBE(file)); + + if (!IS_CUSTOM_ELEMENT(element)) + { + Error(ERR_WARN, "invalid custom element number %d", element); + + ReadUnusedBytesFromFile(file, chunk_size - 2); + return chunk_size; + } + + ei = &element_info[element]; + + for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++) + ei->description[i] = getFile8Bit(file); + ei->description[MAX_ELEMENT_NAME_LEN] = 0; + + ei->properties[EP_BITFIELD_BASE_NR] = getFile32BitBE(file); + + ReadUnusedBytesFromFile(file, 4); /* reserved for more base properties */ + + ei->num_change_pages = getFile8Bit(file); + + chunk_size_expected = LEVEL_CHUNK_CUS4_SIZE(ei->num_change_pages); + if (chunk_size_expected != chunk_size) + { + ReadUnusedBytesFromFile(file, chunk_size - 43); + return chunk_size_expected; + } + + ei->ce_value_fixed_initial = getFile16BitBE(file); + ei->ce_value_random_initial = getFile16BitBE(file); + ei->use_last_ce_value = getFile8Bit(file); + + ei->use_gfx_element = getFile8Bit(file); + ei->gfx_element_initial = getMappedElement(getFile16BitBE(file)); + + ei->collect_score_initial = getFile8Bit(file); + ei->collect_count_initial = getFile8Bit(file); + + ei->drop_delay_fixed = getFile8Bit(file); + ei->push_delay_fixed = getFile8Bit(file); + ei->drop_delay_random = getFile8Bit(file); + ei->push_delay_random = getFile8Bit(file); + ei->move_delay_fixed = getFile16BitBE(file); + ei->move_delay_random = getFile16BitBE(file); + + /* bits 0 - 15 of "move_pattern" ... */ + ei->move_pattern = getFile16BitBE(file); + ei->move_direction_initial = getFile8Bit(file); + ei->move_stepsize = getFile8Bit(file); + + ei->slippery_type = getFile8Bit(file); + + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + ei->content.e[x][y] = getMappedElement(getFile16BitBE(file)); + + ei->move_enter_element = getMappedElement(getFile16BitBE(file)); + ei->move_leave_element = getMappedElement(getFile16BitBE(file)); + ei->move_leave_type = getFile8Bit(file); + + /* ... bits 16 - 31 of "move_pattern" (not nice, but downward compatible) */ + ei->move_pattern |= (getFile16BitBE(file) << 16); + + ei->access_direction = getFile8Bit(file); + + ei->explosion_delay = getFile8Bit(file); + ei->ignition_delay = getFile8Bit(file); + ei->explosion_type = getFile8Bit(file); + + /* some free bytes for future custom property values and padding */ + ReadUnusedBytesFromFile(file, 1); + + /* ---------- change page property values (48 bytes) --------------------- */ + + setElementChangePages(ei, ei->num_change_pages); + + for (i = 0; i < ei->num_change_pages; i++) + { + struct ElementChangeInfo *change = &ei->change_page[i]; + unsigned int event_bits; + + /* always start with reliable default values */ + setElementChangeInfoToDefaults(change); + + /* bits 0 - 31 of "has_event[]" ... */ + event_bits = getFile32BitBE(file); + for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++) + if (event_bits & (1 << j)) + change->has_event[j] = TRUE; + + change->target_element = getMappedElement(getFile16BitBE(file)); + + change->delay_fixed = getFile16BitBE(file); + change->delay_random = getFile16BitBE(file); + change->delay_frames = getFile16BitBE(file); + + change->initial_trigger_element = getMappedElement(getFile16BitBE(file)); + + change->explode = getFile8Bit(file); + change->use_target_content = getFile8Bit(file); + change->only_if_complete = getFile8Bit(file); + change->use_random_replace = getFile8Bit(file); + + change->random_percentage = getFile8Bit(file); + change->replace_when = getFile8Bit(file); + + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + change->target_content.e[x][y]= getMappedElement(getFile16BitBE(file)); + + change->can_change = getFile8Bit(file); + + change->trigger_side = getFile8Bit(file); + + change->trigger_player = getFile8Bit(file); + change->trigger_page = getFile8Bit(file); + + change->trigger_page = (change->trigger_page == CH_PAGE_ANY_FILE ? + CH_PAGE_ANY : (1 << change->trigger_page)); + + change->has_action = getFile8Bit(file); + change->action_type = getFile8Bit(file); + change->action_mode = getFile8Bit(file); + change->action_arg = getFile16BitBE(file); + + /* ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible) */ + event_bits = getFile8Bit(file); + for (j = 32; j < NUM_CHANGE_EVENTS; j++) + if (event_bits & (1 << (j - 32))) + change->has_event[j] = TRUE; + } + + /* mark this custom element as modified */ + ei->modified_settings = TRUE; + + return chunk_size; +} + +static int LoadLevel_GRP1(FILE *file, int chunk_size, struct LevelInfo *level) +{ + struct ElementInfo *ei; + struct ElementGroupInfo *group; + int element; + int i; + + element = getMappedElement(getFile16BitBE(file)); + + if (!IS_GROUP_ELEMENT(element)) + { + Error(ERR_WARN, "invalid group element number %d", element); + + ReadUnusedBytesFromFile(file, chunk_size - 2); + return chunk_size; + } + + ei = &element_info[element]; + + for (i = 0; i < MAX_ELEMENT_NAME_LEN; i++) + ei->description[i] = getFile8Bit(file); + ei->description[MAX_ELEMENT_NAME_LEN] = 0; + + group = element_info[element].group; + + group->num_elements = getFile8Bit(file); + + ei->use_gfx_element = getFile8Bit(file); + ei->gfx_element_initial = getMappedElement(getFile16BitBE(file)); + + group->choice_mode = getFile8Bit(file); + + /* some free bytes for future values and padding */ + ReadUnusedBytesFromFile(file, 3); + + for (i = 0; i < MAX_ELEMENTS_IN_GROUP; i++) + group->element[i] = getMappedElement(getFile16BitBE(file)); + + /* mark this group element as modified */ + element_info[element].modified_settings = TRUE; + + return chunk_size; +} + +static int LoadLevel_MicroChunk(FILE *file, struct LevelFileConfigInfo *conf, + int element, int real_element) +{ + int micro_chunk_size = 0; + int conf_type = getFile8Bit(file); + int byte_mask = conf_type & CONF_MASK_BYTES; + boolean element_found = FALSE; + int i; + + micro_chunk_size += 1; + + if (byte_mask == CONF_MASK_MULTI_BYTES) + { + int num_bytes = getFile16BitBE(file); + byte *buffer = checked_malloc(num_bytes); + + ReadBytesFromFile(file, buffer, num_bytes); + + for (i = 0; conf[i].data_type != -1; i++) + { + if (conf[i].element == element && + conf[i].conf_type == conf_type) + { + int data_type = conf[i].data_type; + int num_entities = num_bytes / CONF_ENTITY_NUM_BYTES(data_type); + int max_num_entities = conf[i].max_num_entities; + + if (num_entities > max_num_entities) + { + Error(ERR_WARN, + "truncating number of entities for element %d from %d to %d", + element, num_entities, max_num_entities); + + num_entities = max_num_entities; + } + + if (num_entities == 0 && (data_type == TYPE_ELEMENT_LIST || + data_type == TYPE_CONTENT_LIST)) + { + /* for element and content lists, zero entities are not allowed */ + Error(ERR_WARN, "found empty list of entities for element %d", + element); + + /* do not set "num_entities" here to prevent reading behind buffer */ + + *(int *)(conf[i].num_entities) = 1; /* at least one is required */ + } + else + { + *(int *)(conf[i].num_entities) = num_entities; + } + + element_found = TRUE; + + if (data_type == TYPE_STRING) + { + char *string = (char *)(conf[i].value); + int j; + + for (j = 0; j < max_num_entities; j++) + string[j] = (j < num_entities ? buffer[j] : '\0'); + } + else if (data_type == TYPE_ELEMENT_LIST) + { + int *element_array = (int *)(conf[i].value); + int j; + + for (j = 0; j < num_entities; j++) + element_array[j] = + getMappedElement(CONF_ELEMENTS_ELEMENT(buffer, j)); + } + else if (data_type == TYPE_CONTENT_LIST) + { + struct Content *content= (struct Content *)(conf[i].value); + int c, x, y; + + for (c = 0; c < num_entities; c++) + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + content[c].e[x][y] = + getMappedElement(CONF_CONTENTS_ELEMENT(buffer, c, x, y)); + } + else + element_found = FALSE; + + break; + } + } + + checked_free(buffer); + + micro_chunk_size += 2 + num_bytes; + } + else /* constant size configuration data (1, 2 or 4 bytes) */ + { + int value = (byte_mask == CONF_MASK_1_BYTE ? getFile8Bit (file) : + byte_mask == CONF_MASK_2_BYTE ? getFile16BitBE(file) : + byte_mask == CONF_MASK_4_BYTE ? getFile32BitBE(file) : 0); + + for (i = 0; conf[i].data_type != -1; i++) + { + if (conf[i].element == element && + conf[i].conf_type == conf_type) + { + int data_type = conf[i].data_type; + + if (data_type == TYPE_ELEMENT) + value = getMappedElement(value); + + if (data_type == TYPE_BOOLEAN) + *(boolean *)(conf[i].value) = value; + else + *(int *) (conf[i].value) = value; + + element_found = TRUE; + + break; + } + } + + micro_chunk_size += CONF_VALUE_NUM_BYTES(byte_mask); + } + + if (!element_found) + { + char *error_conf_chunk_bytes = + (byte_mask == CONF_MASK_1_BYTE ? "CONF_VALUE_8_BIT" : + byte_mask == CONF_MASK_2_BYTE ? "CONF_VALUE_16_BIT" : + byte_mask == CONF_MASK_4_BYTE ? "CONF_VALUE_32_BIT" :"CONF_VALUE_BYTES"); + int error_conf_chunk_token = conf_type & CONF_MASK_TOKEN; + int error_element = real_element; + + Error(ERR_WARN, "cannot load micro chunk '%s(%d)' value for element %d ['%s']", + error_conf_chunk_bytes, error_conf_chunk_token, + error_element, EL_NAME(error_element)); + } + + return micro_chunk_size; +} + +static int LoadLevel_INFO(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int real_chunk_size = 0; + + li = *level; /* copy level data into temporary buffer */ + + while (!feof(file)) + { + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_INFO, -1, -1); + + if (real_chunk_size >= chunk_size) + break; + } + + *level = li; /* copy temporary buffer back to level data */ + + return real_chunk_size; +} + +static int LoadLevel_CONF(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int real_chunk_size = 0; + + li = *level; /* copy level data into temporary buffer */ + + while (!feof(file)) + { + int element = getMappedElement(getFile16BitBE(file)); + + real_chunk_size += 2; + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CONF, + element, element); + if (real_chunk_size >= chunk_size) + break; + } + + *level = li; /* copy temporary buffer back to level data */ + + return real_chunk_size; +} + +static int LoadLevel_ELEM(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int real_chunk_size = 0; + + li = *level; /* copy level data into temporary buffer */ + + while (!feof(file)) + { + int element = getMappedElement(getFile16BitBE(file)); + + real_chunk_size += 2; + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_ELEM, + element, element); + if (real_chunk_size >= chunk_size) + break; + } + + *level = li; /* copy temporary buffer back to level data */ + + return real_chunk_size; +} + +static int LoadLevel_NOTE(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int element = getMappedElement(getFile16BitBE(file)); + int envelope_nr = element - EL_ENVELOPE_1; + int real_chunk_size = 2; + + while (!feof(file)) + { + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_NOTE, + -1, element); + + if (real_chunk_size >= chunk_size) + break; + } + + level->envelope[envelope_nr] = xx_envelope; + + return real_chunk_size; +} + +static int LoadLevel_CUSX(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int element = getMappedElement(getFile16BitBE(file)); + int real_chunk_size = 2; + struct ElementInfo *ei = &element_info[element]; + int i; + + xx_ei = *ei; /* copy element data into temporary buffer */ + + xx_ei.num_change_pages = -1; + + while (!feof(file)) + { + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_base, + -1, element); + if (xx_ei.num_change_pages != -1) + break; + + if (real_chunk_size >= chunk_size) + break; + } + + *ei = xx_ei; + + if (ei->num_change_pages == -1) + { + Error(ERR_WARN, "LoadLevel_CUSX(): missing 'num_change_pages' for '%s'", + EL_NAME(element)); + + ei->num_change_pages = 1; + + setElementChangePages(ei, 1); + setElementChangeInfoToDefaults(ei->change); + + return real_chunk_size; + } + + /* initialize number of change pages stored for this custom element */ + setElementChangePages(ei, ei->num_change_pages); + for (i = 0; i < ei->num_change_pages; i++) + setElementChangeInfoToDefaults(&ei->change_page[i]); + + /* start with reading properties for the first change page */ + xx_current_change_page = 0; + + while (!feof(file)) + { + struct ElementChangeInfo *change = &ei->change_page[xx_current_change_page]; + + xx_change = *change; /* copy change data into temporary buffer */ + + resetEventBits(); /* reset bits; change page might have changed */ + + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_CUSX_change, + -1, element); + + *change = xx_change; + + setEventFlagsFromEventBits(change); + + if (real_chunk_size >= chunk_size) + break; + } + + return real_chunk_size; +} + +static int LoadLevel_GRPX(FILE *file, int chunk_size, struct LevelInfo *level) +{ + int element = getMappedElement(getFile16BitBE(file)); + int real_chunk_size = 2; + struct ElementInfo *ei = &element_info[element]; + struct ElementGroupInfo *group = ei->group; + + xx_ei = *ei; /* copy element data into temporary buffer */ + xx_group = *group; /* copy group data into temporary buffer */ + + while (!feof(file)) + { + real_chunk_size += LoadLevel_MicroChunk(file, chunk_config_GRPX, + -1, element); + + if (real_chunk_size >= chunk_size) + break; + } + + *ei = xx_ei; + *group = xx_group; + + return real_chunk_size; +} + +#endif + +#if 1 + +static void LoadLevelFromFileInfo_RND(struct LevelInfo *level, + struct LevelFileInfo *level_file_info, + boolean level_info_only) +{ + char *filename = level_file_info->filename; + char cookie[MAX_LINE_LEN]; + char chunk_name[CHUNK_ID_LEN + 1]; + int chunk_size; + File *file; + + if (!(file = openFile(filename, MODE_READ))) + { + level->no_valid_file = TRUE; + +#if 1 + if (!level_info_only) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); +#else + if (level != &level_template) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); +#endif + + return; + } + + getFileChunkBE(file, chunk_name, NULL); + if (strEqual(chunk_name, "RND1")) + { + getFile32BitBE(file); /* not used */ + + getFileChunkBE(file, chunk_name, NULL); + if (!strEqual(chunk_name, "CAVE")) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "unknown format of level file '%s'", filename); + + closeFile(file); + + return; + } + } + else /* check for pre-2.0 file format with cookie string */ + { + strcpy(cookie, chunk_name); + if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL) + cookie[4] = '\0'; + if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; + + if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL)) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "unknown format of level file '%s'", filename); + + closeFile(file); + + return; + } + + if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "unsupported version of level file '%s'", filename); + + closeFile(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_CHUNK_HEAD_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", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS }, + { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE }, + { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD }, + { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME }, + { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH }, + { "INFO", -1, LoadLevel_INFO }, + { "BODY", -1, LoadLevel_BODY }, + { "CONT", -1, LoadLevel_CONT }, + { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 }, + { "CNT3", -1, LoadLevel_CNT3 }, + { "CUS1", -1, LoadLevel_CUS1 }, + { "CUS2", -1, LoadLevel_CUS2 }, + { "CUS3", -1, LoadLevel_CUS3 }, + { "CUS4", -1, LoadLevel_CUS4 }, + { "GRP1", -1, LoadLevel_GRP1 }, + { "CONF", -1, LoadLevel_CONF }, + { "ELEM", -1, LoadLevel_ELEM }, + { "NOTE", -1, LoadLevel_NOTE }, + { "CUSX", -1, LoadLevel_CUSX }, + { "GRPX", -1, LoadLevel_GRPX }, + + { NULL, 0, NULL } + }; + + while (getFileChunkBE(file, chunk_name, &chunk_size)) + { + int i = 0; + + while (chunk_info[i].name != NULL && + !strEqual(chunk_name, chunk_info[i].name)) + 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); + } + } + } + } + + closeFile(file); +} + +#else + +static void LoadLevelFromFileInfo_RND(struct LevelInfo *level, + struct LevelFileInfo *level_file_info, + boolean level_info_only) +{ + char *filename = level_file_info->filename; + char cookie[MAX_LINE_LEN]; + char chunk_name[CHUNK_ID_LEN + 1]; + int chunk_size; + FILE *file; + + if (!(file = fopen(filename, MODE_READ))) + { + level->no_valid_file = TRUE; + +#if 1 + if (!level_info_only) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); +#else + if (level != &level_template) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); +#endif + + return; + } + + getFileChunkBE(file, chunk_name, NULL); + if (strEqual(chunk_name, "RND1")) + { + getFile32BitBE(file); /* not used */ + + getFileChunkBE(file, chunk_name, NULL); + if (!strEqual(chunk_name, "CAVE")) + { + level->no_valid_file = TRUE; + + 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); + if (fgets(&cookie[4], MAX_LINE_LEN - 4, file) == NULL) + cookie[4] = '\0'; + if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; + + if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL)) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "unknown format of level file '%s'", filename); + fclose(file); + return; + } + + if ((level->file_version = getFileVersionFromCookieString(cookie)) == -1) + { + level->no_valid_file = TRUE; + + 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_CHUNK_HEAD_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", LEVEL_CHUNK_VERS_SIZE, LoadLevel_VERS }, + { "DATE", LEVEL_CHUNK_DATE_SIZE, LoadLevel_DATE }, + { "HEAD", LEVEL_CHUNK_HEAD_SIZE, LoadLevel_HEAD }, + { "NAME", LEVEL_CHUNK_NAME_SIZE, LoadLevel_NAME }, + { "AUTH", LEVEL_CHUNK_AUTH_SIZE, LoadLevel_AUTH }, + { "INFO", -1, LoadLevel_INFO }, + { "BODY", -1, LoadLevel_BODY }, + { "CONT", -1, LoadLevel_CONT }, + { "CNT2", LEVEL_CHUNK_CNT2_SIZE, LoadLevel_CNT2 }, + { "CNT3", -1, LoadLevel_CNT3 }, + { "CUS1", -1, LoadLevel_CUS1 }, + { "CUS2", -1, LoadLevel_CUS2 }, + { "CUS3", -1, LoadLevel_CUS3 }, + { "CUS4", -1, LoadLevel_CUS4 }, + { "GRP1", -1, LoadLevel_GRP1 }, + { "CONF", -1, LoadLevel_CONF }, + { "ELEM", -1, LoadLevel_ELEM }, + { "NOTE", -1, LoadLevel_NOTE }, + { "CUSX", -1, LoadLevel_CUSX }, + { "GRPX", -1, LoadLevel_GRPX }, + + { NULL, 0, NULL } + }; + + while (getFileChunkBE(file, chunk_name, &chunk_size)) + { + int i = 0; + + while (chunk_info[i].name != NULL && + !strEqual(chunk_name, chunk_info[i].name)) + 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); +} + +#endif + + +/* ------------------------------------------------------------------------- */ +/* functions for loading EM level */ +/* ------------------------------------------------------------------------- */ + +#if 0 + +static int map_em_element_yam(int element) +{ + switch (element) + { + case 0x00: return EL_EMPTY; + case 0x01: return EL_EMERALD; + case 0x02: return EL_DIAMOND; + case 0x03: return EL_ROCK; + case 0x04: return EL_ROBOT; + case 0x05: return EL_SPACESHIP_UP; + case 0x06: return EL_BOMB; + case 0x07: return EL_BUG_UP; + case 0x08: return EL_AMOEBA_DROP; + case 0x09: return EL_NUT; + case 0x0a: return EL_YAMYAM; + case 0x0b: return EL_QUICKSAND_FULL; + case 0x0c: return EL_SAND; case 0x0d: return EL_WALL_SLIPPERY; case 0x0e: return EL_STEELWALL; case 0x0f: return EL_WALL; @@ -5569,688 +6654,1259 @@ int getMappedElement_DC(int element) element = EL_STEEL_CHAR_F; break; - case 0x161b: /* (blue steel) */ + case 0x161b: /* (blue steel) */ + element = EL_STEEL_CHAR_G; + break; + + case 0x161c: /* (blue steel) */ + element = EL_STEEL_CHAR_H; + break; + + case 0x161d: /* (blue steel) */ + element = EL_STEEL_CHAR_I; + break; + + case 0x161e: /* (blue steel) */ + element = EL_STEEL_CHAR_J; + break; + + case 0x161f: /* (blue steel) */ + element = EL_STEEL_CHAR_K; + break; + + case 0x1620: /* (blue steel) */ + element = EL_STEEL_CHAR_L; + break; + + case 0x1621: /* (blue steel) */ + element = EL_STEEL_CHAR_M; + break; + + case 0x1622: /* (blue steel) */ + element = EL_STEEL_CHAR_N; + break; + + case 0x1623: /* (blue steel) */ + element = EL_STEEL_CHAR_O; + break; + + case 0x1624: /* (blue steel) */ + element = EL_STEEL_CHAR_P; + break; + + case 0x1625: /* (blue steel) */ + element = EL_STEEL_CHAR_Q; + break; + + case 0x1626: /* (blue steel) */ + element = EL_STEEL_CHAR_R; + break; + + case 0x1627: /* (blue steel) */ + element = EL_STEEL_CHAR_S; + break; + + case 0x1628: /* (blue steel) */ + element = EL_STEEL_CHAR_T; + break; + + case 0x1629: /* (blue steel) */ + element = EL_STEEL_CHAR_U; + break; + + case 0x162a: /* (blue steel) */ + element = EL_STEEL_CHAR_V; + break; + + case 0x162b: /* (blue steel) */ + element = EL_STEEL_CHAR_W; + break; + + case 0x162c: /* (blue steel) */ + element = EL_STEEL_CHAR_X; + break; + + case 0x162d: /* (blue steel) */ + element = EL_STEEL_CHAR_Y; + break; + + case 0x162e: /* (blue steel) */ + element = EL_STEEL_CHAR_Z; + break; + + case 0x162f: /* (blue steel) */ + element = EL_STEEL_CHAR_AUMLAUT; + break; + + case 0x1630: /* (blue steel) */ + element = EL_STEEL_CHAR_OUMLAUT; + break; + + case 0x1631: /* (blue steel) */ + element = EL_STEEL_CHAR_UUMLAUT; + break; + + case 0x1632: /* (blue steel) */ + element = EL_STEEL_CHAR_0; + break; + + case 0x1633: /* (blue steel) */ + element = EL_STEEL_CHAR_1; + break; + + case 0x1634: /* (blue steel) */ + element = EL_STEEL_CHAR_2; + break; + + case 0x1635: /* (blue steel) */ + element = EL_STEEL_CHAR_3; + break; + + case 0x1636: /* (blue steel) */ + element = EL_STEEL_CHAR_4; + break; + + case 0x1637: /* (blue steel) */ + element = EL_STEEL_CHAR_5; + break; + + case 0x1638: /* (blue steel) */ + element = EL_STEEL_CHAR_6; + break; + + case 0x1639: /* (blue steel) */ + element = EL_STEEL_CHAR_7; + break; + + case 0x163a: /* (blue steel) */ + element = EL_STEEL_CHAR_8; + break; + + case 0x163b: /* (blue steel) */ + element = EL_STEEL_CHAR_9; + break; + + case 0x163c: /* (blue steel) */ + element = EL_STEEL_CHAR_PERIOD; + break; + + case 0x163d: /* (blue steel) */ + element = EL_STEEL_CHAR_EXCLAM; + break; + + case 0x163e: /* (blue steel) */ + element = EL_STEEL_CHAR_COLON; + break; + + case 0x163f: /* (blue steel) */ + element = EL_STEEL_CHAR_LESS; + break; + + case 0x1640: /* (blue steel) */ + element = EL_STEEL_CHAR_GREATER; + break; + + case 0x1641: /* (blue steel) */ + element = EL_STEEL_CHAR_QUESTION; + break; + + case 0x1642: /* (blue steel) */ + element = EL_STEEL_CHAR_COPYRIGHT; + break; + + case 0x1643: /* (blue steel) */ + element = EL_STEEL_CHAR_UP; + break; + + case 0x1644: /* (blue steel) */ + element = EL_STEEL_CHAR_DOWN; + break; + + case 0x1645: /* (blue steel) */ + element = EL_STEEL_CHAR_BUTTON; + break; + + case 0x1646: /* (blue steel) */ + element = EL_STEEL_CHAR_PLUS; + break; + + case 0x1647: /* (blue steel) */ + element = EL_STEEL_CHAR_MINUS; + break; + + case 0x1648: /* (blue steel) */ + element = EL_STEEL_CHAR_APOSTROPHE; + break; + + case 0x1649: /* (blue steel) */ + element = EL_STEEL_CHAR_PARENLEFT; + break; + + case 0x164a: /* (blue steel) */ + element = EL_STEEL_CHAR_PARENRIGHT; + break; + + case 0x164b: /* (green steel) */ + element = EL_STEEL_CHAR_A; + break; + + case 0x164c: /* (green steel) */ + element = EL_STEEL_CHAR_B; + break; + + case 0x164d: /* (green steel) */ + element = EL_STEEL_CHAR_C; + break; + + case 0x164e: /* (green steel) */ + element = EL_STEEL_CHAR_D; + break; + + case 0x164f: /* (green steel) */ + element = EL_STEEL_CHAR_E; + break; + + case 0x1650: /* (green steel) */ + element = EL_STEEL_CHAR_F; + break; + + case 0x1651: /* (green steel) */ element = EL_STEEL_CHAR_G; break; - case 0x161c: /* (blue steel) */ + case 0x1652: /* (green steel) */ element = EL_STEEL_CHAR_H; break; - case 0x161d: /* (blue steel) */ + case 0x1653: /* (green steel) */ element = EL_STEEL_CHAR_I; break; - case 0x161e: /* (blue steel) */ + case 0x1654: /* (green steel) */ element = EL_STEEL_CHAR_J; break; - case 0x161f: /* (blue steel) */ + case 0x1655: /* (green steel) */ element = EL_STEEL_CHAR_K; break; - case 0x1620: /* (blue steel) */ + case 0x1656: /* (green steel) */ element = EL_STEEL_CHAR_L; break; - case 0x1621: /* (blue steel) */ + case 0x1657: /* (green steel) */ element = EL_STEEL_CHAR_M; break; - case 0x1622: /* (blue steel) */ + case 0x1658: /* (green steel) */ element = EL_STEEL_CHAR_N; break; - case 0x1623: /* (blue steel) */ + case 0x1659: /* (green steel) */ element = EL_STEEL_CHAR_O; break; - case 0x1624: /* (blue steel) */ + case 0x165a: /* (green steel) */ element = EL_STEEL_CHAR_P; break; - case 0x1625: /* (blue steel) */ + case 0x165b: /* (green steel) */ element = EL_STEEL_CHAR_Q; break; - case 0x1626: /* (blue steel) */ + case 0x165c: /* (green steel) */ element = EL_STEEL_CHAR_R; break; - case 0x1627: /* (blue steel) */ + case 0x165d: /* (green steel) */ element = EL_STEEL_CHAR_S; break; - case 0x1628: /* (blue steel) */ + case 0x165e: /* (green steel) */ element = EL_STEEL_CHAR_T; break; - case 0x1629: /* (blue steel) */ + case 0x165f: /* (green steel) */ element = EL_STEEL_CHAR_U; break; - case 0x162a: /* (blue steel) */ + case 0x1660: /* (green steel) */ element = EL_STEEL_CHAR_V; break; - case 0x162b: /* (blue steel) */ + case 0x1661: /* (green steel) */ element = EL_STEEL_CHAR_W; break; - case 0x162c: /* (blue steel) */ + case 0x1662: /* (green steel) */ element = EL_STEEL_CHAR_X; break; - case 0x162d: /* (blue steel) */ + case 0x1663: /* (green steel) */ element = EL_STEEL_CHAR_Y; break; - case 0x162e: /* (blue steel) */ + case 0x1664: /* (green steel) */ element = EL_STEEL_CHAR_Z; break; - case 0x162f: /* (blue steel) */ + case 0x1665: /* (green steel) */ element = EL_STEEL_CHAR_AUMLAUT; break; - case 0x1630: /* (blue steel) */ + case 0x1666: /* (green steel) */ element = EL_STEEL_CHAR_OUMLAUT; break; - case 0x1631: /* (blue steel) */ + case 0x1667: /* (green steel) */ element = EL_STEEL_CHAR_UUMLAUT; break; - case 0x1632: /* (blue steel) */ + case 0x1668: /* (green steel) */ element = EL_STEEL_CHAR_0; break; - case 0x1633: /* (blue steel) */ + case 0x1669: /* (green steel) */ element = EL_STEEL_CHAR_1; break; - case 0x1634: /* (blue steel) */ + case 0x166a: /* (green steel) */ element = EL_STEEL_CHAR_2; break; - case 0x1635: /* (blue steel) */ + case 0x166b: /* (green steel) */ element = EL_STEEL_CHAR_3; break; - case 0x1636: /* (blue steel) */ + case 0x166c: /* (green steel) */ element = EL_STEEL_CHAR_4; break; - case 0x1637: /* (blue steel) */ + case 0x166d: /* (green steel) */ element = EL_STEEL_CHAR_5; break; - case 0x1638: /* (blue steel) */ + case 0x166e: /* (green steel) */ element = EL_STEEL_CHAR_6; break; - case 0x1639: /* (blue steel) */ + case 0x166f: /* (green steel) */ element = EL_STEEL_CHAR_7; break; - case 0x163a: /* (blue steel) */ + case 0x1670: /* (green steel) */ element = EL_STEEL_CHAR_8; break; - case 0x163b: /* (blue steel) */ + case 0x1671: /* (green steel) */ element = EL_STEEL_CHAR_9; break; - case 0x163c: /* (blue steel) */ + case 0x1672: /* (green steel) */ element = EL_STEEL_CHAR_PERIOD; break; - case 0x163d: /* (blue steel) */ + case 0x1673: /* (green steel) */ element = EL_STEEL_CHAR_EXCLAM; break; - case 0x163e: /* (blue steel) */ + case 0x1674: /* (green steel) */ element = EL_STEEL_CHAR_COLON; break; - case 0x163f: /* (blue steel) */ + case 0x1675: /* (green steel) */ element = EL_STEEL_CHAR_LESS; break; - case 0x1640: /* (blue steel) */ + case 0x1676: /* (green steel) */ element = EL_STEEL_CHAR_GREATER; break; - case 0x1641: /* (blue steel) */ + case 0x1677: /* (green steel) */ element = EL_STEEL_CHAR_QUESTION; break; - case 0x1642: /* (blue steel) */ + case 0x1678: /* (green steel) */ element = EL_STEEL_CHAR_COPYRIGHT; break; - case 0x1643: /* (blue steel) */ + case 0x1679: /* (green steel) */ element = EL_STEEL_CHAR_UP; break; - case 0x1644: /* (blue steel) */ + case 0x167a: /* (green steel) */ element = EL_STEEL_CHAR_DOWN; break; - case 0x1645: /* (blue steel) */ + case 0x167b: /* (green steel) */ element = EL_STEEL_CHAR_BUTTON; break; - case 0x1646: /* (blue steel) */ + case 0x167c: /* (green steel) */ element = EL_STEEL_CHAR_PLUS; break; - case 0x1647: /* (blue steel) */ + case 0x167d: /* (green steel) */ element = EL_STEEL_CHAR_MINUS; break; - case 0x1648: /* (blue steel) */ + case 0x167e: /* (green steel) */ element = EL_STEEL_CHAR_APOSTROPHE; break; - case 0x1649: /* (blue steel) */ + case 0x167f: /* (green steel) */ element = EL_STEEL_CHAR_PARENLEFT; break; - case 0x164a: /* (blue steel) */ + case 0x1680: /* (green steel) */ element = EL_STEEL_CHAR_PARENRIGHT; break; - case 0x164b: /* (green steel) */ - element = EL_STEEL_CHAR_A; + case 0x1681: /* gate (red) */ + element = EL_EM_GATE_1; break; - case 0x164c: /* (green steel) */ - element = EL_STEEL_CHAR_B; + case 0x1682: /* secret gate (red) */ + element = EL_GATE_1_GRAY; break; - case 0x164d: /* (green steel) */ - element = EL_STEEL_CHAR_C; + case 0x1683: /* gate (yellow) */ + element = EL_EM_GATE_2; break; - case 0x164e: /* (green steel) */ - element = EL_STEEL_CHAR_D; + case 0x1684: /* secret gate (yellow) */ + element = EL_GATE_2_GRAY; break; - case 0x164f: /* (green steel) */ - element = EL_STEEL_CHAR_E; + case 0x1685: /* gate (blue) */ + element = EL_EM_GATE_4; break; - case 0x1650: /* (green steel) */ - element = EL_STEEL_CHAR_F; + case 0x1686: /* secret gate (blue) */ + element = EL_GATE_4_GRAY; break; - case 0x1651: /* (green steel) */ - element = EL_STEEL_CHAR_G; + case 0x1687: /* gate (green) */ + element = EL_EM_GATE_3; break; - case 0x1652: /* (green steel) */ - element = EL_STEEL_CHAR_H; + case 0x1688: /* secret gate (green) */ + element = EL_GATE_3_GRAY; break; - case 0x1653: /* (green steel) */ - element = EL_STEEL_CHAR_I; + case 0x1689: /* gate (white) */ + element = EL_DC_GATE_WHITE; break; - case 0x1654: /* (green steel) */ - element = EL_STEEL_CHAR_J; + case 0x168a: /* secret gate (white) */ + element = EL_DC_GATE_WHITE_GRAY; break; - case 0x1655: /* (green steel) */ - element = EL_STEEL_CHAR_K; + case 0x168b: /* secret gate (no key) */ + element = EL_DC_GATE_FAKE_GRAY; break; - case 0x1656: /* (green steel) */ - element = EL_STEEL_CHAR_L; + case 0x168c: + element = EL_ROBOT_WHEEL; + break; + + case 0x168d: + element = EL_DC_TIMEGATE_SWITCH; + break; + + case 0x168e: + element = EL_ACID_POOL_BOTTOM; + break; + + case 0x168f: + element = EL_ACID_POOL_TOPLEFT; + break; + + case 0x1690: + element = EL_ACID_POOL_TOPRIGHT; + break; + + case 0x1691: + element = EL_ACID_POOL_BOTTOMLEFT; + break; + + case 0x1692: + element = EL_ACID_POOL_BOTTOMRIGHT; + break; + + case 0x1693: + element = EL_STEELWALL; + break; + + case 0x1694: + element = EL_STEELWALL_SLIPPERY; + break; + + case 0x1695: /* steel wall (not round) */ + element = EL_STEELWALL; + break; + + case 0x1696: /* steel wall (left) */ + element = EL_DC_STEELWALL_1_LEFT; + break; + + case 0x1697: /* steel wall (bottom) */ + element = EL_DC_STEELWALL_1_BOTTOM; + break; + + case 0x1698: /* steel wall (right) */ + element = EL_DC_STEELWALL_1_RIGHT; + break; + + case 0x1699: /* steel wall (top) */ + element = EL_DC_STEELWALL_1_TOP; + break; + + case 0x169a: /* steel wall (left/bottom) */ + element = EL_DC_STEELWALL_1_BOTTOMLEFT; + break; + + case 0x169b: /* steel wall (right/bottom) */ + element = EL_DC_STEELWALL_1_BOTTOMRIGHT; + break; + + case 0x169c: /* steel wall (right/top) */ + element = EL_DC_STEELWALL_1_TOPRIGHT; + break; + + case 0x169d: /* steel wall (left/top) */ + element = EL_DC_STEELWALL_1_TOPLEFT; + break; + + case 0x169e: /* steel wall (right/bottom small) */ + element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2; + break; + + case 0x169f: /* steel wall (left/bottom small) */ + element = EL_DC_STEELWALL_1_BOTTOMLEFT_2; + break; + + case 0x16a0: /* steel wall (right/top small) */ + element = EL_DC_STEELWALL_1_TOPRIGHT_2; + break; + + case 0x16a1: /* steel wall (left/top small) */ + element = EL_DC_STEELWALL_1_TOPLEFT_2; + break; + + case 0x16a2: /* steel wall (left/right) */ + element = EL_DC_STEELWALL_1_VERTICAL; + break; + + case 0x16a3: /* steel wall (top/bottom) */ + element = EL_DC_STEELWALL_1_HORIZONTAL; + break; + + case 0x16a4: /* steel wall 2 (left end) */ + element = EL_DC_STEELWALL_2_LEFT; + break; + + case 0x16a5: /* steel wall 2 (right end) */ + element = EL_DC_STEELWALL_2_RIGHT; + break; + + case 0x16a6: /* steel wall 2 (top end) */ + element = EL_DC_STEELWALL_2_TOP; + break; + + case 0x16a7: /* steel wall 2 (bottom end) */ + element = EL_DC_STEELWALL_2_BOTTOM; + break; + + case 0x16a8: /* steel wall 2 (left/right) */ + element = EL_DC_STEELWALL_2_HORIZONTAL; break; - case 0x1657: /* (green steel) */ - element = EL_STEEL_CHAR_M; + case 0x16a9: /* steel wall 2 (up/down) */ + element = EL_DC_STEELWALL_2_VERTICAL; break; - case 0x1658: /* (green steel) */ - element = EL_STEEL_CHAR_N; + case 0x16aa: /* steel wall 2 (mid) */ + element = EL_DC_STEELWALL_2_MIDDLE; break; - case 0x1659: /* (green steel) */ - element = EL_STEEL_CHAR_O; + case 0x16ab: + element = EL_SIGN_EXCLAMATION; break; - case 0x165a: /* (green steel) */ - element = EL_STEEL_CHAR_P; + case 0x16ac: + element = EL_SIGN_RADIOACTIVITY; break; - case 0x165b: /* (green steel) */ - element = EL_STEEL_CHAR_Q; + case 0x16ad: + element = EL_SIGN_STOP; break; - case 0x165c: /* (green steel) */ - element = EL_STEEL_CHAR_R; + case 0x16ae: + element = EL_SIGN_WHEELCHAIR; break; - case 0x165d: /* (green steel) */ - element = EL_STEEL_CHAR_S; + case 0x16af: + element = EL_SIGN_PARKING; break; - case 0x165e: /* (green steel) */ - element = EL_STEEL_CHAR_T; + case 0x16b0: + element = EL_SIGN_NO_ENTRY; break; - case 0x165f: /* (green steel) */ - element = EL_STEEL_CHAR_U; + case 0x16b1: + element = EL_SIGN_HEART; break; - case 0x1660: /* (green steel) */ - element = EL_STEEL_CHAR_V; + case 0x16b2: + element = EL_SIGN_GIVE_WAY; break; - case 0x1661: /* (green steel) */ - element = EL_STEEL_CHAR_W; + case 0x16b3: + element = EL_SIGN_ENTRY_FORBIDDEN; break; - case 0x1662: /* (green steel) */ - element = EL_STEEL_CHAR_X; + case 0x16b4: + element = EL_SIGN_EMERGENCY_EXIT; break; - case 0x1663: /* (green steel) */ - element = EL_STEEL_CHAR_Y; + case 0x16b5: + element = EL_SIGN_YIN_YANG; break; - case 0x1664: /* (green steel) */ - element = EL_STEEL_CHAR_Z; + case 0x16b6: + element = EL_WALL_EMERALD; break; - case 0x1665: /* (green steel) */ - element = EL_STEEL_CHAR_AUMLAUT; + case 0x16b7: + element = EL_WALL_DIAMOND; break; - case 0x1666: /* (green steel) */ - element = EL_STEEL_CHAR_OUMLAUT; + case 0x16b8: + element = EL_WALL_PEARL; break; - case 0x1667: /* (green steel) */ - element = EL_STEEL_CHAR_UUMLAUT; + case 0x16b9: + element = EL_WALL_CRYSTAL; break; - case 0x1668: /* (green steel) */ - element = EL_STEEL_CHAR_0; + case 0x16ba: + element = EL_INVISIBLE_WALL; break; - case 0x1669: /* (green steel) */ - element = EL_STEEL_CHAR_1; + case 0x16bb: + element = EL_INVISIBLE_STEELWALL; break; - case 0x166a: /* (green steel) */ - element = EL_STEEL_CHAR_2; + /* 0x16bc - 0x16cb: */ + /* EL_INVISIBLE_SAND */ + + case 0x16cc: + element = EL_LIGHT_SWITCH; break; - case 0x166b: /* (green steel) */ - element = EL_STEEL_CHAR_3; + case 0x16cd: + element = EL_ENVELOPE_1; break; - case 0x166c: /* (green steel) */ - element = EL_STEEL_CHAR_4; + default: + if (element >= 0x0117 && element <= 0x036e) /* (?) */ + element = EL_DIAMOND; + else if (element >= 0x042d && element <= 0x0684) /* (?) */ + element = EL_EMERALD; + else if (element >= 0x157c && element <= 0x158b) + element = EL_SAND; + else if (element >= 0x1590 && element <= 0x159f) + element = EL_DC_LANDMINE; + else if (element >= 0x16bc && element <= 0x16cb) + element = EL_INVISIBLE_SAND; + else + { + Error(ERR_WARN, "unknown Diamond Caves element 0x%04x", element); + element = EL_UNKNOWN; + } break; + } + + return getMappedElement(element); +} + +#if 1 + +#if 1 + +static void LoadLevelFromFileStream_DC(File *file, struct LevelInfo *level, + int nr) +{ + byte header[DC_LEVEL_HEADER_SIZE]; + int envelope_size; + int envelope_header_pos = 62; + int envelope_content_pos = 94; + int level_name_pos = 251; + int level_author_pos = 292; + int envelope_header_len; + int envelope_content_len; + int level_name_len; + int level_author_len; + int fieldx, fieldy; + int num_yamyam_contents; + int i, x, y; + + getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */ + + for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++) + { + unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE); + + header[i * 2 + 0] = header_word >> 8; + header[i * 2 + 1] = header_word & 0xff; + } + + /* read some values from level header to check level decoding integrity */ + fieldx = header[6] | (header[7] << 8); + fieldy = header[8] | (header[9] << 8); + num_yamyam_contents = header[60] | (header[61] << 8); + + /* do some simple sanity checks to ensure that level was correctly decoded */ + if (fieldx < 1 || fieldx > 256 || + fieldy < 1 || fieldy > 256 || + num_yamyam_contents < 1 || num_yamyam_contents > 8) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "cannot decode level from stream -- using empty level"); + + return; + } + + /* maximum envelope header size is 31 bytes */ + envelope_header_len = header[envelope_header_pos]; + /* maximum envelope content size is 110 (156?) bytes */ + envelope_content_len = header[envelope_content_pos]; + + /* maximum level title size is 40 bytes */ + level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN); + /* maximum level author size is 30 (51?) bytes */ + level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN); + + envelope_size = 0; + + for (i = 0; i < envelope_header_len; i++) + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = + header[envelope_header_pos + 1 + i]; + + if (envelope_header_len > 0 && envelope_content_len > 0) + { + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = '\n'; + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = '\n'; + } + + for (i = 0; i < envelope_content_len; i++) + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = + header[envelope_content_pos + 1 + i]; + + level->envelope[0].text[envelope_size] = '\0'; + + level->envelope[0].xsize = MAX_ENVELOPE_XSIZE; + level->envelope[0].ysize = 10; + level->envelope[0].autowrap = TRUE; + level->envelope[0].centered = TRUE; + + for (i = 0; i < level_name_len; i++) + level->name[i] = header[level_name_pos + 1 + i]; + level->name[level_name_len] = '\0'; + + for (i = 0; i < level_author_len; i++) + level->author[i] = header[level_author_pos + 1 + i]; + level->author[level_author_len] = '\0'; + + num_yamyam_contents = header[60] | (header[61] << 8); + level->num_yamyam_contents = + MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS); + + for (i = 0; i < num_yamyam_contents; i++) + { + for (y = 0; y < 3; y++) for (x = 0; x < 3; x++) + { + unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); +#if 1 + int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); +#else + int element_dc = word; +#endif + + if (i < MAX_ELEMENT_CONTENTS) + level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc); + } + } + + fieldx = header[6] | (header[7] << 8); + fieldy = header[8] | (header[9] << 8); + level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX); + level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY); + + for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++) + { + unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); +#if 1 + int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); +#else + int element_dc = word; +#endif + + if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY) + level->field[x][y] = getMappedElement_DC(element_dc); + } + + x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1); + y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1); + level->field[x][y] = EL_PLAYER_1; + + x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1); + y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1); + level->field[x][y] = EL_PLAYER_2; + + level->gems_needed = header[18] | (header[19] << 8); + + level->score[SC_EMERALD] = header[20] | (header[21] << 8); + level->score[SC_DIAMOND] = header[22] | (header[23] << 8); + level->score[SC_PEARL] = header[24] | (header[25] << 8); + level->score[SC_CRYSTAL] = header[26] | (header[27] << 8); + level->score[SC_NUT] = header[28] | (header[29] << 8); + level->score[SC_ROBOT] = header[30] | (header[31] << 8); + level->score[SC_SPACESHIP] = header[32] | (header[33] << 8); + level->score[SC_BUG] = header[34] | (header[35] << 8); + level->score[SC_YAMYAM] = header[36] | (header[37] << 8); + level->score[SC_DYNAMITE] = header[38] | (header[39] << 8); + level->score[SC_KEY] = header[40] | (header[41] << 8); + level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8); - case 0x166d: /* (green steel) */ - element = EL_STEEL_CHAR_5; - break; + level->time = header[44] | (header[45] << 8); - case 0x166e: /* (green steel) */ - element = EL_STEEL_CHAR_6; - break; + level->amoeba_speed = header[46] | (header[47] << 8); + level->time_light = header[48] | (header[49] << 8); + level->time_timegate = header[50] | (header[51] << 8); + level->time_wheel = header[52] | (header[53] << 8); + level->time_magic_wall = header[54] | (header[55] << 8); + level->extra_time = header[56] | (header[57] << 8); + level->shield_normal_time = header[58] | (header[59] << 8); - case 0x166f: /* (green steel) */ - element = EL_STEEL_CHAR_7; - break; + /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems + can slip down from flat walls, like normal walls and steel walls */ + level->em_slippery_gems = TRUE; - case 0x1670: /* (green steel) */ - element = EL_STEEL_CHAR_8; - break; +#if 0 + /* Diamond Caves II levels are always surrounded by indestructible wall, but + not necessarily in a rectangular way -- fill with invisible steel wall */ - case 0x1671: /* (green steel) */ - element = EL_STEEL_CHAR_9; - break; + /* !!! not always true !!! keep level and set BorderElement instead !!! */ - case 0x1672: /* (green steel) */ - element = EL_STEEL_CHAR_PERIOD; - break; + for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++) + { +#if 1 + if ((x == 0 || x == level->fieldx - 1 || + y == 0 || y == level->fieldy - 1) && + level->field[x][y] == EL_EMPTY) + level->field[x][y] = EL_INVISIBLE_STEELWALL; +#else + if ((x == 0 || x == level->fieldx - 1 || + y == 0 || y == level->fieldy - 1) && + level->field[x][y] == EL_EMPTY) + FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL, + level->field, level->fieldx, level->fieldy); +#endif + } +#endif +} - case 0x1673: /* (green steel) */ - element = EL_STEEL_CHAR_EXCLAM; - break; +static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, + struct LevelFileInfo *level_file_info, + boolean level_info_only) +{ + char *filename = level_file_info->filename; + File *file; + int num_magic_bytes = 8; + char magic_bytes[num_magic_bytes + 1]; + int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level; - case 0x1674: /* (green steel) */ - element = EL_STEEL_CHAR_COLON; - break; + if (!(file = openFile(filename, MODE_READ))) + { + level->no_valid_file = TRUE; - case 0x1675: /* (green steel) */ - element = EL_STEEL_CHAR_LESS; - break; + if (!level_info_only) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); - case 0x1676: /* (green steel) */ - element = EL_STEEL_CHAR_GREATER; - break; + return; + } - case 0x1677: /* (green steel) */ - element = EL_STEEL_CHAR_QUESTION; - break; + // fseek(file, 0x0000, SEEK_SET); - case 0x1678: /* (green steel) */ - element = EL_STEEL_CHAR_COPYRIGHT; - break; + if (level_file_info->packed) + { + /* read "magic bytes" from start of file */ + if (getStringFromFile(file, magic_bytes, num_magic_bytes + 1) == NULL) + magic_bytes[0] = '\0'; - case 0x1679: /* (green steel) */ - element = EL_STEEL_CHAR_UP; - break; + /* check "magic bytes" for correct file format */ + if (!strPrefix(magic_bytes, "DC2")) + { + level->no_valid_file = TRUE; - case 0x167a: /* (green steel) */ - element = EL_STEEL_CHAR_DOWN; - break; + Error(ERR_WARN, "unknown DC level file '%s' -- using empty level", + filename); - case 0x167b: /* (green steel) */ - element = EL_STEEL_CHAR_BUTTON; - break; + return; + } - case 0x167c: /* (green steel) */ - element = EL_STEEL_CHAR_PLUS; - break; + if (strPrefix(magic_bytes, "DC2Win95") || + strPrefix(magic_bytes, "DC2Win98")) + { + int position_first_level = 0x00fa; + int extra_bytes = 4; + int skip_bytes; - case 0x167d: /* (green steel) */ - element = EL_STEEL_CHAR_MINUS; - break; + /* advance file stream to first level inside the level package */ + skip_bytes = position_first_level - num_magic_bytes - extra_bytes; - case 0x167e: /* (green steel) */ - element = EL_STEEL_CHAR_APOSTROPHE; - break; + /* each block of level data is followed by block of non-level data */ + num_levels_to_skip *= 2; - case 0x167f: /* (green steel) */ - element = EL_STEEL_CHAR_PARENLEFT; - break; + /* at least skip header bytes, therefore use ">= 0" instead of "> 0" */ + while (num_levels_to_skip >= 0) + { + /* advance file stream to next level inside the level package */ + if (seekFile(file, skip_bytes, SEEK_CUR) != 0) + { + level->no_valid_file = TRUE; - case 0x1680: /* (green steel) */ - element = EL_STEEL_CHAR_PARENRIGHT; - break; + Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", + filename); - case 0x1681: /* gate (red) */ - element = EL_EM_GATE_1; - break; + return; + } - case 0x1682: /* secret gate (red) */ - element = EL_GATE_1_GRAY; - break; + /* skip apparently unused extra bytes following each level */ + ReadUnusedBytesFromFile(file, extra_bytes); - case 0x1683: /* gate (yellow) */ - element = EL_EM_GATE_2; - break; + /* read size of next level in level package */ + skip_bytes = getFile32BitLE(file); - case 0x1684: /* secret gate (yellow) */ - element = EL_GATE_2_GRAY; - break; + num_levels_to_skip--; + } + } + else + { + level->no_valid_file = TRUE; - case 0x1685: /* gate (blue) */ - element = EL_EM_GATE_4; - break; + Error(ERR_WARN, "unknown DC2 level file '%s' -- using empty level", + filename); - case 0x1686: /* secret gate (blue) */ - element = EL_GATE_4_GRAY; - break; + return; + } + } - case 0x1687: /* gate (green) */ - element = EL_EM_GATE_3; - break; + LoadLevelFromFileStream_DC(file, level, level_file_info->nr); - case 0x1688: /* secret gate (green) */ - element = EL_GATE_3_GRAY; - break; + closeFile(file); +} - case 0x1689: /* gate (white) */ - element = EL_DC_GATE_WHITE; - break; +#else - case 0x168a: /* secret gate (white) */ - element = EL_DC_GATE_WHITE_GRAY; - break; +static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, + int nr) +{ + byte header[DC_LEVEL_HEADER_SIZE]; + int envelope_size; + int envelope_header_pos = 62; + int envelope_content_pos = 94; + int level_name_pos = 251; + int level_author_pos = 292; + int envelope_header_len; + int envelope_content_len; + int level_name_len; + int level_author_len; + int fieldx, fieldy; + int num_yamyam_contents; + int i, x, y; - case 0x168b: /* secret gate (no key) */ - element = EL_DC_GATE_FAKE_GRAY; - break; + getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */ - case 0x168c: - element = EL_ROBOT_WHEEL; - break; + for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++) + { + unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE); - case 0x168d: - element = EL_DC_TIMEGATE_SWITCH; - break; + header[i * 2 + 0] = header_word >> 8; + header[i * 2 + 1] = header_word & 0xff; + } - case 0x168e: - element = EL_ACID_POOL_BOTTOM; - break; + /* read some values from level header to check level decoding integrity */ + fieldx = header[6] | (header[7] << 8); + fieldy = header[8] | (header[9] << 8); + num_yamyam_contents = header[60] | (header[61] << 8); - case 0x168f: - element = EL_ACID_POOL_TOPLEFT; - break; + /* do some simple sanity checks to ensure that level was correctly decoded */ + if (fieldx < 1 || fieldx > 256 || + fieldy < 1 || fieldy > 256 || + num_yamyam_contents < 1 || num_yamyam_contents > 8) + { + level->no_valid_file = TRUE; - case 0x1690: - element = EL_ACID_POOL_TOPRIGHT; - break; + Error(ERR_WARN, "cannot decode level from stream -- using empty level"); - case 0x1691: - element = EL_ACID_POOL_BOTTOMLEFT; - break; + return; + } - case 0x1692: - element = EL_ACID_POOL_BOTTOMRIGHT; - break; + /* maximum envelope header size is 31 bytes */ + envelope_header_len = header[envelope_header_pos]; + /* maximum envelope content size is 110 (156?) bytes */ + envelope_content_len = header[envelope_content_pos]; - case 0x1693: - element = EL_STEELWALL; - break; + /* maximum level title size is 40 bytes */ + level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN); + /* maximum level author size is 30 (51?) bytes */ + level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN); - case 0x1694: - element = EL_STEELWALL_SLIPPERY; - break; + envelope_size = 0; - case 0x1695: /* steel wall (not round) */ - element = EL_STEELWALL; - break; + for (i = 0; i < envelope_header_len; i++) + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = + header[envelope_header_pos + 1 + i]; - case 0x1696: /* steel wall (left) */ - element = EL_DC_STEELWALL_1_LEFT; - break; + if (envelope_header_len > 0 && envelope_content_len > 0) + { + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = '\n'; + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = '\n'; + } - case 0x1697: /* steel wall (bottom) */ - element = EL_DC_STEELWALL_1_BOTTOM; - break; + for (i = 0; i < envelope_content_len; i++) + if (envelope_size < MAX_ENVELOPE_TEXT_LEN) + level->envelope[0].text[envelope_size++] = + header[envelope_content_pos + 1 + i]; - case 0x1698: /* steel wall (right) */ - element = EL_DC_STEELWALL_1_RIGHT; - break; + level->envelope[0].text[envelope_size] = '\0'; - case 0x1699: /* steel wall (top) */ - element = EL_DC_STEELWALL_1_TOP; - break; + level->envelope[0].xsize = MAX_ENVELOPE_XSIZE; + level->envelope[0].ysize = 10; + level->envelope[0].autowrap = TRUE; + level->envelope[0].centered = TRUE; - case 0x169a: /* steel wall (left/bottom) */ - element = EL_DC_STEELWALL_1_BOTTOMLEFT; - break; + for (i = 0; i < level_name_len; i++) + level->name[i] = header[level_name_pos + 1 + i]; + level->name[level_name_len] = '\0'; - case 0x169b: /* steel wall (right/bottom) */ - element = EL_DC_STEELWALL_1_BOTTOMRIGHT; - break; + for (i = 0; i < level_author_len; i++) + level->author[i] = header[level_author_pos + 1 + i]; + level->author[level_author_len] = '\0'; - case 0x169c: /* steel wall (right/top) */ - element = EL_DC_STEELWALL_1_TOPRIGHT; - break; + num_yamyam_contents = header[60] | (header[61] << 8); + level->num_yamyam_contents = + MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS); - case 0x169d: /* steel wall (left/top) */ - element = EL_DC_STEELWALL_1_TOPLEFT; - break; + for (i = 0; i < num_yamyam_contents; i++) + { + for (y = 0; y < 3; y++) for (x = 0; x < 3; x++) + { + unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); +#if 1 + int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); +#else + int element_dc = word; +#endif - case 0x169e: /* steel wall (right/bottom small) */ - element = EL_DC_STEELWALL_1_BOTTOMRIGHT_2; - break; + if (i < MAX_ELEMENT_CONTENTS) + level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc); + } + } - case 0x169f: /* steel wall (left/bottom small) */ - element = EL_DC_STEELWALL_1_BOTTOMLEFT_2; - break; + fieldx = header[6] | (header[7] << 8); + fieldy = header[8] | (header[9] << 8); + level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX); + level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY); - case 0x16a0: /* steel wall (right/top small) */ - element = EL_DC_STEELWALL_1_TOPRIGHT_2; - break; + for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++) + { + unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); +#if 1 + int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); +#else + int element_dc = word; +#endif - case 0x16a1: /* steel wall (left/top small) */ - element = EL_DC_STEELWALL_1_TOPLEFT_2; - break; + if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY) + level->field[x][y] = getMappedElement_DC(element_dc); + } - case 0x16a2: /* steel wall (left/right) */ - element = EL_DC_STEELWALL_1_VERTICAL; - break; + x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1); + y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1); + level->field[x][y] = EL_PLAYER_1; - case 0x16a3: /* steel wall (top/bottom) */ - element = EL_DC_STEELWALL_1_HORIZONTAL; - break; + x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1); + y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1); + level->field[x][y] = EL_PLAYER_2; - case 0x16a4: /* steel wall 2 (left end) */ - element = EL_DC_STEELWALL_2_LEFT; - break; + level->gems_needed = header[18] | (header[19] << 8); - case 0x16a5: /* steel wall 2 (right end) */ - element = EL_DC_STEELWALL_2_RIGHT; - break; + level->score[SC_EMERALD] = header[20] | (header[21] << 8); + level->score[SC_DIAMOND] = header[22] | (header[23] << 8); + level->score[SC_PEARL] = header[24] | (header[25] << 8); + level->score[SC_CRYSTAL] = header[26] | (header[27] << 8); + level->score[SC_NUT] = header[28] | (header[29] << 8); + level->score[SC_ROBOT] = header[30] | (header[31] << 8); + level->score[SC_SPACESHIP] = header[32] | (header[33] << 8); + level->score[SC_BUG] = header[34] | (header[35] << 8); + level->score[SC_YAMYAM] = header[36] | (header[37] << 8); + level->score[SC_DYNAMITE] = header[38] | (header[39] << 8); + level->score[SC_KEY] = header[40] | (header[41] << 8); + level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8); - case 0x16a6: /* steel wall 2 (top end) */ - element = EL_DC_STEELWALL_2_TOP; - break; + level->time = header[44] | (header[45] << 8); - case 0x16a7: /* steel wall 2 (bottom end) */ - element = EL_DC_STEELWALL_2_BOTTOM; - break; + level->amoeba_speed = header[46] | (header[47] << 8); + level->time_light = header[48] | (header[49] << 8); + level->time_timegate = header[50] | (header[51] << 8); + level->time_wheel = header[52] | (header[53] << 8); + level->time_magic_wall = header[54] | (header[55] << 8); + level->extra_time = header[56] | (header[57] << 8); + level->shield_normal_time = header[58] | (header[59] << 8); - case 0x16a8: /* steel wall 2 (left/right) */ - element = EL_DC_STEELWALL_2_HORIZONTAL; - break; + /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems + can slip down from flat walls, like normal walls and steel walls */ + level->em_slippery_gems = TRUE; - case 0x16a9: /* steel wall 2 (up/down) */ - element = EL_DC_STEELWALL_2_VERTICAL; - break; +#if 0 + /* Diamond Caves II levels are always surrounded by indestructible wall, but + not necessarily in a rectangular way -- fill with invisible steel wall */ - case 0x16aa: /* steel wall 2 (mid) */ - element = EL_DC_STEELWALL_2_MIDDLE; - break; + /* !!! not always true !!! keep level and set BorderElement instead !!! */ - case 0x16ab: - element = EL_SIGN_EXCLAMATION; - break; + for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++) + { +#if 1 + if ((x == 0 || x == level->fieldx - 1 || + y == 0 || y == level->fieldy - 1) && + level->field[x][y] == EL_EMPTY) + level->field[x][y] = EL_INVISIBLE_STEELWALL; +#else + if ((x == 0 || x == level->fieldx - 1 || + y == 0 || y == level->fieldy - 1) && + level->field[x][y] == EL_EMPTY) + FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL, + level->field, level->fieldx, level->fieldy); +#endif + } +#endif +} - case 0x16ac: - element = EL_SIGN_RADIOACTIVITY; - break; +static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, + struct LevelFileInfo *level_file_info, + boolean level_info_only) +{ + char *filename = level_file_info->filename; + FILE *file; + int num_magic_bytes = 8; + char magic_bytes[num_magic_bytes + 1]; + int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level; - case 0x16ad: - element = EL_SIGN_STOP; - break; + if (!(file = fopen(filename, MODE_READ))) + { + level->no_valid_file = TRUE; - case 0x16ae: - element = EL_SIGN_WHEELCHAIR; - break; + if (!level_info_only) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); - case 0x16af: - element = EL_SIGN_PARKING; - break; + return; + } - case 0x16b0: - element = EL_SIGN_NO_ENTRY; - break; + // fseek(file, 0x0000, SEEK_SET); - case 0x16b1: - element = EL_SIGN_HEART; - break; + if (level_file_info->packed) + { + /* read "magic bytes" from start of file */ + if (fgets(magic_bytes, num_magic_bytes + 1, file) == NULL) + magic_bytes[0] = '\0'; - case 0x16b2: - element = EL_SIGN_GIVE_WAY; - break; + /* check "magic bytes" for correct file format */ + if (!strPrefix(magic_bytes, "DC2")) + { + level->no_valid_file = TRUE; - case 0x16b3: - element = EL_SIGN_ENTRY_FORBIDDEN; - break; + Error(ERR_WARN, "unknown DC level file '%s' -- using empty level", + filename); - case 0x16b4: - element = EL_SIGN_EMERGENCY_EXIT; - break; + return; + } - case 0x16b5: - element = EL_SIGN_YIN_YANG; - break; + if (strPrefix(magic_bytes, "DC2Win95") || + strPrefix(magic_bytes, "DC2Win98")) + { + int position_first_level = 0x00fa; + int extra_bytes = 4; + int skip_bytes; - case 0x16b6: - element = EL_WALL_EMERALD; - break; + /* advance file stream to first level inside the level package */ + skip_bytes = position_first_level - num_magic_bytes - extra_bytes; - case 0x16b7: - element = EL_WALL_DIAMOND; - break; + /* each block of level data is followed by block of non-level data */ + num_levels_to_skip *= 2; - case 0x16b8: - element = EL_WALL_PEARL; - break; + /* at least skip header bytes, therefore use ">= 0" instead of "> 0" */ + while (num_levels_to_skip >= 0) + { + /* advance file stream to next level inside the level package */ + if (fseek(file, skip_bytes, SEEK_CUR) != 0) + { + level->no_valid_file = TRUE; - case 0x16b9: - element = EL_WALL_CRYSTAL; - break; + Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", + filename); - case 0x16ba: - element = EL_INVISIBLE_WALL; - break; + return; + } - case 0x16bb: - element = EL_INVISIBLE_STEELWALL; - break; + /* skip apparently unused extra bytes following each level */ + ReadUnusedBytesFromFile(file, extra_bytes); - /* 0x16bc - 0x16cb: */ - /* EL_INVISIBLE_SAND */ + /* read size of next level in level package */ + skip_bytes = getFile32BitLE(file); - case 0x16cc: - element = EL_LIGHT_SWITCH; - break; + num_levels_to_skip--; + } + } + else + { + level->no_valid_file = TRUE; - case 0x16cd: - element = EL_ENVELOPE_1; - break; + Error(ERR_WARN, "unknown DC2 level file '%s' -- using empty level", + filename); - default: - if (element >= 0x0117 && element <= 0x036e) /* (?) */ - element = EL_DIAMOND; - else if (element >= 0x042d && element <= 0x0684) /* (?) */ - element = EL_EMERALD; - else if (element >= 0x157c && element <= 0x158b) - element = EL_SAND; - else if (element >= 0x1590 && element <= 0x159f) - element = EL_DC_LANDMINE; - else if (element >= 0x16bc && element <= 0x16cb) - element = EL_INVISIBLE_SAND; - else - { - Error(ERR_WARN, "unknown Diamond Caves element 0x%04x", element); - element = EL_UNKNOWN; - } - break; + return; + } } - return getMappedElement(element); + LoadLevelFromFileStream_DC(file, level, level_file_info->nr); + + fclose(file); } -#if 1 +#endif -static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, - int nr) +#else + +static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, + struct LevelFileInfo *level_file_info) { + char *filename = level_file_info->filename; + FILE *file; +#if 0 + int nr = level_file_info->nr - leveldir_current->first_level; +#endif byte header[DC_LEVEL_HEADER_SIZE]; int envelope_size; int envelope_header_pos = 62; @@ -6265,6 +7921,28 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, int num_yamyam_contents; int i, x, y; + if (!(file = fopen(filename, MODE_READ))) + { + level->no_valid_file = TRUE; + + if (!level_info_only) + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); + + return; + } + +#if 0 + /* position file stream to the requested level inside the level package */ + if (fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0) + { + level->no_valid_file = TRUE; + + Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename); + + return; + } +#endif + getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */ for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++) @@ -6287,7 +7965,8 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, { level->no_valid_file = TRUE; - Error(ERR_WARN, "cannot decode level from stream -- using empty level"); + Error(ERR_WARN, "cannot read level from file '%s' -- using empty level", + filename); return; } @@ -6408,6 +8087,8 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, level->extra_time = header[56] | (header[57] << 8); level->shield_normal_time = header[58] | (header[59] << 8); + fclose(file); + /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems can slip down from flat walls, like normal walls and steel walls */ level->em_slippery_gems = TRUE; @@ -6436,17 +8117,79 @@ static void LoadLevelFromFileStream_DC(FILE *file, struct LevelInfo *level, #endif } -static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, +#endif + + +/* ------------------------------------------------------------------------- */ +/* functions for loading SB level */ +/* ------------------------------------------------------------------------- */ + +int getMappedElement_SB(int element_ascii, boolean use_ces) +{ + static struct + { + int ascii; + int sb; + int ce; + } + sb_element_mapping[] = + { + { ' ', EL_EMPTY, EL_CUSTOM_1 }, /* floor (space) */ + { '#', EL_STEELWALL, EL_CUSTOM_2 }, /* wall */ + { '@', EL_PLAYER_1, EL_CUSTOM_3 }, /* player */ + { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, /* box */ + { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, /* goal square */ + { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, /* box on goal square */ + { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, /* player on goal square */ +#if 0 + { '_', EL_INVISIBLE_STEELWALL, EL_CUSTOM_8 }, /* floor beyond border */ +#else + { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, /* floor beyond border */ +#endif + + { 0, -1, -1 }, + }; + + int i; + + for (i = 0; sb_element_mapping[i].ascii != 0; i++) + if (element_ascii == sb_element_mapping[i].ascii) + return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb); + + return EL_UNDEFINED; +} + +#if 1 + +static void LoadLevelFromFileInfo_SB(struct LevelInfo *level, struct LevelFileInfo *level_file_info, boolean level_info_only) { char *filename = level_file_info->filename; - FILE *file; - int num_magic_bytes = 8; - char magic_bytes[num_magic_bytes + 1]; + char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN]; + char last_comment[MAX_LINE_LEN]; + char level_name[MAX_LINE_LEN]; + char *line_ptr; + File *file; int num_levels_to_skip = level_file_info->nr - leveldir_current->first_level; + boolean read_continued_line = FALSE; + boolean reading_playfield = FALSE; + boolean got_valid_playfield_line = FALSE; + boolean invalid_playfield_char = FALSE; + boolean load_xsb_to_ces = check_special_flags("load_xsb_to_ces"); + int file_level_nr = 0; + int line_nr = 0; + int x = 0, y = 0; /* initialized to make compilers happy */ - if (!(file = fopen(filename, MODE_READ))) +#if 0 + printf("::: looking for level number %d [%d]\n", + level_file_info->nr, num_levels_to_skip); +#endif + + last_comment[0] = '\0'; + level_name[0] = '\0'; + + if (!(file = openFile(filename, MODE_READ))) { level->no_valid_file = TRUE; @@ -6456,339 +8199,274 @@ static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, return; } - // fseek(file, 0x0000, SEEK_SET); - - if (level_file_info->packed) + while (!checkEndOfFile(file)) { - /* read "magic bytes" from start of file */ - if (fgets(magic_bytes, num_magic_bytes + 1, file) == NULL) - magic_bytes[0] = '\0'; - - /* check "magic bytes" for correct file format */ - if (!strPrefix(magic_bytes, "DC2")) + /* level successfully read, but next level may follow here */ + if (!got_valid_playfield_line && reading_playfield) { - level->no_valid_file = TRUE; +#if 0 + printf("::: read complete playfield\n"); +#endif - Error(ERR_WARN, "unknown DC level file '%s' -- using empty level", - filename); + /* read playfield from single level file -- skip remaining file */ + if (!level_file_info->packed) + break; - return; - } + if (file_level_nr >= num_levels_to_skip) + break; - if (strPrefix(magic_bytes, "DC2Win95") || - strPrefix(magic_bytes, "DC2Win98")) - { - int position_first_level = 0x00fa; - int extra_bytes = 4; - int skip_bytes; + file_level_nr++; - /* advance file stream to first level inside the level package */ - skip_bytes = position_first_level - num_magic_bytes - extra_bytes; + last_comment[0] = '\0'; + level_name[0] = '\0'; - /* each block of level data is followed by block of non-level data */ - num_levels_to_skip *= 2; + reading_playfield = FALSE; + } - /* at least skip header bytes, therefore use ">= 0" instead of "> 0" */ - while (num_levels_to_skip >= 0) - { - /* advance file stream to next level inside the level package */ - if (fseek(file, skip_bytes, SEEK_CUR) != 0) - { - level->no_valid_file = TRUE; + got_valid_playfield_line = FALSE; - Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", - filename); + /* read next line of input file */ + if (!getStringFromFile(file, line, MAX_LINE_LEN)) + break; - return; - } + /* check if line was completely read and is terminated by line break */ + if (strlen(line) > 0 && line[strlen(line) - 1] == '\n') + line_nr++; - /* skip apparently unused extra bytes following each level */ - ReadUnusedBytesFromFile(file, extra_bytes); + /* cut trailing line break (this can be newline and/or carriage return) */ + for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--) + if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0') + *line_ptr = '\0'; + + /* copy raw input line for later use (mainly debugging output) */ + strcpy(line_raw, line); + + if (read_continued_line) + { + /* append new line to existing line, if there is enough space */ + if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN) + strcat(previous_line, line_ptr); - /* read size of next level in level package */ - skip_bytes = getFile32BitLE(file); + strcpy(line, previous_line); /* copy storage buffer to line */ - num_levels_to_skip--; - } + read_continued_line = FALSE; } - else + + /* if the last character is '\', continue at next line */ + if (strlen(line) > 0 && line[strlen(line) - 1] == '\\') { - level->no_valid_file = TRUE; + line[strlen(line) - 1] = '\0'; /* cut off trailing backslash */ + strcpy(previous_line, line); /* copy line to storage buffer */ - Error(ERR_WARN, "unknown DC2 level file '%s' -- using empty level", - filename); + read_continued_line = TRUE; - return; + continue; } - } - LoadLevelFromFileStream_DC(file, level, level_file_info->nr); + /* skip empty lines */ + if (line[0] == '\0') + continue; - fclose(file); -} + /* extract comment text from comment line */ + if (line[0] == ';') + { + for (line_ptr = line; *line_ptr; line_ptr++) + if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != ';') + break; -#else + strcpy(last_comment, line_ptr); -static void LoadLevelFromFileInfo_DC(struct LevelInfo *level, - struct LevelFileInfo *level_file_info) -{ - char *filename = level_file_info->filename; - FILE *file; #if 0 - int nr = level_file_info->nr - leveldir_current->first_level; + printf("::: found comment '%s' in line %d\n", last_comment, line_nr); #endif - byte header[DC_LEVEL_HEADER_SIZE]; - int envelope_size; - int envelope_header_pos = 62; - int envelope_content_pos = 94; - int level_name_pos = 251; - int level_author_pos = 292; - int envelope_header_len; - int envelope_content_len; - int level_name_len; - int level_author_len; - int fieldx, fieldy; - int num_yamyam_contents; - int i, x, y; - if (!(file = fopen(filename, MODE_READ))) - { - level->no_valid_file = TRUE; + continue; + } - if (!level_info_only) - Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); + /* extract level title text from line containing level title */ + if (line[0] == '\'') + { + strcpy(level_name, &line[1]); - return; - } + if (strlen(level_name) > 0 && level_name[strlen(level_name) - 1] == '\'') + level_name[strlen(level_name) - 1] = '\0'; #if 0 - /* position file stream to the requested level inside the level package */ - if (fseek(file, nr * SP_LEVEL_SIZE, SEEK_SET) != 0) - { - level->no_valid_file = TRUE; - - Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename); - - return; - } + printf("::: found level name '%s' in line %d\n", level_name, line_nr); #endif - getDecodedWord_DC(0, TRUE); /* initialize DC2 decoding engine */ + continue; + } - for (i = 0; i < DC_LEVEL_HEADER_SIZE / 2; i++) - { - unsigned short header_word = getDecodedWord_DC(getFile16BitBE(file), FALSE); + /* skip lines containing only spaces (or empty lines) */ + for (line_ptr = line; *line_ptr; line_ptr++) + if (*line_ptr != ' ') + break; + if (*line_ptr == '\0') + continue; - header[i * 2 + 0] = header_word >> 8; - header[i * 2 + 1] = header_word & 0xff; - } + /* at this point, we have found a line containing part of a playfield */ - /* read some values from level header to check level decoding integrity */ - fieldx = header[6] | (header[7] << 8); - fieldy = header[8] | (header[9] << 8); - num_yamyam_contents = header[60] | (header[61] << 8); +#if 0 + printf("::: found playfield row in line %d\n", line_nr); +#endif - /* do some simple sanity checks to ensure that level was correctly decoded */ - if (fieldx < 1 || fieldx > 256 || - fieldy < 1 || fieldy > 256 || - num_yamyam_contents < 1 || num_yamyam_contents > 8) - { - level->no_valid_file = TRUE; + got_valid_playfield_line = TRUE; - Error(ERR_WARN, "cannot read level from file '%s' -- using empty level", - filename); + if (!reading_playfield) + { + reading_playfield = TRUE; + invalid_playfield_char = FALSE; - return; - } + for (x = 0; x < MAX_LEV_FIELDX; x++) + for (y = 0; y < MAX_LEV_FIELDY; y++) + level->field[x][y] = getMappedElement_SB(' ', load_xsb_to_ces); - /* maximum envelope header size is 31 bytes */ - envelope_header_len = header[envelope_header_pos]; - /* maximum envelope content size is 110 (156?) bytes */ - envelope_content_len = header[envelope_content_pos]; + level->fieldx = 0; + level->fieldy = 0; - /* maximum level title size is 40 bytes */ - level_name_len = MIN(header[level_name_pos], MAX_LEVEL_NAME_LEN); - /* maximum level author size is 30 (51?) bytes */ - level_author_len = MIN(header[level_author_pos], MAX_LEVEL_AUTHOR_LEN); + /* start with topmost tile row */ + y = 0; + } - envelope_size = 0; + /* skip playfield line if larger row than allowed */ + if (y >= MAX_LEV_FIELDY) + continue; - for (i = 0; i < envelope_header_len; i++) - if (envelope_size < MAX_ENVELOPE_TEXT_LEN) - level->envelope[0].text[envelope_size++] = - header[envelope_header_pos + 1 + i]; + /* start with leftmost tile column */ + x = 0; - if (envelope_header_len > 0 && envelope_content_len > 0) - { - if (envelope_size < MAX_ENVELOPE_TEXT_LEN) - level->envelope[0].text[envelope_size++] = '\n'; - if (envelope_size < MAX_ENVELOPE_TEXT_LEN) - level->envelope[0].text[envelope_size++] = '\n'; - } + /* read playfield elements from line */ + for (line_ptr = line; *line_ptr; line_ptr++) + { + int mapped_sb_element = getMappedElement_SB(*line_ptr, load_xsb_to_ces); - for (i = 0; i < envelope_content_len; i++) - if (envelope_size < MAX_ENVELOPE_TEXT_LEN) - level->envelope[0].text[envelope_size++] = - header[envelope_content_pos + 1 + i]; + /* stop parsing playfield line if larger column than allowed */ + if (x >= MAX_LEV_FIELDX) + break; - level->envelope[0].text[envelope_size] = '\0'; + if (mapped_sb_element == EL_UNDEFINED) + { + invalid_playfield_char = TRUE; - level->envelope[0].xsize = MAX_ENVELOPE_XSIZE; - level->envelope[0].ysize = 10; - level->envelope[0].autowrap = TRUE; - level->envelope[0].centered = TRUE; + break; + } - for (i = 0; i < level_name_len; i++) - level->name[i] = header[level_name_pos + 1 + i]; - level->name[level_name_len] = '\0'; + level->field[x][y] = mapped_sb_element; - for (i = 0; i < level_author_len; i++) - level->author[i] = header[level_author_pos + 1 + i]; - level->author[level_author_len] = '\0'; + /* continue with next tile column */ + x++; - num_yamyam_contents = header[60] | (header[61] << 8); - level->num_yamyam_contents = - MIN(MAX(MIN_ELEMENT_CONTENTS, num_yamyam_contents), MAX_ELEMENT_CONTENTS); + level->fieldx = MAX(x, level->fieldx); + } - for (i = 0; i < num_yamyam_contents; i++) - { - for (y = 0; y < 3; y++) for (x = 0; x < 3; x++) + if (invalid_playfield_char) { - unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); -#if 1 - int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); -#else - int element_dc = word; -#endif + /* if first playfield line, treat invalid lines as comment lines */ + if (y == 0) + reading_playfield = FALSE; - if (i < MAX_ELEMENT_CONTENTS) - level->yamyam_content[i].e[x][y] = getMappedElement_DC(element_dc); + continue; } - } - - fieldx = header[6] | (header[7] << 8); - fieldy = header[8] | (header[9] << 8); - level->fieldx = MIN(MAX(MIN_LEV_FIELDX, fieldx), MAX_LEV_FIELDX); - level->fieldy = MIN(MAX(MIN_LEV_FIELDY, fieldy), MAX_LEV_FIELDY); - for (y = 0; y < fieldy; y++) for (x = 0; x < fieldx; x++) - { - unsigned short word = getDecodedWord_DC(getFile16BitBE(file), FALSE); -#if 1 - int element_dc = ((word & 0xff) << 8) | ((word >> 8) & 0xff); -#else - int element_dc = word; -#endif - - if (x < MAX_LEV_FIELDX && y < MAX_LEV_FIELDY) - level->field[x][y] = getMappedElement_DC(element_dc); + /* continue with next tile row */ + y++; } - x = MIN(MAX(0, (header[10] | (header[11] << 8)) - 1), MAX_LEV_FIELDX - 1); - y = MIN(MAX(0, (header[12] | (header[13] << 8)) - 1), MAX_LEV_FIELDY - 1); - level->field[x][y] = EL_PLAYER_1; - - x = MIN(MAX(0, (header[14] | (header[15] << 8)) - 1), MAX_LEV_FIELDX - 1); - y = MIN(MAX(0, (header[16] | (header[17] << 8)) - 1), MAX_LEV_FIELDY - 1); - level->field[x][y] = EL_PLAYER_2; - - level->gems_needed = header[18] | (header[19] << 8); + closeFile(file); - level->score[SC_EMERALD] = header[20] | (header[21] << 8); - level->score[SC_DIAMOND] = header[22] | (header[23] << 8); - level->score[SC_PEARL] = header[24] | (header[25] << 8); - level->score[SC_CRYSTAL] = header[26] | (header[27] << 8); - level->score[SC_NUT] = header[28] | (header[29] << 8); - level->score[SC_ROBOT] = header[30] | (header[31] << 8); - level->score[SC_SPACESHIP] = header[32] | (header[33] << 8); - level->score[SC_BUG] = header[34] | (header[35] << 8); - level->score[SC_YAMYAM] = header[36] | (header[37] << 8); - level->score[SC_DYNAMITE] = header[38] | (header[39] << 8); - level->score[SC_KEY] = header[40] | (header[41] << 8); - level->score[SC_TIME_BONUS] = header[42] | (header[43] << 8); + level->fieldy = y; - level->time = header[44] | (header[45] << 8); + level->fieldx = MIN(MAX(MIN_LEV_FIELDX, level->fieldx), MAX_LEV_FIELDX); + level->fieldy = MIN(MAX(MIN_LEV_FIELDY, level->fieldy), MAX_LEV_FIELDY); - level->amoeba_speed = header[46] | (header[47] << 8); - level->time_light = header[48] | (header[49] << 8); - level->time_timegate = header[50] | (header[51] << 8); - level->time_wheel = header[52] | (header[53] << 8); - level->time_magic_wall = header[54] | (header[55] << 8); - level->extra_time = header[56] | (header[57] << 8); - level->shield_normal_time = header[58] | (header[59] << 8); + if (!reading_playfield) + { + level->no_valid_file = TRUE; - fclose(file); + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); - /* Diamond Caves has the same (strange) behaviour as Emerald Mine that gems - can slip down from flat walls, like normal walls and steel walls */ - level->em_slippery_gems = TRUE; + return; + } + + if (*level_name != '\0') + { + strncpy(level->name, level_name, MAX_LEVEL_NAME_LEN); + level->name[MAX_LEVEL_NAME_LEN] = '\0'; #if 0 - /* Diamond Caves II levels are always surrounded by indestructible wall, but - not necessarily in a rectangular way -- fill with invisible steel wall */ + printf(":1: level name: '%s'\n", level->name); +#endif + } + else if (*last_comment != '\0') + { + strncpy(level->name, last_comment, MAX_LEVEL_NAME_LEN); + level->name[MAX_LEVEL_NAME_LEN] = '\0'; - /* !!! not always true !!! keep level and set BorderElement instead !!! */ +#if 0 + printf(":2: level name: '%s'\n", level->name); +#endif + } + else + { + sprintf(level->name, "--> Level %d <--", level_file_info->nr); + } + /* set all empty fields beyond the border walls to invisible steel wall */ for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++) { -#if 1 if ((x == 0 || x == level->fieldx - 1 || y == 0 || y == level->fieldy - 1) && - level->field[x][y] == EL_EMPTY) - level->field[x][y] = EL_INVISIBLE_STEELWALL; -#else - if ((x == 0 || x == level->fieldx - 1 || - y == 0 || y == level->fieldy - 1) && - level->field[x][y] == EL_EMPTY) - FloodFillLevel(x, y, EL_INVISIBLE_STEELWALL, + level->field[x][y] == getMappedElement_SB(' ', load_xsb_to_ces)) + FloodFillLevel(x, y, getMappedElement_SB('_', load_xsb_to_ces), level->field, level->fieldx, level->fieldy); -#endif } -#endif -} - -#endif + /* set special level settings for Sokoban levels */ -/* ------------------------------------------------------------------------- */ -/* functions for loading SB level */ -/* ------------------------------------------------------------------------- */ + level->time = 0; + level->use_step_counter = TRUE; -int getMappedElement_SB(int element_ascii, boolean use_ces) -{ - static struct - { - int ascii; - int sb; - int ce; - } - sb_element_mapping[] = + if (load_xsb_to_ces) { - { ' ', EL_EMPTY, EL_CUSTOM_1 }, /* floor (space) */ - { '#', EL_STEELWALL, EL_CUSTOM_2 }, /* wall */ - { '@', EL_PLAYER_1, EL_CUSTOM_3 }, /* player */ - { '$', EL_SOKOBAN_OBJECT, EL_CUSTOM_4 }, /* box */ - { '.', EL_SOKOBAN_FIELD_EMPTY, EL_CUSTOM_5 }, /* goal square */ - { '*', EL_SOKOBAN_FIELD_FULL, EL_CUSTOM_6 }, /* box on goal square */ - { '+', EL_SOKOBAN_FIELD_PLAYER, EL_CUSTOM_7 }, /* player on goal square */ -#if 0 - { '_', EL_INVISIBLE_STEELWALL, EL_CUSTOM_8 }, /* floor beyond border */ +#if 1 + /* !!! special global settings can now be set in level template !!! */ #else - { '_', EL_INVISIBLE_STEELWALL, EL_FROM_LEVEL_TEMPLATE }, /* floor beyond border */ + level->initial_player_stepsize[0] = STEPSIZE_SLOW; #endif - { 0, -1, -1 }, - }; + /* fill smaller playfields with padding "beyond border wall" elements */ + if (level->fieldx < SCR_FIELDX || + level->fieldy < SCR_FIELDY) + { + short field[level->fieldx][level->fieldy]; + int new_fieldx = MAX(level->fieldx, SCR_FIELDX); + int new_fieldy = MAX(level->fieldy, SCR_FIELDY); + int pos_fieldx = (new_fieldx - level->fieldx) / 2; + int pos_fieldy = (new_fieldy - level->fieldy) / 2; - int i; + /* copy old playfield (which is smaller than the visible area) */ + for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++) + field[x][y] = level->field[x][y]; - for (i = 0; sb_element_mapping[i].ascii != 0; i++) - if (element_ascii == sb_element_mapping[i].ascii) - return (use_ces ? sb_element_mapping[i].ce : sb_element_mapping[i].sb); + /* fill new, larger playfield with "beyond border wall" elements */ + for (y = 0; y < new_fieldy; y++) for (x = 0; x < new_fieldx; x++) + level->field[x][y] = getMappedElement_SB('_', load_xsb_to_ces); - return EL_UNDEFINED; + /* copy the old playfield to the middle of the new playfield */ + for (y = 0; y < level->fieldy; y++) for (x = 0; x < level->fieldx; x++) + level->field[pos_fieldx + x][pos_fieldy + y] = field[x][y]; + + level->fieldx = new_fieldx; + level->fieldy = new_fieldy; + } + + level->use_custom_template = TRUE; + } } +#else + static void LoadLevelFromFileInfo_SB(struct LevelInfo *level, struct LevelFileInfo *level_file_info, boolean level_info_only) @@ -7093,6 +8771,8 @@ static void LoadLevelFromFileInfo_SB(struct LevelInfo *level, } } +#endif + /* ------------------------------------------------------------------------- */ /* functions for handling native levels */ @@ -8557,28 +10237,183 @@ static void setTapeInfoToDefaults() { int i; - /* always start with reliable default values (empty tape) */ - TapeErase(); + /* always start with reliable default values (empty tape) */ + TapeErase(); + + /* default values (also for pre-1.2 tapes) with only the first player */ + tape.player_participates[0] = TRUE; + for (i = 1; i < MAX_PLAYERS; i++) + tape.player_participates[i] = FALSE; + + /* at least one (default: the first) player participates in every tape */ + tape.num_participating_players = 1; + + tape.level_nr = level_nr; + tape.counter = 0; + tape.changed = FALSE; + + tape.recording = FALSE; + tape.playing = FALSE; + tape.pausing = FALSE; + + tape.no_valid_file = FALSE; +} + +#if 1 + +static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape) +{ + tape->file_version = getFileVersion(file); + tape->game_version = getFileVersion(file); + + return chunk_size; +} + +static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape) +{ + int i; + + tape->random_seed = getFile32BitBE(file); + tape->date = getFile32BitBE(file); + tape->length = getFile32BitBE(file); + + /* read header fields that are new since version 1.2 */ + if (tape->file_version >= FILE_VERSION_1_2) + { + byte store_participating_players = getFile8Bit(file); + int engine_version; + + /* since version 1.2, tapes store which players participate in the tape */ + tape->num_participating_players = 0; + for (i = 0; i < MAX_PLAYERS; i++) + { + tape->player_participates[i] = FALSE; + + if (store_participating_players & (1 << i)) + { + tape->player_participates[i] = TRUE; + tape->num_participating_players++; + } + } + + ReadUnusedBytesFromFile(file, TAPE_CHUNK_HEAD_UNUSED); + + engine_version = getFileVersion(file); + if (engine_version > 0) + tape->engine_version = engine_version; + else + tape->engine_version = tape->game_version; + } + + return chunk_size; +} + +static int LoadTape_INFO(File *file, int chunk_size, struct TapeInfo *tape) +{ + int level_identifier_size; + int i; + + level_identifier_size = getFile16BitBE(file); + + tape->level_identifier = + checked_realloc(tape->level_identifier, level_identifier_size); + + for (i = 0; i < level_identifier_size; i++) + tape->level_identifier[i] = getFile8Bit(file); + + tape->level_nr = getFile16BitBE(file); + + chunk_size = 2 + level_identifier_size + 2; + + return chunk_size; +} + +static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape) +{ + int i, j; + int chunk_size_expected = + (tape->num_participating_players + 1) * tape->length; + + if (chunk_size_expected != chunk_size) + { + ReadUnusedBytesFromFile(file, chunk_size); + return chunk_size_expected; + } + + for (i = 0; i < tape->length; i++) + { + if (i >= MAX_TAPE_LEN) + break; + + for (j = 0; j < MAX_PLAYERS; j++) + { + tape->pos[i].action[j] = MV_NONE; + + if (tape->player_participates[j]) + tape->pos[i].action[j] = getFile8Bit(file); + } + + tape->pos[i].delay = getFile8Bit(file); + + if (tape->file_version == FILE_VERSION_1_0) + { + /* eliminate possible diagonal moves in old tapes */ + /* this is only for backward compatibility */ + + byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN }; + byte action = tape->pos[i].action[0]; + int k, num_moves = 0; + + for (k = 0; k<4; k++) + { + if (action & joy_dir[k]) + { + tape->pos[i + num_moves].action[0] = joy_dir[k]; + if (num_moves > 0) + tape->pos[i + num_moves].delay = 0; + num_moves++; + } + } + + if (num_moves > 1) + { + num_moves--; + i += num_moves; + tape->length += num_moves; + } + } + else if (tape->file_version < FILE_VERSION_2_0) + { + /* convert pre-2.0 tapes to new tape format */ + + if (tape->pos[i].delay > 1) + { + /* action part */ + tape->pos[i + 1] = tape->pos[i]; + tape->pos[i + 1].delay = 1; - /* default values (also for pre-1.2 tapes) with only the first player */ - tape.player_participates[0] = TRUE; - for (i = 1; i < MAX_PLAYERS; i++) - tape.player_participates[i] = FALSE; + /* delay part */ + for (j = 0; j < MAX_PLAYERS; j++) + tape->pos[i].action[j] = MV_NONE; + tape->pos[i].delay--; - /* at least one (default: the first) player participates in every tape */ - tape.num_participating_players = 1; + i++; + tape->length++; + } + } - tape.level_nr = level_nr; - tape.counter = 0; - tape.changed = FALSE; + if (checkEndOfFile(file)) + break; + } - tape.recording = FALSE; - tape.playing = FALSE; - tape.pausing = FALSE; + if (i != tape->length) + chunk_size = (tape->num_participating_players + 1) * i; - tape.no_valid_file = FALSE; + return chunk_size; } +#else + static int LoadTape_VERS(FILE *file, int chunk_size, struct TapeInfo *tape) { tape->file_version = getFileVersion(file); @@ -8730,6 +10565,85 @@ static int LoadTape_BODY(FILE *file, int chunk_size, struct TapeInfo *tape) return chunk_size; } +#endif + +#if 1 + +void LoadTape_SokobanSolution(char *filename) +{ + File *file; + int move_delay = TILESIZE / level.initial_player_stepsize[0]; + + if (!(file = openFile(filename, MODE_READ))) + { + tape.no_valid_file = TRUE; + + return; + } + + while (!checkEndOfFile(file)) + { + unsigned char c = getByteFromFile(file); + + if (checkEndOfFile(file)) + break; + + switch (c) + { + case 'u': + case 'U': + tape.pos[tape.length].action[0] = MV_UP; + tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0); + tape.length++; + break; + + case 'd': + case 'D': + tape.pos[tape.length].action[0] = MV_DOWN; + tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0); + tape.length++; + break; + + case 'l': + case 'L': + tape.pos[tape.length].action[0] = MV_LEFT; + tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0); + tape.length++; + break; + + case 'r': + case 'R': + tape.pos[tape.length].action[0] = MV_RIGHT; + tape.pos[tape.length].delay = move_delay + (c < 'a' ? 2 : 0); + tape.length++; + break; + + case '\n': + case '\r': + case '\t': + case ' ': + /* ignore white-space characters */ + break; + + default: + tape.no_valid_file = TRUE; + + Error(ERR_WARN, "unsupported Sokoban solution file '%s' ['%d']", filename, c); + + break; + } + } + + closeFile(file); + + if (tape.no_valid_file) + return; + + tape.length_seconds = GetTapeLength(); +} + +#else + void LoadTape_SokobanSolution(char *filename) { FILE *file; @@ -8803,6 +10717,160 @@ void LoadTape_SokobanSolution(char *filename) tape.length_seconds = GetTapeLength(); } +#endif + +#if 1 + +void LoadTapeFromFilename(char *filename) +{ + char cookie[MAX_LINE_LEN]; + char chunk_name[CHUNK_ID_LEN + 1]; + File *file; + int chunk_size; + + /* always start with reliable default values */ + setTapeInfoToDefaults(); + + if (strSuffix(filename, ".sln")) + { + LoadTape_SokobanSolution(filename); + + return; + } + + if (!(file = openFile(filename, MODE_READ))) + { + tape.no_valid_file = TRUE; + + return; + } + + getFileChunkBE(file, chunk_name, NULL); + if (strEqual(chunk_name, "RND1")) + { + getFile32BitBE(file); /* not used */ + + getFileChunkBE(file, chunk_name, NULL); + if (!strEqual(chunk_name, "TAPE")) + { + tape.no_valid_file = TRUE; + + Error(ERR_WARN, "unknown format of tape file '%s'", filename); + + closeFile(file); + + return; + } + } + else /* check for pre-2.0 file format with cookie string */ + { + strcpy(cookie, chunk_name); + if (getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4) == NULL) + cookie[4] = '\0'; + if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; + + if (!checkCookieString(cookie, TAPE_COOKIE_TMPL)) + { + tape.no_valid_file = TRUE; + + Error(ERR_WARN, "unknown format of tape file '%s'", filename); + + closeFile(file); + + return; + } + + if ((tape.file_version = getFileVersionFromCookieString(cookie)) == -1) + { + tape.no_valid_file = TRUE; + + Error(ERR_WARN, "unsupported version of tape file '%s'", filename); + + closeFile(file); + + return; + } + + /* pre-2.0 tape files have no game version, so use file version here */ + tape.game_version = tape.file_version; + } + + if (tape.file_version < FILE_VERSION_1_2) + { + /* tape files from versions before 1.2.0 without chunk structure */ + LoadTape_HEAD(file, TAPE_CHUNK_HEAD_SIZE, &tape); + LoadTape_BODY(file, 2 * tape.length, &tape); + } + else + { + static struct + { + char *name; + int size; + int (*loader)(File *, int, struct TapeInfo *); + } + chunk_info[] = + { + { "VERS", TAPE_CHUNK_VERS_SIZE, LoadTape_VERS }, + { "HEAD", TAPE_CHUNK_HEAD_SIZE, LoadTape_HEAD }, + { "INFO", -1, LoadTape_INFO }, + { "BODY", -1, LoadTape_BODY }, + { NULL, 0, NULL } + }; + + while (getFileChunkBE(file, chunk_name, &chunk_size)) + { + int i = 0; + + while (chunk_info[i].name != NULL && + !strEqual(chunk_name, chunk_info[i].name)) + i++; + + if (chunk_info[i].name == NULL) + { + Error(ERR_WARN, "unknown chunk '%s' in tape 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 tape file '%s'", + chunk_size, chunk_name, filename); + ReadUnusedBytesFromFile(file, chunk_size); + } + else + { + /* call function to load this tape chunk */ + int chunk_size_expected = + (chunk_info[i].loader)(file, chunk_size, &tape); + + /* 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 tape file '%s'", + chunk_size, chunk_name, filename); + } + } + } + } + + closeFile(file); + + tape.length_seconds = GetTapeLength(); + +#if 0 + printf("::: tape file version: %d\n", tape.file_version); + printf("::: tape game version: %d\n", tape.game_version); + printf("::: tape engine version: %d\n", tape.engine_version); +#endif +} + +#else + void LoadTapeFromFilename(char *filename) { char cookie[MAX_LINE_LEN]; @@ -8885,7 +10953,7 @@ void LoadTapeFromFilename(char *filename) { char *name; int size; - int (*loader)(FILE *, int, struct TapeInfo *); + int (*loader)(File *, int, struct TapeInfo *); } chunk_info[] = { @@ -8946,6 +11014,8 @@ void LoadTapeFromFilename(char *filename) #endif } +#endif + void LoadTape(int nr) { char *filename = getTapeFilename(nr); diff --git a/src/game_em/cave.c b/src/game_em/cave.c index b816aff2..09cc5955 100644 --- a/src/game_em/cave.c +++ b/src/game_em/cave.c @@ -74,6 +74,54 @@ void setLevelInfoToDefaults_EM(void) #define MAX_EM_LEVEL_SIZE 16384 +#if 1 + +boolean LoadNativeLevel_EM(char *filename, boolean level_info_only) +{ + unsigned char raw_leveldata[MAX_EM_LEVEL_SIZE]; + int raw_leveldata_length; + int file_version; + File *file; + + /* always start with reliable default values */ + setLevelInfoToDefaults_EM(); + + if (!(file = openFile(filename, MODE_READ))) + { + if (!level_info_only) + Error(ERR_WARN, "cannot open level '%s' -- using empty level", filename); + + return FALSE; + } + + raw_leveldata_length = readFile(file, raw_leveldata, 1, MAX_EM_LEVEL_SIZE); + + closeFile(file); + + if (raw_leveldata_length <= 0) + { + Error(ERR_WARN, "cannot read level '%s' -- using empty level", filename); + + return FALSE; + } + + file_version = cleanup_em_level(raw_leveldata, raw_leveldata_length,filename); + + if (file_version == FILE_VERSION_EM_UNKNOWN) + { + Error(ERR_WARN, "unknown EM level '%s' -- using empty level", filename); + + return FALSE; + } + + convert_em_level(raw_leveldata, file_version); + prepare_em_level(); + + return TRUE; +} + +#else + boolean LoadNativeLevel_EM(char *filename, boolean level_info_only) { unsigned char raw_leveldata[MAX_EM_LEVEL_SIZE]; @@ -117,3 +165,5 @@ boolean LoadNativeLevel_EM(char *filename, boolean level_info_only) return TRUE; } + +#endif diff --git a/src/game_sp/file.c b/src/game_sp/file.c index 53eb22ff..438e7664 100644 --- a/src/game_sp/file.c +++ b/src/game_sp/file.c @@ -168,6 +168,140 @@ void copyInternalEngineVars_SP() LevelLoaded = True; } +#if 1 + +static void LoadNativeLevelFromFileStream_SP(File *file, int width, int height, + boolean demo_available) +{ + LevelInfoType *header = &native_sp_level.header; + int i, x, y; + + /* for details of the Supaplex level format, see Herman Perk's Supaplex + documentation file "SPFIX63.DOC" from his Supaplex "SpeedFix" package */ + + native_sp_level.width = MIN(width, SP_MAX_PLAYFIELD_WIDTH); + native_sp_level.height = MIN(height, SP_MAX_PLAYFIELD_HEIGHT); + + /* read level playfield (width * height == 60 * 24 tiles == 1440 bytes) */ + /* (MPX levels may have non-standard playfield size -- check max. size) */ + for (y = 0; y < height; y++) + { + for (x = 0; x < width; x++) + { + byte element = getFile8Bit(file); + + if (x < SP_MAX_PLAYFIELD_WIDTH && + y < SP_MAX_PLAYFIELD_HEIGHT) + native_sp_level.playfield[x][y] = element; + } + } + + /* read level header (96 bytes) */ + + ReadUnusedBytesFromFile(file, 4); /* (not used by Supaplex engine) */ + + /* initial gravity: 1 == "on", anything else (0) == "off" */ + header->InitialGravity = getFile8Bit(file); + + /* SpeedFixVersion XOR 0x20 */ + header->Version = getFile8Bit(file); + + /* level title in uppercase letters, padded with dashes ("-") (23 bytes) */ + for (i = 0; i < SP_LEVEL_NAME_LEN; i++) + header->LevelTitle[i] = getFile8Bit(file); + + /* initial "freeze zonks": 2 == "on", anything else (0, 1) == "off" */ + header->InitialFreezeZonks = getFile8Bit(file); + + /* number of infotrons needed; 0 means that Supaplex will count the total + amount of infotrons in the level and use the low byte of that number + (a multiple of 256 infotrons will result in "0 infotrons needed"!) */ + header->InfotronsNeeded = getFile8Bit(file); + + /* number of special ("gravity") port entries below (maximum 10 allowed) */ + header->SpecialPortCount = getFile8Bit(file); + + /* database of properties of up to 10 special ports (6 bytes per port) */ + for (i = 0; i < SP_MAX_SPECIAL_PORTS; i++) + { + SpecialPortType *port = &header->SpecialPort[i]; + + /* high and low byte of the location of a special port; if (x, y) are the + coordinates of a port in the field and (0, 0) is the top-left corner, + the 16 bit value here calculates as 2 * (x + (y * 60)) (this is twice + of what may be expected: Supaplex works with a game field in memory + which is 2 bytes per tile) */ + port->PortLocation = getFile16BitBE(file); /* yes, big endian */ + + /* change gravity: 1 == "turn on", anything else (0) == "turn off" */ + port->Gravity = getFile8Bit(file); + + /* "freeze zonks": 2 == "turn on", anything else (0, 1) == "turn off" */ + port->FreezeZonks = getFile8Bit(file); + + /* "freeze enemies": 1 == "turn on", anything else (0) == "turn off" */ + port->FreezeEnemies = getFile8Bit(file); + + ReadUnusedBytesFromFile(file, 1); /* (not used by Supaplex engine) */ + } + + /* SpeedByte XOR Highbyte(RandomSeed) */ + header->SpeedByte = getFile8Bit(file); + + /* CheckSum XOR SpeedByte */ + header->CheckSumByte = getFile8Bit(file); + + /* random seed used for recorded demos */ + header->DemoRandomSeed = getFile16BitLE(file); /* yes, little endian */ + + /* auto-determine number of infotrons if it was stored as "0" -- see above */ + if (header->InfotronsNeeded == 0) + { + for (x = 0; x < native_sp_level.width; x++) + for (y = 0; y < native_sp_level.height; y++) + if (native_sp_level.playfield[x][y] == fiInfotron) + header->InfotronsNeeded++; + + header->InfotronsNeeded &= 0xff; /* only use low byte -- see above */ + } + + /* read raw level header bytes (96 bytes) */ + + seekFile(file, -(SP_HEADER_SIZE), SEEK_CUR); /* rewind file */ + for (i = 0; i < SP_HEADER_SIZE; i++) + native_sp_level.header_raw_bytes[i] = getByteFromFile(file); + + /* also load demo tape, if available (only in single level files) */ + + if (demo_available) + { + int level_nr = getFile8Bit(file); + + level_nr &= 0x7f; /* clear highest bit */ + level_nr = (level_nr < 1 ? 1 : + level_nr > 111 ? 111 : level_nr); + + native_sp_level.demo.level_nr = level_nr; + + for (i = 0; i < SP_MAX_TAPE_LEN && !checkEndOfFile(file); i++) + { + native_sp_level.demo.data[i] = getFile8Bit(file); + + if (native_sp_level.demo.data[i] == 0xff) /* "end of demo" byte */ + { + i++; + + break; + } + } + + native_sp_level.demo.length = i; + native_sp_level.demo.is_available = (native_sp_level.demo.length > 0); + } +} + +#else + static void LoadNativeLevelFromFileStream_SP(FILE *file, int width, int height, boolean demo_available) { @@ -298,6 +432,269 @@ static void LoadNativeLevelFromFileStream_SP(FILE *file, int width, int height, } } +#endif + +#if 1 + +boolean LoadNativeLevel_SP(char *filename, int level_pos, + boolean level_info_only) +{ + File *file; + int i, l, x, y; + char name_first, name_last; + struct LevelInfo_SP multipart_level; + int multipart_xpos, multipart_ypos; + boolean is_multipart_level; + boolean is_first_part; + boolean reading_multipart_level = FALSE; + boolean use_empty_level = FALSE; + LevelInfoType *header = &native_sp_level.header; + boolean is_single_level_file = (strSuffixLower(filename, ".sp") || + strSuffixLower(filename, ".mpx")); + boolean demo_available = is_single_level_file; + boolean is_mpx_file = strSuffixLower(filename, ".mpx"); + int file_seek_pos = level_pos * SP_STD_LEVEL_SIZE; + int level_width = SP_STD_PLAYFIELD_WIDTH; + int level_height = SP_STD_PLAYFIELD_HEIGHT; + + /* always start with reliable default values */ + setLevelInfoToDefaults_SP(); + copyInternalEngineVars_SP(); + + if (!(file = openFile(filename, MODE_READ))) + { + if (!level_info_only) + Error(ERR_WARN, "cannot open file '%s' -- using empty level", filename); + + return FALSE; + } + + if (is_mpx_file) + { + char mpx_chunk_name[4 + 1]; + int mpx_version; + int mpx_level_count; + LevelDescriptor *mpx_level_desc; + + getFileChunkBE(file, mpx_chunk_name, NULL); + + if (!strEqual(mpx_chunk_name, "MPX ")) + { + Error(ERR_WARN, "cannot find MPX ID in file '%s' -- using empty level", + filename); + + return FALSE; + } + + mpx_version = getFile16BitLE(file); + + if (mpx_version != 1) + { + Error(ERR_WARN, "unknown MPX version in file '%s' -- using empty level", + filename); + + return FALSE; + } + + mpx_level_count = getFile16BitLE(file); + + if (mpx_level_count < 1) + { + Error(ERR_WARN, "no MPX levels found in file '%s' -- using empty level", + filename); + + return FALSE; + } + + if (level_pos >= mpx_level_count) + { + Error(ERR_WARN, "MPX level not found in file '%s' -- using empty level", + filename); + + return FALSE; + } + + mpx_level_desc = checked_calloc(mpx_level_count * sizeof(LevelDescriptor)); + + for (i = 0; i < mpx_level_count; i++) + { + LevelDescriptor *ldesc = &mpx_level_desc[i]; + + ldesc->Width = getFile16BitLE(file); + ldesc->Height = getFile16BitLE(file); + ldesc->OffSet = getFile32BitLE(file); /* starts with 1, not with 0 */ + ldesc->Size = getFile32BitLE(file); + } + + level_width = mpx_level_desc[level_pos].Width; + level_height = mpx_level_desc[level_pos].Height; + + file_seek_pos = mpx_level_desc[level_pos].OffSet - 1; + } + + /* position file stream to the requested level (in case of level package) */ + if (seekFile(file, file_seek_pos, SEEK_SET) != 0) + { + Error(ERR_WARN, "cannot fseek in file '%s' -- using empty level", filename); + + return FALSE; + } + + /* there exist Supaplex level package files with multi-part levels which + can be detected as follows: instead of leading and trailing dashes ('-') + to pad the level name, they have leading and trailing numbers which are + the x and y coordinations of the current part of the multi-part level; + if there are '?' characters instead of numbers on the left or right side + of the level name, the multi-part level consists of only horizontal or + vertical parts */ + + for (l = level_pos; l < SP_NUM_LEVELS_PER_PACKAGE; l++) + { + LoadNativeLevelFromFileStream_SP(file, level_width, level_height, + demo_available); + + /* check if this level is a part of a bigger multi-part level */ + + if (is_single_level_file) + break; + + name_first = header->LevelTitle[0]; + name_last = header->LevelTitle[SP_LEVEL_NAME_LEN - 1]; + + is_multipart_level = + ((name_first == '?' || (name_first >= '0' && name_first <= '9')) && + (name_last == '?' || (name_last >= '0' && name_last <= '9'))); + + is_first_part = + ((name_first == '?' || name_first == '1') && + (name_last == '?' || name_last == '1')); + + if (is_multipart_level) + { + /* correct leading multipart level meta information in level name */ + for (i = 0; + i < SP_LEVEL_NAME_LEN && header->LevelTitle[i] == name_first; + i++) + header->LevelTitle[i] = '-'; + + /* correct trailing multipart level meta information in level name */ + for (i = SP_LEVEL_NAME_LEN - 1; + i >= 0 && header->LevelTitle[i] == name_last; + i--) + header->LevelTitle[i] = '-'; + } + + /* ---------- check for normal single level ---------- */ + + if (!reading_multipart_level && !is_multipart_level) + { + /* the current level is simply a normal single-part level, and we are + not reading a multi-part level yet, so return the level as it is */ + + break; + } + + /* ---------- check for empty level (unused multi-part) ---------- */ + + if (!reading_multipart_level && is_multipart_level && !is_first_part) + { + /* this is a part of a multi-part level, but not the first part + (and we are not already reading parts of a multi-part level); + in this case, use an empty level instead of the single part */ + + use_empty_level = TRUE; + + break; + } + + /* ---------- check for finished multi-part level ---------- */ + + if (reading_multipart_level && + (!is_multipart_level || + !strEqualN(header->LevelTitle, multipart_level.header.LevelTitle, + SP_LEVEL_NAME_LEN))) + { + /* we are already reading parts of a multi-part level, but this level is + either not a multi-part level, or a part of a different multi-part + level; in both cases, the multi-part level seems to be complete */ + + break; + } + + /* ---------- here we have one part of a multi-part level ---------- */ + + reading_multipart_level = TRUE; + + if (is_first_part) /* start with first part of new multi-part level */ + { + /* copy level info structure from first part */ + multipart_level = native_sp_level; + + /* clear playfield of new multi-part level */ + for (x = 0; x < SP_MAX_PLAYFIELD_WIDTH; x++) + for (y = 0; y < SP_MAX_PLAYFIELD_HEIGHT; y++) + multipart_level.playfield[x][y] = fiSpace; + } + + if (name_first == '?') + name_first = '1'; + if (name_last == '?') + name_last = '1'; + + multipart_xpos = (int)(name_first - '0'); + multipart_ypos = (int)(name_last - '0'); + +#if 0 + printf("----------> part (%d/%d) of multi-part level '%s'\n", + multipart_xpos, multipart_ypos, multipart_level.header.LevelTitle); +#endif + + if (multipart_xpos * SP_STD_PLAYFIELD_WIDTH > SP_MAX_PLAYFIELD_WIDTH || + multipart_ypos * SP_STD_PLAYFIELD_HEIGHT > SP_MAX_PLAYFIELD_HEIGHT) + { + Error(ERR_WARN, "multi-part level is too big -- ignoring part of it"); + + break; + } + + multipart_level.width = MAX(multipart_level.width, + multipart_xpos * SP_STD_PLAYFIELD_WIDTH); + multipart_level.height = MAX(multipart_level.height, + multipart_ypos * SP_STD_PLAYFIELD_HEIGHT); + + /* copy level part at the right position of multi-part level */ + for (x = 0; x < SP_STD_PLAYFIELD_WIDTH; x++) + { + for (y = 0; y < SP_STD_PLAYFIELD_HEIGHT; y++) + { + int start_x = (multipart_xpos - 1) * SP_STD_PLAYFIELD_WIDTH; + int start_y = (multipart_ypos - 1) * SP_STD_PLAYFIELD_HEIGHT; + + multipart_level.playfield[start_x + x][start_y + y] = + native_sp_level.playfield[x][y]; + } + } + } + + closeFile(file); + + if (use_empty_level) + { + setLevelInfoToDefaults_SP(); + + Error(ERR_WARN, "single part of multi-part level -- using empty level"); + } + + if (reading_multipart_level) + native_sp_level = multipart_level; + + copyInternalEngineVars_SP(); + + return TRUE; +} + +#else + boolean LoadNativeLevel_SP(char *filename, int level_pos, boolean level_info_only) { @@ -555,6 +952,8 @@ boolean LoadNativeLevel_SP(char *filename, int level_pos, return TRUE; } +#endif + void SaveNativeLevel_SP(char *filename) { LevelInfoType *header = &native_sp_level.header; diff --git a/src/libgame/misc.c b/src/libgame/misc.c index eda87185..710917a3 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -1208,11 +1208,22 @@ inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2) of the (not yet written) chunk, write the correct chunk size and finally write the chunk itself */ +#if 1 + +int getFile8BitInteger(File *file) +{ + return getByteFromFile(file); +} + +#else + int getFile8BitInteger(FILE *file) { return fgetc(file); } +#endif + int putFile8BitInteger(FILE *file, int value) { if (file != NULL) @@ -1221,6 +1232,20 @@ int putFile8BitInteger(FILE *file, int value) return 1; } +#if 1 + +int getFile16BitInteger(File *file, int byte_order) +{ + if (byte_order == BYTE_ORDER_BIG_ENDIAN) + return ((getByteFromFile(file) << 8) | + (getByteFromFile(file) << 0)); + else /* BYTE_ORDER_LITTLE_ENDIAN */ + return ((getByteFromFile(file) << 0) | + (getByteFromFile(file) << 8)); +} + +#else + int getFile16BitInteger(FILE *file, int byte_order) { if (byte_order == BYTE_ORDER_BIG_ENDIAN) @@ -1231,6 +1256,8 @@ int getFile16BitInteger(FILE *file, int byte_order) (fgetc(file) << 8)); } +#endif + int putFile16BitInteger(FILE *file, int value, int byte_order) { if (file != NULL) @@ -1250,6 +1277,24 @@ int putFile16BitInteger(FILE *file, int value, int byte_order) return 2; } +#if 1 + +int getFile32BitInteger(File *file, int byte_order) +{ + if (byte_order == BYTE_ORDER_BIG_ENDIAN) + return ((getByteFromFile(file) << 24) | + (getByteFromFile(file) << 16) | + (getByteFromFile(file) << 8) | + (getByteFromFile(file) << 0)); + else /* BYTE_ORDER_LITTLE_ENDIAN */ + return ((getByteFromFile(file) << 0) | + (getByteFromFile(file) << 8) | + (getByteFromFile(file) << 16) | + (getByteFromFile(file) << 24)); +} + +#else + int getFile32BitInteger(FILE *file, int byte_order) { if (byte_order == BYTE_ORDER_BIG_ENDIAN) @@ -1264,6 +1309,8 @@ int getFile32BitInteger(FILE *file, int byte_order) (fgetc(file) << 24)); } +#endif + int putFile32BitInteger(FILE *file, int value, int byte_order) { if (file != NULL) @@ -1287,6 +1334,28 @@ int putFile32BitInteger(FILE *file, int value, int byte_order) return 4; } +#if 1 + +boolean getFileChunk(File *file, char *chunk_name, int *chunk_size, + int byte_order) +{ + const int chunk_name_length = 4; + + /* read chunk name */ + if (getStringFromFile(file, chunk_name, chunk_name_length + 1) == NULL) + return FALSE; + + if (chunk_size != NULL) + { + /* read chunk size */ + *chunk_size = getFile32BitInteger(file, byte_order); + } + + return (checkEndOfFile(file) ? FALSE : TRUE); +} + +#else + boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size, int byte_order) { @@ -1305,6 +1374,8 @@ boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size, return (feof(file) || ferror(file) ? FALSE : TRUE); } +#endif + int putFileChunk(FILE *file, char *chunk_name, int chunk_size, int byte_order) { @@ -1328,6 +1399,21 @@ int putFileChunk(FILE *file, char *chunk_name, int chunk_size, return num_bytes; } +#if 1 + +int getFileVersion(File *file) +{ + int version_major = getByteFromFile(file); + int version_minor = getByteFromFile(file); + int version_patch = getByteFromFile(file); + int version_build = getByteFromFile(file); + + return VERSION_IDENT(version_major, version_minor, version_patch, + version_build); +} + +#else + int getFileVersion(FILE *file) { int version_major = fgetc(file); @@ -1339,6 +1425,8 @@ int getFileVersion(FILE *file) version_build); } +#endif + int putFileVersion(FILE *file, int version) { if (file != NULL) @@ -1357,6 +1445,18 @@ int putFileVersion(FILE *file, int version) return 4; } +#if 1 + +void ReadBytesFromFile(File *file, byte *buffer, unsigned int bytes) +{ + int i; + + for (i = 0; i < bytes && !checkEndOfFile(file); i++) + buffer[i] = getByteFromFile(file); +} + +#else + void ReadBytesFromFile(FILE *file, byte *buffer, unsigned int bytes) { int i; @@ -1365,6 +1465,8 @@ void ReadBytesFromFile(FILE *file, byte *buffer, unsigned int bytes) buffer[i] = fgetc(file); } +#endif + void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes) { int i; @@ -1373,12 +1475,24 @@ void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes) fputc(buffer[i], file); } +#if 1 + +void ReadUnusedBytesFromFile(File *file, unsigned int bytes) +{ + while (bytes-- && !checkEndOfFile(file)) + getByteFromFile(file); +} + +#else + void ReadUnusedBytesFromFile(FILE *file, unsigned int bytes) { while (bytes-- && !feof(file)) fgetc(file); } +#endif + void WriteUnusedBytesToFile(FILE *file, unsigned int bytes) { while (bytes--) @@ -1967,6 +2081,64 @@ int checkEndOfFile(File *file) return feof(file->file); } +size_t readFile(File *file, void *buffer, size_t item_size, size_t num_items) +{ +#if defined(PLATFORM_ANDROID) + if (file->file_is_asset) + { + if (file->end_of_file) + return 0; + + size_t num_items_read = + SDL_RWread(file->asset_file, buffer, item_size, num_items); + + if (num_items_read < num_items) + file->end_of_file = TRUE; + + return num_items_read; + } +#endif + + return fread(buffer, item_size, num_items, file->file); +} + +int seekFile(File *file, long offset, int whence) +{ +#if defined(PLATFORM_ANDROID) + if (file->file_is_asset) + { + int sdl_whence = (whence == SEEK_SET ? RW_SEEK_SET : + whence == SEEK_CUR ? RW_SEEK_CUR : + whence == SEEK_END ? RW_SEEK_END : 0); + + return (SDL_RWseek(file->asset_file, offset, sdl_whence) == -1 ? -1 : 0); + } +#endif + + return fseek(file->file, offset, whence); +} + +int getByteFromFile(File *file) +{ +#if defined(PLATFORM_ANDROID) + if (file->file_is_asset) + { + if (file->end_of_file) + return EOF; + + byte c; + size_t num_bytes_read = SDL_RWread(file->asset_file, &c, 1, 1); + + if (num_bytes_read < 1) + file->end_of_file = TRUE; + + return (file->end_of_file ? EOF : (int)c); + } +#endif + + return fgetc(file->file); +} + char *getStringFromFile(File *file, char *line, int size) { #if defined(PLATFORM_ANDROID) diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 244a3992..465d30eb 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -180,6 +180,28 @@ void clear_mem(void *, unsigned int); void swap_numbers(int *, int *); void swap_number_pairs(int *, int *, int *, int *); +#if 1 + +int getFile8BitInteger(File *); +int putFile8BitInteger(FILE *, int); +int getFile16BitInteger(File *, int); +int putFile16BitInteger(FILE *, int, int); +int getFile32BitInteger(File *, int); +int putFile32BitInteger(FILE *, int, int); + +boolean getFileChunk(File *, char *, int *, int); +int putFileChunk(FILE *, char *, int, int); +int getFileVersion(File *); +int putFileVersion(FILE *, int); + +void ReadBytesFromFile(File *, byte *, unsigned int); +void WriteBytesToFile(FILE *, byte *, unsigned int); + +void ReadUnusedBytesFromFile(File *, unsigned int); +void WriteUnusedBytesToFile(FILE *, unsigned int); + +#else + int getFile8BitInteger(FILE *); int putFile8BitInteger(FILE *, int); int getFile16BitInteger(FILE *, int); @@ -198,6 +220,8 @@ void WriteBytesToFile(FILE *, byte *, unsigned int); void ReadUnusedBytesFromFile(FILE *, unsigned int); void WriteUnusedBytesToFile(FILE *, unsigned int); +#endif + #define getFile8Bit(f) getFile8BitInteger(f) #define putFile8Bit(f,x) putFile8BitInteger(f,x) #define getFile16BitBE(f) getFile16BitInteger(f,BYTE_ORDER_BIG_ENDIAN) @@ -234,6 +258,9 @@ int getNumNodes(ListNode *); File *openFile(char *, char *); int closeFile(File *); int checkEndOfFile(File *); +size_t readFile(File *, void *, size_t, size_t); +int seekFile(File *, long, int); +int getByteFromFile(File *); char *getStringFromFile(File *, char *, int); Directory *openDirectory(char *); diff --git a/src/libgame/text.c b/src/libgame/text.c index dd926f62..8f8bf8f6 100644 --- a/src/libgame/text.c +++ b/src/libgame/text.c @@ -448,6 +448,44 @@ void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text, #define MAX_LINES_FROM_FILE 1024 +#if 1 + +char *GetTextBufferFromFile(char *filename, int max_lines) +{ + File *file; + char *buffer; + int num_lines = 0; + + if (filename == NULL) + return NULL; + + if (!(file = openFile(filename, MODE_READ))) + return NULL; + + buffer = checked_calloc(1); /* start with valid, but empty text buffer */ + + while (!checkEndOfFile(file) && num_lines < max_lines) + { + char line[MAX_LINE_LEN]; + + /* read next line of input file */ + if (!getStringFromFile(file, line, MAX_LINE_LEN)) + break; + + buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1); + + strcat(buffer, line); + + num_lines++; + } + + closeFile(file); + + return buffer; +} + +#else + char *GetTextBufferFromFile(char *filename, int max_lines) { FILE *file; @@ -482,6 +520,8 @@ char *GetTextBufferFromFile(char *filename, int max_lines) return buffer; } +#endif + void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length, int area_xsize, int area_ysize, int mask_mode) {