2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file depot_cmd.cpp %Command Handling for depots. */
11 #include "command_func.h"
12 #include "depot_base.h"
13 #include "company_func.h"
14 #include "string_func.h"
16 #include "vehicle_gui.h"
17 #include "vehiclelist.h"
18 #include "window_func.h"
19 #include "depot_cmd.h"
21 #include "table/strings.h"
23 #include "safeguards.h"
26 * Check whether the given name is globally unique amongst depots.
27 * @param name The name to check.
28 * @return True if there is no depot with the given name.
30 static bool IsUniqueDepotName(const std::string
&name
)
32 for (const Depot
*d
: Depot::Iterate()) {
33 if (!d
->name
.empty() && d
->name
== name
) return false;
41 * @param flags type of operation
42 * @param depot_id id of depot
43 * @param text the new name or an empty string when resetting to the default
44 * @return the cost of this operation or an error
46 CommandCost
CmdRenameDepot(DoCommandFlag flags
, DepotID depot_id
, const std::string
&text
)
48 Depot
*d
= Depot::GetIfValid(depot_id
);
49 if (d
== nullptr) return CMD_ERROR
;
51 CommandCost ret
= CheckTileOwnership(d
->xy
);
52 if (ret
.Failed()) return ret
;
54 bool reset
= text
.empty();
57 if (Utf8StringLength(text
) >= MAX_LENGTH_DEPOT_NAME_CHARS
) return CMD_ERROR
;
58 if (!IsUniqueDepotName(text
)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE
);
61 if (flags
& DC_EXEC
) {
69 /* Update the orders and depot */
70 SetWindowClassesDirty(WC_VEHICLE_ORDERS
);
71 SetWindowDirty(WC_VEHICLE_DEPOT
, d
->xy
);
73 /* Update the depot list */
74 VehicleType vt
= GetDepotVehicleType(d
->xy
);
75 SetWindowDirty(GetWindowClassForVehicleType(vt
), VehicleListIdentifier(VL_DEPOT_LIST
, vt
, GetTileOwner(d
->xy
), d
->index
).Pack());