minor code formatting changes
[rocksndiamonds.git] / src / game_bd / bd_c64import.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 "main_bd.h"
18
19
20 // make all caves selectable.
21 static boolean gd_import_as_all_caves_selectable = TRUE;
22
23 // conversion table for imported bd1 caves.
24 static const GdElement bd1_import_table[] =
25 {
26   /*  0 */ O_SPACE, O_DIRT, O_BRICK, O_MAGIC_WALL,
27   /*  4 */ O_PRE_OUTBOX, O_OUTBOX, O_STEEL_EXPLODABLE, O_STEEL,
28   /*  8 */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
29   /*  c */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
30   /* 10 */ O_STONE, O_STONE, O_STONE_F, O_STONE_F,
31   /* 14 */ O_DIAMOND, O_DIAMOND, O_DIAMOND_F, O_DIAMOND_F,
32   // ----- ACID: marek roth extension in crazy dream 3
33   /* 18 */ O_ACID, O_ACID, O_EXPLODE_1, O_EXPLODE_2,
34   /* 1c */ O_EXPLODE_3, O_EXPLODE_4, O_EXPLODE_5, O_PRE_DIA_1,
35   /* 20 */ O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4, O_PRE_DIA_5,
36   /* 24 */ O_PRE_DIA_5, O_INBOX, O_PRE_PL_1, O_PRE_PL_2,
37   /* 28 */ O_PRE_PL_3, O_PRE_PL_3, O_H_EXPANDING_WALL, O_H_EXPANDING_WALL,
38   /* 2c */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
39   /* 30 */ O_BUTTER_4, O_BUTTER_1, O_BUTTER_2, O_BUTTER_3,
40   /* 34 */ O_BUTTER_4, O_BUTTER_1, O_BUTTER_2, O_BUTTER_3,
41   /* 38 */ O_PLAYER, O_PLAYER, O_AMOEBA, O_AMOEBA,
42   /* 3c */ O_VOODOO, O_INVIS_OUTBOX, O_SLIME, O_UNKNOWN
43 };
44
45 // conversion table for imported plck caves.
46 static const GdElement plck_import_nybble[] =
47 {
48   /*  0 */ O_STONE, O_DIAMOND, O_MAGIC_WALL, O_BRICK,
49   /*  4 */ O_STEEL, O_H_EXPANDING_WALL, O_VOODOO, O_DIRT,
50   /*  8 */ O_FIREFLY_1, O_BUTTER_4, O_AMOEBA, O_SLIME,
51   /* 12 */ O_PRE_INVIS_OUTBOX, O_PRE_OUTBOX, O_INBOX, O_SPACE
52 };
53
54 // conversion table for imported 1stb caves.
55 static const GdElement firstboulder_import_table[] =
56 {
57   /*  0 */ O_SPACE, O_DIRT, O_BRICK, O_MAGIC_WALL,
58   /*  4 */ O_PRE_OUTBOX, O_OUTBOX, O_PRE_INVIS_OUTBOX, O_INVIS_OUTBOX,
59   /*  8 */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
60   /*  c */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
61   /* 10 */ O_STONE, O_STONE, O_STONE_F, O_STONE_F,
62   /* 14 */ O_DIAMOND, O_DIAMOND, O_DIAMOND_F, O_DIAMOND_F,
63   /* 18 */ O_PRE_CLOCK_1, O_PRE_CLOCK_2, O_PRE_CLOCK_3, O_PRE_CLOCK_4,
64   /* 1c */ O_BITER_SWITCH, O_BITER_SWITCH, O_BLADDER_SPENDER, O_PRE_DIA_1,
65   /* 20 */ O_PRE_DIA_1, O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4,
66   /* 24 */ O_PRE_DIA_5, O_INBOX, O_PRE_PL_1, O_PRE_PL_2,
67   // ----- CLOCK: not mentioned in marek's bd inside faq
68   /* 28 */ O_PRE_PL_3, O_CLOCK, O_H_EXPANDING_WALL, O_H_EXPANDING_WALL,
69   /* 2c */ O_CREATURE_SWITCH, O_CREATURE_SWITCH, O_EXPANDING_WALL_SWITCH, O_EXPANDING_WALL_SWITCH,
70   /* 30 */ O_BUTTER_3, O_BUTTER_4, O_BUTTER_1, O_BUTTER_2,
71   /* 34 */ O_BUTTER_3, O_BUTTER_4, O_BUTTER_1, O_BUTTER_2,
72   /* 38 */ O_STEEL, O_SLIME, O_BOMB, O_SWEET,
73   /* 3c */ O_PRE_STONE_1, O_PRE_STONE_2, O_PRE_STONE_3, O_PRE_STONE_4,
74   /* 40 */ O_BLADDER, O_BLADDER_1, O_BLADDER_2, O_BLADDER_3,
75   /* 44 */ O_BLADDER_4, O_BLADDER_5, O_BLADDER_6, O_BLADDER_7,
76   /* 48 */ O_BLADDER_8, O_BLADDER_8, O_EXPLODE_1, O_EXPLODE_1,
77   /* 4c */ O_EXPLODE_2, O_EXPLODE_3, O_EXPLODE_4, O_EXPLODE_5,
78   /* 50 */ O_PLAYER, O_PLAYER, O_PLAYER_BOMB, O_PLAYER_BOMB,
79   /* 54 */ O_PLAYER_GLUED, O_PLAYER_GLUED, O_VOODOO, O_AMOEBA,
80   /* 58 */ O_AMOEBA, O_BOMB_TICK_1, O_BOMB_TICK_2, O_BOMB_TICK_3,
81   /* 5c */ O_BOMB_TICK_4, O_BOMB_TICK_5, O_BOMB_TICK_6, O_BOMB_TICK_7,
82   /* 60 */ O_BOMB_EXPL_1, O_BOMB_EXPL_2, O_BOMB_EXPL_3, O_BOMB_EXPL_4,
83   /* 64 */ O_GHOST, O_GHOST, O_GHOST_EXPL_1, O_GHOST_EXPL_2,
84   /* 68 */ O_GHOST_EXPL_3, O_GHOST_EXPL_4, O_GRAVESTONE, O_STONE_GLUED,
85   /* 6c */ O_DIAMOND_GLUED, O_DIAMOND_KEY, O_TRAPPED_DIAMOND, O_GRAVESTONE,
86   /* 70 */ O_WAITING_STONE, O_WAITING_STONE, O_CHASING_STONE, O_CHASING_STONE,
87   /* 74 */ O_PRE_STEEL_1, O_PRE_STEEL_2, O_PRE_STEEL_3, O_PRE_STEEL_4,
88   /* 78 */ O_BITER_1, O_BITER_2, O_BITER_3, O_BITER_4,
89   /* 7c */ O_BITER_1, O_BITER_2, O_BITER_3, O_BITER_4,
90 };
91
92 // conversion table for imported crazy dream caves.
93 static const GdElement crazydream_import_table[] =
94 {
95   /*  0 */ O_SPACE, O_DIRT, O_BRICK, O_MAGIC_WALL,
96   /*  4 */ O_PRE_OUTBOX, O_OUTBOX, O_PRE_INVIS_OUTBOX, O_INVIS_OUTBOX,
97   /*  8 */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
98   /*  c */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
99   /* 10 */ O_STONE, O_STONE, O_STONE_F, O_STONE_F,
100   /* 14 */ O_DIAMOND, O_DIAMOND, O_DIAMOND_F, O_DIAMOND_F,
101   /* 18 */ O_PRE_CLOCK_1, O_PRE_CLOCK_2, O_PRE_CLOCK_3, O_PRE_CLOCK_4,
102   /* 1c */ O_BITER_SWITCH, O_BITER_SWITCH, O_BLADDER_SPENDER, O_PRE_DIA_1,
103   // ----- 6 different stages
104   /* 20 */ O_PRE_DIA_1, O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4,
105   /* 24 */ O_PRE_DIA_5, O_INBOX, O_PRE_PL_1, O_PRE_PL_2,
106   // ----- CLOCK: not mentioned in marek's bd inside faq
107   /* 28 */ O_PRE_PL_3, O_CLOCK, O_H_EXPANDING_WALL, O_H_EXPANDING_WALL,
108   /* 2c */ O_CREATURE_SWITCH, O_CREATURE_SWITCH, O_EXPANDING_WALL_SWITCH, O_EXPANDING_WALL_SWITCH,
109   /* 30 */ O_BUTTER_3, O_BUTTER_4, O_BUTTER_1, O_BUTTER_2,
110   /* 34 */ O_BUTTER_3, O_BUTTER_4, O_BUTTER_1, O_BUTTER_2,
111   /* 38 */ O_STEEL, O_SLIME, O_BOMB, O_SWEET,
112   /* 3c */ O_PRE_STONE_1, O_PRE_STONE_2, O_PRE_STONE_3, O_PRE_STONE_4,
113   /* 40 */ O_BLADDER, O_BLADDER_1, O_BLADDER_2, O_BLADDER_3,
114   /* 44 */ O_BLADDER_4, O_BLADDER_5, O_BLADDER_6, O_BLADDER_7,
115   /* 48 */ O_BLADDER_8, O_BLADDER_8|SCANNED, O_EXPLODE_1, O_EXPLODE_1,
116   /* 4c */ O_EXPLODE_2, O_EXPLODE_3, O_EXPLODE_4, O_EXPLODE_5,
117   /* 50 */ O_PLAYER, O_PLAYER, O_PLAYER_BOMB, O_PLAYER_BOMB,
118   /* 54 */ O_PLAYER_GLUED, O_PLAYER_GLUED, O_VOODOO, O_AMOEBA,
119   /* 58 */ O_AMOEBA, O_BOMB_TICK_1, O_BOMB_TICK_2, O_BOMB_TICK_3,
120   /* 5c */ O_BOMB_TICK_4, O_BOMB_TICK_5, O_BOMB_TICK_6, O_BOMB_TICK_7,
121   /* 60 */ O_BOMB_EXPL_1, O_BOMB_EXPL_2, O_BOMB_EXPL_3, O_BOMB_EXPL_4,
122   /* 64 */ O_GHOST, O_GHOST, O_GHOST_EXPL_1, O_GHOST_EXPL_2,
123   /* 68 */ O_GHOST_EXPL_3, O_GHOST_EXPL_4, O_GRAVESTONE, O_STONE_GLUED,
124   /* 6c */ O_DIAMOND_GLUED, O_DIAMOND_KEY, O_TRAPPED_DIAMOND, O_GRAVESTONE,
125   /* 70 */ O_WAITING_STONE, O_WAITING_STONE, O_CHASING_STONE, O_CHASING_STONE,
126   /* 74 */ O_PRE_STEEL_1, O_PRE_STEEL_2, O_PRE_STEEL_3, O_PRE_STEEL_4,
127   /* 78 */ O_BITER_1, O_BITER_2, O_BITER_3, O_BITER_4,
128   /* 7c */ O_BITER_1, O_BITER_2, O_BITER_3, O_BITER_4,
129
130   /* 80 */ O_POT, O_PLAYER_STIRRING, O_GRAVITY_SWITCH, O_GRAVITY_SWITCH,
131   /* 84 */ O_PNEUMATIC_HAMMER, O_PNEUMATIC_HAMMER, O_BOX, O_BOX,
132   /* 88 */ O_UNKNOWN, O_UNKNOWN, O_ACID, O_ACID,
133   /* 8c */ O_KEY_1, O_KEY_2, O_KEY_3, O_UNKNOWN,
134   /* 90 */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
135   /* 94 */ O_UNKNOWN, O_TELEPORTER, O_UNKNOWN, O_SKELETON,
136   /* 98 */ O_WATER, O_WATER_16, O_WATER_15, O_WATER_14,
137   /* 9c */ O_WATER_13, O_WATER_12, O_WATER_11, O_WATER_10,
138   /* a0 */ O_WATER_9, O_WATER_8, O_WATER_7, O_WATER_6,
139   /* a4 */ O_WATER_5, O_WATER_4, O_WATER_3, O_WATER_2,
140   /* a8 */ O_WATER_1, O_COW_ENCLOSED_1, O_COW_ENCLOSED_2, O_COW_ENCLOSED_3,
141   /* ac */ O_COW_ENCLOSED_4, O_COW_ENCLOSED_5, O_COW_ENCLOSED_6, O_COW_ENCLOSED_7,
142   /* b0 */ O_COW_1, O_COW_2, O_COW_3, O_COW_4,
143   /* b4 */ O_COW_1, O_COW_2, O_COW_3, O_COW_4,
144   /* b8 */ O_DIRT_GLUED, O_STEEL_EXPLODABLE, O_DOOR_1, O_DOOR_2,
145   /* bc */ O_DOOR_3, O_FALLING_WALL, O_FALLING_WALL_F, O_FALLING_WALL_F,
146   /* c0 */ O_WALLED_DIAMOND, O_UNKNOWN, O_WALLED_KEY_1, O_WALLED_KEY_2,
147   /* c5 = brick?! (vital key), c7 = dirt?! (think twice) */
148   /* c7 = dirt, as it has a code which will change it to dirt. */
149   /* c4 */ O_WALLED_KEY_3, O_BRICK, O_UNKNOWN, O_DIRT,
150   /* c8 */ O_DIRT2, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
151   /* cc */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
152   /* d0 */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
153   /* d4 */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
154   /* d8 */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
155   /* dc */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
156   /* e0 */ O_ALT_FIREFLY_1, O_ALT_FIREFLY_2, O_ALT_FIREFLY_3, O_ALT_FIREFLY_4,
157   /* e4 */ O_ALT_FIREFLY_1, O_ALT_FIREFLY_2, O_ALT_FIREFLY_3, O_ALT_FIREFLY_4,
158   /* e8 */ O_ALT_BUTTER_3, O_ALT_BUTTER_4, O_ALT_BUTTER_1, O_ALT_BUTTER_2,
159   /* ec */ O_ALT_BUTTER_3, O_ALT_BUTTER_4, O_ALT_BUTTER_1, O_ALT_BUTTER_2,
160   /* f0 */ O_WATER, O_WATER, O_WATER, O_WATER,
161   /* f4 */ O_WATER, O_WATER, O_WATER, O_WATER,
162   /* f8 */ O_WATER, O_WATER, O_WATER, O_WATER,
163   /* fc */ O_WATER, O_WATER, O_WATER, O_WATER,
164 };
165
166 // conversion table for imported 1stb caves.
167 const GdElement gd_crazylight_import_table[] =
168 {
169   /*  0 */ O_SPACE, O_DIRT, O_BRICK, O_MAGIC_WALL,
170   /*  4 */ O_PRE_OUTBOX, O_OUTBOX, O_PRE_INVIS_OUTBOX, O_INVIS_OUTBOX,
171   /*  8 */ O_FIREFLY_1, O_FIREFLY_2, O_FIREFLY_3, O_FIREFLY_4,
172   /*  c */ O_FIREFLY_1|SCANNED, O_FIREFLY_2|SCANNED, O_FIREFLY_3|SCANNED, O_FIREFLY_4|SCANNED,
173   /* 10 */ O_STONE, O_STONE|SCANNED, O_STONE_F, O_STONE_F|SCANNED,
174   /* 14 */ O_DIAMOND, O_DIAMOND|SCANNED, O_DIAMOND_F, O_DIAMOND_F|SCANNED,
175   /* 18 */ O_PRE_CLOCK_1, O_PRE_CLOCK_2, O_PRE_CLOCK_3, O_PRE_CLOCK_4,
176   /* 1c */ O_BITER_SWITCH, O_BITER_SWITCH, O_BLADDER_SPENDER, O_PRE_DIA_1,
177   // ----- 6 different stages, the first is the pre_dia_0
178   /* 20 */ O_PRE_DIA_1, O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4,
179   /* 24 */ O_PRE_DIA_5, O_INBOX, O_PRE_PL_1, O_PRE_PL_2,
180   // ----- CLOCK: not mentioned in marek's bd inside faq
181   /* 28 */ O_PRE_PL_3, O_CLOCK, O_H_EXPANDING_WALL, O_H_EXPANDING_WALL|SCANNED,
182   /* 2c */ O_CREATURE_SWITCH, O_CREATURE_SWITCH, O_EXPANDING_WALL_SWITCH, O_EXPANDING_WALL_SWITCH,
183   /* 30 */ O_BUTTER_3, O_BUTTER_4, O_BUTTER_1, O_BUTTER_2,
184   /* 34 */ O_BUTTER_3|SCANNED, O_BUTTER_4|SCANNED, O_BUTTER_1|SCANNED, O_BUTTER_2|SCANNED,
185   /* 38 */ O_STEEL, O_SLIME, O_BOMB, O_SWEET,
186   /* 3c */ O_PRE_STONE_1, O_PRE_STONE_2, O_PRE_STONE_3, O_PRE_STONE_4,
187   /* 40 */ O_BLADDER, O_BLADDER_1, O_BLADDER_2, O_BLADDER_3,
188   /* 44 */ O_BLADDER_4, O_BLADDER_5, O_BLADDER_6, O_BLADDER_7,
189   /* 48 */ O_BLADDER_8, O_BLADDER_8|SCANNED, O_EXPLODE_1, O_EXPLODE_1,
190   /* 4c */ O_EXPLODE_2, O_EXPLODE_3, O_EXPLODE_4, O_EXPLODE_5,
191   /* 50 */ O_PLAYER, O_PLAYER|SCANNED, O_PLAYER_BOMB, O_PLAYER_BOMB|SCANNED,
192   /* 54 */ O_PLAYER_GLUED, O_PLAYER_GLUED|SCANNED, O_VOODOO, O_AMOEBA,
193   /* 58 */ O_AMOEBA|SCANNED, O_BOMB_TICK_1, O_BOMB_TICK_2, O_BOMB_TICK_3,
194   /* 5c */ O_BOMB_TICK_4, O_BOMB_TICK_5, O_BOMB_TICK_6, O_BOMB_TICK_7,
195   /* 60 */ O_BOMB_EXPL_1, O_BOMB_EXPL_2, O_BOMB_EXPL_3, O_BOMB_EXPL_4,
196   /* 64 */ O_ACID, O_ACID, O_FALLING_WALL, O_FALLING_WALL_F,
197   /* 68 */ O_FALLING_WALL_F|SCANNED, O_BOX, O_GRAVESTONE, O_STONE_GLUED,
198   /* 6c */ O_DIAMOND_GLUED, O_DIAMOND_KEY, O_TRAPPED_DIAMOND, O_GRAVESTONE,
199   /* 70 */ O_WAITING_STONE, O_WAITING_STONE|SCANNED, O_CHASING_STONE, O_CHASING_STONE|SCANNED,
200   /* 74 */ O_PRE_STEEL_1, O_PRE_STEEL_2, O_PRE_STEEL_3, O_PRE_STEEL_4,
201   /* 78 */ O_BITER_1, O_BITER_2, O_BITER_3, O_BITER_4,
202   /* 7c */ O_BITER_1|SCANNED, O_BITER_2|SCANNED, O_BITER_3|SCANNED, O_BITER_4|SCANNED,
203 };
204
205 GdPropertyDefault gd_defaults_bd1[] =
206 {
207   { CAVE_OFFSET(level_amoeba_threshold), 200 },
208   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
209   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
210   { CAVE_OFFSET(amoeba_timer_started_immediately), TRUE },
211   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), FALSE },
212   { CAVE_OFFSET(lineshift), TRUE },
213   { CAVE_OFFSET(wraparound_objects), TRUE },
214   { CAVE_OFFSET(diagonal_movements), FALSE },
215   { CAVE_OFFSET(voodoo_collects_diamonds), FALSE },
216   { CAVE_OFFSET(voodoo_dies_by_stone), FALSE },
217   { CAVE_OFFSET(voodoo_disappear_in_explosion), TRUE },
218   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
219   { CAVE_OFFSET(creatures_backwards), FALSE },
220   { CAVE_OFFSET(creatures_direction_auto_change_on_start), FALSE },
221   { CAVE_OFFSET(creatures_direction_auto_change_time), 0 },
222   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
223   { CAVE_OFFSET(intermission_instantlife), TRUE },
224   { CAVE_OFFSET(intermission_rewardlife), FALSE },
225   { CAVE_OFFSET(magic_wall_stops_amoeba), TRUE },
226   { CAVE_OFFSET(magic_wall_breakscan), TRUE },
227   { CAVE_OFFSET(magic_timer_zero_is_infinite), TRUE },
228   { CAVE_OFFSET(magic_timer_wait_for_hatching), FALSE },
229   { CAVE_OFFSET(pushing_stone_prob), 250000 },
230   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
231   { CAVE_OFFSET(active_is_first_found), FALSE },
232   { CAVE_OFFSET(short_explosions), TRUE },
233   { CAVE_OFFSET(slime_predictable), TRUE },
234   { CAVE_OFFSET(snap_element), O_SPACE },
235   { CAVE_OFFSET(max_time), 999 },
236
237   { CAVE_OFFSET(scheduling), GD_SCHEDULING_BD1 },
238   { CAVE_OFFSET(pal_timing), TRUE },
239
240   { -1 },
241 };
242
243 GdPropertyDefault gd_defaults_bd2[] =
244 {
245   { CAVE_OFFSET(level_amoeba_threshold), 200 },
246   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
247   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
248   { CAVE_OFFSET(amoeba_timer_started_immediately), FALSE },
249   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), FALSE },
250   { CAVE_OFFSET(lineshift), TRUE },
251   { CAVE_OFFSET(wraparound_objects), TRUE },
252   { CAVE_OFFSET(diagonal_movements), FALSE },
253   { CAVE_OFFSET(voodoo_collects_diamonds), FALSE },
254   { CAVE_OFFSET(voodoo_dies_by_stone), FALSE },
255   { CAVE_OFFSET(voodoo_disappear_in_explosion), TRUE },
256   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
257   { CAVE_OFFSET(creatures_backwards), FALSE },
258   { CAVE_OFFSET(creatures_direction_auto_change_on_start), FALSE },
259   { CAVE_OFFSET(creatures_direction_auto_change_time), 0 },
260   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
261   { CAVE_OFFSET(intermission_instantlife), TRUE },
262   { CAVE_OFFSET(intermission_rewardlife), FALSE },
263   { CAVE_OFFSET(magic_wall_stops_amoeba), FALSE },    // marek roth bd inside faq 3.0
264   { CAVE_OFFSET(magic_timer_zero_is_infinite), TRUE },
265   { CAVE_OFFSET(magic_timer_wait_for_hatching), FALSE },
266   { CAVE_OFFSET(pushing_stone_prob), 250000 },
267   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
268   { CAVE_OFFSET(active_is_first_found), FALSE },
269   { CAVE_OFFSET(short_explosions), TRUE },
270   { CAVE_OFFSET(slime_predictable), TRUE },
271   { CAVE_OFFSET(snap_element), O_SPACE },
272   { CAVE_OFFSET(max_time), 999 },
273
274   { CAVE_OFFSET(pal_timing), TRUE },
275   { CAVE_OFFSET(scheduling), GD_SCHEDULING_BD2 },
276
277   { -1 },
278 };
279
280 GdPropertyDefault gd_defaults_plck[] =
281 {
282   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
283   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
284   { CAVE_OFFSET(amoeba_timer_started_immediately), FALSE },
285   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), FALSE },
286   { CAVE_OFFSET(lineshift), TRUE },
287   { CAVE_OFFSET(wraparound_objects), TRUE },
288   { CAVE_OFFSET(border_scan_first_and_last), FALSE },
289   { CAVE_OFFSET(diagonal_movements), FALSE },
290   { CAVE_OFFSET(voodoo_collects_diamonds), FALSE },
291   { CAVE_OFFSET(voodoo_dies_by_stone), FALSE },
292   { CAVE_OFFSET(voodoo_disappear_in_explosion), TRUE },
293   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
294   { CAVE_OFFSET(creatures_backwards), FALSE },
295   { CAVE_OFFSET(creatures_direction_auto_change_on_start), FALSE },
296   { CAVE_OFFSET(creatures_direction_auto_change_time), 0 },
297   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
298   { CAVE_OFFSET(intermission_instantlife), TRUE },
299   { CAVE_OFFSET(intermission_rewardlife), FALSE },
300   { CAVE_OFFSET(magic_wall_stops_amoeba), FALSE },
301   { CAVE_OFFSET(magic_timer_zero_is_infinite), TRUE },
302   { CAVE_OFFSET(magic_timer_wait_for_hatching), FALSE },
303   { CAVE_OFFSET(pushing_stone_prob), 250000 },
304   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
305   { CAVE_OFFSET(active_is_first_found), FALSE },
306   { CAVE_OFFSET(short_explosions), TRUE },
307   { CAVE_OFFSET(snap_element), O_SPACE },
308   { CAVE_OFFSET(max_time), 999 },
309
310   { CAVE_OFFSET(pal_timing), TRUE },
311   { CAVE_OFFSET(scheduling), GD_SCHEDULING_PLCK },
312
313   { -1 },
314 };
315
316 GdPropertyDefault gd_defaults_1stb[] =
317 {
318   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
319   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
320   { CAVE_OFFSET(amoeba_timer_started_immediately), FALSE },
321   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), TRUE },
322   { CAVE_OFFSET(lineshift), TRUE },
323   { CAVE_OFFSET(wraparound_objects), TRUE },
324   { CAVE_OFFSET(voodoo_collects_diamonds), TRUE },
325   { CAVE_OFFSET(voodoo_dies_by_stone), TRUE },
326   { CAVE_OFFSET(voodoo_disappear_in_explosion), FALSE },
327   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
328   { CAVE_OFFSET(creatures_direction_auto_change_on_start), TRUE },
329   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
330   { CAVE_OFFSET(intermission_instantlife), FALSE },
331   { CAVE_OFFSET(intermission_rewardlife), TRUE },
332   { CAVE_OFFSET(magic_timer_zero_is_infinite), TRUE },
333   { CAVE_OFFSET(magic_timer_wait_for_hatching), TRUE },
334   { CAVE_OFFSET(pushing_stone_prob), 250000 },
335   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
336   { CAVE_OFFSET(active_is_first_found), TRUE },
337   { CAVE_OFFSET(short_explosions), FALSE },
338   { CAVE_OFFSET(slime_predictable), TRUE },
339   { CAVE_OFFSET(snap_element), O_SPACE },
340   { CAVE_OFFSET(max_time), 999 },
341
342   { CAVE_OFFSET(pal_timing), TRUE },
343   { CAVE_OFFSET(scheduling), GD_SCHEDULING_PLCK },
344   // not immediately to diamond, but with animation
345   { CAVE_OFFSET(amoeba_enclosed_effect), O_PRE_DIA_1 },
346   { CAVE_OFFSET(dirt_looks_like), O_DIRT2 },
347
348   { -1 },
349 };
350
351 GdPropertyDefault gd_defaults_crdr_7[] =
352 {
353   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
354   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
355   { CAVE_OFFSET(amoeba_timer_started_immediately), FALSE },
356   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), TRUE },
357   { CAVE_OFFSET(lineshift), TRUE },
358   { CAVE_OFFSET(wraparound_objects), TRUE },
359   { CAVE_OFFSET(voodoo_collects_diamonds), TRUE },
360   { CAVE_OFFSET(voodoo_dies_by_stone), TRUE },
361   { CAVE_OFFSET(voodoo_disappear_in_explosion), FALSE },
362   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
363   { CAVE_OFFSET(creatures_direction_auto_change_on_start), FALSE },
364   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
365   { CAVE_OFFSET(intermission_instantlife), FALSE },
366   { CAVE_OFFSET(intermission_rewardlife), TRUE },
367   { CAVE_OFFSET(magic_timer_zero_is_infinite), FALSE },
368   { CAVE_OFFSET(magic_timer_wait_for_hatching), TRUE },
369   { CAVE_OFFSET(pushing_stone_prob), 250000 },
370   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
371   { CAVE_OFFSET(active_is_first_found), TRUE },
372   { CAVE_OFFSET(short_explosions), FALSE },
373   { CAVE_OFFSET(slime_predictable), TRUE },
374   { CAVE_OFFSET(snap_element), O_SPACE },
375   { CAVE_OFFSET(max_time), 999 },
376
377   { CAVE_OFFSET(pal_timing), TRUE },
378   { CAVE_OFFSET(scheduling), GD_SCHEDULING_CRDR },
379   // not immediately to diamond, but with animation
380   { CAVE_OFFSET(amoeba_enclosed_effect), O_PRE_DIA_1 },
381   { CAVE_OFFSET(water_does_not_flow_down), TRUE },
382   // in crdr, skeletons can also be used to open the gate
383   { CAVE_OFFSET(skeletons_worth_diamonds), 1 },
384   // the intermission "survive" needs this flag
385   { CAVE_OFFSET(gravity_affects_all), FALSE },
386
387   { -1 },
388 };
389
390 GdPropertyDefault gd_defaults_crli[] =
391 {
392   { CAVE_OFFSET(amoeba_growth_prob), 31250 },
393   { CAVE_OFFSET(amoeba_fast_growth_prob), 250000 },
394   { CAVE_OFFSET(amoeba_timer_started_immediately), FALSE },
395   { CAVE_OFFSET(amoeba_timer_wait_for_hatching), TRUE },
396   { CAVE_OFFSET(lineshift), TRUE },
397   { CAVE_OFFSET(wraparound_objects), TRUE },
398   { CAVE_OFFSET(voodoo_collects_diamonds), TRUE },
399   { CAVE_OFFSET(voodoo_dies_by_stone), TRUE },
400   { CAVE_OFFSET(voodoo_disappear_in_explosion), FALSE },
401   { CAVE_OFFSET(voodoo_any_hurt_kills_player), FALSE },
402   { CAVE_OFFSET(creatures_direction_auto_change_on_start), FALSE },
403   { CAVE_OFFSET(level_hatching_delay_time[0]), 2 },
404   { CAVE_OFFSET(intermission_instantlife), FALSE },
405   { CAVE_OFFSET(intermission_rewardlife), TRUE },
406   { CAVE_OFFSET(magic_timer_zero_is_infinite), FALSE },
407   { CAVE_OFFSET(magic_timer_wait_for_hatching), TRUE },
408   { CAVE_OFFSET(pushing_stone_prob), 250000 },
409   { CAVE_OFFSET(pushing_stone_prob_sweet), 1000000 },
410   { CAVE_OFFSET(active_is_first_found), TRUE },
411   { CAVE_OFFSET(short_explosions), FALSE },
412   { CAVE_OFFSET(slime_predictable), TRUE },
413   { CAVE_OFFSET(max_time), 999 },
414
415   { CAVE_OFFSET(pal_timing), TRUE },
416   { CAVE_OFFSET(scheduling), GD_SCHEDULING_PLCK },
417   // not immediately to diamond, but with animation
418   { CAVE_OFFSET(amoeba_enclosed_effect), O_PRE_DIA_1 },
419
420   { -1 },
421 };
422
423
424 // internal character (letter) codes in c64 games.
425 // missing: "triple line" after >, diamond between ()s, player's head after )
426 // used for converting names of caves imported from crli and other types of binary data
427 const char gd_bd_internal_chars[] =
428   "            ,!./0123456789:*<=>  ABCDEFGHIJKLMNOPQRSTUVWXYZ( ) _";
429
430 // used for bdcff engine flag.
431 const char *gd_engines[] =
432 {
433   "BD1",
434   "BD2",
435   "PLCK",
436   "1stB",
437   "CrDr",
438   "CrLi"
439 };
440
441 // to convert predictable slime values to bit masks
442 static int slime_shift_msb(int c64_data)
443 {
444   int i, perm;
445
446   perm = 0;
447
448   for (i = 0; i < c64_data; i++)
449     // shift in this many msb 1's
450     perm = (0x100|perm) >> 1;
451
452   return perm;
453 }
454
455 static GdElement bd1_import(byte c, int i)
456 {
457   if (c < ARRAY_SIZE(bd1_import_table))
458     return bd1_import_table[c];
459
460   Warn("Invalid BD1 element in imported file at cave data %d: %d", i, c);
461
462   return O_UNKNOWN;
463 }
464
465 // deluxe caves 1 contained a special element, non-sloped brick.
466 static GdElement deluxecaves_1_import(byte c, int i)
467 {
468   GdElement e = bd1_import(c, i);
469
470   if (e == O_H_EXPANDING_WALL)
471     e = O_BRICK_NON_SLOPED;
472
473   return e;
474 }
475
476 static GdElement firstboulder_import(byte c, int i)
477 {
478   if (c < ARRAY_SIZE(firstboulder_import_table))
479     return firstboulder_import_table[c];
480
481   Warn("Invalid 1stB element in imported file at cave data %d: %d", i, c);
482
483   return O_UNKNOWN;
484 }
485
486 static GdElement crazylight_import(byte c, int i)
487 {
488   if (c < ARRAY_SIZE(gd_crazylight_import_table))
489     return gd_crazylight_import_table[c] & O_MASK;    // & O_MASK: do not import "scanned" flag
490
491   Warn("Invalid CrLi element in imported file at cave data %d: %d", i, c);
492
493   return O_UNKNOWN;
494 }
495
496 GdPropertyDefault *gd_get_engine_default_array(GdEngine engine)
497 {
498   switch(engine)
499   {
500     case GD_ENGINE_BD1:
501       return gd_defaults_bd1;
502       break;
503
504     case GD_ENGINE_BD2:
505       return gd_defaults_bd2;
506       break;
507
508     case GD_ENGINE_PLCK:
509       return gd_defaults_plck;
510       break;
511
512     case GD_ENGINE_1STB:
513       return gd_defaults_1stb;
514       break;
515
516     case GD_ENGINE_CRDR7:
517       return gd_defaults_crdr_7;
518       break;
519
520     case GD_ENGINE_CRLI:
521       return gd_defaults_crli;
522       break;
523
524       // to avoid compiler warning
525     case GD_ENGINE_INVALID:
526       break;
527   }
528
529   return gd_defaults_bd1;
530 }
531
532 void gd_cave_set_engine_defaults(GdCave *cave, GdEngine engine)
533 {
534   gd_cave_set_defaults_from_array(cave, gd_get_engine_default_array(engine));
535
536   // these have hardcoded ckdelay.
537   // setting this ckdelay array does not fit into the gd_struct_default scheme.
538   if (engine == GD_ENGINE_BD1)
539   {
540     cave->level_ckdelay[0] = 12;
541     cave->level_ckdelay[1] = 6;
542     cave->level_ckdelay[2] = 3;
543     cave->level_ckdelay[3] = 1;
544     cave->level_ckdelay[4] = 0;
545   }
546
547   if (engine == GD_ENGINE_BD2)
548   {
549     cave->level_ckdelay[0] = 9;    // 180ms
550     cave->level_ckdelay[1] = 8;    // 160ms
551     cave->level_ckdelay[2] = 7;    // 140ms
552     cave->level_ckdelay[3] = 6;    // 120ms
553     cave->level_ckdelay[4] = 6;    // 120ms (!) not faster than level4
554   }
555 }
556
557 GdEngine gd_cave_get_engine_from_string(const char *param)
558 {
559   int i;
560
561   for (i = 0; i < GD_ENGINE_INVALID; i++)
562     if (strcasecmp(param, gd_engines[i]) == 0)
563       return (GdEngine)i;
564
565   return GD_ENGINE_INVALID;
566 }
567
568 // ============================================================================
569 //
570 // cave import routines.
571 // take a cave, data, and maybe remaining bytes.
572 // return the number of bytes read, -1 if error.
573 //
574 // ============================================================================
575
576 /*
577   take care of required diamonds values == 0 or > 100.
578   in original bd, the counter was only two-digit. so bd3 cave f
579   says 150 diamonds required, but you only had to collect 50.
580   also, gate opening is triggered by incrementing diamond
581   count and THEN checking if more required; so if required was
582   0, you had to collect 100. (also check crazy light 8 cave "1000")
583
584   http://www.boulder-dash.nl/forum/viewtopic.php?t=88
585 */
586
587 // import bd1 cave data into our format.
588 static int cave_copy_from_bd1(GdCave *cave, const byte *data, int remaining_bytes,
589                               GdCavefileFormat format)
590 {
591   int length, direction;
592   int index;
593   int level;
594   int x1, y1, x2, y2;
595   byte code;
596   GdElement elem;
597   GdElement (* import_func) (byte c, int i);
598   int i;
599
600   // cant be shorted than this: header + no objects + delimiter
601   if (remaining_bytes < 33)
602   {
603     Error("truncated BD1 cave data, %d bytes", remaining_bytes);
604
605     return -1;
606   }
607
608   gd_cave_set_engine_defaults(cave, GD_ENGINE_BD1);
609
610   if (format == GD_FORMAT_BD1_ATARI)
611     cave->scheduling = GD_SCHEDULING_BD1_ATARI;
612
613   if (format == GD_FORMAT_DC1)
614     import_func = deluxecaves_1_import;
615   else
616     import_func = bd1_import;
617
618   // set visible size for intermission
619   if (cave->intermission)
620   {
621     cave->x2 = 19;
622     cave->y2 = 11;
623   }
624
625   // cave number data[0]
626   cave->diamond_value = data[2];
627   cave->extra_diamond_value = data[3];
628
629   for (level = 0; level < 5; level++)
630   {
631     cave->level_amoeba_time[level] = data[1];
632
633     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
634     if (cave->level_amoeba_time[level] == 0)
635       cave->level_amoeba_time[level] = 999;
636
637     cave->level_magic_wall_time[level] = data[1];
638     cave->level_rand[level] = data[4 + level];
639     cave->level_diamonds[level] = data[9 + level] % 100;    // check comment above
640
641     // gate opening is checked AFTER adding to diamonds collected, so 0 here means 100 to collect
642     if (cave->level_diamonds[level] == 0)
643       cave->level_diamonds[level] = 100;
644     cave->level_time[level] = data[14 + level];
645   }
646
647   /*
648     LogicDeLuxe extension: acid
649     $16 Acid speed (unused in the original BD1)
650     $17 Bit 2: if set, Acid's original position converts to explosion puff during spreading.
651     Otherwise, Acid remains intact, ie. it's just growing. (unused in the original BD1)
652     $1C Acid eats this element. (also Probability of element 1)
653
654     there is no problem importing these; as other bd1 caves did not contain acid at all,
655     so it does not matter how we set the values.
656   */
657
658   // 0x1c index: same as probability1 !!!!! don't be surprised. we do a &0x3f because of this
659   cave->acid_eats_this = import_func(data[0x1c] & 0x3F, 0x1c);
660
661   // acid speed, *1e6 as probabilities are stored in int
662   cave->acid_spread_ratio = data[0x16] / 255.0 * 1E6 + 0.5;
663
664   cave->acid_turns_to = (data[0x17] & (1 << 2)) ? O_EXPLODE_3 : O_ACID;
665
666   cave->colorb = GD_GDASH_BLACK;    // border - black
667   cave->color0 = GD_GDASH_BLACK;    // background - black
668   cave->color1= GD_GDASH_RED;
669   cave->color2 = GD_GDASH_PURPLE;
670   cave->color3 = GD_GDASH_YELLOW;
671   cave->color4 = cave->color3;    // in bd1, amoeba was color3
672   cave->color5 = cave->color3;    // no slime, but let it be color 3
673
674   // random fill
675   for (i = 0; i < 4; i++)
676   {
677     cave->random_fill[i] = import_func(data[24 + i], 24 + i);
678     cave->random_fill_probability[i] = data[28 + i];
679   }
680
681   /*
682    * Decode the explicit cave data
683    */
684   index = 32;
685
686   while (data[index] != 0xFF && index < remaining_bytes && index < 255)
687   {
688     code = data[index];
689
690     // crazy dream 3 extension:
691     if (code == 0x0f)
692     {
693       int x1, y1, nx, ny, dx, dy;
694       int x, y;
695
696       // as this one uses nonstandard dx dy values, create points instead
697       elem = import_func(data[index + 1], index + 1);
698       x1 = data[index + 2];
699       y1 = data[index + 3] - 2;
700       nx = data[index + 4];
701       ny = data[index + 5];
702       dx = data[index + 6];
703       dy = data[index + 7] + 1;
704
705       for (y = 0; y < ny; y++)
706       {
707         for (x = 0; x < nx; x++)
708         {
709           int pos = x1 + y1 * 40 + y * dy * 40 + x * dx;
710
711           cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, pos % 40, pos / 40, elem));
712         }
713       }
714
715       index += 8;
716     }
717     else
718     {
719       // object is code & 3f, object type is upper 2 bits
720       elem = import_func(code & 0x3F, index);
721
722       switch ((code >> 6) & 3)
723       {
724         case 0:                // 00: POINT
725           x1 = data[index + 1];
726           y1 = data[index + 2] - 2;
727
728           if (x1 >= cave->w || y1 >= cave->h)
729             Warn("invalid point coordinates %d,%d at byte %d", x1, y1, index);
730
731           cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, x1, y1, elem));
732
733           index += 3;
734           break;
735
736         case 1:                // 01: LINE
737           x1 = data[index + 1];
738           y1 = data[index + 2] - 2;
739           length = (byte)data[index + 3] - 1;
740           direction = data[index + 4];
741
742           if (length < 0)
743           {
744             Warn("line length negative, not displaying line at all, at byte %d", index);
745           }
746           else
747           {
748             if (direction > GD_MV_UP_LEFT)
749             {
750               Warn("invalid line direction %d at byte %d", direction, index);
751               direction = GD_MV_STILL;
752             }
753
754             x2 = x1 + length * gd_dx[direction + 1];
755             y2 = y1 + length * gd_dy[direction + 1];
756
757             if (x1 >= cave->w ||
758                 y1 >= cave->h ||
759                 x2 >= cave->w ||
760                 y2 >= cave->h)
761               Warn("invalid line coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
762
763             cave->objects = list_append(cave->objects, gd_object_new_line(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
764           }
765
766           index += 5;
767           break;
768
769         case 2:                // 10: FILLED RECTANGLE
770           x1 = data[index + 1];
771           y1 = data[index + 2] - 2;
772           x2 = x1 + data[index + 3] - 1;    // width
773           y2 = y1 + data[index + 4] - 1;    // height
774
775           if (x1 >= cave->w ||
776               y1 >= cave->h ||
777               x2 >= cave->w ||
778               y2 >= cave->h)
779             Warn("invalid filled rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
780
781           cave->objects = list_append(cave->objects, gd_object_new_filled_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem, import_func(data[index + 5], index + 5)));
782
783           index += 6;
784           break;
785
786         case 3:                // 11: OPEN RECTANGLE (OUTLINE)
787           x1 = data[index + 1];
788           y1 = data[index + 2] - 2;
789           x2 = x1 + data[index + 3] - 1;
790           y2 = y1 + data[index + 4] - 1;
791
792           if (x1 >= cave->w ||
793               y1 >= cave->h ||
794               x2 >= cave->w ||
795               y2 >= cave->h)
796             Warn("invalid rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
797
798           cave->objects = list_append(cave->objects, gd_object_new_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
799
800           index += 5;
801           break;
802       }
803     }
804   }
805
806   if (data[index] != 0xFF)
807   {
808     Error("import error, cave not delimited with 0xFF");
809     return -1;
810   }
811
812   return index + 1;
813 }
814
815 // import bd2 cave data into our format. return number of bytes if pointer passed.
816 // this is pretty much the same as above, only the encoding was different.
817 static int cave_copy_from_bd2(GdCave *cave, const byte *data, int remaining_bytes,
818                               GdCavefileFormat format)
819 {
820   int index;
821   int i;
822   int x, y, rx, ry;
823   int x1, y1, x2, y2, dx, dy;
824   GdElement elem;
825
826   if (remaining_bytes < 0x1A + 5)
827   {
828     Error("truncated BD2 cave data, %d bytes", remaining_bytes);
829     return -1;
830   }
831
832   gd_cave_set_engine_defaults(cave, GD_ENGINE_BD2);
833
834   if (format == GD_FORMAT_BD2_ATARI)
835     cave->scheduling = GD_SCHEDULING_BD2_PLCK_ATARI;
836
837   // set visible size for intermission
838   if (cave->intermission)
839   {
840     cave->x2 = 19;
841     cave->y2 = 11;
842   }
843
844   cave->diamond_value = data[1];
845   cave->extra_diamond_value = data[2];
846
847   for (i = 0; i < 5; i++)
848   {
849     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
850     cave->level_amoeba_time[i] = data[0] == 0 ? 999 : data[0];
851     cave->level_rand[i] = data[13 + i];
852
853     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 needed
854     cave->level_diamonds[i] = data[8 + i] == 0 ? 1000 : data[8 + i];
855     cave->level_time[i] = data[3 + i];
856     cave->level_magic_wall_time[i] = data[0];
857   }
858
859   for (i = 0; i < 4; i++)
860   {
861     cave->random_fill[i] = bd1_import(data[0x16 + i], 0x16 + i);
862     cave->random_fill_probability[i] = data[0x12 + i];
863   }
864
865   /*
866    * Decode the explicit cave data
867    */
868   index = 0x1A;
869
870   while (data[index] != 0xFF && index < remaining_bytes)
871   {
872     int nx, ny;
873     unsigned int addr;
874     int val, n, bytes;
875     int length, direction;
876
877     switch (data[index])
878     {
879       case 0:                // LINE
880         elem = bd1_import(data[index + 1], index + 1);
881         y1 = data[index + 2];
882         x1 = data[index + 3];
883
884         // they are multiplied by two - 0 is up, 2 is upright, 4 is right...
885         direction = data[index + 4] / 2;
886         length = data[index + 5] - 1;
887
888         if (direction > GD_MV_UP_LEFT)
889         {
890           Warn("invalid line direction %d at byte %d", direction, index);
891           direction = GD_MV_STILL;
892         }
893
894         x2 = x1 + length * gd_dx[direction + 1];
895         y2 = y1 + length * gd_dy[direction + 1];
896
897         if (x1 >= cave->w ||
898             y1 >= cave->h ||
899             x2 >= cave->w ||
900             y2 >= cave->h)
901           Warn("invalid line coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
902
903         cave->objects = list_append(cave->objects, gd_object_new_line(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
904
905         index += 6;
906         break;
907
908       case 1:                // OPEN RECTANGLE
909         elem = bd1_import(data[index + 1], index + 1);
910         y1 = data[index + 2];
911         x1 = data[index + 3];
912         y2 = y1 + data[index + 4] - 1;    // height
913         x2 = x1 + data[index + 5] - 1;
914
915         if (x1 >= cave->w ||
916             y1 >= cave->h ||
917             x2 >= cave->w ||
918             y2 >= cave->h)
919           Warn("invalid rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
920
921         cave->objects = list_append(cave->objects, gd_object_new_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
922
923         index += 6;
924         break;
925
926       case 2:                // FILLED RECTANGLE
927         elem = bd1_import(data[index + 1], index + 1);
928         y1 = data[index + 2];
929         x1 = data[index + 3];
930         y2 = y1 + data[index + 4] - 1;
931         x2 = x1 + data[index + 5] - 1;
932
933         if (x1 >= cave->w ||
934             y1 >= cave->h ||
935             x2 >= cave->w ||
936             y2 >= cave->h)
937           Warn("invalid filled rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
938
939         cave->objects = list_append(cave->objects, gd_object_new_filled_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem, bd1_import(data[index+6], index+6)));
940
941         index += 7;
942         break;
943
944       case 3:                // POINT
945         elem = bd1_import(data[index + 1], index + 1);
946         y1 = data[index + 2];
947         x1 = data[index + 3];
948
949         if (x1 >= cave->w ||
950             y1 >= cave->h)
951           Warn("invalid point coordinates %d,%d at byte %d", x1, y1, index);
952
953         cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, x1, y1, elem));
954
955         index += 4;
956         break;
957
958       case 4:                // RASTER
959         elem = bd1_import(data[index + 1], index + 1);
960         y1 = data[index + 2];     // starting pos
961         x1 = data[index + 3];
962         ny = data[index + 4] - 1; // number of elements
963         nx = data[index + 5] - 1;
964         dy = data[index + 6];     // displacement
965         dx = data[index + 7];
966         y2 = y1 + dy * ny;        // calculate rectangle
967         x2 = x1 + dx * nx;
968
969         // guess this has to be here, after x2,y2 calculation, because of some bugs in imported data
970         if (dy < 1)
971           dy = 1;
972         if (dx < 1)
973           dx = 1;
974
975         if (x1 >= cave->w ||
976             y1 >= cave->h ||
977             x2 >= cave->w ||
978             y2 >= cave->h)
979           Warn("invalid raster coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
980
981         cave->objects = list_append(cave->objects, gd_object_new_raster(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, dx, dy, elem));
982
983         index += 8;
984         break;
985
986       case 5:
987         // profi boulder extension: bitmap
988         elem = bd1_import(data[index + 1], index + 1);
989         bytes = data[index + 2];    // number of bytes in bitmap
990
991         if (bytes >= cave->w * cave->h / 8)
992           Warn("invalid bitmap length at byte %d", index - 4);
993
994         addr = 0;
995         addr += data[index + 3];         // msb
996         addr += data[index + 4] << 8;    // lsb
997
998         // this was a pointer to the cave work memory (used during game).
999         addr -= 0x0850;
1000
1001         if (addr >= cave->w * cave->h)
1002           Warn("invalid bitmap start address at byte %d", index - 4);
1003
1004         x1 = addr % 40;
1005         y1 = addr / 40;
1006
1007         for (i = 0; i < bytes; i++)
1008         {
1009           // for ("bytes" number of bytes)
1010           val = data[index + 5 + i];
1011
1012           for (n = 0; n < 8; n++)
1013           {
1014             // for (8 bits in a byte)
1015             if ((val & 1) != 0) // convert to single points...
1016               cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, x1, y1, elem));
1017
1018             val >>= 1;
1019             x1++;   // next cave pos
1020
1021             if (x1 >= cave->w)
1022             {
1023               // maybe next line in map
1024               x1 = 0;
1025               y1++;
1026             }
1027           }
1028         }
1029
1030         index += 5 + bytes;    // 5 description bytes and "bytes" data bytes
1031         break;
1032
1033       case 6:                // JOIN
1034         dy = data[index + 3] / 40;
1035         dx = data[index + 3] % 40;    // same byte!!!
1036         cave->objects = list_append(cave->objects, gd_object_new_join(GD_OBJECT_LEVEL_ALL, dx, dy, bd1_import(data[index+1], index+1), bd1_import(data[index+2], index+2)));
1037
1038         index += 4;
1039         break;
1040
1041       case 7:             // SLIME PERMEABILITY
1042         // interesting this is set here, and not in the cave header
1043         for (i = 0; i < 5; i++)
1044           cave->level_slime_permeability_c64[i] = data[index + 1];
1045
1046         index += 2;
1047         break;
1048
1049       case 9:
1050         // profi boulder extension by player: plck-like cave map. the import
1051         // routine (any2gdash) inserts it here.
1052         if (cave->map != NULL)
1053         {
1054           Error("contains more than one PLCK map");
1055           gd_cave_map_free(cave->map);
1056         }
1057
1058         cave->map = gd_cave_map_new(cave, GdElement);
1059
1060         for (x = 0; x < cave->w; x++)
1061         {
1062           // fill the first and the last row with steel wall.
1063           cave->map[0][x] = O_STEEL;
1064           cave->map[cave->h - 1][x] = O_STEEL;
1065         }
1066
1067         n = 0;    // number of bytes read from map
1068
1069         // the first and the last rows are not stored.
1070         for (y = 1; y < cave->h - 1; y++)
1071         {
1072           for (x = 0; x < cave->w; x += 2)
1073           {
1074             cave->map[y][x]     = plck_import_nybble[data[index + 3 + n] >> 4];    // msb 4 bits
1075             cave->map[y][x + 1] = plck_import_nybble[data[index + 3 + n] % 16];    // lsb 4 bits
1076             n++;
1077           }
1078         }
1079
1080         // the position of inbox is stored. this is to check the cave
1081         ry = data[index + 1] - 2;
1082         rx = data[index + 2];
1083
1084         // at the start of the cave, bd scrolled to the last player placed during the drawing
1085         // (setup) of the cave.
1086         // i think this is why a map also stored the coordinates of the player - we can use
1087         // this to check its integrity
1088         if (rx >= cave->w || ry < 0 ||
1089             ry >= cave->h || cave->map[ry][rx] != O_INBOX)
1090           Warn ("embedded PLCK map may be corrupted, player coordinates %d,%d", rx, rx);
1091
1092         index += 3 + n;
1093         break;
1094
1095       default:
1096         Warn ("unknown bd2 extension no. %02x at byte %d", data[index], index);
1097
1098         index += 1;    // skip that byte
1099     }
1100   }
1101
1102   if (data[index] != 0xFF)
1103   {
1104     Error("import error, cave not delimited with 0xFF");
1105     return -1;
1106   }
1107
1108   // skip delimiter
1109   index++;
1110
1111   // animation byte - told the engine which objects to animate - to make game faster
1112   index++;
1113
1114   // the colors from the memory dump are appended here by any2gdash
1115   cave->colorb = GD_GDASH_BLACK;    // border - black
1116   cave->color0 = GD_GDASH_BLACK;    // background - black
1117   cave->color1 = GD_GDASH_RED;
1118   cave->color2 = GD_GDASH_PURPLE;
1119   cave->color3 = GD_GDASH_YELLOW;
1120   cave->color4 = cave->color3;      // in bd1, amoeba was color3
1121   cave->color5 = cave->color3;      // no slime, but let it be color 3
1122
1123   return index;
1124 }
1125
1126 // import plck cave data into our format.
1127 // length is always 512 bytes, and contains if it is an intermission cave.
1128 static int cave_copy_from_plck(GdCave *cave, const byte *data,
1129                                int remaining_bytes, GdCavefileFormat format)
1130 {
1131   // i don't really think that all this table is needed, but included to be complete.
1132   // this is for the dirt and expanding wall looks like effect.
1133   // it also contains the individual frames
1134   static GdElement plck_graphic_table[] =
1135   {
1136     /* 3000 */ O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN, O_UNKNOWN,
1137     /* 3100 */ O_BUTTER_1, O_MAGIC_WALL, O_PRE_DIA_1, O_PRE_DIA_2, O_PRE_DIA_3, O_PRE_DIA_4, O_PRE_DIA_5, O_OUTBOX_CLOSED,
1138     /* 3200 */ O_AMOEBA, O_VOODOO, O_STONE, O_DIRT, O_DIAMOND, O_STEEL, O_PLAYER, O_BRICK,
1139     /* 3300 */ O_SPACE, O_OUTBOX_OPEN, O_FIREFLY_1, O_EXPLODE_1, O_EXPLODE_2, O_EXPLODE_3, O_MAGIC_WALL, O_MAGIC_WALL,
1140     /* 3400 */ O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK, O_PLAYER_TAP_BLINK,
1141     /* 3500 */ O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT, O_PLAYER_LEFT,
1142     /* 3600 */ O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT, O_PLAYER_RIGHT,
1143     /* 3700 */ O_BUTTER_1, O_BUTTER_1, O_BUTTER_1, O_BUTTER_1, O_BUTTER_1, O_BUTTER_1, O_BUTTER_1, O_BUTTER_1,
1144     /* 3800 */ O_AMOEBA, O_AMOEBA, O_AMOEBA, O_AMOEBA, O_AMOEBA, O_AMOEBA, O_AMOEBA, O_AMOEBA,
1145   };
1146
1147   int i;
1148   int x, y;
1149
1150   if (remaining_bytes < 512)
1151   {
1152     Error("truncated plck cave data!");
1153     return -1;
1154   }
1155
1156   gd_cave_set_engine_defaults(cave, GD_ENGINE_PLCK);
1157
1158   if (format == GD_FORMAT_PLC_ATARI)
1159     cave->scheduling = GD_SCHEDULING_BD2_PLCK_ATARI;
1160
1161   cave->intermission = data[0x1da] != 0;
1162
1163   if (cave->intermission)
1164   {
1165     // set visible size for intermission
1166     cave->x2 = 19;
1167     cave->y2 = 11;
1168   }
1169
1170   // cave selection table, was not part of cave data, rather given in game packers.
1171   // if a new enough version of any2gdash is used, it will put information after the cave.
1172   // detect this here and act accordingly
1173   if ((data[0x1f0] == data[0x1f1] - 1) &&
1174       (data[0x1f0] == 0x19 ||
1175        data[0x1f0] == 0x0e))
1176   {
1177     int j;
1178
1179     // found selection table
1180     cave->selectable = data[0x1f0] == 0x19;
1181     gd_strcpy(cave->name, "              ");
1182
1183     for (j = 0; j < 12; j++)
1184       cave->name[j] = data[0x1f2 + j];
1185
1186     chompString(cave->name);    // remove spaces
1187   }
1188   else
1189   {
1190     // no selection info found, let intermissions be unselectable
1191     cave->selectable = !cave->intermission;
1192   }
1193
1194   cave->diamond_value = data[0x1be];
1195   cave->extra_diamond_value = data[0x1c0];
1196
1197   for (i = 0; i < 5; i++)
1198   {
1199     // plck doesnot really have levels, so just duplicate data five times
1200     cave->level_amoeba_time[i] = data[0x1c4];
1201
1202     // immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
1203     if (cave->level_amoeba_time[i] == 0)
1204       cave->level_amoeba_time[i] = 999;
1205
1206     cave->level_time[i] = data[0x1ba];
1207     cave->level_diamonds[i] = data[0x1bc];
1208
1209     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 needed
1210     if (cave->level_diamonds[i] == 0)
1211       cave->level_diamonds[i] = 1000;
1212
1213     cave->level_ckdelay[i] = data[0x1b8];
1214     cave->level_magic_wall_time[i] = data[0x1c6];
1215     cave->level_slime_permeability_c64[i] = slime_shift_msb(data[0x1c2]);
1216   }
1217
1218   cave->colorb = GD_GDASH_BLACK;    // border - black
1219   cave->color0 = GD_GDASH_BLACK;    // background - black
1220   cave->color1 = GD_GDASH_RED;
1221   cave->color2 = GD_GDASH_PURPLE;
1222   cave->color3 = GD_GDASH_YELLOW;
1223   cave->color4 = cave->color3;      // in bd1, amoeba was color3
1224   cave->color5 = cave->color3;      // no slime, but let it be color 3
1225
1226   // ... the cave is stored like a map.
1227   cave->map = gd_cave_map_new(cave, GdElement);
1228
1229   // cave map looked like this.
1230   // two rows of steel wall ($44's), then cave description, 20 bytes (40 nybbles) for each line.
1231   // the bottom and top lines were not stored... originally.
1232   // some games write to the top line; so we import that, too.
1233   // also dlp 155 allowed writing to the bottom line; the first 20 $44-s now store the bottom line.
1234   // so the cave is essentially shifted one row down in the file:
1235   // cave->map[y][x] = data[... y+1 mod height ][x]
1236   for (y = 0; y < cave->h; y++)
1237   {
1238     for (x = 0; x < cave->w; x += 2)
1239     {
1240       // msb 4 bits: we do not check index ranges, as >>4 and %16 will result in 0..15
1241       cave->map[y][x]     = plck_import_nybble[data[((y + 1) % cave->h) * 20 + x / 2] >> 4];
1242
1243       // lsb 4 bits
1244       cave->map[y][x + 1] = plck_import_nybble[data[((y + 1) % cave->h) * 20 + x / 2] % 16];
1245     }
1246   }
1247
1248   // FOR NOW, WE DO NOT IMPORT THE BOTTOM BORDER
1249   for (x = 0; x < cave->w; x++)
1250     cave->map[cave->h - 1][x] = O_STEEL;
1251
1252   // check for diego-effects
1253   // c64 magic values (byte sequences)  0x20 0x90 0x46, also 0xa9 0x1c 0x85
1254   if ((data[0x1e5] == 0x20 && data[0x1e6] == 0x90 && data[0x1e7] == 0x46) ||
1255       (data[0x1e5] == 0xa9 && data[0x1e6] == 0x1c && data[0x1e7] == 0x85))
1256   {
1257     // diego effects enabled.
1258     cave->stone_bouncing_effect = bd1_import(data[0x1ea], 0x1ea);
1259     cave->diamond_falling_effect = bd1_import(data[0x1eb], 0x1eb);
1260
1261     // explosions: 0x1e was explosion 5, if this is set to default, we also do not read it,
1262     // as in our engine this would cause an O_EXPLODE_5 to stay there.
1263     if (data[0x1ec] != 0x1e)
1264       cave->explosion_effect = bd1_import(data[0x1ec], 0x1ec);
1265
1266     /*
1267       pointer to element graphic.
1268       two bytes/column (one element), that is data[xxx] % 16 / 2.
1269       also there are 16bytes/row.
1270       that is, 0x44 = stone, upper left character. 0x45 = upper right,
1271       0x54 = lower right, 0x55 = lower right.
1272       so high nybble must be shifted right twice -> data[xxx]/16*4.
1273     */
1274     cave->dirt_looks_like           = plck_graphic_table[(data[0x1ed] / 16) * 4 + (data[0x1ed] % 16) / 2];
1275     cave->expanding_wall_looks_like = plck_graphic_table[(data[0x1ee] / 16) * 4 + (data[0x1ee] % 16) / 2];
1276
1277     for (i = 0; i < 5; i++)
1278       cave->level_amoeba_threshold[i] = data[0x1ef];
1279   }
1280
1281   return 512;
1282 }
1283
1284 // no one's delight boulder dash essentially: rle compressed plck maps.
1285 static int cave_copy_from_dlb(GdCave *cave, const byte *data, int remaining_bytes)
1286 {
1287   byte decomp[512];
1288   enum
1289   {
1290     START,        // initial state
1291     SEPARATOR,    // got a separator
1292     RLE,          // after a separator, got the byte to duplicate
1293     NORMAL        // normal, copy bytes till separator
1294   } state;
1295   int pos, cavepos, i, x, y;
1296   byte byte, separator;
1297
1298   gd_cave_set_engine_defaults(cave, GD_ENGINE_PLCK); // essentially the plck engine
1299
1300   for (i = 0; i < 5; i++)
1301   {
1302     // does not really have levels, so just duplicate data five times
1303     cave->level_time[i] = data[1];
1304     cave->level_diamonds[i] = data[2];
1305
1306     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 needed
1307     if (cave->level_diamonds[i] == 0)
1308       cave->level_diamonds[i] = 1000;
1309
1310     cave->level_ckdelay[i] = data[0];
1311     cave->level_amoeba_time[i] = data[6];
1312
1313     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
1314     if (cave->level_amoeba_time[i] == 0)
1315       cave->level_amoeba_time[i] = 999;
1316
1317     cave->level_magic_wall_time[i] = data[7];
1318     cave->level_slime_permeability_c64[i] = slime_shift_msb(data[5]);
1319   }
1320
1321   cave->diamond_value = data[3];
1322   cave->extra_diamond_value = data[4];
1323
1324   // then 5 color bytes follow
1325   cave->colorb = gd_c64_color(data[8]  & 0xf);    // border
1326   cave->color0 = gd_c64_color(data[9]  & 0xf);
1327   cave->color1 = gd_c64_color(data[10] & 0xf);
1328   cave->color2 = gd_c64_color(data[11] & 0xf);
1329   cave->color3 = gd_c64_color(data[12] & 0x7);    // lower 3 bits only!
1330   cave->color4 = cave->color3;                    // in plck, amoeba was color3
1331   cave->color5 = cave->color3;                    // same for slime
1332
1333   // cave map
1334   pos = 13;                     // those 13 bytes were the cave values above
1335   cavepos = 0;
1336   byte = 0;                     // just to get rid of compiler warning
1337   separator = 0;                // just to get rid of compiler warning
1338
1339   // employ a state machine.
1340   state = START;
1341
1342   while (cavepos < 400 && pos < remaining_bytes)
1343   {
1344     switch (state)
1345     {
1346       case START:
1347         // first byte is a separator. remember it
1348         separator = data[pos];
1349
1350         // after the first separator, no rle data, just copy.
1351         state = NORMAL;
1352         break;
1353
1354       case SEPARATOR:
1355         // we had a separator. remember this byte, as this will be duplicated (or more)
1356         byte = data[pos];
1357         state = RLE;
1358         break;
1359
1360       case RLE:
1361         // we had the first byte, duplicate this n times.
1362         if (data[pos] == 0xff)
1363         {
1364           // if it is a 0xff, we will have another byte, which is also a length specifier.
1365           // and for this one, duplicate only 254 times
1366           if (cavepos + 254 > 400)
1367           {
1368             Error("DLB import error: RLE data overflows buffer");
1369             return -1;
1370           }
1371
1372           for (i = 0; i < 254; i++)
1373             decomp[cavepos++] = byte;
1374         }
1375         else
1376         {
1377           // if not 0xff, duplicate n times and back to copy mode
1378           if (cavepos + data[pos] > 400)
1379           {
1380             Error("DLB import error: RLE data overflows buffer");
1381             return -1;
1382           }
1383
1384           for (i = 0; i < data[pos]; i++)
1385             decomp[cavepos++] = byte;
1386
1387           state = NORMAL;
1388         }
1389         break;
1390
1391       case NORMAL:
1392         // bytes duplicated; now only copy the remaining, till the next separator.
1393         if (data[pos] == separator)
1394           state = SEPARATOR;
1395         else
1396           decomp[cavepos++] = data[pos];    // copy this byte and state is still NORMAL
1397         break;
1398     }
1399
1400     pos++;
1401   }
1402
1403   if (cavepos != 400)
1404   {
1405     Error("DLB import error: RLE processing, cave length %d, should be 400", cavepos);
1406     return -1;
1407   }
1408
1409   // process uncompressed map
1410   cave->map = gd_cave_map_new(cave, GdElement);
1411
1412   for (x = 0; x < cave->w; x++)
1413   {
1414     // fill the first and the last row with steel wall.
1415     cave->map[0][x] = O_STEEL;
1416     cave->map[cave->h - 1][x] = O_STEEL;
1417   }
1418
1419   for (y = 1; y < cave->h - 1; y++)
1420   {
1421     for (x = 0; x < cave->w; x += 2)
1422     {
1423       // msb 4 bits
1424       cave->map[y][x]     = plck_import_nybble[decomp[((y - 1) * cave->w + x) / 2] >> 4];
1425       // lsb 4 bits
1426       cave->map[y][x + 1] = plck_import_nybble[decomp[((y - 1) * cave->w + x) / 2] % 16];
1427     }
1428   }
1429
1430   // return number of bytes read from buffer
1431   return pos;
1432 }
1433
1434 // import plck cave data into our format.
1435 static int cave_copy_from_1stb(GdCave *cave, const byte *data, int remaining_bytes)
1436 {
1437   int i;
1438   int x, y;
1439
1440   if (remaining_bytes < 1024)
1441   {
1442     Error("truncated 1stb cave data!");
1443
1444     return -1;
1445   }
1446
1447   gd_cave_set_engine_defaults(cave, GD_ENGINE_1STB);
1448
1449   // copy name
1450   gd_strcpy(cave->name, "              ");
1451
1452   for (i = 0; i < 14; i++)
1453   {
1454     int c = data[0x3a0 + i];
1455
1456     // import cave name; a conversion table is used for each character
1457     if (c < 0x40)
1458       c = gd_bd_internal_chars[c];
1459     else if (c == 0x74)
1460       c = ' ';
1461     else if (c == 0x76)
1462       c = '?';
1463     else
1464       c = ' ';    // don't know this, so change to space
1465
1466     if (i > 0)
1467       c = tolower(c);
1468
1469     cave->name[i] = c;
1470   }
1471
1472   chompString(cave->name);
1473
1474   cave->intermission = data[0x389] != 0;
1475
1476   // if it is intermission but not scrollable
1477   if (cave->intermission && !data[0x38c])
1478   {
1479     cave->x2 = 19;
1480     cave->y2 = 11;
1481   }
1482
1483   cave->diamond_value = 100 * data[0x379] + 10 * data[0x379 + 1] + data[0x379 + 2];
1484   cave->extra_diamond_value = 100 * data[0x376] + 10 * data[0x376 + 1] + data[0x376 + 2];
1485
1486   for (i = 0; i < 5; i++)
1487   {
1488     // plck doesnot really have levels, so just duplicate data five times
1489     cave->level_time[i] = 100 * data[0x370] + 10 * data[0x370+1] + data[0x370 + 2];
1490
1491     // same as gate opening after 0 diamonds
1492     if (cave->level_time[i] == 0)
1493       cave->level_time[i] = 1000;
1494
1495     cave->level_diamonds[i] = 100 * data[0x373] + 10 * data[0x373 + 1] + data[0x373 + 2];
1496
1497     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 (!) needed
1498     if (cave->level_diamonds[i] == 0)
1499       cave->level_diamonds[i] = 1000;
1500
1501     cave->level_ckdelay[i] = data[0x38a];
1502     cave->level_amoeba_time[i] = 256 * (int)data[0x37c] + data[0x37d];
1503
1504     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
1505     if (cave->level_amoeba_time[i] == 0)
1506       cave->level_amoeba_time[i] = 999;
1507
1508     cave->level_magic_wall_time[i] = 256 * (int)data[0x37e] + data[0x37f];
1509     cave->level_slime_permeability_c64[i] = data[0x38b];
1510     cave->level_bonus_time[i] = data[0x392];
1511     cave->level_penalty_time[i] = data[0x393];
1512     cave->level_amoeba_threshold[i] = 256 * (int)data[0x390] + data[0x390 + 1];
1513   }
1514
1515   // also has no random data...
1516
1517   cave->colorb = gd_c64_color(data[0x384] & 0xf);    // border
1518   cave->color0 = gd_c64_color(data[0x385] & 0xf);
1519   cave->color1 = gd_c64_color(data[0x386] & 0xf);
1520   cave->color2 = gd_c64_color(data[0x387] & 0xf);
1521   cave->color3 = gd_c64_color(data[0x388] & 0x7);     // lower 3 bits only!
1522   cave->color4 = cave->color1;
1523   cave->color5 = cave->color1;
1524
1525   cave->amoeba_growth_prob = (4.0 * 1E6 / (data[0x382] + 1)) + 0.5;   // probabilities store *1M
1526   if (cave->amoeba_growth_prob > 1000000)
1527     cave->amoeba_growth_prob = 1000000;
1528
1529   cave->amoeba_fast_growth_prob = (4.0 * 1E6 / (data[0x383] + 1)) + 0.5;
1530   if (cave->amoeba_fast_growth_prob > 1000000)
1531     cave->amoeba_fast_growth_prob = 1000000;
1532
1533   if (data[0x380] != 0)
1534     cave->creatures_direction_auto_change_time = data[0x381];
1535   else
1536     cave->diagonal_movements = data[0x381] != 0;
1537
1538   // ... the cave is stored like a map.
1539   cave->map = gd_cave_map_new(cave, GdElement);
1540   for (y = 0; y < cave->h; y++)
1541     for (x = 0; x < cave->w; x++)
1542       cave->map[y][x] = firstboulder_import(data[y * 40 + x], y * 40 + x);
1543
1544   cave->magic_wall_sound = data[0x38d] == 0xf1;
1545
1546   // 2d was a normal switch, 2e a changed one.
1547   cave->creatures_backwards = data[0x38f] == 0x2d;
1548
1549   // 2e horizontal, 2f vertical.
1550   cave->expanding_wall_changed = data[0x38e] == 0x2f;
1551
1552   cave->biter_delay_frame       = data[0x394];
1553   cave->magic_wall_stops_amoeba = data[0x395] == 0;    // negated!!
1554
1555   cave->bomb_explosion_effect   = firstboulder_import(data[0x396], 0x396);
1556   cave->explosion_effect        = firstboulder_import(data[0x397], 0x397);
1557   cave->stone_bouncing_effect   = firstboulder_import(data[0x398], 0x398);
1558   cave->diamond_birth_effect    = firstboulder_import(data[0x399], 0x399);
1559   cave->magic_diamond_to        = firstboulder_import(data[0x39a], 0x39a);
1560
1561   cave->bladder_converts_by     = firstboulder_import(data[0x39b], 0x39b);
1562   cave->diamond_falling_effect  = firstboulder_import(data[0x39c], 0x39c);
1563   cave->biter_eat               = firstboulder_import(data[0x39d], 0x39d);
1564   cave->slime_eats_1            = firstboulder_import(data[0x39e], 0x39e);
1565   cave->slime_converts_1        = firstboulder_import(data[0x39e] + 3, 0x39e);
1566   cave->slime_eats_2            = firstboulder_import(data[0x39f], 0x39f);
1567   cave->slime_converts_2        = firstboulder_import(data[0x39f] + 3, 0x39f);
1568   cave->magic_diamond_to        = firstboulder_import(data[0x39a], 0x39a);
1569
1570   // length is always 1024 bytes
1571   return 1024;
1572 }
1573
1574 // crazy dream 7
1575 static int cave_copy_from_crdr_7(GdCave *cave, const byte *data, int remaining_bytes)
1576 {
1577   int i, index;
1578   byte checksum;
1579
1580   // if we have name, convert
1581   gd_strcpy(cave->name, "              ");
1582
1583   for (i = 0; i < 14; i++)
1584   {
1585     int c = data[i];
1586
1587     // import cave name; a conversion table is used for each character
1588     if (c < 0x40)
1589       c = gd_bd_internal_chars[c];
1590     else if (c == 0x74)
1591       c = ' ';
1592     else if (c == 0x76)
1593       c = '?';
1594     else
1595       c = ' ';
1596     if (i > 0)
1597       c = tolower(c);
1598
1599     cave->name[i] = c;
1600   }
1601
1602   chompString(cave->name);    // remove trailing and leading spaces
1603
1604   cave->selectable = data[14] != 0;
1605
1606   // jump 15 bytes, 14 was the name and 15 selectability
1607   data += 15;
1608
1609   if (memcmp((char *)data + 0x30, "V4\0020", 4) != 0)
1610     Warn("unknown crdr version %c%c%c%c", data[0x30], data[0x31], data[0x32], data[0x33]);
1611
1612   gd_cave_set_engine_defaults(cave, GD_ENGINE_CRDR7);
1613
1614   for (i = 0; i < 5; i++)
1615   {
1616     cave->level_time[i] = (int)data[0x0] * 100 + data[0x1] * 10 + data[0x2];
1617
1618     // same as gate opening after 0 diamonds
1619     if (cave->level_time[i] == 0)
1620       cave->level_time[i] = 1000;
1621
1622     cave->level_diamonds[i] = (int)data[0x3] * 100 + data[0x4] * 10 + data[0x5];
1623
1624     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 (!) needed
1625     if (cave->level_diamonds[i] == 0)
1626       cave->level_diamonds[i] = 1000;
1627
1628     cave->level_ckdelay[i] = data[0x1A];
1629     cave->level_rand[i] = data[0x40];
1630     cave->level_amoeba_time[i] = (int)data[0xC] * 256 + data[0xD];
1631
1632     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
1633     if (cave->level_amoeba_time[i] == 0)
1634       cave->level_amoeba_time[i] = 999;
1635
1636     cave->level_magic_wall_time[i] = (int)data[0xE] * 256 + data[0xF];
1637     cave->level_slime_permeability_c64[i] = data[0x1B];
1638     cave->level_bonus_time[i] = data[0x22];
1639     cave->level_penalty_time[i] = data[0x23];
1640     cave->level_bonus_time[i] = data[0x22];
1641     cave->level_penalty_time[i] = data[0x23];
1642     cave->level_amoeba_threshold[i] = 256 * (int)data[0x20] + data[0x21];
1643   }
1644
1645   cave->extra_diamond_value = (int)data[0x6] * 100 + data[0x7] * 10 + data[0x8];
1646   cave->diamond_value       = (int)data[0x9] * 100 + data[0xA] * 10 + data[0xB];
1647
1648   if (data[0x10])
1649     cave->creatures_direction_auto_change_time = data[0x11];
1650
1651   cave->colorb = gd_c64_color(data[0x14] & 0xf);    // border
1652   cave->color0 = gd_c64_color(data[0x15] & 0xf);
1653   cave->color1 = gd_c64_color(data[0x16] & 0xf);
1654   cave->color2 = gd_c64_color(data[0x17] & 0xf);
1655   cave->color3 = gd_c64_color(data[0x18] & 0x7);    // lower 3 bits only!
1656   cave->color4 = cave->color3;
1657   cave->color5 = cave->color1;
1658
1659   cave->intermission = data[0x19] != 0;
1660
1661   // if it is intermission but not scrollable
1662   if (cave->intermission && !data[0x1c])
1663   {
1664     cave->x2 = 19;
1665     cave->y2 = 11;
1666   }
1667
1668   /*
1669     AMOEBA in crazy dash 8:
1670     jsr $2500      ; generate true random
1671     and $94        ; binary and the current "probability"
1672     cmp #$04       ; compare to 4
1673     bcs out        ; jump out (do not expand) if carry set, ie. result was less than 4.
1674
1675     prob values can be like num = 3, 7, 15, 31, 63, ... n lsb bits count.
1676     0..3>=4?  0..7>=4?  0..15>=4? and similar.
1677     this way, probability of growing is 4/(num+1)
1678   */
1679
1680   cave->amoeba_growth_prob = (4.0 * 1E6 / (data[0x12] + 1)) + 0.5;   // probabilities store * 1M
1681   if (cave->amoeba_growth_prob > 1000000)
1682     cave->amoeba_growth_prob = 1000000;
1683
1684   cave->amoeba_fast_growth_prob = (4.0 * 1E6 / (data[0x13] + 1)) + 0.5;
1685   if (cave->amoeba_fast_growth_prob > 1000000)
1686     cave->amoeba_fast_growth_prob = 1000000;
1687
1688   // expanding wall direction change - 2e horizontal, 2f vertical
1689   cave->expanding_wall_changed = data[0x1e] == 0x2f;
1690
1691   // 2c was a normal switch, 2d a changed one.
1692   cave->creatures_backwards     = data[0x1f] == 0x2d;
1693   cave->biter_delay_frame       = data[0x24];
1694   cave->magic_wall_stops_amoeba = data[0x25] == 0;    // negated!!
1695
1696   cave->bomb_explosion_effect   = crazydream_import_table[data[0x26]];
1697   cave->explosion_effect        = crazydream_import_table[data[0x27]];
1698   cave->stone_bouncing_effect   = crazydream_import_table[data[0x28]];
1699   cave->diamond_birth_effect    = crazydream_import_table[data[0x29]];
1700   cave->magic_diamond_to        = crazydream_import_table[data[0x2a]];
1701
1702   cave->bladder_converts_by     = crazydream_import_table[data[0x2b]];
1703   cave->diamond_falling_effect  = crazydream_import_table[data[0x2c]];
1704   cave->biter_eat               = crazydream_import_table[data[0x2d]];
1705   cave->slime_eats_1            = crazydream_import_table[data[0x2e]];
1706   cave->slime_converts_1        = crazydream_import_table[data[0x2e] + 3];
1707   cave->slime_eats_2            = crazydream_import_table[data[0x2f]];
1708   cave->slime_converts_2        = crazydream_import_table[data[0x2f] + 3];
1709
1710   cave->diagonal_movements              = (data[0x34] & 1) != 0;
1711   cave->gravity_change_time             = data[0x35];
1712   cave->pneumatic_hammer_frame          = data[0x36];
1713   cave->hammered_wall_reappear_frame    = data[0x37];
1714   cave->hammered_walls_reappear         = data[0x3f] != 0;
1715
1716   /*
1717     acid in crazy dream 8:
1718     jsr $2500    ; true random
1719     cmp    $03a8    ; compare to ratio
1720     bcs out        ; if it was smaller, forget it for now.
1721
1722     ie. random<=ratio, then acid grows.
1723   */
1724
1725   // 1e6, probabilities are stored as int
1726   cave->acid_spread_ratio = data[0x38] / 255.0 * 1E6 + 0.5;
1727
1728   cave->acid_eats_this = crazydream_import_table[data[0x39]];
1729   switch(data[0x3a] & 3)
1730   {
1731     case 0: cave->gravity = GD_MV_UP; break;
1732     case 1: cave->gravity = GD_MV_DOWN; break;
1733     case 2: cave->gravity = GD_MV_LEFT; break;
1734     case 3: cave->gravity = GD_MV_RIGHT; break;
1735   }
1736
1737   cave->snap_element = ((data[0x3a] & 4) != 0) ? O_EXPLODE_1 : O_SPACE;
1738
1739   // we do not know the values for these, so do not import
1740   //    cave->dirt_looks_like... data[0x3c]
1741   //    cave->expanding_wall_looks_like... data[0x3b]
1742   for (i = 0; i < 4; i++)
1743   {
1744     cave->random_fill[i] = crazydream_import_table[data[0x41 + i]];
1745     cave->random_fill_probability[i] = data[0x45 + i];
1746   }
1747
1748   data += 0x49;
1749   index = 0;
1750
1751   while (data[index] != 0xff)
1752   {
1753     GdElement elem;
1754     int x1, y1, x2, y2, dx, dy;
1755     int nx, ny;
1756     int length, direction;
1757
1758     // for copy&paste; copy&paste are different objects, static = ugly solution :)
1759     static int cx1, cy1, cw, ch;
1760
1761     switch (data[index])
1762     {
1763       case 1:    // point
1764         elem = crazydream_import_table[data[index + 1]];
1765         x1 = data[index + 2];
1766         y1 = data[index + 3];
1767         if (x1 >= cave->w || y1 >= cave->h)
1768           Warn("invalid point coordinates %d,%d at byte %d", x1, y1, index);
1769
1770         cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, x1, y1, elem));
1771
1772         index += 4;
1773         break;
1774
1775       case 2: // rectangle
1776         elem = crazydream_import_table[data[index + 1]];
1777         x1 = data[index + 2];
1778         y1 = data[index + 3];
1779         x2 = x1 + data[index + 4] - 1;
1780         y2 = y1 + data[index + 5] - 1;    // height
1781
1782         if (x1 >= cave->w ||
1783             y1 >= cave->h ||
1784             x2 >= cave->w ||
1785             y2 >= cave->h)
1786           Warn("invalid rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
1787
1788         cave->objects = list_append(cave->objects, gd_object_new_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
1789
1790         index += 6;
1791         break;
1792
1793       case 3: // fillrect
1794         x1 = data[index + 2];
1795         y1 = data[index + 3];
1796         x2 = x1 + data[index + 4] - 1;
1797         y2 = y1 + data[index + 5] - 1;
1798
1799         if (x1 >= cave->w ||
1800             y1 >= cave->h ||
1801             x2 >= cave->w ||
1802             y2 >= cave->h)
1803           Warn("invalid filled rectangle coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
1804
1805         // border and inside of fill is the same element.
1806         cave->objects = list_append(cave->objects, gd_object_new_filled_rectangle(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, crazydream_import_table[data[index + 1]], crazydream_import_table[data[index + 1]]));
1807
1808         index += 6;
1809         break;
1810
1811       case 4: // line
1812         elem = crazydream_import_table[data[index + 1]];
1813         if (elem == O_UNKNOWN)
1814           Warn("unknown element at %d: %x", index + 1, data[index + 1]);
1815
1816         x1 = data[index + 2];
1817         y1 = data[index + 3];
1818         length = data[index + 4];
1819         direction = data[index + 5];
1820         nx = ((signed)direction - 128) % 40;
1821         ny = ((signed)direction - 128) / 40;
1822         x2 = x1 + (length - 1) * nx;
1823         y2 = y1 + (length - 1) * ny;
1824
1825         // if either is bigger than one, we cannot treat this as a line. create points instead
1826         if (ABS(nx) >= 2 || ABS(ny) >= 2)
1827         {
1828           for (i = 0; i < length; i++)
1829           {
1830             cave->objects = list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, x1, y1, elem));
1831             x1 += nx;
1832             y1 += ny;
1833           }
1834         }
1835         else
1836         {
1837           // this is a normal line, and will be appended. only do the checking here
1838           if (x1 >= cave->w ||
1839               y1 >= cave->h ||
1840               x2 >= cave->w ||
1841               y2 >= cave->h)
1842             Warn("invalid line coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index - 5);
1843
1844           cave->objects = list_append(cave->objects, gd_object_new_line(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, elem));
1845         }
1846
1847         index += 6;
1848         break;
1849
1850       case 6: // copy
1851         cx1 = data[index + 1];
1852         cy1 = data[index + 2];
1853         cw = data[index + 3];
1854         ch = data[index + 4];
1855
1856         if (cx1 >= cave->w ||
1857             cy1 >= cave->h ||
1858             cx1 + cw > cave->w ||
1859             cy1 + ch > cave->h)
1860           Warn("invalid copy coordinates %d,%d or size %d,%d at byte %d", cx1, cy1, cw, ch, index);
1861
1862         index += 5;
1863         break;
1864
1865       case 7: // paste
1866         x1 = cx1;
1867         y1 = cy1;
1868
1869         // original stored width and height, we store the coordinates of the source area
1870         x2 = cx1 + cw - 1;
1871         y2 = cy1 + ch - 1;
1872         dx = data[index + 1];    // new pos
1873         dy = data[index + 2];
1874
1875         if (dx >= cave->w ||
1876             dy >= cave->h ||
1877             dx + cw > cave->w ||
1878             dy + ch > cave->h)
1879           Warn("invalid paste coordinates %d,%d at byte %d", dx, dy, index);
1880
1881         cave->objects = list_append(cave->objects, gd_object_new_copy_paste(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, dx, dy, FALSE, FALSE));
1882
1883         index += 3;
1884         break;
1885
1886       case 11: // raster
1887         elem = crazydream_import_table[data[index + 1]];
1888         x1 = data[index + 2];
1889         y1 = data[index + 3];
1890         dx = data[index + 4];
1891         dy = data[index + 5];
1892         nx = data[index + 6] - 1;
1893         ny = data[index + 7] - 1;
1894         x2 = x1 + dx * nx;    // calculate rectangle we use
1895         y2 = y1 + dy * ny;
1896
1897         if (dx < 1)
1898           dx = 1;
1899         if (dy < 1)
1900           dy = 1;
1901
1902         if (x1 >= cave->w ||
1903             y1 >= cave->h ||
1904             x2 >= cave->w ||
1905             y2 >= cave->h)
1906           Warn("invalid raster coordinates %d,%d %d,%d at byte %d", x1, y1, x2, y2, index);
1907
1908         cave->objects = list_append(cave->objects, gd_object_new_raster(GD_OBJECT_LEVEL_ALL, x1, y1, x2, y2, dx, dy, elem));
1909
1910         index += 8;
1911         break;
1912
1913       default:
1914         Warn ("unknown crdr extension no. %02x at byte %d", data[index], index);
1915         index += 1;    // skip that byte
1916         break;
1917     }
1918   }
1919
1920   index++;    // skip $ff
1921
1922   // crazy dream 7 hack
1923   checksum = 0;
1924
1925   for (i = 0; i < 0x3b0; i++)
1926     checksum = checksum^data[i];
1927
1928   if (strEqual(cave->name, "Crazy maze") && checksum == 195)
1929     cave->skeletons_needed_for_pot = 0;
1930
1931   return 15 + 0x49 + index;
1932 }
1933
1934 static void crazy_dream_9_add_specials(GdCave *cave, const byte *buf, const int length)
1935 {
1936   byte checksum;
1937   int i;
1938
1939   // crazy dream 9 hack
1940   checksum = 0;
1941   for (i = 0; i < length; i++)
1942     checksum = checksum^buf[i];
1943
1944   // check cave name and the checksum. both are hardcoded here
1945   if (strEqual(cave->name, "Rockfall") && checksum == 134)
1946   {
1947     GdElement rand[4] = { O_DIAMOND, O_STONE, O_ACID, O_DIRT };
1948     int prob[4] = { 37, 32, 2, 0 };
1949     int seeds[5] = { -1, -1, -1, -1, -1 };
1950
1951     cave->objects =
1952       list_append(cave->objects,
1953                   gd_object_new_random_fill(GD_OBJECT_LEVEL_ALL, 0, 0, 39, 21, seeds,
1954                                             O_DIRT, rand, prob, O_BLADDER_SPENDER, FALSE));
1955   }
1956
1957   if (strEqual(cave->name, "Roll dice now!") && checksum == 235)
1958   {
1959     GdElement rand[4] = { O_STONE, O_BUTTER_3, O_DIRT, O_DIRT };
1960     int prob[4] = { 0x18, 0x08, 0, 0 };
1961     int seeds[5] = { -1, -1, -1, -1, -1 };
1962
1963     cave->objects =
1964       list_append(cave->objects,
1965                   gd_object_new_random_fill(GD_OBJECT_LEVEL_ALL, 0, 0, 39, 21, seeds,
1966                                             O_DIRT, rand, prob, O_BLADDER_SPENDER, FALSE));
1967   }
1968
1969   if (strEqual(cave->name, "Random maze") && checksum == 24)
1970   {
1971     int seeds[5] = { -1, -1, -1, -1, -1 };
1972     cave->objects =
1973       list_append(cave->objects,
1974                   gd_object_new_maze(GD_OBJECT_LEVEL_ALL, 1, 4, 35, 20, 1, 1,
1975                                      O_NONE, O_DIRT, 50, seeds));
1976   }
1977
1978   if (strEqual(cave->name, "Metamorphosis") && checksum == 53)
1979   {
1980     int seeds[5] = { -1, -1, -1, -1, -1 };
1981     GdElement rand[4] = { O_STONE, O_DIRT, O_DIRT, O_DIRT };
1982     int prob[4] = { 0x18, 0, 0, 0 };
1983
1984     cave->objects =
1985       list_append(cave->objects,
1986                   gd_object_new_maze(GD_OBJECT_LEVEL_ALL, 4, 1, 38, 19, 1, 3,
1987                                      O_NONE, O_BLADDER_SPENDER, 50, seeds));
1988
1989     cave->objects =
1990       list_append(cave->objects,
1991                   gd_object_new_random_fill(GD_OBJECT_LEVEL_ALL, 4, 1, 38, 19, seeds,
1992                                             O_DIRT, rand, prob, O_BLADDER_SPENDER, FALSE));
1993
1994     cave->creatures_backwards = TRUE;    // for some reason, this level worked like that
1995   }
1996
1997   if (strEqual(cave->name, "All the way") && checksum == 33)
1998   {
1999     int seeds[5] = { -1, -1, -1, -1, -1 };
2000
2001     cave->objects =
2002       list_append(cave->objects,
2003                   gd_object_new_maze_unicursal(GD_OBJECT_LEVEL_ALL, 1, 1, 35, 19, 1, 1,
2004                                                O_BRICK, O_PRE_DIA_1, 50, seeds));
2005
2006     // a point which "breaks" the unicursal maze, making it one very long path
2007     cave->objects =
2008       list_append(cave->objects, gd_object_new_point(GD_OBJECT_LEVEL_ALL, 35, 18, O_BRICK));
2009   }
2010 }
2011
2012 // crazy light contruction kit
2013 static int cave_copy_from_crli(GdCave *cave, const byte *data, int remaining_bytes)
2014 {
2015   byte uncompressed[1024];
2016   int datapos, cavepos, i, x, y;
2017   boolean cavefile;
2018   const char *versions[] = { "V2.2", "V2.6", "V3.0" };
2019   enum
2020   {
2021     none,
2022     V2_2,    // XXX whats the difference between 2.2 and 2.6?
2023     V2_6,
2024     V3_0
2025   } version = none;
2026   GdElement (*import) (byte c, int i) = NULL;    // import function
2027
2028   gd_cave_set_engine_defaults(cave, GD_ENGINE_CRLI);
2029
2030   // detect if this is a cavefile
2031   if (data[0] == 0 &&
2032       data[1] == 0xc4 &&
2033       data[2] == 'D' &&
2034       data[3] == 'L' &&
2035       data[4] == 'P')
2036   {
2037     datapos = 5;    // cavefile, skipping 0x00 0xc4 D L P
2038     cavefile = TRUE;
2039   }
2040   else
2041   {
2042     // converted from snapshot, skip "selectable" and 14byte name
2043     datapos = 15;
2044     cavefile = FALSE;
2045   }
2046
2047   // if we have name, convert
2048   if (!cavefile)
2049   {
2050     gd_strcpy(cave->name, "              ");
2051
2052     for (i = 0; i < 14; i++)
2053     {
2054       int c = data[i + 1];
2055
2056       // import cave name; a conversion table is used for each character
2057       if (c < 0x40)
2058         c = gd_bd_internal_chars[c];
2059       else if (c == 0x74)
2060         c = ' ';
2061       else if (c == 0x76)
2062         c = '?';
2063       else
2064         c = ' ';
2065
2066       if (i > 0)
2067         c = tolower(c);
2068
2069       cave->name[i] = c;
2070     }
2071
2072     chompString(cave->name);    // remove trailing and leading spaces
2073   }
2074
2075   // uncompress rle data
2076   cavepos = 0;
2077
2078   while (cavepos < 0x3b0)
2079   {
2080     // <- loop until the uncompressed reaches its size
2081     if (datapos >= remaining_bytes)
2082     {
2083       Error("truncated crli cave data");
2084       return -1;
2085     }
2086
2087     if (data[datapos] == 0xbf)
2088     {
2089       // magic value 0xbf is the escape byte
2090       if (datapos + 2 >= remaining_bytes)
2091       {
2092         Error("truncated crli cave data");
2093         return -1;
2094       }
2095
2096       if (data[datapos + 2] + datapos >= sizeof(uncompressed))
2097       {
2098         // we would run out of buffer, this must be some error
2099         Error("invalid crli cave data - RLE length value is too big");
2100         return -1;
2101       }
2102
2103       // 0xbf, number, byte to dup
2104       for (i = 0; i < data[datapos + 2]; i++)
2105         uncompressed[cavepos++] = data[datapos + 1];
2106
2107       datapos += 3;
2108     }
2109     else
2110     {
2111       uncompressed[cavepos++] = data[datapos++];
2112     }
2113   }
2114
2115   // check crli version
2116   for (i = 0; i < ARRAY_SIZE(versions); i++)
2117     if (memcmp((char *)uncompressed + 0x3a0, versions[i], 4) == 0)
2118       version = i + 1;
2119
2120   // v3.0 has falling wall and box, and no ghost.
2121   import = version >= V3_0 ? crazylight_import : firstboulder_import;
2122
2123   if (version == none)
2124   {
2125     Warn("unknown crli version %c%c%c%c", uncompressed[0x3a0], uncompressed[0x3a1], uncompressed[0x3a2], uncompressed[0x3a3]);
2126     import = crazylight_import;
2127   }
2128
2129   // process map
2130   cave->map = gd_cave_map_new(cave, GdElement);
2131
2132   for (y = 0; y < cave->h; y++)
2133   {
2134     for (x = 0; x < cave->w; x++)
2135     {
2136       int index = y * cave->w + x;
2137
2138       cave->map[y][x] = import(uncompressed[index], index);
2139     }
2140   }
2141
2142   // crli has no levels
2143   for (i = 0; i < 5; i++)
2144   {
2145     cave->level_time[i] = (int)uncompressed[0x370] * 100 + uncompressed[0x371] * 10 + uncompressed[0x372];
2146
2147     // same as gate opening after 0 diamonds
2148     if (cave->level_time[i] == 0)
2149       cave->level_time[i] = 1000;
2150
2151     cave->level_diamonds[i] = (int)uncompressed[0x373] * 100 + uncompressed[0x374] * 10 + uncompressed[0x375];
2152
2153     // gate opening is checked AFTER adding to diamonds collected, so 0 here is 1000 (!) needed
2154     if (cave->level_diamonds[i] == 0)
2155       cave->level_diamonds[i] = 1000;
2156
2157     cave->level_ckdelay[i] = uncompressed[0x38A];
2158     cave->level_amoeba_time[i] = (int)uncompressed[0x37C] * 256 + uncompressed[0x37D];
2159
2160     // 0 immediately underflowed to 999, so we use 999. example: sendydash 3, cave 02.
2161     if (cave->level_amoeba_time[i] == 0)
2162       cave->level_amoeba_time[i] = 999;
2163
2164     cave->level_magic_wall_time[i] = (int)uncompressed[0x37E] * 256 + uncompressed[0x37F];
2165     cave->level_slime_permeability_c64[i] = uncompressed[0x38B];
2166     cave->level_bonus_time[i] = uncompressed[0x392];
2167     cave->level_penalty_time[i] = uncompressed[0x393];
2168     cave->level_amoeba_threshold[i] = 256 * (int)uncompressed[0x390] + uncompressed[0x390 + 1];
2169   }
2170
2171   cave->extra_diamond_value = (int)uncompressed[0x376] * 100 + uncompressed[0x377] * 10 + uncompressed[0x378];
2172   cave->diamond_value = (int)uncompressed[0x379] * 100 + uncompressed[0x37A] * 10 + uncompressed[0x37B];
2173
2174   if (uncompressed[0x380])
2175     cave->creatures_direction_auto_change_time = uncompressed[0x381];
2176
2177   cave->colorb = gd_c64_color(uncompressed[0x384] & 0xf);    // border
2178   cave->color0 = gd_c64_color(uncompressed[0x385] & 0xf);
2179   cave->color1 = gd_c64_color(uncompressed[0x386] & 0xf);
2180   cave->color2 = gd_c64_color(uncompressed[0x387] & 0xf);
2181   cave->color3 = gd_c64_color(uncompressed[0x388] & 0x7);    // lower 3 bits only!
2182   cave->color4 = cave->color3;
2183   cave->color5 = cave->color1;
2184
2185   cave->intermission = uncompressed[0x389] != 0;
2186
2187   // if it is intermission but not scrollable
2188   if (cave->intermission && !uncompressed[0x38c])
2189   {
2190     cave->x2 = 19;
2191     cave->y2 = 11;
2192   }
2193
2194   /*
2195     AMOEBA in crazy dash 8:
2196     jsr $2500        ; generate true random
2197     and $94            ; binary and the current "probability"
2198     cmp #$04        ; compare to 4
2199     bcs out            ; jump out (do not expand) if carry set, ie. result was less than 4.
2200
2201     prob values can be like num = 3, 7, 15, 31, 63, ... n lsb bits count.
2202     0..3>=4?  0..7>=4?  0..15>=4? and similar.
2203     this way, probability of growing is 4/(num+1)
2204   */
2205
2206   // probabilities store * 1M
2207   cave->amoeba_growth_prob = (1E6 * 4.0 / (uncompressed[0x382] + 1)) + 0.5;
2208
2209   if (cave->amoeba_growth_prob > 1000000)
2210     cave->amoeba_growth_prob = 1000000;
2211
2212   cave->amoeba_fast_growth_prob = (1E6*4.0/(uncompressed[0x383] + 1)) + 0.5;
2213
2214   if (cave->amoeba_fast_growth_prob > 1000000)
2215     cave->amoeba_fast_growth_prob = 1000000;
2216
2217   // 2c was a normal switch, 2d a changed one.
2218   cave->creatures_backwards = uncompressed[0x38f] == 0x2d;
2219   cave->magic_wall_sound = uncompressed[0x38d] == 0xf1;
2220
2221   // 2e horizontal, 2f vertical. we implement this by changing them
2222   if (uncompressed[0x38e] == 0x2f)
2223   {
2224     for (y = 0; y < cave->h; y++)
2225     {
2226       for (x = 0; x < cave->w; x++)
2227       {
2228         if (cave->map[y][x] == O_H_EXPANDING_WALL)
2229           cave->map[y][x] = O_V_EXPANDING_WALL;
2230       }
2231     }
2232   }
2233
2234   cave->biter_delay_frame       = uncompressed[0x394];
2235   cave->magic_wall_stops_amoeba = uncompressed[0x395] == 0;    // negated!!
2236   cave->bomb_explosion_effect   = import(uncompressed[0x396], 0x396);
2237   cave->explosion_effect        = import(uncompressed[0x397], 0x397);
2238   cave->stone_bouncing_effect   = import(uncompressed[0x398], 0x398);
2239   cave->diamond_birth_effect    = import(uncompressed[0x399], 0x399);
2240   cave->magic_diamond_to        = import(uncompressed[0x39a], 0x39a);
2241
2242   cave->bladder_converts_by     = import(uncompressed[0x39b], 0x39b);
2243   cave->diamond_falling_effect  = import(uncompressed[0x39c], 0x39c);
2244   cave->biter_eat               = import(uncompressed[0x39d], 0x39d);
2245   cave->slime_eats_1            = import(uncompressed[0x39e], 0x39e);
2246   cave->slime_converts_1        = import(uncompressed[0x39e] + 3, 0x39e);
2247   cave->slime_eats_2            = import(uncompressed[0x39f], 0x39f);
2248   cave->slime_converts_2        = import(uncompressed[0x39f] + 3, 0x39f);
2249
2250   // v3.0 has some new properties.
2251   if (version >= V3_0)
2252   {
2253     cave->diagonal_movements     = uncompressed[0x3a4] != 0;
2254     cave->amoeba_too_big_effect  = import(uncompressed[0x3a6], 0x3a6);
2255     cave->amoeba_enclosed_effect = import(uncompressed[0x3a7], 0x3a7);
2256
2257     /*
2258       acid in crazy dream 8:
2259       jsr $2500    ; true random
2260       cmp    $03a8    ; compare to ratio
2261       bcs out        ; if it was smaller, forget it for now.
2262
2263       ie. random<=ratio, then acid grows.
2264     */
2265
2266     // * 1e6, probabilities are stored as int
2267     cave->acid_spread_ratio             = uncompressed[0x3a8] / 255.0 * 1E6;
2268     cave->acid_eats_this                = import(uncompressed[0x3a9], 0x3a9);
2269     cave->expanding_wall_looks_like     = import(uncompressed[0x3ab], 0x3ab);
2270     cave->dirt_looks_like               = import(uncompressed[0x3ac], 0x3ac);
2271   }
2272   else
2273   {
2274     // version is <= 3.0, so this is a 1stb cave.
2275     // the only parameters, for which this matters, are these:
2276     if (uncompressed[0x380] != 0)
2277       cave->creatures_direction_auto_change_time = uncompressed[0x381];
2278     else
2279       cave->diagonal_movements = uncompressed[0x381] != 0;
2280   }
2281
2282   if (cavefile)
2283     cave->selectable = !cave->intermission;     // best we can do
2284   else
2285     cave->selectable = !data[0];                // given by converter
2286
2287   return datapos;
2288 }
2289
2290 GdCavefileFormat gd_caveset_imported_get_format(const byte *buf)
2291 {
2292   const char *s_bd1       = "GDashBD1";
2293   const char *s_bd1_atari = "GDashB1A";
2294   const char *s_dc1       = "GDashDC1";
2295   const char *s_bd2       = "GDashBD2";
2296   const char *s_bd2_atari = "GDashB2A";
2297   const char *s_plc       = "GDashPLC";
2298   const char *s_plc_atari = "GDashPCA";
2299   const char *s_dlb       = "GDashDLB";
2300   const char *s_crl       = "GDashCRL";
2301   const char *s_cd7       = "GDashCD7";
2302   const char *s_cd9       = "GDashCD9";
2303   const char *s_1st       = "GDash1ST";
2304
2305   if (memcmp((char *)buf, s_bd1, strlen(s_bd1)) == 0)
2306     return GD_FORMAT_BD1;
2307   if (memcmp((char *)buf, s_bd1_atari, strlen(s_bd1_atari)) == 0)
2308     return GD_FORMAT_BD1_ATARI;
2309   if (memcmp((char *)buf, s_dc1, strlen(s_dc1)) == 0)
2310     return GD_FORMAT_DC1;
2311   if (memcmp((char *)buf, s_bd2, strlen(s_bd2)) == 0)
2312     return GD_FORMAT_BD2;
2313   if (memcmp((char *)buf, s_bd2_atari, strlen(s_bd2_atari)) == 0)
2314     return GD_FORMAT_BD2_ATARI;
2315   if (memcmp((char *)buf, s_plc, strlen(s_plc)) == 0)
2316     return GD_FORMAT_PLC;
2317   if (memcmp((char *)buf, s_plc_atari, strlen(s_plc_atari)) == 0)
2318     return GD_FORMAT_PLC_ATARI;
2319   if (memcmp((char *)buf, s_dlb, strlen(s_dlb)) == 0)
2320     return GD_FORMAT_DLB;
2321   if (memcmp((char *)buf, s_crl, strlen(s_crl)) == 0)
2322     return GD_FORMAT_CRLI;
2323   if (memcmp((char *)buf, s_cd7, strlen(s_cd7)) == 0)
2324     return GD_FORMAT_CRDR_7;
2325   if (memcmp((char *)buf, s_cd9, strlen(s_cd9)) == 0)
2326     return GD_FORMAT_CRDR_9;
2327   if (memcmp((char *)buf, s_1st, strlen(s_1st)) == 0)
2328     return GD_FORMAT_FIRSTB;
2329
2330   return GD_FORMAT_UNKNOWN;
2331 }
2332
2333
2334 // ----------------------------------------------------------------------------
2335 //  Load caveset from memory buffer.
2336 //  Loads the caveset from a memory buffer.
2337 //  returns: List * of caves.
2338 // ----------------------------------------------------------------------------
2339
2340 List *gd_caveset_import_from_buffer (const byte *buf, size_t length)
2341 {
2342   boolean numbering;
2343   int cavenum, intermissionnum, num;
2344   int cavelength, bufp;
2345   List *caveset = NULL, *iter;
2346   unsigned int encodedlength;
2347   GdCavefileFormat format;
2348
2349   if (length != -1 && length < 12)
2350   {
2351     Warn("buffer too short to be a GDash datafile");
2352     return NULL;
2353   }
2354
2355   encodedlength = (unsigned int)(*((unsigned int *)(buf + 8)));
2356   if (length != -1 && encodedlength != length - 12)
2357   {
2358     Warn("file length and data size mismatch in GDash datafile");
2359     return NULL;
2360   }
2361
2362   format = gd_caveset_imported_get_format(buf);
2363   if (format == GD_FORMAT_UNKNOWN)
2364   {
2365     Warn("buffer does not contain a GDash datafile");
2366     return NULL;
2367   }
2368
2369   buf += 12;
2370   length = encodedlength;
2371
2372   bufp = 0;
2373   cavenum = 0;
2374
2375   while (bufp < length)
2376   {
2377     GdCave *newcave;
2378     // default is to append cave to caveset; list_insert appends when pos = -1
2379     int insertpos = -1;
2380
2381     newcave = gd_cave_new();
2382
2383     cavelength = 0;    // to avoid compiler warning
2384
2385     switch (format)
2386     {
2387       case GD_FORMAT_BD1:                // boulder dash 1
2388       case GD_FORMAT_BD1_ATARI:          // boulder dash 1, atari version
2389       case GD_FORMAT_DC1:                // deluxe caves 1
2390       case GD_FORMAT_BD2:                // boulder dash 2
2391       case GD_FORMAT_BD2_ATARI:          // boulder dash 2
2392         // these are not in the data so we guess
2393         newcave->selectable = (cavenum < 16) && (cavenum % 4 == 0);
2394         newcave->intermission = cavenum > 15;
2395
2396         // no name, so we make up one
2397         if (newcave->intermission)
2398           snprintf(newcave->name, sizeof(newcave->name), _("Intermission %d"), cavenum - 15);
2399         else
2400           snprintf(newcave->name, sizeof(newcave->name), _("Cave %c"), 'A' + cavenum);
2401
2402         switch(format)
2403         {
2404           case GD_FORMAT_BD1:
2405           case GD_FORMAT_BD1_ATARI:
2406           case GD_FORMAT_DC1:
2407             cavelength = cave_copy_from_bd1(newcave, buf + bufp, length - bufp, format);
2408             break;
2409           case GD_FORMAT_BD2:
2410           case GD_FORMAT_BD2_ATARI:
2411             cavelength = cave_copy_from_bd2(newcave, buf + bufp, length - bufp, format);
2412             break;
2413
2414           default:
2415             break;
2416         };
2417
2418         // original bd1 had level order ABCDEFGH... and then the last four were the intermissions.
2419         // those should be inserted between D-E, H-I... caves.
2420         if (cavenum > 15)
2421           insertpos = (cavenum - 15) * 5 - 1;
2422         break;
2423
2424       case GD_FORMAT_FIRSTB:
2425         cavelength = cave_copy_from_1stb(newcave, buf + bufp, length - bufp);
2426
2427         // every fifth cave (4+1 intermission) is selectable.
2428         newcave->selectable = cavenum % 5 == 0;
2429         break;
2430
2431       case GD_FORMAT_PLC:                // peter liepa construction kit
2432       case GD_FORMAT_PLC_ATARI:          // peter liepa construction kit, atari version
2433         cavelength = cave_copy_from_plck(newcave, buf + bufp, length - bufp, format);
2434         break;
2435
2436       case GD_FORMAT_DLB:
2437         // no one's delight boulder dash, something like rle compressed plck caves
2438         // but there are 20 of them, as if it was a bd1 or bd2 game.
2439         // also num%5 = 4 is intermission.
2440         // we have to set intermission flag on our own, as the file did not contain
2441         // the info explicitly
2442
2443         newcave->intermission = (cavenum % 5) == 4;
2444         if (newcave->intermission)
2445         {
2446           // also set visible size
2447           newcave->x2 = 19;
2448           newcave->y2 = 11;
2449         }
2450
2451         newcave->selectable = cavenum % 5 == 0;    // original selection scheme
2452         if (newcave->intermission)
2453           snprintf(newcave->name, sizeof(newcave->name), _("Intermission %d"), cavenum / 5 + 1);
2454         else
2455           snprintf(newcave->name, sizeof(newcave->name), _("Cave %c"), 'A'+(cavenum % 5 + cavenum / 5 * 4));
2456
2457         cavelength = cave_copy_from_dlb (newcave, buf + bufp, length - bufp);
2458         break;
2459
2460       case GD_FORMAT_CRLI:
2461         cavelength = cave_copy_from_crli (newcave, buf + bufp, length - bufp);
2462         break;
2463
2464       case GD_FORMAT_CRDR_7:
2465         cavelength = cave_copy_from_crdr_7 (newcave, buf + bufp, length - bufp);
2466         break;
2467
2468       case GD_FORMAT_CRDR_9:
2469         cavelength = cave_copy_from_crli (newcave, buf + bufp, length - bufp);
2470         if (cavelength != -1)
2471           crazy_dream_9_add_specials(newcave, buf, cavelength);
2472         break;
2473
2474       case GD_FORMAT_UNKNOWN:
2475         break;
2476     }
2477
2478     if (cavelength == -1)
2479     {
2480       gd_cave_free(newcave);
2481
2482       Error("Aborting cave import.");
2483       break;
2484     }
2485     else
2486     {
2487       caveset = list_insert(caveset, newcave, insertpos);
2488     }
2489
2490     cavenum++;
2491     bufp += cavelength;
2492
2493     // hack: some dlb files contain junk data after 20 caves.
2494     if (format == GD_FORMAT_DLB && cavenum == 20)
2495     {
2496       if (bufp < length)
2497         Warn("excess data in dlb file, %d bytes", (int)(length-bufp));
2498       break;
2499     }
2500   }
2501
2502   // try to detect if plc caves are in standard layout.
2503   // that is, caveset looks like an original, (4 cave,1 intermission)+
2504   if (format == GD_FORMAT_PLC)
2505     // if no selection table stored by any2gdash
2506     if ((buf[2 + 0x1f0] != buf[2 + 0x1f1] - 1) ||
2507         (buf[2 + 0x1f0] != 0x19 && buf[2 + 0x1f0] != 0x0e))
2508     {
2509       List *iter;
2510       int n;
2511       boolean standard;
2512
2513       standard = (list_length(caveset)%5) == 0;    // cave count % 5 != 0 -> nonstandard
2514
2515       for (n = 0, iter = caveset; iter != NULL; n++, iter = iter->next)
2516       {
2517         GdCave *cave = iter->data;
2518
2519         if ((n % 5 == 4 && !cave->intermission) ||
2520             (n % 5 != 4 && cave->intermission))
2521           standard = FALSE;    // 4 cave, 1 intermission
2522       }
2523
2524       // if test passed, update selectability
2525       if (standard)
2526         for (n = 0, iter = caveset; iter != NULL; n++, iter = iter->next)
2527         {
2528           GdCave *cave = iter->data;
2529
2530           // update "selectable"
2531           cave->selectable = (n % 5) == 0;
2532         }
2533     }
2534
2535   // try to give some names for the caves
2536   cavenum = 1;
2537   intermissionnum = 1;
2538   num = 1;
2539
2540   // use numbering instead of letters, if following formats or too many caves
2541   // (as we would run out of letters)
2542   numbering = format == GD_FORMAT_PLC || format == GD_FORMAT_CRLI || list_length(caveset) > 26;
2543
2544   for (iter = caveset; iter != NULL; iter = iter->next)
2545   {
2546     GdCave *cave = (GdCave *)iter->data;
2547
2548     if (!strEqual(cave->name, ""))    // if it already has a name, skip
2549       continue;
2550
2551     if (cave->intermission)
2552     {
2553       // intermission
2554       if (numbering)
2555         snprintf(cave->name, sizeof(cave->name), _("Intermission %02d"), num);
2556       else
2557         snprintf(cave->name, sizeof(cave->name), _("Intermission %d"), intermissionnum);
2558     } else {
2559       if (numbering)
2560         snprintf(cave->name, sizeof(cave->name), _("Cave %02d"), num);
2561       else
2562         snprintf(cave->name, sizeof(cave->name), _("Cave %c"), 'A' - 1 + cavenum);
2563     }
2564
2565     num++;
2566     if (cave->intermission)
2567       intermissionnum++;
2568     else
2569       cavenum++;
2570   }
2571
2572   // if the user requests, we make all caves selectable. intermissions not.
2573   if (gd_import_as_all_caves_selectable)
2574   {
2575     for (iter = caveset; iter != NULL; iter = iter->next)
2576     {
2577       GdCave *cave = (GdCave *)iter->data;
2578
2579       // make selectable if not an intermission.
2580       // also selectable, if it was selectable originally, for some reason.
2581       cave->selectable = cave->selectable || !cave->intermission;
2582     }
2583   }
2584
2585   return caveset;
2586 }
2587
2588 // to be called at program start.
2589 void gd_c64_import_init_tables(void)
2590 {
2591 }