2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file town.h Base of the town class. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef TOWN_H
|
|
|
|
#define TOWN_H
|
|
|
|
|
2009-05-22 15:39:22 +00:00
|
|
|
#include "core/pool_type.hpp"
|
2008-05-07 13:10:15 +00:00
|
|
|
#include "viewport_type.h"
|
2009-02-09 21:20:05 +00:00
|
|
|
#include "command_type.h"
|
2009-06-26 13:44:14 +00:00
|
|
|
#include "town_map.h"
|
2009-08-08 16:42:55 +00:00
|
|
|
#include "subsidy_type.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2009-03-17 19:40:10 +00:00
|
|
|
template <typename T>
|
2007-03-19 11:27:30 +00:00
|
|
|
struct BuildingCounts {
|
2009-03-17 19:40:10 +00:00
|
|
|
T id_count[HOUSE_MAX];
|
|
|
|
T class_count[HOUSE_CLASS_MAX];
|
2006-12-09 14:18:08 +00:00
|
|
|
};
|
|
|
|
|
2009-03-13 00:48:03 +00:00
|
|
|
static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY = 4; ///< value for custom town number in difficulty settings
|
|
|
|
static const uint CUSTOM_TOWN_MAX_NUMBER = 5000; ///< this is the maximum number of towns a user can specify in customisation
|
2009-03-12 23:54:20 +00:00
|
|
|
|
2009-06-26 13:44:14 +00:00
|
|
|
static const uint INVALID_TOWN = 0xFFFF;
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
typedef Pool<Town, TownID, 64, 64000> TownPool;
|
|
|
|
extern TownPool _town_pool;
|
2007-08-02 21:05:54 +00:00
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/** Town data structure. */
|
2009-05-22 15:13:50 +00:00
|
|
|
struct Town : TownPool::PoolItem<&_town_pool> {
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex xy;
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Current population of people and amount of houses. */
|
2008-03-18 12:28:21 +00:00
|
|
|
uint32 num_houses;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 population;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Town name */
|
2007-06-18 23:00:55 +00:00
|
|
|
uint32 townnamegrfid;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 townnametype;
|
|
|
|
uint32 townnameparts;
|
2008-01-12 19:58:06 +00:00
|
|
|
char *name;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
/* NOSAVE: Location of name sign, UpdateVirtCoord updates this. */
|
2004-08-09 17:04:08 +00:00
|
|
|
ViewportSign sign;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Makes sure we don't build certain house types twice.
|
|
|
|
* bit 0 = Building funds received
|
|
|
|
* bit 1 = CHURCH
|
|
|
|
* bit 2 = STADIUM */
|
2009-06-23 09:52:26 +00:00
|
|
|
byte flags;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-24 02:54:47 +00:00
|
|
|
/* level of noise that all the airports are generating */
|
|
|
|
uint16 noise_reached;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Which companies have a statue? */
|
2008-12-23 21:03:43 +00:00
|
|
|
CompanyMask statues;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Company ratings as well as a mask that determines which companies have a rating. */
|
|
|
|
CompanyMask have_ratings;
|
|
|
|
uint8 unwanted[MAX_COMPANIES]; ///< how many months companies aren't wanted by towns (bribe)
|
|
|
|
CompanyByte exclusivity; ///< which company has exclusivity
|
|
|
|
uint8 exclusive_counter; ///< months till the exclusivity expires
|
2010-08-01 17:45:53 +00:00
|
|
|
int16 ratings[MAX_COMPANIES]; ///< Ratings of each company for this town.
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Maximum amount of passengers and mail that can be transported. */
|
2005-02-17 10:56:19 +00:00
|
|
|
uint32 max_pass;
|
|
|
|
uint32 max_mail;
|
|
|
|
uint32 new_max_pass;
|
|
|
|
uint32 new_max_mail;
|
|
|
|
uint32 act_pass;
|
|
|
|
uint32 act_mail;
|
|
|
|
uint32 new_act_pass;
|
|
|
|
uint32 new_act_mail;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Amount of passengers that were transported. */
|
2004-08-09 17:04:08 +00:00
|
|
|
byte pct_pass_transported;
|
|
|
|
byte pct_mail_transported;
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Amount of food and paper that was transported. Actually a bit mask would be enough. */
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 act_food;
|
2004-08-10 14:42:52 +00:00
|
|
|
uint16 act_water;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 new_act_food;
|
2004-08-10 14:42:52 +00:00
|
|
|
uint16 new_act_water;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Time until we rebuild a house. */
|
2007-04-12 17:24:34 +00:00
|
|
|
uint16 time_until_rebuild;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* When to grow town next time. */
|
2007-04-12 17:24:34 +00:00
|
|
|
uint16 grow_counter;
|
|
|
|
int16 growth_rate;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Fund buildings program in action? */
|
2004-08-09 17:04:08 +00:00
|
|
|
byte fund_buildings_months;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* Fund road reconstruction in action? */
|
2004-08-09 17:04:08 +00:00
|
|
|
byte road_build_months;
|
|
|
|
|
2007-04-18 14:23:30 +00:00
|
|
|
/* If this is a larger town, and should grow more quickly. */
|
|
|
|
bool larger_town;
|
2009-02-04 20:17:25 +00:00
|
|
|
TownLayoutByte layout; ///< town specific road layout
|
2007-04-18 14:23:30 +00:00
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this town a source/destination of a subsidy?
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/* NOSAVE: UpdateTownRadius updates this given the house count. */
|
2008-04-15 22:27:28 +00:00
|
|
|
uint32 squared_town_zone_radius[HZB_END];
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
BuildingCounts<uint16> building_counts; ///< NOSAVE: The number of each type of building in the town.
|
2007-08-02 21:05:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new town
|
|
|
|
*/
|
2009-05-22 15:13:50 +00:00
|
|
|
Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
|
2007-08-02 21:05:54 +00:00
|
|
|
|
|
|
|
/** Destroy the town */
|
|
|
|
~Town();
|
|
|
|
|
2009-02-04 22:52:34 +00:00
|
|
|
void InitializeLayout(TownLayout layout);
|
2008-04-01 16:27:01 +00:00
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/**
|
|
|
|
* Calculate the max town noise.
|
2008-05-24 02:54:47 +00:00
|
|
|
* The value is counted using the population divided by the content of the
|
2010-08-01 17:45:53 +00:00
|
|
|
* entry in town_noise_population corresponding to the town's tolerance.
|
|
|
|
* @return the maximum noise level the town will tolerate.
|
|
|
|
*/
|
2009-02-04 20:17:25 +00:00
|
|
|
inline uint16 MaxTownNoise() const
|
|
|
|
{
|
2008-05-24 02:54:47 +00:00
|
|
|
if (this->population == 0) return 0; // no population? no noise
|
|
|
|
|
2010-08-01 17:45:53 +00:00
|
|
|
/* 3 is added (the noise of the lowest airport), so the user can at least build a small airfield. */
|
|
|
|
return (this->population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
|
2008-05-24 02:54:47 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
void UpdateVirtCoord();
|
|
|
|
|
2009-06-26 13:46:23 +00:00
|
|
|
static FORCEINLINE Town *GetByTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
return Town::Get(GetTownIndex(tile));
|
|
|
|
}
|
2009-06-26 15:08:54 +00:00
|
|
|
|
|
|
|
static Town *GetRandom();
|
2009-07-05 13:21:51 +00:00
|
|
|
static void PostDestructor(size_t index);
|
2009-06-26 13:46:23 +00:00
|
|
|
};
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
uint32 GetWorldPopulation();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-25 20:20:16 +00:00
|
|
|
void UpdateAllTownVirtCoords();
|
2006-03-26 22:41:56 +00:00
|
|
|
void ShowTownViewWindow(TownID town);
|
2004-08-09 17:04:08 +00:00
|
|
|
void ExpandTown(Town *t);
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Action types that a company must ask permission for to a town authority.
|
2010-03-13 15:13:19 +00:00
|
|
|
* @see CheckforTownRating
|
|
|
|
*/
|
2009-05-11 11:55:41 +00:00
|
|
|
enum TownRatingCheckType {
|
2010-03-13 15:13:19 +00:00
|
|
|
ROAD_REMOVE = 0, ///< Removal of a road owned by the town.
|
|
|
|
TUNNELBRIDGE_REMOVE = 1, ///< Removal of a tunnel or bridge owned by the towb.
|
|
|
|
TOWN_RATING_CHECK_TYPE_COUNT, ///< Number of town checking action types.
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* This is the number of ticks between towns being processed for building new
|
2007-04-12 17:24:34 +00:00
|
|
|
* houses or roads. This value originally came from the size of the town array
|
2010-08-01 19:44:49 +00:00
|
|
|
* in TTD.
|
|
|
|
*/
|
2007-04-12 17:24:34 +00:00
|
|
|
static const byte TOWN_GROWTH_FREQUENCY = 70;
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2010-08-02 20:47:27 +00:00
|
|
|
* This enum is used in conjunction with town->flags.
|
2006-04-03 14:56:07 +00:00
|
|
|
* IT simply states what bit is used for.
|
|
|
|
* It is pretty unrealistic (IMHO) to only have one church/stadium
|
|
|
|
* per town, NO MATTER the population of it.
|
2009-06-23 09:52:26 +00:00
|
|
|
* And there are 5 more bits available on flags...
|
2006-04-03 14:56:07 +00:00
|
|
|
*/
|
2010-05-13 09:44:44 +00:00
|
|
|
enum TownFlags {
|
2007-04-04 03:21:14 +00:00
|
|
|
TOWN_IS_FUNDED = 0, ///< Town has received some funds for
|
|
|
|
TOWN_HAS_CHURCH = 1, ///< There can be only one church by town.
|
|
|
|
TOWN_HAS_STADIUM = 2 ///< There can be only one stadium by town.
|
2005-01-14 09:20:12 +00:00
|
|
|
};
|
|
|
|
|
2010-03-13 15:33:07 +00:00
|
|
|
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-10-20 19:35:48 +00:00
|
|
|
TileIndexDiff GetHouseNorthPart(HouseID &house);
|
2008-06-25 20:04:27 +00:00
|
|
|
|
2009-07-05 13:21:51 +00:00
|
|
|
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
|
2007-02-09 16:21:03 +00:00
|
|
|
|
2009-05-22 14:23:36 +00:00
|
|
|
#define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
|
|
|
|
#define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
void ResetHouses();
|
|
|
|
|
|
|
|
void ClearTownHouse(Town *t, TileIndex tile);
|
2007-12-25 11:26:07 +00:00
|
|
|
void UpdateTownMaxPass(Town *t);
|
2008-03-18 12:28:21 +00:00
|
|
|
void UpdateTownRadius(Town *t);
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
|
2007-12-25 11:26:07 +00:00
|
|
|
Town *ClosestTownFromTile(TileIndex tile, uint threshold);
|
2009-02-11 18:50:47 +00:00
|
|
|
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
|
2009-01-10 00:31:47 +00:00
|
|
|
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
|
2008-01-09 17:47:05 +00:00
|
|
|
void SetTownRatingTestMode(bool mode);
|
2009-01-19 12:07:01 +00:00
|
|
|
uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
|
2009-02-04 22:52:34 +00:00
|
|
|
bool GenerateTowns(TownLayout layout);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2009-09-20 10:08:10 +00:00
|
|
|
|
|
|
|
/** Town actions of a company. */
|
|
|
|
enum TownActions {
|
|
|
|
TACT_NONE = 0x00, ///< Empty action set.
|
|
|
|
|
|
|
|
TACT_ADVERTISE_SMALL = 0x01, ///< Small advertising campaign.
|
|
|
|
TACT_ADVERTISE_MEDIUM = 0x02, ///< Medium advertising campaign.
|
|
|
|
TACT_ADVERTISE_LARGE = 0x04, ///< Large advertising campaign.
|
|
|
|
TACT_ROAD_REBUILD = 0x08, ///< Rebuild the roads.
|
|
|
|
TACT_BUILD_STATUE = 0x10, ///< Build a statue.
|
|
|
|
TACT_FOUND_BUILDINGS = 0x20, ///< Found new buildings.
|
|
|
|
TACT_BUY_RIGHTS = 0x40, ///< Buy exclusive transport rights.
|
|
|
|
TACT_BRIBE = 0x80, ///< Try to bribe the counsil.
|
|
|
|
|
|
|
|
TACT_COUNT = 8, ///< Number of available town actions.
|
|
|
|
|
|
|
|
TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, ///< All possible advertising actions.
|
|
|
|
TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS, ///< All possible construction actions.
|
|
|
|
TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, ///< All possible funding actions.
|
|
|
|
TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS, ///< All possible actions.
|
|
|
|
};
|
2010-03-23 22:25:43 +00:00
|
|
|
DECLARE_ENUM_AS_BIT_SET(TownActions)
|
2009-09-20 10:08:10 +00:00
|
|
|
|
|
|
|
extern const byte _town_action_costs[TACT_COUNT];
|
2009-09-21 20:22:56 +00:00
|
|
|
extern TownID _new_town_id;
|
2009-09-20 10:08:10 +00:00
|
|
|
|
2010-05-12 18:31:39 +00:00
|
|
|
/**
|
|
|
|
* Set the default name for a depot/waypoint
|
|
|
|
* @tparam T The type/class to make a default name for
|
|
|
|
* @param obj The object/instance we want to find the name for
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
void MakeDefaultName(T *obj)
|
|
|
|
{
|
2010-09-16 16:31:57 +00:00
|
|
|
/* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
|
|
|
|
assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
|
2010-05-12 18:31:39 +00:00
|
|
|
|
|
|
|
obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
|
|
|
|
|
|
|
|
/* Find first unused number belonging to this town. This can never fail,
|
|
|
|
* as long as there can be at most 65535 waypoints/depots in total.
|
|
|
|
*
|
|
|
|
* This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
|
|
|
|
* most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
|
|
|
|
* m - number of waypoints near this town).
|
|
|
|
* Usually, it needs only 'n' steps.
|
|
|
|
*
|
|
|
|
* If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
|
|
|
|
* but this way it is faster */
|
|
|
|
|
|
|
|
uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
|
|
|
|
uint32 next = 0; // first number in the bitmap
|
|
|
|
uint32 idx = 0; // index where we will stop
|
|
|
|
uint32 cid = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
|
|
|
|
|
|
|
|
do {
|
|
|
|
T *lobj = T::GetIfValid(cid);
|
|
|
|
|
|
|
|
/* check only valid waypoints... */
|
|
|
|
if (lobj != NULL && obj != lobj) {
|
2010-09-16 16:31:57 +00:00
|
|
|
/* only objects within the same city and with the same type */
|
|
|
|
if (lobj->town == obj->town && lobj->IsOfType(obj)) {
|
2010-05-12 18:31:39 +00:00
|
|
|
/* if lobj->town_cn < next, uint will overflow to '+inf' */
|
|
|
|
uint i = (uint)lobj->town_cn - next;
|
|
|
|
|
|
|
|
if (i < 32) {
|
|
|
|
SetBit(used, i); // update bitmap
|
|
|
|
if (i == 0) {
|
|
|
|
/* shift bitmap while the lowest bit is '1';
|
|
|
|
* increase the base of the bitmap too */
|
|
|
|
do {
|
|
|
|
used >>= 1;
|
|
|
|
next++;
|
|
|
|
} while (HasBit(used, 0));
|
|
|
|
/* when we are at 'idx' again at end of the loop and
|
|
|
|
* 'next' hasn't changed, then no object had town_cn == next,
|
|
|
|
* so we can safely use it */
|
|
|
|
idx = cid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cid++;
|
|
|
|
if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
|
|
|
|
} while (cid != idx);
|
|
|
|
|
|
|
|
obj->town_cn = (uint16)next; // set index...
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* TOWN_H */
|