OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
32 #include "table/strings.h"
45 assert(!this->IsAwarded());
47 this->awarded = company;
60 reftype.first, this->src, reftype.second, this->dst,
88 SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
92 SetDParam(parameter_offset + 1, STR_TOWN_NAME);
94 default: NOT_REACHED();
101 SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
105 SetDParam(parameter_offset + 4, STR_TOWN_NAME);
107 default: NOT_REACHED();
116 return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
130 default: NOT_REACHED();
157 if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
181 if (s->cargo_type == cargo &&
182 s->src_type == src_type && s->src == src &&
183 s->dst_type == dst_type && s->dst == dst) {
334 AddProducedCargo(tile, town_cargo_produced);
340 town_cargo_produced[cs->Index()] = 0;
343 uint8_t cargo_count = town_cargo_produced.GetCount();
346 if (cargo_count == 0)
return false;
352 if (town_cargo_produced[cid] > 0) {
353 if (cargo_number == 0)
break;
384 if (src_ind ==
nullptr)
return false;
391 int num_cargos = std::count_if(std::begin(src_ind->
produced), std::end(src_ind->
produced), [](
const auto &p) { return IsValidCargoID(p.cargo); });
392 if (num_cargos == 0)
return false;
395 auto it = std::begin(src_ind->
produced);
396 for (; it != std::end(src_ind->
produced); ++it) {
398 if (cargo_num == 0)
break;
400 assert(it != std::end(src_ind->
produced));
403 trans = it->history[LAST_MONTH].PctTransported();
404 total = it->history[LAST_MONTH].production;
443 AddAcceptedCargo(tile, town_cargo_accepted,
nullptr);
448 if (town_cargo_accepted[cid] < 8)
return false;
450 dst = dst_town->
index;
457 if (dst_ind ==
nullptr)
return false;
462 dst = dst_ind->
index;
466 default: NOT_REACHED();
470 if (src_type == dst_type && src == dst)
return false;
486 bool modified =
false;
489 if (--s->remaining == 0) {
490 if (!s->IsAwarded()) {
522 bool passenger_subsidy =
false;
523 bool town_subsidy =
false;
524 bool industry_subsidy =
false;
534 }
while (!passenger_subsidy && n--);
535 }
else if (random_chance == 2) {
541 }
while (!town_subsidy && n--);
542 }
else if (random_chance == 3) {
548 }
while (!industry_subsidy && n--);
551 modified |= passenger_subsidy || town_subsidy || industry_subsidy;
576 default:
return false;
581 std::vector<const Town *> towns_near;
582 if (!st->
rect.IsEmpty()) {
586 if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src)
continue;
587 if (s->IsAwarded() && s->awarded != company)
continue;
592 const Town *t = Town::GetByTile(tile);
599 bool subsidised =
false;
604 if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
605 switch (s->dst_type) {
608 if (s->dst == i.industry->index) {
609 assert(i.industry->part_of_subsidy &
POS_DST);
611 if (!s->IsAwarded()) s->AwardTo(company);
616 for (
const Town *tp : towns_near) {
617 if (s->dst == tp->index) {
618 assert(tp->cache.part_of_subsidy &
POS_DST);
620 if (!s->IsAwarded()) s->AwardTo(company);
@ MP_HOUSE
A house by a town.
@ OWNER_DEITY
The object is owned by a superuser / goal script.
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Check whether a specific subsidy already exists.
static Titem * Get(size_t index)
Returns Titem with given index.
uint16_t remaining
Remaining months when this subsidy is valid.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ NewsAwarded
News item for an awarded subsidy.
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
@ INVALID_COMPANY
An invalid company.
@ NT_SUBSIDIES
News about subsidies (announcements, expirations, acceptance)
Iterator to iterate over all tiles belonging to a bitmaptilearea.
An interval timer will fire every interval, and will continue to fire until it is deleted.
static const uint SUBSIDY_OFFER_MONTHS
Constants related to subsidies.
Owner
Enum for all companies/owners.
Tindex index
Index of this pool item.
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
DistributionType distribution_mail
distribution type for mail
static const uint SUBSIDY_MAX_PCT_TRANSPORTED
Subsidy will be created only for towns/industries with less % transported.
Class for storing amounts of cargo.
@ NewsOffered
News item for an offered subsidy.
DifficultySettings difficulty
settings related to the difficulty
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
PartOfSubsidy part_of_subsidy
Is this town a source/destination of a subsidy?
TileIndex xy
town center tile
Specification of a cargo type.
Defines the internal data of a functional industry.
NewsReferenceType
References to objects in news.
@ DC_EXEC
execute the given command
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 fo...
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
DoCommandFlag
List of flags for a command.
SourceType
Types of cargo source and destination.
static Town * GetRandom()
Return a random valid town.
bool FindSubsidyTownCargoRoute()
Tries to create a cargo subsidy with a town as source.
@ NR_TOWN
Reference town. Scroll to town when clicking on the news.
uint8_t subsidy_multiplier
payment multiplier for subsidized deliveries
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
static Industry * GetRandom()
Return a random valid industry.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
static const uint SUBSIDY_CARGO_MIN_POPULATION
Min. population of destination town for cargo route.
CargoID cargo_type
Cargo type involved in this subsidy, INVALID_CARGO for invalid subsidy.
Struct about subsidies, offered and awarded.
ProducedCargoes produced
produced cargo slots
static const uint SUBSIDY_TOWN_CARGO_RADIUS
Extent of a tile area around town center when scanning for town cargo acceptance and production (6 ~=...
Common return value for all commands.
uint16_t subsidy_duration
duration of subsidies
bool FindSubsidyIndustryCargoRoute()
Tries to create a cargo subsidy with an industry as source.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
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.
static std::array< std::vector< const CargoSpec * >, NUM_TPE > town_production_cargoes
List of cargo specs for each Town Product Effect.
SourceID src
Index of source. Either TownID or IndustryID.
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
static void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
Sets a flag indicating that given town/industry is part of subsidised route.
Represents the covered area of e.g.
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Container for a single string to be passed as NewsAllocatedData.
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
SubsidyPool _subsidy_pool("Subsidy")
Pool for the subsidies.
SourceID dst
Index of destination. Either TownID or IndustryID.
LinkGraphSettings linkgraph
settings for link graph calculations
@ NR_INDUSTRY
Reference industry. Scroll to industry when clicking on the news. Delete news when industry is delete...
@ NF_NORMAL
Normal news item. (Newspaper with text only)
@ NewsWithdrawn
News item for a subsidy offer withdrawn, or expired subsidy.
@ Town
Source/destination is a town.
uint32_t population
Current population of people.
@ DT_MANUAL
Manual distribution. No link graph calculations are run.
SourceType dst_type
Destination of subsidised path (SourceType::Industry or SourceType::Town)
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
CompanyID _current_company
Company currently doing an action.
@ POS_SRC
bit 0 set -> town/industry is source of subsidised path
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Base class for all pools.
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
static constexpr int MONTHS_IN_YEAR
months per year
static const uint SUBSIDY_MAX_DISTANCE
Max. length of subsidised route (DistanceManhattan)
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Create a new subsidy.
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32_t ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32_t ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
TownCache cache
Container for all cacheable data.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
StringID name
Name of this type of cargo.
BitmapTileArea catchment_tiles
NOSAVE: Set of individual tiles covered by catchment area.
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
Tries to find a suitable destination for the given source and cargo.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Creates a subsidy with the given parameters.
SubsidyDecodeParamType
Types of subsidy news messages, which determine how the date is printed and whether to use singular o...
std::string string
The string to retain.
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
DistributionType distribution_armoured
distribution type for armoured cargo class
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.
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
SourceType src_type
Source of subsidised path (SourceType::Industry or SourceType::Town)
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone.
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.
DistributionType distribution_pax
distribution type for passengers
@ Industry
Source/destination is an industry.
@ NR_NONE
Empty reference.
@ POS_DST
bit 1 set -> town/industry is destination of subsidised path
DistributionType distribution_default
distribution type for all other goods
static const uint SUBSIDY_PAX_MIN_POPULATION
Min. population of towns for subsidised pax route.
bool FindSubsidyPassengerRoute()
Tries to create a passenger subsidy between two towns.
PartOfSubsidy
What part of a subsidy is something?