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