Changeset - r3363:45595914cada
[Not reviewed]
master
0 1 0
bjarni - 18 years ago 2006-03-29 20:41:15
bjarni@openttd.org
(svn r4158) -Fix: [autoreplace] cost for refitting the new vehicle is now added to the cost animation. The player always paid for it, but it was not displayed until now
1 file changed with 5 insertions and 1 deletions:
0 comments (0 inline, 0 general)
vehicle.c
Show inline comments
 
@@ -1449,385 +1449,389 @@ void CheckVehicleBreakdown(Vehicle *v)
 
}
 

	
 
static const StringID _vehicle_type_names[4] = {
 
	STR_019F_TRAIN,
 
	STR_019C_ROAD_VEHICLE,
 
	STR_019E_SHIP,
 
	STR_019D_AIRCRAFT,
 
};
 

	
 
static void ShowVehicleGettingOld(Vehicle *v, StringID msg)
 
{
 
	if (v->owner != _local_player) return;
 

	
 
	// Do not show getting-old message if autorenew is active
 
	if (GetPlayer(v->owner)->engine_renew) return;
 

	
 
	SetDParam(0, _vehicle_type_names[v->type - 0x10]);
 
	SetDParam(1, v->unitnumber);
 
	AddNewsItem(msg, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
}
 

	
 
void AgeVehicle(Vehicle *v)
 
{
 
	int age;
 

	
 
	if (v->age < 65535)
 
		v->age++;
 

	
 
	age = v->age - v->max_age;
 
	if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4)
 
		v->reliability_spd_dec <<= 1;
 

	
 
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 

	
 
	if (age == -366) {
 
		ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD);
 
	} else if (age == 0) {
 
		ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD);
 
	} else if (age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4 || age == 366*5) {
 
		ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND);
 
	}
 
}
 

	
 
/** Clone a vehicle. If it is a train, it will clone all the cars too
 
* @param x,y depot where the cloned vehicle is build
 
* @param p1 the original vehicle's index
 
* @param p2 1 = shared orders, else copied orders
 
*/
 
int32 CmdCloneVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v_front, *v;
 
	Vehicle *w_front, *w, *w_rear;
 
	int cost, total_cost = 0;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	v = GetVehicle(p1);
 
	v_front = v;
 
	w = NULL;
 
	w_front = NULL;
 
	w_rear = NULL;
 

	
 

	
 
	/*
 
	 * v_front is the front engine in the original vehicle
 
	 * v is the car/vehicle of the original vehicle, that is currently being copied
 
	 * w_front is the front engine of the cloned vehicle
 
	 * w is the car/vehicle currently being cloned
 
	 * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
 
	 */
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->type == VEH_Train && (!IsFrontEngine(v) || v->u.rail.crash_anim_pos >= 4400)) return CMD_ERROR;
 

	
 
	// check that we can allocate enough vehicles
 
	if (!(flags & DC_EXEC)) {
 
		int veh_counter = 0;
 
		do {
 
			veh_counter++;
 
		} while ((v = v->next) != NULL);
 

	
 
		if (!AllocateVehicles(NULL, veh_counter)) {
 
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
		}
 
	}
 

	
 
	v = v_front;
 

	
 
	do {
 

	
 
		if (IsMultiheaded(v) && !IsTrainEngine(v)) {
 
			/* we build the rear ends of multiheaded trains with the front ones */
 
			continue;
 
		}
 

	
 
		cost = DoCommand(x, y, v->engine_type, 1, flags, CMD_BUILD_VEH(v->type));
 

	
 
		if (CmdFailed(cost)) return cost;
 

	
 
		total_cost += cost;
 

	
 
		if (flags & DC_EXEC) {
 
			w = GetVehicle(_new_vehicle_id);
 

	
 
			if (v->type != VEH_Road) { // road vehicles can't be refitted
 
				if (v->cargo_type != w->cargo_type) {
 
					DoCommand(x, y, w->index, v->cargo_type, flags, CMD_REFIT_VEH(v->type));
 
				}
 
			}
 

	
 
			if (v->type == VEH_Train && !IsFrontEngine(v)) {
 
				// this s a train car
 
				// add this unit to the end of the train
 
				DoCommand(x, y, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
 
			} else {
 
				// this is a front engine or not a train. It need orders
 
				w_front = w;
 
				DoCommand(x, y, (v->index << 16) | w->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
 
			}
 
			w_rear = w;	// trains needs to know the last car in the train, so they can add more in next loop
 
		}
 
	} while (v->type == VEH_Train && (v = GetNextVehicle(v)) != NULL);
 

	
 
	if (flags & DC_EXEC && v_front->type == VEH_Train) {
 
		// _new_train_id needs to be the front engine due to the callback function
 
		_new_train_id = w_front->index;
 
	}
 
	return total_cost;
 
}
 

	
 
/*
 
 * move the cargo from one engine to another if possible
 
 */
 
static void MoveVehicleCargo(Vehicle *dest, Vehicle *source)
 
{
 
	Vehicle *v = dest;
 
	int units_moved;
 

	
 
	do {
 
		do {
 
			if (source->cargo_type != dest->cargo_type)
 
				continue;	// cargo not compatible
 

	
 
			if (dest->cargo_count == dest->cargo_cap)
 
				continue;	// the destination vehicle is already full
 

	
 
			units_moved = min(source->cargo_count, dest->cargo_cap - dest->cargo_count);
 
			source->cargo_count -= units_moved;
 
			dest->cargo_count   += units_moved;
 
			dest->cargo_source   = source->cargo_source;
 

	
 
			// copy the age of the cargo
 
			dest->cargo_days   = source->cargo_days;
 
			dest->day_counter  = source->day_counter;
 
			dest->tick_counter = source->tick_counter;
 

	
 
		} 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 = 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));
 
				// we add the refit cost to cost, so it's added to the cost animation
 
				// it's not in the calculation of having enough money to actually do the replace since it's rather hard to do by design, but since
 
				// we pay for it, it's nice to make the cost animation include it
 
				int32 temp_cost = DoCommand(0, 0, new_v->index, old_v->cargo_type, DC_EXEC, CMD_REFIT_VEH(new_v->type));
 
				if (!CmdFailed(temp_cost)) cost += temp_cost;
 
			}
 
		}
 

	
 

	
 
		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 */
 
			DoCommand(0, 0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
 
		} else {
 
			// copy/clone the orders
 
			DoCommand(0, 0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
 
			new_v->cur_order_index = old_v->cur_order_index;
 
			ChangeVehicleViewWindow(old_v, new_v);
 
			new_v->profit_this_year = old_v->profit_this_year;
 
			new_v->profit_last_year = old_v->profit_last_year;
 
			new_front = true;
 

	
 
			new_v->current_order = old_v->current_order;
 
			if (old_v->type == VEH_Train && GetNextVehicle(old_v) != NULL){
 
				Vehicle *temp_v = GetNextVehicle(old_v);
 

	
 
				// move the entire train to the new engine, excluding the old engine
 
				if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) {
 
					// we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead
 
					temp_v = GetNextVehicle(temp_v);
 
				}
 

	
 
				if (temp_v != NULL) {
 
					DoCommand(0, 0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
 
				}
 
			}
 
		}
 
		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
 
		MoveVehicleCargo(new_v->type == VEH_Train ? GetFirstVehicleInChain(new_v) : new_v, old_v);
 

	
 
		// Get the name of the old vehicle if it has a custom name.
 
		if ((old_v->string_id & 0xF800) != 0x7800) {
 
			vehicle_name[0] = '\0';
 
		} else {
 
			GetName(old_v->string_id & 0x7FF, vehicle_name);
 
		}
 
	}
 

	
 
	// sell the engine/ find out how much you get for the old engine
 
	cost += DoCommand(0, 0, old_v->index, 0, flags, CMD_SELL_VEH(old_v->type));
 

	
 
	if (new_front) {
 
		// now we assign the old unitnumber to the new vehicle
 
		new_v->unitnumber = cached_unitnumber;
 
	}
 

	
 
	// Transfer the name of the old vehicle.
 
	if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
 
		_cmd_text = vehicle_name;
 
		DoCommand(0, 0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
 
	}
 

	
 
	return cost;
 
}
 

	
 
/** replaces a vehicle if it's set for autoreplace or is too old
 
 * (used to be called autorenew)
 
 * @param v The vehicle to replace
 
 *	if the vehicle is a train, v needs to be the front engine
 
 *	return value is a pointer to the new vehicle, which is the same as the argument if nothing happened
 
 */
 
static void MaybeReplaceVehicle(Vehicle *v)
 
{
 
	Vehicle *w;
 
	const Player *p = GetPlayer(v->owner);
 
	byte flags = 0;
 
	int32 cost, temp_cost = 0;
 
	bool stopped = false;
 

	
 
	/* Remember the length in case we need to trim train later on
 
	 * If it's not a train, the value is unused
 
	 * round up to the length of the tiles used for the train instead of the train length instead
 
	 * Useful when newGRF uses custom length */
 
	uint16 old_total_length = (v->type == VEH_Train) ? ((v->u.rail.cached_total_length + 15 )/ 16)* 16 : -1;
 

	
 

	
 
	_current_player = v->owner;
 

	
 
	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 (!EngineHasReplacementForPlayer(p, w->engine_type)) // updates to a new model
 
					continue;
 
			}
 

	
 
			if (w->type == VEH_Train && IsTrainWagon(w) && !CanRefitTo(EngineReplacementForPlayer(p, w->engine_type), w->cargo_type)) {
 
				// we can't replace this wagon since we can't refit the new one to the right cargo type
 
				continue;
 
			}
 

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

	
 
			if (flags & DC_EXEC &&
 
					(w->type != VEH_Train || w->u.rail.first_engine == INVALID_ENGINE)) {
 
				/* 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);
 
				switch (v->type) {
 
					case VEH_Train:    message = STR_TRAIN_AUTORENEW_FAILED;       break;
 
					case VEH_Road:     message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
 
					case VEH_Ship:     message = STR_SHIP_AUTORENEW_FAILED;        break;
 
					case VEH_Aircraft: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
 
						// This should never happen
 
					default: NOT_REACHED(); message = 0; break;
 
				}
 

	
 
				AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
			}
 
			if (stopped) v->vehstatus &= ~VS_STOPPED;
 
			_current_player = OWNER_NONE;
 
			return;
 
		}
 

	
 
		if (flags & DC_EXEC) {
 
			break;	// we are done replacing since the loop ran once with DC_EXEC
 
		}
 
		// now we redo the loop, but this time we actually do stuff since we know that we can do it
 
		flags |= DC_EXEC;
 
	}
 

	
 
	/* If setting is on to try not to exceed the old length of the train with the replacement */
 
	if (v->type == VEH_Train && p->renew_keep_length) {
 
		Vehicle *temp;
 
		w = v;
 

	
 
		while (v->u.rail.cached_total_length > old_total_length) {
 
			// the train is too long. We will remove cars one by one from the start of the train until it's short enough
 
			while (w != NULL && !(RailVehInfo(w->engine_type)->flags&RVI_WAGON) ) {
 
				w = GetNextVehicle(w);
 
			}
 
			if (w == NULL) {
 
				// we failed to make the train short enough
 
				SetDParam(0, v->unitnumber);
 
				AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
				break;
 
			}
 
			temp = w;
 
			w = GetNextVehicle(w);
 
			DoCommand(0, 0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
 
			MoveVehicleCargo(v, temp);
 
			cost += DoCommand(0, 0, temp->index, 0, flags, CMD_SELL_VEH(temp->type));
 
		}
 
	}
 

	
 
	if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
 

	
 
	if (stopped) v->vehstatus &= ~VS_STOPPED;
 
	_current_player = OWNER_NONE;
 
}
0 comments (0 inline, 0 general)