From e20312a672196fb55ca7638055143d178709ca4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 20 Dec 2023 19:11:33 +0100 Subject: [PATCH] Make marking events as "sim events" slightly harder to mess up A sim event being an event that takes place in the context of the simulation. Currently, this simply means that the RNG math.random uses is the sim's rather than the UI's. --- src/gui/game/GameControllerEvents.h | 14 ++++++++++++++ src/lua/LuaScriptInterface.cpp | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/gui/game/GameControllerEvents.h b/src/gui/game/GameControllerEvents.h index f93f24ca5..7c9905e54 100644 --- a/src/gui/game/GameControllerEvents.h +++ b/src/gui/game/GameControllerEvents.h @@ -4,16 +4,19 @@ struct TextInputEvent { + static constexpr bool simEvent = false; String text; }; struct TextEditingEvent { + static constexpr bool simEvent = false; String text; }; struct KeyEvent { + static constexpr bool simEvent = false; int key; int scan; bool repeat; @@ -24,14 +27,17 @@ struct KeyEvent struct KeyPressEvent : public KeyEvent { + static constexpr bool simEvent = false; }; struct KeyReleaseEvent : public KeyEvent { + static constexpr bool simEvent = false; }; struct MouseDownEvent { + static constexpr bool simEvent = false; int x; int y; unsigned int button; @@ -39,6 +45,7 @@ struct MouseDownEvent struct MouseUpEvent { + static constexpr bool simEvent = false; int x; int y; unsigned int button; @@ -47,6 +54,7 @@ struct MouseUpEvent struct MouseMoveEvent { + static constexpr bool simEvent = false; int x; int y; int dx; @@ -55,6 +63,7 @@ struct MouseMoveEvent struct MouseWheelEvent { + static constexpr bool simEvent = false; int x; int y; int d; @@ -62,22 +71,27 @@ struct MouseWheelEvent struct TickEvent { + static constexpr bool simEvent = false; }; struct BlurEvent { + static constexpr bool simEvent = false; }; struct CloseEvent { + static constexpr bool simEvent = false; }; struct BeforeSimEvent { + static constexpr bool simEvent = true; }; struct AfterSimEvent { + static constexpr bool simEvent = true; }; using GameControllerEvent = std::variant< diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index f3e12645b..1bc7b0730 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -4672,8 +4672,9 @@ bool LuaScriptInterface::HandleEvent(const GameControllerEvent &event) { lua_rawgeti(l, -1, i); int numArgs = PushGameControllerEvent(l, event); - auto simEvent = std::get_if(&event) || - std::get_if(&event); + auto simEvent = std::visit([](auto &event) { + return event.simEvent; + }, event); int callret = tpt_lua_pcall(l, numArgs, 1, 0, simEvent); if (callret) {