diff options
author | glx <glx@openttd.org> | 2009-01-25 00:57:03 +0000 |
---|---|---|
committer | glx <glx@openttd.org> | 2009-01-25 00:57:03 +0000 |
commit | c1cf3934fa5bd523dd1df01cf505c9f06927701d (patch) | |
tree | a8b3e55a2f1168021a45917b6fbf5777c3476634 /src | |
parent | 80ee3d3440233b50bbe818b2f74f6aac8841edfa (diff) | |
download | openttd-c1cf3934fa5bd523dd1df01cf505c9f06927701d.tar.xz |
(svn r15263) -Codechange: added Engine::GetCost() to remove some code duplication.
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/api/ai_engine.cpp | 24 | ||||
-rw-r--r-- | src/ai/api/ai_event_types.cpp | 24 | ||||
-rw-r--r-- | src/aircraft_cmd.cpp | 10 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 28 | ||||
-rw-r--r-- | src/engine.cpp | 22 | ||||
-rw-r--r-- | src/engine_base.h | 1 | ||||
-rw-r--r-- | src/engine_gui.cpp | 22 | ||||
-rw-r--r-- | src/roadveh_cmd.cpp | 11 | ||||
-rw-r--r-- | src/ship_cmd.cpp | 12 | ||||
-rw-r--r-- | src/train_cmd.cpp | 11 |
10 files changed, 67 insertions, 98 deletions
diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index 545305f3f..5d301f47c 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -157,29 +157,7 @@ { if (!IsValidEngine(engine_id)) return -1; - switch (::GetEngine(engine_id)->type) { - case VEH_ROAD: { - const RoadVehicleInfo *vi = ::RoadVehInfo(engine_id); - return (_price.roadveh_base >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_TRAIN: { - const RailVehicleInfo *vi = ::RailVehInfo(engine_id); - return (_price.build_railvehicle >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_SHIP: { - const ShipVehicleInfo *vi = ::ShipVehInfo(engine_id); - return (_price.ship_base >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_AIRCRAFT: { - const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine_id); - return (_price.aircraft_base >> 3) * vi->cost_factor >> 5; - } break; - - default: NOT_REACHED(); - } + return ::GetEngine(engine_id)->GetCost(); } /* static */ int32 AIEngine::GetMaxAge(EngineID engine_id) diff --git a/src/ai/api/ai_event_types.cpp b/src/ai/api/ai_event_types.cpp index 9792f92f9..6230caad6 100644 --- a/src/ai/api/ai_event_types.cpp +++ b/src/ai/api/ai_event_types.cpp @@ -113,29 +113,7 @@ int32 AIEventEnginePreview::GetMaxSpeed() Money AIEventEnginePreview::GetPrice() { - switch (::GetEngine(engine)->type) { - case VEH_ROAD: { - const RoadVehicleInfo *vi = ::RoadVehInfo(engine); - return (_price.roadveh_base >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_TRAIN: { - const RailVehicleInfo *vi = ::RailVehInfo(engine); - return (_price.build_railvehicle >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_SHIP: { - const ShipVehicleInfo *vi = ::ShipVehInfo(engine); - return (_price.ship_base >> 3) * vi->cost_factor >> 5; - } break; - - case VEH_AIRCRAFT: { - const AircraftVehicleInfo *vi = ::AircraftVehInfo(engine); - return (_price.aircraft_base >> 3) * vi->cost_factor >> 5; - } break; - - default: NOT_REACHED(); - } + return ::GetEngine(engine)->GetCost(); } Money AIEventEnginePreview::GetRunningCost() diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 2effe517a..cec144be4 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -223,12 +223,6 @@ void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height) height = spr->height; } -static CommandCost EstimateAircraftCost(EngineID engine, const AircraftVehicleInfo *avi) -{ - return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x0B, avi->cost_factor) * (_price.aircraft_base >> 3) >> 5); -} - - /** * Calculates cargo capacity based on an aircraft's passenger * and mail capacities. @@ -266,7 +260,8 @@ CommandCost CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, if (!IsEngineBuildable(p1, VEH_AIRCRAFT, _current_company)) return_cmd_error(STR_AIRCRAFT_NOT_AVAILABLE); const AircraftVehicleInfo *avi = AircraftVehInfo(p1); - CommandCost value = EstimateAircraftCost(p1, avi); + const Engine *e = GetEngine(p1); + CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost()); /* to just query the cost, it is not neccessary to have a valid tile (automation/AI) */ if (flags & DC_QUERY_COST) return value; @@ -374,7 +369,6 @@ CommandCost CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, u->cargo_cap = 0; } - const Engine *e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 73945ba3a..a7840150f 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -482,8 +482,10 @@ static int DrawCargoCapacityInfo(int x, int y, EngineID engine, VehicleType type /* Draw rail wagon specific details */ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi) { + const Engine *e = GetEngine(engine_number); + /* Purchase cost */ - SetDParam(0, (GetEngineProperty(engine_number, 0x17, rvi->cost_factor) * _price.build_railwagon) >> 8); + SetDParam(0, e->GetCost()); DrawString(x, y, STR_PURCHASE_INFO_COST, TC_FROMSTRING); y += 10; @@ -506,7 +508,7 @@ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const /* Running cost */ if (rvi->running_cost_class != 0xFF) { - SetDParam(0, GetEngine(engine_number)->GetRunningCost()); + SetDParam(0, e->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; } @@ -519,9 +521,10 @@ static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, cons { int multihead = (rvi->railveh_type == RAILVEH_MULTIHEAD ? 1 : 0); uint weight = GetEngineProperty(engine_number, 0x16, rvi->weight); + const Engine *e = GetEngine(engine_number); /* Purchase Cost - Engine weight */ - SetDParam(0, GetEngineProperty(engine_number, 0x17, rvi->cost_factor) * (_price.build_railvehicle >> 3) >> 5); + SetDParam(0, e->GetCost()); SetDParam(1, weight << multihead); DrawString(x, y, STR_PURCHASE_INFO_COST_WEIGHT, TC_FROMSTRING); y += 10; @@ -541,7 +544,7 @@ static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, cons /* Running cost */ if (rvi->running_cost_class != 0xFF) { - SetDParam(0, GetEngine(engine_number)->GetRunningCost()); + SetDParam(0, e->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; } @@ -560,14 +563,16 @@ static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, cons /* Draw road vehicle specific details */ static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number, const RoadVehicleInfo *rvi) { + const Engine *e = GetEngine(engine_number); + /* Purchase cost - Max speed */ - SetDParam(0, GetEngineProperty(engine_number, 0x11, rvi->cost_factor) * (_price.roadveh_base >> 3) >> 5); + SetDParam(0, e->GetCost()); SetDParam(1, rvi->max_speed * 10 / 32); DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING); y += 10; /* Running cost */ - SetDParam(0, GetEngine(engine_number)->GetRunningCost()); + SetDParam(0, e->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; @@ -578,8 +583,10 @@ static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number, const R /* Draw ship specific details */ static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const ShipVehicleInfo *svi) { + const Engine *e = GetEngine(engine_number); + /* Purchase cost - Max speed */ - SetDParam(0, GetEngineProperty(engine_number, 0x0A, svi->cost_factor) * (_price.ship_base >> 3) >> 5); + SetDParam(0, e->GetCost()); SetDParam(1, GetEngineProperty(engine_number, 0x0B, svi->max_speed) * 10 / 32); DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING); y += 10; @@ -592,7 +599,7 @@ static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const Ship y += 10; /* Running cost */ - SetDParam(0, GetEngine(engine_number)->GetRunningCost()); + SetDParam(0, e->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; @@ -603,9 +610,10 @@ static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const Ship static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const AircraftVehicleInfo *avi) { CargoID cargo; + const Engine *e = GetEngine(engine_number); /* Purchase cost - Max speed */ - SetDParam(0, GetEngineProperty(engine_number, 0x0B, avi->cost_factor) * (_price.aircraft_base >> 3) >> 5); + SetDParam(0, e->GetCost()); SetDParam(1, avi->max_speed * 10 / 16); DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING); y += 10; @@ -627,7 +635,7 @@ static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const y += 10; /* Running cost */ - SetDParam(0, GetEngine(engine_number)->GetRunningCost()); + SetDParam(0, e->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; diff --git a/src/engine.cpp b/src/engine.cpp index 32d591373..37886699d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -137,6 +137,28 @@ Money Engine::GetRunningCost() const } } +Money Engine::GetCost() const +{ + switch (this->type) { + case VEH_ROAD: + return GetEngineProperty(this->index, 0x11, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5; + + case VEH_TRAIN: + if (this->u.rail.railveh_type == RAILVEH_WAGON) { + return (GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * _price.build_railwagon) >> 8; + } else { + return GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5; + } + case VEH_SHIP: + return GetEngineProperty(this->index, 0x0A, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5; + + case VEH_AIRCRAFT: + return GetEngineProperty(this->index, 0x0B, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5; + + default: NOT_REACHED(); + } +} + /** Sets cached values in Company::num_vehicles and Group::num_vehicles */ void SetCachedEngineCounts() diff --git a/src/engine_base.h b/src/engine_base.h index a0a2df468..d5fd93329 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -51,6 +51,7 @@ struct Engine : PoolItem<Engine, EngineID, &_Engine_pool> { inline bool IsValid() const { return this->info.climates != 0; } Money GetRunningCost() const; + Money GetCost() const; }; static inline bool IsEngineIndex(uint index) diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 679dd09fb..2593994b7 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -129,13 +129,14 @@ static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw) { const RailVehicleInfo *rvi = RailVehInfo(engine); int multihead = (rvi->railveh_type == RAILVEH_MULTIHEAD) ? 1 : 0; + const Engine *e = GetEngine(engine); - SetDParam(0, (_price.build_railvehicle >> 3) * GetEngineProperty(engine, 0x17, rvi->cost_factor) >> 5); + SetDParam(0, e->GetCost()); SetDParam(2, GetEngineProperty(engine, 0x09, rvi->max_speed) * 10 / 16); SetDParam(3, GetEngineProperty(engine, 0x0B, rvi->power)); SetDParam(1, GetEngineProperty(engine, 0x16, rvi->weight) << multihead); - SetDParam(4, GetEngine(engine)->GetRunningCost()); + SetDParam(4, e->GetRunningCost()); uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN); if (capacity != 0) { @@ -150,11 +151,13 @@ static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw) static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw) { const AircraftVehicleInfo *avi = AircraftVehInfo(engine); - SetDParam(0, (_price.aircraft_base >> 3) * GetEngineProperty(engine, 0x0B, avi->cost_factor) >> 5); + const Engine *e = GetEngine(engine); + + SetDParam(0, e->GetCost()); SetDParam(1, avi->max_speed * 10 / 16); SetDParam(2, avi->passenger_capacity); SetDParam(3, avi->mail_capacity); - SetDParam(4, GetEngine(engine)->GetRunningCost()); + SetDParam(4, e->GetRunningCost()); DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw); } @@ -162,10 +165,11 @@ static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw) static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw) { const RoadVehicleInfo *rvi = RoadVehInfo(engine); + const Engine *e = GetEngine(engine); - SetDParam(0, (_price.roadveh_base >> 3) * GetEngineProperty(engine, 0x11, rvi->cost_factor) >> 5); + SetDParam(0, e->GetCost()); SetDParam(1, rvi->max_speed * 10 / 32); - SetDParam(2, GetEngine(engine)->GetRunningCost()); + SetDParam(2, e->GetRunningCost()); SetDParam(3, rvi->cargo_type); SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD)); @@ -175,11 +179,13 @@ static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw) static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw) { const ShipVehicleInfo *svi = ShipVehInfo(engine); - SetDParam(0, GetEngineProperty(engine, 0x0A, svi->cost_factor) * (_price.ship_base >> 3) >> 5); + const Engine *e = GetEngine(engine); + + SetDParam(0, e->GetCost()); SetDParam(1, GetEngineProperty(engine, 0x0B, svi->max_speed) * 10 / 32); SetDParam(2, svi->cargo_type); SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity)); - SetDParam(4, GetEngine(engine)->GetRunningCost()); + SetDParam(4, e->GetRunningCost()); DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); } diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 914f8e2d8..7e587b5be 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -134,11 +134,6 @@ void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal) DrawSprite(GetRoadVehIcon(engine), pal, x, y); } -static CommandCost EstimateRoadVehCost(EngineID engine_type) -{ - return CommandCost(EXPENSES_NEW_VEHICLES, ((_price.roadveh_base >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->cost_factor)) >> 5); -} - byte GetRoadVehLength(const Vehicle *v) { byte length = 8; @@ -176,14 +171,13 @@ void RoadVehUpdateCache(Vehicle *v) */ CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text) { - CommandCost cost; Vehicle *v; UnitID unit_num; - Engine *e; if (!IsEngineBuildable(p1, VEH_ROAD, _current_company)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE); - cost = EstimateRoadVehCost(p1); + const Engine *e = GetEngine(p1); + CommandCost cost(EXPENSES_NEW_VEHICLES, e->GetCost()); if (flags & DC_QUERY_COST) return cost; /* The ai_new queries the vehicle cost before building the route, @@ -251,7 +245,6 @@ CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, v->max_speed = rvi->max_speed; v->engine_type = (EngineID)p1; - e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 45361fc96..37d2ef78e 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -341,11 +341,6 @@ static bool ShipAccelerate(Vehicle *v) return (t < v->progress); } -static CommandCost EstimateShipCost(EngineID engine_type) -{ - return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->cost_factor) * (_price.ship_base >> 3) >> 5); -} - static void ShipArrivesAt(const Vehicle *v, Station *st) { /* Check if station was ever visited before */ @@ -746,13 +741,13 @@ void Ship::Tick() */ CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text) { - CommandCost value; UnitID unit_num; - Engine *e; if (!IsEngineBuildable(p1, VEH_SHIP, _current_company)) return_cmd_error(STR_SHIP_NOT_AVAILABLE); - value = EstimateShipCost(p1); + const Engine *e = GetEngine(p1); + CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost()); + if (flags & DC_QUERY_COST) return value; /* The ai_new queries the vehicle cost before building the route, @@ -797,7 +792,6 @@ CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, con v->max_speed = svi->max_speed; v->engine_type = p1; - e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 258daabc0..d9793a70d 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -605,7 +605,7 @@ void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal) static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags) { const RailVehicleInfo *rvi = RailVehInfo(engine); - CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->cost_factor) * _price.build_railwagon) >> 8); + CommandCost value(EXPENSES_NEW_VEHICLES, GetEngine(engine)->GetCost()); uint num_vehicles = 1 + CountArticulatedParts(engine, false); @@ -713,11 +713,6 @@ static void NormalizeTrainVehInDepot(const Vehicle *u) } } -static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo *rvi) -{ - return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x17, rvi->cost_factor) * (_price.build_railvehicle >> 3) >> 5); -} - static void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building) { u = new (u) Train(); @@ -768,7 +763,8 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags); - CommandCost value = EstimateTrainCost(p1, rvi); + const Engine *e = GetEngine(p1); + CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost()); uint num_vehicles = (rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + @@ -820,7 +816,6 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 v->engine_type = p1; - const Engine *e = GetEngine(p1); v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR; |