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 /* ========================================================================= */
34 /* level file functions */
35 /* ========================================================================= */
37 static void ReadChunk_MM_VERS(File *file, int *file_version, int *game_version)
39 int file_version_major, file_version_minor, file_version_patch;
40 int game_version_major, game_version_minor, game_version_patch;
42 file_version_major = getFile8Bit(file);
43 file_version_minor = getFile8Bit(file);
44 file_version_patch = getFile8Bit(file);
45 getFile8Bit(file); /* not used */
47 game_version_major = getFile8Bit(file);
48 game_version_minor = getFile8Bit(file);
49 game_version_patch = getFile8Bit(file);
50 getFile8Bit(file); /* not used */
52 *file_version = MM_VERSION_IDENT(file_version_major,
56 *game_version = MM_VERSION_IDENT(game_version_major,
61 static void WriteChunk_MM_VERS(FILE *file, int file_version, int game_version)
63 int file_version_major = MM_VERSION_MAJOR(file_version);
64 int file_version_minor = MM_VERSION_MINOR(file_version);
65 int file_version_patch = MM_VERSION_PATCH(file_version);
66 int game_version_major = MM_VERSION_MAJOR(game_version);
67 int game_version_minor = MM_VERSION_MINOR(game_version);
68 int game_version_patch = MM_VERSION_PATCH(game_version);
70 fputc(file_version_major, file);
71 fputc(file_version_minor, file);
72 fputc(file_version_patch, file);
73 fputc(0, file); /* not used */
75 fputc(game_version_major, file);
76 fputc(game_version_minor, file);
77 fputc(game_version_patch, file);
78 fputc(0, file); /* not used */
81 void setLevelInfoToDefaults_MM()
85 native_mm_level.file_version = MM_FILE_VERSION_ACTUAL;
86 native_mm_level.game_version = MM_GAME_VERSION_ACTUAL;
88 native_mm_level.encoding_16bit_field = FALSE; /* default: only 8-bit elements */
90 lev_fieldx = native_mm_level.fieldx = STD_LEV_FIELDX;
91 lev_fieldy = native_mm_level.fieldy = STD_LEV_FIELDY;
93 for (x = 0; x < MAX_LEV_FIELDX; x++)
94 for (y = 0; y < MAX_LEV_FIELDY; y++)
95 native_mm_level.field[x][y] = Feld[x][y] = Ur[x][y] = EL_EMPTY;
97 native_mm_level.time = 100;
98 native_mm_level.kettles_needed = 0;
99 native_mm_level.auto_count_kettles = TRUE;
100 native_mm_level.amoeba_speed = 0;
101 native_mm_level.time_fuse = 0;
102 native_mm_level.laser_red = FALSE;
103 native_mm_level.laser_green = FALSE;
104 native_mm_level.laser_blue = TRUE;
106 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
107 native_mm_level.name[i] = '\0';
108 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
109 native_mm_level.author[i] = '\0';
111 strcpy(native_mm_level.name, NAMELESS_LEVEL_NAME);
112 strcpy(native_mm_level.author, ANONYMOUS_NAME);
114 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
115 native_mm_level.score[i] = 10;
117 native_mm_level.field[0][0] = Feld[0][0] = Ur[0][0] = EL_MCDUFFIN_RIGHT;
118 native_mm_level.field[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
119 Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
120 Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_EXIT_CLOSED;
123 static int checkLevelElement(int element)
125 if (element >= EL_FIRST_RUNTIME_EL)
127 Error(ERR_WARN, "invalid level element %d", element);
128 element = EL_CHAR_FRAGE;
134 static int LoadLevel_MM_VERS(File *file, int chunk_size,
135 struct LevelInfo_MM *level)
137 ReadChunk_MM_VERS(file, &level->file_version, &level->game_version);
142 static int LoadLevel_MM_HEAD(File *file, int chunk_size,
143 struct LevelInfo_MM *level)
148 lev_fieldx = level->fieldx = getFile8Bit(file);
149 lev_fieldy = level->fieldy = getFile8Bit(file);
151 level->time = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
152 level->kettles_needed = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
154 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
155 level->name[i] = getFile8Bit(file);
156 level->name[MAX_LEVEL_NAME_LEN] = 0;
158 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
159 level->score[i] = getFile8Bit(file);
161 level->auto_count_kettles = (getFile8Bit(file) == 1 ? TRUE : FALSE);
162 level->amoeba_speed = getFile8Bit(file);
163 level->time_fuse = getFile8Bit(file);
165 laser_color = getFile8Bit(file);
166 level->laser_red = (laser_color >> 2) & 0x01;
167 level->laser_green = (laser_color >> 1) & 0x01;
168 level->laser_blue = (laser_color >> 0) & 0x01;
170 level->encoding_16bit_field = (getFile8Bit(file) == 1 ? TRUE : FALSE);
172 ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
177 static int LoadLevel_MM_AUTH(File *file, int chunk_size,
178 struct LevelInfo_MM *level)
182 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
183 level->author[i] = getFile8Bit(file);
184 level->author[MAX_LEVEL_NAME_LEN] = 0;
189 static int LoadLevel_MM_BODY(File *file, int chunk_size,
190 struct LevelInfo_MM *level)
193 int chunk_size_expected = level->fieldx * level->fieldy;
195 /* Note: "chunk_size" was wrong before version 2.0 when elements are
196 stored with 16-bit encoding (and should be twice as big then).
197 Even worse, playfield data was stored 16-bit when only yamyam content
198 contained 16-bit elements and vice versa. */
200 if (level->encoding_16bit_field && level->file_version >= MM_FILE_VERSION_2_0)
201 chunk_size_expected *= 2;
203 if (chunk_size_expected != chunk_size)
205 ReadUnusedBytesFromFile(file, chunk_size);
207 return chunk_size_expected;
210 for (y = 0; y < level->fieldy; y++)
211 for (x = 0; x < level->fieldx; x++)
212 native_mm_level.field[x][y] = Feld[x][y] = Ur[x][y] =
213 checkLevelElement(level->encoding_16bit_field ?
214 getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN) :
219 boolean LoadNativeLevel_MM(char *filename, boolean level_info_only)
221 char cookie[MAX_LINE_LEN];
222 char chunk_name[CHUNK_ID_LEN + 1];
230 int (*loader)(File *, int, struct LevelInfo_MM *);
234 { "VERS", FILE_VERS_CHUNK_SIZE, LoadLevel_MM_VERS },
235 { "HEAD", LEVEL_HEADER_SIZE, LoadLevel_MM_HEAD },
236 { "AUTH", MAX_LEVEL_AUTHOR_LEN, LoadLevel_MM_AUTH },
237 { "BODY", -1, LoadLevel_MM_BODY },
241 /* always start with reliable default values */
242 setLevelInfoToDefaults_MM();
244 if (!(file = openFile(filename, MODE_READ)))
246 if (!level_info_only)
247 Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
252 getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
253 if (strcmp(chunk_name, "MMII") == 0)
255 getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN); /* not used */
257 getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
258 if (strcmp(chunk_name, "CAVE") != 0)
260 Error(ERR_WARN, "unknown format of level file '%s'", filename);
267 else /* check for pre-2.0 file format with cookie string */
269 strcpy(cookie, chunk_name);
270 getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4);
271 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
272 cookie[strlen(cookie) - 1] = '\0';
274 if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
276 Error(ERR_WARN, "unknown format of level file '%s'", filename);
283 if ((native_mm_level.file_version = getFileVersionFromCookieString(cookie))
286 Error(ERR_WARN, "unsupported version of level file '%s'", filename);
294 while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_BIG_ENDIAN))
298 while (chunk_info[i].name != NULL &&
299 strcmp(chunk_name, chunk_info[i].name) != 0)
302 if (chunk_info[i].name == NULL)
304 Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
305 chunk_name, filename);
307 ReadUnusedBytesFromFile(file, chunk_size);
309 else if (chunk_info[i].size != -1 &&
310 chunk_info[i].size != chunk_size)
312 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
313 chunk_size, chunk_name, filename);
315 ReadUnusedBytesFromFile(file, chunk_size);
319 /* call function to load this level chunk */
320 int chunk_size_expected =
321 (chunk_info[i].loader)(file, chunk_size, &native_mm_level);
323 /* the size of some chunks cannot be checked before reading other
324 chunks first (like "HEAD" and "BODY") that contain some header
325 information, so check them here */
326 if (chunk_size_expected != chunk_size)
327 Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
328 chunk_size, chunk_name, filename);
337 static void SaveLevel_MM_HEAD(FILE *file, struct LevelInfo_MM *level)
342 fputc(level->fieldx, file);
343 fputc(level->fieldy, file);
345 putFile16BitInteger(file, level->time, BYTE_ORDER_BIG_ENDIAN);
346 putFile16BitInteger(file, level->kettles_needed, BYTE_ORDER_BIG_ENDIAN);
348 for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
349 fputc(level->name[i], file);
351 for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
352 fputc(level->score[i], file);
354 fputc((level->auto_count_kettles ? 1 : 0), file);
355 fputc(level->amoeba_speed, file);
356 fputc(level->time_fuse, file);
358 laser_color = ((level->laser_red << 2) |
359 (level->laser_green << 1) |
360 (level->laser_blue << 0));
361 fputc(laser_color, file);
363 fputc((level->encoding_16bit_field ? 1 : 0), file);
365 WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
368 static void SaveLevel_MM_AUTH(FILE *file, struct LevelInfo_MM *level)
372 for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
373 fputc(level->author[i], file);
376 static void SaveLevel_MM_BODY(FILE *file, struct LevelInfo_MM *level)
380 for (y = 0; y < level->fieldy; y++)
381 for (x = 0; x < level->fieldx; x++)
382 if (level->encoding_16bit_field)
383 putFile16BitInteger(file, Ur[x][y], BYTE_ORDER_BIG_ENDIAN);
385 fputc(Ur[x][y], file);
388 void SaveNativeLevel_MM(char *filename)
394 if (!(file = fopen(filename, MODE_WRITE)))
396 Error(ERR_WARN, "cannot save level file '%s'", filename);
401 /* check level field for 16-bit elements */
402 native_mm_level.encoding_16bit_field = FALSE;
404 for (y = 0; y < native_mm_level.fieldy; y++)
405 for (x = 0; x < native_mm_level.fieldx; x++)
407 native_mm_level.encoding_16bit_field = TRUE;
410 native_mm_level.fieldx * native_mm_level.fieldy *
411 (native_mm_level.encoding_16bit_field ? 2 : 1);
413 putFileChunk(file, "MMII", CHUNK_SIZE_UNDEFINED, BYTE_ORDER_BIG_ENDIAN);
414 putFileChunk(file, "CAVE", CHUNK_SIZE_NONE, BYTE_ORDER_BIG_ENDIAN);
416 putFileChunk(file, "VERS", FILE_VERS_CHUNK_SIZE, BYTE_ORDER_BIG_ENDIAN);
417 WriteChunk_MM_VERS(file, MM_FILE_VERSION_ACTUAL, MM_GAME_VERSION_ACTUAL);
419 putFileChunk(file, "HEAD", LEVEL_HEADER_SIZE, BYTE_ORDER_BIG_ENDIAN);
420 SaveLevel_MM_HEAD(file, &native_mm_level);
422 putFileChunk(file, "AUTH", MAX_LEVEL_AUTHOR_LEN, BYTE_ORDER_BIG_ENDIAN);
423 SaveLevel_MM_AUTH(file, &native_mm_level);
425 putFileChunk(file, "BODY", body_chunk_size, BYTE_ORDER_BIG_ENDIAN);
426 SaveLevel_MM_BODY(file, &native_mm_level);
430 SetFilePermissions(filename, PERMS_PRIVATE);