ai_industry.cpp

Go to the documentation of this file.
00001 /* $Id: ai_industry.cpp 20399 2010-08-07 20:51:53Z yexo $ */
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_industry.hpp"
00013 #include "ai_cargo.hpp"
00014 #include "ai_map.hpp"
00015 #include "../../industry.h"
00016 #include "../../strings_func.h"
00017 #include "../../station_base.h"
00018 #include "../../newgrf_industries.h"
00019 #include "table/strings.h"
00020 
00021 /* static */ int32 AIIndustry::GetIndustryCount()
00022 {
00023   return (int32)::Industry::GetNumItems();
00024 }
00025 
00026 /* static */ bool AIIndustry::IsValidIndustry(IndustryID industry_id)
00027 {
00028   return ::Industry::IsValidID(industry_id);
00029 }
00030 
00031 /* static */ IndustryID AIIndustry::GetIndustryID(TileIndex tile)
00032 {
00033   if (!::IsValidTile(tile) || !::IsTileType(tile, MP_INDUSTRY)) return INVALID_INDUSTRY;
00034   return ::GetIndustryIndex(tile);
00035 }
00036 
00037 /* static */ char *AIIndustry::GetName(IndustryID industry_id)
00038 {
00039   if (!IsValidIndustry(industry_id)) return NULL;
00040   static const int len = 64;
00041   char *industry_name = MallocT<char>(len);
00042 
00043 	::SetDParam(0, industry_id);
00044   ::GetString(industry_name, STR_INDUSTRY_NAME, &industry_name[len - 1]);
00045 
00046   return industry_name;
00047 }
00048 
00049 /* static */ AIIndustry::CargoAcceptState AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
00050 {
00051   if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
00052   if (!AICargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED;
00053 
00054   Industry *i = ::Industry::Get(industry_id);
00055 
00056   for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00057     if (i->accepts_cargo[j] == cargo_id) {
00058       if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED;
00059       return CAS_ACCEPTED;
00060     }
00061   }
00062 
00063   return CAS_NOT_ACCEPTED;
00064 }
00065 
00066 /* static */ int32 AIIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)
00067 {
00068   if (!IsValidIndustry(industry_id)) return -1;
00069   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00070 
00071   Industry *ind = ::Industry::Get(industry_id);
00072   for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
00073     CargoID cid = ind->accepts_cargo[i];
00074     if (cid == cargo_id) {
00075       return ind->incoming_cargo_waiting[i];
00076     }
00077   }
00078 
00079   return -1;
00080 }
00081 
00082 /* static */ int32 AIIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id)
00083 {
00084   if (!IsValidIndustry(industry_id)) return -1;
00085   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00086 
00087   const Industry *i = ::Industry::Get(industry_id);
00088 
00089   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00090     if (i->produced_cargo[j] == cargo_id) return i->last_month_production[j];
00091   }
00092 
00093   return -1;
00094 }
00095 
00096 /* static */ int32 AIIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id)
00097 {
00098   if (!IsValidIndustry(industry_id)) return -1;
00099   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00100 
00101   const Industry *i = ::Industry::Get(industry_id);
00102 
00103   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00104     if (i->produced_cargo[j] == cargo_id) return i->last_month_transported[j];
00105   }
00106 
00107   return -1;
00108 }
00109 
00110 /* static */ int32 AIIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id)
00111 {
00112   if (!IsValidIndustry(industry_id)) return -1;
00113   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00114 
00115   const Industry *i = ::Industry::Get(industry_id);
00116 
00117   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00118     if (i->produced_cargo[j] == cargo_id) return ::ToPercent8(i->last_month_pct_transported[j]);
00119   }
00120 
00121   return -1;
00122 }
00123 
00124 /* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id)
00125 {
00126   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00127 
00128   return ::Industry::Get(industry_id)->location.tile;
00129 }
00130 
00131 /* static */ int32 AIIndustry::GetAmountOfStationsAround(IndustryID industry_id)
00132 {
00133   if (!IsValidIndustry(industry_id)) return -1;
00134 
00135   Industry *ind = ::Industry::Get(industry_id);
00136   StationList stations;
00137 	::FindStationsAroundTiles(ind->location, &stations);
00138   return (int32)stations.Length();
00139 }
00140 
00141 /* static */ int32 AIIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile)
00142 {
00143   if (!IsValidIndustry(industry_id)) return -1;
00144 
00145   return AIMap::DistanceManhattan(tile, GetLocation(industry_id));
00146 }
00147 
00148 /* static */ int32 AIIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile)
00149 {
00150   if (!IsValidIndustry(industry_id)) return -1;
00151 
00152   return AIMap::DistanceSquare(tile, GetLocation(industry_id));
00153 }
00154 
00155 /* static */ bool AIIndustry::IsBuiltOnWater(IndustryID industry_id)
00156 {
00157   if (!IsValidIndustry(industry_id)) return false;
00158 
00159   return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
00160 }
00161 
00162 /* static */ bool AIIndustry::HasHeliport(IndustryID industry_id)
00163 {
00164   if (!IsValidIndustry(industry_id)) return false;
00165 
00166   return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00167 }
00168 
00169 /* static */ TileIndex AIIndustry::GetHeliportLocation(IndustryID industry_id)
00170 {
00171   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00172   if (!HasHeliport(industry_id)) return INVALID_TILE;
00173 
00174   const Industry *ind = ::Industry::Get(industry_id);
00175   TILE_AREA_LOOP(tile_cur, ind->location) {
00176     if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00177       return tile_cur;
00178     }
00179   }
00180 
00181   return INVALID_TILE;
00182 }
00183 
00184 /* static */ bool AIIndustry::HasDock(IndustryID industry_id)
00185 {
00186   if (!IsValidIndustry(industry_id)) return false;
00187 
00188   return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00189 }
00190 
00191 /* static */ TileIndex AIIndustry::GetDockLocation(IndustryID industry_id)
00192 {
00193   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00194   if (!HasDock(industry_id)) return INVALID_TILE;
00195 
00196   const Industry *ind = ::Industry::Get(industry_id);
00197   TILE_AREA_LOOP(tile_cur, ind->location) {
00198     if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00199       return tile_cur;
00200     }
00201   }
00202 
00203   return INVALID_TILE;
00204 }
00205 
00206 /* static */ IndustryType AIIndustry::GetIndustryType(IndustryID industry_id)
00207 {
00208   if (!IsValidIndustry(industry_id)) return INVALID_INDUSTRYTYPE;
00209 
00210   return ::Industry::Get(industry_id)->type;
00211 }

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