Changeset - r6773:93083dcf60ec
[Not reviewed]
master
0 10 0
maedhros - 17 years ago 2007-06-01 12:03:10
maedhros@openttd.org
(svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.
10 files changed with 31 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/aircraft.h
Show inline comments
 
@@ -135,6 +135,7 @@ struct Aircraft : public Vehicle {
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_AIRCRAFT_INC : EXPENSES_AIRCRAFT_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_AIRCRAFT_LIST; }
 
	bool IsPrimaryVehicle() const { return IsNormalAircraft(this); }
 
};
 

	
 
#endif /* AIRCRAFT_H */
src/depot_gui.cpp
Show inline comments
 
@@ -488,10 +488,10 @@ static void HandleCloneVehClick(const Ve
 

	
 
	if (v == NULL) return;
 

	
 
	if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
 
	if (v->HasFront() && !v->IsPrimaryVehicle()) {
 
		v = GetFirstVehicleInChain(v);
 
		/* Do nothing when clicking on a train in depot with no loc attached */
 
		if (!IsFrontEngine(v)) return;
 
		if (v->type == VEH_TRAIN && !IsFrontEngine(v)) return;
 
	}
 

	
 
	switch (v->type) {
src/economy.cpp
Show inline comments
 
@@ -117,10 +117,7 @@ int UpdateCompanyRatingAndValue(Player *
 

	
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->owner != owner) continue;
 
			if ((v->type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					 v->type == VEH_ROAD ||
 
					(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
 
					 v->type == VEH_SHIP) {
 
			if (IsPlayerBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
 
				num++;
 
				if (v->age > 730) {
 
					/* Find the vehicle with the lowest amount of profit */
src/group_cmd.cpp
Show inline comments
 
@@ -211,7 +211,7 @@ int32 CmdAddVehicleGroup(TileIndex tile,
 
	}
 

	
 
	Vehicle *v = GetVehicle(p2);
 
	if (v->owner != _current_player || (v->type == VEH_TRAIN && !IsFrontEngine(v))) return CMD_ERROR;
 
	if (v->owner != _current_player || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DecreaseGroupNumVehicle(v->group_id);
 
@@ -254,14 +254,11 @@ int32 CmdAddSharedVehicleGroup(TileIndex
 
		Vehicle *v;
 
		VehicleType type = (VehicleType)p2;
 
		GroupID id_g = p1;
 
		uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0;
 

	
 
		/* Find the first front engine which belong to the group id_g
 
		 * then add all shared vehicles of this front engine to the group id_g */
 
		FOR_ALL_VEHICLES(v) {
 
			if ((v->type == type) && (
 
					(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					(type != VEH_TRAIN && v->subtype <= subtype))) {
 
			if (v->type == type && v->IsPrimaryVehicle()) {
 
				if (v->group_id != id_g) continue;
 

	
 
				/* For each shared vehicles add it to the group */
 
@@ -295,14 +292,11 @@ int32 CmdRemoveAllVehiclesGroup(TileInde
 

	
 
	if (flags & DC_EXEC) {
 
		GroupID old_g = p1;
 
		uint subtype = (type == VEH_AIRCRAFT) ? AIR_AIRCRAFT : 0;
 
		Vehicle *v;
 

	
 
		/* Find each Vehicle that belongs to the group old_g and add it to the default group */
 
		FOR_ALL_VEHICLES(v) {
 
			if ((v->type == type) && (
 
					(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					(type != VEH_TRAIN && v->subtype <= subtype))) {
 
			if (v->type == type && v->IsPrimaryVehicle()) {
 
				if (v->group_id != old_g) continue;
 

	
 
				/* Add The Vehicle to the default group */
 
@@ -348,7 +342,7 @@ int32 CmdSetGroupReplaceProtection(TileI
 
 */
 
void RemoveVehicleFromGroup(const Vehicle *v)
 
{
 
	if (!IsValidVehicle(v) || v->type != VEH_TRAIN || !IsFrontEngine(v)) return;
 
	if (!IsValidVehicle(v) || !(v->HasFront() && v->IsPrimaryVehicle())) return;
 

	
 
	if (!IsDefaultGroupID(v->group_id)) DecreaseGroupNumVehicle(v->group_id);
 
}
src/order_gui.cpp
Show inline comments
 
@@ -320,9 +320,9 @@ static bool HandleOrderVehClick(const Ve
 
{
 
	if (u->type != v->type) return false;
 

	
 
	if (u->type == VEH_TRAIN && !IsFrontEngine(u)) {
 
	if (u->HasFront() && !u->IsPrimaryVehicle()) {
 
		u = GetFirstVehicleInChain(u);
 
		if (!IsFrontEngine(u)) return false;
 
		if (!u->IsPrimaryVehicle()) return false;
 
	}
 

	
 
	// v is vehicle getting orders. Only copy/clone orders if vehicle doesn't have any orders yet
src/roadveh.h
Show inline comments
 
@@ -43,6 +43,7 @@ struct RoadVehicle : public Vehicle {
 
	void UpdateDeltaXY(Direction direction);
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_ROADVEH_LIST; }
 
	bool IsPrimaryVehicle() const { return true; }
 
};
 

	
 
#endif /* ROADVEH_H */
src/ship.h
Show inline comments
 
@@ -45,6 +45,7 @@ struct Ship: public Vehicle {
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_SHIP_INC : EXPENSES_SHIP_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_SHIPS_LIST; }
 
	void PlayLeaveStationSound() const;
 
	bool IsPrimaryVehicle() const { return true; }
 
};
 

	
 
#endif /* SHIP_H */
src/train.h
Show inline comments
 
@@ -270,6 +270,8 @@ struct Train : public Vehicle {
 
	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
 
	WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; }
 
	void PlayLeaveStationSound() const;
 
	bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
 
	bool HasFront() const { return true; }
 
};
 

	
 
#endif /* TRAIN_H */
src/vehicle.cpp
Show inline comments
 
@@ -590,7 +590,7 @@ void DestroyVehicle(Vehicle *v)
 
		if (v->owner == _local_player) InvalidateAutoreplaceWindow(v->engine_type);
 

	
 
		if (IsValidGroupID(v->group_id)) GetGroup(v->group_id)->num_engines[v->engine_type]--;
 
		if (v->type != VEH_TRAIN || IsFrontEngine(v)) DecreaseGroupNumVehicle(v->group_id);
 
		if (v->IsPrimaryVehicle()) DecreaseGroupNumVehicle(v->group_id);
 
	}
 

	
 
	DeleteVehicleNews(v->index, INVALID_STRING_ID);
 
@@ -1997,16 +1997,13 @@ void BuildDepotVehicleList(VehicleType t
 
*/
 
uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
 
{
 
	const byte subtype = (type != VEH_AIRCRAFT) ? (byte)Train_Front : (byte)AIR_AIRCRAFT;
 
	uint n = 0;
 
	const Vehicle *v;
 

	
 
	switch (window_type) {
 
		case VLW_STATION_LIST: {
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == type && (
 
					(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					(type != VEH_TRAIN && v->subtype <= subtype))) {
 
				if (v->type == type && v->IsPrimaryVehicle()) {
 
					const Order *order;
 

	
 
					FOR_VEHICLE_ORDERS(v, order) {
 
@@ -2039,9 +2036,7 @@ uint GenerateVehicleSortList(const Vehic
 

	
 
		case VLW_STANDARD: {
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == type && v->owner == owner && (
 
					(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					(type != VEH_TRAIN && v->subtype <= subtype))) {
 
				if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
 
					/* TODO find a better estimate on the total number of vehicles for current player */
 
					if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles()/4);
 
					(*sort_list)[n++] = v;
 
@@ -2052,9 +2047,7 @@ uint GenerateVehicleSortList(const Vehic
 

	
 
		case VLW_DEPOT_LIST: {
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == type && (
 
					(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
					(type != VEH_TRAIN && v->subtype <= subtype))) {
 
				if (v->type == type && v->IsPrimaryVehicle()) {
 
					const Order *order;
 

	
 
					FOR_VEHICLE_ORDERS(v, order) {
 
@@ -2071,10 +2064,8 @@ uint GenerateVehicleSortList(const Vehic
 

	
 
 		case VLW_GROUP_LIST:
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == type && (
 
							(type == VEH_TRAIN && IsFrontEngine(v)) ||
 
							(type != VEH_TRAIN && v->subtype <= subtype)
 
						) && v->owner == owner && v->group_id == index) {
 
				if (v->type == type && v->IsPrimaryVehicle() &&
 
						v->owner == owner && v->group_id == index) {
 
					if (n == *length_of_array) ExtendVehicleListSize(sort_list, length_of_array, GetNumVehicles() / 4);
 

	
 
					(*sort_list)[n++] = v;
src/vehicle.h
Show inline comments
 
@@ -399,6 +399,17 @@ struct Vehicle {
 
	 * Play the sound associated with leaving the station
 
	 */
 
	virtual void PlayLeaveStationSound() const {}
 

	
 
	/**
 
	 * Whether this is the primary vehicle in the chain.
 
	 */
 
	virtual bool IsPrimaryVehicle() const { return false; }
 

	
 
	/**
 
	 * Whether this vehicle understands the concept of a front engine, so
 
	 * basically, if GetFirstVehicleInChain() can be called for it.
 
	 */
 
	virtual bool HasFront() const { return false; }
 
};
 

	
 
/**
0 comments (0 inline, 0 general)