You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.9 KiB
C++
136 lines
4.9 KiB
C++
16 years ago
|
/* $Id$ */
|
||
|
|
||
15 years ago
|
/*
|
||
|
* 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/>.
|
||
|
*/
|
||
|
|
||
13 years ago
|
/** @file script_tilelist.cpp Implementation of AITileList and friends. */
|
||
16 years ago
|
|
||
14 years ago
|
#include "../../stdafx.h"
|
||
13 years ago
|
#include "script_tilelist.hpp"
|
||
|
#include "script_industry.hpp"
|
||
15 years ago
|
#include "../../industry.h"
|
||
15 years ago
|
#include "../../station_base.h"
|
||
16 years ago
|
|
||
|
void AITileList::AddRectangle(TileIndex t1, TileIndex t2)
|
||
|
{
|
||
|
if (!::IsValidTile(t1)) return;
|
||
|
if (!::IsValidTile(t2)) return;
|
||
|
|
||
15 years ago
|
TileArea ta(t1, t2);
|
||
|
TILE_AREA_LOOP(t, ta) this->AddItem(t);
|
||
16 years ago
|
}
|
||
|
|
||
|
void AITileList::AddTile(TileIndex tile)
|
||
|
{
|
||
|
if (!::IsValidTile(tile)) return;
|
||
|
|
||
|
this->AddItem(tile);
|
||
|
}
|
||
|
|
||
|
void AITileList::RemoveRectangle(TileIndex t1, TileIndex t2)
|
||
|
{
|
||
|
if (!::IsValidTile(t1)) return;
|
||
|
if (!::IsValidTile(t2)) return;
|
||
|
|
||
15 years ago
|
TileArea ta(t1, t2);
|
||
|
TILE_AREA_LOOP(t, ta) this->RemoveItem(t);
|
||
16 years ago
|
}
|
||
|
|
||
|
void AITileList::RemoveTile(TileIndex tile)
|
||
|
{
|
||
|
if (!::IsValidTile(tile)) return;
|
||
|
|
||
|
this->RemoveItem(tile);
|
||
|
}
|
||
|
|
||
16 years ago
|
AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_id, int radius)
|
||
16 years ago
|
{
|
||
16 years ago
|
if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
|
||
16 years ago
|
|
||
16 years ago
|
const Industry *i = ::Industry::Get(industry_id);
|
||
16 years ago
|
|
||
|
/* Check if this industry accepts anything */
|
||
|
{
|
||
|
bool cargo_accepts = false;
|
||
16 years ago
|
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
|
||
|
if (i->accepts_cargo[j] != CT_INVALID) cargo_accepts = true;
|
||
16 years ago
|
}
|
||
|
if (!cargo_accepts) return;
|
||
|
}
|
||
|
|
||
|
if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
|
||
|
|
||
14 years ago
|
TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
|
||
|
TILE_AREA_LOOP(cur_tile, ta) {
|
||
16 years ago
|
if (!::IsValidTile(cur_tile)) continue;
|
||
|
/* Exclude all tiles that belong to this industry */
|
||
|
if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
|
||
|
|
||
|
/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
|
||
|
* industry triggers the acceptance). */
|
||
15 years ago
|
CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
|
||
16 years ago
|
{
|
||
|
bool cargo_accepts = false;
|
||
16 years ago
|
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
|
||
15 years ago
|
if (i->accepts_cargo[j] != CT_INVALID && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
|
||
16 years ago
|
}
|
||
|
if (!cargo_accepts) continue;
|
||
|
}
|
||
|
|
||
|
this->AddTile(cur_tile);
|
||
15 years ago
|
}
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_id, int radius)
|
||
16 years ago
|
{
|
||
16 years ago
|
if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
|
||
16 years ago
|
|
||
16 years ago
|
const Industry *i = ::Industry::Get(industry_id);
|
||
16 years ago
|
|
||
|
/* Check if this industry produces anything */
|
||
15 years ago
|
bool cargo_produces = false;
|
||
|
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
|
||
|
if (i->produced_cargo[j] != CT_INVALID) cargo_produces = true;
|
||
16 years ago
|
}
|
||
15 years ago
|
if (!cargo_produces) return;
|
||
16 years ago
|
|
||
|
if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
|
||
|
|
||
14 years ago
|
TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
|
||
|
TILE_AREA_LOOP(cur_tile, ta) {
|
||
16 years ago
|
if (!::IsValidTile(cur_tile)) continue;
|
||
|
/* Exclude all tiles that belong to this industry */
|
||
|
if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
|
||
|
|
||
|
this->AddTile(cur_tile);
|
||
15 years ago
|
}
|
||
16 years ago
|
}
|
||
|
|
||
|
AITileList_StationType::AITileList_StationType(StationID station_id, AIStation::StationType station_type)
|
||
|
{
|
||
|
if (!AIStation::IsValidStation(station_id)) return;
|
||
|
|
||
16 years ago
|
const StationRect *rect = &::Station::Get(station_id)->rect;
|
||
16 years ago
|
|
||
|
uint station_type_value = 0;
|
||
|
/* Convert AIStation::StationType to ::StationType, but do it in a
|
||
|
* bitmask, so we can scan for multiple entries at the same time. */
|
||
|
if ((station_type & AIStation::STATION_TRAIN) != 0) station_type_value |= (1 << ::STATION_RAIL);
|
||
|
if ((station_type & AIStation::STATION_TRUCK_STOP) != 0) station_type_value |= (1 << ::STATION_TRUCK);
|
||
|
if ((station_type & AIStation::STATION_BUS_STOP) != 0) station_type_value |= (1 << ::STATION_BUS);
|
||
|
if ((station_type & AIStation::STATION_AIRPORT) != 0) station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG);
|
||
|
if ((station_type & AIStation::STATION_DOCK) != 0) station_type_value |= (1 << ::STATION_DOCK) | (1 << ::STATION_OILRIG);
|
||
|
|
||
14 years ago
|
TileArea ta(::TileXY(rect->left, rect->top), rect->right - rect->left + 1, rect->bottom - rect->top + 1);
|
||
|
TILE_AREA_LOOP(cur_tile, ta) {
|
||
16 years ago
|
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);
|
||
15 years ago
|
}
|
||
16 years ago
|
}
|