Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine last_rot_check and last_temp_check variables #38370

Closed
wants to merge 25 commits into from
Closed
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
19 changes: 10 additions & 9 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,9 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
item obj( drop, calendar::turn, roll );
if( obj.has_temperature() ) {
obj.set_item_temperature( 0.00001 * corpse_item->temperature );
}
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
}
}
for( const std::string &flg : entry.flags ) {
obj.set_flag( flg );
Expand All @@ -1067,9 +1067,9 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
item obj( drop, calendar::turn, roll );
if( obj.has_temperature() ) {
obj.set_item_temperature( 0.00001 * corpse_item->temperature );
}
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
}
}
for( const std::string &flg : entry.flags ) {
obj.set_flag( flg );
Expand All @@ -1086,9 +1086,9 @@ static void butchery_drops_harvest( item *corpse_item, const mtype &mt, player &
obj.set_mtype( &mt );
if( obj.has_temperature() ) {
obj.set_item_temperature( 0.00001 * corpse_item->temperature );
}
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
if( obj.goes_bad() ) {
obj.set_rot( corpse_item->get_rot() );
}
}
for( const std::string &flg : entry.flags ) {
obj.set_flag( flg );
Expand Down Expand Up @@ -1572,6 +1572,7 @@ void activity_handlers::fill_liquid_do_turn( player_activity *act, player *p )
if( liquid.has_temperature() && liquid.specific_energy < 0 ) {
liquid.set_item_temperature( std::max( temp_to_kelvin( g->weather.get_temperature( p->pos() ) ),
277.15 ) );
liquid.reset_temp_check();
}

// 2. Transfer charges.
Expand Down
19 changes: 11 additions & 8 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,11 +1179,13 @@ void player::complete_craft( item &craft, const tripoint &loc )
newit_counter++;
}

if( food_contained.goes_bad() ) {
food_contained.set_relative_rot( relative_rot );
}


if( food_contained.has_temperature() ) {
if( food_contained.goes_bad() ) {
food_contained.set_relative_rot( relative_rot );
}
food_contained.reset_temp_check();
if( should_heat ) {
food_contained.heat_up();
} else {
Expand All @@ -1195,7 +1197,6 @@ void player::complete_craft( item &craft, const tripoint &loc )
//
// Temperature is not functional for non-foods
food_contained.set_item_temperature( 293.15 );
food_contained.reset_temp_check();
}
}

Expand All @@ -1217,15 +1218,16 @@ void player::complete_craft( item &craft, const tripoint &loc )
if( making.has_byproducts() ) {
std::vector<item> bps = making.create_byproducts( batch_size );
for( auto &bp : bps ) {
if( bp.goes_bad() ) {
bp.set_relative_rot( relative_rot );
}

if( bp.has_temperature() ) {
if( bp.goes_bad() ) {
bp.set_relative_rot( relative_rot );
}
bp.reset_temp_check();
if( should_heat ) {
bp.heat_up();
} else {
bp.set_item_temperature( 293.15 );
bp.reset_temp_check();
}
}
bp.set_owner( get_faction()->id );
Expand Down Expand Up @@ -2265,6 +2267,7 @@ void player::complete_disassemble( item_location &target, const recipe &dis )

if( act_item.has_temperature() ) {
act_item.set_item_temperature( temp_to_kelvin( g->weather.get_temperature( loc ) ) );
act_item.reset_temp_check();
}

// Refitted clothing disassembles into refitted components (when applicable)
Expand Down
87 changes: 30 additions & 57 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ item::item( const itype *type, time_point turn, int qty ) : type( type ), bday(
} else if( has_temperature() || goes_bad() ) {
active = true;
last_temp_check = bday;
last_rot_check = bday;

} else if( type->tool ) {
if( ammo_remaining() && !ammo_types().empty() ) {
Expand Down Expand Up @@ -428,7 +427,6 @@ item::item( const recipe *rec, int qty, std::list<item> items, std::vector<item_
if( is_food() ) {
active = true;
last_temp_check = bday;
last_rot_check = bday;
if( goes_bad() ) {
const item *most_rotten = get_most_rotten_component( *this );
if( most_rotten ) {
Expand Down Expand Up @@ -1451,14 +1449,11 @@ void item::basic_info( std::vector<iteminfo> &info, const iteminfo_query *parts,
info.push_back( iteminfo( "BASE", space + _( "max rot (turns): " ),
"", iteminfo::lower_is_better,
to_turns<int>( food->get_shelf_life() ) ) );
info.push_back( iteminfo( "BASE", _( "last rot: " ),
"", iteminfo::lower_is_better,
to_turn<int>( food->last_rot_check ) ) );
}
if( food && food->has_temperature() ) {
info.push_back( iteminfo( "BASE", _( "last temp: " ),
"", iteminfo::lower_is_better,
to_turn<int>( food->last_temp_check ) ) );
}
if( food && food->has_temperature() ) {
info.push_back( iteminfo( "BASE", _( "Temp: " ), "", iteminfo::lower_is_better,
food->temperature ) );
info.push_back( iteminfo( "BASE", _( "Spec ener: " ), "",
Expand Down Expand Up @@ -4944,12 +4939,6 @@ void item::set_relative_rot( double val )
{
if( goes_bad() ) {
rot = get_shelf_life() * val;
// calc_rot uses last_rot_check (when it's not turn_zero) instead of bday.
// this makes sure the rotting starts from now, not from bday.
// if this item is the result of smoking or milling don't do this, we want to start from bday.
if( !has_flag( flag_PROCESSING_RESULT ) ) {
last_rot_check = calendar::turn;
}
}
}

Expand Down Expand Up @@ -5054,20 +5043,15 @@ int get_hourly_rotpoints_at_temp( const int temp )

void item::calc_rot( time_point time, int temp )
{
if( !goes_bad() ) {
return;
}
// Avoid needlessly calculating already rotten things. Corpses should
// always rot away and food rots away at twice the shelf life. If the food
// is in a sealed container they won't rot away, this avoids needlessly
// calculating their rot in that case.
if( !is_corpse() && get_relative_rot() > 2.0 ) {
last_rot_check = time;
return;
}

if( item_tags.count( "FROZEN" ) ) {
last_rot_check = time;
return;
}
// rot modifier
Expand All @@ -5087,14 +5071,13 @@ void item::calc_rot( time_point time, int temp )
// conditions by applying starting variation bonus/penalty of +/- 20% of base shelf-life
// positive = food was produced some time before calendar::start and/or bad storage
// negative = food was stored in good conditions before calendar::start
if( last_rot_check <= calendar::start_of_cataclysm ) {
if( last_temp_check <= calendar::start_of_cataclysm ) {
time_duration spoil_variation = get_shelf_life() * 0.2f;
rot += rng( -spoil_variation, spoil_variation );
}

time_duration time_delta = time - last_rot_check;
time_duration time_delta = time - last_temp_check;
rot += factor * time_delta / 1_hours * get_hourly_rotpoints_at_temp( temp ) * 1_turns;
last_rot_check = time;
}

void item::calc_rot_while_processing( time_duration processing_duration )
Expand All @@ -5105,7 +5088,6 @@ void item::calc_rot_while_processing( time_duration processing_duration )
}

// Apply no rot or temperature while smoking
last_rot_check += processing_duration;
last_temp_check += processing_duration;
}

Expand Down Expand Up @@ -8069,7 +8051,6 @@ void item::set_item_temperature( float new_temperature )
} else if( new_temperature < temp_to_kelvin( temperatures::cold ) ) {
item_tags.insert( "COLD" );
}
reset_temp_check();
}

void item::fill_with( item &liquid, int amount )
Expand Down Expand Up @@ -8469,17 +8450,9 @@ void item::process_temperature_rot( float insulation, const tripoint &pos,
const time_point now = calendar::turn;

// if player debug menu'd the time backward it breaks stuff, just reset the
// last_temp_check and last_rot_check in this case
// last_temp_check in this case
if( now - last_temp_check < 0_turns ) {
reset_temp_check();
last_rot_check = now;
return;
}

// process temperature and rot at most once every 100_turns (10 min)
// note we're also gated by item::processing_speed
time_duration smallest_interval = 10_minutes;
if( now - last_temp_check < smallest_interval && specific_energy > 0 ) {
return;
}

Expand Down Expand Up @@ -8512,14 +8485,13 @@ void item::process_temperature_rot( float insulation, const tripoint &pos,
temp += 5;
}

time_point time;
const bool preserved = type->container && type->container->preserves;

item_internal::scoped_goes_bad_cache _( this );
if( goes_bad() ) {
time = std::min( last_rot_check, last_temp_check );
} else {
time = last_temp_check;
}
bool process_rot = goes_bad() && !preserved;
const time_duration smallest_interval = 10_minutes;

time_point time = last_temp_check;
if( now - time > 1_hours ) {
// This code is for items that were left out of reality bubble for long time

Expand All @@ -8542,7 +8514,7 @@ void item::process_temperature_rot( float insulation, const tripoint &pos,
}

// Process the past of this item since the last time it was processed
while( time < now - 1_hours ) {
while( now - time > 1_hours ) {
// Get the environment temperature
time_duration time_delta = std::min( 1_hours, now - 1_hours - time );
time += time_delta;
Expand Down Expand Up @@ -8581,34 +8553,40 @@ void item::process_temperature_rot( float insulation, const tripoint &pos,
if( now - time > 2_days ) {
// This value shouldn't be there anymore after the loop is done so we don't bother with the set_item_temperature()
temperature = static_cast<int>( 100000 * temp_to_kelvin( env_temperature ) );
last_temp_check = time;
} else if( time - last_temp_check > smallest_interval ) {
} else {
calc_temp( env_temperature, insulation, time );
}

// Calculate item rot from item temperature
if( time - last_rot_check > smallest_interval ) {
if( process_rot ) {
calc_rot( time, env_temperature );

if( has_rotten_away() || ( is_corpse() && rot > 10_days ) ) {
// No need to track item that will be gone
last_temp_check = time;
return;
}
}

last_temp_check = time;
}
}

// Remaining <1 h from above
// and items that are held near the player
if( now - time > smallest_interval ) {
calc_temp( temp, insulation, now );
calc_rot( now, temp );
if( process_rot ) {
calc_rot( now, temp );
}
last_temp_check = now;
return;
}

// Just now created items will get here.
if( specific_energy < 0 ) {
set_item_temperature( temp_to_kelvin( temp ) );
last_temp_check = now;
}
}

Expand All @@ -8621,15 +8599,13 @@ void item::calc_temp( const int temp, const float insulation, const time_point &

// If no or only small temperature difference then no need to do math.
if( std::abs( temperature_difference ) < 0.9 ) {
last_temp_check = time;
return;
}
const float mass = to_gram( weight() ); // g

// If item has negative energy set to environment temperature (it not been processed ever)
if( specific_energy < 0 ) {
set_item_temperature( env_temperature );
last_temp_check = time;
return;
}

Expand Down Expand Up @@ -8679,7 +8655,6 @@ void item::calc_temp( const int temp, const float insulation, const time_point &
// This may happen rarely with very small items
// Just set the item to environment temperature
set_item_temperature( env_temperature );
last_temp_check = time;
return;
}
}
Expand Down Expand Up @@ -8708,7 +8683,6 @@ void item::calc_temp( const int temp, const float insulation, const time_point &
// This may happen rarely with very small items
// Just set the item to environment temperature
set_item_temperature( env_temperature );
last_temp_check = time;
return;
}
}
Expand Down Expand Up @@ -8789,8 +8763,6 @@ void item::calc_temp( const int temp, const float insulation, const time_point &
}
temperature = lround( 100000 * new_item_temperature );
specific_energy = lround( 100000 * new_specific_energy );

last_temp_check = time;
}

float item::get_item_thermal_energy()
Expand Down Expand Up @@ -9274,13 +9246,7 @@ bool item::process_blackpowder_fouling( player *carrier )
bool item::process( player *carrier, const tripoint &pos, bool activate,
float insulation, const temperature_flag flag )
{
const bool preserves = type->container && type->container->preserves;
for( auto it = contents.begin(); it != contents.end(); ) {
if( preserves ) {
// Simulate that the item has already "rotten" up to last_rot_check, but as item::rot
// is not changed, the item is still fresh.
it->last_rot_check = calendar::turn;
}
if( it->process( carrier, pos, activate, type->insulation_factor * insulation, flag ) ) {
it = contents.erase( it );
} else {
Expand Down Expand Up @@ -9368,6 +9334,13 @@ bool item::process( player *carrier, const tripoint &pos, bool activate,
}
// All foods that go bad have temperature
if( has_temperature() ) {
// process temperature and rot at most once every 9_minutes
// note we're also gated by item::processing_speed
const time_point now = calendar::turn;
const time_duration smallest_interval = 9_minutes;
if( now - last_temp_check < smallest_interval && specific_energy > 0 ) {
return false;
}
process_temperature_rot( insulation, pos, carrier, flag );
}

Expand Down Expand Up @@ -9684,8 +9657,8 @@ void item::legacy_fast_forward_time()

rot *= 6;

const time_duration tmp_rot = ( last_rot_check - calendar::turn_zero ) * 6;
last_rot_check = calendar::turn_zero + tmp_rot;
const time_duration tmp_temp = ( last_temp_check - calendar::turn_zero ) * 6;
last_temp_check = calendar::turn_zero + tmp_temp;
}

time_point item::birthday() const
Expand Down
2 changes: 0 additions & 2 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2178,8 +2178,6 @@ class item : public visitable<item>
* the item is rotten.
*/
time_duration rot = 0_turns;
/** Time when the rot calculation was last performed. */
time_point last_rot_check = calendar::turn_zero;
/** the last time the temperature was updated for this item */
time_point last_temp_check = calendar::turn_zero;
/// The time the item was created.
Expand Down
1 change: 0 additions & 1 deletion src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,6 @@ void item::io( Archive &archive )
archive.io( "is_favorite", is_favorite, false );
archive.io( "item_counter", item_counter, static_cast<decltype( item_counter )>( 0 ) );
archive.io( "rot", rot, 0_turns );
archive.io( "last_rot_check", last_rot_check, calendar::start_of_cataclysm );
archive.io( "last_temp_check", last_temp_check, calendar::start_of_cataclysm );
archive.io( "current_phase", cur_phase, static_cast<int>( type->phase ) );
archive.io( "techniques", techniques, io::empty_default_tag() );
Expand Down