OpenTTD Source: src/signs_cmd.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
35std::tuple<CommandCost, SignID> CmdPlaceSign(DoCommandFlags 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), SignID::Invalid() };
39
40 /* Check sign text length if any */
41 if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return { CMD_ERROR, SignID::Invalid() };
42
43 /* When we execute, really make the sign */
44 if (flags.Test(DoCommandFlag::Execute)) {
45 int x = TileX(tile) * TILE_SIZE;
46 int y = TileY(tile) * TILE_SIZE;
47
48 Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company, x, y, GetSlopePixelZ(x, y), text);
49
50 si->UpdateVirtCoord();
51 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
52 return { CommandCost(), si->index };
53 }
54
55 return { CommandCost(), SignID::Invalid() };
56}
57
67 CommandCost CmdRenameSign(DoCommandFlags flags, SignID sign_id, const std::string &text)
68{
69 Sign *si = Sign::GetIfValid(sign_id);
70 if (si == nullptr) return CMD_ERROR;
71 if (!CompanyCanEditSign(si)) return CMD_ERROR;
72
73 /* Rename the signs when empty, otherwise remove it */
74 if (!text.empty()) {
75 if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
76
77 if (flags.Test(DoCommandFlag::Execute)) {
78 /* Assign the new one */
79 si->name = text;
80 if (_game_mode != GM_EDITOR) si->owner = _current_company;
81
82 si->UpdateVirtCoord();
83 InvalidateWindowData(WC_SIGN_LIST, 0, 1);
84 }
85 } else { // Delete sign
86 if (flags.Test(DoCommandFlag::Execute)) {
87 si->sign.MarkDirty();
88 if (si->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeSign(si->index));
89 delete si;
90
91 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
92 }
93 }
94
95 return CommandCost();
96}
97
106 CommandCost CmdMoveSign(DoCommandFlags flags, SignID sign_id, TileIndex tile)
107{
108 Sign *si = Sign::GetIfValid(sign_id);
109 if (si == nullptr) return CMD_ERROR;
110 if (!CompanyCanEditSign(si)) return CMD_ERROR;
111
112 /* Move the sign */
113 if (flags.Test(DoCommandFlag::Execute)) {
114 int x = TileX(tile) * TILE_SIZE;
115 int y = TileY(tile) * TILE_SIZE;
116
117 si->x = x;
118 si->y = y;
119 si->z = GetSlopePixelZ(x, y);
120 if (_game_mode != GM_EDITOR) si->owner = _current_company;
121
122 si->UpdateVirtCoord();
123 }
124
125 return CommandCost();
126}
127
133 void CcPlaceSign(Commands, const CommandCost &result, SignID new_sign)
134{
135 if (result.Failed()) return;
136
137 ShowRenameSignWindow(Sign::Get(new_sign));
138 ResetObjectToPlace();
139}
140
147 void PlaceProc_Sign(TileIndex tile)
148{
149 Command<CMD_PLACE_SIGN>::Post(STR_ERROR_CAN_T_PLACE_SIGN_HERE, CcPlaceSign, tile, {});
150}
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
Kdtree::Remove
void Remove(const T &element)
Remove a single element from the tree, if it exists.
Definition kdtree.hpp:415
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
Commands
Commands
List of commands.
Definition command_type.h:196
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
company_func.h
Functions related to companies.
OWNER_DEITY
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:28
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZoomLevel::Max) const
Mark the sign dirty in all viewports.
Definition viewport.cpp:1555
GetSlopePixelZ
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Definition landscape.cpp:306
landscape.h
Functions related to OTTD's landscape.
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:437
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:427
safeguards.h
A number of safeguards to prevent using unsafe methods.
CompanyCanEditSign
bool CompanyCanEditSign(const Sign *si)
Check if the current company can rename or move a given sign.
Definition signs.cpp:62
signs_base.h
Base class for signs.
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:147
CmdMoveSign
CommandCost CmdMoveSign(DoCommandFlags flags, SignID sign_id, TileIndex tile)
Move a sign to the given coordinates.
Definition signs_cmd.cpp:106
CmdPlaceSign
std::tuple< CommandCost, SignID > CmdPlaceSign(DoCommandFlags flags, TileIndex tile, const std::string &text)
Place a sign at the given coordinates.
Definition signs_cmd.cpp:35
CcPlaceSign
void CcPlaceSign(Commands, const CommandCost &result, SignID new_sign)
Callback function that is called after a sign is placed.
Definition signs_cmd.cpp:133
CmdRenameSign
CommandCost CmdRenameSign(DoCommandFlags flags, SignID sign_id, const std::string &text)
Rename a sign.
Definition signs_cmd.cpp:67
signs_cmd.h
Command definitions related to signs.
signs_func.h
Functions related to signs.
ShowRenameSignWindow
void ShowRenameSignWindow(const Sign *si)
Show the window to change the text of a sign.
Definition signs_gui.cpp:589
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:20
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.
CommandHelper
Definition command_func.h:83
PoolID< uint16_t, struct SignIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_sign_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
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:357
Pool::PoolItem<&_sign_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
Sign
Definition signs_base.h:21
Sign::UpdateVirtCoord
void UpdateVirtCoord()
Update the coordinate of one sign.
Definition signs.cpp:38
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TrackedViewportSign::kdtree_valid
bool kdtree_valid
Are the sign data valid for use with the _viewport_sign_kdtree?
Definition viewport_type.h:61
TILE_SIZE
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
tilehighlight_func.h
Functions related to tile highlights.
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition viewport.cpp:3573
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:3294
window_func.h
Window functions not directly related to making/drawing windows.
WC_SIGN_LIST
@ WC_SIGN_LIST
Sign list; Window numbers:
Definition window_type.h:283