Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../../stdafx.h"
00013 #include "script_town.hpp"
00014 #include "script_map.hpp"
00015 #include "script_cargo.hpp"
00016 #include "script_error.hpp"
00017 #include "../../town.h"
00018 #include "../../strings_func.h"
00019 #include "../../company_func.h"
00020 #include "../../station_base.h"
00021 #include "../../landscape.h"
00022 #include "table/strings.h"
00023
00024 int32 ScriptTown::GetTownCount()
00025 {
00026 return (int32)::Town::GetNumItems();
00027 }
00028
00029 bool ScriptTown::IsValidTown(TownID town_id)
00030 {
00031 return ::Town::IsValidID(town_id);
00032 }
00033
00034 char *ScriptTown::GetName(TownID town_id)
00035 {
00036 if (!IsValidTown(town_id)) return NULL;
00037 static const int len = 64;
00038 char *town_name = MallocT<char>(len);
00039
00040 ::SetDParam(0, town_id);
00041 ::GetString(town_name, STR_TOWN_NAME, &town_name[len - 1]);
00042
00043 return town_name;
00044 }
00045
00046 int32 ScriptTown::GetPopulation(TownID town_id)
00047 {
00048 if (!IsValidTown(town_id)) return -1;
00049 const Town *t = ::Town::Get(town_id);
00050 return t->population;
00051 }
00052
00053 int32 ScriptTown::GetHouseCount(TownID town_id)
00054 {
00055 if (!IsValidTown(town_id)) return -1;
00056 const Town *t = ::Town::Get(town_id);
00057 return t->num_houses;
00058 }
00059
00060 TileIndex ScriptTown::GetLocation(TownID town_id)
00061 {
00062 if (!IsValidTown(town_id)) return INVALID_TILE;
00063 const Town *t = ::Town::Get(town_id);
00064 return t->xy;
00065 }
00066
00067 int32 ScriptTown::GetLastMonthProduction(TownID town_id, CargoID cargo_id)
00068 {
00069 if (!IsValidTown(town_id)) return -1;
00070 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
00071
00072 const Town *t = ::Town::Get(town_id);
00073
00074 return t->supplied[cargo_id].old_max;
00075 }
00076
00077 int32 ScriptTown::GetLastMonthSupplied(TownID town_id, CargoID cargo_id)
00078 {
00079 if (!IsValidTown(town_id)) return -1;
00080 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
00081
00082 const Town *t = ::Town::Get(town_id);
00083
00084 return t->supplied[cargo_id].old_act;
00085 }
00086
00087 int32 ScriptTown::GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id)
00088 {
00089 if (!IsValidTown(town_id)) return -1;
00090 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
00091
00092 const Town *t = ::Town::Get(town_id);
00093 return ::ToPercent8(t->GetPercentTransported(cargo_id));
00094 }
00095
00096 int32 ScriptTown::GetLastMonthReceived(TownID town_id, ScriptCargo::TownEffect towneffect_id)
00097 {
00098 if (!IsValidTown(town_id)) return -1;
00099 if (!ScriptCargo::IsValidTownEffect(towneffect_id)) return -1;
00100
00101 const Town *t = ::Town::Get(town_id);
00102
00103 return t->received[towneffect_id].old_act;
00104 }
00105
00106 uint32 ScriptTown::GetCargoGoal(TownID town_id, ScriptCargo::TownEffect towneffect_id)
00107 {
00108 if (!IsValidTown(town_id)) return -1;
00109 if (!ScriptCargo::IsValidTownEffect(towneffect_id)) return -1;
00110
00111 const Town *t = ::Town::Get(town_id);
00112
00113 switch (t->goal[towneffect_id]) {
00114 case TOWN_GROWTH_WINTER:
00115 if (TileHeight(t->xy) >= GetSnowLine() && t->population > 90) return 1;
00116 return 0;
00117
00118 case TOWN_GROWTH_DESERT:
00119 if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->population > 60) return 1;
00120 return 0;
00121
00122 default: return t->goal[towneffect_id];
00123 }
00124 }
00125
00126 int32 ScriptTown::GetGrowthRate(TownID town_id)
00127 {
00128 if (!IsValidTown(town_id)) return false;
00129
00130 const Town *t = ::Town::Get(town_id);
00131
00132 return (t->growth_rate * TOWN_GROWTH_TICKS + DAY_TICKS) / DAY_TICKS;
00133 }
00134
00135 int32 ScriptTown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
00136 {
00137 return ScriptMap::DistanceManhattan(tile, GetLocation(town_id));
00138 }
00139
00140 int32 ScriptTown::GetDistanceSquareToTile(TownID town_id, TileIndex tile)
00141 {
00142 return ScriptMap::DistanceSquare(tile, GetLocation(town_id));
00143 }
00144
00145 bool ScriptTown::IsWithinTownInfluence(TownID town_id, TileIndex tile)
00146 {
00147 if (!IsValidTown(town_id)) return false;
00148
00149 const Town *t = ::Town::Get(town_id);
00150 return ((uint32)GetDistanceSquareToTile(town_id, tile) <= t->squared_town_zone_radius[0]);
00151 }
00152
00153 bool ScriptTown::HasStatue(TownID town_id)
00154 {
00155 if (!IsValidTown(town_id)) return false;
00156
00157 return ::HasBit(::Town::Get(town_id)->statues, _current_company);
00158 }
00159
00160 bool ScriptTown::IsCity(TownID town_id)
00161 {
00162 if (!IsValidTown(town_id)) return false;
00163
00164 return ::Town::Get(town_id)->larger_town;
00165 }
00166
00167 int ScriptTown::GetRoadReworkDuration(TownID town_id)
00168 {
00169 if (!IsValidTown(town_id)) return -1;
00170
00171 return ::Town::Get(town_id)->road_build_months;
00172 }
00173
00174 ScriptCompany::CompanyID ScriptTown::GetExclusiveRightsCompany(TownID town_id)
00175 {
00176 if (!IsValidTown(town_id)) return ScriptCompany::COMPANY_INVALID;
00177
00178 return (ScriptCompany::CompanyID)(int8)::Town::Get(town_id)->exclusivity;
00179 }
00180
00181 int32 ScriptTown::GetExclusiveRightsDuration(TownID town_id)
00182 {
00183 if (!IsValidTown(town_id)) return -1;
00184
00185 return ::Town::Get(town_id)->exclusive_counter;
00186 }
00187
00188 bool ScriptTown::IsActionAvailable(TownID town_id, TownAction town_action)
00189 {
00190 if (!IsValidTown(town_id)) return false;
00191
00192 return HasBit(::GetMaskOfTownActions(NULL, _current_company, ::Town::Get(town_id)), town_action);
00193 }
00194
00195 bool ScriptTown::PerformTownAction(TownID town_id, TownAction town_action)
00196 {
00197 EnforcePrecondition(false, IsValidTown(town_id));
00198 EnforcePrecondition(false, IsActionAvailable(town_id, town_action));
00199
00200 return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
00201 }
00202
00203 ScriptTown::TownRating ScriptTown::GetRating(TownID town_id, ScriptCompany::CompanyID company_id)
00204 {
00205 if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
00206 ScriptCompany::CompanyID company = ScriptCompany::ResolveCompanyID(company_id);
00207 if (company == ScriptCompany::COMPANY_INVALID) return TOWN_RATING_INVALID;
00208
00209 const Town *t = ::Town::Get(town_id);
00210 if (!HasBit(t->have_ratings, company)) {
00211 return TOWN_RATING_NONE;
00212 } else if (t->ratings[company] <= RATING_APPALLING) {
00213 return TOWN_RATING_APPALLING;
00214 } else if (t->ratings[company] <= RATING_VERYPOOR) {
00215 return TOWN_RATING_VERY_POOR;
00216 } else if (t->ratings[company] <= RATING_POOR) {
00217 return TOWN_RATING_POOR;
00218 } else if (t->ratings[company] <= RATING_MEDIOCRE) {
00219 return TOWN_RATING_MEDIOCRE;
00220 } else if (t->ratings[company] <= RATING_GOOD) {
00221 return TOWN_RATING_GOOD;
00222 } else if (t->ratings[company] <= RATING_VERYGOOD) {
00223 return TOWN_RATING_VERY_GOOD;
00224 } else if (t->ratings[company] <= RATING_EXCELLENT) {
00225 return TOWN_RATING_EXCELLENT;
00226 } else {
00227 return TOWN_RATING_OUTSTANDING;
00228 }
00229 }
00230
00231 int ScriptTown::GetAllowedNoise(TownID town_id)
00232 {
00233 if (!IsValidTown(town_id)) return -1;
00234
00235 const Town *t = ::Town::Get(town_id);
00236 if (_settings_game.economy.station_noise_level) {
00237 return t->MaxTownNoise() - t->noise_reached;
00238 }
00239
00240 int num = 0;
00241 const Station *st;
00242 FOR_ALL_STATIONS(st) {
00243 if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport.type != AT_OILRIG) num++;
00244 }
00245 return max(0, 2 - num);
00246 }
00247
00248 ScriptTown::RoadLayout ScriptTown::GetRoadLayout(TownID town_id)
00249 {
00250 if (!IsValidTown(town_id)) return ROAD_LAYOUT_INVALID;
00251
00252 return (ScriptTown::RoadLayout)((TownLayout)::Town::Get(town_id)->layout);
00253 }