added and adjusted source files for Mirror Magic game engine
[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 //                  http://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 /* ========================================================================= */
34 /* level file functions                                                      */
35 /* ========================================================================= */
36
37 static void ReadChunk_MM_VERS(File *file, int *file_version, int *game_version)
38 {
39   int file_version_major, file_version_minor, file_version_patch;
40   int game_version_major, game_version_minor, game_version_patch;
41
42   file_version_major = getFile8Bit(file);
43   file_version_minor = getFile8Bit(file);
44   file_version_patch = getFile8Bit(file);
45   getFile8Bit(file);            /* not used */
46
47   game_version_major = getFile8Bit(file);
48   game_version_minor = getFile8Bit(file);
49   game_version_patch = getFile8Bit(file);
50   getFile8Bit(file);            /* not used */
51
52   *file_version = MM_VERSION_IDENT(file_version_major,
53                                    file_version_minor,
54                                    file_version_patch);
55
56   *game_version = MM_VERSION_IDENT(game_version_major,
57                                    game_version_minor,
58                                    game_version_patch);
59 }
60
61 static void WriteChunk_MM_VERS(FILE *file, int file_version, int game_version)
62 {
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);
69
70   fputc(file_version_major, file);
71   fputc(file_version_minor, file);
72   fputc(file_version_patch, file);
73   fputc(0, file);       /* not used */
74
75   fputc(game_version_major, file);
76   fputc(game_version_minor, file);
77   fputc(game_version_patch, file);
78   fputc(0, file);       /* not used */
79 }
80
81 void setLevelInfoToDefaults_MM()
82 {
83   int i, x, y;
84
85   native_mm_level.file_version = MM_FILE_VERSION_ACTUAL;
86   native_mm_level.game_version = MM_GAME_VERSION_ACTUAL;
87
88   native_mm_level.encoding_16bit_field = FALSE; /* default: only 8-bit elements */
89
90   lev_fieldx = native_mm_level.fieldx = STD_LEV_FIELDX;
91   lev_fieldy = native_mm_level.fieldy = STD_LEV_FIELDY;
92
93   for(x=0; x<MAX_LEV_FIELDX; x++)
94     for(y=0; y<MAX_LEV_FIELDY; y++)
95       Feld[x][y] = Ur[x][y] = EL_EMPTY;
96
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;
105
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';
110
111   strcpy(native_mm_level.name, NAMELESS_LEVEL_NAME);
112   strcpy(native_mm_level.author, ANONYMOUS_NAME);
113
114   for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
115     native_mm_level.score[i] = 10;
116
117   Feld[0][0] = Ur[0][0] = EL_MCDUFFIN_RIGHT;
118   Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
119     Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_EXIT_CLOSED;
120 }
121
122 static int checkLevelElement(int element)
123 {
124   if (element >= EL_FIRST_RUNTIME_EL)
125   {
126     Error(ERR_WARN, "invalid level element %d", element);
127     element = EL_CHAR_FRAGE;
128   }
129
130   return element;
131 }
132
133 static int LoadLevel_MM_VERS(File *file, int chunk_size, struct LevelInfo_MM *level)
134 {
135   ReadChunk_MM_VERS(file, &(level->file_version), &(level->game_version));
136
137   return chunk_size;
138 }
139
140 static int LoadLevel_MM_HEAD(File *file, int chunk_size, struct LevelInfo_MM *level)
141 {
142   int i;
143   int laser_color;
144
145   lev_fieldx = level->fieldx = getFile8Bit(file);
146   lev_fieldy = level->fieldy = getFile8Bit(file);
147
148   level->time           = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
149   level->kettles_needed = getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN);
150
151   for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
152     level->name[i] = getFile8Bit(file);
153   level->name[MAX_LEVEL_NAME_LEN] = 0;
154
155   for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
156     level->score[i] = getFile8Bit(file);
157
158   level->auto_count_kettles     = (getFile8Bit(file) == 1 ? TRUE : FALSE);
159   level->amoeba_speed           = getFile8Bit(file);
160   level->time_fuse              = getFile8Bit(file);
161
162   laser_color                   = getFile8Bit(file);
163   level->laser_red              = (laser_color >> 2) & 0x01;
164   level->laser_green            = (laser_color >> 1) & 0x01;
165   level->laser_blue             = (laser_color >> 0) & 0x01;
166
167   level->encoding_16bit_field   = (getFile8Bit(file) == 1 ? TRUE : FALSE);
168
169   ReadUnusedBytesFromFile(file, LEVEL_HEADER_UNUSED);
170
171   return chunk_size;
172 }
173
174 static int LoadLevel_MM_AUTH(File *file, int chunk_size, struct LevelInfo_MM *level)
175 {
176   int i;
177
178   for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
179     level->author[i] = getFile8Bit(file);
180   level->author[MAX_LEVEL_NAME_LEN] = 0;
181
182   return chunk_size;
183 }
184
185 static int LoadLevel_MM_BODY(File *file, int chunk_size, struct LevelInfo_MM *level)
186 {
187   int x, y;
188   int chunk_size_expected = level->fieldx * level->fieldy;
189
190   /* Note: "chunk_size" was wrong before version 2.0 when elements are
191      stored with 16-bit encoding (and should be twice as big then).
192      Even worse, playfield data was stored 16-bit when only yamyam content
193      contained 16-bit elements and vice versa. */
194
195   if (level->encoding_16bit_field && level->file_version >= MM_FILE_VERSION_2_0)
196     chunk_size_expected *= 2;
197
198   if (chunk_size_expected != chunk_size)
199   {
200     ReadUnusedBytesFromFile(file, chunk_size);
201     return chunk_size_expected;
202   }
203
204   for(y=0; y<level->fieldy; y++)
205     for(x=0; x<level->fieldx; x++)
206       Feld[x][y] = Ur[x][y] =
207         checkLevelElement(level->encoding_16bit_field ?
208                           getFile16BitInteger(file, BYTE_ORDER_BIG_ENDIAN) :
209                           getFile8Bit(file));
210   return chunk_size;
211 }
212
213 boolean LoadNativeLevel_MM(char *filename, boolean level_info_only)
214 {
215   char cookie[MAX_LINE_LEN];
216   char chunk_name[CHUNK_ID_LEN + 1];
217   int chunk_size;
218   File *file;
219
220   static struct
221   {
222     char *name;
223     int size;
224     int (*loader)(File *, int, struct LevelInfo_MM *);
225   }
226   chunk_info[] =
227   {
228     { "VERS", FILE_VERS_CHUNK_SIZE,     LoadLevel_MM_VERS },
229     { "HEAD", LEVEL_HEADER_SIZE,        LoadLevel_MM_HEAD },
230     { "AUTH", MAX_LEVEL_AUTHOR_LEN,     LoadLevel_MM_AUTH },
231     { "BODY", -1,                       LoadLevel_MM_BODY },
232     {  NULL,  0,                        NULL }
233   };
234
235   /* always start with reliable default values */
236   setLevelInfoToDefaults_MM();
237
238   if (!(file = openFile(filename, MODE_READ)))
239   {
240     if (!level_info_only)
241       Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
242
243     return FALSE;
244   }
245
246   getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
247   if (strcmp(chunk_name, "MMII") == 0)
248   {
249     getFile32BitInteger(file, BYTE_ORDER_BIG_ENDIAN);   /* not used */
250
251     getFileChunk(file, chunk_name, NULL, BYTE_ORDER_BIG_ENDIAN);
252     if (strcmp(chunk_name, "CAVE") != 0)
253     {
254       Error(ERR_WARN, "unknown format of level file '%s'", filename);
255       closeFile(file);
256
257       return FALSE;
258     }
259   }
260   else  /* check for pre-2.0 file format with cookie string */
261   {
262     strcpy(cookie, chunk_name);
263     getStringFromFile(file, &cookie[4], MAX_LINE_LEN - 4);
264     if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
265       cookie[strlen(cookie) - 1] = '\0';
266
267     if (!checkCookieString(cookie, LEVEL_COOKIE_TMPL))
268     {
269       Error(ERR_WARN, "unknown format of level file '%s'", filename);
270       closeFile(file);
271
272       return FALSE;
273     }
274
275     if ((native_mm_level.file_version = getFileVersionFromCookieString(cookie)) == -1)
276     {
277       Error(ERR_WARN, "unsupported version of level file '%s'", filename);
278       closeFile(file);
279
280       return FALSE;
281     }
282   }
283
284   while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_BIG_ENDIAN))
285   {
286     int i = 0;
287
288     while (chunk_info[i].name != NULL &&
289            strcmp(chunk_name, chunk_info[i].name) != 0)
290       i++;
291
292     if (chunk_info[i].name == NULL)
293     {
294       Error(ERR_WARN, "unknown chunk '%s' in level file '%s'",
295             chunk_name, filename);
296       ReadUnusedBytesFromFile(file, chunk_size);
297     }
298     else if (chunk_info[i].size != -1 &&
299              chunk_info[i].size != chunk_size)
300     {
301       Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
302             chunk_size, chunk_name, filename);
303       ReadUnusedBytesFromFile(file, chunk_size);
304     }
305     else
306     {
307       /* call function to load this level chunk */
308       int chunk_size_expected =
309         (chunk_info[i].loader)(file, chunk_size, &native_mm_level);
310
311       /* the size of some chunks cannot be checked before reading other
312          chunks first (like "HEAD" and "BODY") that contain some header
313          information, so check them here */
314       if (chunk_size_expected != chunk_size)
315       {
316         Error(ERR_WARN, "wrong size (%d) of chunk '%s' in level file '%s'",
317               chunk_size, chunk_name, filename);
318       }
319     }
320   }
321
322   closeFile(file);
323
324   return TRUE;
325 }
326
327 static void SaveLevel_MM_HEAD(FILE *file, struct LevelInfo_MM *level)
328 {
329   int i;
330   int laser_color;
331
332   fputc(level->fieldx, file);
333   fputc(level->fieldy, file);
334
335   putFile16BitInteger(file, level->time,           BYTE_ORDER_BIG_ENDIAN);
336   putFile16BitInteger(file, level->kettles_needed, BYTE_ORDER_BIG_ENDIAN);
337
338   for(i=0; i<MAX_LEVEL_NAME_LEN; i++)
339     fputc(level->name[i], file);
340
341   for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
342     fputc(level->score[i], file);
343
344   fputc((level->auto_count_kettles ? 1 : 0), file);
345   fputc(level->amoeba_speed, file);
346   fputc(level->time_fuse, file);
347
348   laser_color = ((level->laser_red   << 2) |
349                  (level->laser_green << 1) |
350                  (level->laser_blue  << 0));
351   fputc(laser_color, file);
352
353   fputc((level->encoding_16bit_field ? 1 : 0), file);
354
355   WriteUnusedBytesToFile(file, LEVEL_HEADER_UNUSED);
356 }
357
358 static void SaveLevel_MM_AUTH(FILE *file, struct LevelInfo_MM *level)
359 {
360   int i;
361
362   for(i=0; i<MAX_LEVEL_AUTHOR_LEN; i++)
363     fputc(level->author[i], file);
364 }
365
366 static void SaveLevel_MM_BODY(FILE *file, struct LevelInfo_MM *level)
367 {
368   int x, y;
369
370   for(y=0; y<level->fieldy; y++)
371     for(x=0; x<level->fieldx; x++)
372       if (level->encoding_16bit_field)
373         putFile16BitInteger(file, Ur[x][y], BYTE_ORDER_BIG_ENDIAN);
374       else
375         fputc(Ur[x][y], file);
376 }
377
378 void SaveNativeLevel_MM(char *filename)
379 {
380   int x, y;
381   int body_chunk_size;
382   FILE *file;
383
384   if (!(file = fopen(filename, MODE_WRITE)))
385   {
386     Error(ERR_WARN, "cannot save level file '%s'", filename);
387     return;
388   }
389
390   /* check level field for 16-bit elements */
391   native_mm_level.encoding_16bit_field = FALSE;
392   for(y=0; y<native_mm_level.fieldy; y++)
393     for(x=0; x<native_mm_level.fieldx; x++)
394       if (Ur[x][y] > 255)
395         native_mm_level.encoding_16bit_field = TRUE;
396
397   body_chunk_size =
398     native_mm_level.fieldx * native_mm_level.fieldy * (native_mm_level.encoding_16bit_field ? 2 : 1);
399
400   putFileChunk(file, "MMII", CHUNK_SIZE_UNDEFINED, BYTE_ORDER_BIG_ENDIAN);
401   putFileChunk(file, "CAVE", CHUNK_SIZE_NONE,      BYTE_ORDER_BIG_ENDIAN);
402
403   putFileChunk(file, "VERS", FILE_VERS_CHUNK_SIZE, BYTE_ORDER_BIG_ENDIAN);
404   WriteChunk_MM_VERS(file, MM_FILE_VERSION_ACTUAL, MM_GAME_VERSION_ACTUAL);
405
406   putFileChunk(file, "HEAD", LEVEL_HEADER_SIZE, BYTE_ORDER_BIG_ENDIAN);
407   SaveLevel_MM_HEAD(file, &native_mm_level);
408
409   putFileChunk(file, "AUTH", MAX_LEVEL_AUTHOR_LEN, BYTE_ORDER_BIG_ENDIAN);
410   SaveLevel_MM_AUTH(file, &native_mm_level);
411
412   putFileChunk(file, "BODY", body_chunk_size, BYTE_ORDER_BIG_ENDIAN);
413   SaveLevel_MM_BODY(file, &native_mm_level);
414
415   fclose(file);
416
417   SetFilePermissions(filename, PERMS_PRIVATE);
418 }