ai_industry.cpp

Go to the documentation of this file.
00001 /* $Id: ai_industry.cpp 17305 2009-08-29 11:18:03Z smatz $ */
00002 
00005 #include "ai_industry.hpp"
00006 #include "ai_cargo.hpp"
00007 #include "ai_map.hpp"
00008 #include "../../tile_type.h"
00009 #include "../../industry.h"
00010 #include "../../strings_func.h"
00011 #include "../../station_map.h"
00012 #include "table/strings.h"
00013 
00014 /* static */ int32 AIIndustry::GetIndustryCount()
00015 {
00016   return ::GetNumIndustries();
00017 }
00018 
00019 /* static */ bool AIIndustry::IsValidIndustry(IndustryID industry_id)
00020 {
00021   return ::IsValidIndustryID(industry_id);
00022 }
00023 
00024 /* static */ char *AIIndustry::GetName(IndustryID industry_id)
00025 {
00026   if (!IsValidIndustry(industry_id)) return NULL;
00027   static const int len = 64;
00028   char *industry_name = MallocT<char>(len);
00029 
00030   ::SetDParam(0, industry_id);
00031   ::GetString(industry_name, STR_INDUSTRY, &industry_name[len - 1]);
00032 
00033   return industry_name;
00034 }
00035 
00036 /* static */ bool AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
00037 {
00038   if (!IsValidIndustry(industry_id)) return false;
00039   if (!AICargo::IsValidCargo(cargo_id)) return false;
00040 
00041   const Industry *i = ::GetIndustry(industry_id);
00042 
00043   for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00044     if (i->accepts_cargo[j] == cargo_id) return true;
00045   }
00046 
00047   return false;
00048 }
00049 
00050 /* static */ int32 AIIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)
00051 {
00052   if (!IsValidIndustry(industry_id)) return -1;
00053   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00054 
00055   Industry *ind = ::GetIndustry(industry_id);
00056   for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
00057     CargoID cid = ind->accepts_cargo[i];
00058     if (cid == cargo_id) {
00059       return ind->incoming_cargo_waiting[i];
00060     }
00061   }
00062 
00063   return -1;
00064 }
00065 
00066 /* static */ int32 AIIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id)
00067 {
00068   if (!IsValidIndustry(industry_id)) return -1;
00069   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00070 
00071   const Industry *i = ::GetIndustry(industry_id);
00072 
00073   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00074     if (i->produced_cargo[j] == cargo_id) return i->last_month_production[j];
00075   }
00076 
00077   return -1;
00078 }
00079 
00080 /* static */ int32 AIIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id)
00081 {
00082   if (!IsValidIndustry(industry_id)) return -1;
00083   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00084 
00085   const Industry *i = ::GetIndustry(industry_id);
00086 
00087   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00088     if (i->produced_cargo[j] == cargo_id) return i->last_month_transported[j];
00089   }
00090 
00091   return -1;
00092 }
00093 
00094 /* static */ int32 AIIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id)
00095 {
00096   if (!IsValidIndustry(industry_id)) return -1;
00097   if (!AICargo::IsValidCargo(cargo_id)) return -1;
00098 
00099   const Industry *i = ::GetIndustry(industry_id);
00100 
00101   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00102     if (i->produced_cargo[j] == cargo_id) return i->last_month_pct_transported[j] * 100 >> 8;
00103   }
00104 
00105   return -1;
00106 }
00107 
00108 /* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id)
00109 {
00110   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00111 
00112   return ::GetIndustry(industry_id)->xy;
00113 }
00114 
00115 /* static */ int32 AIIndustry::GetAmountOfStationsAround(IndustryID industry_id)
00116 {
00117   if (!IsValidIndustry(industry_id)) return -1;
00118 
00119   Industry *ind = ::GetIndustry(industry_id);
00120   StationList stations;
00121 	::FindStationsAroundTiles(ind->xy, ind->width, ind->height, &stations);
00122   return (int32)stations.Length();
00123 }
00124 
00125 /* static */ int32 AIIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile)
00126 {
00127   if (!IsValidIndustry(industry_id)) return -1;
00128 
00129   return AIMap::DistanceManhattan(tile, GetLocation(industry_id));
00130 }
00131 
00132 /* static */ int32 AIIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile)
00133 {
00134   if (!IsValidIndustry(industry_id)) return -1;
00135 
00136   return AIMap::DistanceSquare(tile, GetLocation(industry_id));
00137 }
00138 
00139 /* static */ bool AIIndustry::IsBuiltOnWater(IndustryID industry_id)
00140 {
00141   if (!IsValidIndustry(industry_id)) return false;
00142 
00143   return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
00144 }
00145 
00146 /* static */ bool AIIndustry::HasHeliport(IndustryID industry_id)
00147 {
00148   if (!IsValidIndustry(industry_id)) return false;
00149 
00150   return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00151 }
00152 
00153 /* static */ TileIndex AIIndustry::GetHeliportLocation(IndustryID industry_id)
00154 {
00155   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00156   if (!HasHeliport(industry_id)) return INVALID_TILE;
00157 
00158   const Industry *ind = ::GetIndustry(industry_id);
00159   BEGIN_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00160     if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00161       return tile_cur;
00162     }
00163   END_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00164 
00165   return INVALID_TILE;
00166 }
00167 
00168 /* static */ bool AIIndustry::HasDock(IndustryID industry_id)
00169 {
00170   if (!IsValidIndustry(industry_id)) return false;
00171 
00172   return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00173 }
00174 
00175 /* static */ TileIndex AIIndustry::GetDockLocation(IndustryID industry_id)
00176 {
00177   if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00178   if (!HasDock(industry_id)) return INVALID_TILE;
00179 
00180   const Industry *ind = ::GetIndustry(industry_id);
00181   BEGIN_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00182     if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00183       return tile_cur;
00184     }
00185   END_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00186 
00187   return INVALID_TILE;
00188 }
00189 
00190 /* static */ IndustryType AIIndustry::GetIndustryType(IndustryID industry_id)
00191 {
00192   if (!IsValidIndustry(industry_id)) return INVALID_INDUSTRYTYPE;
00193 
00194   return ::GetIndustry(industry_id)->type;
00195 }

Generated on Thu Sep 24 19:35:00 2009 for OpenTTD by  doxygen 1.5.6