OpenTTD Source  13.2.1
signs_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 "landscape.h"
12 #include "company_func.h"
13 #include "signs_base.h"
14 #include "signs_func.h"
15 #include "command_func.h"
16 #include "tilehighlight_func.h"
17 #include "viewport_kdtree.h"
18 #include "window_func.h"
19 #include "string_func.h"
20 #include "signs_cmd.h"
21 
22 #include "table/strings.h"
23 
24 #include "safeguards.h"
25 
35 std::tuple<CommandCost, SignID> CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text)
36 {
37  /* Try to locate a new sign */
38  if (!Sign::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_SIGNS), INVALID_SIGN };
39 
40  /* Check sign text length if any */
42 
43  /* When we execute, really make the sign */
44  if (flags & DC_EXEC) {
45  Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
46  int x = TileX(tile) * TILE_SIZE;
47  int y = TileY(tile) * TILE_SIZE;
48 
49  si->x = x;
50  si->y = y;
51  si->z = GetSlopePixelZ(x, y);
52  if (!text.empty()) {
53  si->name = text;
54  }
55  si->UpdateVirtCoord();
57  return { CommandCost(), si->index };
58  }
59 
60  return { CommandCost(), INVALID_SIGN };
61 }
62 
72 CommandCost CmdRenameSign(DoCommandFlag flags, SignID sign_id, const std::string &text)
73 {
74  Sign *si = Sign::GetIfValid(sign_id);
75  if (si == nullptr) return CMD_ERROR;
76  if (!CompanyCanRenameSign(si)) return CMD_ERROR;
77 
78  /* Rename the signs when empty, otherwise remove it */
79  if (!text.empty()) {
81 
82  if (flags & DC_EXEC) {
83  /* Assign the new one */
84  si->name = text;
85  if (_game_mode != GM_EDITOR) si->owner = _current_company;
86 
87  si->UpdateVirtCoord();
89  }
90  } else { // Delete sign
91  if (flags & DC_EXEC) {
92  si->sign.MarkDirty();
93  if (si->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeSign(si->index));
94  delete si;
95 
97  }
98  }
99 
100  return CommandCost();
101 }
102 
109 void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign)
110 {
111  if (result.Failed()) return;
112 
113  ShowRenameSignWindow(Sign::Get(new_sign));
115 }
116 
124 {
125  Command<CMD_PLACE_SIGN>::Post(STR_ERROR_CAN_T_PLACE_SIGN_HERE, CcPlaceSign, tile, {});
126 }
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3254
Pool::PoolItem<&_sign_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
command_func.h
Pool::PoolItem<&_sign_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:28
Commands
Commands
List of commands.
Definition: command_type.h:176
signs_func.h
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
MAX_LENGTH_SIGN_NAME_CHARS
static const uint MAX_LENGTH_SIGN_NAME_CHARS
The maximum length of a sign name in characters including '\0'.
Definition: signs_type.h:19
WC_SIGN_LIST
@ WC_SIGN_LIST
Sign list; Window numbers:
Definition: window_type.h:271
CmdPlaceSign
std::tuple< CommandCost, SignID > CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text)
Place a sign at the given coordinates.
Definition: signs_cmd.cpp:35
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:357
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:355
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
Kdtree::Remove
void Remove(const T &element)
Remove a single element from the tree, if it exists.
Definition: kdtree.hpp:419
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:435
signs_cmd.h
CommandCost
Common return value for all commands.
Definition: command_type.h:24
tilehighlight_func.h
Sign::UpdateVirtCoord
void UpdateVirtCoord()
Update the coordinate of one sign.
Definition: signs.cpp:46
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:160
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1478
INVALID_SIGN
static const SignID INVALID_SIGN
Sentinel for an invalid sign.
Definition: signs_type.h:17
safeguards.h
CompanyCanRenameSign
bool CompanyCanRenameSign(const Sign *si)
Check if the current company can rename a given sign.
Definition: signs.cpp:71
stdafx.h
landscape.h
string_func.h
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
TrackedViewportSign::kdtree_valid
bool kdtree_valid
Are the sign data valid for use with the _viewport_sign_kdtree?
Definition: viewport_type.h:50
Pool::PoolItem<&_sign_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
Sign
Definition: signs_base.h:22
PlaceProc_Sign
void PlaceProc_Sign(TileIndex tile)
PlaceProc function, called when someone pressed the button if the sign-tool is selected.
Definition: signs_cmd.cpp:123
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
company_func.h
CommandHelper
Definition: command_func.h:94
window_func.h
ShowRenameSignWindow
void ShowRenameSignWindow(const Sign *si)
Show the window to change the text of a sign.
Definition: signs_gui.cpp:584
CmdRenameSign
CommandCost CmdRenameSign(DoCommandFlag flags, SignID sign_id, const std::string &text)
Rename a sign.
Definition: signs_cmd.cpp:72
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition: viewport.cpp:3434
signs_base.h
CcPlaceSign
void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:109
SignID
uint16 SignID
The type of the IDs of signs.
Definition: signs_type.h:14