changeset 8891:384f81a7ba31 draft

(svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
author rubidium <rubidium@openttd.org>
date Fri, 11 Apr 2008 08:40:10 +0000
parents b1d45af72e04
children bb542150b318
files src/aircraft_cmd.cpp src/roadveh_cmd.cpp src/ship_cmd.cpp src/train_cmd.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_func.h src/vehicle_type.h
diffstat 8 files changed, 96 insertions(+), 210 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -80,7 +80,7 @@
 static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc);
 static void CrashAirplane(Vehicle *v);
 
-static void AircraftNextAirportPos_and_Order(Vehicle *v);
+void AircraftNextAirportPos_and_Order(Vehicle *v);
 static byte GetAircraftFlyingAltitude(const Vehicle *v);
 
 static const SpriteID _aircraft_sprite[] = {
@@ -586,59 +586,9 @@
 
 	Vehicle *v = GetVehicle(p1);
 
-	if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner) || v->IsInDepot()) return CMD_ERROR;
-
-	if (v->current_order.IsType(OT_GOTO_DEPOT) && !(p2 & DEPOT_LOCATE_HANGAR)) {
-		bool halt_in_depot = v->current_order.GetDepotActionType() & ODATFB_HALT;
-		if (!!(p2 & DEPOT_SERVICE) == halt_in_depot) {
-			/* We called with a different DEPOT_SERVICE setting.
-			 * Now we change the setting to apply the new one and let the vehicle head for the same hangar.
-			 * Note: the if is (true for requesting service == true for ordered to stop in hangar) */
-			if (flags & DC_EXEC) {
-				v->current_order.SetDepotOrderType(ODTF_MANUAL);
-				v->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
-				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-			}
-			return CommandCost();
-		}
-
-		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of hangar orders
-		if (flags & DC_EXEC) {
-			/* If the orders to 'goto depot' are in the orders list (forced servicing),
-			 * then skip to the next order; effectively cancelling this forced service */
-			if (v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) v->cur_order_index++;
+	if (v->type != VEH_AIRCRAFT) return CMD_ERROR;
 
-			v->current_order.MakeDummy();
-			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-		}
-	} else {
-		bool next_airport_has_hangar = true;
-		StationID next_airport_index = v->u.air.targetairport;
-		const Station *st = GetStation(next_airport_index);
-		/* If the station is not a valid airport or if it has no hangars */
-		if (!st->IsValid() || st->airport_tile == 0 || st->Airport()->nof_depots == 0) {
-			/* the aircraft has to search for a hangar on its own */
-			StationID station = FindNearestHangar(v);
-
-			next_airport_has_hangar = false;
-			if (station == INVALID_STATION) return CMD_ERROR;
-			next_airport_index = station;
-		}
-
-		if (flags & DC_EXEC) {
-			if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
-
-			v->current_order.MakeGoToDepot(next_airport_index, ODTF_MANUAL);
-			if (!(p2 & DEPOT_SERVICE)) v->current_order.SetDepotActionType(ODATFB_HALT);
-			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-			if (v->u.air.state == FLYING && !next_airport_has_hangar) {
-				/* The aircraft is now heading for a different hangar than the next in the orders */
-				AircraftNextAirportPos_and_Order(v);
-			}
-		}
-	}
-
-	return CommandCost();
+	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 }
 
 
@@ -1520,7 +1470,7 @@
 
 
 /** set the right pos when heading to other airports after takeoff */
-static void AircraftNextAirportPos_and_Order(Vehicle *v)
+void AircraftNextAirportPos_and_Order(Vehicle *v)
 {
 	if (v->current_order.IsType(OT_GOTO_STATION) ||
 			v->current_order.IsType(OT_GOTO_DEPOT))
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -468,9 +468,6 @@
  */
 CommandCost CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Vehicle *v;
-	const Depot *dep;
-
 	if (p2 & DEPOT_MASS_SEND) {
 		/* Mass goto depot requested */
 		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
@@ -479,55 +476,11 @@
 
 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 
-	v = GetVehicle(p1);
-
-	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
-
-	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
-
-	if (v->IsInDepot()) return CMD_ERROR;
-
-	/* If the current orders are already goto-depot */
-	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
-		bool halt_in_depot = v->current_order.GetDepotActionType() & ODATFB_HALT;
-		if (!!(p2 & DEPOT_SERVICE) == halt_in_depot) {
-			/* We called with a different DEPOT_SERVICE setting.
-			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
-			 * Note: the if is (true for requesting service == true for ordered to stop in depot) */
-			if (flags & DC_EXEC) {
-				v->current_order.SetDepotOrderType(ODTF_MANUAL);
-				v->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
-				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-			}
-			return CommandCost();
-		}
+	Vehicle *v = GetVehicle(p1);
 
-		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
-		if (flags & DC_EXEC) {
-			/* If the orders to 'goto depot' are in the orders list (forced servicing),
-			 * then skip to the next order; effectively cancelling this forced service */
-			if (v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) v->cur_order_index++;
-
-			v->current_order.MakeDummy();
-			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-		}
-		return CommandCost();
-	}
+	if (v->type != VEH_ROAD) return CMD_ERROR;
 
-	dep = FindClosestRoadDepot(v);
-	if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
-
-	if (flags & DC_EXEC) {
-		if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
-
-		ClearSlot(v);
-		v->current_order.MakeGoToDepot(dep->index, ODTF_MANUAL);
-		if (!(p2 & DEPOT_SERVICE)) v->current_order.SetDepotActionType(ODATFB_HALT);
-		v->dest_tile = dep->xy;
-		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-	}
-
-	return CommandCost();
+	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 }
 
 /** Turn a roadvehicle around.
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -934,9 +934,6 @@
  */
 CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Vehicle *v;
-	const Depot *dep;
-
 	if (p2 & DEPOT_MASS_SEND) {
 		/* Mass goto depot requested */
 		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
@@ -945,54 +942,11 @@
 
 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 
-	v = GetVehicle(p1);
-
-	if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
-
-	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
-
-	if (v->IsInDepot()) return CMD_ERROR;
-
-	/* If the current orders are already goto-depot */
-	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
-		bool halt_in_depot = v->current_order.GetDepotActionType() & ODATFB_HALT;
-		if (!!(p2 & DEPOT_SERVICE) == halt_in_depot) {
-			/* We called with a different DEPOT_SERVICE setting.
-			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
-			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
-			if (flags & DC_EXEC) {
-				v->current_order.SetDepotOrderType(ODTF_MANUAL);
-				v->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
-				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-			}
-			return CommandCost();
-		}
+	Vehicle *v = GetVehicle(p1);
 
-		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
-		if (flags & DC_EXEC) {
-			/* If the orders to 'goto depot' are in the orders list (forced servicing),
-			 * then skip to the next order; effectively cancelling this forced service */
-			if (v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) v->cur_order_index++;
-
-			v->current_order.MakeDummy();
-			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-		}
-		return CommandCost();
-	}
+	if (v->type != VEH_SHIP) return CMD_ERROR;
 
-	dep = FindClosestShipDepot(v);
-	if (dep == NULL) return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT);
-
-	if (flags & DC_EXEC) {
-		if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
-
-		v->dest_tile = dep->xy;
-		v->current_order.MakeGoToDepot(dep->index, ODTF_MANUAL);
-		if (!(p2 & DEPOT_SERVICE)) v->current_order.SetDepotActionType(ODATFB_HALT);
-		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-	}
-
-	return CommandCost();
+	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 }
 
 
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -2116,55 +2116,9 @@
 
 	Vehicle *v = GetVehicle(p1);
 
-	if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
-
-	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
-
-	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
-		bool halt_in_depot = v->current_order.GetDepotActionType() & ODATFB_HALT;
-		if (!!(p2 & DEPOT_SERVICE) == halt_in_depot) {
-			/* We called with a different DEPOT_SERVICE setting.
-			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
-			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
-			if (flags & DC_EXEC) {
-				v->current_order.SetDepotOrderType(ODTF_MANUAL);
-				v->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
-				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-			}
-			return CommandCost();
-		}
-
-		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
-		if (flags & DC_EXEC) {
-			/* If the orders to 'goto depot' are in the orders list (forced servicing),
-			 * then skip to the next order; effectively cancelling this forced service */
-			if (v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) v->cur_order_index++;
-
-			v->current_order.MakeDummy();
-			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-		}
-		return CommandCost();
-	}
-
-	/* check if at a standstill (not stopped only) in a depot
-	 * the check is down here to make it possible to alter stop/service for trains entering the depot */
-	if (IsTileDepotType(v->tile, TRANSPORT_RAIL) && v->cur_speed == 0) return CMD_ERROR;
-
-	TrainFindDepotData tfdd = FindClosestTrainDepot(v, 0);
-	if (tfdd.best_length == (uint)-1) return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
-
-	if (flags & DC_EXEC) {
-		if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
-
-		v->dest_tile = tfdd.tile;
-		v->current_order.MakeGoToDepot(GetDepotByTile(tfdd.tile)->index, ODTF_MANUAL);
-		if (!(p2 & DEPOT_SERVICE)) v->current_order.SetDepotActionType(ODATFB_HALT);
-		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
-		/* If there is no depot in front, reverse automatically */
-		if (tfdd.reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
-	}
-
-	return CommandCost();
+	if (v->type != VEH_TRAIN) return CMD_ERROR;
+
+	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 }
 
 
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -3209,6 +3209,71 @@
 	InvalidateVehicleOrder(this);
 }
 
+CommandCost Vehicle::SendToDepot(uint32 flags, DepotCommand command)
+{
+	if (!CheckOwnership(this->owner)) return CMD_ERROR;
+	if (this->vehstatus & VS_CRASHED) return CMD_ERROR;
+	if (this->IsInDepot()) return CMD_ERROR;
+
+	if (this->current_order.IsType(OT_GOTO_DEPOT)) {
+		bool halt_in_depot = this->current_order.GetDepotActionType() & ODATFB_HALT;
+		if (!!(command & DEPOT_SERVICE) == halt_in_depot) {
+			/* We called with a different DEPOT_SERVICE setting.
+			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
+			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
+			if (flags & DC_EXEC) {
+				this->current_order.SetDepotOrderType(ODTF_MANUAL);
+				this->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
+				InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
+			}
+			return CommandCost();
+		}
+
+		if (command & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
+		if (flags & DC_EXEC) {
+			/* If the orders to 'goto depot' are in the orders list (forced servicing),
+			 * then skip to the next order; effectively cancelling this forced service */
+			if (this->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) this->cur_order_index++;
+
+			this->current_order.MakeDummy();
+			InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
+		}
+		return CommandCost();
+	}
+
+	/* check if at a standstill (not stopped only) in a depot
+	 * the check is down here to make it possible to alter stop/service for trains entering the depot */
+	if (this->type == VEH_TRAIN && IsTileDepotType(this->tile, TRANSPORT_RAIL) && this->cur_speed == 0) return CMD_ERROR;
+
+	TileIndex location;
+	DestinationID destination;
+	bool reverse;
+	static const StringID no_depot[] = {STR_883A_UNABLE_TO_FIND_ROUTE_TO, STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT, STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT, STR_A012_CAN_T_SEND_AIRCRAFT_TO};
+	if (!this->FindClosestDepot	(&location, &destination, &reverse)) return_cmd_error(no_depot[this->type]);
+
+	if (flags & DC_EXEC) {
+		if (this->current_order.IsType(OT_LOADING)) this->LeaveStation();
+
+		this->dest_tile = location;
+		this->current_order.MakeGoToDepot(destination, ODTF_MANUAL);
+		if (!(command & DEPOT_SERVICE)) this->current_order.SetDepotActionType(ODATFB_HALT);
+		InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
+
+		/* If there is no depot in front, reverse automatically (trains only) */
+		if (this->type == VEH_TRAIN && reverse) DoCommand(this->tile, this->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
+
+		if (this->type == VEH_AIRCRAFT && this->u.air.state == FLYING && this->u.air.targetairport != destination) {
+				/* The aircraft is now heading for a different hangar than the next in the orders */
+				extern void AircraftNextAirportPos_and_Order(Vehicle *v);
+				AircraftNextAirportPos_and_Order(this);
+			}
+
+	}
+
+	return CommandCost();
+
+}
+
 void Vehicle::SetNext(Vehicle *next)
 {
 	if (this->next != NULL) {
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -522,6 +522,14 @@
 	 * @return true if a depot could be found.
 	 */
 	virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
+
+	/**
+	 * Send this vehicle to the depot using the given command(s).
+	 * @param flags   the command flags (like execute and such).
+	 * @param command the command to execute.
+	 * @return the cost of the depot action.
+	 */
+	CommandCost SendToDepot(uint32 flags, DepotCommand command);
 };
 
 /**
--- a/src/vehicle_func.h
+++ b/src/vehicle_func.h
@@ -76,14 +76,6 @@
 
 void CcCloneVehicle(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
-/* Flags to add to p2 for goto depot commands */
-/* Note: bits 8-10 are used for VLW flags */
-enum {
-	DEPOT_SERVICE       = (1 << 0), // The vehicle will leave the depot right after arrival (serivce only)
-	DEPOT_MASS_SEND     = (1 << 1), // Tells that it's a mass send to depot command (type in VLW flag)
-	DEPOT_DONT_CANCEL   = (1 << 2), // Don't cancel current goto depot command if any
-	DEPOT_LOCATE_HANGAR = (1 << 3), // Find another airport if the target one lacks a hangar
-};
 
 struct GetNewVehiclePosResult {
 	int x, y;
--- a/src/vehicle_type.h
+++ b/src/vehicle_type.h
@@ -60,4 +60,14 @@
 	VPF_YAPF = 2, ///< Yet Another PathFinder
 };
 
+/* Flags to add to p2 for goto depot commands */
+/* Note: bits 8-10 are used for VLW flags */
+enum DepotCommand {
+	DEPOT_SERVICE       = (1 << 0), ///< The vehicle will leave the depot right after arrival (serivce only)
+	DEPOT_MASS_SEND     = (1 << 1), ///< Tells that it's a mass send to depot command (type in VLW flag)
+	DEPOT_DONT_CANCEL   = (1 << 2), ///< Don't cancel current goto depot command if any
+	DEPOT_LOCATE_HANGAR = (1 << 3), ///< Find another airport if the target one lacks a hangar
+	DEPOT_COMMAND_MASK  = 0xF,
+};
+
 #endif /* VEHICLE_TYPE_H */