OpenTTD Source: src/subsidy.cpp File Reference

OpenTTD Source 15.0-beta1

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
std::pair< NewsReferenceType, NewsReferenceType > | SetupSubsidyDecodeParam (const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
| Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
static void | SetPartOfSubsidyFlag (SourceType type, SourceID index, 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 (SourceType type, SourceID index)
| Delete the subsidies associated with a given cargo source type and id.
static bool | CheckSubsidyDuplicate (CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
| Check whether a specific subsidy already exists.
static bool | CheckSubsidyDistance (SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
| Checks if the source and destination of a subsidy are inside the distance limit.
void | CreateSubsidy (CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
| Creates a subsidy with the given parameters.
CommandCost | CmdCreateSubsidy (DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
| Create a new subsidy.
bool | FindSubsidyPassengerRoute ()
| Tries to create a passenger subsidy between two towns.
bool | FindSubsidyCargoDestination (CargoID cid, SourceType src_type, SourceID 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 (CargoID cargo_type, CompanyID company, SourceType src_type, SourceID 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()) { std::pair< NewsReferenceType, NewsReferenceType > reftype=SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));} else { if(s->awarded==_local_company) { std::pair< NewsReferenceType, NewsReferenceType > reftype=SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);} 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 | ( | CargoID | cargo_type,
|  | CompanyID | company,
|  | SourceType | src_type,
|  | SourceID | 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_type | type of src
  src | index of source
  st | station where the cargo is delivered to

Returns
  is the delivery subsidised?

Definition at line 564 of file subsidy.cpp.

References Town::cache, Station::catchment_tiles, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_industry_pool >::Get(), include(), Station::industries_near, Industry, INVALID_SOURCE, INVALID_TILE, IsTileType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::Iterate(), MP_HOUSE, TownCache::part_of_subsidy, POS_DST, POS_SRC, BaseStation::rect, and Town.

Referenced by DeliverGoods().

## ◆ CheckSubsidyDistance()

static bool CheckSubsidyDistance | ( | SourceType | src_type,
|  | SourceID | src,
|  | SourceType | dst_type,
|  | SourceID | dst
| ) |  |
| static

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

Parameters

  src_type | Type of src.
  src | Index of source.
  dst_type | Type of dst.
  dst | Index of destination.

Returns
  True if they are inside the distance limit.

Definition at line 197 of file subsidy.cpp.

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

Referenced by FindSubsidyCargoDestination().

## ◆ CheckSubsidyDuplicate()

static bool CheckSubsidyDuplicate | ( | CargoID | cargo,
|  | SourceType | src_type,
|  | SourceID | src,
|  | SourceType | dst_type,
|  | SourceID | dst
| ) |  |
| static

Check whether a specific subsidy already exists.

Parameters

  cargo | Cargo type.
  src_type | Type of source of the cargo, affects interpretation of src.
  src | Id of the source.
  dst_type | Type of the destination of the cargo, affects interpretation of dst.
  dst | Id of the destination.

Returns
  `true` if the subsidy already exists, `false` if not.

Definition at line 177 of file subsidy.cpp.

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

Referenced by FindSubsidyCargoDestination(), and FindSubsidyPassengerRoute().

## ◆ CmdCreateSubsidy()

CommandCost CmdCreateSubsidy | ( | DoCommandFlag | flags,
|  | CargoID | cid,
|  | SourceType | src_type,
|  | SourceID | src,
|  | SourceType | dst_type,
|  | SourceID | dst
| ) |  |

Create a new subsidy.

Parameters

  flags | type of operation
  cid | CargoID of subsidy.
  src_type | SourceType of source.
  src | SourceID of source.
  dst_type | SourceType of destination.
  dst | SourceID of destination.

Returns
  the cost of this operation or an error

Definition at line 244 of file subsidy.cpp.

References _current_company, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), CMD_ERROR, CreateSubsidy(), DC_EXEC, CargoSpec::Get(), Industry, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_industry_pool >::IsValidID(), NUM_CARGO, OWNER_DEITY, and Town.

## ◆ CreateSubsidy()

void CreateSubsidy | ( | CargoID | cid,
|  | SourceType | src_type,
|  | SourceID | src,
|  | SourceType | dst_type,
|  | SourceID | dst
| ) |  |

Creates a subsidy with the given parameters.

Parameters

  cid | Subsidised cargo.
  src_type | Type of src.
  src | Index of source.
  dst_type | Type of dst.
  dst | Index of destination.

Definition at line 213 of file subsidy.cpp.

References AddNewsItem(), Subsidy::awarded, AI::BroadcastNewEvent(), Subsidy::cargo_type, Subsidy::dst, Subsidy::dst_type, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_COMPANY, InvalidateWindowData(), Game::NewEvent(), NewsOffered, NF_NORMAL, NT_SUBSIDIES, POS_DST, POS_SRC, Subsidy::remaining, SetPartOfSubsidyFlag(), SetupSubsidyDecodeParam(), Subsidy::src, Subsidy::src_type, SUBSIDY_OFFER_MONTHS, and WC_SUBSIDIES_LIST.

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

## ◆ DeleteSubsidyWith()

void DeleteSubsidyWith | ( | SourceType | type,
|  | SourceID | index
| ) |  |

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

Parameters

  type | Cargo source type of the id.
  index | Id to remove.

Definition at line 151 of file subsidy.cpp.

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

Referenced by Town::~Town().

## ◆ FindSubsidyCargoDestination()

bool FindSubsidyCargoDestination | ( | CargoID | cid,
|  | SourceType | src_type,
|  | SourceID | src
| ) |  |

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

Parameters

  cid | Subsidized cargo.
  src_type | Type of src.
  src | Index of source.

Returns
  True iff the subsidy was created.

Definition at line 426 of file subsidy.cpp.

References Chance16(), CheckSubsidyDistance(), CheckSubsidyDuplicate(), CreateSubsidy(), OrthogonalTileArea::Expand(), Industry::GetRandom(), Town::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Industry, 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 375 of file subsidy.cpp.

References _settings_game, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), DT_MANUAL, FindSubsidyCargoDestination(), Industry::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Industry, IsValidCargoID(), 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 284 of file subsidy.cpp.

References Town::cache, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), CheckSubsidyDuplicate(), CreateSubsidy(), DistanceManhattan(), Town::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 318 of file subsidy.cpp.

References _settings_game, Town::cache, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), DT_MANUAL, OrthogonalTileArea::Expand(), FindSubsidyCargoDestination(), CargoSpec::Get(), Town::GetRandom(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 134 of file subsidy.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_industry_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Iterate(), POS_DST, POS_NONE, POS_SRC, and SetPartOfSubsidyFlag().

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

## ◆ SetPartOfSubsidyFlag()

static void SetPartOfSubsidyFlag | ( | SourceType | type,
|  | SourceID | index,
|  | PartOfSubsidy | flag
| ) |  |
| inlinestatic

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

Parameters

  type | is it a town or an industry?
  index | index of town/industry
  flag | flag to set

Definition at line 124 of file subsidy.cpp.

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

Referenced by CreateSubsidy(), and RebuildSubsidisedSourceAndDestinationCache().

## ◆ SetupSubsidyDecodeParam()

std::pair< NewsReferenceType, NewsReferenceType > SetupSubsidyDecodeParam | ( | const Subsidy * | s,
|  | SubsidyDecodeParamType | mode,
|  | uint | parameter_offset
| ) |  |

Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.

Parameters

  s | Subsidy being printed.
  mode | Type of subsidy news message to decide on parameter format.
  parameter_offset | The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.

Returns
  Reference of the subsidy in the news system.

Definition at line 75 of file subsidy.cpp.

References _settings_game, Subsidy::cargo_type, GameSettings::difficulty, Subsidy::dst, Subsidy::dst_type, CargoSpec::Get(), Industry, CargoSpec::name, NewsAwarded, NewsOffered, NR_INDUSTRY, NR_NONE, NR_TOWN, SetDParam(), Subsidy::src, Subsidy::src_type, DifficultySettings::subsidy_duration, and Town.

Referenced by Subsidy::AwardTo(), CreateSubsidy(), and SubsidyListWindow::DrawWidget().