From 982df3e96b7eb63b5fba94ad562a5e06b4ec588c Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 18 Aug 2010 00:47:31 +0000 Subject: [PATCH] (svn r20536) -Codechange: unify the refitting of vehicles --- src/aircraft_cmd.cpp | 43 ------------------------ src/command.cpp | 12 ++----- src/command_type.h | 5 +-- src/roadveh_cmd.cpp | 46 -------------------------- src/ship_cmd.cpp | 44 ------------------------- src/train_cmd.cpp | 46 -------------------------- src/vehicle_cmd.cpp | 78 +++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 78 insertions(+), 196 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index cfd24d5340..ecba1df29a 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -383,49 +383,6 @@ CommandCost CmdSendAircraftToHangar(TileIndex tile, DoCommandFlag flags, uint32 } -/** - * 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; diff --git a/src/command.cpp b/src/command.cpp index f9d64d8995..f95e2577b5 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -81,6 +81,7 @@ CommandProc CmdMoveRailVehicle; CommandProc CmdBuildVehicle; CommandProc CmdSellVehicle; +CommandProc CmdRefitVehicle; CommandProc CmdSendTrainToDepot; CommandProc CmdForceTrainProceed; @@ -113,14 +114,12 @@ CommandProc CmdRenameStation; CommandProc CmdRenameDepot; CommandProc CmdSendAircraftToHangar; -CommandProc CmdRefitAircraft; CommandProc CmdPlaceSign; CommandProc CmdRenameSign; CommandProc CmdSendRoadVehToDepot; CommandProc CmdTurnRoadVeh; -CommandProc CmdRefitRoadVeh; CommandProc CmdPause; @@ -138,7 +137,6 @@ CommandProc CmdChangeSetting; CommandProc CmdChangeCompanySetting; CommandProc CmdSendShipToDepot; -CommandProc CmdRefitShip; CommandProc CmdOrderRefit; CommandProc CmdCloneOrder; @@ -154,8 +152,6 @@ CommandProc CmdCompanyCtrl; CommandProc CmdLevelLand; -CommandProc CmdRefitRailVehicle; - CommandProc CmdBuildSignalTrack; CommandProc CmdRemoveSignalTrack; @@ -223,8 +219,10 @@ static const Command _command_proc_table[] = { 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 @@ static const Command _command_proc_table[] = { 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 @@ static const Command _command_proc_table[] = { 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 @@ static const Command _command_proc_table[] = { 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 diff --git a/src/command_type.h b/src/command_type.h index e43132f7de..35e2b665ac 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -179,6 +179,7 @@ enum Commands { 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 @@ enum Commands { 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 @@ enum Commands { 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 @@ enum Commands { 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 diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index b23bd8a5bc..bc8786850d 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1692,49 +1692,3 @@ Trackdir RoadVehicle::GetVehicleTrackdir() const * 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; -} diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 3e96dbefa5..f2becb60f1 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -704,47 +704,3 @@ CommandCost CmdSendShipToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, u 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; - -} diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index a2152567cf..420aa1b206 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1887,52 +1887,6 @@ CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, return CommandCost(); } -/** - * 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! diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 5cff7a90fa..0d9c5437c0 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -51,10 +51,10 @@ const uint32 _veh_sell_proc_table[] = { }; 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[] = { @@ -189,6 +189,76 @@ CommandCost CmdSellVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 return ret; } +/** + * 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