tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file industry.h */ belugas@6527: truelight@0: #ifndef INDUSTRY_H truelight@0: #define INDUSTRY_H truelight@0: matthijs@5216: #include "oldpool.h" belugas@6886: #include "helpers.hpp" rubidium@8106: #include "newgrf_storage.h" truelight@1267: rubidium@8135: typedef uint16 IndustryGfx; belugas@3689: typedef uint8 IndustryType; belugas@3689: truelight@4328: enum { belugas@7239: INVALID_INDUSTRY = 0xFFFF, belugas@7239: NEW_INDUSTRYOFFSET = 37, ///< original number of industries belugas@7239: NUM_INDUSTRYTYPES = 37, ///< total number of industries, new and old belugas@7239: INDUSTRYTILE_NOANIM = 0xFF, ///< flag to mark industry tiles as having no animation belugas@7239: NEW_INDUSTRYTILEOFFSET = 175, ///< original number of tiles belugas@7239: INVALID_INDUSTRYTYPE = NUM_INDUSTRYTYPES, ///< one above amount is considered invalid belugas@7239: NUM_INDUSTRYTILES = NEW_INDUSTRYTILEOFFSET, ///< total number of industry tiles, new and old belugas@7239: INVALID_INDUSTRYTILE = NUM_INDUSTRYTILES, ///< one above amount is considered invalid belugas@7787: INDUSTRY_COMPLETED = 3, ///< final stage of industry construction. truelight@4328: }; truelight@4328: belugas@7261: enum { belugas@7261: CLEAN_RANDOMSOUNDS, ///< Free the dynamically allocated sounds table belugas@7261: CLEAN_TILELSAYOUT, ///< Free the dynamically allocated tile layout structure belugas@7261: }; belugas@7261: rubidium@6574: enum IndustryLifeType { rubidium@7640: INDUSTRYLIFE_BLACK_HOLE = 0, ///< Like power plants and banks rubidium@7640: INDUSTRYLIFE_EXTRACTIVE = 1 << 0, ///< Like mines rubidium@7640: INDUSTRYLIFE_ORGANIC = 1 << 1, ///< Like forests rubidium@7640: INDUSTRYLIFE_PROCESSING = 1 << 2, ///< Like factories rubidium@6574: }; belugas@4924: belugas@6936: /* Procedures that can be run to check whether an industry may belugas@6936: * build at location the given to the procedure */ belugas@6936: enum CheckProc { belugas@7669: CHECK_NOTHING, belugas@7669: CHECK_FOREST, belugas@7669: CHECK_REFINERY, belugas@7669: CHECK_FARM, belugas@7669: CHECK_PLANTATION, belugas@7669: CHECK_WATER, belugas@7669: CHECK_LUMBERMILL, belugas@7669: CHECK_BUBBLEGEN, belugas@7669: CHECK_OIL_RIG, belugas@6936: CHECK_END, belugas@6936: }; belugas@6936: belugas@7755: /** How was the industry created */ rubidium@7682: enum IndustryConstructionType { belugas@7755: ICT_UNKNOWN, ///< in previous game version or without newindustries activated belugas@7755: ICT_NORMAL_GAMEPLAY, ///< either by user or random creation proccess belugas@7755: ICT_MAP_GENERATION, ///< during random map creation belugas@7755: ICT_SCENARIO_EDITOR ///< while scenarion edition belugas@7755: }; belugas@7755: belugas@7755: /** From where is callback CBID_INDUSTRY_AVAILABLE been called */ belugas@7755: enum IndustryAvailabilityCallType { belugas@7755: IACT_MAPGENERATION, ///< during random map generation belugas@7755: IACT_RANDOMCREATION, ///< during creation of random ingame industry belugas@7755: IACT_USERCREATION, ///< from the Fund/build window rubidium@7682: }; rubidium@7682: belugas@6886: enum IndustyBehaviour { belugas@6886: INDUSTRYBEH_NONE = 0, belugas@6886: INDUSTRYBEH_PLANT_FIELDS = 1 << 0, ///< periodically plants fileds around itself (temp and artic farms) belugas@6886: INDUSTRYBEH_CUT_TREES = 1 << 1, ///< cuts trees and produce first output cargo from them (lumber mill) belugas@6886: INDUSTRYBEH_BUILT_ONWATER = 1 << 2, ///< is built on water (oil rig) belugas@6886: INDUSTRYBEH_TOWN1200_MORE = 1 << 3, ///< can only be built in towns larger then 1200 inhabitants (temperate bank) belugas@6886: INDUSTRYBEH_ONLY_INTOWN = 1 << 4, ///< can only be built in towns (arctic/tropic banks, water tower) belugas@6886: INDUSTRYBEH_ONLY_NEARTOWN = 1 << 5, ///< is always built near towns (toy shop) belugas@6886: INDUSTRYBEH_PLANT_ON_BUILT = 1 << 6, ///< Fields are planted around when built (all farms) rubidium@7628: INDUSTRYBEH_DONT_INCR_PROD = 1 << 7, ///< do not increase production (oil wells) in the temperate climate belugas@6886: INDUSTRYBEH_BEFORE_1950 = 1 << 8, ///< can only be built before 1950 (oil wells) belugas@6886: INDUSTRYBEH_AFTER_1960 = 1 << 9, ///< can only be built after 1960 (oil rigs) belugas@6886: INDUSTRYBEH_AI_AIRSHIP_ROUTES = 1 << 10, ///< ai will attempt to establish air/ship routes to this industry (oil rig) belugas@6886: INDUSTRYBEH_AIRPLANE_ATTACKS = 1 << 11, ///< can be exploded by a military airplane (oil refinery) belugas@6886: INDUSTRYBEH_CHOPPER_ATTACKS = 1 << 12, ///< can be exploded by a military helicopter (factory) belugas@6886: INDUSTRYBEH_CAN_SUBSIDENCE = 1 << 13, ///< can cause a subsidence (coal mine, shaft that collapses) rubidium@8110: /* The following flags are only used for newindustries and do no represent any normal behaviour */ rubidium@8110: INDUSTRYBEH_PROD_MULTI_HNDLING = 1 << 14, ///< Automatic production multiplier handling rubidium@8110: INDUSTRYBEH_PRODCALLBACK_RANDOM = 1 << 15, ///< Production callback needs random bits in var 10 rubidium@8110: INDUSTRYBEH_NOBUILT_MAPCREATION = 1 << 16, ///< Do not force one instance of this type to appear on map generation belugas@8104: INDUSTRYBEH_CANCLOSE_LASTINSTANCE = 1 << 17, ///< Allow closing down the last instance of this type belugas@6886: }; belugas@6886: belugas@6886: belugas@6886: DECLARE_ENUM_AS_BIT_SET(IndustyBehaviour); belugas@6886: rubidium@7886: struct Industry; rubidium@7886: DECLARE_OLD_POOL(Industry, Industry, 3, 8000) rubidium@7886: belugas@6527: /** belugas@6527: * Defines the internal data of a functionnal industry belugas@6527: */ rubidium@7886: struct Industry : PoolItem { rubidium@8106: typedef PersistentStorageArray PersistentStorage; rubidium@8106: rubidium@7315: TileIndex xy; ///< coordinates of the primary tile the industry is built one belugas@6527: byte width; truelight@0: byte height; rubidium@7315: const Town *town; ///< Nearest town rubidium@7661: uint16 produced_cargo_waiting[2]; ///< amount of cargo produced per cargo rubidium@7661: uint16 incoming_cargo_waiting[3]; ///< incoming cargo waiting to be processed rubidium@7315: byte production_rate[2]; ///< production rate for each cargo rubidium@7315: byte prod_level; ///< general production level rubidium@7315: uint16 this_month_production[2]; ///< stats of this month's production per cargo rubidium@7315: uint16 this_month_transported[2]; ///< stats of this month's transport per cargo rubidium@7315: byte last_month_pct_transported[2]; ///< percentage transported per cargo in the last full month rubidium@7315: uint16 last_month_production[2]; ///< total units produced per cargo in the last full month rubidium@7315: uint16 last_month_transported[2]; ///< total units transported per cargo in the last full month rubidium@7315: uint16 counter; ///< used for animation and/or production (if available cargo) truelight@0: rubidium@7815: IndustryType type; ///< type of industry. rubidium@7315: OwnerByte owner; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE rubidium@7315: byte random_color; ///< randomized colour of the industry, for display purpose rubidium@7315: Year last_prod_year; ///< last year of production rubidium@7315: byte was_cargo_delivered; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry truelight@919: rubidium@7682: OwnerByte founder; ///< Founder of the industry rubidium@7682: Date construction_date; ///< Date of the construction of the industry rubidium@7682: uint8 construction_type; ///< Way the industry was constructed (@see IndustryConstructionType) rubidium@7682: Date last_cargo_accepted_at; ///< Last day cargo was accepted by this industry belugas@7941: byte selected_layout; ///< Which tile layout was used when creating the industry rubidium@7886: rubidium@8106: PersistentStorage psa; ///< Persistent storage for NewGRF industries. rubidium@8106: rubidium@7886: Industry(TileIndex tile = 0) : xy(tile) {} rubidium@7886: ~Industry(); rubidium@7886: rubidium@7992: inline bool IsValid() const { return this->xy != 0; } truelight@0: }; truelight@0: rubidium@6574: struct IndustryTileTable { belugas@3689: TileIndexDiffC ti; belugas@3689: IndustryGfx gfx; rubidium@6574: }; belugas@3689: belugas@7171: /** Data related to the handling of grf files. Common to both industry and industry tile */ belugas@7171: struct GRFFileProps { belugas@7956: uint16 subst_id; belugas@7171: uint16 local_id; ///< id defined by the grf file for this industry belugas@7171: struct SpriteGroup *spritegroup; ///< pointer to the different sprites of the industry belugas@7344: const struct GRFFile *grffile; ///< grf file that introduced this industry belugas@7603: uint16 override; ///< id of the entity been replaced by belugas@7171: }; belugas@7171: belugas@6527: /** belugas@6527: * Defines the data structure for constructing industry. belugas@6527: */ rubidium@6574: struct IndustrySpec { belugas@6527: const IndustryTileTable *const *table;///< List of the tiles composing the industry belugas@6527: byte num_table; ///< Number of elements in the table rubidium@7673: uint8 cost_multiplier; ///< Base cost multiplier. rubidium@7673: uint16 raw_industry_cost_multiplier; ///< Multiplier for the raw industries cost rubidium@7673: uint32 prospecting_chance; ///< Chance prospecting succeeds belugas@6527: IndustryType conflicting[3]; ///< Industries this industry cannot be close to belugas@6527: byte check_proc; ///< Index to a procedure to check for conflicting circumstances belugas@3689: CargoID produced_cargo[2]; belugas@3689: byte production_rate[2]; belugas@6527: byte minimal_cargo; ///< minimum amount of cargo transported to the stations belugas@6527: ///< If the waiting cargo is less than this number, no cargo is moved to it belugas@6527: CargoID accepts_cargo[3]; ///< 3 accepted cargos rubidium@7131: uint16 input_cargo_multiplier[3][2]; ///< Input cargo multipliers (multiply amount of incoming cargo for the produced cargos) belugas@6527: IndustryLifeType life_type; ///< This is also known as Industry production flag, in newgrf specs belugas@6527: byte climate_availability; ///< Bitmask, giving landscape enums as bit position belugas@6886: IndustyBehaviour behaviour; ///< How this industry will behave, and how others entities can use it belugas@6936: byte map_colour; ///< colour used for the small map belugas@6527: StringID name; ///< Displayed name of the industry belugas@6592: StringID new_industry_text; ///< Message appearing when the industry is built belugas@6527: StringID closure_text; ///< Message appearing when the industry closes belugas@6527: StringID production_up_text; ///< Message appearing when the industry's production is increasing belugas@6527: StringID production_down_text; ///< Message appearing when the industry's production is decreasing belugas@6910: byte appear_ingame[NUM_LANDSCAPE]; ///< Probability of appearance in game belugas@6910: byte appear_creation[NUM_LANDSCAPE]; ///< Probability of appearance during map creation belugas@7151: uint8 number_of_sounds; ///< Number of sounds available in the sounds array belugas@7151: const uint8 *random_sounds; ///< array of random sounds. belugas@7171: /* Newgrf data */ belugas@6936: uint16 callback_flags; ///< Flags telling which grf callback is set belugas@7261: uint8 cleanup_flag; ///< flags indicating which data should be freed upon cleaning up belugas@7237: bool enabled; ///< entity still avaible (by default true).newgrf can disable it, though belugas@7171: struct GRFFileProps grf_prop; ///< properties related the the grf file rubidium@7681: rubidium@7681: /** rubidium@7681: * Is an industry with the spec a raw industry? rubidium@7681: * @return true if it should be handled as a raw industry rubidium@7681: */ rubidium@7681: bool IsRawIndustry() const; rubidium@7681: rubidium@7681: /** rubidium@7681: * Get the cost for constructing this industry rubidium@7681: * @return the cost (inflation corrected etc) rubidium@7681: */ rubidium@7681: Money GetConstructionCost() const; rubidium@6574: }; belugas@3689: belugas@6527: /** belugas@6527: * Defines the data structure of each indivudual tile of an industry. belugas@6527: */ rubidium@6574: struct IndustryTileSpec { belugas@6527: CargoID accepts_cargo[3]; ///< Cargo accepted by this tile belugas@7232: uint8 acceptance[3]; ///< Level of aceptance per cargo type belugas@6527: Slope slopes_refused; ///< slope pattern on which this tile cannot be built belugas@6631: byte anim_production; ///< Animation frame to start when goods are produced belugas@6631: byte anim_next; ///< Next frame in an animation belugas@6631: bool anim_state; ///< When true, the tile has to be drawn using the animation belugas@7133: ///< state instead of the construction state belugas@7171: /* Newgrf data */ belugas@7239: uint8 callback_flags; ///< Flags telling which grf callback is set rubidium@7725: uint16 animation_info; ///< Information about the animation (is it looping, how many loops etc) rubidium@7725: uint8 animation_speed; ///< The speed of the animation rubidium@7725: uint8 animation_triggers; ///< When to start the animation rubidium@7725: uint8 animation_special_flags; ///< Extra flags to influence the animation belugas@7237: bool enabled; ///< entity still avaible (by default true).newgrf can disable it, though belugas@7171: struct GRFFileProps grf_prop; rubidium@6574: }; belugas@6418: belugas@7133: /* industry_cmd.cpp*/ belugas@7239: const IndustrySpec *GetIndustrySpec(IndustryType thistype); ///< Array of industries data belugas@7958: const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx); ///< Array of industry tiles data belugas@7133: void ResetIndustries(); belugas@7133: void PlantRandomFarmField(const Industry *i); belugas@3689: belugas@7239: /* writable arrays of specs */ belugas@7239: extern IndustrySpec _industry_specs[NUM_INDUSTRYTYPES]; belugas@7239: extern IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES]; belugas@7239: belugas@7963: static inline IndustryGfx GetTranslatedIndustryTileID(IndustryGfx gfx) belugas@7963: { belugas@7969: /* the 0xFF should be GFX_WATERTILE_SPECIALCHECK but for reasons of include mess, belugas@7969: * we'll simplify the writing. belugas@7969: * Basically, the first test is required since the GFX_WATERTILE_SPECIALCHECK value belugas@7969: * will never be assigned as a tile index and is only required in order to do some belugas@7969: * tests while building the industry (as in WATER REQUIRED */ belugas@7969: if (gfx != 0xFF) { belugas@7969: assert(gfx < INVALID_INDUSTRYTILE); belugas@7969: const IndustryTileSpec *it = &_industry_tile_specs[gfx]; belugas@7969: return it->grf_prop.override == INVALID_INDUSTRYTILE ? gfx : it->grf_prop.override; belugas@7969: } else { belugas@7969: return gfx; belugas@7969: } belugas@7963: } belugas@7963: belugas@7029: /* smallmap_gui.cpp */ belugas@7029: void BuildIndustriesLegend(); belugas@7029: belugas@6527: /** belugas@6527: * Check if an Industry exists whithin the pool of industries belugas@6527: * @param index of the desired industry belugas@6527: * @return true if it is inside the pool belugas@6527: */ rubidium@5299: static inline bool IsValidIndustryID(IndustryID index) rubidium@5299: { rubidium@7886: return index < GetIndustryPoolSize() && GetIndustry(index)->IsValid(); rubidium@5299: } rubidium@5299: truelight@4354: rubidium@6573: static inline IndustryID GetMaxIndustryIndex() truelight@4354: { truelight@4354: /* TODO - This isn't the real content of the function, but truelight@4354: * with the new pool-system this will be replaced with one that matthijs@5247: * _really_ returns the highest index. Now it just returns truelight@4354: * the next safe value we are sure about everything is below. truelight@4354: */ rubidium@5298: return GetIndustryPoolSize() - 1; matthijs@5247: } matthijs@5247: belugas@7335: extern int _total_industries; // general counter belugas@7335: extern uint16 _industry_counts[NUM_INDUSTRYTYPES]; // Number of industries per type ingame belugas@7335: rubidium@6573: static inline uint GetNumIndustries() matthijs@5247: { truelight@4357: return _total_industries; truelight@4354: } truelight@4354: belugas@7320: /** Increment the count of industries for this type belugas@7320: * @param type IndustryType to increment belugas@7320: * @pre type < INVALID_INDUSTRYTYPE */ belugas@7320: static inline void IncIndustryTypeCount(IndustryType type) belugas@7320: { belugas@7320: assert(type < INVALID_INDUSTRYTYPE); belugas@7320: _industry_counts[type]++; belugas@7335: _total_industries++; belugas@7320: } belugas@7320: belugas@7320: /** Decrement the count of industries for this type belugas@7320: * @param type IndustryType to decrement belugas@7320: * @pre type < INVALID_INDUSTRYTYPE */ belugas@7320: static inline void DecIndustryTypeCount(IndustryType type) belugas@7320: { belugas@7320: assert(type < INVALID_INDUSTRYTYPE); belugas@7320: _industry_counts[type]--; belugas@7335: _total_industries--; belugas@7320: } belugas@7320: belugas@7320: /** get the count of industries for this type belugas@7320: * @param type IndustryType to query belugas@7320: * @pre type < INVALID_INDUSTRYTYPE */ belugas@7320: static inline uint8 GetIndustryTypeCount(IndustryType type) belugas@7320: { belugas@7320: assert(type < INVALID_INDUSTRYTYPE); belugas@7320: return min(_industry_counts[type], 0xFF); // callback expects only a byte, so cut it belugas@7320: } belugas@7320: belugas@7335: /** Resets both the total_industries and the _industry_counts belugas@7335: * This way, we centralize all counts activities */ belugas@7335: static inline void ResetIndustryCounts() belugas@7335: { belugas@7335: _total_industries = 0; belugas@7335: memset(&_industry_counts, 0, sizeof(_industry_counts)); belugas@7335: } belugas@7335: truelight@4356: /** rubidium@5299: * Return a random valid industry. truelight@4356: */ rubidium@6573: static inline Industry *GetRandomIndustry() truelight@4356: { rubidium@5299: int num = RandomRange(GetNumIndustries()); rubidium@5299: IndustryID index = INVALID_INDUSTRY; truelight@4356: matthijs@5247: if (GetNumIndustries() == 0) return NULL; truelight@4356: rubidium@5299: while (num >= 0) { truelight@4356: num--; truelight@4356: index++; rubidium@5299: truelight@4356: /* Make sure we have a valid industry */ rubidium@5299: while (!IsValidIndustryID(index)) { truelight@4356: index++; rubidium@5299: assert(index <= GetMaxIndustryIndex()); truelight@4356: } truelight@4356: } truelight@4356: truelight@4356: return GetIndustry(index); truelight@4356: } truelight@4356: rubidium@7886: #define FOR_ALL_INDUSTRIES_FROM(i, start) for (i = GetIndustry(start); i != NULL; i = (i->index + 1U < GetIndustryPoolSize()) ? GetIndustry(i->index + 1U) : NULL) if (i->IsValid()) truelight@1267: #define FOR_ALL_INDUSTRIES(i) FOR_ALL_INDUSTRIES_FROM(i, 0) truelight@1267: belugas@7320: extern const Industry **_industry_sort; belugas@7320: extern bool _industry_sort_dirty; truelight@919: rubidium@7815: static const uint8 IT_INVALID = 255; truelight@0: Darkvater@2436: #endif /* INDUSTRY_H */