1 /* $Id: script_basestation.cpp 26307 2014-02-06 19:50:34Z zuu $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file script_basestation.cpp Implementation of ScriptBaseStation. */
12 #include "../../stdafx.h"
13 #include "script_basestation.hpp"
14 #include "script_error.hpp"
15 #include "../../station_base.h"
16 #include "../../string_func.h"
17 #include "../../strings_func.h"
18 #include "table/strings.h"
20 #include "../../safeguards.h"
22 /* static */ bool ScriptBaseStation::IsValidBaseStation(StationID station_id
)
24 const BaseStation
*st
= ::BaseStation::GetIfValid(station_id
);
25 return st
!= NULL
&& (st
->owner
== ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY
|| st
->owner
== OWNER_NONE
);
28 /* static */ char *ScriptBaseStation::GetName(StationID station_id
)
30 if (!IsValidBaseStation(station_id
)) return NULL
;
32 ::SetDParam(0, station_id
);
33 return GetString(::Station::IsValidID(station_id
) ? STR_STATION_NAME
: STR_WAYPOINT_NAME
);
36 /* static */ bool ScriptBaseStation::SetName(StationID station_id
, Text
*name
)
38 CCountedPtr
<Text
> counter(name
);
40 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY
);
41 EnforcePrecondition(false, IsValidBaseStation(station_id
));
42 EnforcePrecondition(false, name
!= NULL
);
43 const char *text
= name
->GetDecodedText();
44 EnforcePreconditionEncodedText(false, text
);
45 EnforcePreconditionCustomError(false, ::Utf8StringLength(text
) < MAX_LENGTH_STATION_NAME_CHARS
, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG
);
47 return ScriptObject::DoCommand(0, station_id
, 0, ::Station::IsValidID(station_id
) ? CMD_RENAME_STATION
: CMD_RENAME_WAYPOINT
, text
);
50 /* static */ TileIndex
ScriptBaseStation::GetLocation(StationID station_id
)
52 if (!IsValidBaseStation(station_id
)) return INVALID_TILE
;
54 return ::BaseStation::Get(station_id
)->xy
;
57 /* static */ ScriptDate::Date
ScriptBaseStation::GetConstructionDate(StationID station_id
)
59 if (!IsValidBaseStation(station_id
)) return ScriptDate::DATE_INVALID
;
61 return (ScriptDate::Date
)::BaseStation::Get(station_id
)->build_date
;