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

Convert more code to use new time classes #23327

Merged
merged 31 commits into from Apr 9, 2018
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dd9f318
Change submap::turn_last_touched to be a time_point.
BevapDin Jan 1, 2018
796c541
Replace usage of calendar::turn_of_year with time_past_new_year
BevapDin Jan 1, 2018
741a5a0
Change weather_generator::get_weather to take a time_point
BevapDin Jan 2, 2018
c4a8eac
Change weather_generator::get_weather_conditions to take a time_point
BevapDin Jan 2, 2018
21c7b63
Move include of "calendar.h" from "weather.h" into the cpp file.
BevapDin Jan 2, 2018
a0a0253
Change get_rot_since to take time_point values.
BevapDin Jan 2, 2018
e056ed6
Use time_point and time_duration in messages.cpp
BevapDin Jan 2, 2018
a7d75ee
Split calendar::print_time into two functions.
BevapDin Jan 2, 2018
e20c012
Move calendar::print_time_just_hour as free function into weather.cpp
BevapDin Jan 2, 2018
3f13be3
Change map actualizing code to use time_duration as parameters.
BevapDin Jan 2, 2018
c04fed6
Change map::fill_funnels to take a time_point
BevapDin Jan 2, 2018
6bdf4e4
Change retroactively_fill_from_funnel to take time_point
BevapDin Jan 2, 2018
7940306
Change musical_instrument_actor::description_frequency to time_duration.
BevapDin Jan 2, 2018
93264c8
Change islot_comestible::spoils to be time_duration.
BevapDin Jan 2, 2018
55c929e
Change item::rot to be a time_duration
BevapDin Jan 2, 2018
f33a740
Change item::last_rot_check to be a time_point
BevapDin Jan 2, 2018
45d7fe9
Change get_rot_since to return time_duration
BevapDin Jan 2, 2018
ad3ef1b
Change item::fridge to be a time_point
BevapDin Jan 2, 2018
d7b90c8
Change npc::complaints to contain time_point
BevapDin Jan 2, 2018
3f3d198
Make calendar::print_time free functions.
BevapDin Mar 29, 2018
f104c02
Change monster::try_upgrade to use time_point
BevapDin Mar 29, 2018
0cc2e54
Change MonsterGroupEntry::starts and ends to time_duration.
BevapDin Mar 29, 2018
acf0c5e
Remove unused includes from "calendar.cpp"
BevapDin Mar 29, 2018
fb1d0e9
Use calendar::once_every instead of modulo operator
BevapDin Jan 2, 2018
a6ff1b9
Remove redundant explicit conversion from calendar to int
BevapDin Jan 7, 2018
6d42ca4
Change npc::companion_mission_time to be a time_point
BevapDin Mar 29, 2018
b2233da
Change npc::last_updated to time_point
BevapDin Mar 29, 2018
8ee1d04
Change npc::restock to be a time_point.
BevapDin Mar 30, 2018
66e34af
Fix compilation for gcc 4.8.4
BevapDin Mar 30, 2018
19986c4
Fix display of seconds in to_string_time_of_day
BevapDin Apr 4, 2018
c06b59a
Fix display of seconds in to_string_time_of_day (again)
BevapDin Apr 7, 2018
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -103,11 +103,8 @@ classes = {
{ name = "day_of_year", rval = "int", args = { } },
{ name = "get_turn", rval = "int", args = { } },
{ name = "increment", rval = nil, args = { } },
{ name = "print_time", rval = "string", args = { "bool" } },
{ name = "print_time", rval = "string", args = { } },
{ name = "is_night", rval = "bool", args = { } },
{ name = "sunlight", rval = "float", args = { } },
{ name = "turn_of_year", rval = "int", args = { } },
{ name = "years", rval = "int", args = { } },
{ name = "sunset", rval = "calendar", args = { } },
{ name = "sunrise", rval = "calendar", args = { } },
@@ -825,7 +822,7 @@ classes = {
burnt = { type = "int", writable = true },
charges = { type = "int", writable = true },
frequency = { type = "int", writable = true },
fridge = { type = "int", writable = true },
fridge = { type = "time_point", writable = true },
invlet = { type = "int", writable = true },
irridation = { type = "int", writable = true },
item_counter = { type = "int", writable = true },
@@ -1427,7 +1427,7 @@ void advanced_inventory::display()
w_width - ( minimap_width + 2 ) - utf8_width(msg) - 1,
c_white, msg.c_str() );
if( g->u.has_watch() ) {
const std::string time = calendar::turn.print_time();
const std::string time = to_string_time_of_day( calendar::turn );
mvwprintz( head, 0, 2, c_white, time.c_str() );
}
wrefresh( head );
@@ -1,14 +1,12 @@
#include "calendar.h"
#include <cmath>
#include <sstream>
#include <limits>
#include <array>

#include "output.h"
#include "options.h"
#include "translations.h"
#include "string_formatter.h"
#include "debug.h"
#include "rng.h"

// Divided by 100 to prevent overflowing when converted to moves
@@ -391,41 +389,32 @@ std::string to_string_approx( const time_duration &d, const bool verbose )
return make_result( turns, _( "about %s" ), "%s" );
}

std::string calendar::print_time(bool just_hour) const
std::string to_string_time_of_day( const time_point &p )
{
std::ostringstream time_string;
int hour_param;
const int hour = hour_of_day<int>( p );
const int minute = minute_of_hour<int>( p );
//@todo add a to_seconds function?
const int second = ( to_turns<int>( time_past_midnight( p ) ) * 6 ) % 60;
const std::string format_type = get_option<std::string>( "24_HOUR" );

if (get_option<std::string>( "24_HOUR" ) == "military") {
hour_param = hour % 24;
time_string << string_format("%02d%02d.%02d", hour_param, minute, second);
} else if (get_option<std::string>( "24_HOUR" ) == "24h") {
hour_param = hour % 24;
if (just_hour) {
time_string << hour_param;
} else {
//~ hour:minute (24hr time display)
time_string << string_format(_("%02d:%02d:%02d"), hour_param, minute, second);
}
if( format_type == "military" ) {
return string_format( "%02d%02d.%02d", hour, minute, second );
} else if( format_type == "24h" ) {
//~ hour:minute (24hr time display)
return string_format( _( "%02d:%02d:%02d" ), hour, minute, second );
} else {
hour_param = hour % 12;
if (hour_param == 0) {
int hour_param = hour % 12;
if( hour_param == 0 ) {
hour_param = 12;
}
// Padding is removed as necessary to prevent clipping with SAFE notification in wide sidebar mode
std::string padding = hour_param < 10 ? " " : "";
if (just_hour && hour < 12) {
time_string << string_format(_("%d AM"), hour_param);
} else if (just_hour) {
time_string << string_format(_("%d PM"), hour_param);
} else if (hour < 12) {
time_string << string_format(_("%d:%02d:%02d%sAM"), hour_param, minute, second, padding.c_str());
const std::string padding = hour_param < 10 ? " " : "";
if( hour < 12 ) {
return string_format( _( "%d:%02d:%02d%sAM" ), hour_param, minute, second, padding );
} else {
time_string << string_format(_("%d:%02d:%02d%sPM"), hour_param, minute, second, padding.c_str());
return string_format( _( "%d:%02d:%02d%sPM" ), hour_param, minute, second, padding );
}
}

return time_string.str();
}

std::string calendar::day_of_week() const
@@ -494,12 +483,6 @@ float calendar::season_from_default_ratio()
return to_days<float>( season_length() ) / default_season_length;
}

int calendar::turn_of_year() const
{
const int season_turns = to_turns<int>( season_length() );
return (season * season_turns) + (turn_number % season_turns);
}

int calendar::day_of_year() const
{
return day + to_days<int>( season_length() ) * season;
@@ -554,6 +537,11 @@ time_duration rng( time_duration lo, time_duration hi )
return time_duration( rng( lo.turns_, hi.turns_ ) );
}

bool x_in_y( const time_duration &a, const time_duration &b )
{
return ::x_in_y( to_turns<int>( a ), to_turns<int>( b ) );
}

season_type season_of_year( const time_point &p )
{
static time_point prev_turn = calendar::before_time_starts;
@@ -573,3 +561,19 @@ season_type season_of_year( const time_point &p )

return prev_season;
}

std::string to_string( const time_point &p )
{
const int year = to_turns<int>( p - calendar::time_of_cataclysm ) / to_turns<int>( calendar::year_length() ) + 1;
const std::string time = to_string_time_of_day( p );
if( calendar::eternal_season() ) {
const int day = to_days<int>( time_past_new_year( p ) );
//~ 1 is the year, 2 is the day (of the *year*), 3 is the time of the day in its usual format
return string_format( _( "Year %1$d, day %2$d %3$s" ), year, day, time );
} else {
const int day = day_of_season<int>( p );
//~ 1 is the year, 2 is the season name, 3 is the day (of the season), 4 is the time of the day in its usual format
return string_format( _( "Year %1$d, %2$s, day %3$d %4$s" ), year,
calendar::name_season( season_of_year( p ) ), day, time );
}
}
@@ -250,9 +250,6 @@ class calendar
/// @returns relative length of game season to real life season.
static float season_ratio();

/** @returns Number of turns elapsed in current year */
int turn_of_year() const;

/** @returns Number of days elapsed in current year */
int day_of_year() const;
/**
@@ -261,9 +258,6 @@ class calendar
* option) to actual in-game length.
*/
static float season_from_default_ratio();

/** Returns the current time in a string according to the options set */
std::string print_time( bool just_hour = false ) const;
/**
* Returns the name of the current day of the week
*
@@ -349,6 +343,9 @@ class time_duration
/// Allows writing `time_duration d = 0;`
time_duration( const std::nullptr_t ) : turns_( 0 ) { }

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );

/**
* Named constructors to get a duration representing a multiple of the named time
* units. Note that a duration is stored as integer number of turns, so
@@ -472,6 +469,9 @@ class time_duration
friend time_duration rng( time_duration lo, time_duration hi );
};

/// @see x_in_y(int,int)
bool x_in_y( const time_duration &a, const time_duration &b );

/**
* Convert the given number into an duration by calling the matching
* `time_duration::from_*` function.
@@ -611,6 +611,11 @@ inline time_duration time_past_midnight( const time_point &p )
return ( p - calendar::time_of_cataclysm ) % 1_days;
}

inline time_duration time_past_new_year( const time_point &p )
{
return ( p - calendar::time_of_cataclysm ) % calendar::year_length();
}

template<typename T>
inline T minute_of_hour( const time_point &p )
{
@@ -633,5 +638,9 @@ inline T day_of_season( const time_point &p )
/// @returns The season of the of the given time point. Returns the same season for
/// any input if the calendar::eternal_season yields true.
season_type season_of_year( const time_point &p );
/// @returns The time point formatted to be shown to the player. Contains year, season, day and time of day.
std::string to_string( const time_point &p );
/// @returns The time point formatted to be shown to the player. Contains only the time of day, not the year, day or season.
std::string to_string_time_of_day( const time_point &p );

#endif
@@ -1790,7 +1790,7 @@ int editmap::mapgen_preview( real_coords &tc, uimenu &gmenu )
destsm->active_items = srcsm->active_items;

destsm->temperature = srcsm->temperature;
destsm->turn_last_touched = int( calendar::turn );
destsm->last_touched = calendar::turn;
destsm->comp = std::move( srcsm->comp );
destsm->camp = srcsm->camp;

@@ -4567,7 +4567,7 @@ void game::draw_sidebar()
const catacurses::window &time_window = sideStyle ? w_status2 : w_status;
wmove(time_window, sideStyle ? 0 : 1, sideStyle ? 15 : 41);
if ( u.has_watch() ) {
wprintz( time_window, c_white, calendar::turn.print_time() );
wprintz( time_window, c_white, to_string_time_of_day( calendar::turn ) );
} else if( get_levz() >= 0 ) {
std::vector<std::pair<char, nc_color> > vGlyphs;
vGlyphs.push_back(std::make_pair('_', c_red));
@@ -13028,7 +13028,7 @@ void game::wait()

add_menu_item( 12, 'q', _( "Exit" ) );

as_m.text = ( has_watch ) ? string_format( _( "It's %s now. " ), calendar::turn.print_time().c_str() ) : "";
as_m.text = ( has_watch ) ? string_format( _( "It's %s now. " ), to_string_time_of_day( calendar::turn ) ) : "";
as_m.text += _( "Wait for how long?" );
as_m.return_invalid = true;
as_m.query(); /* calculate key and window variables, generate window, and loop until we get a valid answer */
@@ -366,9 +366,9 @@ class comestible_inventory_preset : public inventory_selector_preset
}, _( "JOY" ) );

append_cell( [ this ]( const item_location & loc ) {
const int spoils = get_edible_comestible( loc ).spoils;
const time_duration spoils = get_edible_comestible( loc ).spoils;
if( spoils > 0 ) {
return to_string_clipped( time_duration::from_turns( spoils ) );
return to_string_clipped( spoils );
}
return std::string();
}, _( "SPOILS IN" ) );
@@ -1911,7 +1911,7 @@ void iexamine::aggie_plant(player &p, const tripoint &examp)
// must be on the square of the plant, therefore this hack:
const auto old_furn = g->m.furn( examp );
g->m.furn_set( examp, f_null );
g->m.spawn_item( examp, "fertilizer", 1, 1, (int)calendar::turn );
g->m.spawn_item( examp, "fertilizer", 1, 1, calendar::turn );
g->m.furn_set( examp, old_furn );
}
}
@@ -530,7 +530,7 @@ bool item::stacks_with( const item &rhs ) const
// Because spoiling items are only processed every processing_speed()-th turn
// the rotting value becomes slightly different for items that have
// been created at the same time and place and with the same initial rot.
if( std::abs( rot - rhs.rot ) > processing_speed() ) {
if( std::abs( to_turns<int>( rot - rhs.rot ) ) > processing_speed() ) {
return false;
} else if( rotten() != rhs.rotten() ) {
// just to be save that rotten and unrotten food is *never* stacked.
@@ -814,13 +814,13 @@ std::string item::info( bool showtext, std::vector<iteminfo> &info, int batch )
info.push_back( iteminfo( "BASE", _( "bday rot: " ), "",
to_turns<int>( food->age() ), true, "", true, true ) );
info.push_back( iteminfo( "BASE", _( "temp rot: " ), "",
( int )food->rot, true, "", true, true ) );
to_turns<int>( food->rot ), true, "", true, true ) );
info.push_back( iteminfo( "BASE", space + _( "max rot: " ), "",
food->type->comestible->spoils, true, "", true, true ) );
to_turns<int>( food->type->comestible->spoils ), true, "", true, true ) );
info.push_back( iteminfo( "BASE", space + _( "fridge: " ), "",
( int )food->fridge, true, "", true, true ) );
to_turn<int>( food->fridge ), true, "", true, true ) );
info.push_back( iteminfo( "BASE", _( "last rot: " ), "",
( int )food->last_rot_check, true, "", true, true ) );
to_turn<int>( food->last_rot_check ), true, "", true, true ) );
}
}
info.push_back( iteminfo( "BASE", _( "burn: " ), "", burnt, true, "", true, true ) );
@@ -884,7 +884,7 @@ std::string item::info( bool showtext, std::vector<iteminfo> &info, int batch )
}

if( food_item->goes_bad() ) {
const std::string rot_time = to_string_clipped( time_duration::from_turns( food_item->type->comestible->spoils ) );
const std::string rot_time = to_string_clipped( food_item->type->comestible->spoils );
info.emplace_back( "DESCRIPTION",
string_format( _( "* This food is <neutral>perishable</neutral>, and takes <info>%s</info> to rot from full freshness, at room temperature." ),
rot_time.c_str() ) );
@@ -2863,22 +2863,22 @@ std::set<matec_id> item::get_techniques() const

bool item::goes_bad() const
{
return is_food() && type->comestible->spoils;
return is_food() && type->comestible->spoils != 0;
}

double item::get_relative_rot() const
{
return goes_bad() ? rot / double( type->comestible->spoils ) : 0;
return goes_bad() ? rot / type->comestible->spoils : 0;
}

void item::set_relative_rot( double val )
{
if( goes_bad() ) {
rot = type->comestible->spoils * val;
// calc_rot uses last_rot_check (when it's not 0) instead of bday.
// calc_rot uses last_rot_check (when it's not time_of_cataclysm) instead of bday.
// this makes sure the rotting starts from now, not from bday.
last_rot_check = calendar::turn;
fridge = 0;
fridge = calendar::before_time_starts;
active = !rotten();
}
}
@@ -2898,7 +2898,7 @@ int item::spoilage_sort_order()
}

if ( subject->goes_bad() ) {
return subject->type->comestible->spoils - subject->rot;
return to_turns<int>( subject->type->comestible->spoils - subject->rot );
}

if ( subject->type->comestible ) {
@@ -2915,22 +2915,20 @@ int item::spoilage_sort_order()

void item::calc_rot(const tripoint &location)
{
const int now = calendar::turn;
if ( last_rot_check + 10 < now ) {
const int since = ( last_rot_check == 0 ? to_turn<int>( bday ) : last_rot_check );
const int until = ( fridge > 0 ? fridge : now );
const time_point now = calendar::turn;
if( now - last_rot_check > 10_turns ) {
const time_point since = last_rot_check == calendar::time_of_cataclysm ? bday : last_rot_check;
const time_point until = fridge != calendar::before_time_starts ? fridge : now;
if ( since < until ) {
// rot (outside of fridge) from bday/last_rot_check until fridge/now
int old = rot;
rot += get_rot_since( since, until, location );
add_msg( m_debug, "r: %s %d,%d %d->%d", typeId().c_str(), since, until, old, rot );
}
last_rot_check = now;

if (fridge > 0) {
if( fridge != calendar::before_time_starts ) {
// Flat 20%, rot from time of putting it into fridge up to now
rot += (now - fridge) * 0.2;
fridge = 0;
rot += ( now - fridge ) * 0.2;
fridge = calendar::before_time_starts;
}
// item stays active to let the item counter work
if( item_counter == 0 && rotten() ) {
@@ -677,18 +677,23 @@ class item : public visitable<item>
bool has_rotten_away() const { return get_relative_rot() > 2.0; }

private:
int rot = 0; /** Accumulated rot is compared to shelf life to decide if item is rotten. */
/** Turn when the rot calculation was last performed */
int last_rot_check = 0;
/**
* Accumulated rot, expressed as time the item has been in standard temperature.
* It is compared to shelf life (@ref islot_comestible::spoils) to decide if
* the item is rotten.
*/
time_duration rot = 0;
/** Time when the rot calculation was last performed. */
time_point last_rot_check = calendar::time_of_cataclysm;

public:
int get_rot() const
{
return rot;
return to_turns<int>( rot );
}

/** Turn item was put into a fridge or 0 if not in any fridge. */
int fridge = 0;
/** Turn item was put into a fridge or calendar::before_time_starts if not in any fridge. */
time_point fridge = calendar::before_time_starts;

/** Time for this item to be fully fermented. */
time_duration brewing_time() const;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.