Go to the documentation of this file.
1 /* $Id: signs_cmd.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
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 <>.
8  */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "company_func.h"
15 #include "signs_base.h"
16 #include "signs_func.h"
17 #include "command_func.h"
18 #include "tilehighlight_func.h"
19 #include "window_func.h"
20 #include "string_func.h"
22 #include "table/strings.h"
24 #include "safeguards.h"
40 CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
41 {
42  /* Try to locate a new sign */
43  if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
45  /* Check sign text length if any */
46  if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
48  /* When we execute, really make the sign */
49  if (flags & DC_EXEC) {
50  Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
51  int x = TileX(tile) * TILE_SIZE;
52  int y = TileY(tile) * TILE_SIZE;
54  si->x = x;
55  si->y = y;
56  si->z = GetSlopePixelZ(x, y);
57  if (!StrEmpty(text)) {
58  si->name = stredup(text);
59  }
60  si->UpdateVirtCoord();
62  _new_sign_id = si->index;
63  }
65  return CommandCost();
66 }
79 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
80 {
81  Sign *si = Sign::GetIfValid(p1);
82  if (si == NULL) return CMD_ERROR;
83  if (si->owner == OWNER_DEITY && _current_company != OWNER_DEITY && _game_mode != GM_EDITOR) return CMD_ERROR;
85  /* Rename the signs when empty, otherwise remove it */
86  if (!StrEmpty(text)) {
89  if (flags & DC_EXEC) {
90  /* Delete the old name */
91  free(si->name);
92  /* Assign the new one */
93  si->name = stredup(text);
94  if (_game_mode != GM_EDITOR) si->owner = _current_company;
96  si->UpdateVirtCoord();
98  }
99  } else { // Delete sign
100  if (flags & DC_EXEC) {
101  si->sign.MarkDirty();
102  delete si;
105  }
106  }
108  return CommandCost();
109 }
118 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
119 {
120  if (result.Failed()) return;
124 }
133 {
134  DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
135 }
void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Callback function that is called after a sign is placed.
Definition: signs_cmd.cpp:118
CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Rename a sign.
Definition: signs_cmd.cpp:79
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
static const uint MAX_LENGTH_SIGN_NAME_CHARS
The maximum length of a sign name in characters including &#39;\0&#39;.
Definition: signs_type.h:21
CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Place a sign at the given coordinates.
Definition: signs_cmd.cpp:40
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Sign list; Window numbers:
Definition: window_type.h:273
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
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 signs.
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1341
The object is owned by a superuser / goal script.
Definition: company_type.h:29
Functions related to low-level strings.
void ShowRenameSignWindow(const Sign *si)
Show the window to change the text of a sign.
Definition: signs_gui.cpp:568
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.
SignID _new_sign_id
The last built sign.
Definition: signs_cmd.cpp:27
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
place a sign
Definition: command_type.h:250
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:527
bool Failed() const
Did this command fail?
Definition: command_type.h:161
void UpdateVirtCoord()
Update the coordinate of one sign.
Definition: signs.cpp:48
#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
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Functions related to OTTD&#39;s landscape.
Functions related to commands.
uint16 SignID
The type of the IDs of signs.
Definition: signs_type.h:16
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
#define CMD_MSG(x)
Used to combine a StringID with the command.
Definition: command_type.h:366
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3211
Functions related to tile highlights.
Window functions not directly related to making/drawing windows.
void PlaceProc_Sign(TileIndex tile)
PlaceProc function, called when someone pressed the button if the sign-tool is selected.
Definition: signs_cmd.cpp:132
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:3220
Base class for signs.