changeset 15854:43d172eac154 draft

(svn r20536) -Codechange: unify the refitting of vehicles
author rubidium <rubidium@openttd.org>
date Wed, 18 Aug 2010 00:47:31 +0000
parents 982e325ab103
children 8071bb199a2c
files 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 7 files changed, 78 insertions(+), 196 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -383,49 +383,6 @@
 }
 
 
-/**
- * Refits an aircraft to the specified cargo type.
- * @param tile unused
- * @param flags for command type
- * @param p1 vehicle ID of the aircraft to refit
- * @param p2 various bitstuffed elements
- * - p2 = (bit 0-7) - the new cargo type to refit to
- * - p2 = (bit 8-15) - the new cargo subtype to refit to
- * - p2 = (bit 16) - refit only this vehicle (ignored)
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdRefitAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	byte new_subtype = GB(p2, 8, 8);
-
-	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_INSIDE_HANGAR);
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	/* Check cargo */
-	CargoID new_cid = GB(p2, 0, 8);
-	if (new_cid >= NUM_CARGO) return CMD_ERROR;
-
-	CommandCost cost = RefitVehicle(v, true, new_cid, new_subtype, flags);
-
-	if (flags & DC_EXEC) {
-		v->colourmap = PAL_NONE; // invalidate vehicle colour map
-		SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
-		SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
-		InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
-	}
-	v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
-
-	return cost;
-}
-
-
 static void CheckIfAircraftNeedsService(Aircraft *v)
 {
 	if (Company::Get(v->owner)->settings.vehicle.servint_aircraft == 0 || !v->NeedsAutomaticServicing()) return;
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -81,6 +81,7 @@
 
 CommandProc CmdBuildVehicle;
 CommandProc CmdSellVehicle;
+CommandProc CmdRefitVehicle;
 
 CommandProc CmdSendTrainToDepot;
 CommandProc CmdForceTrainProceed;
@@ -113,14 +114,12 @@
 CommandProc CmdRenameDepot;
 
 CommandProc CmdSendAircraftToHangar;
-CommandProc CmdRefitAircraft;
 
 CommandProc CmdPlaceSign;
 CommandProc CmdRenameSign;
 
 CommandProc CmdSendRoadVehToDepot;
 CommandProc CmdTurnRoadVeh;
-CommandProc CmdRefitRoadVeh;
 
 CommandProc CmdPause;
 
@@ -138,7 +137,6 @@
 CommandProc CmdChangeCompanySetting;
 
 CommandProc CmdSendShipToDepot;
-CommandProc CmdRefitShip;
 
 CommandProc CmdOrderRefit;
 CommandProc CmdCloneOrder;
@@ -154,8 +152,6 @@
 
 CommandProc CmdLevelLand;
 
-CommandProc CmdRefitRailVehicle;
-
 CommandProc CmdBuildSignalTrack;
 CommandProc CmdRemoveSignalTrack;
 
@@ -223,8 +219,10 @@
 	DEF_CMD(CmdBuildShipDepot,                          CMD_AUTO), // CMD_BUILD_SHIP_DEPOT
 	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(CmdSellVehicle,                                    0), // CMD_SELL_VEHICLE
+	DEF_CMD(CmdRefitVehicle,                                   0), // CMD_REFIT_VEHICLE
 
 	DEF_CMD(CmdMoveRailVehicle,                                0), // CMD_MOVE_RAIL_VEHICLE
 	DEF_CMD(CmdSendTrainToDepot,                               0), // CMD_SEND_TRAIN_TO_DEPOT
@@ -257,14 +255,12 @@
 	DEF_CMD(CmdRenameDepot,                                    0), // CMD_RENAME_DEPOT
 
 	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(CmdSendRoadVehToDepot,                             0), // CMD_SEND_ROADVEH_TO_DEPOT
 	DEF_CMD(CmdTurnRoadVeh,                                    0), // CMD_TURN_ROADVEH
-	DEF_CMD(CmdRefitRoadVeh,                                   0), // CMD_REFIT_ROAD_VEH
 
 	DEF_CMD(CmdPause,                                 CMD_SERVER), // CMD_PAUSE
 
@@ -279,7 +275,6 @@
 	DEF_CMD(CmdDeleteTown,                           CMD_OFFLINE), // CMD_DELETE_TOWN
 
 	DEF_CMD(CmdSendShipToDepot,                                0), // CMD_SEND_SHIP_TO_DEPOT
-	DEF_CMD(CmdRefitShip,                                      0), // CMD_REFIT_SHIP
 
 	DEF_CMD(CmdOrderRefit,                                     0), // CMD_ORDER_REFIT
 	DEF_CMD(CmdCloneOrder,                                     0), // CMD_CLONE_ORDER
@@ -292,7 +287,6 @@
 
 	DEF_CMD(CmdLevelLand, CMD_ALL_TILES | CMD_NO_TEST | CMD_AUTO), // CMD_LEVEL_LAND; test run might clear tiles multiple times, in execution that only happens once
 
-	DEF_CMD(CmdRefitRailVehicle,                               0), // CMD_REFIT_RAIL_VEHICLE
 	DEF_CMD(CmdRestoreOrderIndex,                              0), // CMD_RESTORE_ORDER_INDEX
 	DEF_CMD(CmdBuildLock,                               CMD_AUTO), // CMD_BUILD_LOCK
 
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -179,6 +179,7 @@
 
 	CMD_BUILD_VEHICLE,                ///< build a vehicle
 	CMD_SELL_VEHICLE,                 ///< sell a vehicle
+	CMD_REFIT_VEHICLE,                ///< refit the cargo space of a vehicle
 
 	CMD_MOVE_RAIL_VEHICLE,            ///< move a rail vehicle (in the depot)
 	CMD_SEND_TRAIN_TO_DEPOT,          ///< send a train to a depot
@@ -210,14 +211,12 @@
 	CMD_RENAME_DEPOT,                 ///< rename a depot
 
 	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_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
 
 	CMD_PAUSE,                        ///< pause the game
 
@@ -232,7 +231,6 @@
 	CMD_DELETE_TOWN,                  ///< delete a town
 
 	CMD_SEND_SHIP_TO_DEPOT,           ///< send a ship to a depot
-	CMD_REFIT_SHIP,                   ///< refit the cargo space of a ship
 
 	CMD_ORDER_REFIT,                  ///< change the refit informaction of an order (for "goto depot" )
 	CMD_CLONE_ORDER,                  ///< clone (and share) an order
@@ -244,7 +242,6 @@
 	CMD_COMPANY_CTRL,                 ///< used in multiplayer to create a new companies etc.
 	CMD_LEVEL_LAND,                   ///< level land
 
-	CMD_REFIT_RAIL_VEHICLE,           ///< refit the cargo space of a train
 	CMD_RESTORE_ORDER_INDEX,          ///< restore vehicle order-index and service interval
 	CMD_BUILD_LOCK,                   ///< build a lock
 
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -1692,49 +1692,3 @@
 	 * otherwise transform it into a valid track direction */
 	return (Trackdir)((IsReversingRoadTrackdir((Trackdir)this->state)) ? (this->state - 6) : this->state);
 }
-
-
-/**
- * Refit a road vehicle to the specified cargo type
- * @param tile unused
- * @param flags operation to perform
- * @param p1 Vehicle ID of the vehicle to refit
- * @param p2 Bitstuffed elements
- * - p2 = (bit 0-7) - the new cargo type to refit to
- * - p2 = (bit 8-15) - the new cargo subtype to refit to
- * - p2 = (bit 16) - refit only this vehicle
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	CargoID new_cid = GB(p2, 0, 8);
-	byte new_subtype = GB(p2, 8, 8);
-	bool only_this = HasBit(p2, 16);
-
-	RoadVehicle *v = RoadVehicle::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_ROAD_VEHICLE_MUST_BE_STOPPED_INSIDE_DEPOT);
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	if (new_cid >= NUM_CARGO) return CMD_ERROR;
-
-	CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
-
-	if (flags & DC_EXEC) {
-		RoadVehicle *front = v->First();
-		RoadVehUpdateCache(front);
-		if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) front->CargoChanged();
-		InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
-		SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
-		InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
-	} else {
-		v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
-	}
-
-	return cost;
-}
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -704,47 +704,3 @@
 
 	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
 }
-
-
-/**
- * Refits a ship to the specified cargo type.
- * @param tile unused
- * @param flags type of operation
- * @param p1 vehicle ID of the ship to refit
- * @param p2 various bitstuffed elements
- * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
- * - p2 = (bit 8-15) - the new cargo subtype to refit to
- * - p2 = (bit 16) - refit only this vehicle (ignored)
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdRefitShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	CargoID new_cid = GB(p2, 0, 8); // gets the cargo number
-	byte new_subtype = GB(p2, 8, 8);
-
-	Ship *v = Ship::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_SHIP_MUST_BE_STOPPED_INSIDE_DEPOT);
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	/* Check cargo */
-	if (new_cid >= NUM_CARGO) return CMD_ERROR;
-
-	CommandCost cost = RefitVehicle(v, true, new_cid, new_subtype, flags);
-
-	if (flags & DC_EXEC) {
-		v->colourmap = PAL_NONE; // invalidate vehicle colour map
-		SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
-		SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
-		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
-	}
-	v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
-
-	return cost;
-
-}
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1888,52 +1888,6 @@
 }
 
 /**
- * Refits a train to the specified cargo type.
- * @param tile unused
- * @param flags type of operation
- * @param p1 vehicle ID of the train to refit
- * @param p2 various bitstuffed elements
- * - p2 = (bit 0-7) - the new cargo type to refit to
- * - p2 = (bit 8-15) - the new cargo subtype to refit to
- * - p2 = (bit 16) - refit only this vehicle
- * @param text unused
- * @return the cost of this operation or an error
- */
-CommandCost CmdRefitRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
-{
-	CargoID new_cid = GB(p2, 0, 8);
-	byte new_subtype = GB(p2, 8, 8);
-	bool only_this = HasBit(p2, 16);
-
-	Train *v = Train::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_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT);
-	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
-
-	/* Check cargo */
-	if (new_cid >= NUM_CARGO) return CMD_ERROR;
-
-	CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
-
-	/* Update the train's cached variables */
-	if (flags & DC_EXEC) {
-		Train *front = v->First();
-		front->ConsistChanged(false);
-		SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
-		SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
-		InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
-	} else {
-		v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
-	}
-
-	return cost;
-}
-
-/**
  * returns the tile of a depot to goto to. The given vehicle must not be
  * crashed!
  */
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -51,10 +51,10 @@
 };
 
 const uint32 _veh_refit_proc_table[] = {
-	CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
-	CMD_REFIT_ROAD_VEH     | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
-	CMD_REFIT_SHIP         | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
-	CMD_REFIT_AIRCRAFT     | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
+	CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_TRAIN),
+	CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_ROAD_VEHICLE),
+	CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_SHIP),
+	CMD_REFIT_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_REFIT_AIRCRAFT),
 };
 
 const uint32 _send_to_depot_proc_table[] = {
@@ -190,6 +190,76 @@
 }
 
 /**
+ * Refits a vehicle to the specified cargo type.
+ * @param tile unused
+ * @param flags type of operation
+ * @param p1 vehicle ID of the train to refit
+ * @param p2 various bitstuffed elements
+ * - p2 = (bit 0-7) - the new cargo type to refit to
+ * - p2 = (bit 8-15) - the new cargo subtype to refit to
+ * - p2 = (bit 16) - refit only this vehicle
+ * @param text unused
+ * @return the cost of this operation or an error
+ */
+CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+	Vehicle *v = Vehicle::GetIfValid(p1);
+	if (v == NULL) return CMD_ERROR;
+
+	Vehicle *front = v->First();
+
+	CommandCost ret = CheckOwnership(front->owner);
+	if (ret.Failed()) return ret;
+
+	/* Don't allow disasters and sparks and such to be refitted. */
+	if (!front->IsPrimaryVehicle()) return CMD_ERROR;
+	/* Don't allow shadows and such to be refitted. */
+	if (v != front && (v->type == VEH_SHIP || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
+	if (!front->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAIN_MUST_BE_STOPPED_INSIDE_DEPOT + front->type);
+	if (front->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_VEHICLE_IS_DESTROYED);
+
+	/* Check cargo */
+	CargoID new_cid = GB(p2, 0, 8);
+	byte new_subtype = GB(p2, 8, 8);
+	if (new_cid >= NUM_CARGO) return CMD_ERROR;
+
+	/* For ships and aircrafts there is always only one. */
+	bool only_this = HasBit(p2, 16) || front->type == VEH_SHIP || front->type == VEH_AIRCRAFT;
+
+	CommandCost cost = RefitVehicle(v, only_this, new_cid, new_subtype, flags);
+
+	if (flags & DC_EXEC) {
+		/* Update the cached variables */
+		switch (v->type) {
+			case VEH_TRAIN:
+				Train::From(front)->ConsistChanged(false);
+				break;
+			case VEH_ROAD:
+				RoadVehUpdateCache(RoadVehicle::From(front));
+				if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) RoadVehicle::From(front)->CargoChanged();
+				break;
+
+			case VEH_SHIP:
+			case VEH_AIRCRAFT:
+				v->InvalidateNewGRFCacheOfChain();
+				v->colourmap = PAL_NONE; // invalidate vehicle colour map
+				break;
+
+			default: NOT_REACHED();
+		}
+
+		InvalidateWindowData(WC_VEHICLE_DETAILS, front->index);
+		SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
+		InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
+	} else {
+		/* Always invalidate the cache; querycost might have filled it. */
+		v->InvalidateNewGRFCacheOfChain();
+	}
+
+	return cost;
+}
+
+/**
  * Start/Stop a vehicle
  * @param tile unused
  * @param flags type of operation