Cache current ticks per calendar day
This commit is contained in:
17
src/date.cpp
17
src/date.cpp
@@ -39,6 +39,7 @@ namespace DateDetail {
|
|||||||
StateTicksDelta _state_ticks_offset; ///< Offset to add when calculating a StateTicks value from an economy date, date fract and tick skip counter
|
StateTicksDelta _state_ticks_offset; ///< Offset to add when calculating a StateTicks value from an economy date, date fract and tick skip counter
|
||||||
uint8_t _tick_skip_counter; ///< Counter for ticks, when only vehicles are moving and nothing else happens
|
uint8_t _tick_skip_counter; ///< Counter for ticks, when only vehicles are moving and nothing else happens
|
||||||
uint8_t _effective_day_length; ///< Current effective day length
|
uint8_t _effective_day_length; ///< Current effective day length
|
||||||
|
Ticks _ticks_per_calendar_day; ///< Current ticks per calendar day
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void ClearOutOfDateSignalSpeedRestrictions();
|
extern void ClearOutOfDateSignalSpeedRestrictions();
|
||||||
@@ -124,6 +125,16 @@ void UpdateEffectiveDayLengthFactor()
|
|||||||
{
|
{
|
||||||
DateDetail::_effective_day_length = _settings_game.EffectiveDayLengthFactor();
|
DateDetail::_effective_day_length = _settings_game.EffectiveDayLengthFactor();
|
||||||
|
|
||||||
|
if (EconTime::UsingWallclockUnits()) {
|
||||||
|
if (CalTime::IsCalendarFrozen()) {
|
||||||
|
DateDetail::_ticks_per_calendar_day = INT32_MAX;
|
||||||
|
} else {
|
||||||
|
DateDetail::_ticks_per_calendar_day = (_settings_game.economy.minutes_per_calendar_year * DAY_TICKS) / CalTime::DEF_MINUTES_PER_YEAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DateDetail::_ticks_per_calendar_day = DAY_TICKS * DateDetail::_effective_day_length;
|
||||||
|
}
|
||||||
|
|
||||||
SetupTileLoopCounts();
|
SetupTileLoopCounts();
|
||||||
UpdateCargoScalers();
|
UpdateCargoScalers();
|
||||||
}
|
}
|
||||||
@@ -134,7 +145,7 @@ CalTime::Date StateTicksToCalendarDate(StateTicks ticks)
|
|||||||
|
|
||||||
if (CalTime::IsCalendarFrozen()) return CalTime::CurDate();
|
if (CalTime::IsCalendarFrozen()) return CalTime::CurDate();
|
||||||
|
|
||||||
Ticks ticks_per_cal_day = (_settings_game.economy.minutes_per_calendar_year * DAY_TICKS) / CalTime::DEF_MINUTES_PER_YEAR;
|
Ticks ticks_per_cal_day = TicksPerCalendarDay();
|
||||||
uint subticks_left_this_day = ((DAY_TICKS - CalTime::CurDateFract()) * ticks_per_cal_day) - CalTime::CurSubDateFract();
|
uint subticks_left_this_day = ((DAY_TICKS - CalTime::CurDateFract()) * ticks_per_cal_day) - CalTime::CurSubDateFract();
|
||||||
Ticks ticks_into_this_day = ticks_per_cal_day - CeilDiv(subticks_left_this_day, DAY_TICKS);
|
Ticks ticks_into_this_day = ticks_per_cal_day - CeilDiv(subticks_left_this_day, DAY_TICKS);
|
||||||
|
|
||||||
@@ -259,7 +270,7 @@ EconTime::YearMonthDay EconTime::ConvertDateToYMD(EconTime::Date date)
|
|||||||
if (EconTime::UsingWallclockUnits()) {
|
if (EconTime::UsingWallclockUnits()) {
|
||||||
/* If we're using wallclock units, economy months have 30 days and an economy year has 360 days. */
|
/* If we're using wallclock units, economy months have 30 days and an economy year has 360 days. */
|
||||||
EconTime::YearMonthDay ymd;
|
EconTime::YearMonthDay ymd;
|
||||||
ymd.year =date.base() / EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR;
|
ymd.year = date.base() / EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR;
|
||||||
ymd.month = (date.base() % EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR) / EconTime::DAYS_IN_ECONOMY_WALLCLOCK_MONTH;
|
ymd.month = (date.base() % EconTime::DAYS_IN_ECONOMY_WALLCLOCK_YEAR) / EconTime::DAYS_IN_ECONOMY_WALLCLOCK_MONTH;
|
||||||
ymd.day = (date.base() % EconTime::DAYS_IN_ECONOMY_WALLCLOCK_MONTH) + 1;
|
ymd.day = (date.base() % EconTime::DAYS_IN_ECONOMY_WALLCLOCK_MONTH) + 1;
|
||||||
return ymd;
|
return ymd;
|
||||||
@@ -456,7 +467,7 @@ static void IncreaseCalendarDate()
|
|||||||
CalTime::Detail::now.sub_date_fract += DAY_TICKS;
|
CalTime::Detail::now.sub_date_fract += DAY_TICKS;
|
||||||
|
|
||||||
/* Check if we are ready to increment date_fract */
|
/* Check if we are ready to increment date_fract */
|
||||||
const uint16_t threshold = (_settings_game.economy.minutes_per_calendar_year * DAY_TICKS) / CalTime::DEF_MINUTES_PER_YEAR;
|
const uint16_t threshold = TicksPerCalendarDay();
|
||||||
if (CalTime::Detail::now.sub_date_fract < threshold) return;
|
if (CalTime::Detail::now.sub_date_fract < threshold) return;
|
||||||
|
|
||||||
CalTime::Detail::now.sub_date_fract = std::min<uint16_t>(CalTime::Detail::now.sub_date_fract - threshold, DAY_TICKS - 1);
|
CalTime::Detail::now.sub_date_fract = std::min<uint16_t>(CalTime::Detail::now.sub_date_fract - threshold, DAY_TICKS - 1);
|
||||||
|
@@ -22,6 +22,7 @@ namespace DateDetail {
|
|||||||
extern StateTicksDelta _state_ticks_offset;
|
extern StateTicksDelta _state_ticks_offset;
|
||||||
extern uint8_t _tick_skip_counter;
|
extern uint8_t _tick_skip_counter;
|
||||||
extern uint8_t _effective_day_length;
|
extern uint8_t _effective_day_length;
|
||||||
|
extern Ticks _ticks_per_calendar_day;
|
||||||
};
|
};
|
||||||
|
|
||||||
StateTicks GetStateTicksFromDateWithoutOffset(EconTime::Date date, EconTime::DateFract date_fract);
|
StateTicks GetStateTicksFromDateWithoutOffset(EconTime::Date date, EconTime::DateFract date_fract);
|
||||||
@@ -37,6 +38,11 @@ inline uint8_t DayLengthFactor()
|
|||||||
return DateDetail::_effective_day_length;
|
return DateDetail::_effective_day_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Ticks TicksPerCalendarDay()
|
||||||
|
{
|
||||||
|
return DateDetail::_ticks_per_calendar_day;
|
||||||
|
}
|
||||||
|
|
||||||
void UpdateEffectiveDayLengthFactor();
|
void UpdateEffectiveDayLengthFactor();
|
||||||
|
|
||||||
inline constexpr YearDelta DateDeltaToYearDelta(DateDelta date)
|
inline constexpr YearDelta DateDeltaToYearDelta(DateDelta date)
|
||||||
@@ -78,7 +84,7 @@ inline Ticks TimetableDisplayUnitSize()
|
|||||||
} else if (EconTime::UsingWallclockUnits()) {
|
} else if (EconTime::UsingWallclockUnits()) {
|
||||||
return TICKS_PER_SECOND;
|
return TICKS_PER_SECOND;
|
||||||
} else {
|
} else {
|
||||||
return DAY_TICKS * DayLengthFactor();
|
return TicksPerCalendarDay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1309,6 +1309,8 @@ static void ChangeMinutesPerYear(int32_t new_value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdateEffectiveDayLengthFactor();
|
||||||
|
|
||||||
/* If the setting value is not the default, force the game to use wallclock timekeeping units.
|
/* If the setting value is not the default, force the game to use wallclock timekeeping units.
|
||||||
* This can only happen in the menu, since the pre_cb ensures this setting can only be changed there, or if we're already using wallclock units.
|
* This can only happen in the menu, since the pre_cb ensures this setting can only be changed there, or if we're already using wallclock units.
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user