OpenTTD Source: src/depot_cmd.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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#include "depot_cmd.h"
20
21#include "table/strings.h"
22
23#include "safeguards.h"
24
30 static bool IsUniqueDepotName(const std::string &name)
31{
32 for (const Depot *d : Depot::Iterate()) {
33 if (!d->name.empty() && d->name == name) return false;
34 }
35
36 return true;
37}
38
46 CommandCost CmdRenameDepot(DoCommandFlags flags, DepotID depot_id, const std::string &text)
47{
48 Depot *d = Depot::GetIfValid(depot_id);
49 if (d == nullptr) return CMD_ERROR;
50
51 CommandCost ret = CheckTileOwnership(d->xy);
52 if (ret.Failed()) return ret;
53
54 bool reset = text.empty();
55
56 if (!reset) {
57 if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
58 if (!IsUniqueDepotName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
59 }
60
61 if (flags.Test(DoCommandFlag::Execute)) {
62 if (reset) {
63 d->name.clear();
64 MakeDefaultName(d);
65 } else {
66 d->name = text;
67 }
68
69 /* Update the orders and depot */
70 SetWindowClassesDirty(WC_VEHICLE_ORDERS);
71 SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
72
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).ToWindowNumber());
76 }
77 return CommandCost();
78}
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CommandCost
Common return value for all commands.
Definition command_type.h:24
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition command_type.h:177
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
command_func.h
Functions related to commands.
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition command_func.h:28
DoCommandFlag::Execute
@ Execute
execute the given command
CheckTileOwnership
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
Definition company_cmd.cpp:404
company_func.h
Functions related to companies.
depot_base.h
Base for all depots (except hangars)
IsUniqueDepotName
static bool IsUniqueDepotName(const std::string &name)
Check whether the given name is globally unique amongst depots.
Definition depot_cmd.cpp:30
CmdRenameDepot
CommandCost CmdRenameDepot(DoCommandFlags flags, DepotID depot_id, const std::string &text)
Rename a depot.
Definition depot_cmd.cpp:46
depot_cmd.h
Command definitions related to depots.
GetDepotVehicleType
VehicleType GetDepotVehicleType(Tile t)
Get the type of vehicles that can use a depot.
Definition depot_map.h:78
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:18
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Utf8StringLength
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:349
string_func.h
Functions related to low-level strings.
Depot
Definition depot_base.h:20
PoolID
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
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:436
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_depot_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
VehicleListIdentifier
The information about a vehicle list.
Definition vehiclelist.h:32
VehicleListIdentifier::ToWindowNumber
WindowNumber ToWindowNumber() const
Pack a VehicleListIdentifier in 32 bits so it can be used as unique WindowNumber.
Definition vehiclelist.cpp:23
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
town.h
Base of the town class.
MakeDefaultName
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition town.h:284
vehicle_gui.h
Functions related to the vehicle's GUIs.
GetWindowClassForVehicleType
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition vehicle_gui.h:97
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
vehiclelist.h
Functions and type for generating vehicle lists.
VL_DEPOT_LIST
@ VL_DEPOT_LIST
Index is the destination (station for hangar of aircraft, depot for others)
Definition vehiclelist.h:26
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition window.cpp:3205
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3179
window_func.h
Window functions not directly related to making/drawing windows.
WC_VEHICLE_ORDERS
@ WC_VEHICLE_ORDERS
Vehicle orders; Window numbers:
Definition window_type.h:217
WC_VEHICLE_DEPOT
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
Definition window_type.h:356