/** Perform the monthly update of open subsidies, and try to create a new one. */ void SubsidyMonthlyLoop() { bool modified = false; Subsidy *s; FOR_ALL_SUBSIDIES(s) { if (--s->remaining == 0) { if (!s->IsAwarded()) { Pair reftype = SetupSubsidyDecodeParam(s, true); AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst); AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index)); Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index)); } else { if (s->awarded == _local_company) { Pair reftype = SetupSubsidyDecodeParam(s, true); AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst); } AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index)); Game::NewEvent(new ScriptEventSubsidyExpired(s->index)); } delete s; modified = true; } } if (modified) RebuildSubsidisedSourceAndDestinationCache(); bool passenger_subsidy = false; bool town_subsidy = false; bool industry_subsidy = false; int random_chance = RandomRange(16); if (random_chance < 2) { /* There is a 1/8 chance each month of generating a passenger subsidy. */ int n = 1000; do { passenger_subsidy = FindSubsidyPassengerRoute(); } while (!passenger_subsidy && n--); } else if (random_chance == 2) { /* Cargo subsidies with a town as a source have a 1/16 chance. */ int n = 1000; do { town_subsidy = FindSubsidyTownCargoRoute(); } while (!town_subsidy && n--); } else if (random_chance == 3) { /* Cargo subsidies with an industry as a source have a 1/16 chance. */ int n = 1000; do { industry_subsidy = FindSubsidyIndustryCargoRoute(); } while (!industry_subsidy && n--); } modified |= passenger_subsidy || town_subsidy || industry_subsidy; if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0); }
void SubsidyMonthlyLoop() { bool modified = false; Subsidy *s; FOR_ALL_SUBSIDIES(s) { if (--s->remaining == 0) { if (!s->IsAwarded()) { Pair reftype = SetupSubsidyDecodeParam(s, 1); AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst); AI::BroadcastNewEvent(new AIEventSubsidyOfferExpired(s->index)); } else { if (s->awarded == _local_company) { Pair reftype = SetupSubsidyDecodeParam(s, 1); AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst); } AI::BroadcastNewEvent(new AIEventSubsidyExpired(s->index)); } delete s; modified = true; } } if (modified) RebuildSubsidisedSourceAndDestinationCache(); /* 25% chance to go on */ if (Subsidy::CanAllocateItem() && Chance16(1, 4)) { uint n = 1000; do { Subsidy *s = FindSubsidyPassengerRoute(); if (s == NULL) s = FindSubsidyCargoRoute(); if (s != NULL) { s->remaining = SUBSIDY_OFFER_MONTHS; s->awarded = INVALID_COMPANY; Pair reftype = SetupSubsidyDecodeParam(s, 0); AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NS_SUBSIDIES, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst); SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC); SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST); AI::BroadcastNewEvent(new AIEventSubsidyOffer(s->index)); modified = true; break; } } while (n--); } if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0); }
/** * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company * @param cargo_type type of cargo * @param company company delivering the cargo * @param src_type type of #src * @param src index of source * @param st station where the cargo is delivered to * @return is the delivery subsidised? */ bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st) { /* If the source isn't subsidised, don't continue */ if (src == INVALID_SOURCE) return false; switch (src_type) { case ST_INDUSTRY: if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false; break; case ST_TOWN: if (!( Town::Get(src)->part_of_subsidy & POS_SRC)) return false; break; default: return false; } /* Remember all towns near this station (at least one house in its catchment radius) * which are destination of subsidised path. Do that only if needed */ SmallVector<const Town *, 2> towns_near; if (!st->rect.IsEmpty()) { Subsidy *s; FOR_ALL_SUBSIDIES(s) { /* Don't create the cache if there is no applicable subsidy with town as destination */ if (s->dst_type != ST_TOWN) continue; if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue; if (s->IsAwarded() && s->awarded != company) continue; Rect rect = st->GetCatchmentRect(); for (int y = rect.top; y <= rect.bottom; y++) { for (int x = rect.left; x <= rect.right; x++) { TileIndex tile = TileXY(x, y); if (!IsTileType(tile, MP_HOUSE)) continue; const Town *t = Town::GetByTile(tile); if (t->part_of_subsidy & POS_DST) towns_near.Include(t); } } break; } }