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 <http://www.gnu.org/licenses/>.
8 /** @file signs_cmd.cpp Handling of sign related commands. */
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"
22 #include "table/strings.h"
24 #include "safeguards.h"
27 * Place a sign at the given coordinates. Ownership of sign has
28 * no effect whatsoever except for the colour the sign gets for easy recognition,
29 * but everybody is able to rename/remove it.
30 * @param tile tile to place sign at
31 * @param flags type of operation
32 * @param text contents of the sign
33 * @return the cost of this operation + the ID of the new sign or an error
35 std::tuple
<CommandCost
, SignID
> CmdPlaceSign(DoCommandFlag flags
, TileIndex tile
, const std::string
&text
)
37 /* Try to locate a new sign */
38 if (!Sign::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_SIGNS
), INVALID_SIGN
};
40 /* Check sign text length if any */
41 if (Utf8StringLength(text
) >= MAX_LENGTH_SIGN_NAME_CHARS
) return { CMD_ERROR
, INVALID_SIGN
};
43 /* When we execute, really make the sign */
44 if (flags
& DC_EXEC
) {
45 Sign
*si
= new Sign(_game_mode
== GM_EDITOR
? OWNER_DEITY
: _current_company
);
46 int x
= TileX(tile
) * TILE_SIZE
;
47 int y
= TileY(tile
) * TILE_SIZE
;
51 si
->z
= GetSlopePixelZ(x
, y
);
55 si
->UpdateVirtCoord();
56 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
57 return { CommandCost(), si
->index
};
60 return { CommandCost(), INVALID_SIGN
};
64 * Rename a sign. If the new name of the sign is empty, we assume
65 * the user wanted to delete it. So delete it. Ownership of signs
66 * has no meaning/effect whatsoever except for eyecandy
67 * @param flags type of operation
68 * @param sign_id index of the sign to be renamed/removed
69 * @param text the new name or an empty string when resetting to the default
70 * @return the cost of this operation or an error
72 CommandCost
CmdRenameSign(DoCommandFlag flags
, SignID sign_id
, const std::string
&text
)
74 Sign
*si
= Sign::GetIfValid(sign_id
);
75 if (si
== nullptr) return CMD_ERROR
;
76 if (!CompanyCanRenameSign(si
)) return CMD_ERROR
;
78 /* Rename the signs when empty, otherwise remove it */
80 if (Utf8StringLength(text
) >= MAX_LENGTH_SIGN_NAME_CHARS
) return CMD_ERROR
;
82 if (flags
& DC_EXEC
) {
83 /* Assign the new one */
85 if (_game_mode
!= GM_EDITOR
) si
->owner
= _current_company
;
87 si
->UpdateVirtCoord();
88 InvalidateWindowData(WC_SIGN_LIST
, 0, 1);
90 } else { // Delete sign
91 if (flags
& DC_EXEC
) {
93 if (si
->sign
.kdtree_valid
) _viewport_sign_kdtree
.Remove(ViewportSignKdtreeItem::MakeSign(si
->index
));
96 InvalidateWindowData(WC_SIGN_LIST
, 0, 0);
100 return CommandCost();
104 * Callback function that is called after a sign is placed
105 * @param result of the operation
106 * @param new_sign ID of the placed sign.
108 void CcPlaceSign(Commands
, const CommandCost
&result
, SignID new_sign
)
110 if (result
.Failed()) return;
112 ShowRenameSignWindow(Sign::Get(new_sign
));
113 ResetObjectToPlace();
118 * PlaceProc function, called when someone pressed the button if the
119 * sign-tool is selected
120 * @param tile on which to place the sign
122 void PlaceProc_Sign(TileIndex tile
)
124 Command
<CMD_PLACE_SIGN
>::Post(STR_ERROR_CAN_T_PLACE_SIGN_HERE
, CcPlaceSign
, tile
, {});