Files
@ r28827:f131debacb19
Branch filter:
Location: cpp/openttd-patchpack/source/src/timer/timer_game_calendar.cpp - annotation
r28827:f131debacb19
6.5 KiB
text/x-c
Codechange: Use `GetVisibleRangeIterators()` in more places. (#12190)
This replaces more first/last index calculation, along with indexed array/vector access, with iterator access instead.
This replaces more first/last index calculation, along with indexed array/vector access, with iterator access instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r28549:6bc33bc6ee38 r27984:4d71f61bffdb r27984:4d71f61bffdb r27088:7e2816121458 r27088:7e2816121458 r27984:4d71f61bffdb r27088:7e2816121458 r27984:4d71f61bffdb r27088:7e2816121458 r27166:64e04a3ef9b1 r27166:64e04a3ef9b1 r27166:64e04a3ef9b1 r27166:64e04a3ef9b1 r28561:d3b2600c3b1b r27148:4e041ae27b9d r27148:4e041ae27b9d r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r28559:3924f7d56927 r27148:4e041ae27b9d r27148:4e041ae27b9d r27148:4e041ae27b9d r27148:4e041ae27b9d r27166:64e04a3ef9b1 r27148:4e041ae27b9d r27883:ddbd33508a8a r27148:4e041ae27b9d r27148:4e041ae27b9d r27148:4e041ae27b9d r28343:242bc14cc675 r27148:4e041ae27b9d r27148:4e041ae27b9d r27148:4e041ae27b9d r27148:4e041ae27b9d r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r28561:d3b2600c3b1b r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r27088:7e2816121458 r27148:4e041ae27b9d r28561:d3b2600c3b1b r28561:d3b2600c3b1b r28561:d3b2600c3b1b r27148:4e041ae27b9d r28561:d3b2600c3b1b r27088:7e2816121458 r28561:d3b2600c3b1b r27148:4e041ae27b9d r27088:7e2816121458 r28343:242bc14cc675 r27088:7e2816121458 r28561:d3b2600c3b1b r27148:4e041ae27b9d r27088:7e2816121458 r28561:d3b2600c3b1b r27148:4e041ae27b9d r27088:7e2816121458 r28561:d3b2600c3b1b r27148:4e041ae27b9d r27148:4e041ae27b9d r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27088:7e2816121458 r27090:1ea30951c0f3 r28561:d3b2600c3b1b r27884:803962be0328 r27090:1ea30951c0f3 r27090:1ea30951c0f3 r27148:4e041ae27b9d r27884:803962be0328 r27148:4e041ae27b9d r27090:1ea30951c0f3 r28561:d3b2600c3b1b r28561:d3b2600c3b1b r27088:7e2816121458 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 r27091:9e41ca246188 | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file timer_game_calendar.cpp
* This file implements the timer logic for the game-calendar-timer.
*/
/**
* Calendar time is used for technology and time-of-year changes, including:
* - Vehicle, airport, station, object introduction and obsolescence
* - Vehicle and engine age
* - NewGRF variables for visual styles or behavior based on year or time of year (e.g. variable snow line)
* - Inflation, since it is tied to original game years. One interpretation of inflation is that it compensates for faster and higher capacity vehicles,
* another is that it compensates for more established companies. Each of these point to a different choice of calendar versus economy time, but we have to pick one
* so we follow a previous decision to tie inflation to original TTD game years.
*/
#include "../stdafx.h"
#include "../openttd.h"
#include "timer.h"
#include "timer_game_calendar.h"
#include "../vehicle_base.h"
#include "../safeguards.h"
TimerGameCalendar::Year TimerGameCalendar::year = {};
TimerGameCalendar::Month TimerGameCalendar::month = {};
TimerGameCalendar::Date TimerGameCalendar::date = {};
TimerGameCalendar::DateFract TimerGameCalendar::date_fract = {};
uint16_t TimerGameCalendar::sub_date_fract = {};
/**
* Converts a Date to a Year, Month & Day.
* @param date the date to convert from
* @returns YearMonthDay representation of the Date.
*/
/* static */ TimerGameCalendar::YearMonthDay TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::Date date)
{
/* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
return CalendarConvertDateToYMD(date);
}
/**
* Converts a tuple of Year, Month and Day to a Date.
* @param year is a number between 0..MAX_YEAR
* @param month is a number between 0..11
* @param day is a number between 1..31
* @returns The equivalent date.
*/
/* static */ TimerGameCalendar::Date TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year year, TimerGameCalendar::Month month, TimerGameCalendar::Day day)
{
/* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
return CalendarConvertYMDToDate(year, month, day);
}
/**
* Set the date.
* @param date New date
* @param fract The number of ticks that have passed on this date.
*/
/* static */ void TimerGameCalendar::SetDate(TimerGameCalendar::Date date, TimerGameCalendar::DateFract fract)
{
assert(fract < Ticks::DAY_TICKS);
TimerGameCalendar::date = date;
TimerGameCalendar::date_fract = fract;
TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
TimerGameCalendar::year = ymd.year;
TimerGameCalendar::month = ymd.month;
}
template<>
void IntervalTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
{
if (trigger == this->period.trigger) {
this->callback(1);
}
}
template<>
void TimeoutTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
{
if (this->fired) return;
if (trigger == this->period.trigger) {
this->callback();
this->fired = true;
}
}
template<>
bool TimerManager<TimerGameCalendar>::Elapsed([[maybe_unused]] TimerGameCalendar::TElapsed delta)
{
assert(delta == 1);
if (_game_mode == GM_MENU) return false;
/* If calendar day progress is frozen, don't try to advance time. */
if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false;
/* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */
if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) {
TimerGameCalendar::sub_date_fract++;
/* Check if we are ready to increment date_fract */
if (TimerGameCalendar::sub_date_fract < (Ticks::DAY_TICKS * _settings_game.economy.minutes_per_calendar_year) / CalendarTime::DEF_MINUTES_PER_YEAR) return false;
}
TimerGameCalendar::date_fract++;
/* Check if we entered a new day. */
if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return true;
TimerGameCalendar::date_fract = 0;
TimerGameCalendar::sub_date_fract = 0;
/* Increase day counter. */
TimerGameCalendar::date++;
TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
/* Check if we entered a new month. */
bool new_month = ymd.month != TimerGameCalendar::month;
/* Check if we entered a new year. */
bool new_year = ymd.year != TimerGameCalendar::year;
/* Update internal variables before calling the daily/monthly/yearly loops. */
TimerGameCalendar::month = ymd.month;
TimerGameCalendar::year = ymd.year;
/* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
auto timers = TimerManager<TimerGameCalendar>::GetTimers();
for (auto timer : timers) {
timer->Elapsed(TimerGameCalendar::DAY);
}
if (new_month) {
for (auto timer : timers) {
timer->Elapsed(TimerGameCalendar::MONTH);
}
}
if (new_year) {
for (auto timer : timers) {
timer->Elapsed(TimerGameCalendar::YEAR);
}
}
/* If we reached the maximum year, decrement dates by a year. */
if (TimerGameCalendar::year == CalendarTime::MAX_YEAR + 1) {
int days_this_year;
TimerGameCalendar::year--;
days_this_year = TimerGameCalendar::IsLeapYear(TimerGameCalendar::year) ? CalendarTime::DAYS_IN_LEAP_YEAR : CalendarTime::DAYS_IN_YEAR;
TimerGameCalendar::date -= days_this_year;
}
return true;
}
#ifdef WITH_ASSERT
template<>
void TimerManager<TimerGameCalendar>::Validate(TimerGameCalendar::TPeriod period)
{
if (period.priority == TimerGameCalendar::Priority::NONE) return;
/* Validate we didn't make a developer error and scheduled more than one
* entry on the same priority/trigger. There can only be one timer on
* a specific trigger/priority, to ensure we are deterministic. */
for (const auto &timer : TimerManager<TimerGameCalendar>::GetTimers()) {
if (timer->period.trigger != period.trigger) continue;
assert(timer->period.priority != period.priority);
}
}
#endif /* WITH_ASSERT */
|