Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / depot_cmd.cpp
blob8174be6771e92df91a395b34cd51a674ab7ba2dc
1 /* $Id: depot_cmd.cpp 25865 2013-10-13 20:11:05Z zuu $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file depot_cmd.cpp %Command Handling for depots. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "depot_base.h"
15 #include "company_func.h"
16 #include "string_func.h"
17 #include "town.h"
18 #include "vehicle_gui.h"
19 #include "vehiclelist.h"
20 #include "window_func.h"
22 #include "table/strings.h"
24 #include "safeguards.h"
26 /**
27 * Check whether the given name is globally unique amongst depots.
28 * @param name The name to check.
29 * @return True if there is no depot with the given name.
31 static bool IsUniqueDepotName(const char *name)
33 const Depot *d;
35 FOR_ALL_DEPOTS(d) {
36 if (d->name != NULL && strcmp(d->name, name) == 0) return false;
39 return true;
42 /**
43 * Rename a depot.
44 * @param tile unused
45 * @param flags type of operation
46 * @param p1 id of depot
47 * @param p2 unused
48 * @param text the new name or an empty string when resetting to the default
49 * @return the cost of this operation or an error
51 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
53 Depot *d = Depot::GetIfValid(p1);
54 if (d == NULL) return CMD_ERROR;
56 CommandCost ret = CheckTileOwnership(d->xy);
57 if (ret.Failed()) return ret;
59 bool reset = StrEmpty(text);
61 if (!reset) {
62 if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
63 if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
66 if (flags & DC_EXEC) {
67 free(d->name);
69 if (reset) {
70 d->name = NULL;
71 MakeDefaultName(d);
72 } else {
73 d->name = stredup(text);
76 /* Update the orders and depot */
77 SetWindowClassesDirty(WC_VEHICLE_ORDERS);
78 SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
80 /* Update the depot list */
81 VehicleType vt = GetDepotVehicleType(d->xy);
82 SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack());
84 return CommandCost();