Cleaned up ridiculously excessive passing around of terrain data cache

Keep in mind ter_data_cache is simply a shared_ptr, and in all call locations it was grabbed from game_config_manager::terrain_types().

1. campaign_controller took and kept a member, which was passed to either...
2. playsingle_controller or playmp_controller, which passed it to their parent class...
3. play_controller, which kept a member to initialize...
4. game_state, which passed it on to...
5. game_board, which passed it on to...
6. gamemap, where it was *finally* actually needed.

This entire chain has been replaced with a single call to game_config_manager::terrain_types() in the game_board constructor.
gamemap retains its ctor parameter since editor_map passes in its own object.

The ter_data_cache alias has also been removed. It was just confusing.
This commit is contained in:
Charles Dang 2021-01-02 17:03:15 +11:00
parent bd8b5be3fe
commit a4b749524b
23 changed files with 51 additions and 66 deletions

View File

@ -14,6 +14,7 @@
#include "config.hpp"
#include "game_board.hpp"
#include "game_config_manager.hpp"
#include "preferences/game.hpp"
#include "log.hpp"
#include "map/map.hpp"
@ -33,9 +34,9 @@ static lg::log_domain log_engine("enginerefac");
static lg::log_domain log_engine_enemies("engine/enemies");
#define DBG_EE LOG_STREAM(debug, log_engine_enemies)
game_board::game_board(const ter_data_cache & tdata, const config & level)
game_board::game_board(const config& level)
: teams_()
, map_(new gamemap(tdata, level["map_data"]))
, map_(new gamemap(game_config_manager::get()->terrain_types(), level["map_data"]))
, unit_id_manager_(level["next_underlying_unit_id"])
, units_()
{

View File

@ -86,7 +86,7 @@ public:
n_unit::id_manager& unit_id_manager() { return unit_id_manager_; }
// Constructors, trivial dtor, and const accessors
game_board(const ter_data_cache & tdata, const config & level);
game_board(const config& level);
virtual ~game_board();
virtual const std::vector<team>& teams() const override

View File

@ -41,7 +41,7 @@ public:
const game_config_view& game_config() const { return game_config_view_; }
const preproc_map& old_defines_map() const { return old_defines_map_; }
const ter_data_cache & terrain_types() const { return tdata_; }
const std::shared_ptr<terrain_type_data> & terrain_types() const { return tdata_; }
bool init_game_config(FORCE_RELOAD_CONFIG force_reload);
void reload_changed_game_config();
@ -84,5 +84,5 @@ private:
game_config::config_cache& cache_;
ter_data_cache tdata_;
std::shared_ptr<terrain_type_data> tdata_;
};

View File

@ -628,7 +628,7 @@ void mp_manager::enter_staging_mode() const
} // end connect_engine
if(dlg_ok) {
campaign_controller controller(state, game_config_manager::get()->terrain_types());
campaign_controller controller(state);
controller.set_mp_info(metadata.get());
controller.play_game();
}
@ -672,7 +672,7 @@ void mp_manager::enter_wait_mode(int game_id, bool observe) const
}
if(dlg_ok) {
campaign_controller controller(state, game_config_manager::get()->terrain_types());
campaign_controller controller(state);
controller.set_mp_info(&metadata);
controller.play_game();
}
@ -819,7 +819,7 @@ void start_local_game_commandline(saved_game& state, const commandline_options&
unsigned int repeat = (cmdline_opts.multiplayer_repeat) ? *cmdline_opts.multiplayer_repeat : 1;
for(unsigned int i = 0; i < repeat; i++){
saved_game state_copy(state);
campaign_controller controller(state_copy, game_config_manager::get()->terrain_types());
campaign_controller controller(state_copy);
controller.play_game();
}
}

View File

@ -188,7 +188,7 @@ void campaign_controller::show_carryover_message(playsingle_controller& playcont
LEVEL_RESULT campaign_controller::playsingle_scenario(end_level_data &end_level)
{
playsingle_controller playcontroller(is_replay_ ? state_.get_replay_starting_point() : state_.get_starting_point(),
state_, tdata_, false);
state_, false);
LOG_NG << "created objects... " << (SDL_GetTicks() - playcontroller.get_ticks()) << "\n";
if(is_replay_) {
@ -221,8 +221,7 @@ LEVEL_RESULT campaign_controller::playsingle_scenario(end_level_data &end_level)
LEVEL_RESULT campaign_controller::playmp_scenario(end_level_data &end_level)
{
playmp_controller playcontroller(state_.get_starting_point(), state_, tdata_, mp_info_);
playmp_controller playcontroller(state_.get_starting_point(), state_, mp_info_);
LEVEL_RESULT res = playcontroller.play_scenario(state_.get_starting_point());
//Check if the player started as mp client and changed to host

View File

@ -28,11 +28,9 @@ class saved_game;
class terrain_type_data;
class team;
class playsingle_controller;
typedef std::shared_ptr<terrain_type_data> ter_data_cache;
class config;
class wesnothd_connection;
struct mp_game_metadata
{
mp_game_metadata(wesnothd_connection& wdc)
@ -57,14 +55,12 @@ struct mp_game_metadata
class campaign_controller
{
saved_game& state_;
const ter_data_cache & tdata_;
const bool is_unit_test_;
bool is_replay_;
mp_game_metadata* mp_info_;
public:
campaign_controller(saved_game& state, const ter_data_cache & tdata, bool is_unit_test = false)
campaign_controller(saved_game& state, bool is_unit_test = false)
: state_(state)
, tdata_(tdata)
, is_unit_test_(is_unit_test)
, is_replay_(false)
, mp_info_(nullptr)

View File

@ -481,7 +481,7 @@ bool game_launcher::play_test()
game_config_manager::get()->load_game_config_for_game(state_.classification(), state_.get_scenario_id());
try {
campaign_controller ccontroller(state_, game_config_manager::get()->terrain_types());
campaign_controller ccontroller(state_);
ccontroller.play_game();
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
@ -554,7 +554,7 @@ game_launcher::unit_test_result game_launcher::single_unit_test()
LEVEL_RESULT game_res = LEVEL_RESULT::TEST_FAIL;
try {
campaign_controller ccontroller(state_, game_config_manager::get()->terrain_types(), true);
campaign_controller ccontroller(state_, true);
game_res = ccontroller.play_game();
// TODO: How to handle the case where a unit test scenario ends without an explicit {SUCCEED} or {FAIL}?
// ex: check_victory_never_ai_fail results in victory by killing one side's leaders
@ -587,7 +587,7 @@ game_launcher::unit_test_result game_launcher::single_unit_test()
}
try {
campaign_controller ccontroller(state_, game_config_manager::get()->terrain_types(), true);
campaign_controller ccontroller(state_, true);
ccontroller.play_replay();
if(lg::broke_strict()) {
std::cerr << "Observed failure on replay" << std::endl;
@ -1017,7 +1017,7 @@ void game_launcher::launch_game(RELOAD_GAME_DATA reload)
});
try {
campaign_controller ccontroller(state_, game_config_manager::get()->terrain_types());
campaign_controller ccontroller(state_);
LEVEL_RESULT result = ccontroller.play_game();
ai::manager::singleton_ = nullptr;
// don't show The End for multiplayer scenario
@ -1041,7 +1041,7 @@ void game_launcher::play_replay()
{
assert(!load_data_);
try {
campaign_controller ccontroller(state_, game_config_manager::get()->terrain_types());
campaign_controller ccontroller(state_);
ccontroller.play_replay();
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));

View File

@ -44,9 +44,9 @@ static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)
game_state::game_state(const config& level, play_controller& pc, const ter_data_cache& tdata)
game_state::game_state(const config& level, play_controller& pc)
: gamedata_(level)
, board_(tdata, level)
, board_(level)
, tod_manager_(level)
, pathfind_manager_(new pathfind::manager(level))
, reports_(new reports())

View File

@ -74,7 +74,7 @@ public:
int first_human_team_; //needed to initialize the viewpoint during setup
bool has_human_sides() const { return first_human_team_ != -1; }
game_state(const config & level, play_controller &, const ter_data_cache & tdata);
game_state(const config & level, play_controller &);
/** The third parameter is an optimisation. */
game_state(const config & level, play_controller &, game_board& board);

View File

@ -150,7 +150,7 @@ static inline std::string get_mp_tooltip(int total_movement, std::function<int (
std::ostringstream tooltip;
tooltip << "<big>" << _("Movement Costs:") << "</big>";
ter_data_cache tdata = help::load_terrain_types_data();
std::shared_ptr<terrain_type_data> tdata = help::load_terrain_types_data();
if(!tdata) {
return "";

View File

@ -836,7 +836,7 @@ void generate_era_sections(const config* help_cfg, section & sec, int level)
void generate_terrain_sections(section& sec, int /*level*/)
{
ter_data_cache tdata = load_terrain_types_data();
std::shared_ptr<terrain_type_data> tdata = load_terrain_types_data();
if (!tdata) {
WRN_HP << "When building terrain help sections, couldn't acquire terrain types data, aborting.\n";
@ -1565,13 +1565,14 @@ std::string escape(const std::string &s)
}
/** Load the appropriate terrain types data to use */
ter_data_cache load_terrain_types_data() {
std::shared_ptr<terrain_type_data> load_terrain_types_data()
{
if (display::get_singleton()) {
return display::get_singleton()->get_disp_context().map().tdata();
} else if (game_config_manager::get()){
return game_config_manager::get()->terrain_types();
} else {
return ter_data_cache();
return {};
}
}

View File

@ -49,7 +49,7 @@ class game_config_view;
class config;
class unit_type;
class terrain_type_data;
typedef std::shared_ptr<terrain_type_data> ter_data_cache;
namespace help {
/**
@ -330,7 +330,7 @@ std::string escape(const std::string &s);
std::string get_first_word(const std::string &s);
/** Load the appropriate terrain types data to use */
ter_data_cache load_terrain_types_data();
std::shared_ptr<terrain_type_data> load_terrain_types_data();
extern const game_config_view *game_cfg;
// The default toplevel.

View File

@ -69,7 +69,7 @@ static std::string best_str(bool best) {
typedef t_translation::ter_list::const_iterator ter_iter;
// Gets an english description of a terrain ter_list alias behavior: "Best of cave, hills", "Worst of Swamp, Forest" etc.
static std::string print_behavior_description(ter_iter start, ter_iter end, const ter_data_cache & tdata, bool first_level = true, bool begin_best = true)
static std::string print_behavior_description(ter_iter start, ter_iter end, const std::shared_ptr<terrain_type_data> & tdata, bool first_level = true, bool begin_best = true)
{
if (start == end) return "";
@ -146,7 +146,7 @@ std::string terrain_topic_generator::operator()() const {
else
ss << "\n";
ter_data_cache tdata = load_terrain_types_data();
std::shared_ptr<terrain_type_data> tdata = load_terrain_types_data();
if (!tdata) {
WRN_HP << "When building terrain help topics, we couldn't acquire any terrain types data\n";
@ -692,7 +692,7 @@ std::string unit_topic_generator::operator()() const {
}
ss << generate_table(resistance_table);
if (ter_data_cache tdata = load_terrain_types_data()) {
if (std::shared_ptr<terrain_type_data> tdata = load_terrain_types_data()) {
// Print the terrain modifier table of the unit.
ss << "\n\n<header>text='" << escape(_("Terrain Modifiers"))
<< "'</header>\n\n";

View File

@ -100,12 +100,12 @@ void gamemap::write_terrain(const map_location &loc, config& cfg) const
cfg["terrain"] = t_translation::write_terrain_code(get_terrain(loc));
}
gamemap::gamemap(const ter_data_cache& tdata, const std::string& data):
tiles_(1, 1),
tdata_(tdata),
villages_(),
w_(-1),
h_(-1)
gamemap::gamemap(const std::shared_ptr<terrain_type_data>& tdata, const std::string& data)
: tiles_(1, 1)
, tdata_(tdata)
, villages_()
, w_(-1)
, h_(-1)
{
DBG_G << "loading map: '" << data << "'\n";

View File

@ -65,7 +65,7 @@ public:
const terrain_type& get_terrain_info(const t_translation::terrain_code & terrain) const;
/* Get the underlying terrain_type_data object. */
const ter_data_cache & tdata() const { return tdata_; }
const std::shared_ptr<terrain_type_data>& tdata() const { return tdata_; }
/**
* Loads a map, with the given terrain configuration.
@ -76,7 +76,7 @@ public:
* @param tdata the terrain data
* @param data the map data to load.
*/
gamemap(const ter_data_cache &tdata, const std::string &data); //throw(incorrect_map_format_error)
gamemap(const std::shared_ptr<terrain_type_data>& tdata, const std::string& data); // throw(incorrect_map_format_error)
virtual ~gamemap();
@ -254,7 +254,7 @@ private:
*/
int read_header(const std::string& data);
ter_data_cache tdata_;
std::shared_ptr<terrain_type_data> tdata_;
protected:
std::vector<map_location> villages_;

View File

@ -287,7 +287,7 @@ int movetype::terrain_info::data::calc_value(
return params_.default_value;
}
ter_data_cache tdata;
std::shared_ptr<terrain_type_data> tdata;
if (game_config_manager::get()){
tdata = game_config_manager::get()->terrain_types(); //This permits to get terrain info in unit help pages from the help in title screen, even if there is no residual gamemap object
}

View File

@ -137,13 +137,11 @@ static void clear_resources()
resources::classification = nullptr;
}
play_controller::play_controller(const config& level, saved_game& state_of_game,
const ter_data_cache& tdata, bool skip_replay)
play_controller::play_controller(const config& level, saved_game& state_of_game, bool skip_replay)
: controller_base()
, observer()
, quit_confirmation()
, ticks_(SDL_GetTicks())
, tdata_(tdata)
, gamestate_()
, level_()
, saved_game_(state_of_game)
@ -219,7 +217,7 @@ void play_controller::init(const config& level)
gui2::dialogs::loading_screen::progress(loading_stage::load_level);
LOG_NG << "initializing game_state..." << (SDL_GetTicks() - ticks()) << std::endl;
gamestate_.reset(new game_state(level, *this, tdata_));
gamestate_.reset(new game_state(level, *this));
resources::gameboard = &gamestate().board_;
resources::gamedata = &gamestate().gamedata_;
@ -319,7 +317,7 @@ void play_controller::reset_gamestate(const config& level, int replay_pos)
This is necessary to ensure that while the old AI manager is being destroyed,
all its member objects access the old manager instead of the new. */
gamestate_.reset();
gamestate_.reset(new game_state(level, *this, tdata_));
gamestate_.reset(new game_state(level, *this));
resources::gameboard = &gamestate().board_;
resources::gamedata = &gamestate().gamedata_;
resources::tod_manager = &gamestate().tod_manager_;

View File

@ -80,8 +80,7 @@ class game_state;
class play_controller : public controller_base, public events::observer, public quit_confirmation
{
public:
play_controller(const config& level, saved_game& state_of_game,
const ter_data_cache& tdata, bool skip_replay);
play_controller(const config& level, saved_game& state_of_game, bool skip_replay);
virtual ~play_controller();
//event handler, overridden from observer
@ -334,7 +333,6 @@ private:
protected:
//gamestate
const ter_data_cache& tdata_;
std::unique_ptr<game_state> gamestate_;
config level_;
saved_game& saved_game_;

View File

@ -40,11 +40,8 @@
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
playmp_controller::playmp_controller(const config& level,
saved_game& state_of_game,
const ter_data_cache & tdata,
mp_game_metadata* mp_info)
: playsingle_controller(level, state_of_game, tdata, mp_info && mp_info->skip_replay)
playmp_controller::playmp_controller(const config& level, saved_game& state_of_game, mp_game_metadata* mp_info)
: playsingle_controller(level, state_of_game, mp_info && mp_info->skip_replay)
, network_processing_stopped_(false)
, blindfold_(*gui_, mp_info && mp_info->skip_replay_blindfolded)
, mp_info_(mp_info)

View File

@ -23,8 +23,7 @@ struct mp_game_metadata;
class playmp_controller : public playsingle_controller, public syncmp_handler
{
public:
playmp_controller(const config& level, saved_game& state_of_game,
const ter_data_cache & tdata, mp_game_metadata* mp_info);
playmp_controller(const config& level, saved_game& state_of_game, mp_game_metadata* mp_info);
virtual ~playmp_controller();
void maybe_linger() override;

View File

@ -63,9 +63,8 @@ static lg::log_domain log_engine("engine");
static lg::log_domain log_enginerefac("enginerefac");
#define LOG_RG LOG_STREAM(info, log_enginerefac)
playsingle_controller::playsingle_controller(const config& level,
saved_game& state_of_game, const ter_data_cache & tdata, bool skip_replay)
: play_controller(level, state_of_game, tdata, skip_replay)
playsingle_controller::playsingle_controller(const config& level, saved_game& state_of_game, bool skip_replay)
: play_controller(level, state_of_game, skip_replay)
, cursor_setter_(cursor::NORMAL)
, textbox_info_()
, replay_sender_(*resources::recorder)

View File

@ -37,8 +37,7 @@ struct reset_gamestate_exception : public std::exception
class playsingle_controller : public play_controller
{
public:
playsingle_controller(const config& level, saved_game& state_of_game,
const ter_data_cache & tdata, bool skip_replay);
playsingle_controller(const config& level, saved_game& state_of_game, bool skip_replay);
LEVEL_RESULT play_scenario(const config& level);
void play_scenario_init();

View File

@ -150,5 +150,3 @@ public:
private:
tcodeToTerrain_t::const_iterator find_or_create(t_translation::terrain_code) const;
};
typedef std::shared_ptr<terrain_type_data> ter_data_cache;