From e9250440905404365524a2dcb325697841115045 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Sun, 2 Feb 2020 00:11:30 +0100 Subject: [PATCH] Encapsulate the `mapgen_function` function pointers within `mapgen_factory` Don't expose them at all. All access to them is done internally. --- src/mapgen.cpp | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index bed02cb7c2767..0a7282b2ad4ab 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -215,21 +215,26 @@ class mapgen_basic_container mapgens_[index]->weight = 0; } /** - * Pick a mapgen function randomly. + * Pick a mapgen function randomly and call its generate function. + * This basically runs the mapgen functions with the given @ref mapgendata + * as argument. + * @return Whether the mapgen function has been run. It may not get run if + * the list of mapgen functions is effectively empty. * @p hardcoded_weight Weight for an additional entry. If that entry is chosen, - * a null pointer is returned. If unsure, just use 0 for it. + * false is returned. If unsure, just use 0 for it. */ - mapgen_function *pick( const int hardcoded_weight ) const { + bool generate( mapgendata &dat, const int hardcoded_weight ) const { if( hardcoded_weight > 0 && rng( 1, weights_.get_weight() + hardcoded_weight ) > weights_.get_weight() ) { - return nullptr; + return false; } const std::shared_ptr *const ptr = weights_.pick(); if( !ptr ) { - return nullptr; + return false; } assert( *ptr ); - return ptr->get(); + ( *ptr )->generate( dat ); + return true; } /** * Calls @ref mapgen_function::setup and sets up the internal weighted list using @@ -273,7 +278,7 @@ class mapgen_factory result.insert( elem.generator_id ); } } - // Used in C++ code only, see calls to `oter_mapgen.pick()` below + // Used in C++ code only, see calls to `oter_mapgen.generate()` below result.insert( "lab_1side" ); result.insert( "lab_4side" ); result.insert( "lab_finale_1level" ); @@ -313,13 +318,13 @@ class mapgen_factory int add( const std::string &key, const std::shared_ptr ptr ) { return mapgens_[key].add( ptr ); } - /// @see mapgen_basic_container::pick - mapgen_function *pick( const std::string &key, const int hardcoded_weight = 0 ) const { + /// @see mapgen_basic_container::generate + bool generate( mapgendata &dat, const std::string &key, const int hardcoded_weight = 0 ) const { const auto iter = mapgens_.find( key ); if( iter == mapgens_.end() ) { - return nullptr; + return false; } - return iter->second.pick( hardcoded_weight ); + return iter->second.generate( dat, hardcoded_weight ); } /// @see mapgen_basic_container::erase void erase( const std::string &key, const size_t index ) { @@ -3558,8 +3563,7 @@ void map::draw_lab( mapgendata &dat ) //A lab area with only one entrance if( boarders == 1 ) { // If you remove the usage of "lab_1side" here, remove it from mapgen_factory::get_usages above as well. - if( const auto ptr = oter_mapgen.pick( "lab_1side" ) ) { - ptr->generate( dat ); + if( oter_mapgen.generate( dat, "lab_1side" ) ) { if( tw == 2 ) { rotate( 2 ); } @@ -3577,8 +3581,7 @@ void map::draw_lab( mapgendata &dat ) } else { const int hardcoded_4side_map_weight = 1500; // weight of all hardcoded maps. // If you remove the usage of "lab_4side" here, remove it from mapgen_factory::get_usages above as well. - if( const auto ptr = oter_mapgen.pick( "lab_4side", hardcoded_4side_map_weight ) ) { - ptr->generate( dat ); + if( oter_mapgen.generate( dat, "lab_4side", hardcoded_4side_map_weight ) ) { // If the map template hasn't handled borders, handle them in code. // Rotated maps cannot handle borders and have to be caught in code. // We determine if a border isn't handled by checking the east-facing @@ -4123,9 +4126,7 @@ void map::draw_lab( mapgendata &dat ) const int hardcoded_finale_map_weight = 500; // weight of all hardcoded maps. // If you remove the usage of "lab_finale_1level" here, remove it from mapgen_factory::get_usages above as well. - if( const auto ptr = oter_mapgen.pick( "lab_finale_1level", hardcoded_finale_map_weight ) ) { - ptr->generate( dat ); - + if( oter_mapgen.generate( dat, "lab_finale_1level", hardcoded_finale_map_weight ) ) { // If the map template hasn't handled borders, handle them in code. // Rotated maps cannot handle borders and have to be caught in code. // We determine if a border isn't handled by checking the east-facing @@ -7340,11 +7341,7 @@ std::pair, std::map> get_changed_ids_from_up bool run_mapgen_func( const std::string &mapgen_id, mapgendata &dat ) { - if( const auto ptr = oter_mapgen.pick( mapgen_id ) ) { - ptr->generate( dat ); - return true; - } - return false; + return oter_mapgen.generate( dat, mapgen_id ); } int register_mapgen_function( const std::string &key )