From daf5a2f1bc93c13c459b29ddf6f99751691d95d4 Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 11 Apr 2008 08:40:10 +0000 Subject: (svn r12658) -Codechange: unify a vast portion of the CmdSendToDepot commands. --- src/aircraft_cmd.cpp | 58 ++++------------------------------------------ src/roadveh_cmd.cpp | 53 +++--------------------------------------- src/ship_cmd.cpp | 52 +++-------------------------------------- src/train_cmd.cpp | 50 ++-------------------------------------- src/vehicle.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vehicle_base.h | 8 +++++++ src/vehicle_func.h | 8 ------- src/vehicle_type.h | 10 ++++++++ 8 files changed, 95 insertions(+), 209 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 0fc9b3a3a..29da2d603 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -80,7 +80,7 @@ static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc); 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 @@ CommandCost CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uin 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++; - - 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 (v->type != VEH_AIRCRAFT) return CMD_ERROR; - 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 @@ static void AircraftLandAirplane(Vehicle *v) /** 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)) diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 3c90018be..2946753cd 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -468,9 +468,6 @@ bool RoadVehicle::FindClosestDepot(TileIndex *location, DestinationID *destinati */ 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 @@ CommandCost CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint3 if (!IsValidVehicleID(p1)) return CMD_ERROR; - v = GetVehicle(p1); + Vehicle *v = GetVehicle(p1); - if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v->type != VEH_ROAD) 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(); - } - - 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(); - } - - 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. diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index bcd04025f..6d55a3658 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -934,9 +934,6 @@ bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, boo */ 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 @@ CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p 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; + Vehicle *v = GetVehicle(p1); - if (v->IsInDepot()) return CMD_ERROR; + if (v->type != VEH_SHIP) 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(); - } - - 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(); - } - - 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)); } diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 3813178d5..39236c261 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -2116,55 +2116,9 @@ CommandCost CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 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 (v->type != VEH_TRAIN) return CMD_ERROR; - 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(); + return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 89cfe1b5b..c6cf4de77 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -3209,6 +3209,71 @@ void Vehicle::HandleLoading(bool mode) 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) { diff --git a/src/vehicle_base.h b/src/vehicle_base.h index ac415fa64..55966821f 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -522,6 +522,14 @@ public: * @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); }; /** diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 460f30f5e..a884e8fd9 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -76,14 +76,6 @@ bool CanBuildVehicleInfrastructure(VehicleType type); 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; diff --git a/src/vehicle_type.h b/src/vehicle_type.h index 629c7bb3e..1fbd3da04 100644 --- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -60,4 +60,14 @@ enum { 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 */ -- cgit v1.2.3-70-g09d2