File diff r11907:e9ad0e83ab1e → r11908:0875f47fab11
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -603,49 +603,49 @@ static CommandCost ReplaceChain(Vehicle 
 
 * @param p1 Index of vehicle
 
 * @param p2 not used
 
 */
 
CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
 
	bool nothing_to_do = true;
 

	
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 
	Vehicle *v = GetVehicle(p1);
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsInDepot()) return CMD_ERROR;
 
	if (HASBITS(v->vehstatus, VS_CRASHED)) return CMD_ERROR;
 

	
 
	bool free_wagon = false;
 
	if (v->type == VEH_TRAIN) {
 
		if (IsArticulatedPart(v) || IsRearDualheaded(v)) return CMD_ERROR;
 
		free_wagon = !IsFrontEngine(v);
 
		if (free_wagon && IsFrontEngine(v->First())) return CMD_ERROR;
 
	} else {
 
		if (!v->IsPrimaryVehicle()) return CMD_ERROR;
 
	}
 

	
 
	const Company *c = GetCompany(_current_company);
 
	bool wagon_removal = c->renew_keep_length;
 
	bool wagon_removal = c->settings.renew_keep_length;
 

	
 
	/* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
 
	Vehicle *w = v;
 
	bool any_replacements = false;
 
	while (w != NULL && !any_replacements) {
 
		any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE);
 
		w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit(w) : NULL);
 
	}
 

	
 
	if (any_replacements) {
 
		bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
 

	
 
		/* Stop the vehicle */
 
		if (!was_stopped) cost.AddCost(StartStopVehicle(v, true));
 
		if (cost.Failed()) return cost;
 

	
 
		assert(v->IsStoppedInDepot());
 

	
 
		/* We have to construct the new vehicle chain to test whether it is valid.
 
		 * Vehicle construction needs random bits, so we have to save the random seeds
 
		 * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */
 
		SavedRandomSeeds saved_seeds;
 
		SaveRandomSeeds(&saved_seeds);
 
		if (free_wagon) {