1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-29 07:04:35 +01:00

Codechange: Use EncodedStrings for News messages. (#13654)

This commit is contained in:
Peter Nelson
2025-02-23 20:24:02 +00:00
committed by GitHub
parent 0afae7c546
commit 6e10584b91
30 changed files with 304 additions and 293 deletions

View File

@@ -36,6 +36,32 @@
SubsidyPool _subsidy_pool("Subsidy"); ///< Pool for the subsidies.
INSTANTIATE_POOL_METHODS(Subsidy)
/**
* Get the NewsReference for a subsidy Source.
* @returns NewsReference.
*/
NewsReference Source::GetNewsReference() const
{
switch (this->type) {
case SourceType::Industry: return static_cast<IndustryID>(this->id);
case SourceType::Town: return static_cast<TownID>(this->id);
default: NOT_REACHED();
}
}
/**
* Get the format string for a subsidy Source.
* @returns The format string.
*/
StringID Source::GetFormat() const
{
switch (this->type) {
case SourceType::Industry: return STR_INDUSTRY_NAME;
case SourceType::Town: return STR_TOWN_NAME;
default: NOT_REACHED();
}
}
/**
* Marks subsidy as awarded, creates news and AI event
* @param company awarded company
@@ -50,70 +76,15 @@ void Subsidy::AwardTo(CompanyID company)
std::string company_name = GetString(STR_COMPANY_NAME, company);
/* Add a news item */
std::pair<NewsReference, NewsReference> references = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
SetDParamStr(0, company_name);
AddNewsItem(
STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
NewsType::Subsidies, NewsStyle::Normal, {},
references.first, references.second
);
const CargoSpec *cs = CargoSpec::Get(this->cargo_type);
EncodedString headline = GetEncodedString(STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier, std::move(company_name), cs->name, this->src.GetFormat(), this->src.id, this->dst.GetFormat(), this->dst.id, _settings_game.difficulty.subsidy_duration);
AddNewsItem(std::move(headline), NewsType::Subsidies, NewsStyle::Normal, {}, this->src.GetNewsReference(), this->dst.GetNewsReference());
AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
}
/**
* Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
* @param s %Subsidy being printed.
* @param mode Type of subsidy news message to decide on parameter format.
* @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
* @return Reference of the subsidy in the news system.
*/
std::pair<NewsReference, NewsReference> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
{
NewsReference reference1{};
NewsReference reference2{};
/* Always use the plural form of the cargo name - trying to decide between plural or singular causes issues for translations */
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
SetDParam(parameter_offset, cs->name);
switch (s->src.type) {
case SourceType::Industry:
reference1 = static_cast<IndustryID>(s->src.id);
SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
break;
case SourceType::Town:
reference1 = static_cast<TownID>(s->src.id);
SetDParam(parameter_offset + 1, STR_TOWN_NAME);
break;
default: NOT_REACHED();
}
SetDParam(parameter_offset + 2, s->src.id);
switch (s->dst.type) {
case SourceType::Industry:
reference2 = static_cast<IndustryID>(s->dst.id);
SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
break;
case SourceType::Town:
reference2 = static_cast<TownID>(s->dst.id);
SetDParam(parameter_offset + 4, STR_TOWN_NAME);
break;
default: NOT_REACHED();
}
SetDParam(parameter_offset + 5, s->dst.id);
/* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
if (mode == SubsidyDecodeParamType::NewsOffered || mode == SubsidyDecodeParamType::NewsAwarded) {
SetDParam(parameter_offset + 7, _settings_game.difficulty.subsidy_duration);
}
return {reference1, reference2};
}
/**
* Sets a flag indicating that given town/industry is part of subsidised route.
* @param source actual source
@@ -203,8 +174,9 @@ void CreateSubsidy(CargoType cargo_type, Source src, Source dst)
{
Subsidy *s = new Subsidy(cargo_type, src, dst, SUBSIDY_OFFER_MONTHS);
std::pair<NewsReference, NewsReference> references = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsOffered);
AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NewsType::Subsidies, NewsStyle::Normal, {}, references.first, references.second);
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
EncodedString headline = GetEncodedString(STR_NEWS_SERVICE_SUBSIDY_OFFERED, cs->name, s->src.GetFormat(), s->src.id, s->dst.GetFormat(), s->dst.id, _settings_game.difficulty.subsidy_duration);
AddNewsItem(std::move(headline), NewsType::Subsidies, NewsStyle::Normal, {}, s->src.GetNewsReference(), s->dst.GetNewsReference());
SetPartOfSubsidyFlag(s->src, POS_SRC);
SetPartOfSubsidyFlag(s->dst, POS_DST);
AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
@@ -457,14 +429,16 @@ static IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEcon
for (Subsidy *s : Subsidy::Iterate()) {
if (--s->remaining == 0) {
if (!s->IsAwarded()) {
std::pair<NewsReference, NewsReference> references = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NewsType::Subsidies, NewsStyle::Normal, {}, references.first, references.second);
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
EncodedString headline = GetEncodedString(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, cs->name, s->src.GetFormat(), s->src.id, s->dst.GetFormat(), s->dst.id);
AddNewsItem(std::move(headline), NewsType::Subsidies, NewsStyle::Normal, {}, s->src.GetNewsReference(), s->dst.GetNewsReference());
AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
} else {
if (s->awarded == _local_company) {
std::pair<NewsReference, NewsReference> references = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NewsType::Subsidies, NewsStyle::Normal, {}, references.first, references.second);
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
EncodedString headline = GetEncodedString(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, cs->name, s->src.GetFormat(), s->src.id, s->dst.GetFormat(), s->dst.id);
AddNewsItem(std::move(headline), NewsType::Subsidies, NewsStyle::Normal, {}, s->src.GetNewsReference(), s->dst.GetNewsReference());
}
AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
Game::NewEvent(new ScriptEventSubsidyExpired(s->index));