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_tilelist.cpp Implementation of ScriptTileList and friends. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_tilelist.hpp"
|
|
|
|
#include "script_industry.hpp"
|
2009-08-30 11:47:41 +00:00
|
|
|
#include "../../industry.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "../../station_base.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
void ScriptTileList::AddRectangle(TileIndex t1, TileIndex t2)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(t1)) return;
|
|
|
|
if (!::IsValidTile(t2)) return;
|
|
|
|
|
2010-01-04 18:39:46 +00:00
|
|
|
TileArea ta(t1, t2);
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex t : ta) this->AddItem(t);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
void ScriptTileList::AddTile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return;
|
|
|
|
|
|
|
|
this->AddItem(tile);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
void ScriptTileList::RemoveRectangle(TileIndex t1, TileIndex t2)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(t1)) return;
|
|
|
|
if (!::IsValidTile(t2)) return;
|
|
|
|
|
2010-01-04 18:39:46 +00:00
|
|
|
TileArea ta(t1, t2);
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex t : ta) this->RemoveItem(t);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
void ScriptTileList::RemoveTile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return;
|
|
|
|
|
|
|
|
this->RemoveItem(tile);
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:27:57 +00:00
|
|
|
/**
|
|
|
|
* Helper to get list of tiles that will cover an industry's production or acceptance.
|
|
|
|
* @param i Industry in question
|
|
|
|
* @param radius Catchment radius to test
|
|
|
|
* @param bta BitmapTileArea to fill
|
|
|
|
*/
|
2023-03-04 18:32:14 +00:00
|
|
|
static void FillIndustryCatchment(const Industry *i, SQInteger radius, BitmapTileArea &bta)
|
2019-04-13 13:27:57 +00:00
|
|
|
{
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex cur_tile : i->location) {
|
2019-04-13 13:27:57 +00:00
|
|
|
if (!::IsTileType(cur_tile, MP_INDUSTRY) || ::GetIndustryIndex(cur_tile) != i->index) continue;
|
|
|
|
|
|
|
|
int tx = TileX(cur_tile);
|
|
|
|
int ty = TileY(cur_tile);
|
|
|
|
for (int y = -radius; y <= radius; y++) {
|
2023-01-21 09:43:03 +00:00
|
|
|
if (ty + y < 0 || ty + y > (int)Map::MaxY()) continue;
|
2019-04-13 13:27:57 +00:00
|
|
|
for (int x = -radius; x <= radius; x++) {
|
2023-01-21 09:43:03 +00:00
|
|
|
if (tx + x < 0 || tx + x > (int)Map::MaxX()) continue;
|
2019-04-13 13:27:57 +00:00
|
|
|
TileIndex tile = TileXY(tx + x, ty + y);
|
|
|
|
if (!IsValidTile(tile)) continue;
|
|
|
|
if (::IsTileType(tile, MP_INDUSTRY) && ::GetIndustryIndex(tile) == i->index) continue;
|
|
|
|
bta.SetTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 18:32:14 +00:00
|
|
|
ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID industry_id, SQInteger radius)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Industry *i = ::Industry::Get(industry_id);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2020-03-03 20:46:54 +00:00
|
|
|
/* Check if this industry is only served by its neutral station */
|
|
|
|
if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return;
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Check if this industry accepts anything */
|
2023-05-23 23:52:44 +00:00
|
|
|
if (!i->IsCargoAccepted()) return;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
|
|
|
|
|
2019-04-13 13:27:57 +00:00
|
|
|
BitmapTileArea bta(TileArea(i->location).Expand(radius));
|
|
|
|
FillIndustryCatchment(i, radius, bta);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2019-04-13 13:27:57 +00:00
|
|
|
BitmapTileIterator it(bta);
|
|
|
|
for (TileIndex cur_tile = it; cur_tile != INVALID_TILE; cur_tile = ++it) {
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
|
|
|
|
* industry triggers the acceptance). */
|
2009-06-27 21:06:58 +00:00
|
|
|
CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
|
2023-05-25 20:25:46 +00:00
|
|
|
if (std::none_of(std::begin(i->accepted), std::end(i->accepted), [&acceptance](const auto &a) { return ::IsValidCargoID(a.cargo) && acceptance[a.cargo] != 0; })) continue;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
this->AddTile(cur_tile);
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 18:32:14 +00:00
|
|
|
ScriptTileList_IndustryProducing::ScriptTileList_IndustryProducing(IndustryID industry_id, SQInteger radius)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Industry *i = ::Industry::Get(industry_id);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2020-03-03 20:46:54 +00:00
|
|
|
/* Check if this industry is only served by its neutral station */
|
|
|
|
if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return;
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Check if this industry produces anything */
|
2023-05-23 23:52:44 +00:00
|
|
|
if (!i->IsCargoProduced()) return;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
|
|
|
|
|
2019-04-13 13:27:57 +00:00
|
|
|
BitmapTileArea bta(TileArea(i->location).Expand(radius));
|
|
|
|
FillIndustryCatchment(i, radius, bta);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2019-04-13 13:27:57 +00:00
|
|
|
BitmapTileIterator it(bta);
|
|
|
|
for (TileIndex cur_tile = it; cur_tile != INVALID_TILE; cur_tile = ++it) {
|
2009-01-12 17:11:45 +00:00
|
|
|
this->AddTile(cur_tile);
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptTileList_StationType::ScriptTileList_StationType(StationID station_id, ScriptStation::StationType station_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptStation::IsValidStation(station_id)) return;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const StationRect *rect = &::Station::Get(station_id)->rect;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
uint station_type_value = 0;
|
2011-11-29 23:15:35 +00:00
|
|
|
/* Convert ScriptStation::StationType to ::StationType, but do it in a
|
2009-01-12 17:11:45 +00:00
|
|
|
* bitmask, so we can scan for multiple entries at the same time. */
|
2011-11-29 23:15:35 +00:00
|
|
|
if ((station_type & ScriptStation::STATION_TRAIN) != 0) station_type_value |= (1 << ::STATION_RAIL);
|
|
|
|
if ((station_type & ScriptStation::STATION_TRUCK_STOP) != 0) station_type_value |= (1 << ::STATION_TRUCK);
|
|
|
|
if ((station_type & ScriptStation::STATION_BUS_STOP) != 0) station_type_value |= (1 << ::STATION_BUS);
|
|
|
|
if ((station_type & ScriptStation::STATION_AIRPORT) != 0) station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG);
|
|
|
|
if ((station_type & ScriptStation::STATION_DOCK) != 0) station_type_value |= (1 << ::STATION_DOCK) | (1 << ::STATION_OILRIG);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2022-09-28 23:10:41 +00:00
|
|
|
TileArea ta(::TileXY(rect->left, rect->top), rect->Width(), rect->Height());
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex cur_tile : ta) {
|
2009-01-12 17:11:45 +00:00
|
|
|
if (!::IsTileType(cur_tile, MP_STATION)) continue;
|
|
|
|
if (::GetStationIndex(cur_tile) != station_id) continue;
|
|
|
|
if (!HasBit(station_type_value, ::GetStationType(cur_tile))) continue;
|
|
|
|
this->AddTile(cur_tile);
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|