Changeset - r17175:412ca7ec4689
[Not reviewed]
master
0 9 0
terkhen - 14 years ago 2011-01-29 17:30:25
terkhen@openttd.org
(svn r21924) -Codechange: Unify some parts of the articulated vehicle code.
-Cleanup: Avoid conversions to Train and RoadVehicle that are no longer required.
9 files changed with 26 insertions and 56 deletions:
0 comments (0 inline, 0 general)
src/articulated_vehicles.cpp
Show inline comments
 
@@ -218,25 +218,13 @@ bool IsArticulatedVehicleCarryingDiffere
 
			if (first_cargo != v->cargo_type) {
 
				if (cargo_type != NULL) *cargo_type = CT_INVALID;
 
				return true;
 
			}
 
		}
 

	
 
		switch (v->type) {
 
			case VEH_TRAIN:
 
				v = Train::From(v)->HasArticulatedPart() ? Train::From(v)->GetNextArticulatedPart() : NULL;
 
				break;
 

	
 
			case VEH_ROAD:
 
				v = RoadVehicle::From(v)->HasArticulatedPart() ? v->Next() : NULL;
 
				break;
 

	
 
			default:
 
				v = NULL;
 
				break;
 
		}
 
		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
 
	} while (v != NULL);
 

	
 
	if (cargo_type != NULL) *cargo_type = first_cargo;
 
	return false;
 
}
 

	
 
@@ -265,25 +253,13 @@ void CheckConsistencyOfArticulatedVehicl
 
		real_refit_union |= refit_mask;
 
		if (refit_mask != 0) real_refit_intersection &= refit_mask;
 

	
 
		assert(v->cargo_type < NUM_CARGO);
 
		real_default_capacity[v->cargo_type] += v->cargo_cap;
 

	
 
		switch (v->type) {
 
			case VEH_TRAIN:
 
				v = Train::From(v)->HasArticulatedPart() ? Train::From(v)->GetNextArticulatedPart() : NULL;
 
				break;
 

	
 
			case VEH_ROAD:
 
				v = RoadVehicle::From(v)->HasArticulatedPart() ? v->Next() : NULL;
 
				break;
 

	
 
			default:
 
				v = NULL;
 
				break;
 
		}
 
		v = v->HasArticulatedPart() ? v->GetNextArticulatedPart() : NULL;
 
	} while (v != NULL);
 

	
 
	/* Check whether the vehicle carries more cargos than expected */
 
	bool carries_more = false;
 
	for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
 
		if (real_default_capacity[cid] != 0 && purchase_default_capacity[cid] == 0) {
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -98,24 +98,24 @@ bool CheckAutoreplaceValidity(EngineID f
 
 */
 
static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
 
{
 
	assert(!part_of_chain || new_head->IsPrimaryVehicle());
 
	/* Loop through source parts */
 
	for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
 
		if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !Train::From(src)->IsArticulatedPart()) {
 
		if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !src->IsArticulatedPart()) {
 
			/* Skip vehicles, which do not belong to old_veh */
 
			src = Train::From(src)->GetLastEnginePart();
 
			src = src->GetLastEnginePart();
 
			continue;
 
		}
 
		if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
 

	
 
		/* Find free space in the new chain */
 
		for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
 
			if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !Train::From(dest)->IsArticulatedPart()) {
 
			if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !dest->IsArticulatedPart()) {
 
				/* Skip vehicles, which do not belong to new_head */
 
				dest = Train::From(dest)->GetLastEnginePart();
 
				dest = dest->GetLastEnginePart();
 
				continue;
 
			}
 
			if (dest->cargo_type != src->cargo_type) continue;
 

	
 
			uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
 
			if (amount <= 0) continue;
 
@@ -215,13 +215,13 @@ static CargoID GetNewCargoTypeForReplace
 
 * @param v The vehicle to find a replacement for
 
 * @param c The vehicle's owner (it's faster to forward the pointer than refinding it)
 
 * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
 
 */
 
static EngineID GetNewEngineType(const Vehicle *v, const Company *c)
 
{
 
	assert(v->type != VEH_TRAIN || !Train::From(v)->IsArticulatedPart());
 
	assert(v->type != VEH_TRAIN || !v->IsArticulatedPart());
 

	
 
	if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
 
		/* we build the rear ends of multiheaded trains with the front ones */
 
		return INVALID_ENGINE;
 
	}
 

	
src/depot_gui.cpp
Show inline comments
 
@@ -794,13 +794,13 @@ struct DepotWindow : Window {
 
		for (const Vehicle *w = v; w != NULL; w = w->Next()) {
 
			if (w->cargo_cap > 0 && w->cargo_type < NUM_CARGO) {
 
				capacity[w->cargo_type] += w->cargo_cap;
 
				loaded  [w->cargo_type] += w->cargo.Count();
 
			}
 

	
 
			if (w->type == VEH_TRAIN && !Train::From(w)->HasArticulatedPart()) {
 
			if (w->type == VEH_TRAIN && !w->HasArticulatedPart()) {
 
				num++;
 
				if (!whole_chain) break;
 
			}
 
		}
 

	
 
		/* Build tooltipstring */
 
@@ -908,13 +908,13 @@ struct DepotWindow : Window {
 
						if (gdvp.wagon != NULL && gdvp.wagon->index == sel && _ctrl_pressed) {
 
							DoCommandP(Vehicle::Get(sel)->tile, Vehicle::Get(sel)->index, true,
 
									CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE));
 
						} else if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
 
							this->vehicle_over = INVALID_VEHICLE;
 
							TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
 
						} else if (gdvp.head != NULL && Train::From(gdvp.head)->IsFrontEngine()) {
 
						} else if (gdvp.head != NULL && gdvp.head->IsFrontEngine()) {
 
							ShowVehicleViewWindow(gdvp.head);
 
						}
 
					}
 
				} else if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, NULL) == MODE_DRAG_VEHICLE && v != NULL && sel == v->index) {
 
					ShowVehicleViewWindow(v);
 
				}
src/industry_cmd.cpp
Show inline comments
 
@@ -2295,13 +2295,13 @@ static int WhoCanServiceIndustry(Industr
 
		/* Is it worthwhile to try this vehicle? */
 
		if (v->owner != _local_company && result != 0) continue;
 

	
 
		/* Check whether it accepts the right kind of cargo */
 
		bool c_accepts = false;
 
		bool c_produces = false;
 
		if (v->type == VEH_TRAIN && Train::From(v)->IsFrontEngine()) {
 
		if (v->type == VEH_TRAIN && v->IsFrontEngine()) {
 
			for (const Vehicle *u = v; u != NULL; u = u->Next()) {
 
				CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
 
			}
 
		} else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
 
			CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
 
		} else {
src/roadveh_gui.cpp
Show inline comments
 
@@ -28,24 +28,22 @@
 
 * @param left  The left most coordinate to draw
 
 * @param right The right most coordinate to draw
 
 * @param y     The y coordinate
 
 */
 
void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
 
{
 
	const RoadVehicle *rv = RoadVehicle::From(v);
 

	
 
	uint y_offset = rv->HasArticulatedPart() ? 15 : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
 
	uint y_offset = v->HasArticulatedPart() ? 15 : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
 
	StringID str;
 
	Money feeder_share = 0;
 

	
 
	SetDParam(0, v->engine_type);
 
	SetDParam(1, v->build_year);
 
	SetDParam(2, v->value);
 
	DrawString(left, right, y + y_offset, STR_VEHICLE_INFO_BUILT_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
 

	
 
	if (rv->HasArticulatedPart()) {
 
	if (v->HasArticulatedPart()) {
 
		CargoArray max_cargo;
 
		StringID subtype_text[NUM_CARGO];
 
		char capacity[512];
 

	
 
		memset(subtype_text, 0, sizeof(subtype_text));
 

	
src/station_cmd.cpp
Show inline comments
 
@@ -2950,13 +2950,13 @@ static bool ClickTile_Station(TileIndex 
 

	
 
static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	if (v->type == VEH_TRAIN) {
 
		StationID station_id = GetStationIndex(tile);
 
		if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE;
 
		if (!IsRailStation(tile) || !Train::From(v)->IsFrontEngine()) return VETSB_CONTINUE;
 
		if (!IsRailStation(tile) || !v->IsFrontEngine()) return VETSB_CONTINUE;
 

	
 
		int station_ahead;
 
		int station_length;
 
		int stop = GetTrainStopLocation(station_id, tile, Train::From(v), &station_ahead, &station_length);
 

	
 
		/* Stop whenever that amount of station ahead + the distance from the
src/vehicle.cpp
Show inline comments
 
@@ -623,13 +623,13 @@ void CountCompanyVehicles(CompanyID cid,
 
 */
 
bool Vehicle::IsEngineCountable() const
 
{
 
	switch (this->type) {
 
		case VEH_AIRCRAFT: return Aircraft::From(this)->IsNormalAircraft(); // don't count plane shadows and helicopter rotors
 
		case VEH_TRAIN:
 
			return !Train::From(this)->IsArticulatedPart() && // tenders and other articulated parts
 
			return !this->IsArticulatedPart() && // tenders and other articulated parts
 
					!Train::From(this)->IsRearDualheaded(); // rear parts of multiheaded engines
 
		case VEH_ROAD: return RoadVehicle::From(this)->IsFrontEngine();
 
		case VEH_SHIP: return true;
 
		default: return false; // Only count company buildable vehicles
 
	}
 
}
 
@@ -1535,13 +1535,13 @@ LiveryScheme GetEngineLiveryScheme(Engin
 
{
 
	CargoID cargo_type = v == NULL ? (CargoID)CT_INVALID : v->cargo_type;
 
	const Engine *e = Engine::Get(engine_type);
 
	switch (e->type) {
 
		default: NOT_REACHED();
 
		case VEH_TRAIN:
 
			if (v != NULL && parent_engine_type != INVALID_ENGINE && (UsesWagonOverride(v) || (Train::From(v)->IsArticulatedPart() && e->u.rail.railveh_type != RAILVEH_WAGON))) {
 
			if (v != NULL && parent_engine_type != INVALID_ENGINE && (UsesWagonOverride(v) || (v->IsArticulatedPart() && e->u.rail.railveh_type != RAILVEH_WAGON))) {
 
				/* Wagonoverrides use the colour 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);
 
				/* Note: Luckily cargo_type is not needed for engines */
 
			}
src/vehicle_cmd.cpp
Show inline comments
 
@@ -668,13 +668,13 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
	 * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
 
	 */
 

	
 
	CommandCost ret = CheckOwnership(v->owner);
 
	if (ret.Failed()) return ret;
 

	
 
	if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
 
	if (v->type == VEH_TRAIN && (!v->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR;
 

	
 
	/* check that we can allocate enough vehicles */
 
	if (!(flags & DC_EXEC)) {
 
		int veh_counter = 0;
 
		do {
 
			veh_counter++;
 
@@ -716,13 +716,13 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
			w = Vehicle::Get(_new_vehicle_id);
 

	
 
			if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION)) {
 
				SetBit(Train::From(w)->flags, VRF_REVERSE_DIRECTION);
 
			}
 

	
 
			if (v->type == VEH_TRAIN && !Train::From(v)->IsFrontEngine()) {
 
			if (v->type == VEH_TRAIN && !v->IsFrontEngine()) {
 
				/* this s a train car
 
				 * add this unit to the end of the train */
 
				CommandCost result = DoCommand(0, w->index | 1 << 20, w_rear->index, flags, CMD_MOVE_RAIL_VEHICLE);
 
				if (result.Failed()) {
 
					/* The train can't be joined to make the same consist as the original.
 
					 * Sell what we already made (clean up) and return an error.           */
 
@@ -734,13 +734,13 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
				/* this is a front engine or not a train. */
 
				w_front = w;
 
				w->service_interval = v->service_interval;
 
			}
 
			w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
 
		}
 
	} while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
 
	} while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
 

	
 
	if ((flags & DC_EXEC) && v_front->type == VEH_TRAIN) {
 
		/* for trains this needs to be the front engine due to the callback function */
 
		_new_vehicle_id = w_front->index;
 
	}
 

	
 
@@ -769,39 +769,35 @@ CommandCost CmdCloneVehicle(TileIndex ti
 
				byte subtype = GetBestFittingSubType(v, w);
 
				if (w->cargo_type != v->cargo_type || w->cargo_subtype != subtype) {
 
					CommandCost cost = DoCommand(0, w->index, v->cargo_type | 1U << 7 | (subtype << 8), flags, GetCmdRefitVeh(v));
 
					if (cost.Succeeded()) total_cost.AddCost(cost);
 
				}
 

	
 
				if (w->type == VEH_TRAIN && Train::From(w)->HasArticulatedPart()) {
 
					w = Train::From(w)->GetNextArticulatedPart();
 
				} else if (w->type == VEH_ROAD && RoadVehicle::From(w)->HasArticulatedPart()) {
 
					w = w->Next();
 
				if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
 
					w = w->GetNextArticulatedPart();
 
				} else {
 
					break;
 
				}
 
			} else {
 
				const Engine *e = Engine::Get(v->engine_type);
 
				CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
 

	
 
				if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
 
					total_cost.AddCost(GetRefitCost(v->engine_type));
 
				}
 
			}
 

	
 
			if (v->type == VEH_TRAIN && Train::From(v)->HasArticulatedPart()) {
 
				v = Train::From(v)->GetNextArticulatedPart();
 
			} else if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
 
				v = v->Next();
 
			if (w->IsGroundVehicle() && w->HasArticulatedPart()) {
 
				w = w->GetNextArticulatedPart();
 
			} else {
 
				break;
 
			}
 
		} while (v != NULL);
 

	
 
		if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = Train::From(w)->GetNextVehicle();
 
	} while (v->type == VEH_TRAIN && (v = Train::From(v)->GetNextVehicle()) != NULL);
 
		if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = w->GetNextVehicle();
 
	} while (v->type == VEH_TRAIN && (v = v->GetNextVehicle()) != NULL);
 

	
 
	if (flags & DC_EXEC) {
 
		/*
 
		 * Set the orders of the vehicle. Cannot do it earlier as we need
 
		 * the vehicle refitted before doing this, otherwise the moved
 
		 * cargo types might not match (passenger vs non-passenger)
src/vehicle_gui.cpp
Show inline comments
 
@@ -1782,13 +1782,13 @@ struct VehicleDetailsWindow : Window {
 
	 * @param v Road vehicle being shown.
 
	 * @return Desired height in pixels.
 
	 */
 
	uint GetRoadVehDetailsHeight(const Vehicle *v)
 
	{
 
		uint desired_height;
 
		if (RoadVehicle::From(v)->HasArticulatedPart()) {
 
		if (v->HasArticulatedPart()) {
 
			/* An articulated RV has its text drawn under the sprite instead of after it, hence 15 pixels extra. */
 
			desired_height = WD_FRAMERECT_TOP + 15 + 3 * FONT_HEIGHT_NORMAL + 2 + WD_FRAMERECT_BOTTOM;
 
			/* Add space for the cargo amount for each part. */
 
			for (const Vehicle *u = v; u != NULL; u = u->Next()) {
 
				if (u->cargo_cap != 0) desired_height += FONT_HEIGHT_NORMAL + 1;
 
			}
 
@@ -1959,13 +1959,13 @@ struct VehicleDetailsWindow : Window {
 
				uint sprite_width = max<uint>(GetSprite(v->GetImage(rtl ? DIR_E : DIR_W), ST_NORMAL)->width, 70U) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
 

	
 
				uint text_left  = r.left  + (rtl ? 0 : sprite_width);
 
				uint text_right = r.right - (rtl ? sprite_width : 0);
 

	
 
				/* Articulated road vehicles use a complete line. */
 
				if (v->type == VEH_ROAD && RoadVehicle::From(v)->HasArticulatedPart()) {
 
				if (v->type == VEH_ROAD && v->HasArticulatedPart()) {
 
					DrawVehicleImage(v, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, INVALID_VEHICLE, 0);
 
				} else {
 
					uint sprite_left  = rtl ? text_right : r.left;
 
					uint sprite_right = rtl ? r.right : text_left;
 

	
 
					DrawVehicleImage(v, sprite_left + WD_FRAMERECT_LEFT, sprite_right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, INVALID_VEHICLE, 0);
0 comments (0 inline, 0 general)