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 static List *getNativeLevelReplay_BD_Ext(List *item, boolean only_successful_replays)
100 // look for replay that was recorded for the current difficulty level
101 while (item != NULL &&
102 (item->data == NULL ||
103 (((GdReplay *)item->data)->success == FALSE && only_successful_replays) ||
104 ((GdReplay *)item->data)->level != native_bd_level.level_nr))
110 static List *getNativeLevelReplay_BD(List *replays)
112 // 1st try: look for successful replay
113 List *item = getNativeLevelReplay_BD_Ext(replays, TRUE);
118 // 2nd try: look for any replay
119 return getNativeLevelReplay_BD_Ext(replays, FALSE);
122 boolean LoadNativeLevel_BD(char *filename, int level_pos, boolean level_info_only)
124 static char *filename_loaded = NULL;
126 if (filename_loaded == NULL || !strEqual(filename, filename_loaded))
128 if (!gd_caveset_load_from_file(filename))
130 if (!level_info_only)
131 Warn("cannot load BD cave set from file '%s'", filename);
136 setString(&filename_loaded, filename);
139 if (level_pos < 0 || level_pos >= 5 * gd_caveset_count())
141 Warn("invalid level position %d in BD cave set", level_pos);
146 native_bd_level.cave_nr = level_pos % gd_caveset_count();
147 native_bd_level.level_nr = level_pos / gd_caveset_count();
149 if (native_bd_level.cave != NULL)
150 gd_cave_free(native_bd_level.cave);
152 // get selected cave, prepared for playing
153 native_bd_level.cave = gd_get_prepared_cave_from_caveset(native_bd_level.cave_nr,
154 native_bd_level.level_nr);
156 // set better initial cave speed (to set better native replay tape length)
157 set_initial_cave_speed(native_bd_level.cave);
159 native_bd_level.loaded_from_caveset = TRUE;
161 // check if this cave has any replays
162 if (native_bd_level.cave->replays != NULL)
164 List *item = getNativeLevelReplay_BD(native_bd_level.cave->replays);
166 // check if any matching replay was found
168 native_bd_level.replay = (GdReplay *)item->data;
174 boolean SaveNativeLevel_BD(char *filename)
176 GdCave *cave = gd_cave_new_from_cave(native_bd_level.cave);
179 gd_caveset = list_append(gd_caveset, cave);
181 return gd_caveset_save_to_file(filename);
185 // ============================================================================
186 // game engine functions
187 // ============================================================================
189 int map_action_RND_to_BD(int action)
191 GdDirection player_move = gd_direction_from_keypress(action & JOY_UP,
195 boolean player_fire = (action & (JOY_BUTTON_1 | JOY_BUTTON_2));
197 return (player_move | (player_fire ? GD_REPLAY_FIRE_MASK : 0));
200 int map_action_BD_to_RND(int action)
202 GdDirection player_move = action & GD_REPLAY_MOVE_MASK;
203 boolean player_fire = action & GD_REPLAY_FIRE_MASK;
205 int action_move = (player_move == GD_MV_UP ? JOY_UP :
206 player_move == GD_MV_UP_RIGHT ? JOY_UP | JOY_RIGHT :
207 player_move == GD_MV_RIGHT ? JOY_RIGHT :
208 player_move == GD_MV_DOWN_RIGHT ? JOY_DOWN | JOY_RIGHT :
209 player_move == GD_MV_DOWN ? JOY_DOWN :
210 player_move == GD_MV_DOWN_LEFT ? JOY_DOWN | JOY_LEFT :
211 player_move == GD_MV_LEFT ? JOY_LEFT :
212 player_move == GD_MV_UP_LEFT ? JOY_UP | JOY_LEFT : JOY_NO_ACTION);
213 int action_fire = (player_fire ? JOY_BUTTON_1 : JOY_NO_ACTION);
215 return (action_move | action_fire);
218 boolean checkGameRunning_BD(void)
220 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CAVE_RUNNING);
223 boolean checkGamePlaying_BD(void)
225 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CAVE_RUNNING &&
226 game_bd.game->cave != NULL && game_bd.game->cave->hatched);
229 boolean checkBonusTime_BD(void)
231 return (game_bd.game != NULL && game_bd.game->state_counter == GAME_INT_CHECK_BONUS_TIME);
234 int getFramesPerSecond_BD(void)
236 if (game_bd.game != NULL && game_bd.game->cave != NULL && game_bd.game->cave->pal_timing)
237 return FRAMES_PER_SECOND_NTSC;
239 return FRAMES_PER_SECOND_PAL;
242 int getTimeLeft_BD(void)
244 if (game_bd.game != NULL && game_bd.game->cave != NULL)
245 return gd_cave_time_show(game_bd.game->cave, game_bd.game->cave->time);
250 static void UpdateGameDoorValues_BD(void)
252 GdCave *cave = game_bd.game->cave;
253 int time_secs = gd_cave_time_show(cave, cave->time);
254 int gems_still_needed = MAX(0, (cave->diamonds_needed - cave->diamonds_collected));
256 game_bd.time_played = time_secs;
257 game_bd.gems_still_needed = gems_still_needed;
258 game_bd.score = game_bd.game->player_score;
260 if (game.LevelSolved)
262 // update time and score in panel while counting bonus time
263 game.LevelSolved_CountingTime = game_bd.time_played;
264 game.LevelSolved_CountingScore = game_bd.score;
268 static void PrepareGameTileBitmap_BD(void)
270 struct GraphicInfo_BD *g_template = &graphic_info_bd_color_template;
271 struct GraphicInfo_BD *g_default = &graphic_info_bd_object[O_STONE][0];
273 gd_prepare_tile_bitmap(native_bd_level.cave, g_template->bitmap, 1);
274 gd_set_tile_bitmap_reference(g_default->bitmap);
277 void PreparePreviewTileBitmap_BD(Bitmap *bitmap, int scale_down_factor)
279 gd_prepare_tile_bitmap(native_bd_level.cave, bitmap, scale_down_factor);
282 void SetPreviewTileBitmapReference_BD(Bitmap *bitmap)
284 gd_set_tile_bitmap_reference(bitmap);
287 Bitmap *GetPreviewTileBitmap_BD(Bitmap *bitmap)
289 return gd_get_tile_bitmap(bitmap);
292 unsigned int InitEngineRandom_BD(int seed)
294 if (seed == NEW_RANDOMIZE)
296 // get randomly selected seed to render the cave
297 seed = gd_random_int_range(0, GD_CAVE_SEED_MAX);
300 game_bd.random_seed = seed;
302 return (unsigned int)seed;
305 void InitGameEngine_BD(void)
307 game_bd.level_solved = FALSE;
308 game_bd.game_over = FALSE;
309 game_bd.cover_screen = FALSE;
311 game_bd.time_played = 0;
312 game_bd.gems_still_needed = 0;
315 gd_caveset_last_selected = native_bd_level.cave_nr;
316 gd_caveset_last_selected_level = native_bd_level.level_nr;
318 if (game_bd.game != NULL)
319 gd_game_free(game_bd.game);
321 game_bd.game = gd_game_new(native_bd_level.cave_nr, native_bd_level.level_nr);
323 game_bd.game->itercycle = 0;
324 game_bd.game->itermax = 8; // default; dynamically changed at runtime
325 game_bd.game->itermax_last = game_bd.game->itermax;
327 // prepare tile bitmap with level-specific colors, if available
328 PrepareGameTileBitmap_BD();
330 // default: start with completely covered playfield
331 int next_state = GAME_INT_START_UNCOVER + 1;
333 // when skipping uncovering, start with uncovered playfield
334 if (setup.bd_skip_uncovering)
335 next_state = GAME_INT_LOAD_CAVE + 1;
337 // fast-forward game engine until cave loaded (covered or uncovered)
338 while (game_bd.game->state_counter < next_state)
339 play_game_func(game_bd.game, 0);
341 // when skipping uncovering, continue with uncovered playfield
342 if (setup.bd_skip_uncovering)
343 game_bd.game->state_counter = GAME_INT_UNCOVER_ALL + 1;
345 if (setup.bd_skip_uncovering)
346 gd_scroll(game_bd.game, TRUE, TRUE);
348 ClearRectangle(gd_screen_bitmap, 0, 0, SXSIZE, SYSIZE);
350 RedrawPlayfield_BD(TRUE);
352 UpdateGameDoorValues_BD();
355 void GameActions_BD(byte action[MAX_PLAYERS])
357 GdCave *cave = game_bd.game->cave;
358 boolean player_found = FALSE;
365 for (y = 0; y < cave->h && !player_found; y++)
367 for (x = 0; x < cave->w && !player_found; x++)
369 int element = *cave->getp(cave, x, y);
371 if (element == O_PLAYER ||
372 element == O_PLAYER_BOMB ||
373 element == O_PLAYER_STIRRING ||
374 element == O_PLAYER_PNEUMATIC_LEFT ||
375 element == O_PLAYER_PNEUMATIC_RIGHT)
386 UpdateEngineValues(get_scroll_x(),
391 if (setup.bd_skip_hatching && !game_bd.game->cave->hatched &&
392 game_bd.game->state_counter == GAME_INT_CAVE_RUNNING)
394 // fast-forward game engine until player hatched
395 while (!game_bd.game->cave->hatched)
397 play_game_func(game_bd.game, 0);
399 // also record or replay tape action during fast-forward
400 action = TapeCorrectAction_BD(action);
404 play_game_func(game_bd.game, action[0]);
406 // scroll without iterating engine if player out of sight (mainly due to wrap-around)
407 // (this is needed to prevent broken tapes in case of viewport or tile size changes)
408 while (game_bd.game->out_of_window)
410 RedrawPlayfield_BD(TRUE);
412 BlitScreenToBitmap_BD(backbuffer);
416 play_game_func(game_bd.game, action[0]);
419 RedrawPlayfield_BD(FALSE);
421 UpdateGameDoorValues_BD();
425 // ============================================================================
426 // graphics functions
427 // ============================================================================
429 Bitmap *GetTitleScreen_BD(void)
431 return gd_get_title_screen_bitmap();
434 void CoverScreen_BD(void)
436 game_bd.cover_screen = FALSE;
438 if (setup.bd_skip_uncovering)
441 game_bd.game->state_counter = GAME_INT_COVER_START;
443 // play game engine (with normal speed) until cave covered
444 while (game_bd.game->state_counter < GAME_INT_COVER_ALL + 1)
446 play_game_func(game_bd.game, 0);
448 RedrawPlayfield_BD(TRUE);
450 BlitScreenToBitmap_BD(backbuffer);
455 // stop uncovering loop sound when not using native sound engine
459 void BlitScreenToBitmap_BD(Bitmap *target_bitmap)
461 GdCave *cave = native_bd_level.cave;
464 int full_xsize = (cave->x2 - cave->x1 + 1) * TILESIZE_VAR;
465 int full_ysize = (cave->y2 - cave->y1 + 1) * TILESIZE_VAR;
466 int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
467 int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
468 int sxsize = (full_xsize < xsize ? full_xsize : xsize);
469 int sysize = (full_ysize < ysize ? full_ysize : ysize);
471 BlitBitmap(gd_screen_bitmap, target_bitmap, 0, 0, sxsize, sysize, sx, sy);
474 void RedrawPlayfield_BD(boolean force_redraw)
476 gd_drawcave(gd_screen_bitmap, game_bd.game, force_redraw);