changeset 18238:3141f1ed78eb draft

(svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
author frosch <frosch@openttd.org>
date Tue, 01 Nov 2011 00:21:08 +0000
parents 594f7483c683
children e6cbcbf3780f
files src/aircraft_cmd.cpp src/articulated_vehicles.cpp src/economy.cpp src/newgrf_engine.cpp src/roadveh_cmd.cpp src/saveload/vehicle_sl.cpp src/ship_cmd.cpp src/table/newgrf_debug_data.h src/train_cmd.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_cmd.cpp src/vehicle_gui.cpp
diffstat 13 files changed, 45 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -151,7 +151,7 @@
 		SpriteID sprite = GetCustomVehicleSprite(this, direction);
 		if (sprite != 0) return sprite;
 
-		spritenum = Engine::Get(this->engine_type)->original_image_index;
+		spritenum = this->GetEngine()->original_image_index;
 	}
 
 	return direction + _aircraft_sprite[spritenum];
@@ -389,7 +389,7 @@
 
 Money Aircraft::GetRunningCost() const
 {
-	const Engine *e = Engine::Get(this->engine_type);
+	const Engine *e = this->GetEngine();
 	uint cost_factor = GetVehicleProperty(this, PROP_AIRCRAFT_RUNNING_COST_FACTOR, e->u.air.running_cost);
 	return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->grf_prop.grffile);
 }
@@ -1364,7 +1364,7 @@
 				/* an exerpt of ServiceAircraft, without the invisibility stuff */
 				v->date_of_last_service = _date;
 				v->breakdowns_since_last_service = 0;
-				v->reliability = Engine::Get(v->engine_type)->reliability;
+				v->reliability = v->GetEngine()->reliability;
 				SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
 			}
 		}
--- a/src/articulated_vehicles.cpp
+++ b/src/articulated_vehicles.cpp
@@ -251,7 +251,7 @@
  */
 void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
 {
-	const Engine *engine = Engine::Get(v->engine_type);
+	const Engine *engine = v->GetEngine();
 
 	uint32 purchase_refit_union, purchase_refit_intersection;
 	GetArticulatedRefitMasks(v->engine_type, true, &purchase_refit_union, &purchase_refit_intersection);
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1201,7 +1201,7 @@
 	for (Vehicle *v = front; v != NULL; v = v->Next()) {
 		if (v->cargo_cap == 0) continue;
 
-		const Engine *e = Engine::Get(v->engine_type);
+		const Engine *e = v->GetEngine();
 		byte load_amount = e->info.load_amount;
 
 		/* The default loadamount for mail is 1/4 of the load amount for passengers */
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -618,7 +618,7 @@
 			return (cs->classes << 16) | (cs->weight << 8) | GetEngineGRF(v->engine_type)->cargo_map[v->cargo_type];
 		}
 
-		case 0x48: return Engine::Get(v->engine_type)->flags; // Vehicle Type Info
+		case 0x48: return v->GetEngine()->flags; // Vehicle Type Info
 		case 0x49: return v->build_year;
 
 		case 0x4A: {
@@ -633,12 +633,12 @@
 		/* Variables which use the parameter */
 		case 0x60: // Count consist's engine ID occurance
 			//EngineID engine = GetNewEngineID(GetEngineGRF(v->engine_type), v->type, parameter);
-			if (v->type != VEH_TRAIN) return Engine::Get(v->engine_type)->grf_prop.local_id == parameter;
+			if (v->type != VEH_TRAIN) return v->GetEngine()->grf_prop.local_id == parameter;
 
 			{
 				uint count = 0;
 				for (; v != NULL; v = v->Next()) {
-					if (Engine::Get(v->engine_type)->grf_prop.local_id == parameter) count++;
+					if (v->GetEngine()->grf_prop.local_id == parameter) count++;
 				}
 				return count;
 			}
@@ -776,8 +776,8 @@
 		case 0x43: return GB(ClampToU16(v->max_age), 8, 8);
 		case 0x44: return Clamp(v->build_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
 		case 0x45: return v->unitnumber;
-		case 0x46: return Engine::Get(v->engine_type)->grf_prop.local_id;
-		case 0x47: return GB(Engine::Get(v->engine_type)->grf_prop.local_id, 8, 8);
+		case 0x46: return v->GetEngine()->grf_prop.local_id;
+		case 0x47: return GB(v->GetEngine()->grf_prop.local_id, 8, 8);
 		case 0x48:
 			if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum;
 			return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD;
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -127,7 +127,7 @@
 		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
 		if (sprite != 0) return sprite;
 
-		spritenum = Engine::Get(this->engine_type)->original_image_index;
+		spritenum = this->GetEngine()->original_image_index;
 	}
 
 	sprite = direction + _roadveh_images[spritenum];
@@ -1527,7 +1527,7 @@
 
 Money RoadVehicle::GetRunningCost() const
 {
-	const Engine *e = Engine::Get(this->engine_type);
+	const Engine *e = this->GetEngine();
 	if (e->u.road.running_cost_class == INVALID_PRICE) return 0;
 
 	uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, e->u.road.running_cost);
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -226,7 +226,7 @@
 			case VEH_ROAD:
 			case VEH_SHIP:
 			case VEH_AIRCRAFT:
-				if (v->engine_type >= total_engines || v->type != Engine::Get(v->engine_type)->type) {
+				if (v->engine_type >= total_engines || v->type != v->GetEngine()->type) {
 					v->engine_type = first_engine[v->type];
 				}
 				break;
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -107,7 +107,7 @@
 		SpriteID sprite = GetCustomVehicleSprite(this, direction);
 		if (sprite != 0) return sprite;
 
-		spritenum = Engine::Get(this->engine_type)->original_image_index;
+		spritenum = this->GetEngine()->original_image_index;
 	}
 
 	return _ship_sprites[spritenum] + direction;
@@ -190,7 +190,7 @@
 
 Money Ship::GetRunningCost() const
 {
-	const Engine *e = Engine::Get(this->engine_type);
+	const Engine *e = this->GetEngine();
 	uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
 	return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grf_prop.grffile);
 }
--- a/src/table/newgrf_debug_data.h
+++ b/src/table/newgrf_debug_data.h
@@ -61,12 +61,12 @@
 };
 
 class NIHVehicle : public NIHelper {
-	bool IsInspectable(uint index) const                 { return Engine::Get(Vehicle::Get(index)->engine_type)->grf_prop.grffile != NULL; }
+	bool IsInspectable(uint index) const                 { return Vehicle::Get(index)->GetEngine()->grf_prop.grffile != NULL; }
 	uint GetParent(uint index) const                     { const Vehicle *first = Vehicle::Get(index)->First(); return GetInspectWindowNumber(GetGrfSpecFeature(first->type), first->index); }
 	const void *GetInstance(uint index)const             { return Vehicle::Get(index); }
-	const void *GetSpec(uint index) const                { return Engine::Get(Vehicle::Get(index)->engine_type); }
+	const void *GetSpec(uint index) const                { return Vehicle::Get(index)->GetEngine(); }
 	void SetStringParameters(uint index) const           { this->SetSimpleStringParameters(STR_VEHICLE_NAME, index); }
-	uint32 GetGRFID(uint index) const                    { return (this->IsInspectable(index)) ? Engine::Get(Vehicle::Get(index)->engine_type)->grf_prop.grffile->grfid : 0; }
+	uint32 GetGRFID(uint index) const                    { return (this->IsInspectable(index)) ? Vehicle::Get(index)->GetEngine()->grf_prop.grffile->grfid : 0; }
 	void Resolve(ResolverObject *ro, uint32 index) const { extern void GetVehicleResolver(ResolverObject *ro, uint index); GetVehicleResolver(ro, index); }
 };
 
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -92,7 +92,7 @@
 static void RailVehicleLengthChanged(const Train *u)
 {
 	/* show a warning once for each engine in whole game and once for each GRF after each game load */
-	const Engine *engine = Engine::Get(u->engine_type);
+	const Engine *engine = u->GetEngine();
 	uint32 grfid = engine->grf_prop.grffile->grfid;
 	GRFConfig *grfconfig = GetGRFConfig(grfid);
 	if (GamelogGRFBugReverse(grfid, engine->grf_prop.local_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
@@ -189,7 +189,7 @@
 	}
 
 	for (Train *u = this; u != NULL; u = u->Next()) {
-		const Engine *e_u = Engine::Get(u->engine_type);
+		const Engine *e_u = u->GetEngine();
 		const RailVehicleInfo *rvi_u = &e_u->u.rail;
 
 		if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
@@ -455,7 +455,7 @@
 	int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
 	int vehicle_pitch = 0;
 
-	const Engine *e = Engine::Get(this->engine_type);
+	const Engine *e = this->GetEngine();
 	if (e->grf_prop.grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
 		reference_width = e->grf_prop.grffile->traininfo_vehicle_width;
 		vehicle_pitch = e->grf_prop.grffile->traininfo_vehicle_pitch;
@@ -484,7 +484,7 @@
 		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
 		if (sprite != 0) return sprite;
 
-		spritenum = Engine::Get(this->engine_type)->original_image_index;
+		spritenum = this->GetEngine()->original_image_index;
 	}
 
 	sprite = GetDefaultTrainSprite(spritenum, direction);
@@ -3687,7 +3687,7 @@
 	const Train *v = this;
 
 	do {
-		const Engine *e = Engine::Get(v->engine_type);
+		const Engine *e = v->GetEngine();
 		if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
 
 		uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -92,7 +92,7 @@
 {
 	v->date_of_last_service = _date;
 	v->breakdowns_since_last_service = 0;
-	v->reliability = Engine::Get(v->engine_type)->reliability;
+	v->reliability = v->GetEngine()->reliability;
 	SetWindowDirty(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated
 }
 
@@ -111,7 +111,7 @@
 	/* Are we ready for the next service cycle? */
 	const Company *c = Company::Get(this->owner);
 	if (c->settings.vehicle.servint_ispercent ?
-			(this->reliability >= Engine::Get(this->engine_type)->reliability * (100 - this->service_interval) / 100) :
+			(this->reliability >= this->GetEngine()->reliability * (100 - this->service_interval) / 100) :
 			(this->date_of_last_service + this->service_interval >= _date)) {
 		return false;
 	}
@@ -632,6 +632,16 @@
 }
 
 /**
+ * Retrieves the engine of the vehicle.
+ * @return Engine of the vehicle.
+ * @pre HasEngineType() == true
+ */
+const Engine *Vehicle::GetEngine() const
+{
+	return Engine::Get(this->engine_type);
+}
+
+/**
  * Handle the pathfinding result, especially the lost status.
  * If the vehicle is now lost and wasn't previously fire an
  * event to the AIs and a news message to the user. If the
@@ -1158,7 +1168,7 @@
 	if (v->Previous() != NULL || v->owner != _local_company || (v->vehstatus & VS_CRASHED) != 0) return;
 
 	/* Don't warn if a renew is active */
-	if (Company::Get(v->owner)->settings.engine_renew && Engine::Get(v->engine_type)->company_avail != 0) return;
+	if (Company::Get(v->owner)->settings.engine_renew && v->GetEngine()->company_avail != 0) return;
 
 	StringID str;
 	if (age == -DAYS_IN_LEAP_YEAR) {
@@ -1756,7 +1766,7 @@
 uint GetVehicleCapacity(const Vehicle *v, uint16 *mail_capacity)
 {
 	if (mail_capacity != NULL) *mail_capacity = 0;
-	const Engine *e = Engine::Get(v->engine_type);
+	const Engine *e = v->GetEngine();
 
 	if (!e->CanCarryCargo()) return 0;
 
@@ -2113,7 +2123,7 @@
 void Vehicle::UpdateVisualEffect(bool allow_power_change)
 {
 	bool powered_before = HasBit(this->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER);
-	const Engine *e = Engine::Get(this->engine_type);
+	const Engine *e = this->GetEngine();
 
 	/* Evaluate properties */
 	byte visual_effect;
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -338,6 +338,8 @@
 	 */
 	virtual bool IsPrimaryVehicle() const { return false; }
 
+	const Engine *GetEngine() const;
+
 	/**
 	 * Gets the sprite to show for the given direction
 	 * @param direction the direction the vehicle is facing
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -273,7 +273,7 @@
 	for (; v != NULL; v = (only_this ? NULL : v->Next())) {
 		if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index) && !only_this) continue;
 
-		const Engine *e = Engine::Get(v->engine_type);
+		const Engine *e = v->GetEngine();
 		if (!e->CanCarryCargo()) continue;
 
 		/* If the vehicle is not refittable, count its capacity nevertheless if the cargo matches */
@@ -789,7 +789,7 @@
 					break;
 				}
 			} else {
-				const Engine *e = Engine::Get(v->engine_type);
+				const Engine *e = v->GetEngine();
 				CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
 
 				if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -217,8 +217,8 @@
  */
 byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for)
 {
-	const Engine *e_from = Engine::Get(v_from->engine_type);
-	const Engine *e_for  = Engine::Get(v_for->engine_type);
+	const Engine *e_from = v_from->GetEngine();
+	const Engine *e_for  = v_for->GetEngine();
 
 	/* If one them doesn't carry cargo, there's no need to find a sub type */
 	if (!e_from->CanCarryCargo() || !e_for->CanCarryCargo()) return 0;
@@ -386,7 +386,7 @@
 
 		do {
 			if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index)) continue;
-			const Engine *e = Engine::Get(v->engine_type);
+			const Engine *e = v->GetEngine();
 			uint32 cmask = e->info.refit_mask;
 			byte callback_mask = e->info.callback_mask;