Changeset - r27852:2937e4e53677
[Not reviewed]
master
0 5 0
Michael Lutz - 16 months ago 2023-08-06 14:05:04
michi@icosahedron.de
Add: [Script] Custom news message text for industry SetProductionLevel.
5 files changed with 18 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -2120,15 +2120,16 @@ CommandCost CmdIndustrySetFlags(DoComman
 
/**
 
 * Set industry production.
 
 * @param flags Type of operation.
 
 * @param ind_id IndustryID
 
 * @param prod_level Production level.
 
 * @param show_news Show a news message on production change.
 
 * @param custom_news Custom news message text.
 
 * @return Empty cost or an error.
 
 */
 
CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byte prod_level, bool show_news)
 
CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byte prod_level, bool show_news, const std::string &custom_news)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 
	if (prod_level < PRODLEVEL_MINIMUM || prod_level > PRODLEVEL_MAXIMUM) return CMD_ERROR;
 

	
 
	Industry *ind = Industry::GetIfValid(ind_id);
 
	if (ind == nullptr) return CMD_ERROR;
 
@@ -2137,12 +2138,13 @@ CommandCost CmdIndustrySetProduction(DoC
 
		StringID str = STR_NULL;
 
		if (prod_level > ind->prod_level) {
 
			str = GetIndustrySpec(ind->type)->production_up_text;
 
		} else if (prod_level < ind->prod_level) {
 
			str = GetIndustrySpec(ind->type)->production_down_text;
 
		}
 
		if (prod_level != ind->prod_level && !custom_news.empty()) str = STR_NEWS_CUSTOM_ITEM;
 

	
 
		ind->ctlflags |= INDCTL_EXTERNAL_PROD_LEVEL;
 
		ind->prod_level = prod_level;
 
		ind->RecomputeProductionMultipliers();
 

	
 
		/* Show news message if requested. */
 
@@ -2153,20 +2155,24 @@ CommandCost CmdIndustrySetProduction(DoC
 
				case 1: nt = NT_INDUSTRY_OTHER;   break;
 
				case 2: nt = NT_INDUSTRY_COMPANY; break;
 
				default: NOT_REACHED();
 
			}
 

	
 
			/* Set parameters of news string */
 
			if (str > STR_LAST_STRINGID) {
 
			NewsAllocatedData *data = nullptr;
 
			if (str == STR_NEWS_CUSTOM_ITEM) {
 
				NewsStringData *news = new NewsStringData(custom_news);
 
				SetDParamStr(0, news->string);
 
			} else if (str > STR_LAST_STRINGID) {
 
				SetDParam(0, STR_TOWN_NAME);
 
				SetDParam(1, ind->town->index);
 
				SetDParam(2, GetIndustrySpec(ind->type)->name);
 
			} else {
 
				SetDParam(0, ind->index);
 
			}
 
			AddIndustryNewsItem(str, nt, ind->index);
 
			AddIndustryNewsItem(str, nt, ind->index, data);
 
		}
 
	}
 

	
 
	return CommandCost();
 
}
 

	
src/industry_cmd.h
Show inline comments
 
@@ -17,13 +17,13 @@
 
enum IndustryControlFlags : byte;
 

	
 
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 CmdIndustrySetExclusivity(DoCommandFlag flags, IndustryID ind_id, Owner company_id, bool consumer);
 
CommandCost CmdIndustrySetText(DoCommandFlag flags, IndustryID ind_id, const std::string &text);
 
CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byte prod_level, bool show_news);
 
CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byte prod_level, bool show_news, const std::string &text);
 

	
 
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)
 
DEF_CMD_TRAIT(CMD_INDUSTRY_SET_PRODUCTION, CmdIndustrySetProduction, CMD_DEITY, CMDT_OTHER_MANAGEMENT)
src/news_func.h
Show inline comments
 
@@ -44,15 +44,15 @@ static inline void AddVehicleAdviceNewsI
 

	
 
static inline void AddTileNewsItem(StringID string, NewsType type, TileIndex tile, const NewsAllocatedData *data = nullptr, StationID station = INVALID_STATION)
 
{
 
	AddNewsItem(string, type, NF_NO_TRANSPARENT | NF_SHADE | NF_THIN, NR_TILE, static_cast<uint32_t>(tile), station == INVALID_STATION ? NR_NONE : NR_STATION, station, data);
 
}
 

	
 
static inline void AddIndustryNewsItem(StringID string, NewsType type, IndustryID industry)
 
static inline void AddIndustryNewsItem(StringID string, NewsType type, IndustryID industry, const NewsAllocatedData *data = nullptr)
 
{
 
	AddNewsItem(string, type, NF_NO_TRANSPARENT | NF_SHADE | NF_THIN, NR_INDUSTRY, industry);
 
	AddNewsItem(string, type, NF_NO_TRANSPARENT | NF_SHADE | NF_THIN, NR_INDUSTRY, industry, NR_NONE, UINT32_MAX, data);
 
}
 

	
 
void NewsLoop();
 
void InitNewsItemStructs();
 

	
 
extern const NewsItem *_statusbar_news_item;
src/script/api/script_industry.cpp
Show inline comments
 
@@ -299,14 +299,16 @@
 
{
 
	Industry *i = Industry::GetIfValid(industry_id);
 
	if (i == nullptr) return 0;
 
	return i->prod_level;
 
}
 

	
 
/* static */ bool ScriptIndustry::SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news)
 
/* static */ bool ScriptIndustry::SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news, Text *custom_news)
 
{
 
	CCountedPtr<Text> counter(custom_news);
 

	
 
	EnforceDeityMode(false);
 
	EnforcePrecondition(false, IsValidIndustry(industry_id));
 
	EnforcePrecondition(false, prod_level >= PRODLEVEL_MINIMUM && prod_level <= PRODLEVEL_MAXIMUM);
 

	
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_PRODUCTION>::Do(industry_id, prod_level, show_news);
 
	return ScriptObject::Command<CMD_INDUSTRY_SET_PRODUCTION>::Do(industry_id, prod_level, show_news, custom_news != nullptr ? custom_news->GetEncodedText() : std::string{});
 
}
src/script/api/script_industry.hpp
Show inline comments
 
@@ -338,16 +338,17 @@ public:
 
	 * Sets the current production level of an industry.
 
	 * @note Setting the production level automatically sets the control flag INDCTL_EXTERNAL_PROD_LEVEL if it wasn't already set.
 
	 *     Normal production behaviour can be restored by clearing the control flag.
 
	 * @param industry_id The index of the industry.
 
	 * @param prod_level The production level to set.
 
	 * @param show_news If set to true and the production changed, generate a production change news message. If set to false, no news message is shown.
 
	 * @param custom_news Custom news message text to override the default news text with. Pass null to use the default text. Only used if \c show_news is set to true.
 
	 * @pre IsValidIndustry(industry_id).
 
	 * @pre ScriptCompanyMode::IsDeity().
 
	 * @pre prod_level >= 4 && prod_level <= 128.
 
	 * @return True if the action succeeded.
 
	 * @api -ai
 
	 */
 
	static bool SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news);
 
	static bool SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news, Text *custom_news);
 
};
 

	
 
#endif /* SCRIPT_INDUSTRY_HPP */
0 comments (0 inline, 0 general)