Changeset - r18330:f0d616eef655
[Not reviewed]
master
0 2 0
frosch - 13 years ago 2011-11-09 16:41:36
frosch@openttd.org
(svn r23176) -Codechange: Deduplicate code between Engine::DetermineCapacity() and Engine::GetDisplayDefaultCapacity().
2 files changed with 31 insertions and 57 deletions:
0 comments (0 inline, 0 general)
src/engine.cpp
Show inline comments
 
@@ -191,143 +191,101 @@ bool Engine::CanCarryCargo() const
 
		case VEH_AIRCRAFT:
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
	return this->GetDefaultCargoType() != CT_INVALID;
 
}
 

	
 

	
 
/**
 
 * Determines capacity of a given vehicle from scratch.
 
 * For aircraft the main capacity is determined. Mail might be present as well.
 
 * @note Keep this function consistent with Engine::GetDisplayDefaultCapacity().
 
 * @param v Vehicle of interest
 
 * @param v Vehicle of interest; NULL in purchase list
 
 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
 
 * @return Capacity
 
 */
 
uint Engine::DetermineCapacity(const Vehicle *v, uint16 *mail_capacity) const
 
{
 
	assert(this->index == v->engine_type);
 
	assert(v == NULL || this->index == v->engine_type);
 
	if (mail_capacity != NULL) *mail_capacity = 0;
 

	
 
	if (!this->CanCarryCargo()) return 0;
 

	
 
	if (mail_capacity != NULL && this->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
 
	CargoID default_cargo = this->GetDefaultCargoType();
 
	CargoID cargo_type = (v != NULL) ? v->cargo_type : default_cargo;
 

	
 
	if (mail_capacity != NULL && this->type == VEH_AIRCRAFT && IsCargoInClass(cargo_type, CC_PASSENGERS)) {
 
		*mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
 
	}
 
	CargoID default_cargo = this->GetDefaultCargoType();
 

	
 
	/* Check the refit capacity callback if we are not in the default configuration.
 
	 * Note: This might change to become more consistent/flexible/sane, esp. when default cargo is first refittable. */
 
	if (HasBit(this->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY) &&
 
			(default_cargo != v->cargo_type || v->cargo_subtype != 0)) {
 
			(default_cargo != cargo_type || (v != NULL && v->cargo_subtype != 0))) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, this->index, v);
 
		if (callback != CALLBACK_FAILED) return callback;
 
	}
 

	
 
	/* Get capacity according to property resp. CB */
 
	uint capacity;
 
	switch (this->type) {
 
		case VEH_TRAIN:
 
			capacity = GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY,        this->u.rail.capacity, v);
 

	
 
			/* In purchase list add the capacity of the second head. Always use the plain property for this. */
 
			if (v == NULL && this->u.rail.railveh_type == RAILVEH_MULTIHEAD) capacity += this->u.rail.capacity;
 
			break;
 

	
 
		case VEH_ROAD:
 
			capacity = GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY,      this->u.road.capacity, v);
 
			break;
 

	
 
		case VEH_SHIP:
 
			capacity = GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY,         this->u.ship.capacity, v);
 
			break;
 

	
 
		case VEH_AIRCRAFT:
 
			capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity, v);
 
			if (!IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
 
			if (!IsCargoInClass(cargo_type, CC_PASSENGERS)) {
 
				capacity += GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
 
			}
 
			if (v->cargo_type == CT_MAIL) return capacity;
 
			if (cargo_type == CT_MAIL) return capacity;
 
			default_cargo = CT_PASSENGERS; // Always use 'passengers' wrt. cargo multipliers
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 

	
 
	/* Apply multipliers depending on cargo- and vehicletype.
 
	 * Note: This might change to become more consistent/flexible. */
 
	if (this->type != VEH_SHIP) {
 
	if (this->type != VEH_SHIP && default_cargo != cargo_type) {
 
		switch (default_cargo) {
 
			case CT_PASSENGERS: break;
 
			case CT_MAIL:
 
			case CT_GOODS: capacity *= 2; break;
 
			default:       capacity *= 4; break;
 
		}
 
		switch (v->cargo_type) {
 
		switch (cargo_type) {
 
			case CT_PASSENGERS: break;
 
			case CT_MAIL:
 
			case CT_GOODS: capacity /= 2; break;
 
			default:       capacity /= 4; break;
 
		}
 
	}
 

	
 
	return capacity;
 
}
 

	
 
/**
 
 * Determines the default cargo capacity of an engine for display purposes.
 
 *
 
 * For planes carrying both passenger and mail this is the passenger capacity.
 
 * For multiheaded engines this is the capacity of both heads.
 
 * For articulated engines use GetCapacityOfArticulatedParts
 
 *
 
 * @note Keep this function consistent with Engine::DetermineCapacity().
 
 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
 
 * @return The default capacity
 
 * @see GetDefaultCargoType
 
 */
 
uint Engine::GetDisplayDefaultCapacity(uint16 *mail_capacity) const
 
{
 
	if (mail_capacity != NULL) *mail_capacity = 0;
 
	if (!this->CanCarryCargo()) return 0;
 
	switch (type) {
 
		case VEH_TRAIN:
 
			return GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0);
 

	
 
		case VEH_ROAD:
 
			return GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity);
 

	
 
		case VEH_SHIP:
 
			return GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity);
 

	
 
		case VEH_AIRCRAFT: {
 
			uint capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity);
 
			CargoID cargo = this->GetDefaultCargoType();
 
			if (IsCargoInClass(cargo, CC_PASSENGERS)) {
 
				if (mail_capacity != NULL) *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
 
			} else {
 
				capacity += GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
 
			}
 
			switch (cargo) {
 
				case CT_PASSENGERS:
 
				case CT_MAIL:       return capacity;
 
				case CT_GOODS:      return capacity / 2;
 
				default:            return capacity / 4;
 
			}
 
		}
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Return how much the running costs of this engine are.
 
 * @return Yearly running cost of the engine.
 
 */
 
Money Engine::GetRunningCost() const
 
{
 
	Price base_price;
 
	uint cost_factor;
 
	switch (this->type) {
 
		case VEH_ROAD:
 
			base_price = this->u.road.running_cost_class;
 
			if (base_price == INVALID_PRICE) return 0;
 
			cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
src/engine_base.h
Show inline comments
 
@@ -77,25 +77,41 @@ struct Engine : EnginePool::PoolItem<&_e
 
	 *
 
	 * @return The default cargo type.
 
	 * @see CanCarryCargo
 
	 */
 
	CargoID GetDefaultCargoType() const
 
	{
 
		return this->info.cargo_type;
 
	}
 

	
 
	uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity = NULL) const;
 

	
 
	bool CanCarryCargo() const;
 
	uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const;
 

	
 
	/**
 
	 * Determines the default cargo capacity of an engine for display purposes.
 
	 *
 
	 * For planes carrying both passenger and mail this is the passenger capacity.
 
	 * For multiheaded engines this is the capacity of both heads.
 
	 * For articulated engines use GetCapacityOfArticulatedParts
 
	 *
 
	 * @param mail_capacity returns secondary cargo (mail) capacity of aircraft
 
	 * @return The default capacity
 
	 * @see GetDefaultCargoType
 
	 */
 
	uint GetDisplayDefaultCapacity(uint16 *mail_capacity = NULL) const
 
	{
 
		return this->DetermineCapacity(NULL, mail_capacity);
 
	}
 

	
 
	Money GetRunningCost() const;
 
	Money GetCost() const;
 
	uint GetDisplayMaxSpeed() const;
 
	uint GetPower() const;
 
	uint GetDisplayWeight() const;
 
	uint GetDisplayMaxTractiveEffort() const;
 
	Date GetLifeLengthInDays() const;
 

	
 
	/**
 
	 * Check if the engine is a ground vehicle.
 
	 * @return True iff the engine is a train or a road vehicle.
 
	 */
0 comments (0 inline, 0 general)