ai_tilelist.cpp

Go to the documentation of this file.
00001 /* $Id: ai_tilelist.cpp 21488 2010-12-12 18:47:08Z 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   TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
00067   TILE_AREA_LOOP(cur_tile, ta) {
00068     if (!::IsValidTile(cur_tile)) continue;
00069     /* Exclude all tiles that belong to this industry */
00070     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
00071 
00072     /* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
00073      *  industry triggers the acceptance). */
00074     CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
00075     {
00076       bool cargo_accepts = false;
00077       for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00078         if (i->accepts_cargo[j] != CT_INVALID && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
00079       }
00080       if (!cargo_accepts) continue;
00081     }
00082 
00083     this->AddTile(cur_tile);
00084   }
00085 }
00086 
00087 AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_id, int radius)
00088 {
00089   if (!AIIndustry::IsValidIndustry(industry_id) || radius <= 0) return;
00090 
00091   const Industry *i = ::Industry::Get(industry_id);
00092 
00093   /* Check if this industry produces anything */
00094   bool cargo_produces = false;
00095   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00096     if (i->produced_cargo[j] != CT_INVALID) cargo_produces = true;
00097   }
00098   if (!cargo_produces) return;
00099 
00100   if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
00101 
00102   TileArea ta(i->location.tile - ::TileDiffXY(radius, radius), i->location.w + radius * 2, i->location.h + radius * 2);
00103   TILE_AREA_LOOP(cur_tile, ta) {
00104     if (!::IsValidTile(cur_tile)) continue;
00105     /* Exclude all tiles that belong to this industry */
00106     if (::IsTileType(cur_tile, MP_INDUSTRY) && ::GetIndustryIndex(cur_tile) == industry_id) continue;
00107 
00108     this->AddTile(cur_tile);
00109   }
00110 }
00111 
00112 AITileList_StationType::AITileList_StationType(StationID station_id, AIStation::StationType station_type)
00113 {
00114   if (!AIStation::IsValidStation(station_id)) return;
00115 
00116   const StationRect *rect = &::Station::Get(station_id)->rect;
00117 
00118   uint station_type_value = 0;
00119   /* Convert AIStation::StationType to ::StationType, but do it in a
00120    *  bitmask, so we can scan for multiple entries at the same time. */
00121   if ((station_type & AIStation::STATION_TRAIN) != 0)      station_type_value |= (1 << ::STATION_RAIL);
00122   if ((station_type & AIStation::STATION_TRUCK_STOP) != 0) station_type_value |= (1 << ::STATION_TRUCK);
00123   if ((station_type & AIStation::STATION_BUS_STOP) != 0)   station_type_value |= (1 << ::STATION_BUS);
00124   if ((station_type & AIStation::STATION_AIRPORT) != 0)    station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG);
00125   if ((station_type & AIStation::STATION_DOCK) != 0)       station_type_value |= (1 << ::STATION_DOCK)    | (1 << ::STATION_OILRIG);
00126 
00127   TileArea ta(::TileXY(rect->left, rect->top), rect->right - rect->left + 1, rect->bottom - rect->top + 1);
00128   TILE_AREA_LOOP(cur_tile, ta) {
00129     if (!::IsTileType(cur_tile, MP_STATION)) continue;
00130     if (::GetStationIndex(cur_tile) != station_id) continue;
00131     if (!HasBit(station_type_value, ::GetStationType(cur_tile))) continue;
00132     this->AddTile(cur_tile);
00133   }
00134 }

Generated on Sun Jan 9 16:01:51 2011 for OpenTTD by  doxygen 1.6.1