# ai_tilelist.cpp

Go to the documentation of this file.

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