diff --git a/src/industry.h b/src/industry.h index 6e19921cdf..97eecf53a9 100644 --- a/src/industry.h +++ b/src/industry.h @@ -39,23 +39,20 @@ static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80; ///< the industry is running * Flags to control/override the behaviour of an industry. * These flags are controlled by game scripts. */ -enum IndustryControlFlags : uint8_t { - /** No flags in effect */ - INDCTL_NONE = 0, +enum class IndustryControlFlag : uint8_t { /** When industry production change is evaluated, rolls to decrease are ignored. */ - INDCTL_NO_PRODUCTION_DECREASE = 1 << 0, + NoProductionDecrease = 0, /** When industry production change is evaluated, rolls to increase are ignored. */ - INDCTL_NO_PRODUCTION_INCREASE = 1 << 1, + NoProductionIncrease = 1, /** * Industry can not close regardless of production level or time since last delivery. * This does not prevent a closure already announced. */ - INDCTL_NO_CLOSURE = 1 << 2, + NoClosure = 2, /** Indicates that the production level of the industry is externally controlled. */ - INDCTL_EXTERNAL_PROD_LEVEL = 1 << 3, - /** Mask of all flags set */ - INDCTL_MASK = INDCTL_NO_PRODUCTION_DECREASE | INDCTL_NO_PRODUCTION_INCREASE | INDCTL_NO_CLOSURE | INDCTL_EXTERNAL_PROD_LEVEL, + ExternalProdLevel = 3, + End, }; -DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags); +using IndustryControlFlags = EnumBitSet; static const int THIS_MONTH = 0; static const int LAST_MONTH = 1; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 2df4b79d44..2e059cc5eb 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -1804,7 +1804,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, i->was_cargo_delivered = false; i->last_prod_year = TimerGameEconomy::year; i->founder = founder; - i->ctlflags = INDCTL_NONE; + i->ctlflags = {}; i->construction_date = TimerGameCalendar::date; i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR : @@ -2143,8 +2143,9 @@ CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, Industry Industry *ind = Industry::GetIfValid(ind_id); if (ind == nullptr) return CMD_ERROR; + if (!ctlflags.IsValid()) return CMD_ERROR; - if (flags & DC_EXEC) ind->ctlflags = ctlflags & INDCTL_MASK; + if (flags & DC_EXEC) ind->ctlflags = ctlflags; return CommandCost(); } @@ -2175,7 +2176,7 @@ CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, uin } if (prod_level != ind->prod_level && !custom_news.empty()) str = STR_NEWS_CUSTOM_ITEM; - ind->ctlflags |= INDCTL_EXTERNAL_PROD_LEVEL; + ind->ctlflags.Set(IndustryControlFlag::ExternalProdLevel); ind->prod_level = prod_level; ind->RecomputeProductionMultipliers(); @@ -2855,7 +2856,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly) } } } else if (_settings_game.economy.type == ET_SMOOTH) { - closeit = !(i->ctlflags & (INDCTL_NO_CLOSURE | INDCTL_NO_PRODUCTION_DECREASE)); + closeit = !i->ctlflags.Any({IndustryControlFlag::NoClosure, IndustryControlFlag::NoProductionDecrease}); for (auto &p : i->produced) { if (!IsValidCargoType(p.cargo)) continue; uint32_t r = Random(); @@ -2888,8 +2889,8 @@ static void ChangeIndustryProduction(Industry *i, bool monthly) } /* If override flags are set, prevent actually changing production if any was decided on */ - if ((i->ctlflags & INDCTL_NO_PRODUCTION_DECREASE) && new_prod < old_prod) continue; - if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && new_prod > old_prod) continue; + if (i->ctlflags.Test(IndustryControlFlag::NoProductionDecrease) && new_prod < old_prod) continue; + if (i->ctlflags.Test(IndustryControlFlag::NoProductionIncrease) && new_prod > old_prod) continue; /* Do not stop closing the industry when it has the lowest possible production rate */ if (new_prod == old_prod && old_prod > 1) { @@ -2911,9 +2912,9 @@ static void ChangeIndustryProduction(Industry *i, bool monthly) } /* If override flags are set, prevent actually changing production if any was decided on */ - if ((i->ctlflags & INDCTL_NO_PRODUCTION_DECREASE) && (div > 0 || increment < 0)) return; - if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && (mul > 0 || increment > 0)) return; - if (i->ctlflags & INDCTL_EXTERNAL_PROD_LEVEL) { + if (i->ctlflags.Test(IndustryControlFlag::NoProductionDecrease) && (div > 0 || increment < 0)) return; + if (i->ctlflags.Test(IndustryControlFlag::NoProductionIncrease) && (mul > 0 || increment > 0)) return; + if (i->ctlflags.Test(IndustryControlFlag::ExternalProdLevel)) { div = 0; mul = 0; increment = 0; @@ -2959,7 +2960,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly) if (recalculate_multipliers) i->RecomputeProductionMultipliers(); /* Close if needed and allowed */ - if (closeit && !CheckIndustryCloseDownProtection(i->type) && !(i->ctlflags & INDCTL_NO_CLOSURE)) { + if (closeit && !CheckIndustryCloseDownProtection(i->type) && !i->ctlflags.Test(IndustryControlFlag::NoClosure)) { i->prod_level = PRODLEVEL_CLOSURE; SetWindowDirty(WC_INDUSTRY_VIEW, i->index); str = indspec->closure_text; diff --git a/src/industry_cmd.h b/src/industry_cmd.h index fee8034269..f6a988d02d 100644 --- a/src/industry_cmd.h +++ b/src/industry_cmd.h @@ -13,8 +13,7 @@ #include "command_type.h" #include "company_type.h" #include "industry_type.h" - -enum IndustryControlFlags : uint8_t; +#include "industry.h" CommandCost CmdBuildIndustry(DoCommandFlag flags, TileIndex tile, IndustryType it, uint32_t first_layout, bool fund, uint32_t seed); CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, IndustryControlFlags ctlflags); diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index 761685315d..c6e2bf1d00 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -254,7 +254,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID, uint8_ case 0x46: return this->industry->construction_date.base(); // Date when built - long format - (in days) /* Override flags from GS */ - case 0x47: return this->industry->ctlflags; + case 0x47: return this->industry->ctlflags.base(); /* Get industry ID at offset param */ case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro.grffile->grfid); diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp index 287b34b496..3d74965cdf 100644 --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -245,7 +245,7 @@ { const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return 0; - return i->ctlflags; + return i->ctlflags.base(); } /* static */ bool ScriptIndustry::SetControlFlags(IndustryID industry_id, SQInteger control_flags) @@ -253,7 +253,7 @@ EnforceDeityMode(false); if (!IsValidIndustry(industry_id)) return false; - return ScriptObject::Command::Do(industry_id, (::IndustryControlFlags)control_flags & ::INDCTL_MASK); + return ScriptObject::Command::Do(industry_id, ::IndustryControlFlags(control_flags)); } /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id) diff --git a/src/script/api/script_industry.hpp b/src/script/api/script_industry.hpp index a23234368c..e35667fcf6 100644 --- a/src/script/api/script_industry.hpp +++ b/src/script/api/script_industry.hpp @@ -37,20 +37,20 @@ public: * When industry production change is evaluated, rolls to decrease are ignored. * This also prevents industry closure due to production dropping to the lowest level. */ - INDCTL_NO_PRODUCTION_DECREASE = ::INDCTL_NO_PRODUCTION_DECREASE, + INDCTL_NO_PRODUCTION_DECREASE = ::IndustryControlFlags{::IndustryControlFlag::NoProductionDecrease}.base(), /** * When industry production change is evaluated, rolls to increase are ignored. */ - INDCTL_NO_PRODUCTION_INCREASE = ::INDCTL_NO_PRODUCTION_INCREASE, + INDCTL_NO_PRODUCTION_INCREASE = ::IndustryControlFlags{::IndustryControlFlag::NoProductionIncrease}.base(), /** * Industry can not close regardless of production level or time since last delivery. * This does not prevent a closure already announced. */ - INDCTL_NO_CLOSURE = ::INDCTL_NO_CLOSURE, + INDCTL_NO_CLOSURE = ::IndustryControlFlags{::IndustryControlFlag::NoClosure}.base(), /** * Indicates that the production level of the industry is controlled by a game script. */ - INDCTL_EXTERNAL_PROD_LEVEL = ::INDCTL_EXTERNAL_PROD_LEVEL, + INDCTL_EXTERNAL_PROD_LEVEL = ::IndustryControlFlags{::IndustryControlFlag::ExternalProdLevel}.base(), }; /**