1
0
Fork 0

Codechange: Use ecomomy dates for timetables

pull/11588/head
Tyler Trahan 2023-01-31 18:25:49 -05:00
parent 02302a9fe9
commit b08e2e0527
5 changed files with 38 additions and 38 deletions

View File

@ -9,7 +9,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "strings_func.h" #include "strings_func.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "window_func.h" #include "window_func.h"
#include "window_gui.h" #include "window_gui.h"
#include "date_gui.h" #include "date_gui.h"
@ -25,9 +25,9 @@
struct SetDateWindow : Window { struct SetDateWindow : Window {
SetDateCallback *callback; ///< Callback to call when a date has been selected SetDateCallback *callback; ///< Callback to call when a date has been selected
void *callback_data; ///< Callback data pointer. void *callback_data; ///< Callback data pointer.
TimerGameCalendar::YearMonthDay date; ///< The currently selected date TimerGameEconomy::YearMonthDay date; ///< The currently selected date
TimerGameCalendar::Year min_year; ///< The minimum year in the year dropdown TimerGameEconomy::Year min_year; ///< The minimum year in the year dropdown
TimerGameCalendar::Year max_year; ///< The maximum year (inclusive) in the year dropdown TimerGameEconomy::Year max_year; ///< The maximum year (inclusive) in the year dropdown
/** /**
* Create the new 'set date' window * Create the new 'set date' window
@ -39,19 +39,19 @@ struct SetDateWindow : Window {
* @param max_year the maximum year (inclusive) to show in the year dropdown * @param max_year the maximum year (inclusive) to show in the year dropdown
* @param callback the callback to call once a date has been selected * @param callback the callback to call once a date has been selected
*/ */
SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, TimerGameCalendar::Date initial_date, TimerGameCalendar::Year min_year, TimerGameCalendar::Year max_year, SetDateCallback *callback, void *callback_data) : SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data) :
Window(desc), Window(desc),
callback(callback), callback(callback),
callback_data(callback_data), callback_data(callback_data),
min_year(std::max(CalendarTime::MIN_YEAR, min_year)), min_year(std::max(EconomyTime::MIN_YEAR, min_year)),
max_year(std::min(CalendarTime::MAX_YEAR, max_year)) max_year(std::min(EconomyTime::MAX_YEAR, max_year))
{ {
assert(this->min_year <= this->max_year); assert(this->min_year <= this->max_year);
this->parent = parent; this->parent = parent;
this->InitNested(window_number); this->InitNested(window_number);
if (initial_date == 0) initial_date = TimerGameCalendar::date; if (initial_date == 0) initial_date = TimerGameEconomy::date;
TimerGameCalendar::ConvertDateToYMD(initial_date, &this->date); TimerGameEconomy::ConvertDateToYMD(initial_date, &this->date);
this->date.year = Clamp(this->date.year, min_year, max_year); this->date.year = Clamp(this->date.year, min_year, max_year);
} }
@ -88,7 +88,7 @@ struct SetDateWindow : Window {
break; break;
case WID_SD_YEAR: case WID_SD_YEAR:
for (TimerGameCalendar::Year i = this->min_year; i <= this->max_year; i++) { for (TimerGameEconomy::Year i = this->min_year; i <= this->max_year; i++) {
SetDParam(0, i); SetDParam(0, i);
list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_INT, i.base(), false)); list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_INT, i.base(), false));
} }
@ -147,7 +147,7 @@ struct SetDateWindow : Window {
break; break;
case WID_SD_SET_DATE: case WID_SD_SET_DATE:
if (this->callback != nullptr) this->callback(this, TimerGameCalendar::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data); if (this->callback != nullptr) this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data);
this->Close(); this->Close();
break; break;
} }
@ -212,7 +212,7 @@ static WindowDesc _set_date_desc(__FILE__, __LINE__,
* @param callback the callback to call once a date has been selected * @param callback the callback to call once a date has been selected
* @param callback_data extra callback data * @param callback_data extra callback data
*/ */
void ShowSetDateWindow(Window *parent, int window_number, TimerGameCalendar::Date initial_date, TimerGameCalendar::Year min_year, TimerGameCalendar::Year max_year, SetDateCallback *callback, void *callback_data) void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
{ {
CloseWindowByClass(WC_SET_DATE); CloseWindowByClass(WC_SET_DATE);
new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data); new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data);

View File

@ -18,8 +18,8 @@
* @param w the window that sends the callback * @param w the window that sends the callback
* @param date the date that has been chosen * @param date the date that has been chosen
*/ */
typedef void SetDateCallback(const Window *w, TimerGameCalendar::Date date, void *data); typedef void SetDateCallback(const Window *w, TimerGameEconomy::Date date, void *data);
void ShowSetDateWindow(Window *parent, int window_number, TimerGameCalendar::Date initial_date, TimerGameCalendar::Year min_year, TimerGameCalendar::Year max_year, SetDateCallback *callback, void *callback_data); void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data);
#endif /* DATE_GUI_H */ #endif /* DATE_GUI_H */

View File

@ -11,10 +11,10 @@
#define TIMETABLE_H #define TIMETABLE_H
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "vehicle_type.h" #include "vehicle_type.h"
static const TimerGameCalendar::Year MAX_TIMETABLE_START_YEARS = 15; ///< The maximum start date offset, in years. static const TimerGameEconomy::Year MAX_TIMETABLE_START_YEARS = 15; ///< The maximum start date offset, in economy years.
enum class TimetableMode : uint8_t { enum class TimetableMode : uint8_t {
Days, Days,
@ -22,8 +22,8 @@ enum class TimetableMode : uint8_t {
Ticks, Ticks,
}; };
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameCalendar::Date start_date); TimerGameTick::TickCounter GetStartTickFromDate(TimerGameEconomy::Date start_date);
TimerGameCalendar::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick); TimerGameEconomy::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick);
void ShowTimetableWindow(const Vehicle *v); void ShowTimetableWindow(const Vehicle *v);
void UpdateVehicleTimetable(Vehicle *v, bool travelling); void UpdateVehicleTimetable(Vehicle *v, bool travelling);

View File

@ -11,7 +11,7 @@
#include "command_func.h" #include "command_func.h"
#include "company_func.h" #include "company_func.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "window_func.h" #include "window_func.h"
#include "vehicle_base.h" #include "vehicle_base.h"
#include "timetable_cmd.h" #include "timetable_cmd.h"
@ -26,13 +26,13 @@
* @param start_date The date when the timetable starts. * @param start_date The date when the timetable starts.
* @return The first tick of this date. * @return The first tick of this date.
*/ */
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameCalendar::Date start_date) TimerGameTick::TickCounter GetStartTickFromDate(TimerGameEconomy::Date start_date)
{ {
/* Calculate the offset in ticks from the current date. */ /* Calculate the offset in ticks from the current date. */
TimerGameTick::Ticks tick_offset = (start_date - TimerGameCalendar::date).base() * Ticks::DAY_TICKS; TimerGameTick::Ticks tick_offset = (start_date - TimerGameEconomy::date).base() * Ticks::DAY_TICKS;
/* Compensate for the current date_fract. */ /* Compensate for the current date_fract. */
tick_offset -= TimerGameCalendar::date_fract; tick_offset -= TimerGameEconomy::date_fract;
/* Return the current tick plus the offset. */ /* Return the current tick plus the offset. */
return TimerGameTick::counter + tick_offset; return TimerGameTick::counter + tick_offset;
@ -43,16 +43,16 @@ TimerGameTick::TickCounter GetStartTickFromDate(TimerGameCalendar::Date start_da
* @param start_tick The TimerGameTick::TickCounter when the timetable starts. * @param start_tick The TimerGameTick::TickCounter when the timetable starts.
* @return The date when we reach this tick. * @return The date when we reach this tick.
*/ */
TimerGameCalendar::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick) TimerGameEconomy::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick)
{ {
/* Calculate the offset in ticks from the current counter tick. */ /* Calculate the offset in ticks from the current counter tick. */
TimerGameTick::Ticks tick_offset = start_tick - TimerGameTick::counter; TimerGameTick::Ticks tick_offset = start_tick - TimerGameTick::counter;
/* Compensate for the current date_fract. */ /* Compensate for the current date_fract. */
tick_offset += TimerGameCalendar::date_fract; tick_offset += TimerGameEconomy::date_fract;
/* Return the current date plus the offset in days. */ /* Return the current date plus the offset in days. */
return TimerGameCalendar::date + (tick_offset / Ticks::DAY_TICKS); return TimerGameEconomy::date + (tick_offset / Ticks::DAY_TICKS);
} }
/** /**
@ -347,21 +347,21 @@ CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool tim
TimerGameTick::Ticks total_duration = v->orders->GetTimetableTotalDuration(); TimerGameTick::Ticks total_duration = v->orders->GetTimetableTotalDuration();
TimerGameCalendar::Date start_date = GetDateFromStartTick(start_tick); TimerGameEconomy::Date start_date = GetDateFromStartTick(start_tick);
/* Don't let a timetable start at an invalid date. */ /* Don't let a timetable start at an invalid date. */
if (start_date < 0 || start_date > CalendarTime::MAX_DATE) return CMD_ERROR; if (start_date < 0 || start_date > EconomyTime::MAX_DATE) return CMD_ERROR;
/* Don't let a timetable start more than 15 years into the future... */ /* Don't let a timetable start more than 15 years into the future... */
if (start_date - TimerGameCalendar::date > TimerGameCalendar::DateAtStartOfYear(MAX_TIMETABLE_START_YEARS)) return CMD_ERROR; if (start_date - TimerGameEconomy::date > TimerGameEconomy::DateAtStartOfYear(MAX_TIMETABLE_START_YEARS)) return CMD_ERROR;
/* ...or 1 year in the past. */ /* ...or 1 year in the past. */
if (TimerGameCalendar::date - start_date > CalendarTime::DAYS_IN_LEAP_YEAR) return CMD_ERROR; if (TimerGameEconomy::date - start_date > EconomyTime::DAYS_IN_LEAP_YEAR) return CMD_ERROR;
/* If trying to distribute start dates over a shared order group, we need to know the total duration. */ /* If trying to distribute start dates over a shared order group, we need to know the total duration. */
if (timetable_all && !v->orders->IsCompleteTimetable()) return CommandCost(STR_ERROR_TIMETABLE_INCOMPLETE); if (timetable_all && !v->orders->IsCompleteTimetable()) return CommandCost(STR_ERROR_TIMETABLE_INCOMPLETE);
/* Don't allow invalid start dates for other vehicles in the shared order group. */ /* Don't allow invalid start dates for other vehicles in the shared order group. */
if (timetable_all && start_date + (total_duration / Ticks::DAY_TICKS) > CalendarTime::MAX_DATE) return CMD_ERROR; if (timetable_all && start_date + (total_duration / Ticks::DAY_TICKS) > EconomyTime::MAX_DATE) return CMD_ERROR;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
std::vector<Vehicle *> vehs; std::vector<Vehicle *> vehs;

View File

@ -20,7 +20,7 @@
#include "company_func.h" #include "company_func.h"
#include "timer/timer.h" #include "timer/timer.h"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
#include "timer/timer_game_calendar.h" #include "timer/timer_game_economy.h"
#include "timer/timer_window.h" #include "timer/timer_window.h"
#include "date_gui.h" #include "date_gui.h"
#include "vehicle_gui.h" #include "vehicle_gui.h"
@ -192,7 +192,7 @@ static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID
* @param w the window related to the setting of the date * @param w the window related to the setting of the date
* @param date the actually chosen date * @param date the actually chosen date
*/ */
static void ChangeTimetableStartCallback(const Window *w, TimerGameCalendar::Date date, void *data) static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
{ {
Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, (VehicleID)w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date)); Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, (VehicleID)w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date));
} }
@ -235,7 +235,7 @@ struct TimetableWindow : Window {
TimerGameTick::Ticks start_time = -v->current_order_time; TimerGameTick::Ticks start_time = -v->current_order_time;
/* If arrival and departure times are in days, compensate for the current date_fract. */ /* If arrival and departure times are in days, compensate for the current date_fract. */
if (_settings_client.gui.timetable_mode != TimetableMode::Seconds) start_time += TimerGameCalendar::date_fract; if (_settings_client.gui.timetable_mode != TimetableMode::Seconds) start_time += TimerGameEconomy::date_fract;
FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time); FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time);
@ -252,7 +252,7 @@ struct TimetableWindow : Window {
SetDParamMaxDigits(1, 4, FS_SMALL); SetDParamMaxDigits(1, 4, FS_SMALL);
size->width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE).width) + WidgetDimensions::scaled.hsep_wide + padding.width; size->width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE).width) + WidgetDimensions::scaled.hsep_wide + padding.width;
} else { } else {
SetDParamMaxValue(1, TimerGameCalendar::DateAtStartOfYear(CalendarTime::MAX_YEAR), 0, FS_SMALL); SetDParamMaxValue(1, TimerGameEconomy::DateAtStartOfYear(EconomyTime::MAX_YEAR), 0, FS_SMALL);
size->width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_DATE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_DATE).width) + WidgetDimensions::scaled.hsep_wide + padding.width; size->width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_DATE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_DATE).width) + WidgetDimensions::scaled.hsep_wide + padding.width;
} }
FALLTHROUGH; FALLTHROUGH;
@ -525,7 +525,7 @@ struct TimetableWindow : Window {
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK); DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK);
} else { } else {
/* Show a date. */ /* Show a date. */
SetDParam(1, TimerGameCalendar::date + (arr_dep[i / 2].arrival + this_offset) / Ticks::DAY_TICKS); SetDParam(1, TimerGameEconomy::date + (arr_dep[i / 2].arrival + this_offset) / Ticks::DAY_TICKS);
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_DATE, i == selected ? TC_WHITE : TC_BLACK); DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_DATE, i == selected ? TC_WHITE : TC_BLACK);
} }
} }
@ -538,7 +538,7 @@ struct TimetableWindow : Window {
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK); DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK);
} else { } else {
/* Show a date. */ /* Show a date. */
SetDParam(1, TimerGameCalendar::date + (arr_dep[i / 2].departure + offset) / Ticks::DAY_TICKS); SetDParam(1, TimerGameEconomy::date + (arr_dep[i / 2].departure + offset) / Ticks::DAY_TICKS);
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_DATE, i == selected ? TC_WHITE : TC_BLACK); DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_DATE, i == selected ? TC_WHITE : TC_BLACK);
} }
} }
@ -572,7 +572,7 @@ struct TimetableWindow : Window {
SetDParam(0, (static_cast<TimerGameTick::Ticks>(v->timetable_start - TimerGameTick::counter) / Ticks::TICKS_PER_SECOND)); SetDParam(0, (static_cast<TimerGameTick::Ticks>(v->timetable_start - TimerGameTick::counter) / Ticks::TICKS_PER_SECOND));
DrawString(tr, STR_TIMETABLE_STATUS_START_IN_SECONDS); DrawString(tr, STR_TIMETABLE_STATUS_START_IN_SECONDS);
} else { } else {
/* Calendar units use dates. */ /* Other units use dates. */
SetDParam(0, STR_JUST_DATE_TINY); SetDParam(0, STR_JUST_DATE_TINY);
SetDParam(1, GetDateFromStartTick(v->timetable_start)); SetDParam(1, GetDateFromStartTick(v->timetable_start));
DrawString(tr, STR_TIMETABLE_STATUS_START_AT_DATE); DrawString(tr, STR_TIMETABLE_STATUS_START_AT_DATE);
@ -643,7 +643,7 @@ struct TimetableWindow : Window {
this->change_timetable_all = _ctrl_pressed; this->change_timetable_all = _ctrl_pressed;
ShowQueryString(STR_EMPTY, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED); ShowQueryString(STR_EMPTY, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
} else { } else {
ShowSetDateWindow(this, v->index, TimerGameCalendar::date, TimerGameCalendar::year, TimerGameCalendar::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed))); ShowSetDateWindow(this, v->index, TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed)));
} }
break; break;