d334b8a0b3fb8932bb289172f9012c55c76be3b4
[rocksndiamonds.git] / src / game_mm / mm_files.c
1 // ============================================================================
2 // Mirror Magic -- McDuffin's Revenge
3 // ----------------------------------------------------------------------------
4 // (c) 1994-2017 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // mm_files.c
10 // ============================================================================
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <ctype.h>
15 #include <dirent.h>
16
17 #include "main_mm.h"
18
19 #include "mm_main.h"
20
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
27
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"
31
32
33 int default_score[LEVEL_SCORE_ELEMENTS] =
34 {
35   [SC_COLLECTIBLE]      = 10,
36   [SC_PACMAN]           = 50,
37   [SC_KEY]              = 10,
38   [SC_TIME_BONUS]       = 1,
39   [SC_LIGHTBALL]        = 10,
40 };
41
42
43 // ============================================================================
44 // level file functions
45 // ============================================================================
46
47 static void ReadChunk_MM_VERS(File *file, int *file_version, int *game_version)
48 {
49   int file_version_major, file_version_minor, file_version_patch;
50   int game_version_major, game_version_minor, game_version_patch;
51
52   file_version_major = getFile8Bit(file);
53   file_version_minor = getFile8Bit(file);
54   file_version_patch = getFile8Bit(file);
55   getFile8Bit(file);            // not used
56
57   game_version_major = getFile8Bit(file);
58   game_version_minor = getFile8Bit(file);
59   game_version_patch = getFile8Bit(file);
60   getFile8Bit(file);            // not used
61
62   *file_version = MM_VERSION_IDENT(file_version_major,
63                                    file_version_minor,
64                                    file_version_patch);
65
66   *game_version = MM_VERSION_IDENT(game_version_major,
67                                    game_version_minor,
68                                    game_version_patch);
69 }
70
71 static void WriteChunk_MM_VERS(FILE *file, int file_version, int game_version)
72 {
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);
79
80   fputc(file_version_major, file);
81   fputc(file_version_minor, file);
82   fputc(file_version_patch, file);
83   fputc(0, file);       // not used
84
85   fputc(game_version_major, file);
86   fputc(game_version_minor, file);
87   fputc(game_version_patch, file);
88   fputc(0, file);       // not used
89 }
90
91 void setLevelInfoToDefaults_MM(void)
92 {
93   int i, x, y;
94
95   native_mm_level.file_version = MM_FILE_VERSION_ACTUAL;
96   native_mm_level.game_version = MM_GAME_VERSION_ACTUAL;
97
98   native_mm_level.encoding_16bit_field = FALSE; // default: only 8-bit elements
99
100   native_mm_level.fieldx = STD_LEV_FIELDX;
101   native_mm_level.fieldy = STD_LEV_FIELDY;
102
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] = Ur[x][y] = EL_EMPTY;
106
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;
118
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';
123
124   strcpy(native_mm_level.name, NAMELESS_LEVEL_NAME);
125   strcpy(native_mm_level.author, ANONYMOUS_NAME);
126
127   for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
128     native_mm_level.score[i] = 10;
129
130   int ball_content[] =
131   {
132     EL_MIRROR_START,
133     EL_MIRROR_FIXED_START,
134     EL_POLAR_START,
135     EL_POLAR_CROSS_START,
136     EL_PACMAN_START,
137     EL_KETTLE,
138     EL_BOMB,
139     EL_PRISM
140   };
141   int num_ball_contents = sizeof(ball_content) / sizeof(int);
142
143   native_mm_level.num_ball_contents = num_ball_contents;
144   native_mm_level.ball_choice_mode = ANIM_RANDOM;
145
146   for (i = 0; i < num_ball_contents; i++)
147     native_mm_level.ball_content[i] = ball_content[i];
148
149   native_mm_level.field[0][0] = Ur[0][0] = EL_MCDUFFIN_RIGHT;
150   native_mm_level.field[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
151     Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_EXIT_CLOSED;
152 }
153
154 static int checkLevelElement(int element)
155 {
156   if (element >= EL_FIRST_RUNTIME_EL)
157   {
158     Warn("invalid level element %d", element);
159
160     element = EL_CHAR_FRAGE;
161   }
162
163   return element;
164 }
165
166 static int LoadLevel_MM_VERS(File *file, int chunk_size,
167                              struct LevelInfo_MM *level)
168 {
169   ReadChunk_MM_VERS(file, &level->file_version, &level->game_version);
170
171   return chunk_size;
172 }
173
174 static int LoadLevel_MM_HEAD(File *file, int chunk_size,
175                              struct LevelInfo_MM *level)
176 {
177   int i;
178   int laser_color;
179
180   level->fieldx = getFile8Bit(file);
181   level->fieldy = getFile8Bit(file);
182
183   level->time           = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
184   level->kettles_needed = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
185
186   // one time unit was equivalent to four seconds in level files up to 2.0.x
187   if (level->file_version <= MM_FILE_VERSION_2_0)
188     level->time *= 4;
189
190   for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
191     level->name[i] = getFile8Bit(file);
192   level->name[MAX_LEVEL_NAME_LEN] = 0;
193
194   for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
195     level->score[i] = getFile8Bit(file);
196
197   // scores were 0 and hardcoded in game engine in level files up to 2.0.x
198   if (level->file_version <= MM_FILE_VERSION_2_0)
199     for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
200       if (level->score[i] == 0)
201         level->score[i] = default_score[i];
202
203   level->auto_count_kettles     = (getFile8Bit(file) == 1 ? TRUE : FALSE);
204   level->amoeba_speed           = getFile8Bit(file);
205   level->time_fuse              = getFile8Bit(file);
206
207   // fuse time was 0 and hardcoded in game engine in level files up to 2.0.x
208   if (level->file_version <= MM_FILE_VERSION_2_0)
209     level->time_fuse = 25;
210
211   laser_color                   = getFile8Bit(file);
212   level->laser_red              = (laser_color >> 2) & 0x01;
213   level->laser_green            = (laser_color >> 1) & 0x01;
214   level->laser_blue             = (laser_color >> 0) & 0x01;
215
216   level->encoding_16bit_field   = (getFile8Bit(file) == 1 ? TRUE : FALSE);
217
218   ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
219
220   return chunk_size;
221 }
222
223 static int LoadLevel_MM_AUTH(File *file, int chunk_size,
224                              struct LevelInfo_MM *level)
225 {
226   int i;
227
228   for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
229     level->author[i] = getFile8Bit(file);
230   level->author[MAX_LEVEL_NAME_LEN] = 0;
231
232   return chunk_size;
233 }
234
235 static int LoadLevel_MM_BODY(File *file, int chunk_size,
236                              struct LevelInfo_MM *level)
237 {
238   int x, y;
239   int chunk_size_expected = level->fieldx * level->fieldy;
240
241   /* Note: "chunk_size" was wrong before version 2.0 when elements are
242      stored with 16-bit encoding (and should be twice as big then).
243      Even worse, playfield data was stored 16-bit when only yamyam content
244      contained 16-bit elements and vice versa. */
245
246   if (level->encoding_16bit_field && level->file_version >= MM_FILE_VERSION_2_0)
247     chunk_size_expected *= 2;
248
249   if (chunk_size_expected != chunk_size)
250   {
251     ReadUnusedBytesFromFile(file, chunk_size);
252
253     return chunk_size_expected;
254   }
255
256   for (y = 0; y < level->fieldy; y++)
257     for (x = 0; x < level->fieldx; x++)
258       native_mm_level.field[x][y] = Ur[x][y] =
259         checkLevelElement(level->encoding_16bit_field ?
260                           getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN) :
261                           getFile8Bit(file));
262   return chunk_size;
263 }
264
265 boolean LoadNativeLevel_MM(char *filename, boolean level_info_only)
266 {
267   char cookie[MAX_LINE_LEN];
268   char chunk_name[CHUNK_ID_LEN + 1];
269   int chunk_size;
270   File *file;
271
272   static struct
273   {
274     char *name;
275     int size;
276     int (*loader)(File *, int, struct LevelInfo_MM *);
277   }
278   chunk_info[] =
279   {
280     { "VERS", FILE_VERS_CHUNK_SIZE,     LoadLevel_MM_VERS },
281     { "HEAD", LEVEL_HEADER_SIZE,        LoadLevel_MM_HEAD },
282     { "AUTH", MAX_LEVEL_AUTHOR_LEN,     LoadLevel_MM_AUTH },
283     { "BODY", -1,                       LoadLevel_MM_BODY },
284     {  NULL,  0,                        NULL }
285   };
286
287   // always start with reliable default values
288   setLevelInfoToDefaults_MM();
289
290   if (!(file = openFile(filename, MODE_READ)))
291   {
292     if (!level_info_only)
293       Warn("cannot read level '%s' - creating new level", filename);
294
295     return FALSE;
296   }
297
298   getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
299   if (strcmp(chunk_name, "MMII") == 0)
300   {
301     getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN);   // not used
302
303     getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
304     if (strcmp(chunk_name, "CAVE") != 0)
305     {
306       Warn("unknown format of level file '%s'", filename);
307
308       closeFile(file);
309
310       return FALSE;
311     }
312   }
313   else  // check for pre-2.0 file format with cookie string
314   {
315     strcpy(cookie, chunk_name);
316     getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4);
317     if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
318       cookie[strlen(cookie) - 1] = '\0';
319
320     if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
321     {
322       Warn("unknown format of level file '%s'", filename);
323
324       closeFile(file);
325
326       return FALSE;
327     }
328
329     if ((native_mm_level.file_version = getFileVersionFromCookieString(cookie))
330         == -1)
331     {
332       Warn("unsupported version of level file '%s'", filename);
333
334       closeFile(file);
335
336       return FALSE;
337     }
338   }
339
340   while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_BIG_ENDIAN))
341   {
342     int i = 0;
343
344     while (chunk_info[i].name != NULL &&
345            strcmp(chunk_name, chunk_info[i].name) != 0)
346       i++;
347
348     if (chunk_info[i].name == NULL)
349     {
350       Warn("unknown chunk '%s' in level file '%s'",
351             chunk_name, filename);
352
353       ReadUnusedBytesFromFile(file, chunk_size);
354     }
355     else if (chunk_info[i].size != -1 &&
356              chunk_info[i].size != chunk_size)
357     {
358       Warn("wrong size (%d) of chunk '%s' in level file '%s'",
359             chunk_size, chunk_name, filename);
360
361       ReadUnusedBytesFromFile(file, chunk_size);
362     }
363     else
364     {
365       // call function to load this level chunk
366       int chunk_size_expected =
367         (chunk_info[i].loader)(file, chunk_size, &native_mm_level);
368
369       // the size of some chunks cannot be checked before reading other
370       // chunks first (like "HEAD" and "BODY") that contain some header
371       // information, so check them here
372       if (chunk_size_expected != chunk_size)
373         Warn("wrong size (%d) of chunk '%s' in level file '%s'",
374               chunk_size, chunk_name, filename);
375     }
376   }
377
378   closeFile(file);
379
380   return TRUE;
381 }
382
383 static void SaveLevel_MM_HEAD(FILE *file, struct LevelInfo_MM *level)
384 {
385   int i;
386   int laser_color;
387
388   fputc(level->fieldx, file);
389   fputc(level->fieldy, file);
390
391   putFile16BitInteger(file, level->time,           BYTE_ORDER_BIG_ENDIAN);
392   putFile16BitInteger(file, level->kettles_needed, BYTE_ORDER_BIG_ENDIAN);
393
394   for (i = 0; i < MAX_LEVEL_NAME_LEN; i++)
395     fputc(level->name[i], file);
396
397   for (i = 0; i < LEVEL_SCORE_ELEMENTS; i++)
398     fputc(level->score[i], file);
399
400   fputc((level->auto_count_kettles ? 1 : 0), file);
401   fputc(level->amoeba_speed, file);
402   fputc(level->time_fuse, file);
403
404   laser_color = ((level->laser_red   << 2) |
405                  (level->laser_green << 1) |
406                  (level->laser_blue  << 0));
407   fputc(laser_color, file);
408
409   fputc((level->encoding_16bit_field ? 1 : 0), file);
410
411   WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
412 }
413
414 static void SaveLevel_MM_AUTH(FILE *file, struct LevelInfo_MM *level)
415 {
416   int i;
417
418   for (i = 0; i < MAX_LEVEL_AUTHOR_LEN; i++)
419     fputc(level->author[i], file);
420 }
421
422 static void SaveLevel_MM_BODY(FILE *file, struct LevelInfo_MM *level)
423 {
424   int x, y;
425
426   for (y = 0; y < level->fieldy; y++)
427     for (x = 0; x < level->fieldx; x++)
428       if (level->encoding_16bit_field)
429         putFile16BitInteger(file, Ur[x][y], BYTE_ORDER_BIG_ENDIAN);
430       else
431         fputc(Ur[x][y], file);
432 }
433
434 void SaveNativeLevel_MM(char *filename)
435 {
436   int x, y;
437   int body_chunk_size;
438   FILE *file;
439
440   if (!(file = fopen(filename, MODE_WRITE)))
441   {
442     Warn("cannot save level file '%s'", filename);
443
444     return;
445   }
446
447   // check level field for 16-bit elements
448   native_mm_level.encoding_16bit_field = FALSE;
449
450   for (y = 0; y < native_mm_level.fieldy; y++)
451     for (x = 0; x < native_mm_level.fieldx; x++)
452       if (Ur[x][y] > 255)
453         native_mm_level.encoding_16bit_field = TRUE;
454
455   body_chunk_size =
456     native_mm_level.fieldx * native_mm_level.fieldy *
457     (native_mm_level.encoding_16bit_field ? 2 : 1);
458
459   putFileChunk(file, "MMII", CHUNK_SIZE_UNDEFINED, BYTE_ORDER_BIG_ENDIAN);
460   putFileChunk(file, "CAVE", CHUNK_SIZE_NONE,      BYTE_ORDER_BIG_ENDIAN);
461
462   putFileChunk(file, "VERS", FILE_VERS_CHUNK_SIZE, BYTE_ORDER_BIG_ENDIAN);
463   WriteChunk_MM_VERS(file, MM_FILE_VERSION_ACTUAL, MM_GAME_VERSION_ACTUAL);
464
465   putFileChunk(file, "HEAD", LEVEL_HEADER_SIZE, BYTE_ORDER_BIG_ENDIAN);
466   SaveLevel_MM_HEAD(file, &native_mm_level);
467
468   putFileChunk(file, "AUTH", MAX_LEVEL_AUTHOR_LEN, BYTE_ORDER_BIG_ENDIAN);
469   SaveLevel_MM_AUTH(file, &native_mm_level);
470
471   putFileChunk(file, "BODY", body_chunk_size, BYTE_ORDER_BIG_ENDIAN);
472   SaveLevel_MM_BODY(file, &native_mm_level);
473
474   fclose(file);
475
476   SetFilePermissions(filename, PERMS_PRIVATE);
477 }