Changeset - r13208:40382e8abf19
[Not reviewed]
master
0 12 0
frosch - 15 years ago 2009-10-06 19:17:07
frosch@openttd.org
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
12 files changed with 88 insertions and 68 deletions:
0 comments (0 inline, 0 general)
src/aircraft_cmd.cpp
Show inline comments
 
@@ -108,21 +108,22 @@ enum HelicopterRotorStates {
 
static StationID FindNearestHangar(const Aircraft *v)
 
{
 
	const Station *st;
 
	uint best = 0;
 
	StationID index = INVALID_STATION;
 
	TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT)) continue;
 

	
 
		const AirportFTAClass *afc = st->Airport();
 
		if (afc->nof_depots == 0 || (
 
					/* don't crash the plane if we know it can't land at the airport */
 
					(afc->flags & AirportFTAClass::SHORT_STRIP) &&
 
					(AircraftVehInfo(v->engine_type)->subtype & AIR_FAST) &&
 
					(avi->subtype & AIR_FAST) &&
 
					!_cheats.no_jetcrash.value)) {
 
			continue;
 
		}
 

	
 
		/* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
 
		uint distance = DistanceSquare(vtile, st->airport_tile);
 
@@ -182,19 +183,20 @@ SpriteID GetRotorImage(const Aircraft *v
 
	/* Return standard rotor sprites if there are no custom sprites for this helicopter */
 
	return SPR_ROTOR_STOPPED + w->state;
 
}
 

	
 
static SpriteID GetAircraftIcon(EngineID engine)
 
{
 
	uint8 spritenum = AircraftVehInfo(engine)->image_index;
 
	const Engine *e = Engine::Get(engine);
 
	uint8 spritenum = e->u.air.image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = Engine::Get(engine)->original_image_index;
 
		spritenum = e->original_image_index;
 
	}
 

	
 
	return DIR_W + _aircraft_sprite[spritenum];
 
}
 

	
 
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
 
@@ -255,14 +257,14 @@ uint16 AircraftDefaultCargoCapacity(Carg
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdBuildAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (!IsEngineBuildable(p1, VEH_AIRCRAFT, _current_company)) return_cmd_error(STR_ERROR_AIRCRAFT_NOT_AVAILABLE);
 

	
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(p1);
 
	const Engine *e = Engine::Get(p1);
 
	const AircraftVehicleInfo *avi = &e->u.air;
 
	CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
 

	
 
	/* Engines without valid cargo should not be available */
 
	if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
 

	
 
	/* to just query the cost, it is not neccessary to have a valid tile (automation/AI) */
 
@@ -383,13 +385,13 @@ CommandCost CmdBuildAircraft(TileIndex t
 

	
 
		v->InvalidateNewGRFCacheOfChain();
 

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

	
 
			if (HasBit(EngInfo(p1)->callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
			if (HasBit(e->info.callback_mask, 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 */
 
				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, avi);
 
@@ -534,15 +536,17 @@ CommandCost CmdRefitAircraft(TileIndex t
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
 

	
 
	/* Check cargo */
 
	CargoID new_cid = GB(p2, 0, 8);
 
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
 

	
 
	const Engine *e = Engine::Get(v->engine_type);
 

	
 
	/* Check the refit capacity callback */
 
	uint16 callback = CALLBACK_FAILED;
 
	if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
	if (HasBit(e->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
		/* Back up the existing cargo type */
 
		CargoID temp_cid = v->cargo_type;
 
		byte temp_subtype = v->cargo_subtype;
 
		v->cargo_type = new_cid;
 
		v->cargo_subtype = new_subtype;
 

	
 
@@ -550,19 +554,17 @@ CommandCost CmdRefitAircraft(TileIndex t
 

	
 
		/* Restore the cargo type */
 
		v->cargo_type = temp_cid;
 
		v->cargo_subtype = temp_subtype;
 
	}
 

	
 
	const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
 

	
 
	uint pass;
 
	if (callback == CALLBACK_FAILED) {
 
		/* If the callback failed, or wasn't executed, use the aircraft's
 
		 * default cargo capacity */
 
		pass = AircraftDefaultCargoCapacity(new_cid, avi);
 
		pass = AircraftDefaultCargoCapacity(new_cid, &e->u.air);
 
	} else {
 
		pass = callback;
 
	}
 
	_returned_refit_capacity = pass;
 

	
 
	CommandCost cost;
 
@@ -571,13 +573,13 @@ CommandCost CmdRefitAircraft(TileIndex t
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		v->cargo_cap = pass;
 

	
 
		Vehicle *u = v->Next();
 
		uint mail = IsCargoInClass(new_cid, CC_PASSENGERS) ? avi->mail_capacity : 0;
 
		uint mail = IsCargoInClass(new_cid, CC_PASSENGERS) ? e->u.air.mail_capacity : 0;
 
		u->cargo_cap = mail;
 
		v->cargo.Truncate(v->cargo_type == new_cid ? pass : 0);
 
		u->cargo.Truncate(v->cargo_type == new_cid ? mail : 0);
 
		v->cargo_type = new_cid;
 
		v->cargo_subtype = new_subtype;
 
		v->colourmap = PAL_NONE; // invalidate vehicle colour map
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -53,38 +53,37 @@ bool CheckAutoreplaceValidity(EngineID f
 
	 * check that it's an engine that is in the engine array */
 
	if (!Engine::IsValidID(from) || !Engine::IsValidID(to)) return false;
 

	
 
	/* we can't replace an engine into itself (that would be autorenew) */
 
	if (from == to) return false;
 

	
 
	VehicleType type = Engine::Get(from)->type;
 
	const Engine *e_from = Engine::Get(from);
 
	const Engine *e_to = Engine::Get(to);
 
	VehicleType type = e_from->type;
 

	
 
	/* check that the new vehicle type is available to the company and its type is the same as the original one */
 
	if (!IsEngineBuildable(to, type, company)) return false;
 

	
 
	switch (type) {
 
		case VEH_TRAIN: {
 
			const RailVehicleInfo *rvi_from = RailVehInfo(from);
 
			const RailVehicleInfo *rvi_to   = RailVehInfo(to);
 

	
 
			/* make sure the railtypes are compatible */
 
			if ((GetRailTypeInfo(rvi_from->railtype)->compatible_railtypes & GetRailTypeInfo(rvi_to->railtype)->compatible_railtypes) == 0) return false;
 
			if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false;
 

	
 
			/* make sure we do not replace wagons with engines or vise versa */
 
			if ((rvi_from->railveh_type == RAILVEH_WAGON) != (rvi_to->railveh_type == RAILVEH_WAGON)) return false;
 
			if ((e_from->u.rail.railveh_type == RAILVEH_WAGON) != (e_to->u.rail.railveh_type == RAILVEH_WAGON)) return false;
 
			break;
 
		}
 

	
 
		case VEH_ROAD:
 
			/* make sure that we do not replace a tram with a normal road vehicles or vise versa */
 
			if (HasBit(EngInfo(from)->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(to)->misc_flags, EF_ROAD_TRAM)) return false;
 
			if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false;
 
			break;
 

	
 
		case VEH_AIRCRAFT:
 
			/* make sure that we do not replace a plane with a helicopter or vise versa */
 
			if ((AircraftVehInfo(from)->subtype & AIR_CTOL) != (AircraftVehInfo(to)->subtype & AIR_CTOL)) return false;
 
			if ((e_from->u.air.subtype & AIR_CTOL) != (e_to->u.air.subtype & AIR_CTOL)) return false;
 
			break;
 

	
 
		default: break;
 
	}
 

	
 
	/* the engines needs to be able to carry the same cargo */
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -295,14 +295,14 @@ static int CDECL AircraftEngineCargoSort
 
	int va = e_a->GetDisplayDefaultCapacity();
 
	int vb = e_b->GetDisplayDefaultCapacity();
 
	int r = va - vb;
 

	
 
	if (r == 0) {
 
		/* The planes has the same passenger capacity. Check mail capacity instead */
 
		va = AircraftVehInfo(*a)->mail_capacity;
 
		vb = AircraftVehInfo(*b)->mail_capacity;
 
		va = e_a->u.air.mail_capacity;
 
		vb = e_b->u.air.mail_capacity;
 
		r = va - vb;
 

	
 
		if (r == 0) {
 
			/* Use EngineID to sort instead since we want consistent sorting */
 
			return EngineNumberSorter(a, b);
 
		}
 
@@ -630,17 +630,16 @@ int DrawVehiclePurchaseInfo(int left, in
 
	ConvertDateToYMD(e->intro_date, &ymd);
 
	bool refittable = IsArticulatedVehicleRefittable(engine_number);
 

	
 
	switch (e->type) {
 
		default: NOT_REACHED();
 
		case VEH_TRAIN: {
 
			const RailVehicleInfo *rvi = RailVehInfo(engine_number);
 
			if (rvi->railveh_type == RAILVEH_WAGON) {
 
				y = DrawRailWagonPurchaseInfo(left, right, y, engine_number, rvi);
 
			if (e->u.rail.railveh_type == RAILVEH_WAGON) {
 
				y = DrawRailWagonPurchaseInfo(left, right, y, engine_number, &e->u.rail);
 
			} else {
 
				y = DrawRailEnginePurchaseInfo(left, right, y, engine_number, rvi);
 
				y = DrawRailEnginePurchaseInfo(left, right, y, engine_number, &e->u.rail);
 
			}
 

	
 
			/* Cargo type + capacity, or N/A */
 
			int new_y = DrawCargoCapacityInfo(left, right, y, engine_number, VEH_TRAIN, refittable);
 

	
 
			if (new_y == y) {
 
@@ -667,21 +666,21 @@ int DrawVehiclePurchaseInfo(int left, in
 
			} else {
 
				y = new_y;
 
			}
 
			break;
 
		}
 
		case VEH_SHIP:
 
			y = DrawShipPurchaseInfo(left, right, y, engine_number, ShipVehInfo(engine_number), refittable);
 
			y = DrawShipPurchaseInfo(left, right, y, engine_number, &e->u.ship, refittable);
 
			break;
 
		case VEH_AIRCRAFT:
 
			y = DrawAircraftPurchaseInfo(left, right, y, engine_number, AircraftVehInfo(engine_number), refittable);
 
			y = DrawAircraftPurchaseInfo(left, right, y, engine_number, &e->u.air, refittable);
 
			break;
 
	}
 

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

	
src/economy.cpp
Show inline comments
 
@@ -1136,18 +1136,19 @@ static void LoadUnloadVehicle(Vehicle *v
 

	
 
	CargoPayment *payment = v->cargo_payment;
 

	
 
	for (; v != NULL; v = v->Next()) {
 
		if (v->cargo_cap == 0) continue;
 

	
 
		byte load_amount = EngInfo(v->engine_type)->load_amount;
 
		const Engine *e = Engine::Get(v->engine_type);
 
		byte load_amount = e->info.load_amount;
 

	
 
		/* The default loadamount for mail is 1/4 of the load amount for passengers */
 
		if (v->type == VEH_AIRCRAFT && !Aircraft::From(v)->IsNormalAircraft()) load_amount = (load_amount + 3) / 4;
 

	
 
		if (_settings_game.order.gradual_loading && HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_LOAD_AMOUNT)) {
 
		if (_settings_game.order.gradual_loading && HasBit(e->info.callback_mask, CBM_VEHICLE_LOAD_AMOUNT)) {
 
			uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
 
			if (cb_load_amount != CALLBACK_FAILED && GB(cb_load_amount, 0, 8) != 0) load_amount = GB(cb_load_amount, 0, 8);
 
		}
 

	
 
		GoodsEntry *ge = &st->goods[v->cargo_type];
 

	
src/engine.cpp
Show inline comments
 
@@ -520,18 +520,16 @@ static void AcceptEnginePreview(EngineID
 
{
 
	Engine *e = Engine::Get(eid);
 
	Company *c = Company::Get(company);
 

	
 
	SetBit(e->company_avail, company);
 
	if (e->type == VEH_TRAIN) {
 
		const RailVehicleInfo *rvi = RailVehInfo(eid);
 

	
 
		assert(rvi->railtype < RAILTYPE_END);
 
		SetBit(c->avail_railtypes, rvi->railtype);
 
		assert(e->u.rail.railtype < RAILTYPE_END);
 
		SetBit(c->avail_railtypes, e->u.rail.railtype);
 
	} else if (e->type == VEH_ROAD) {
 
		SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
 
		SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
 
	}
 

	
 
	e->preview_company_rank = 0xFF;
 
	if (company == _local_company) {
 
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
 
	}
 
@@ -762,13 +760,13 @@ bool IsEngineBuildable(EngineID engine, 
 
	/* check if it's available */
 
	if (!HasBit(e->company_avail, company)) return false;
 

	
 
	if (type == VEH_TRAIN) {
 
		/* Check if the rail type is available to this company */
 
		const Company *c = Company::Get(company);
 
		if (((GetRailTypeInfo(RailVehInfo(engine)->railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
 
		if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
/**
src/engine_gui.cpp
Show inline comments
 
@@ -27,19 +27,20 @@
 
/** Return the category of an engine.
 
 * @param engine Engine to examine.
 
 * @return String describing the category ("road veh", "train". "airplane", or "ship") of the engine.
 
 */
 
StringID GetEngineCategoryName(EngineID engine)
 
{
 
	switch (Engine::Get(engine)->type) {
 
	const Engine *e = Engine::Get(engine);
 
	switch (e->type) {
 
		default: NOT_REACHED();
 
		case VEH_ROAD:              return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
 
		case VEH_AIRCRAFT:          return STR_ENGINE_PREVIEW_AIRCRAFT;
 
		case VEH_SHIP:              return STR_ENGINE_PREVIEW_SHIP;
 
		case VEH_TRAIN:
 
			return GetRailTypeInfo(RailVehInfo(engine)->railtype)->strings.new_loco;
 
			return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
 
	}
 
}
 

	
 
/** Widgets used for the engine preview window */
 
enum EnginePreviewWidgets {
 
	EPW_CLOSE,      ///< Close button
src/roadveh.h
Show inline comments
 
@@ -120,13 +120,19 @@ struct RoadVehicle : public SpecializedV
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
 
	bool IsPrimaryVehicle() const { return this->IsRoadVehFront(); }
 
	SpriteID GetImage(Direction direction) const;
 
	int GetDisplaySpeed() const { return this->cur_speed / 2; }
 
	int GetDisplayMaxSpeed() const { return this->max_speed / 2; }
 
	Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * GetPriceByIndex(RoadVehInfo(this->engine_type)->running_cost_class); }
 

	
 
	Money GetRunningCost() const
 
	{
 
		const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type);
 
		return rvi->running_cost * GetPriceByIndex(rvi->running_cost_class);
 
	}
 

	
 
	int GetDisplayImageWidth(Point *offset = NULL) const;
 
	bool IsInDepot() const { return this->state == RVSB_IN_DEPOT; }
 
	bool IsStoppedInDepot() const;
 
	bool Tick();
 
	void OnNewDay();
 
	Trackdir GetVehicleTrackdir() const;
src/roadveh_cmd.cpp
Show inline comments
 
@@ -104,19 +104,20 @@ int RoadVehicle::GetDisplayImageWidth(Po
 
	}
 
	return this->rcache.cached_veh_length * reference_width / 8;
 
}
 

	
 
static SpriteID GetRoadVehIcon(EngineID engine)
 
{
 
	uint8 spritenum = RoadVehInfo(engine)->image_index;
 
	const Engine *e = Engine::Get(engine);
 
	uint8 spritenum = e->u.road.image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = Engine::Get(engine)->original_image_index;
 
		spritenum = e->original_image_index;
 
	}
 

	
 
	return DIR_W + _roadveh_images[spritenum];
 
}
 

	
 
SpriteID RoadVehicle::GetImage(Direction direction) const
 
@@ -198,13 +199,13 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 

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

	
 
	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE);
 
	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(e->info.misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE);
 

	
 
	uint num_vehicles = 1 + CountArticulatedParts(p1, false);
 

	
 
	/* Allow for the front and the articulated parts */
 
	if (!Vehicle::CanAllocateItem(num_vehicles)) {
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 
@@ -214,13 +215,13 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
	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);
 
		const RoadVehicleInfo *rvi = &e->u.road;
 

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

	
 
@@ -267,13 +268,13 @@ CommandCost CmdBuildRoadVeh(TileIndex ti
 
		v->build_year = _cur_year;
 

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

	
 
		v->roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
 
		v->roadtype = HasBit(e->info.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);
 

	
 
@@ -2026,13 +2027,13 @@ CommandCost CmdRefitRoadVeh(TileIndex ti
 
		 * [Refit] button near each wagon. */
 
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
 

	
 
		const Engine *e = Engine::Get(v->engine_type);
 
		if (!e->CanCarryCargo()) continue;
 

	
 
		if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
		if (HasBit(e->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
			/* Back up the cargo type */
 
			CargoID temp_cid = v->cargo_type;
 
			byte temp_subtype = v->cargo_subtype;
 
			v->cargo_type = new_cid;
 
			v->cargo_subtype = new_subtype;
 

	
src/ship_cmd.cpp
Show inline comments
 
@@ -52,19 +52,20 @@ static inline TrackBits GetTileShipTrack
 
{
 
	return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
 
}
 

	
 
static SpriteID GetShipIcon(EngineID engine)
 
{
 
	uint8 spritenum = ShipVehInfo(engine)->image_index;
 
	const Engine *e = Engine::Get(engine);
 
	uint8 spritenum = e->u.ship.image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
 
		if (sprite != 0) return sprite;
 

	
 
		spritenum = Engine::Get(engine)->original_image_index;
 
		spritenum = e->original_image_index;
 
	}
 

	
 
	return DIR_W + _ship_sprites[spritenum];
 
}
 

	
 
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
 
@@ -762,13 +763,13 @@ CommandCost CmdBuildShip(TileIndex tile,
 
		return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
 

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

	
 
		const ShipVehicleInfo *svi = ShipVehInfo(p1);
 
		const ShipVehicleInfo *svi = &e->u.ship;
 

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

	
 
		v->owner = _current_company;
 
		v->tile = tile;
 
@@ -916,17 +917,19 @@ CommandCost CmdRefitShip(TileIndex tile,
 
	Ship *v = Ship::GetIfValid(p1);
 

	
 
	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
 
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
 

	
 
	const Engine *e = Engine::Get(v->engine_type);
 

	
 
	/* Check cargo */
 
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
 

	
 
	/* Check the refit capacity callback */
 
	if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
	if (HasBit(e->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
		/* Back up the existing cargo type */
 
		CargoID temp_cid = v->cargo_type;
 
		byte temp_subtype = v->cargo_subtype;
 
		v->cargo_type = new_cid;
 
		v->cargo_subtype = new_subtype;
 

	
 
@@ -935,13 +938,13 @@ CommandCost CmdRefitShip(TileIndex tile,
 
		/* Restore the cargo type */
 
		v->cargo_type = temp_cid;
 
		v->cargo_subtype = temp_subtype;
 
	}
 

	
 
	if (capacity == CALLBACK_FAILED) {
 
		capacity = GetVehicleProperty(v, PROP_SHIP_CARGO_CAPACITY, ShipVehInfo(v->engine_type)->capacity);
 
		capacity = GetVehicleProperty(v, PROP_SHIP_CARGO_CAPACITY, e->u.ship.capacity);
 
	}
 
	_returned_refit_capacity = capacity;
 

	
 
	if (new_cid != v->cargo_type) {
 
		cost = GetRefitCost(v->engine_type);
 
	}
src/train_cmd.cpp
Show inline comments
 
@@ -254,13 +254,13 @@ void TrainConsistChanged(Train *v, bool 
 
	}
 

	
 
	for (Train *u = v; u != NULL; u = u->Next()) {
 
		const Engine *e_u = Engine::Get(u->engine_type);
 
		const RailVehicleInfo *rvi_u = &e_u->u.rail;
 

	
 
		if (!HasBit(EngInfo(u->engine_type)->misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
 
		if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
 

	
 
		/* Cache wagon override sprite group. NULL is returned if there is none */
 
		u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->tcache.first_engine);
 

	
 
		/* Reset colour map */
 
		u->colourmap = PAL_NONE;
 
@@ -278,13 +278,13 @@ void TrainConsistChanged(Train *v, bool 
 
				/* Diesel fumes and sparks come from the centre */
 
				u->tcache.cached_vis_effect = 8;
 
			}
 
		}
 

	
 
		/* Check powered wagon / visual effect callback */
 
		if (HasBit(EngInfo(u->engine_type)->callback_mask, CBM_TRAIN_WAGON_POWER)) {
 
		if (HasBit(e_u->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
 
			uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
 

	
 
			if (callback != CALLBACK_FAILED) u->tcache.cached_vis_effect = GB(callback, 0, 8);
 
		}
 

	
 
		if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
 
@@ -320,13 +320,13 @@ void TrainConsistChanged(Train *v, bool 
 
			/* Set cargo capacity if we've not been refitted */
 
			u->cargo_cap = GetVehicleProperty(u, PROP_TRAIN_CARGO_CAPACITY, rvi_u->capacity);
 
		}
 

	
 
		/* check the vehicle length (callback) */
 
		uint16 veh_len = CALLBACK_FAILED;
 
		if (HasBit(EngInfo(u->engine_type)->callback_mask, CBM_VEHICLE_LENGTH)) {
 
		if (HasBit(e_u->info.callback_mask, CBM_VEHICLE_LENGTH)) {
 
			veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
 
		}
 
		if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
 
		veh_len = 8 - Clamp(veh_len, 0, 7);
 

	
 
		/* verify length hasn't changed */
 
@@ -834,25 +834,25 @@ static void AddRearEngineToMultiheadedTr
 
CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	/* Check if the engine-type is valid (for the company) */
 
	if (!IsEngineBuildable(p1, VEH_TRAIN, _current_company)) return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE);
 

	
 
	const Engine *e = Engine::Get(p1);
 
	const RailVehicleInfo *rvi = &e->u.rail;
 
	CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
 

	
 
	/* Engines with CT_INVALID should not be available */
 
	if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
 

	
 
	if (flags & DC_QUERY_COST) return value;
 

	
 
	/* Check if the train is actually being built in a depot belonging
 
	 * to the company. Doesn't matter if only the cost is queried */
 
	if (!IsRailDepotTile(tile)) return CMD_ERROR;
 
	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
 

	
 
	const RailVehicleInfo *rvi = RailVehInfo(p1);
 
	if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags);
 

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

	
 
@@ -2118,13 +2118,13 @@ CommandCost CmdRefitRailVehicle(TileInde
 
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
 

	
 
		const Engine *e = Engine::Get(v->engine_type);
 
		if (e->CanCarryCargo()) {
 
			uint16 amount = CALLBACK_FAILED;
 

	
 
			if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
			if (HasBit(e->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY)) {
 
				/* Back up the vehicle's cargo type */
 
				CargoID temp_cid = v->cargo_type;
 
				byte temp_subtype = v->cargo_subtype;
 
				v->cargo_type = new_cid;
 
				v->cargo_subtype = new_subtype;
 
				/* Check the refit capacity callback */
src/vehicle.cpp
Show inline comments
 
@@ -698,13 +698,14 @@ bool CanRefitTo(EngineID engine_type, Ca
 
 * @return Price for refitting
 
 */
 
CommandCost GetRefitCost(EngineID engine_type)
 
{
 
	Money base_cost;
 
	ExpensesType expense_type;
 
	switch (Engine::Get(engine_type)->type) {
 
	const Engine *e = Engine::Get(engine_type);
 
	switch (e->type) {
 
		case VEH_SHIP:
 
			base_cost = _price.ship_base;
 
			expense_type = EXPENSES_SHIP_RUN;
 
			break;
 

	
 
		case VEH_ROAD:
 
@@ -715,20 +716,20 @@ CommandCost GetRefitCost(EngineID engine
 
		case VEH_AIRCRAFT:
 
			base_cost = _price.aircraft_base;
 
			expense_type = EXPENSES_AIRCRAFT_RUN;
 
			break;
 

	
 
		case VEH_TRAIN:
 
			base_cost = 2 * ((RailVehInfo(engine_type)->railveh_type == RAILVEH_WAGON) ?
 
			base_cost = 2 * ((e->u.rail.railveh_type == RAILVEH_WAGON) ?
 
							 _price.build_railwagon : _price.build_railvehicle);
 
			expense_type = EXPENSES_TRAIN_RUN;
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
	return CommandCost(expense_type, (EngInfo(engine_type)->refit_cost * base_cost) >> 10);
 
	return CommandCost(expense_type, (e->info.refit_cost * base_cost) >> 10);
 
}
 

	
 
static void DoDrawVehicle(const Vehicle *v)
 
{
 
	SpriteID image = v->cur_image;
 
	SpriteID pal = PAL_NONE;
 
@@ -1245,12 +1246,20 @@ bool CanBuildVehicleInfrastructure(Vehic
 
	}
 

	
 
	return false;
 
}
 

	
 

	
 
/**
 
 * Determines the livery for a vehicle.
 
 * @param engine_type EngineID of the vehicle
 
 * @param company Owner of the vehicle
 
 * @param parent_engine_type EngineID of the front vehicle. INVALID_VEHICLE if vehicle is at front itself.
 
 * @param v the vehicle. NULL if in purchase list etc.
 
 * @return livery to use
 
 */
 
const Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v)
 
{
 
	const Company *c = Company::Get(company);
 
	LiveryScheme scheme = LS_DEFAULT;
 
	CargoID cargo_type = v == NULL ? (CargoID)CT_INVALID : v->cargo_type;
 

	
 
@@ -1259,25 +1268,23 @@ const Livery *GetEngineLivery(EngineID e
 
	if (c->livery[LS_DEFAULT].in_use && (_settings_client.gui.liveries == 2 || (_settings_client.gui.liveries == 1 && company == _local_company))) {
 
		/* Determine the livery scheme to use */
 
		const Engine *e = Engine::Get(engine_type);
 
		switch (e->type) {
 
			default: NOT_REACHED();
 
			case VEH_TRAIN: {
 
				const RailVehicleInfo *rvi = RailVehInfo(engine_type);
 
				if (v != NULL && parent_engine_type != INVALID_ENGINE && (UsesWagonOverride(v) || (Train::From(v)->IsArticulatedPart() && rvi->railveh_type != RAILVEH_WAGON))) {
 
				if (v != NULL && parent_engine_type != INVALID_ENGINE && (UsesWagonOverride(v) || (Train::From(v)->IsArticulatedPart() && e->u.rail.railveh_type != RAILVEH_WAGON))) {
 
					/* Wagonoverrides use the coloir scheme of the front engine.
 
					 * Articulated parts use the colour scheme of the first part. (Not supported for articulated wagons) */
 
					engine_type = parent_engine_type;
 
					e = Engine::Get(engine_type);
 
					rvi = RailVehInfo(engine_type);
 
					/* Note: Luckily cargo_type is not needed for engines */
 
				}
 

	
 
				if (cargo_type == CT_INVALID) cargo_type = e->GetDefaultCargoType();
 
				if (cargo_type == CT_INVALID) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo
 
				if (rvi->railveh_type == RAILVEH_WAGON) {
 
				if (e->u.rail.railveh_type == RAILVEH_WAGON) {
 
					if (!CargoSpec::Get(cargo_type)->is_freight) {
 
						if (parent_engine_type == INVALID_ENGINE) {
 
							scheme = LS_PASSENGER_WAGON_STEAM;
 
						} else {
 
							switch (RailVehInfo(parent_engine_type)->engclass) {
 
								default: NOT_REACHED();
 
@@ -1289,15 +1296,15 @@ const Livery *GetEngineLivery(EngineID e
 
							}
 
						}
 
					} else {
 
						scheme = LS_FREIGHT_WAGON;
 
					}
 
				} else {
 
					bool is_mu = HasBit(EngInfo(engine_type)->misc_flags, EF_RAIL_IS_MU);
 
					bool is_mu = HasBit(e->info.misc_flags, EF_RAIL_IS_MU);
 

	
 
					switch (rvi->engclass) {
 
					switch (e->u.rail.engclass) {
 
						default: NOT_REACHED();
 
						case EC_STEAM:    scheme = LS_STEAM; break;
 
						case EC_DIESEL:   scheme = is_mu ? LS_DMU : LS_DIESEL;   break;
 
						case EC_ELECTRIC: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break;
 
						case EC_MONORAIL: scheme = LS_MONORAIL; break;
 
						case EC_MAGLEV:   scheme = LS_MAGLEV; break;
 
@@ -1314,13 +1321,13 @@ const Livery *GetEngineLivery(EngineID e
 
					cargo_type = v->First()->cargo_type;
 
				}
 
				if (cargo_type == CT_INVALID) cargo_type = e->GetDefaultCargoType();
 
				if (cargo_type == CT_INVALID) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo
 

	
 
				/* Important: Use Tram Flag of front part. Luckily engine_type refers to the front part here. */
 
				if (HasBit(EngInfo(engine_type)->misc_flags, EF_ROAD_TRAM)) {
 
				if (HasBit(e->info.misc_flags, EF_ROAD_TRAM)) {
 
					/* Tram */
 
					scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM;
 
				} else {
 
					/* Bus or truck */
 
					scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_BUS : LS_TRUCK;
 
				}
 
@@ -1356,14 +1363,16 @@ static SpriteID GetEngineColourMap(Engin
 
{
 
	SpriteID map = (v != NULL) ? v->colourmap : PAL_NONE;
 

	
 
	/* Return cached value if any */
 
	if (map != PAL_NONE) return map;
 

	
 
	const Engine *e = Engine::Get(engine_type);
 

	
 
	/* Check if we should use the colour map callback */
 
	if (HasBit(EngInfo(engine_type)->callback_mask, CBM_VEHICLE_COLOUR_REMAP)) {
 
	if (HasBit(e->info.callback_mask, CBM_VEHICLE_COLOUR_REMAP)) {
 
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_COLOUR_MAPPING, 0, 0, engine_type, v);
 
		/* A return value of 0xC000 is stated to "use the default two-colour
 
		 * maps" which happens to be the failure action too... */
 
		if (callback != CALLBACK_FAILED && callback != 0xC000) {
 
			map = GB(callback, 0, 14);
 
			/* If bit 14 is set, then the company colours are applied to the
 
@@ -1373,13 +1382,13 @@ static SpriteID GetEngineColourMap(Engin
 
				if (v != NULL) const_cast<Vehicle *>(v)->colourmap = map;
 
				return map;
 
			}
 
		}
 
	}
 

	
 
	bool twocc = HasBit(EngInfo(engine_type)->misc_flags, EF_USES_2CC);
 
	bool twocc = HasBit(e->info.misc_flags, EF_USES_2CC);
 

	
 
	if (map == PAL_NONE) map = twocc ? (SpriteID)SPR_2CCMAP_BASE : (SpriteID)PALETTE_RECOLOUR_START;
 

	
 
	/* Spectator has news shown too, but has invalid company ID - as well as dedicated server */
 
	if (!Company::IsValidID(company)) return map;
 

	
src/vehicle_gui.cpp
Show inline comments
 
@@ -158,14 +158,15 @@ static RefitList *BuildRefitList(const V
 
	RefitList *list = CallocT<RefitList>(1);
 
	Vehicle *u = const_cast<Vehicle *>(v);
 
	uint num_lines = 0;
 
	uint i;
 

	
 
	do {
 
		uint32 cmask = EngInfo(u->engine_type)->refit_mask;
 
		byte callback_mask = EngInfo(u->engine_type)->callback_mask;
 
		const Engine *e = Engine::Get(u->engine_type);
 
		uint32 cmask = e->info.refit_mask;
 
		byte callback_mask = e->info.callback_mask;
 

	
 
		/* Skip this engine if it has no capacity */
 
		if (u->cargo_cap == 0) continue;
 

	
 
		/* Loop through all cargos in the refit mask */
 
		for (CargoID cid = 0; cid < NUM_CARGO && num_lines < max_lines; cid++) {
0 comments (0 inline, 0 general)