summaryrefslogtreecommitdiff
path: root/src/input_common/drivers/tas_input.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/drivers/tas_input.h')
-rw-r--r--src/input_common/drivers/tas_input.h201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/input_common/drivers/tas_input.h b/src/input_common/drivers/tas_input.h
new file mode 100644
index 000000000..4b4e6c417
--- /dev/null
+++ b/src/input_common/drivers/tas_input.h
@@ -0,0 +1,201 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <string>
9#include <vector>
10
11#include "common/common_types.h"
12#include "input_common/input_engine.h"
13
14/*
15To play back TAS scripts on Yuzu, select the folder with scripts in the configuration menu below
16Tools -> Configure TAS. The file itself has normal text format and has to be called script0-1.txt
17for 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
46namespace InputCommon::TasInput {
47
48constexpr size_t PLAYER_NUMBER = 10;
49
50enum class TasButton : u64 {
51 BUTTON_A = 1U << 0,
52 BUTTON_B = 1U << 1,
53 BUTTON_X = 1U << 2,
54 BUTTON_Y = 1U << 3,
55 STICK_L = 1U << 4,
56 STICK_R = 1U << 5,
57 TRIGGER_L = 1U << 6,
58 TRIGGER_R = 1U << 7,
59 TRIGGER_ZL = 1U << 8,
60 TRIGGER_ZR = 1U << 9,
61 BUTTON_PLUS = 1U << 10,
62 BUTTON_MINUS = 1U << 11,
63 BUTTON_LEFT = 1U << 12,
64 BUTTON_UP = 1U << 13,
65 BUTTON_RIGHT = 1U << 14,
66 BUTTON_DOWN = 1U << 15,
67 BUTTON_SL = 1U << 16,
68 BUTTON_SR = 1U << 17,
69 BUTTON_HOME = 1U << 18,
70 BUTTON_CAPTURE = 1U << 19,
71};
72
73struct TasAnalog {
74 float x{};
75 float y{};
76};
77
78enum class TasState {
79 Running,
80 Recording,
81 Stopped,
82};
83
84class Tas final : public InputEngine {
85public:
86 explicit Tas(std::string input_engine_);
87 ~Tas() override;
88
89 /**
90 * Changes the input status that will be stored in each frame
91 * @param buttons Bitfield with the status of the buttons
92 * @param left_axis Value of the left axis
93 * @param right_axis Value of the right axis
94 */
95 void RecordInput(u64 buttons, TasAnalog left_axis, TasAnalog right_axis);
96
97 // Main loop that records or executes input
98 void UpdateThread();
99
100 // Sets the flag to start or stop the TAS command execution and swaps controllers profiles
101 void StartStop();
102
103 // Stop the TAS and reverts any controller profile
104 void Stop();
105
106 // Sets the flag to reload the file and start from the beginning in the next update
107 void Reset();
108
109 /**
110 * Sets the flag to enable or disable recording of inputs
111 * @returns true if the current recording status is enabled
112 */
113 bool Record();
114
115 /**
116 * Saves contents of record_commands on a file
117 * @param overwrite_file Indicates if player 1 should be overwritten
118 */
119 void SaveRecording(bool overwrite_file);
120
121 /**
122 * Returns the current status values of TAS playback/recording
123 * @returns A Tuple of
124 * TasState indicating the current state out of Running ;
125 * Current playback progress ;
126 * Total length of script file currently loaded or being recorded
127 */
128 std::tuple<TasState, size_t, size_t> GetStatus() const;
129
130private:
131 enum class TasAxis : u8;
132
133 struct TASCommand {
134 u64 buttons{};
135 TasAnalog l_axis{};
136 TasAnalog r_axis{};
137 };
138
139 /// Loads TAS files from all players
140 void LoadTasFiles();
141
142 /**
143 * Loads TAS file from the specified player
144 * @param player_index Player number to save the script
145 * @param file_index Script number of the file
146 */
147 void LoadTasFile(size_t player_index, size_t file_index);
148
149 /**
150 * Writes a TAS file from the recorded commands
151 * @param file_name Name of the file to be written
152 */
153 void WriteTasFile(std::u8string_view file_name);
154
155 /**
156 * Parses a string containing the axis values. X and Y have a range from -32767 to 32767
157 * @param line String containing axis values with the following format "x;y"
158 * @returns A TAS analog object with axis values with range from -1.0 to 1.0
159 */
160 TasAnalog ReadCommandAxis(const std::string& line) const;
161
162 /**
163 * Parses a string containing the button values. Each button is represented by it's text format
164 * specified in text_to_tas_button array
165 * @param line string containing button name with the following format "a;b;c;d..."
166 * @returns A u64 with each bit representing the status of a button
167 */
168 u64 ReadCommandButtons(const std::string& line) const;
169
170 /**
171 * Reset state of all players
172 */
173 void ClearInput();
174
175 /**
176 * Converts an u64 containing the button status into the text equivalent
177 * @param buttons Bitfield with the status of the buttons
178 * @returns A string with the name of the buttons to be written to the file
179 */
180 std::string WriteCommandButtons(u64 buttons) const;
181
182 /**
183 * Converts an TAS analog object containing the axis status into the text equivalent
184 * @param analog Value of the axis
185 * @returns A string with the value of the axis to be written to the file
186 */
187 std::string WriteCommandAxis(TasAnalog analog) const;
188
189 /// Sets an axis for a particular pad to the given value.
190 void SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value);
191
192 size_t script_length{0};
193 bool is_recording{false};
194 bool is_running{false};
195 bool needs_reset{false};
196 std::array<std::vector<TASCommand>, PLAYER_NUMBER> commands{};
197 std::vector<TASCommand> record_commands{};
198 size_t current_command{0};
199 TASCommand last_input{}; // only used for recording
200};
201} // namespace InputCommon::TasInput