ai_town.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "ai_town.hpp"
00013 #include "ai_map.hpp"
00014 #include "ai_cargo.hpp"
00015 #include "ai_error.hpp"
00016 #include "../../town.h"
00017 #include "../../strings_func.h"
00018 #include "../../company_func.h"
00019 #include "../../station_base.h"
00020 #include "table/strings.h"
00021
00022 int32 AITown::GetTownCount()
00023 {
00024 return (int32)::Town::GetNumItems();
00025 }
00026
00027 bool AITown::IsValidTown(TownID town_id)
00028 {
00029 return ::Town::IsValidID(town_id);
00030 }
00031
00032 char *AITown::GetName(TownID town_id)
00033 {
00034 if (!IsValidTown(town_id)) return NULL;
00035 static const int len = 64;
00036 char *town_name = MallocT<char>(len);
00037
00038 ::SetDParam(0, town_id);
00039 ::GetString(town_name, STR_TOWN_NAME, &town_name[len - 1]);
00040
00041 return town_name;
00042 }
00043
00044 int32 AITown::GetPopulation(TownID town_id)
00045 {
00046 if (!IsValidTown(town_id)) return -1;
00047 const Town *t = ::Town::Get(town_id);
00048 return t->population;
00049 }
00050
00051 int32 AITown::GetHouseCount(TownID town_id)
00052 {
00053 if (!IsValidTown(town_id)) return -1;
00054 const Town *t = ::Town::Get(town_id);
00055 return t->num_houses;
00056 }
00057
00058 TileIndex AITown::GetLocation(TownID town_id)
00059 {
00060 if (!IsValidTown(town_id)) return INVALID_TILE;
00061 const Town *t = ::Town::Get(town_id);
00062 return t->xy;
00063 }
00064
00065 int32 AITown::GetLastMonthProduction(TownID town_id, CargoID cargo_id)
00066 {
00067 if (!IsValidTown(town_id)) return -1;
00068 if (!AICargo::IsValidCargo(cargo_id)) return -1;
00069
00070 const Town *t = ::Town::Get(town_id);
00071
00072 switch (AICargo::GetTownEffect(cargo_id)) {
00073 case AICargo::TE_PASSENGERS: return t->max_pass;
00074 case AICargo::TE_MAIL: return t->max_mail;
00075 default: return -1;
00076 }
00077 }
00078
00079 int32 AITown::GetLastMonthTransported(TownID town_id, CargoID cargo_id)
00080 {
00081 if (!IsValidTown(town_id)) return -1;
00082 if (!AICargo::IsValidCargo(cargo_id)) return -1;
00083
00084 const Town *t = ::Town::Get(town_id);
00085
00086 switch (AICargo::GetTownEffect(cargo_id)) {
00087 case AICargo::TE_PASSENGERS: return t->act_pass;
00088 case AICargo::TE_MAIL: return t->act_mail;
00089 default: return -1;
00090 }
00091 }
00092
00093 int32 AITown::GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id)
00094 {
00095 if (!IsValidTown(town_id)) return -1;
00096 if (!AICargo::IsValidCargo(cargo_id)) return -1;
00097
00098 const Town *t = ::Town::Get(town_id);
00099
00100 switch (AICargo::GetTownEffect(cargo_id)) {
00101 case AICargo::TE_PASSENGERS: return ::ToPercent8(t->pct_pass_transported);
00102 case AICargo::TE_MAIL: return ::ToPercent8(t->pct_mail_transported);
00103 default: return -1;
00104 }
00105 }
00106
00107 int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
00108 {
00109 return AIMap::DistanceManhattan(tile, GetLocation(town_id));
00110 }
00111
00112 int32 AITown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
00113 {
00114 return AIMap::DistanceSquare(tile, GetLocation(town_id));
00115 }
00116
00117 bool AITown::IsWithinTownInfluence(TownID town_id, TileIndex tile)
00118 {
00119 if (!IsValidTown(town_id)) return false;
00120
00121 const Town *t = ::Town::Get(town_id);
00122 return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
00123 }
00124
00125 bool AITown::HasStatue(TownID town_id)
00126 {
00127 if (!IsValidTown(town_id)) return false;
00128
00129 return ::HasBit(::Town::Get(town_id)->statues, _current_company);
00130 }
00131
00132 bool AITown::IsCity(TownID town_id)
00133 {
00134 if (!IsValidTown(town_id)) return false;
00135
00136 return ::Town::Get(town_id)->larger_town;
00137 }
00138
00139 int AITown::GetRoadReworkDuration(TownID town_id)
00140 {
00141 if (!IsValidTown(town_id)) return -1;
00142
00143 return ::Town::Get(town_id)->road_build_months;
00144 }
00145
00146 AICompany::CompanyID AITown::GetExclusiveRightsCompany(TownID town_id)
00147 {
00148 if (!IsValidTown(town_id)) return AICompany::COMPANY_INVALID;
00149
00150 return (AICompany::CompanyID)(int8)::Town::Get(town_id)->exclusivity;
00151 }
00152
00153 int32 AITown::GetExclusiveRightsDuration(TownID town_id)
00154 {
00155 if (!IsValidTown(town_id)) return -1;
00156
00157 return ::Town::Get(town_id)->exclusive_counter;
00158 }
00159
00160 bool AITown::IsActionAvailable(TownID town_id, TownAction town_action)
00161 {
00162 if (!IsValidTown(town_id)) return false;
00163
00164 return HasBit(::GetMaskOfTownActions(NULL, _current_company, ::Town::Get(town_id)), town_action);
00165 }
00166
00167 bool AITown::PerformTownAction(TownID town_id, TownAction town_action)
00168 {
00169 EnforcePrecondition(false, IsValidTown(town_id));
00170 EnforcePrecondition(false, IsActionAvailable(town_id, town_action));
00171
00172 return AIObject::DoCommand(::Town::Get(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
00173 }
00174
00175 AITown::TownRating AITown::GetRating(TownID town_id, AICompany::CompanyID company_id)
00176 {
00177 if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
00178 AICompany::CompanyID company = AICompany::ResolveCompanyID(company_id);
00179 if (company == AICompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
00180
00181 const Town *t = ::Town::Get(town_id);
00182 if (!HasBit(t->have_ratings, company)) {
00183 return TOWN_RATING_NONE;
00184 } else if (t->ratings[company] <= RATING_APPALLING) {
00185 return TOWN_RATING_APPALLING;
00186 } else if (t->ratings[company] <= RATING_VERYPOOR) {
00187 return TOWN_RATING_VERY_POOR;
00188 } else if (t->ratings[company] <= RATING_POOR) {
00189 return TOWN_RATING_POOR;
00190 } else if (t->ratings[company] <= RATING_MEDIOCRE) {
00191 return TOWN_RATING_MEDIOCRE;
00192 } else if (t->ratings[company] <= RATING_GOOD) {
00193 return TOWN_RATING_GOOD;
00194 } else if (t->ratings[company] <= RATING_VERYGOOD) {
00195 return TOWN_RATING_VERY_GOOD;
00196 } else if (t->ratings[company] <= RATING_EXCELLENT) {
00197 return TOWN_RATING_EXCELLENT;
00198 } else {
00199 return TOWN_RATING_OUTSTANDING;
00200 }
00201 }
00202
00203 int AITown::GetAllowedNoise(TownID town_id)
00204 {
00205 if (!IsValidTown(town_id)) return -1;
00206
00207 const Town *t = ::Town::Get(town_id);
00208 if (_settings_game.economy.station_noise_level) {
00209 return t->MaxTownNoise() - t->noise_reached;
00210 }
00211
00212 int num = 0;
00213 const Station *st;
00214 FOR_ALL_STATIONS(st) {
00215 if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport.type != AT_OILRIG) num++;
00216 }
00217 return max(0, 2 - num);
00218 }
00219
00220 AITown::RoadLayout AITown::GetRoadLayout(TownID town_id)
00221 {
00222 if (!IsValidTown(town_id)) return ROAD_LAYOUT_INVALID;
00223
00224 return (AITown::RoadLayout)((TownLayout)::Town::Get(town_id)->layout);
00225 }