OpenTTD Source 20250312-master-gcdcc6b491d
|
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. | |
Handling of subsidies.
Definition in file subsidy.cpp.
Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company.
cargo_type | type of cargo |
company | company delivering the cargo |
src | source of cargo |
st | station where the cargo is delivered to |
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().
Checks if the source and destination of a subsidy are inside the distance limit.
src | Source of cargo. |
dst | Destination of cargo. |
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().
Check whether a specific subsidy already exists.
cargo | Cargo type. |
src | The source. |
dst | The destination. |
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().
CommandCost CmdCreateSubsidy | ( | DoCommandFlags | flags, |
CargoType | cargo_type, | ||
Source | src, | ||
Source | dst | ||
) |
Create a new subsidy.
flags | type of operation |
cargo_type | CargoType of subsidy. |
src | Source. |
dst | Destination. |
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.
Creates a subsidy with the given 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().
void DeleteSubsidyWith | ( | Source | source | ) |
Delete the subsidies associated with a given cargo source type and id.
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().
Tries to find a suitable destination for the given source and cargo.
cargo_type | Subsidized cargo. |
src | Source of cargo. |
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().
bool FindSubsidyIndustryCargoRoute | ( | ) |
Tries to create a cargo subsidy with an industry as source.
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.
bool FindSubsidyPassengerRoute | ( | ) |
Tries to create a passenger subsidy between two towns.
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.
bool FindSubsidyTownCargoRoute | ( | ) |
Tries to create a cargo subsidy with a town as source.
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.
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().
|
inlinestatic |
Sets a flag indicating that given town/industry is part of subsidised route.
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().