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