1 // ============================================================================
2 // Mirror Magic -- McDuffin's Revenge
3 // ----------------------------------------------------------------------------
4 // (c) 1994-2017 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include <sys/types.h>
21 #define CHUNK_ID_LEN 4 /* IFF style chunk id length */
22 #define CHUNK_SIZE_UNDEFINED 0 /* undefined chunk size == 0 */
23 #define CHUNK_SIZE_NONE -1 /* do not write chunk size */
24 #define FILE_VERS_CHUNK_SIZE 8 /* size of file version chunk */
25 #define LEVEL_HEADER_SIZE 80 /* size of level file header */
26 #define LEVEL_HEADER_UNUSED 19 /* unused level header bytes */
28 /* file identifier strings */
29 #define LEVEL_COOKIE_TMPL "MIRRORMAGIC_LEVEL_FILE_VERSION_x.x"
30 #define SCORE_COOKIE "MIRRORMAGIC_SCORE_FILE_VERSION_1.4"
33 int default_score[LEVEL_SCORE_ELEMENTS] =
35 [SC_COLLECTIBLE] = 10,
43 /* ========================================================================= */
44 /* level file functions */
45 /* ========================================================================= */
47 static void ReadChunk_MM_VERS(File *file, int *file_version, int *game_version)
49 int file_version_major, file_version_minor, file_version_patch;
50 int game_version_major, game_version_minor, game_version_patch;
52 file_version_major = getFile8Bit(file);
53 file_version_minor = getFile8Bit(file);
54 file_version_patch = getFile8Bit(file);
55 getFile8Bit(file); /* not used */
57 game_version_major = getFile8Bit(file);
58 game_version_minor = getFile8Bit(file);
59 game_version_patch = getFile8Bit(file);
60 getFile8Bit(file); /* not used */
62 *file_version = MM_VERSION_IDENT(file_version_major,
66 *game_version = MM_VERSION_IDENT(game_version_major,
71 static void WriteChunk_MM_VERS(FILE *file, int file_version, int game_version)
73 int file_version_major = MM_VERSION_MAJOR(file_version);
74 int file_version_minor = MM_VERSION_MINOR(file_version);
75 int file_version_patch = MM_VERSION_PATCH(file_version);
76 int game_version_major = MM_VERSION_MAJOR(game_version);
77 int game_version_minor = MM_VERSION_MINOR(game_version);
78 int game_version_patch = MM_VERSION_PATCH(game_version);
80 fputc(file_version_major, file);
81 fputc(file_version_minor, file);
82 fputc(file_version_patch, file);
83 fputc(0, file); /* not used */
85 fputc(game_version_major, file);
86 fputc(game_version_minor, file);
87 fputc(game_version_patch, file);
88 fputc(0, file); /* not used */
91 void setLevelInfoToDefaults_MM()
95 native_mm_level.file_version = MM_FILE_VERSION_ACTUAL;
96 native_mm_level.game_version = MM_GAME_VERSION_ACTUAL;
98 native_mm_level.encoding_16bit_field = FALSE; /* default: only 8-bit elements */
100 lev_fieldx = native_mm_level.fieldx = STD_LEV_FIELDX;
101 lev_fieldy = native_mm_level.fieldy = STD_LEV_FIELDY;
103 for (x = 0; x < MAX_LEV_FIELDX; x++)
104 for (y = 0; y < MAX_LEV_FIELDY; y++)
105 native_mm_level.field[x][y] = Feld[x][y] = Ur[x][y] = EL_EMPTY;
107 native_mm_level.time = 100;
108 native_mm_level.kettles_needed = 0;
109 native_mm_level.auto_count_kettles = TRUE;
110 native_mm_level.amoeba_speed = 0;
111 native_mm_level.time_fuse = 25;
112 native_mm_level.time_bomb = 75;
113 native_mm_level.time_ball = 75;
114 native_mm_level.time_block = 75;
115 native_mm_level.laser_red = FALSE;
116 native_mm_level.laser_green = FALSE;
117 native_mm_level.laser_blue = TRUE;
119 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
120 native_mm_level.name[i] = '\0';
121 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
122 native_mm_level.author[i] = '\0';
124 strcpy(native_mm_level.name, NAMELESS_LEVEL_NAME);
125 strcpy(native_mm_level.author, ANONYMOUS_NAME);
127 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
128 native_mm_level.score[i] = 10;
130 native_mm_level.field[0][0] = Feld[0][0] = Ur[0][0] = EL_MCDUFFIN_RIGHT;
131 native_mm_level.field[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
132 Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
133 Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_EXIT_CLOSED;
136 static int checkLevelElement(int element)
138 if (element >= EL_FIRST_RUNTIME_EL)
140 Error(ERR_WARN, "invalid level element %d", element);
141 element = EL_CHAR_FRAGE;
147 static int LoadLevel_MM_VERS(File *file, int chunk_size,
148 struct LevelInfo_MM *level)
150 ReadChunk_MM_VERS(file, &level->file_version, &level->game_version);
155 static int LoadLevel_MM_HEAD(File *file, int chunk_size,
156 struct LevelInfo_MM *level)
161 lev_fieldx = level->fieldx = getFile8Bit(file);
162 lev_fieldy = level->fieldy = getFile8Bit(file);
164 level->time = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
165 level->kettles_needed = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
167 // one time unit was equivalent to four seconds in level files up to 2.0.x
168 if (level->file_version <= MM_FILE_VERSION_2_0)
171 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
172 level->name[i] = getFile8Bit(file);
173 level->name[MAX_LEVEL_NAME_LEN] = 0;
175 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
176 level->score[i] = getFile8Bit(file);
178 // scores were 0 and hardcoded in game engine in level files up to 2.0.x
179 if (level->file_version <= MM_FILE_VERSION_2_0)
180 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
181 if (level->score[i] == 0)
182 level->score[i] = default_score[i];
184 level->auto_count_kettles = (getFile8Bit(file) == 1 ? TRUE : FALSE);
185 level->amoeba_speed = getFile8Bit(file);
186 level->time_fuse = getFile8Bit(file);
188 // fuse time was 0 and hardcoded in game engine in level files up to 2.0.x
189 if (level->file_version <= MM_FILE_VERSION_2_0)
190 level->time_fuse = 25;
192 laser_color = getFile8Bit(file);
193 level->laser_red = (laser_color >> 2) & 0x01;
194 level->laser_green = (laser_color >> 1) & 0x01;
195 level->laser_blue = (laser_color >> 0) & 0x01;
197 level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
199 ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
204 static int LoadLevel_MM_AUTH(File *file, int chunk_size,
205 struct LevelInfo_MM *level)
209 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
210 level->author[i] = getFile8Bit(file);
211 level->author[MAX_LEVEL_NAME_LEN] = 0;
216 static int LoadLevel_MM_BODY(File *file, int chunk_size,
217 struct LevelInfo_MM *level)
220 int chunk_size_expected = level->fieldx * level->fieldy;
222 /* Note: "chunk_size" was wrong before version 2.0 when elements are
223 stored with 16-bit encoding (and should be twice as big then).
224 Even worse, playfield data was stored 16-bit when only yamyam content
225 contained 16-bit elements and vice versa. */
227 if (level->encoding_16bit_field && level->file_version >= MM_FILE_VERSION_2_0)
228 chunk_size_expected *= 2;
230 if (chunk_size_expected != chunk_size)
232 ReadUnusedBytesFromFile(file, chunk_size);
234 return chunk_size_expected;
237 for (y = 0; y < level->fieldy; y++)
238 for (x = 0; x < level->fieldx; x++)
239 native_mm_level.field[x][y] = Feld[x][y] = Ur[x][y] =
240 checkLevelElement(level->encoding_16bit_field ?
241 getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN) :
246 boolean LoadNativeLevel_MM(char *filename, boolean level_info_only)
248 char cookie[MAX_LINE_LEN];
249 char chunk_name[CHUNK_ID_LEN + 1];
257 int (*loader)(File *, int, struct LevelInfo_MM *);
261 { "VERS", FILE_VERS_CHUNK_SIZE, LoadLevel_MM_VERS },
262 { "HEAD", LEVEL_HEADER_SIZE, LoadLevel_MM_HEAD },
263 { "AUTH", MAX_LEVEL_AUTHOR_LEN, LoadLevel_MM_AUTH },
264 { "BODY", -1, LoadLevel_MM_BODY },
268 /* always start with reliable default values */
269 setLevelInfoToDefaults_MM();
271 if (!(file = openFile(filename, MODE_READ)))
273 if (!level_info_only)
274 Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
279 getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
280 if (strcmp(chunk_name, "MMII") == 0)
282 getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN); /* not used */
284 getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
285 if (strcmp(chunk_name, "CAVE") != 0)
287 Error(ERR_WARN, "unknown format of level file '%s'", filename);
294 else /* check for pre-2.0 file format with cookie string */
296 strcpy(cookie, chunk_name);
297 getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4);
298 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
299 cookie[strlen(cookie) - 1] = '\0';
301 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
303 Error(ERR_WARN, "unknown format of level file '%s'", filename);
310 if ((native_mm_level.file_version = getFileVersionFromCookieString(cookie))
313 Error(ERR_WARN, "unsupported version of level file '%s'", filename);
321 while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_BIG_ENDIAN))
325 while (chunk_info[i].name != NULL &&
326 strcmp(chunk_name, chunk_info[i].name) != 0)
329 if (chunk_info[i].name == NULL)
331 Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
332 chunk_name, filename);
334 ReadUnusedBytesFromFile(file, chunk_size);
336 else if (chunk_info[i].size != -1 &&
337 chunk_info[i].size != chunk_size)
339 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
340 chunk_size, chunk_name, filename);
342 ReadUnusedBytesFromFile(file, chunk_size);
346 /* call function to load this level chunk */
347 int chunk_size_expected =
348 (chunk_info[i].loader)(file, chunk_size, &native_mm_level);
350 /* the size of some chunks cannot be checked before reading other
351 chunks first (like "HEAD" and "BODY") that contain some header
352 information, so check them here */
353 if (chunk_size_expected != chunk_size)
354 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
355 chunk_size, chunk_name, filename);
364 static void SaveLevel_MM_HEAD(FILE *file, struct LevelInfo_MM *level)
369 fputc(level->fieldx, file);
370 fputc(level->fieldy, file);
372 putFile16BitInteger(file, level->time, BYTE_ORDER_BIG_ENDIAN);
373 putFile16BitInteger(file, level->kettles_needed, BYTE_ORDER_BIG_ENDIAN);
375 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
376 fputc(level->name[i], file);
378 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
379 fputc(level->score[i], file);
381 fputc((level->auto_count_kettles ? 1 : 0), file);
382 fputc(level->amoeba_speed, file);
383 fputc(level->time_fuse, file);
385 laser_color = ((level->laser_red << 2) |
386 (level->laser_green << 1) |
387 (level->laser_blue << 0));
388 fputc(laser_color, file);
390 fputc((level->encoding_16bit_field ? 1 : 0), file);
392 WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
395 static void SaveLevel_MM_AUTH(FILE *file, struct LevelInfo_MM *level)
399 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
400 fputc(level->author[i], file);
403 static void SaveLevel_MM_BODY(FILE *file, struct LevelInfo_MM *level)
407 for (y = 0; y < level->fieldy; y++)
408 for (x = 0; x < level->fieldx; x++)
409 if (level->encoding_16bit_field)
410 putFile16BitInteger(file, Ur[x][y], BYTE_ORDER_BIG_ENDIAN);
412 fputc(Ur[x][y], file);
415 void SaveNativeLevel_MM(char *filename)
421 if (!(file = fopen(filename, MODE_WRITE)))
423 Error(ERR_WARN, "cannot save level file '%s'", filename);
428 /* check level field for 16-bit elements */
429 native_mm_level.encoding_16bit_field = FALSE;
431 for (y = 0; y < native_mm_level.fieldy; y++)
432 for (x = 0; x < native_mm_level.fieldx; x++)
434 native_mm_level.encoding_16bit_field = TRUE;
437 native_mm_level.fieldx * native_mm_level.fieldy *
438 (native_mm_level.encoding_16bit_field ? 2 : 1);
440 putFileChunk(file, "MMII", CHUNK_SIZE_UNDEFINED, BYTE_ORDER_BIG_ENDIAN);
441 putFileChunk(file, "CAVE", CHUNK_SIZE_NONE, BYTE_ORDER_BIG_ENDIAN);
443 putFileChunk(file, "VERS", FILE_VERS_CHUNK_SIZE, BYTE_ORDER_BIG_ENDIAN);
444 WriteChunk_MM_VERS(file, MM_FILE_VERSION_ACTUAL, MM_GAME_VERSION_ACTUAL);
446 putFileChunk(file, "HEAD", LEVEL_HEADER_SIZE, BYTE_ORDER_BIG_ENDIAN);
447 SaveLevel_MM_HEAD(file, &native_mm_level);
449 putFileChunk(file, "AUTH", MAX_LEVEL_AUTHOR_LEN, BYTE_ORDER_BIG_ENDIAN);
450 SaveLevel_MM_AUTH(file, &native_mm_level);
452 putFileChunk(file, "BODY", body_chunk_size, BYTE_ORDER_BIG_ENDIAN);
453 SaveLevel_MM_BODY(file, &native_mm_level);
457 SetFilePermissions(filename, PERMS_PRIVATE);