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