(svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.
authormaedhros
Fri, 01 Jun 2007 12:03:10 +0000
changeset 7269 c7f39d91255e
parent 7268 9c77f6baef3b
child 7270 f38724fb558a
(svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.
src/aircraft.h
src/depot_gui.cpp
src/economy.cpp
src/group_cmd.cpp
src/order_gui.cpp
src/roadveh.h
src/ship.h
src/train.h
src/vehicle.cpp
src/vehicle.h
--- a/src/aircraft.h	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/aircraft.h	Fri Jun 01 12:03:10 2007 +0000
@@ -135,6 +135,7 @@
 	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 */
--- a/src/depot_gui.cpp	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/depot_gui.cpp	Fri Jun 01 12:03:10 2007 +0000
@@ -488,10 +488,10 @@
 
 	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) {
--- a/src/economy.cpp	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/economy.cpp	Fri Jun 01 12:03:10 2007 +0000
@@ -117,10 +117,7 @@
 
 		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 */
--- a/src/group_cmd.cpp	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/group_cmd.cpp	Fri Jun 01 12:03:10 2007 +0000
@@ -211,7 +211,7 @@
 	}
 
 	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 @@
 		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 @@
 
 	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 @@
  */
 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);
 }
--- a/src/order_gui.cpp	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/order_gui.cpp	Fri Jun 01 12:03:10 2007 +0000
@@ -320,9 +320,9 @@
 {
 	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
--- a/src/roadveh.h	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/roadveh.h	Fri Jun 01 12:03:10 2007 +0000
@@ -43,6 +43,7 @@
 	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 */
--- a/src/ship.h	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/ship.h	Fri Jun 01 12:03:10 2007 +0000
@@ -45,6 +45,7 @@
 	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 */
--- a/src/train.h	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/train.h	Fri Jun 01 12:03:10 2007 +0000
@@ -270,6 +270,8 @@
 	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 */
--- a/src/vehicle.cpp	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/vehicle.cpp	Fri Jun 01 12:03:10 2007 +0000
@@ -590,7 +590,7 @@
 		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 @@
 */
 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 @@
 
 		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 @@
 
 		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 @@
 
  		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;
--- a/src/vehicle.h	Fri Jun 01 11:41:02 2007 +0000
+++ b/src/vehicle.h	Fri Jun 01 12:03:10 2007 +0000
@@ -399,6 +399,17 @@
 	 * 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; }
 };
 
 /**