OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

signs_cmd.cpp
Go to the documentation of this file.
1 /* $Id: signs_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 "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"
21
22 #include "table/strings.h"
23
24 #include "safeguards.h"
25
27 SignID _new_sign_id;
28
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);
44
45 /* Check sign text length if any */
46 if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
47
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;
53
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();
61 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
62 _new_sign_id = si->index;
63 }
64
65 return CommandCost();
66 }
67
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;
84
85 /* Rename the signs when empty, otherwise remove it */
86 if (!StrEmpty(text)) {
87 if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
88
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;
95
96 si->UpdateVirtCoord();
97 InvalidateWindowData(WC_SIGN_LIST, 0, 1);
98 }
99 } else { // Delete sign
100 if (flags & DC_EXEC) {
101 si->sign.MarkDirty();
102 delete si;
103
104 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
105 }
106 }
107
108 return CommandCost();
109 }
110
118 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
119 {
120 if (result.Failed()) return;
121
122 ShowRenameSignWindow(Sign::Get(_new_sign_id));
123 ResetObjectToPlace();
124 }
125
132 void PlaceProc_Sign(TileIndex tile)
133 {
134 DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
135 }