Merge 1.9.0-beta2

This commit is contained in:
Pavel Stupnikov
2019-02-14 00:47:22 +03:00
1364 changed files with 22213 additions and 10721 deletions

View File

@@ -1,4 +1,4 @@
/* $Id: town_cmd.cpp 27893 2017-08-13 18:38:42Z frosch $ */
/* $Id$ */
/*
* This file is part of OpenTTD.
@@ -65,7 +65,7 @@ TownsGrowthTilesIndex _towns_growth_tiles_last_month;
TownsGrowthTilesIndex _towns_growth_tiles;
TownID _new_town_id;
uint32 _town_cargoes_accepted; ///< Bitmap of all cargoes accepted by houses.
CargoTypes _town_cargoes_accepted; ///< Bitmap of all cargoes accepted by houses.
/* Initialize the town-pool */
TownPool _town_pool("Town");
@@ -653,17 +653,17 @@ static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced)
}
}
static inline void AddAcceptedCargoSetMask(CargoID cargo, uint amount, CargoArray &acceptance, uint32 *always_accepted)
static inline void AddAcceptedCargoSetMask(CargoID cargo, uint amount, CargoArray &acceptance, CargoTypes *always_accepted)
{
if (cargo == CT_INVALID || amount == 0) return;
acceptance[cargo] += amount;
SetBit(*always_accepted, cargo);
}
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
{
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
CargoID accepts[3];
CargoID accepts[lengthof(hs->accepts_cargo)];
/* Set the initial accepted cargo types */
for (uint8 i = 0; i < lengthof(accepts); i++) {
@@ -772,7 +772,7 @@ void UpdateTownCargoTotal(Town *t)
static void UpdateTownCargoes(Town *t, TileIndex start, bool update_total = true)
{
CargoArray accepted, produced;
uint32 dummy;
CargoTypes dummy = 0;
/* Gather acceptance for all houses in an area around the start tile.
* The area is composed of the square the tile is in, extended one square in all
@@ -786,7 +786,7 @@ static void UpdateTownCargoes(Town *t, TileIndex start, bool update_total = true
}
/* Create bitmap of produced and accepted cargoes. */
uint32 acc = 0;
CargoTypes acc = 0;
for (uint cid = 0; cid < NUM_CARGO; cid++) {
if (accepted[cid] >= 8) SetBit(acc, cid);
if (produced[cid] > 0) SetBit(t->cargo_produced, cid);
@@ -849,7 +849,6 @@ static void DoRegularFunding(Town *t)
if (CB_Enabled() && !t->growing)
return;
uint16 gr = (t->growth_rate & ~TOWN_GROW_RATE_CUSTOM);
// Funding with grow_counter == 1 doesn't speed up next house building,
// so trying to avoid it. This means no powerfunding with gr <= 1, and no
@@ -857,16 +856,21 @@ static void DoRegularFunding(Town *t)
// (we need to fund anyway even if it doesn't speed up next house)
// And in case town is not growing ignore grow_counter and growth_rate
// completely (it will start growing once we fund it)
bool not_growing = !HasBit(t->flags, TOWN_IS_GROWING);
if ((fund_regularly && t->fund_buildings_months == 0 &&
((t->grow_counter > 0 && (t->grow_counter > 1 || gr == 1)) || not_growing)) ||
(do_powerfund && gr > 1 && (t->grow_counter == gr || not_growing))) {
bool is_growing = !HasBit(t->flags, TOWN_IS_GROWING);
if (fund_regularly) {
if (t->fund_buildings_months > 0) return;
if (is_growing
&& t->growth_rate > 2 * TOWN_GROWTH_TICKS
&& t->grow_counter <= 2 * TOWN_GROWTH_TICKS) return;
} else if (do_powerfund) {
if (t->growth_rate <= 2 * TOWN_GROWTH_TICKS) return;
if (t->grow_counter != t->growth_rate && is_growing) return;
} else return;
CompanyByte old = _current_company;
_current_company = _local_company;
DoCommandP(t->xy, t->index, HK_FUND, CMD_DO_TOWN_ACTION | CMD_NO_ESTIMATE);
_current_company = old;
}
CompanyByte old = _current_company;
_current_company = _local_company;
DoCommandP(t->xy, t->index, HK_FUND, CMD_DO_TOWN_ACTION | CMD_NO_ESTIMATE);
_current_company = old;
}
static void DoRegularAdvertising(Town *t) {
@@ -902,11 +906,11 @@ static void DoRegularAdvertising(Town *t) {
static void TownTickHandler(Town *t)
{
if (HasBit(t->flags, TOWN_IS_GROWING)) {
int i = t->grow_counter - 1;
int i = (int)t->grow_counter - 1;
uint16 houses_prev = t->cache.num_houses;
if (i < 0) {
if (GrowTown(t)) {
i = t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
i = t->growth_rate;
if (t->cache.num_houses <= houses_prev && (t->growing || !CB_Enabled())){
t->houses_skipped++;
}
@@ -914,7 +918,8 @@ static void TownTickHandler(Town *t)
if (t->growing || !CB_Enabled()){
t->cycles_skipped++;
}
i = 0;
/* If growth failed wait a bit before retrying */
i = min(t->growth_rate, TOWN_GROWTH_TICKS - 1);
}
}
t->grow_counter = i;
@@ -929,10 +934,7 @@ void OnTick_Town()
Town *t;
FOR_ALL_TOWNS(t) {
/* Run town tick at regular intervals, but not all at once. */
if ((_tick_counter + t->index) % TOWN_GROWTH_TICKS == 0) {
TownTickHandler(t);
}
TownTickHandler(t);
}
}
@@ -1359,17 +1361,48 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
/* Possibly extend the road in a direction.
* Randomize a direction and if it has a road, bail out. */
target_dir = RandomDiagDir();
if (cur_rb & DiagDirToRoadBits(target_dir)) return;
RoadBits target_rb = DiagDirToRoadBits(target_dir);
TileIndex house_tile; // position of a possible house
/* This is the tile we will reach if we extend to this direction. */
TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house
if (cur_rb & target_rb) {
/* If it's a road turn possibly build a house in a corner.
* Use intersection with straight road as an indicator
* that we randomed corner house position.
* A turn (and we check for that later) always has only
* one common bit with a straight road so it has the same
* chance to be chosen as the house on the side of a road.
*/
if ((cur_rb & ROAD_X) != target_rb) return;
/* Check whether it is a turn and if so determine
* position of the corner tile */
switch (cur_rb) {
case ROAD_N:
house_tile = TileAddByDir(tile, DIR_S);
break;
case ROAD_S:
house_tile = TileAddByDir(tile, DIR_N);
break;
case ROAD_E:
house_tile = TileAddByDir(tile, DIR_W);
break;
case ROAD_W:
house_tile = TileAddByDir(tile, DIR_E);
break;
default:
return; // not a turn
}
target_dir = DIAGDIR_END;
} else {
house_tile = TileAddByDiagDir(tile, target_dir);
}
/* Don't walk into water. */
if (HasTileWaterGround(house_tile)) return;
if (!IsValidTile(house_tile)) return;
if (_settings_game.economy.allow_town_roads || _generating_world) {
if (target_dir != DIAGDIR_END && (_settings_game.economy.allow_town_roads || _generating_world)) {
switch (t1->layout) {
default: NOT_REACHED();
@@ -1379,7 +1412,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
case TL_2X2_GRID:
rcmd = GetTownRoadGridElement(t1, tile, target_dir);
allow_house = (rcmd & DiagDirToRoadBits(target_dir)) == ROAD_NONE;
allow_house = (rcmd & target_rb) == ROAD_NONE;
break;
case TL_BETTER_ROADS: // Use original afterwards!
@@ -1389,7 +1422,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection t
case TL_ORIGINAL:
/* Allow a house at the edge. 60% chance or
* always ok if no road allowed. */
rcmd = DiagDirToRoadBits(target_dir);
rcmd = target_rb;
allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10));
break;
}
@@ -1686,6 +1719,9 @@ void UpdateTownMaxPass(Town *t)
t->supplied[CT_MAIL].old_max = t->cache.population >> 4;
}
static void UpdateTownGrowthRate(Town *t);
static void UpdateTownGrowth(Town *t);
//CB
bool CB_Enabled(){
@@ -1825,8 +1861,10 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize
UpdateTownRadius(t);
t->flags = 0;
t->cache.population = 0;
t->grow_counter = 0;
t->growth_rate = 250;
/* Spread growth across ticks so even if there are many
* similar towns they're unlikely to grow all in one tick */
t->grow_counter = t->index % TOWN_GROWTH_TICKS;
t->growth_rate = TownTicksToGameTicks(250);
/* Set the default cargo requirement for town growth */
switch (_settings_game.game_creation.landscape) {
@@ -1902,6 +1940,7 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize
t->cache.num_houses -= x;
_closest_cache_ref = INVALID_TILE;
UpdateTownRadius(t);
UpdateTownGrowthRate(t);
UpdateTownMaxPass(t);
UpdateAirportsNoise();
}
@@ -2042,15 +2081,21 @@ CommandCost CmdFoundTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
if (_game_mode != GM_EDITOR) {
/* 't' can't be NULL since 'random' is false outside scenedit */
assert(!random);
char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
SetDParam(0, _current_company);
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
char *cn = stredup(company_name);
SetDParamStr(0, cn);
SetDParam(1, t->index);
if (_current_company == OWNER_DEITY) {
SetDParam(0, t->index);
AddTileNewsItem(STR_NEWS_NEW_TOWN_UNSPONSORED, NT_INDUSTRY_OPEN, tile);
} else {
char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
SetDParam(0, _current_company);
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, cn);
char *cn = stredup(company_name);
SetDParamStr(0, cn);
SetDParam(1, t->index);
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, cn);
}
AI::BroadcastNewEvent(new ScriptEventTownFounded(t->index));
Game::NewEvent(new ScriptEventTownFounded(t->index));
}
@@ -2664,6 +2709,7 @@ static bool BuildTownHouse(Town *t, TileIndex tile)
MakeTownHouse(tile, t, construction_counter, construction_stage, house, random_bits);
UpdateTownGrowthTile(tile, TGTS_NEW_HOUSE);
UpdateTownRadius(t);
UpdateTownGrowthRate(t);
UpdateTownCargoes(t, tile);
return true;
@@ -2802,8 +2848,6 @@ const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect)
return NULL;
}
static void UpdateTownGrowRate(Town *t);
/**
* Change the cargo goal of a town.
* @param tile Unused.
@@ -2832,7 +2876,7 @@ CommandCost CmdTownCargoGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
if (flags & DC_EXEC) {
t->goal[te] = p2;
UpdateTownGrowRate(t);
UpdateTownGrowth(t);
InvalidateWindowData(WC_TOWN_VIEW, index);
}
@@ -2868,14 +2912,13 @@ CommandCost CmdTownSetText(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
* @param tile Unused.
* @param flags Type of operation.
* @param p1 Town ID to cargo game of.
* @param p2 Amount of days between growth, or TOWN_GROW_RATE_CUSTOM_NONE, or 0 to reset custom growth rate.
* @param p2 Amount of days between growth, or TOWN_GROWTH_RATE_NONE, or 0 to reset custom growth rate.
* @param text Unused.
* @return Empty cost or an error.
*/
CommandCost CmdTownGrowthRate(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
if (_current_company != OWNER_DEITY) return CMD_ERROR;
if ((p2 & TOWN_GROW_RATE_CUSTOM) != 0 && p2 != TOWN_GROW_RATE_CUSTOM_NONE) return CMD_ERROR;
if (GB(p2, 16, 16) != 0) return CMD_ERROR;
Town *t = Town::GetIfValid(p1);
@@ -2883,10 +2926,10 @@ CommandCost CmdTownGrowthRate(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
if (flags & DC_EXEC) {
if (p2 == 0) {
/* Clear TOWN_GROW_RATE_CUSTOM, UpdateTownGrowRate will determine a proper value */
t->growth_rate = 0;
/* Just clear the flag, UpdateTownGrowth will determine a proper growth rate */
ClrBit(t->flags, TOWN_CUSTOM_GROWTH);
} else {
uint old_rate = t->growth_rate & ~TOWN_GROW_RATE_CUSTOM;
uint old_rate = t->growth_rate;
if (t->grow_counter >= old_rate) {
/* This also catches old_rate == 0 */
t->grow_counter = p2;
@@ -2894,9 +2937,10 @@ CommandCost CmdTownGrowthRate(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
/* Scale grow_counter, so half finished houses stay half finished */
t->grow_counter = t->grow_counter * p2 / old_rate;
}
t->growth_rate = p2 | TOWN_GROW_RATE_CUSTOM;
t->growth_rate = p2;
SetBit(t->flags, TOWN_CUSTOM_GROWTH);
}
UpdateTownGrowRate(t);
UpdateTownGrowth(t);
InvalidateWindowData(WC_TOWN_VIEW, p1);
InvalidateWindowData(WC_CB_TOWN, p1);
}
@@ -3180,13 +3224,21 @@ static CommandCost TownActionFundBuildings(Town *t, DoCommandFlag flags)
if (!_settings_game.economy.fund_buildings) return CMD_ERROR;
if (flags & DC_EXEC) {
/* Build next tick */
t->grow_counter = 1;
/* And grow for 3 months */
t->fund_buildings_months = 3;
/* Enable growth (also checking GameScript's opinion) */
UpdateTownGrowRate(t);
UpdateTownGrowth(t);
/* Build a new house, but add a small delay to make sure
* that spamming funding doesn't let town grow any faster
* than 1 house per 2 * TOWN_GROWTH_TICKS ticks.
* Also emulate original behaviour when town was only growing in
* TOWN_GROWTH_TICKS intervals, to make sure that it's not too
* tick-perfect and gives player some time window where he can
* spam funding with the exact same efficiency.
*/
t->grow_counter = min(t->grow_counter, 2 * TOWN_GROWTH_TICKS - (t->growth_rate - t->grow_counter) % TOWN_GROWTH_TICKS);
SetWindowDirty(WC_TOWN_VIEW, t->index);
SetWindowDirty(WC_CB_TOWN, t->index);
@@ -3383,8 +3435,87 @@ static void UpdateTownRating(Town *t)
SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
}
static void UpdateTownGrowRate(Town *t)
/**
* Updates town grow counter after growth rate change.
* Preserves relative house builting progress whenever it can.
* @param t The town to calculate grow counter for
* @param prev_growth_rate Town growth rate before it changed (one that was used with grow counter to be updated)
*/
static void UpdateTownGrowCounter(Town *t, uint16 prev_growth_rate)
{
if (t->growth_rate == TOWN_GROWTH_RATE_NONE) return;
if (prev_growth_rate == TOWN_GROWTH_RATE_NONE) {
t->grow_counter = min(t->growth_rate, t->grow_counter);
return;
}
t->grow_counter = RoundDivSU((uint32)t->grow_counter * (t->growth_rate + 1), prev_growth_rate + 1);
}
/**
* Calculates amount of active stations in the range of town (HZB_TOWN_EDGE).
* @param t The town to calculate stations for
* @returns Amount of active stations
*/
static int CountActiveStations(Town *t)
{
int n = 0;
const Station *st;
FOR_ALL_STATIONS(st) {
if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) {
if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
n++;
}
}
}
return n;
}
/**
* Calculates town growth rate in normal conditions (custom growth rate not set).
* If town growth speed is set to None(0) returns the same rate as if it was Normal(2).
* @param t The town to calculate growth rate for
* @returns Calculated growth rate
*/
static uint GetNormalGrowthRate(Town *t)
{
static const uint16 _grow_count_values[2][6] = {
{ 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated
{ 320, 420, 300, 220, 160, 100 } // Normal values
};
int n = CountActiveStations(t);
uint16 m = _grow_count_values[t->fund_buildings_months != 0 ? 0 : 1][min(n, 5)];
uint growth_multiplier = _settings_game.economy.town_growth_rate != 0 ? _settings_game.economy.town_growth_rate - 1 : 1;
m >>= growth_multiplier;
if (t->larger_town) m /= 2;
return TownTicksToGameTicks(m / (t->cache.num_houses / 50 + 1));
}
/**
* Updates town growth rate.
* @param t The town to update growth rate for
*/
static void UpdateTownGrowthRate(Town *t)
{
if (HasBit(t->flags, TOWN_CUSTOM_GROWTH)) return;
uint old_rate = t->growth_rate;
t->growth_rate = GetNormalGrowthRate(t);
UpdateTownGrowCounter(t, old_rate);
SetWindowDirty(WC_TOWN_VIEW, t->index);
}
/**
* Updates town growth state (whether it is growing or not).
* @param t The town to update growth for
*/
static void UpdateTownGrowth(Town *t)
{
UpdateTownGrowthRate(t);
ClrBit(t->flags, TOWN_IS_GROWING);
t->growing_by_chance = false;
SetWindowDirty(WC_TOWN_VIEW, t->index);
@@ -3409,53 +3540,18 @@ static void UpdateTownGrowRate(Town *t)
}
}
if ((t->growth_rate & TOWN_GROW_RATE_CUSTOM) != 0) {
if (t->growth_rate != TOWN_GROW_RATE_CUSTOM_NONE) SetBit(t->flags, TOWN_IS_GROWING);
if (HasBit(t->flags, TOWN_CUSTOM_GROWTH)) {
if (t->growth_rate != TOWN_GROWTH_RATE_NONE) SetBit(t->flags, TOWN_IS_GROWING);
SetWindowDirty(WC_TOWN_VIEW, t->index);
SetWindowDirty(WC_CB_TOWN, t->index);
return;
}
/**
* Towns are processed every TOWN_GROWTH_TICKS ticks, and this is the
* number of times towns are processed before a new building is built.
*/
static const uint16 _grow_count_values[2][6] = {
{ 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated
{ 320, 420, 300, 220, 160, 100 } // Normal values
};
int n = 0;
const Station *st;
FOR_ALL_STATIONS(st) {
if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) {
if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
n++;
}
}
if (t->fund_buildings_months == 0 && CountActiveStations(t) == 0) {
if(!Chance16(1, 12)) return;
t->growing_by_chance = true;
}
uint16 m;
if (t->fund_buildings_months != 0) {
m = _grow_count_values[0][min(n, 5)];
} else {
m = _grow_count_values[1][min(n, 5)];
if (n == 0 && !Chance16(1, 12)) return;
if (n == 0) t->growing_by_chance = true;
}
/* Use the normal growth rate values if new buildings have been funded in
* this town and the growth rate is set to none. */
uint growth_multiplier = _settings_game.economy.town_growth_rate != 0 ? _settings_game.economy.town_growth_rate - 1 : 1;
m >>= growth_multiplier;
if (t->larger_town) m /= 2;
t->growth_rate = m / (t->cache.num_houses / 50 + 1);
t->grow_counter = min(t->growth_rate, t->grow_counter);
SetBit(t->flags, TOWN_IS_GROWING);
SetWindowDirty(WC_TOWN_VIEW, t->index);
SetWindowDirty(WC_CB_TOWN, t->index);
@@ -3541,7 +3637,7 @@ Town *ClosestTownFromTile(TileIndex tile, uint threshold)
if (!HasTownOwnedRoad(tile)) {
TownID tid = GetTownIndex(tile);
if (tid == (TownID)INVALID_TOWN) {
if (tid == INVALID_TOWN) {
/* in the case we are generating "many random towns", this value may be INVALID_TOWN */
if (_generating_world) return CalcClosestTownFromTile(tile, threshold);
assert(Town::GetNumItems() == 0);
@@ -3696,8 +3792,8 @@ void TownsMonthlyLoop()
if (CB_Enabled() && !t->larger_town) CB_UpdateTownStorage(t); //CB
UpdateTownAmounts(t);
UpdateTownGrowth(t);
UpdateTownRating(t);
UpdateTownGrowRate(t);
UpdateTownUnwanted(t);
UpdateTownCargoes(t);