Files @ r17613:a9b2554a5d79
Branch filter:

Location: cpp/openttd-patchpack/source/src/industry.h - annotation

rubidium
(svn r22387) -Fix-ish [FS#4601]: Windows' recv seems to return "graceful closed" before having passed the remaining buffer which causes OpenTTD to think all connections are "incorrectly" terminated, i.e. without the "I'm leaving" packet from the client. So let the client wait a tiny bit after sending the "I'm leaving" packet and before gracefully closing the connection
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6201:2e76eb9a1d7a
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11969:3526c3cc75dc
r7610:6031d09c03ef
r12648:24c912bd53f6
r12816:33099b7286d4
r14159:6cb75763eb03
r5475:3f5cd13d1b63
r6390:c8b3b64c49ff
r11967:df0600d2c7e7
r11967:df0600d2c7e7
r7390:444ff4a56f72
r6201:2e76eb9a1d7a
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16190:5ca23ce41763
r16531:d1a9e93ee01b
r6201:2e76eb9a1d7a
r11967:df0600d2c7e7
r14890:f3cb6e331a95
r7610:6031d09c03ef
r14159:6cb75763eb03
r6819:52478124ff4e
r7645:e35d73ba358a
r7165:b30ba7925dc4
r7165:b30ba7925dc4
r6819:52478124ff4e
r6819:52478124ff4e
r7645:e35d73ba358a
r6819:52478124ff4e
r6819:52478124ff4e
r6819:52478124ff4e
r6819:52478124ff4e
r6819:52478124ff4e
r6819:52478124ff4e
r5475:3f5cd13d1b63
r7319:94412b8dacf8
r6819:52478124ff4e
r11085:c87a330fb4c2
r6819:52478124ff4e
r6819:52478124ff4e
r5475:3f5cd13d1b63
r12648:24c912bd53f6
r12648:24c912bd53f6
r7186:dea62b7d11ec
r7186:dea62b7d11ec
r7186:dea62b7d11ec
r7186:dea62b7d11ec
r7445:0ebcc38f1de8
r7390:444ff4a56f72
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7610:6031d09c03ef
r7610:6031d09c03ef
r14159:6cb75763eb03
r7390:444ff4a56f72
r12242:f11b2e8973ab
r16186:88dee3f13edf
r16186:88dee3f13edf
r12816:33099b7286d4
r12816:33099b7286d4
r13060:e57594b0ca84
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12816:33099b7286d4
r12242:f11b2e8973ab
r12968:8f3f0d6af8a6
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r14875:c6c6704d8c04
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6637:08ab9ae07700
r5475:3f5cd13d1b63
r10491:e3e4bb1ebaea
r10491:e3e4bb1ebaea
r11966:921bea0ca112
r11966:921bea0ca112
r5475:3f5cd13d1b63
r16441:6b2cb101747b
r16441:6b2cb101747b
r16441:6b2cb101747b
r16444:36b3473785ee
r16442:65c76775ce4b
r16444:36b3473785ee
r16444:36b3473785ee
r16441:6b2cb101747b
r16445:78655b2f7c0d
r16445:78655b2f7c0d
r16447:4063bb00c470
r16441:6b2cb101747b
r16441:6b2cb101747b
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r16441:6b2cb101747b
r16444:36b3473785ee
r16441:6b2cb101747b
r16445:78655b2f7c0d
r16445:78655b2f7c0d
r16442:65c76775ce4b
r16440:1bdd98dfbb9e
r16446:9455c82d3bdb
r16446:9455c82d3bdb
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r16440:1bdd98dfbb9e
r5475:3f5cd13d1b63
/* $Id$ */

/*
 * 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/>.
 */

/** @file industry.h Base of all industries. */

#ifndef INDUSTRY_H
#define INDUSTRY_H

#include "core/pool_type.hpp"
#include "newgrf_storage.h"
#include "subsidy_type.h"
#include "industry_map.h"
#include "tilearea_type.h"


typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
extern IndustryPool _industry_pool;

/**
 * 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
};

/**
 * Defines the internal data of a functional industry.
 */
struct Industry : IndustryPool::PoolItem<&_industry_pool> {
	typedef PersistentStorageArray<int32, 16> PersistentStorage;

	TileArea location;                  ///< Location of the industry
	const Town *town;                   ///< Nearest town
	CargoID produced_cargo[2];          ///< 2 production cargo slots
	uint16 produced_cargo_waiting[2];   ///< amount of cargo produced per cargo
	uint16 incoming_cargo_waiting[3];   ///< incoming cargo waiting to be processed
	byte production_rate[2];            ///< production rate for each cargo
	byte prod_level;                    ///< general production level
	CargoID accepts_cargo[3];           ///< 3 input cargo slots
	uint16 this_month_production[2];    ///< stats of this month's production per cargo
	uint16 this_month_transported[2];   ///< stats of this month's transport per cargo
	byte last_month_pct_transported[2]; ///< percentage transported per cargo in the last full month
	uint16 last_month_production[2];    ///< total units produced per cargo in the last full month
	uint16 last_month_transported[2];   ///< total units transported per cargo in the last full month
	uint16 counter;                     ///< used for animation and/or production (if available cargo)

	IndustryType type;                  ///< type of industry.
	OwnerByte 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

	PartOfSubsidyByte part_of_subsidy;  ///< NOSAVE: is this industry a source/destination of a subsidy?

	OwnerByte 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)
	Date last_cargo_accepted_at;        ///< Last day cargo was accepted by this industry
	byte selected_layout;               ///< Which tile layout was used when creating the industry

	byte random_triggers;               ///< Triggers for the random
	uint16 random;                      ///< Random value used for randomisation of all kinds of things

	PersistentStorage psa;              ///< Persistent storage for NewGRF industries.

	Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
	~Industry();

	void RecomputeProductionMultipliers();

	/**
	 * Get the industry of the given tile
	 * @param tile the tile to get the industry from
	 * @pre IsTileType(t, MP_INDUSTRY)
	 * @return the industry
	 */
	static FORCEINLINE Industry *GetByTile(TileIndex tile)
	{
		return Industry::Get(GetIndustryIndex(tile));
	}

	static Industry *GetRandom();
	static void PostDestructor(size_t index);

	/**
	 * 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));
	}

protected:
	static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
};

void PlantRandomFarmField(const Industry *i);

void ReleaseDisastersTargetingIndustry(IndustryID);

#define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
#define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)

/** Data for managing the number of industries of a single industry type. */
struct IndustryTypeBuildData {
	uint32 probability;  ///< Relative probability of building this industry.
	byte   min_number;   ///< Smallest number of industries that should exist (either \c 0 or \c 1).
	uint16 target_count; ///< Desired number of industries of this type.
	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.

	void Reset();

	bool GetIndustryTypeData(IndustryType it);
};

/**
 * Data for managing the number and type of industries in the game.
 */
struct IndustryBuildData {
	IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
	uint32 wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).

	void Reset();

	void SetupTargetCount();
	void TryBuildNewIndustry();

	void MonthlyLoop();
};

extern IndustryBuildData _industry_builder;

#endif /* INDUSTRY_H */