2009-01-12 18:11:45 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02: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-30 00:15:35 +01:00
|
|
|
/** @file script_station.cpp Implementation of ScriptStation. */
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-01-22 11:33:16 +01:00
|
|
|
#include "../../stdafx.h"
|
2011-11-30 00:07:38 +01:00
|
|
|
#include "script_station.hpp"
|
|
|
|
#include "script_map.hpp"
|
|
|
|
#include "script_town.hpp"
|
2014-03-10 23:18:53 +01:00
|
|
|
#include "script_cargo.hpp"
|
2009-06-24 19:39:54 +02:00
|
|
|
#include "../../station_base.h"
|
2009-06-23 23:44:48 +02:00
|
|
|
#include "../../roadstop_base.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
#include "../../town.h"
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ bool ScriptStation::IsValidStation(StationID station_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2009-05-18 18:21:28 +02:00
|
|
|
const Station *st = ::Station::GetIfValid(station_id);
|
2011-12-19 22:05:25 +01:00
|
|
|
return st != NULL && (st->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY || st->owner == OWNER_NONE);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-12-19 22:00:40 +01:00
|
|
|
/* static */ ScriptCompany::CompanyID ScriptStation::GetOwner(StationID station_id)
|
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return ScriptCompany::COMPANY_INVALID;
|
|
|
|
|
|
|
|
return static_cast<ScriptCompany::CompanyID>((int)::Station::Get(station_id)->owner);
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ StationID ScriptStation::GetStationID(TileIndex tile)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2009-01-15 18:00:47 +01:00
|
|
|
if (!::IsValidTile(tile) || !::IsTileType(tile, MP_STATION)) return INVALID_STATION;
|
2009-01-12 18:11:45 +01:00
|
|
|
return ::GetStationIndex(tile);
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ int32 ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
2011-11-30 00:15:35 +01:00
|
|
|
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2013-04-13 15:42:08 +02:00
|
|
|
return ::Station::Get(station_id)->goods[cargo_id].cargo.TotalCount();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2014-03-10 23:18:53 +01:00
|
|
|
/* static */ int32 ScriptStation::GetCargoWaitingFrom(StationID station_id, StationID from_station_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
|
|
|
if (!IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return -1;
|
|
|
|
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
|
|
|
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
|
|
|
|
uint16 cargo_count = 0;
|
|
|
|
for (StationCargoList::ConstIterator it = cargo_list.Packets()->begin(); it != cargo_list.Packets()->end(); it++) {
|
|
|
|
CargoPacket *cp = *it;
|
|
|
|
if (cp->SourceStation() == from_station_id) cargo_count += cp->Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cargo_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int32 ScriptStation::GetCargoWaitingVia(StationID station_id, StationID via_station_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
|
|
|
if (!IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return -1;
|
|
|
|
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
|
|
|
|
|
|
|
|
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
|
|
|
|
uint16 cargo_count = 0;
|
2014-03-11 23:08:58 +01:00
|
|
|
std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = cargo_list.Packets()->equal_range(via_station_id);
|
2014-03-10 23:18:53 +01:00
|
|
|
for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) {
|
|
|
|
CargoPacket *cp = *it;
|
|
|
|
cargo_count += cp->Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cargo_count;
|
|
|
|
}
|
|
|
|
|
2013-04-06 14:36:33 +02:00
|
|
|
/* static */ bool ScriptStation::HasCargoRating(StationID station_id, CargoID cargo_id)
|
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return false;
|
|
|
|
if (!ScriptCargo::IsValidCargo(cargo_id)) return false;
|
|
|
|
|
|
|
|
return ::Station::Get(station_id)->goods[cargo_id].HasRating();
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ int32 ScriptStation::GetCargoRating(StationID station_id, CargoID cargo_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2013-04-06 14:36:33 +02:00
|
|
|
if (!ScriptStation::HasCargoRating(station_id, cargo_id)) return -1;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-08-27 15:47:03 +02:00
|
|
|
return ::ToPercent8(::Station::Get(station_id)->goods[cargo_id].rating);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ int32 ScriptStation::GetCoverageRadius(ScriptStation::StationType station_type)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2011-12-09 00:28:02 +01:00
|
|
|
if (station_type == STATION_AIRPORT) return -1;
|
2010-02-05 18:05:58 +01:00
|
|
|
if (!HasExactlyOneBit(station_type)) return -1;
|
2011-12-09 00:28:02 +01:00
|
|
|
|
2009-01-12 18:11:45 +01:00
|
|
|
if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
|
|
|
|
|
|
|
|
switch (station_type) {
|
|
|
|
case STATION_TRAIN: return CA_TRAIN;
|
|
|
|
case STATION_TRUCK_STOP: return CA_TRUCK;
|
|
|
|
case STATION_BUS_STOP: return CA_BUS;
|
|
|
|
case STATION_DOCK: return CA_DOCK;
|
|
|
|
default: return CA_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 00:28:43 +01:00
|
|
|
/* static */ int32 ScriptStation::GetStationCoverageRadius(StationID station_id)
|
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
|
|
|
|
|
|
|
return Station::Get(station_id)->GetCatchmentRadius();
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ int32 ScriptStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptMap::DistanceManhattan(tile, GetLocation(station_id));
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ int32 ScriptStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return -1;
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptMap::DistanceSquare(tile, GetLocation(station_id));
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ bool ScriptStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return false;
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
return ScriptTown::IsWithinTownInfluence(town_id, GetLocation(station_id));
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ bool ScriptStation::HasStationType(StationID station_id, StationType station_type)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return false;
|
2010-02-05 18:05:58 +01:00
|
|
|
if (!HasExactlyOneBit(station_type)) return false;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return (::Station::Get(station_id)->facilities & station_type) != 0;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ bool ScriptStation::HasRoadType(StationID station_id, ScriptRoad::RoadType road_type)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return false;
|
2011-11-30 00:15:35 +01:00
|
|
|
if (!ScriptRoad::IsRoadTypeAvailable(road_type)) return false;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
|
2009-01-12 18:11:45 +01:00
|
|
|
if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
|
|
|
|
}
|
2009-05-17 01:34:14 +02:00
|
|
|
for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
|
2009-01-12 18:11:45 +01:00
|
|
|
if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/* static */ TownID ScriptStation::GetNearestTown(StationID station_id)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
|
|
|
if (!IsValidStation(station_id)) return INVALID_TOWN;
|
|
|
|
|
2009-05-17 01:34:14 +02:00
|
|
|
return ::Station::Get(station_id)->town->index;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
2012-04-17 21:43:18 +02:00
|
|
|
|
|
|
|
/*static */ bool ScriptStation::IsAirportClosed(StationID station_id)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidStation(station_id));
|
|
|
|
EnforcePrecondition(false, HasStationType(station_id, STATION_AIRPORT));
|
|
|
|
|
|
|
|
return (::Station::Get(station_id)->airport.flags & AIRPORT_CLOSED_block) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static */ bool ScriptStation::OpenCloseAirport(StationID station_id)
|
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidStation(station_id));
|
|
|
|
EnforcePrecondition(false, HasStationType(station_id, STATION_AIRPORT));
|
|
|
|
|
|
|
|
return ScriptObject::DoCommand(0, station_id, 0, CMD_OPEN_CLOSE_AIRPORT);
|
|
|
|
}
|