OpenTTD Source: src/subsidy.cpp File Reference

OpenTTD Source 15.3

Functions | Variables
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::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_type | type of cargo
  company | company delivering the cargo
  src | source of cargo
  st | station 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

  src | Source of cargo.
  dst | Destination 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

  cargo | Cargo type.
  src | The source.
  dst | The 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()

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

Create a new subsidy.

Parameters

  flags | type of operation
  cargo_type | CargoType of subsidy.
  src | Source.
  dst | Destination.

Returns
  the cost of this operation or an error

Definition at line 196 of file subsidy.cpp.

References _current_company, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::CanAllocateItem(), CMD_ERROR, CreateSubsidy(), Execute, CargoSpec::Get(), Industry, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_industry_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_town_pool >::IsValidID(), NUM_CARGO, OWNER_DEITY, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), Town, and Source::type.

## ◆ CreateSubsidy()

void CreateSubsidy | ( | CargoType | cargo_type,
|  | Source | src,
|  | Source | dst
| ) |  |

Creates a subsidy with the given parameters.

Parameters

  cargo_type | Subsidised cargo.
  src | Source of cargo.
  dst | Destination of cargo.

Definition at line 173 of file subsidy.cpp.

References _settings_game, AddNewsItem(), AI::BroadcastNewEvent(), Subsidy::cargo_type, Destination, GameSettings::difficulty, Subsidy::dst, CargoSpec::Get(), GetEncodedString(), Source::GetFormat(), Source::GetNewsReference(), Source::id, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, InvalidateWindowData(), CargoSpec::name, Game::NewEvent(), Normal, SetPartOfSubsidyFlag(), Source, Subsidy::src, Subsidies, DifficultySettings::subsidy_duration, SUBSIDY_OFFER_MONTHS, and WC_SUBSIDIES_LIST.

Referenced by CmdCreateSubsidy(), FindSubsidyCargoDestination(), and FindSubsidyPassengerRoute().

## ◆ DeleteSubsidyWith()

void DeleteSubsidyWith | ( | Source | source | ) |

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

Parameters

  source | The 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
| ) |  |

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

Parameters

  cargo_type | Subsidized cargo.
  src | Source 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(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, Industry, Source::Invalid, Industry::IsCargoAccepted(), IsTileType(), MP_HOUSE, SUBSIDY_TOWN_CARGO_RADIUS, Town, 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, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::CanAllocateItem(), DT_MANUAL, FindSubsidyCargoDestination(), Industry::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, Industry, IsValidCargoType(), GameSettings::linkgraph, Industry::produced, RandomRange(), and SUBSIDY_MAX_PCT_TRANSPORTED.

## ◆ FindSubsidyPassengerRoute()

bool FindSubsidyPassengerRoute | ( |  | ) |

Tries to create a passenger subsidy between two towns.

Returns
  True iff the subsidy was created.

Definition at line 236 of file subsidy.cpp.

References Town::cache, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::CanAllocateItem(), CheckSubsidyDuplicate(), CreateSubsidy(), DistanceManhattan(), Town::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, TownCache::population, RandomRange(), SUBSIDY_MAX_DISTANCE, SUBSIDY_MAX_PCT_TRANSPORTED, SUBSIDY_PAX_MIN_POPULATION, Town, CargoSpec::town_production_cargoes, TPE_PASSENGERS, and Town::xy.

## ◆ FindSubsidyTownCargoRoute()

bool FindSubsidyTownCargoRoute | ( |  | ) |

Tries to create a cargo subsidy with a town as source.

Returns
  True iff the subsidy was created.

Definition at line 270 of file subsidy.cpp.

References _settings_game, Town::cache, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_subsidy_pool >::CanAllocateItem(), DT_MANUAL, OrthogonalTileArea::Expand(), FindSubsidyCargoDestination(), CargoSpec::Get(), Town::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::index, IsTileType(), GameSettings::linkgraph, MP_HOUSE, NUM_CARGO, TownCache::population, RandomRange(), SUBSIDY_CARGO_MIN_POPULATION, SUBSIDY_MAX_PCT_TRANSPORTED, SUBSIDY_TOWN_CARGO_RADIUS, Town, CargoSpec::town_production_cargoes, TPE_PASSENGERS, and Town::xy.

## ◆ RebuildSubsidisedSourceAndDestinationCache()

void RebuildSubsidisedSourceAndDestinationCache | ( |  | ) |

Perform a full rebuild of the subsidies cache.

Definition at line 103 of file subsidy.cpp.

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

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

## ◆ SetPartOfSubsidyFlag()

static void SetPartOfSubsidyFlag | ( | Source | source,
|  | PartOfSubsidy | flag
| ) |  |
| inlinestatic

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

Parameters

  source | actual source
  flag | flag 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().