diff --git a/src/industry.h b/src/industry.h index 99ba24924e..63c647a77f 100644 --- a/src/industry.h +++ b/src/industry.h @@ -78,6 +78,48 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { 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); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 93f508aac6..999194762a 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -56,7 +56,7 @@ void BuildOilRig(TileIndex tile); static byte _industry_sound_ctr; static TileIndex _industry_sound_tile; -uint16 _industry_counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame +uint16 Industry::counts[NUM_INDUSTRYTYPES]; IndustrySpec _industry_specs[NUM_INDUSTRYTYPES]; IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES]; @@ -134,7 +134,8 @@ Industry::~Industry() if (CleaningPool()) return; /* Industry can also be destroyed when not fully initialized. - * This means that we do not have to clear tiles either. */ + * This means that we do not have to clear tiles either. + * Also we must not decrement industry counts in that case. */ if (this->location.w == 0) return; TILE_AREA_LOOP(tile_cur, this->location) { @@ -1584,7 +1585,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const Ind i->location = TileArea(tile, 1, 1); i->type = type; - IncIndustryTypeCount(type); + Industry::IncIndustryTypeCount(type); i->produced_cargo[0] = indspec->produced_cargo[0]; i->produced_cargo[1] = indspec->produced_cargo[1]; @@ -2059,7 +2060,7 @@ static bool CheckIndustryCloseDownProtection(IndustryType type) /* oil wells (or the industries with that flag set) are always allowed to closedown */ if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false; - return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1; + return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && Industry::GetIndustryTypeCount(type) <= 1; } /** @@ -2469,7 +2470,7 @@ void InitializeIndustries() { _industry_pool.CleanPool(); - ResetIndustryCounts(); + Industry::ResetIndustryCounts(); _industry_sound_tile = 0; } diff --git a/src/industrytype.h b/src/industrytype.h index c3570d7854..f454836341 100644 --- a/src/industrytype.h +++ b/src/industrytype.h @@ -204,42 +204,6 @@ static inline IndustryGfx GetTranslatedIndustryTileID(IndustryGfx gfx) } } -extern uint16 _industry_counts[NUM_INDUSTRYTYPES]; // Number of industries per type ingame - -/** Increment the count of industries for this type - * @param type IndustryType to increment - * @pre type < INVALID_INDUSTRYTYPE */ -static inline void IncIndustryTypeCount(IndustryType type) -{ - assert(type < INVALID_INDUSTRYTYPE); - _industry_counts[type]++; -} - -/** Decrement the count of industries for this type - * @param type IndustryType to decrement - * @pre type < INVALID_INDUSTRYTYPE */ -static inline void DecIndustryTypeCount(IndustryType type) -{ - assert(type < INVALID_INDUSTRYTYPE); - _industry_counts[type]--; -} - -/** get the count of industries for this type - * @param type IndustryType to query - * @pre type < INVALID_INDUSTRYTYPE */ -static inline uint8 GetIndustryTypeCount(IndustryType type) -{ - assert(type < INVALID_INDUSTRYTYPE); - return min(_industry_counts[type], 0xFF); // callback expects only a byte, so cut it -} - -/** Resets both the total_industries and the _industry_counts - * This way, we centralize all counts activities */ -static inline void ResetIndustryCounts() -{ - memset(&_industry_counts, 0, sizeof(_industry_counts)); -} - static const uint8 IT_INVALID = 255; #endif /* INDUSTRYTYPE_H */ diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index ceff4923f4..f5d5301fa1 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -135,7 +135,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout /* If the filter is 0, it could be because none was specified as well as being really a 0. * In either case, just do the regular var67 */ closest_dist = GetClosestIndustry(current->location.tile, ind_index, current); - count = GetIndustryTypeCount(ind_index); + count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit } else { /* Count only those who match the same industry type and layout filter * Unfortunately, we have to do it manually */ @@ -465,7 +465,7 @@ CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uin Industry ind; ind.index = INVALID_INDUSTRY; ind.location.tile = tile; - ind.location.w = 0; + ind.location.w = 0; // important to mark the industry invalid ind.type = type; ind.selected_layout = layout; ind.town = ClosestTownFromTile(tile, UINT_MAX); diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index 9b4dd2b3db..42c8fda328 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -107,12 +107,12 @@ static void Load_INDY() { int index; - ResetIndustryCounts(); + Industry::ResetIndustryCounts(); while ((index = SlIterateArray()) != -1) { Industry *i = new (index) Industry(); SlObject(i, _industry_desc); - IncIndustryTypeCount(i->type); + Industry::IncIndustryTypeCount(i->type); } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index e6dc18660e..68c3daf7a3 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -841,7 +841,7 @@ static bool LoadOldIndustry(LoadgameState *ls, int num) i->random_colour = RemapTTOColour(i->random_colour); } - IncIndustryTypeCount(i->type); + Industry::IncIndustryTypeCount(i->type); } else { delete i; } diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 40d238c9b0..e449572171 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -1092,8 +1092,7 @@ public: /* Industry name must be formatted, since it's not in tiny font in the specs. * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */ SetDParam(0, tbl->legend); - assert(tbl->type < NUM_INDUSTRYTYPES); - SetDParam(1, _industry_counts[tbl->type]); + SetDParam(1, Industry::GetIndustryTypeCount(tbl->type)); if (!tbl->show_on_map) { /* Simply draw the string, not the black border of the legend colour. * This will enforce the idea of the disabled item */