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 industry.h Base of all industries. */
|
2007-03-03 04:04:22 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef INDUSTRY_H
|
|
|
|
#define INDUSTRY_H
|
|
|
|
|
2007-09-22 13:56:38 +00:00
|
|
|
#include "newgrf_storage.h"
|
2009-08-08 16:42:55 +00:00
|
|
|
#include "subsidy_type.h"
|
2009-08-30 11:47:41 +00:00
|
|
|
#include "industry_map.h"
|
2018-07-25 17:20:17 +00:00
|
|
|
#include "industrytype.h"
|
2010-01-04 18:21:07 +00:00
|
|
|
#include "tilearea_type.h"
|
2019-03-08 18:30:44 +00:00
|
|
|
#include "station_base.h"
|
2006-04-28 21:58:16 +00:00
|
|
|
|
2007-03-28 20:06:28 +00:00
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
|
|
|
|
extern IndustryPool _industry_pool;
|
2007-08-02 23:21:52 +00:00
|
|
|
|
2023-04-24 19:58:48 +00:00
|
|
|
static const Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS = 5; ///< If a processing industry doesn't produce for this many consecutive years, it may close.
|
|
|
|
|
2010-10-04 20:10:31 +00:00
|
|
|
/**
|
|
|
|
* Production level maximum, minimum and default values.
|
|
|
|
* It is not a value been really used in order to change, but rather an indicator
|
|
|
|
* of how the industry is behaving.
|
|
|
|
*/
|
|
|
|
enum ProductionLevels {
|
|
|
|
PRODLEVEL_CLOSURE = 0x00, ///< signal set to actually close the industry
|
|
|
|
PRODLEVEL_MINIMUM = 0x04, ///< below this level, the industry is set to be closing
|
|
|
|
PRODLEVEL_DEFAULT = 0x10, ///< default level set when the industry is created
|
|
|
|
PRODLEVEL_MAXIMUM = 0x80, ///< the industry is running at full speed
|
|
|
|
};
|
|
|
|
|
2020-12-22 13:21:31 +00:00
|
|
|
/**
|
|
|
|
* Flags to control/override the behaviour of an industry.
|
|
|
|
* These flags are controlled by game scripts.
|
|
|
|
*/
|
|
|
|
enum IndustryControlFlags : byte {
|
|
|
|
/** No flags in effect */
|
|
|
|
INDCTL_NONE = 0,
|
|
|
|
/** When industry production change is evaluated, rolls to decrease are ignored. */
|
|
|
|
INDCTL_NO_PRODUCTION_DECREASE = 1 << 0,
|
|
|
|
/** When industry production change is evaluated, rolls to increase are ignored. */
|
|
|
|
INDCTL_NO_PRODUCTION_INCREASE = 1 << 1,
|
|
|
|
/**
|
|
|
|
* Industry can not close regardless of production level or time since last delivery.
|
|
|
|
* This does not prevent a closure already announced. */
|
|
|
|
INDCTL_NO_CLOSURE = 1 << 2,
|
|
|
|
/** Mask of all flags set */
|
|
|
|
INDCTL_MASK = INDCTL_NO_PRODUCTION_DECREASE | INDCTL_NO_PRODUCTION_INCREASE | INDCTL_NO_CLOSURE,
|
|
|
|
};
|
|
|
|
DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags);
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/**
|
2010-11-20 13:33:24 +00:00
|
|
|
* Defines the internal data of a functional industry.
|
2007-03-03 04:04:22 +00:00
|
|
|
*/
|
2009-05-22 15:13:50 +00:00
|
|
|
struct Industry : IndustryPool::PoolItem<&_industry_pool> {
|
2018-07-25 17:20:17 +00:00
|
|
|
TileArea location; ///< Location of the industry
|
|
|
|
Town *town; ///< Nearest town
|
2019-03-08 18:30:44 +00:00
|
|
|
Station *neutral_station; ///< Associated neutral station
|
2018-07-25 17:20:17 +00:00
|
|
|
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]; ///< 16 production cargo slots
|
|
|
|
uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]; ///< amount of cargo produced per cargo
|
|
|
|
uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]; ///< incoming cargo waiting to be processed
|
|
|
|
byte production_rate[INDUSTRY_NUM_OUTPUTS]; ///< production rate for each cargo
|
|
|
|
byte prod_level; ///< general production level
|
|
|
|
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]; ///< 16 input cargo slots
|
|
|
|
uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]; ///< stats of this month's production per cargo
|
|
|
|
uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]; ///< stats of this month's transport per cargo
|
|
|
|
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]; ///< percentage transported per cargo in the last full month
|
|
|
|
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]; ///< total units produced per cargo in the last full month
|
|
|
|
uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]; ///< total units transported per cargo in the last full month
|
|
|
|
uint16 counter; ///< used for animation and/or production (if available cargo)
|
2007-06-07 14:38:45 +00:00
|
|
|
|
2019-04-22 09:53:40 +00:00
|
|
|
IndustryType type; ///< type of industry.
|
|
|
|
Owner owner; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE
|
|
|
|
byte random_colour; ///< randomized colour of the industry, for display purpose
|
|
|
|
Year last_prod_year; ///< last year of production
|
|
|
|
byte was_cargo_delivered; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
|
2020-12-22 13:21:31 +00:00
|
|
|
IndustryControlFlags ctlflags; ///< flags overriding standard behaviours
|
2019-04-22 09:53:40 +00:00
|
|
|
|
|
|
|
PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
|
|
|
|
StationList stations_near; ///< NOSAVE: List of nearby stations.
|
2020-01-06 20:40:31 +00:00
|
|
|
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the industry
|
2019-04-22 09:53:40 +00:00
|
|
|
|
|
|
|
Owner founder; ///< Founder of the industry
|
|
|
|
Date construction_date; ///< Date of the construction of the industry
|
|
|
|
uint8 construction_type; ///< Way the industry was constructed (@see IndustryConstructionType)
|
2018-07-26 17:29:54 +00:00
|
|
|
Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]; ///< Last day each cargo type was accepted by this industry
|
2019-04-22 09:53:40 +00:00
|
|
|
byte selected_layout; ///< Which tile layout was used when creating the industry
|
2020-12-22 13:29:48 +00:00
|
|
|
Owner exclusive_supplier; ///< Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
|
|
|
|
Owner exclusive_consumer; ///< Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
|
2021-01-15 14:38:14 +00:00
|
|
|
std::string text; ///< General text with additional information.
|
2007-08-02 23:21:52 +00:00
|
|
|
|
2019-04-22 09:53:40 +00:00
|
|
|
uint16 random; ///< Random value used for randomisation of all kinds of things
|
2007-11-11 17:56:37 +00:00
|
|
|
|
2019-04-22 09:53:40 +00:00
|
|
|
PersistentStorage *psa; ///< Persistent storage for NewGRF industries.
|
2007-09-22 13:56:38 +00:00
|
|
|
|
2010-01-04 18:21:07 +00:00
|
|
|
Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
|
2007-08-02 23:21:52 +00:00
|
|
|
~Industry();
|
2009-06-26 15:08:54 +00:00
|
|
|
|
2010-10-04 19:23:50 +00:00
|
|
|
void RecomputeProductionMultipliers();
|
|
|
|
|
2011-12-09 16:11:42 +00:00
|
|
|
/**
|
|
|
|
* Check if a given tile belongs to this industry.
|
|
|
|
* @param tile The tile to check.
|
2013-01-08 22:46:42 +00:00
|
|
|
* @return True if the tile is part of this industry.
|
2011-12-09 16:11:42 +00:00
|
|
|
*/
|
|
|
|
inline bool TileBelongsToIndustry(TileIndex tile) const
|
|
|
|
{
|
|
|
|
return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:29:54 +00:00
|
|
|
inline int GetCargoProducedIndex(CargoID cargo) const
|
|
|
|
{
|
|
|
|
if (cargo == CT_INVALID) return -1;
|
|
|
|
const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo);
|
|
|
|
if (pos == endof(this->produced_cargo)) return -1;
|
|
|
|
return pos - this->produced_cargo;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int GetCargoAcceptedIndex(CargoID cargo) const
|
|
|
|
{
|
|
|
|
if (cargo == CT_INVALID) return -1;
|
|
|
|
const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo);
|
|
|
|
if (pos == endof(this->accepts_cargo)) return -1;
|
|
|
|
return pos - this->accepts_cargo;
|
|
|
|
}
|
|
|
|
|
2009-08-30 11:47:41 +00:00
|
|
|
/**
|
|
|
|
* Get the industry of the given tile
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param tile the tile to get the industry from
|
2009-08-30 11:47:41 +00:00
|
|
|
* @pre IsTileType(t, MP_INDUSTRY)
|
|
|
|
* @return the industry
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline Industry *GetByTile(TileIndex tile)
|
2009-08-30 11:47:41 +00:00
|
|
|
{
|
|
|
|
return Industry::Get(GetIndustryIndex(tile));
|
|
|
|
}
|
|
|
|
|
2009-06-26 15:08:54 +00:00
|
|
|
static Industry *GetRandom();
|
2009-09-08 12:22:28 +00:00
|
|
|
static void PostDestructor(size_t index);
|
2010-03-20 14:30:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment the count of industries for this type.
|
|
|
|
* @param type IndustryType to increment
|
|
|
|
* @pre type < NUM_INDUSTRYTYPES
|
|
|
|
*/
|
|
|
|
static inline void IncIndustryTypeCount(IndustryType type)
|
|
|
|
{
|
|
|
|
assert(type < NUM_INDUSTRYTYPES);
|
|
|
|
counts[type]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrement the count of industries for this type.
|
|
|
|
* @param type IndustryType to decrement
|
|
|
|
* @pre type < NUM_INDUSTRYTYPES
|
|
|
|
*/
|
|
|
|
static inline void DecIndustryTypeCount(IndustryType type)
|
|
|
|
{
|
|
|
|
assert(type < NUM_INDUSTRYTYPES);
|
|
|
|
counts[type]--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the count of industries for this type.
|
|
|
|
* @param type IndustryType to query
|
|
|
|
* @pre type < NUM_INDUSTRYTYPES
|
|
|
|
*/
|
|
|
|
static inline uint16 GetIndustryTypeCount(IndustryType type)
|
|
|
|
{
|
|
|
|
assert(type < NUM_INDUSTRYTYPES);
|
|
|
|
return counts[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resets industry counts. */
|
|
|
|
static inline void ResetIndustryCounts()
|
|
|
|
{
|
|
|
|
memset(&counts, 0, sizeof(counts));
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:40:31 +00:00
|
|
|
inline const char *GetCachedName() const
|
|
|
|
{
|
|
|
|
if (this->cached_name.empty()) this->FillCachedName();
|
|
|
|
return this->cached_name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FillCachedName() const;
|
|
|
|
|
2010-03-20 14:30:16 +00:00
|
|
|
protected:
|
|
|
|
static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2020-01-06 20:40:31 +00:00
|
|
|
void ClearAllIndustryCachedNames();
|
|
|
|
|
2007-05-18 14:34:15 +00:00
|
|
|
void PlantRandomFarmField(const Industry *i);
|
2006-04-28 21:58:16 +00:00
|
|
|
|
2008-12-26 19:37:50 +00:00
|
|
|
void ReleaseDisastersTargetingIndustry(IndustryID);
|
|
|
|
|
2011-10-21 19:10:35 +00:00
|
|
|
bool IsTileForestIndustry(TileIndex tile);
|
|
|
|
|
2010-11-13 15:04:58 +00:00
|
|
|
/** Data for managing the number of industries of a single industry type. */
|
|
|
|
struct IndustryTypeBuildData {
|
|
|
|
uint32 probability; ///< Relative probability of building this industry.
|
2010-11-13 15:13:14 +00:00
|
|
|
byte min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
|
2010-11-13 15:07:34 +00:00
|
|
|
uint16 target_count; ///< Desired number of industries of this type.
|
2010-11-13 15:13:14 +00:00
|
|
|
uint16 max_wait; ///< Starting number of turns to wait (copied to #wait_count).
|
|
|
|
uint16 wait_count; ///< Number of turns to wait before trying to build again.
|
2010-11-13 15:04:58 +00:00
|
|
|
|
2010-11-13 15:15:25 +00:00
|
|
|
void Reset();
|
|
|
|
|
2010-11-13 15:19:43 +00:00
|
|
|
bool GetIndustryTypeData(IndustryType it);
|
2010-11-13 15:04:58 +00:00
|
|
|
};
|
|
|
|
|
2010-11-13 15:02:31 +00:00
|
|
|
/**
|
|
|
|
* Data for managing the number and type of industries in the game.
|
|
|
|
*/
|
|
|
|
struct IndustryBuildData {
|
2010-11-13 15:04:58 +00:00
|
|
|
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
|
2010-11-13 15:13:14 +00:00
|
|
|
uint32 wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
|
2010-11-13 15:04:58 +00:00
|
|
|
|
2010-11-13 15:15:25 +00:00
|
|
|
void Reset();
|
|
|
|
|
2010-11-13 15:07:34 +00:00
|
|
|
void SetupTargetCount();
|
2010-11-13 15:02:31 +00:00
|
|
|
void TryBuildNewIndustry();
|
2010-11-13 15:17:55 +00:00
|
|
|
|
|
|
|
void MonthlyLoop();
|
2010-11-13 15:02:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern IndustryBuildData _industry_builder;
|
|
|
|
|
2020-01-06 20:31:57 +00:00
|
|
|
|
|
|
|
/** Special values for the industry list window for the data parameter of #InvalidateWindowData. */
|
|
|
|
enum IndustryDirectoryInvalidateWindowData {
|
|
|
|
IDIWD_FORCE_REBUILD,
|
|
|
|
IDIWD_PRODUCTION_CHANGE,
|
|
|
|
IDIWD_FORCE_RESORT,
|
|
|
|
};
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
#endif /* INDUSTRY_H */
|