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/>.
10 /** @file signs_cmd.cpp Handling of sign related commands. */
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 /** The last built sign. */
28 * Place a sign at the given coordinates. Ownership of sign has
29 * no effect whatsoever except for the colour the sign gets for easy recognition,
30 * but everybody is able to rename/remove it.
31 * @param tile tile to place sign at
32 * @param flags type of operation
36 * @return the cost of this operation or an error
38 CommandCost
CmdPlaceSign(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
40 /* Try to locate a new sign */
41 if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS
);
43 /* Check sign text length if any */
44 if (!StrEmpty(text
) && Utf8StringLength(text
) >= MAX_LENGTH_SIGN_NAME_CHARS
) return CMD_ERROR
;
46 /* When we execute, really make the sign */
47 if (flags
& DC_EXEC
) {
48 Sign
*si
= new Sign(_game_mode
== GM_EDITOR
? OWNER_DEITY
: _current_company
);
49 int x
= TileX(tile
) * TILE_SIZE
;
50 int y
= TileY(tile
) * TILE_SIZE
;
54 si
->z
= GetSlopePixelZ(x
, y
);
55 if (!StrEmpty(text
)) {
56 si
->name
= strdup(text
);
58 si
->UpdateVirtCoord();
59 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
60 _new_sign_id
= si
->index
;
67 * Rename a sign. If the new name of the sign is empty, we assume
68 * the user wanted to delete it. So delete it. Ownership of signs
69 * has no meaning/effect whatsoever except for eyecandy
71 * @param flags type of operation
72 * @param p1 index of the sign to be renamed/removed
74 * @param text the new name or an empty string when resetting to the default
75 * @return the cost of this operation or an error
77 CommandCost
CmdRenameSign(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
79 Sign
*si
= Sign::GetIfValid(p1
);
80 if (si
== NULL
) return CMD_ERROR
;
81 if (si
->owner
== OWNER_DEITY
&& _current_company
!= OWNER_DEITY
&& _game_mode
!= GM_EDITOR
) return CMD_ERROR
;
83 /* Rename the signs when empty, otherwise remove it */
84 if (!StrEmpty(text
)) {
85 if (Utf8StringLength(text
) >= MAX_LENGTH_SIGN_NAME_CHARS
) return CMD_ERROR
;
87 if (flags
& DC_EXEC
) {
88 /* Delete the old name */
90 /* Assign the new one */
91 si
->name
= strdup(text
);
92 if (_game_mode
!= GM_EDITOR
) si
->owner
= _current_company
;
94 si
->UpdateVirtCoord();
95 InvalidateWindowData(WC_SIGN_LIST
, 0, 1);
97 } else { // Delete sign
98 if (flags
& DC_EXEC
) {
102 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
106 return CommandCost();
110 * Callback function that is called after a sign is placed
111 * @param result of the operation
116 void CcPlaceSign(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
118 if (result
.Failed()) return;
120 ShowRenameSignWindow(Sign::Get(_new_sign_id
));
121 ResetObjectToPlace();
126 * PlaceProc function, called when someone pressed the button if the
127 * sign-tool is selected
128 * @param tile on which to place the sign
130 void PlaceProc_Sign(TileIndex tile
)
132 DoCommandP(tile
, 0, 0, CMD_PLACE_SIGN
| CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE
), CcPlaceSign
);