OpenTTD Source 20260206-master-g4d4e37dbf1
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 const IntervalTimer< TimerGameEconomy_economy_subsidies_monthly ({TimerGameEconomy::Trigger::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, House, include(), Station::industries_near, Industry, INVALID_TILE, IsTileType(), TownCache::part_of_subsidy, BaseStation::rect, Source, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), Town, and Source::type.

Referenced by DeliverGoods().

◆ CheckSubsidyDistance()

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(), SUBSIDY_MAX_DISTANCE, Town, and Source::type.

Referenced by FindSubsidyCargoDestination().

◆ CheckSubsidyDuplicate()

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.

Referenced by FindSubsidyCargoDestination(), and FindSubsidyPassengerRoute().

◆ CmdCreateSubsidy()

CommandCost CmdCreateSubsidy ( DoCommandFlags flags,
CargoType cargo_type,
Source src,
Source dst )

Create a new subsidy.

Parameters
flagstype of operation
cargo_typeCargoType of subsidy.
srcSource.
dstDestination.
Returns
the cost of this operation or an error

Definition at line 196 of file subsidy.cpp.

References _current_company, CMD_ERROR, CreateSubsidy(), Execute, CargoSpec::Get(), Industry, NUM_CARGO, OWNER_DEITY, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), Town, and Source::type.

◆ 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(), RebuildSubsidisedSourceAndDestinationCache(), and WC_SUBSIDIES_LIST.

Referenced by Town::~Town().

◆ FindSubsidyCargoDestination()

bool FindSubsidyCargoDestination ( CargoType cargo_type,
Source src )

Tries to find a suitable destination for the given source and cargo.

Parameters
cargo_typeSubsidized cargo.
srcSource of cargo.
Returns
True iff the subsidy was created.

Definition at line 369 of file subsidy.cpp.

References Chance16(), CheckSubsidyDistance(), CheckSubsidyDuplicate(), CreateSubsidy(), OrthogonalTileArea::Expand(), Industry::GetRandom(), Town::GetRandom(), House, Industry, Source::Invalid, Industry::IsCargoAccepted(), IsTileType(), SUBSIDY_TOWN_CARGO_RADIUS, Town, Source::type, and Town::xy.

Referenced by FindSubsidyIndustryCargoRoute(), and FindSubsidyTownCargoRoute().

◆ FindSubsidyIndustryCargoRoute()

bool FindSubsidyIndustryCargoRoute ( )

Tries to create a cargo subsidy with an industry as source.

Returns
True iff the subsidy was created.

Definition at line 323 of file subsidy.cpp.

References _settings_game, DT_MANUAL, FindSubsidyCargoDestination(), Industry::GetRandom(), Industry, IsValidCargoType(), Industry::produced, RandomRange(), and SUBSIDY_MAX_PCT_TRANSPORTED.

◆ FindSubsidyPassengerRoute()

bool FindSubsidyPassengerRoute ( )

◆ FindSubsidyTownCargoRoute()

◆ RebuildSubsidisedSourceAndDestinationCache()

void RebuildSubsidisedSourceAndDestinationCache ( )

Perform a full rebuild of the subsidies cache.

Definition at line 103 of file subsidy.cpp.

References Destination, SetPartOfSubsidyFlag(), and Source.

Referenced by ChangeOwnershipOfCompanyItems(), CheckCaches(), DeleteSubsidyWith(), and InitializeWindowsAndCaches().

◆ SetPartOfSubsidyFlag()

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 Industry, Town, and Source::type.

Referenced by CreateSubsidy(), and RebuildSubsidisedSourceAndDestinationCache().