Changeset - r26879:c9ca06904031
[Not reviewed]
master
0 5 0
dP - 16 months ago 2023-02-14 10:29:11
dp@dpointer.org
Codechange: Decouple INDUSTRY_CTRL into separate commands (#10475)
5 files changed with 68 insertions and 53 deletions:
0 comments (0 inline, 0 general)
src/command_type.h
Show inline comments
 
@@ -223,25 +223,27 @@ enum Commands : uint16 {
 
	CMD_FORCE_TRAIN_PROCEED,          ///< proceed a train to pass a red signal
 
	CMD_REVERSE_TRAIN_DIRECTION,      ///< turn a train around
 

	
 
	CMD_CLEAR_ORDER_BACKUP,           ///< clear the order backup of a given user/tile
 
	CMD_MODIFY_ORDER,                 ///< modify an order (like set full-load)
 
	CMD_SKIP_TO_ORDER,                ///< skip an order to the next of specific one
 
	CMD_DELETE_ORDER,                 ///< delete an order
 
	CMD_INSERT_ORDER,                 ///< insert a new order
 

	
 
	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
 

	
 
	CMD_INCREASE_LOAN,                ///< increase the loan from the bank
 
	CMD_DECREASE_LOAN,                ///< decrease the loan from the bank
 

	
 
	CMD_WANT_ENGINE_PREVIEW,          ///< confirm the preview of an engine
 
	CMD_ENGINE_CTRL,                  ///< control availability of the engine for companies
 

	
 
	CMD_RENAME_VEHICLE,               ///< rename a whole vehicle
 
	CMD_RENAME_ENGINE,                ///< rename a engine (in the engine list)
src/industry.h
Show inline comments
 
@@ -24,31 +24,24 @@ extern IndustryPool _industry_pool;
 
/**
 
 * Production level maximum, minimum and default values.
 
 * It is not a value been really used in order to change, but rather an indicator
 
 * of how the industry is behaving.
 
 */
 
enum ProductionLevels {
 
	PRODLEVEL_CLOSURE = 0x00,  ///< signal set to actually close the industry
 
	PRODLEVEL_MINIMUM = 0x04,  ///< below this level, the industry is set to be closing
 
	PRODLEVEL_DEFAULT = 0x10,  ///< default level set when the industry is created
 
	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.
 
 */
 
enum IndustryControlFlags : byte {
 
	/** No flags in effect */
 
	INDCTL_NONE                   = 0,
 
	/** When industry production change is evaluated, rolls to decrease are ignored. */
 
	INDCTL_NO_PRODUCTION_DECREASE = 1 << 0,
 
	/** When industry production change is evaluated, rolls to increase are ignored. */
 
	INDCTL_NO_PRODUCTION_INCREASE = 1 << 1,
 
	/**
src/industry_cmd.cpp
Show inline comments
 
@@ -2083,74 +2083,91 @@ CommandCost CmdBuildIndustry(DoCommandFl
 
		/* If it still failed, there's no suitable layout to build here, return the error */
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	if ((flags & DC_EXEC) && ind != nullptr && _game_mode != GM_EDITOR) {
 
		AdvertiseIndustryOpening(ind);
 
	}
 

	
 
	return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
 
}
 

	
 
/**
 
 * 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();
 
}
 

	
 
/**
 
 * Create a new industry of random layout.
 
 * @param tile The location to build the industry.
 
 * @param type The industry type to build.
 
 * @param creation_type The circumstances the industry is created under.
 
 * @return the created industry or nullptr if it failed.
 
 */
src/industry_cmd.h
Show inline comments
 
@@ -5,24 +5,27 @@
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file industry_cmd.h Command definitions related to industries. */
 

	
 
#ifndef INDUSTRY_CMD_H
 
#define INDUSTRY_CMD_H
 

	
 
#include "command_type.h"
 
#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);
 

	
 
#endif /* INDUSTRY_CMD_H */
src/script/api/script_industry.cpp
Show inline comments
 
@@ -51,25 +51,25 @@
 

	
 
/* static */ bool ScriptIndustry::SetText(IndustryID industry_id, Text *text)
 
{
 
	CCountedPtr<Text> counter(text);
 

	
 
	const char *encoded_text = nullptr;
 
	if (text != nullptr) {
 
		encoded_text = text->GetEncodedText();
 
		EnforcePreconditionEncodedText(false, encoded_text);
 
	}
 
	EnforcePrecondition(false, IsValidIndustry(industry_id));
 

	
 
	return ScriptObject::Command<CMD_INDUSTRY_CTRL>::Do(industry_id, IndustryAction::SetText, INDCTL_NONE, INVALID_OWNER, std::string{ encoded_text ? encoded_text : "" });
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_TEXT>::Do(industry_id, std::string{ encoded_text ? encoded_text : "" });
 
}
 

	
 
/* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
 
{
 
	if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
 
	if (!ScriptCargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED;
 

	
 
	Industry *i = ::Industry::Get(industry_id);
 

	
 
	for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
 
		if (i->accepts_cargo[j] == cargo_id) {
 
			if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED;
 
@@ -249,52 +249,52 @@ ScriptDate::Date ScriptIndustry::GetCarg
 
uint32 ScriptIndustry::GetControlFlags(IndustryID industry_id)
 
{
 
	Industry *i = Industry::GetIfValid(industry_id);
 
	if (i == nullptr) return 0;
 
	return i->ctlflags;
 
}
 

	
 
bool ScriptIndustry::SetControlFlags(IndustryID industry_id, uint32 control_flags)
 
{
 
	if (ScriptObject::GetCompany() != OWNER_DEITY) return false;
 
	if (!IsValidIndustry(industry_id)) return false;
 

	
 
	return ScriptObject::Command<CMD_INDUSTRY_CTRL>::Do(industry_id, IndustryAction::SetControlFlags, (::IndustryControlFlags)control_flags & ::INDCTL_MASK, INVALID_OWNER, {});
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_FLAGS>::Do(industry_id, (::IndustryControlFlags)control_flags & ::INDCTL_MASK);
 
}
 

	
 
/* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id)
 
{
 
	if (!IsValidIndustry(industry_id)) return ScriptCompany::COMPANY_INVALID;
 

	
 
	auto company_id = ::Industry::Get(industry_id)->exclusive_supplier;
 
	if (!::Company::IsValidID(company_id)) return ScriptCompany::COMPANY_INVALID;
 

	
 
	return (ScriptCompany::CompanyID)((byte)company_id);
 
}
 

	
 
/* static */ bool ScriptIndustry::SetExclusiveSupplier(IndustryID industry_id, ScriptCompany::CompanyID company_id)
 
{
 
	EnforcePrecondition(false, IsValidIndustry(industry_id));
 

	
 
	auto company = ScriptCompany::ResolveCompanyID(company_id);
 
	::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company);
 
	return ScriptObject::Command<CMD_INDUSTRY_CTRL>::Do(industry_id, IndustryAction::SetExclusiveSupplier, INDCTL_NONE, owner, {});
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_EXCLUSIVITY>::Do(industry_id, owner, false);
 
}
 

	
 
/* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveConsumer(IndustryID industry_id)
 
{
 
	if (!IsValidIndustry(industry_id)) return ScriptCompany::COMPANY_INVALID;
 

	
 
	auto company_id = ::Industry::Get(industry_id)->exclusive_consumer;
 
	if (!::Company::IsValidID(company_id)) return ScriptCompany::COMPANY_INVALID;
 

	
 
	return (ScriptCompany::CompanyID)((byte)company_id);
 
}
 

	
 
/* static */ bool ScriptIndustry::SetExclusiveConsumer(IndustryID industry_id, ScriptCompany::CompanyID company_id)
 
{
 
	EnforcePrecondition(false, IsValidIndustry(industry_id));
 

	
 
	auto company = ScriptCompany::ResolveCompanyID(company_id);
 
	::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company);
 
	return ScriptObject::Command<CMD_INDUSTRY_CTRL>::Do(industry_id, IndustryAction::SetExclusiveConsumer, INDCTL_NONE, owner, {});
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_EXCLUSIVITY>::Do(industry_id, owner, true);
 
}
0 comments (0 inline, 0 general)