ai_town.cpp

Go to the documentation of this file.
00001 /* $Id: ai_town.cpp 17693 2009-10-04 17:16:41Z 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 "../../strings_func.h"
00018 #include "../../company_func.h"
00019 #include "../../station_base.h"
00020 #include "table/strings.h"
00021 
00022 /* static */ int32 AITown::GetTownCount()
00023 {
00024   return (int32)::Town::GetNumItems();
00025 }
00026 
00027 /* static */ bool AITown::IsValidTown(TownID town_id)
00028 {
00029   return ::Town::IsValidID(town_id);
00030 }
00031 
00032 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* 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 = ::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 /* 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 = ::Town::Get(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(::Town::Get(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 ::Town::Get(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)::Town::Get(town_id)->exclusivity;
00144 }
00145 
00146 /* static */ int32 AITown::GetExclusiveRightsDuration(TownID town_id)
00147 {
00148   if (!IsValidTown(town_id)) return -1;
00149 
00150   return ::Town::Get(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, ::Town::Get(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(::Town::Get(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 = ::Town::Get(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 = ::Town::Get(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)::Town::Get(town_id)->layout);
00201 }

Generated on Wed Mar 3 23:32:19 2010 for OpenTTD by  doxygen 1.6.1