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