2009-05-23 15:46:00 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2011-12-19 20:59:36 +00:00
|
|
|
#include "game/game.hpp"
|
2011-12-19 21:01:12 +00:00
|
|
|
#include "command_func.h"
|
2009-05-23 15:46:00 +00:00
|
|
|
|
|
|
|
#include "table/strings.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;
|
2009-08-08 18:26:25 +00:00
|
|
|
this->remaining = SUBSIDY_CONTRACT_MONTHS;
|
2009-08-07 20:24:33 +00:00
|
|
|
|
2010-12-05 22:24:50 +00:00
|
|
|
char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
|
2010-12-09 15:19:43 +00:00
|
|
|
SetDParam(0, company);
|
2010-12-05 22:24:50 +00:00
|
|
|
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
|
|
|
|
|
|
|
|
char *cn = strdup(company_name);
|
2009-08-07 20:24:33 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/* Add a news item */
|
2010-11-24 01:08:49 +00:00
|
|
|
Pair reftype = SetupSubsidyDecodeParam(this, false);
|
2009-08-08 16:42:55 +00:00
|
|
|
InjectDParam(1);
|
|
|
|
|
2010-12-05 22:24:50 +00:00
|
|
|
SetDParamStr(0, cn);
|
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,
|
2009-08-07 22:23:34 +00:00
|
|
|
(NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst,
|
2010-12-05 22:24:50 +00:00
|
|
|
cn
|
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.
|
|
|
|
* @param mode Unit of cargo used, \c true means general name, \c false means singular form.
|
|
|
|
* @return Reference of the subsidy in the news system.
|
|
|
|
*/
|
2009-05-23 15:46:00 +00:00
|
|
|
Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
|
|
|
|
{
|
2009-05-24 16:52:42 +00:00
|
|
|
NewsReferenceType reftype1 = NR_NONE;
|
|
|
|
NewsReferenceType reftype2 = NR_NONE;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
|
|
|
/* if mode is false, use the singular form */
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
|
2009-05-23 15:46:00 +00:00
|
|
|
SetDParam(0, mode ? cs->name : cs->name_single);
|
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
switch (s->src_type) {
|
|
|
|
case ST_INDUSTRY:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype1 = NR_INDUSTRY;
|
2009-08-07 22:23:34 +00:00
|
|
|
SetDParam(1, STR_INDUSTRY_NAME);
|
|
|
|
break;
|
|
|
|
case ST_TOWN:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype1 = NR_TOWN;
|
2009-08-07 22:23:34 +00:00
|
|
|
SetDParam(1, STR_TOWN_NAME);
|
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
SetDParam(2, s->src);
|
|
|
|
|
|
|
|
switch (s->dst_type) {
|
|
|
|
case ST_INDUSTRY:
|
|
|
|
reftype2 = NR_INDUSTRY;
|
|
|
|
SetDParam(4, STR_INDUSTRY_NAME);
|
|
|
|
break;
|
|
|
|
case ST_TOWN:
|
2009-05-24 16:52:42 +00:00
|
|
|
reftype2 = NR_TOWN;
|
2009-08-07 22:23:34 +00:00
|
|
|
SetDParam(4, STR_TOWN_NAME);
|
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
2009-08-07 22:23:34 +00:00
|
|
|
SetDParam(5, s->dst);
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2009-05-24 16:52:42 +00:00
|
|
|
Pair p;
|
|
|
|
p.a = reftype1;
|
|
|
|
p.b = reftype2;
|
|
|
|
return p;
|
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) {
|
|
|
|
case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
|
2012-04-25 20:50:13 +00:00
|
|
|
case ST_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()
|
|
|
|
{
|
|
|
|
Town *t;
|
2012-04-25 20:50:13 +00:00
|
|
|
FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
|
2009-08-08 16:42:55 +00:00
|
|
|
|
|
|
|
Industry *i;
|
|
|
|
FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
|
|
|
|
|
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
|
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;
|
|
|
|
|
2009-07-01 17:43:26 +00:00
|
|
|
Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
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)
|
|
|
|
{
|
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
TileIndex tile_src = (src_type == ST_TOWN) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
|
|
|
|
TileIndex tile_dst = (dst_type == ST_TOWN) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
Pair reftype = SetupSubsidyDecodeParam(s, false);
|
2012-05-26 14:16:03 +00:00
|
|
|
AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, 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 tile unused.
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 various bitstuffed elements
|
|
|
|
* - p1 = (bit 0 - 7) - SourceType of source.
|
|
|
|
* - p1 = (bit 8 - 23) - SourceID of source.
|
|
|
|
* - p1 = (bit 24 - 31) - CargoID of subsidy.
|
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0 - 7) - SourceType of destination.
|
|
|
|
* - p2 = (bit 8 - 23) - SourceID of destionation.
|
|
|
|
* @param text unused.
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
|
|
|
CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CargoID cid = GB(p1, 24, 8);
|
|
|
|
SourceType src_type = (SourceType)GB(p1, 0, 8);
|
|
|
|
SourceID src = GB(p1, 8, 16);
|
|
|
|
SourceType dst_type = (SourceType)GB(p2, 0, 8);
|
|
|
|
SourceID dst = GB(p2, 8, 16);
|
|
|
|
|
|
|
|
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
|
|
|
|
|
|
|
|
switch (src_type) {
|
|
|
|
case ST_TOWN:
|
|
|
|
if (!Town::IsValidID(src)) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
case ST_INDUSTRY:
|
|
|
|
if (!Industry::IsValidID(src)) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
switch (dst_type) {
|
|
|
|
case ST_TOWN:
|
|
|
|
if (!Town::IsValidID(dst)) return CMD_ERROR;
|
|
|
|
break;
|
|
|
|
case ST_INDUSTRY:
|
|
|
|
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
|
|
|
|
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 ||
|
2011-11-23 16:05:19 +00:00
|
|
|
src->GetPercentTransported(CT_PASSENGERS) > 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;
|
|
|
|
if (CheckSubsidyDuplicate(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index)) return false;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_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;
|
|
|
|
|
|
|
|
SourceType src_type = ST_TOWN;
|
|
|
|
|
|
|
|
/* Select a random town. */
|
|
|
|
const Town *src_town = Town::GetRandom();
|
|
|
|
|
|
|
|
uint32 town_cargo_produced = src_town->cargo_produced;
|
|
|
|
|
|
|
|
/* Passenger subsidies are not handled here. */
|
|
|
|
ClrBit(town_cargo_produced, CT_PASSENGERS);
|
|
|
|
|
2012-04-21 20:03:58 +00:00
|
|
|
/* No cargo produced at all? */
|
|
|
|
if (town_cargo_produced == 0) return false;
|
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Choose a random cargo that is produced in the town. */
|
|
|
|
uint8 cargo_number = RandomRange(CountBits(town_cargo_produced));
|
|
|
|
CargoID cid;
|
|
|
|
FOR_EACH_SET_CARGO_ID(cid, town_cargo_produced) {
|
|
|
|
if (cargo_number == 0) break;
|
|
|
|
cargo_number--;
|
|
|
|
}
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-15 21:56:00 +00:00
|
|
|
/* Avoid using invalid NewGRF cargoes. */
|
2011-12-03 22:26:30 +00:00
|
|
|
if (!CargoSpec::Get(cid)->IsValid()) return false;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
SourceType src_type = ST_INDUSTRY;
|
|
|
|
|
|
|
|
/* Select a random industry. */
|
|
|
|
const Industry *src_ind = Industry::GetRandom();
|
|
|
|
if (src_ind == NULL) 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 */
|
2011-12-03 22:26:30 +00:00
|
|
|
if (src_ind->produced_cargo[1] != CT_INVALID && HasBit(Random(), 0)) {
|
|
|
|
cid = src_ind->produced_cargo[1];
|
|
|
|
trans = src_ind->last_month_pct_transported[1];
|
|
|
|
total = src_ind->last_month_production[1];
|
2009-05-23 15:46:00 +00:00
|
|
|
} else {
|
2011-12-03 22:26:30 +00:00
|
|
|
cid = src_ind->produced_cargo[0];
|
|
|
|
trans = src_ind->last_month_pct_transported[0];
|
|
|
|
total = src_ind->last_month_production[0];
|
2009-05-23 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Quit if no production in this industry
|
|
|
|
* or if the pct transported is already large enough */
|
2011-12-03 22:26:30 +00:00
|
|
|
if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED || cid == CT_INVALID) 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)
|
|
|
|
{
|
|
|
|
/* Choose a random destination. Only consider towns if they can accept the cargo. */
|
2011-12-15 21:56:00 +00:00
|
|
|
SourceType dst_type = (HasBit(_town_cargoes_accepted, cid) && Chance16(1, 2)) ? ST_TOWN : ST_INDUSTRY;
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
SourceID dst;
|
|
|
|
switch (dst_type) {
|
|
|
|
case ST_TOWN: {
|
|
|
|
/* Select a random town. */
|
|
|
|
const Town *dst_town = Town::GetRandom();
|
2009-05-23 15:46:00 +00:00
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
/* Check if the town can accept this cargo. */
|
|
|
|
if (!HasBit(dst_town->cargo_accepted_total, cid)) 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
|
|
|
}
|
|
|
|
|
2011-12-03 22:26:30 +00:00
|
|
|
case ST_INDUSTRY: {
|
|
|
|
/* Select a random industry. */
|
|
|
|
const Industry *dst_ind = Industry::GetRandom();
|
|
|
|
|
|
|
|
/* The industry must accept the cargo */
|
|
|
|
if (dst_ind == NULL ||
|
|
|
|
(cid != dst_ind->accepts_cargo[0] &&
|
|
|
|
cid != dst_ind->accepts_cargo[1] &&
|
|
|
|
cid != dst_ind->accepts_cargo[2])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/** Perform the monthly update of open subsidies, and try to create a new one. */
|
2009-05-23 15:46:00 +00:00
|
|
|
void SubsidyMonthlyLoop()
|
|
|
|
{
|
|
|
|
bool modified = false;
|
|
|
|
|
2009-07-01 17:43:26 +00:00
|
|
|
Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-08-08 16:42:55 +00:00
|
|
|
if (--s->remaining == 0) {
|
|
|
|
if (!s->IsAwarded()) {
|
2010-11-24 01:08:49 +00:00
|
|
|
Pair reftype = SetupSubsidyDecodeParam(s, true);
|
2012-05-26 14:16:03 +00:00
|
|
|
AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, 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) {
|
2010-11-24 01:08:49 +00:00
|
|
|
Pair reftype = SetupSubsidyDecodeParam(s, true);
|
2012-05-26 14:16:03 +00:00
|
|
|
AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 20:53:36 +00:00
|
|
|
if (modified) RebuildSubsidisedSourceAndDestinationCache();
|
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);
|
|
|
|
|
|
|
|
if (random_chance < 2) {
|
|
|
|
/* 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);
|
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) {
|
|
|
|
case ST_INDUSTRY:
|
|
|
|
if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
|
|
|
|
break;
|
|
|
|
case ST_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 */
|
|
|
|
SmallVector<const Town *, 2> towns_near;
|
|
|
|
if (!st->rect.IsEmpty()) {
|
|
|
|
Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
|
/* Don't create the cache if there is no applicable subsidy with town as destination */
|
|
|
|
if (s->dst_type != ST_TOWN) continue;
|
|
|
|
if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
|
|
|
|
if (s->IsAwarded() && s->awarded != company) continue;
|
|
|
|
|
|
|
|
Rect rect = st->GetCatchmentRect();
|
|
|
|
|
|
|
|
for (int y = rect.top; y <= rect.bottom; y++) {
|
|
|
|
for (int x = rect.left; x <= rect.right; x++) {
|
|
|
|
TileIndex tile = TileXY(x, y);
|
|
|
|
if (!IsTileType(tile, MP_HOUSE)) continue;
|
|
|
|
const Town *t = Town::GetByTile(tile);
|
2012-04-25 20:50:13 +00:00
|
|
|
if (t->cache.part_of_subsidy & POS_DST) towns_near.Include(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 */
|
|
|
|
Subsidy *s;
|
2009-07-01 17:43:26 +00:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
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) {
|
|
|
|
case ST_INDUSTRY:
|
|
|
|
for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
|
|
|
|
if (s->dst == (*ip)->index) {
|
|
|
|
assert((*ip)->part_of_subsidy & POS_DST);
|
|
|
|
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
|
|
|
case ST_TOWN:
|
|
|
|
for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
|
|
|
|
if (s->dst == (*tp)->index) {
|
2012-04-25 20:50:13 +00:00
|
|
|
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
|
|
|
}
|