File diff r2847:8df2aeef9e0e → r2848:ae67043fa1f1
vehicle.c
Show inline comments
 
@@ -1597,49 +1597,49 @@ static void MoveVehicleCargo(Vehicle *de
 

	
 
		} while (source->cargo_count > 0 && (dest = dest->next) != NULL);
 
		dest = v;
 
	} while ((source = source->next) != NULL);
 
}
 

	
 
/* Replaces a vehicle (used to be called autorenew)
 
 * This function is only called from MaybeReplaceVehicle()
 
 * Must be called with _current_player set to the owner of the vehicle
 
 * @param w Vehicle to replace
 
 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
 
 * @return value is cost of the replacement or CMD_ERROR
 
 */
 
static int32 ReplaceVehicle(Vehicle **w, byte flags)
 
{
 
	int32 cost;
 
	Vehicle *old_v = *w;
 
	const Player *p = GetPlayer(old_v->owner);
 
	EngineID new_engine_type;
 
	const UnitID cached_unitnumber = old_v->unitnumber;
 
	bool new_front = false;
 
	Vehicle *new_v = NULL;
 
	char vehicle_name[32];
 

	
 
	new_engine_type = EngineReplacement(p, old_v->engine_type);
 
	new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type);
 
	if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
 

	
 
	cost = DoCommand(old_v->x_pos, old_v->y_pos, new_engine_type, 1, flags, CMD_BUILD_VEH(old_v->type));
 
	if (CmdFailed(cost)) return cost;
 

	
 
	if (flags & DC_EXEC) {
 
		new_v = GetVehicle(_new_vehicle_id);
 
		*w = new_v;	//we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is
 

	
 
		/* refit if needed */
 
		if (new_v->type != VEH_Road) { // road vehicles can't be refitted
 
			if (old_v->cargo_type != new_v->cargo_type && old_v->cargo_cap != 0 && new_v->cargo_cap != 0) {// some train engines do not have cargo capacity
 
				DoCommand(0, 0, new_v->index, old_v->cargo_type, DC_EXEC, CMD_REFIT_VEH(new_v->type));
 
			}
 
		}
 

	
 

	
 
		if (old_v->type == VEH_Train && !IsFrontEngine(old_v)) {
 
			/* this is a railcar. We need to move the car into the train
 
			 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
 
			 * sell the old engine in a moment
 
			 */
 
			DoCommand(0, 0, (GetPrevVehicleInChain(old_v)->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
 
			/* Now we move the old one out of the train */
 
@@ -1718,49 +1718,49 @@ static void MaybeReplaceVehicle(Vehicle 
 

	
 
	assert(v->type == VEH_Train || v->type == VEH_Road || v->type == VEH_Ship || v->type == VEH_Aircraft);
 

	
 
	assert(v->vehstatus & VS_STOPPED);	// the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed
 

	
 
	if (v->leave_depot_instantly) {
 
		// we stopped the vehicle to do this, so we have to remember to start it again when we are done
 
		// we need to store this info as the engine might be replaced and lose this info
 
		stopped = true;
 
	}
 

	
 
	for (;;) {
 
		cost = 0;
 
		w = v;
 
		do {
 
			if (w->type == VEH_Train && IsMultiheaded(w) && !IsTrainEngine(w)) {
 
				/* we build the rear ends of multiheaded trains with the front ones */
 
				continue;
 
			}
 

	
 
			// 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
 
					w->max_age == 0) { // rail cars got a max age of 0
 
				if (!EngineHasReplacement(p, w->engine_type)) // updates to a new model
 
				if (!EngineHasReplacementForPlayer(p, w->engine_type)) // updates to a new model
 
					continue;
 
			}
 

	
 
			/* Now replace the vehicle */
 
			temp_cost = ReplaceVehicle(&w, flags);
 

	
 
			if (flags & DC_EXEC &&
 
					(w->type != VEH_Train || w->u.rail.first_engine == INVALID_VEHICLE)) {
 
				/* now we bought a new engine and sold the old one. We need to fix the
 
				 * pointers in order to avoid pointing to the old one for trains: these
 
				 * pointers should point to the front engine and not the cars
 
				 */
 
				v = w;
 
			}
 

	
 
			if (CmdFailed(temp_cost)) break;
 

	
 
			cost += temp_cost;
 
		} while (w->type == VEH_Train && (w = GetNextVehicle(w)) != NULL);
 

	
 
		if (!(flags & DC_EXEC) && (CmdFailed(temp_cost) || p->money64 < (int32)(cost + p->engine_renew_money) || cost == 0)) {
 
			if (p->money64 < (int32)(cost + p->engine_renew_money) && ( _local_player == v->owner ) && cost != 0) {
 
				StringID message;
 
				SetDParam(0, v->unitnumber);