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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_basestation.cpp Implementation of ScriptBaseStation. */
|
2009-07-31 22:30:54 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_basestation.hpp"
|
2012-01-03 20:26:05 +00:00
|
|
|
#include "script_error.hpp"
|
2009-07-31 22:30:54 +00:00
|
|
|
#include "../../station_base.h"
|
|
|
|
#include "../../string_func.h"
|
|
|
|
#include "../../strings_func.h"
|
2021-11-01 20:30:34 +00:00
|
|
|
#include "../../station_cmd.h"
|
|
|
|
#include "../../waypoint_cmd.h"
|
2009-07-31 22:30:54 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptBaseStation::IsValidBaseStation(StationID station_id)
|
2009-07-31 22:30:54 +00:00
|
|
|
{
|
|
|
|
const BaseStation *st = ::BaseStation::GetIfValid(station_id);
|
2019-04-10 21:07:06 +00:00
|
|
|
return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY || st->owner == OWNER_NONE);
|
2009-07-31 22:30:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ char *ScriptBaseStation::GetName(StationID station_id)
|
2009-07-31 22:30:54 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!IsValidBaseStation(station_id)) return nullptr;
|
2009-07-31 22:30:54 +00:00
|
|
|
|
|
|
|
::SetDParam(0, station_id);
|
2012-01-08 21:48:05 +00:00
|
|
|
return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME);
|
2009-07-31 22:30:54 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 21:06:06 +00:00
|
|
|
/* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name)
|
2009-07-31 22:30:54 +00:00
|
|
|
{
|
2011-12-19 21:06:06 +00:00
|
|
|
CCountedPtr<Text> counter(name);
|
|
|
|
|
2011-12-19 21:05:36 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
|
2009-07-31 22:30:54 +00:00
|
|
|
EnforcePrecondition(false, IsValidBaseStation(station_id));
|
2019-04-10 21:07:06 +00:00
|
|
|
EnforcePrecondition(false, name != nullptr);
|
2023-02-16 01:05:54 +00:00
|
|
|
const std::string &text = name->GetDecodedText();
|
2013-02-08 20:34:27 +00:00
|
|
|
EnforcePreconditionEncodedText(false, text);
|
2011-12-19 21:06:06 +00:00
|
|
|
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_STATION_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
|
2009-07-31 22:30:54 +00:00
|
|
|
|
2021-11-01 20:30:34 +00:00
|
|
|
if (::Station::IsValidID(station_id)) {
|
2021-11-14 15:39:17 +00:00
|
|
|
return ScriptObject::Command<CMD_RENAME_STATION>::Do(station_id, text);
|
2021-11-01 20:30:34 +00:00
|
|
|
} else {
|
2021-11-14 15:39:17 +00:00
|
|
|
return ScriptObject::Command<CMD_RENAME_WAYPOINT>::Do(station_id, text);
|
2021-11-01 20:30:34 +00:00
|
|
|
}
|
2009-07-31 22:30:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptBaseStation::GetLocation(StationID station_id)
|
2009-07-31 22:30:54 +00:00
|
|
|
{
|
|
|
|
if (!IsValidBaseStation(station_id)) return INVALID_TILE;
|
|
|
|
|
|
|
|
return ::BaseStation::Get(station_id)->xy;
|
|
|
|
}
|
2009-07-31 22:38:09 +00:00
|
|
|
|
2014-02-06 19:50:34 +00:00
|
|
|
/* static */ ScriptDate::Date ScriptBaseStation::GetConstructionDate(StationID station_id)
|
2009-07-31 22:38:09 +00:00
|
|
|
{
|
2014-02-06 19:50:34 +00:00
|
|
|
if (!IsValidBaseStation(station_id)) return ScriptDate::DATE_INVALID;
|
2009-07-31 22:38:09 +00:00
|
|
|
|
2014-02-06 19:50:34 +00:00
|
|
|
return (ScriptDate::Date)::BaseStation::Get(station_id)->build_date;
|
2009-07-31 22:38:09 +00:00
|
|
|
}
|