changeset 15850:efc03d77eb93 draft

(svn r20532) -Codechange: unify selling of vehicles a bit
author rubidium <rubidium@openttd.org>
date Tue, 17 Aug 2010 23:55:22 +0000 (2010-08-17)
parents f4ed8e905fee
children 17927bf26be2
files src/ai/api/ai_vehicle.cpp src/aircraft_cmd.cpp src/command.cpp src/command_type.h src/roadveh_cmd.cpp src/ship_cmd.cpp src/train_cmd.cpp src/vehicle_cmd.cpp
diffstat 8 files changed, 58 insertions(+), 140 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/api/ai_vehicle.cpp
+++ b/src/ai/api/ai_vehicle.cpp
@@ -148,7 +148,7 @@
 	const Train *v = ::Train::Get(vehicle_id);
 	while (wagon-- > 0) v = v->GetNextUnit();
 
-	return AIObject::DoCommand(0, v->index, sell_attached_wagons ? 1 : 0, CMD_SELL_RAIL_WAGON);
+	return AIObject::DoCommand(0, v->index, sell_attached_wagons ? 1 : 0, CMD_SELL_VEHICLE);
 }
 
 /* static */ bool AIVehicle::SellWagon(VehicleID vehicle_id, int wagon)
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -338,36 +338,6 @@
 }
 
 
-/**
- * Sell an aircraft.
- * @param tile unused
- * @param flags for command type
- * @param p1 vehicle ID to be sold
- * @param p2 unused
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdSellAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	Aircraft *v = Aircraft::GetIfValid(p1);
-	if (v == NULL) return CMD_ERROR;
-
-	CommandCost ret = CheckOwnership(v->owner);
-	if (ret.Failed()) return ret;
-
-	if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED);
-
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value);
-
-	if (flags & DC_EXEC) {
-		delete v;
-	}
-
-	return ret;
-}
-
 bool Aircraft::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
 {
 	const Station *st = GetTargetAirportIfValid(this);
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -80,7 +80,7 @@
 CommandProc CmdMoveRailVehicle;
 
 CommandProc CmdBuildVehicle;
-CommandProc CmdSellRailWagon;
+CommandProc CmdSellVehicle;
 
 CommandProc CmdSendTrainToDepot;
 CommandProc CmdForceTrainProceed;
@@ -112,14 +112,12 @@
 CommandProc CmdRenameStation;
 CommandProc CmdRenameDepot;
 
-CommandProc CmdSellAircraft;
 CommandProc CmdSendAircraftToHangar;
 CommandProc CmdRefitAircraft;
 
 CommandProc CmdPlaceSign;
 CommandProc CmdRenameSign;
 
-CommandProc CmdSellRoadVeh;
 CommandProc CmdSendRoadVehToDepot;
 CommandProc CmdTurnRoadVeh;
 CommandProc CmdRefitRoadVeh;
@@ -139,7 +137,6 @@
 CommandProc CmdChangeSetting;
 CommandProc CmdChangeCompanySetting;
 
-CommandProc CmdSellShip;
 CommandProc CmdSendShipToDepot;
 CommandProc CmdRefitShip;
 
@@ -227,9 +224,9 @@
 	DEF_CMD(CmdBuildBuoy,                               CMD_AUTO), // CMD_BUILD_BUOY
 	DEF_CMD(CmdPlantTree,                               CMD_AUTO), // CMD_PLANT_TREE
 	DEF_CMD(CmdBuildVehicle,                                   0), // CMD_BUILD_VEHICLE
-	DEF_CMD(CmdMoveRailVehicle,                                0), // CMD_MOVE_RAIL_VEHICLE
+	DEF_CMD(CmdSellVehicle,                                    0), // CMD_SELL_VEHICLE
 
-	DEF_CMD(CmdSellRailWagon,                                  0), // CMD_SELL_RAIL_WAGON
+	DEF_CMD(CmdMoveRailVehicle,                                0), // CMD_MOVE_RAIL_VEHICLE
 	DEF_CMD(CmdSendTrainToDepot,                               0), // CMD_SEND_TRAIN_TO_DEPOT
 	DEF_CMD(CmdForceTrainProceed,                              0), // CMD_FORCE_TRAIN_PROCEED
 	DEF_CMD(CmdReverseTrainDirection,                          0), // CMD_REVERSE_TRAIN_DIRECTION
@@ -259,15 +256,12 @@
 	DEF_CMD(CmdRenameStation,                                  0), // CMD_RENAME_STATION
 	DEF_CMD(CmdRenameDepot,                                    0), // CMD_RENAME_DEPOT
 
-	DEF_CMD(CmdSellAircraft,                                   0), // CMD_SELL_AIRCRAFT
-
 	DEF_CMD(CmdSendAircraftToHangar,                           0), // CMD_SEND_AIRCRAFT_TO_HANGAR
 	DEF_CMD(CmdRefitAircraft,                                  0), // CMD_REFIT_AIRCRAFT
 
 	DEF_CMD(CmdPlaceSign,                                      0), // CMD_PLACE_SIGN
 	DEF_CMD(CmdRenameSign,                                     0), // CMD_RENAME_SIGN
 
-	DEF_CMD(CmdSellRoadVeh,                                    0), // CMD_SELL_ROAD_VEH
 	DEF_CMD(CmdSendRoadVehToDepot,                             0), // CMD_SEND_ROADVEH_TO_DEPOT
 	DEF_CMD(CmdTurnRoadVeh,                                    0), // CMD_TURN_ROADVEH
 	DEF_CMD(CmdRefitRoadVeh,                                   0), // CMD_REFIT_ROAD_VEH
@@ -284,7 +278,6 @@
 	DEF_CMD(CmdExpandTown,                           CMD_OFFLINE), // CMD_EXPAND_TOWN
 	DEF_CMD(CmdDeleteTown,                           CMD_OFFLINE), // CMD_DELETE_TOWN
 
-	DEF_CMD(CmdSellShip,                                       0), // CMD_SELL_SHIP
 	DEF_CMD(CmdSendShipToDepot,                                0), // CMD_SEND_SHIP_TO_DEPOT
 	DEF_CMD(CmdRefitShip,                                      0), // CMD_REFIT_SHIP
 
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -178,10 +178,9 @@
 	CMD_PLANT_TREE,                   ///< plant a tree
 
 	CMD_BUILD_VEHICLE,                ///< build a vehicle
-	CMD_MOVE_RAIL_VEHICLE,            ///< move a rail vehicle (in the depot)
+	CMD_SELL_VEHICLE,                 ///< sell a vehicle
 
-	CMD_SELL_RAIL_WAGON,              ///< sell a rail wagon
-
+	CMD_MOVE_RAIL_VEHICLE,            ///< move a rail vehicle (in the depot)
 	CMD_SEND_TRAIN_TO_DEPOT,          ///< send a train to a depot
 	CMD_FORCE_TRAIN_PROCEED,          ///< proceed a train to pass a red signal
 	CMD_REVERSE_TRAIN_DIRECTION,      ///< turn a train around
@@ -210,14 +209,12 @@
 	CMD_RENAME_STATION,               ///< rename a station
 	CMD_RENAME_DEPOT,                 ///< rename a depot
 
-	CMD_SELL_AIRCRAFT,                ///< sell an aircraft
 	CMD_SEND_AIRCRAFT_TO_HANGAR,      ///< send an aircraft to a hanger
 	CMD_REFIT_AIRCRAFT,               ///< refit the cargo space of an aircraft
 
 	CMD_PLACE_SIGN,                   ///< place a sign
 	CMD_RENAME_SIGN,                  ///< rename a sign
 
-	CMD_SELL_ROAD_VEH,                ///< sell a road vehicle
 	CMD_SEND_ROADVEH_TO_DEPOT,        ///< send a road vehicle to the depot
 	CMD_TURN_ROADVEH,                 ///< turn a road vehicle around
 	CMD_REFIT_ROAD_VEH,               ///< refit the cargo space of a road vehicle
@@ -234,7 +231,6 @@
 	CMD_EXPAND_TOWN,                  ///< expand a town
 	CMD_DELETE_TOWN,                  ///< delete a town
 
-	CMD_SELL_SHIP,                    ///< sell a ship
 	CMD_SEND_SHIP_TO_DEPOT,           ///< send a ship to a depot
 	CMD_REFIT_SHIP,                   ///< refit the cargo space of a ship
 
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -294,38 +294,6 @@
 	return true;
 }
 
-/**
- * Sell a road vehicle.
- * @param tile unused
- * @param flags operation to perform
- * @param p1 vehicle ID to be sold
- * @param p2 unused
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdSellRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	RoadVehicle *v = RoadVehicle::GetIfValid(p1);
-	if (v == NULL) return CMD_ERROR;
-
-	CommandCost ret = CheckOwnership(v->owner);
-	if (ret.Failed()) return ret;
-
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	if (!v->IsStoppedInDepot()) {
-		return_cmd_error(STR_ERROR_ROAD_VEHICLE_MUST_BE_STOPPED_INSIDE_DEPOT);
-	}
-
-	ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value);
-
-	if (flags & DC_EXEC) {
-		delete v;
-	}
-
-	return ret;
-}
-
 static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance)
 {
 	if (IsRoadDepotTile(v->tile)) return FindDepotData(v->tile, 0);
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -668,38 +668,6 @@
 	return CommandCost();
 }
 
-/**
- * Sell a ship.
- * @param tile unused
- * @param flags type of operation
- * @param p1 vehicle ID to be sold
- * @param p2 unused
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	Ship *v = Ship::GetIfValid(p1);
-	if (v == NULL) return CMD_ERROR;
-
-	CommandCost ret = CheckOwnership(v->owner);
-	if (ret.Failed()) return ret;
-
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	if (!v->IsStoppedInDepot()) {
-		return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
-	}
-
-	ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value);
-
-	if (flags & DC_EXEC) {
-		delete v;
-	}
-
-	return ret;
-}
-
 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
 {
 	const Depot *depot = FindClosestShipDepot(this, 0);
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1319,40 +1319,25 @@
 
 /**
  * Sell a (single) train wagon/engine.
- * @param tile unused
  * @param flags type of operation
- * @param p1 the wagon/engine index
- * @param p2 the selling mode
- * - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
- * - p2 = 1: sell the vehicle and all vehicles following it in the chain
- *           if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
- * @param text unused
+ * @param t     the train wagon to sell
+ * @param data  the selling mode
+ * - data = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
+ * - data = 1: sell the vehicle and all vehicles following it in the chain
+ *             if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
  * @return the cost of this operation or an error
  */
-CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *t, uint16 data)
 {
 	/* Check if we deleted a vehicle window */
 	Window *w = NULL;
 
-	Train *v = Train::GetIfValid(p1);
-	if (v == NULL) return CMD_ERROR;
-
-	CommandCost ret = CheckOwnership(v->owner);
-	if (ret.Failed()) return ret;
-
 	/* Sell a chain of vehicles or not? */
-	bool sell_chain = HasBit(p2, 0);
-
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	v = v->GetFirstEnginePart();
+	bool sell_chain = HasBit(data, 0);
+
+	Train *v = Train::From(t)->GetFirstEnginePart();
 	Train *first = v->First();
 
-	/* make sure the vehicle is stopped in the depot */
-	if (!first->IsStoppedInDepot()) {
-		return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
-	}
-
 	if (v->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
 
 	/* First make a backup of the order of the train. That way we can do
@@ -1368,7 +1353,7 @@
 	ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain);
 
 	/* We don't need to validate the second train; it's going to be sold. */
-	ret = ValidateTrains(NULL, NULL, first, new_head);
+	CommandCost ret = ValidateTrains(NULL, NULL, first, new_head);
 	if (ret.Failed()) {
 		/* Restore the train we had. */
 		RestoreTrainBackup(original);
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -44,10 +44,10 @@
 };
 
 const uint32 _veh_sell_proc_table[] = {
-	CMD_SELL_RAIL_WAGON | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
-	CMD_SELL_ROAD_VEH   | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
-	CMD_SELL_SHIP       | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
-	CMD_SELL_AIRCRAFT   | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
+	CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN),
+	CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_ROAD_VEHICLE),
+	CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_SHIP),
+	CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_AIRCRAFT),
 };
 
 const uint32 _veh_refit_proc_table[] = {
@@ -149,6 +149,44 @@
 	return value;
 }
 
+CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *v, uint16 data);
+
+/**
+ * Sell a vehicle.
+ * @param tile unused.
+ * @param flags for command.
+ * @param p1 vehicle ID to be sold.
+ * @param p2 unused.
+ * @param text unused.
+ * @return the cost of this operation or an error.
+ */
+CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+	Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 16));
+	if (v == NULL) return CMD_ERROR;
+
+	Vehicle *front = v->First();
+
+	CommandCost ret = CheckOwnership(front->owner);
+	if (ret.Failed()) return ret;
+
+	if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
+
+	if (!front->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED + front->type);
+
+	if (v->type == VEH_TRAIN) {
+		ret = CmdSellRailWagon(flags, v, GB(p2, 0, 16));
+	} else {
+		ret = CommandCost(EXPENSES_NEW_VEHICLES, -front->value);
+
+		if (flags & DC_EXEC) {
+			delete front;
+		}
+	}
+
+	return ret;
+}
+
 /**
  * Start/Stop a vehicle
  * @param tile unused