Skip to content

Commit

Permalink
Encapsulate the mapgen_function function pointers within `mapgen_fa…
Browse files Browse the repository at this point in the history
…ctory`

Don't expose them at all. All access to them is done internally.
  • Loading branch information
BevapDin committed Feb 2, 2020
1 parent 7959976 commit e925044
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mapgen_function> *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
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -313,13 +318,13 @@ class mapgen_factory
int add( const std::string &key, const std::shared_ptr<mapgen_function> 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 ) {
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -7340,11 +7341,7 @@ std::pair<std::map<ter_id, int>, std::map<furn_id, int>> 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 )
Expand Down

0 comments on commit e925044

Please sign in to comment.