Codechange: Use EnumBitSet for IndustryControlFlags.

This commit is contained in:
Peter Nelson 2025-02-06 02:52:07 +00:00 committed by Peter Nelson
parent 965a45812a
commit 50b384032d
6 changed files with 26 additions and 29 deletions

View File

@ -39,23 +39,20 @@ static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80; ///< the industry is running
* Flags to control/override the behaviour of an industry. * Flags to control/override the behaviour of an industry.
* These flags are controlled by game scripts. * These flags are controlled by game scripts.
*/ */
enum IndustryControlFlags : uint8_t { enum class IndustryControlFlag : uint8_t {
/** No flags in effect */
INDCTL_NONE = 0,
/** When industry production change is evaluated, rolls to decrease are ignored. */ /** 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. */ /** 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. * Industry can not close regardless of production level or time since last delivery.
* This does not prevent a closure already announced. */ * 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. */ /** Indicates that the production level of the industry is externally controlled. */
INDCTL_EXTERNAL_PROD_LEVEL = 1 << 3, ExternalProdLevel = 3,
/** Mask of all flags set */ End,
INDCTL_MASK = INDCTL_NO_PRODUCTION_DECREASE | INDCTL_NO_PRODUCTION_INCREASE | INDCTL_NO_CLOSURE | INDCTL_EXTERNAL_PROD_LEVEL,
}; };
DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags); using IndustryControlFlags = EnumBitSet<IndustryControlFlag, uint8_t, IndustryControlFlag::End>;
static const int THIS_MONTH = 0; static const int THIS_MONTH = 0;
static const int LAST_MONTH = 1; static const int LAST_MONTH = 1;

View File

@ -1804,7 +1804,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
i->was_cargo_delivered = false; i->was_cargo_delivered = false;
i->last_prod_year = TimerGameEconomy::year; i->last_prod_year = TimerGameEconomy::year;
i->founder = founder; i->founder = founder;
i->ctlflags = INDCTL_NONE; i->ctlflags = {};
i->construction_date = TimerGameCalendar::date; i->construction_date = TimerGameCalendar::date;
i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR : 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); Industry *ind = Industry::GetIfValid(ind_id);
if (ind == nullptr) return CMD_ERROR; 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(); 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; 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->prod_level = prod_level;
ind->RecomputeProductionMultipliers(); ind->RecomputeProductionMultipliers();
@ -2855,7 +2856,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
} }
} }
} else if (_settings_game.economy.type == ET_SMOOTH) { } 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) { for (auto &p : i->produced) {
if (!IsValidCargoType(p.cargo)) continue; if (!IsValidCargoType(p.cargo)) continue;
uint32_t r = Random(); 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 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.Test(IndustryControlFlag::NoProductionDecrease) && new_prod < old_prod) continue;
if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && 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 */ /* Do not stop closing the industry when it has the lowest possible production rate */
if (new_prod == old_prod && old_prod > 1) { 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 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.Test(IndustryControlFlag::NoProductionDecrease) && (div > 0 || increment < 0)) return;
if ((i->ctlflags & INDCTL_NO_PRODUCTION_INCREASE) && (mul > 0 || increment > 0)) return; if (i->ctlflags.Test(IndustryControlFlag::NoProductionIncrease) && (mul > 0 || increment > 0)) return;
if (i->ctlflags & INDCTL_EXTERNAL_PROD_LEVEL) { if (i->ctlflags.Test(IndustryControlFlag::ExternalProdLevel)) {
div = 0; div = 0;
mul = 0; mul = 0;
increment = 0; increment = 0;
@ -2959,7 +2960,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
if (recalculate_multipliers) i->RecomputeProductionMultipliers(); if (recalculate_multipliers) i->RecomputeProductionMultipliers();
/* Close if needed and allowed */ /* 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; i->prod_level = PRODLEVEL_CLOSURE;
SetWindowDirty(WC_INDUSTRY_VIEW, i->index); SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
str = indspec->closure_text; str = indspec->closure_text;

View File

@ -13,8 +13,7 @@
#include "command_type.h" #include "command_type.h"
#include "company_type.h" #include "company_type.h"
#include "industry_type.h" #include "industry_type.h"
#include "industry.h"
enum IndustryControlFlags : uint8_t;
CommandCost CmdBuildIndustry(DoCommandFlag flags, TileIndex tile, IndustryType it, uint32_t first_layout, bool fund, uint32_t seed); 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); CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, IndustryControlFlags ctlflags);

View File

@ -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) case 0x46: return this->industry->construction_date.base(); // Date when built - long format - (in days)
/* Override flags from GS */ /* Override flags from GS */
case 0x47: return this->industry->ctlflags; case 0x47: return this->industry->ctlflags.base();
/* Get industry ID at offset param */ /* Get industry ID at offset param */
case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro.grffile->grfid); case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro.grffile->grfid);

View File

@ -245,7 +245,7 @@
{ {
const Industry *i = Industry::GetIfValid(industry_id); const Industry *i = Industry::GetIfValid(industry_id);
if (i == nullptr) return 0; if (i == nullptr) return 0;
return i->ctlflags; return i->ctlflags.base();
} }
/* static */ bool ScriptIndustry::SetControlFlags(IndustryID industry_id, SQInteger control_flags) /* static */ bool ScriptIndustry::SetControlFlags(IndustryID industry_id, SQInteger control_flags)
@ -253,7 +253,7 @@
EnforceDeityMode(false); EnforceDeityMode(false);
if (!IsValidIndustry(industry_id)) return false; if (!IsValidIndustry(industry_id)) return false;
return ScriptObject::Command<CMD_INDUSTRY_SET_FLAGS>::Do(industry_id, (::IndustryControlFlags)control_flags & ::INDCTL_MASK); return ScriptObject::Command<CMD_INDUSTRY_SET_FLAGS>::Do(industry_id, ::IndustryControlFlags(control_flags));
} }
/* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id) /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id)

View File

@ -37,20 +37,20 @@ public:
* When industry production change is evaluated, rolls to decrease are ignored. * When industry production change is evaluated, rolls to decrease are ignored.
* This also prevents industry closure due to production dropping to the lowest level. * 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. * 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. * Industry can not close regardless of production level or time since last delivery.
* This does not prevent a closure already announced. * 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. * 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(),
}; };
/** /**