Changeset - r12160:b49804528c72
[Not reviewed]
master
0 8 0
rubidium - 15 years ago 2009-06-16 13:52:18
rubidium@openttd.org
(svn r16581) -Codechange: unify the access to Engine::lifelength.
8 files changed with 17 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_engine.cpp
Show inline comments
 
@@ -96,49 +96,49 @@
 
}
 

	
 
/* static */ int32 AIEngine::GetMaxSpeed(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 

	
 
	const Engine *e = ::Engine::Get(engine_id);
 
	int32 max_speed = e->GetDisplayMaxSpeed(); // km-ish/h
 
	if (e->type == VEH_AIRCRAFT) max_speed /= _settings_game.vehicle.plane_speed;
 
	return max_speed;
 
}
 

	
 
/* static */ Money AIEngine::GetPrice(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 

	
 
	return ::Engine::Get(engine_id)->GetCost();
 
}
 

	
 
/* static */ int32 AIEngine::GetMaxAge(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 
	if (GetVehicleType(engine_id) == AIVehicle::VT_RAIL && IsWagon(engine_id)) return -1;
 

	
 
	return ::Engine::Get(engine_id)->lifelength * DAYS_IN_LEAP_YEAR;
 
	return ::Engine::Get(engine_id)->GetLifeLengthInDays();
 
}
 

	
 
/* static */ Money AIEngine::GetRunningCost(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 

	
 
	return ::Engine::Get(engine_id)->GetRunningCost();
 
}
 

	
 
/* static */ int32 AIEngine::GetPower(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 
	if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
 
	if (IsWagon(engine_id)) return -1;
 

	
 
	return ::Engine::Get(engine_id)->GetPower();
 
}
 

	
 
/* static */ int32 AIEngine::GetWeight(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return -1;
 
	if (GetVehicleType(engine_id) != AIVehicle::VT_RAIL) return -1;
 

	
 
	return ::Engine::Get(engine_id)->GetDisplayWeight();
src/aircraft_cmd.cpp
Show inline comments
 
@@ -313,49 +313,49 @@ CommandCost CmdBuildAircraft(TileIndex t
 
		v->cargo_subtype = 0;
 

	
 
		v->name = NULL;
 
//		v->next_order_param = v->next_order = 0;
 

	
 
//		v->load_unload_time_rem = 0;
 
//		v->progress = 0;
 
		v->last_station_visited = INVALID_STATION;
 
//		v->destination_coords = 0;
 

	
 
		v->max_speed = avi->max_speed;
 
		v->acceleration = avi->acceleration;
 
		v->engine_type = p1;
 
		u->engine_type = p1;
 

	
 
		v->subtype = (avi->subtype & AIR_CTOL ? AIR_AIRCRAFT : AIR_HELICOPTER);
 
		v->UpdateDeltaXY(INVALID_DIR);
 
		v->value = value.GetCost();
 

	
 
		u->subtype = AIR_SHADOW;
 
		u->UpdateDeltaXY(INVALID_DIR);
 

	
 
		v->reliability = e->reliability;
 
		v->reliability_spd_dec = e->reliability_spd_dec;
 
		v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR;
 
		v->max_age = e->GetLifeLengthInDays();
 

	
 
		_new_vehicle_id = v->index;
 

	
 
		/* When we click on hangar we know the tile it is on. By that we know
 
		 * its position in the array of depots the airport has.....we can search
 
		 * layout for #th position of depot. Since layout must start with a listing
 
		 * of all depots, it is simple */
 
		for (uint i = 0;; i++) {
 
			const Station *st = GetStationByTile(tile);
 
			const AirportFTAClass *apc = st->Airport();
 

	
 
			assert(i != apc->nof_depots);
 
			if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
 
				assert(apc->layout[i].heading == HANGAR);
 
				v->pos = apc->layout[i].position;
 
				break;
 
			}
 
		}
 

	
 
		v->state = HANGAR;
 
		v->previous_pos = v->pos;
 
		v->targetairport = GetStationIndex(tile);
 
		v->SetNext(u);
 

	
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -655,49 +655,49 @@ int DrawVehiclePurchaseInfo(int left, in
 
			int new_y = DrawCargoCapacityInfo(left, right, y, engine_number, VEH_ROAD, refittable);
 

	
 
			if (new_y == y) {
 
				SetDParam(0, CT_INVALID);
 
				SetDParam(2, STR_EMPTY);
 
				DrawString(left, right, y, STR_PURCHASE_INFO_CAPACITY);
 
				y += FONT_HEIGHT_NORMAL;
 
			} else {
 
				y = new_y;
 
			}
 
			break;
 
		}
 
		case VEH_SHIP:
 
			y = DrawShipPurchaseInfo(left, right, y, engine_number, ShipVehInfo(engine_number), refittable);
 
			break;
 
		case VEH_AIRCRAFT:
 
			y = DrawAircraftPurchaseInfo(left, right, y, engine_number, AircraftVehInfo(engine_number), refittable);
 
			break;
 
	}
 

	
 
	/* Draw details, that applies to all types except rail wagons */
 
	if (e->type != VEH_TRAIN || RailVehInfo(engine_number)->railveh_type != RAILVEH_WAGON) {
 
		/* Design date - Life length */
 
		SetDParam(0, ymd.year);
 
		SetDParam(1, e->lifelength);
 
		SetDParam(1, e->GetLifeLengthInDays() / DAYS_IN_LEAP_YEAR);
 
		DrawString(left, right, y, STR_PURCHASE_INFO_DESIGNED_LIFE);
 
		y += FONT_HEIGHT_NORMAL;
 

	
 
		/* Reliability */
 
		SetDParam(0, e->reliability * 100 >> 16);
 
		DrawString(left, right, y, STR_PURCHASE_INFO_RELIABILITY);
 
		y += FONT_HEIGHT_NORMAL;
 
	}
 

	
 
	/* Additional text from NewGRF */
 
	y = ShowAdditionalText(left, right, y, engine_number);
 
	if (refittable) y = ShowRefitOptionsList(left, right, y, engine_number);
 

	
 
	return y;
 
}
 

	
 
static void DrawVehicleEngine(VehicleType type, int x, int y, EngineID engine, SpriteID pal)
 
{
 
	switch (type) {
 
		case VEH_TRAIN:    DrawTrainEngine(   x, y, engine, pal); break;
 
		case VEH_ROAD:     DrawRoadVehEngine( x, y, engine, pal); break;
 
		case VEH_SHIP:     DrawShipEngine(    x, y, engine, pal); break;
 
		case VEH_AIRCRAFT: DrawAircraftEngine(x, y, engine, pal); break;
 
		default: NOT_REACHED();
src/engine.cpp
Show inline comments
 
@@ -295,48 +295,58 @@ uint Engine::GetDisplayWeight() const
 
		case VEH_TRAIN:
 
			return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Returns the tractive effort for display purposes.
 
 * For dual-headed train-engines this is the tractive effort of both heads
 
 * @return tractive effort in display units kN
 
 */
 
uint Engine::GetDisplayMaxTractiveEffort() const
 
{
 
	/* Currently only trains have 'tractive effort' */
 
	switch (this->type) {
 
		case VEH_TRAIN:
 
			return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, 0x1F, this->u.rail.tractive_effort)) / 256;
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Returns the vehicle's life length in days.
 
 * @return the life length
 
 */
 
Date Engine::GetLifeLengthInDays() const
 
{
 
	/* Assume leap years; this gives the player a bit more than the given amount of years, but never less. */
 
	return this->lifelength * DAYS_IN_LEAP_YEAR;
 
}
 

	
 
/**
 
 * Initializes the EngineOverrideManager with the default engines.
 
 */
 
void EngineOverrideManager::ResetToDefaultMapping()
 
{
 
	this->Clear();
 
	for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
 
		for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
 
			EngineIDMapping *eid = this->Append();
 
			eid->type            = type;
 
			eid->grfid           = INVALID_GRFID;
 
			eid->internal_id     = internal_id;
 
			eid->substitute_id   = internal_id;
 
		}
 
	}
 
}
 

	
 
/**
 
 * Looks up an EngineID in the EngineOverrideManager
 
 * @param type Vehicle type
 
 * @param grf_local_id The local id in the newgrf
 
 * @param grfid The GrfID that defines the scope of grf_local_id.
 
 *              If a newgrf overrides the engines of another newgrf, the "scope grfid" is the ID of the overridden newgrf.
 
 *              If dynnamic_engines is disabled, all newgrf share the same ID scope identified by INVALID_GRFID.
 
 * @return The engine ID if present, or INVALID_ENGINE if not.
src/engine_base.h
Show inline comments
 
@@ -38,48 +38,49 @@ struct Engine : EnginePool::PoolItem<&_e
 
		AircraftVehicleInfo air;
 
	} u;
 

	
 
	/* NewGRF related data */
 
	const struct GRFFile *grffile;
 
	const struct SpriteGroup *group[NUM_CARGO + 2];
 
	uint16 internal_id;                             ///< ID within the GRF file
 
	uint16 overrides_count;
 
	struct WagonOverride *overrides;
 
	uint16 list_position;
 

	
 
	Engine();
 
	Engine(VehicleType type, EngineID base);
 
	~Engine();
 

	
 
	CargoID GetDefaultCargoType() const;
 
	bool CanCarryCargo() const;
 
	uint GetDisplayDefaultCapacity() const;
 
	Money GetRunningCost() const;
 
	Money GetCost() const;
 
	uint GetDisplayMaxSpeed() const;
 
	uint GetPower() const;
 
	uint GetDisplayWeight() const;
 
	uint GetDisplayMaxTractiveEffort() const;
 
	Date GetLifeLengthInDays() const;
 
};
 

	
 
struct EngineIDMapping {
 
	uint32 grfid;          ///< The GRF ID of the file the entity belongs to
 
	uint16 internal_id;    ///< The internal ID within the GRF file
 
	VehicleTypeByte type;  ///< The engine type
 
	uint8  substitute_id;  ///< The (original) entity ID to use if this GRF is not available (currently not used)
 
};
 

	
 
/**
 
 * Stores the mapping of EngineID to the internal id of newgrfs.
 
 * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases.
 
 */
 
struct EngineOverrideManager : SmallVector<EngineIDMapping, 256> {
 
	static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries
 

	
 
	void ResetToDefaultMapping();
 
	EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid);
 
};
 

	
 
extern EngineOverrideManager _engine_mngr;
 

	
 
#define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start)
 
#define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0)
src/roadveh_cmd.cpp
Show inline comments
 
@@ -209,49 +209,49 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
//		v->running_ticks = 0;
 

	
 
		v->state = RVSB_IN_DEPOT;
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 

	
 
		v->spritenum = rvi->image_index;
 
		v->cargo_type = e->GetDefaultCargoType();
 
//		v->cargo_subtype = 0;
 
		v->cargo_cap = rvi->capacity;
 
//		v->cargo_count = 0;
 
		v->value = cost.GetCost();
 
//		v->day_counter = 0;
 
//		v->next_order_param = v->next_order = 0;
 
//		v->load_unload_time_rem = 0;
 
//		v->progress = 0;
 

	
 
//		v->overtaking = 0;
 

	
 
		v->last_station_visited = INVALID_STATION;
 
		v->max_speed = rvi->max_speed;
 
		v->engine_type = (EngineID)p1;
 

	
 
		v->reliability = e->reliability;
 
		v->reliability_spd_dec = e->reliability_spd_dec;
 
		v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR;
 
		v->max_age = e->GetLifeLengthInDays();
 
		_new_vehicle_id = v->index;
 

	
 
		v->name = NULL;
 

	
 
		v->service_interval = Company::Get(v->owner)->settings.vehicle.servint_roadveh;
 

	
 
		v->date_of_last_service = _date;
 
		v->build_year = _cur_year;
 

	
 
		v->cur_image = SPR_IMG_QUERY;
 
		v->random_bits = VehicleRandomBits();
 
		SetRoadVehFront(v);
 

	
 
		v->roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
 
		v->compatible_roadtypes = RoadTypeToRoadTypes(v->roadtype);
 
		v->rcache.cached_veh_length = 8;
 

	
 
		v->vehicle_flags = 0;
 
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
 

	
 
		v->cargo_cap = rvi->capacity;
 

	
 
		AddArticulatedParts(v, VEH_ROAD);
 
		v->InvalidateNewGRFCacheOfChain();
src/ship_cmd.cpp
Show inline comments
 
@@ -778,49 +778,49 @@ CommandCost CmdBuildShip(TileIndex tile,
 
		v->tile = tile;
 
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = GetSlopeZ(x, y);
 

	
 
		v->running_ticks = 0;
 

	
 
		v->UpdateDeltaXY(v->direction);
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 

	
 
		v->spritenum = svi->image_index;
 
		v->cargo_type = e->GetDefaultCargoType();
 
		v->cargo_subtype = 0;
 
		v->cargo_cap = svi->capacity;
 
		v->value = value.GetCost();
 

	
 
		v->last_station_visited = INVALID_STATION;
 
		v->max_speed = svi->max_speed;
 
		v->engine_type = p1;
 

	
 
		v->reliability = e->reliability;
 
		v->reliability_spd_dec = e->reliability_spd_dec;
 
		v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR;
 
		v->max_age = e->GetLifeLengthInDays();
 
		_new_vehicle_id = v->index;
 

	
 
		v->name = NULL;
 
		v->state = TRACK_BIT_DEPOT;
 

	
 
		v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_ships;
 
		v->date_of_last_service = _date;
 
		v->build_year = _cur_year;
 
		v->cur_image = SPR_IMG_QUERY;
 
		v->random_bits = VehicleRandomBits();
 

	
 
		v->vehicle_flags = 0;
 
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
 

	
 
		v->InvalidateNewGRFCacheOfChain();
 

	
 
		v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity);
 

	
 
		v->InvalidateNewGRFCacheOfChain();
 

	
 
		VehicleMove(v, false);
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
src/train_cmd.cpp
Show inline comments
 
@@ -848,49 +848,49 @@ CommandCost CmdBuildRailVehicle(TileInde
 
		Train *v = new Train();
 
		v->unitnumber = unit_num;
 
		v->direction = DiagDirToDir(dir);
 
		v->tile = tile;
 
		v->owner = _current_company;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = GetSlopeZ(x, y);
 
//		v->running_ticks = 0;
 
		v->track = TRACK_BIT_DEPOT;
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
		v->spritenum = rvi->image_index;
 
		v->cargo_type = e->GetDefaultCargoType();
 
//		v->cargo_subtype = 0;
 
		v->cargo_cap = rvi->capacity;
 
		v->max_speed = rvi->max_speed;
 
		v->value = value.GetCost();
 
		v->last_station_visited = INVALID_STATION;
 
//		v->dest_tile = 0;
 

	
 
		v->engine_type = p1;
 

	
 
		v->reliability = e->reliability;
 
		v->reliability_spd_dec = e->reliability_spd_dec;
 
		v->max_age = e->lifelength * DAYS_IN_LEAP_YEAR;
 
		v->max_age = e->GetLifeLengthInDays();
 

	
 
		v->name = NULL;
 
		v->railtype = rvi->railtype;
 
		_new_vehicle_id = v->index;
 

	
 
		v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_trains;
 
		v->date_of_last_service = _date;
 
		v->build_year = _cur_year;
 
		v->cur_image = SPR_IMG_QUERY;
 
		v->random_bits = VehicleRandomBits();
 

	
 
//		v->vehicle_flags = 0;
 
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
 

	
 
		v->group_id = DEFAULT_GROUP;
 

	
 
//		v->subtype = 0;
 
		SetFrontEngine(v);
 
		SetTrainEngine(v);
 

	
 
		VehicleMove(v, false);
 

	
 
		if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
 
			AddRearEngineToMultiheadedTrain(v);
0 comments (0 inline, 0 general)