/** 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); }
/** * Marks subsidy as awarded, creates news and AI event * @param company awarded company */ void Subsidy::AwardTo(CompanyID company) { assert(!this->IsAwarded()); this->awarded = company; this->remaining = SUBSIDY_CONTRACT_MONTHS; char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES); SetDParam(0, company); GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1); /* Add a news item */ Pair reftype = SetupSubsidyDecodeParam(this, 0); InjectDParam(1); SetDParamStr(0, company_name); AddNewsItem( STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier, NS_SUBSIDIES, (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst, company_name ); AI::BroadcastNewEvent(new AIEventSubsidyAwarded(this->index)); InvalidateWindowData(WC_SUBSIDIES_LIST, 0); }
/** * Marks subsidy as awarded, creates news and AI event * @param company awarded company */ void Subsidy::AwardTo(CompanyID company) { assert(!this->IsAwarded()); this->awarded = company; this->remaining = SUBSIDY_CONTRACT_MONTHS; char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH]; SetDParam(0, company); GetString(company_name, STR_COMPANY_NAME, lastof(company_name)); char *cn = strdup(company_name); /* Add a news item */ Pair reftype = SetupSubsidyDecodeParam(this, false); InjectDParam(1); SetDParamStr(0, cn); AddNewsItem( STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst, cn ); AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index)); Game::NewEvent(new ScriptEventSubsidyAwarded(this->index)); 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); }
/** * Creates a subsidy with the given parameters. * @param cid Subsidised cargo. * @param src_type Type of \a src. * @param src Index of source. * @param dst_type Type of \a dst. * @param dst Index of destination. */ void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst) { Subsidy *s = new Subsidy(); s->cargo_type = cid; s->src_type = src_type; s->src = src; s->dst_type = dst_type; s->dst = dst; s->remaining = SUBSIDY_OFFER_MONTHS; s->awarded = INVALID_COMPANY; Pair reftype = SetupSubsidyDecodeParam(s, false); AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, (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 ScriptEventSubsidyOffer(s->index)); Game::NewEvent(new ScriptEventSubsidyOffer(s->index)); InvalidateWindowData(WC_SUBSIDIES_LIST, 0); }
virtual void DrawWidget(const Rect &r, int widget) const { if (widget != SLW_PANEL) return; YearMonthDay ymd; ConvertDateToYMD(_date, &ymd); int right = r.right - WD_FRAMERECT_RIGHT; int y = r.top + WD_FRAMERECT_TOP; int x = r.left + WD_FRAMERECT_LEFT; int pos = -this->vscroll->GetPosition(); const int cap = this->vscroll->GetCapacity(); /* Section for drawing the offered subisidies */ if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE); pos++; uint num = 0; const Subsidy *s; FOR_ALL_SUBSIDIES(s) { if (!s->IsAwarded()) { if (IsInsideMM(pos, 0, cap)) { /* Displays the two offered towns */ SetupSubsidyDecodeParam(s, true); SetDParam(7, _date - ymd.day + s->remaining * 32); DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO); } pos++; num++; } } if (num == 0) { if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE); pos++; } /* Section for drawing the already granted subisidies */ pos++; if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE); pos++; num = 0; FOR_ALL_SUBSIDIES(s) { if (s->IsAwarded()) { if (IsInsideMM(pos, 0, cap)) { SetupSubsidyDecodeParam(s, true); SetDParam(7, s->awarded); SetDParam(8, _date - ymd.day + s->remaining * 32); /* Displays the two connected stations */ DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO); } pos++; num++; } } if (num == 0) { if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE); pos++; } }