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 script_sign.cpp Implementation of ScriptSign. */
10 #include "../../stdafx.h"
11 #include "script_sign.hpp"
12 #include "table/strings.h"
13 #include "../script_instance.hpp"
14 #include "../../signs_base.h"
15 #include "../../string_func.h"
16 #include "../../strings_func.h"
17 #include "../../tile_map.h"
18 #include "../../signs_cmd.h"
20 #include "../../safeguards.h"
22 /* static */ bool ScriptSign::IsValidSign(SignID sign_id
)
24 EnforceDeityOrCompanyModeValid(false);
25 const Sign
*si
= ::Sign::GetIfValid(sign_id
);
26 return si
!= nullptr && (si
->owner
== ScriptObject::GetCompany() || si
->owner
== OWNER_DEITY
);
29 /* static */ ScriptCompany::CompanyID
ScriptSign::GetOwner(SignID sign_id
)
31 if (!IsValidSign(sign_id
)) return ScriptCompany::COMPANY_INVALID
;
33 return static_cast<ScriptCompany::CompanyID
>((int)::Sign::Get(sign_id
)->owner
);
36 /* static */ bool ScriptSign::SetName(SignID sign_id
, Text
*name
)
38 CCountedPtr
<Text
> counter(name
);
40 EnforceDeityOrCompanyModeValid(false);
41 EnforcePrecondition(false, IsValidSign(sign_id
));
42 EnforcePrecondition(false, name
!= nullptr);
43 const std::string
&text
= name
->GetDecodedText();
44 EnforcePreconditionEncodedText(false, text
);
45 EnforcePreconditionCustomError(false, ::Utf8StringLength(text
) < MAX_LENGTH_SIGN_NAME_CHARS
, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG
);
47 return ScriptObject::Command
<CMD_RENAME_SIGN
>::Do(sign_id
, text
);
50 /* static */ std::optional
<std::string
> ScriptSign::GetName(SignID sign_id
)
52 if (!IsValidSign(sign_id
)) return std::nullopt
;
54 ::SetDParam(0, sign_id
);
55 return GetString(STR_SIGN_NAME
);
58 /* static */ TileIndex
ScriptSign::GetLocation(SignID sign_id
)
60 if (!IsValidSign(sign_id
)) return INVALID_TILE
;
62 const Sign
*sign
= ::Sign::Get(sign_id
);
63 return ::TileVirtXY(sign
->x
, sign
->y
);
66 /* static */ bool ScriptSign::RemoveSign(SignID sign_id
)
68 EnforceDeityOrCompanyModeValid(false);
69 EnforcePrecondition(false, IsValidSign(sign_id
));
70 return ScriptObject::Command
<CMD_RENAME_SIGN
>::Do(sign_id
, "");
73 /* static */ SignID
ScriptSign::BuildSign(TileIndex location
, Text
*name
)
75 CCountedPtr
<Text
> counter(name
);
77 EnforceDeityOrCompanyModeValid(INVALID_SIGN
);
78 EnforcePrecondition(INVALID_SIGN
, ::IsValidTile(location
));
79 EnforcePrecondition(INVALID_SIGN
, name
!= nullptr);
80 const std::string
&text
= name
->GetDecodedText();
81 EnforcePreconditionEncodedText(INVALID_SIGN
, text
);
82 EnforcePreconditionCustomError(INVALID_SIGN
, ::Utf8StringLength(text
) < MAX_LENGTH_SIGN_NAME_CHARS
, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG
);
84 if (!ScriptObject::Command
<CMD_PLACE_SIGN
>::Do(&ScriptInstance::DoCommandReturnSignID
, location
, text
)) return INVALID_SIGN
;
86 /* In case of test-mode, we return SignID 0 */