1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2024 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
15 struct GameInfo_BD game_bd;
16 struct LevelInfo_BD native_bd_level;
17 struct EngineSnapshotInfo_BD engine_snapshot_bd;
20 // ============================================================================
21 // initialization functions
22 // ============================================================================
24 void InitGfxBuffers_BD(void)
26 ReCreateBitmap(&gd_screen_bitmap, SXSIZE, SYSIZE);
28 set_cell_size(TILESIZE_VAR);
29 set_play_area(SXSIZE, SYSIZE);
32 void bd_open_all(void)
39 gd_c64_import_init_tables();
48 void bd_close_all(void)
53 // ============================================================================
54 // level file functions
55 // ============================================================================
57 void setLevelInfoToDefaults_BD_Ext(int width, int height)
59 GdCave *cave = native_bd_level.cave;
64 // get empty cave, using default values
67 // set cave size, if defined
68 if (width > 0 && height > 0)
75 cave->x2 = cave->w - 1;
76 cave->y2 = cave->h - 1;
79 gd_flatten_cave(cave, 0);
81 cave->selectable = TRUE;
82 cave->intermission = FALSE;
84 native_bd_level.cave = cave;
85 native_bd_level.replay = NULL;
87 native_bd_level.cave_nr = 0;
88 native_bd_level.level_nr = 0;
90 native_bd_level.loaded_from_caveset = FALSE;
93 void setLevelInfoToDefaults_BD(void)
95 setLevelInfoToDefaults_BD_Ext(0, 0);
98 boolean LoadNativeLevel_BD(char *filename, int level_pos, boolean level_info_only)
100 static char *filename_loaded = NULL;
102 if (filename_loaded == NULL || !strEqual(filename, filename_loaded))
104 if (!gd_caveset_load_from_file(filename))
106 if (!level_info_only)
107 Warn("cannot load BD cave set from file '%s'", filename);
112 setString(&filename_loaded, filename);
115 if (level_pos < 0 || level_pos >= 5 * gd_caveset_count())
117 Warn("invalid level position %d in BD cave set", level_pos);
122 native_bd_level.cave_nr = level_pos % gd_caveset_count();
123 native_bd_level.level_nr = level_pos / gd_caveset_count();
125 if (native_bd_level.cave != NULL)
126 gd_cave_free(native_bd_level.cave);
128 // get selected cave, prepared for playing
129 native_bd_level.cave = gd_get_prepared_cave_from_caveset(native_bd_level.cave_nr,
130 native_bd_level.level_nr);
132 // set better initial cave speed (to set better native replay tape length)
133 set_initial_cave_speed(native_bd_level.cave);
135 native_bd_level.loaded_from_caveset = TRUE;
137 // check if this cave has any replays
138 if (native_bd_level.cave->replays != NULL)
140 List *item = native_bd_level.cave->replays;
142 // try to find replay that was recorded for this difficulty level
143 while (item != NULL &&
144 (item->data == NULL ||
145 ((GdReplay *)item->data)->success == FALSE ||
146 ((GdReplay *)item->data)->level != native_bd_level.level_nr))
149 // matching replay found
151 native_bd_level.replay = (GdReplay *)item->data;
157 boolean SaveNativeLevel_BD(char *filename)
159 GdCave *cave = gd_cave_new_from_cave(native_bd_level.cave);
162 gd_caveset = list_append(gd_caveset, cave);
164 return gd_caveset_save_to_file(filename);
168 // ============================================================================
169 // game engine functions
170 // ============================================================================
172 int map_action_RND_to_BD(int action)
174 GdDirection player_move = gd_direction_from_keypress(action & JOY_UP,
178 boolean player_fire = (action & (JOY_BUTTON_1 | JOY_BUTTON_2));
180 return (player_move | (player_fire ? GD_REPLAY_FIRE_MASK : 0));
183 int map_action_BD_to_RND(int action)
185 GdDirection player_move = action & GD_REPLAY_MOVE_MASK;
186 boolean player_fire = action & GD_REPLAY_FIRE_MASK;
188 int action_move = (player_move == GD_MV_UP ? JOY_UP :
189 player_move == GD_MV_UP_RIGHT ? JOY_UP | JOY_RIGHT :
190 player_move == GD_MV_RIGHT ? JOY_RIGHT :
191 player_move == GD_MV_DOWN_RIGHT ? JOY_DOWN | JOY_RIGHT :
192 player_move == GD_MV_DOWN ? JOY_DOWN :
193 player_move == GD_MV_DOWN_LEFT ? JOY_DOWN | JOY_LEFT :
194 player_move == GD_MV_LEFT ? JOY_LEFT :
195 player_move == GD_MV_UP_LEFT ? JOY_UP | JOY_LEFT : JOY_NO_ACTION);
196 int action_fire = (player_fire ? JOY_BUTTON_1 : JOY_NO_ACTION);
198 return (action_move | action_fire);
201 boolean checkGameRunning_BD(void)
203 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CAVE_RUNNING);
206 boolean checkGamePlaying_BD(void)
208 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CAVE_RUNNING &&
209 game_bd.game->cave != NULL && game_bd.game->cave->hatched);
212 boolean checkBonusTime_BD(void)
214 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CHECK_BONUS_TIME);
217 int getFramesPerSecond_BD(void)
219 if (game_bd.game != NULL && game_bd.game->cave != NULL && game_bd.game->cave->pal_timing)
220 return FRAMES_PER_SECOND_NTSC;
222 return FRAMES_PER_SECOND_PAL;
225 int getTimeLeft_BD(void)
227 if (game_bd.game != NULL && game_bd.game->cave != NULL)
228 return gd_cave_time_show(game_bd.game->cave, game_bd.game->cave->time);
233 static void UpdateGameDoorValues_BD(void)
235 GdCave *cave = game_bd.game->cave;
236 int time_secs = gd_cave_time_show(cave, cave->time);
237 int gems_still_needed = MAX(0, (cave->diamonds_needed - cave->diamonds_collected));
239 game_bd.time_played = time_secs;
240 game_bd.gems_still_needed = gems_still_needed;
241 game_bd.score = game_bd.game->player_score;
243 if (game.LevelSolved)
245 // update time and score in panel while counting bonus time
246 game.LevelSolved_CountingTime = game_bd.time_played;
247 game.LevelSolved_CountingScore = game_bd.score;
251 static void PrepareGameTileBitmap_BD(void)
253 struct GraphicInfo_BD *g_template = &graphic_info_bd_color_template;
254 struct GraphicInfo_BD *g_default = &graphic_info_bd_object[O_STONE][0];
256 gd_prepare_tile_bitmap(native_bd_level.cave, g_template->bitmap, 1);
257 gd_set_tile_bitmap_reference(g_default->bitmap);
260 void PreparePreviewTileBitmap_BD(Bitmap *bitmap, int scale_down_factor)
262 gd_prepare_tile_bitmap(native_bd_level.cave, bitmap, scale_down_factor);
265 void SetPreviewTileBitmapReference_BD(Bitmap *bitmap)
267 gd_set_tile_bitmap_reference(bitmap);
270 Bitmap *GetPreviewTileBitmap_BD(Bitmap *bitmap)
272 return gd_get_tile_bitmap(bitmap);
275 unsigned int InitEngineRandom_BD(int seed)
277 if (seed == NEW_RANDOMIZE)
279 // get randomly selected seed to render the cave
280 seed = gd_random_int_range(0, GD_CAVE_SEED_MAX);
283 game_bd.random_seed = seed;
285 return (unsigned int)seed;
288 void InitGameEngine_BD(void)
290 game_bd.level_solved = FALSE;
291 game_bd.game_over = FALSE;
292 game_bd.cover_screen = FALSE;
294 game_bd.time_played = 0;
295 game_bd.gems_still_needed = 0;
298 gd_caveset_last_selected = native_bd_level.cave_nr;
299 gd_caveset_last_selected_level = native_bd_level.level_nr;
301 if (game_bd.game != NULL)
302 gd_game_free(game_bd.game);
304 game_bd.game = gd_game_new(native_bd_level.cave_nr, native_bd_level.level_nr);
306 game_bd.game->itercycle = 0;
307 game_bd.game->itermax = 8; // default; dynamically changed at runtime
308 game_bd.game->itermax_last = game_bd.game->itermax;
310 // prepare tile bitmap with level-specific colors, if available
311 PrepareGameTileBitmap_BD();
313 // default: start with completely covered playfield
314 int next_state = GAME_INT_START_UNCOVER + 1;
316 // when skipping uncovering, start with uncovered playfield
317 if (setup.bd_skip_uncovering)
318 next_state = GAME_INT_LOAD_CAVE + 1;
320 // fast-forward game engine until cave loaded (covered or uncovered)
321 while (game_bd.game->state_counter < next_state)
322 play_game_func(game_bd.game, 0);
324 // when skipping uncovering, continue with uncovered playfield
325 if (setup.bd_skip_uncovering)
326 game_bd.game->state_counter = GAME_INT_UNCOVER_ALL + 1;
328 if (setup.bd_skip_uncovering)
329 gd_scroll(game_bd.game, TRUE, TRUE);
331 ClearRectangle(gd_screen_bitmap, 0, 0, SXSIZE, SYSIZE);
333 RedrawPlayfield_BD(TRUE);
335 UpdateGameDoorValues_BD();
338 void GameActions_BD(byte action[MAX_PLAYERS])
340 GdCave *cave = game_bd.game->cave;
341 boolean player_found = FALSE;
348 for (y = 0; y < cave->h && !player_found; y++)
350 for (x = 0; x < cave->w && !player_found; x++)
352 int element = *cave->getp(cave, x, y);
354 if (element == O_PLAYER ||
355 element == O_PLAYER_BOMB ||
356 element == O_PLAYER_STIRRING ||
357 element == O_PLAYER_PNEUMATIC_LEFT ||
358 element == O_PLAYER_PNEUMATIC_RIGHT)
369 UpdateEngineValues(get_scroll_x(),
374 if (setup.bd_skip_hatching && !game_bd.game->cave->hatched &&
375 game_bd.game->state_counter == GAME_INT_CAVE_RUNNING)
377 // fast-forward game engine until player hatched
378 while (!game_bd.game->cave->hatched)
380 play_game_func(game_bd.game, 0);
382 // also record or replay tape action during fast-forward
383 action = TapeCorrectAction_BD(action);
387 play_game_func(game_bd.game, action[0]);
389 // scroll without iterating engine if player out of sight (mainly due to wrap-around)
390 // (this is needed to prevent broken tapes in case of viewport or tile size changes)
391 while (game_bd.game->out_of_window)
393 RedrawPlayfield_BD(TRUE);
395 BlitScreenToBitmap_BD(backbuffer);
399 play_game_func(game_bd.game, action[0]);
402 RedrawPlayfield_BD(FALSE);
404 UpdateGameDoorValues_BD();
408 // ============================================================================
409 // graphics functions
410 // ============================================================================
412 Bitmap *GetTitleScreen_BD(void)
414 return gd_get_title_screen_bitmap();
417 void CoverScreen_BD(void)
419 game_bd.cover_screen = FALSE;
421 if (setup.bd_skip_uncovering)
424 game_bd.game->state_counter = GAME_INT_COVER_START;
426 // play game engine (with normal speed) until cave covered
427 while (game_bd.game->state_counter < GAME_INT_COVER_ALL + 1)
429 play_game_func(game_bd.game, 0);
431 RedrawPlayfield_BD(TRUE);
433 BlitScreenToBitmap_BD(backbuffer);
438 // stop uncovering loop sound when not using native sound engine
442 void BlitScreenToBitmap_BD(Bitmap *target_bitmap)
444 GdCave *cave = native_bd_level.cave;
447 int full_xsize = (cave->x2 - cave->x1 + 1) * TILESIZE_VAR;
448 int full_ysize = (cave->y2 - cave->y1 + 1) * TILESIZE_VAR;
449 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
450 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
451 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
452 int sysize = (full_ysize < ysize ? full_ysize : ysize);
454 BlitBitmap(gd_screen_bitmap, target_bitmap, 0, 0, sxsize, sysize, sx, sy);
457 void RedrawPlayfield_BD(boolean force_redraw)
459 gd_drawcave(gd_screen_bitmap, game_bd.game, force_redraw);