2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-03-13 20:29:35 +00:00
|
|
|
/** @file signs_cmd.cpp Handling of sign related commands. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "landscape.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "signs_base.h"
|
|
|
|
#include "signs_func.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "tilehighlight_func.h"
|
2019-02-22 16:57:28 +00:00
|
|
|
#include "viewport_kdtree.h"
|
2009-03-13 20:29:35 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/** The last built sign. */
|
2009-03-13 20:29:35 +00:00
|
|
|
SignID _new_sign_id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place a sign at the given coordinates. Ownership of sign has
|
|
|
|
* no effect whatsoever except for the colour the sign gets for easy recognition,
|
|
|
|
* but everybody is able to rename/remove it.
|
|
|
|
* @param tile tile to place sign at
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:29:35 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
|
2009-03-13 20:29:35 +00:00
|
|
|
{
|
|
|
|
/* Try to locate a new sign */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
|
2009-03-13 20:29:35 +00:00
|
|
|
|
|
|
|
/* Check sign text length if any */
|
2010-12-05 22:25:08 +00:00
|
|
|
if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
|
2009-03-13 20:29:35 +00:00
|
|
|
|
|
|
|
/* When we execute, really make the sign */
|
|
|
|
if (flags & DC_EXEC) {
|
2012-01-22 13:54:02 +00:00
|
|
|
Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
|
2009-03-13 20:29:35 +00:00
|
|
|
int x = TileX(tile) * TILE_SIZE;
|
|
|
|
int y = TileY(tile) * TILE_SIZE;
|
|
|
|
|
|
|
|
si->x = x;
|
|
|
|
si->y = y;
|
2011-11-04 10:18:13 +00:00
|
|
|
si->z = GetSlopePixelZ(x, y);
|
2009-03-13 20:29:35 +00:00
|
|
|
if (!StrEmpty(text)) {
|
2021-05-09 20:51:26 +00:00
|
|
|
si->name = text;
|
2009-03-13 20:29:35 +00:00
|
|
|
}
|
2019-03-25 20:01:55 +00:00
|
|
|
si->UpdateVirtCoord();
|
2009-03-13 20:29:35 +00:00
|
|
|
InvalidateWindowData(WC_SIGN_LIST, 0, 0);
|
|
|
|
_new_sign_id = si->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Rename a sign. If the new name of the sign is empty, we assume
|
2009-03-13 20:29:35 +00:00
|
|
|
* the user wanted to delete it. So delete it. Ownership of signs
|
|
|
|
* has no meaning/effect whatsoever except for eyecandy
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 index of the sign to be renamed/removed
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 20:29:35 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
|
2009-03-13 20:29:35 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Sign *si = Sign::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (si == nullptr) return CMD_ERROR;
|
2021-11-28 14:16:42 +00:00
|
|
|
if (!CompanyCanRenameSign(si)) return CMD_ERROR;
|
2009-03-13 20:29:35 +00:00
|
|
|
|
|
|
|
/* Rename the signs when empty, otherwise remove it */
|
|
|
|
if (!StrEmpty(text)) {
|
2010-12-05 22:25:08 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
|
2009-03-13 20:29:35 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* Assign the new one */
|
2020-05-17 21:31:59 +00:00
|
|
|
si->name = text;
|
2012-01-22 13:54:02 +00:00
|
|
|
if (_game_mode != GM_EDITOR) si->owner = _current_company;
|
2009-03-13 20:29:35 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
si->UpdateVirtCoord();
|
2009-03-13 20:29:35 +00:00
|
|
|
InvalidateWindowData(WC_SIGN_LIST, 0, 1);
|
|
|
|
}
|
|
|
|
} else { // Delete sign
|
|
|
|
if (flags & DC_EXEC) {
|
2020-02-17 23:23:15 +00:00
|
|
|
if (HasBit(_display_opt, DO_SHOW_SIGNS) && !(si->IsCompetitorOwned() && !HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS))) {
|
|
|
|
si->sign.MarkDirty(ZOOM_LVL_DRAW_SPR);
|
|
|
|
}
|
2019-12-11 18:20:24 +00:00
|
|
|
if (_viewport_sign_kdtree_valid && si->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeSign(si->index));
|
2009-03-13 20:29:35 +00:00
|
|
|
delete si;
|
|
|
|
|
|
|
|
InvalidateWindowData(WC_SIGN_LIST, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function that is called after a sign is placed
|
2010-01-11 18:46:09 +00:00
|
|
|
* @param result of the operation
|
2009-03-13 20:29:35 +00:00
|
|
|
* @param tile unused
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2019-09-07 16:37:01 +00:00
|
|
|
* @param cmd unused
|
2009-03-13 20:29:35 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32_t p1, uint32_t p2, uint64_t p3, uint32_t cmd)
|
2009-03-13 20:29:35 +00:00
|
|
|
{
|
2010-01-11 18:46:09 +00:00
|
|
|
if (result.Failed()) return;
|
|
|
|
|
|
|
|
ShowRenameSignWindow(Sign::Get(_new_sign_id));
|
|
|
|
ResetObjectToPlace();
|
2009-03-13 20:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* PlaceProc function, called when someone pressed the button if the
|
|
|
|
* sign-tool is selected
|
|
|
|
* @param tile on which to place the sign
|
|
|
|
*/
|
|
|
|
void PlaceProc_Sign(TileIndex tile)
|
|
|
|
{
|
2009-04-21 23:40:56 +00:00
|
|
|
DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
|
2009-03-13 20:29:35 +00:00
|
|
|
}
|