Update to 14.0-beta1
This commit is contained in:
+55
-51
@@ -20,11 +20,14 @@
|
||||
#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"
|
||||
|
||||
@@ -42,7 +45,7 @@ void Subsidy::AwardTo(CompanyID company)
|
||||
assert(!this->IsAwarded());
|
||||
|
||||
this->awarded = company;
|
||||
this->remaining = _settings_game.difficulty.subsidy_duration * MONTHS_IN_YEAR;
|
||||
this->remaining = _settings_game.difficulty.subsidy_duration * CalendarTime::MONTHS_IN_YEAR;
|
||||
|
||||
SetDParam(0, company);
|
||||
NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
|
||||
@@ -80,11 +83,11 @@ std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Su
|
||||
SetDParam(parameter_offset, cs->name);
|
||||
|
||||
switch (s->src_type) {
|
||||
case ST_INDUSTRY:
|
||||
case SourceType::Industry:
|
||||
reftype1 = NR_INDUSTRY;
|
||||
SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
|
||||
break;
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
reftype1 = NR_TOWN;
|
||||
SetDParam(parameter_offset + 1, STR_TOWN_NAME);
|
||||
break;
|
||||
@@ -93,11 +96,11 @@ std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Su
|
||||
SetDParam(parameter_offset + 2, s->src);
|
||||
|
||||
switch (s->dst_type) {
|
||||
case ST_INDUSTRY:
|
||||
case SourceType::Industry:
|
||||
reftype2 = NR_INDUSTRY;
|
||||
SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
|
||||
break;
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
reftype2 = NR_TOWN;
|
||||
SetDParam(parameter_offset + 4, STR_TOWN_NAME);
|
||||
break;
|
||||
@@ -122,8 +125,8 @@ std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Su
|
||||
static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
|
||||
{
|
||||
switch (type) {
|
||||
case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
|
||||
case ST_TOWN: Town::Get(index)->cache.part_of_subsidy |= flag; return;
|
||||
case SourceType::Industry: Industry::Get(index)->part_of_subsidy |= flag; return;
|
||||
case SourceType::Town: Town::Get(index)->cache.part_of_subsidy |= flag; return;
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
}
|
||||
@@ -194,8 +197,8 @@ static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID s
|
||||
*/
|
||||
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;
|
||||
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;
|
||||
|
||||
return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
|
||||
}
|
||||
@@ -248,20 +251,20 @@ CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_ty
|
||||
if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
|
||||
|
||||
switch (src_type) {
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
if (!Town::IsValidID(src)) return CMD_ERROR;
|
||||
break;
|
||||
case ST_INDUSTRY:
|
||||
case SourceType::Industry:
|
||||
if (!Industry::IsValidID(src)) return CMD_ERROR;
|
||||
break;
|
||||
default:
|
||||
return CMD_ERROR;
|
||||
}
|
||||
switch (dst_type) {
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
if (!Town::IsValidID(dst)) return CMD_ERROR;
|
||||
break;
|
||||
case ST_INDUSTRY:
|
||||
case SourceType::Industry:
|
||||
if (!Industry::IsValidID(dst)) return CMD_ERROR;
|
||||
break;
|
||||
default:
|
||||
@@ -283,9 +286,13 @@ bool FindSubsidyPassengerRoute()
|
||||
{
|
||||
if (!Subsidy::CanAllocateItem()) return false;
|
||||
|
||||
/* 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();
|
||||
|
||||
const Town *src = Town::GetRandom();
|
||||
if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
|
||||
src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
|
||||
src->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -295,9 +302,9 @@ bool FindSubsidyPassengerRoute()
|
||||
}
|
||||
|
||||
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;
|
||||
if (CheckSubsidyDuplicate(cid, SourceType::Town, src->index, SourceType::Town, dst->index)) return false;
|
||||
|
||||
CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index);
|
||||
CreateSubsidy(cid, SourceType::Town, src->index, SourceType::Town, dst->index);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -313,14 +320,14 @@ bool FindSubsidyTownCargoRoute()
|
||||
{
|
||||
if (!Subsidy::CanAllocateItem()) return false;
|
||||
|
||||
SourceType src_type = ST_TOWN;
|
||||
SourceType src_type = SourceType::Town;
|
||||
|
||||
/* Select a random town. */
|
||||
const Town *src_town = Town::GetRandom();
|
||||
if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
|
||||
|
||||
/* Calculate the produced cargo of houses around town center. */
|
||||
CargoArray town_cargo_produced;
|
||||
CargoArray town_cargo_produced{};
|
||||
TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
||||
for (TileIndex tile : ta) {
|
||||
if (IsTileType(tile, MP_HOUSE)) {
|
||||
@@ -329,18 +336,17 @@ bool FindSubsidyTownCargoRoute()
|
||||
}
|
||||
|
||||
/* Passenger subsidies are not handled here. */
|
||||
town_cargo_produced[CT_PASSENGERS] = 0;
|
||||
|
||||
uint8 cargo_count = 0;
|
||||
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
||||
if (town_cargo_produced[i] > 0) cargo_count++;
|
||||
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_PASSENGERS]) {
|
||||
town_cargo_produced[cs->Index()] = 0;
|
||||
}
|
||||
|
||||
uint8_t cargo_count = town_cargo_produced.GetCount();
|
||||
|
||||
/* No cargo produced at all? */
|
||||
if (cargo_count == 0) return false;
|
||||
|
||||
/* Choose a random cargo that is produced in the town. */
|
||||
uint8 cargo_number = RandomRange(cargo_count);
|
||||
uint8_t cargo_number = RandomRange(cargo_count);
|
||||
CargoID cid;
|
||||
for (cid = 0; cid < NUM_CARGO; cid++) {
|
||||
if (town_cargo_produced[cid] > 0) {
|
||||
@@ -371,7 +377,7 @@ bool FindSubsidyIndustryCargoRoute()
|
||||
{
|
||||
if (!Subsidy::CanAllocateItem()) return false;
|
||||
|
||||
SourceType src_type = ST_INDUSTRY;
|
||||
SourceType src_type = SourceType::Industry;
|
||||
|
||||
/* Select a random industry. */
|
||||
const Industry *src_ind = Industry::GetRandom();
|
||||
@@ -382,27 +388,26 @@ bool FindSubsidyIndustryCargoRoute()
|
||||
CargoID cid;
|
||||
|
||||
/* Randomize cargo type */
|
||||
int num_cargos = 0;
|
||||
uint cargo_index;
|
||||
for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
|
||||
if (src_ind->produced_cargo[cargo_index] != CT_INVALID) num_cargos++;
|
||||
}
|
||||
int num_cargos = std::count_if(std::begin(src_ind->produced), std::end(src_ind->produced), [](const auto &p) { return IsValidCargoID(p.cargo); });
|
||||
if (num_cargos == 0) return false; // industry produces nothing
|
||||
int cargo_num = RandomRange(num_cargos) + 1;
|
||||
for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
|
||||
if (src_ind->produced_cargo[cargo_index] != CT_INVALID) cargo_num--;
|
||||
|
||||
auto it = std::begin(src_ind->produced);
|
||||
for (/* nothing */; it != std::end(src_ind->produced); ++it) {
|
||||
if (IsValidCargoID(it->cargo)) cargo_num--;
|
||||
if (cargo_num == 0) break;
|
||||
}
|
||||
assert(cargo_num == 0); // indicates loop didn't break as intended
|
||||
cid = src_ind->produced_cargo[cargo_index];
|
||||
trans = src_ind->last_month_pct_transported[cargo_index];
|
||||
total = src_ind->last_month_production[cargo_index];
|
||||
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;
|
||||
|
||||
/* Quit if no production in this industry
|
||||
* or if the pct transported is already large enough
|
||||
* or if the cargo is automatically distributed */
|
||||
if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
|
||||
cid == CT_INVALID ||
|
||||
!IsValidCargoID(cid) ||
|
||||
_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
|
||||
return false;
|
||||
}
|
||||
@@ -422,16 +427,16 @@ bool FindSubsidyIndustryCargoRoute()
|
||||
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
|
||||
{
|
||||
/* Choose a random destination. */
|
||||
SourceType dst_type = Chance16(1, 2) ? ST_TOWN : ST_INDUSTRY;
|
||||
SourceType dst_type = Chance16(1, 2) ? SourceType::Town : SourceType::Industry;
|
||||
|
||||
SourceID dst;
|
||||
switch (dst_type) {
|
||||
case ST_TOWN: {
|
||||
case SourceType::Town: {
|
||||
/* Select a random town. */
|
||||
const Town *dst_town = Town::GetRandom();
|
||||
|
||||
/* Calculate cargo acceptance of houses around town center. */
|
||||
CargoArray town_cargo_accepted;
|
||||
CargoArray town_cargo_accepted{};
|
||||
TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
|
||||
for (TileIndex tile : ta) {
|
||||
if (IsTileType(tile, MP_HOUSE)) {
|
||||
@@ -446,14 +451,13 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
|
||||
break;
|
||||
}
|
||||
|
||||
case ST_INDUSTRY: {
|
||||
case SourceType::Industry: {
|
||||
/* Select a random industry. */
|
||||
const Industry *dst_ind = Industry::GetRandom();
|
||||
if (dst_ind == nullptr) return false;
|
||||
|
||||
/* The industry must accept the cargo */
|
||||
bool valid = std::find(dst_ind->accepts_cargo, endof(dst_ind->accepts_cargo), cid) != endof(dst_ind->accepts_cargo);
|
||||
if (!valid) return false;
|
||||
if (!dst_ind->IsCargoAccepted(cid)) return false;
|
||||
|
||||
dst = dst_ind->index;
|
||||
break;
|
||||
@@ -476,8 +480,8 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Perform the monthly update of open subsidies, and try to create a new one. */
|
||||
void SubsidyMonthlyLoop()
|
||||
/** 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)
|
||||
{
|
||||
bool modified = false;
|
||||
|
||||
@@ -547,7 +551,7 @@ void SubsidyMonthlyLoop()
|
||||
modified |= passenger_subsidy || town_subsidy || industry_subsidy;
|
||||
|
||||
if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
|
||||
@@ -563,10 +567,10 @@ bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type,
|
||||
/* If the source isn't subsidised, don't continue */
|
||||
if (src == INVALID_SOURCE) return false;
|
||||
switch (src_type) {
|
||||
case ST_INDUSTRY:
|
||||
case SourceType::Industry:
|
||||
if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
|
||||
break;
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
|
||||
break;
|
||||
default: return false;
|
||||
@@ -578,7 +582,7 @@ bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type,
|
||||
if (!st->rect.IsEmpty()) {
|
||||
for (const Subsidy *s : Subsidy::Iterate()) {
|
||||
/* Don't create the cache if there is no applicable subsidy with town as destination */
|
||||
if (s->dst_type != ST_TOWN) continue;
|
||||
if (s->dst_type != SourceType::Town) continue;
|
||||
if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
|
||||
if (s->IsAwarded() && s->awarded != company) continue;
|
||||
|
||||
@@ -599,7 +603,7 @@ bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type,
|
||||
for (Subsidy *s : Subsidy::Iterate()) {
|
||||
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:
|
||||
case SourceType::Industry:
|
||||
for (const auto &i : st->industries_near) {
|
||||
if (s->dst == i.industry->index) {
|
||||
assert(i.industry->part_of_subsidy & POS_DST);
|
||||
@@ -608,7 +612,7 @@ bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type,
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_TOWN:
|
||||
case SourceType::Town:
|
||||
for (const Town *tp : towns_near) {
|
||||
if (s->dst == tp->index) {
|
||||
assert(tp->cache.part_of_subsidy & POS_DST);
|
||||
|
||||
Reference in New Issue
Block a user