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
 
@@ -1593,97 +1593,101 @@ static void MoveVehicleCargo(Vehicle *de
 
				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
0 comments (0 inline, 0 general)