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 int32 AIIndustry::GetIndustryCount()
00015 {
00016 return ::GetNumIndustries();
00017 }
00018
00019 bool AIIndustry::IsValidIndustry(IndustryID industry_id)
00020 {
00021 return ::IsValidIndustryID(industry_id);
00022 }
00023
00024 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 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 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 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 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 TileIndex AIIndustry::GetLocation(IndustryID industry_id)
00095 {
00096 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00097
00098 return ::GetIndustry(industry_id)->xy;
00099 }
00100
00101 int32 AIIndustry::GetAmountOfStationsAround(IndustryID industry_id)
00102 {
00103 if (!IsValidIndustry(industry_id)) return -1;
00104
00105 Industry *ind = ::GetIndustry(industry_id);
00106 StationList stations;
00107 ::FindStationsAroundTiles(ind->xy, ind->width, ind->height, &stations);
00108 return (int32)stations.Length();
00109 }
00110
00111 int32 AIIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile)
00112 {
00113 if (!IsValidIndustry(industry_id)) return -1;
00114
00115 return AIMap::DistanceManhattan(tile, GetLocation(industry_id));
00116 }
00117
00118 int32 AIIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile)
00119 {
00120 if (!IsValidIndustry(industry_id)) return -1;
00121
00122 return AIMap::DistanceSquare(tile, GetLocation(industry_id));
00123 }
00124
00125 bool AIIndustry::IsBuiltOnWater(IndustryID industry_id)
00126 {
00127 if (!IsValidIndustry(industry_id)) return false;
00128
00129 return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
00130 }
00131
00132 bool AIIndustry::HasHeliport(IndustryID industry_id)
00133 {
00134 if (!IsValidIndustry(industry_id)) return false;
00135
00136 return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00137 }
00138
00139 TileIndex AIIndustry::GetHeliportLocation(IndustryID industry_id)
00140 {
00141 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00142 if (!HasHeliport(industry_id)) return INVALID_TILE;
00143
00144 const Industry *ind = ::GetIndustry(industry_id);
00145 BEGIN_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00146 if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00147 return tile_cur;
00148 }
00149 END_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00150
00151 return INVALID_TILE;
00152 }
00153
00154 bool AIIndustry::HasDock(IndustryID industry_id)
00155 {
00156 if (!IsValidIndustry(industry_id)) return false;
00157
00158 return (::GetIndustrySpec(::GetIndustry(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
00159 }
00160
00161 TileIndex AIIndustry::GetDockLocation(IndustryID industry_id)
00162 {
00163 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
00164 if (!HasDock(industry_id)) return INVALID_TILE;
00165
00166 const Industry *ind = ::GetIndustry(industry_id);
00167 BEGIN_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00168 if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00169 return tile_cur;
00170 }
00171 END_TILE_LOOP(tile_cur, ind->width, ind->height, ind->xy);
00172
00173 return INVALID_TILE;
00174 }
00175
00176 IndustryType AIIndustry::GetIndustryType(IndustryID industry_id)
00177 {
00178 if (!IsValidIndustry(industry_id)) return INVALID_INDUSTRYTYPE;
00179
00180 return ::GetIndustry(industry_id)->type;
00181 }