# HG changeset patch # User dP # Date 2023-02-14 10:29:11 # Node ID c9ca06904031904cacd9935654a43f60ffdb61cc # Parent b9c8507af6237700902e09c12563129d7dd91505 Codechange: Decouple INDUSTRY_CTRL into separate commands (#10475) diff --git a/src/command_type.h b/src/command_type.h --- a/src/command_type.h +++ b/src/command_type.h @@ -232,7 +232,9 @@ enum Commands : uint16 { CMD_CHANGE_SERVICE_INT, ///< change the server interval of a vehicle CMD_BUILD_INDUSTRY, ///< build a new industry - CMD_INDUSTRY_CTRL, ///< change industry properties + CMD_INDUSTRY_SET_FLAGS, ///< change industry control flags + CMD_INDUSTRY_SET_EXCLUSIVITY, ///< change industry exclusive consumer/supplier + CMD_INDUSTRY_SET_TEXT, ///< change additional text for the industry CMD_SET_COMPANY_MANAGER_FACE, ///< set the manager's face of the company CMD_SET_COMPANY_COLOUR, ///< set the colour of the company diff --git a/src/industry.h b/src/industry.h --- a/src/industry.h +++ b/src/industry.h @@ -33,13 +33,6 @@ enum ProductionLevels { PRODLEVEL_MAXIMUM = 0x80, ///< the industry is running at full speed }; -enum class IndustryAction : byte { - SetControlFlags = 0, ///< Set IndustryControlFlags - SetExclusiveSupplier = 1, ///< Set exclusive supplier - SetExclusiveConsumer = 2, ///< Set exclusive consumer - SetText = 3, ///< Set additional text -}; - /** * Flags to control/override the behaviour of an industry. * These flags are controlled by game scripts. diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2092,56 +2092,73 @@ CommandCost CmdBuildIndustry(DoCommandFl } /** - * Change industry properties + * Set industry control flags. * @param flags Type of operation. * @param ind_id IndustryID - * @param action IndustryAction to perform - * @param ctlflags IndustryControlFlags (only used with set control flags) + * @param ctlflags IndustryControlFlags + * @return Empty cost or an error. + */ +CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, IndustryControlFlags ctlflags) +{ + if (_current_company != OWNER_DEITY) return CMD_ERROR; + + Industry *ind = Industry::GetIfValid(ind_id); + if (ind == nullptr) return CMD_ERROR; + + if (flags & DC_EXEC) ind->ctlflags = ctlflags & INDCTL_MASK; + + return CommandCost(); +} + +/** + * Change exclusive consumer or supplier for the industry. + * @param flags Type of operation. + * @param ind_id IndustryID * @param company_id CompanyID to set or INVALID_OWNER (available to everyone) or * OWNER_NONE (neutral stations only) or OWNER_DEITY (no one) - * (only used with set exclusive supplier / consumer) - * @param text - Additional industry text (only used with set text action) + * @param consumer Set exclusive consumer if true, supplier if false. * @return Empty cost or an error. */ -CommandCost CmdIndustryCtrl(DoCommandFlag flags, IndustryID ind_id, IndustryAction action, IndustryControlFlags ctlflags, Owner company_id, const std::string &text) +CommandCost CmdIndustrySetExclusivity(DoCommandFlag flags, IndustryID ind_id, Owner company_id, bool consumer) { if (_current_company != OWNER_DEITY) return CMD_ERROR; Industry *ind = Industry::GetIfValid(ind_id); if (ind == nullptr) return CMD_ERROR; - switch (action) { - case IndustryAction::SetControlFlags: { - if (flags & DC_EXEC) ind->ctlflags = ctlflags & INDCTL_MASK; - - break; + if (company_id != OWNER_NONE && company_id != INVALID_OWNER && company_id != OWNER_DEITY + && !Company::IsValidID(company_id)) return CMD_ERROR; + + if (flags & DC_EXEC) { + if (consumer) { + ind->exclusive_consumer = company_id; + } else { + ind->exclusive_supplier = company_id; } - - case IndustryAction::SetExclusiveSupplier: - case IndustryAction::SetExclusiveConsumer: { - if (company_id != OWNER_NONE && company_id != INVALID_OWNER && company_id != OWNER_DEITY - && !Company::IsValidID(company_id)) return CMD_ERROR; - - if (flags & DC_EXEC) { - if (action == IndustryAction::SetExclusiveSupplier) { - ind->exclusive_supplier = company_id; - } else { - ind->exclusive_consumer = company_id; - } - } - - break; - } - - case IndustryAction::SetText: { - ind->text.clear(); - if (!text.empty()) ind->text = text; - InvalidateWindowData(WC_INDUSTRY_VIEW, ind->index); - break; - } - - default: - return CMD_ERROR; + } + + + return CommandCost(); +} + +/** + * Change additional industry text. + * @param flags Type of operation. + * @param ind_id IndustryID + * @param text - Additional industry text. + * @return Empty cost or an error. + */ +CommandCost CmdIndustrySetText(DoCommandFlag flags, IndustryID ind_id, const std::string &text) +{ + if (_current_company != OWNER_DEITY) return CMD_ERROR; + + Industry *ind = Industry::GetIfValid(ind_id); + if (ind == nullptr) return CMD_ERROR; + + if (flags & DC_EXEC) { + ind->text.clear(); + if (!text.empty()) ind->text = text; + InvalidateWindowData(WC_INDUSTRY_VIEW, ind->index); } return CommandCost(); diff --git a/src/industry_cmd.h b/src/industry_cmd.h --- a/src/industry_cmd.h +++ b/src/industry_cmd.h @@ -14,14 +14,17 @@ #include "company_type.h" #include "industry_type.h" -enum class IndustryAction : byte; enum IndustryControlFlags : byte; CommandCost CmdBuildIndustry(DoCommandFlag flags, TileIndex tile, IndustryType it, uint32 first_layout, bool fund, uint32 seed); -CommandCost CmdIndustryCtrl(DoCommandFlag flags, IndustryID ind_id, IndustryAction action, IndustryControlFlags ctlflags, Owner company_id, const std::string &text); +CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, IndustryControlFlags ctlflags); +CommandCost CmdIndustrySetExclusivity(DoCommandFlag flags, IndustryID ind_id, Owner company_id, bool consumer); +CommandCost CmdIndustrySetText(DoCommandFlag flags, IndustryID ind_id, const std::string &text); -DEF_CMD_TRAIT(CMD_BUILD_INDUSTRY, CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION) -DEF_CMD_TRAIT(CMD_INDUSTRY_CTRL, CmdIndustryCtrl, CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT) +DEF_CMD_TRAIT(CMD_BUILD_INDUSTRY, CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION) +DEF_CMD_TRAIT(CMD_INDUSTRY_SET_FLAGS, CmdIndustrySetFlags, CMD_DEITY, CMDT_OTHER_MANAGEMENT) +DEF_CMD_TRAIT(CMD_INDUSTRY_SET_EXCLUSIVITY, CmdIndustrySetExclusivity, CMD_DEITY, CMDT_OTHER_MANAGEMENT) +DEF_CMD_TRAIT(CMD_INDUSTRY_SET_TEXT, CmdIndustrySetText, CMD_DEITY | CMD_STR_CTRL, CMDT_OTHER_MANAGEMENT) void CcBuildIndustry(Commands cmd, const CommandCost &result, TileIndex tile, IndustryType indtype, uint32, bool, uint32); diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -60,7 +60,7 @@ } EnforcePrecondition(false, IsValidIndustry(industry_id)); - return ScriptObject::Command::Do(industry_id, IndustryAction::SetText, INDCTL_NONE, INVALID_OWNER, std::string{ encoded_text ? encoded_text : "" }); + return ScriptObject::Command::Do(industry_id, std::string{ encoded_text ? encoded_text : "" }); } /* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id) @@ -258,7 +258,7 @@ bool ScriptIndustry::SetControlFlags(Ind if (ScriptObject::GetCompany() != OWNER_DEITY) return false; if (!IsValidIndustry(industry_id)) return false; - return ScriptObject::Command::Do(industry_id, IndustryAction::SetControlFlags, (::IndustryControlFlags)control_flags & ::INDCTL_MASK, INVALID_OWNER, {}); + return ScriptObject::Command::Do(industry_id, (::IndustryControlFlags)control_flags & ::INDCTL_MASK); } /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id) @@ -277,7 +277,7 @@ bool ScriptIndustry::SetControlFlags(Ind auto company = ScriptCompany::ResolveCompanyID(company_id); ::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company); - return ScriptObject::Command::Do(industry_id, IndustryAction::SetExclusiveSupplier, INDCTL_NONE, owner, {}); + return ScriptObject::Command::Do(industry_id, owner, false); } /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveConsumer(IndustryID industry_id) @@ -296,5 +296,5 @@ bool ScriptIndustry::SetControlFlags(Ind auto company = ScriptCompany::ResolveCompanyID(company_id); ::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company); - return ScriptObject::Command::Do(industry_id, IndustryAction::SetExclusiveConsumer, INDCTL_NONE, owner, {}); + return ScriptObject::Command::Do(industry_id, owner, true); }