changeset 15763:a60a50f29f64 draft

(svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
author rubidium <rubidium@openttd.org>
date Tue, 10 Aug 2010 15:54:53 +0000
parents 640ed6b3475c
children c4c6c235d9e6
files src/aircraft_cmd.cpp src/engine.cpp src/engine_base.h src/newgrf.cpp src/newgrf_engine.cpp src/roadveh_cmd.cpp src/ship_cmd.cpp src/table/newgrf_debug_data.h src/train_cmd.cpp src/vehicle.cpp src/vehicle_cmd.cpp
diffstat 11 files changed, 48 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -540,7 +540,7 @@
 {
 	const Engine *e = Engine::Get(this->engine_type);
 	uint cost_factor = GetVehicleProperty(this, PROP_AIRCRAFT_RUNNING_COST_FACTOR, e->u.air.running_cost);
-	return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->grffile);
+	return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->grf_prop.grffile);
 }
 
 void Aircraft::OnNewDay()
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -74,7 +74,7 @@
 Engine::Engine(VehicleType type, EngineID base)
 {
 	this->type = type;
-	this->internal_id = base;
+	this->grf_prop.local_id = base;
 	this->list_position = base;
 
 	/* Check if this base engine is within the original engine data range */
@@ -241,7 +241,7 @@
 		default: NOT_REACHED();
 	}
 
-	return GetPrice(base_price, cost_factor, this->grffile, -8);
+	return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
 }
 
 /**
@@ -281,7 +281,7 @@
 		default: NOT_REACHED();
 	}
 
-	return GetPrice(base_price, cost_factor, this->grffile, -8);
+	return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
 }
 
 /**
--- a/src/engine_base.h
+++ b/src/engine_base.h
@@ -17,6 +17,7 @@
 #include "vehicle_type.h"
 #include "core/pool_type.hpp"
 #include "core/smallvec_type.hpp"
+#include "newgrf_commons.h"
 
 typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
 extern EnginePool _engine_pool;
@@ -46,9 +47,13 @@
 	} u;
 
 	/* NewGRF related data */
-	const struct GRFFile *grffile;
-	const struct SpriteGroup *group[NUM_CARGO + 2];
-	uint16 internal_id;                             ///< ID within the GRF file
+	/**
+	 * Properties related the the grf file.
+	 * NUM_CARGO real cargo plus two pseudo cargo sprite groups.
+	 * Used for obtaining the sprite offset of custom sprites, and for
+	 * evaluating callbacks.
+	 */
+	GRFFilePropsBase<NUM_CARGO + 2> grf_prop;
 	uint16 overrides_count;
 	struct WagonOverride *overrides;
 	uint16 list_position;
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -357,7 +357,7 @@
 		EngineID engine = _engine_mngr.GetID(type, internal_id, scope_grfid);
 		if (engine != INVALID_ENGINE) {
 			Engine *e = Engine::Get(engine);
-			if (e->grffile == NULL) e->grffile = file;
+			if (e->grf_prop.grffile == NULL) e->grf_prop.grffile = file;
 			return e;
 		}
 	}
@@ -367,8 +367,8 @@
 	if (engine != INVALID_ENGINE) {
 		Engine *e = Engine::Get(engine);
 
-		if (e->grffile == NULL) {
-			e->grffile = file;
+		if (e->grf_prop.grffile == NULL) {
+			e->grf_prop.grffile = file;
 			grfmsg(5, "Replaced engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
 		}
 
@@ -387,7 +387,7 @@
 
 	/* ... it's not, so create a new one based off an existing engine */
 	Engine *e = new Engine(type, internal_id);
-	e->grffile = file;
+	e->grf_prop.grffile = file;
 
 	/* Reserve the engine slot */
 	assert(_engine_mngr.Length() == e->index);
@@ -6925,7 +6925,7 @@
 		/* Did the newgrf specify any refitting? If not, use defaults. */
 		if (_gted[engine].refitmask_valid) {
 			if (ei->refit_mask != 0) {
-				const GRFFile *file = e->grffile;
+				const GRFFile *file = e->grf_prop.grffile;
 				if (file != NULL && file->cargo_max != 0) {
 					/* Apply cargo translation table to the refit mask */
 					uint num_cargo = min(32, file->cargo_max);
@@ -6987,7 +6987,7 @@
 	Engine *e;
 
 	FOR_ALL_ENGINES(e) {
-		if (e->grffile == NULL) {
+		if (e->grf_prop.grffile == 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;
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -92,12 +92,12 @@
 void SetCustomEngineSprites(EngineID engine, byte cargo, const SpriteGroup *group)
 {
 	Engine *e = Engine::Get(engine);
-	assert(cargo < lengthof(e->group));
+	assert(cargo < lengthof(e->grf_prop.spritegroup));
 
-	if (e->group[cargo] != NULL) {
+	if (e->grf_prop.spritegroup[cargo] != NULL) {
 		grfmsg(6, "SetCustomEngineSprites: engine %d cargo %d already has group -- replacing", engine, cargo);
 	}
-	e->group[cargo] = group;
+	e->grf_prop.spritegroup[cargo] = group;
 }
 
 
@@ -110,7 +110,7 @@
 void SetEngineGRF(EngineID engine, const GRFFile *file)
 {
 	Engine *e = Engine::Get(engine);
-	e->grffile = file;
+	e->grf_prop.grffile = file;
 }
 
 
@@ -121,7 +121,7 @@
  */
 const GRFFile *GetEngineGRF(EngineID engine)
 {
-	return Engine::Get(engine)->grffile;
+	return Engine::Get(engine)->grf_prop.grffile;
 }
 
 
@@ -655,12 +655,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)->internal_id == parameter;
+			if (v->type != VEH_TRAIN) return Engine::Get(v->engine_type)->grf_prop.local_id == parameter;
 
 			{
 				uint count = 0;
 				for (; v != NULL; v = v->Next()) {
-					if (Engine::Get(v->engine_type)->internal_id == parameter) count++;
+					if (Engine::Get(v->engine_type)->grf_prop.local_id == parameter) count++;
 				}
 				return count;
 			}
@@ -745,8 +745,8 @@
 		case 0x43: return GB(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)->internal_id;
-		case 0x47: return GB(Engine::Get(v->engine_type)->internal_id, 8, 8);
+		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 0x48:
 			if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum;
 			return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD;
@@ -877,7 +877,7 @@
 	res->count           = 0;
 
 	const Engine *e = Engine::Get(engine_type);
-	res->grffile         = (e != NULL ? e->grffile : NULL);
+	res->grffile         = (e != NULL ? e->grf_prop.grffile : NULL);
 }
 
 
@@ -914,12 +914,12 @@
 
 	const Engine *e = Engine::Get(engine);
 
-	assert(cargo < lengthof(e->group));
-	group = e->group[cargo];
+	assert(cargo < lengthof(e->grf_prop.spritegroup));
+	group = e->grf_prop.spritegroup[cargo];
 	if (group != NULL) return group;
 
 	/* Fall back to the default set if the selected cargo type is not defined */
-	return e->group[CT_DEFAULT];
+	return e->grf_prop.spritegroup[CT_DEFAULT];
 }
 
 
@@ -1133,10 +1133,10 @@
 uint ListPositionOfEngine(EngineID engine)
 {
 	const Engine *e = Engine::Get(engine);
-	if (e->grffile == NULL) return e->list_position;
+	if (e->grf_prop.grffile == NULL) return e->list_position;
 
 	/* Crude sorting to group by GRF ID */
-	return (e->grffile->grfid * 256) + e->list_position;
+	return (e->grf_prop.grffile->grfid * 256) + e->list_position;
 }
 
 struct ListOrderChange {
@@ -1173,8 +1173,8 @@
 		/* Populate map with current list positions */
 		Engine *e;
 		FOR_ALL_ENGINES_OF_TYPE(e, source_e->type) {
-			if (!_settings_game.vehicle.dynamic_engines || e->grffile == source_e->grffile) {
-				if (e->internal_id == target) target_e = e;
+			if (!_settings_game.vehicle.dynamic_engines || e->grf_prop.grffile == source_e->grf_prop.grffile) {
+				if (e->grf_prop.local_id == target) target_e = e;
 				lptr_map[e->list_position] = e;
 			}
 		}
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -1650,7 +1650,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->grffile);
+	return GetPrice(e->u.road.running_cost_class, cost_factor, e->grf_prop.grffile);
 }
 
 bool RoadVehicle::Tick()
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -162,7 +162,7 @@
 {
 	const Engine *e = Engine::Get(this->engine_type);
 	uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
-	return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grffile);
+	return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grf_prop.grffile);
 }
 
 void Ship::OnNewDay()
--- a/src/table/newgrf_debug_data.h
+++ b/src/table/newgrf_debug_data.h
@@ -61,7 +61,7 @@
 };
 
 class NIHVehicle : public NIHelper {
-	bool IsInspectable(uint index) const                 { return Engine::Get(Vehicle::Get(index)->engine_type)->grffile != NULL; }
+	bool IsInspectable(uint index) const                 { return Engine::Get(Vehicle::Get(index)->engine_type)->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); }
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -98,9 +98,9 @@
 {
 	/* 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);
-	uint32 grfid = engine->grffile->grfid;
+	uint32 grfid = engine->grf_prop.grffile->grfid;
 	GRFConfig *grfconfig = GetGRFConfig(grfid);
-	if (GamelogGRFBugReverse(grfid, engine->internal_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
+	if (GamelogGRFBugReverse(grfid, engine->grf_prop.local_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
 		ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_VEHICLE_LENGTH, GBUG_VEH_LENGTH, true);
 	}
 }
@@ -458,9 +458,9 @@
 	int vehicle_pitch = 0;
 
 	const Engine *e = Engine::Get(this->engine_type);
-	if (e->grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
-		reference_width = e->grffile->traininfo_vehicle_width;
-		vehicle_pitch = e->grffile->traininfo_vehicle_pitch;
+	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 (offset != NULL) {
@@ -505,8 +505,8 @@
 	if (is_custom_sprite(spritenum)) {
 		SpriteID sprite = GetCustomVehicleIcon(engine, dir);
 		if (sprite != 0) {
-			if (e->grffile != NULL) {
-				y += e->grffile->traininfo_vehicle_pitch;
+			if (e->grf_prop.grffile != NULL) {
+				y += e->grf_prop.grffile->traininfo_vehicle_pitch;
 			}
 			return sprite;
 		}
@@ -3984,7 +3984,7 @@
 		/* Halve running cost for multiheaded parts */
 		if (v->IsMultiheaded()) cost_factor /= 2;
 
-		cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->grffile);
+		cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->grf_prop.grffile);
 	} while ((v = v->GetNextVehicle()) != NULL);
 
 	return cost;
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -200,7 +200,7 @@
 void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical)
 {
 	const Engine *e = Engine::Get(engine);
-	uint32 grfid = e->grffile->grfid;
+	uint32 grfid = e->grf_prop.grffile->grfid;
 	GRFConfig *grfconfig = GetGRFConfig(grfid);
 
 	if (!HasBit(grfconfig->grf_bugs, bug_type)) {
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -281,7 +281,7 @@
 
 		default: NOT_REACHED();
 	}
-	return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grffile, -10));
+	return CommandCost(expense_type, GetPrice(base_price, cost_factor, e->grf_prop.grffile, -10));
 }
 
 /**