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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file town_cmd.cpp Handling of town tiles. */
|
2007-03-03 04:04:22 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2019-04-06 06:46:15 +00:00
|
|
|
#include "road.h"
|
2007-12-18 20:38:16 +00:00
|
|
|
#include "road_internal.h" /* Cleaning up road bits */
|
2008-10-25 13:51:47 +00:00
|
|
|
#include "road_cmd.h"
|
2007-03-20 13:47:00 +00:00
|
|
|
#include "landscape.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
2019-02-22 16:57:28 +00:00
|
|
|
#include "viewport_kdtree.h"
|
2010-04-17 13:31:41 +00:00
|
|
|
#include "cmd_helper.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "industry.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "station_base.h"
|
2019-02-18 20:14:52 +00:00
|
|
|
#include "station_kdtree.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_base.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "error.h"
|
2010-08-08 10:59:30 +00:00
|
|
|
#include "object.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#include "genworld.h"
|
2010-04-24 13:39:11 +00:00
|
|
|
#include "newgrf_debug.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
#include "newgrf_house.h"
|
2009-01-20 16:06:57 +00:00
|
|
|
#include "newgrf_text.h"
|
2007-09-14 22:27:40 +00:00
|
|
|
#include "autoslope.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-03-31 06:42:26 +00:00
|
|
|
#include "newgrf_cargo.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "cheat_type.h"
|
2008-04-20 08:43:31 +00:00
|
|
|
#include "animated_tile_func.h"
|
|
|
|
#include "date_func.h"
|
2009-05-23 15:46:00 +00:00
|
|
|
#include "subsidy_func.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2009-06-26 13:44:14 +00:00
|
|
|
#include "town.h"
|
2019-02-18 16:32:53 +00:00
|
|
|
#include "town_kdtree.h"
|
2009-09-22 13:54:54 +00:00
|
|
|
#include "townname_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
2010-05-31 20:22:57 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2010-08-02 21:07:23 +00:00
|
|
|
#include "depot_base.h"
|
2010-08-13 11:57:59 +00:00
|
|
|
#include "object_map.h"
|
2010-08-13 12:45:26 +00:00
|
|
|
#include "object_base.h"
|
2010-12-29 23:44:39 +00:00
|
|
|
#include "ai/ai.hpp"
|
2011-12-19 20:59:36 +00:00
|
|
|
#include "game/game.hpp"
|
2007-12-16 15:38:51 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/town_land.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-09-21 20:22:56 +00:00
|
|
|
TownID _new_town_id;
|
2008-01-13 13:36:01 +00:00
|
|
|
|
2007-08-02 21:05:54 +00:00
|
|
|
/* Initialize the town-pool */
|
2009-05-22 15:13:50 +00:00
|
|
|
TownPool _town_pool("Town");
|
|
|
|
INSTANTIATE_POOL_METHODS(Town)
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2019-02-18 16:32:53 +00:00
|
|
|
|
|
|
|
TownKdtree _town_kdtree(&Kdtree_TownXYFunc);
|
|
|
|
|
|
|
|
void RebuildTownKdtree()
|
|
|
|
{
|
|
|
|
std::vector<TownID> townids;
|
2019-12-17 21:04:09 +00:00
|
|
|
for (const Town *town : Town::Iterate()) {
|
2019-02-18 16:32:53 +00:00
|
|
|
townids.push_back(town->index);
|
|
|
|
}
|
|
|
|
_town_kdtree.Build(townids.begin(), townids.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-03 22:28:55 +00:00
|
|
|
/**
|
|
|
|
* Check if a town 'owns' a bridge.
|
|
|
|
* Bridges to not directly have an owner, so we check the tiles adjacent to the bridge ends.
|
|
|
|
* If either adjacent tile belongs to the town then it will be assumed that the town built
|
|
|
|
* the bridge.
|
|
|
|
* @param tile Bridge tile to test
|
|
|
|
* @param t Town we are interested in
|
|
|
|
* @return true if town 'owns' a bridge.
|
|
|
|
*/
|
|
|
|
static bool TestTownOwnsBridge(TileIndex tile, const Town *t)
|
|
|
|
{
|
|
|
|
if (!IsTileOwner(tile, OWNER_TOWN)) return false;
|
|
|
|
|
|
|
|
TileIndex adjacent = tile + TileOffsByDiagDir(ReverseDiagDir(GetTunnelBridgeDirection(tile)));
|
|
|
|
bool town_owned = IsTileType(adjacent, MP_ROAD) && IsTileOwner(adjacent, OWNER_TOWN) && GetTownIndex(adjacent) == t->index;
|
|
|
|
|
|
|
|
if (!town_owned) {
|
|
|
|
/* Or other adjacent road */
|
|
|
|
TileIndex adjacent = tile + TileOffsByDiagDir(ReverseDiagDir(GetTunnelBridgeDirection(GetOtherTunnelBridgeEnd(tile))));
|
|
|
|
town_owned = IsTileType(adjacent, MP_ROAD) && IsTileOwner(adjacent, OWNER_TOWN) && GetTownIndex(adjacent) == t->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return town_owned;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:05:54 +00:00
|
|
|
Town::~Town()
|
2006-08-26 18:05:05 +00:00
|
|
|
{
|
2007-08-05 21:20:55 +00:00
|
|
|
if (CleaningPool()) return;
|
|
|
|
|
2006-08-26 18:05:05 +00:00
|
|
|
/* Delete town authority window
|
|
|
|
* and remove from list of sorted towns */
|
2007-08-02 21:05:54 +00:00
|
|
|
DeleteWindowById(WC_TOWN_VIEW, this->index);
|
2006-08-26 18:05:05 +00:00
|
|
|
|
2010-08-13 11:57:59 +00:00
|
|
|
/* Check no industry is related to us. */
|
2021-04-01 22:57:49 +00:00
|
|
|
#ifdef WITH_ASSERT
|
2019-12-16 17:51:20 +00:00
|
|
|
for (const Industry *i : Industry::Iterate()) assert(i->town != this);
|
2006-08-26 18:05:05 +00:00
|
|
|
|
2010-08-13 12:45:26 +00:00
|
|
|
/* ... and no object is related to us. */
|
2019-12-17 18:05:46 +00:00
|
|
|
for (const Object *o : Object::Iterate()) assert(o->town != this);
|
2021-04-01 09:16:19 +00:00
|
|
|
#endif
|
2010-08-13 12:45:26 +00:00
|
|
|
|
2010-08-13 11:57:59 +00:00
|
|
|
/* Check no tile is related to us. */
|
2007-08-02 21:05:54 +00:00
|
|
|
for (TileIndex tile = 0; tile < MapSize(); ++tile) {
|
2006-08-26 18:05:05 +00:00
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_HOUSE:
|
2010-08-13 11:57:59 +00:00
|
|
|
assert(GetTownIndex(tile) != this->index);
|
2006-08-26 18:05:05 +00:00
|
|
|
break;
|
|
|
|
|
2007-07-29 23:42:59 +00:00
|
|
|
case MP_ROAD:
|
2010-08-13 11:57:59 +00:00
|
|
|
assert(!HasTownOwnedRoad(tile) || GetTownIndex(tile) != this->index);
|
2008-11-18 22:43:59 +00:00
|
|
|
break;
|
|
|
|
|
2006-08-26 18:05:05 +00:00
|
|
|
case MP_TUNNELBRIDGE:
|
2019-03-03 22:28:55 +00:00
|
|
|
assert(!TestTownOwnsBridge(tile, this));
|
2006-08-26 18:05:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-12 20:50:03 +00:00
|
|
|
/* Clear the persistent storage list. */
|
|
|
|
this->psa_list.clear();
|
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
DeleteSubsidyWith(ST_TOWN, this->index);
|
2010-04-24 13:39:11 +00:00
|
|
|
DeleteNewGRFInspectWindow(GSF_FAKE_TOWNS, this->index);
|
2009-08-08 16:42:55 +00:00
|
|
|
CargoPacket::InvalidateAllFrom(ST_TOWN, this->index);
|
2006-08-26 18:05:05 +00:00
|
|
|
MarkWholeScreenDirty();
|
2009-07-05 13:21:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-02 21:05:54 +00:00
|
|
|
|
2009-07-05 13:21:51 +00:00
|
|
|
/**
|
|
|
|
* Invalidating of the "nearest town cache" has to be done
|
|
|
|
* after removing item from the pool.
|
|
|
|
* @param index index of deleted item
|
|
|
|
*/
|
|
|
|
void Town::PostDestructor(size_t index)
|
|
|
|
{
|
2020-01-05 21:55:57 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_REBUILD);
|
2009-07-05 13:21:51 +00:00
|
|
|
UpdateNearestTownForRoadTiles(false);
|
2010-08-13 12:45:26 +00:00
|
|
|
|
|
|
|
/* Give objects a new home! */
|
2019-12-17 18:05:46 +00:00
|
|
|
for (Object *o : Object::Iterate()) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (o->town == nullptr) o->town = CalcClosestTownFromTile(o->location.tile, UINT_MAX);
|
2010-08-13 12:45:26 +00:00
|
|
|
}
|
2007-08-02 21:05:54 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 16:27:01 +00:00
|
|
|
/**
|
2009-02-04 20:17:25 +00:00
|
|
|
* Assigns town layout. If Random, generates one based on TileHash.
|
2008-04-01 16:27:01 +00:00
|
|
|
*/
|
2009-02-04 22:52:34 +00:00
|
|
|
void Town::InitializeLayout(TownLayout layout)
|
2008-04-01 16:27:01 +00:00
|
|
|
{
|
2009-02-04 22:52:34 +00:00
|
|
|
if (layout != TL_RANDOM) {
|
|
|
|
this->layout = layout;
|
2009-02-04 20:17:25 +00:00
|
|
|
return;
|
2008-04-01 16:27:01 +00:00
|
|
|
}
|
2009-02-04 20:17:25 +00:00
|
|
|
|
2019-04-21 21:56:35 +00:00
|
|
|
this->layout = static_cast<TownLayout>(TileHash(TileX(this->xy), TileY(this->xy)) % (NUM_TLS - 1));
|
2008-04-01 16:27:01 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 15:08:54 +00:00
|
|
|
/**
|
|
|
|
* Return a random valid town.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return random town, nullptr if there are no towns
|
2009-06-26 15:08:54 +00:00
|
|
|
*/
|
|
|
|
/* static */ Town *Town::GetRandom()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (Town::GetNumItems() == 0) return nullptr;
|
2009-06-26 15:08:54 +00:00
|
|
|
int num = RandomRange((uint16)Town::GetNumItems());
|
|
|
|
size_t index = MAX_UVALUE(size_t);
|
|
|
|
|
|
|
|
while (num >= 0) {
|
|
|
|
num--;
|
|
|
|
index++;
|
|
|
|
|
|
|
|
/* Make sure we have a valid town */
|
|
|
|
while (!Town::IsValidID(index)) {
|
|
|
|
index++;
|
|
|
|
assert(index < Town::GetPoolSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Town::Get(index);
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:40:31 +00:00
|
|
|
void Town::FillCachedName() const
|
|
|
|
{
|
|
|
|
char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
|
|
|
|
char *end = GetTownName(buf, this, lastof(buf));
|
|
|
|
this->cached_name.assign(buf, end);
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
|
|
|
* Get the cost for removing this house
|
|
|
|
* @return the cost (inflation corrected etc)
|
|
|
|
*/
|
2008-06-05 01:43:03 +00:00
|
|
|
Money HouseSpec::GetRemovalCost() const
|
|
|
|
{
|
2009-11-07 22:47:54 +00:00
|
|
|
return (_price[PR_CLEAR_HOUSE] * this->removal_cost) >> 8;
|
2008-06-05 01:43:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 18:31:37 +00:00
|
|
|
/* Local */
|
2004-08-09 17:04:08 +00:00
|
|
|
static int _grow_town_result;
|
|
|
|
|
2007-10-05 22:39:15 +00:00
|
|
|
/* Describe the possible states */
|
|
|
|
enum TownGrowthResult {
|
|
|
|
GROWTH_SUCCEED = -1,
|
|
|
|
GROWTH_SEARCH_STOPPED = 0
|
|
|
|
// GROWTH_SEARCH_RUNNING >= 1
|
|
|
|
};
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool BuildTownHouse(Town *t, TileIndex tile);
|
2009-09-21 20:22:56 +00:00
|
|
|
static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static void TownDrawHouseLift(const TileInfo *ti)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-14 19:57:49 +00:00
|
|
|
AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
typedef void TownDrawTileProc(const TileInfo *ti);
|
2009-03-15 16:04:39 +00:00
|
|
|
static TownDrawTileProc * const _town_draw_tile_procs[1] = {
|
2005-07-15 16:29:30 +00:00
|
|
|
TownDrawHouseLift
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
/**
|
|
|
|
* Return a random direction
|
|
|
|
*
|
|
|
|
* @return a random direction
|
|
|
|
*/
|
|
|
|
static inline DiagDirection RandomDiagDir()
|
|
|
|
{
|
|
|
|
return (DiagDirection)(3 & Random());
|
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* House Tile drawing handler.
|
|
|
|
* Part of the tile loop process
|
|
|
|
* @param ti TileInfo of the tile to draw
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static void DrawTile_Town(TileInfo *ti)
|
|
|
|
{
|
2007-03-19 11:27:30 +00:00
|
|
|
HouseID house_id = GetHouseType(ti->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
if (house_id >= NEW_HOUSE_OFFSET) {
|
2007-03-20 10:06:39 +00:00
|
|
|
/* Houses don't necessarily need new graphics. If they don't have a
|
|
|
|
* spritegroup associated with them, then the sprite for the substitute
|
|
|
|
* house id is drawn instead. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (HouseSpec::Get(house_id)->grf_prop.spritegroup[0] != nullptr) {
|
2007-03-20 10:06:39 +00:00
|
|
|
DrawNewHouseTile(ti, house_id);
|
|
|
|
return;
|
|
|
|
} else {
|
2010-08-07 21:03:38 +00:00
|
|
|
house_id = HouseSpec::Get(house_id)->grf_prop.subst_id;
|
2007-03-20 10:06:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
/* Retrieve pointer to the draw town tile struct */
|
2008-04-04 14:53:06 +00:00
|
|
|
const DrawBuildingsTileStruct *dcts = &_town_draw_tile_data[house_id << 4 | TileHash2Bit(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)];
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
|
2007-01-14 19:57:49 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
DrawGroundSprite(dcts->ground.sprite, dcts->ground.pal);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-03 19:55:40 +00:00
|
|
|
/* If houses are invisible, do not draw the upper part */
|
|
|
|
if (IsInvisibilitySet(TO_HOUSES)) return;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Add a house on top of the ground? */
|
2008-04-04 14:53:06 +00:00
|
|
|
SpriteID image = dcts->building.sprite;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (image != 0) {
|
2007-07-26 14:07:11 +00:00
|
|
|
AddSortableSpriteToDraw(image, dcts->building.pal,
|
2004-11-22 11:01:30 +00:00
|
|
|
ti->x + dcts->subtile_x,
|
|
|
|
ti->y + dcts->subtile_y,
|
2007-09-14 21:32:21 +00:00
|
|
|
dcts->width,
|
|
|
|
dcts->height,
|
2004-08-09 17:04:08 +00:00
|
|
|
dcts->dz,
|
2007-07-26 14:07:11 +00:00
|
|
|
ti->z,
|
2007-11-10 01:17:15 +00:00
|
|
|
IsTransparencySet(TO_HOUSES)
|
2006-06-21 16:40:23 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-10 01:17:15 +00:00
|
|
|
if (IsTransparencySet(TO_HOUSES)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2006-04-24 21:10:56 +00:00
|
|
|
int proc = dcts->draw_proc - 1;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
|
|
|
if (proc >= 0) _town_draw_tile_procs[proc](ti);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 11:36:10 +00:00
|
|
|
static int GetSlopePixelZ_Town(TileIndex tile, uint x, uint y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-11-04 10:18:13 +00:00
|
|
|
return GetTileMaxPixelZ(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-19 15:39:12 +00:00
|
|
|
/** Tile callback routine */
|
2007-07-26 16:51:10 +00:00
|
|
|
static Foundation GetFoundation_Town(TileIndex tile, Slope tileh)
|
2004-08-13 18:27:33 +00:00
|
|
|
{
|
2009-09-16 19:10:50 +00:00
|
|
|
HouseID hid = GetHouseType(tile);
|
|
|
|
|
|
|
|
/* For NewGRF house tiles we might not be drawing a foundation. We need to
|
|
|
|
* account for this, as other structures should
|
|
|
|
* draw the wall of the foundation in this case.
|
|
|
|
*/
|
|
|
|
if (hid >= NEW_HOUSE_OFFSET) {
|
|
|
|
const HouseSpec *hs = HouseSpec::Get(hid);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.spritegroup[0] != nullptr && HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
|
2009-09-16 19:10:50 +00:00
|
|
|
uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, hid, Town::GetByTile(tile), tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res)) return FOUNDATION_NONE;
|
2009-09-16 19:10:50 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-26 16:51:10 +00:00
|
|
|
return FlatteningFoundation(tileh);
|
2004-08-13 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Animate a tile for a town
|
|
|
|
* Only certain houses can be animated
|
2013-01-08 22:46:42 +00:00
|
|
|
* The newhouses animation supersedes regular ones
|
2007-04-27 02:27:44 +00:00
|
|
|
* @param tile TileIndex of the house to animate
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static void AnimateTile_Town(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-19 11:27:30 +00:00
|
|
|
if (GetHouseType(tile) >= NEW_HOUSE_OFFSET) {
|
|
|
|
AnimateNewHouseTile(tile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_tick_counter & 3) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* If the house is not one with a lift anymore, then stop this animating.
|
|
|
|
* Not exactly sure when this happens, but probably when a house changes.
|
|
|
|
* Before this was just a return...so it'd leak animated tiles..
|
|
|
|
* That bug seems to have been here since day 1?? */
|
2009-06-26 14:02:27 +00:00
|
|
|
if (!(HouseSpec::Get(GetHouseType(tile))->building_flags & BUILDING_IS_ANIMATED)) {
|
2005-07-15 16:29:30 +00:00
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
if (!LiftHasDestination(tile)) {
|
2008-04-04 14:53:06 +00:00
|
|
|
uint i;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
/* Building has 6 floors, number 0 .. 6, where 1 is illegal.
|
|
|
|
* This is due to the fact that the first floor is, in the graphics,
|
2006-01-26 18:36:14 +00:00
|
|
|
* the height of 2 'normal' floors.
|
2008-04-04 14:53:06 +00:00
|
|
|
* Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2008-04-04 14:53:06 +00:00
|
|
|
i = RandomRange(7);
|
|
|
|
} while (i == 1 || i * 6 == GetLiftPosition(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
SetLiftDestination(tile, i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
int pos = GetLiftPosition(tile);
|
|
|
|
int dest = GetLiftDestination(tile) * 6;
|
2006-04-03 11:46:28 +00:00
|
|
|
pos += (pos < dest) ? 1 : -1;
|
|
|
|
SetLiftPosition(tile, pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-20 08:43:31 +00:00
|
|
|
if (pos == dest) {
|
|
|
|
HaltLift(tile);
|
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Determines if a town is close to a tile
|
|
|
|
* @param tile TileIndex of the tile to query
|
|
|
|
* @param dist maximum distance to be accepted
|
|
|
|
* @returns true if the tile correspond to the distance criteria
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool IsCloseToTown(TileIndex tile, uint dist)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2019-02-18 16:32:53 +00:00
|
|
|
if (_town_kdtree.Count() == 0) return false;
|
|
|
|
Town *t = Town::Get(_town_kdtree.FindNearest(TileX(tile), TileY(tile)));
|
|
|
|
return DistanceManhattan(tile, t->xy) < dist;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Resize the sign(label) of the town after changes in
|
|
|
|
* population (creation or growth or else)
|
|
|
|
*/
|
2009-07-13 22:33:25 +00:00
|
|
|
void Town::UpdateVirtCoord()
|
2004-12-31 18:57:24 +00:00
|
|
|
{
|
2009-07-13 22:33:25 +00:00
|
|
|
Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
|
2019-12-01 22:17:33 +00:00
|
|
|
|
|
|
|
if (this->cache.sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeTown(this->index));
|
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
SetDParam(0, this->index);
|
2012-04-25 20:50:13 +00:00
|
|
|
SetDParam(1, this->cache.population);
|
|
|
|
this->cache.sign.UpdatePosition(pt.x, pt.y - 24 * ZOOM_LVL_BASE,
|
2015-04-25 11:58:19 +00:00
|
|
|
_settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
|
|
|
|
STR_VIEWPORT_TOWN);
|
2009-08-22 20:28:30 +00:00
|
|
|
|
2019-12-01 22:17:33 +00:00
|
|
|
_viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeTown(this->index));
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, this->index);
|
2004-12-31 18:57:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 20:20:16 +00:00
|
|
|
/** Update the virtual coords needed to draw the town sign for all towns. */
|
|
|
|
void UpdateAllTownVirtCoords()
|
|
|
|
{
|
2019-12-17 21:04:09 +00:00
|
|
|
for (Town *t : Town::Iterate()) {
|
2009-07-13 22:33:25 +00:00
|
|
|
t->UpdateVirtCoord();
|
2007-11-25 20:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:40:31 +00:00
|
|
|
void ClearAllTownCachedNames()
|
|
|
|
{
|
|
|
|
for (Town *t : Town::Iterate()) {
|
|
|
|
t->cached_name.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Change the towns population
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param t Town which population has changed
|
|
|
|
* @param mod population change (can be positive or negative)
|
2007-04-27 02:27:44 +00:00
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static void ChangePopulation(Town *t, int mod)
|
|
|
|
{
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.population += mod;
|
2012-02-15 21:34:58 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_VIEW, t->index); // Cargo requirements may appear/vanish for small populations
|
2019-03-12 19:12:34 +00:00
|
|
|
if (_settings_client.gui.population_in_label) t->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2020-01-06 20:19:50 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_POPULATION_CHANGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Determines the world population
|
|
|
|
* Basically, count population of all towns, one by one
|
|
|
|
* @return uint32 the calculated population of the world
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
uint32 GetWorldPopulation()
|
2005-01-21 16:51:25 +00:00
|
|
|
{
|
2008-04-04 14:53:06 +00:00
|
|
|
uint32 pop = 0;
|
2019-12-17 21:04:09 +00:00
|
|
|
for (const Town *t : Town::Iterate()) pop += t->cache.population;
|
2005-01-21 16:51:25 +00:00
|
|
|
return pop;
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:07:15 +00:00
|
|
|
/**
|
|
|
|
* Remove stations from nearby station list if a town is no longer in the catchment area of each.
|
2020-05-15 20:18:03 +00:00
|
|
|
* To improve performance only checks stations that cover the provided house area (doesn't need to contain an actual house).
|
2019-02-14 21:07:15 +00:00
|
|
|
* @param t Town to work on
|
2020-05-15 20:18:03 +00:00
|
|
|
* @param tile Location of house area (north part)
|
|
|
|
* @param flags BuildingFlags containing the size of house area
|
2019-02-14 21:07:15 +00:00
|
|
|
*/
|
2020-05-15 20:18:03 +00:00
|
|
|
static void RemoveNearbyStations(Town *t, TileIndex tile, BuildingFlags flags)
|
2019-02-14 21:07:15 +00:00
|
|
|
{
|
|
|
|
for (StationList::iterator it = t->stations_near.begin(); it != t->stations_near.end(); /* incremented inside loop */) {
|
|
|
|
const Station *st = *it;
|
2020-05-15 20:18:03 +00:00
|
|
|
|
|
|
|
bool covers_area = st->TileIsInCatchment(tile);
|
|
|
|
if (flags & BUILDING_2_TILES_Y) covers_area |= st->TileIsInCatchment(tile + TileDiffXY(0, 1));
|
|
|
|
if (flags & BUILDING_2_TILES_X) covers_area |= st->TileIsInCatchment(tile + TileDiffXY(1, 0));
|
|
|
|
if (flags & BUILDING_HAS_4_TILES) covers_area |= st->TileIsInCatchment(tile + TileDiffXY(1, 1));
|
|
|
|
|
|
|
|
if (covers_area && !st->CatchmentCoversTown(t->index)) {
|
2019-02-14 21:07:15 +00:00
|
|
|
it = t->stations_near.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Helper function for house completion stages progression
|
|
|
|
* @param tile TileIndex of the house (or parts of it) to "grow"
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static void MakeSingleHouseBigger(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/* progress in construction stages */
|
2006-04-03 14:56:07 +00:00
|
|
|
IncHouseConstructionTick(tile);
|
|
|
|
if (GetHouseConstructionTick(tile) != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
AnimateNewHouseConstruction(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
if (IsHouseCompleted(tile)) {
|
|
|
|
/* Now that construction is complete, we can add the population of the
|
|
|
|
* building to the town. */
|
2010-08-26 17:01:17 +00:00
|
|
|
ChangePopulation(Town::GetByTile(tile), HouseSpec::Get(GetHouseType(tile))->population);
|
2008-11-23 14:17:41 +00:00
|
|
|
ResetHouseAge(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Make the house advance in its construction stages until completion
|
2007-04-27 02:27:44 +00:00
|
|
|
* @param tile TileIndex of house
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static void MakeTownHouseBigger(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
uint flags = HouseSpec::Get(GetHouseType(tile))->building_flags;
|
2007-03-19 11:27:30 +00:00
|
|
|
if (flags & BUILDING_HAS_1_TILE) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
|
|
|
|
if (flags & BUILDING_2_TILES_Y) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
|
|
|
|
if (flags & BUILDING_2_TILES_X) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
|
|
|
|
if (flags & BUILDING_HAS_4_TILES) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:44 +00:00
|
|
|
/**
|
2008-10-19 15:39:12 +00:00
|
|
|
* Tile callback function.
|
|
|
|
*
|
2007-04-27 02:27:44 +00:00
|
|
|
* Periodic tic handler for houses and town
|
|
|
|
* @param tile been asked to do its stuff
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static void TileLoop_Town(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-19 11:27:30 +00:00
|
|
|
HouseID house_id = GetHouseType(tile);
|
|
|
|
|
|
|
|
/* NewHouseTileLoop returns false if Callback 21 succeeded, i.e. the house
|
|
|
|
* doesn't exist any more, so don't continue here. */
|
|
|
|
if (house_id >= NEW_HOUSE_OFFSET && !NewHouseTileLoop(tile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
if (!IsHouseCompleted(tile)) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Construction is not completed. See if we can go further in construction*/
|
2004-08-09 17:04:08 +00:00
|
|
|
MakeTownHouseBigger(tile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2008-04-04 14:53:06 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
/* If the lift has a destination, it is already an animated tile. */
|
2007-04-27 02:27:44 +00:00
|
|
|
if ((hs->building_flags & BUILDING_IS_ANIMATED) &&
|
|
|
|
house_id < NEW_HOUSE_OFFSET &&
|
|
|
|
!LiftHasDestination(tile) &&
|
2008-04-04 14:53:06 +00:00
|
|
|
Chance16(1, 2)) {
|
2007-04-27 02:27:44 +00:00
|
|
|
AddAnimatedTile(tile);
|
2008-04-04 14:53:06 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-26 13:46:23 +00:00
|
|
|
Town *t = Town::GetByTile(tile);
|
2008-04-04 14:53:06 +00:00
|
|
|
uint32 r = Random();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-04 18:12:10 +00:00
|
|
|
StationFinder stations(TileArea(tile, 1, 1));
|
2009-11-15 21:06:13 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
|
2007-05-19 11:17:30 +00:00
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile);
|
|
|
|
|
2008-01-14 18:19:29 +00:00
|
|
|
if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
|
2007-05-19 11:17:30 +00:00
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile);
|
2007-05-19 11:17:30 +00:00
|
|
|
if (cargo == CT_INVALID) continue;
|
|
|
|
|
|
|
|
uint amt = GB(callback, 0, 8);
|
2009-05-10 15:42:59 +00:00
|
|
|
if (amt == 0) continue;
|
|
|
|
|
2009-11-15 21:06:13 +00:00
|
|
|
uint moved = MoveGoodsToStation(cargo, amt, ST_TOWN, t->index, stations.GetStations());
|
2007-05-19 11:17:30 +00:00
|
|
|
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cs = CargoSpec::Get(cargo);
|
2011-11-23 16:05:19 +00:00
|
|
|
t->supplied[cs->Index()].new_max += amt;
|
|
|
|
t->supplied[cs->Index()].new_act += moved;
|
2007-05-19 11:17:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-07 18:06:39 +00:00
|
|
|
switch (_settings_game.economy.town_cargogen_mode) {
|
|
|
|
case TCGM_ORIGINAL:
|
|
|
|
/* Original (quadratic) cargo generation algorithm */
|
|
|
|
if (GB(r, 0, 8) < hs->population) {
|
|
|
|
uint amt = GB(r, 0, 8) / 8 + 1;
|
|
|
|
|
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
|
|
|
t->supplied[CT_PASSENGERS].new_max += amt;
|
|
|
|
t->supplied[CT_PASSENGERS].new_act += MoveGoodsToStation(CT_PASSENGERS, amt, ST_TOWN, t->index, stations.GetStations());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GB(r, 8, 8) < hs->mail_generation) {
|
|
|
|
uint amt = GB(r, 8, 8) / 8 + 1;
|
|
|
|
|
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
|
|
|
t->supplied[CT_MAIL].new_max += amt;
|
|
|
|
t->supplied[CT_MAIL].new_act += MoveGoodsToStation(CT_MAIL, amt, ST_TOWN, t->index, stations.GetStations());
|
|
|
|
}
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2018-11-07 18:06:39 +00:00
|
|
|
case TCGM_BITCOUNT:
|
|
|
|
/* Binomial distribution per tick, by a series of coin flips */
|
|
|
|
/* Reduce generation rate to a 1/4, using tile bits to spread out distribution.
|
|
|
|
* As tick counter is incremented by 256 between each call, we ignore the lower 8 bits. */
|
|
|
|
if (GB(_tick_counter, 8, 2) == GB(tile, 0, 2)) {
|
|
|
|
/* Make a bitmask with up to 32 bits set, one for each potential pax */
|
|
|
|
int genmax = (hs->population + 7) / 8;
|
|
|
|
uint32 genmask = (genmax >= 32) ? 0xFFFFFFFF : ((1 << genmax) - 1);
|
|
|
|
/* Mask random value by potential pax and count number of actual pax */
|
|
|
|
uint amt = CountBits(r & genmask);
|
|
|
|
/* Adjust and apply */
|
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
|
|
|
t->supplied[CT_PASSENGERS].new_max += amt;
|
|
|
|
t->supplied[CT_PASSENGERS].new_act += MoveGoodsToStation(CT_PASSENGERS, amt, ST_TOWN, t->index, stations.GetStations());
|
|
|
|
|
|
|
|
/* Do the same for mail, with a fresh random */
|
|
|
|
r = Random();
|
|
|
|
genmax = (hs->mail_generation + 7) / 8;
|
|
|
|
genmask = (genmax >= 32) ? 0xFFFFFFFF : ((1 << genmax) - 1);
|
|
|
|
amt = CountBits(r & genmask);
|
|
|
|
if (EconomyIsInRecession()) amt = (amt + 1) >> 1;
|
|
|
|
t->supplied[CT_MAIL].new_max += amt;
|
|
|
|
t->supplied[CT_MAIL].new_act += MoveGoodsToStation(CT_MAIL, amt, ST_TOWN, t->index, stations.GetStations());
|
|
|
|
}
|
|
|
|
break;
|
2005-07-21 06:31:02 +00:00
|
|
|
|
2018-11-07 18:06:39 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
2007-05-19 11:17:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 08:10:04 +00:00
|
|
|
Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((hs->building_flags & BUILDING_HAS_1_TILE) &&
|
2013-10-28 10:55:34 +00:00
|
|
|
HasBit(t->flags, TOWN_IS_GROWING) &&
|
2007-04-27 02:27:44 +00:00
|
|
|
CanDeleteHouse(tile) &&
|
2008-11-23 14:17:41 +00:00
|
|
|
GetHouseAge(tile) >= hs->minimum_life &&
|
2007-04-27 02:27:44 +00:00
|
|
|
--t->time_until_rebuild == 0) {
|
2007-04-12 17:24:34 +00:00
|
|
|
t->time_until_rebuild = GB(r, 16, 8) + 192;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ClearTownHouse(t, tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-12 17:24:34 +00:00
|
|
|
/* Rebuild with another house? */
|
2021-01-07 07:01:23 +00:00
|
|
|
if (GB(r, 24, 8) >= 12) {
|
|
|
|
/* If we are multi-tile houses, make sure to replace the house
|
|
|
|
* closest to city center. If we do not do this, houses tend to
|
|
|
|
* wander away from roads and other houses. */
|
|
|
|
if (hs->building_flags & BUILDING_HAS_2_TILES) {
|
|
|
|
/* House tiles are always the most north tile. Move the new
|
|
|
|
* house to the south if we are north of the city center. */
|
|
|
|
TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
|
|
|
|
int x = Clamp(grid_pos.x, 0, 1);
|
|
|
|
int y = Clamp(grid_pos.y, 0, 1);
|
|
|
|
|
|
|
|
if (hs->building_flags & TILE_SIZE_2x2) {
|
|
|
|
tile = TILE_ADDXY(tile, x, y);
|
|
|
|
} else if (hs->building_flags & TILE_SIZE_1x2) {
|
|
|
|
tile = TILE_ADDXY(tile, 0, y);
|
|
|
|
} else if (hs->building_flags & TILE_SIZE_2x1) {
|
|
|
|
tile = TILE_ADDXY(tile, x, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildTownHouse(t, tile);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 21:20:05 +00:00
|
|
|
static CommandCost ClearTile_Town(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-04-21 23:40:56 +00:00
|
|
|
if (flags & DC_AUTO) return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
|
2007-03-19 11:27:30 +00:00
|
|
|
if (!CanDeleteHouse(tile)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2008-04-04 14:53:06 +00:00
|
|
|
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2008-06-05 01:43:03 +00:00
|
|
|
cost.AddCost(hs->GetRemovalCost());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
int rating = hs->remove_rating_decrease;
|
2010-10-01 16:42:28 +00:00
|
|
|
Town *t = Town::GetByTile(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(_current_company)) {
|
2009-02-11 18:33:27 +00:00
|
|
|
if (rating > t->ratings[_current_company] && !(flags & DC_NO_TEST_TOWN_RATING) && !_cheats.magic_bulldozer.value) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-11 18:50:47 +00:00
|
|
|
ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
ClearTownHouse(t, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-06-27 21:06:58 +00:00
|
|
|
static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced)
|
2008-04-06 22:32:20 +00:00
|
|
|
{
|
|
|
|
HouseID house_id = GetHouseType(tile);
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2009-06-26 13:46:23 +00:00
|
|
|
Town *t = Town::GetByTile(tile);
|
2008-04-06 22:32:20 +00:00
|
|
|
|
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
|
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, tile);
|
|
|
|
|
|
|
|
if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
|
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile);
|
2008-04-06 22:32:20 +00:00
|
|
|
|
|
|
|
if (cargo == CT_INVALID) continue;
|
2009-06-27 18:26:50 +00:00
|
|
|
produced[cargo]++;
|
2008-04-06 22:32:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hs->population > 0) {
|
2009-06-27 18:26:50 +00:00
|
|
|
produced[CT_PASSENGERS]++;
|
2008-04-06 22:32:20 +00:00
|
|
|
}
|
|
|
|
if (hs->mail_generation > 0) {
|
2009-06-27 18:26:50 +00:00
|
|
|
produced[CT_MAIL]++;
|
2008-04-06 22:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:08:39 +00:00
|
|
|
static inline void AddAcceptedCargoSetMask(CargoID cargo, uint amount, CargoArray &acceptance, CargoTypes *always_accepted)
|
2009-09-07 07:39:08 +00:00
|
|
|
{
|
|
|
|
if (cargo == CT_INVALID || amount == 0) return;
|
|
|
|
acceptance[cargo] += amount;
|
2009-09-20 17:44:33 +00:00
|
|
|
SetBit(*always_accepted, cargo);
|
2009-09-07 07:39:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:08:39 +00:00
|
|
|
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2018-07-28 21:28:24 +00:00
|
|
|
CargoID accepts[lengthof(hs->accepts_cargo)];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-21 20:23:20 +00:00
|
|
|
/* Set the initial accepted cargo types */
|
|
|
|
for (uint8 i = 0; i < lengthof(accepts); i++) {
|
|
|
|
accepts[i] = hs->accepts_cargo[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for custom accepted cargo types */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) {
|
2009-06-26 13:46:23 +00:00
|
|
|
uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
|
2007-04-21 20:23:20 +00:00
|
|
|
if (callback != CALLBACK_FAILED) {
|
|
|
|
/* Replace accepted cargo types with translated values from callback */
|
2010-08-07 21:03:38 +00:00
|
|
|
accepts[0] = GetCargoTranslation(GB(callback, 0, 5), hs->grf_prop.grffile);
|
|
|
|
accepts[1] = GetCargoTranslation(GB(callback, 5, 5), hs->grf_prop.grffile);
|
|
|
|
accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grf_prop.grffile);
|
2007-04-21 20:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for custom cargo acceptance */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) {
|
2009-06-26 13:46:23 +00:00
|
|
|
uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
|
2007-04-21 20:23:20 +00:00
|
|
|
if (callback != CALLBACK_FAILED) {
|
2009-09-20 17:44:33 +00:00
|
|
|
AddAcceptedCargoSetMask(accepts[0], GB(callback, 0, 4), acceptance, always_accepted);
|
|
|
|
AddAcceptedCargoSetMask(accepts[1], GB(callback, 4, 4), acceptance, always_accepted);
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.game_creation.landscape != LT_TEMPERATE && HasBit(callback, 12)) {
|
2007-04-21 20:23:20 +00:00
|
|
|
/* The 'S' bit indicates food instead of goods */
|
2009-09-20 17:44:33 +00:00
|
|
|
AddAcceptedCargoSetMask(CT_FOOD, GB(callback, 8, 4), acceptance, always_accepted);
|
2007-04-21 20:23:20 +00:00
|
|
|
} else {
|
2009-09-20 17:44:33 +00:00
|
|
|
AddAcceptedCargoSetMask(accepts[2], GB(callback, 8, 4), acceptance, always_accepted);
|
2007-04-21 20:23:20 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No custom acceptance, so fill in with the default values */
|
|
|
|
for (uint8 i = 0; i < lengthof(accepts); i++) {
|
2009-09-20 17:44:33 +00:00
|
|
|
AddAcceptedCargoSetMask(accepts[i], hs->cargo_acceptance[i], acceptance, always_accepted);
|
2007-04-21 20:23:20 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetTileDesc_Town(TileIndex tile, TileDesc *td)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-20 16:06:57 +00:00
|
|
|
const HouseID house = GetHouseType(tile);
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2009-01-20 16:06:57 +00:00
|
|
|
bool house_completed = IsHouseCompleted(tile);
|
2008-07-25 22:37:34 +00:00
|
|
|
|
|
|
|
td->str = hs->building_name;
|
2009-01-20 16:06:57 +00:00
|
|
|
|
2009-06-26 13:46:23 +00:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_CUSTOM_NAME, house_completed ? 1 : 0, 0, house, Town::GetByTile(tile), tile);
|
2011-11-08 17:24:43 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
|
|
|
|
if (callback_res > 0x400) {
|
|
|
|
ErrorUnknownCallbackResult(hs->grf_prop.grffile->grfid, CBID_HOUSE_CUSTOM_NAME, callback_res);
|
|
|
|
} else {
|
|
|
|
StringID new_name = GetGRFStringID(hs->grf_prop.grffile->grfid, 0xD000 + callback_res);
|
|
|
|
if (new_name != STR_NULL && new_name != STR_UNDEFINED) {
|
|
|
|
td->str = new_name;
|
|
|
|
}
|
2009-01-20 16:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!house_completed) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParamX(td->dparam, 0, td->str);
|
2009-08-05 17:59:21 +00:00
|
|
|
td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.grffile != nullptr) {
|
2010-08-07 21:03:38 +00:00
|
|
|
const GRFConfig *gc = GetGRFConfig(hs->grf_prop.grffile->grfid);
|
2010-02-28 20:28:08 +00:00
|
|
|
td->grf = gc->GetName();
|
2008-07-25 22:37:34 +00:00
|
|
|
}
|
|
|
|
|
2008-05-21 22:15:39 +00:00
|
|
|
td->owner[0] = OWNER_TOWN;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
static TrackStatus GetTileTrackStatus_Town(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void ChangeTileOwner_Town(TileIndex tile, Owner old_owner, Owner new_owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
}
|
|
|
|
|
2006-01-05 12:40:50 +00:00
|
|
|
static bool GrowTown(Town *t);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void TownTickHandler(Town *t)
|
|
|
|
{
|
2013-10-28 10:55:34 +00:00
|
|
|
if (HasBit(t->flags, TOWN_IS_GROWING)) {
|
2018-05-02 19:01:30 +00:00
|
|
|
int i = (int)t->grow_counter - 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (i < 0) {
|
|
|
|
if (GrowTown(t)) {
|
2018-05-02 19:01:30 +00:00
|
|
|
i = t->growth_rate;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2018-05-02 19:01:30 +00:00
|
|
|
/* If growth failed wait a bit before retrying */
|
2021-01-08 10:16:18 +00:00
|
|
|
i = std::min<uint16>(t->growth_rate, TOWN_GROWTH_TICKS - 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t->grow_counter = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OnTick_Town()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_game_mode == GM_EDITOR) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-12-17 21:04:09 +00:00
|
|
|
for (Town *t : Town::Iterate()) {
|
2018-05-02 19:01:30 +00:00
|
|
|
TownTickHandler(t);
|
2005-03-06 23:46:52 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
/**
|
|
|
|
* Return the RoadBits of a tile
|
|
|
|
*
|
|
|
|
* @note There are many other functions doing things like that.
|
|
|
|
* @note Needs to be checked for needlessness.
|
|
|
|
* @param tile The tile we want to analyse
|
|
|
|
* @return The roadbits of the given tile
|
|
|
|
*/
|
|
|
|
static RoadBits GetTownRoadBits(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-12 09:39:30 +00:00
|
|
|
if (IsRoadDepotTile(tile) || IsStandardRoadStopTile(tile)) return ROAD_NONE;
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
return GetAnyRoadBits(tile, RTT_ROAD, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadType GetTownRoadType(const Town *t)
|
|
|
|
{
|
|
|
|
RoadType best_rt = ROADTYPE_ROAD;
|
|
|
|
const RoadTypeInfo *best = nullptr;
|
|
|
|
const uint16 assume_max_speed = 50;
|
|
|
|
|
|
|
|
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
|
|
|
|
if (RoadTypeIsTram(rt)) continue;
|
|
|
|
|
|
|
|
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
|
|
|
|
|
|
|
|
/* Unused road type. */
|
|
|
|
if (rti->label == 0) continue;
|
|
|
|
|
|
|
|
/* Can town build this road. */
|
|
|
|
if (!HasBit(rti->flags, ROTF_TOWN_BUILD)) continue;
|
|
|
|
|
|
|
|
/* Not yet introduced at this date. */
|
|
|
|
if (IsInsideMM(rti->introduction_date, 0, MAX_DAY) && rti->introduction_date > _date) continue;
|
|
|
|
|
|
|
|
if (best != nullptr) {
|
|
|
|
if ((rti->max_speed == 0 ? assume_max_speed : rti->max_speed) < (best->max_speed == 0 ? assume_max_speed : best->max_speed)) continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
best_rt = rt;
|
|
|
|
best = rti;
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_rt;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/**
|
2009-02-06 20:51:24 +00:00
|
|
|
* Check for parallel road inside a given distance.
|
|
|
|
* Assuming a road from (tile - TileOffsByDiagDir(dir)) to tile,
|
|
|
|
* is there a parallel road left or right of it within distance dist_multi?
|
2007-05-04 16:27:13 +00:00
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param tile current tile
|
2007-05-04 16:27:13 +00:00
|
|
|
* @param dir target direction
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param dist_multi distance multiplayer
|
2009-02-06 20:51:24 +00:00
|
|
|
* @return true if there is a parallel road
|
2007-05-04 16:27:13 +00:00
|
|
|
*/
|
2008-01-30 17:31:39 +00:00
|
|
|
static bool IsNeighborRoadTile(TileIndex tile, const DiagDirection dir, uint dist_multi)
|
2007-05-04 16:27:13 +00:00
|
|
|
{
|
2009-02-06 20:24:44 +00:00
|
|
|
if (!IsValidTile(tile)) return false;
|
|
|
|
|
2008-01-30 17:31:39 +00:00
|
|
|
/* Lookup table for the used diff values */
|
|
|
|
const TileIndexDiff tid_lt[3] = {
|
|
|
|
TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)),
|
|
|
|
TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)),
|
|
|
|
TileOffsByDiagDir(ReverseDiagDir(dir)),
|
|
|
|
};
|
2007-09-12 23:56:21 +00:00
|
|
|
|
|
|
|
dist_multi = (dist_multi + 1) * 4;
|
|
|
|
for (uint pos = 4; pos < dist_multi; pos++) {
|
2009-02-06 20:51:24 +00:00
|
|
|
/* Go (pos / 4) tiles to the left or the right */
|
|
|
|
TileIndexDiff cur = tid_lt[(pos & 1) ? 0 : 1] * (pos / 4);
|
2007-09-12 23:56:21 +00:00
|
|
|
|
2009-02-06 20:51:24 +00:00
|
|
|
/* Use the current tile as origin, or go one tile backwards */
|
|
|
|
if (pos & 2) cur += tid_lt[2];
|
2009-02-06 20:24:44 +00:00
|
|
|
|
2009-02-06 20:51:24 +00:00
|
|
|
/* Test for roadbit parallel to dir and facing towards the middle axis */
|
2009-02-06 20:24:44 +00:00
|
|
|
if (IsValidTile(tile + cur) &&
|
2010-07-24 10:14:39 +00:00
|
|
|
GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true;
|
2007-09-12 23:56:21 +00:00
|
|
|
}
|
|
|
|
return false;
|
2007-05-04 16:27:13 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
/**
|
|
|
|
* Check if a Road is allowed on a given tile
|
|
|
|
*
|
2008-04-01 16:27:01 +00:00
|
|
|
* @param t The current town
|
2007-09-12 23:56:21 +00:00
|
|
|
* @param tile The target tile
|
|
|
|
* @param dir The direction in which we want to extend the town
|
|
|
|
* @return true if it is allowed else false
|
|
|
|
*/
|
2008-04-01 16:27:01 +00:00
|
|
|
static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-12 09:39:30 +00:00
|
|
|
if (DistanceFromEdge(tile) == 0) return false;
|
2007-06-21 15:13:09 +00:00
|
|
|
|
2012-07-09 19:43:22 +00:00
|
|
|
/* Prevent towns from building roads under bridges along the bridge. Looks silly. */
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(tile) && GetBridgeAxis(tile) == DiagDirToAxis(dir)) return false;
|
2012-07-09 19:43:22 +00:00
|
|
|
|
2011-07-31 09:42:05 +00:00
|
|
|
/* Check if there already is a road at this point? */
|
|
|
|
if (GetTownRoadBits(tile) == ROAD_NONE) {
|
|
|
|
/* No, try if we are able to build a road piece there.
|
|
|
|
* If that fails clear the land, and if that fails exit.
|
|
|
|
* This is to make sure that we can build a road here later. */
|
2019-04-06 06:46:15 +00:00
|
|
|
RoadType rt = GetTownRoadType(t);
|
2021-01-09 11:18:31 +00:00
|
|
|
if (DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_Y : ROAD_X) | (rt << 4), 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD).Failed() &&
|
|
|
|
DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Failed()) {
|
2011-07-31 09:42:05 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2011-07-31 09:42:05 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-04 10:23:48 +00:00
|
|
|
Slope cur_slope = _settings_game.construction.build_on_slopes ? GetFoundationSlope(tile) : GetTileSlope(tile);
|
2011-07-31 09:42:05 +00:00
|
|
|
bool ret = !IsNeighborRoadTile(tile, dir, t->layout == TL_ORIGINAL ? 1 : 2);
|
|
|
|
if (cur_slope == SLOPE_FLAT) return ret;
|
|
|
|
|
|
|
|
/* If the tile is not a slope in the right direction, then
|
|
|
|
* maybe terraform some. */
|
|
|
|
Slope desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE;
|
|
|
|
if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
|
|
|
|
if (Chance16(1, 8)) {
|
|
|
|
CommandCost res = CMD_ERROR;
|
|
|
|
if (!_generating_world && Chance16(1, 10)) {
|
|
|
|
/* Note: Do not replace "^ SLOPE_ELEVATED" with ComplementSlope(). The slope might be steep. */
|
|
|
|
res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ SLOPE_ELEVATED, 0,
|
|
|
|
DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
|
|
|
|
}
|
|
|
|
if (res.Failed() && Chance16(1, 3)) {
|
|
|
|
/* We can consider building on the slope, though. */
|
|
|
|
return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-31 09:42:05 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2011-07-31 09:42:05 +00:00
|
|
|
return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool TerraformTownTile(TileIndex tile, int edges, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-02-25 21:50:54 +00:00
|
|
|
assert(tile < MapSize());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
CommandCost r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (r.Failed() || r.GetCost() >= (_price[PR_TERRAFORM] + 2) * 8) return false;
|
2006-04-10 07:15:58 +00:00
|
|
|
DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void LevelTownLand(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-02-25 21:50:54 +00:00
|
|
|
assert(tile < MapSize());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Don't terraform if land is plain or if there's a house there. */
|
2006-02-21 07:41:54 +00:00
|
|
|
if (IsTileType(tile, MP_HOUSE)) return;
|
2011-11-04 10:22:27 +00:00
|
|
|
Slope tileh = GetTileSlope(tile);
|
2006-04-23 13:48:16 +00:00
|
|
|
if (tileh == SLOPE_FLAT) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* First try up, then down */
|
2008-01-30 17:31:39 +00:00
|
|
|
if (!TerraformTownTile(tile, ~tileh & SLOPE_ELEVATED, 1)) {
|
|
|
|
TerraformTownTile(tile, tileh & SLOPE_ELEVATED, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/**
|
|
|
|
* Generate the RoadBits of a grid tile
|
|
|
|
*
|
|
|
|
* @param t current town
|
|
|
|
* @param tile tile in reference to the town
|
2007-09-12 23:56:21 +00:00
|
|
|
* @param dir The direction to which we are growing ATM
|
2007-05-04 16:27:13 +00:00
|
|
|
* @return the RoadBit of the current tile regarding
|
|
|
|
* the selected town layout
|
|
|
|
*/
|
2008-04-01 16:27:01 +00:00
|
|
|
static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir)
|
2007-05-04 16:27:13 +00:00
|
|
|
{
|
|
|
|
/* align the grid to the downtown */
|
2007-09-26 16:12:43 +00:00
|
|
|
TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile
|
2007-09-12 23:56:21 +00:00
|
|
|
RoadBits rcmd = ROAD_NONE;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t->layout) {
|
2007-05-04 16:27:13 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
|
|
|
|
case TL_2X2_GRID:
|
2007-09-12 23:56:21 +00:00
|
|
|
if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y;
|
|
|
|
if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_3X3_GRID:
|
2007-09-12 23:56:21 +00:00
|
|
|
if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
|
|
|
|
if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-19 18:15:59 +00:00
|
|
|
/* Optimise only X-junctions */
|
2007-10-20 17:38:23 +00:00
|
|
|
if (rcmd != ROAD_ALL) return rcmd;
|
2007-09-12 23:56:21 +00:00
|
|
|
|
2007-10-20 17:38:23 +00:00
|
|
|
RoadBits rb_template;
|
|
|
|
|
2011-11-04 10:22:27 +00:00
|
|
|
switch (GetTileSlope(tile)) {
|
2007-10-20 17:38:23 +00:00
|
|
|
default: rb_template = ROAD_ALL; break;
|
|
|
|
case SLOPE_W: rb_template = ROAD_NW | ROAD_SW; break;
|
|
|
|
case SLOPE_SW: rb_template = ROAD_Y | ROAD_SW; break;
|
|
|
|
case SLOPE_S: rb_template = ROAD_SW | ROAD_SE; break;
|
|
|
|
case SLOPE_SE: rb_template = ROAD_X | ROAD_SE; break;
|
|
|
|
case SLOPE_E: rb_template = ROAD_SE | ROAD_NE; break;
|
|
|
|
case SLOPE_NE: rb_template = ROAD_Y | ROAD_NE; break;
|
|
|
|
case SLOPE_N: rb_template = ROAD_NE | ROAD_NW; break;
|
|
|
|
case SLOPE_NW: rb_template = ROAD_X | ROAD_NW; break;
|
|
|
|
case SLOPE_STEEP_W:
|
|
|
|
case SLOPE_STEEP_S:
|
|
|
|
case SLOPE_STEEP_E:
|
|
|
|
case SLOPE_STEEP_N:
|
|
|
|
rb_template = ROAD_NONE;
|
|
|
|
break;
|
2007-09-19 18:15:59 +00:00
|
|
|
}
|
2007-09-12 23:56:21 +00:00
|
|
|
|
2007-10-20 17:38:23 +00:00
|
|
|
/* Stop if the template is compatible to the growth dir */
|
|
|
|
if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
|
|
|
|
/* If not generate a straight road in the direction of the growth */
|
|
|
|
return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
|
2007-05-04 16:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-26 16:12:43 +00:00
|
|
|
* Grows the town with an extra house.
|
|
|
|
* Check if there are enough neighbor house tiles
|
|
|
|
* next to the current tile. If there are enough
|
|
|
|
* add another house.
|
2007-05-04 16:27:13 +00:00
|
|
|
*
|
2007-09-26 16:12:43 +00:00
|
|
|
* @param t The current town
|
|
|
|
* @param tile The target tile for the extra house
|
|
|
|
* @return true if an extra house has been added
|
2007-05-04 16:27:13 +00:00
|
|
|
*/
|
2007-09-26 16:12:43 +00:00
|
|
|
static bool GrowTownWithExtraHouse(Town *t, TileIndex tile)
|
2007-05-04 16:27:13 +00:00
|
|
|
{
|
|
|
|
/* We can't look further than that. */
|
2009-07-12 09:39:30 +00:00
|
|
|
if (DistanceFromEdge(tile) == 0) return false;
|
2007-09-26 16:12:43 +00:00
|
|
|
|
|
|
|
uint counter = 0; // counts the house neighbor tiles
|
|
|
|
|
|
|
|
/* Check the tiles E,N,W and S of the current tile for houses */
|
|
|
|
for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
|
2009-07-12 09:39:30 +00:00
|
|
|
/* Count both void and house tiles for checking whether there
|
|
|
|
* are enough houses in the area. This to make it likely that
|
|
|
|
* houses get build up to the edge of the map. */
|
|
|
|
switch (GetTileType(TileAddByDiagDir(tile, dir))) {
|
|
|
|
case MP_HOUSE:
|
|
|
|
case MP_VOID:
|
|
|
|
counter++;
|
|
|
|
break;
|
2007-09-26 16:12:43 +00:00
|
|
|
|
2009-07-12 09:39:30 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-09-26 16:12:43 +00:00
|
|
|
|
|
|
|
/* If there are enough neighbors stop here */
|
|
|
|
if (counter >= 3) {
|
|
|
|
if (BuildTownHouse(t, tile)) {
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SUCCEED;
|
2007-09-26 16:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grows the town with a road piece.
|
|
|
|
*
|
|
|
|
* @param t The current town
|
|
|
|
* @param tile The current tile
|
|
|
|
* @param rcmd The RoadBits we want to build on the tile
|
|
|
|
* @return true if the RoadBits have been added else false
|
|
|
|
*/
|
|
|
|
static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd)
|
|
|
|
{
|
2019-04-06 06:46:15 +00:00
|
|
|
RoadType rt = GetTownRoadType(t);
|
|
|
|
if (DoCommand(tile, rcmd | (rt << 4), t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD).Succeeded()) {
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SUCCEED;
|
2007-09-26 16:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-24 23:37:13 +00:00
|
|
|
/**
|
2021-01-08 17:32:44 +00:00
|
|
|
* Checks if a town road can be continued into the next tile.
|
|
|
|
* Road vehicle stations, bridges, and tunnels are fine, as long as they are facing the right direction.
|
2020-12-24 23:37:13 +00:00
|
|
|
*
|
2021-01-08 17:56:39 +00:00
|
|
|
* @param t The current town
|
2021-01-08 17:32:44 +00:00
|
|
|
* @param tile The tile where the road would be built
|
|
|
|
* @param road_dir The direction of the road
|
2020-12-24 23:37:13 +00:00
|
|
|
* @return true if the road can be continued, else false
|
|
|
|
*/
|
2021-02-05 10:00:36 +00:00
|
|
|
static bool CanRoadContinueIntoNextTile(const Town *t, const TileIndex tile, const DiagDirection road_dir)
|
2020-12-24 23:37:13 +00:00
|
|
|
{
|
2021-01-08 17:32:44 +00:00
|
|
|
const int delta = TileOffsByDiagDir(road_dir); // +1 tile in the direction of the road
|
|
|
|
TileIndex next_tile = tile + delta; // The tile beyond which must be connectable to the target tile
|
|
|
|
RoadBits rcmd = DiagDirToRoadBits(ReverseDiagDir(road_dir));
|
2020-12-24 23:37:13 +00:00
|
|
|
RoadType rt = GetTownRoadType(t);
|
|
|
|
|
|
|
|
/* Before we try anything, make sure the tile is on the map and not the void. */
|
|
|
|
if (!IsValidTile(next_tile)) return false;
|
|
|
|
|
2021-01-08 17:32:44 +00:00
|
|
|
/* If the next tile is a bridge or tunnel, allow if it's continuing in the same direction. */
|
2020-12-24 23:37:13 +00:00
|
|
|
if (IsTileType(next_tile, MP_TUNNELBRIDGE)) {
|
2021-01-08 17:32:44 +00:00
|
|
|
return GetTunnelBridgeTransportType(next_tile) == TRANSPORT_ROAD && GetTunnelBridgeDirection(next_tile) == road_dir;
|
2020-12-24 23:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the next tile is a station, allow if it's a road station facing the proper direction. Otherwise return false. */
|
|
|
|
if (IsTileType(next_tile, MP_STATION)) {
|
2021-01-08 17:56:39 +00:00
|
|
|
/* If the next tile is a road station, allow if it can be entered by the new tunnel/bridge, otherwise disallow. */
|
|
|
|
return IsRoadStop(next_tile) && (GetRoadStopDir(next_tile) == ReverseDiagDir(road_dir) || (IsDriveThroughStopTile(next_tile) && GetRoadStopDir(next_tile) == road_dir));
|
2020-12-24 23:37:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 17:32:44 +00:00
|
|
|
/* If the next tile is a road depot, allow if it's facing the right way. */
|
2020-12-24 23:37:13 +00:00
|
|
|
if (IsTileType(next_tile, MP_ROAD)) {
|
2021-01-08 17:32:44 +00:00
|
|
|
return IsRoadDepot(next_tile) && GetRoadDepotDirection(next_tile) == ReverseDiagDir(road_dir);
|
2020-12-24 23:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the next tile is a railroad track, check if towns are allowed to build level crossings.
|
2021-01-08 17:32:44 +00:00
|
|
|
* If level crossing are not allowed, reject the construction. Else allow DoCommand to determine if the rail track is buildable. */
|
2020-12-24 23:37:13 +00:00
|
|
|
if (IsTileType(next_tile, MP_RAILWAY) && !_settings_game.economy.allow_town_level_crossings) return false;
|
|
|
|
|
2021-01-08 17:32:44 +00:00
|
|
|
/* If a road tile can be built, the construction is allowed. */
|
2020-12-24 23:37:13 +00:00
|
|
|
return DoCommand(next_tile, rcmd | (rt << 4), t->index, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD).Succeeded();
|
|
|
|
}
|
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
/**
|
|
|
|
* Grows the town with a bridge.
|
|
|
|
* At first we check if a bridge is reasonable.
|
|
|
|
* If so we check if we are able to build it.
|
|
|
|
*
|
|
|
|
* @param t The current town
|
|
|
|
* @param tile The current tile
|
2007-11-09 11:20:36 +00:00
|
|
|
* @param bridge_dir The valid direction in which to grow a bridge
|
2007-09-26 16:12:43 +00:00
|
|
|
* @return true if a bridge has been build else false
|
|
|
|
*/
|
2008-01-30 17:31:39 +00:00
|
|
|
static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
|
2007-09-26 16:12:43 +00:00
|
|
|
{
|
2007-11-09 11:20:36 +00:00
|
|
|
assert(bridge_dir < DIAGDIR_END);
|
|
|
|
|
2011-11-04 10:22:27 +00:00
|
|
|
const Slope slope = GetTileSlope(tile);
|
2007-11-09 11:20:36 +00:00
|
|
|
|
|
|
|
/* Make sure the direction is compatible with the slope.
|
2008-01-30 17:31:39 +00:00
|
|
|
* Well we check if the slope has an up bit set in the
|
|
|
|
* reverse direction. */
|
2011-09-06 13:32:37 +00:00
|
|
|
if (slope != SLOPE_FLAT && slope & InclinedSlope(bridge_dir)) return false;
|
2007-09-26 16:12:43 +00:00
|
|
|
|
2007-11-09 13:21:48 +00:00
|
|
|
/* Assure that the bridge is connectable to the start side */
|
|
|
|
if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
|
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
/* We are in the right direction */
|
2008-01-30 17:31:39 +00:00
|
|
|
uint8 bridge_length = 0; // This value stores the length of the possible bridge
|
2007-09-26 16:12:43 +00:00
|
|
|
TileIndex bridge_tile = tile; // Used to store the other waterside
|
|
|
|
|
2008-01-30 17:31:39 +00:00
|
|
|
const int delta = TileOffsByDiagDir(bridge_dir);
|
2011-09-06 13:32:37 +00:00
|
|
|
|
2020-12-27 00:10:21 +00:00
|
|
|
/* To prevent really small towns from building disproportionately
|
|
|
|
* long bridges, make the max a function of its population. */
|
2021-01-06 13:20:33 +00:00
|
|
|
int base_bridge_length = 5;
|
2020-12-27 00:10:21 +00:00
|
|
|
int max_bridge_length = t->cache.population / 1000 + base_bridge_length;
|
|
|
|
|
2011-09-06 13:32:37 +00:00
|
|
|
if (slope == SLOPE_FLAT) {
|
2019-02-28 17:45:17 +00:00
|
|
|
/* Bridges starting on flat tiles are only allowed when crossing rivers, rails or one-way roads. */
|
2011-09-06 13:32:37 +00:00
|
|
|
do {
|
2020-12-27 00:10:21 +00:00
|
|
|
if (bridge_length++ >= base_bridge_length) {
|
2019-02-28 17:45:17 +00:00
|
|
|
/* Allow to cross rivers, not big lakes, nor large amounts of rails or one-way roads. */
|
2011-09-06 13:32:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bridge_tile += delta;
|
2019-02-28 17:45:17 +00:00
|
|
|
} while (IsValidTile(bridge_tile) && ((IsWaterTile(bridge_tile) && !IsSea(bridge_tile)) || IsPlainRailTile(bridge_tile) || (IsNormalRoadTile(bridge_tile) && GetDisallowedRoadDirections(bridge_tile) != DRD_NONE)));
|
2011-09-06 13:32:37 +00:00
|
|
|
} else {
|
|
|
|
do {
|
2020-12-27 00:10:21 +00:00
|
|
|
if (bridge_length++ >= max_bridge_length) {
|
|
|
|
/* Ensure the bridge is not longer than the max allowed length. */
|
2011-09-06 13:32:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bridge_tile += delta;
|
2019-02-28 17:45:17 +00:00
|
|
|
} while (IsValidTile(bridge_tile) && (IsWaterTile(bridge_tile) || IsPlainRailTile(bridge_tile) || (IsNormalRoadTile(bridge_tile) && GetDisallowedRoadDirections(bridge_tile) != DRD_NONE)));
|
2011-09-06 13:32:37 +00:00
|
|
|
}
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2020-12-24 23:37:13 +00:00
|
|
|
/* Don't allow a bridge where the start and end tiles are adjacent with no span between. */
|
2007-09-26 16:12:43 +00:00
|
|
|
if (bridge_length == 1) return false;
|
|
|
|
|
2020-12-24 23:37:13 +00:00
|
|
|
/* Make sure the road can be continued past the bridge. At this point, bridge_tile holds the end tile of the bridge. */
|
2021-01-08 17:32:44 +00:00
|
|
|
if (!CanRoadContinueIntoNextTile(t, bridge_tile, bridge_dir)) return false;
|
2020-12-24 23:37:13 +00:00
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
for (uint8 times = 0; times <= 22; times++) {
|
|
|
|
byte bridge_type = RandomRange(MAX_BRIDGES - 1);
|
|
|
|
|
|
|
|
/* Can we actually build the bridge? */
|
2019-04-06 06:46:15 +00:00
|
|
|
RoadType rt = GetTownRoadType(t);
|
|
|
|
if (DoCommand(tile, bridge_tile, bridge_type | rt << 8 | TRANSPORT_ROAD << 15, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)), CMD_BUILD_BRIDGE).Succeeded()) {
|
|
|
|
DoCommand(tile, bridge_tile, bridge_type | rt << 8 | TRANSPORT_ROAD << 15, DC_EXEC | CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)), CMD_BUILD_BRIDGE);
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SUCCEED;
|
2007-05-04 16:27:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-12-01 13:12:39 +00:00
|
|
|
/* Quit if it selecting an appropriate bridge type fails a large number of times. */
|
2007-05-04 16:27:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:32:44 +00:00
|
|
|
/**
|
|
|
|
* Grows the town with a tunnel.
|
|
|
|
* First we check if a tunnel is reasonable.
|
|
|
|
* If so we check if we are able to build it.
|
|
|
|
*
|
|
|
|
* @param t The current town
|
|
|
|
* @param tile The current tile
|
|
|
|
* @param tunnel_dir The valid direction in which to grow a tunnel
|
|
|
|
* @return true if a tunnel has been built, else false
|
|
|
|
*/
|
2021-02-05 10:00:36 +00:00
|
|
|
static bool GrowTownWithTunnel(const Town *t, const TileIndex tile, const DiagDirection tunnel_dir)
|
2021-01-08 17:32:44 +00:00
|
|
|
{
|
|
|
|
assert(tunnel_dir < DIAGDIR_END);
|
|
|
|
|
|
|
|
Slope slope = GetTileSlope(tile);
|
|
|
|
|
|
|
|
/* Only consider building a tunnel if the starting tile is sloped properly. */
|
|
|
|
if (slope != InclinedSlope(tunnel_dir)) return false;
|
|
|
|
|
|
|
|
/* Assure that the tunnel is connectable to the start side */
|
|
|
|
if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(tunnel_dir))) & DiagDirToRoadBits(tunnel_dir))) return false;
|
|
|
|
|
|
|
|
const int delta = TileOffsByDiagDir(tunnel_dir);
|
|
|
|
int max_tunnel_length = 0;
|
|
|
|
|
|
|
|
/* There are two conditions for building tunnels: Under a mountain and under an obstruction. */
|
|
|
|
if (CanRoadContinueIntoNextTile(t, tile, tunnel_dir)) {
|
|
|
|
/* Only tunnel under a mountain if the slope is continuous for at least 4 tiles. We want tunneling to be a last resort for large hills. */
|
|
|
|
TileIndex slope_tile = tile;
|
|
|
|
for (uint8 tiles = 0; tiles < 4; tiles++) {
|
2021-01-31 09:04:22 +00:00
|
|
|
if (!IsValidTile(slope_tile)) return false;
|
2021-01-08 17:32:44 +00:00
|
|
|
slope = GetTileSlope(slope_tile);
|
|
|
|
if (slope != InclinedSlope(tunnel_dir) && !IsSteepSlope(slope) && !IsSlopeWithOneCornerRaised(slope)) return false;
|
|
|
|
slope_tile += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* More population means longer tunnels, but make sure we can at least cover the smallest mountain which neccesitates tunneling. */
|
|
|
|
max_tunnel_length = (t->cache.population / 1000) + 7;
|
|
|
|
} else {
|
|
|
|
/* When tunneling under an obstruction, the length limit is 5, enough to tunnel under a four-track railway. */
|
|
|
|
max_tunnel_length = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 tunnel_length = 0;
|
|
|
|
TileIndex tunnel_tile = tile; // Iteratator to store the other end tile of the tunnel.
|
|
|
|
|
|
|
|
/* Find the end tile of the tunnel for length and continuation checks. */
|
|
|
|
do {
|
|
|
|
if (tunnel_length++ >= max_tunnel_length) return false;
|
|
|
|
tunnel_tile += delta;
|
|
|
|
/* The tunnel ends when start and end tiles are the same height. */
|
|
|
|
} while (IsValidTile(tunnel_tile) && GetTileZ(tile) != GetTileZ(tunnel_tile));
|
|
|
|
|
|
|
|
/* Don't allow a tunnel where the start and end tiles are adjacent. */
|
|
|
|
if (tunnel_length == 1) return false;
|
|
|
|
|
|
|
|
/* Make sure the road can be continued past the tunnel. At this point, tunnel_tile holds the end tile of the tunnel. */
|
|
|
|
if (!CanRoadContinueIntoNextTile(t, tunnel_tile, tunnel_dir)) return false;
|
|
|
|
|
|
|
|
/* Attempt to build the tunnel. Return false if it fails to let the town build a road instead. */
|
|
|
|
RoadType rt = GetTownRoadType(t);
|
|
|
|
if (DoCommand(tile, rt | (TRANSPORT_ROAD << 8), 0, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_TUNNEL)), CMD_BUILD_TUNNEL).Succeeded()) {
|
|
|
|
DoCommand(tile, rt | (TRANSPORT_ROAD << 8), 0, DC_EXEC | CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_TUNNEL)), CMD_BUILD_TUNNEL);
|
|
|
|
_grow_town_result = GROWTH_SUCCEED;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
/**
|
|
|
|
* Checks whether at least one surrounding roads allows to build a house here
|
|
|
|
*
|
|
|
|
* @param t the tile where the house will be built
|
|
|
|
* @return true if at least one surrounding roadtype allows building houses here
|
|
|
|
*/
|
|
|
|
static inline bool RoadTypesAllowHouseHere(TileIndex t)
|
|
|
|
{
|
|
|
|
static const TileIndexDiffC tiles[] = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
|
|
|
|
bool allow = false;
|
|
|
|
|
|
|
|
for (const TileIndexDiffC *ptr = tiles; ptr != endof(tiles); ++ptr) {
|
|
|
|
TileIndex cur_tile = t + ToTileIndexDiff(*ptr);
|
|
|
|
if (!IsValidTile(cur_tile)) continue;
|
|
|
|
|
|
|
|
if (!(IsTileType(cur_tile, MP_ROAD) || IsTileType(cur_tile, MP_STATION))) continue;
|
|
|
|
allow = true;
|
|
|
|
|
|
|
|
RoadType road_rt = GetRoadTypeRoad(cur_tile);
|
|
|
|
RoadType tram_rt = GetRoadTypeTram(cur_tile);
|
|
|
|
if (road_rt != INVALID_ROADTYPE && !HasBit(GetRoadTypeInfo(road_rt)->flags, ROTF_NO_HOUSES)) return true;
|
|
|
|
if (tram_rt != INVALID_ROADTYPE && !HasBit(GetRoadTypeInfo(tram_rt)->flags, ROTF_NO_HOUSES)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no road was found surrounding the tile we can allow building the house since there is
|
|
|
|
* nothing which forbids it, if a road was found but the execution reached this point, then
|
|
|
|
* all the found roads don't allow houses to be built */
|
|
|
|
return !allow;
|
|
|
|
}
|
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/**
|
|
|
|
* Grows the given town.
|
|
|
|
* There are at the moment 3 possible way's for
|
|
|
|
* the town expansion:
|
2007-09-26 16:12:43 +00:00
|
|
|
* @li Generate a random tile and check if there is a road allowed
|
|
|
|
* @li TL_ORIGINAL
|
|
|
|
* @li TL_BETTER_ROADS
|
|
|
|
* @li Check if the town geometry allows a road and which one
|
|
|
|
* @li TL_2X2_GRID
|
|
|
|
* @li TL_3X3_GRID
|
|
|
|
* @li Forbid roads, only build houses
|
2007-05-04 16:27:13 +00:00
|
|
|
*
|
2007-09-12 23:56:21 +00:00
|
|
|
* @param tile_ptr The current tile
|
|
|
|
* @param cur_rb The current tiles RoadBits
|
|
|
|
* @param target_dir The target road dir
|
|
|
|
* @param t1 The current town
|
2007-05-04 16:27:13 +00:00
|
|
|
*/
|
2007-09-26 16:12:43 +00:00
|
|
|
static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-09-26 16:12:43 +00:00
|
|
|
RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command
|
|
|
|
TileIndex tile = *tile_ptr; // The main tile on which we base our growth
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-25 21:50:54 +00:00
|
|
|
assert(tile < MapSize());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
if (cur_rb == ROAD_NONE) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Tile has no road. First reset the status counter
|
|
|
|
* to say that this is the last iteration. */
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SEARCH_STOPPED;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
if (!_settings_game.economy.allow_town_roads && !_generating_world) return;
|
2010-07-02 16:34:07 +00:00
|
|
|
if (!_settings_game.economy.allow_town_level_crossings && IsTileType(tile, MP_RAILWAY)) return;
|
2009-02-04 20:17:25 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Remove hills etc */
|
2008-09-26 19:11:15 +00:00
|
|
|
if (!_settings_game.construction.build_on_slopes || Chance16(1, 6)) LevelTownLand(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Is a road allowed here? */
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t1->layout) {
|
2007-05-04 16:27:13 +00:00
|
|
|
default: NOT_REACHED();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
case TL_3X3_GRID:
|
|
|
|
case TL_2X2_GRID:
|
2007-09-12 23:56:21 +00:00
|
|
|
rcmd = GetTownRoadGridElement(t1, tile, target_dir);
|
|
|
|
if (rcmd == ROAD_NONE) return;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_BETTER_ROADS:
|
|
|
|
case TL_ORIGINAL:
|
2008-04-01 16:27:01 +00:00
|
|
|
if (!IsRoadAllowedHere(t1, tile, target_dir)) return;
|
2007-09-12 23:56:21 +00:00
|
|
|
|
|
|
|
DiagDirection source_dir = ReverseDiagDir(target_dir);
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2007-11-25 15:35:25 +00:00
|
|
|
if (Chance16(1, 4)) {
|
2007-09-12 23:56:21 +00:00
|
|
|
/* Randomize a new target dir */
|
|
|
|
do target_dir = RandomDiagDir(); while (target_dir == source_dir);
|
2007-05-04 16:27:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 16:27:01 +00:00
|
|
|
if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) {
|
2007-05-04 16:27:13 +00:00
|
|
|
/* A road is not allowed to continue the randomized road,
|
2007-09-12 23:56:21 +00:00
|
|
|
* return if the road we're trying to build is curved. */
|
|
|
|
if (target_dir != ReverseDiagDir(source_dir)) return;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
|
|
|
/* Return if neither side of the new road is a house */
|
2007-09-26 16:12:43 +00:00
|
|
|
if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) &&
|
|
|
|
!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) {
|
2007-05-04 16:27:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* That means that the road is only allowed if there is a house
|
|
|
|
* at any side of the new road. */
|
|
|
|
}
|
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
} else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Continue building on a partial road.
|
2013-01-08 22:46:42 +00:00
|
|
|
* Should be always OK, so we only generate
|
2007-09-12 23:56:21 +00:00
|
|
|
* the fitting RoadBits */
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SEARCH_STOPPED;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
if (!_settings_game.economy.allow_town_roads && !_generating_world) return;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t1->layout) {
|
|
|
|
default: NOT_REACHED();
|
2007-05-04 16:27:13 +00:00
|
|
|
|
|
|
|
case TL_3X3_GRID:
|
|
|
|
case TL_2X2_GRID:
|
2008-04-18 04:54:09 +00:00
|
|
|
rcmd = GetTownRoadGridElement(t1, tile, target_dir);
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_BETTER_ROADS:
|
|
|
|
case TL_ORIGINAL:
|
2007-09-12 23:56:21 +00:00
|
|
|
rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-02-04 20:17:25 +00:00
|
|
|
bool allow_house = true; // Value which decides if we want to construct a house
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-08-22 12:41:24 +00:00
|
|
|
/* Reached a tunnel/bridge? Then continue at the other side of it, unless
|
|
|
|
* it is the starting tile. Half the time, we stay on this side then.*/
|
2006-12-31 13:24:44 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2011-08-22 12:41:24 +00:00
|
|
|
if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD && (target_dir != DIAGDIR_END || Chance16(1, 2))) {
|
2008-01-04 19:45:29 +00:00
|
|
|
*tile_ptr = GetOtherTunnelBridgeEnd(tile);
|
2006-12-31 13:24:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Possibly extend the road in a direction.
|
|
|
|
* Randomize a direction and if it has a road, bail out. */
|
2007-09-12 23:56:21 +00:00
|
|
|
target_dir = RandomDiagDir();
|
2018-04-28 21:47:36 +00:00
|
|
|
RoadBits target_rb = DiagDirToRoadBits(target_dir);
|
|
|
|
TileIndex house_tile; // 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);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
/* Don't walk into water. */
|
2010-11-21 18:39:12 +00:00
|
|
|
if (HasTileWaterGround(house_tile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-12 09:39:30 +00:00
|
|
|
if (!IsValidTile(house_tile)) return;
|
2009-01-21 02:31:55 +00:00
|
|
|
|
2018-04-28 21:47:36 +00:00
|
|
|
if (target_dir != DIAGDIR_END && (_settings_game.economy.allow_town_roads || _generating_world)) {
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t1->layout) {
|
|
|
|
default: NOT_REACHED();
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case TL_3X3_GRID: // Use 2x2 grid afterwards!
|
2009-02-04 20:17:25 +00:00
|
|
|
GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
case TL_2X2_GRID:
|
2015-10-30 17:17:54 +00:00
|
|
|
rcmd = GetTownRoadGridElement(t1, tile, target_dir);
|
2018-04-28 21:47:36 +00:00
|
|
|
allow_house = (rcmd & target_rb) == ROAD_NONE;
|
2009-02-04 20:17:25 +00:00
|
|
|
break;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case TL_BETTER_ROADS: // Use original afterwards!
|
2009-02-04 20:17:25 +00:00
|
|
|
GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
case TL_ORIGINAL:
|
|
|
|
/* Allow a house at the edge. 60% chance or
|
|
|
|
* always ok if no road allowed. */
|
2018-04-28 21:47:36 +00:00
|
|
|
rcmd = target_rb;
|
2009-02-04 20:17:25 +00:00
|
|
|
allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10));
|
|
|
|
break;
|
|
|
|
}
|
2007-05-04 16:27:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
allow_house &= RoadTypesAllowHouseHere(house_tile);
|
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
if (allow_house) {
|
|
|
|
/* Build a house, but not if there already is a house there. */
|
2007-09-26 16:12:43 +00:00
|
|
|
if (!IsTileType(house_tile, MP_HOUSE)) {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Level the land if possible */
|
2007-11-25 15:35:25 +00:00
|
|
|
if (Chance16(1, 6)) LevelTownLand(house_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* And build a house.
|
|
|
|
* Set result to -1 if we managed to build it. */
|
2007-09-26 16:12:43 +00:00
|
|
|
if (BuildTownHouse(t1, house_tile)) {
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SUCCEED;
|
2007-05-04 16:27:13 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-05 22:39:15 +00:00
|
|
|
_grow_town_result = GROWTH_SEARCH_STOPPED;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Return if a water tile */
|
2010-11-21 18:39:12 +00:00
|
|
|
if (HasTileWaterGround(tile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
/* Make the roads look nicer */
|
|
|
|
rcmd = CleanUpRoadBits(tile, rcmd);
|
|
|
|
if (rcmd == ROAD_NONE) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2021-01-08 17:32:44 +00:00
|
|
|
/* Only use the target direction for bridges and tunnels to ensure they're connected.
|
2007-11-09 11:20:36 +00:00
|
|
|
* The target_dir is as computed previously according to town layout, so
|
|
|
|
* it will match it perfectly. */
|
|
|
|
if (GrowTownWithBridge(t1, tile, target_dir)) return;
|
2021-01-08 17:32:44 +00:00
|
|
|
if (GrowTownWithTunnel(t1, tile, target_dir)) return;
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
GrowTownWithRoad(t1, tile, rcmd);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-25 11:12:47 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a road can be followed or is a dead end, that can not be extended to the next tile.
|
|
|
|
* This only checks trivial but often cases.
|
|
|
|
* @param tile Start tile for road.
|
|
|
|
* @param dir Direction for road to follow or build.
|
|
|
|
* @return true If road is or can be connected in the specified direction.
|
|
|
|
*/
|
|
|
|
static bool CanFollowRoad(TileIndex tile, DiagDirection dir)
|
|
|
|
{
|
|
|
|
TileIndex target_tile = tile + TileOffsByDiagDir(dir);
|
|
|
|
if (!IsValidTile(target_tile)) return false;
|
|
|
|
if (HasTileWaterGround(target_tile)) return false;
|
|
|
|
|
|
|
|
RoadBits target_rb = GetTownRoadBits(target_tile);
|
2015-04-28 17:25:21 +00:00
|
|
|
if (_settings_game.economy.allow_town_roads || _generating_world) {
|
2015-04-25 11:12:47 +00:00
|
|
|
/* Check whether a road connection exists or can be build. */
|
|
|
|
switch (GetTileType(target_tile)) {
|
|
|
|
case MP_ROAD:
|
|
|
|
return target_rb != ROAD_NONE;
|
|
|
|
|
|
|
|
case MP_STATION:
|
2015-06-20 12:55:17 +00:00
|
|
|
return IsDriveThroughStopTile(target_tile);
|
2015-06-20 12:40:04 +00:00
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
2015-06-20 12:55:17 +00:00
|
|
|
return GetTunnelBridgeTransportType(target_tile) == TRANSPORT_ROAD;
|
2015-06-20 12:40:04 +00:00
|
|
|
|
|
|
|
case MP_HOUSE:
|
2015-04-25 11:12:47 +00:00
|
|
|
case MP_INDUSTRY:
|
|
|
|
case MP_OBJECT:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Checked for void and water earlier */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Check whether a road connection already exists,
|
|
|
|
* and it leads somewhere else. */
|
|
|
|
RoadBits back_rb = DiagDirToRoadBits(ReverseDiagDir(dir));
|
2015-04-27 21:40:46 +00:00
|
|
|
return (target_rb & back_rb) != 0 && (target_rb & ~back_rb) != 0;
|
2015-04-25 11:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Returns "growth" if a house was built, or no if the build failed.
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param t town to inquiry
|
|
|
|
* @param tile to inquiry
|
2015-04-25 11:46:10 +00:00
|
|
|
* @return true if town expansion was possible
|
2007-04-04 03:21:14 +00:00
|
|
|
*/
|
2015-04-25 11:46:10 +00:00
|
|
|
static bool GrowTownAtRoad(Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-09-12 23:56:21 +00:00
|
|
|
/* Special case.
|
|
|
|
* @see GrowTownInTile Check the else if
|
|
|
|
*/
|
2007-09-26 16:12:43 +00:00
|
|
|
DiagDirection target_dir = DIAGDIR_END; // The direction in which we want to extend the town
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-25 21:50:54 +00:00
|
|
|
assert(tile < MapSize());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-04 16:27:13 +00:00
|
|
|
/* Number of times to search.
|
|
|
|
* Better roads, 2X2 and 3X3 grid grow quite fast so we give
|
|
|
|
* them a little handicap. */
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t->layout) {
|
2007-05-04 16:27:13 +00:00
|
|
|
case TL_BETTER_ROADS:
|
2012-04-25 20:50:13 +00:00
|
|
|
_grow_town_result = 10 + t->cache.num_houses * 2 / 9;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_3X3_GRID:
|
|
|
|
case TL_2X2_GRID:
|
2012-04-25 20:50:13 +00:00
|
|
|
_grow_town_result = 10 + t->cache.num_houses * 1 / 9;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-04-25 20:50:13 +00:00
|
|
|
_grow_town_result = 10 + t->cache.num_houses * 4 / 9;
|
2007-05-04 16:27:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2007-09-26 16:12:43 +00:00
|
|
|
RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Try to grow the town from this point */
|
2007-09-12 23:56:21 +00:00
|
|
|
GrowTownInTile(&tile, cur_rb, target_dir, t);
|
2015-10-30 16:19:09 +00:00
|
|
|
if (_grow_town_result == GROWTH_SUCCEED) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Exclude the source position from the bitmask
|
|
|
|
* and return if no more road blocks available */
|
2013-11-25 13:13:35 +00:00
|
|
|
if (IsValidDiagDirection(target_dir)) cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir));
|
2015-10-30 16:19:09 +00:00
|
|
|
if (cur_rb == ROAD_NONE) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-08-22 11:47:15 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
/* Only build in the direction away from the tunnel or bridge. */
|
|
|
|
target_dir = ReverseDiagDir(GetTunnelBridgeDirection(tile));
|
|
|
|
} else {
|
|
|
|
/* Select a random bit from the blockmask, walk a step
|
|
|
|
* and continue the search from there. */
|
2015-04-25 11:12:47 +00:00
|
|
|
do {
|
2015-04-25 11:46:10 +00:00
|
|
|
if (cur_rb == ROAD_NONE) return false;
|
2015-04-25 11:12:47 +00:00
|
|
|
RoadBits target_bits;
|
|
|
|
do {
|
|
|
|
target_dir = RandomDiagDir();
|
|
|
|
target_bits = DiagDirToRoadBits(target_dir);
|
|
|
|
} while (!(cur_rb & target_bits));
|
|
|
|
cur_rb &= ~target_bits;
|
|
|
|
} while (!CanFollowRoad(tile, target_dir));
|
2011-08-22 11:47:15 +00:00
|
|
|
}
|
2007-09-26 16:12:43 +00:00
|
|
|
tile = TileAddByDiagDir(tile, target_dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
if (IsTileType(tile, MP_ROAD) && !IsRoadDepot(tile) && HasTileRoadType(tile, RTT_ROAD)) {
|
2005-02-06 20:29:32 +00:00
|
|
|
/* Don't allow building over roads of other cities */
|
2019-04-06 06:46:15 +00:00
|
|
|
if (IsRoadOwner(tile, RTT_ROAD, OWNER_TOWN) && Town::GetByTile(tile) != t) {
|
2015-04-25 12:17:16 +00:00
|
|
|
return false;
|
2019-04-06 06:46:15 +00:00
|
|
|
} else if (IsRoadOwner(tile, RTT_ROAD, OWNER_NONE) && _game_mode == GM_EDITOR) {
|
2005-02-06 20:29:32 +00:00
|
|
|
/* If we are in the SE, and this road-piece has no town owner yet, it just found an
|
2006-09-04 20:40:33 +00:00
|
|
|
* owner :) (happy happy happy road now) */
|
2019-04-06 06:46:15 +00:00
|
|
|
SetRoadOwner(tile, RTT_ROAD, OWNER_TOWN);
|
2006-04-03 14:56:07 +00:00
|
|
|
SetTownIndex(tile, t->index);
|
2005-02-06 20:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-04 13:56:51 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Max number of times is checked. */
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--_grow_town_result >= 0);
|
|
|
|
|
2015-10-30 16:19:09 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 23:56:21 +00:00
|
|
|
/**
|
|
|
|
* Generate a random road block.
|
2007-04-04 03:21:14 +00:00
|
|
|
* The probability of a straight road
|
2007-09-12 23:56:21 +00:00
|
|
|
* is somewhat higher than a curved.
|
|
|
|
*
|
|
|
|
* @return A RoadBits value with 2 bits set
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static RoadBits GenRandomRoadBits()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint32 r = Random();
|
2005-07-20 15:29:28 +00:00
|
|
|
uint a = GB(r, 0, 2);
|
|
|
|
uint b = GB(r, 8, 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (a == b) b ^= 2;
|
2007-09-12 23:56:21 +00:00
|
|
|
return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Grow the town
|
2008-04-04 14:53:06 +00:00
|
|
|
* @param t town to grow
|
2015-04-25 12:17:16 +00:00
|
|
|
* @return true iff something (house, road, bridge, ...) was built
|
2008-04-04 14:53:06 +00:00
|
|
|
*/
|
2006-01-05 12:40:50 +00:00
|
|
|
static bool GrowTown(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _town_coord_mod[] = {
|
|
|
|
{-1, 0},
|
|
|
|
{ 1, 1},
|
|
|
|
{ 1, -1},
|
|
|
|
{-1, -1},
|
|
|
|
{-1, 0},
|
|
|
|
{ 0, 2},
|
|
|
|
{ 2, 0},
|
|
|
|
{ 0, -2},
|
|
|
|
{-1, -1},
|
|
|
|
{-2, 2},
|
|
|
|
{ 2, 2},
|
|
|
|
{ 2, -2},
|
|
|
|
{ 0, 0}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2007-05-04 16:27:13 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Current "company" is a town */
|
2019-04-22 08:10:04 +00:00
|
|
|
Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
TileIndex tile = t->xy; // The tile we are working with ATM
|
2007-09-12 23:56:21 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Find a road that we can base the construction on. */
|
2008-04-04 14:53:06 +00:00
|
|
|
const TileIndexDiffC *ptr;
|
2005-01-06 11:39:00 +00:00
|
|
|
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
|
2007-09-12 23:56:21 +00:00
|
|
|
if (GetTownRoadBits(tile) != ROAD_NONE) {
|
2015-04-25 11:46:10 +00:00
|
|
|
bool success = GrowTownAtRoad(t, tile);
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2015-04-25 11:46:10 +00:00
|
|
|
return success;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* No road available, try to build a random road block by
|
|
|
|
* clearing some land and then building a road there. */
|
2009-09-07 08:36:52 +00:00
|
|
|
if (_settings_game.economy.allow_town_roads || _generating_world) {
|
|
|
|
tile = t->xy;
|
|
|
|
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
|
|
|
|
/* Only work with plain land that not already has a house */
|
2013-10-12 22:07:58 +00:00
|
|
|
if (!IsTileType(tile, MP_HOUSE) && IsTileFlat(tile)) {
|
2010-01-18 22:57:21 +00:00
|
|
|
if (DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Succeeded()) {
|
2019-04-06 06:46:15 +00:00
|
|
|
RoadType rt = GetTownRoadType(t);
|
|
|
|
DoCommand(tile, GenRandomRoadBits() | (rt << 4), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2009-09-07 08:36:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-09-07 08:36:52 +00:00
|
|
|
tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-06 11:39:00 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-18 12:28:21 +00:00
|
|
|
void UpdateTownRadius(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-15 23:07:06 +00:00
|
|
|
static const uint32 _town_squared_town_zone_radius_data[23][5] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ 4, 0, 0, 0, 0}, // 0
|
|
|
|
{ 16, 0, 0, 0, 0},
|
|
|
|
{ 25, 0, 0, 0, 0},
|
|
|
|
{ 36, 0, 0, 0, 0},
|
|
|
|
{ 49, 0, 4, 0, 0},
|
|
|
|
{ 64, 0, 4, 0, 0}, // 20
|
|
|
|
{ 64, 0, 9, 0, 1},
|
|
|
|
{ 64, 0, 9, 0, 4},
|
|
|
|
{ 64, 0, 16, 0, 4},
|
|
|
|
{ 81, 0, 16, 0, 4},
|
|
|
|
{ 81, 0, 16, 0, 4}, // 40
|
|
|
|
{ 81, 0, 25, 0, 9},
|
|
|
|
{ 81, 36, 25, 0, 9},
|
|
|
|
{ 81, 36, 25, 16, 9},
|
|
|
|
{ 81, 49, 0, 25, 9},
|
|
|
|
{ 81, 64, 0, 25, 9}, // 60
|
|
|
|
{ 81, 64, 0, 36, 9},
|
|
|
|
{ 81, 64, 0, 36, 16},
|
2004-11-20 11:57:45 +00:00
|
|
|
{100, 81, 0, 49, 16},
|
|
|
|
{100, 81, 0, 49, 25},
|
|
|
|
{121, 81, 0, 49, 25}, // 80
|
|
|
|
{121, 81, 0, 49, 25},
|
|
|
|
{121, 81, 0, 49, 36}, // 88
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2004-11-20 11:57:45 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
if (t->cache.num_houses < 92) {
|
|
|
|
memcpy(t->cache.squared_town_zone_radius, _town_squared_town_zone_radius_data[t->cache.num_houses / 4], sizeof(t->cache.squared_town_zone_radius));
|
2004-11-20 11:57:45 +00:00
|
|
|
} else {
|
2012-04-25 20:50:13 +00:00
|
|
|
int mass = t->cache.num_houses / 8;
|
2008-04-15 22:27:28 +00:00
|
|
|
/* Actually we are proportional to sqrt() but that's right because we are covering an area.
|
|
|
|
* The offsets are to make sure the radii do not decrease in size when going from the table
|
|
|
|
* to the calculated value.*/
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.squared_town_zone_radius[0] = mass * 15 - 40;
|
|
|
|
t->cache.squared_town_zone_radius[1] = mass * 9 - 15;
|
|
|
|
t->cache.squared_town_zone_radius[2] = 0;
|
|
|
|
t->cache.squared_town_zone_radius[3] = mass * 5 - 5;
|
|
|
|
t->cache.squared_town_zone_radius[4] = mass * 3 + 5;
|
2004-11-20 11:57:45 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-02-17 10:56:19 +00:00
|
|
|
void UpdateTownMaxPass(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-04-25 20:50:13 +00:00
|
|
|
t->supplied[CT_PASSENGERS].old_max = t->cache.population >> 3;
|
|
|
|
t->supplied[CT_MAIL].old_max = t->cache.population >> 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 18:55:48 +00:00
|
|
|
static void UpdateTownGrowthRate(Town *t);
|
|
|
|
static void UpdateTownGrowth(Town *t);
|
|
|
|
|
2007-04-18 14:23:30 +00:00
|
|
|
/**
|
|
|
|
* Does the actual town creation.
|
|
|
|
*
|
|
|
|
* @param t The town
|
|
|
|
* @param tile Where to put it
|
|
|
|
* @param townnameparts The town name
|
|
|
|
* @param size Parameter for size determination
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param city whether to build a city or town
|
|
|
|
* @param layout the (road) layout of the town
|
2009-11-26 22:04:11 +00:00
|
|
|
* @param manual was the town placed manually?
|
2007-04-18 14:23:30 +00:00
|
|
|
*/
|
2009-11-26 22:04:11 +00:00
|
|
|
static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout, bool manual)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
t->xy = tile;
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
t->time_until_rebuild = 10;
|
|
|
|
UpdateTownRadius(t);
|
2009-06-23 09:52:26 +00:00
|
|
|
t->flags = 0;
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.population = 0;
|
2018-05-02 19:01:30 +00:00
|
|
|
/* 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);
|
2019-01-05 13:22:07 +00:00
|
|
|
t->show_zone = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-02-18 16:32:53 +00:00
|
|
|
_town_kdtree.Insert(t->index);
|
|
|
|
|
2011-11-23 16:07:14 +00:00
|
|
|
/* Set the default cargo requirement for town growth */
|
|
|
|
switch (_settings_game.game_creation.landscape) {
|
|
|
|
case LT_ARCTIC:
|
2019-04-10 21:07:06 +00:00
|
|
|
if (FindFirstCargoWithTownEffect(TE_FOOD) != nullptr) t->goal[TE_FOOD] = TOWN_GROWTH_WINTER;
|
2011-11-23 16:07:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LT_TROPIC:
|
2019-04-10 21:07:06 +00:00
|
|
|
if (FindFirstCargoWithTownEffect(TE_FOOD) != nullptr) t->goal[TE_FOOD] = TOWN_GROWTH_DESERT;
|
|
|
|
if (FindFirstCargoWithTownEffect(TE_WATER) != nullptr) t->goal[TE_WATER] = TOWN_GROWTH_DESERT;
|
2011-11-23 16:07:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
t->fund_buildings_months = 0;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
for (uint i = 0; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
t->have_ratings = 0;
|
2008-09-30 20:39:50 +00:00
|
|
|
t->exclusivity = INVALID_COMPANY;
|
2004-08-23 21:04:39 +00:00
|
|
|
t->exclusive_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
t->statues = 0;
|
|
|
|
|
2021-01-12 21:11:12 +00:00
|
|
|
{
|
|
|
|
TownNameParams tnp(_settings_game.game_creation.town_name);
|
|
|
|
t->townnamegrfid = tnp.grfid;
|
|
|
|
t->townnametype = tnp.type;
|
2007-06-18 23:00:55 +00:00
|
|
|
}
|
2005-03-05 13:49:43 +00:00
|
|
|
t->townnameparts = townnameparts;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
t->UpdateVirtCoord();
|
2020-01-05 21:55:57 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_REBUILD);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-04 22:52:34 +00:00
|
|
|
t->InitializeLayout(layout);
|
2008-04-01 16:27:01 +00:00
|
|
|
|
2009-02-05 13:33:37 +00:00
|
|
|
t->larger_town = city;
|
2007-04-18 14:23:30 +00:00
|
|
|
|
2009-02-05 13:33:37 +00:00
|
|
|
int x = (int)size * 16 + 3;
|
2010-03-20 10:55:08 +00:00
|
|
|
if (size == TSZ_RANDOM) x = (Random() & 0xF) + 8;
|
2009-11-26 22:04:11 +00:00
|
|
|
/* Don't create huge cities when founding town in-game */
|
|
|
|
if (city && (!manual || _game_mode == GM_EDITOR)) x *= _settings_game.economy.initial_city_size;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses += x;
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownRadius(t);
|
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
int i = x * 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
GrowTown(t);
|
|
|
|
} while (--i);
|
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses -= x;
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownRadius(t);
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowthRate(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownMaxPass(t);
|
2009-02-16 23:23:33 +00:00
|
|
|
UpdateAirportsNoise();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-29 15:06:44 +00:00
|
|
|
/**
|
|
|
|
* Checks if it's possible to place a town at given tile
|
|
|
|
* @param tile tile to check
|
|
|
|
* @return error value or zero cost
|
|
|
|
*/
|
|
|
|
static CommandCost TownCanBePlacedHere(TileIndex tile)
|
|
|
|
{
|
|
|
|
/* Check if too close to the edge of map */
|
|
|
|
if (DistanceFromEdge(tile) < 12) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP_SUB);
|
2009-03-29 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check distance to all other towns. */
|
|
|
|
if (IsCloseToTown(tile, 20)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_TOWN);
|
2009-03-29 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Can only build on clear flat areas, possibly with trees. */
|
2013-10-12 22:07:58 +00:00
|
|
|
if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || !IsTileFlat(tile)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2009-03-29 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 20:22:56 +00:00
|
|
|
return CommandCost(EXPENSES_OTHER);
|
2009-03-29 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 13:54:54 +00:00
|
|
|
/**
|
|
|
|
* Verifies this custom name is unique. Only custom names are checked.
|
|
|
|
* @param name name to check
|
|
|
|
* @return is this name unique?
|
|
|
|
*/
|
|
|
|
static bool IsUniqueTownName(const char *name)
|
|
|
|
{
|
2019-12-17 21:04:09 +00:00
|
|
|
for (const Town *t : Town::Iterate()) {
|
2020-05-17 21:31:59 +00:00
|
|
|
if (!t->name.empty() && t->name == name) return false;
|
2009-09-22 13:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-24 21:18:11 +00:00
|
|
|
/**
|
|
|
|
* Create a new town.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile coordinates where town is built
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2009-02-05 13:33:37 +00:00
|
|
|
* @param p1 0..1 size of the town (@see TownSize)
|
|
|
|
* 2 true iff it should be a city
|
|
|
|
* 3..5 town road layout (@see TownLayout)
|
2009-09-21 20:22:56 +00:00
|
|
|
* 6 use random location (randomize \c tile )
|
2009-02-16 23:23:33 +00:00
|
|
|
* @param p2 town name parts
|
2010-11-14 16:13:07 +00:00
|
|
|
* @param text Custom name for the town. If empty, the town name parts will be used.
|
2009-09-18 14:23:58 +00:00
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-09-21 18:16:00 +00:00
|
|
|
CommandCost CmdFoundTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-04-17 13:31:41 +00:00
|
|
|
TownSize size = Extract<TownSize, 0, 2>(p1);
|
2009-02-05 13:33:37 +00:00
|
|
|
bool city = HasBit(p1, 2);
|
2010-04-17 13:31:41 +00:00
|
|
|
TownLayout layout = Extract<TownLayout, 3, 3>(p1);
|
2009-02-16 23:23:33 +00:00
|
|
|
TownNameParams par(_settings_game.game_creation.town_name);
|
2009-09-21 20:22:56 +00:00
|
|
|
bool random = HasBit(p1, 6);
|
2009-02-16 23:23:33 +00:00
|
|
|
uint32 townnameparts = p2;
|
2009-02-04 22:52:34 +00:00
|
|
|
|
2010-03-20 10:59:06 +00:00
|
|
|
if (size >= TSZ_END) return CMD_ERROR;
|
|
|
|
if (layout >= NUM_TLS) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-09-19 18:48:05 +00:00
|
|
|
/* Some things are allowed only in the scenario editor and for game scripts. */
|
|
|
|
if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) {
|
2009-11-24 21:18:11 +00:00
|
|
|
if (_settings_game.economy.found_town == TF_FORBIDDEN) return CMD_ERROR;
|
2010-03-20 10:55:08 +00:00
|
|
|
if (size == TSZ_LARGE) return CMD_ERROR;
|
2009-11-24 21:18:11 +00:00
|
|
|
if (random) return CMD_ERROR;
|
|
|
|
if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT && layout != _settings_game.economy.town_layout) {
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
2013-09-19 18:48:05 +00:00
|
|
|
} else if (_current_company == OWNER_DEITY && random) {
|
|
|
|
/* Random parameter is not allowed for Game Scripts. */
|
|
|
|
return CMD_ERROR;
|
2009-11-24 21:18:11 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 13:54:54 +00:00
|
|
|
if (StrEmpty(text)) {
|
|
|
|
/* If supplied name is empty, townnameparts has to generate unique automatic name */
|
|
|
|
if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
|
|
|
} else {
|
|
|
|
/* If name is not empty, it has to be unique custom name */
|
2010-12-05 22:25:21 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return CMD_ERROR;
|
2009-09-22 13:54:54 +00:00
|
|
|
if (!IsUniqueTownName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Allocate town struct */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Town::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_TOWNS);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-21 20:22:56 +00:00
|
|
|
if (!random) {
|
2009-11-24 21:18:11 +00:00
|
|
|
CommandCost ret = TownCanBePlacedHere(tile);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2009-11-24 21:18:11 +00:00
|
|
|
}
|
|
|
|
|
2010-03-20 10:55:08 +00:00
|
|
|
static const byte price_mult[][TSZ_RANDOM + 1] = {{ 15, 25, 40, 25 }, { 20, 35, 55, 35 }};
|
2009-11-24 21:18:11 +00:00
|
|
|
/* multidimensional arrays have to have defined length of non-first dimension */
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(lengthof(price_mult[0]) == 4);
|
2009-11-24 21:18:11 +00:00
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
CommandCost cost(EXPENSES_OTHER, _price[PR_BUILD_TOWN]);
|
2009-11-24 21:18:11 +00:00
|
|
|
byte mult = price_mult[city][size];
|
|
|
|
|
|
|
|
cost.MultiplyCost(mult);
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Create the town */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-12-19 21:42:19 +00:00
|
|
|
if (cost.GetCost() > GetAvailableMoneyForCommand()) {
|
|
|
|
_additional_cash_required = cost.GetCost();
|
|
|
|
return CommandCost(EXPENSES_OTHER);
|
|
|
|
}
|
|
|
|
|
2013-09-21 15:09:00 +00:00
|
|
|
Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
|
2008-11-18 22:43:59 +00:00
|
|
|
UpdateNearestTownForRoadTiles(true);
|
2009-09-22 13:54:54 +00:00
|
|
|
Town *t;
|
2009-09-21 20:22:56 +00:00
|
|
|
if (random) {
|
2009-09-22 13:54:54 +00:00
|
|
|
t = CreateRandomTown(20, townnameparts, size, city, layout);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) {
|
2009-09-21 20:22:56 +00:00
|
|
|
cost = CommandCost(STR_ERROR_NO_SPACE_FOR_TOWN);
|
|
|
|
} else {
|
|
|
|
_new_town_id = t->index;
|
|
|
|
}
|
|
|
|
} else {
|
2009-09-22 13:54:54 +00:00
|
|
|
t = new Town(tile);
|
2009-11-26 22:04:11 +00:00
|
|
|
DoCreateTown(t, tile, townnameparts, size, city, layout, true);
|
2009-09-21 20:22:56 +00:00
|
|
|
}
|
2008-11-18 22:43:59 +00:00
|
|
|
UpdateNearestTownForRoadTiles(false);
|
2013-09-21 15:09:00 +00:00
|
|
|
old_generating_world.Restore();
|
2009-11-24 21:18:11 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t != nullptr && !StrEmpty(text)) {
|
2021-05-09 20:51:26 +00:00
|
|
|
t->name = text;
|
2009-09-22 18:55:15 +00:00
|
|
|
t->UpdateVirtCoord();
|
|
|
|
}
|
2009-11-24 21:18:11 +00:00
|
|
|
|
|
|
|
if (_game_mode != GM_EDITOR) {
|
2019-04-10 21:07:06 +00:00
|
|
|
/* 't' can't be nullptr since 'random' is false outside scenedit */
|
2009-11-24 21:18:11 +00:00
|
|
|
assert(!random);
|
|
|
|
|
2018-09-16 03:17:10 +00:00
|
|
|
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));
|
|
|
|
|
|
|
|
char *cn = stredup(company_name);
|
|
|
|
SetDParamStr(0, cn);
|
|
|
|
SetDParam(1, t->index);
|
2009-11-24 21:18:11 +00:00
|
|
|
|
2018-09-16 03:17:10 +00:00
|
|
|
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, cn);
|
|
|
|
}
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventTownFounded(t->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventTownFounded(t->index));
|
2009-11-24 21:18:11 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-09-21 20:22:56 +00:00
|
|
|
return cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 22:12:59 +00:00
|
|
|
/**
|
|
|
|
* Towns must all be placed on the same grid or when they eventually
|
|
|
|
* interpenetrate their road networks will not mesh nicely; this
|
|
|
|
* function adjusts a tile so that it aligns properly.
|
|
|
|
*
|
|
|
|
* @param tile the tile to start at
|
|
|
|
* @param layout which town layout algo is in effect
|
|
|
|
* @return the adjusted tile
|
|
|
|
*/
|
|
|
|
static TileIndex AlignTileToGrid(TileIndex tile, TownLayout layout)
|
|
|
|
{
|
|
|
|
switch (layout) {
|
|
|
|
case TL_2X2_GRID: return TileXY(TileX(tile) - TileX(tile) % 3, TileY(tile) - TileY(tile) % 3);
|
|
|
|
case TL_3X3_GRID: return TileXY(TileX(tile) & ~3, TileY(tile) & ~3);
|
|
|
|
default: return tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Towns must all be placed on the same grid or when they eventually
|
|
|
|
* interpenetrate their road networks will not mesh nicely; this
|
|
|
|
* function tells you if a tile is properly aligned.
|
|
|
|
*
|
|
|
|
* @param tile the tile to start at
|
|
|
|
* @param layout which town layout algo is in effect
|
|
|
|
* @return true if the tile is in the correct location
|
|
|
|
*/
|
|
|
|
static bool IsTileAlignedToGrid(TileIndex tile, TownLayout layout)
|
|
|
|
{
|
|
|
|
switch (layout) {
|
|
|
|
case TL_2X2_GRID: return TileX(tile) % 3 == 0 && TileY(tile) % 3 == 0;
|
|
|
|
case TL_3X3_GRID: return TileX(tile) % 4 == 0 && TileY(tile) % 4 == 0;
|
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used as the user_data for FindFurthestFromWater
|
|
|
|
*/
|
|
|
|
struct SpotData {
|
|
|
|
TileIndex tile; ///< holds the tile that was found
|
2009-07-16 10:01:32 +00:00
|
|
|
uint max_dist; ///< holds the distance that tile is from the water
|
2009-04-25 22:12:59 +00:00
|
|
|
TownLayout layout; ///< tells us what kind of town we're building
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CircularTileSearch callback; finds the tile furthest from any
|
2010-04-12 14:12:47 +00:00
|
|
|
* water. slightly bit tricky, since it has to do a search of its own
|
2009-04-25 22:12:59 +00:00
|
|
|
* in order to find the distance to the water from each square in the
|
|
|
|
* radius.
|
|
|
|
*
|
|
|
|
* Also, this never returns true, because it needs to take into
|
|
|
|
* account all locations being searched before it knows which is the
|
|
|
|
* furthest.
|
|
|
|
*
|
|
|
|
* @param tile Start looking from this tile
|
|
|
|
* @param user_data Storage area for data that must last across calls;
|
|
|
|
* must be a pointer to struct SpotData
|
|
|
|
*
|
|
|
|
* @return always false
|
|
|
|
*/
|
|
|
|
static bool FindFurthestFromWater(TileIndex tile, void *user_data)
|
|
|
|
{
|
|
|
|
SpotData *sp = (SpotData*)user_data;
|
|
|
|
uint dist = GetClosestWaterDistance(tile, true);
|
|
|
|
|
|
|
|
if (IsTileType(tile, MP_CLEAR) &&
|
2013-10-12 22:07:58 +00:00
|
|
|
IsTileFlat(tile) &&
|
2009-04-25 22:12:59 +00:00
|
|
|
IsTileAlignedToGrid(tile, sp->layout) &&
|
|
|
|
dist > sp->max_dist) {
|
|
|
|
sp->tile = tile;
|
|
|
|
sp->max_dist = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CircularTileSearch callback; finds the nearest land tile
|
|
|
|
*
|
|
|
|
* @param tile Start looking from this tile
|
|
|
|
* @param user_data not used
|
|
|
|
*/
|
|
|
|
static bool FindNearestEmptyLand(TileIndex tile, void *user_data)
|
|
|
|
{
|
|
|
|
return IsTileType(tile, MP_CLEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a spot on the map (presumed to be a water tile), find a good
|
|
|
|
* coastal spot to build a city. We don't want to build too close to
|
|
|
|
* the edge if we can help it (since that retards city growth) hence
|
|
|
|
* the search within a search within a search. O(n*m^2), where n is
|
|
|
|
* how far to search for land, and m is how far inland to look for a
|
|
|
|
* flat spot.
|
|
|
|
*
|
|
|
|
* @param tile Start looking from this spot.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param layout the road layout to search for
|
2009-04-25 22:12:59 +00:00
|
|
|
* @return tile that was found
|
|
|
|
*/
|
|
|
|
static TileIndex FindNearestGoodCoastalTownSpot(TileIndex tile, TownLayout layout)
|
|
|
|
{
|
|
|
|
SpotData sp = { INVALID_TILE, 0, layout };
|
|
|
|
|
|
|
|
TileIndex coast = tile;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (CircularTileSearch(&coast, 40, FindNearestEmptyLand, nullptr)) {
|
2009-04-25 22:12:59 +00:00
|
|
|
CircularTileSearch(&coast, 10, FindFurthestFromWater, &sp);
|
|
|
|
return sp.tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we get here just give up */
|
|
|
|
return INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
2009-09-21 20:22:56 +00:00
|
|
|
static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-01-17 18:46:09 +00:00
|
|
|
assert(_game_mode == GM_EDITOR || _generating_world); // These are the preconditions for CMD_DELETE_TOWN
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!Town::CanAllocateItem()) return nullptr;
|
2009-01-09 14:59:02 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Generate a tile index not too close from the edge */
|
2009-04-25 22:12:59 +00:00
|
|
|
TileIndex tile = AlignTileToGrid(RandomTile(), layout);
|
|
|
|
|
|
|
|
/* if we tried to place the town on water, slide it over onto
|
|
|
|
* the nearest likely-looking spot */
|
|
|
|
if (IsTileType(tile, MP_WATER)) {
|
|
|
|
tile = FindNearestGoodCoastalTownSpot(tile, layout);
|
|
|
|
if (tile == INVALID_TILE) continue;
|
2009-01-04 10:50:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-29 15:06:44 +00:00
|
|
|
/* Make sure town can be placed here */
|
2010-01-18 22:57:21 +00:00
|
|
|
if (TownCanBePlacedHere(tile).Failed()) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Allocate a town struct */
|
2008-04-04 14:53:06 +00:00
|
|
|
Town *t = new Town(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-26 22:04:11 +00:00
|
|
|
DoCreateTown(t, tile, townnameparts, size, city, layout, false);
|
2009-04-25 22:12:59 +00:00
|
|
|
|
|
|
|
/* if the population is still 0 at the point, then the
|
|
|
|
* placement is so bad it couldn't grow at all */
|
2012-04-25 20:50:13 +00:00
|
|
|
if (t->cache.population > 0) return t;
|
2013-04-03 20:24:10 +00:00
|
|
|
|
2019-04-22 08:10:04 +00:00
|
|
|
Backup<CompanyID> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
|
2012-01-17 18:46:09 +00:00
|
|
|
CommandCost rc = DoCommand(t->xy, t->index, 0, DC_EXEC, CMD_DELETE_TOWN);
|
2021-04-01 09:16:19 +00:00
|
|
|
(void)rc; // assert only
|
2013-04-03 20:24:10 +00:00
|
|
|
cur_company.Restore();
|
2012-01-17 18:46:09 +00:00
|
|
|
assert(rc.Succeeded());
|
2011-02-12 18:41:24 +00:00
|
|
|
|
|
|
|
/* We already know that we can allocate a single town when
|
|
|
|
* entering this function. However, we create and delete
|
|
|
|
* a town which "resets" the allocation checks. As such we
|
|
|
|
* need to check again when assertions are enabled. */
|
|
|
|
assert(Town::CanAllocateItem());
|
2008-04-04 14:53:06 +00:00
|
|
|
} while (--attempts != 0);
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-24 02:54:47 +00:00
|
|
|
static const byte _num_initial_towns[4] = {5, 11, 23, 46}; // very low, low, normal, high
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* This function will generate a certain amount of towns, with a certain layout
|
2009-07-29 03:02:13 +00:00
|
|
|
* It can be called from the scenario editor (i.e.: generate Random Towns)
|
|
|
|
* as well as from world creation.
|
|
|
|
* @param layout which towns will be set to, when created
|
|
|
|
* @return true if towns have been successfully created
|
|
|
|
*/
|
2009-02-04 22:52:34 +00:00
|
|
|
bool GenerateTowns(TownLayout layout)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-09-01 18:52:27 +00:00
|
|
|
uint current_number = 0;
|
2010-09-01 19:00:32 +00:00
|
|
|
uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.number_towns : 0;
|
2010-09-01 18:52:27 +00:00
|
|
|
uint total = (difficulty == (uint)CUSTOM_TOWN_NUMBER_DIFFICULTY) ? _settings_game.game_creation.custom_town_number : ScaleByMapSize(_num_initial_towns[difficulty] + (Random() & 7));
|
2021-01-08 10:16:18 +00:00
|
|
|
total = std::min<uint>(TownPool::MAX_SIZE, total);
|
2009-09-21 20:22:56 +00:00
|
|
|
uint32 townnameparts;
|
2014-02-06 21:06:59 +00:00
|
|
|
TownNames town_names;
|
2005-01-28 15:31:04 +00:00
|
|
|
|
2010-09-01 18:52:27 +00:00
|
|
|
SetGeneratingWorldProgress(GWP_TOWN, total);
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
|
2009-07-29 03:02:13 +00:00
|
|
|
/* First attempt will be made at creating the suggested number of towns.
|
2009-07-29 12:45:15 +00:00
|
|
|
* Note that this is really a suggested value, not a required one.
|
|
|
|
* We would not like the system to lock up just because the user wanted 100 cities on a 64*64 map, would we? */
|
2005-02-13 09:42:49 +00:00
|
|
|
do {
|
2009-03-16 18:38:28 +00:00
|
|
|
bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns));
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
IncreaseGeneratingWorldProgress(GWP_TOWN);
|
2009-09-21 20:22:56 +00:00
|
|
|
/* Get a unique name for the town. */
|
2014-02-06 21:06:59 +00:00
|
|
|
if (!GenerateTownName(&townnameparts, &town_names)) continue;
|
2007-04-04 03:21:14 +00:00
|
|
|
/* try 20 times to create a random-sized town for the first loop. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout) != nullptr) current_number++; // If creation was successful, raise a flag.
|
2010-09-01 18:52:27 +00:00
|
|
|
} while (--total);
|
2005-02-13 09:42:49 +00:00
|
|
|
|
2014-02-06 21:06:59 +00:00
|
|
|
town_names.clear();
|
|
|
|
|
2019-02-18 16:32:53 +00:00
|
|
|
/* Build the town k-d tree again to make sure it's well balanced */
|
|
|
|
RebuildTownKdtree();
|
|
|
|
|
2010-09-01 18:52:27 +00:00
|
|
|
if (current_number != 0) return true;
|
2009-09-21 20:22:56 +00:00
|
|
|
|
2010-09-01 18:52:27 +00:00
|
|
|
/* If current_number is still zero at this point, it means that not a single town has been created.
|
2009-07-29 12:45:15 +00:00
|
|
|
* So give it a last try, but now more aggressive */
|
2009-09-21 20:22:56 +00:00
|
|
|
if (GenerateTownName(&townnameparts) &&
|
2019-04-10 21:07:06 +00:00
|
|
|
CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout) != nullptr) {
|
2009-09-21 20:22:56 +00:00
|
|
|
return true;
|
2005-02-13 09:42:49 +00:00
|
|
|
}
|
2009-09-21 20:22:56 +00:00
|
|
|
|
|
|
|
/* If there are no towns at all and we are generating new game, bail out */
|
|
|
|
if (Town::GetNumItems() == 0 && _game_mode != GM_EDITOR) {
|
2011-12-10 15:16:58 +00:00
|
|
|
ShowErrorMessage(STR_ERROR_COULD_NOT_CREATE_TOWN, INVALID_STRING_ID, WL_CRITICAL);
|
2009-09-21 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false; // we are still without a town? we failed, simply
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the bit corresponding to the town zone of the specified tile
|
2008-04-15 22:27:28 +00:00
|
|
|
* @param t Town on which town zone is to be found
|
|
|
|
* @param tile TileIndex where town zone needs to be found
|
2008-01-16 02:53:55 +00:00
|
|
|
* @return the bit position of the given zone, as defined in HouseZones
|
|
|
|
*/
|
2008-04-04 14:53:06 +00:00
|
|
|
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
uint dist = DistanceSquare(tile, t->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-16 02:53:55 +00:00
|
|
|
if (t->fund_buildings_months && dist <= 25) return HZB_TOWN_CENTRE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
HouseZonesBits smallest = HZB_TOWN_EDGE;
|
|
|
|
for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) {
|
2012-04-25 20:50:13 +00:00
|
|
|
if (dist < t->cache.squared_town_zone_radius[i]) smallest = i;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return smallest;
|
|
|
|
}
|
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/**
|
|
|
|
* Clears tile and builds a house or house part.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param tile tile index
|
|
|
|
* @param t The town to clear the house for
|
2008-02-04 22:15:48 +00:00
|
|
|
* @param counter of construction step
|
|
|
|
* @param stage of construction (used for drawing)
|
|
|
|
* @param type of house. Index into house specs array
|
|
|
|
* @param random_bits required for newgrf houses
|
|
|
|
* @pre house can be built here
|
|
|
|
*/
|
2008-06-14 19:13:23 +00:00
|
|
|
static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-06-01 15:08:14 +00:00
|
|
|
CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
|
2021-04-01 09:16:19 +00:00
|
|
|
(void)cc; // assert only
|
2010-01-18 22:57:21 +00:00
|
|
|
assert(cc.Succeeded());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-14 19:13:23 +00:00
|
|
|
IncreaseBuildingCount(t, type);
|
|
|
|
MakeHouseTile(tile, t->index, counter, stage, type, random_bits);
|
2009-06-26 14:02:27 +00:00
|
|
|
if (HouseSpec::Get(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile);
|
2008-04-20 08:43:31 +00:00
|
|
|
|
|
|
|
MarkTileDirtyByTile(tile);
|
2008-02-04 22:15:48 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-16 17:10:14 +00:00
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/**
|
|
|
|
* Write house information into the map. For houses > 1 tile, all tiles are marked.
|
|
|
|
* @param t tile index
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param town The town related to this house
|
2008-02-04 22:15:48 +00:00
|
|
|
* @param counter of construction step
|
|
|
|
* @param stage of construction (used for drawing)
|
|
|
|
* @param type of house. Index into house specs array
|
|
|
|
* @param random_bits required for newgrf houses
|
|
|
|
* @pre house can be built here
|
|
|
|
*/
|
2008-06-14 19:13:23 +00:00
|
|
|
static void MakeTownHouse(TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits)
|
2008-02-04 22:15:48 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
BuildingFlags size = HouseSpec::Get(type)->building_flags;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-14 19:13:23 +00:00
|
|
|
ClearMakeHouseTile(t, town, counter, stage, type, random_bits);
|
|
|
|
if (size & BUILDING_2_TILES_Y) ClearMakeHouseTile(t + TileDiffXY(0, 1), town, counter, stage, ++type, random_bits);
|
|
|
|
if (size & BUILDING_2_TILES_X) ClearMakeHouseTile(t + TileDiffXY(1, 0), town, counter, stage, ++type, random_bits);
|
|
|
|
if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), town, counter, stage, ++type, random_bits);
|
2019-02-14 21:07:15 +00:00
|
|
|
|
2020-05-11 22:36:28 +00:00
|
|
|
if (!_generating_world) {
|
2020-05-11 23:21:14 +00:00
|
|
|
ForAllStationsAroundTiles(TileArea(t, (size & BUILDING_2_TILES_X) ? 2 : 1, (size & BUILDING_2_TILES_Y) ? 2 : 1), [town](Station *st, TileIndex tile) {
|
2020-05-11 22:36:28 +00:00
|
|
|
town->stations_near.insert(st);
|
2020-05-11 23:21:14 +00:00
|
|
|
return true;
|
2020-05-11 22:36:28 +00:00
|
|
|
});
|
|
|
|
}
|
2008-02-04 22:15:48 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/**
|
|
|
|
* Checks if a house can be built here. Important is slope, bridge above
|
|
|
|
* and ability to clear the land.
|
|
|
|
* @param tile tile to check
|
|
|
|
* @param noslope are slopes (foundations) allowed?
|
|
|
|
* @return true iff house can be built here
|
|
|
|
*/
|
2017-01-22 13:13:15 +00:00
|
|
|
static inline bool CanBuildHouseHere(TileIndex tile, bool noslope)
|
2008-02-04 22:15:48 +00:00
|
|
|
{
|
|
|
|
/* cannot build on these slopes... */
|
2011-11-04 10:22:27 +00:00
|
|
|
Slope slope = GetTileSlope(tile);
|
2008-02-04 22:15:48 +00:00
|
|
|
if ((noslope && slope != SLOPE_FLAT) || IsSteepSlope(slope)) return false;
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
/* at least one RoadTypes allow building the house here? */
|
|
|
|
if (!RoadTypesAllowHouseHere(tile)) return false;
|
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/* building under a bridge? */
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(tile)) return false;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
|
|
|
/* can we clear the land? */
|
2010-01-18 22:57:21 +00:00
|
|
|
return DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Succeeded();
|
2008-02-04 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a house can be built at this tile, must have the same max z as parameter.
|
|
|
|
* @param tile tile to check
|
|
|
|
* @param z max z of this tile so more parts of a house are at the same height (with foundation)
|
|
|
|
* @param noslope are slopes (foundations) allowed?
|
|
|
|
* @return true iff house can be built here
|
|
|
|
* @see CanBuildHouseHere()
|
|
|
|
*/
|
2017-01-22 13:13:15 +00:00
|
|
|
static inline bool CheckBuildHouseSameZ(TileIndex tile, int z, bool noslope)
|
2008-02-04 22:15:48 +00:00
|
|
|
{
|
2017-01-22 13:13:15 +00:00
|
|
|
if (!CanBuildHouseHere(tile, noslope)) return false;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
|
|
|
/* if building on slopes is allowed, there will be flattening foundation (to tile max z) */
|
2011-11-04 10:31:13 +00:00
|
|
|
if (GetTileMaxZ(tile) != z) return false;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a house of size 2x2 can be built at this tile
|
|
|
|
* @param tile tile, N corner
|
|
|
|
* @param z maximum tile z so all tile have the same max z
|
|
|
|
* @param noslope are slopes (foundations) allowed?
|
|
|
|
* @return true iff house can be built
|
|
|
|
* @see CheckBuildHouseSameZ()
|
|
|
|
*/
|
2017-01-22 13:13:15 +00:00
|
|
|
static bool CheckFree2x2Area(TileIndex tile, int z, bool noslope)
|
2008-02-04 22:15:48 +00:00
|
|
|
{
|
|
|
|
/* we need to check this tile too because we can be at different tile now */
|
2017-01-22 13:13:15 +00:00
|
|
|
if (!CheckBuildHouseSameZ(tile, z, noslope)) return false;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
|
|
|
for (DiagDirection d = DIAGDIR_SE; d < DIAGDIR_END; d++) {
|
|
|
|
tile += TileOffsByDiagDir(d);
|
2017-01-22 13:13:15 +00:00
|
|
|
if (!CheckBuildHouseSameZ(tile, z, noslope)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
|
2008-02-16 17:10:14 +00:00
|
|
|
/**
|
|
|
|
* Checks if current town layout allows building here
|
|
|
|
* @param t town
|
|
|
|
* @param tile tile to check
|
|
|
|
* @return true iff town layout allows building here
|
|
|
|
* @note see layouts
|
|
|
|
*/
|
|
|
|
static inline bool TownLayoutAllowsHouseHere(Town *t, TileIndex tile)
|
|
|
|
{
|
2009-03-03 22:46:08 +00:00
|
|
|
/* Allow towns everywhere when we don't build roads */
|
|
|
|
if (!_settings_game.economy.allow_town_roads && !_generating_world) return true;
|
|
|
|
|
2008-02-16 17:10:14 +00:00
|
|
|
TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
|
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t->layout) {
|
2008-02-16 17:10:14 +00:00
|
|
|
case TL_2X2_GRID:
|
|
|
|
if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_3X3_GRID:
|
|
|
|
if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if current town layout allows 2x2 building here
|
|
|
|
* @param t town
|
|
|
|
* @param tile tile to check
|
|
|
|
* @return true iff town layout allows 2x2 building here
|
|
|
|
* @note see layouts
|
|
|
|
*/
|
|
|
|
static inline bool TownLayoutAllows2x2HouseHere(Town *t, TileIndex tile)
|
|
|
|
{
|
2009-03-03 22:46:08 +00:00
|
|
|
/* Allow towns everywhere when we don't build roads */
|
|
|
|
if (!_settings_game.economy.allow_town_roads && !_generating_world) return true;
|
|
|
|
|
2010-09-12 19:40:47 +00:00
|
|
|
/* Compute relative position of tile. (Positive offsets are towards north) */
|
|
|
|
TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
|
2008-02-16 17:10:14 +00:00
|
|
|
|
2009-02-04 20:17:25 +00:00
|
|
|
switch (t->layout) {
|
2008-02-16 17:10:14 +00:00
|
|
|
case TL_2X2_GRID:
|
2010-09-12 19:40:47 +00:00
|
|
|
grid_pos.x %= 3;
|
|
|
|
grid_pos.y %= 3;
|
|
|
|
if ((grid_pos.x != 2 && grid_pos.x != -1) ||
|
|
|
|
(grid_pos.y != 2 && grid_pos.y != -1)) return false;
|
2008-02-16 17:10:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TL_3X3_GRID:
|
2010-09-12 19:40:47 +00:00
|
|
|
if ((grid_pos.x & 3) < 2 || (grid_pos.y & 3) < 2) return false;
|
2008-02-16 17:10:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout
|
|
|
|
* Also, tests both building positions that occupy this tile
|
|
|
|
* @param tile tile where the building should be built
|
|
|
|
* @param t town
|
|
|
|
* @param maxz all tiles should have the same height
|
|
|
|
* @param noslope are slopes forbidden?
|
|
|
|
* @param second diagdir from first tile to second tile
|
2010-08-01 19:44:49 +00:00
|
|
|
*/
|
2011-11-04 11:52:19 +00:00
|
|
|
static bool CheckTownBuild2House(TileIndex *tile, Town *t, int maxz, bool noslope, DiagDirection second)
|
2008-02-16 17:10:14 +00:00
|
|
|
{
|
|
|
|
/* 'tile' is already checked in BuildTownHouse() - CanBuildHouseHere() and slope test */
|
|
|
|
|
|
|
|
TileIndex tile2 = *tile + TileOffsByDiagDir(second);
|
2017-01-22 13:13:15 +00:00
|
|
|
if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, maxz, noslope)) return true;
|
2008-02-16 17:10:14 +00:00
|
|
|
|
|
|
|
tile2 = *tile + TileOffsByDiagDir(ReverseDiagDir(second));
|
2017-01-22 13:13:15 +00:00
|
|
|
if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, maxz, noslope)) {
|
2008-02-16 17:10:14 +00:00
|
|
|
*tile = tile2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if 2x2 building is allowed here, also takes into account current town layout
|
|
|
|
* Also, tests all four building positions that occupy this tile
|
|
|
|
* @param tile tile where the building should be built
|
|
|
|
* @param t town
|
|
|
|
* @param maxz all tiles should have the same height
|
|
|
|
* @param noslope are slopes forbidden?
|
2010-08-01 19:44:49 +00:00
|
|
|
*/
|
2011-11-04 11:52:19 +00:00
|
|
|
static bool CheckTownBuild2x2House(TileIndex *tile, Town *t, int maxz, bool noslope)
|
2008-02-16 17:10:14 +00:00
|
|
|
{
|
|
|
|
TileIndex tile2 = *tile;
|
|
|
|
|
2011-05-14 18:35:40 +00:00
|
|
|
for (DiagDirection d = DIAGDIR_SE;; d++) { // 'd' goes through DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_END
|
2017-01-22 13:13:15 +00:00
|
|
|
if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, maxz, noslope)) {
|
2008-02-16 17:10:14 +00:00
|
|
|
*tile = tile2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (d == DIAGDIR_END) break;
|
|
|
|
tile2 += TileOffsByDiagDir(ReverseDiagDir(d)); // go clockwise
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/**
|
|
|
|
* Tries to build a house at this tile
|
|
|
|
* @param t town the house will belong to
|
|
|
|
* @param tile where the house will be built
|
|
|
|
* @return false iff no house can be built at this tile
|
|
|
|
*/
|
|
|
|
static bool BuildTownHouse(Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-16 17:10:14 +00:00
|
|
|
/* forbidden building here by town layout */
|
|
|
|
if (!TownLayoutAllowsHouseHere(t, tile)) return false;
|
|
|
|
|
2008-02-04 22:15:48 +00:00
|
|
|
/* no house allowed at all, bail out */
|
2017-01-22 13:13:15 +00:00
|
|
|
if (!CanBuildHouseHere(tile, false)) return false;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
2012-03-25 18:19:21 +00:00
|
|
|
Slope slope = GetTileSlope(tile);
|
|
|
|
int maxz = GetTileMaxZ(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-16 02:53:55 +00:00
|
|
|
/* Get the town zone type of the current tile, as well as the climate.
|
|
|
|
* This will allow to easily compare with the specs of the new house to build */
|
2008-02-05 00:30:58 +00:00
|
|
|
HouseZonesBits rad = GetTownRadiusGroup(t, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* Above snow? */
|
2008-05-29 15:13:28 +00:00
|
|
|
int land = _settings_game.game_creation.landscape;
|
2012-03-25 18:19:21 +00:00
|
|
|
if (land == LT_ARCTIC && maxz > HighestSnowLine()) land = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
uint bitmask = (1 << rad) + (1 << (land + 12));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* bits 0-4 are used
|
|
|
|
* bits 11-15 are used
|
|
|
|
* bits 5-10 are not used. */
|
2013-10-12 16:33:19 +00:00
|
|
|
HouseID houses[NUM_HOUSES];
|
2008-02-05 00:30:58 +00:00
|
|
|
uint num = 0;
|
2013-10-12 16:33:19 +00:00
|
|
|
uint probs[NUM_HOUSES];
|
2008-02-05 00:30:58 +00:00
|
|
|
uint probability_max = 0;
|
|
|
|
|
|
|
|
/* Generate a list of all possible houses that can be built. */
|
2013-10-12 16:33:19 +00:00
|
|
|
for (uint i = 0; i < NUM_HOUSES; i++) {
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(i);
|
2009-03-23 14:09:05 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* Verify that the candidate house spec matches the current tile status */
|
2010-08-07 21:03:38 +00:00
|
|
|
if ((~hs->building_availability & bitmask) != 0 || !hs->enabled || hs->grf_prop.override != INVALID_HOUSE_ID) continue;
|
2009-03-23 14:09:05 +00:00
|
|
|
|
|
|
|
/* Don't let these counters overflow. Global counters are 32bit, there will never be that many houses. */
|
|
|
|
if (hs->class_id != HOUSE_NO_CLASS) {
|
|
|
|
/* id_count is always <= class_count, so it doesn't need to be checked */
|
2012-04-25 20:50:13 +00:00
|
|
|
if (t->cache.building_counts.class_count[hs->class_id] == UINT16_MAX) continue;
|
2009-03-23 14:09:05 +00:00
|
|
|
} else {
|
|
|
|
/* If the house has no class, check id_count instead */
|
2012-04-25 20:50:13 +00:00
|
|
|
if (t->cache.building_counts.id_count[i] == UINT16_MAX) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-03-23 14:09:05 +00:00
|
|
|
|
2020-05-29 20:31:14 +00:00
|
|
|
uint cur_prob = hs->probability;
|
2009-03-23 14:09:05 +00:00
|
|
|
probability_max += cur_prob;
|
|
|
|
probs[num] = cur_prob;
|
|
|
|
houses[num++] = (HouseID)i;
|
2008-02-05 00:30:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-07-30 12:17:46 +00:00
|
|
|
TileIndex baseTile = tile;
|
2008-02-04 22:15:48 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
while (probability_max > 0) {
|
2011-07-30 12:17:46 +00:00
|
|
|
/* Building a multitile building can change the location of tile.
|
|
|
|
* The building would still be built partially on that tile, but
|
2013-01-08 22:46:42 +00:00
|
|
|
* its northern tile would be elsewhere. However, if the callback
|
2011-07-30 12:17:46 +00:00
|
|
|
* fails we would be basing further work from the changed tile.
|
|
|
|
* So a next 1x1 tile building could be built on the wrong tile. */
|
|
|
|
tile = baseTile;
|
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
uint r = RandomRange(probability_max);
|
|
|
|
uint i;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (probs[i] > r) break;
|
|
|
|
r -= probs[i];
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
HouseID house = houses[i];
|
|
|
|
probability_max -= probs[i];
|
2008-02-04 23:24:22 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* remove tested house from the set */
|
|
|
|
num--;
|
|
|
|
houses[i] = houses[num];
|
|
|
|
probs[i] = probs[num];
|
2008-02-04 23:24:22 +00:00
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2020-05-29 20:31:14 +00:00
|
|
|
if (!_generating_world && _game_mode != GM_EDITOR && (hs->extra_flags & BUILDING_IS_HISTORICAL) != 0) {
|
2009-12-20 14:53:32 +00:00
|
|
|
continue;
|
2008-02-05 00:30:58 +00:00
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-06-03 03:06:16 +00:00
|
|
|
if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* Special houses that there can be only one of. */
|
|
|
|
uint oneof = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
if (hs->building_flags & BUILDING_IS_CHURCH) {
|
|
|
|
SetBit(oneof, TOWN_HAS_CHURCH);
|
|
|
|
} else if (hs->building_flags & BUILDING_IS_STADIUM) {
|
|
|
|
SetBit(oneof, TOWN_HAS_STADIUM);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-23 09:52:26 +00:00
|
|
|
if (t->flags & oneof) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* Make sure there is no slope? */
|
|
|
|
bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0;
|
|
|
|
if (noslope && slope != SLOPE_FLAT) continue;
|
|
|
|
|
|
|
|
if (hs->building_flags & TILE_SIZE_2x2) {
|
2008-02-16 17:10:14 +00:00
|
|
|
if (!CheckTownBuild2x2House(&tile, t, maxz, noslope)) continue;
|
2008-02-05 00:30:58 +00:00
|
|
|
} else if (hs->building_flags & TILE_SIZE_2x1) {
|
2008-02-16 17:10:14 +00:00
|
|
|
if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SW)) continue;
|
2008-02-05 00:30:58 +00:00
|
|
|
} else if (hs->building_flags & TILE_SIZE_1x2) {
|
2008-02-16 17:10:14 +00:00
|
|
|
if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue;
|
|
|
|
} else {
|
|
|
|
/* 1x1 house checks are already done */
|
2008-02-05 00:30:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-05-01 13:09:49 +00:00
|
|
|
byte random_bits = Random();
|
|
|
|
|
2008-06-12 14:41:29 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
|
2010-05-01 13:09:49 +00:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile, true, random_bits);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && !Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_ALLOW_CONSTRUCTION, callback_res)) continue;
|
2008-06-12 14:41:29 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* build the house */
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
/* Special houses that there can be only one of. */
|
2009-06-23 09:52:26 +00:00
|
|
|
t->flags |= oneof;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
byte construction_counter = 0;
|
|
|
|
byte construction_stage = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-07 21:36:13 +00:00
|
|
|
if (_generating_world || _game_mode == GM_EDITOR) {
|
2008-02-05 00:30:58 +00:00
|
|
|
uint32 r = Random();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
construction_stage = TOWN_HOUSE_COMPLETED;
|
|
|
|
if (Chance16(1, 7)) construction_stage = GB(r, 0, 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-05 00:30:58 +00:00
|
|
|
if (construction_stage == TOWN_HOUSE_COMPLETED) {
|
|
|
|
ChangePopulation(t, hs->population);
|
|
|
|
} else {
|
|
|
|
construction_counter = GB(r, 2, 2);
|
2006-03-31 08:18:14 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-02-05 00:30:58 +00:00
|
|
|
|
2010-05-01 13:09:49 +00:00
|
|
|
MakeTownHouse(tile, t, construction_counter, construction_stage, house, random_bits);
|
2012-04-26 21:04:18 +00:00
|
|
|
UpdateTownRadius(t);
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowthRate(t);
|
2008-02-05 00:30:58 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-04 23:24:22 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-19 15:39:12 +00:00
|
|
|
/**
|
|
|
|
* Update data structures when a house is removed
|
|
|
|
* @param tile Tile of the house
|
|
|
|
* @param t Town owning the house
|
|
|
|
* @param house House type
|
|
|
|
*/
|
2008-06-14 19:13:23 +00:00
|
|
|
static void DoClearTownHouseHelper(TileIndex tile, Town *t, HouseID house)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2008-06-14 19:13:23 +00:00
|
|
|
DecreaseBuildingCount(t, house);
|
2004-08-09 17:04:08 +00:00
|
|
|
DoClearSquare(tile);
|
|
|
|
DeleteAnimatedTile(tile);
|
2010-04-24 13:39:11 +00:00
|
|
|
|
|
|
|
DeleteNewGRFInspectWindow(GSF_HOUSES, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-15 13:10:01 +00:00
|
|
|
/**
|
|
|
|
* Determines if a given HouseID is part of a multitile house.
|
|
|
|
* The given ID is set to the ID of the north tile and the TileDiff to the north tile is returned.
|
|
|
|
*
|
|
|
|
* @param house Is changed to the HouseID of the north tile of the same house
|
|
|
|
* @return TileDiff from the tile of the given HouseID to the north tile
|
|
|
|
*/
|
2008-10-20 19:35:48 +00:00
|
|
|
TileIndexDiff GetHouseNorthPart(HouseID &house)
|
2008-04-15 13:10:01 +00:00
|
|
|
{
|
|
|
|
if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks.
|
2009-06-26 14:02:27 +00:00
|
|
|
if (HouseSpec::Get(house - 1)->building_flags & TILE_SIZE_2x1) {
|
2008-04-15 13:10:01 +00:00
|
|
|
house--;
|
|
|
|
return TileDiffXY(-1, 0);
|
2009-06-26 14:02:27 +00:00
|
|
|
} else if (HouseSpec::Get(house - 1)->building_flags & BUILDING_2_TILES_Y) {
|
2008-04-15 13:10:01 +00:00
|
|
|
house--;
|
|
|
|
return TileDiffXY(0, -1);
|
2009-06-26 14:02:27 +00:00
|
|
|
} else if (HouseSpec::Get(house - 2)->building_flags & BUILDING_HAS_4_TILES) {
|
2008-04-15 13:10:01 +00:00
|
|
|
house -= 2;
|
|
|
|
return TileDiffXY(-1, 0);
|
2009-06-26 14:02:27 +00:00
|
|
|
} else if (HouseSpec::Get(house - 3)->building_flags & BUILDING_HAS_4_TILES) {
|
2008-04-15 13:10:01 +00:00
|
|
|
house -= 3;
|
|
|
|
return TileDiffXY(-1, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
void ClearTownHouse(Town *t, TileIndex tile)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-04 14:53:06 +00:00
|
|
|
HouseID house = GetHouseType(tile);
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* need to align the tile to point to the upper left corner of the house */
|
2008-04-15 13:10:01 +00:00
|
|
|
tile += GetHouseNorthPart(house); // modifies house to the ID of the north tile
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Remove population from the town if the house is finished. */
|
2007-03-19 11:27:30 +00:00
|
|
|
if (IsHouseCompleted(tile)) {
|
|
|
|
ChangePopulation(t, -hs->population);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Clear flags for houses that only may exist once/town. */
|
2007-03-19 11:27:30 +00:00
|
|
|
if (hs->building_flags & BUILDING_IS_CHURCH) {
|
2009-06-23 09:52:26 +00:00
|
|
|
ClrBit(t->flags, TOWN_HAS_CHURCH);
|
2007-03-19 11:27:30 +00:00
|
|
|
} else if (hs->building_flags & BUILDING_IS_STADIUM) {
|
2009-06-23 09:52:26 +00:00
|
|
|
ClrBit(t->flags, TOWN_HAS_STADIUM);
|
2004-11-22 18:42:03 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Do the actual clearing of tiles */
|
2008-06-14 19:13:23 +00:00
|
|
|
DoClearTownHouseHelper(tile, t, house);
|
2020-05-15 20:18:03 +00:00
|
|
|
if (hs->building_flags & BUILDING_2_TILES_Y) DoClearTownHouseHelper(tile + TileDiffXY(0, 1), t, ++house);
|
|
|
|
if (hs->building_flags & BUILDING_2_TILES_X) DoClearTownHouseHelper(tile + TileDiffXY(1, 0), t, ++house);
|
|
|
|
if (hs->building_flags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1), t, ++house);
|
|
|
|
|
|
|
|
RemoveNearbyStations(t, tile, hs->building_flags);
|
2011-12-03 22:21:27 +00:00
|
|
|
|
2012-04-26 21:04:18 +00:00
|
|
|
UpdateTownRadius(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Rename a town (server-only).
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 town ID to rename
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
bool reset = StrEmpty(text);
|
2008-09-15 19:02:50 +00:00
|
|
|
|
|
|
|
if (!reset) {
|
2010-12-05 22:25:21 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return CMD_ERROR;
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsUniqueTownName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2008-09-15 19:02:50 +00:00
|
|
|
}
|
2007-06-27 20:53:25 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2020-01-06 20:40:31 +00:00
|
|
|
t->cached_name.clear();
|
2020-05-17 21:31:59 +00:00
|
|
|
if (reset) {
|
|
|
|
t->name.clear();
|
|
|
|
} else {
|
|
|
|
t->name = text;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
t->UpdateVirtCoord();
|
2020-01-05 21:55:57 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_RESORT);
|
2020-01-06 20:40:31 +00:00
|
|
|
ClearAllStationCachedNames();
|
|
|
|
ClearAllIndustryCachedNames();
|
2009-07-13 22:33:25 +00:00
|
|
|
UpdateAllStationVirtCoords();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 16:07:14 +00:00
|
|
|
/**
|
|
|
|
* Determines the first cargo with a certain town effect
|
|
|
|
* @param effect Town effect of interest
|
|
|
|
* @return first active cargo slot with that effect
|
|
|
|
*/
|
|
|
|
const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect)
|
|
|
|
{
|
2021-04-29 15:51:05 +00:00
|
|
|
for (const CargoSpec *cs : CargoSpec::Iterate()) {
|
2011-11-23 16:07:14 +00:00
|
|
|
if (cs->town_effect == effect) return cs;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2011-11-23 16:07:14 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 20:59:19 +00:00
|
|
|
/**
|
|
|
|
* Change the cargo goal of a town.
|
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 various bitstuffed elements
|
|
|
|
* - p1 = (bit 0 - 15) - Town ID to cargo game of.
|
|
|
|
* - p1 = (bit 16 - 23) - TownEffect to change the game of.
|
|
|
|
* @param p2 The new goal value.
|
|
|
|
* @param text Unused.
|
|
|
|
* @return Empty cost or an error.
|
|
|
|
*/
|
|
|
|
CommandCost CmdTownCargoGoal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
|
|
|
|
|
|
|
TownEffect te = (TownEffect)GB(p1, 16, 8);
|
2013-11-22 21:44:23 +00:00
|
|
|
if (te < TE_BEGIN || te >= TE_END) return CMD_ERROR;
|
2011-12-19 20:59:19 +00:00
|
|
|
|
|
|
|
uint16 index = GB(p1, 0, 16);
|
|
|
|
Town *t = Town::GetIfValid(index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2011-12-19 20:59:19 +00:00
|
|
|
|
|
|
|
/* Validate if there is a cargo which is the requested TownEffect */
|
|
|
|
const CargoSpec *cargo = FindFirstCargoWithTownEffect(te);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (cargo == nullptr) return CMD_ERROR;
|
2011-12-19 20:59:19 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
t->goal[te] = p2;
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowth(t);
|
2011-12-19 20:59:19 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_VIEW, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:55 +00:00
|
|
|
/**
|
|
|
|
* Set a custom text in the Town window.
|
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 Town ID to change the text of.
|
|
|
|
* @param p2 Unused.
|
|
|
|
* @param text The new text (empty to remove the text).
|
|
|
|
* @return Empty cost or an error.
|
|
|
|
*/
|
|
|
|
CommandCost CmdTownSetText(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2011-12-19 21:00:55 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2020-05-17 21:31:59 +00:00
|
|
|
t->text.clear();
|
|
|
|
if (!StrEmpty(text)) t->text = text;
|
2011-12-19 21:00:55 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_VIEW, p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2011-12-19 20:59:19 +00:00
|
|
|
/**
|
|
|
|
* Change the growth rate of the town.
|
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 Town ID to cargo game of.
|
2018-05-02 19:01:30 +00:00
|
|
|
* @param p2 Amount of days between growth, or TOWN_GROWTH_RATE_NONE, or 0 to reset custom growth rate.
|
2011-12-19 20:59:19 +00:00
|
|
|
* @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 (GB(p2, 16, 16) != 0) return CMD_ERROR;
|
|
|
|
|
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2011-12-19 20:59:19 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2013-10-30 10:01:49 +00:00
|
|
|
if (p2 == 0) {
|
2018-06-24 18:55:48 +00:00
|
|
|
/* Just clear the flag, UpdateTownGrowth will determine a proper growth rate */
|
2018-05-02 19:01:30 +00:00
|
|
|
ClrBit(t->flags, TOWN_CUSTOM_GROWTH);
|
2013-10-30 10:01:49 +00:00
|
|
|
} else {
|
2018-05-02 19:01:30 +00:00
|
|
|
uint old_rate = t->growth_rate;
|
2013-10-30 10:01:49 +00:00
|
|
|
if (t->grow_counter >= old_rate) {
|
|
|
|
/* This also catches old_rate == 0 */
|
|
|
|
t->grow_counter = p2;
|
|
|
|
} else {
|
|
|
|
/* Scale grow_counter, so half finished houses stay half finished */
|
|
|
|
t->grow_counter = t->grow_counter * p2 / old_rate;
|
|
|
|
}
|
2018-05-02 19:01:30 +00:00
|
|
|
t->growth_rate = p2;
|
|
|
|
SetBit(t->flags, TOWN_CUSTOM_GROWTH);
|
2013-10-30 10:01:49 +00:00
|
|
|
}
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowth(t);
|
2011-12-19 20:59:19 +00:00
|
|
|
InvalidateWindowData(WC_TOWN_VIEW, p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2020-01-04 16:31:52 +00:00
|
|
|
/**
|
|
|
|
* Change the rating of a company in a town
|
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 Bit 0..15 = Town ID to change, bit 16..23 = Company ID to change.
|
|
|
|
* @param p2 Bit 0..15 = New rating of company (signed int16).
|
|
|
|
* @param text Unused.
|
|
|
|
* @return Empty cost or an error.
|
|
|
|
*/
|
|
|
|
CommandCost CmdTownRating(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
if (_current_company != OWNER_DEITY) return CMD_ERROR;
|
|
|
|
|
|
|
|
TownID town_id = (TownID)GB(p1, 0, 16);
|
|
|
|
Town *t = Town::GetIfValid(town_id);
|
|
|
|
if (t == nullptr) return CMD_ERROR;
|
|
|
|
|
|
|
|
CompanyID company_id = (CompanyID)GB(p1, 16, 8);
|
|
|
|
if (!Company::IsValidID(company_id)) return CMD_ERROR;
|
|
|
|
|
|
|
|
int16 new_rating = Clamp((int16)GB(p2, 0, 16), RATING_MINIMUM, RATING_MAXIMUM);
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
t->ratings[company_id] = new_rating;
|
|
|
|
InvalidateWindowData(WC_TOWN_AUTHORITY, town_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2010-08-02 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Expand a town (scenario editor only).
|
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 Town ID to expand.
|
2011-12-19 20:57:51 +00:00
|
|
|
* @param p2 Amount to grow, or 0 to grow a random size up to the current amount of houses.
|
2010-08-02 21:06:06 +00:00
|
|
|
* @param text Unused.
|
|
|
|
* @return Empty cost or an error.
|
|
|
|
*/
|
|
|
|
CommandCost CmdExpandTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-12-19 20:57:51 +00:00
|
|
|
if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) return CMD_ERROR;
|
2010-08-02 21:06:06 +00:00
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-08-02 21:06:06 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* The more houses, the faster we grow */
|
2011-12-19 20:57:51 +00:00
|
|
|
if (p2 == 0) {
|
2012-04-25 20:50:13 +00:00
|
|
|
uint amount = RandomRange(ClampToU16(t->cache.num_houses / 10)) + 3;
|
|
|
|
t->cache.num_houses += amount;
|
2011-12-19 20:57:51 +00:00
|
|
|
UpdateTownRadius(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-12-19 20:57:51 +00:00
|
|
|
uint n = amount * 10;
|
|
|
|
do GrowTown(t); while (--n);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.num_houses -= amount;
|
2011-12-19 20:57:51 +00:00
|
|
|
} else {
|
|
|
|
for (; p2 > 0; p2--) {
|
|
|
|
/* Try several times to grow, as we are really suppose to grow */
|
|
|
|
for (uint i = 0; i < 25; i++) if (GrowTown(t)) break;
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 21:06:06 +00:00
|
|
|
UpdateTownRadius(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-02 21:06:06 +00:00
|
|
|
UpdateTownMaxPass(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-02 21:07:23 +00:00
|
|
|
/**
|
2012-01-17 18:46:09 +00:00
|
|
|
* Delete a town (scenario editor or worldgen only).
|
2010-08-02 21:07:23 +00:00
|
|
|
* @param tile Unused.
|
|
|
|
* @param flags Type of operation.
|
|
|
|
* @param p1 Town ID to delete.
|
|
|
|
* @param p2 Unused.
|
|
|
|
* @param text Unused.
|
|
|
|
* @return Empty cost or an error.
|
|
|
|
*/
|
|
|
|
CommandCost CmdDeleteTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
2012-01-17 18:46:09 +00:00
|
|
|
if (_game_mode != GM_EDITOR && !_generating_world) return CMD_ERROR;
|
2010-08-02 21:07:23 +00:00
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CMD_ERROR;
|
2010-08-02 21:07:23 +00:00
|
|
|
|
|
|
|
/* Stations refer to towns. */
|
2019-12-15 04:55:59 +00:00
|
|
|
for (const Station *st : Station::Iterate()) {
|
2010-08-02 21:07:23 +00:00
|
|
|
if (st->town == t) {
|
|
|
|
/* Non-oil rig stations are always a problem. */
|
|
|
|
if (!(st->facilities & FACIL_AIRPORT) || st->airport.type != AT_OILRIG) return CMD_ERROR;
|
|
|
|
/* We can only automatically delete oil rigs *if* there's no vehicle on them. */
|
2010-08-13 11:57:59 +00:00
|
|
|
CommandCost ret = DoCommand(st->airport.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2010-08-02 21:07:23 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Depots refer to towns. */
|
2019-12-15 16:54:05 +00:00
|
|
|
for (const Depot *d : Depot::Iterate()) {
|
2010-08-02 21:07:23 +00:00
|
|
|
if (d->town == t) return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:28:55 +00:00
|
|
|
/* Check all tiles for town ownership. First check for bridge tiles, as
|
|
|
|
* these do not directly have an owner so we need to check adjacent
|
|
|
|
* tiles. This won't work correctly in the same loop if the adjacent
|
|
|
|
* tile was already deleted earlier in the loop. */
|
2021-05-26 18:51:17 +00:00
|
|
|
for (TileIndex current_tile = 0; current_tile < MapSize(); ++current_tile) {
|
|
|
|
if (IsTileType(current_tile, MP_TUNNELBRIDGE) && TestTownOwnsBridge(current_tile, t)) {
|
|
|
|
CommandCost ret = DoCommand(current_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2019-03-03 22:28:55 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check all remaining tiles for town ownership. */
|
2021-05-26 18:51:17 +00:00
|
|
|
for (TileIndex current_tile = 0; current_tile < MapSize(); ++current_tile) {
|
2010-08-13 11:57:59 +00:00
|
|
|
bool try_clear = false;
|
2021-05-26 18:51:17 +00:00
|
|
|
switch (GetTileType(current_tile)) {
|
2010-08-02 21:07:23 +00:00
|
|
|
case MP_ROAD:
|
2021-05-26 18:51:17 +00:00
|
|
|
try_clear = HasTownOwnedRoad(current_tile) && GetTownIndex(current_tile) == t->index;
|
2010-08-02 21:07:23 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-13 11:57:59 +00:00
|
|
|
case MP_HOUSE:
|
2021-05-26 18:51:17 +00:00
|
|
|
try_clear = GetTownIndex(current_tile) == t->index;
|
2010-08-13 11:57:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_INDUSTRY:
|
2021-05-26 18:51:17 +00:00
|
|
|
try_clear = Industry::GetByTile(current_tile)->town == t;
|
2010-08-02 21:07:23 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-13 11:58:45 +00:00
|
|
|
case MP_OBJECT:
|
2010-08-13 12:45:26 +00:00
|
|
|
if (Town::GetNumItems() == 1) {
|
|
|
|
/* No towns will be left, remove it! */
|
|
|
|
try_clear = true;
|
|
|
|
} else {
|
2021-05-26 18:51:17 +00:00
|
|
|
Object *o = Object::GetByTile(current_tile);
|
2010-08-13 12:45:26 +00:00
|
|
|
if (o->town == t) {
|
2013-10-12 16:30:42 +00:00
|
|
|
if (o->type == OBJECT_STATUE) {
|
2010-08-13 12:45:26 +00:00
|
|
|
/* Statue... always remove. */
|
|
|
|
try_clear = true;
|
|
|
|
} else {
|
|
|
|
/* Tell to find a new town. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (flags & DC_EXEC) o->town = nullptr;
|
2010-08-13 12:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-13 11:58:45 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-02 21:07:23 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-08-13 11:57:59 +00:00
|
|
|
if (try_clear) {
|
2021-05-26 18:51:17 +00:00
|
|
|
CommandCost ret = DoCommand(current_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2010-08-13 11:57:59 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2010-08-02 21:07:23 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 11:57:59 +00:00
|
|
|
/* The town destructor will delete the other things related to the town. */
|
2019-02-18 16:32:53 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
_town_kdtree.Remove(t->index);
|
2019-12-01 22:17:33 +00:00
|
|
|
if (t->cache.sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeTown(t->index));
|
2019-02-18 16:32:53 +00:00
|
|
|
delete t;
|
|
|
|
}
|
2010-08-02 21:07:23 +00:00
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Factor in the cost of each town action.
|
2009-09-20 10:08:10 +00:00
|
|
|
* @see TownActions
|
|
|
|
*/
|
|
|
|
const byte _town_action_costs[TACT_COUNT] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
2, 4, 9, 35, 48, 53, 117, 175
|
|
|
|
};
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionAdvertiseSmall(Town *t, DoCommandFlag flags)
|
2006-05-20 16:46:37 +00:00
|
|
|
{
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
ModifyStationRatingAround(t->xy, _current_company, 0x40, 10);
|
|
|
|
}
|
|
|
|
return CommandCost();
|
2006-05-20 16:46:37 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionAdvertiseMedium(Town *t, DoCommandFlag flags)
|
2006-05-20 16:46:37 +00:00
|
|
|
{
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
ModifyStationRatingAround(t->xy, _current_company, 0x70, 15);
|
|
|
|
}
|
|
|
|
return CommandCost();
|
2006-05-20 16:46:37 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionAdvertiseLarge(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
ModifyStationRatingAround(t->xy, _current_company, 0xA0, 20);
|
|
|
|
}
|
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionRoadRebuild(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-02-05 13:46:09 +00:00
|
|
|
/* Check if the company is allowed to fund new roads. */
|
|
|
|
if (!_settings_game.economy.fund_roads) return CMD_ERROR;
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
t->road_build_months = 6;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-12-05 22:24:50 +00:00
|
|
|
char company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
|
2010-07-27 16:40:16 +00:00
|
|
|
SetDParam(0, _current_company);
|
|
|
|
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
|
2008-07-18 16:26:51 +00:00
|
|
|
|
2014-04-25 15:40:32 +00:00
|
|
|
char *cn = stredup(company_name);
|
2010-07-27 16:40:16 +00:00
|
|
|
SetDParam(0, t->index);
|
|
|
|
SetDParamStr(1, cn);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-05-26 14:16:03 +00:00
|
|
|
AddNewsItem(STR_NEWS_ROAD_REBUILDING, NT_GENERAL, NF_NORMAL, NR_TOWN, t->index, NR_NONE, UINT32_MAX, cn);
|
2012-05-26 14:16:45 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
|
|
|
|
Game::NewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
|
2010-07-27 16:40:16 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-08 17:26:59 +00:00
|
|
|
/**
|
|
|
|
* Check whether the land can be cleared.
|
|
|
|
* @param tile Tile to check.
|
|
|
|
* @return The tile can be cleared.
|
|
|
|
*/
|
|
|
|
static bool TryClearTile(TileIndex tile)
|
|
|
|
{
|
2019-04-22 08:10:04 +00:00
|
|
|
Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
|
2012-04-08 17:26:59 +00:00
|
|
|
CommandCost r = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
|
|
|
|
cur_company.Restore();
|
|
|
|
return r.Succeeded();
|
|
|
|
}
|
|
|
|
|
2012-04-08 17:29:00 +00:00
|
|
|
/** Structure for storing data while searching the best place to build a statue. */
|
|
|
|
struct StatueBuildSearchData {
|
|
|
|
TileIndex best_position; ///< Best position found so far.
|
2012-04-08 17:30:20 +00:00
|
|
|
int tile_count; ///< Number of tiles tried.
|
2012-04-08 17:29:00 +00:00
|
|
|
|
2012-04-08 17:30:20 +00:00
|
|
|
StatueBuildSearchData(TileIndex best_pos, int count) : best_position(best_pos), tile_count(count) { }
|
2012-04-08 17:29:00 +00:00
|
|
|
};
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
/**
|
2012-04-08 17:29:00 +00:00
|
|
|
* Search callback function for #TownActionBuildStatue.
|
2010-07-27 16:40:16 +00:00
|
|
|
* @param tile Tile on which to perform the search.
|
2012-04-08 17:29:00 +00:00
|
|
|
* @param user_data Reference to the statue search data.
|
2010-07-27 16:40:16 +00:00
|
|
|
* @return Result of the test.
|
|
|
|
*/
|
|
|
|
static bool SearchTileForStatue(TileIndex tile, void *user_data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-04-08 17:30:20 +00:00
|
|
|
static const int STATUE_NUMBER_INNER_TILES = 25; // Number of tiles int the center of the city, where we try to protect houses.
|
|
|
|
|
2012-04-08 17:29:00 +00:00
|
|
|
StatueBuildSearchData *statue_data = (StatueBuildSearchData *)user_data;
|
2012-04-08 17:30:20 +00:00
|
|
|
statue_data->tile_count++;
|
2012-04-08 17:29:00 +00:00
|
|
|
|
2007-09-09 11:30:44 +00:00
|
|
|
/* Statues can be build on slopes, just like houses. Only the steep slopes is a no go. */
|
2011-11-04 10:22:27 +00:00
|
|
|
if (IsSteepSlope(GetTileSlope(tile))) return false;
|
2009-02-07 16:02:29 +00:00
|
|
|
/* Don't build statues under bridges. */
|
2014-09-21 11:24:51 +00:00
|
|
|
if (IsBridgeAbove(tile)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-04-08 17:27:47 +00:00
|
|
|
/* A clear-able open space is always preferred. */
|
|
|
|
if ((IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) && TryClearTile(tile)) {
|
2012-04-08 17:29:00 +00:00
|
|
|
statue_data->best_position = tile;
|
2012-04-08 17:27:47 +00:00
|
|
|
return true;
|
2006-02-06 08:15:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-04-08 17:27:47 +00:00
|
|
|
bool house = IsTileType(tile, MP_HOUSE);
|
|
|
|
|
2012-04-08 17:30:20 +00:00
|
|
|
/* Searching inside the inner circle. */
|
|
|
|
if (statue_data->tile_count <= STATUE_NUMBER_INNER_TILES) {
|
|
|
|
/* Save first house in inner circle. */
|
|
|
|
if (house && statue_data->best_position == INVALID_TILE && TryClearTile(tile)) {
|
|
|
|
statue_data->best_position = tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have reached the end of the inner circle, and have a saved house, terminate the search. */
|
|
|
|
return statue_data->tile_count == STATUE_NUMBER_INNER_TILES && statue_data->best_position != INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Searching outside the circle, just pick the first possible spot. */
|
2012-04-08 17:29:00 +00:00
|
|
|
statue_data->best_position = tile; // Is optimistic, the condition below must also hold.
|
2012-04-08 17:27:47 +00:00
|
|
|
return house && TryClearTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 23:01:58 +00:00
|
|
|
/**
|
|
|
|
* Perform a 9x9 tiles circular search from the center of the town
|
|
|
|
* in order to find a free tile to place a statue
|
|
|
|
* @param t town to search in
|
2010-07-27 16:40:16 +00:00
|
|
|
* @param flags Used to check if the statue must be built or not.
|
|
|
|
* @return Empty cost or an error.
|
2006-11-17 23:01:58 +00:00
|
|
|
*/
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionBuildStatue(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-02-12 18:45:05 +00:00
|
|
|
if (!Object::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_OBJECTS);
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = t->xy;
|
2012-04-08 17:30:20 +00:00
|
|
|
StatueBuildSearchData statue_data(INVALID_TILE, 0);
|
2012-04-08 17:29:00 +00:00
|
|
|
if (!CircularTileSearch(&tile, 9, SearchTileForStatue, &statue_data)) return_cmd_error(STR_ERROR_STATUE_NO_SUITABLE_PLACE);
|
2012-04-08 17:26:01 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2019-04-22 08:10:04 +00:00
|
|
|
Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
|
2012-04-08 17:29:00 +00:00
|
|
|
DoCommand(statue_data.best_position, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
2012-04-08 17:26:01 +00:00
|
|
|
cur_company.Restore();
|
2012-04-08 17:29:00 +00:00
|
|
|
BuildObject(OBJECT_STATUE, statue_data.best_position, _current_company, t);
|
2012-04-08 17:26:01 +00:00
|
|
|
SetBit(t->statues, _current_company); // Once found and built, "inform" the Town.
|
2012-04-08 17:29:00 +00:00
|
|
|
MarkTileDirtyByTile(statue_data.best_position);
|
2008-04-04 14:53:06 +00:00
|
|
|
}
|
2012-04-08 17:26:01 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionFundBuildings(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-11-23 16:10:18 +00:00
|
|
|
/* Check if it's allowed to buy the rights */
|
|
|
|
if (!_settings_game.economy.fund_buildings) return CMD_ERROR;
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* And grow for 3 months */
|
|
|
|
t->fund_buildings_months = 3;
|
2011-11-23 16:07:14 +00:00
|
|
|
|
2013-11-12 17:57:12 +00:00
|
|
|
/* Enable growth (also checking GameScript's opinion) */
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowth(t);
|
2013-11-12 17:57:12 +00:00
|
|
|
|
2018-05-02 19:01:30 +00:00
|
|
|
/* 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
|
2021-05-08 10:02:30 +00:00
|
|
|
* tick-perfect and gives player some time window where they can
|
2019-01-17 22:01:07 +00:00
|
|
|
* spam funding with the exact same efficiency.
|
2018-05-02 19:01:30 +00:00
|
|
|
*/
|
2021-01-08 10:16:18 +00:00
|
|
|
t->grow_counter = std::min<uint16>(t->grow_counter, 2 * TOWN_GROWTH_TICKS - (t->growth_rate - t->grow_counter) % TOWN_GROWTH_TICKS);
|
2018-05-02 19:01:30 +00:00
|
|
|
|
2011-11-23 16:07:14 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, t->index);
|
2010-07-27 16:40:16 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionBuyRights(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-31 21:02:56 +00:00
|
|
|
/* Check if it's allowed to buy the rights */
|
2010-07-27 16:40:16 +00:00
|
|
|
if (!_settings_game.economy.exclusive_rights) return CMD_ERROR;
|
2007-09-30 17:38:42 +00:00
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
t->exclusive_counter = 12;
|
|
|
|
t->exclusivity = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
ModifyStationRatingAround(t->xy, _current_company, 130, 17);
|
2012-05-06 11:38:52 +00:00
|
|
|
|
|
|
|
SetWindowClassesDirty(WC_STATION_VIEW);
|
2012-05-26 14:16:23 +00:00
|
|
|
|
|
|
|
/* Spawn news message */
|
|
|
|
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
|
|
|
|
cni->FillData(Company::Get(_current_company));
|
|
|
|
SetDParam(0, STR_NEWS_EXCLUSIVE_RIGHTS_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_EXCLUSIVE_RIGHTS_DESCRIPTION);
|
|
|
|
SetDParam(2, t->index);
|
|
|
|
SetDParamStr(3, cni->company_name);
|
|
|
|
AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NT_GENERAL, NF_COMPANY, NR_TOWN, t->index, NR_NONE, UINT32_MAX, cni);
|
2012-05-26 14:16:38 +00:00
|
|
|
AI::BroadcastNewEvent(new ScriptEventExclusiveTransportRights((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
|
|
|
|
Game::NewEvent(new ScriptEventExclusiveTransportRights((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
|
2010-07-27 16:40:16 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
static CommandCost TownActionBribe(Town *t, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-27 16:40:16 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (Chance16(1, 14)) {
|
|
|
|
/* set as unwanted for 6 months */
|
|
|
|
t->unwanted[_current_company] = 6;
|
|
|
|
|
|
|
|
/* set all close by station ratings to 0 */
|
2019-12-15 04:55:59 +00:00
|
|
|
for (Station *st : Station::Iterate()) {
|
2010-07-27 16:40:16 +00:00
|
|
|
if (st->town == t && st->owner == _current_company) {
|
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) st->goods[i].rating = 0;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* only show error message to the executing player. All errors are handled command.c
|
2010-07-27 16:40:16 +00:00
|
|
|
* but this is special, because it can only 'fail' on a DC_EXEC */
|
2011-11-10 18:09:09 +00:00
|
|
|
if (IsLocalCompany()) ShowErrorMessage(STR_ERROR_BRIBE_FAILED, INVALID_STRING_ID, WL_INFO);
|
2010-07-27 16:40:16 +00:00
|
|
|
|
|
|
|
/* decrease by a lot!
|
|
|
|
* ChangeTownRating is only for stuff in demolishing. Bribe failure should
|
|
|
|
* be independent of any cheat settings
|
|
|
|
*/
|
|
|
|
if (t->ratings[_current_company] > RATING_BRIBE_DOWN_TO) {
|
|
|
|
t->ratings[_current_company] = RATING_BRIBE_DOWN_TO;
|
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM, DC_EXEC);
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-07-27 16:40:16 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
typedef CommandCost TownActionProc(Town *t, DoCommandFlag flags);
|
2009-03-15 16:04:39 +00:00
|
|
|
static TownActionProc * const _town_action_proc[] = {
|
2006-05-20 16:46:37 +00:00
|
|
|
TownActionAdvertiseSmall,
|
|
|
|
TownActionAdvertiseMedium,
|
|
|
|
TownActionAdvertiseLarge,
|
2004-08-09 17:04:08 +00:00
|
|
|
TownActionRoadRebuild,
|
|
|
|
TownActionBuildStatue,
|
|
|
|
TownActionFundBuildings,
|
|
|
|
TownActionBuyRights,
|
|
|
|
TownActionBribe
|
|
|
|
};
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Get a list of available actions to do at a town.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param nump if not nullptr add put the number of available actions in it
|
2009-01-19 12:07:01 +00:00
|
|
|
* @param cid the company that is querying the town
|
|
|
|
* @param t the town that is queried
|
|
|
|
* @return bitmasked value of enabled actions
|
|
|
|
*/
|
|
|
|
uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t)
|
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
TownActions buttons = TACT_NONE;
|
|
|
|
|
|
|
|
/* Spectators and unwanted have no options */
|
|
|
|
if (cid != COMPANY_SPECTATOR && !(_settings_game.economy.bribe && t->unwanted[cid])) {
|
|
|
|
|
|
|
|
/* Things worth more than this are not shown */
|
2009-11-07 22:47:54 +00:00
|
|
|
Money avail = Company::Get(cid)->money + _price[PR_STATION_VALUE] * 200;
|
2009-01-19 12:07:01 +00:00
|
|
|
|
|
|
|
/* Check the action bits for validity and
|
|
|
|
* if they are valid add them */
|
|
|
|
for (uint i = 0; i != lengthof(_town_action_costs); i++) {
|
|
|
|
const TownActions cur = (TownActions)(1 << i);
|
|
|
|
|
|
|
|
/* Is the company not able to bribe ? */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (cur == TACT_BRIBE && (!_settings_game.economy.bribe || t->ratings[cid] >= RATING_BRIBE_MAXIMUM)) continue;
|
2009-01-19 12:07:01 +00:00
|
|
|
|
|
|
|
/* Is the company not able to buy exclusive rights ? */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (cur == TACT_BUY_RIGHTS && !_settings_game.economy.exclusive_rights) continue;
|
2009-01-19 12:07:01 +00:00
|
|
|
|
2011-11-23 16:10:18 +00:00
|
|
|
/* Is the company not able to fund buildings ? */
|
|
|
|
if (cur == TACT_FUND_BUILDINGS && !_settings_game.economy.fund_buildings) continue;
|
|
|
|
|
2011-02-05 13:46:09 +00:00
|
|
|
/* Is the company not able to fund local road reconstruction? */
|
|
|
|
if (cur == TACT_ROAD_REBUILD && !_settings_game.economy.fund_roads) continue;
|
|
|
|
|
2009-01-19 12:07:01 +00:00
|
|
|
/* Is the company not able to build a statue ? */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (cur == TACT_BUILD_STATUE && HasBit(t->statues, cid)) continue;
|
2009-01-19 12:07:01 +00:00
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
if (avail >= _town_action_costs[i] * _price[PR_TOWN_ACTION] >> 8) {
|
2009-01-19 12:07:01 +00:00
|
|
|
buttons |= cur;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (nump != nullptr) *nump = num;
|
2009-01-19 12:07:01 +00:00
|
|
|
return buttons;
|
|
|
|
}
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Do a town action.
|
2005-05-12 00:11:37 +00:00
|
|
|
* This performs an action such as advertising, building a statue, funding buildings,
|
|
|
|
* but also bribing the town-council
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 03:21:14 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 town to do the action at
|
|
|
|
* @param p2 action to perform, @see _town_action_proc for the list of available actions
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdDoTownAction(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Town *t = Town::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr || p2 >= lengthof(_town_action_proc)) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!HasBit(GetMaskOfTownActions(nullptr, _current_company, t), p2)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
CommandCost cost(EXPENSES_OTHER, _price[PR_TOWN_ACTION] * _town_action_costs[p2] >> 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-07-27 16:40:16 +00:00
|
|
|
CommandCost ret = _town_action_proc[p2](t, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:14:52 +00:00
|
|
|
template <typename Func>
|
|
|
|
static void ForAllStationsNearTown(Town *t, Func func)
|
|
|
|
{
|
|
|
|
/* Ideally the search radius should be close to the actual town zone 0 radius.
|
|
|
|
* The true radius is not stored or calculated anywhere, only the squared radius. */
|
|
|
|
/* The efficiency of this search might be improved for large towns and many stations on the map,
|
|
|
|
* by using an integer square root approximation giving a value not less than the true square root. */
|
|
|
|
uint search_radius = t->cache.squared_town_zone_radius[0] / 2;
|
|
|
|
ForAllStationsRadius(t->xy, search_radius, [&](const Station * st) {
|
|
|
|
if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) {
|
|
|
|
func(st);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-23 16:07:14 +00:00
|
|
|
static void UpdateTownRating(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Increase company ratings if they're low */
|
2019-12-14 16:22:38 +00:00
|
|
|
for (const Company *c : Company::Iterate()) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (t->ratings[c->index] < RATING_GROWTH_MAXIMUM) {
|
2021-01-08 10:16:18 +00:00
|
|
|
t->ratings[c->index] = std::min((int)RATING_GROWTH_MAXIMUM, t->ratings[c->index] + RATING_GROWTH_UP_STEP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:14:52 +00:00
|
|
|
ForAllStationsNearTown(t, [&](const Station *st) {
|
|
|
|
if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
|
|
|
|
if (Company::IsValidID(st->owner)) {
|
|
|
|
int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP;
|
2021-01-08 10:16:18 +00:00
|
|
|
t->ratings[st->owner] = std::min<int>(new_rating, INT16_MAX); // do not let it overflow
|
2019-02-18 20:14:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Company::IsValidID(st->owner)) {
|
|
|
|
int new_rating = t->ratings[st->owner] + RATING_STATION_DOWN_STEP;
|
2021-01-08 10:16:18 +00:00
|
|
|
t->ratings[st->owner] = std::max(new_rating, INT16_MIN);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 20:14:52 +00:00
|
|
|
});
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-04 15:10:54 +00:00
|
|
|
/* clamp all ratings to valid values */
|
2008-09-30 20:39:50 +00:00
|
|
|
for (uint i = 0; i < MAX_COMPANIES; i++) {
|
2008-04-04 15:10:54 +00:00
|
|
|
t->ratings[i] = Clamp(t->ratings[i], RATING_MINIMUM, RATING_MAXIMUM);
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
|
2011-11-23 16:07:14 +00:00
|
|
|
}
|
2008-06-02 14:34:23 +00:00
|
|
|
|
2018-06-24 18:55:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates town grow counter after growth rate change.
|
|
|
|
* Preserves relative house builting progress whenever it can.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t The town to calculate grow counter for
|
2018-06-24 18:55:48 +00:00
|
|
|
* @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) {
|
2021-01-08 10:16:18 +00:00
|
|
|
t->grow_counter = std::min<uint16>(t->growth_rate, t->grow_counter);
|
2018-06-24 18:55:48 +00:00
|
|
|
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).
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t The town to calculate stations for
|
2018-06-24 18:55:48 +00:00
|
|
|
* @returns Amount of active stations
|
|
|
|
*/
|
|
|
|
static int CountActiveStations(Town *t)
|
|
|
|
{
|
|
|
|
int n = 0;
|
2019-02-18 20:14:52 +00:00
|
|
|
ForAllStationsNearTown(t, [&](const Station * st) {
|
|
|
|
if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
|
|
|
|
n++;
|
2018-06-24 18:55:48 +00:00
|
|
|
}
|
2019-02-18 20:14:52 +00:00
|
|
|
});
|
2018-06-24 18:55:48 +00:00
|
|
|
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).
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t The town to calculate growth rate for
|
2018-06-24 18:55:48 +00:00
|
|
|
* @returns Calculated growth rate
|
|
|
|
*/
|
|
|
|
static uint GetNormalGrowthRate(Town *t)
|
2011-11-23 16:07:14 +00:00
|
|
|
{
|
2019-11-20 00:00:07 +00:00
|
|
|
/**
|
|
|
|
* Note:
|
|
|
|
* Unserviced+unfunded towns get an additional malus in UpdateTownGrowth(),
|
|
|
|
* so the "320" is actually not better than the "420".
|
|
|
|
*/
|
2018-06-24 18:55:48 +00:00
|
|
|
static const uint16 _grow_count_values[2][6] = {
|
|
|
|
{ 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated
|
2019-11-20 00:00:07 +00:00
|
|
|
{ 320, 420, 300, 220, 160, 100 } // Normal values
|
2018-06-24 18:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int n = CountActiveStations(t);
|
2021-01-08 10:16:18 +00:00
|
|
|
uint16 m = _grow_count_values[t->fund_buildings_months != 0 ? 0 : 1][std::min(n, 5)];
|
2018-06-24 18:55:48 +00:00
|
|
|
|
|
|
|
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.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t The town to update growth rate for
|
2018-06-24 18:55:48 +00:00
|
|
|
*/
|
|
|
|
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).
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t The town to update growth for
|
2018-06-24 18:55:48 +00:00
|
|
|
*/
|
|
|
|
static void UpdateTownGrowth(Town *t)
|
|
|
|
{
|
|
|
|
UpdateTownGrowthRate(t);
|
|
|
|
|
2013-10-28 10:55:34 +00:00
|
|
|
ClrBit(t->flags, TOWN_IS_GROWING);
|
2011-11-23 16:07:14 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, t->index);
|
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.economy.town_growth_rate == 0 && t->fund_buildings_months == 0) return;
|
2007-04-12 17:24:34 +00:00
|
|
|
|
2011-11-25 11:35:15 +00:00
|
|
|
if (t->fund_buildings_months == 0) {
|
|
|
|
/* Check if all goals are reached for this town to grow (given we are not funding it) */
|
|
|
|
for (int i = TE_BEGIN; i < TE_END; i++) {
|
2011-11-27 11:42:52 +00:00
|
|
|
switch (t->goal[i]) {
|
|
|
|
case TOWN_GROWTH_WINTER:
|
2012-04-25 20:50:13 +00:00
|
|
|
if (TileHeight(t->xy) >= GetSnowLine() && t->received[i].old_act == 0 && t->cache.population > 90) return;
|
2011-11-27 11:42:52 +00:00
|
|
|
break;
|
|
|
|
case TOWN_GROWTH_DESERT:
|
2012-04-25 20:50:13 +00:00
|
|
|
if (GetTropicZone(t->xy) == TROPICZONE_DESERT && t->received[i].old_act == 0 && t->cache.population > 60) return;
|
2011-11-27 11:42:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (t->goal[i] > t->received[i].old_act) return;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-25 11:35:15 +00:00
|
|
|
}
|
2011-11-23 16:07:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:01:30 +00:00
|
|
|
if (HasBit(t->flags, TOWN_CUSTOM_GROWTH)) {
|
|
|
|
if (t->growth_rate != TOWN_GROWTH_RATE_NONE) SetBit(t->flags, TOWN_IS_GROWING);
|
2011-12-19 20:59:19 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, t->index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-24 18:55:48 +00:00
|
|
|
if (t->fund_buildings_months == 0 && CountActiveStations(t) == 0 && !Chance16(1, 12)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-10-28 10:55:34 +00:00
|
|
|
SetBit(t->flags, TOWN_IS_GROWING);
|
2011-11-23 16:07:14 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownAmounts(Town *t)
|
|
|
|
{
|
2011-11-23 16:05:19 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) t->supplied[i].NewMonth();
|
|
|
|
for (int i = TE_BEGIN; i < TE_END; i++) t->received[i].NewMonth();
|
2011-12-19 20:59:19 +00:00
|
|
|
if (t->fund_buildings_months != 0) t->fund_buildings_months--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownUnwanted(Town *t)
|
|
|
|
{
|
2019-12-14 16:22:38 +00:00
|
|
|
for (const Company *c : Company::Iterate()) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (t->unwanted[c->index] > 0) t->unwanted[c->index]--;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-11 14:26:11 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the local authority allows construction of a new station (rail, road, airport, dock) on the given tile
|
|
|
|
* @param tile The tile where the station shall be constructed.
|
2009-02-11 18:33:27 +00:00
|
|
|
* @param flags Command flags. DC_NO_TEST_TOWN_RATING is tested.
|
2010-02-27 17:42:55 +00:00
|
|
|
* @return Succeeded or failed command.
|
2009-01-11 14:26:11 +00:00
|
|
|
*/
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-02-27 17:42:55 +00:00
|
|
|
if (!Company::IsValidID(_current_company) || (flags & DC_NO_TEST_TOWN_RATING)) return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-27 17:42:55 +00:00
|
|
|
if (t->ratings[_current_company] > RATING_VERYPOOR) return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2010-02-27 17:42:55 +00:00
|
|
|
return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Return the town closest to the given tile within \a threshold.
|
2010-03-20 15:23:26 +00:00
|
|
|
* @param tile Starting point of the search.
|
|
|
|
* @param threshold Biggest allowed distance to the town.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return Closest town to \a tile within \a threshold, or \c nullptr if there is no such town.
|
2010-03-20 15:23:26 +00:00
|
|
|
*
|
|
|
|
* @note This function only uses distance, the #ClosestTownFromTile function also takes town ownership into account.
|
|
|
|
*/
|
2009-07-05 13:21:51 +00:00
|
|
|
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (Town::GetNumItems() == 0) return nullptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-02-18 16:32:53 +00:00
|
|
|
TownID tid = _town_kdtree.FindNearest(TileX(tile), TileY(tile));
|
|
|
|
Town *town = Town::Get(tid);
|
|
|
|
if (DistanceManhattan(tile, town->xy) < threshold) return town;
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/**
|
|
|
|
* Return the town closest (in distance or ownership) to a given tile, within a given threshold.
|
|
|
|
* @param tile Starting point of the search.
|
|
|
|
* @param threshold Biggest allowed distance to the town.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return Closest town to \a tile within \a threshold, or \c nullptr if there is no such town.
|
2010-08-01 17:45:53 +00:00
|
|
|
*
|
|
|
|
* @note If you only care about distance, you can use the #CalcClosestTownFromTile function.
|
|
|
|
*/
|
2006-06-08 18:31:54 +00:00
|
|
|
Town *ClosestTownFromTile(TileIndex tile, uint threshold)
|
|
|
|
{
|
2008-11-18 22:43:59 +00:00
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_ROAD:
|
2009-09-10 14:33:07 +00:00
|
|
|
if (IsRoadDepot(tile)) return CalcClosestTownFromTile(tile, threshold);
|
|
|
|
|
2008-11-18 22:43:59 +00:00
|
|
|
if (!HasTownOwnedRoad(tile)) {
|
|
|
|
TownID tid = GetTownIndex(tile);
|
2009-05-22 15:13:50 +00:00
|
|
|
|
2019-01-12 23:20:10 +00:00
|
|
|
if (tid == INVALID_TOWN) {
|
2008-11-18 22:43:59 +00:00
|
|
|
/* in the case we are generating "many random towns", this value may be INVALID_TOWN */
|
2008-11-19 16:53:24 +00:00
|
|
|
if (_generating_world) return CalcClosestTownFromTile(tile, threshold);
|
2009-05-22 15:23:47 +00:00
|
|
|
assert(Town::GetNumItems() == 0);
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2008-11-18 22:43:59 +00:00
|
|
|
}
|
2008-10-25 13:51:47 +00:00
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
assert(Town::IsValidID(tid));
|
2009-05-16 23:34:14 +00:00
|
|
|
Town *town = Town::Get(tid);
|
2008-11-18 22:43:59 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (DistanceManhattan(tile, town->xy) >= threshold) town = nullptr;
|
2008-11-18 22:43:59 +00:00
|
|
|
|
|
|
|
return town;
|
|
|
|
}
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2008-11-18 22:43:59 +00:00
|
|
|
|
|
|
|
case MP_HOUSE:
|
2009-06-26 13:46:23 +00:00
|
|
|
return Town::GetByTile(tile);
|
2008-11-18 22:43:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return CalcClosestTownFromTile(tile, threshold);
|
2006-06-08 18:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
static bool _town_rating_test = false; ///< If \c true, town rating is in test-mode.
|
2019-03-04 20:49:33 +00:00
|
|
|
static SmallMap<const Town *, int> _town_test_ratings; ///< Map of towns to modified ratings, while in town rating test-mode.
|
2008-01-09 17:47:05 +00:00
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/**
|
|
|
|
* Switch the town rating to test-mode, to allow commands to be tested without affecting current ratings.
|
|
|
|
* The function is safe to use in nested calls.
|
|
|
|
* @param mode Test mode switch (\c true means go to test-mode, \c false means leave test-mode).
|
|
|
|
*/
|
2008-01-09 17:47:05 +00:00
|
|
|
void SetTownRatingTestMode(bool mode)
|
|
|
|
{
|
2010-08-01 17:45:53 +00:00
|
|
|
static int ref_count = 0; // Number of times test-mode is switched on.
|
2008-01-09 17:47:05 +00:00
|
|
|
if (mode) {
|
|
|
|
if (ref_count == 0) {
|
2018-09-20 22:44:14 +00:00
|
|
|
_town_test_ratings.clear();
|
2008-01-09 17:47:05 +00:00
|
|
|
}
|
|
|
|
ref_count++;
|
|
|
|
} else {
|
|
|
|
assert(ref_count > 0);
|
|
|
|
ref_count--;
|
|
|
|
}
|
|
|
|
_town_rating_test = !(ref_count == 0);
|
|
|
|
}
|
2006-06-08 18:31:54 +00:00
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/**
|
|
|
|
* Get the rating of a town for the #_current_company.
|
|
|
|
* @param t Town to get the rating from.
|
|
|
|
* @return Rating of the current company in the given town.
|
|
|
|
*/
|
2008-04-23 22:55:11 +00:00
|
|
|
static int GetRating(const Town *t)
|
|
|
|
{
|
|
|
|
if (_town_rating_test) {
|
2008-12-26 13:50:46 +00:00
|
|
|
SmallMap<const Town *, int>::iterator it = _town_test_ratings.Find(t);
|
|
|
|
if (it != _town_test_ratings.End()) {
|
|
|
|
return it->second;
|
2008-04-23 22:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-30 20:39:50 +00:00
|
|
|
return t->ratings[_current_company];
|
2008-04-23 22:55:11 +00:00
|
|
|
}
|
|
|
|
|
2009-02-11 18:50:47 +00:00
|
|
|
/**
|
|
|
|
* Changes town rating of the current company
|
|
|
|
* @param t Town to affect
|
|
|
|
* @param add Value to add
|
2010-08-02 20:47:27 +00:00
|
|
|
* @param max Minimum (add < 0) resp. maximum (add > 0) rating that should be achievable with this change.
|
2009-02-11 18:50:47 +00:00
|
|
|
* @param flags Command flags, especially DC_NO_MODIFY_TOWN_RATING is tested
|
|
|
|
*/
|
|
|
|
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-02 20:47:27 +00:00
|
|
|
/* if magic_bulldozer cheat is active, town doesn't penalize for removing stuff */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr || (flags & DC_NO_MODIFY_TOWN_RATING) ||
|
2009-05-17 01:00:56 +00:00
|
|
|
!Company::IsValidID(_current_company) ||
|
2005-11-14 19:48:04 +00:00
|
|
|
(_cheats.magic_bulldozer.value && add < 0)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-23 22:55:11 +00:00
|
|
|
int rating = GetRating(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (add < 0) {
|
|
|
|
if (rating > max) {
|
|
|
|
rating += add;
|
|
|
|
if (rating < max) rating = max;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rating < max) {
|
|
|
|
rating += add;
|
|
|
|
if (rating > max) rating = max;
|
|
|
|
}
|
|
|
|
}
|
2008-01-09 17:47:05 +00:00
|
|
|
if (_town_rating_test) {
|
2008-04-23 22:55:11 +00:00
|
|
|
_town_test_ratings[t] = rating;
|
2008-01-09 17:47:05 +00:00
|
|
|
} else {
|
2009-03-10 06:53:00 +00:00
|
|
|
SetBit(t->have_ratings, _current_company);
|
2008-09-30 20:39:50 +00:00
|
|
|
t->ratings[_current_company] = rating;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_AUTHORITY, t->index);
|
2008-01-09 17:47:05 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-13 15:33:07 +00:00
|
|
|
/**
|
|
|
|
* Does the town authority allow the (destructive) action of the current company?
|
|
|
|
* @param flags Checking flags of the command.
|
|
|
|
* @param t Town that must allow the company action.
|
|
|
|
* @param type Type of action that is wanted.
|
|
|
|
* @return A succeeded command if the action is allowed, a failed command if it is not allowed.
|
|
|
|
*/
|
|
|
|
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-04 03:21:14 +00:00
|
|
|
/* if magic_bulldozer cheat is active, town doesn't restrict your destructive actions */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr || !Company::IsValidID(_current_company) ||
|
2009-05-11 11:55:41 +00:00
|
|
|
_cheats.magic_bulldozer.value || (flags & DC_NO_TEST_TOWN_RATING)) {
|
2010-03-13 15:33:07 +00:00
|
|
|
return CommandCost();
|
2009-05-11 11:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* minimum rating needed to be allowed to remove stuff */
|
|
|
|
static const int needed_rating[][TOWN_RATING_CHECK_TYPE_COUNT] = {
|
|
|
|
/* ROAD_REMOVE, TUNNELBRIDGE_REMOVE */
|
|
|
|
{ RATING_ROAD_NEEDED_PERMISSIVE, RATING_TUNNEL_BRIDGE_NEEDED_PERMISSIVE}, // Permissive
|
|
|
|
{ RATING_ROAD_NEEDED_NEUTRAL, RATING_TUNNEL_BRIDGE_NEEDED_NEUTRAL}, // Neutral
|
|
|
|
{ RATING_ROAD_NEEDED_HOSTILE, RATING_TUNNEL_BRIDGE_NEEDED_HOSTILE}, // Hostile
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-11 11:55:41 +00:00
|
|
|
/* check if you're allowed to remove the road/bridge/tunnel
|
2006-08-28 18:53:03 +00:00
|
|
|
* owned by a town no removal if rating is lower than ... depends now on
|
|
|
|
* difficulty setting. Minimum town rating selected by difficulty level
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-05-11 11:55:41 +00:00
|
|
|
int needed = needed_rating[_settings_game.difficulty.town_council_tolerance][type];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-11 11:55:41 +00:00
|
|
|
if (GetRating(t) < needed) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2010-03-13 15:33:07 +00:00
|
|
|
return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-13 15:33:07 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void TownsMonthlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2019-12-17 21:04:09 +00:00
|
|
|
for (Town *t : Town::Iterate()) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t->road_build_months != 0) t->road_build_months--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-07-24 10:14:39 +00:00
|
|
|
if (t->exclusive_counter != 0) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (--t->exclusive_counter == 0) t->exclusivity = INVALID_COMPANY;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-08-23 21:04:39 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownAmounts(t);
|
2018-06-24 18:55:48 +00:00
|
|
|
UpdateTownGrowth(t);
|
2011-11-23 16:07:14 +00:00
|
|
|
UpdateTownRating(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownUnwanted(t);
|
|
|
|
}
|
2011-12-03 22:23:10 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-11-23 14:17:41 +00:00
|
|
|
void TownsYearlyLoop()
|
|
|
|
{
|
|
|
|
/* Increment house ages */
|
|
|
|
for (TileIndex t = 0; t < MapSize(); t++) {
|
|
|
|
if (!IsTileType(t, MP_HOUSE)) continue;
|
|
|
|
IncrementHouseAge(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 11:36:10 +00:00
|
|
|
static CommandCost TerraformTile_Town(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
|
2007-08-30 17:17:04 +00:00
|
|
|
{
|
2007-09-14 22:27:40 +00:00
|
|
|
if (AutoslopeEnabled()) {
|
|
|
|
HouseID house = GetHouseType(tile);
|
2008-04-15 13:10:01 +00:00
|
|
|
GetHouseNorthPart(house); // modifies house to the ID of the north tile
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2007-09-14 22:27:40 +00:00
|
|
|
|
|
|
|
/* Here we differ from TTDP by checking TILE_NOT_SLOPED */
|
|
|
|
if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) &&
|
2011-11-04 10:30:10 +00:00
|
|
|
(GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
|
2009-09-16 19:10:50 +00:00
|
|
|
bool allow_terraform = true;
|
|
|
|
|
|
|
|
/* Call the autosloping callback per tile, not for the whole building at once. */
|
|
|
|
house = GetHouseType(tile);
|
|
|
|
hs = HouseSpec::Get(house);
|
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_AUTOSLOPE)) {
|
|
|
|
/* If the callback fails, allow autoslope. */
|
|
|
|
uint16 res = GetHouseCallback(CBID_HOUSE_AUTOSLOPE, 0, 0, house, Town::GetByTile(tile), tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (res != CALLBACK_FAILED && ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_AUTOSLOPE, res)) allow_terraform = false;
|
2009-09-16 19:10:50 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
if (allow_terraform) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
2009-09-16 19:10:50 +00:00
|
|
|
}
|
2007-09-14 22:27:40 +00:00
|
|
|
}
|
|
|
|
|
2007-08-30 17:17:04 +00:00
|
|
|
return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
|
2008-10-19 15:39:12 +00:00
|
|
|
/** Tile callback functions for a town */
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const TileTypeProcs _tile_type_town_procs = {
|
2008-10-19 09:51:19 +00:00
|
|
|
DrawTile_Town, // draw_tile_proc
|
2011-11-04 10:18:13 +00:00
|
|
|
GetSlopePixelZ_Town, // get_slope_z_proc
|
2008-10-19 09:51:19 +00:00
|
|
|
ClearTile_Town, // clear_tile_proc
|
2009-06-25 19:23:09 +00:00
|
|
|
AddAcceptedCargo_Town, // add_accepted_cargo_proc
|
2008-10-19 09:51:19 +00:00
|
|
|
GetTileDesc_Town, // get_tile_desc_proc
|
|
|
|
GetTileTrackStatus_Town, // get_tile_track_status_proc
|
2019-04-10 21:07:06 +00:00
|
|
|
nullptr, // click_tile_proc
|
2008-10-19 09:51:19 +00:00
|
|
|
AnimateTile_Town, // animate_tile_proc
|
2011-11-08 17:37:32 +00:00
|
|
|
TileLoop_Town, // tile_loop_proc
|
|
|
|
ChangeTileOwner_Town, // change_tile_owner_proc
|
2009-06-27 17:05:20 +00:00
|
|
|
AddProducedCargo_Town, // add_produced_cargo_proc
|
2019-04-10 21:07:06 +00:00
|
|
|
nullptr, // vehicle_enter_tile_proc
|
2008-10-19 09:51:19 +00:00
|
|
|
GetFoundation_Town, // get_foundation_proc
|
|
|
|
TerraformTile_Town, // terraform_tile_proc
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
|
2013-10-12 16:33:19 +00:00
|
|
|
HouseSpec _house_specs[NUM_HOUSES];
|
2009-06-26 14:02:27 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
void ResetHouses()
|
|
|
|
{
|
|
|
|
memset(&_house_specs, 0, sizeof(_house_specs));
|
|
|
|
memcpy(&_house_specs, &_original_house_specs, sizeof(_original_house_specs));
|
2007-10-03 00:57:54 +00:00
|
|
|
|
|
|
|
/* Reset any overrides that have been set. */
|
|
|
|
_house_mngr.ResetOverride();
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|