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