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
 
@@ -72,97 +72,97 @@
 
		case VEH_TRAIN: {
 
			uint16 *capacities = GetCapacityOfArticulatedParts(engine_id, e->type);
 
			for (CargoID c = 0; c < NUM_CARGO; c++) {
 
				if (capacities[c] == 0) continue;
 
				return capacities[c];
 
			}
 
			return -1;
 
		} break;
 

	
 
		case VEH_SHIP:
 
		case VEH_AIRCRAFT:
 
			return e->GetDisplayDefaultCapacity();
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/* static */ int32 AIEngine::GetReliability(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)->reliability * 100 >> 16);
 
}
 

	
 
/* 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();
 
}
 

	
 
/* static */ int32 AIEngine::GetMaxTractiveEffort(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)->GetDisplayMaxTractiveEffort();
 
}
 

	
 
/* static */ AIVehicle::VehicleType AIEngine::GetVehicleType(EngineID engine_id)
 
{
 
	if (!IsValidEngine(engine_id)) return AIVehicle::VT_INVALID;
 

	
 
	switch (::Engine::Get(engine_id)->type) {
 
		case VEH_ROAD:     return AIVehicle::VT_ROAD;
 
		case VEH_TRAIN:    return AIVehicle::VT_RAIL;
 
		case VEH_SHIP:     return AIVehicle::VT_WATER;
 
		case VEH_AIRCRAFT: return AIVehicle::VT_AIR;
 
		default: NOT_REACHED();
 
	}
 
}
 

	
src/aircraft_cmd.cpp
Show inline comments
 
@@ -289,97 +289,97 @@ CommandCost CmdBuildAircraft(TileIndex t
 
		uint y = TileY(tile) * TILE_SIZE + 3;
 

	
 
		v->x_pos = u->x_pos = x;
 
		v->y_pos = u->y_pos = y;
 

	
 
		u->z_pos = GetSlopeZ(x, y);
 
		v->z_pos = u->z_pos + 1;
 

	
 
		v->running_ticks = 0;
 

	
 
//		u->delta_x = u->delta_y = 0;
 

	
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
		u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_SHADOW;
 

	
 
		v->spritenum = avi->image_index;
 
//		v->cargo_count = u->number_of_pieces = 0;
 

	
 
		v->cargo_cap = avi->passenger_capacity;
 
		u->cargo_cap = avi->mail_capacity;
 

	
 
		v->cargo_type = e->GetDefaultCargoType();
 
		u->cargo_type = CT_MAIL;
 

	
 
		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);
 

	
 
		v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_aircraft;
 

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

	
 
		v->cur_image = u->cur_image = SPR_IMG_QUERY;
 

	
 
		v->random_bits = VehicleRandomBits();
 
		u->random_bits = VehicleRandomBits();
 

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

	
 
		v->InvalidateNewGRFCacheOfChain();
 

	
 
		if (v->cargo_type != CT_PASSENGERS) {
 
			uint16 callback = CALLBACK_FAILED;
 

	
 
			if (HasBit(EngInfo(p1)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
				callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
 
			}
 

	
 
			if (callback == CALLBACK_FAILED) {
 
				/* Callback failed, or not executed; use the default cargo capacity */
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -631,97 +631,97 @@ int DrawVehiclePurchaseInfo(int left, in
 
			const RailVehicleInfo *rvi = RailVehInfo(engine_number);
 
			if (rvi->railveh_type == RAILVEH_WAGON) {
 
				y = DrawRailWagonPurchaseInfo(left, right, y, engine_number, rvi);
 
			} else {
 
				y = DrawRailEnginePurchaseInfo(left, right, y, engine_number, rvi);
 
			}
 

	
 
			/* Cargo type + capacity, or N/A */
 
			int new_y = DrawCargoCapacityInfo(left, right, y, engine_number, VEH_TRAIN, 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_ROAD: {
 
			y = DrawRoadVehPurchaseInfo(left, right, y, engine_number);
 

	
 
			/* Cargo type + capacity, or N/A */
 
			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();
 
	}
 
}
 

	
 
/** Engine drawing loop
 
 * @param type Type of vehicle (VEH_*)
 
 * @param x,y Where should the list start
 
 * @param eng_list What engines to draw
 
 * @param min where to start in the list
 
 * @param max where in the list to end
 
 * @param selected_id what engine to highlight as selected, if any
 
 * @param count_location Offset to print the engine count (used by autoreplace). 0 means it's off
 
 */
 
void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group)
 
{
 
	byte step_size = GetVehicleListHeight(type);
 
	byte x_offset = 0;
 
	byte y_offset = 0;
 

	
 
	assert(max <= eng_list->Length());
 

	
 
	switch (type) {
 
		case VEH_TRAIN:
 
			x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
 
			/* Fallthough */
src/engine.cpp
Show inline comments
 
@@ -271,96 +271,106 @@ uint Engine::GetDisplayMaxSpeed() const
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
uint Engine::GetPower() const
 
{
 
	/* Currently only trains have 'power' */
 
	switch (this->type) {
 
		case VEH_TRAIN:
 
			return GetEngineProperty(this->index, 0x0B, this->u.rail.power);
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Returns the weight for display purposes.
 
 * For dual-headed train-engines this is the weight of both heads
 
 * @return weight in display units metric tons
 
 */
 
uint Engine::GetDisplayWeight() const
 
{
 
	/* Currently only trains have 'weight' */
 
	switch (this->type) {
 
		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.
 
 */
 
EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
 
{
 
	const EngineIDMapping *end = this->End();
 
	EngineID index = 0;
 
	for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
 
		if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
 
			return index;
 
		}
 
	}
 
	return INVALID_ENGINE;
 
}
 

	
 
/** Sets cached values in Company::num_vehicles and Group::num_vehicles
 
 */
 
void SetCachedEngineCounts()
 
{
 
	size_t engines = Engine::GetPoolSize();
 

	
 
	/* Set up the engine count for all companies */
 
	Company *c;
 
	FOR_ALL_COMPANIES(c) {
 
		free(c->num_engines);
 
		c->num_engines = CallocT<EngineID>(engines);
src/engine_base.h
Show inline comments
 
@@ -14,96 +14,97 @@ typedef Pool<Engine, EngineID, 64, 64000
 
extern EnginePool _engine_pool;
 

	
 
struct Engine : EnginePool::PoolItem<&_engine_pool> {
 
	char *name;         ///< Custom name of engine
 
	Date intro_date;
 
	Date age;
 
	uint16 reliability;
 
	uint16 reliability_spd_dec;
 
	uint16 reliability_start, reliability_max, reliability_final;
 
	uint16 duration_phase_1, duration_phase_2, duration_phase_3;
 
	byte lifelength;
 
	byte flags;
 
	uint8 preview_company_rank;
 
	byte preview_wait;
 
	CompanyMask company_avail;
 
	uint8 original_image_index; ///< Original vehicle image index, thus the image index of the overridden vehicle
 
	VehicleType type; ///< type, ie VEH_ROAD, VEH_TRAIN, etc.
 

	
 
	EngineInfo info;
 

	
 
	union {
 
		RailVehicleInfo rail;
 
		RoadVehicleInfo road;
 
		ShipVehicleInfo ship;
 
		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)
 

	
 
#define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type)
 

	
 
static inline const EngineInfo *EngInfo(EngineID e)
 
{
 
	return &Engine::Get(e)->info;
 
}
 

	
 
static inline const RailVehicleInfo *RailVehInfo(EngineID e)
 
{
 
	return &Engine::Get(e)->u.rail;
 
}
 

	
 
static inline const RoadVehicleInfo *RoadVehInfo(EngineID e)
 
{
 
	return &Engine::Get(e)->u.road;
 
}
 

	
 
static inline const ShipVehicleInfo *ShipVehInfo(EngineID e)
 
{
 
	return &Engine::Get(e)->u.ship;
 
}
 

	
 
static inline const AircraftVehicleInfo *AircraftVehInfo(EngineID e)
src/roadveh_cmd.cpp
Show inline comments
 
@@ -185,97 +185,97 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	/* find the first free roadveh id */
 
	UnitID unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_ROAD);
 
	if (unit_num > _settings_game.vehicle.max_roadveh) {
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		const RoadVehicleInfo *rvi = RoadVehInfo(p1);
 

	
 
		RoadVehicle *v = new RoadVehicle();
 
		v->unitnumber = unit_num;
 
		v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
 
		v->owner = _current_company;
 

	
 
		v->tile = tile;
 
		int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		int 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->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();
 

	
 
		/* Call various callbacks after the whole consist has been constructed */
 
		for (RoadVehicle *u = v; u != NULL; u = u->Next()) {
 
			u->rcache.cached_veh_length = GetRoadVehLength(u);
 
			/* Cargo capacity is zero if and only if the vehicle cannot carry anything */
 
			if (u->cargo_cap != 0) u->cargo_cap = GetVehicleProperty(u, 0x0F, u->cargo_cap);
 
			v->InvalidateNewGRFCache();
 
			u->InvalidateNewGRFCache();
 
		}
 

	
 
		VehicleMove(v, false);
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
		InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
 
		InvalidateWindow(WC_COMPANY, v->owner);
 
		if (IsLocalCompany()) {
 
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Road window
 
		}
 

	
 
		Company::Get(_current_company)->num_engines[p1]++;
 

	
 
		CheckConsistencyOfArticulatedVehicle(v);
 
	}
 

	
src/ship_cmd.cpp
Show inline comments
 
@@ -754,97 +754,97 @@ CommandCost CmdBuildShip(TileIndex tile,
 
	if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
 

	
 
	if (flags & DC_QUERY_COST) return value;
 

	
 
	/* The ai_new queries the vehicle cost before building the route,
 
	 * so we must check against cheaters no sooner than now. --pasky */
 
	if (!IsShipDepotTile(tile)) return CMD_ERROR;
 
	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
 

	
 
	unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_SHIP);
 

	
 
	if (!Vehicle::CanAllocateItem() || unit_num > _settings_game.vehicle.max_ships)
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 

	
 
	if (flags & DC_EXEC) {
 
		int x;
 
		int y;
 

	
 
		const ShipVehicleInfo *svi = ShipVehInfo(p1);
 

	
 
		Ship *v = new Ship();
 
		v->unitnumber = unit_num;
 

	
 
		v->owner = _current_company;
 
		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);
 
		InvalidateWindow(WC_COMPANY, v->owner);
 
		if (IsLocalCompany())
 
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
 

	
 
		Company::Get(_current_company)->num_engines[p1]++;
 
	}
 

	
 
	return value;
 
}
 

	
 
/** Sell a ship.
 
 * @param tile unused
 
 * @param flags type of operation
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Ship *v = Ship::GetIfValid(p1);
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
 

	
 
	if (!v->IsStoppedInDepot()) {
src/train_cmd.cpp
Show inline comments
 
@@ -824,97 +824,97 @@ CommandCost CmdBuildRailVehicle(TileInde
 

	
 
	uint num_vehicles =
 
		(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) +
 
		CountArticulatedParts(p1, false);
 

	
 
	/* Check if depot and new engine uses the same kind of tracks *
 
	 * We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */
 
	if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
 

	
 
	/* Allow for the dual-heads and the articulated parts */
 
	if (!Vehicle::CanAllocateItem(num_vehicles)) {
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	UnitID unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_TRAIN);
 
	if (unit_num > _settings_game.vehicle.max_trains) {
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		DiagDirection dir = GetRailDepotDirection(tile);
 
		int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
 
		int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
 

	
 
		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);
 
		} else {
 
			AddArticulatedParts(v, VEH_TRAIN);
 
		}
 

	
 
		TrainConsistChanged(v, false);
 
		UpdateTrainGroupID(v);
 

	
 
		if (!HasBit(p2, 1) && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
 
			NormalizeTrainVehInDepot(v);
 
		}
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
		InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
 
		InvalidateWindow(WC_COMPANY, v->owner);
 
		if (IsLocalCompany()) {
 
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
 
		}
 

	
 
		Company::Get(_current_company)->num_engines[p1]++;
 

	
 
		CheckConsistencyOfArticulatedVehicle(v);
 
	}
 

	
 
	return value;
0 comments (0 inline, 0 general)