OpenTTD Source: src/depot_cmd.cpp Source File

OpenTTD Source 12.0-beta1

depot_cmd.cpp
Go to the documentation of this file.
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 */
7
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
20 #include "table/strings.h"
21
22 #include "safeguards.h"
23
29 static bool IsUniqueDepotName(const std::string &name)
30 {
31 for (const Depot *d : Depot::Iterate()) {
32 if (!d->name.empty() && d->name == name) return false;
33 }
34
35 return true;
36 }
37
47 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
48 {
49 Depot *d = Depot::GetIfValid(p1);
50 if (d == nullptr) return CMD_ERROR;
51
52 CommandCost ret = CheckTileOwnership(d->xy);
53 if (ret.Failed()) return ret;
54
55 bool reset = text.empty();
56
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);
60 }
61
62 if (flags & DC_EXEC) {
63 if (reset) {
64 d->name.clear();
65 MakeDefaultName(d);
66 } else {
67 d->name = text;
68 }
69
70 /* Update the orders and depot */
71 SetWindowClassesDirty(WC_VEHICLE_ORDERS);
72 SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
73
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());
77 }
78 return CommandCost();
79 }
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
vehicle_gui.h
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3120
command_func.h
Pool::PoolItem<&_depot_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:29
vehiclelist.h
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
town.h
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
Utf8StringLength
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition: string.cpp:417
depot_base.h
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:33
CommandCost
Common return value for all commands.
Definition: command_type.h:23
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:159
VehicleListIdentifier::Pack
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:21
safeguards.h
stdafx.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
GetWindowClassForVehicleType
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:91
string_func.h
CmdRenameDepot
CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
Rename a depot.
Definition: depot_cmd.cpp:47
MakeDefaultName
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition: town.h:239
Pool::PoolItem<&_depot_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
MAX_LENGTH_DEPOT_NAME_CHARS
static const uint MAX_LENGTH_DEPOT_NAME_CHARS
The maximum length of a depot name in characters including '\0'.
Definition: depot_type.h:16
IsUniqueDepotName
static bool IsUniqueDepotName(const std::string &name)
Check whether the given name is globally unique amongst depots.
Definition: depot_cmd.cpp:29
WC_VEHICLE_DEPOT
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
Definition: window_type.h:343
company_func.h
CheckTileOwnership
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
Definition: company_cmd.cpp:329
window_func.h
Depot
Definition: depot_base.h:19
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3146
WC_VEHICLE_ORDERS
@ WC_VEHICLE_ORDERS
Vehicle orders; Window numbers:
Definition: window_type.h:204
GetDepotVehicleType
static VehicleType GetDepotVehicleType(TileIndex t)
Get the type of vehicles that can use a depot.
Definition: depot_map.h:65