(svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.

This commit is contained in:
frosch 2011-11-01 00:23:41 +00:00
parent 72cd855978
commit acc3c75951
17 changed files with 84 additions and 64 deletions

View File

@ -391,7 +391,7 @@ Money Aircraft::GetRunningCost() const
{
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()

View File

@ -37,7 +37,8 @@ static EngineID GetNextArticulatedPart(uint index, EngineID front_type, Vehicle
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));
}
/**

View File

@ -756,7 +756,7 @@ static uint ShowAdditionalText(int left, int right, int y, EngineID engine)
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;
}

View File

@ -155,6 +155,17 @@ bool Engine::IsEnabled() const
return this->info.string_id != STR_NEWGRF_INVALID_ENGINE;
}
/**
* 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.
@ -265,7 +276,7 @@ Money Engine::GetRunningCost() const
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 @@ Money Engine::GetCost() const
default: NOT_REACHED();
}
return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
}
/**

View File

@ -101,6 +101,18 @@ struct Engine : EnginePool::PoolItem<&_engine_pool> {
{
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 {

View File

@ -7956,7 +7956,7 @@ static void CalculateRefitMasks()
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 @@ static void FinaliseEngineArray()
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 @@ static void FinaliseEngineArray()
/* 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);
}

View File

@ -111,29 +111,6 @@ void SetEngineGRF(EngineID engine, const GRFFile *file)
}
/**
* 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 @@ static uint32 VehicleGetVariable(Vehicle *v, const ResolverObject *object, byte
/* 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 @@ static uint32 VehicleGetVariable(Vehicle *v, const ResolverObject *object, byte
*/
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 @@ static uint32 VehicleGetVariable(Vehicle *v, const ResolverObject *object, byte
/* 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 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by
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 @@ static inline void NewVehicleResolver(ResolverObject *res, EngineID engine_type,
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 @@ void TriggerVehicle(Vehicle *v, VehicleTrigger trigger)
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 @@ void CommitVehicleListOrderChanges()
/* 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;
}

View File

@ -36,8 +36,6 @@ SpriteID GetRotorOverrideSprite(EngineID engine, const struct Aircraft *v, bool
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);

View File

@ -59,7 +59,7 @@ uint GetNumSounds()
*/
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 */

View File

@ -1533,7 +1533,7 @@ Money RoadVehicle::GetRunningCost() const
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()

View File

@ -192,7 +192,7 @@ Money Ship::GetRunningCost() const
{
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()

View File

@ -61,12 +61,12 @@ static const NIVariable _niv_vehicles[] = {
};
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); }
};

View File

@ -456,9 +456,9 @@ int Train::GetDisplayImageWidth(Point *offset) const
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 @@ static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y)
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 @@ static CommandCost CheckTrainAttachment(Train *t)
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 @@ Money Train::GetRunningCost() const
/* 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;

View File

@ -211,8 +211,7 @@ uint Vehicle::Crash(bool flooded)
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);
@ -641,6 +640,26 @@ const Engine *Vehicle::GetEngine() const
return Engine::Get(this->engine_type);
}
/**
* 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

View File

@ -118,6 +118,8 @@ struct LoadgameState;
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 @@ public:
*/
virtual SpriteID GetImage(Direction direction) const { return 0; }
const GRFFile *GetGRF() const;
uint32 GetGRFID() const;
/**
* Invalidates cached NewGRF variables
* @see InvalidateNewGRFCacheOfChain

View File

@ -241,7 +241,7 @@ static CommandCost GetRefitCost(EngineID engine_type)
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 @@ CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1,
* 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);
}

View File

@ -276,7 +276,7 @@ struct RefitOption {
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 @@ static void DrawVehicleRefitWindow(const SubtypeList list[NUM_CARGO], int sel, u
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 @@ struct RefitWindow : public Window {
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 @@ struct RefitWindow : public Window {
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 @@ StringID GetCargoSubtypeText(const Vehicle *v)
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;