From e30b028ec9648df0f5dbf8bcb11f4676a42e4286 Mon Sep 17 00:00:00 2001 From: Michael Gerhold Date: Sat, 31 Aug 2024 18:22:33 +0200 Subject: [PATCH 1/2] fix: misbehavior when rotate and drop is pressed in the same frame --- src/simulator/tetrion.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/simulator/tetrion.cpp b/src/simulator/tetrion.cpp index 7b57a4c..465c0b1 100644 --- a/src/simulator/tetrion.cpp +++ b/src/simulator/tetrion.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -229,10 +228,36 @@ void ObpfTetrion::process_keys(KeyState const key_state) { auto const released_keys = determine_released_keys(m_last_key_state, key_state); m_last_key_state = key_state; - for (auto const [i, is_pressed] : filter(enumerate(pressed_keys), [](auto const tuple) { return get<1>(tuple); })) { - auto const key = magic_enum::enum_cast(gsl::narrow>(i)); - assert(key.has_value()); - handle_key_press(key.value()); + /* To avoid certain kinds of errors, we have to process the different keys in a certain order. That is: + * 1. hold + * 2. sideways movement (left, right) + * - if hold was pressed, ignore the following keys: + * 3. rotation + * 4. soft drop + * 5. hard drop */ + auto const is_key_pressed = [&pressed_keys](Key const key) { + return pressed_keys.at(std::to_underlying(key)); + }; + auto const hold_pressed = is_key_pressed(Key::Hold); + if (hold_pressed) { + handle_key_press(Key::Hold); + } + for (auto const key : std::array{ Key::Left, Key::Right }) { + if (is_key_pressed(key)) { + handle_key_press(key); + } + } + if (not hold_pressed) { + for (auto const key : std::array{ + Key::RotateClockwise, + Key::RotateCounterClockwise, + Key::Down, + Key::Drop, + }) { + if (is_key_pressed(key)) { + handle_key_press(key); + } + } } for (auto const [i, is_released] : filter(enumerate(released_keys), [](auto const tuple) { return get<1>(tuple); })) { From 40610d9c1269bfc5643ed34744f8d659467e5c1a Mon Sep 17 00:00:00 2001 From: Michael Gerhold Date: Sat, 31 Aug 2024 18:27:28 +0200 Subject: [PATCH 2/2] fix: narrowing conversion warning --- src/simulator/tetrion.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simulator/tetrion.cpp b/src/simulator/tetrion.cpp index 465c0b1..ba7986c 100644 --- a/src/simulator/tetrion.cpp +++ b/src/simulator/tetrion.cpp @@ -236,7 +236,7 @@ void ObpfTetrion::process_keys(KeyState const key_state) { * 4. soft drop * 5. hard drop */ auto const is_key_pressed = [&pressed_keys](Key const key) { - return pressed_keys.at(std::to_underlying(key)); + return pressed_keys.at(gsl::narrow(std::to_underlying(key))); }; auto const hold_pressed = is_key_pressed(Key::Hold); if (hold_pressed) {