Skip to content

Commit

Permalink
Use weighted list for nested mapgen with same id
Browse files Browse the repository at this point in the history
  • Loading branch information
ralreegorganon committed Apr 13, 2020
1 parent 141c18d commit 65f816c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ static const trait_id trait_ASTHMA( "ASTHMA" );

class vehicle;

extern std::map<std::string, std::vector<std::unique_ptr<mapgen_function_json_nested>>>
nested_mapgen;
extern std::map<std::string, weighted_int_list<std::shared_ptr<mapgen_function_json_nested>> >
nested_mapgen;

#if defined(TILES)
#include "sdl_wrappers.h"
Expand Down Expand Up @@ -416,8 +416,11 @@ void spawn_nested_mapgen()
target_map.load( abs_sub, true );
const tripoint local_ms = target_map.getlocal( abs_ms );
mapgendata md( abs_omt, target_map, 0.0f, calendar::turn, nullptr );
const auto &ptr = random_entry_ref( nested_mapgen[nest_str[nest_choice]] );
ptr->nest( md, local_ms.xy() );
const auto &ptr = nested_mapgen[nest_str[nest_choice]].pick();
if( ptr == nullptr ) {
return;
}
( *ptr )->nest( md, local_ms.xy() );
target_map.save();
g->load_npcs();
g->m.invalidate_map_cache( g->get_levz() );
Expand Down
21 changes: 11 additions & 10 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ static mapgen_factory oter_mapgen;
/*
* stores function ref and/or required data
*/
std::map<std::string, std::vector<std::unique_ptr<mapgen_function_json_nested>> > nested_mapgen;
std::map<std::string, weighted_int_list<std::shared_ptr<mapgen_function_json_nested>> >
nested_mapgen;
std::map<std::string, std::vector<std::unique_ptr<update_mapgen_function_json>> > update_mapgen;

/*
Expand All @@ -365,8 +366,8 @@ void calculate_mapgen_weights() // TODO: rename as it runs jsonfunction setup
oter_mapgen.setup();
// Not really calculate weights, but let's keep it here for now
for( auto &pr : nested_mapgen ) {
for( std::unique_ptr<mapgen_function_json_nested> &ptr : pr.second ) {
ptr->setup();
for( weighted_object<int, std::shared_ptr<mapgen_function_json_nested>> &ptr : pr.second ) {
ptr.obj->setup();
}
}
for( auto &pr : update_mapgen ) {
Expand All @@ -382,7 +383,7 @@ void check_mapgen_definitions()
oter_mapgen.check_consistency();
for( auto &oter_definition : nested_mapgen ) {
for( auto &mapgen_function_ptr : oter_definition.second ) {
mapgen_function_ptr->check( oter_definition.first );
mapgen_function_ptr.obj->check( oter_definition.first );
}
}
for( auto &oter_definition : update_mapgen ) {
Expand Down Expand Up @@ -451,10 +452,10 @@ static void load_nested_mapgen( const JsonObject &jio, const std::string &id_bas
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "json" ) {
if( jio.has_object( "object" ) ) {
int weight = jio.get_int( "weight", 1000 );
JsonObject jo = jio.get_object( "object" );
std::string jstr = jo.str();
nested_mapgen[id_base].push_back(
std::make_unique<mapgen_function_json_nested>( jstr ) );
nested_mapgen[id_base].add( std::make_shared<mapgen_function_json_nested>( jstr ), weight );
} else {
debugmsg( "Nested mapgen: Invalid mapgen function (missing \"object\" object)", id_base.c_str() );
}
Expand Down Expand Up @@ -1829,12 +1830,12 @@ class jmapgen_nested : public jmapgen_piece
}

// A second roll? Let's allow it for now
const auto &ptr = random_entry_ref( iter->second );
const auto &ptr = iter->second.pick();
if( ptr == nullptr ) {
return;
}

ptr->nest( dat, point( x.get(), y.get() ) );
( *ptr )->nest( dat, point( x.get(), y.get() ) );
}
bool has_vehicle_collision( mapgendata &dat, int x, int y ) const override {
const weighted_int_list<std::string> &selected_entries = neighbors.test(
Expand All @@ -1851,8 +1852,8 @@ class jmapgen_nested : public jmapgen_piece
if( iter == nested_mapgen.end() ) {
return false;
}
for( auto &nest : iter->second ) {
if( nest->has_vehicle_collision( dat, {x, y} ) ) {
for( const auto &nest : iter->second ) {
if( nest.obj->has_vehicle_collision( dat, { x, y } ) ) {
return true;
}
}
Expand Down

0 comments on commit 65f816c

Please sign in to comment.