summaryrefslogtreecommitdiff
path: root/src/vehicle.cpp
diff options
context:
space:
mode:
authorbjarni <bjarni@openttd.org>2008-04-19 15:14:20 +0000
committerbjarni <bjarni@openttd.org>2008-04-19 15:14:20 +0000
commitad3ef0fb26c5cf32228480a2d14510f27f1939af (patch)
tree4d4e76388db7fe5636cde9b69e8e82c19cad7126 /src/vehicle.cpp
parent752c8d8f2e966bd9102c1e73b9af99aa58b053b4 (diff)
downloadopenttd-ad3ef0fb26c5cf32228480a2d14510f27f1939af.tar.xz
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
This allowed cleaning up the code in MaybeReplaceVehicle()
Diffstat (limited to 'src/vehicle.cpp')
-rw-r--r--src/vehicle.cpp38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 3cc82188b..028ae5351 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -691,11 +691,32 @@ void CallVehicleTicks()
/* now we handle all the vehicles that entered a depot this tick */
v = _first_veh_in_depot_list;
- while (v != NULL) {
- Vehicle *w = v->depot_list;
- v->depot_list = NULL; // it should always be NULL at the end of each tick
- MaybeReplaceVehicle(v, false, true);
- v = w;
+ if (v != NULL) {
+ while (v != NULL) {
+ /* Autoreplace needs the current player set as the vehicle owner */
+ _current_player = v->owner;
+
+ /* Buffer v->depot_list and clear it.
+ * Autoreplace might clear this so it has to be buffered. */
+ Vehicle *w = v->depot_list;
+ v->depot_list = NULL; // it should always be NULL at the end of each tick
+
+ /* Start vehicle if we stopped them in VehicleEnteredDepotThisTick()
+ * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that
+ * they are already leaving the depot again before being replaced. */
+ if (v->leave_depot_instantly) {
+ v->leave_depot_instantly = false;
+ v->vehstatus &= ~VS_STOPPED;
+ }
+
+ CommandCost cost = MaybeReplaceVehicle(v, 0, true);
+ if (CmdSucceeded(cost) && cost.GetCost() != 0) {
+ /* Looks like we can replace this vehicle so we go ahead and do so */
+ MaybeReplaceVehicle(v, DC_EXEC, true);
+ }
+ v = w;
+ }
+ _current_player = OWNER_NONE;
}
}
@@ -1695,16 +1716,11 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uin
for (uint i = 0; i < engine_count; i++) {
Vehicle *v = vl[i];
- bool stopped = !(v->vehstatus & VS_STOPPED);
/* Ensure that the vehicle completely in the depot */
if (!v->IsInDepot()) continue;
- if (stopped) {
- v->vehstatus |= VS_STOPPED; // Stop the vehicle
- v->leave_depot_instantly = true;
- }
- CommandCost ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
+ CommandCost ret = MaybeReplaceVehicle(v, flags, false);
if (CmdSucceeded(ret)) {
cost.AddCost(ret);