Skip to content

Commit

Permalink
Move terrain_type_data pointer handling directly to gamemap
Browse files Browse the repository at this point in the history
Drops the ctor argument from gamemap. I checked editor_map, and while it was creating its own terrain_type_data object,
it was using the same data as in the game_config_manager, so there's little point in not simply using the gcm's ttd
object in all cases. If the GCM singleton is null, a dummy object is created. Should cover the unit tests case.

This also removes the game_config_view arguments from the editor_map ctors since they're no longer needed.
  • Loading branch information
Vultraz committed Jan 2, 2021
1 parent ba55d38 commit 75e9d8a
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/editor/map/context_manager.cpp
Expand Up @@ -718,7 +718,7 @@ void context_manager::generate_map_dialog()
if(map_string.empty()) {
gui2::show_transient_message("", _("Map creation failed."));
} else {
editor_map new_map(game_config_, map_string);
editor_map new_map(map_string);
editor_action_whole_map a(new_map);
get_map_context().set_needs_labels_reset(); // Ensure Player Start labels are updated together with newly generated map
perform_refresh(a);
Expand Down Expand Up @@ -951,7 +951,7 @@ void context_manager::revert_map()
void context_manager::new_map(int width, int height, const t_translation::terrain_code& fill, bool new_context)
{
const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
editor_map m(game_config_, width, height, fill);
editor_map m(width, height, fill);

if(new_context) {
int new_id = add_map_context(m, true, default_schedule);
Expand All @@ -964,7 +964,7 @@ void context_manager::new_map(int width, int height, const t_translation::terrai
void context_manager::new_scenario(int width, int height, const t_translation::terrain_code& fill, bool new_context)
{
const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
editor_map m(game_config_, width, height, fill);
editor_map m(width, height, fill);

if(new_context) {
int new_id = add_map_context(m, false, default_schedule);
Expand Down Expand Up @@ -1017,7 +1017,7 @@ void context_manager::create_default_context()
t_translation::read_terrain_code(game_config::default_terrain);

const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
add_map_context(editor_map(game_config_, 44, 33, default_terrain), true, default_schedule);
add_map_context(editor_map(44, 33, default_terrain), true, default_schedule);
} else {
for(const std::string& filename : saved_windows_) {
add_map_context(game_config_, filename);
Expand Down
16 changes: 8 additions & 8 deletions src/editor/map/editor_map.cpp
Expand Up @@ -39,23 +39,23 @@ editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, c
return editor_map_load_exception(filename, msg);
}

editor_map::editor_map(const game_config_view& terrain_cfg)
: gamemap(std::make_shared<terrain_type_data>(terrain_cfg), "")
editor_map::editor_map()
: gamemap("")
, selection_()
{
}

editor_map::editor_map(const game_config_view& terrain_cfg, const std::string& data)
: gamemap(std::make_shared<terrain_type_data>(terrain_cfg), data)
editor_map::editor_map(const std::string& data)
: gamemap(data)
, selection_()
{
sanity_check();
}

editor_map editor_map::from_string(const game_config_view& terrain_cfg, const std::string& data)
editor_map editor_map::from_string(const std::string& data)
{
try {
return editor_map(terrain_cfg, data);
return editor_map(data);
} catch (const incorrect_map_format_error& e) {
throw wrap_exc("format", e.message, "");
} catch (const wml_exception& e) {
Expand All @@ -65,8 +65,8 @@ editor_map editor_map::from_string(const game_config_view& terrain_cfg, const s
}
}

editor_map::editor_map(const game_config_view& terrain_cfg, std::size_t width, std::size_t height, const t_translation::terrain_code & filler)
: gamemap(std::make_shared<terrain_type_data>(terrain_cfg), t_translation::write_game_map(t_translation::ter_map(width + 2, height + 2, filler)))
editor_map::editor_map(std::size_t width, std::size_t height, const t_translation::terrain_code & filler)
: gamemap(t_translation::write_game_map(t_translation::ter_map(width + 2, height + 2, filler)))
, selection_()
{
sanity_check();
Expand Down
9 changes: 4 additions & 5 deletions src/editor/map/editor_map.hpp
Expand Up @@ -69,27 +69,26 @@ editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, c
class editor_map : public gamemap
{
public:

/**
* Empty map constructor
*/
explicit editor_map(const game_config_view& terrain_cfg);
editor_map();

/**
* Create an editor map from a map data string
*/
editor_map(const game_config_view& terrain_cfg, const std::string& data);
editor_map(const std::string& data);

/**
* Wrapper around editor_map(cfg, data) that catches possible exceptions
* and wraps them in a editor_map_load_exception
*/
static editor_map from_string(const game_config_view& terrain_cfg, const std::string& data);
static editor_map from_string(const std::string& data);

/**
* Create an editor map with the given dimensions and filler terrain
*/
editor_map(const game_config_view& terrain_cfg, std::size_t width, std::size_t height, const t_translation::terrain_code & filler);
editor_map(std::size_t width, std::size_t height, const t_translation::terrain_code & filler);

/**
* Create an editor_map by upgrading an existing gamemap. The map data is
Expand Down
10 changes: 5 additions & 5 deletions src/editor/map/map_context.cpp
Expand Up @@ -91,7 +91,7 @@ map_context::map_context(const game_config_view& game_config, const std::string&
, map_data_key_()
, embedded_(false)
, pure_map_(false)
, map_(game_config)
, map_()
, undo_stack_()
, redo_stack_()
, actions_since_save_(0)
Expand Down Expand Up @@ -161,7 +161,7 @@ map_context::map_context(const game_config_view& game_config, const std::string&
if(!boost::regex_search(
file_string, matched_map_data, rexpression_map_data, boost::regex_constants::match_not_dot_null)
) {
map_ = editor_map::from_string(game_config, file_string); // throws on error
map_ = editor_map::from_string(file_string); // throws on error
pure_map_ = true;

add_to_recent_files();
Expand Down Expand Up @@ -191,7 +191,7 @@ map_context::map_context(const game_config_view& game_config, const std::string&
LOG_ED << "Loading embedded map file" << std::endl;
embedded_ = true;
pure_map_ = true;
map_ = editor_map::from_string(game_config, map_data);
map_ = editor_map::from_string(map_data);
}

add_to_recent_files();
Expand All @@ -216,7 +216,7 @@ map_context::map_context(const game_config_view& game_config, const std::string&

filename_ = new_filename;
file_string = filesystem::read_file(filename_);
map_ = editor_map::from_string(game_config, file_string);
map_ = editor_map::from_string(file_string);
pure_map_ = true;

add_to_recent_files();
Expand Down Expand Up @@ -334,7 +334,7 @@ void map_context::load_scenario(const game_config_view& game_config)
victory_defeated_ = scenario["victory_when_enemies_defeated"].to_bool(true);
random_time_ = scenario["random_start_time"].to_bool(false);

map_ = editor_map::from_string(game_config, scenario["map_data"]); // throws on error
map_ = editor_map::from_string(scenario["map_data"]); // throws on error

labels_.read(scenario);

Expand Down
7 changes: 4 additions & 3 deletions src/game_board.cpp
Expand Up @@ -14,7 +14,6 @@

#include "config.hpp"
#include "game_board.hpp"
#include "game_config_manager.hpp"
#include "preferences/game.hpp"
#include "log.hpp"
#include "map/map.hpp"
Expand All @@ -36,7 +35,7 @@ static lg::log_domain log_engine_enemies("engine/enemies");

game_board::game_board(const config& level)
: teams_()
, map_(new gamemap(game_config_manager::get()->terrain_types(), level["map_data"]))
, map_(std::make_unique<gamemap>(level["map_data"]))
, unit_id_manager_(level["next_underlying_unit_id"])
, units_()
{
Expand All @@ -47,7 +46,9 @@ game_board::game_board(const game_board & other)
, labels_(other.labels_)
, map_(new gamemap(*(other.map_)))
, unit_id_manager_(other.unit_id_manager_)
, units_(other.units_) {}
, units_(other.units_)
{
}

game_board::~game_board() {}

Expand Down
5 changes: 2 additions & 3 deletions src/game_initialization/create_engine.cpp
Expand Up @@ -67,8 +67,7 @@ void scenario::set_metadata()
const std::string& map_data = data_["map_data"];

try {
map_.reset(new gamemap(game_config_manager::get()->terrain_types(),
map_data));
map_.reset(new gamemap(map_data));
} catch(const incorrect_map_format_error& e) {
data_["description"] = _("Map could not be loaded: ") + e.message;

Expand Down Expand Up @@ -658,7 +657,7 @@ void create_engine::init_all_levels()
bool add_map = true;
std::unique_ptr<gamemap> map;
try {
map.reset(new gamemap(game_config_manager::get()->terrain_types(), user_map_data["map_data"]));
map.reset(new gamemap(user_map_data["map_data"]));
} catch (const incorrect_map_format_error& e) {
user_map_data["description"] = _("Map could not be loaded: ") + e.message;

Expand Down
3 changes: 1 addition & 2 deletions src/game_initialization/lobby_data.cpp
Expand Up @@ -32,7 +32,6 @@
#include "mp_game_settings.hpp"
#include "preferences/credentials.hpp"
#include "preferences/game.hpp"
#include "terrain/type_data.hpp"
#include "wml_exception.hpp"

#include <iterator>
Expand Down Expand Up @@ -297,7 +296,7 @@ game_info::game_info(const config& game, const std::vector<std::string>& install
info_stream << " — ??×??";
} else {
try {
gamemap map(std::make_shared<terrain_type_data>(game_config), map_data);
gamemap map(map_data);
std::ostringstream msi;
msi << map.w() << font::unicode_multiplication_sign << map.h();
map_size_info = msi.str();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/minimap.cpp
Expand Up @@ -210,7 +210,7 @@ const surface minimap::get_image(const int w, const int h) const

try
{
const gamemap map(std::make_shared<terrain_type_data>(*terrain_), map_data_);
const gamemap map(map_data_);
const surface surf = image::getMinimap(w, h, map, nullptr, nullptr, true);
cache.emplace(key, value_type(surf));
#ifdef DEBUG_MINIMAP_CACHE
Expand Down
13 changes: 10 additions & 3 deletions src/map/map.cpp
Expand Up @@ -21,6 +21,7 @@

#include "config.hpp"
#include "formula/string_utils.hpp"
#include "game_config_manager.hpp"
#include "log.hpp"
#include "map/exception.hpp"
#include "serialization/parser.hpp"
Expand Down Expand Up @@ -100,15 +101,21 @@ void gamemap::write_terrain(const map_location &loc, config& cfg) const
cfg["terrain"] = t_translation::write_terrain_code(get_terrain(loc));
}

gamemap::gamemap(const std::shared_ptr<terrain_type_data>& tdata, const std::string& data)
gamemap::gamemap(const std::string& data)
: tiles_(1, 1)
, tdata_(tdata)
, tdata_()
, villages_()
, w_(-1)
, h_(-1)
{
DBG_G << "loading map: '" << data << "'\n";
if(const auto* gcm = game_config_manager::get()) {
tdata_ = gcm->terrain_types();
} else {
// Should only be encountered in unit tests
tdata_ = std::make_shared<terrain_type_data>(game_config_view::wrap({}));
}

DBG_G << "loading map: '" << data << "'\n";
read(data);
}

Expand Down
5 changes: 2 additions & 3 deletions src/map/map.hpp
Expand Up @@ -68,15 +68,14 @@ class gamemap
const std::shared_ptr<terrain_type_data>& tdata() const { return tdata_; }

/**
* Loads a map, with the given terrain configuration.
* Loads a map.
*
* Data should be a series of lines, with each character representing one
* hex on the map. Starting locations are represented by numbers.
*
* @param tdata the terrain data
* @param data the map data to load.
*/
gamemap(const std::shared_ptr<terrain_type_data>& tdata, const std::string& data); // throw(incorrect_map_format_error)
gamemap(const std::string& data); // throw(incorrect_map_format_error)

virtual ~gamemap();

Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -1069,7 +1069,7 @@ int game_lua_kernel::intf_terrain_mask(lua_State *L)
}


gamemap mask_map(board().map().tdata(), "");
gamemap mask_map("");
mask_map.read(t_str, false);
board().map_->overlay(mask_map, loc, rules, is_odd, ignore_special_locations);

Expand Down

0 comments on commit 75e9d8a

Please sign in to comment.