Changeset - r10744:a93a9430d707
[Not reviewed]
master
0 20 0
smatz - 16 years ago 2009-01-13 22:58:03
smatz@openttd.org
(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
20 files changed with 55 insertions and 47 deletions:
0 comments (0 inline, 0 general)
src/ai/ai.hpp
Show inline comments
 
@@ -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,
src/ai/ai_core.cpp
Show inline comments
 
@@ -235,7 +235,7 @@ void CcAI(bool success, TileIndex tile, 
 
	}
 

	
 
	/* 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)
src/ai/api/ai_engine.cpp
Show inline comments
 
@@ -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)
src/ai/api/ai_engine.hpp
Show inline comments
 
@@ -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);
 

	
src/ai/api/ai_event_types.hpp
Show inline comments
 
@@ -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();
 

	
src/ai/api/ai_vehicle.hpp
Show inline comments
 
@@ -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.
 
	 */
src/aircraft_cmd.cpp
Show inline comments
 
@@ -377,7 +377,7 @@ CommandCost CmdBuildAircraft(TileIndex t
 
		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;
src/date.cpp
Show inline comments
 
@@ -84,35 +84,35 @@ void ConvertDateToYMD(Date date, YearMon
 
	 */
 

	
 
	/* 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 m
 
	/* 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;
 

	
src/date_type.h
Show inline comments
 
@@ -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
src/depot_gui.cpp
Show inline comments
 
@@ -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)
src/engine.cpp
Show inline comments
 
@@ -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) {
src/order_cmd.cpp
Show inline comments
 
@@ -1597,7 +1597,7 @@ VehicleOrderID ProcessConditionalOrder(c
 
		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();
src/rail.cpp
Show inline comments
 
@@ -201,7 +201,7 @@ RailTypes GetCompanyRailtypes(CompanyID 
 
		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) {
src/road.cpp
Show inline comments
 
@@ -104,7 +104,7 @@ RoadTypes GetCompanyRoadtypes(CompanyID 
 
		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);
 
		}
 
	}
src/roadveh_cmd.cpp
Show inline comments
 
@@ -254,7 +254,7 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
		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;
src/saveload/oldloader.cpp
Show inline comments
 
@@ -1514,7 +1514,7 @@ static bool LoadOldMain(LoadgameState *l
 
	 * 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;
 
		}
src/ship_cmd.cpp
Show inline comments
 
@@ -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,
 
		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;
src/train_cmd.cpp
Show inline comments
 
@@ -829,7 +829,7 @@ CommandCost CmdBuildRailVehicle(TileInde
 
			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;
src/vehicle.cpp
Show inline comments
 
@@ -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);
 
	}
 
}
src/vehicle_gui.cpp
Show inline comments
 
@@ -124,7 +124,7 @@ void DrawVehicleProfitButton(const Vehic
 
	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::DrawVehicleL
 
		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);
 

	
0 comments (0 inline, 0 general)