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

Generated on Fri Jun 3 05:18:48 2011 for OpenTTD by  doxygen 1.6.1