tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file industry.h Base of all industries. */ belugas@6201: truelight@0: #ifndef INDUSTRY_H truelight@0: #define INDUSTRY_H truelight@0: matthijs@5216: #include "oldpool.h" rubidium@8119: #include "core/random_func.hpp" rubidium@7610: #include "newgrf_storage.h" rubidium@8119: #include "cargo_type.h" rubidium@8119: #include "economy_type.h" rubidium@8139: #include "map_type.h" rubidium@8138: #include "slope_type.h" rubidium@8140: #include "date_type.h" rubidium@8213: #include "town_type.h" rubidium@8213: #include "industry_type.h" rubidium@9126: #include "landscape_type.h" belugas@3689: truelight@4328: enum { belugas@6743: INVALID_INDUSTRY = 0xFFFF, belugas@6743: NEW_INDUSTRYOFFSET = 37, ///< original number of industries rubidium@7654: NUM_INDUSTRYTYPES = 64, ///< total number of industries, new and old belugas@6743: INDUSTRYTILE_NOANIM = 0xFF, ///< flag to mark industry tiles as having no animation belugas@6743: NEW_INDUSTRYTILEOFFSET = 175, ///< original number of tiles belugas@6743: INVALID_INDUSTRYTYPE = NUM_INDUSTRYTYPES, ///< one above amount is considered invalid rubidium@7654: NUM_INDUSTRYTILES = 512, ///< total number of industry tiles, new and old belugas@6743: INVALID_INDUSTRYTILE = NUM_INDUSTRYTILES, ///< one above amount is considered invalid belugas@7291: INDUSTRY_COMPLETED = 3, ///< final stage of industry construction. truelight@4328: }; truelight@4328: belugas@6765: enum { belugas@6765: CLEAN_RANDOMSOUNDS, ///< Free the dynamically allocated sounds table belugas@6765: CLEAN_TILELSAYOUT, ///< Free the dynamically allocated tile layout structure belugas@6765: }; belugas@6765: rubidium@6248: enum IndustryLifeType { rubidium@7144: INDUSTRYLIFE_BLACK_HOLE = 0, ///< Like power plants and banks rubidium@7144: INDUSTRYLIFE_EXTRACTIVE = 1 << 0, ///< Like mines rubidium@7144: INDUSTRYLIFE_ORGANIC = 1 << 1, ///< Like forests rubidium@7144: INDUSTRYLIFE_PROCESSING = 1 << 2, ///< Like factories rubidium@6248: }; belugas@4924: belugas@6440: /* Procedures that can be run to check whether an industry may belugas@6440: * build at location the given to the procedure */ belugas@6440: enum CheckProc { belugas@7173: CHECK_NOTHING, belugas@7173: CHECK_FOREST, belugas@7173: CHECK_REFINERY, belugas@7173: CHECK_FARM, belugas@7173: CHECK_PLANTATION, belugas@7173: CHECK_WATER, belugas@7173: CHECK_LUMBERMILL, belugas@7173: CHECK_BUBBLEGEN, belugas@7173: CHECK_OIL_RIG, belugas@6440: CHECK_END, belugas@6440: }; belugas@6440: belugas@7259: /** How was the industry created */ rubidium@7186: enum IndustryConstructionType { belugas@7259: ICT_UNKNOWN, ///< in previous game version or without newindustries activated belugas@7259: ICT_NORMAL_GAMEPLAY, ///< either by user or random creation proccess belugas@7259: ICT_MAP_GENERATION, ///< during random map creation belugas@7259: ICT_SCENARIO_EDITOR ///< while scenarion edition belugas@7259: }; belugas@7259: glx@7757: enum IndustryBehaviour { belugas@6390: INDUSTRYBEH_NONE = 0, belugas@6390: INDUSTRYBEH_PLANT_FIELDS = 1 << 0, ///< periodically plants fileds around itself (temp and artic farms) belugas@6390: INDUSTRYBEH_CUT_TREES = 1 << 1, ///< cuts trees and produce first output cargo from them (lumber mill) belugas@6390: INDUSTRYBEH_BUILT_ONWATER = 1 << 2, ///< is built on water (oil rig) belugas@6390: INDUSTRYBEH_TOWN1200_MORE = 1 << 3, ///< can only be built in towns larger then 1200 inhabitants (temperate bank) belugas@6390: INDUSTRYBEH_ONLY_INTOWN = 1 << 4, ///< can only be built in towns (arctic/tropic banks, water tower) belugas@6390: INDUSTRYBEH_ONLY_NEARTOWN = 1 << 5, ///< is always built near towns (toy shop) belugas@6390: INDUSTRYBEH_PLANT_ON_BUILT = 1 << 6, ///< Fields are planted around when built (all farms) rubidium@7132: INDUSTRYBEH_DONT_INCR_PROD = 1 << 7, ///< do not increase production (oil wells) in the temperate climate belugas@6390: INDUSTRYBEH_BEFORE_1950 = 1 << 8, ///< can only be built before 1950 (oil wells) belugas@6390: INDUSTRYBEH_AFTER_1960 = 1 << 9, ///< can only be built after 1960 (oil rigs) belugas@6390: INDUSTRYBEH_AI_AIRSHIP_ROUTES = 1 << 10, ///< ai will attempt to establish air/ship routes to this industry (oil rig) belugas@6390: INDUSTRYBEH_AIRPLANE_ATTACKS = 1 << 11, ///< can be exploded by a military airplane (oil refinery) belugas@6390: INDUSTRYBEH_CHOPPER_ATTACKS = 1 << 12, ///< can be exploded by a military helicopter (factory) belugas@6390: INDUSTRYBEH_CAN_SUBSIDENCE = 1 << 13, ///< can cause a subsidence (coal mine, shaft that collapses) rubidium@7614: /* The following flags are only used for newindustries and do no represent any normal behaviour */ rubidium@7614: INDUSTRYBEH_PROD_MULTI_HNDLING = 1 << 14, ///< Automatic production multiplier handling rubidium@7614: INDUSTRYBEH_PRODCALLBACK_RANDOM = 1 << 15, ///< Production callback needs random bits in var 10 rubidium@7614: INDUSTRYBEH_NOBUILT_MAPCREATION = 1 << 16, ///< Do not force one instance of this type to appear on map generation belugas@7608: INDUSTRYBEH_CANCLOSE_LASTINSTANCE = 1 << 17, ///< Allow closing down the last instance of this type belugas@6390: }; belugas@6390: belugas@6390: glx@7757: DECLARE_ENUM_AS_BIT_SET(IndustryBehaviour); belugas@6390: rubidium@7390: DECLARE_OLD_POOL(Industry, Industry, 3, 8000) rubidium@7390: belugas@6201: /** belugas@6201: * Defines the internal data of a functionnal industry belugas@6201: */ rubidium@7390: struct Industry : PoolItem { rubidium@7610: typedef PersistentStorageArray PersistentStorage; rubidium@7610: rubidium@6819: TileIndex xy; ///< coordinates of the primary tile the industry is built one belugas@6201: byte width; truelight@0: byte height; rubidium@6819: const Town *town; ///< Nearest town glx@7645: CargoID produced_cargo[2]; ///< 2 production cargo slots rubidium@7165: uint16 produced_cargo_waiting[2]; ///< amount of cargo produced per cargo rubidium@7165: uint16 incoming_cargo_waiting[3]; ///< incoming cargo waiting to be processed rubidium@6819: byte production_rate[2]; ///< production rate for each cargo rubidium@6819: byte prod_level; ///< general production level glx@7645: CargoID accepts_cargo[3]; ///< 3 input cargo slots rubidium@6819: uint16 this_month_production[2]; ///< stats of this month's production per cargo rubidium@6819: uint16 this_month_transported[2]; ///< stats of this month's transport per cargo rubidium@6819: byte last_month_pct_transported[2]; ///< percentage transported per cargo in the last full month rubidium@6819: uint16 last_month_production[2]; ///< total units produced per cargo in the last full month rubidium@6819: uint16 last_month_transported[2]; ///< total units transported per cargo in the last full month rubidium@6819: uint16 counter; ///< used for animation and/or production (if available cargo) truelight@0: rubidium@7319: IndustryType type; ///< type of industry. rubidium@6819: OwnerByte owner; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE rubidium@6819: byte random_color; ///< randomized colour of the industry, for display purpose rubidium@6819: Year last_prod_year; ///< last year of production rubidium@6819: 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@7186: OwnerByte founder; ///< Founder of the industry rubidium@7186: Date construction_date; ///< Date of the construction of the industry rubidium@7186: uint8 construction_type; ///< Way the industry was constructed (@see IndustryConstructionType) rubidium@7186: Date last_cargo_accepted_at; ///< Last day cargo was accepted by this industry belugas@7445: byte selected_layout; ///< Which tile layout was used when creating the industry rubidium@7390: rubidium@7860: byte random_triggers; ///< Triggers for the random rubidium@7860: uint16 random; ///< Random value used for randomisation of all kinds of things rubidium@7860: rubidium@7610: PersistentStorage psa; ///< Persistent storage for NewGRF industries. rubidium@7610: rubidium@7390: Industry(TileIndex tile = 0) : xy(tile) {} rubidium@7390: ~Industry(); rubidium@7390: rubidium@7496: inline bool IsValid() const { return this->xy != 0; } truelight@0: }; truelight@0: rubidium@6248: struct IndustryTileTable { belugas@3689: TileIndexDiffC ti; belugas@3689: IndustryGfx gfx; rubidium@6248: }; belugas@3689: belugas@6675: /** Data related to the handling of grf files. Common to both industry and industry tile */ belugas@6675: struct GRFFileProps { belugas@7460: uint16 subst_id; belugas@6675: uint16 local_id; ///< id defined by the grf file for this industry belugas@6675: struct SpriteGroup *spritegroup; ///< pointer to the different sprites of the industry belugas@6848: const struct GRFFile *grffile; ///< grf file that introduced this industry belugas@7107: uint16 override; ///< id of the entity been replaced by belugas@6675: }; belugas@6675: belugas@6201: /** belugas@6201: * Defines the data structure for constructing industry. belugas@6201: */ rubidium@6248: struct IndustrySpec { belugas@6201: const IndustryTileTable *const *table;///< List of the tiles composing the industry belugas@6201: byte num_table; ///< Number of elements in the table belugas@7978: uint8 cost_multiplier; ///< Base construction cost multiplier. belugas@7978: uint32 removal_cost_multiplier; ///< Base removal cost multiplier. rubidium@7177: uint16 raw_industry_cost_multiplier; ///< Multiplier for the raw industries cost rubidium@7177: uint32 prospecting_chance; ///< Chance prospecting succeeds belugas@6201: IndustryType conflicting[3]; ///< Industries this industry cannot be close to belugas@6201: 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@6201: byte minimal_cargo; ///< minimum amount of cargo transported to the stations belugas@6201: ///< If the waiting cargo is less than this number, no cargo is moved to it belugas@6201: CargoID accepts_cargo[3]; ///< 3 accepted cargos rubidium@6635: uint16 input_cargo_multiplier[3][2]; ///< Input cargo multipliers (multiply amount of incoming cargo for the produced cargos) belugas@6201: IndustryLifeType life_type; ///< This is also known as Industry production flag, in newgrf specs belugas@6201: byte climate_availability; ///< Bitmask, giving landscape enums as bit position glx@7757: IndustryBehaviour behaviour; ///< How this industry will behave, and how others entities can use it belugas@6440: byte map_colour; ///< colour used for the small map rubidium@8959: StringID name; ///< Displayed name of the industry rubidium@8959: StringID new_industry_text; ///< Message appearing when the industry is built rubidium@8959: StringID closure_text; ///< Message appearing when the industry closes rubidium@8959: StringID production_up_text; ///< Message appearing when the industry's production is increasing rubidium@8959: StringID production_down_text; ///< Message appearing when the industry's production is decreasing rubidium@8959: StringID station_name; ///< Default name for nearby station belugas@6414: byte appear_ingame[NUM_LANDSCAPE]; ///< Probability of appearance in game belugas@6414: byte appear_creation[NUM_LANDSCAPE]; ///< Probability of appearance during map creation belugas@6655: uint8 number_of_sounds; ///< Number of sounds available in the sounds array belugas@6655: const uint8 *random_sounds; ///< array of random sounds. belugas@6675: /* Newgrf data */ belugas@6440: uint16 callback_flags; ///< Flags telling which grf callback is set belugas@6765: uint8 cleanup_flag; ///< flags indicating which data should be freed upon cleaning up belugas@6741: bool enabled; ///< entity still avaible (by default true).newgrf can disable it, though belugas@6675: struct GRFFileProps grf_prop; ///< properties related the the grf file rubidium@7185: rubidium@7185: /** rubidium@7185: * Is an industry with the spec a raw industry? rubidium@7185: * @return true if it should be handled as a raw industry rubidium@7185: */ rubidium@7185: bool IsRawIndustry() const; rubidium@7185: rubidium@7185: /** rubidium@7185: * Get the cost for constructing this industry rubidium@7185: * @return the cost (inflation corrected etc) rubidium@7185: */ rubidium@7185: Money GetConstructionCost() const; belugas@7978: belugas@7978: /** belugas@7978: * Get the cost for removing this industry belugas@7978: * Take note that the cost will always be zero for non-grf industries. belugas@7978: * Only if the grf author did specified a cost will it be applicable. belugas@7978: * @return the cost (inflation corrected etc) belugas@7978: */ belugas@7978: Money GetRemovalCost() const; rubidium@6248: }; belugas@3689: belugas@6201: /** belugas@6201: * Defines the data structure of each indivudual tile of an industry. belugas@6201: */ rubidium@6248: struct IndustryTileSpec { belugas@6201: CargoID accepts_cargo[3]; ///< Cargo accepted by this tile belugas@6736: uint8 acceptance[3]; ///< Level of aceptance per cargo type belugas@6201: Slope slopes_refused; ///< slope pattern on which this tile cannot be built belugas@6305: byte anim_production; ///< Animation frame to start when goods are produced belugas@6305: byte anim_next; ///< Next frame in an animation belugas@6305: bool anim_state; ///< When true, the tile has to be drawn using the animation belugas@6637: ///< state instead of the construction state belugas@6675: /* Newgrf data */ belugas@6743: uint8 callback_flags; ///< Flags telling which grf callback is set rubidium@7229: uint16 animation_info; ///< Information about the animation (is it looping, how many loops etc) rubidium@7229: uint8 animation_speed; ///< The speed of the animation rubidium@7229: uint8 animation_triggers; ///< When to start the animation rubidium@7229: uint8 animation_special_flags; ///< Extra flags to influence the animation belugas@6741: bool enabled; ///< entity still avaible (by default true).newgrf can disable it, though belugas@6675: struct GRFFileProps grf_prop; rubidium@6248: }; belugas@6092: belugas@6637: /* industry_cmd.cpp*/ belugas@6743: const IndustrySpec *GetIndustrySpec(IndustryType thistype); ///< Array of industries data belugas@7462: const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx); ///< Array of industry tiles data belugas@6637: void ResetIndustries(); belugas@6637: void PlantRandomFarmField(const Industry *i); belugas@3689: belugas@6743: /* writable arrays of specs */ belugas@6743: extern IndustrySpec _industry_specs[NUM_INDUSTRYTYPES]; belugas@6743: extern IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES]; belugas@6743: belugas@7467: static inline IndustryGfx GetTranslatedIndustryTileID(IndustryGfx gfx) belugas@7467: { belugas@7473: /* the 0xFF should be GFX_WATERTILE_SPECIALCHECK but for reasons of include mess, belugas@7473: * we'll simplify the writing. belugas@7473: * Basically, the first test is required since the GFX_WATERTILE_SPECIALCHECK value belugas@7473: * will never be assigned as a tile index and is only required in order to do some belugas@7473: * tests while building the industry (as in WATER REQUIRED */ belugas@7473: if (gfx != 0xFF) { belugas@7473: assert(gfx < INVALID_INDUSTRYTILE); belugas@7473: const IndustryTileSpec *it = &_industry_tile_specs[gfx]; belugas@7473: return it->grf_prop.override == INVALID_INDUSTRYTILE ? gfx : it->grf_prop.override; belugas@7473: } else { belugas@7473: return gfx; belugas@7473: } belugas@7467: } belugas@7467: belugas@6533: /* smallmap_gui.cpp */ belugas@6533: void BuildIndustriesLegend(); belugas@6533: belugas@6201: /** belugas@6201: * Check if an Industry exists whithin the pool of industries belugas@6201: * @param index of the desired industry belugas@6201: * @return true if it is inside the pool belugas@6201: */ rubidium@5299: static inline bool IsValidIndustryID(IndustryID index) rubidium@5299: { rubidium@7390: return index < GetIndustryPoolSize() && GetIndustry(index)->IsValid(); rubidium@5299: } rubidium@5299: truelight@4354: rubidium@6247: 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@6839: extern int _total_industries; // general counter belugas@6839: extern uint16 _industry_counts[NUM_INDUSTRYTYPES]; // Number of industries per type ingame belugas@6839: rubidium@6247: static inline uint GetNumIndustries() matthijs@5247: { truelight@4357: return _total_industries; truelight@4354: } truelight@4354: belugas@6824: /** Increment the count of industries for this type belugas@6824: * @param type IndustryType to increment belugas@6824: * @pre type < INVALID_INDUSTRYTYPE */ belugas@6824: static inline void IncIndustryTypeCount(IndustryType type) belugas@6824: { belugas@6824: assert(type < INVALID_INDUSTRYTYPE); belugas@6824: _industry_counts[type]++; belugas@6839: _total_industries++; belugas@6824: } belugas@6824: belugas@6824: /** Decrement the count of industries for this type belugas@6824: * @param type IndustryType to decrement belugas@6824: * @pre type < INVALID_INDUSTRYTYPE */ belugas@6824: static inline void DecIndustryTypeCount(IndustryType type) belugas@6824: { belugas@6824: assert(type < INVALID_INDUSTRYTYPE); belugas@6824: _industry_counts[type]--; belugas@6839: _total_industries--; belugas@6824: } belugas@6824: belugas@6824: /** get the count of industries for this type belugas@6824: * @param type IndustryType to query belugas@6824: * @pre type < INVALID_INDUSTRYTYPE */ belugas@6824: static inline uint8 GetIndustryTypeCount(IndustryType type) belugas@6824: { belugas@6824: assert(type < INVALID_INDUSTRYTYPE); belugas@6824: return min(_industry_counts[type], 0xFF); // callback expects only a byte, so cut it belugas@6824: } belugas@6824: belugas@6839: /** Resets both the total_industries and the _industry_counts belugas@6839: * This way, we centralize all counts activities */ belugas@6839: static inline void ResetIndustryCounts() belugas@6839: { belugas@6839: _total_industries = 0; belugas@6839: memset(&_industry_counts, 0, sizeof(_industry_counts)); belugas@6839: } belugas@6839: truelight@4356: /** rubidium@5299: * Return a random valid industry. truelight@4356: */ rubidium@6247: 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@7390: #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: rubidium@7319: static const uint8 IT_INVALID = 255; truelight@0: Darkvater@2436: #endif /* INDUSTRY_H */