OpenTTD
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 "game/game.hpp"
#include "command_func.h"
#include "string_func.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

Pair SetupSubsidyDecodeParam (const Subsidy *s, bool mode)
 Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy. More...
 
static void SetPartOfSubsidyFlag (SourceType type, SourceID index, PartOfSubsidy flag)
 Sets a flag indicating that given town/industry is part of subsidised route. More...
 
void RebuildSubsidisedSourceAndDestinationCache ()
 Perform a full rebuild of the subsidies cache. More...
 
void DeleteSubsidyWith (SourceType type, SourceID index)
 Delete the subsidies associated with a given cargo source type and id. More...
 
static bool CheckSubsidyDuplicate (CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
 Check whether a specific subsidy already exists. More...
 
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. More...
 
void CreateSubsidy (CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
 Creates a subsidy with the given parameters. More...
 
CommandCost CmdCreateSubsidy (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 Create a new subsidy. More...
 
bool FindSubsidyPassengerRoute ()
 Tries to create a passenger subsidy between two towns. More...
 
bool FindSubsidyCargoDestination (CargoID cid, SourceType src_type, SourceID src)
 Tries to find a suitable destination for the given source and cargo. More...
 
bool FindSubsidyTownCargoRoute ()
 Tries to create a cargo subsidy with a town as source. More...
 
bool FindSubsidyIndustryCargoRoute ()
 Tries to create a cargo subsidy with an industry as source. More...
 
void SubsidyMonthlyLoop ()
 Perform the monthly update of open subsidies, and try to create a new one. More...
 
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. More...
 

Variables

SubsidyPool _subsidy_pool ("Subsidy")
 Pool for the subsidies.
 

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_typetype of cargo
companycompany delivering the cargo
src_typetype of src
srcindex of source
ststation where the cargo is delivered to
Returns
is the delivery subsidised?

Definition at line 548 of file subsidy.cpp.

References 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(), INVALID_SOURCE, POS_SRC, BaseStation::rect, ST_INDUSTRY, and ST_TOWN.

◆ 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_typeType of src.
srcIndex of source.
dst_typeType of dst.
dstIndex of destination.
Returns
True if they are inside the distance limit.

Definition at line 201 of file subsidy.cpp.

References DistanceManhattan(), 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(), ST_TOWN, and SUBSIDY_MAX_DISTANCE.

◆ 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
cargoCargo type.
src_typeType of source of the cargo, affects interpretation of src.
srcId of the source.
dst_typeType of the destination of the cargo, affects interpretation of dst.
dstId of the destination.
Returns
true if the subsidy already exists, false if not.

Definition at line 180 of file subsidy.cpp.

◆ CmdCreateSubsidy()

CommandCost CmdCreateSubsidy ( TileIndex  tile,
DoCommandFlag  flags,
uint32  p1,
uint32  p2,
const char *  text 
)

Create a new subsidy.

Parameters
tileunused.
flagstype of operation
p1various bitstuffed elements
  • p1 = (bit 0 - 7) - SourceType of source.
  • p1 = (bit 8 - 23) - SourceID of source.
  • p1 = (bit 24 - 31) - CargoID of subsidy.
p2various bitstuffed elements
  • p2 = (bit 0 - 7) - SourceType of destination.
  • p2 = (bit 8 - 23) - SourceID of destination.
textunused.
Returns
the cost of this operation or an error

Definition at line 252 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, GB(), CargoSpec::Get(), 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, ST_INDUSTRY, and ST_TOWN.

◆ CreateSubsidy()

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

◆ DeleteSubsidyWith()

void DeleteSubsidyWith ( SourceType  type,
SourceID  index 
)

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

Parameters
typeCargo source type of the id.
indexId to remove.

Definition at line 153 of file subsidy.cpp.

◆ FindSubsidyCargoDestination()

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

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

Parameters
cidSubsidized cargo.
src_typeType of src.
srcIndex of source.
Returns
True iff the subsidy was created.

Definition at line 417 of file subsidy.cpp.

References _town_cargoes_accepted, Industry::accepts_cargo, Town::cargo_accepted_total, Chance16(), Industry::GetRandom(), Town::GetRandom(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, ST_INDUSTRY, and ST_TOWN.

◆ FindSubsidyIndustryCargoRoute()

bool FindSubsidyIndustryCargoRoute ( )

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

Returns
True iff the subsidy was created.

Definition at line 371 of file subsidy.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), CT_INVALID, Industry::GetRandom(), HasBit(), Industry::produced_cargo, and ST_INDUSTRY.

◆ FindSubsidyPassengerRoute()

bool FindSubsidyPassengerRoute ( )

Tries to create a passenger subsidy between two towns.

Returns
True iff the subsidy was created.

Definition at line 298 of file subsidy.cpp.

References Town::cache, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), Town::GetRandom(), TownCache::population, and SUBSIDY_PAX_MIN_POPULATION.

◆ FindSubsidyTownCargoRoute()

bool FindSubsidyTownCargoRoute ( )

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

Returns
True iff the subsidy was created.

Definition at line 328 of file subsidy.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_subsidy_pool >::CanAllocateItem(), Town::cargo_produced, ClrBit(), Town::GetRandom(), and ST_TOWN.

◆ RebuildSubsidisedSourceAndDestinationCache()

void RebuildSubsidisedSourceAndDestinationCache ( )

Perform a full rebuild of the subsidies cache.

Definition at line 133 of file subsidy.cpp.

◆ 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
typeis it a town or an industry?
indexindex of town/industry
flagflag to set

Definition at line 123 of file subsidy.cpp.

References 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(), ST_INDUSTRY, and ST_TOWN.

Referenced by CreateSubsidy().

◆ SetupSubsidyDecodeParam()

Pair SetupSubsidyDecodeParam ( const Subsidy s,
bool  mode 
)

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

Parameters
sSubsidy being printed.
modeUnit of cargo used, true means general name, false means singular form.
Returns
Reference of the subsidy in the news system.

Definition at line 76 of file subsidy.cpp.

References Subsidy::cargo_type, CargoSpec::Get(), CargoSpec::name, CargoSpec::name_single, NR_INDUSTRY, NR_NONE, NR_TOWN, SetDParam(), Subsidy::src_type, ST_INDUSTRY, and ST_TOWN.

Referenced by CreateSubsidy().

◆ SubsidyMonthlyLoop()

void SubsidyMonthlyLoop ( )

Perform the monthly update of open subsidies, and try to create a new one.

Definition at line 469 of file subsidy.cpp.