Skip to content

Commit

Permalink
Add: [Script] Custom news message text for industry SetProductionLevel.
Browse files Browse the repository at this point in the history
  • Loading branch information
michicc committed Sep 2, 2023
1 parent 213b501 commit bbf9b2f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/industry_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2123,9 +2123,10 @@ CommandCost CmdIndustrySetFlags(DoCommandFlag flags, IndustryID ind_id, Industry
* @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;
Expand All @@ -2140,6 +2141,7 @@ CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byt
} 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;
Expand All @@ -2156,14 +2158,18 @@ CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, byt
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/industry_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CommandCost CmdBuildIndustry(DoCommandFlag flags, TileIndex tile, IndustryType i
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)
Expand Down
4 changes: 2 additions & 2 deletions src/news_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ static inline void AddTileNewsItem(StringID string, NewsType type, TileIndex til
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();
Expand Down
6 changes: 4 additions & 2 deletions src/script/api/script_industry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,13 @@
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{});
}
3 changes: 2 additions & 1 deletion src/script/api/script_industry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,14 @@ class ScriptIndustry : public ScriptObject {
* @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 on commit bbf9b2f

Please sign in to comment.