summaryrefslogtreecommitdiff
path: root/src/input_common/tas
diff options
context:
space:
mode:
authorGravatar german772021-07-05 20:58:52 -0500
committerGravatar MonsterDruide12021-09-18 23:22:48 +0200
commit33a1d790e8a5f67c73d0eef4a141f936345f104f (patch)
tree16138c61b831ca3a6016fbb31fe183fbc557c7dd /src/input_common/tas
parentinput_common/tas: Add swap controller (diff)
downloadyuzu-33a1d790e8a5f67c73d0eef4a141f936345f104f.tar.gz
yuzu-33a1d790e8a5f67c73d0eef4a141f936345f104f.tar.xz
yuzu-33a1d790e8a5f67c73d0eef4a141f936345f104f.zip
input_common/tas: Document the main class
Diffstat (limited to 'src/input_common/tas')
-rw-r--r--src/input_common/tas/tas_input.cpp3
-rw-r--r--src/input_common/tas/tas_input.h108
-rw-r--r--src/input_common/tas/tas_poller.h4
3 files changed, 112 insertions, 3 deletions
diff --git a/src/input_common/tas/tas_input.cpp b/src/input_common/tas/tas_input.cpp
index aceb13adc..877d35088 100644
--- a/src/input_common/tas/tas_input.cpp
+++ b/src/input_common/tas/tas_input.cpp
@@ -14,6 +14,7 @@
14 14
15namespace TasInput { 15namespace TasInput {
16 16
17// Supported keywords and buttons from a TAS file
17constexpr std::array<std::pair<std::string_view, TasButton>, 20> text_to_tas_button = { 18constexpr std::array<std::pair<std::string_view, TasButton>, 20> text_to_tas_button = {
18 std::pair{"KEY_A", TasButton::BUTTON_A}, 19 std::pair{"KEY_A", TasButton::BUTTON_A},
19 {"KEY_B", TasButton::BUTTON_B}, 20 {"KEY_B", TasButton::BUTTON_B},
@@ -214,7 +215,7 @@ void Tas::UpdateThread() {
214 } 215 }
215 } 216 }
216 } else { 217 } else {
217 is_running = Settings::values.tas_loop; 218 is_running = Settings::values.tas_loop.GetValue();
218 current_command = 0; 219 current_command = 0;
219 tas_data.fill({}); 220 tas_data.fill({});
220 if (!is_running) { 221 if (!is_running) {
diff --git a/src/input_common/tas/tas_input.h b/src/input_common/tas/tas_input.h
index e1f351251..52d000db4 100644
--- a/src/input_common/tas/tas_input.h
+++ b/src/input_common/tas/tas_input.h
@@ -11,6 +11,38 @@
11#include "core/frontend/input.h" 11#include "core/frontend/input.h"
12#include "input_common/main.h" 12#include "input_common/main.h"
13 13
14/*
15To play back TAS scripts on Yuzu, select the folder with scripts in the configuration menu below
16Emulation -> Configure TAS. The file itself has normal text format and has to be called
17script0-1.txt for controller 1, script0-2.txt for controller 2 and so forth (with max. 8 players).
18
19A script file has the same format as TAS-nx uses, so final files will look like this:
20
211 KEY_B 0;0 0;0
226 KEY_ZL 0;0 0;0
2341 KEY_ZL;KEY_Y 0;0 0;0
2443 KEY_X;KEY_A 32767;0 0;0
2544 KEY_A 32767;0 0;0
2645 KEY_A 32767;0 0;0
2746 KEY_A 32767;0 0;0
2847 KEY_A 32767;0 0;0
29
30After placing the file at the correct location, it can be read into Yuzu with the (default) hotkey
31CTRL+F6 (refresh). In the bottom left corner, it will display the amount of frames the script file
32has. Playback can be started or stopped using CTRL+F5.
33
34However, for playback to actually work, the correct input device has to be selected: In the Controls
35menu, select TAS from the device list for the controller that the script should be played on.
36
37Recording a new script file is really simple: Just make sure that the proper device (not TAS) is
38connected on P1, and press CTRL+F7 to start recording. When done, just press the same keystroke
39again (CTRL+F7). The new script will be saved at the location previously selected, as the filename
40record.txt.
41
42For debugging purposes, the common controller debugger can be used (View -> Debugging -> Controller
43P1).
44*/
45
14namespace TasInput { 46namespace TasInput {
15 47
16constexpr size_t PLAYER_NUMBER = 8; 48constexpr size_t PLAYER_NUMBER = 8;
@@ -64,12 +96,26 @@ public:
64 Tas(); 96 Tas();
65 ~Tas(); 97 ~Tas();
66 98
99 // Changes the input status that will be stored in each frame
67 void RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes); 100 void RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes);
101
102 // Main loop that records or executes input
68 void UpdateThread(); 103 void UpdateThread();
69 104
105 // Sets the flag to start or stop the TAS command excecution and swaps controllers profiles
70 void StartStop(); 106 void StartStop();
107
108 // Sets the flag to reload the file and start from the begining in the next update
71 void Reset(); 109 void Reset();
110
111 /**
112 * Sets the flag to enable or disable recording of inputs
113 * @return Returns true if the current recording status is enabled
114 */
72 bool Record(); 115 bool Record();
116
117 // Saves contents of record_commands on a file if overwrite is enabled player 1 will be
118 // overwritten with the recorded commands
73 void SaveRecording(bool overwrite_file); 119 void SaveRecording(bool overwrite_file);
74 120
75 /** 121 /**
@@ -80,7 +126,11 @@ public:
80 * Total length of script file currently loaded or amount of frames (so far) for Recording 126 * Total length of script file currently loaded or amount of frames (so far) for Recording
81 */ 127 */
82 std::tuple<TasState, size_t, size_t> GetStatus() const; 128 std::tuple<TasState, size_t, size_t> GetStatus() const;
129
130 // Retuns an array of the default button mappings
83 InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const; 131 InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const;
132
133 // Retuns an array of the default analog mappings
84 InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const; 134 InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const;
85 [[nodiscard]] const TasData& GetTasState(std::size_t pad) const; 135 [[nodiscard]] const TasData& GetTasState(std::size_t pad) const;
86 136
@@ -90,23 +140,81 @@ private:
90 TasAnalog l_axis{}; 140 TasAnalog l_axis{};
91 TasAnalog r_axis{}; 141 TasAnalog r_axis{};
92 }; 142 };
143
144 // Loads TAS files from all players
93 void LoadTasFiles(); 145 void LoadTasFiles();
146
147 // Loads TAS file from the specified player
94 void LoadTasFile(size_t player_index); 148 void LoadTasFile(size_t player_index);
149
150 // Writes a TAS file from the recorded commands
95 void WriteTasFile(std::u8string file_name); 151 void WriteTasFile(std::u8string file_name);
152
153 /**
154 * Parses a string containing the axis values with the following format "x;y"
155 * X and Y have a range from -32767 to 32767
156 * @return Returns a TAS analog object with axis values with range from -1.0 to 1.0
157 */
96 TasAnalog ReadCommandAxis(const std::string& line) const; 158 TasAnalog ReadCommandAxis(const std::string& line) const;
159
160 /**
161 * Parses a string containing the button values with the following format "a;b;c;d..."
162 * Each button is represented by it's text format specified in text_to_tas_button array
163 * @return Returns a u32 with each bit representing the status of a button
164 */
97 u32 ReadCommandButtons(const std::string& line) const; 165 u32 ReadCommandButtons(const std::string& line) const;
166
167 /**
168 * Converts an u32 containing the button status into the text equivalent
169 * @return Returns a string with the name of the buttons to be written to the file
170 */
98 std::string WriteCommandButtons(u32 data) const; 171 std::string WriteCommandButtons(u32 data) const;
172
173 /**
174 * Converts an TAS analog object containing the axis status into the text equivalent
175 * @return Returns a string with the value of the axis to be written to the file
176 */
99 std::string WriteCommandAxis(TasAnalog data) const; 177 std::string WriteCommandAxis(TasAnalog data) const;
100 178
179 // Inverts the Y axis polarity
101 std::pair<float, float> FlipAxisY(std::pair<float, float> old); 180 std::pair<float, float> FlipAxisY(std::pair<float, float> old);
102 181
182 /**
183 * Converts an u32 containing the button status into the text equivalent
184 * @return Returns a string with the name of the buttons to be printed on console
185 */
103 std::string DebugButtons(u32 buttons) const; 186 std::string DebugButtons(u32 buttons) const;
187
188 /**
189 * Converts an TAS analog object containing the axis status into the text equivalent
190 * @return Returns a string with the value of the axis to be printed on console
191 */
104 std::string DebugJoystick(float x, float y) const; 192 std::string DebugJoystick(float x, float y) const;
193
194 /**
195 * Converts the given TAS status into the text equivalent
196 * @return Returns a string with the value of the TAS status to be printed on console
197 */
105 std::string DebugInput(const TasData& data) const; 198 std::string DebugInput(const TasData& data) const;
199
200 /**
201 * Converts the given TAS status of multiple players into the text equivalent
202 * @return Returns a string with the value of the status of all TAS players to be printed on
203 * console
204 */
106 std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const; 205 std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const;
206
207 /**
208 * Converts an u32 containing the button status into the text equivalent
209 * @return Returns a string with the name of the buttons
210 */
107 std::string ButtonsToString(u32 button) const; 211 std::string ButtonsToString(u32 button) const;
108 212
213 // Stores current controller configuration and sets a TAS controller for every active controller
214 // to the current config
109 void SwapToTasController(); 215 void SwapToTasController();
216
217 // Sets the stored controller configuration to the current config
110 void SwapToStoredController(); 218 void SwapToStoredController();
111 219
112 size_t script_length{0}; 220 size_t script_length{0};
diff --git a/src/input_common/tas/tas_poller.h b/src/input_common/tas/tas_poller.h
index 1bc0d173b..09e426cef 100644
--- a/src/input_common/tas/tas_poller.h
+++ b/src/input_common/tas/tas_poller.h
@@ -11,7 +11,7 @@
11namespace InputCommon { 11namespace InputCommon {
12 12
13/** 13/**
14 * A button device factory representing a mouse. It receives mouse events and forward them 14 * A button device factory representing a tas bot. It receives tas events and forward them
15 * to all button devices it created. 15 * to all button devices it created.
16 */ 16 */
17class TasButtonFactory final : public Input::Factory<Input::ButtonDevice> { 17class TasButtonFactory final : public Input::Factory<Input::ButtonDevice> {
@@ -29,7 +29,7 @@ private:
29 std::shared_ptr<TasInput::Tas> tas_input; 29 std::shared_ptr<TasInput::Tas> tas_input;
30}; 30};
31 31
32/// An analog device factory that creates analog devices from mouse 32/// An analog device factory that creates analog devices from tas
33class TasAnalogFactory final : public Input::Factory<Input::AnalogDevice> { 33class TasAnalogFactory final : public Input::Factory<Input::AnalogDevice> {
34public: 34public:
35 explicit TasAnalogFactory(std::shared_ptr<TasInput::Tas> tas_input_); 35 explicit TasAnalogFactory(std::shared_ptr<TasInput::Tas> tas_input_);