# ai_tilelist.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_tilelist.cpp 17693 2009-10-04 17:16:41Z rubidium $ */
    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 "ai_tilelist.hpp"
    00013 #include "ai_industry.hpp"
    00014 #include "../../industry.h"
    00015 #include "../../station_base.h"
    00016
    00017 void AITileList::FixRectangleSpan(TileIndex &t1, TileIndex &t2)
    00018 {
    00019   uint x1 = ::TileX(t1);
    00020   uint x2 = ::TileX(t2);
    00021
    00022   uint y1 = ::TileY(t1);
    00023   uint y2 = ::TileY(t2);
    00024
    00025   if (x1 >= x2) ::Swap(x1, x2);
    00026   if (y1 >= y2) ::Swap(y1, y2);
    00027
    00028   t1 = ::TileXY(x1, y1);
    00029   t2 = ::TileXY(x2, y2);
    00030 }
    00031
    00032 void AITileList::AddRectangle(TileIndex t1, TileIndex t2)
    00033 {
    00034   if (!::IsValidTile(t1)) return;
    00035   if (!::IsValidTile(t2)) return;
    00036
    00037   this->FixRectangleSpan(t1, t2);
    00038
    00039   uint w = TileX(t2) - TileX(t1) + 1;
    00040   uint h = TileY(t2) - TileY(t1) + 1;
    00041
    00042   TILE_LOOP(t, w, h, t1) this->AddItem(t);
    00043 }
    00044
    00045 void AITileList::AddTile(TileIndex tile)
    00046 {
    00047   if (!::IsValidTile(tile)) return;
    00048
    00049   this->AddItem(tile);
    00050 }
    00051
    00052 void AITileList::RemoveRectangle(TileIndex t1, TileIndex t2)
    00053 {
    00054   if (!::IsValidTile(t1)) return;
    00055   if (!::IsValidTile(t2)) return;
    00056
    00057   this->FixRectangleSpan(t1, t2);
    00058
    00059   uint w = TileX(t2) - TileX(t1) + 1;
    00060   uint h = TileY(t2) - TileY(t1) + 1;
    00061
    00062   TILE_LOOP(t, w, h, t1) this->RemoveItem(t);
    00063 }
    00064
    00065 void AITileList::RemoveTile(TileIndex tile)
    00066 {
    00067   if (!::IsValidTile(tile)) return;
    00068
    00069   this->RemoveItem(tile);
    00070 }
    00071
    00072 AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_id, int radius)
    00073 {
    00074   if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
    00075
    00076   const Industry *i = ::Industry::Get(industry_id);
    00077
    00078   /* Check if this industry accepts anything */
    00079   {
    00080     bool cargo_accepts = false;
    00081     for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00082       if (i->accepts_cargo[j] != CT_INVALID) cargo_accepts = true;
    00083     }
    00084     if (!cargo_accepts) return;
    00085   }
    00086
    00087   if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
    00088
    00089   TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius)) {
    00090     if (!::IsValidTile(cur_tile)) continue;
    00091     /* Exclude all tiles that belong to this industry */
    00092     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
    00093
    00094     /* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
    00095      *  industry triggers the acceptance). */
    00096     CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
    00097     {
    00098       bool cargo_accepts = false;
    00099       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
    00100         if (i->accepts_cargo[j] != CT_INVALID && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
    00101       }
    00102       if (!cargo_accepts) continue;
    00103     }
    00104
    00105     this->AddTile(cur_tile);
    00106   }
    00107 }
    00108
    00109 AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_id, int radius)
    00110 {
    00111   if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
    00112
    00113   const Industry *i = ::Industry::Get(industry_id);
    00114
    00115   /* Check if this industry produces anything */
    00116   {
    00117     bool cargo_produces = false;
    00118     for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00119       if (i->produced_cargo[j] != CT_INVALID) cargo_produces = true;
    00120     }
    00121     if (!cargo_produces) return;
    00122   }
    00123
    00124   if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
    00125
    00126   TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius)) {
    00127     if (!::IsValidTile(cur_tile)) continue;
    00128     /* Exclude all tiles that belong to this industry */
    00129     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
    00130
    00131     /* Only add the tile if it produces the cargo (a bug in OpenTTD makes this
    00132      *  inconsitance). */
    00133     CargoArray produced = ::GetProductionAroundTiles(cur_tile, 1, 1, radius);
    00134     {
    00135       bool cargo_produces = false;
    00136       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    00137         if (i->produced_cargo[j] != CT_INVALID && produced[i->produced_cargo[j]] != 0) cargo_produces = true;
    00138       }
    00139       if (!cargo_produces) continue;
    00140     }
    00141
    00142     this->AddTile(cur_tile);
    00143   }
    00144 }
    00145
    00146 AITileList_StationType::AITileList_StationType(StationID station_id, AIStation::StationType station_type)
    00147 {
    00148   if (!AIStation::IsValidStation(station_id)) return;
    00149
    00150   const StationRect *rect = &::Station::Get(station_id)->rect;
    00151
    00152   uint station_type_value = 0;
    00153   /* Convert AIStation::StationType to ::StationType, but do it in a
    00154    *  bitmask, so we can scan for multiple entries at the same time. */
    00155   if ((station_type & AIStation::STATION_TRAIN) != 0)      station_type_value |= (1 << ::STATION_RAIL);
    00156   if ((station_type & AIStation::STATION_TRUCK_STOP) != 0) station_type_value |= (1 << ::STATION_TRUCK);
    00157   if ((station_type & AIStation::STATION_BUS_STOP) != 0)   station_type_value |= (1 << ::STATION_BUS);
    00158   if ((station_type & AIStation::STATION_AIRPORT) != 0)    station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG);
    00159   if ((station_type & AIStation::STATION_DOCK) != 0)       station_type_value |= (1 << ::STATION_DOCK)    | (1 << ::STATION_OILRIG);
    00160
    00161   TILE_LOOP(cur_tile, rect->right - rect->left + 1, rect->bottom - rect->top + 1, ::TileXY(rect->left, rect->top)) {
    00162     if (!::IsTileType(cur_tile, MP_STATION)) continue;
    00163     if (::GetStationIndex(cur_tile) != station_id) continue;
    00164     if (!HasBit(station_type_value, ::GetStationType(cur_tile))) continue;
    00165     this->AddTile(cur_tile);
    00166   }
    00167 }