1 /*
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/>.
6  */
7 
8 /** @file script_basestation.cpp Implementation of ScriptBaseStation. */
9 
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 "table/strings.h"
17 
18 #include "../../safeguards.h"
19 
IsValidBaseStation(StationID station_id)20 /* static */ bool ScriptBaseStation::IsValidBaseStation(StationID station_id)
21 {
22 	const BaseStation *st = ::BaseStation::GetIfValid(station_id);
23 	return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY || st->owner == OWNER_NONE);
24 }
25 
GetName(StationID station_id)26 /* static */ char *ScriptBaseStation::GetName(StationID station_id)
27 {
28 	if (!IsValidBaseStation(station_id)) return nullptr;
29 
30 	::SetDParam(0, station_id);
31 	return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME);
32 }
33 
SetName(StationID station_id,Text * name)34 /* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name)
35 {
36 	CCountedPtr<Text> counter(name);
37 
38 	EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
39 	EnforcePrecondition(false, IsValidBaseStation(station_id));
40 	EnforcePrecondition(false, name != nullptr);
41 	const char *text = name->GetDecodedText();
42 	EnforcePreconditionEncodedText(false, text);
43 	EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_STATION_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
44 
45 	return ScriptObject::DoCommand(0, station_id, 0, ::Station::IsValidID(station_id) ? CMD_RENAME_STATION : CMD_RENAME_WAYPOINT, text);
46 }
47 
GetLocation(StationID station_id)48 /* static */ TileIndex ScriptBaseStation::GetLocation(StationID station_id)
49 {
50 	if (!IsValidBaseStation(station_id)) return INVALID_TILE;
51 
52 	return ::BaseStation::Get(station_id)->xy;
53 }
54 
GetConstructionDate(StationID station_id)55 /* static */ ScriptDate::Date ScriptBaseStation::GetConstructionDate(StationID station_id)
56 {
57 	if (!IsValidBaseStation(station_id)) return ScriptDate::DATE_INVALID;
58 
59 	return (ScriptDate::Date)::BaseStation::Get(station_id)->build_date;
60 }
61