replaced glib function calls to g_list_*()
[rocksndiamonds.git] / src / game_bd / bd_caveset.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <glib.h>
18 #include <glib/gi18n.h>
19 #include <glib/gstdio.h>
20
21 #include "main_bd.h"
22
23
24 /* this stores the caves. */
25 List *gd_caveset;
26
27 /* the data of the caveset: name, highscore, max number of lives, etc. */
28 GdCavesetData *gd_caveset_data;
29
30 /* is set to true, when the caveset was edited since the last save. */
31 boolean gd_caveset_edited;
32
33 /* last selected-to-play cave */
34 int gd_caveset_last_selected;
35 int gd_caveset_last_selected_level;
36
37 /* list of possible extensions which can be opened */
38 char *gd_caveset_extensions[] =
39 {
40   "*.gds",
41   "*.bd",
42   "*.bdr",
43   "*.brc",
44
45   NULL
46 };
47
48 #define CAVESET_OFFSET(property) (STRUCT_OFFSET(GdCavesetData, property))
49
50 const GdStructDescriptor gd_caveset_properties[] =
51 {
52   /* default data */
53   {"", GD_TAB, 0, N_("Caveset data")},
54   {"Name", GD_TYPE_STRING, 0, N_("Name"), CAVESET_OFFSET(name), 1, N_("Name of the game")},
55   {"Description", GD_TYPE_STRING, 0, N_("Description"), CAVESET_OFFSET(description), 1, N_("Some words about the game")},
56   {"Author", GD_TYPE_STRING, 0, N_("Author"), CAVESET_OFFSET(author), 1, N_("Name of author")},
57   {"Date", GD_TYPE_STRING, 0, N_("Date"), CAVESET_OFFSET(date), 1, N_("Date of creation")},
58   {"WWW", GD_TYPE_STRING, 0, N_("WWW"), CAVESET_OFFSET(www), 1, N_("Web page or e-mail address")},
59   {"Difficulty", GD_TYPE_STRING, 0, N_("Difficulty"), CAVESET_OFFSET(difficulty), 1, N_("Difficulty (informative)")},
60
61   {"Lives", GD_TYPE_INT, 0, N_("Initial lives"), CAVESET_OFFSET(initial_lives), 1, N_("Number of lives you get at game start."), 3, 9},
62   {"Lives", GD_TYPE_INT, 0, N_("Maximum lives"), CAVESET_OFFSET(maximum_lives), 1, N_("Maximum number of lives you can have by collecting bonus points."), 3, 99},
63   {"BonusLife", GD_TYPE_INT, 0, N_("Bonus life score"), CAVESET_OFFSET(bonus_life_score), 1, N_("Number of points to collect for a bonus life."), 100, 5000},
64
65   {"Story", GD_TYPE_LONGSTRING, 0, N_("Story"), CAVESET_OFFSET(story), 1, N_("Long description of the game.")},
66   {"Remark", GD_TYPE_LONGSTRING, 0, N_("Remark"), CAVESET_OFFSET(remark), 1, N_("Remark (informative).")},
67
68   {"TitleScreen", GD_TYPE_LONGSTRING, GD_DONT_SHOW_IN_EDITOR, N_("Title screen"), CAVESET_OFFSET(title_screen), 1, N_("Title screen image")},
69   {"TitleScreenScroll", GD_TYPE_LONGSTRING, GD_DONT_SHOW_IN_EDITOR, N_("Title screen, scrolling"), CAVESET_OFFSET(title_screen_scroll), 1, N_("Scrolling background for title screen image")},
70
71   {NULL},
72 };
73
74 static GdPropertyDefault caveset_defaults[] =
75 {
76   /* default data */
77   {CAVESET_OFFSET(initial_lives), 3},
78   {CAVESET_OFFSET(maximum_lives), 9},
79   {CAVESET_OFFSET(bonus_life_score), 500},
80
81   {-1},
82 };
83
84 GdCavesetData *gd_caveset_data_new(void)
85 {
86   GdCavesetData *data;
87
88   data = checked_calloc(sizeof(GdCavesetData));
89
90   gd_struct_set_defaults_from_array(data, gd_caveset_properties, caveset_defaults);
91
92   return data;
93 }
94
95 void gd_caveset_data_free(GdCavesetData *data)
96 {
97   int i;
98
99   /* free strings */
100   for (i = 0; gd_caveset_properties[i].identifier != NULL; i++)
101     if (gd_caveset_properties[i].type == GD_TYPE_LONGSTRING)
102       checked_free(G_STRUCT_MEMBER(char *, data, gd_caveset_properties[i].offset));
103
104   free(data);
105 }
106
107 /******************************************************************************
108  *
109  * Misc caveset functions
110  *
111  */
112
113 /** Clears all caves in the caveset. also to be called at application start */
114 void gd_caveset_clear(void)
115 {
116   if (gd_caveset)
117   {
118     list_foreach(gd_caveset, (list_fn) gd_cave_free, NULL);
119     list_free(gd_caveset);
120     gd_caveset = NULL;
121   }
122
123   if (gd_caveset_data)
124   {
125     free(gd_caveset_data);
126     gd_caveset_data = NULL;
127   }
128
129   /* always newly create this */
130   /* create pseudo cave containing default values */
131   gd_caveset_data = gd_caveset_data_new();
132   gd_strcpy(gd_caveset_data->name, _("New caveset"));
133 }
134
135 /* return number of caves currently in memory. */
136 int gd_caveset_count(void)
137 {
138   return list_length(gd_caveset);
139 }
140
141 /* return index of first selectable cave */
142 static int caveset_first_selectable_cave_index(void)
143 {
144   List *iter;
145   int i;
146
147   for (i = 0, iter = gd_caveset; iter != NULL; i++, iter = iter->next)
148   {
149     GdCave *cave = (GdCave *)iter->data;
150
151     if (cave->selectable)
152       return i;
153   }
154
155   Warn("no selectable cave in caveset!");
156
157   /* and return the first one. */
158   return 0;
159 }
160
161 /* return a cave identified by its index */
162 GdCave *gd_return_nth_cave(const int cave)
163 {
164   return list_nth_data(gd_caveset, cave);
165 }
166
167 /* get a selected cave from the loaded caveset (original, unmodified cave) */
168 GdCave *gd_get_original_cave_from_caveset(const int cave)
169 {
170   /* get specified cave from caveset already stored in memory */
171   GdCave *original_cave = gd_return_nth_cave(cave);
172
173   return original_cave;
174 }
175
176 /* get a selected cave from the loaded caveset (cave prepared for playing) */
177 GdCave *gd_get_prepared_cave_from_caveset(const int cave, const int level)
178 {
179   /* get specified cave from caveset already stored in memory */
180   GdCave *original_cave = gd_return_nth_cave(cave);
181
182   /* get prepared cave from original cave */
183   GdCave *prepared_cave = gd_get_prepared_cave(original_cave, level);
184
185   return prepared_cave;
186 }
187
188 /* get a cave prepared for playing from a given original, unmodified cave (with seed) */
189 GdCave *gd_get_prepared_cave(const GdCave *original_cave, const int level)
190 {
191   /* get rendered cave using the selected seed for playing */
192   GdCave *prepared_cave = gd_cave_new_rendered(original_cave, level, game_bd.random_seed);
193
194   /* initialize some cave variables (like player position) */
195   gd_cave_setup_for_game(prepared_cave);
196
197   return prepared_cave;
198 }
199
200 /* colors: 4: purple  3: ciklamen 2: orange 1: blue 0: green */
201 static GdElement brc_import_table[] =
202 {
203   /* 0 */
204   O_SPACE, O_DIRT, O_BRICK, O_MAGIC_WALL, O_PRE_OUTBOX, O_OUTBOX, O_UNKNOWN, O_STEEL,
205   O_H_EXPANDING_WALL, O_H_EXPANDING_WALL /* scanned */, O_FIREFLY_1 /* scanned */, O_FIREFLY_1 /* scanned */, O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
206
207   /* 1 */
208   O_BUTTER_1 /* scanned */, O_BUTTER_1 /* scanned */, O_BUTTER_1, O_BUTTER_2, O_BUTTER_3, O_BUTTER_4, O_PLAYER, O_PLAYER /* scanned */,
209   O_STONE, O_STONE /* scanned */, O_STONE_F, O_STONE_F /* scanned */, O_DIAMOND, O_DIAMOND /* scanned */, O_DIAMOND_F, O_DIAMOND_F /* scanned */,
210
211   /* 2 */
212   O_NONE /* WILL_EXPLODE_THING */, O_EXPLODE_1, O_EXPLODE_2, O_EXPLODE_3, O_EXPLODE_4, O_EXPLODE_5, O_NONE /* WILL EXPLODE TO DIAMOND_THING */, O_PRE_DIA_1,
213   O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4, O_PRE_DIA_5, O_AMOEBA, O_AMOEBA /* scanned */, O_SLIME, O_NONE,
214
215   /* 3 */
216   O_CLOCK, O_NONE /* clock eaten */, O_INBOX, O_PRE_PL_1, O_PRE_PL_2, O_PRE_PL_3, O_NONE, O_NONE,
217   O_NONE, O_NONE, O_V_EXPANDING_WALL, O_NONE, O_VOODOO, O_UNKNOWN, O_EXPANDING_WALL, O_EXPANDING_WALL /* sc */,
218
219   /* 4 */
220   O_FALLING_WALL, O_FALLING_WALL_F, O_FALLING_WALL_F /* scanned */, O_UNKNOWN, O_ACID, O_ACID /* scanned */, O_NITRO_PACK, O_NITRO_PACK /* scanned */,
221   O_NITRO_PACK_F, O_NITRO_PACK_F /* scanned */, O_NONE, O_NONE, O_NONE, O_NONE, O_NONE, O_NONE,
222
223   /* 5 */
224   O_NONE /* bomb explosion utolso */, O_UNKNOWN, O_NONE /* solid bomb glued */, O_UNKNOWN, O_STONE_GLUED, O_UNKNOWN, O_DIAMOND_GLUED, O_UNKNOWN,
225   O_UNKNOWN, O_UNKNOWN, O_NONE, O_NONE, O_NONE, O_NONE, O_NONE, O_NONE,
226
227   /* 6 */
228   O_ALT_FIREFLY_1 /* scanned */, O_ALT_FIREFLY_1 /* scanned */, O_ALT_FIREFLY_1, O_ALT_FIREFLY_2, O_ALT_FIREFLY_3, O_ALT_FIREFLY_4, O_PLAYER_BOMB, O_PLAYER_BOMB /* scanned */,
229   O_BOMB, O_BOMB_TICK_1, O_BOMB_TICK_2, O_BOMB_TICK_3, O_BOMB_TICK_4, O_BOMB_TICK_5, O_BOMB_TICK_6, O_BOMB_TICK_7,
230
231   /* 7 */
232   O_BOMB_TICK_7, O_BOMB_EXPL_1, O_BOMB_EXPL_2, O_BOMB_EXPL_3, O_BOMB_EXPL_4, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
233   O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
234 };
235
236 static GdElement brc_effect_table[] =
237 {
238   O_STEEL, O_DIRT, O_SPACE, O_STONE, O_STONE_F, O_STONE_GLUED, O_DIAMOND, O_DIAMOND_F, O_DIAMOND_GLUED, O_PRE_DIA_1,
239   O_PLAYER, O_PRE_PL_1, O_PLAYER_BOMB, O_PRE_OUTBOX, O_OUTBOX, O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
240   O_BUTTER_1, O_BUTTER_2, O_BUTTER_3, O_BUTTER_4, O_BRICK, O_MAGIC_WALL, O_H_EXPANDING_WALL, O_V_EXPANDING_WALL, O_EXPANDING_WALL,
241   O_FALLING_WALL, O_FALLING_WALL_F, O_AMOEBA, O_SLIME, O_ACID, O_VOODOO, O_CLOCK, O_BOMB, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
242   O_ALT_FIREFLY_1, O_ALT_FIREFLY_2, O_ALT_FIREFLY_3, O_ALT_FIREFLY_4, O_ALT_BUTTER_1, O_ALT_BUTTER_2, O_ALT_BUTTER_3, O_ALT_BUTTER_4,
243   O_EXPLODE_1, O_BOMB_EXPL_1, O_UNKNOWN,
244 };
245
246 static GdColor brc_color_table[] =
247 {
248   0x518722, 0x3a96fa, 0xdb7618, 0xff3968,
249   0x9b5fff, 0x0ee06c, 0xc25ea6, 0xf54826,
250   0xf1ff26,
251 };
252
253 static GdColor brc_color_table_comp[] =
254 {
255   0x582287, 0xfa9d39, 0x187ddb, 0x38ffd1,
256   0xc1ff5e, 0xe00d81, 0x5dc27a, 0x27d3f5,
257   0x3526ff,
258 };
259
260 static GdElement brc_effect(guint8 byt)
261 {
262   if (byt >= G_N_ELEMENTS(brc_effect_table))
263   {
264     Warn("invalid element identifier for brc effect: %02x", byt);
265
266     return O_UNKNOWN;
267   }
268
269   return brc_effect_table[byt];
270 }
271
272 static void brc_import(guint8 *data)
273 {
274   int x, y;
275   int level;
276
277   /* we import 100 caves, and the put them in the correct order. */
278   GdCave *imported[100];
279   boolean import_effect;
280
281   gd_caveset_clear();
282
283   /* this is some kind of a version number */
284   import_effect = FALSE;
285
286   switch (data[23])
287   {
288     case 0x0:
289       /* nothing to do */
290       break;
291
292     case 0xde:
293       /* import effects */
294       import_effect = TRUE;
295       break;
296
297     default:
298       Warn("unknown brc version %02x", data[23]);
299       break;
300   }
301
302   for (level = 0; level < 5; level++)
303   {
304     int cavenum;
305     int i;
306
307     for (cavenum = 0; cavenum < 20; cavenum++)
308     {
309       GdCave *cave;
310
311       /* 5 levels, 20 caves, 24 bytes - max 40*2 properties for each cave */
312       int c = 5 * 20 * 24;
313
314       int datapos = (cavenum * 5 +level) * 24 + 22;
315       int colind;
316
317       cave = gd_cave_new();
318       imported[level * 20 + cavenum] = cave;
319
320       if (cavenum < 16)
321         g_snprintf(cave->name, sizeof(GdString), "Cave %c/%d", 'A' + cavenum,
322                    level + 1);
323       else
324         g_snprintf(cave->name, sizeof(GdString), "Intermission %d/%d",
325                    cavenum - 15, level + 1);
326
327       /* fixed intermission caves; are smaller. */
328       if (cavenum >= 16)
329       {
330         cave->w = 20;
331         cave->h = 12;
332       }
333
334       cave->map = gd_cave_map_new(cave, GdElement);
335
336       for (y = 0; y < cave->h; y++)
337       {
338         for (x = 0; x < cave->w; x++)
339         {
340           guint8 import;
341
342           import = data[y + level * 24 + cavenum * 24 * 5 + x * 24 * 5 * 20];
343
344           // if (i == printcave) g_print("%2x", import);
345           if (import < G_N_ELEMENTS(brc_import_table))
346             cave->map[y][x] = brc_import_table[import];
347           else
348             cave->map[y][x] = O_UNKNOWN;
349         }
350       }
351
352       for (i = 0; i < 5; i++)
353       {
354         cave->level_time[i]             = data[0 * c + datapos];
355         cave->level_diamonds[i]         = data[1 * c + datapos];
356         cave->level_magic_wall_time[i]  = data[4 * c + datapos];
357         cave->level_amoeba_time[i]      = data[5 * c + datapos];
358         cave->level_amoeba_threshold[i] = data[6 * c + datapos];
359
360         /* bonus time: 100 was added, so it could also be negative */
361         cave->level_bonus_time[i]      = (int)data[11 * c + datapos + 1] - 100;
362         cave->level_hatching_delay_frame[i] = data[10 * c + datapos];
363
364         /* this was not set in boulder remake. */
365         cave->level_speed[i] = 150;
366       }
367
368       cave->diamond_value = data[2 * c + datapos];
369       cave->extra_diamond_value = data[3 * c +datapos];
370
371       /* BRC PROBABILITIES */
372       /* a typical code example:
373          46:if (random(slime*4)<4) and (tab[x,y+2] = 0) then\r
374          Begin tab[x,y]:=0;col[x,y+2]:=col[x,y];tab[x,y+2]:=27;mat[x,y+2]:=9;Voice4:=2;end;\r
375          where slime is the byte loaded from the file as it is.
376          pascal random function generates a random number between 0..limit-1, inclusive, for random(limit).
377
378          so a random number between 0..limit*4-1 is generated.
379          for limit=1, 0..3, which is always < 4, so P=1.
380          for limit=2, 0..7, 0..7 is < 4 in P=50%.
381          for limit=3, 0..11, is < 4 in P=33%.
382          So the probability is exactly 100%/limit.
383          just make sure we do not divide by zero for some broken input.
384       */
385
386       if (data[7 * c + datapos] == 0)
387         Warn("amoeba growth cannot be zero, error at byte %d",
388              data[7 * c + datapos]);
389       else
390         cave->amoeba_growth_prob = 1E6 / data[7 * c + datapos] + 0.5; /* 0.5 for rounding */
391
392       if (data[8 * c + datapos] == 0)
393         Warn("amoeba growth cannot be zero, error at byte %d",
394              data[8 * c + datapos]);
395       else
396         cave->amoeba_fast_growth_prob = 1E6 / data[8 * c + datapos] + 0.5; /* 0.5 for rounding */
397
398       cave->slime_predictable = FALSE;
399
400       for (i = 0; i < 5; i++)
401         cave->level_slime_permeability[i] = 1E6 / data[9 * c + datapos] + 0.5;   /* 0.5 for rounding */
402
403       /* probability -> *1E6 */
404       cave->acid_spread_ratio = 1E6 / data[10 * c + datapos] + 0.5;
405
406       /* br only allowed values 1..8 in here, but works the same way. prob -> *1E6 */
407       cave->pushing_stone_prob = 1E6 / data[11 * c + datapos] + 0.5;
408
409       cave->magic_wall_stops_amoeba = (data[12 * c + datapos + 1] != 0);
410       cave->intermission = (cavenum >= 16 || data[14 * c + datapos + 1] != 0);
411
412       /* colors */
413       colind = data[31 * c + datapos] % G_N_ELEMENTS(brc_color_table);
414       cave->colorb = 0x000000;    /* fixed rgb black */
415       cave->color0 = 0x000000;    /* fixed rgb black */
416       cave->color1 = brc_color_table[colind];
417       cave->color2 = brc_color_table_comp[colind];    /* complement */
418       cave->color3 = 0xffffff;    /* white for brick */
419       cave->color4 = 0xe5ad23;    /* fixed for amoeba */
420       cave->color5 = 0x8af713;    /* fixed for slime */
421
422       if (import_effect)
423       {
424         cave->amoeba_enclosed_effect = brc_effect(data[14 * c + datapos + 1]);
425         cave->amoeba_too_big_effect  = brc_effect(data[15 * c + datapos + 1]);
426         cave->explosion_effect       = brc_effect(data[16 * c + datapos + 1]);
427         cave->bomb_explosion_effect  = brc_effect(data[17 * c + datapos + 1]);
428
429         /* 18 solid bomb explode to */
430         cave->diamond_birth_effect    = brc_effect(data[19 * c + datapos + 1]);
431         cave->stone_bouncing_effect   = brc_effect(data[20 * c + datapos + 1]);
432         cave->diamond_bouncing_effect = brc_effect(data[21 * c + datapos + 1]);
433         cave->magic_diamond_to        = brc_effect(data[22 * c + datapos + 1]);
434         cave->acid_eats_this          = brc_effect(data[23 * c + datapos + 1]);
435
436         /* slime eats:
437            (diamond,boulder,bomb),
438            (diamond,boulder),
439            (diamond,bomb),
440            (boulder,bomb) */
441         cave->amoeba_enclosed_effect = brc_effect(data[14 * c + datapos + 1]);
442       }
443     }
444   }
445
446   /* put them in the caveset - take correct order into consideration. */
447   for (level = 0; level < 5; level++)
448   {
449     int cavenum;
450
451     for (cavenum = 0; cavenum < 20; cavenum++)
452     {
453       static const int reorder[] =
454       {
455         0, 1, 2, 3, 16, 4, 5, 6, 7, 17, 8, 9, 10, 11, 18, 12, 13, 14, 15, 19
456       };
457       GdCave *cave = imported[level * 20 + reorder[cavenum]];
458       boolean only_dirt;
459       int x, y;
460
461       /* check if cave contains only dirt.
462          that is an empty cave, and do not import. */
463       only_dirt = TRUE;
464
465       for (y = 1; y < cave->h - 1 && only_dirt; y++)
466         for (x = 1; x < cave->w - 1 && only_dirt; x++)
467           if (cave->map[y][x] != O_DIRT)
468             only_dirt = FALSE;
469
470       /* append to caveset or forget it. */
471       if (!only_dirt)
472         gd_caveset = list_append(gd_caveset, cave);
473       else
474         gd_cave_free(cave);
475     }
476   }
477 }
478
479 static void caveset_name_set_from_filename(const char *filename)
480 {
481   char *name;
482   char *c;
483
484   /* make up a caveset name from the filename. */
485   name = g_path_get_basename(filename);
486   gd_strcpy(gd_caveset_data->name, name);
487   free(name);
488
489   /* convert underscores to spaces */
490   while ((c = strchr (gd_caveset_data->name, '_')) != NULL)
491     *c = ' ';
492
493   /* remove extension */
494   if ((c = strrchr (gd_caveset_data->name, '.')) != NULL)
495     *c = 0;
496 }
497
498 /* Load caveset from file.
499    Loads the caveset from a file.
500
501    File type is autodetected by extension.
502    param filename: Name of file.
503    result: FALSE if failed
504 */
505 boolean gd_caveset_load_from_file(char *filename)
506 {
507   GError *error = NULL;
508   gsize length;
509   char *buf;
510   boolean read;
511   List *new_caveset;
512   struct stat st;
513
514   if (g_stat(filename, &st) != 0)
515   {
516     Warn("cannot stat() file");
517
518     return FALSE;
519   }
520
521   if (st.st_size > 1048576)
522   {
523     Warn("file bigger than 1MiB, refusing to load");
524
525     return FALSE;
526   }
527
528   read = g_file_get_contents (filename, &buf, &length, &error);
529   if (!read)
530   {
531     Warn("%s", error->message);
532
533     g_error_free(error);
534
535     return FALSE;
536   }
537
538   if (strSuffix(filename, ".brc") ||
539       strSuffix(filename, ".BRC"))
540   {
541     /* loading a boulder remake file */
542     if (length != 96000)
543     {
544       Warn("BRC files must be 96000 bytes long");
545
546       return FALSE;
547     }
548   }
549
550   if (strSuffix(filename, ".brc") ||
551       strSuffix(filename, "*.BRC"))
552   {
553     brc_import((guint8 *) buf);
554     gd_caveset_edited = FALSE;    /* newly loaded cave is not edited */
555     gd_caveset_last_selected = caveset_first_selectable_cave_index();
556     gd_caveset_last_selected_level = 0;
557     free(buf);
558     caveset_name_set_from_filename(filename);
559
560     return TRUE;
561   }
562
563   /* BDCFF */
564   if (gd_caveset_imported_get_format((guint8 *) buf) == GD_FORMAT_UNKNOWN)
565   {
566     /* try to load as bdcff */
567     boolean result;
568
569     /* bdcff: start another function */
570     result = gd_caveset_load_from_bdcff(buf);
571
572     /* newly loaded file is not edited. */
573     gd_caveset_edited = FALSE;
574
575     gd_caveset_last_selected = caveset_first_selectable_cave_index();
576     gd_caveset_last_selected_level = 0;
577     free(buf);
578
579     return result;
580   }
581
582   /* try to load as a binary file, as we know the format */
583   new_caveset = gd_caveset_import_from_buffer ((guint8 *) buf, length);
584   free(buf);
585
586   /* if unable to load, exit here. error was reported by import_from_buffer() */
587   if (!new_caveset)
588     return FALSE;
589
590   /* no serious error :) */
591
592   /* only clear caveset here. if file read was unsuccessful, caveset remains in memory. */
593   gd_caveset_clear();
594
595   gd_caveset = new_caveset;
596
597   /* newly loaded cave is not edited */
598   gd_caveset_edited = FALSE;
599
600   gd_caveset_last_selected = caveset_first_selectable_cave_index();
601   gd_caveset_last_selected_level = 0;
602   caveset_name_set_from_filename(filename);
603
604   return TRUE;
605 }
606
607 int gd_cave_check_replays(GdCave *cave, boolean report, boolean remove, boolean repair)
608 {
609   List *riter;
610   int wrong = 0;
611
612   riter = cave->replays;
613   while (riter != NULL)
614   {
615     GdReplay *replay = (GdReplay *)riter->data;
616     guint32 checksum;
617     GdCave *rendered;
618     List *next = riter->next;
619
620     rendered = gd_cave_new_rendered(cave, replay->level, replay->seed);
621     checksum = gd_cave_adler_checksum(rendered);
622     gd_cave_free(rendered);
623
624     replay->wrong_checksum = FALSE;
625
626     /* count wrong ones... the checksum might be changed later to "repair" */
627     if (replay->checksum != 0 && checksum != replay->checksum)
628       wrong++;
629
630     if (replay->checksum == 0 || repair)
631     {
632       /* if no checksum found, add one. or if repair requested, overwrite old one. */
633       replay->checksum = checksum;
634     }
635     else
636     {
637       /* if has a checksum, compare with this one. */
638       if (replay->checksum != checksum)
639       {
640         replay->wrong_checksum = TRUE;
641
642         if (report)
643           Warn("%s: replay played by %s at %s has wrong checksum",
644                cave->name, replay->player_name, replay->date);
645
646         if (remove)
647         {
648           /* may remove */
649           cave->replays = list_remove_link(cave->replays, riter);
650           gd_replay_free(replay);
651         }
652       }
653     }
654
655     /* advance to next list item which we remembered. the current one might have been deleted */
656     riter = next;
657   }
658
659   return wrong;
660 }
661
662 boolean gd_caveset_has_replays(void)
663 {
664   List *citer;
665
666   /* for all caves */
667   for (citer = gd_caveset; citer != NULL; citer = citer->next)
668   {
669     GdCave *cave = (GdCave *)citer->data;
670
671     if (cave->replays)
672       return TRUE;
673   }
674
675   /* if neither of the caves had a replay, */
676   return FALSE;
677 }