summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordP <dp@dpointer.org>2021-01-15 17:38:14 +0300
committerCharles Pigott <charlespigott@googlemail.com>2021-01-22 09:50:53 +0000
commitbab7de6cf23dce124ef8b1e284d0d1f3996dc8b8 (patch)
tree3686edd1b3ebb285e802a8a16b79552972c0b1b4
parent4b42ecb0f64c0044786412c15fb31b3705016375 (diff)
downloadopenttd-bab7de6cf23dce124ef8b1e284d0d1f3996dc8b8.tar.xz
Feature: Allow GameScripts to add additional text to Industry view window
-rw-r--r--src/command.cpp2
-rw-r--r--src/industry.h8
-rw-r--r--src/industry_cmd.cpp26
-rw-r--r--src/industry_gui.cpp7
-rw-r--r--src/saveload/industry_sl.cpp1
-rw-r--r--src/saveload/saveload.h1
-rw-r--r--src/script/api/game_changelog.hpp1
-rw-r--r--src/script/api/script_industry.cpp15
-rw-r--r--src/script/api/script_industry.hpp10
9 files changed, 60 insertions, 11 deletions
diff --git a/src/command.cpp b/src/command.cpp
index de5f8397d..7c7ca13e4 100644
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -266,7 +266,7 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdChangeServiceInt, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_CHANGE_SERVICE_INT
DEF_CMD(CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
- DEF_CMD(CmdIndustryCtrl, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_INDUSTRY_CTRL
+ DEF_CMD(CmdIndustryCtrl, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_INDUSTRY_CTRL
DEF_CMD(CmdSetCompanyManagerFace, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_MANAGER_FACE
DEF_CMD(CmdSetCompanyColour, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_COLOUR
diff --git a/src/industry.h b/src/industry.h
index 401051d5a..ce30114cb 100644
--- a/src/industry.h
+++ b/src/industry.h
@@ -33,6 +33,13 @@ 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.
@@ -91,6 +98,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
byte selected_layout; ///< Which tile layout was used when creating the industry
Owner exclusive_supplier; ///< Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Owner exclusive_consumer; ///< Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
+ std::string text; ///< General text with additional information.
uint16 random; ///< Random value used for randomisation of all kinds of things
diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp
index 69d67b9d8..20c38e545 100644
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -40,6 +40,8 @@
#include "object_base.h"
#include "game/game.hpp"
#include "error.h"
+#include "cmd_helper.h"
+#include "string_func.h"
#include "table/strings.h"
#include "table/industry_land.h"
@@ -2063,16 +2065,13 @@ CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
* @param flags Type of operation.
* @param p1 IndustryID
* @param p2 various bitstuffed elements
- * - p2 = (bit 0 - 7) - action to perform:
- * 0 = set control flags
- * 1 = set exclusive supplier
- * 2 = set exclusive consumer
+ * - p2 = (bit 0 - 7) - IndustryAction to perform
* - p2 = (bit 8 - 15) - IndustryControlFlags
* (only used with set control flags)
* - p2 = (bit 16 - 23) - 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 unused
+ * @param text - Additional industry text (only used with set text action)
* @return Empty cost or an error.
*/
CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
@@ -2082,10 +2081,10 @@ CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
Industry *ind = Industry::GetIfValid(p1);
if (ind == nullptr) return CMD_ERROR;
- uint8 action = GB(p2, 0, 8);
+ auto action = static_cast<IndustryAction>(GB(p2, 0, 8));
switch (action) {
- case 0: {
+ case IndustryAction::SetControlFlags: {
IndustryControlFlags ctlflags = (IndustryControlFlags)GB(p2, 8, 8) & INDCTL_MASK;
if (flags & DC_EXEC) ind->ctlflags = ctlflags;
@@ -2093,15 +2092,15 @@ CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
break;
}
- case 1:
- case 2: {
+ case IndustryAction::SetExclusiveSupplier:
+ case IndustryAction::SetExclusiveConsumer: {
Owner company_id = (Owner)GB(p2, 16, 8);
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 == 1) {
+ if (action == IndustryAction::SetExclusiveSupplier) {
ind->exclusive_supplier = company_id;
} else {
ind->exclusive_consumer = company_id;
@@ -2111,6 +2110,13 @@ CommandCost CmdIndustryCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
break;
}
+ case IndustryAction::SetText: {
+ ind->text.clear();
+ if (!StrEmpty(text)) ind->text = text;
+ InvalidateWindowData(WC_INDUSTRY_VIEW, ind->index);
+ break;
+ }
+
default:
NOT_REACHED();
}
diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp
index a89d4b4c5..58f4a6097 100644
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -913,6 +913,13 @@ public:
}
}
}
+
+ if (!i->text.empty()) {
+ SetDParamStr(0, i->text.c_str());
+ y += WD_PAR_VSEP_WIDE;
+ y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
+ }
+
return y + WD_FRAMERECT_BOTTOM;
}
diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp
index 99e25856c..6f41e3455 100644
--- a/src/saveload/industry_sl.cpp
+++ b/src/saveload/industry_sl.cpp
@@ -72,6 +72,7 @@ static const SaveLoad _industry_desc[] = {
SLE_CONDNULL(1, SLV_82, SLV_197), // random_triggers
SLE_CONDVAR(Industry, random, SLE_UINT16, SLV_82, SL_MAX_VERSION),
+ SLE_CONDSSTR(Industry, text, SLE_STR | SLF_ALLOW_CONTROL, SLV_INDUSTRY_TEXT, SL_MAX_VERSION),
SLE_CONDNULL(32, SLV_2, SLV_144), // old reserved space
diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h
index fb7761128..ac4658455 100644
--- a/src/saveload/saveload.h
+++ b/src/saveload/saveload.h
@@ -323,6 +323,7 @@ enum SaveLoadVersion : uint16 {
SLV_GS_INDUSTRY_CONTROL, ///< 287 PR#7912 and PR#8115 GS industry control.
SLV_VEH_MOTION_COUNTER, ///< 288 PR#8591 Desync safe motion counter
+ SLV_INDUSTRY_TEXT, ///< 289 PR#8576 Additional GS text for industries.
SL_MAX_VERSION, ///< Highest possible saveload version
};
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index 82f0c0523..edc588407 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -30,6 +30,7 @@
* \li GSIndustry::SetControlFlags
* \li GSIndustry::SetExclusiveConsumer
* \li GSIndustry::SetExclusiveSupplier
+ * \li GSIndustry::SetText
* \li GSStoryPage::MakePushButtonReference
* \li GSStoryPage::MakeTileButtonReference
* \li GSStoryPage::MakeVehicleButtonReference
diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp
index 14ba8e402..0a5ca98d3 100644
--- a/src/script/api/script_industry.cpp
+++ b/src/script/api/script_industry.cpp
@@ -15,6 +15,7 @@
#include "script_map.hpp"
#include "../../company_base.h"
#include "../../industry.h"
+#include "../../string_func.h"
#include "../../strings_func.h"
#include "../../station_base.h"
#include "../../newgrf_industries.h"
@@ -47,6 +48,20 @@
return GetString(STR_INDUSTRY_NAME);
}
+/* 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::DoCommand(0, industry_id, static_cast<uint32>(IndustryAction::SetText), CMD_INDUSTRY_CTRL, encoded_text);
+}
+
/* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
{
if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
diff --git a/src/script/api/script_industry.hpp b/src/script/api/script_industry.hpp
index dac3d32fd..95133da0e 100644
--- a/src/script/api/script_industry.hpp
+++ b/src/script/api/script_industry.hpp
@@ -82,6 +82,16 @@ public:
static char *GetName(IndustryID industry_id);
/**
+ * Set the custom text of an industry, shown in the GUI.
+ * @param industry_id The industry to set the custom text of.
+ * @param text The text to set it to (can be either a raw string, or a ScriptText object). If null is passed, the text will be removed.
+ * @pre IsValidIndustry(industry_id).
+ * @return True if the action succeeded.
+ * @api -ai
+ */
+ static bool SetText(IndustryID industry_id, Text *text);
+
+ /**
* See whether an industry currently accepts a certain cargo.
* @param industry_id The index of the industry.
* @param cargo_id The index of the cargo.