00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "industry.h"
00015 #include "town.h"
00016 #include "news_func.h"
00017 #include "ai/ai.hpp"
00018 #include "station_base.h"
00019 #include "cargotype.h"
00020 #include "strings_func.h"
00021 #include "window_func.h"
00022 #include "subsidy_base.h"
00023 #include "subsidy_func.h"
00024 #include "core/pool_func.hpp"
00025
00026 #include "table/strings.h"
00027
00028 SubsidyPool _subsidy_pool("Subsidy");
00029 INSTANTIATE_POOL_METHODS(Subsidy)
00030
00031
00035 void Subsidy::AwardTo(CompanyID company)
00036 {
00037 assert(!this->IsAwarded());
00038
00039 this->awarded = company;
00040 this->remaining = SUBSIDY_CONTRACT_MONTHS;
00041
00042 char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
00043 SetDParam(0, company);
00044 GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
00045
00046
00047 Pair reftype = SetupSubsidyDecodeParam(this, 0);
00048 InjectDParam(1);
00049
00050 SetDParamStr(0, company_name);
00051 AddNewsItem(
00052 STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
00053 NS_SUBSIDIES,
00054 (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst,
00055 company_name
00056 );
00057 AI::BroadcastNewEvent(new AIEventSubsidyAwarded(this->index));
00058
00059 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
00060 }
00061
00065 void InitializeSubsidies()
00066 {
00067 _subsidy_pool.CleanPool();
00068 }
00069
00070 Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
00071 {
00072 NewsReferenceType reftype1 = NR_NONE;
00073 NewsReferenceType reftype2 = NR_NONE;
00074
00075
00076 const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
00077 SetDParam(0, mode ? cs->name : cs->name_single);
00078
00079 switch (s->src_type) {
00080 case ST_INDUSTRY:
00081 reftype1 = NR_INDUSTRY;
00082 SetDParam(1, STR_INDUSTRY_NAME);
00083 break;
00084 case ST_TOWN:
00085 reftype1 = NR_TOWN;
00086 SetDParam(1, STR_TOWN_NAME);
00087 break;
00088 default: NOT_REACHED();
00089 }
00090 SetDParam(2, s->src);
00091
00092 switch (s->dst_type) {
00093 case ST_INDUSTRY:
00094 reftype2 = NR_INDUSTRY;
00095 SetDParam(4, STR_INDUSTRY_NAME);
00096 break;
00097 case ST_TOWN:
00098 reftype2 = NR_TOWN;
00099 SetDParam(4, STR_TOWN_NAME);
00100 break;
00101 default: NOT_REACHED();
00102 }
00103 SetDParam(5, s->dst);
00104
00105 Pair p;
00106 p.a = reftype1;
00107 p.b = reftype2;
00108 return p;
00109 }
00110
00117 static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
00118 {
00119 switch (type) {
00120 case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
00121 case ST_TOWN: Town::Get(index)->part_of_subsidy |= flag; return;
00122 default: NOT_REACHED();
00123 }
00124 }
00125
00126 void RebuildSubsidisedSourceAndDestinationCache()
00127 {
00128 Town *t;
00129 FOR_ALL_TOWNS(t) t->part_of_subsidy = POS_NONE;
00130
00131 Industry *i;
00132 FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
00133
00134 const Subsidy *s;
00135 FOR_ALL_SUBSIDIES(s) {
00136 SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
00137 SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
00138 }
00139 }
00140
00141 void DeleteSubsidyWith(SourceType type, SourceID index)
00142 {
00143 bool dirty = false;
00144
00145 Subsidy *s;
00146 FOR_ALL_SUBSIDIES(s) {
00147 if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
00148 delete s;
00149 dirty = true;
00150 }
00151 }
00152
00153 if (dirty) {
00154 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
00155 RebuildSubsidisedSourceAndDestinationCache();
00156 }
00157 }
00158
00159 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
00160 {
00161 const Subsidy *s;
00162 FOR_ALL_SUBSIDIES(s) {
00163 if (s->cargo_type == cargo &&
00164 s->src_type == src_type && s->src == src &&
00165 s->dst_type == dst_type && s->dst == dst) {
00166 return true;
00167 }
00168 }
00169 return false;
00170 }
00171
00172 static Subsidy *FindSubsidyPassengerRoute()
00173 {
00174 assert(Subsidy::CanAllocateItem());
00175
00176 const Town *src = Town::GetRandom();
00177 if (src->population < SUBSIDY_PAX_MIN_POPULATION ||
00178 src->pct_pass_transported > SUBSIDY_MAX_PCT_TRANSPORTED) {
00179 return NULL;
00180 }
00181
00182 const Town *dst = Town::GetRandom();
00183 if (dst->population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
00184 return NULL;
00185 }
00186
00187 if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return NULL;
00188 if (CheckSubsidyDuplicate(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index)) return NULL;
00189
00190 Subsidy *s = new Subsidy();
00191 s->cargo_type = CT_PASSENGERS;
00192 s->src_type = s->dst_type = ST_TOWN;
00193 s->src = src->index;
00194 s->dst = dst->index;
00195
00196 return s;
00197 }
00198
00199 static Subsidy *FindSubsidyCargoRoute()
00200 {
00201 assert(Subsidy::CanAllocateItem());
00202
00203 const Industry *i = Industry::GetRandom();
00204 if (i == NULL) return NULL;
00205
00206 CargoID cargo;
00207 int trans, total;
00208
00209
00210 if (i->produced_cargo[1] != CT_INVALID && HasBit(Random(), 0)) {
00211 cargo = i->produced_cargo[1];
00212 trans = i->last_month_pct_transported[1];
00213 total = i->last_month_production[1];
00214 } else {
00215 cargo = i->produced_cargo[0];
00216 trans = i->last_month_pct_transported[0];
00217 total = i->last_month_production[0];
00218 }
00219
00220
00221
00222 if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED || cargo == CT_INVALID) return NULL;
00223
00224
00225 const CargoSpec *cs = CargoSpec::Get(cargo);
00226 if (cs->town_effect == TE_PASSENGERS) return NULL;
00227
00228 SourceType dst_type;
00229 SourceID dst;
00230
00231 if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
00232
00233 dst_type = ST_TOWN;
00234 const Town *t = Town::GetRandom();
00235
00236
00237 if (t->population < SUBSIDY_CARGO_MIN_POPULATION) return NULL;
00238
00239 if (DistanceManhattan(i->location.tile, t->xy) > SUBSIDY_MAX_DISTANCE) return NULL;
00240
00241 dst = t->index;
00242 } else {
00243
00244 dst_type = ST_INDUSTRY;
00245 const Industry *i2 = Industry::GetRandom();
00246
00247
00248 if (i2 == NULL || i == i2 ||
00249 (cargo != i2->accepts_cargo[0] &&
00250 cargo != i2->accepts_cargo[1] &&
00251 cargo != i2->accepts_cargo[2])) {
00252 return NULL;
00253 }
00254
00255 if (DistanceManhattan(i->location.tile, i2->location.tile) > SUBSIDY_MAX_DISTANCE) return NULL;
00256
00257 dst = i2->index;
00258 }
00259
00260 if (CheckSubsidyDuplicate(cargo, ST_INDUSTRY, i->index, dst_type, dst)) return NULL;
00261
00262 Subsidy *s = new Subsidy();
00263 s->cargo_type = cargo;
00264 s->src_type = ST_INDUSTRY;
00265 s->src = i->index;
00266 s->dst_type = dst_type;
00267 s->dst = dst;
00268
00269 return s;
00270 }
00271
00272 void SubsidyMonthlyLoop()
00273 {
00274 bool modified = false;
00275
00276 Subsidy *s;
00277 FOR_ALL_SUBSIDIES(s) {
00278 if (--s->remaining == 0) {
00279 if (!s->IsAwarded()) {
00280 Pair reftype = SetupSubsidyDecodeParam(s, 1);
00281 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
00282 AI::BroadcastNewEvent(new AIEventSubsidyOfferExpired(s->index));
00283 } else {
00284 if (s->awarded == _local_company) {
00285 Pair reftype = SetupSubsidyDecodeParam(s, 1);
00286 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
00287 }
00288 AI::BroadcastNewEvent(new AIEventSubsidyExpired(s->index));
00289 }
00290 delete s;
00291 modified = true;
00292 }
00293 }
00294
00295 if (modified) RebuildSubsidisedSourceAndDestinationCache();
00296
00297
00298 if (Subsidy::CanAllocateItem() && Chance16(1, 4)) {
00299 uint n = 1000;
00300 do {
00301 Subsidy *s = FindSubsidyPassengerRoute();
00302 if (s == NULL) s = FindSubsidyCargoRoute();
00303 if (s != NULL) {
00304 s->remaining = SUBSIDY_OFFER_MONTHS;
00305 s->awarded = INVALID_COMPANY;
00306 Pair reftype = SetupSubsidyDecodeParam(s, 0);
00307 AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
00308 SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
00309 SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
00310 AI::BroadcastNewEvent(new AIEventSubsidyOffer(s->index));
00311 modified = true;
00312 break;
00313 }
00314 } while (n--);
00315 }
00316
00317 if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
00318 }
00319
00329 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
00330 {
00331
00332 if (src == INVALID_SOURCE) return false;
00333 switch (src_type) {
00334 case ST_INDUSTRY:
00335 if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
00336 break;
00337 case ST_TOWN:
00338 if (!( Town::Get(src)->part_of_subsidy & POS_SRC)) return false;
00339 break;
00340 default: return false;
00341 }
00342
00343
00344
00345 SmallVector<const Town *, 2> towns_near;
00346 if (!st->rect.IsEmpty()) {
00347 Subsidy *s;
00348 FOR_ALL_SUBSIDIES(s) {
00349
00350 if (s->dst_type != ST_TOWN) continue;
00351 if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
00352 if (s->IsAwarded() && s->awarded != company) continue;
00353
00354 Rect rect = st->GetCatchmentRect();
00355
00356 for (int y = rect.top; y <= rect.bottom; y++) {
00357 for (int x = rect.left; x <= rect.right; x++) {
00358 TileIndex tile = TileXY(x, y);
00359 if (!IsTileType(tile, MP_HOUSE)) continue;
00360 const Town *t = Town::GetByTile(tile);
00361 if (t->part_of_subsidy & POS_DST) towns_near.Include(t);
00362 }
00363 }
00364 break;
00365 }
00366 }
00367
00368 bool subsidised = false;
00369
00370
00371
00372 Subsidy *s;
00373 FOR_ALL_SUBSIDIES(s) {
00374 if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
00375 switch (s->dst_type) {
00376 case ST_INDUSTRY:
00377 for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
00378 if (s->dst == (*ip)->index) {
00379 assert((*ip)->part_of_subsidy & POS_DST);
00380 subsidised = true;
00381 if (!s->IsAwarded()) s->AwardTo(company);
00382 }
00383 }
00384 break;
00385 case ST_TOWN:
00386 for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
00387 if (s->dst == (*tp)->index) {
00388 assert((*tp)->part_of_subsidy & POS_DST);
00389 subsidised = true;
00390 if (!s->IsAwarded()) s->AwardTo(company);
00391 }
00392 }
00393 break;
00394 default:
00395 NOT_REACHED();
00396 }
00397 }
00398 }
00399
00400 return subsidised;
00401 }