changeset 10744:0118ca646690 draft

(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
author smatz <smatz@openttd.org>
date Tue, 13 Jan 2009 22:58:03 +0000
parents fbe80daca86b
children 0a66e2976522
files src/ai/ai.hpp src/ai/ai_core.cpp src/ai/api/ai_engine.cpp src/ai/api/ai_engine.hpp src/ai/api/ai_event_types.hpp src/ai/api/ai_vehicle.hpp src/aircraft_cmd.cpp src/date.cpp src/date_type.h src/depot_gui.cpp src/engine.cpp src/order_cmd.cpp src/rail.cpp src/road.cpp src/roadveh_cmd.cpp src/saveload/oldloader.cpp src/ship_cmd.cpp src/train_cmd.cpp src/vehicle.cpp src/vehicle_gui.cpp
diffstat 20 files changed, 55 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 	 * 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,
--- a/src/ai/ai_core.cpp
+++ b/src/ai/ai_core.cpp
@@ -235,7 +235,7 @@
 	}
 
 	/* 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)
--- 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)
--- a/src/ai/api/ai_engine.hpp
+++ b/src/ai/api/ai_engine.hpp
@@ -117,7 +117,7 @@
 	 * @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);
 
--- a/src/ai/api/ai_event_types.hpp
+++ b/src/ai/api/ai_event_types.hpp
@@ -286,7 +286,7 @@
 	/**
 	 * 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();
 
--- a/src/ai/api/ai_vehicle.hpp
+++ b/src/ai/api/ai_vehicle.hpp
@@ -218,7 +218,7 @@
 	 * @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.
 	 */
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -377,7 +377,7 @@
 		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 @@
 
 	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;
--- a/src/date.cpp
+++ b/src/date.cpp
@@ -84,35 +84,35 @@
 	 */
 
 	/* 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 @@
 	/* 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 @@
 		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;
 
--- 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
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -302,7 +302,7 @@
 		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)
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -246,7 +246,7 @@
 
 		/* 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 @@
 				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) {
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -1597,7 +1597,7 @@
 		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();
--- a/src/rail.cpp
+++ b/src/rail.cpp
@@ -201,7 +201,7 @@
 		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) {
--- a/src/road.cpp
+++ b/src/road.cpp
@@ -104,7 +104,7 @@
 		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);
 		}
 	}
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -254,7 +254,7 @@
 		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 @@
 	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;
--- a/src/saveload/oldloader.cpp
+++ b/src/saveload/oldloader.cpp
@@ -1514,7 +1514,7 @@
 	 * 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;
 		}
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -172,7 +172,7 @@
 
 	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 @@
 		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;
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -829,7 +829,7 @@
 			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 @@
 
 		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;
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -959,15 +959,18 @@
 	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);
 	}
 }
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -124,7 +124,7 @@
 	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 @@
 		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 @@
 		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);