changeset 18239:e6cbcbf3780f draft

(svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
author frosch <frosch@openttd.org>
date Tue, 01 Nov 2011 00:23:41 +0000
parents 3141f1ed78eb
children 4ecb22f0d875
files src/aircraft_cmd.cpp src/articulated_vehicles.cpp src/build_vehicle_gui.cpp src/engine.cpp src/engine_base.h src/newgrf.cpp src/newgrf_engine.cpp src/newgrf_engine.h src/newgrf_sound.cpp src/roadveh_cmd.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 17 files changed, 84 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -391,7 +391,7 @@
 {
 	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);
+	return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->GetGRF());
 }
 
 void Aircraft::OnNewDay()
--- a/src/articulated_vehicles.cpp
+++ b/src/articulated_vehicles.cpp
@@ -37,7 +37,8 @@
 	if (callback == CALLBACK_FAILED || GB(callback, 0, 8) == 0xFF) return INVALID_ENGINE;
 
 	if (mirrored != NULL) *mirrored = HasBit(callback, 7);
-	return GetNewEngineID(GetEngineGRF(front_type), Engine::Get(front_type)->type, GB(callback, 0, 7));
+	const Engine *front_engine = Engine::Get(front_type);
+	return GetNewEngineID(front_engine->GetGRF(), front_engine->type, GB(callback, 0, 7));
 }
 
 /**
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -756,7 +756,7 @@
 	if (callback == CALLBACK_FAILED) return y;
 
 	StartTextRefStackUsage(6);
-	uint result = DrawStringMultiLine(left, right, y, INT32_MAX, GetGRFStringID(GetEngineGRFID(engine), 0xD000 + callback), TC_BLACK);
+	uint result = DrawStringMultiLine(left, right, y, INT32_MAX, GetGRFStringID(Engine::Get(engine)->GetGRFID(), 0xD000 + callback), TC_BLACK);
 	StopTextRefStackUsage();
 	return result;
 }
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -156,6 +156,17 @@
 }
 
 /**
+ * Retrieve the GRF ID of the NewGRF the engine is tied to.
+ * This is the GRF providing the Action 3.
+ * @return GRF ID of the associated NewGRF.
+ */
+uint32 Engine::GetGRFID() const
+{
+	const GRFFile *file = this->GetGRF();
+	return file == NULL ? 0 : file->grfid;
+}
+
+/**
  * Determines whether an engine can carry something.
  * A vehicle cannot carry anything if its capacity is zero, or none of the possible cargos is available in the climate.
  * @return true if the vehicle can carry something.
@@ -265,7 +276,7 @@
 		default: NOT_REACHED();
 	}
 
-	return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
+	return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
 }
 
 /**
@@ -305,7 +316,7 @@
 		default: NOT_REACHED();
 	}
 
-	return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
+	return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
 }
 
 /**
--- a/src/engine_base.h
+++ b/src/engine_base.h
@@ -101,6 +101,18 @@
 	{
 		return this->type == VEH_TRAIN || this->type == VEH_ROAD;
 	}
+
+	/**
+	 * Retrieve the NewGRF the engine is tied to.
+	 * This is the GRF providing the Action 3.
+	 * @return NewGRF associated to the engine.
+	 */
+	const GRFFile *GetGRF() const
+	{
+		return this->grf_prop.grffile;
+	}
+
+	uint32 GetGRFID() const;
 };
 
 struct EngineIDMapping {
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -7956,7 +7956,7 @@
 		if (_gted[engine].refitmask_valid) {
 			if (ei->refit_mask != 0) {
 				const GRFFile *file = _gted[engine].refitmask_grf;
-				if (file == NULL) file = e->grf_prop.grffile;
+				if (file == NULL) file = e->GetGRF();
 				if (file != NULL && file->cargo_max != 0) {
 					/* Apply cargo translation table to the refit mask */
 					uint num_cargo = min(32, file->cargo_max);
@@ -8029,7 +8029,7 @@
 	Engine *e;
 
 	FOR_ALL_ENGINES(e) {
-		if (e->grf_prop.grffile == NULL) {
+		if (e->GetGRF() == NULL) {
 			const EngineIDMapping &eid = _engine_mngr[e->index];
 			if (eid.grfid != INVALID_GRFID || eid.internal_id != eid.substitute_id) {
 				e->info.string_id = STR_NEWGRF_INVALID_ENGINE;
@@ -8039,7 +8039,7 @@
 		/* When the train does not set property 27 (misc flags), but it
 		 * is overridden by a NewGRF graphically we want to disable the
 		 * flipping possibility. */
-		if (e->type == VEH_TRAIN && !_gted[e->index].prop27_set && e->grf_prop.grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
+		if (e->type == VEH_TRAIN && !_gted[e->index].prop27_set && e->GetGRF() != NULL && is_custom_sprite(e->u.rail.image_index)) {
 			ClrBit(e->info.misc_flags, EF_RAIL_FLIPS);
 		}
 
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -111,29 +111,6 @@
 }
 
 
-/**
- * Retrieve the GRFFile tied to an engine
- * @param engine Engine ID to retrieve.
- * @return Pointer to GRFFile.
- */
-const GRFFile *GetEngineGRF(EngineID engine)
-{
-	return Engine::Get(engine)->grf_prop.grffile;
-}
-
-
-/**
- * Retrieve the GRF ID of the GRFFile tied to an engine
- * @param engine Engine ID to retrieve.
- * @return 32 bit GRFID value.
- */
-uint32 GetEngineGRFID(EngineID engine)
-{
-	const GRFFile *file = GetEngineGRF(engine);
-	return file == NULL ? 0 : file->grfid;
-}
-
-
 static int MapOldSubType(const Vehicle *v)
 {
 	switch (v->type) {
@@ -488,7 +465,7 @@
 	/* Calculated vehicle parameters */
 	switch (variable) {
 		case 0x25: // Get engine GRF ID
-			return GetEngineGRFID(v->engine_type);
+			return v->GetGRFID();
 
 		case 0x40: // Get length of consist
 			if (!HasBit(v->grf_cache.cache_valid, NCVV_POSITION_CONSIST_LENGTH)) {
@@ -615,7 +592,7 @@
 			 */
 			const CargoSpec *cs = CargoSpec::Get(v->cargo_type);
 
-			return (cs->classes << 16) | (cs->weight << 8) | GetEngineGRF(v->engine_type)->cargo_map[v->cargo_type];
+			return (cs->classes << 16) | (cs->weight << 8) | v->GetGRF()->cargo_map[v->cargo_type];
 		}
 
 		case 0x48: return v->GetEngine()->flags; // Vehicle Type Info
@@ -632,8 +609,7 @@
 
 		/* 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 v->GetEngine()->grf_prop.local_id == parameter;
+			if (v->type != VEH_TRAIN) return v->GetEngine()->grf_prop.local_id == parameter ? 1 : 0;
 
 			{
 				uint count = 0;
@@ -875,7 +851,7 @@
 				CargoID cargo_type = e->GetDefaultCargoType();
 				if (cargo_type != CT_INVALID) {
 					const CargoSpec *cs = CargoSpec::Get(cargo_type);
-					return (cs->classes << 16) | (cs->weight << 8) | GetEngineGRF(e->index)->cargo_map[cargo_type];
+					return (cs->classes << 16) | (cs->weight << 8) | e->GetGRF()->cargo_map[cargo_type];
 				} else {
 					return 0x000000FF;
 				}
@@ -941,7 +917,7 @@
 	res->ResetState();
 
 	const Engine *e = Engine::Get(engine_type);
-	res->grffile         = (e != NULL ? e->grf_prop.grffile : NULL);
+	res->grffile         = (e != NULL ? e->GetGRF() : NULL);
 }
 
 
@@ -1199,10 +1175,8 @@
 uint ListPositionOfEngine(EngineID engine)
 {
 	const Engine *e = Engine::Get(engine);
-	if (e->grf_prop.grffile == NULL) return e->list_position;
-
 	/* Crude sorting to group by GRF ID */
-	return (e->grf_prop.grffile->grfid * 256) + e->list_position;
+	return (e->GetGRFID() * 256) + e->list_position;
 }
 
 struct ListOrderChange {
@@ -1239,7 +1213,7 @@
 		/* Populate map with current list positions */
 		Engine *e;
 		FOR_ALL_ENGINES_OF_TYPE(e, source_e->type) {
-			if (!_settings_game.vehicle.dynamic_engines || e->grf_prop.grffile == source_e->grf_prop.grffile) {
+			if (!_settings_game.vehicle.dynamic_engines || e->GetGRF() == source_e->GetGRF()) {
 				if (e->grf_prop.local_id == target) target_e = e;
 				lptr_map[e->list_position] = e;
 			}
--- a/src/newgrf_engine.h
+++ b/src/newgrf_engine.h
@@ -36,8 +36,6 @@
 struct GRFFile;
 
 void SetEngineGRF(EngineID engine, const struct GRFFile *file);
-const struct GRFFile *GetEngineGRF(EngineID engine);
-uint32 GetEngineGRFID(EngineID engine);
 
 uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v);
 uint16 GetVehicleCallbackParent(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v, const Vehicle *parent);
--- a/src/newgrf_sound.cpp
+++ b/src/newgrf_sound.cpp
@@ -59,7 +59,7 @@
  */
 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
 {
-	const GRFFile *file = GetEngineGRF(v->engine_type);
+	const GRFFile *file = v->GetGRF();
 	uint16 callback;
 
 	/* If the engine has no GRF ID associated it can't ever play any new sounds */
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -1533,7 +1533,7 @@
 	uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, e->u.road.running_cost);
 	if (cost_factor == 0) return 0;
 
-	return GetPrice(e->u.road.running_cost_class, cost_factor, e->grf_prop.grffile);
+	return GetPrice(e->u.road.running_cost_class, cost_factor, e->GetGRF());
 }
 
 bool RoadVehicle::Tick()
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -192,7 +192,7 @@
 {
 	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);
+	return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
 }
 
 void Ship::OnNewDay()
--- 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 Vehicle::Get(index)->GetEngine()->grf_prop.grffile != NULL; }
+	bool IsInspectable(uint index) const                 { return Vehicle::Get(index)->GetGRF() != 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 Vehicle::Get(index)->GetEngine(); }
 	void SetStringParameters(uint index) const           { this->SetSimpleStringParameters(STR_VEHICLE_NAME, index); }
-	uint32 GetGRFID(uint index) const                    { return (this->IsInspectable(index)) ? Vehicle::Get(index)->GetEngine()->grf_prop.grffile->grfid : 0; }
+	uint32 GetGRFID(uint index) const                    { return Vehicle::Get(index)->GetGRFID(); }
 	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
@@ -456,9 +456,9 @@
 	int vehicle_pitch = 0;
 
 	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;
+	if (e->GetGRF() != NULL && is_custom_sprite(e->u.rail.image_index)) {
+		reference_width = e->GetGRF()->traininfo_vehicle_width;
+		vehicle_pitch = e->GetGRF()->traininfo_vehicle_pitch;
 	}
 
 	if (offset != NULL) {
@@ -503,8 +503,8 @@
 	if (is_custom_sprite(spritenum)) {
 		SpriteID sprite = GetCustomVehicleIcon(engine, dir);
 		if (sprite != 0) {
-			if (e->grf_prop.grffile != NULL) {
-				y += e->grf_prop.grffile->traininfo_vehicle_pitch;
+			if (e->GetGRF() != NULL) {
+				y += e->GetGRF()->traininfo_vehicle_pitch;
 			}
 			return sprite;
 		}
@@ -993,7 +993,7 @@
 				StringID error = STR_NULL;
 
 				if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
-				if (callback  < 0xFD) error = GetGRFStringID(GetEngineGRFID(head->engine_type), 0xD000 + callback);
+				if (callback  < 0xFD) error = GetGRFStringID(head->GetGRFID(), 0xD000 + callback);
 
 				if (error != STR_NULL) return_cmd_error(error);
 			}
@@ -3696,7 +3696,7 @@
 		/* Halve running cost for multiheaded parts */
 		if (v->IsMultiheaded()) cost_factor /= 2;
 
-		cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->grf_prop.grffile);
+		cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->GetGRF());
 	} while ((v = v->GetNextVehicle()) != NULL);
 
 	return cost;
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -211,8 +211,7 @@
 void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical)
 {
 	const Engine *e = Engine::Get(engine);
-	uint32 grfid = e->grf_prop.grffile->grfid;
-	GRFConfig *grfconfig = GetGRFConfig(grfid);
+	GRFConfig *grfconfig = GetGRFConfig(e->GetGRFID());
 
 	if (!HasBit(grfconfig->grf_bugs, bug_type)) {
 		SetBit(grfconfig->grf_bugs, bug_type);
@@ -642,6 +641,26 @@
 }
 
 /**
+ * Retrieve the NewGRF the vehicle is tied to.
+ * This is the GRF providing the Action 3 for the engine type.
+ * @return NewGRF associated to the vehicle.
+ */
+const GRFFile *Vehicle::GetGRF() const
+{
+	return this->GetEngine()->GetGRF();
+}
+
+/**
+ * Retrieve the GRF ID of the NewGRF the vehicle is tied to.
+ * This is the GRF providing the Action 3 for the engine type.
+ * @return GRF ID of the associated NewGRF.
+ */
+uint32 Vehicle::GetGRFID() const
+{
+	return this->GetEngine()->GetGRFID();
+}
+
+/**
  * 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
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -118,6 +118,8 @@
 extern bool LoadOldVehicle(LoadgameState *ls, int num);
 extern void FixOldVehicles();
 
+struct GRFFile;
+
 /** %Vehicle data structure. */
 struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle {
 private:
@@ -347,6 +349,9 @@
 	 */
 	virtual SpriteID GetImage(Direction direction) const { return 0; }
 
+	const GRFFile *GetGRF() const;
+	uint32 GetGRFID() const;
+
 	/**
 	 * Invalidates cached NewGRF variables
 	 * @see InvalidateNewGRFCacheOfChain
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -241,7 +241,7 @@
 
 		default: NOT_REACHED();
 	}
-	return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grf_prop.grffile, -10));
+	return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->GetGRF(), -10));
 }
 
 /**
@@ -448,7 +448,7 @@
 	 * return 0xFF if it can. */
 	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 	if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
-		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
+		StringID error = GetGRFStringID(v->GetGRFID(), 0xD000 + callback);
 		return_cmd_error(error);
 	}
 
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -276,7 +276,7 @@
 	CargoID cargo;    ///< Cargo to refit to
 	byte subtype;     ///< Subcargo to use
 	uint16 value;     ///< GRF-local String to display for the cargo
-	EngineID engine;  ///< Engine for which to resolve #value
+	const Engine *engine;  ///< Engine for which to resolve #value
 
 	/**
 	 * Inequality operator for #RefitOption.
@@ -330,7 +330,7 @@
 			SetDParam(0, CargoSpec::Get(refit.cargo)->name);
 			/* If the callback succeeded, draw the cargo suffix. */
 			if (refit.value != CALLBACK_FAILED) {
-				SetDParam(1, GetGRFStringID(GetEngineGRFID(refit.engine), 0xD000 + refit.value));
+				SetDParam(1, GetGRFStringID(refit.engine->GetGRFID(), 0xD000 + refit.value));
 				DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y, STR_JUST_STRING_SPACE_STRING, colour);
 			} else {
 				DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y, STR_JUST_STRING, colour);
@@ -428,7 +428,7 @@
 						option.cargo   = cid;
 						option.subtype = refit_cyc;
 						option.value   = callback;
-						option.engine  = v->engine_type;
+						option.engine  = v->GetEngine();
 						this->list[current_index].Include(option);
 					}
 
@@ -445,7 +445,7 @@
 					option.cargo   = cid;
 					option.subtype = 0;
 					option.value   = CALLBACK_FAILED;
-					option.engine  = INVALID_ENGINE;
+					option.engine  = NULL;
 					this->list[current_index].Include(option);
 				}
 				current_index++;
@@ -976,7 +976,7 @@
 	if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) {
 		uint16 cb = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, v->engine_type, v);
 		if (cb != CALLBACK_FAILED) {
-			return GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + cb);
+			return GetGRFStringID(v->GetGRFID(), 0xD000 + cb);
 		}
 	}
 	return STR_EMPTY;