summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjarni <bjarni@openttd.org>2008-02-23 22:01:55 +0000
committerbjarni <bjarni@openttd.org>2008-02-23 22:01:55 +0000
commit089969e1cac6ebfb8ac14d692d2bfcd09f21ea6a (patch)
tree749b019a5a21f0d18977297475fb38406c644ebf
parente821b3a9b8cd8525e214f2a0f39c517fbeeb6389 (diff)
downloadopenttd-089969e1cac6ebfb8ac14d692d2bfcd09f21ea6a.tar.xz
(svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
This will remove duplicated code and ensure that the check is consistent
-rw-r--r--src/aircraft_cmd.cpp2
-rw-r--r--src/autoreplace_cmd.cpp13
-rw-r--r--src/vehicle_base.h2
3 files changed, 14 insertions, 3 deletions
diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp
index 81f2fc685..18ba5634c 100644
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -1574,7 +1574,7 @@ static inline bool CheckSendAircraftToHangarForReplacement(const Vehicle *v)
/* There is no autoreplace assigned to this EngineID so we will set it to renew to the same type if needed */
new_engine = v->engine_type;
- if(!p->engine_renew || (v->age - v->max_age) < p->engine_renew_months * 30) {
+ if (!v->NeedsAutorenewing(p)) {
/* No need to replace the aircraft */
return false;
}
diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp
index 57083e1ab..90ff7fafe 100644
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -25,6 +25,16 @@
#include "table/strings.h"
+bool Vehicle::NeedsAutorenewing(const Player *p) const
+{
+ assert(p == GetPlayer(this->owner));
+
+ if (!p->engine_renew) return false;
+ if (this->age - this->max_age < (p->engine_renew_months * 30)) return false;
+
+ return true;
+}
+
/*
* move the cargo from one engine to another if possible
*/
@@ -342,8 +352,7 @@ CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs)
}
// check if the vehicle should be replaced
- if (!p->engine_renew ||
- w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old
+ if (!w->NeedsAutorenewing(p) || // replace if engine is too old
w->max_age == 0) { // rail cars got a max age of 0
/* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
If not, chek if an global auto remplacement is defined */
diff --git a/src/vehicle_base.h b/src/vehicle_base.h
index 428398d6c..3b5464a9d 100644
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -481,6 +481,8 @@ public:
* @return true if there are other vehicles sharing the same order
*/
inline bool IsOrderListShared() const { return this->next_shared != NULL || this->prev_shared != NULL; };
+
+ bool NeedsAutorenewing(const Player *p) const;
};
/**