Skip to content

Commit

Permalink
Make CCK share the same minimap code as the file dialog
Browse files Browse the repository at this point in the history
Also refactored the code
  • Loading branch information
crudelios committed Apr 12, 2024
1 parent ae74a79 commit 1a5b36a
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 226 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ set(GAME_FILES
${PROJECT_SOURCE_DIR}/src/game/file.c
${PROJECT_SOURCE_DIR}/src/game/file_editor.c
${PROJECT_SOURCE_DIR}/src/game/file_io.c
${PROJECT_SOURCE_DIR}/src/game/file_minimap.c
${PROJECT_SOURCE_DIR}/src/game/game.c
${PROJECT_SOURCE_DIR}/src/game/mission.c
${PROJECT_SOURCE_DIR}/src/game/orientation.c
Expand Down Expand Up @@ -465,7 +466,6 @@ set(WIDGET_FILES
${PROJECT_SOURCE_DIR}/src/widget/map_editor.c
${PROJECT_SOURCE_DIR}/src/widget/map_editor_tool.c
${PROJECT_SOURCE_DIR}/src/widget/minimap.c
${PROJECT_SOURCE_DIR}/src/widget/scenario_minimap.c
${PROJECT_SOURCE_DIR}/src/widget/top_menu.c
${PROJECT_SOURCE_DIR}/src/widget/top_menu_editor.c
${PROJECT_SOURCE_DIR}/src/widget/sidebar/city.c
Expand Down
46 changes: 24 additions & 22 deletions src/game/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,40 @@ static void initialize_scenario_data(const uint8_t *scenario_name)
game_state_unpause();
}

static void load_empire_data(int is_custom_scenario, int empire_id)
{
empire_load(is_custom_scenario, empire_id);
scenario_distant_battle_set_roman_travel_months();
scenario_distant_battle_set_enemy_travel_months();
}

static int load_scenario_data(const char *scenario_file)
{
if (!game_file_io_read_scenario(scenario_file)) {
return 0;
}

trade_prices_reset();
load_empire_data(1, scenario_empire_id());
city_view_reset_orientation();
return 1;
}


static int load_custom_scenario(const uint8_t *scenario_name, const char *scenario_file)
{
if (!file_exists(scenario_file, NOT_LOCALIZED)) {
return 0;
}

clear_scenario_data();
game_file_load_scenario_data(scenario_file);
if (!load_scenario_data(scenario_file)) {
return 0;
}
initialize_scenario_data(scenario_name);
return 1;
}

static void load_empire_data(int is_custom_scenario, int empire_id)
{
empire_load(is_custom_scenario, empire_id);
scenario_distant_battle_set_roman_travel_months();
scenario_distant_battle_set_enemy_travel_months();
}

static void initialize_saved_game(void)
{
load_empire_data(scenario_is_custom(), scenario_empire_id());
Expand Down Expand Up @@ -332,21 +347,10 @@ int game_file_start_scenario(const char *scenario_file)
uint8_t scenario_name[FILE_NAME_MAX];
encoding_from_utf8(scenario_file, scenario_name, FILE_NAME_MAX);
file_remove_extension(scenario_name);
scenario_set_custom(2);
return start_scenario(scenario_name, scenario_file);
}

int game_file_load_scenario_data(const char *scenario_file)
{
if (!game_file_io_read_scenario(scenario_file)) {
return 0;
}

trade_prices_reset();
load_empire_data(1, scenario_empire_id());
city_view_reset_orientation();
return 1;
}

int game_file_load_saved_game(const char *filename)
{
if (!game_file_io_read_saved_game(filename, 0)) {
Expand All @@ -359,8 +363,6 @@ int game_file_load_saved_game(const char *filename)
return 1;
}



int game_file_write_saved_game(const char *filename)
{
return game_file_io_write_saved_game(filename);
Expand Down
7 changes: 0 additions & 7 deletions src/game/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ int game_file_start_scenario_by_name(const uint8_t *scenario_name);
*/
int game_file_start_scenario(const char *scenario_file);

/**
* Load scenario data only, without starting it
* @param scenario_file File to load
* @return Boolean true on success, false on failure
*/
int game_file_load_scenario_data(const char *scenario_file);

/**
* Load saved game
* @param filename File to load
Expand Down
56 changes: 52 additions & 4 deletions src/game/file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "figure/name.h"
#include "figure/route.h"
#include "figure/trader.h"
#include "game/file_minimap.h"
#include "game/time.h"
#include "game/tutorial.h"
#include "map/aqueduct.h"
Expand All @@ -43,7 +44,6 @@
#include "scenario/invasion.h"
#include "scenario/scenario.h"
#include "sound/city.h"
#include "widget/scenario_minimap.h"

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -528,6 +528,55 @@ int game_file_io_read_scenario(const char *filename)
return 1;
}

int game_file_io_read_scenario_info(const char *filename, scenario_info *info)
{
init_scenario_data();
FILE *fp = file_open(dir_get_file(filename, NOT_LOCALIZED), "rb");
if (!fp) {
return 0;
}
for (int i = 0; i < scenario_data.num_pieces; i++) {
size_t read_size = fread(scenario_data.pieces[i].buf.data, 1, scenario_data.pieces[i].buf.size, fp);
if (read_size != scenario_data.pieces[i].buf.size) {
log_error("Unable to load scenario", filename, 0);
file_close(fp);
return 0;
}
}
file_close(fp);

const scenario_state *state = &scenario_data.state;

scenario_description_from_buffer(state->scenario, info->description);
info->image_id = scenario_image_id_from_buffer(state->scenario);
info->climate = scenario_climate_from_buffer(state->scenario);
info->total_invasions = scenario_invasions_from_buffer(state->scenario);
info->player_rank = scenario_rank_from_buffer(state->scenario);
info->start_year = scenario_start_year_from_buffer(state->scenario);
scenario_open_play_info_from_buffer(state->scenario, &info->is_open_play, &info->open_play_id);

if (!info->is_open_play) {
scenario_objectives_from_buffer(state->scenario, &info->win_criteria);
}

file_buffers minimap_buffers;
minimap_buffers.buildings = 0;
minimap_buffers.map_building = 0;
minimap_buffers.map_bitfields = state->bitfields;
minimap_buffers.map_terrain = state->terrain;
minimap_buffers.map_edge = state->edge;
minimap_buffers.map_random = state->random;
minimap_buffers.climate = info->climate;

scenario_map_data_from_buffer(state->scenario, &minimap_buffers.grid_width, &minimap_buffers.grid_height,
&minimap_buffers.grid_start, &minimap_buffers.grid_border_size);

info->minimap_image = game_file_minimap_create(&minimap_buffers);
info->map_size = minimap_buffers.grid_width;

return 1;
}

int game_file_io_write_scenario(const char *filename)
{
log_info("Saving scenario", filename, 0);
Expand Down Expand Up @@ -688,7 +737,7 @@ static int savegame_read_file_info(FILE *fp, saved_game_info *info)
init_file_piece(&building_grid, 52488, 0);
init_file_piece(&buildings, 256000, 0);

minimap_save_buffers minimap_buffers;
file_buffers minimap_buffers;
minimap_buffers.map_terrain = &terrain_grid.buf;
minimap_buffers.map_random = &random_grid.buf;
minimap_buffers.map_edge = &edge_grid.buf;
Expand Down Expand Up @@ -793,8 +842,7 @@ static int savegame_read_file_info(FILE *fp, saved_game_info *info)
scenario_map_data_from_buffer(&scenario.buf, &minimap_buffers.grid_width, &minimap_buffers.grid_height,
&minimap_buffers.grid_start, &minimap_buffers.grid_border_size);

info->minimap_image = widget_scenario_minimap_draw_from_save(&minimap_buffers,
&info->minimap_image_width, &info->minimap_image_height);
info->minimap_image = game_file_minimap_create(&minimap_buffers);

free_file_piece(&city_data);
free_file_piece(&game_time);
Expand Down
19 changes: 17 additions & 2 deletions src/game/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GAME_FILE_IO_H

#include "graphics/color.h"
#include "scenario/data.h"

typedef struct {
int mission;
Expand All @@ -11,12 +12,26 @@ typedef struct {
int month;
int year;
const color_t *minimap_image;
int minimap_image_width;
int minimap_image_height;
} saved_game_info;

typedef struct {
uint8_t description[MAX_BRIEF_DESCRIPTION];
int image_id;
int start_year;
int climate;
int map_size;
int total_invasions;
int player_rank;
int is_open_play;
int open_play_id;
scenario_win_criteria win_criteria;
const color_t *minimap_image;
} scenario_info;

int game_file_io_read_scenario(const char *filename);

int game_file_io_read_scenario_info(const char *filename, scenario_info *info);

int game_file_io_write_scenario(const char *filename);

int game_file_io_read_saved_game(const char *filename, int offset);
Expand Down
Loading

0 comments on commit 1a5b36a

Please sign in to comment.