Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions engine/player/unique_gear_midnight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,10 +1039,10 @@ void blood( special_effect_t& effect )

void execute( const spell_data_t*, player_t*, action_state_t* ) override
{
auto stats = util::lowest_stats( listener, secondary_ratings );
auto stat = util::find_stat( listener, secondary_ratings, std::less_equal() );
for ( auto [ s, b ] : buffs )
{
if ( std::find( stats.begin(), stats.end(), s ) != stats.end() )
if ( stat == s )
b->trigger();
else
b->expire();
Expand Down
52 changes: 4 additions & 48 deletions engine/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,66 +315,22 @@ double util::stat_value( const player_t* p, stat_e stat )
// "normal" player-driven stuff.
stat_e util::highest_stat( const player_t* p, util::span<const stat_e> stats )
{
assert( !stats.empty() );

stat_e stat = stats.front();
double value = stat_value( p, stat );
for ( stat_e s : stats.subspan( 1 ) )
{
const double v = stat_value( p, s );
if ( value < v )
{
stat = s;
value = v;
}
}

return stat;
return util::find_stat( p, stats, std::greater() );
}

std::vector<stat_e> util::highest_stats( const player_t* p, util::span<const stat_e> stats )
{
double value = stat_value( p, highest_stat( p, stats ) );

std::vector<stat_e> vec;
vec.reserve( stats.size() );
for ( const stat_e stat : stats )
if ( value == stat_value( p, stat ) )
vec.emplace_back( stat );

return vec;
return util::find_stats( p, stats, std::greater() );
}

stat_e util::lowest_stat( const player_t* p, util::span<const stat_e> stats )
{
assert( !stats.empty() );

stat_e stat = stats.front();
double value = stat_value( p, stat );
for ( stat_e s : stats.subspan( 1 ) )
{
const double v = stat_value( p, s );
if ( value > v )
{
stat = s;
value = v;
}
}

return stat;
return util::find_stat( p, stats, std::less() );
}

std::vector<stat_e> util::lowest_stats( const player_t* p, util::span<const stat_e> stats )
{
double value = stat_value( p, lowest_stat( p, stats ) );

std::vector<stat_e> vec;
vec.reserve( stats.size() );
for ( const stat_e stat : stats )
if ( value == stat_value( p, stat ) )
vec.emplace_back( stat );

return vec;
return util::find_stats( p, stats, std::less() );
}

/// case-insensitive string comparison
Expand Down
35 changes: 35 additions & 0 deletions engine/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ SC_EXCEPTION( sc_invalid_item_string, 82, "Invalid item string" );
namespace util
{
double stat_value( const player_t* p, stat_e stat );

template <typename Cmp>
stat_e find_stat( const player_t* p, util::span<const stat_e> stats, Cmp cmp )
{
assert( !stats.empty() );

stat_e stat = stats.front();
double value = stat_value( p, stat );
for ( const stat_e s : stats.subspan( 1 ) )
{
const double v = stat_value( p, s );
if ( cmp( v, value ) )
{
stat = s;
value = v;
}
}

return stat;
}

template <typename Cmp>
std::vector<stat_e> find_stats( const player_t* p, util::span<const stat_e> stats, Cmp cmp )
{
double value = stat_value( p, find_stat( p, stats, cmp ) );

std::vector<stat_e> vec;
vec.reserve( stats.size() );
for ( const stat_e stat : stats )
if ( value == stat_value( p, stat ) )
vec.emplace_back( stat );

return vec;
}

stat_e highest_stat( const player_t* p, util::span<const stat_e> stat );
std::vector<stat_e> highest_stats( const player_t* p, util::span<const stat_e> stat );
stat_e lowest_stat( const player_t* p, util::span<const stat_e> stat );
Expand Down
Loading