2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-05-23 15:46:00 +00:00
|
|
|
/** @file subsidy.cpp Handling of subsidies. */
|
|
|
|
|
|
|
|
#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"
|
2009-07-01 18:45:05 +00:00
|
|
|
#include "subsidy_base.h"
|
2009-08-07 20:24:33 +00:00
|
|
|
#include "subsidy_func.h"
|
2009-08-08 20:53:36 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
2023-05-18 09:20:35 +00:00
|
|
|
#include "core/container_func.hpp"
|
2011-12-19 20:59:36 +00:00
|
|
|
#include "game/game.hpp"
|
2011-12-19 21:01:12 +00:00
|
|
|
#include "command_func.h"
|
2014-04-25 15:40:32 +00:00
|
|
|
#include "string_func.h"
|
2020-09-05 22:57:42 +00:00
|
|
|
#include "tile_cmd.h"
|
2021-10-05 20:02:27 +00:00
|
|
|
#include "subsidy_cmd.h"
|
2023-04-13 11:56:00 +00:00
|
|
|
#include "timer/timer.h"
|
2024-01-22 14:04:34 +00:00
|
|
|
#include "timer/timer_game_economy.h"
|
2009-05-23 15:46:00 +00:00
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
SubsidyPool _subsidy_pool("Subsidy"); ///< Pool for the subsidies.
|
2009-08-08 20:53:36 +00:00
|
|
|
INSTANTIATE_POOL_METHODS(Subsidy)
|
2009-07-01 18:45:05 +00:00
|
|
|
|
2009-08-07 20:24:33 +00:00
|
|
|
/**
|
|
|
|
* Marks subsidy as awarded, creates news and AI event
|
|
|
|
* @param company awarded company
|
|
|
|
*/
|
2009-08-08 16:42:55 +00:00
|
|
|
void Subsidy::AwardTo(CompanyID company)
|
2009-08-07 20:24:33 +00:00
|
|
|
{
|
|
|
|
assert(!this->IsAwarded());
|
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
this->awarded = company;
|
2023-08-16 13:43:31 +00:00
|
|
|
this->remaining = _settings_game.difficulty.subsidy_duration * CalendarTime::MONTHS_IN_YEAR;
|
2009-08-07 20:24:33 +00:00
|
|
|
|
2010-12-09 15:19:43 +00:00
|
|
|
SetDParam(0, company);
|
2021-06-16 15:50:18 +00:00
|
|
|
NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
|
2009-08-07 20:24:33 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/* Add a news item */
|
2021-07-03 06:03:33 +00:00
|
|
|
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
|
2009-08-08 16:42:55 +00:00
|
|
|
|
2021-06-16 15:50:18 +00:00
|
|
|
SetDParamStr(0, company_name->string);
|
2009-08-07 20:24:33 +00:00
|
|
|
AddNewsItem(
|
|
|
|
STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
|
2012-05-26 14:16:03 +00:00
|
|
|
NT_SUBSIDIES, NF_NORMAL,
|
2021-04-21 12:42:30 +00:00
|
|
|
reftype.first, this->src, reftype.second, this->dst,
|
2021-06-16 15:50:18 +00:00
|
|
|
company_name
|
2009-08-07 20:24:33 +00:00
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
|
2009-08-07 20:24:33 +00:00
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
|
2009-08-07 20:24:33 +00:00
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
|
|
|
|
* @param s %Subsidy being printed.
|
2021-04-22 17:54:32 +00:00
|
|
|
* @param mode Type of subsidy news message to decide on parameter format.
|
2021-07-03 06:03:33 +00:00
|
|
|
* @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
|
2012-01-01 17:22:32 +00:00
|
|
|
* @return Reference of the subsidy in the news system.
|
|
|
|
*/
|
2021-07-03 06:03:33 +00:00
|
|
|
std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
2009-05-24 16:52:42 +00:00
|
|
|
NewsReferenceType reftype1 = NR_NONE;
|
|
|
|
NewsReferenceType reftype2 = NR_NONE;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2021-10-18 08:43:14 +00:00
|
|
|
/* Always use the plural form of the cargo name - trying to decide between plural or singular causes issues for translations */
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
|
2021-10-18 08:43:14 +00:00
|
|
|
SetDParam(parameter_offset, cs->name);
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
switch (s->src_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype1 = NR_INDUSTRY;
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
|
2009-08-07 22:23:34 +00:00
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype1 = NR_TOWN;
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 1, STR_TOWN_NAME);
|
2009-08-07 22:23:34 +00:00
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 2, s->src);
|
2009-08-07 22:23:34 +00:00
|
|
|
|
|
|
|
switch (s->dst_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2009-08-07 22:23:34 +00:00
|
|
|
reftype2 = NR_INDUSTRY;
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
|
2009-08-07 22:23:34 +00:00
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype2 = NR_TOWN;
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 4, STR_TOWN_NAME);
|
2009-08-07 22:23:34 +00:00
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 5, s->dst);
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2021-04-22 17:54:32 +00:00
|
|
|
/* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
|
|
|
|
if (mode == SubsidyDecodeParamType::NewsOffered || mode == SubsidyDecodeParamType::NewsAwarded) {
|
2021-07-03 06:03:33 +00:00
|
|
|
SetDParam(parameter_offset + 7, _settings_game.difficulty.subsidy_duration);
|
2021-04-22 17:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 12:42:30 +00:00
|
|
|
return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/**
|
|
|
|
* Sets a flag indicating that given town/industry is part of subsidised route.
|
|
|
|
* @param type is it a town or an industry?
|
|
|
|
* @param index index of town/industry
|
|
|
|
* @param flag flag to set
|
|
|
|
*/
|
|
|
|
static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
|
|
|
|
{
|
|
|
|
switch (type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry: Industry::Get(index)->part_of_subsidy |= flag; return;
|
|
|
|
case SourceType::Town: Town::Get(index)->cache.part_of_subsidy |= flag; return;
|
2009-08-08 16:42:55 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/** Perform a full rebuild of the subsidies cache. */
|
2009-08-08 16:42:55 +00:00
|
|
|
void RebuildSubsidisedSourceAndDestinationCache()
|
|
|
|
{
|
2019-12-17 21:04:09 +00:00
|
|
|
for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE;
|
2009-08-08 16:42:55 +00:00
|
|
|
|
2019-12-16 17:51:20 +00:00
|
|
|
for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;
|
2009-08-08 16:42:55 +00:00
|
|
|
|
2019-12-17 20:22:15 +00:00
|
|
|
for (const Subsidy *s : Subsidy::Iterate()) {
|
2009-08-08 16:42:55 +00:00
|
|
|
SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
|
|
|
|
SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Delete the subsidies associated with a given cargo source type and id.
|
|
|
|
* @param type Cargo source type of the id.
|
|
|
|
* @param index Id to remove.
|
|
|
|
*/
|
2009-08-07 22:23:34 +00:00
|
|
|
void DeleteSubsidyWith(SourceType type, SourceID index)
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
|
|
|
bool dirty = false;
|
|
|
|
|
2019-12-17 20:22:15 +00:00
|
|
|
for (Subsidy *s : Subsidy::Iterate()) {
|
2009-08-07 22:23:34 +00:00
|
|
|
if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
|
2009-08-08 20:53:36 +00:00
|
|
|
delete s;
|
2009-05-23 15:46:00 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 20:53:36 +00:00
|
|
|
if (dirty) {
|
2009-09-01 20:42:12 +00:00
|
|
|
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
|
2009-08-08 20:53:36 +00:00
|
|
|
RebuildSubsidisedSourceAndDestinationCache();
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Check whether a specific subsidy already exists.
|
|
|
|
* @param cargo Cargo type.
|
|
|
|
* @param src_type Type of source of the cargo, affects interpretation of \a src.
|
|
|
|
* @param src Id of the source.
|
|
|
|
* @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
|
|
|
|
* @param dst Id of the destination.
|
|
|
|
* @return \c true if the subsidy already exists, \c false if not.
|
|
|
|
*/
|
2009-08-08 22:58:49 +00:00
|
|
|
static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
|
|
|
|
{
|
2019-12-17 20:22:15 +00:00
|
|
|
for (const Subsidy *s : Subsidy::Iterate()) {
|
2009-08-08 22:58:49 +00:00
|
|
|
if (s->cargo_type == cargo &&
|
|
|
|
s->src_type == src_type && s->src == src &&
|
|
|
|
s->dst_type == dst_type && s->dst == dst) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Checks if the source and destination of a subsidy are inside the distance limit.
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param src_type Type of \a src.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param src Index of source.
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param dst_type Type of \a dst.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param dst Index of destination.
|
|
|
|
* @return True if they are inside the distance limit.
|
|
|
|
*/
|
|
|
|
static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
|
|
|
|
{
|
2023-04-16 19:00:55 +00:00
|
|
|
TileIndex tile_src = (src_type == SourceType::Town) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
|
|
|
|
TileIndex tile_dst = (dst_type == SourceType::Town) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
|
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Creates a subsidy with the given parameters.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param cid Subsidised cargo.
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param src_type Type of \a src.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param src Index of source.
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param dst_type Type of \a dst.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param dst Index of destination.
|
|
|
|
*/
|
|
|
|
void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
|
|
|
|
{
|
|
|
|
Subsidy *s = new Subsidy();
|
|
|
|
s->cargo_type = cid;
|
|
|
|
s->src_type = src_type;
|
|
|
|
s->src = src;
|
|
|
|
s->dst_type = dst_type;
|
|
|
|
s->dst = dst;
|
|
|
|
s->remaining = SUBSIDY_OFFER_MONTHS;
|
|
|
|
s->awarded = INVALID_COMPANY;
|
|
|
|
|
2021-04-22 17:54:32 +00:00
|
|
|
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsOffered);
|
2021-04-21 12:42:30 +00:00
|
|
|
AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
|
2011-12-03 22:26:30 +00:00
|
|
|
SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
|
|
|
|
SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
|
|
|
|
AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
|
2011-12-19 21:01:12 +00:00
|
|
|
|
|
|
|
InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
|
2011-12-03 22:26:30 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 21:01:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new subsidy.
|
|
|
|
* @param flags type of operation
|
2021-11-23 00:05:58 +00:00
|
|
|
* @param cid CargoID of subsidy.
|
|
|
|
* @param src_type SourceType of source.
|
|
|
|
* @param src SourceID of source.
|
|
|
|
* @param dst_type SourceType of destination.
|
|
|
|
* @param dst SourceID of destination.
|
2011-12-19 21:01:12 +00:00
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
2021-11-23 00:05:58 +00:00
|
|
|
CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
|
2011-12-19 21:01:12 +00:00
|
|
|
{
|
|
|
|
if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
|
|
|
|
|
|
|
|
switch (src_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2011-12-19 21:01:12 +00:00
|
|
|
if (!Town::IsValidID(src)) return CMD_ERROR;
|
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2011-12-19 21:01:12 +00:00
|
|
|
if (!Industry::IsValidID(src)) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
switch (dst_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2011-12-19 21:01:12 +00:00
|
|
|
if (!Town::IsValidID(dst)) return CMD_ERROR;
|
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2011-12-19 21:01:12 +00:00
|
|
|
if (!Industry::IsValidID(dst)) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
CreateSubsidy(cid, src_type, src, dst_type, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
2011-12-03 22:26:30 +00:00
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Tries to create a passenger subsidy between two towns.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @return True iff the subsidy was created.
|
|
|
|
*/
|
|
|
|
bool FindSubsidyPassengerRoute()
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
2011-12-03 22:26:30 +00:00
|
|
|
if (!Subsidy::CanAllocateItem()) return false;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2024-01-07 19:32:42 +00:00
|
|
|
/* Pick a random TPE_PASSENGER type */
|
|
|
|
uint32_t r = RandomRange(static_cast<uint>(CargoSpec::town_production_cargoes[TPE_PASSENGERS].size()));
|
|
|
|
CargoID cid = CargoSpec::town_production_cargoes[TPE_PASSENGERS][r]->Index();
|
|
|
|
|
2009-08-08 22:58:49 +00:00
|
|
|
const Town *src = Town::GetRandom();
|
2012-04-25 20:50:13 +00:00
|
|
|
if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
|
2024-01-07 19:32:42 +00:00
|
|
|
src->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) {
|
2011-12-03 22:26:30 +00:00
|
|
|
return false;
|
2009-08-08 22:58:49 +00:00
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2009-08-08 22:58:49 +00:00
|
|
|
const Town *dst = Town::GetRandom();
|
2012-04-25 20:50:13 +00:00
|
|
|
if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
|
2011-12-03 22:26:30 +00:00
|
|
|
return false;
|
2009-08-08 18:26:25 +00:00
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return false;
|
2024-01-07 19:32:42 +00:00
|
|
|
if (CheckSubsidyDuplicate(cid, SourceType::Town, src->index, SourceType::Town, dst->index)) return false;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2024-01-07 19:32:42 +00:00
|
|
|
CreateSubsidy(cid, SourceType::Town, src->index, SourceType::Town, dst->index);
|
2009-08-08 22:58:49 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
return true;
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
|
|
|
|
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Tries to create a cargo subsidy with a town as source.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @return True iff the subsidy was created.
|
|
|
|
*/
|
|
|
|
bool FindSubsidyTownCargoRoute()
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
2011-12-03 22:26:30 +00:00
|
|
|
if (!Subsidy::CanAllocateItem()) return false;
|
|
|
|
|
2023-04-16 19:00:55 +00:00
|
|
|
SourceType src_type = SourceType::Town;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
/* Select a random town. */
|
|
|
|
const Town *src_town = Town::GetRandom();
|
2019-04-11 06:35:46 +00:00
|
|
|
if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
2020-09-05 22:57:42 +00:00
|
|
|
/* Calculate the produced cargo of houses around town center. */
|
2023-05-23 11:23:50 +00:00
|
|
|
CargoArray town_cargo_produced{};
|
2020-09-05 22:57:42 +00:00
|
|
|
TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex tile : ta) {
|
2020-09-05 22:57:42 +00:00
|
|
|
if (IsTileType(tile, MP_HOUSE)) {
|
|
|
|
AddProducedCargo(tile, town_cargo_produced);
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
/* Passenger subsidies are not handled here. */
|
2024-01-07 19:32:42 +00:00
|
|
|
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_PASSENGERS]) {
|
|
|
|
town_cargo_produced[cs->Index()] = 0;
|
|
|
|
}
|
2020-05-18 13:12:20 +00:00
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
uint8_t cargo_count = town_cargo_produced.GetCount();
|
2011-12-03 22:26:30 +00:00
|
|
|
|
2012-04-21 20:03:58 +00:00
|
|
|
/* No cargo produced at all? */
|
2020-05-18 13:12:20 +00:00
|
|
|
if (cargo_count == 0) return false;
|
2012-04-21 20:03:58 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Choose a random cargo that is produced in the town. */
|
2023-05-08 17:01:06 +00:00
|
|
|
uint8_t cargo_number = RandomRange(cargo_count);
|
2011-12-03 22:26:30 +00:00
|
|
|
CargoID cid;
|
2020-05-18 13:12:20 +00:00
|
|
|
for (cid = 0; cid < NUM_CARGO; cid++) {
|
|
|
|
if (town_cargo_produced[cid] > 0) {
|
|
|
|
if (cargo_number == 0) break;
|
|
|
|
cargo_number--;
|
|
|
|
}
|
2011-12-03 22:26:30 +00:00
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-15 21:56:00 +00:00
|
|
|
/* Avoid using invalid NewGRF cargoes. */
|
2013-10-19 11:17:29 +00:00
|
|
|
if (!CargoSpec::Get(cid)->IsValid() ||
|
|
|
|
_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
/* Quit if the percentage transported is large enough. */
|
|
|
|
if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
|
|
|
|
|
|
|
|
SourceID src = src_town->index;
|
|
|
|
|
|
|
|
return FindSubsidyCargoDestination(cid, src_type, src);
|
|
|
|
}
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Tries to create a cargo subsidy with an industry as source.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @return True iff the subsidy was created.
|
|
|
|
*/
|
|
|
|
bool FindSubsidyIndustryCargoRoute()
|
|
|
|
{
|
|
|
|
if (!Subsidy::CanAllocateItem()) return false;
|
|
|
|
|
2023-04-16 19:00:55 +00:00
|
|
|
SourceType src_type = SourceType::Industry;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
/* Select a random industry. */
|
|
|
|
const Industry *src_ind = Industry::GetRandom();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (src_ind == nullptr) return false;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2010-05-13 09:44:44 +00:00
|
|
|
uint trans, total;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
CargoID cid;
|
|
|
|
|
2009-05-23 15:46:00 +00:00
|
|
|
/* Randomize cargo type */
|
2023-05-25 20:25:46 +00:00
|
|
|
int num_cargos = std::count_if(std::begin(src_ind->produced), std::end(src_ind->produced), [](const auto &p) { return IsValidCargoID(p.cargo); });
|
2018-07-25 17:20:17 +00:00
|
|
|
if (num_cargos == 0) return false; // industry produces nothing
|
|
|
|
int cargo_num = RandomRange(num_cargos) + 1;
|
2023-05-25 20:25:46 +00:00
|
|
|
|
|
|
|
auto it = std::begin(src_ind->produced);
|
|
|
|
for (/* nothing */; it != std::end(src_ind->produced); ++it) {
|
|
|
|
if (IsValidCargoID(it->cargo)) cargo_num--;
|
2018-07-25 17:20:17 +00:00
|
|
|
if (cargo_num == 0) break;
|
|
|
|
}
|
2023-05-25 20:25:46 +00:00
|
|
|
assert(it != std::end(src_ind->produced)); // indicates loop didn't end as intended
|
|
|
|
|
|
|
|
cid = it->cargo;
|
|
|
|
trans = it->history[LAST_MONTH].PctTransported();
|
|
|
|
total = it->history[LAST_MONTH].production;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
|
|
|
/* Quit if no production in this industry
|
2013-10-19 11:17:29 +00:00
|
|
|
* or if the pct transported is already large enough
|
|
|
|
* or if the cargo is automatically distributed */
|
|
|
|
if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
|
2023-05-04 10:29:21 +00:00
|
|
|
!IsValidCargoID(cid) ||
|
2013-10-19 11:17:29 +00:00
|
|
|
_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
SourceID src = src_ind->index;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
return FindSubsidyCargoDestination(cid, src_type, src);
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/**
|
|
|
|
* Tries to find a suitable destination for the given source and cargo.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param cid Subsidized cargo.
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param src_type Type of \a src.
|
2011-12-03 22:26:30 +00:00
|
|
|
* @param src Index of source.
|
|
|
|
* @return True iff the subsidy was created.
|
|
|
|
*/
|
|
|
|
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
|
|
|
|
{
|
2020-05-18 13:12:20 +00:00
|
|
|
/* Choose a random destination. */
|
2023-04-16 19:00:55 +00:00
|
|
|
SourceType dst_type = Chance16(1, 2) ? SourceType::Town : SourceType::Industry;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
SourceID dst;
|
|
|
|
switch (dst_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town: {
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Select a random town. */
|
|
|
|
const Town *dst_town = Town::GetRandom();
|
2020-09-05 22:57:42 +00:00
|
|
|
|
|
|
|
/* Calculate cargo acceptance of houses around town center. */
|
2023-05-23 11:23:50 +00:00
|
|
|
CargoArray town_cargo_accepted{};
|
2020-09-05 22:57:42 +00:00
|
|
|
TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
2021-05-12 14:45:28 +00:00
|
|
|
for (TileIndex tile : ta) {
|
2020-09-05 22:57:42 +00:00
|
|
|
if (IsTileType(tile, MP_HOUSE)) {
|
|
|
|
AddAcceptedCargo(tile, town_cargo_accepted, nullptr);
|
|
|
|
}
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Check if the town can accept this cargo. */
|
2020-05-18 14:32:05 +00:00
|
|
|
if (town_cargo_accepted[cid] < 8) return false;
|
2009-08-08 22:58:49 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
dst = dst_town->index;
|
|
|
|
break;
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry: {
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Select a random industry. */
|
|
|
|
const Industry *dst_ind = Industry::GetRandom();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (dst_ind == nullptr) return false;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
/* The industry must accept the cargo */
|
2023-05-23 23:52:44 +00:00
|
|
|
if (!dst_ind->IsCargoAccepted(cid)) return false;
|
2011-12-03 22:26:30 +00:00
|
|
|
|
|
|
|
dst = dst_ind->index;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-08 22:58:49 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
default: NOT_REACHED();
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Check that the source and the destination are not the same. */
|
|
|
|
if (src_type == dst_type && src == dst) return false;
|
2009-08-08 22:58:49 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Check distance between source and destination. */
|
|
|
|
if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
|
2009-08-08 22:58:49 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Avoid duplicate subsidies. */
|
|
|
|
if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
|
|
|
|
|
|
|
|
CreateSubsidy(cid, src_type, src, dst_type, dst);
|
|
|
|
|
|
|
|
return true;
|
2009-08-08 22:58:49 +00:00
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2024-01-22 14:04:34 +00:00
|
|
|
/** Perform the economy monthly update of open subsidies, and try to create a new one. */
|
|
|
|
static IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto)
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
|
2019-12-17 20:22:15 +00:00
|
|
|
for (Subsidy *s : Subsidy::Iterate()) {
|
2009-08-08 16:42:55 +00:00
|
|
|
if (--s->remaining == 0) {
|
|
|
|
if (!s->IsAwarded()) {
|
2021-04-22 17:54:32 +00:00
|
|
|
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
|
2021-04-21 12:42:30 +00:00
|
|
|
AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
|
2009-08-08 16:42:55 +00:00
|
|
|
} else {
|
|
|
|
if (s->awarded == _local_company) {
|
2021-04-22 17:54:32 +00:00
|
|
|
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);
|
2021-04-21 12:42:30 +00:00
|
|
|
AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);
|
2009-08-08 16:42:55 +00:00
|
|
|
}
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
2009-08-08 20:53:36 +00:00
|
|
|
delete s;
|
2009-05-23 15:46:00 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-19 11:17:29 +00:00
|
|
|
if (modified) {
|
|
|
|
RebuildSubsidisedSourceAndDestinationCache();
|
2021-04-22 17:54:32 +00:00
|
|
|
} else if (_settings_game.difficulty.subsidy_duration == 0) {
|
|
|
|
/* If subsidy duration is set to 0, subsidies are disabled, so bail out. */
|
|
|
|
return;
|
2013-10-19 11:17:29 +00:00
|
|
|
} 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 early if there are no manually distributed cargoes and if we
|
|
|
|
* don't need to invalidate the subsidies window. */
|
|
|
|
return;
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
bool passenger_subsidy = false;
|
|
|
|
bool town_subsidy = false;
|
|
|
|
bool industry_subsidy = false;
|
|
|
|
|
|
|
|
int random_chance = RandomRange(16);
|
|
|
|
|
2013-10-19 11:17:29 +00:00
|
|
|
if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
|
2011-12-03 22:26:30 +00:00
|
|
|
/* There is a 1/8 chance each month of generating a passenger subsidy. */
|
|
|
|
int n = 1000;
|
|
|
|
|
2009-05-23 15:46:00 +00:00
|
|
|
do {
|
2011-12-03 22:26:30 +00:00
|
|
|
passenger_subsidy = FindSubsidyPassengerRoute();
|
|
|
|
} while (!passenger_subsidy && n--);
|
|
|
|
} else if (random_chance == 2) {
|
|
|
|
/* Cargo subsidies with a town as a source have a 1/16 chance. */
|
|
|
|
int n = 1000;
|
|
|
|
|
|
|
|
do {
|
|
|
|
town_subsidy = FindSubsidyTownCargoRoute();
|
|
|
|
} while (!town_subsidy && n--);
|
|
|
|
} else if (random_chance == 3) {
|
|
|
|
/* Cargo subsidies with an industry as a source have a 1/16 chance. */
|
|
|
|
int n = 1000;
|
|
|
|
|
|
|
|
do {
|
2012-02-02 19:31:11 +00:00
|
|
|
industry_subsidy = FindSubsidyIndustryCargoRoute();
|
2011-12-03 22:26:30 +00:00
|
|
|
} while (!industry_subsidy && n--);
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
2009-08-08 20:53:36 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
modified |= passenger_subsidy || town_subsidy || industry_subsidy;
|
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
|
2023-04-13 11:56:00 +00:00
|
|
|
});
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/**
|
|
|
|
* Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
|
|
|
|
* @param cargo_type type of cargo
|
|
|
|
* @param company company delivering the cargo
|
2012-07-10 18:37:54 +00:00
|
|
|
* @param src_type type of \a src
|
2009-08-08 16:42:55 +00:00
|
|
|
* @param src index of source
|
|
|
|
* @param st station where the cargo is delivered to
|
|
|
|
* @return is the delivery subsidised?
|
|
|
|
*/
|
|
|
|
bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
|
2009-05-23 15:46:00 +00:00
|
|
|
{
|
2009-08-08 16:42:55 +00:00
|
|
|
/* If the source isn't subsidised, don't continue */
|
|
|
|
if (src == INVALID_SOURCE) return false;
|
|
|
|
switch (src_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2009-08-08 16:42:55 +00:00
|
|
|
if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
|
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2012-04-25 20:50:13 +00:00
|
|
|
if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
|
2009-08-08 16:42:55 +00:00
|
|
|
break;
|
|
|
|
default: return false;
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/* Remember all towns near this station (at least one house in its catchment radius)
|
|
|
|
* which are destination of subsidised path. Do that only if needed */
|
2019-03-03 17:30:09 +00:00
|
|
|
std::vector<const Town *> towns_near;
|
2009-08-08 16:42:55 +00:00
|
|
|
if (!st->rect.IsEmpty()) {
|
2019-12-17 20:22:15 +00:00
|
|
|
for (const Subsidy *s : Subsidy::Iterate()) {
|
2009-08-08 16:42:55 +00:00
|
|
|
/* Don't create the cache if there is no applicable subsidy with town as destination */
|
2023-04-16 19:00:55 +00:00
|
|
|
if (s->dst_type != SourceType::Town) continue;
|
2009-08-08 16:42:55 +00:00
|
|
|
if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
|
|
|
|
if (s->IsAwarded() && s->awarded != company) continue;
|
|
|
|
|
2019-02-14 21:07:15 +00:00
|
|
|
BitmapTileIterator it(st->catchment_tiles);
|
|
|
|
for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) {
|
|
|
|
if (!IsTileType(tile, MP_HOUSE)) continue;
|
|
|
|
const Town *t = Town::GetByTile(tile);
|
2019-02-20 21:35:41 +00:00
|
|
|
if (t->cache.part_of_subsidy & POS_DST) include(towns_near, t);
|
2009-08-08 16:42:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
bool subsidised = false;
|
|
|
|
|
|
|
|
/* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
|
|
|
|
* Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
|
2019-12-17 20:22:15 +00:00
|
|
|
for (Subsidy *s : Subsidy::Iterate()) {
|
2009-08-08 16:42:55 +00:00
|
|
|
if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
|
|
|
|
switch (s->dst_type) {
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Industry:
|
2022-02-19 18:08:23 +00:00
|
|
|
for (const auto &i : st->industries_near) {
|
|
|
|
if (s->dst == i.industry->index) {
|
|
|
|
assert(i.industry->part_of_subsidy & POS_DST);
|
2009-08-08 16:42:55 +00:00
|
|
|
subsidised = true;
|
|
|
|
if (!s->IsAwarded()) s->AwardTo(company);
|
|
|
|
}
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
break;
|
2023-04-16 19:00:55 +00:00
|
|
|
case SourceType::Town:
|
2019-02-17 11:20:52 +00:00
|
|
|
for (const Town *tp : towns_near) {
|
|
|
|
if (s->dst == tp->index) {
|
|
|
|
assert(tp->cache.part_of_subsidy & POS_DST);
|
2009-08-08 16:42:55 +00:00
|
|
|
subsidised = true;
|
|
|
|
if (!s->IsAwarded()) s->AwardTo(company);
|
|
|
|
}
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
break;
|
2009-08-08 16:42:55 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-08 16:42:55 +00:00
|
|
|
|
|
|
|
return subsidised;
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|