Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / depot_cmd.cpp
blob6f5ddda27e2e5bb0d6271f0513f3317940c3c13d
1 /*
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/>.
6 */
8 /** @file depot_cmd.cpp %Command Handling for depots. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "depot_base.h"
13 #include "company_func.h"
14 #include "string_func.h"
15 #include "town.h"
16 #include "vehicle_gui.h"
17 #include "vehiclelist.h"
18 #include "window_func.h"
20 #include "table/strings.h"
22 #include "safeguards.h"
24 /**
25 * Check whether the given name is globally unique amongst depots.
26 * @param name The name to check.
27 * @return True if there is no depot with the given name.
29 static bool IsUniqueDepotName(const std::string &name)
31 for (const Depot *d : Depot::Iterate()) {
32 if (!d->name.empty() && d->name == name) return false;
35 return true;
38 /**
39 * Rename a depot.
40 * @param tile unused
41 * @param flags type of operation
42 * @param p1 id of depot
43 * @param p2 unused
44 * @param text the new name or an empty string when resetting to the default
45 * @return the cost of this operation or an error
47 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
49 Depot *d = Depot::GetIfValid(p1);
50 if (d == nullptr) return CMD_ERROR;
52 CommandCost ret = CheckTileOwnership(d->xy);
53 if (ret.Failed()) return ret;
55 bool reset = text.empty();
57 if (!reset) {
58 if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
59 if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
62 if (flags & DC_EXEC) {
63 if (reset) {
64 d->name.clear();
65 MakeDefaultName(d);
66 } else {
67 d->name = text;
70 /* Update the orders and depot */
71 SetWindowClassesDirty(WC_VEHICLE_ORDERS);
72 SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
74 /* Update the depot list */
75 VehicleType vt = GetDepotVehicleType(d->xy);
76 SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
78 return CommandCost();