# ai_town.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_town.cpp 17305 2009-08-29 11:18:03Z smatz $ */
    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 /* static */ int32 AITown::GetTownCount()
    00018 {
    00019   return ::GetNumTowns();
    00020 }
    00021
    00022 /* static */ bool AITown::IsValidTown(TownID town_id)
    00023 {
    00024   return ::IsValidTownID(town_id);
    00025 }
    00026
    00027 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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->max_pass;
    00069     case AICargo::TE_MAIL:       return t->max_mail;
    00070     default: return -1;
    00071   }
    00072 }
    00073
    00074 /* static */ 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->act_pass;
    00083     case AICargo::TE_MAIL:       return t->act_mail;
    00084     default: return -1;
    00085   }
    00086 }
    00087
    00088 /* static */ int32 AITown::GetMaxProduction(TownID town_id, CargoID cargo_id)
    00089 {
    00090   return AITown::GetLastMonthProduction(town_id, cargo_id);
    00091 }
    00092
    00093 /* static */ 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 = ::GetTown(town_id);
    00099
    00100   switch (AICargo::GetTownEffect(cargo_id)) {
    00101     case AICargo::TE_PASSENGERS: return t->pct_pass_transported * 100 >> 8;
    00102     case AICargo::TE_MAIL:       return t->pct_mail_transported * 100 >> 8;
    00103     default: return -1;
    00104   }
    00105 }
    00106
    00107 /* static */ int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
    00108 {
    00109   return AIMap::DistanceManhattan(tile, GetLocation(town_id));
    00110 }
    00111
    00112 /* static */ int32 AITown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
    00113 {
    00114   return AIMap::DistanceSquare(tile, GetLocation(town_id));
    00115 }
    00116
    00117 /* static */ bool AITown::IsWithinTownInfluence(TownID town_id, TileIndex tile)
    00118 {
    00119   if (!IsValidTown(town_id)) return false;
    00120
    00121   const Town *t = ::GetTown(town_id);
    00122   return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
    00123 }
    00124
    00125 /* static */ bool AITown::HasStatue(TownID town_id)
    00126 {
    00127   if (!IsValidTown(town_id)) return false;
    00128
    00129   return ::HasBit(::GetTown(town_id)->statues, _current_company);
    00130 }
    00131
    00132 /* static */ int AITown::GetRoadReworkDuration(TownID town_id)
    00133 {
    00134   if (!IsValidTown(town_id)) return -1;
    00135
    00136   return ::GetTown(town_id)->road_build_months;
    00137 }
    00138
    00139 /* static */ AICompany::CompanyID AITown::GetExclusiveRightsCompany(TownID town_id)
    00140 {
    00141   if (!IsValidTown(town_id)) return AICompany::COMPANY_INVALID;
    00142
    00143   return (AICompany::CompanyID)(int8)::GetTown(town_id)->exclusivity;
    00144 }
    00145
    00146 /* static */ int32 AITown::GetExclusiveRightsDuration(TownID town_id)
    00147 {
    00148   if (!IsValidTown(town_id)) return -1;
    00149
    00150   return ::GetTown(town_id)->exclusive_counter;
    00151 }
    00152
    00153 /* static */ bool AITown::IsActionAvailable(TownID town_id, TownAction town_action)
    00154 {
    00155   if (!IsValidTown(town_id)) return false;
    00156
    00157   return HasBit(::GetMaskOfTownActions(NULL, _current_company, ::GetTown(town_id)), town_action);
    00158 }
    00159
    00160 /* static */ bool AITown::PerformTownAction(TownID town_id, TownAction town_action)
    00161 {
    00162   EnforcePrecondition(false, IsValidTown(town_id));
    00163   EnforcePrecondition(false, IsActionAvailable(town_id, town_action));
    00164
    00165   return AIObject::DoCommand(::GetTown(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
    00166 }
    00167
    00168 /* static */ AITown::TownRating AITown::GetRating(TownID town_id, AICompany::CompanyID company_id)
    00169 {
    00170   if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
    00171   AICompany::CompanyID company = AICompany::ResolveCompanyID(company_id);
    00172   if (company == AICompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
    00173
    00174   const Town *t = ::GetTown(town_id);
    00175   if (!HasBit(t->have_ratings, company)) return TOWN_RATING_NONE;
    00176   return max(TOWN_RATING_APPALLING, (TownRating)((t->ratings[company] / 200) + 3));
    00177 }
    00178
    00179 /* static */ int AITown::GetAllowedNoise(TownID town_id)
    00180 {
    00181   if (!IsValidTown(town_id)) return -1;
    00182
    00183   const Town *t = ::GetTown(town_id);
    00184   if (_settings_game.economy.station_noise_level) {
    00185     return t->MaxTownNoise() - t->noise_reached;
    00186   }
    00187
    00188   int num = 0;
    00189   const Station *st;
    00190   FOR_ALL_STATIONS(st) {
    00191     if (st->town == t && st->facilities & FACIL_AIRPORT && st->airport_type != AT_OILRIG) num++;
    00192   }
    00193   return max(0, 2 - num);
    00194 }
    00195
    00196 /* static */ AITown::RoadLayout AITown::GetRoadLayout(TownID town_id)
    00197 {
    00198   if (!IsValidTown(town_id)) return ROAD_LAYOUT_INVALID;
    00199
    00200   return (AITown::RoadLayout)((TownLayout)::GetTown(town_id)->layout);
    00201 }