# ai_town.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "../../stdafx.h"
    00013 #include "ai_town.hpp"
    00014 #include "ai_map.hpp"
    00015 #include "ai_cargo.hpp"
    00016 #include "ai_error.hpp"
    00017 #include "../../town.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 */ bool AITown::IsCity(TownID town_id)
    00134 {
    00135   if (!IsValidTown(town_id)) return false;
    00136
    00137   return ::Town::Get(town_id)->larger_town;
    00138 }
    00139
    00140 /* static */ int AITown::GetRoadReworkDuration(TownID town_id)
    00141 {
    00142   if (!IsValidTown(town_id)) return -1;
    00143
    00144   return ::Town::Get(town_id)->road_build_months;
    00145 }
    00146
    00147 /* static */ AICompany::CompanyID AITown::GetExclusiveRightsCompany(TownID town_id)
    00148 {
    00149   if (!IsValidTown(town_id)) return AICompany::COMPANY_INVALID;
    00150
    00151   return (AICompany::CompanyID)(int8)::Town::Get(town_id)->exclusivity;
    00152 }
    00153
    00154 /* static */ int32 AITown::GetExclusiveRightsDuration(TownID town_id)
    00155 {
    00156   if (!IsValidTown(town_id)) return -1;
    00157
    00158   return ::Town::Get(town_id)->exclusive_counter;
    00159 }
    00160
    00161 /* static */ bool AITown::IsActionAvailable(TownID town_id, TownAction town_action)
    00162 {
    00163   if (!IsValidTown(town_id)) return false;
    00164
    00165   return HasBit(::GetMaskOfTownActions(NULL, _current_company, ::Town::Get(town_id)), town_action);
    00166 }
    00167
    00168 /* static */ bool AITown::PerformTownAction(TownID town_id, TownAction town_action)
    00169 {
    00170   EnforcePrecondition(false, IsValidTown(town_id));
    00171   EnforcePrecondition(false, IsActionAvailable(town_id, town_action));
    00172
    00173   return AIObject::DoCommand(::Town::Get(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
    00174 }
    00175
    00176 /* static */ AITown::TownRating AITown::GetRating(TownID town_id, AICompany::CompanyID company_id)
    00177 {
    00178   if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
    00179   AICompany::CompanyID company = AICompany::ResolveCompanyID(company_id);
    00180   if (company == AICompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
    00181
    00182   const Town *t = ::Town::Get(town_id);
    00183   if (!HasBit(t->have_ratings, company)) {
    00184     return TOWN_RATING_NONE;
    00185   } else if (t->ratings[company] <= RATING_APPALLING) {
    00186     return TOWN_RATING_APPALLING;
    00187   } else if (t->ratings[company] <= RATING_VERYPOOR) {
    00188     return TOWN_RATING_VERY_POOR;
    00189   } else if (t->ratings[company] <= RATING_POOR) {
    00190     return TOWN_RATING_POOR;
    00191   } else if (t->ratings[company] <= RATING_MEDIOCRE) {
    00192     return TOWN_RATING_MEDIOCRE;
    00193   } else if (t->ratings[company] <= RATING_GOOD) {
    00194     return TOWN_RATING_GOOD;
    00195   } else if (t->ratings[company] <= RATING_VERYGOOD) {
    00196     return TOWN_RATING_VERY_GOOD;
    00197   } else if (t->ratings[company] <= RATING_EXCELLENT) {
    00198     return TOWN_RATING_EXCELLENT;
    00199   } else {
    00200     return TOWN_RATING_OUTSTANDING;
    00201   }
    00202 }
    00203
    00204 /* static */ int AITown::GetAllowedNoise(TownID town_id)
    00205 {
    00206   if (!IsValidTown(town_id)) return -1;
    00207
    00208   const Town *t = ::Town::Get(town_id);
    00209   if (_settings_game.economy.station_noise_level) {
    00210     return t->MaxTownNoise() - t->noise_reached;
    00211   }
    00212
    00213   int num = 0;
    00214   const Station *st;
    00215   FOR_ALL_STATIONS(st) {
    00216     if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport.type != AT_OILRIG) num++;
    00217   }
    00218   return max(0, 2 - num);
    00219 }
    00220
    00221 /* static */ AITown::RoadLayout AITown::GetRoadLayout(TownID town_id)
    00222 {
    00223   if (!IsValidTown(town_id)) return ROAD_LAYOUT_INVALID;
    00224
    00225   return (AITown::RoadLayout)((TownLayout)::Town::Get(town_id)->layout);
    00226 }