OpenTTD Source 20250312-master-gcdcc6b491d
subsidy.cpp File Reference

Handling of subsidies. More...

#include "stdafx.h"
#include "company_func.h"
#include "industry.h"
#include "town.h"
#include "news_func.h"
#include "ai/ai.hpp"
#include "station_base.h"
#include "strings_func.h"
#include "window_func.h"
#include "subsidy_base.h"
#include "subsidy_func.h"
#include "core/pool_func.hpp"
#include "core/random_func.hpp"
#include "core/container_func.hpp"
#include "game/game.hpp"
#include "command_func.h"
#include "string_func.h"
#include "tile_cmd.h"
#include "subsidy_cmd.h"
#include "timer/timer.h"
#include "timer/timer_game_economy.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

static void SetPartOfSubsidyFlag (Source source, PartOfSubsidy flag)
 Sets a flag indicating that given town/industry is part of subsidised route.
 
void RebuildSubsidisedSourceAndDestinationCache ()
 Perform a full rebuild of the subsidies cache.
 
void DeleteSubsidyWith (Source source)
 Delete the subsidies associated with a given cargo source type and id.
 
static bool CheckSubsidyDuplicate (CargoType cargo, Source src, Source dst)
 Check whether a specific subsidy already exists.
 
static bool CheckSubsidyDistance (Source src, Source dst)
 Checks if the source and destination of a subsidy are inside the distance limit.
 
void CreateSubsidy (CargoType cargo_type, Source src, Source dst)
 Creates a subsidy with the given parameters.
 
CommandCost CmdCreateSubsidy (DoCommandFlags flags, CargoType cargo_type, Source src, Source dst)
 Create a new subsidy.
 
bool FindSubsidyPassengerRoute ()
 Tries to create a passenger subsidy between two towns.
 
bool FindSubsidyCargoDestination (CargoType cargo_type, Source src)
 Tries to find a suitable destination for the given source and cargo.
 
bool FindSubsidyTownCargoRoute ()
 Tries to create a cargo subsidy with a town as source.
 
bool FindSubsidyIndustryCargoRoute ()
 Tries to create a cargo subsidy with an industry as source.
 
bool CheckSubsidised (CargoType cargo_type, CompanyID company, Source src, const Station *st)
 Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company.
 

Variables

SubsidyPool _subsidy_pool ("Subsidy")
 Pool for the subsidies.
 
static IntervalTimer< TimerGameEconomy_economy_subsidies_monthly ({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto) { bool modified=false;for(Subsidy *s :Subsidy::Iterate()) { if(--s->remaining==0) { if(!s->IsAwarded()) { 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) { 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));} delete s;modified=true;} } if(modified) { RebuildSubsidisedSourceAndDestinationCache();} else if(_settings_game.difficulty.subsidy_duration==0) { return;} else if(_settings_game.linkgraph.distribution_pax !=DT_MANUAL &&_settings_game.linkgraph.distribution_mail !=DT_MANUAL &&_settings_game.linkgraph.distribution_armoured !=DT_MANUAL &&_settings_game.linkgraph.distribution_default !=DT_MANUAL) { return;} bool passenger_subsidy=false;bool town_subsidy=false;bool industry_subsidy=false;int random_chance=RandomRange(16);if(random_chance< 2 &&_settings_game.linkgraph.distribution_pax==DT_MANUAL) { int n=1000;do { passenger_subsidy=FindSubsidyPassengerRoute();} while(!passenger_subsidy &&n--);} else if(random_chance==2) { int n=1000;do { town_subsidy=FindSubsidyTownCargoRoute();} while(!town_subsidy &&n--);} else if(random_chance==3) { 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);})
 Perform the economy monthly update of open subsidies, and try to create a new one.
 

Detailed Description

Handling of subsidies.

Definition in file subsidy.cpp.

Function Documentation

◆ CheckSubsidised()

bool CheckSubsidised ( CargoType  cargo_type,
CompanyID  company,
Source  src,
const Station st 
)

Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company.

Parameters
cargo_typetype of cargo
companycompany delivering the cargo
srcsource of cargo
ststation where the cargo is delivered to
Returns
is the delivery subsidised?

Definition at line 507 of file subsidy.cpp.

References Town::cache, Station::catchment_tiles, Destination, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_town_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_industry_pool >::Get(), include(), Station::industries_near, Industry, INVALID_TILE, IsTileType(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::Iterate(), MP_HOUSE, TownCache::part_of_subsidy, BaseStation::rect, Source, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), Town, and Source::type.

Referenced by DeliverGoods().

◆ CheckSubsidyDistance()

static bool CheckSubsidyDistance ( Source  src,
Source  dst 
)
static

Checks if the source and destination of a subsidy are inside the distance limit.

Parameters
srcSource of cargo.
dstDestination of cargo.
Returns
True if they are inside the distance limit.

Definition at line 159 of file subsidy.cpp.

References DistanceManhattan(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_town_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_industry_pool >::Get(), SUBSIDY_MAX_DISTANCE, Town, and Source::type.

Referenced by FindSubsidyCargoDestination().

◆ CheckSubsidyDuplicate()

static bool CheckSubsidyDuplicate ( CargoType  cargo,
Source  src,
Source  dst 
)
static

Check whether a specific subsidy already exists.

Parameters
cargoCargo type.
srcThe source.
dstThe destination.
Returns
true if the subsidy already exists, false if not.

Definition at line 143 of file subsidy.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::Iterate().

Referenced by FindSubsidyCargoDestination(), and FindSubsidyPassengerRoute().

◆ CmdCreateSubsidy()

◆ CreateSubsidy()

◆ DeleteSubsidyWith()

void DeleteSubsidyWith ( Source  source)

Delete the subsidies associated with a given cargo source type and id.

Parameters
sourceThe source to look for.

Definition at line 119 of file subsidy.cpp.

References InvalidateWindowData(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::Iterate(), RebuildSubsidisedSourceAndDestinationCache(), and WC_SUBSIDIES_LIST.

Referenced by Town::~Town().

◆ FindSubsidyCargoDestination()

bool FindSubsidyCargoDestination ( CargoType  cargo_type,
Source  src 
)

◆ FindSubsidyIndustryCargoRoute()

◆ FindSubsidyPassengerRoute()

◆ FindSubsidyTownCargoRoute()

◆ RebuildSubsidisedSourceAndDestinationCache()

void RebuildSubsidisedSourceAndDestinationCache ( )

◆ SetPartOfSubsidyFlag()

static void SetPartOfSubsidyFlag ( Source  source,
PartOfSubsidy  flag 
)
inlinestatic

Sets a flag indicating that given town/industry is part of subsidised route.

Parameters
sourceactual source
flagflag to set

Definition at line 93 of file subsidy.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_town_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_industry_pool >::Get(), Industry, Town, and Source::type.

Referenced by CreateSubsidy().