From e00cf92f47837d26d5b0fffb0668c5c717b3bacc Mon Sep 17 00:00:00 2001 From: smatz Date: Tue, 13 Jan 2009 22:58:03 +0000 Subject: [PATCH] (svn r15077) -Codechange: enumify DAYS_IN_YEAR and DAYS_IN_LEAP_YEAR -Change: when computing daily running cost, divide by 365 (instead of 364). Since r12134, the rounding errors don't need this correction anymore --- src/ai/ai.hpp | 7 ++++--- src/ai/ai_core.cpp | 2 +- src/ai/api/ai_engine.cpp | 2 +- src/ai/api/ai_engine.hpp | 2 +- src/ai/api/ai_event_types.hpp | 2 +- src/ai/api/ai_vehicle.hpp | 2 +- src/aircraft_cmd.cpp | 4 ++-- src/date.cpp | 28 ++++++++++++++-------------- src/date_type.h | 8 ++++++-- src/depot_gui.cpp | 2 +- src/engine.cpp | 4 ++-- src/order_cmd.cpp | 2 +- src/rail.cpp | 2 +- src/road.cpp | 2 +- src/roadveh_cmd.cpp | 4 ++-- src/saveload/oldloader.cpp | 2 +- src/ship_cmd.cpp | 4 ++-- src/train_cmd.cpp | 4 ++-- src/vehicle.cpp | 9 ++++++--- src/vehicle_gui.cpp | 10 +++++----- 20 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/ai/ai.hpp b/src/ai/ai.hpp index 9af2e3cf11..71690bc051 100644 --- a/src/ai/ai.hpp +++ b/src/ai/ai.hpp @@ -6,6 +6,7 @@ #define AI_HPP #include "api/ai_event_types.hpp" +#include "../date_type.h" #ifndef AI_CONFIG_HPP struct ltstr { bool operator()(const char *s1, const char *s2) const { return strcmp(s1, s2) < 0; } }; @@ -21,9 +22,9 @@ public: * The default months AIs start after eachother. */ enum StartNext { - START_NEXT_EASY = 1461, - START_NEXT_MEDIUM = 730, - START_NEXT_HARD = 365, + START_NEXT_EASY = DAYS_IN_YEAR * 3 + DAYS_IN_LEAP_YEAR, + START_NEXT_MEDIUM = DAYS_IN_YEAR * 2, + START_NEXT_HARD = DAYS_IN_YEAR, START_NEXT_MIN = 1, START_NEXT_MAX = 3600, START_NEXT_DEVIATION = 60, diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index 3c5843ab61..ffb2ad04b6 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -235,7 +235,7 @@ void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) } /* Currently no AI can be started, check again in a year. */ - return 365; + return DAYS_IN_YEAR; } /* static */ char *AI::GetConsoleList(char *p, const char *last) diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index 5215c95f34..c3f3aed3e2 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -186,7 +186,7 @@ { if (!IsValidEngine(engine_id)) return -1; - return ::GetEngine(engine_id)->lifelength * 366; + return ::GetEngine(engine_id)->lifelength * DAYS_IN_LEAP_YEAR; } /* static */ Money AIEngine::GetRunningCost(EngineID engine_id) diff --git a/src/ai/api/ai_engine.hpp b/src/ai/api/ai_engine.hpp index 67801107c3..1dfe76e088 100644 --- a/src/ai/api/ai_engine.hpp +++ b/src/ai/api/ai_engine.hpp @@ -117,7 +117,7 @@ public: * @param engine_id The engine to get the running cost of. * @pre IsValidEngine(engine_id). * @return The running cost of a vehicle per year. - * @note Cost is per year; divide by 364 to get per day. + * @note Cost is per year; divide by 365 to get per day. */ static Money GetRunningCost(EngineID engine_id); diff --git a/src/ai/api/ai_event_types.hpp b/src/ai/api/ai_event_types.hpp index c64b2a3678..13c004d979 100644 --- a/src/ai/api/ai_event_types.hpp +++ b/src/ai/api/ai_event_types.hpp @@ -286,7 +286,7 @@ public: /** * Get the running cost of the offered engine. * @return The running cost of the vehicle per year. - * @note Cost is per year; divide by 364 to get per day. + * @note Cost is per year; divide by 365 to get per day. */ Money GetRunningCost(); diff --git a/src/ai/api/ai_vehicle.hpp b/src/ai/api/ai_vehicle.hpp index 8120fe947f..84ddb4734f 100644 --- a/src/ai/api/ai_vehicle.hpp +++ b/src/ai/api/ai_vehicle.hpp @@ -218,7 +218,7 @@ public: * @param vehicle_id The vehicle to get the age of. * @pre IsValidVehicle(vehicle_id). * @return The running cost of the vehicle per year. - * @note Cost is per year; divide by 364 to get per day. + * @note Cost is per year; divide by 365 to get per day. * @note This is not equal to AIEngine::GetRunningCost for Trains, because * wagons and second engines can add up in the calculation too. */ diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index fd22460b25..ac6934837e 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -377,7 +377,7 @@ CommandCost CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const Engine *e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->max_age = e->lifelength * 366; + v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; _new_vehicle_id = v->index; @@ -643,7 +643,7 @@ void Aircraft::OnNewDay() if (this->running_ticks == 0) return; - CommandCost cost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running * this->running_ticks / (364 * DAY_TICKS)); + CommandCost cost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS)); this->profit_this_year -= cost.GetCost(); this->running_ticks = 0; diff --git a/src/date.cpp b/src/date.cpp index f06640d80b..7b442b04b8 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -84,35 +84,35 @@ void ConvertDateToYMD(Date date, YearMonthDay *ymd) */ /* There are 97 leap years in 400 years */ - Year yr = 400 * (date / (365 * 400 + 97)); - int rem = date % (365 * 400 + 97); + Year yr = 400 * (date / (DAYS_IN_YEAR * 400 + 97)); + int rem = date % (DAYS_IN_YEAR * 400 + 97); uint16 x; - if (rem >= 365 * 100 + 25) { + if (rem >= DAYS_IN_YEAR * 100 + 25) { /* There are 25 leap years in the first 100 years after * every 400th year, as every 400th year is a leap year */ yr += 100; - rem -= 365 * 100 + 25; + rem -= DAYS_IN_YEAR * 100 + 25; /* There are 24 leap years in the next couple of 100 years */ - yr += 100 * (rem / (365 * 100 + 24)); - rem = (rem % (365 * 100 + 24)); + yr += 100 * (rem / (DAYS_IN_YEAR * 100 + 24)); + rem = (rem % (DAYS_IN_YEAR * 100 + 24)); } - if (!IsLeapYear(yr) && rem >= 365 * 4) { + if (!IsLeapYear(yr) && rem >= DAYS_IN_YEAR * 4) { /* The first 4 year of the century are not always a leap year */ yr += 4; - rem -= 365 * 4; + rem -= DAYS_IN_YEAR * 4; } /* There is 1 leap year every 4 years */ - yr += 4 * (rem / (365 * 4 + 1)); - rem = rem % (365 * 4 + 1); + yr += 4 * (rem / (DAYS_IN_YEAR * 4 + 1)); + rem = rem % (DAYS_IN_YEAR * 4 + 1); /* The last (max 3) years to account for; the first one * can be, but is not necessarily a leap year */ - while (rem >= (IsLeapYear(yr) ? 366 : 365)) { - rem -= IsLeapYear(yr) ? 366 : 365; + while (rem >= (IsLeapYear(yr) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR)) { + rem -= IsLeapYear(yr) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR; yr++; } @@ -149,7 +149,7 @@ Date ConvertYMDToDate(Year year, Month month, Day day) /* Account for the missing of the 29th of February in non-leap years */ if (!IsLeapYear(year) && days >= ACCUM_MAR) days--; - return year * 365 + nr_of_leap_years + days; + return year * DAYS_IN_YEAR + nr_of_leap_years + days; } /** Functions used by the IncreaseDate function */ @@ -284,7 +284,7 @@ void IncreaseDate() uint days_this_year; _cur_year--; - days_this_year = IsLeapYear(_cur_year) ? 366 : 365; + days_this_year = IsLeapYear(_cur_year) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR; _date -= days_this_year; FOR_ALL_VEHICLES(v) v->date_of_last_service -= days_this_year; diff --git a/src/date_type.h b/src/date_type.h index 67122b0b1b..53e1812398 100644 --- a/src/date_type.h +++ b/src/date_type.h @@ -11,7 +11,11 @@ * 1 tick is approximately 30 ms. * 1 day is thus about 2 seconds (74 * 30 = 2220) on a machine that can run OpenTTD normally */ -#define DAY_TICKS 74 +enum { + DAY_TICKS = 74, ///< ticks per day + DAYS_IN_YEAR = 365, ///< days per year + DAYS_IN_LEAP_YEAR = 366, ///< sometimes, you need one day more... +}; /* * ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR and DAYS_TILL_ORIGINAL_BASE_YEAR are @@ -31,7 +35,7 @@ * The offset in days from the '_date == 0' till * 'ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)' */ -#define DAYS_TILL_ORIGINAL_BASE_YEAR (365 * ORIGINAL_BASE_YEAR + ORIGINAL_BASE_YEAR / 4 - ORIGINAL_BASE_YEAR / 100 + ORIGINAL_BASE_YEAR / 400) +#define DAYS_TILL_ORIGINAL_BASE_YEAR (DAYS_IN_YEAR * ORIGINAL_BASE_YEAR + ORIGINAL_BASE_YEAR / 4 - ORIGINAL_BASE_YEAR / 100 + ORIGINAL_BASE_YEAR / 400) /* The absolute minimum & maximum years in OTTD */ #define MIN_YEAR 0 diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 866ce14e55..c33ab94047 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -302,7 +302,7 @@ struct DepotWindow : Window { DrawSprite((v->vehstatus & VS_STOPPED) ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, PAL_NONE, x + diff_x, y + diff_y); SetDParam(0, v->unitnumber); - DrawString(x, y + 2, (uint16)(v->max_age - 366) >= v->age ? STR_00E2 : STR_00E3, TC_FROMSTRING); + DrawString(x, y + 2, (uint16)(v->max_age - DAYS_IN_LEAP_YEAR) >= v->age ? STR_00E2 : STR_00E3, TC_FROMSTRING); } void DrawDepotWindow(Window *w) diff --git a/src/engine.cpp b/src/engine.cpp index ffecb7bacd..d35e9df546 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -246,7 +246,7 @@ void SetYearEngineAgingStops() /* Base year ending date on half the model life */ YearMonthDay ymd; - ConvertDateToYMD(ei->base_intro + (ei->lifelength * 366) / 2, &ymd); + ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd); _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year); } @@ -475,7 +475,7 @@ void EnginesMonthlyLoop() CalcEngineReliability(e); } - if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) { + if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) { /* Introduce it to all companies */ NewVehicleAvailable(e); } else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) { diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 84a6778a84..5e6f95d3d2 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1597,7 +1597,7 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, NULL), value); break; case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, v->reliability * 100 >> 16, value); break; case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed(), value); break; - case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / 366, value); break; + case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break; case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break; case OCV_UNCONDITIONALLY: skip_order = true; break; default: NOT_REACHED(); diff --git a/src/rail.cpp b/src/rail.cpp index 6e2b141e71..377397d97e 100644 --- a/src/rail.cpp +++ b/src/rail.cpp @@ -201,7 +201,7 @@ RailTypes GetCompanyRailtypes(CompanyID company) const EngineInfo *ei = &e->info; if (HasBit(ei->climates, _settings_game.game_creation.landscape) && - (HasBit(e->company_avail, company) || _date >= e->intro_date + 365)) { + (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) { const RailVehicleInfo *rvi = &e->u.rail; if (rvi->railveh_type != RAILVEH_WAGON) { diff --git a/src/road.cpp b/src/road.cpp index e8d3062fcd..f72440fefa 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -104,7 +104,7 @@ RoadTypes GetCompanyRoadtypes(CompanyID company) const EngineInfo *ei = &e->info; if (HasBit(ei->climates, _settings_game.game_creation.landscape) && - (HasBit(e->company_avail, company) || _date >= e->intro_date + 365)) { + (HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) { SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD); } } diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 4a21b30aa9..10a85cc04e 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -254,7 +254,7 @@ CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->max_age = e->lifelength * 366; + v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; _new_vehicle_id = v->index; v->name = NULL; @@ -2000,7 +2000,7 @@ void RoadVehicle::OnNewDay() if (this->running_ticks == 0) return; const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type); - CommandCost cost(EXPENSES_ROADVEH_RUN, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) * this->running_ticks / (364 * DAY_TICKS)); + CommandCost cost(EXPENSES_ROADVEH_RUN, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS)); this->profit_this_year -= cost.GetCost(); this->running_ticks = 0; diff --git a/src/saveload/oldloader.cpp b/src/saveload/oldloader.cpp index 930e8fa2b9..b6690d9c13 100644 --- a/src/saveload/oldloader.cpp +++ b/src/saveload/oldloader.cpp @@ -1514,7 +1514,7 @@ static bool LoadOldMain(LoadgameState *ls) * we will get a "new vehicle"-spree. */ Engine *e; FOR_ALL_ENGINES(e) { - if (_date >= (e->intro_date + 365)) { + if (_date >= (e->intro_date + DAYS_IN_YEAR)) { e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE; e->company_avail = (CompanyMask)-1; } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index a927dd5424..ca400db875 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -172,7 +172,7 @@ void Ship::OnNewDay() if (this->running_ticks == 0) return; - CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * DAY_TICKS)); + CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS)); this->profit_this_year -= cost.GetCost(); this->running_ticks = 0; @@ -795,7 +795,7 @@ CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, con e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->max_age = e->lifelength * 366; + v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; _new_vehicle_id = v->index; v->name = NULL; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 9877fad96d..7f8f1678ea 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -829,7 +829,7 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 const Engine *e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->max_age = e->lifelength * 366; + v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; v->name = NULL; v->u.rail.railtype = rvi->railtype; @@ -4456,7 +4456,7 @@ void Train::OnNewDay() if (this->running_ticks != 0) { /* running costs */ - CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (364 * DAY_TICKS)); + CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS)); this->profit_this_year -= cost.GetCost(); this->running_ticks = 0; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index f160a32807..3adee91213 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -959,15 +959,18 @@ void AgeVehicle(Vehicle *v) if (v->age < 65535) v->age++; int age = v->age - v->max_age; - if (age == 366 * 0 || age == 366 * 1 || age == 366 * 2 || age == 366 * 3 || age == 366 * 4) v->reliability_spd_dec <<= 1; + if (age == DAYS_IN_LEAP_YEAR * 0 || age == DAYS_IN_LEAP_YEAR * 1 || + age == DAYS_IN_LEAP_YEAR * 2 || age == DAYS_IN_LEAP_YEAR * 3 || age == DAYS_IN_LEAP_YEAR * 4) { + v->reliability_spd_dec <<= 1; + } InvalidateWindow(WC_VEHICLE_DETAILS, v->index); - if (age == -366) { + if (age == -DAYS_IN_LEAP_YEAR) { ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD); } else if (age == 0) { ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD); - } else if (age > 0 && (age % 366) == 0) { + } else if (age > 0 && (age % DAYS_IN_LEAP_YEAR) == 0) { ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND); } } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 1a02167d8c..b0f91f9370 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -124,7 +124,7 @@ void DrawVehicleProfitButton(const Vehicle *v, int x, int y) SpriteID pal; /* draw profit-based colored icons */ - if (v->age <= 365 * 2) { + if (v->age <= DAYS_IN_YEAR * 2) { pal = PALETTE_TO_GREY; } else if (v->GetDisplayProfitLastYear() < 0) { pal = PALETTE_TO_RED; @@ -783,7 +783,7 @@ void BaseVehicleListWindow::DrawVehicleListItems(int x, VehicleID selected_vehic if (v->IsInDepot()) { str = STR_021F; } else { - str = (v->age > v->max_age - 366) ? STR_00E3 : STR_00E2; + str = (v->age > v->max_age - DAYS_IN_LEAP_YEAR) ? STR_00E3 : STR_00E2; } SetDParam(0, v->unitnumber); @@ -1412,9 +1412,9 @@ struct VehicleDetailsWindow : Window { this->DrawWidgets(); /* Draw running cost */ - SetDParam(1, v->age / 366); - SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED); - SetDParam(2, v->max_age / 366); + SetDParam(1, v->age / DAYS_IN_LEAP_YEAR); + SetDParam(0, (v->age + DAYS_IN_YEAR < v->max_age) ? STR_AGE : STR_AGE_RED); + SetDParam(2, v->max_age / DAYS_IN_LEAP_YEAR); SetDParam(3, v->GetDisplayRunningCost()); DrawString(2, 15, _vehicle_translation_table[VST_VEHICLE_AGE_RUNNING_COST_YR][v->type], TC_FROMSTRING);