src/order_cmd.cpp
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6397 b6aae958da82
child 9906 6f41b8713b65
--- a/src/order_cmd.cpp	Mon Mar 19 09:33:17 2007 +0000
+++ b/src/order_cmd.cpp	Mon Mar 19 12:38:16 2007 +0000
@@ -15,6 +15,7 @@
 #include "news.h"
 #include "saveload.h"
 #include "vehicle_gui.h"
+#include "cargotype.h"
 
 /**
  * Called if a new block is added to the order-pool
@@ -49,7 +50,7 @@
 
 	// Sanity check
 	// TTD stores invalid orders as OT_NOTHING with non-zero flags/station
-	if (order.type == OT_NOTHING && (order.flags != 0 || order.dest != 0)) {
+	if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
 		order.type = OT_DUMMY;
 		order.flags = 0;
 	}
@@ -109,14 +110,14 @@
  * @return Order* if a free space is found, else NULL.
  *
  */
-static Order *AllocateOrder(void)
+static Order *AllocateOrder()
 {
 	Order *order;
 
 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 	 * TODO - This is just a temporary stage, this will be removed. */
 	for (order = GetOrder(0); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) {
-		if (!IsValidOrder(order)) {
+		if (!order->IsValid()) {
 			OrderID index = order->index;
 
 			memset(order, 0, sizeof(*order));
@@ -213,23 +214,23 @@
 			}
 
 			switch (v->type) {
-				case VEH_Train:
+				case VEH_TRAIN:
 					if (!(st->facilities & FACIL_TRAIN)) return CMD_ERROR;
 					break;
 
-				case VEH_Road:
-					if (v->cargo_type == CT_PASSENGERS) {
+				case VEH_ROAD:
+					if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
 						if (!(st->facilities & FACIL_BUS_STOP)) return CMD_ERROR;
 					} else {
 						if (!(st->facilities & FACIL_TRUCK_STOP)) return CMD_ERROR;
 					}
 					break;
 
-				case VEH_Ship:
+				case VEH_SHIP:
 					if (!(st->facilities & FACIL_DOCK)) return CMD_ERROR;
 					break;
 
-				case VEH_Aircraft:
+				case VEH_AIRCRAFT:
 					if (!(st->facilities & FACIL_AIRPORT)) return CMD_ERROR;
 					break;
 
@@ -254,7 +255,7 @@
 				case OF_NON_STOP | OF_UNLOAD:
 				case OF_NON_STOP | OF_UNLOAD | OF_TRANSFER:
 				case OF_NON_STOP | OF_TRANSFER:
-					if (v->type != VEH_Train) return CMD_ERROR;
+					if (v->type != VEH_TRAIN) return CMD_ERROR;
 					break;
 
 				default: return CMD_ERROR;
@@ -263,7 +264,7 @@
 		}
 
 		case OT_GOTO_DEPOT: {
-			if (v->type == VEH_Aircraft) {
+			if (v->type == VEH_AIRCRAFT) {
 				const Station* st;
 
 				if (!IsValidStationID(new_order.dest)) return CMD_ERROR;
@@ -283,15 +284,15 @@
 				if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR;
 
 				switch (v->type) {
-					case VEH_Train:
+					case VEH_TRAIN:
 						if (!IsTileDepotType(dp->xy, TRANSPORT_RAIL)) return CMD_ERROR;
 						break;
 
-					case VEH_Road:
+					case VEH_ROAD:
 						if (!IsTileDepotType(dp->xy, TRANSPORT_ROAD)) return CMD_ERROR;
 						break;
 
-					case VEH_Ship:
+					case VEH_SHIP:
 						if (!IsTileDepotType(dp->xy, TRANSPORT_WATER)) return CMD_ERROR;
 						break;
 
@@ -309,7 +310,7 @@
 
 				case OF_NON_STOP | OF_PART_OF_ORDERS:
 				case OF_NON_STOP | OF_PART_OF_ORDERS | OF_HALT_IN_DEPOT:
-					if (v->type != VEH_Train) return CMD_ERROR;
+					if (v->type != VEH_TRAIN) return CMD_ERROR;
 					break;
 
 				default: return CMD_ERROR;
@@ -320,7 +321,7 @@
 		case OT_GOTO_WAYPOINT: {
 			const Waypoint* wp;
 
-			if (v->type != VEH_Train) return CMD_ERROR;
+			if (v->type != VEH_TRAIN) return CMD_ERROR;
 
 			if (!IsValidWaypointID(new_order.dest)) return CMD_ERROR;
 			wp = GetWaypoint(new_order.dest);
@@ -334,7 +335,7 @@
 				case 0: break;
 
 				case OF_NON_STOP:
-					if (v->type != VEH_Train) return CMD_ERROR;
+					if (v->type != VEH_TRAIN) return CMD_ERROR;
 					break;
 
 				default: return CMD_ERROR;
@@ -353,7 +354,7 @@
 	 * handle any more then this.. */
 	if (v->num_orders >= MAX_BACKUP_ORDER_COUNT) return_cmd_error(STR_8832_TOO_MANY_ORDERS);
 
-	if (v->type == VEH_Ship &&
+	if (v->type == VEH_SHIP &&
 			IsHumanPlayer(v->owner) &&
 			!_patches.new_pathfinding_all) {
 		// Make sure the new destination is not too far away from the previous
@@ -496,8 +497,7 @@
 		}
 
 		/* Give the item free */
-		order->type = OT_NOTHING;
-		order->next = NULL;
+		order->Free();
 
 		u = GetFirstVehicleFromSharedList(v);
 		DeleteOrderWarnings(u);
@@ -553,9 +553,9 @@
 
 		v->cur_order_index = b;
 
-		if (v->type == VEH_Train) v->u.rail.days_since_order_progr = 0;
+		if (v->type == VEH_TRAIN) v->u.rail.days_since_order_progr = 0;
 
-		if (v->type == VEH_Road) ClearSlot(v);
+		if (v->type == VEH_ROAD) ClearSlot(v);
 
 		/* NON-stop flag is misused to see if a train is in a station that is
 		 * on his order list or not */
@@ -566,8 +566,8 @@
 	}
 
 	/* We have an aircraft/ship, they have a mini-schedule, so update them all */
-	if (v->type == VEH_Aircraft) InvalidateWindowClasses(WC_AIRCRAFT_LIST);
-	if (v->type == VEH_Ship) InvalidateWindowClasses(WC_SHIPS_LIST);
+	if (v->type == VEH_AIRCRAFT) InvalidateWindowClasses(WC_AIRCRAFT_LIST);
+	if (v->type == VEH_SHIP) InvalidateWindowClasses(WC_SHIPS_LIST);
 
 	return 0;
 }
@@ -685,8 +685,8 @@
 				return CMD_ERROR;
 
 			/* Trucks can't share orders with busses (and visa versa) */
-			if (src->type == VEH_Road) {
-				if (src->cargo_type != dst->cargo_type && (src->cargo_type == CT_PASSENGERS || dst->cargo_type == CT_PASSENGERS))
+			if (src->type == VEH_ROAD) {
+				if (src->cargo_type != dst->cargo_type && (IsCargoInClass(src->cargo_type, CC_PASSENGERS) || IsCargoInClass(dst->cargo_type, CC_PASSENGERS)))
 					return CMD_ERROR;
 			}
 
@@ -732,14 +732,14 @@
 				return CMD_ERROR;
 
 			/* Trucks can't copy all the orders from busses (and visa versa) */
-			if (src->type == VEH_Road) {
+			if (src->type == VEH_ROAD) {
 				const Order *order;
 				TileIndex required_dst = INVALID_TILE;
 
 				FOR_VEHICLE_ORDERS(src, order) {
 					if (order->type == OT_GOTO_STATION) {
 						const Station *st = GetStation(order->dest);
-						if (dst->cargo_type == CT_PASSENGERS) {
+						if (IsCargoInClass(dst->cargo_type, CC_PASSENGERS)) {
 							if (st->bus_stops != NULL) required_dst = st->bus_stops->xy;
 						} else {
 							if (st->truck_stops != NULL) required_dst = st->truck_stops->xy;
@@ -871,9 +871,8 @@
 			*dest = *order;
 			dest++;
 		}
-		/* End the list with an OT_NOTHING */
-		dest->type = OT_NOTHING;
-		dest->next = NULL;
+		/* End the list with an empty order */
+		dest->Free();
 	}
 }
 
@@ -902,7 +901,7 @@
 	 *  order number is one more than the current amount of orders, and because
 	 *  in network the commands are queued before send, the second insert always
 	 *  fails in test mode. By bypassing the test-mode, that no longer is a problem. */
-	for (i = 0; bak->order[i].type != OT_NOTHING; i++) {
+	for (i = 0; bak->order[i].IsValid(); i++) {
 		if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
 			break;
 	}
@@ -951,11 +950,11 @@
 {
 	switch (v->type) {
 		default: NOT_REACHED();
-		case VEH_Train:     return st->train_tile;
-		case VEH_Aircraft:  return st->airport_tile;
-		case VEH_Ship:      return st->dock_tile;
-		case VEH_Road:
-			if (v->cargo_type == CT_PASSENGERS) {
+		case VEH_TRAIN:     return st->train_tile;
+		case VEH_AIRCRAFT:  return st->airport_tile;
+		case VEH_SHIP:      return st->dock_tile;
+		case VEH_ROAD:
+			if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
 				return (st->bus_stops != NULL) ? st->bus_stops->xy : 0;
 			} else {
 				return (st->truck_stops != NULL) ? st->truck_stops->xy : 0;
@@ -1063,7 +1062,7 @@
 		}
 
 		order = &v->current_order;
-		if ((v->type == VEH_Aircraft && order->type == OT_GOTO_DEPOT ? OT_GOTO_STATION : order->type) == type &&
+		if ((v->type == VEH_AIRCRAFT && order->type == OT_GOTO_DEPOT ? OT_GOTO_STATION : order->type) == type &&
 				v->current_order.dest == destination) {
 			order->type = OT_DUMMY;
 			order->flags = 0;
@@ -1073,7 +1072,7 @@
 		/* Clear the order from the order-list */
 		invalidate = false;
 		FOR_VEHICLE_ORDERS(v, order) {
-			if ((v->type == VEH_Aircraft && order->type == OT_GOTO_DEPOT ? OT_GOTO_STATION : order->type) == type &&
+			if ((v->type == VEH_AIRCRAFT && order->type == OT_GOTO_DEPOT ? OT_GOTO_STATION : order->type) == type &&
 					order->dest == destination) {
 				order->type = OT_DUMMY;
 				order->flags = 0;
@@ -1112,8 +1111,6 @@
  */
 void DeleteVehicleOrders(Vehicle *v)
 {
-	Order *cur, *next;
-
 	DeleteOrderWarnings(v);
 
 	/* If we have a shared order-list, don't delete the list, but just
@@ -1146,7 +1143,7 @@
 	}
 
 	/* Remove the orders */
-	cur = v->orders;
+	Order *cur = v->orders;
 	v->orders = NULL;
 	v->num_orders = 0;
 
@@ -1156,18 +1153,14 @@
 
 		switch (v->type) {
 			default: NOT_REACHED();
-			case VEH_Train:    window_class = WC_TRAINS_LIST;   break;
-			case VEH_Road:     window_class = WC_ROADVEH_LIST;  break;
-			case VEH_Ship:     window_class = WC_SHIPS_LIST;    break;
-			case VEH_Aircraft: window_class = WC_AIRCRAFT_LIST; break;
+			case VEH_TRAIN:    window_class = WC_TRAINS_LIST;   break;
+			case VEH_ROAD:     window_class = WC_ROADVEH_LIST;  break;
+			case VEH_SHIP:     window_class = WC_SHIPS_LIST;    break;
+			case VEH_AIRCRAFT: window_class = WC_AIRCRAFT_LIST; break;
 		}
 		DeleteWindowById(window_class, (cur->index << 16) | (v->type << 11) | VLW_SHARED_ORDERS | v->owner);
-	}
 
-	while (cur != NULL) {
-		next = cur->next;
-		DeleteOrder(cur);
-		cur = next;
+		cur->FreeChain(); // Free the orders.
 	}
 }
 
@@ -1199,7 +1192,7 @@
 	return false;
 }
 
-void InitializeOrders(void)
+void InitializeOrders()
 {
 	CleanPool(&_Order_pool);
 	AddBlockToPool(&_Order_pool);
@@ -1221,7 +1214,7 @@
 	SLE_END()
 };
 
-static void Save_ORDR(void)
+static void Save_ORDR()
 {
 	Order *order;
 
@@ -1231,7 +1224,7 @@
 	}
 }
 
-static void Load_ORDR(void)
+static void Load_ORDR()
 {
 	if (CheckSavegameVersionOldStyle(5, 2)) {
 		/* Version older than 5.2 did not have a ->next pointer. Convert them
@@ -1274,9 +1267,9 @@
 		/* Update all the next pointer */
 		for (i = 1; i < len; ++i) {
 			/* The orders were built like this:
-			 *   Vehicle one had order[0], and as long as order++.type was not
-			 *   OT_NOTHING, it was part of the order-list of that vehicle */
-			if (GetOrder(i)->type != OT_NOTHING)
+			 *   While the order is valid, set the previous will get it's next pointer set
+			 *   We start with index 1 because no order will have the first in it's next pointer */
+			if (GetOrder(i)->IsValid())
 				GetOrder(i - 1)->next = GetOrder(i);
 		}
 	} else {