Skip to content

Commit af05c7e

Browse files
committed
Refactor effect parameter updating
1 parent 462ec86 commit af05c7e

File tree

4 files changed

+49
-71
lines changed

4 files changed

+49
-71
lines changed

src/creature.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,14 +1818,6 @@ void Creature::add_effect( const effect_source &source, const efftype_id &eff_id
18181818
const int prev_int = e.get_intensity();
18191819
// If we do, mod the duration, factoring in the mod value
18201820
e.mod_duration( dur * e.get_dur_add_perc() / 100 );
1821-
// Limit to max duration
1822-
if( e.get_duration() > e.get_max_duration() ) {
1823-
e.set_duration( e.get_max_duration() );
1824-
}
1825-
// Adding a permanent effect makes it permanent
1826-
if( e.is_permanent() ) {
1827-
e.pause_effect();
1828-
}
18291821
// int_dur_factor overrides all other intensity settings
18301822
// ...but it's handled in set_duration, so explicitly do nothing here
18311823
if( e.get_int_dur_factor() > 0_turns ) {
@@ -1836,14 +1828,8 @@ void Creature::add_effect( const effect_source &source, const efftype_id &eff_id
18361828
} else if( e.get_int_add_val() != 0 ) {
18371829
e.mod_intensity( e.get_int_add_val(), is_avatar() );
18381830
}
1839-
1840-
// Bound intensity by [1, max intensity]
1841-
if( e.get_intensity() < 1 ) {
1842-
add_msg_debug( debugmode::DF_CREATURE, "Bad intensity, ID: %s", e.get_id().c_str() );
1843-
e.set_intensity( 1 );
1844-
} else if( e.get_intensity() > e.get_max_intensity() ) {
1845-
e.set_intensity( e.get_max_intensity() );
1846-
}
1831+
// Bound new effect intensity by [1, max intensity]
1832+
e.clamp_intensity();
18471833
if( e.get_intensity() != prev_int ) {
18481834
on_effect_int_change( eff_id, e.get_intensity(), bp );
18491835
}
@@ -1855,23 +1841,7 @@ void Creature::add_effect( const effect_source &source, const efftype_id &eff_id
18551841

18561842
// Now we can make the new effect for application
18571843
effect e( effect_source( source ), &type, dur, bp.id(), permanent, intensity, calendar::turn );
1858-
// Bound to max duration
1859-
if( e.get_duration() > e.get_max_duration() ) {
1860-
e.set_duration( e.get_max_duration() );
1861-
}
18621844

1863-
// Force intensity if it is duration based
1864-
if( e.get_int_dur_factor() != 0_turns ) {
1865-
const int intensity = std::ceil( e.get_duration() / e.get_int_dur_factor() );
1866-
e.set_intensity( std::max( 1, intensity ) );
1867-
}
1868-
// Bound new effect intensity by [1, max intensity]
1869-
if( e.get_intensity() < 1 ) {
1870-
add_msg_debug( debugmode::DF_CREATURE, "Bad intensity, ID: %s", e.get_id().c_str() );
1871-
e.set_intensity( 1 );
1872-
} else if( e.get_intensity() > e.get_max_intensity() ) {
1873-
e.set_intensity( e.get_max_intensity() );
1874-
}
18751845
( *effects )[eff_id][bp] = e;
18761846
if( Character *ch = as_character() ) {
18771847
get_event_bus().send<event_type::character_gains_effect>( ch->getID(), bp.id(), eff_id, intensity );

src/effect.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,16 +1081,10 @@ time_duration effect::get_max_duration() const
10811081
void effect::set_duration( const time_duration &dur, bool alert )
10821082
{
10831083
duration = dur;
1084-
// Cap to max_duration
1085-
if( duration > eff_type->max_duration ) {
1086-
duration = eff_type->max_duration;
1087-
}
10881084

1089-
// Force intensity if it is duration based
1090-
if( eff_type->int_dur_factor != 0_turns ) {
1091-
const int intensity = std::ceil( duration / eff_type->int_dur_factor );
1092-
set_intensity( std::max( 1, intensity ), alert );
1093-
}
1085+
clamp_duration();
1086+
1087+
apply_int_dur_factor( alert );
10941088

10951089
add_msg_debug( debugmode::DF_EFFECT, "ID: %s, Duration %s", get_id().c_str(),
10961090
to_string_writable( duration ) );
@@ -1103,6 +1097,11 @@ void effect::mult_duration( double dur, bool alert )
11031097
{
11041098
set_duration( duration * dur, alert );
11051099
}
1100+
void effect::clamp_duration()
1101+
{
1102+
duration = std::min( duration, eff_type->max_duration );
1103+
1104+
}
11061105

11071106
static int cap_to_size( const int max, int attempt )
11081107
{
@@ -1213,13 +1212,9 @@ int effect::get_effective_intensity() const
12131212

12141213
int effect::set_intensity( int val, bool alert )
12151214
{
1216-
if( intensity < 1 ) {
1217-
// Fix bad intensity
1218-
add_msg_debug( debugmode::DF_EFFECT, "Bad intensity, ID: %s", get_id().c_str() );
1219-
intensity = 1;
1220-
}
1215+
clamp_intensity();
12211216

1222-
val = std::max( std::min( val, eff_type->max_intensity ), 0 );
1217+
val = std::clamp( val, 0, eff_type->max_intensity );
12231218
if( val == intensity ) {
12241219
// Nothing to change
12251220
return intensity;
@@ -1243,11 +1238,31 @@ int effect::set_intensity( int val, bool alert )
12431238
return intensity;
12441239
}
12451240

1241+
int effect::clamp_intensity()
1242+
{
1243+
if( intensity < 1 ) {
1244+
add_msg_debug( debugmode::DF_CREATURE, "Bad intensity, ID: %s", eff_type->id.c_str() );
1245+
intensity = 1;
1246+
} else if( intensity > eff_type->max_intensity ) {
1247+
intensity = eff_type->max_intensity;
1248+
}
1249+
return intensity;
1250+
}
1251+
12461252
int effect::mod_intensity( int mod, bool alert )
12471253
{
12481254
return set_intensity( intensity + mod, alert );
12491255
}
12501256

1257+
int effect::apply_int_dur_factor( bool alert )
1258+
{
1259+
if( eff_type->int_dur_factor != 0_turns ) {
1260+
const int new_intensity = std::ceil( duration / eff_type->int_dur_factor );
1261+
set_intensity( std::max( 1, new_intensity ), alert );
1262+
}
1263+
return intensity;
1264+
}
1265+
12511266
const std::vector<trait_id> &effect::get_resist_traits() const
12521267
{
12531268
return eff_type->resist_traits;

src/effect.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ class effect
277277
eff_type( peff_type ), duration( dur ), bp( part ),
278278
permanent( perm ), intensity( nintensity ), start_time( nstart_time ),
279279
source( source ) {
280+
clamp_duration();
281+
apply_int_dur_factor();
282+
clamp_intensity();
280283
}
281284
effect( const effect & ) = default;
282285
effect &operator=( const effect & ) = default;
@@ -318,6 +321,8 @@ class effect
318321
void mod_duration( const time_duration &dur, bool alert = false );
319322
/** Multiplies the duration, capping at max_duration. */
320323
void mult_duration( double dur, bool alert = false );
324+
/** Caps duration at max_duration. */
325+
void clamp_duration();
321326

322327
std::vector<vitamin_applied_effect> vit_effects( bool reduced ) const;
323328

@@ -353,6 +358,12 @@ class effect
353358
*/
354359
int set_intensity( int val, bool alert = false );
355360

361+
/**
362+
* Clamps intensity of effect to range [1..max_intensity]
363+
* @return new clamped intensity of the effect
364+
*/
365+
int clamp_intensity();
366+
356367
/**
357368
* Modify intensity of effect capped by range [1..max_intensity]
358369
* @param mod Amount to increase current intensity by
@@ -361,6 +372,12 @@ class effect
361372
*/
362373
int mod_intensity( int mod, bool alert = false );
363374

375+
/**
376+
* Set intensity of effect if it is duration based
377+
* @return potentially updated intensity of the effect
378+
*/
379+
int apply_int_dur_factor( bool alert = false );
380+
364381
/** Returns the string id of the resist trait to be used in has_trait("id"). */
365382
const std::vector<trait_id> &get_resist_traits() const;
366383
/** Returns the string id of the resist effect to be used in has_effect("id"). */

src/vehicle.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,14 +1579,6 @@ void vehicle::add_effect( const effect_source &source, const efftype_id &eff_id,
15791579
effect &e = found_effect->second;
15801580
// If we do, mod the duration, factoring in the mod value
15811581
e.mod_duration( dur * e.get_dur_add_perc() / 100 );
1582-
// Limit to max duration
1583-
if( e.get_duration() > e.get_max_duration() ) {
1584-
e.set_duration( e.get_max_duration() );
1585-
}
1586-
// Adding a permanent effect makes it permanent
1587-
if( e.is_permanent() ) {
1588-
e.pause_effect();
1589-
}
15901582
}
15911583

15921584
if( !found ) {
@@ -1595,23 +1587,7 @@ void vehicle::add_effect( const effect_source &source, const efftype_id &eff_id,
15951587
// Now we can make the new effect for application
15961588
effect e( effect_source( source ), &type, dur, bodypart_str_id::NULL_ID(), permanent, intensity,
15971589
calendar::turn );
1598-
// Bound to max duration
1599-
if( e.get_duration() > e.get_max_duration() ) {
1600-
e.set_duration( e.get_max_duration() );
1601-
}
16021590

1603-
// Force intensity if it is duration based
1604-
if( e.get_int_dur_factor() != 0_turns ) {
1605-
const int intensity = std::ceil( e.get_duration() / e.get_int_dur_factor() );
1606-
e.set_intensity( std::max( 1, intensity ) );
1607-
}
1608-
// Bound new effect intensity by [1, max intensity]
1609-
if( e.get_intensity() < 1 ) {
1610-
add_msg_debug( debugmode::DF_CREATURE, "Bad intensity, ID: %s", e.get_id().c_str() );
1611-
e.set_intensity( 1 );
1612-
} else if( e.get_intensity() > e.get_max_intensity() ) {
1613-
e.set_intensity( e.get_max_intensity() );
1614-
}
16151591
effects[eff_id] = e;
16161592
}
16171593
}

0 commit comments

Comments
 (0)