OpenTTD
depot_cmd.cpp
Go to the documentation of this file.
1 /* $Id: depot_cmd.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
2 
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  */
9 
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"
21 
22 #include "table/strings.h"
23 
24 #include "safeguards.h"
25 
31 static bool IsUniqueDepotName(const char *name)
32 {
33  const Depot *d;
34 
35  FOR_ALL_DEPOTS(d) {
36  if (d->name != NULL && strcmp(d->name, name) == 0) return false;
37  }
38 
39  return true;
40 }
41 
51 CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
52 {
53  Depot *d = Depot::GetIfValid(p1);
54  if (d == NULL) return CMD_ERROR;
55 
56  CommandCost ret = CheckTileOwnership(d->xy);
57  if (ret.Failed()) return ret;
58 
59  bool reset = StrEmpty(text);
60 
61  if (!reset) {
63  if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
64  }
65 
66  if (flags & DC_EXEC) {
67  free(d->name);
68 
69  if (reset) {
70  d->name = NULL;
71  MakeDefaultName(d);
72  } else {
73  d->name = stredup(text);
74  }
75 
76  /* Update the orders and depot */
79 
80  /* Update the depot list */
81  VehicleType vt = GetDepotVehicleType(d->xy);
83  }
84  return CommandCost();
85 }
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
static const uint MAX_LENGTH_DEPOT_NAME_CHARS
The maximum length of a depot name in characters including &#39;\0&#39;.
Definition: depot_type.h:18
The information about a vehicle list.
Definition: vehiclelist.h:31
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3120
Functions and type for generating vehicle lists.
static bool IsUniqueDepotName(const char *name)
Check whether the given name is globally unique amongst depots.
Definition: depot_cmd.cpp:31
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Depot view; Window numbers:
Definition: window_type.h:346
Base for all depots (except hangars)
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Common return value for all commands.
Definition: command_type.h:25
Functions related to the vehicle&#39;s GUIs.
CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Rename a depot.
Definition: depot_cmd.cpp:51
Vehicle orders; Window numbers:
Definition: window_type.h:207
Functions related to low-level strings.
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition: town.h:229
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:171
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
static VehicleType GetDepotVehicleType(TileIndex t)
Get the type of vehicles that can use a depot.
Definition: depot_map.h:67
bool Failed() const
Did this command fail?
Definition: command_type.h:161
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:35
execute the given command
Definition: command_type.h:342
Functions related to companies.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
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:300
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:23
Functions related to commands.
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:85
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
Base of the town class.
Window functions not directly related to making/drawing windows.
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3148