+ 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 read level from file '%s' -- using empty level",
+ filename);
+
+ 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);
+
+ level->time = header[44] | (header[45] << 8);
+
+ 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);
+
+ 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;
+
+#if 0
+ /* Diamond Caves II levels are always surrounded by indestructible wall, but
+ not necessarily in a rectangular way -- fill with invisible steel wall */
+
+ /* !!! not always true !!! keep level and set BorderElement instead !!! */
+
+ 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
+}
+
+#endif
+
+
+/* ------------------------------------------------------------------------- */
+/* functions for loading generic level */
+/* ------------------------------------------------------------------------- */
+
+void LoadLevelFromFileInfo(struct LevelInfo *level,
+ struct LevelFileInfo *level_file_info)
+{
+ /* always start with reliable default values */
+ setLevelInfoToDefaults(level);
+
+ switch (level_file_info->type)
+ {
+ case LEVEL_FILE_TYPE_RND:
+ LoadLevelFromFileInfo_RND(level, level_file_info);
+ break;
+
+ case LEVEL_FILE_TYPE_EM:
+ LoadLevelFromFileInfo_EM(level, level_file_info);
+ level->game_engine_type = GAME_ENGINE_TYPE_EM;
+ break;
+
+ case LEVEL_FILE_TYPE_SP:
+ LoadLevelFromFileInfo_SP(level, level_file_info);
+ break;
+
+ case LEVEL_FILE_TYPE_DC:
+ LoadLevelFromFileInfo_DC(level, level_file_info);
+ break;
+
+ default:
+ LoadLevelFromFileInfo_RND(level, level_file_info);
+ break;
+ }
+
+ /* if level file is invalid, restore level structure to default values */
+ if (level->no_valid_file)
+ setLevelInfoToDefaults(level);
+
+ if (level->game_engine_type == GAME_ENGINE_TYPE_UNKNOWN)
+ level->game_engine_type = GAME_ENGINE_TYPE_RND;
+
+ if (level_file_info->type != LEVEL_FILE_TYPE_RND)
+ CopyNativeLevel_Native_to_RND(level);
+}
+
+void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
+{
+ static struct LevelFileInfo level_file_info;
+
+ /* always start with reliable default values */
+ setFileInfoToDefaults(&level_file_info);
+
+ level_file_info.nr = 0; /* unknown level number */
+ level_file_info.type = LEVEL_FILE_TYPE_RND; /* no others supported yet */
+ level_file_info.filename = filename;
+
+ LoadLevelFromFileInfo(level, &level_file_info);
+}
+
+static void LoadLevel_InitVersion(struct LevelInfo *level, char *filename)
+{
+ int i, j;
+
+ if (leveldir_current == NULL) /* only when dumping level */
+ return;
+
+ /* all engine modifications also valid for levels which use latest engine */
+ if (level->game_version < VERSION_IDENT(3,2,0,5))
+ {
+ /* time bonus score was given for 10 s instead of 1 s before 3.2.0-5 */
+ level->score[SC_TIME_BONUS] /= 10;
+ }
+
+#if 0
+ leveldir_current->latest_engine = TRUE; /* !!! TEST ONLY !!! */
+#endif
+
+ if (leveldir_current->latest_engine)
+ {
+ /* ---------- use latest game engine ----------------------------------- */
+
+ /* For all levels which are forced to use the latest game engine version
+ (normally all but user contributed, private and undefined levels), set
+ the game engine version to the actual version; this allows for actual
+ corrections in the game engine to take effect for existing, converted
+ levels (from "classic" or other existing games) to make the emulation
+ of the corresponding game more accurate, while (hopefully) not breaking
+ existing levels created from other players. */
+
+ level->game_version = GAME_VERSION_ACTUAL;
+
+ /* Set special EM style gems behaviour: EM style gems slip down from
+ normal, steel and growing wall. As this is a more fundamental change,
+ it seems better to set the default behaviour to "off" (as it is more
+ natural) and make it configurable in the level editor (as a property
+ of gem style elements). Already existing converted levels (neither
+ private nor contributed levels) are changed to the new behaviour. */
+
+ if (level->file_version < FILE_VERSION_2_0)
+ level->em_slippery_gems = TRUE;
+
+ return;
+ }
+
+ /* ---------- use game engine the level was created with ----------------- */