(svn r12617) -Codechange: add type safety to the Order's load and unload types.
authorrubidium
Mon, 07 Apr 2008 20:03:46 +0000
changeset 8855 6d841d5d4789
parent 8854 d3f0e0960fb9
child 8856 6b8adda843c9
(svn r12617) -Codechange: add type safety to the Order's load and unload types.
src/ai/default/default.cpp
src/ai/trolly/trolly.cpp
src/economy.cpp
src/industry_cmd.cpp
src/openttd.cpp
src/order_base.h
src/order_cmd.cpp
src/order_type.h
src/vehicle.cpp
--- a/src/ai/default/default.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/ai/default/default.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -2554,9 +2554,9 @@
 
 		order.MakeGoToStation(AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule));
 
-		if (!is_pass && i == 1) order.SetUnloadType(OFB_UNLOAD);
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.SetLoadType(OFB_FULL_LOAD);
+			order.SetLoadType(OLFB_FULL_LOAD);
 
 		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
@@ -3289,9 +3289,9 @@
 
 		order.MakeGoToStation(AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule));
 
-		if (!is_pass && i == 1) order.SetUnloadType(OFB_UNLOAD);
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.SetLoadType(OFB_FULL_LOAD);
+			order.SetLoadType(OLFB_FULL_LOAD);
 
 		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
@@ -3568,9 +3568,9 @@
 
 		order.MakeGoToStation(AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule));
 
-		if (!is_pass && i == 1) order.SetUnloadType(OFB_UNLOAD);
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.SetLoadType(OFB_FULL_LOAD);
+			order.SetLoadType(OLFB_FULL_LOAD);
 
 		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
--- a/src/ai/trolly/trolly.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/ai/trolly/trolly.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -1190,14 +1190,12 @@
 
 	idx = 0;
 	order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile));
-	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver)
-		order.SetLoadType(OFB_FULL_LOAD);
+	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver) order.SetLoadType(OLFB_FULL_LOAD);
 	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
 	idx = 0;
 	order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile));
-	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver)
-		order.SetLoadType(OFB_FULL_LOAD);
+	if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver) order.SetLoadType(OLFB_FULL_LOAD);
 	AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 
 	// Start the engines!
--- a/src/economy.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/economy.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -1489,7 +1489,7 @@
 			if (!cp->paid_for &&
 					cp->source != last_visited &&
 					HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) &&
-					(front_v->current_order.GetUnloadType() & OFB_TRANSFER) == 0) {
+					(front_v->current_order.GetUnloadType() & OUFB_TRANSFER) == 0) {
 				/* Deliver goods to the station */
 				st->time_since_unload = 0;
 
@@ -1502,8 +1502,8 @@
 				result |= 1;
 
 				SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
-			} else if (front_v->current_order.GetUnloadType() & (OFB_UNLOAD | OFB_TRANSFER)) {
-				if (!cp->paid_for && (front_v->current_order.GetUnloadType() & OFB_TRANSFER) != 0) {
+			} else if (front_v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) {
+				if (!cp->paid_for && (front_v->current_order.GetUnloadType() & OUFB_TRANSFER) != 0) {
 					Money profit = GetTransportedGoodsIncome(
 						cp->count,
 						/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
@@ -1556,7 +1556,7 @@
 
 	/* We have not waited enough time till the next round of loading/unloading */
 	if (--v->load_unload_time_rem != 0) {
-		if (_patches.improved_load && HasBit(v->current_order.GetLoadType(), OF_FULL_LOAD)) {
+		if (_patches.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
 			/* 'Reserve' this cargo for this vehicle, because we were first. */
 			for (; v != NULL; v = v->Next()) {
 				if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
@@ -1604,12 +1604,12 @@
 			uint amount_unloaded = _patches.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
 			bool remaining; // Are there cargo entities in this vehicle that can still be unloaded here?
 
-			if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OFB_TRANSFER)) {
+			if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OUFB_TRANSFER)) {
 				/* The cargo has reached it's final destination, the packets may now be destroyed */
 				remaining = v->cargo.MoveTo(NULL, amount_unloaded, CargoList::MTA_FINAL_DELIVERY, last_visited);
 
 				result |= 1;
-			} else if (u->current_order.GetUnloadType() & (OFB_UNLOAD | OFB_TRANSFER)) {
+			} else if (u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) {
 				remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded);
 				SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
 
@@ -1638,7 +1638,7 @@
 		}
 
 		/* Do not pick up goods that we unloaded */
-		if (u->current_order.GetUnloadType() & OFB_UNLOAD) continue;
+		if (u->current_order.GetUnloadType() & OUFB_UNLOAD) continue;
 
 		/* update stats */
 		int t;
@@ -1710,7 +1710,7 @@
 	 * all wagons at the same time instead of using the same 'improved'
 	 * loading algorithm for the wagons (only fill wagon when there is
 	 * enough to fill the previous wagons) */
-	if (_patches.improved_load && HasBit(u->current_order.GetLoadType(), OF_FULL_LOAD)) {
+	if (_patches.improved_load && (u->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
 		/* Update left cargo */
 		for (v = u; v != NULL; v = v->Next()) {
 			if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
@@ -1729,7 +1729,7 @@
 		}
 	} else {
 		bool finished_loading = true;
-		if (HasBit(v->current_order.GetLoadType(), OF_FULL_LOAD)) {
+		if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) {
 			if (_patches.full_load_any) {
 				/* if the aircraft carries passengers and is NOT full, then
 				 * continue loading, no matter how much mail is in */
--- a/src/industry_cmd.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/industry_cmd.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -1968,13 +1968,13 @@
 		 */
 		const Order *o;
 		FOR_VEHICLE_ORDERS(v, o) {
-			if (o->IsType(OT_GOTO_STATION) && !HasBit(o->GetUnloadType(), OF_TRANSFER)) {
+			if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
 				/* Vehicle visits a station to load or unload */
 				Station *st = GetStation(o->GetDestination());
 				if (!st->IsValid()) continue;
 
 				/* Same cargo produced by industry is dropped here => not serviced by vehicle v */
-				if (HasBit(o->GetUnloadType(), OF_UNLOAD) && !c_accepts) break;
+				if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
 
 				if (stations.find(st) != stations.end()) {
 					if (v->owner == _local_player) return 2; // Player services industry
--- a/src/openttd.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/openttd.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -2306,8 +2306,8 @@
 		Order *order;
 		FOR_ALL_ORDERS(order) {
 			if (order->IsType(OT_GOTO_STATION) && GetStation(order->GetDestination())->IsBuoy()) {
-				order->SetLoadType(0);
-				order->SetUnloadType(0);
+				order->SetLoadType(OLF_LOAD_IF_POSSIBLE);
+				order->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
 			}
 		}
 
--- a/src/order_base.h	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/order_base.h	Mon Apr 07 20:03:46 2008 +0000
@@ -161,9 +161,9 @@
 	void SetRefit(CargoID cargo, byte subtype = 0);
 
 	/** How must the consist be loaded? */
-	inline byte GetLoadType() const { return this->flags & OFB_FULL_LOAD; }
+	inline OrderLoadFlags GetLoadType() const { return (OrderLoadFlags)(this->flags & OLFB_FULL_LOAD); }
 	/** How must the consist be unloaded? */
-	inline byte GetUnloadType() const { return GB(this->flags, 0, 2); }
+	inline OrderUnloadFlags GetUnloadType() const { return (OrderUnloadFlags)GB(this->flags, 0, 2); }
 	/** Where must we stop? */
 	OrderNonStopFlags GetNonStopType() const;
 	/** What caused us going to the depot? */
@@ -172,9 +172,9 @@
 	inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)this->flags; }
 
 	/** Set how the consist must be loaded. */
-	inline void SetLoadType(byte load_type) { SB(this->flags, 2, 1, !!load_type); }
+	inline void SetLoadType(OrderLoadFlags load_type) { SB(this->flags, 2, 1, !!load_type); }
 	/** Set how the consist must be unloaded. */
-	inline void SetUnloadType(byte unload_type) { SB(this->flags, 0, 2, unload_type); }
+	inline void SetUnloadType(OrderUnloadFlags unload_type) { SB(this->flags, 0, 2, unload_type); }
 	/** Set whether we must stop at stations or not. */
 	inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->flags, 3, 1, !!non_stop_type); }
 	/** Set the cause to go to the depot. */
--- a/src/order_cmd.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/order_cmd.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -322,11 +322,11 @@
 			 * non-stop orders (if any) are only valid for trains */
 			switch (new_order.GetLoadType() | new_order.GetUnloadType()) {
 				case 0:
-				case OFB_FULL_LOAD:
-				case OFB_FULL_LOAD | OFB_TRANSFER:
-				case OFB_UNLOAD:
-				case OFB_UNLOAD | OFB_TRANSFER:
-				case OFB_TRANSFER:
+				case OLFB_FULL_LOAD:
+				case OLFB_FULL_LOAD | OUFB_TRANSFER:
+				case OUFB_UNLOAD:
+				case OUFB_UNLOAD | OUFB_TRANSFER:
+				case OUFB_TRANSFER:
 					break;
 
 				default: return CMD_ERROR;
@@ -766,19 +766,19 @@
 				if (order->IsType(OT_GOTO_DEPOT)) {
 					order->SetDepotOrderType((OrderDepotTypeFlags)(order->GetDepotOrderType() ^ ODTFB_SERVICE));
 				} else {
-					order->SetLoadType(order->GetLoadType() ^ OFB_FULL_LOAD);
-					order->SetUnloadType(order->GetUnloadType() & ~OFB_UNLOAD);
+					order->SetLoadType((OrderLoadFlags)(order->GetLoadType() ^ OLFB_FULL_LOAD));
+					order->SetUnloadType((OrderUnloadFlags)(order->GetUnloadType() & ~OUFB_UNLOAD));
 				}
 				break;
 			case OF_UNLOAD:
-				order->SetUnloadType(order->GetUnloadType() ^ OFB_UNLOAD);
-				order->SetLoadType(0);
+				order->SetUnloadType((OrderUnloadFlags)(order->GetUnloadType() ^ OUFB_UNLOAD));
+				order->SetLoadType(OLF_LOAD_IF_POSSIBLE);
 				break;
 			case OF_NON_STOP:
 				order->SetNonStopType(order->GetNonStopType() == ONSF_STOP_EVERYWHERE ? ONSF_NO_STOP_AT_ANY_STATION : ONSF_STOP_EVERYWHERE);
 				break;
 			case OF_TRANSFER:
-				order->SetUnloadType(order->GetUnloadType() ^ OFB_TRANSFER);
+				order->SetUnloadType((OrderUnloadFlags)(order->GetUnloadType() ^ OUFB_TRANSFER));
 				break;
 			default: NOT_REACHED();
 		}
--- a/src/order_type.h	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/order_type.h	Mon Apr 07 20:03:46 2008 +0000
@@ -36,29 +36,31 @@
 typedef TinyEnumT<OrderType> OrderTypeByte;
 
 
-/* Order flags -- please use OF instead OF and use HASBIT/SETBIT/CLEARBIT */
+/**
+ * Flags related to the unloading order.
+ */
+enum OrderUnloadFlags {
+	OUF_UNLOAD_IF_POSSIBLE = 0,      ///< Unload all cargo that the station accepts.
+	OUFB_TRANSFER          = 1 << 0, ///< Transfer all cargo onto the platform.
+	OUFB_UNLOAD            = 1 << 1, ///< Force unloading all cargo onto the platform, possibly not getting paid.
+};
 
-/** Order flag masks - these are for direct bit operations */
-enum OrderFlagMasks {
-	//Flags for stations:
-	/** vehicle will transfer cargo (i. e. not deliver to nearby industry/town even if accepted there) */
-	OFB_TRANSFER           = 0x1,
-	/** If OFB_TRANSFER is not set, drop any cargo loaded. If accepted, deliver, otherwise cargo remains at the station.
-      * No new cargo is loaded onto the vehicle whatsoever */
-	OFB_UNLOAD             = 0x2,
-	/** Wait for full load of all vehicles, or of at least one cargo type, depending on patch setting
-	  * @todo make this two different flags */
-	OFB_FULL_LOAD          = 0x4,
+/**
+ * Flags related to the loading order.
+ */
+enum OrderLoadFlags {
+	OLF_LOAD_IF_POSSIBLE = 0,      ///< Load as long as there is cargo that fits in the train.
+	OLFB_FULL_LOAD       = 1 << 2, ///< Full load the complete (or a single cargo) of the consist.
 };
 
 /**
  * Non-stop order flags.
  */
 enum OrderNonStopFlags {
-	ONSF_STOP_EVERYWHERE                  = 0,
-	ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS = 1,
-	ONSF_NO_STOP_AT_DESTINATION_STATION   = 2,
-	ONSF_NO_STOP_AT_ANY_STATION           = 3
+	ONSF_STOP_EVERYWHERE                  = 0, ///< The vehicle will stop at any station it passes and the destination.
+	ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS = 1, ///< The vehicle will not stop at any stations it passes except the destination.
+	ONSF_NO_STOP_AT_DESTINATION_STATION   = 2, ///< The vehicle will stop at any station it passes except the destination.
+	ONSF_NO_STOP_AT_ANY_STATION           = 3, ///< The vehicle will not stop at any stations it passes including the destination.
 };
 
 /**
@@ -78,9 +80,8 @@
 	ODATFB_HALT          = 1 << 2, ///< Service the vehicle and then halt it.
 };
 
-/** Order flags bits - these are for the *BIT macros
- * for descrption of flags, see OrderFlagMasks
- * @see OrderFlagMasks
+/**
+ * Enumeration for the 'flag' to toggle in CmdModifyOrder.
  */
 enum {
 	OF_TRANSFER          = 0,
--- a/src/vehicle.cpp	Mon Apr 07 20:02:36 2008 +0000
+++ b/src/vehicle.cpp	Mon Apr 07 20:03:46 2008 +0000
@@ -2175,7 +2175,7 @@
 		max += v->cargo_cap;
 		if (v->cargo_cap != 0) {
 			unloading += HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) ? 1 : 0;
-			loading |= !HasBit(u->current_order.GetUnloadType(), OF_UNLOAD) && st->goods[v->cargo_type].days_since_pickup != 255;
+			loading |= !(u->current_order.GetUnloadType() & OUFB_UNLOAD) && st->goods[v->cargo_type].days_since_pickup != 255;
 			cars++;
 		}
 	}