summaryrefslogtreecommitdiff
path: root/src/autoreplace_cmd.cpp
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2012-04-17 19:44:02 +0000
committermichi_cc <michi_cc@openttd.org>2012-04-17 19:44:02 +0000
commit6a70abbd998ed5b54d52ddb44b4df8521413185c (patch)
tree25d404a46a68f3fa314dd27a4b9555ac4bc1a886 /src/autoreplace_cmd.cpp
parented565853889679a409d601a61d661718ac3498cc (diff)
downloadopenttd-6a70abbd998ed5b54d52ddb44b4df8521413185c.tar.xz
(svn r24136) -Feature [FS#4465]: Autoreplace vehicles only when they get old. (Vikthor)
Diffstat (limited to 'src/autoreplace_cmd.cpp')
-rw-r--r--src/autoreplace_cmd.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp
index 31bf75dee..d20882220 100644
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -225,10 +225,11 @@ static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool
* Get the EngineID of the replacement for a vehicle
* @param v The vehicle to find a replacement for
* @param c The vehicle's owner (it's faster to forward the pointer than refinding it)
+ * @param always_replace Always replace, even if not old.
* @param [out] e the EngineID of the replacement. INVALID_ENGINE if no replacement is found
* @return Error if the engine to build is not available
*/
-static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, EngineID &e)
+static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
{
assert(v->type != VEH_TRAIN || !v->IsArticulatedPart());
@@ -239,7 +240,9 @@ static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, EngineID
return CommandCost();
}
- e = EngineReplacementForCompany(c, v->engine_type, v->group_id);
+ bool replace_when_old;
+ e = EngineReplacementForCompany(c, v->engine_type, v->group_id, &replace_when_old);
+ if (!always_replace && replace_when_old && !v->NeedsAutorenewing(c, false)) e = INVALID_ENGINE;
/* Autoreplace, if engine is available */
if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
@@ -271,7 +274,7 @@ static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehic
/* Shall the vehicle be replaced? */
const Company *c = Company::Get(_current_company);
EngineID e;
- CommandCost cost = GetNewEngineType(old_veh, c, e);
+ CommandCost cost = GetNewEngineType(old_veh, c, true, e);
if (cost.Failed()) return cost;
if (e == INVALID_ENGINE) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered
@@ -683,7 +686,7 @@ CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1
bool any_replacements = false;
while (w != NULL) {
EngineID e;
- CommandCost cost = GetNewEngineType(w, c, e);
+ CommandCost cost = GetNewEngineType(w, c, false, e);
if (cost.Failed()) return cost;
any_replacements |= (e != INVALID_ENGINE);
w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
@@ -736,6 +739,7 @@ CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1
* @param tile unused
* @param flags operation to perform
* @param p1 packed data
+ * - bit 0 = replace when engine gets old?
* - bits 16-31 = engine group
* @param p2 packed data
* - bits 0-15 = old engine type
@@ -760,7 +764,7 @@ CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
if (!Engine::IsValidID(new_engine_type)) return CMD_ERROR;
if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
- cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
+ cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, HasBit(p1, 0), flags);
} else {
cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
}