Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / depot_cmd.cpp
blob294de69e32ea7baae7b7734ba84bdef9db81c403
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"
19 #include "depot_cmd.h"
21 #include "table/strings.h"
23 #include "safeguards.h"
25 /**
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;
36 return true;
39 /**
40 * Rename a depot.
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();
56 if (!reset) {
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) {
62 if (reset) {
63 d->name.clear();
64 MakeDefaultName(d);
65 } else {
66 d->name = text;
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());
77 return CommandCost();