Skip to content

Commit 00965e8

Browse files
authored
Weighted list improvements (#82773)
1 parent e71a6af commit 00965e8

13 files changed

+276
-178
lines changed

src/anatomy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ bodypart_id anatomy::select_body_part( int min_hit, int max_hit, bool can_attack
237237
}
238238

239239
// Debug for seeing weights.
240-
for( const weighted_object<double, bodypart_id> &pr : hit_weights ) {
241-
add_msg_debug( debugmode::DF_ANATOMY_BP, "%s = %.3f", pr.obj.obj().name, pr.weight );
240+
for( const std::pair<bodypart_id, double> &pr : hit_weights ) {
241+
add_msg_debug( debugmode::DF_ANATOMY_BP, "%s = %.3f", pr.first.obj().name, pr.second );
242242
}
243243

244244
const bodypart_id *ret = hit_weights.pick();
@@ -303,8 +303,8 @@ bodypart_id anatomy::select_blocking_part( const Creature *blocker, bool arm, bo
303303
}
304304

305305
// Debug for seeing weights.
306-
for( const weighted_object<double, bodypart_id> &pr : block_scores ) {
307-
add_msg_debug( debugmode::DF_MELEE, "%s = %.3f", pr.obj.obj().name, pr.weight );
306+
for( const std::pair<bodypart_id, double > &pr : block_scores ) {
307+
add_msg_debug( debugmode::DF_MELEE, "%s = %.3f", pr.first.obj().name, pr.second );
308308
}
309309

310310
const bodypart_id *ret = block_scores.pick();

src/cata_tiles.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,23 +1011,23 @@ void tileset_cache::loader::process_variations_after_loading(
10111011
// loop through all of the variations
10121012
for( auto &v : vs ) {
10131013
// in a given variation, erase any invalid sprite ids
1014-
v.obj.erase(
1014+
v.first.erase(
10151015
std::remove_if(
1016-
v.obj.begin(),
1017-
v.obj.end(),
1016+
v.first.begin(),
1017+
v.first.end(),
10181018
[&]( int id ) {
10191019
return id >= offset || id < 0;
10201020
} ),
1021-
v.obj.end()
1021+
v.first.end()
10221022
);
10231023
}
10241024
// erase any variations with no valid sprite ids left
10251025
vs.erase(
10261026
std::remove_if(
10271027
vs.begin(),
10281028
vs.end(),
1029-
[&]( const weighted_object<int, std::vector<int>> &o ) {
1030-
return o.obj.empty();
1029+
[&]( const std::pair<std::vector<int>, int> &o ) {
1030+
return o.first.empty();
10311031
}
10321032
),
10331033
vs.end()

src/generic_factory.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "string_formatter.h"
3737
#include "string_id.h"
3838
#include "units.h"
39+
#include "weighted_list.h"
3940

4041
class quantity;
4142

@@ -1253,6 +1254,78 @@ struct handler<std::vector<T>> {
12531254
static constexpr bool is_container = true;
12541255
};
12551256

1257+
template<typename T>
1258+
struct handler<weighted_int_list<T>> {
1259+
void clear( weighted_int_list<T> &container ) const {
1260+
container.clear();
1261+
}
1262+
bool insert( weighted_int_list<T> &container, const std::pair<T, int> &data ) const {
1263+
container.add( data );
1264+
return true;
1265+
}
1266+
bool relative( weighted_int_list<T> &, const std::pair<T, int> & ) const {
1267+
return false;
1268+
}
1269+
template<typename E>
1270+
bool erase( weighted_int_list<T> &container, const E &data ) const {
1271+
const auto pred = [&data]( const std::pair<T, int> &e ) {
1272+
return e.first == data.first;
1273+
};
1274+
if( !erase_if( container, pred ) ) {
1275+
debugmsg( "Did not remove %s in delete", data_string( data ) );
1276+
return false;
1277+
}
1278+
return true;
1279+
}
1280+
template<typename P>
1281+
bool erase_if( weighted_int_list<T> &container, P &predicate ) const {
1282+
const auto iter = std::find_if( container.begin(), container.end(), predicate );
1283+
if( iter != container.end() ) {
1284+
container.remove( iter->first );
1285+
} else {
1286+
return false;
1287+
}
1288+
return true;
1289+
}
1290+
static constexpr bool is_container = true;
1291+
};
1292+
1293+
template<typename T, typename W>
1294+
struct handler<weighted_list<T, W>> {
1295+
void clear( weighted_list<T, W> &container ) const {
1296+
container.clear();
1297+
}
1298+
bool insert( weighted_list<T, W> &container, const std::pair<T, W> &data ) const {
1299+
container.add( data );
1300+
return true;
1301+
}
1302+
bool relative( weighted_list<T, W> &, const std::pair<T, W> & ) const {
1303+
return false;
1304+
}
1305+
template<typename E>
1306+
bool erase( weighted_list<T, W> &container, const E &data ) const {
1307+
const auto pred = [&data]( const T & e ) {
1308+
return e == data;
1309+
};
1310+
if( !erase_if( container, pred ) ) {
1311+
debugmsg( "Did not remove %s in delete", data_string( data ) );
1312+
return false;
1313+
}
1314+
return true;
1315+
}
1316+
template<typename P>
1317+
bool erase_if( weighted_list<T, W> &container, P &predicate ) const {
1318+
const auto iter = std::find_if( container.begin(), container.end(), predicate );
1319+
if( iter != container.end() ) {
1320+
container.remove( *iter.obj );
1321+
} else {
1322+
return false;
1323+
}
1324+
return true;
1325+
}
1326+
static constexpr bool is_container = true;
1327+
};
1328+
12561329
template<typename Key, typename Val>
12571330
struct handler<std::map<Key, Val>> {
12581331
void clear( std::map<Key, Val> &container ) const {

src/item.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,10 +2605,10 @@ void item::debug_info( std::vector<iteminfo> &info, const iteminfo_query *parts,
26052605
}
26062606

26072607
std::string faults;
2608-
for( const weighted_object<int, fault_id> &fault : type->faults ) {
2609-
const int weight_percent = static_cast<float>( fault.weight ) / type->faults.get_weight() * 100;
2610-
faults += colorize( fault.obj.str() + string_format( " (%d, %d%%)\n", fault.weight,
2611-
weight_percent ), has_fault( fault.obj ) ? c_yellow : c_white );
2608+
for( const std::pair<fault_id, int> &fault : type->faults ) {
2609+
const int weight_percent = static_cast<float>( fault.second ) / type->faults.get_weight() * 100;
2610+
faults += colorize( fault.first.str() + string_format( " (%d, %d%%)\n", fault.second,
2611+
weight_percent ), has_fault( fault.first ) ? c_yellow : c_white );
26122612
}
26132613
info.emplace_back( "BASE", string_format( "faults: %s", faults ) );
26142614
}
@@ -7878,9 +7878,9 @@ void item::set_random_fault_of_type( const std::string &fault_type, bool force,
78787878
}
78797879

78807880
weighted_int_list<fault_id> faults_by_type;
7881-
for( const weighted_object<int, fault_id> &f : type->faults ) {
7882-
if( f.obj.obj().type() == fault_type && can_have_fault( f.obj ) ) {
7883-
faults_by_type.add( f.obj, f.weight );
7881+
for( const std::pair<fault_id, int> &f : type->faults ) {
7882+
if( f.first.obj().type() == fault_type && can_have_fault( f.first ) ) {
7883+
faults_by_type.add( f.first, f.second );
78847884
}
78857885

78867886
}
@@ -10439,18 +10439,18 @@ int item::wind_resist() const
1043910439
std::set<fault_id> item::faults_potential() const
1044010440
{
1044110441
std::set<fault_id> res;
10442-
for( const weighted_object<int, fault_id> &fault_pair : type->faults ) {
10443-
res.insert( fault_pair.obj );
10442+
for( const std::pair<fault_id, int> &fault_pair : type->faults ) {
10443+
res.insert( fault_pair.first );
1044410444
}
1044510445
return res;
1044610446
}
1044710447

1044810448
std::set<fault_id> item::faults_potential_of_type( const std::string &fault_type ) const
1044910449
{
1045010450
std::set<fault_id> res;
10451-
for( const weighted_object<int, fault_id> &some_fault : type->faults ) {
10452-
if( some_fault.obj->type() == fault_type ) {
10453-
res.emplace( some_fault.obj );
10451+
for( const std::pair<fault_id, int> &some_fault : type->faults ) {
10452+
if( some_fault.first->type() == fault_type ) {
10453+
res.emplace( some_fault.first );
1045410454
}
1045510455
}
1045610456
return res;

src/item_factory.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,13 @@ void Item_factory::finalize_post( itype &obj )
925925
const int weight_add = std::get<1>( fault_groups );
926926
const float weight_mult = std::get<2>( fault_groups );
927927
for( const auto &id_and_weight : fault_g.obj().get_weighted_list() ) {
928-
obj.faults.add_or_replace( id_and_weight.obj, ( id_and_weight.weight + weight_add ) * weight_mult );
928+
obj.faults.add_or_replace( id_and_weight.first,
929+
( id_and_weight.second + weight_add ) * weight_mult );
929930
}
930931
} else {
931932
// weight_override is not -1, override the weight
932-
for( const weighted_object<int, fault_id> &id_and_weight : fault_g.obj().get_weighted_list() ) {
933-
obj.faults.add( id_and_weight.obj, std::get<0>( fault_groups ) );
933+
for( const std::pair<fault_id, int> &id_and_weight : fault_g.obj().get_weighted_list() ) {
934+
obj.faults.add( id_and_weight.first, std::get<0>( fault_groups ) );
934935
}
935936
}
936937
}
@@ -2466,8 +2467,8 @@ void Item_factory::check_definitions() const
24662467
}
24672468

24682469
for( const auto &f : type->faults ) {
2469-
if( !f.obj.is_valid() ) {
2470-
msg += string_format( "invalid item fault %s\n", f.obj.c_str() );
2470+
if( !f.first.is_valid() ) {
2471+
msg += string_format( "invalid item fault %s\n", f.first.c_str() );
24712472
}
24722473
}
24732474

src/magic_ter_fur_transform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void load_transform_results( const JsonObject &jsi, const std::string &js
7272
list.add( T( jsi.get_string( json_key ) ), 1 );
7373
return;
7474
}
75-
load_weighted_list( jsi.get_member( json_key ), list, 1 );
75+
list.deserialize( jsi.get_member( json_key ) );
7676
}
7777

7878
void ter_furn_transform::reset()

0 commit comments

Comments
 (0)