From ad3ef0fb26c5cf32228480a2d14510f27f1939af Mon Sep 17 00:00:00 2001 From: bjarni Date: Sat, 19 Apr 2008 15:14:20 +0000 Subject: (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() --- src/vehicle.cpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/vehicle.cpp') 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); -- cgit v1.2.3-54-g00ecf