From acc3c75951c4db0873db794b657697774f0ff9e9 Mon Sep 17 00:00:00 2001 From: frosch Date: Tue, 1 Nov 2011 00:23:41 +0000 Subject: [PATCH] (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code. --- src/aircraft_cmd.cpp | 2 +- src/articulated_vehicles.cpp | 3 ++- src/build_vehicle_gui.cpp | 2 +- src/engine.cpp | 15 +++++++++++-- src/engine_base.h | 12 +++++++++++ src/newgrf.cpp | 6 +++--- src/newgrf_engine.cpp | 40 ++++++----------------------------- src/newgrf_engine.h | 2 -- src/newgrf_sound.cpp | 2 +- src/roadveh_cmd.cpp | 2 +- src/ship_cmd.cpp | 2 +- src/table/newgrf_debug_data.h | 4 ++-- src/train_cmd.cpp | 14 ++++++------ src/vehicle.cpp | 23 ++++++++++++++++++-- src/vehicle_base.h | 5 +++++ src/vehicle_cmd.cpp | 4 ++-- src/vehicle_gui.cpp | 10 ++++----- 17 files changed, 84 insertions(+), 64 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 3996e739de..4eec698b3f 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -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() diff --git a/src/articulated_vehicles.cpp b/src/articulated_vehicles.cpp index f186915b9f..8d3932ce29 100644 --- a/src/articulated_vehicles.cpp +++ b/src/articulated_vehicles.cpp @@ -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)); } /** diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 4b40d7365e..2475a90aeb 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -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; } diff --git a/src/engine.cpp b/src/engine.cpp index f4474ea27c..d59e2fb6f7 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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); } /** diff --git a/src/engine_base.h b/src/engine_base.h index a2dd0b1dd0..f118b83872 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -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 { diff --git a/src/newgrf.cpp b/src/newgrf.cpp index af5e250211..a7982be35c 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -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); } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 060f3b51f0..2fe393dea8 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -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; } diff --git a/src/newgrf_engine.h b/src/newgrf_engine.h index edc8b29bde..de47fb141c 100644 --- a/src/newgrf_engine.h +++ b/src/newgrf_engine.h @@ -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); diff --git a/src/newgrf_sound.cpp b/src/newgrf_sound.cpp index 6151f4cb33..47ddf2406e 100644 --- a/src/newgrf_sound.cpp +++ b/src/newgrf_sound.cpp @@ -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 */ diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index a77fa38bbd..15e29f3e59 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -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() diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index bf5e2da7d2..b6e0387b37 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -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() diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index e7867e3aa5..e24a183c82 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -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); } }; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index c644b620ec..7fd6d6a8b7 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -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; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 5827b46daf..7110cb46b6 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -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 diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 4bfe59323c..e822a0b86c 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -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 diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 48d8e42bb6..b9d7fe4d4b 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -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); } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 2475b9b1b0..a464a120e4 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -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;