diff options
author | glx <glx@openttd.org> | 2009-01-24 20:14:15 +0000 |
---|---|---|
committer | glx <glx@openttd.org> | 2009-01-24 20:14:15 +0000 |
commit | 9b73303b4d4c91cb0fcee44679e5443bc8c5fa0b (patch) | |
tree | 9ca7c83bbeeb0c45437a57aa25919d6493f228ac /src | |
parent | af7b7867a7565dc3c18242d9cfad1912b6ef4e40 (diff) | |
download | openttd-9b73303b4d4c91cb0fcee44679e5443bc8c5fa0b.tar.xz |
(svn r15261) -Add: added Engine::GetRunningCost() to remove some code duplication. Also stops AIs decrementing vehicle counter of first company
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/api/ai_engine.cpp | 29 | ||||
-rw-r--r-- | src/ai/api/ai_event_types.cpp | 29 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 36 | ||||
-rw-r--r-- | src/engine.cpp | 19 | ||||
-rw-r--r-- | src/engine_base.h | 3 | ||||
-rw-r--r-- | src/engine_gui.cpp | 8 |
6 files changed, 43 insertions, 81 deletions
diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index 06ed3d728..545305f3f 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -193,34 +193,7 @@ { if (!IsValidEngine(engine_id)) return -1; - /* We need to create an instance in order to obtain GetRunningCost. - * This means we temporary allocate a vehicle in the pool, but - * there is no other way.. */ - Vehicle *vehicle; - switch (::GetEngine(engine_id)->type) { - case VEH_ROAD: { - vehicle = new RoadVehicle(); - } break; - - case VEH_TRAIN: { - vehicle = new Train(); - } break; - - case VEH_SHIP: { - vehicle = new Ship(); - } break; - - case VEH_AIRCRAFT: { - vehicle = new Aircraft(); - } break; - - default: NOT_REACHED(); - } - - vehicle->engine_type = engine_id; - Money runningCost = vehicle->GetRunningCost(); - delete vehicle; - return runningCost >> 8; + return ::GetEngine(engine_id)->GetRunningCost(); } /* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id) diff --git a/src/ai/api/ai_event_types.cpp b/src/ai/api/ai_event_types.cpp index 09d801e70..9792f92f9 100644 --- a/src/ai/api/ai_event_types.cpp +++ b/src/ai/api/ai_event_types.cpp @@ -140,34 +140,7 @@ Money AIEventEnginePreview::GetPrice() Money AIEventEnginePreview::GetRunningCost() { - /* We need to create an instance in order to obtain GetRunningCost. - * This means we temporary allocate a vehicle in the pool, but - * there is no other way.. */ - Vehicle *vehicle; - switch (::GetEngine(engine)->type) { - case VEH_ROAD: { - vehicle = new RoadVehicle(); - } break; - - case VEH_TRAIN: { - vehicle = new Train(); - } break; - - case VEH_SHIP: { - vehicle = new Ship(); - } break; - - case VEH_AIRCRAFT: { - vehicle = new Aircraft(); - } break; - - default: NOT_REACHED(); - } - - vehicle->engine_type = engine; - Money runningCost = vehicle->GetRunningCost(); - delete vehicle; - return runningCost >> 8; + return ::GetEngine(engine)->GetRunningCost(); } AIVehicle::VehicleType AIEventEnginePreview::GetVehicleType() diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 7d321577f..73945ba3a 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -165,11 +165,8 @@ static int CDECL TrainEnginePowerSorter(const void *a, const void *b) static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b) { - const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); - const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); - - Money va = rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class); - Money vb = rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class); + Money va = GetEngine(*(const EngineID*)a)->GetRunningCost(); + Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost(); int r = ClampToI32(va - vb); return _internal_sort_order ? -r : r; @@ -186,8 +183,8 @@ static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void * * Because of this, the return value have to be reversed as well and we return b - a instead of a - b. * Another thing is that both power and running costs should be doubled for multiheaded engines. * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */ - Money va = (rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class)) / max(1U, (uint)rvi_a->power); - Money vb = (rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class)) / max(1U, (uint)rvi_b->power); + Money va = (GetEngine(*(const EngineID*)a)->GetRunningCost()) / max(1U, (uint)rvi_a->power); + Money vb = (GetEngine(*(const EngineID*)b)->GetRunningCost()) / max(1U, (uint)rvi_b->power); int r = ClampToI32(vb - va); return _internal_sort_order ? -r : r; @@ -244,11 +241,8 @@ static int CDECL RoadVehEngineSpeedSorter(const void *a, const void *b) static int CDECL RoadVehEngineRunningCostSorter(const void *a, const void *b) { - const RoadVehicleInfo *rvi_a = RoadVehInfo(*(const EngineID*)a); - const RoadVehicleInfo *rvi_b = RoadVehInfo(*(const EngineID*)b); - - Money va = rvi_a->running_cost * GetPriceByIndex(rvi_a->running_cost_class); - Money vb = rvi_b->running_cost * GetPriceByIndex(rvi_b->running_cost_class); + Money va = GetEngine(*(const EngineID*)a)->GetRunningCost(); + Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost(); int r = ClampToI32(va - vb); if (r == 0) { @@ -292,8 +286,8 @@ static int CDECL ShipEngineSpeedSorter(const void *a, const void *b) static int CDECL ShipEngineRunningCostSorter(const void *a, const void *b) { - const int va = ShipVehInfo(*(const EngineID*)a)->running_cost; - const int vb = ShipVehInfo(*(const EngineID*)b)->running_cost; + const int va = GetEngine(*(const EngineID*)a)->GetRunningCost(); + const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost(); const int r = va - vb; if (r == 0) { @@ -342,8 +336,8 @@ static int CDECL AircraftEngineSpeedSorter(const void *a, const void *b) static int CDECL AircraftEngineRunningCostSorter(const void *a, const void *b) { - const int va = AircraftVehInfo(*(const EngineID*)a)->running_cost; - const int vb = AircraftVehInfo(*(const EngineID*)b)->running_cost; + const int va = GetEngine(*(const EngineID*)a)->GetRunningCost(); + const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost(); const int r = va - vb; if (r == 0) { @@ -512,7 +506,7 @@ static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const /* Running cost */ if (rvi->running_cost_class != 0xFF) { - SetDParam(0, GetEngineProperty(engine_number, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8); + SetDParam(0, GetEngine(engine_number)->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; } @@ -547,7 +541,7 @@ static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, cons /* Running cost */ if (rvi->running_cost_class != 0xFF) { - SetDParam(0, GetEngineProperty(engine_number, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8); + SetDParam(0, GetEngine(engine_number)->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; } @@ -573,7 +567,7 @@ static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number, const R y += 10; /* Running cost */ - SetDParam(0, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) >> 8); + SetDParam(0, GetEngine(engine_number)->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; @@ -598,7 +592,7 @@ static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const Ship y += 10; /* Running cost */ - SetDParam(0, GetEngineProperty(engine_number, 0x0F, svi->running_cost) * _price.ship_running >> 8); + SetDParam(0, GetEngine(engine_number)->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; @@ -633,7 +627,7 @@ static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const y += 10; /* Running cost */ - SetDParam(0, GetEngineProperty(engine_number, 0x0E, avi->running_cost) * _price.aircraft_running >> 8); + SetDParam(0, GetEngine(engine_number)->GetRunningCost()); DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING); y += 10; diff --git a/src/engine.cpp b/src/engine.cpp index ef6d63438..32d591373 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -118,6 +118,25 @@ Engine::~Engine() free(this->name); } +Money Engine::GetRunningCost() const +{ + switch (this->type) { + case VEH_ROAD: + return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8; + + case VEH_TRAIN: + return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8; + + case VEH_SHIP: + return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8; + + case VEH_AIRCRAFT: + return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8; + + 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 aee07e81a..a0a2df468 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -6,6 +6,7 @@ #define ENGINE_BASE_H #include "engine_type.h" +#include "economy_type.h" #include "oldpool.h" DECLARE_OLD_POOL(Engine, Engine, 6, 10000) @@ -48,6 +49,8 @@ struct Engine : PoolItem<Engine, EngineID, &_Engine_pool> { ~Engine(); inline bool IsValid() const { return this->info.climates != 0; } + + Money GetRunningCost() const; }; static inline bool IsEngineIndex(uint index) diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 9ac465291..679dd09fb 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -135,7 +135,7 @@ static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw) SetDParam(3, GetEngineProperty(engine, 0x0B, rvi->power)); SetDParam(1, GetEngineProperty(engine, 0x16, rvi->weight) << multihead); - SetDParam(4, GetEngineProperty(engine, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8); + SetDParam(4, GetEngine(engine)->GetRunningCost()); uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN); if (capacity != 0) { @@ -154,7 +154,7 @@ static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw) SetDParam(1, avi->max_speed * 10 / 16); SetDParam(2, avi->passenger_capacity); SetDParam(3, avi->mail_capacity); - SetDParam(4, GetEngineProperty(engine, 0x0E, avi->running_cost) * _price.aircraft_running >> 8); + SetDParam(4, GetEngine(engine)->GetRunningCost()); DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw); } @@ -165,7 +165,7 @@ static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw) SetDParam(0, (_price.roadveh_base >> 3) * GetEngineProperty(engine, 0x11, rvi->cost_factor) >> 5); SetDParam(1, rvi->max_speed * 10 / 32); - SetDParam(2, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) >> 8); + SetDParam(2, GetEngine(engine)->GetRunningCost()); SetDParam(3, rvi->cargo_type); SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD)); @@ -179,7 +179,7 @@ static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw) SetDParam(1, GetEngineProperty(engine, 0x0B, svi->max_speed) * 10 / 32); SetDParam(2, svi->cargo_type); SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity)); - SetDParam(4, GetEngineProperty(engine, 0x0F, svi->running_cost) * _price.ship_running >> 8); + SetDParam(4, GetEngine(engine)->GetRunningCost()); DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); } |