# ai_town.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_town.cpp 20148 2010-07-14 19:29:13Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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 "../../town_type.h"
    00018 #include "../../strings_func.h"
    00019 #include "../../company_func.h"
    00020 #include "../../station_base.h"
    00021 #include "table/strings.h"
    00022
    00023 /* static */ int32 AITown::GetTownCount()
    00024 {
    00025   return (int32)::Town::GetNumItems();
    00026 }
    00027
    00028 /* static */ bool AITown::IsValidTown(TownID town_id)
    00029 {
    00030   return ::Town::IsValidID(town_id);
    00031 }
    00032
    00033 /* static */ char *AITown::GetName(TownID town_id)
    00034 {
    00035   if (!IsValidTown(town_id)) return NULL;
    00036   static const int len = 64;
    00037   char *town_name = MallocT<char>(len);
    00038
    00039 	::SetDParam(0, town_id);
    00040   ::GetString(town_name, STR_TOWN_NAME, &town_name[len - 1]);
    00041
    00042   return town_name;
    00043 }
    00044
    00045 /* static */ int32 AITown::GetPopulation(TownID town_id)
    00046 {
    00047   if (!IsValidTown(town_id)) return -1;
    00048   const Town *t = ::Town::Get(town_id);
    00049   return t->population;
    00050 }
    00051
    00052 /* static */ int32 AITown::GetHouseCount(TownID town_id)
    00053 {
    00054   if (!IsValidTown(town_id)) return -1;
    00055   const Town *t = ::Town::Get(town_id);
    00056   return t->num_houses;
    00057 }
    00058
    00059 /* static */ TileIndex AITown::GetLocation(TownID town_id)
    00060 {
    00061   if (!IsValidTown(town_id)) return INVALID_TILE;
    00062   const Town *t = ::Town::Get(town_id);
    00063   return t->xy;
    00064 }
    00065
    00066 /* static */ int32 AITown::GetLastMonthProduction(TownID town_id, CargoID cargo_id)
    00067 {
    00068   if (!IsValidTown(town_id)) return -1;
    00069   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00070
    00071   const Town *t = ::Town::Get(town_id);
    00072
    00073   switch (AICargo::GetTownEffect(cargo_id)) {
    00074     case AICargo::TE_PASSENGERS: return t->max_pass;
    00075     case AICargo::TE_MAIL:       return t->max_mail;
    00076     default: return -1;
    00077   }
    00078 }
    00079
    00080 /* static */ int32 AITown::GetLastMonthTransported(TownID town_id, CargoID cargo_id)
    00081 {
    00082   if (!IsValidTown(town_id)) return -1;
    00083   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00084
    00085   const Town *t = ::Town::Get(town_id);
    00086
    00087   switch (AICargo::GetTownEffect(cargo_id)) {
    00088     case AICargo::TE_PASSENGERS: return t->act_pass;
    00089     case AICargo::TE_MAIL:       return t->act_mail;
    00090     default: return -1;
    00091   }
    00092 }
    00093
    00094 /* static */ int32 AITown::GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id)
    00095 {
    00096   if (!IsValidTown(town_id)) return -1;
    00097   if (!AICargo::IsValidCargo(cargo_id)) return -1;
    00098
    00099   const Town *t = ::Town::Get(town_id);
    00100
    00101   switch (AICargo::GetTownEffect(cargo_id)) {
    00102     case AICargo::TE_PASSENGERS: return ::ToPercent8(t->pct_pass_transported);
    00103     case AICargo::TE_MAIL:       return ::ToPercent8(t->pct_mail_transported);
    00104     default: return -1;
    00105   }
    00106 }
    00107
    00108 /* static */ int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
    00109 {
    00110   return AIMap::DistanceManhattan(tile, GetLocation(town_id));
    00111 }
    00112
    00113 /* static */ int32 AITown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
    00114 {
    00115   return AIMap::DistanceSquare(tile, GetLocation(town_id));
    00116 }
    00117
    00118 /* static */ bool AITown::IsWithinTownInfluence(TownID town_id, TileIndex tile)
    00119 {
    00120   if (!IsValidTown(town_id)) return false;
    00121
    00122   const Town *t = ::Town::Get(town_id);
    00123   return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
    00124 }
    00125
    00126 /* static */ bool AITown::HasStatue(TownID town_id)
    00127 {
    00128   if (!IsValidTown(town_id)) return false;
    00129
    00130   return ::HasBit(::Town::Get(town_id)->statues, _current_company);
    00131 }
    00132
    00133 /* static */ int AITown::GetRoadReworkDuration(TownID town_id)
    00134 {
    00135   if (!IsValidTown(town_id)) return -1;
    00136
    00137   return ::Town::Get(town_id)->road_build_months;
    00138 }
    00139
    00140 /* static */ AICompany::CompanyID AITown::GetExclusiveRightsCompany(TownID town_id)
    00141 {
    00142   if (!IsValidTown(town_id)) return AICompany::COMPANY_INVALID;
    00143
    00144   return (AICompany::CompanyID)(int8)::Town::Get(town_id)->exclusivity;
    00145 }
    00146
    00147 /* static */ int32 AITown::GetExclusiveRightsDuration(TownID town_id)
    00148 {
    00149   if (!IsValidTown(town_id)) return -1;
    00150
    00151   return ::Town::Get(town_id)->exclusive_counter;
    00152 }
    00153
    00154 /* static */ bool AITown::IsActionAvailable(TownID town_id, TownAction town_action)
    00155 {
    00156   if (!IsValidTown(town_id)) return false;
    00157
    00158   return HasBit(::GetMaskOfTownActions(NULL, _current_company, ::Town::Get(town_id)), town_action);
    00159 }
    00160
    00161 /* static */ bool AITown::PerformTownAction(TownID town_id, TownAction town_action)
    00162 {
    00163   EnforcePrecondition(false, IsValidTown(town_id));
    00164   EnforcePrecondition(false, IsActionAvailable(town_id, town_action));
    00165
    00166   return AIObject::DoCommand(::Town::Get(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
    00167 }
    00168
    00169 /* static */ AITown::TownRating AITown::GetRating(TownID town_id, AICompany::CompanyID company_id)
    00170 {
    00171   if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
    00172   AICompany::CompanyID company = AICompany::ResolveCompanyID(company_id);
    00173   if (company == AICompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
    00174
    00175   const Town *t = ::Town::Get(town_id);
    00176   if (!HasBit(t->have_ratings, company)) {
    00177     return TOWN_RATING_NONE;
    00178   } else if (t->ratings[company] <= RATING_APPALLING) {
    00179     return TOWN_RATING_APPALLING;
    00180   } else if (t->ratings[company] <= RATING_VERYPOOR) {
    00181     return TOWN_RATING_VERY_POOR;
    00182   } else if (t->ratings[company] <= RATING_POOR) {
    00183     return TOWN_RATING_POOR;
    00184   } else if (t->ratings[company] <= RATING_MEDIOCRE) {
    00185     return TOWN_RATING_MEDIOCRE;
    00186   } else if (t->ratings[company] <= RATING_GOOD) {
    00187     return TOWN_RATING_GOOD;
    00188   } else if (t->ratings[company] <= RATING_VERYGOOD) {
    00189     return TOWN_RATING_VERY_GOOD;
    00190   } else if (t->ratings[company] <= RATING_EXCELLENT) {
    00191     return TOWN_RATING_EXCELLENT;
    00192   } else {
    00193     return TOWN_RATING_OUTSTANDING;
    00194   }
    00195 }
    00196
    00197 /* static */ int AITown::GetAllowedNoise(TownID town_id)
    00198 {
    00199   if (!IsValidTown(town_id)) return -1;
    00200
    00201   const Town *t = ::Town::Get(town_id);
    00202   if (_settings_game.economy.station_noise_level) {
    00203     return t->MaxTownNoise() - t->noise_reached;
    00204   }
    00205
    00206   int num = 0;
    00207   const Station *st;
    00208   FOR_ALL_STATIONS(st) {
    00209     if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport_type != AT_OILRIG) num++;
    00210   }
    00211   return max(0, 2 - num);
    00212 }
    00213
    00214 /* static */ AITown::RoadLayout AITown::GetRoadLayout(TownID town_id)
    00215 {
    00216   if (!IsValidTown(town_id)) return ROAD_LAYOUT_INVALID;
    00217
    00218   return (AITown::RoadLayout)((TownLayout)::Town::Get(town_id)->layout);
    00219 }