Change: Only resort town directory window on population change if necessary
[openttd-github.git] / src / depot_cmd.cpp
bloba142fbe7d33e5723d0974c44df94614d56e96d2b
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 char *name)
31 for (const Depot *d : Depot::Iterate()) {
32 if (d->name != nullptr && strcmp(d->name, name) == 0) 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 char *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 = StrEmpty(text);
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 free(d->name);
65 if (reset) {
66 d->name = nullptr;
67 MakeDefaultName(d);
68 } else {
69 d->name = stredup(text);
72 /* Update the orders and depot */
73 SetWindowClassesDirty(WC_VEHICLE_ORDERS);
74 SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
76 /* Update the depot list */
77 VehicleType vt = GetDepotVehicleType(d->xy);
78 SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
80 return CommandCost();