Skip to content

Commit

Permalink
Make time_point( int ) constructor explicit
Browse files Browse the repository at this point in the history
Resolves a TODO on that constructor, and one more TODO elsewhere due to
the resulting refactoring.

time_point was implicitly convertible from int.  This led to unclear
code.  In particular, the item constructor took a time_point and a
quantity, and these two ints appeared adjacent with no obvious way to
see what they meant at call sites.

Make the constructor explicit, so the conversion is no longer implicit.
Update the rest of the code accordingly.

In most cases switched from 0 to calendar::turn_zero, which is
equivalent.  In a couple of places, used start_of_cataclysm instead,
when it seemed more appropriate (such as birthday of toilet water).
It's possible there are more cases which should have been something
other than turn_zero.
  • Loading branch information
jbytheway authored and kevingranade committed Nov 10, 2020
1 parent e884feb commit b285a13
Show file tree
Hide file tree
Showing 62 changed files with 275 additions and 249 deletions.
6 changes: 3 additions & 3 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,11 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
if( vp ) {
const int veh_battery = vp->vehicle().fuel_left( itype_battery, true );

item welder( itype_welder, 0 );
item welder( itype_welder, calendar::turn_zero );
welder.charges = veh_battery;
welder.set_flag( flag_PSEUDO );
temp_inv.add_item( welder );
item soldering_iron( itype_soldering_iron, 0 );
item soldering_iron( itype_soldering_iron, calendar::turn_zero );
soldering_iron.charges = veh_battery;
soldering_iron.set_flag( flag_PSEUDO );
temp_inv.add_item( soldering_iron );
Expand Down Expand Up @@ -1597,7 +1597,7 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
for( auto it = requirement_map.begin(); it != requirement_map.end(); ) {
tripoint pos_here = std::get<0>( *it );
itype_id item_here = std::get<1>( *it );
item test_item = item( item_here, 0 );
item test_item = item( item_here, calendar::turn_zero );
if( test_item.has_quality( tool_qual, qual_level ) ) {
// it's just this spot that can fulfil the requirement on its own
final_map.push_back( std::make_tuple( pos_here, item_here, 1 ) );
Expand Down
6 changes: 3 additions & 3 deletions src/basecamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void basecamp::add_resource( const itype_id &camp_resource )
{
basecamp_resource bcp_r;
bcp_r.fake_id = camp_resource;
item camp_item( bcp_r.fake_id, 0 );
item camp_item( bcp_r.fake_id, calendar::turn_zero );
bcp_r.ammo_id = camp_item.ammo_default();
resources.emplace_back( bcp_r );
fuel_types.insert( bcp_r.ammo_id );
Expand Down Expand Up @@ -589,7 +589,7 @@ std::list<item> basecamp::use_charges( const itype_id &fake_id, int &quantity )
}
for( basecamp_resource &bcp_r : resources ) {
if( bcp_r.fake_id == fake_id ) {
item camp_item( bcp_r.fake_id, 0 );
item camp_item( bcp_r.fake_id, calendar::turn_zero );
camp_item.charges = std::min( bcp_r.available, quantity );
quantity -= camp_item.charges;
bcp_r.available -= camp_item.charges;
Expand Down Expand Up @@ -646,7 +646,7 @@ void basecamp::form_crafting_inventory( map &target_map )
}
for( basecamp_resource &bcp_r : resources ) {
bcp_r.consumed = 0;
item camp_item( bcp_r.fake_id, 0 );
item camp_item( bcp_r.fake_id, calendar::turn_zero );
camp_item.set_flag( STATIC( flag_str_id( "PSEUDO" ) ) );
if( !bcp_r.ammo_id.is_null() ) {
for( basecamp_fuel &bcp_f : fuels ) {
Expand Down
6 changes: 3 additions & 3 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,9 @@ bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
if( choice >= 0 && choice <= 1 ) {
item ctr;
if( choice == 0 ) {
ctr = item( "remotevehcontrol", 0 );
ctr = item( "remotevehcontrol", calendar::turn_zero );
} else {
ctr = item( "radiocontrol", 0 );
ctr = item( "radiocontrol", calendar::turn_zero );
}
ctr.charges = units::to_kilojoule( get_power_level() );
int power_use = invoke_item( &ctr );
Expand Down Expand Up @@ -2246,7 +2246,7 @@ bool Character::uninstall_bionic( const bionic &target_cbm, monster &installer,
return false;
}

item bionic_to_uninstall = item( target_cbm.id.str(), 0 );
item bionic_to_uninstall = item( target_cbm.id.str(), calendar::turn_zero );
const itype *itemtype = bionic_to_uninstall.type;
int difficulty = itemtype->bionic->difficulty;
int chance_of_success = bionic_manip_cos( adjusted_skill, difficulty + 2 );
Expand Down
3 changes: 1 addition & 2 deletions src/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ class time_point
public:
time_point();
// TODO: make private
// TODO: make explicit
constexpr time_point( const int t ) : turn_( t ) { }
explicit constexpr time_point( const int t ) : turn_( t ) { }

public:
// TODO: remove this, nobody should need it, one should use a constant `time_point`
Expand Down
4 changes: 2 additions & 2 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,9 +1768,9 @@ bool cata_tiles::draw_from_id_string( const std::string &id, TILE_CATEGORY categ
} else if( category == C_ITEM ) {
item tmp;
if( string_starts_with( found_id, "corpse_" ) ) {
tmp = item( itype_corpse, 0 );
tmp = item( itype_corpse, calendar::turn_zero );
} else {
tmp = item( found_id, 0 );
tmp = item( found_id, calendar::turn_zero );
}
sym = tmp.symbol().empty() ? ' ' : tmp.symbol().front();
col = tmp.color();
Expand Down
12 changes: 6 additions & 6 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4081,7 +4081,7 @@ void Character::normalize()

weary.clear();
martial_arts_data->reset_style();
weapon = item( "null", 0 );
weapon = item( "null", calendar::turn_zero );

set_body();
recalc_hp();
Expand All @@ -4094,15 +4094,15 @@ void Character::die( Creature *nkiller )
set_killer( nkiller );
set_time_died( calendar::turn );
if( has_effect( effect_lightsnare ) ) {
inv->add_item( item( "string_36", 0 ) );
inv->add_item( item( "snare_trigger", 0 ) );
inv->add_item( item( "string_36", calendar::turn_zero ) );
inv->add_item( item( "snare_trigger", calendar::turn_zero ) );
}
if( has_effect( effect_heavysnare ) ) {
inv->add_item( item( "rope_6", 0 ) );
inv->add_item( item( "snare_trigger", 0 ) );
inv->add_item( item( "rope_6", calendar::turn_zero ) );
inv->add_item( item( "snare_trigger", calendar::turn_zero ) );
}
if( has_effect( effect_beartrap ) ) {
inv->add_item( item( "beartrap", 0 ) );
inv->add_item( item( "beartrap", calendar::turn_zero ) );
}
mission::on_creature_death( *this );
}
Expand Down
4 changes: 2 additions & 2 deletions src/computer_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ void computer_session::action_download_software()
return;
}
get_player_character().moves -= 30;
item software( miss->get_item_id(), 0 );
item software( miss->get_item_id(), calendar::turn_zero );
software.mission_id = comp.mission_id;
usb->contents.clear_items();
usb->put_in( software, item_pocket::pocket_type::SOFTWARE );
Expand Down Expand Up @@ -840,7 +840,7 @@ void computer_session::action_blood_anal()
print_line( _( "Pathogen bonded to erythrocytes and leukocytes." ) );
if( query_bool( _( "Download data?" ) ) ) {
if( item *const usb = pick_usb() ) {
item software( "software_blood_data", 0 );
item software( "software_blood_data", calendar::turn_zero );
usb->contents.clear_items();
usb->put_in( software, item_pocket::pocket_type::SOFTWARE );
print_line( _( "Software downloaded." ) );
Expand Down
2 changes: 1 addition & 1 deletion src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ void conditional_t<T>::set_days_since( const JsonObject &jo )
{
const unsigned int days = jo.get_int( "days_since_cataclysm" );
condition = [days]( const T & ) {
return to_turn<int>( calendar::turn ) >= calendar::start_of_cataclysm + 1_days * days;
return calendar::turn >= calendar::start_of_cataclysm + 1_days * days;
};
}

Expand Down
28 changes: 15 additions & 13 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ void game::setup()
weather.nextweather = calendar::start_of_cataclysm + time_duration::from_hours(
get_option<int>( "INITIAL_TIME" ) ) + 30_minutes;

turnssincelastmon = 0; //Auto safe mode init
turnssincelastmon = 0_turns; //Auto safe mode init

sounds::reset_sounds();
clear_zombies();
Expand Down Expand Up @@ -4294,10 +4294,9 @@ void game::mon_info_update( )
const tripoint view = u.pos() + u.view_offset;
new_seen_mon.clear();

static int previous_turn = 0;
// TODO: change current_turn to time_point
const int current_turn = to_turns<int>( calendar::turn - calendar::turn_zero );
const int sm_ignored_turns = get_option<int>( "SAFEMODEIGNORETURNS" );
static time_point previous_turn = calendar::turn_zero;
const time_duration sm_ignored_turns =
time_duration::from_turns( get_option<int>( "SAFEMODEIGNORETURNS" ) );

for( Creature *c : u.get_visible_creatures( MAPSIZE_X ) ) {
monster *m = dynamic_cast<monster *>( c );
Expand Down Expand Up @@ -4385,12 +4384,13 @@ void game::mon_info_update( )
if( critter.ignoring > 0 ) {
if( safe_mode != SAFE_MODE_ON ) {
critter.ignoring = 0;
} else if( ( sm_ignored_turns == 0 || ( critter.lastseen_turn &&
to_turn<int>( *critter.lastseen_turn ) > current_turn - sm_ignored_turns ) ) &&
} else if( ( sm_ignored_turns == time_duration() ||
( critter.lastseen_turn &&
*critter.lastseen_turn > calendar::turn - sm_ignored_turns ) ) &&
( mon_dist > critter.ignoring / 2 || mon_dist < 6 ) ) {
passmon = true;
}
critter.lastseen_turn = current_turn;
critter.lastseen_turn = calendar::turn;
}

if( !passmon ) {
Expand Down Expand Up @@ -4449,14 +4449,16 @@ void game::mon_info_update( )
} else {
cancel_activity_or_ignore_query( distraction_type::hostile_spotted_far, _( "Monsters spotted!" ) );
}
turnssincelastmon = 0;
turnssincelastmon = 0_turns;
if( safe_mode == SAFE_MODE_ON ) {
set_safe_mode( SAFE_MODE_STOP );
}
} else if( current_turn > previous_turn && get_option<bool>( "AUTOSAFEMODE" ) &&
} else if( calendar::turn > previous_turn && get_option<bool>( "AUTOSAFEMODE" ) &&
newseen == 0 ) { // Auto-safe mode, but only if it's a new turn
turnssincelastmon += current_turn - previous_turn;
if( turnssincelastmon >= get_option<int>( "AUTOSAFEMODETURNS" ) && safe_mode == SAFE_MODE_OFF ) {
turnssincelastmon += calendar::turn - previous_turn;
time_duration auto_safe_mode =
time_duration::from_turns( get_option<int>( "AUTOSAFEMODETURNS" ) );
if( turnssincelastmon >= auto_safe_mode && safe_mode == SAFE_MODE_OFF ) {
set_safe_mode( SAFE_MODE_ON );
add_msg( m_info, _( "Safe mode ON!" ) );
}
Expand All @@ -4466,7 +4468,7 @@ void game::mon_info_update( )
set_safe_mode( SAFE_MODE_ON );
}

previous_turn = current_turn;
previous_turn = calendar::turn;
mostseen = newseen;
}

Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ class game

//pixel minimap management
int pixel_minimap_option = 0;
int turnssincelastmon = 0; // needed for auto run mode
time_duration turnssincelastmon = 0_turns; // needed for auto run mode

weather_manager weather;

Expand Down
14 changes: 9 additions & 5 deletions src/gamemode_defense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,8 @@ void defense_game::caravan()
if( current_window == 1 && !items[category_selected].empty() ) {
item_count[category_selected][item_selected]++;
itype_id tmp_itm = items[category_selected][item_selected];
total_price += caravan_price( player_character, item( tmp_itm, 0 ).price( false ) );
total_price += caravan_price( player_character,
item( tmp_itm, calendar::turn_zero ).price( false ) );
if( category_selected == CARAVAN_CART ) { // Find the item in its category
for( int i = 1; i < NUM_CARAVAN_CATEGORIES; i++ ) {
for( size_t j = 0; j < items[i].size(); j++ ) {
Expand Down Expand Up @@ -1007,7 +1008,8 @@ void defense_game::caravan()
item_count[category_selected][item_selected] > 0 ) {
item_count[category_selected][item_selected]--;
itype_id tmp_itm = items[category_selected][item_selected];
total_price -= caravan_price( player_character, item( tmp_itm, 0 ).price( false ) );
total_price -= caravan_price( player_character,
item( tmp_itm, calendar::turn_zero ).price( false ) );
if( category_selected == CARAVAN_CART ) { // Find the item in its category
for( int i = 1; i < NUM_CARAVAN_CATEGORIES; i++ ) {
for( size_t j = 0; j < items[i].size(); j++ ) {
Expand Down Expand Up @@ -1245,7 +1247,7 @@ void draw_caravan_items( const catacurses::window &w, std::vector<itype_id> *ite
}
// THEN print it--if item_selected is valid
if( item_selected < static_cast<int>( items->size() ) ) {
item tmp( ( *items )[item_selected], 0 ); // Dummy item to get info
item tmp( ( *items )[item_selected], calendar::turn_zero ); // Dummy item to get info
fold_and_print( w, point( 1, 12 ), 38, c_white, tmp.info() );
}
// Next, clear the item list on the right
Expand All @@ -1260,8 +1262,10 @@ void draw_caravan_items( const catacurses::window &w, std::vector<itype_id> *ite
item::nname( ( *items )[i], ( *counts )[i] ) );
wprintz( w, c_white, " x %2d", ( *counts )[i] );
if( ( *counts )[i] > 0 ) {
int price = caravan_price( player_character, item( ( *items )[i],
0 ).price( false ) * ( *counts )[i] );
int price =
caravan_price(
player_character,
item( ( *items )[i], calendar::turn_zero ).price( false ) * ( *counts )[i] );
wprintz( w, ( price > player_character.cash ? c_red : c_green ), " (%s)", format_money( price ) );
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/gamemode_tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool tutorial_game::init()
starting_om.clear_mon_groups();

player_character.toggle_trait( trait_QUICK );
item lighter( "lighter", 0 );
item lighter( "lighter", calendar::turn_zero );
lighter.invlet = 'e';
player_character.inv->add_item( lighter, true, false );
player_character.set_skill_level( skill_gun, 5 );
Expand Down Expand Up @@ -279,7 +279,7 @@ void tutorial_game::post_action( action_id act )
break;

case ACTION_WEAR: {
item it( player_character.last_item, 0 );
item it( player_character.last_item, calendar::turn_zero );
if( it.is_armor() ) {
if( it.get_avg_coverage() >= 2 || it.get_thickness() >= 2 ) {
add_message( tut_lesson::LESSON_WORE_ARMOR );
Expand All @@ -301,7 +301,7 @@ void tutorial_game::post_action( action_id act )
add_message( tut_lesson::LESSON_INTERACT );
/* fallthrough */
case ACTION_PICKUP: {
item it( player_character.last_item, 0 );
item it( player_character.last_item, calendar::turn_zero );
if( it.is_armor() ) {
add_message( tut_lesson::LESSON_GOT_ARMOR );
} else if( it.is_gun() ) {
Expand Down
8 changes: 4 additions & 4 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,16 +919,16 @@ static void wait()

add_menu_item( 7, 'd',
setting_alarm ? _( "Set alarm for dawn" ) : _( "Wait till daylight" ),
diurnal_time_before( to_turns<int>( daylight_time( calendar::turn ) - calendar::turn_zero ) ) );
diurnal_time_before( daylight_time( calendar::turn ) ) );
add_menu_item( 8, 'n',
setting_alarm ? _( "Set alarm for noon" ) : _( "Wait till noon" ),
diurnal_time_before( last_midnight + 12_hours ) );
add_menu_item( 9, 'k',
setting_alarm ? _( "Set alarm for dusk" ) : _( "Wait till night" ),
diurnal_time_before( to_turns<int>( night_time( calendar::turn ) - calendar::turn_zero ) ) );
diurnal_time_before( night_time( calendar::turn ) ) );
add_menu_item( 10, 'm',
setting_alarm ? _( "Set alarm for midnight" ) : _( "Wait till midnight" ),
diurnal_time_before( last_midnight + 0_hours ) );
diurnal_time_before( last_midnight ) );
if( setting_alarm ) {
if( player_character.has_effect( effect_alarm_clock ) ) {
add_menu_item( 11, 'x', _( "Cancel the currently set alarm." ),
Expand Down Expand Up @@ -2255,7 +2255,7 @@ bool game::handle_action()
mostseen = 0;
add_msg( m_info, _( "Safe mode ON!" ) );
} else {
turnssincelastmon = 0;
turnssincelastmon = 0_turns;
set_safe_mode( SAFE_MODE_OFF );
add_msg( m_info, get_option<bool>( "AUTOSAFEMODE" )
? _( "Safe mode OFF! (Auto safe mode still enabled!)" ) : _( "Safe mode OFF!" ) );
Expand Down
Loading

0 comments on commit b285a13

Please sign in to comment.