diff options
Diffstat (limited to 'src/script/api')
-rw-r--r-- | src/script/api/script_object.cpp | 20 | ||||
-rw-r--r-- | src/script/api/script_object.hpp | 10 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 62a71574f..ac06c4c2d 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -83,6 +83,23 @@ ScriptObject::ActiveInstance::~ActiveInstance() return GetStorage()->mode_instance; } +/* static */ void ScriptObject::SetLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd) +{ + GetStorage()->last_tile = tile; + GetStorage()->last_p1 = p1; + GetStorage()->last_p2 = p2; + GetStorage()->last_cmd = cmd; +} + +/* static */ bool ScriptObject::CheckLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd) +{ + if (GetStorage()->last_tile != tile) return false; + if (GetStorage()->last_p1 != p1) return false; + if (GetStorage()->last_p2 != p2) return false; + if (GetStorage()->last_cmd != cmd) return false; + return true; +} + /* static */ void ScriptObject::SetDoCommandCosts(Money value) { GetStorage()->costs = CommandCost(value); @@ -304,6 +321,9 @@ ScriptObject::ActiveInstance::~ActiveInstance() /* Only set p2 when the command does not come from the network. */ if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX; + /* Store the command for command callback validation. */ + if (!estimate_only && _networking && !_generating_world) SetLastCommand(tile, p1, p2, cmd); + /* Try to perform the command. */ CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, (_networking && !_generating_world) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : nullptr, text, false, estimate_only); diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 55dd5e39b..fba4b69f2 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -74,6 +74,16 @@ protected: static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = nullptr, Script_SuspendCallbackProc *callback = nullptr); /** + * Store the latest command executed by the script. + */ + static void SetLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd); + + /** + * Check if it's the latest command executed by the script. + */ + static bool CheckLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd); + + /** * Sets the DoCommand costs counter to a value. */ static void SetDoCommandCosts(Money value); |