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_basestation.cpp Implementation of ScriptBaseStation. */
10 #include "../../stdafx.h"
11 #include "script_basestation.hpp"
12 #include "script_error.hpp"
13 #include "../../station_base.h"
14 #include "../../string_func.h"
15 #include "../../strings_func.h"
16 #include "../../station_cmd.h"
17 #include "../../waypoint_cmd.h"
18 #include "../../timer/timer_game_calendar.h"
19 #include "table/strings.h"
21 #include "../../safeguards.h"
23 /* static */ bool ScriptBaseStation::IsValidBaseStation(StationID station_id
)
25 EnforceDeityOrCompanyModeValid(false);
26 const BaseStation
*st
= ::BaseStation::GetIfValid(station_id
);
27 return st
!= nullptr && (st
->owner
== ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st
->owner
== OWNER_NONE
);
30 /* static */ std::optional
<std::string
> ScriptBaseStation::GetName(StationID station_id
)
32 if (!IsValidBaseStation(station_id
)) return std::nullopt
;
34 ::SetDParam(0, station_id
);
35 return GetString(::Station::IsValidID(station_id
) ? STR_STATION_NAME
: STR_WAYPOINT_NAME
);
38 /* static */ bool ScriptBaseStation::SetName(StationID station_id
, Text
*name
)
40 CCountedPtr
<Text
> counter(name
);
42 EnforceCompanyModeValid(false);
43 EnforcePrecondition(false, IsValidBaseStation(station_id
));
44 EnforcePrecondition(false, name
!= nullptr);
45 const std::string
&text
= name
->GetDecodedText();
46 EnforcePreconditionEncodedText(false, text
);
47 EnforcePreconditionCustomError(false, ::Utf8StringLength(text
) < MAX_LENGTH_STATION_NAME_CHARS
, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG
);
49 if (::Station::IsValidID(station_id
)) {
50 return ScriptObject::Command
<CMD_RENAME_STATION
>::Do(station_id
, text
);
52 return ScriptObject::Command
<CMD_RENAME_WAYPOINT
>::Do(station_id
, text
);
56 /* static */ TileIndex
ScriptBaseStation::GetLocation(StationID station_id
)
58 if (!IsValidBaseStation(station_id
)) return INVALID_TILE
;
60 return ::BaseStation::Get(station_id
)->xy
;
63 /* static */ ScriptDate::Date
ScriptBaseStation::GetConstructionDate(StationID station_id
)
65 if (!IsValidBaseStation(station_id
)) return ScriptDate::DATE_INVALID
;
67 return (ScriptDate::Date
)::BaseStation::Get(station_id
)->build_date
.base();