# ai_tilelist.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #include "ai_tilelist.hpp"
    00014 #include "ai_industry.hpp"
    00015 #include "../../industry.h"
    00016 #include "../../station_base.h"
    00017
    00018 void AITileList::AddRectangle(TileIndex t1, TileIndex t2)
    00019 {
    00020   if (!::IsValidTile(t1)) return;
    00021   if (!::IsValidTile(t2)) return;
    00022
    00023   TileArea ta(t1, t2);
    00024   TILE_AREA_LOOP(t, ta) this->AddItem(t);
    00025 }
    00026
    00027 void AITileList::AddTile(TileIndex tile)
    00028 {
    00029   if (!::IsValidTile(tile)) return;
    00030
    00031   this->AddItem(tile);
    00032 }
    00033
    00034 void AITileList::RemoveRectangle(TileIndex t1, TileIndex t2)
    00035 {
    00036   if (!::IsValidTile(t1)) return;
    00037   if (!::IsValidTile(t2)) return;
    00038
    00039   TileArea ta(t1, t2);
    00040   TILE_AREA_LOOP(t, ta) this->RemoveItem(t);
    00041 }
    00042
    00043 void AITileList::RemoveTile(TileIndex tile)
    00044 {
    00045   if (!::IsValidTile(tile)) return;
    00046
    00047   this->RemoveItem(tile);
    00048 }
    00049
    00050 AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_id, int radius)
    00051 {
    00052   if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
    00053
    00054   const Industry *i = ::Industry::Get(industry_id);
    00055
    00056   /* Check if this industry accepts anything */
    00057   {
    00058     bool cargo_accepts = false;
    00059     for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00060       if (i->accepts_cargo[j] != CT_INVALID) cargo_accepts = true;
    00061     }
    00062     if (!cargo_accepts) return;
    00063   }
    00064
    00065   if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
    00066
    00067   TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
    00068   TILE_AREA_LOOP(cur_tile, ta) {
    00069     if (!::IsValidTile(cur_tile)) continue;
    00070     /* Exclude all tiles that belong to this industry */
    00071     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
    00072
    00073     /* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
    00074      *  industry triggers the acceptance). */
    00075     CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
    00076     {
    00077       bool cargo_accepts = false;
    00078       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00079         if (i->accepts_cargo[j] != CT_INVALID && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
    00080       }
    00081       if (!cargo_accepts) continue;
    00082     }
    00083
    00084     this->AddTile(cur_tile);
    00085   }
    00086 }
    00087
    00088 AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_id, int radius)
    00089 {
    00090   if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
    00091
    00092   const Industry *i = ::Industry::Get(industry_id);
    00093
    00094   /* Check if this industry produces anything */
    00095   bool cargo_produces = false;
    00096   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00097     if (i->produced_cargo[j] != CT_INVALID) cargo_produces = true;
    00098   }
    00099   if (!cargo_produces) return;
    00100
    00101   if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
    00102
    00103   TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
    00104   TILE_AREA_LOOP(cur_tile, ta) {
    00105     if (!::IsValidTile(cur_tile)) continue;
    00106     /* Exclude all tiles that belong to this industry */
    00107     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
    00108
    00109     this->AddTile(cur_tile);
    00110   }
    00111 }
    00112
    00113 AITileList_StationType::AITileList_StationType(StationID station_id, AIStation::StationType station_type)
    00114 {
    00115   if (!AIStation::IsValidStation(station_id)) return;
    00116
    00117   const StationRect *rect = &::Station::Get(station_id)->rect;
    00118
    00119   uint station_type_value = 0;
    00120   /* Convert AIStation::StationType to ::StationType, but do it in a
    00121    *  bitmask, so we can scan for multiple entries at the same time. */
    00122   if ((station_type & AIStation::STATION_TRAIN) != 0)      station_type_value |= (1 << ::STATION_RAIL);
    00123   if ((station_type & AIStation::STATION_TRUCK_STOP) != 0) station_type_value |= (1 << ::STATION_TRUCK);
    00124   if ((station_type & AIStation::STATION_BUS_STOP) != 0)   station_type_value |= (1 << ::STATION_BUS);
    00125   if ((station_type & AIStation::STATION_AIRPORT) != 0)    station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG);
    00126   if ((station_type & AIStation::STATION_DOCK) != 0)       station_type_value |= (1 << ::STATION_DOCK)    | (1 << ::STATION_OILRIG);
    00127
    00128   TileArea ta(::TileXY(rect->left, rect->top), rect->right - rect->left + 1, rect->bottom - rect->top + 1);
    00129   TILE_AREA_LOOP(cur_tile, ta) {
    00130     if (!::IsTileType(cur_tile, MP_STATION)) continue;
    00131     if (::GetStationIndex(cur_tile) != station_id) continue;
    00132     if (!HasBit(station_type_value, ::GetStationType(cur_tile))) continue;
    00133     this->AddTile(cur_tile);
    00134   }
    00135 }