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 #include "safeguards.h"
26 /** The last built sign. */
30 * Place a sign at the given coordinates. Ownership of sign has
31 * no effect whatsoever except for the colour the sign gets for easy recognition,
32 * but everybody is able to rename/remove it.
33 * @param tile tile to place sign at
34 * @param flags type of operation
38 * @return the cost of this operation or an error
40 CommandCost
CmdPlaceSign(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
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
;
56 si
->z
= GetSlopePixelZ(x
, y
);
57 if (!StrEmpty(text
)) {
58 si
->name
= stredup(text
);
60 si
->UpdateVirtCoord();
61 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
62 _new_sign_id
= si
->index
;
69 * Rename a sign. If the new name of the sign is empty, we assume
70 * the user wanted to delete it. So delete it. Ownership of signs
71 * has no meaning/effect whatsoever except for eyecandy
73 * @param flags type of operation
74 * @param p1 index of the sign to be renamed/removed
76 * @param text the new name or an empty string when resetting to the default
77 * @return the cost of this operation or an error
79 CommandCost
CmdRenameSign(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
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
)) {
87 if (Utf8StringLength(text
) >= MAX_LENGTH_SIGN_NAME_CHARS
) return CMD_ERROR
;
89 if (flags
& DC_EXEC
) {
90 /* Delete the old name */
92 /* Assign the new one */
93 si
->name
= stredup(text
);
94 if (_game_mode
!= GM_EDITOR
) si
->owner
= _current_company
;
96 si
->UpdateVirtCoord();
97 InvalidateWindowData(WC_SIGN_LIST
, 0, 1);
99 } else { // Delete sign
100 if (flags
& DC_EXEC
) {
101 si
->sign
.MarkDirty();
104 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
108 return CommandCost();
112 * Callback function that is called after a sign is placed
113 * @param result of the operation
118 void CcPlaceSign(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
120 if (result
.Failed()) return;
122 ShowRenameSignWindow(Sign::Get(_new_sign_id
));
123 ResetObjectToPlace();
128 * PlaceProc function, called when someone pressed the button if the
129 * sign-tool is selected
130 * @param tile on which to place the sign
132 void PlaceProc_Sign(TileIndex tile
)
134 DoCommandP(tile
, 0, 0, CMD_PLACE_SIGN
| CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE
), CcPlaceSign
);