tas_input.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include "common/common_types.h"
  7. #include "common/settings_input.h"
  8. #include "input_common/input_engine.h"
  9. #include "input_common/main.h"
  10. /*
  11. To play back TAS scripts on Yuzu, select the folder with scripts in the configuration menu below
  12. Tools -> Configure TAS. The file itself has normal text format and has to be called script0-1.txt
  13. for controller 1, script0-2.txt for controller 2 and so forth (with max. 8 players).
  14. A script file has the same format as TAS-nx uses, so final files will look like this:
  15. 1 KEY_B 0;0 0;0
  16. 6 KEY_ZL 0;0 0;0
  17. 41 KEY_ZL;KEY_Y 0;0 0;0
  18. 43 KEY_X;KEY_A 32767;0 0;0
  19. 44 KEY_A 32767;0 0;0
  20. 45 KEY_A 32767;0 0;0
  21. 46 KEY_A 32767;0 0;0
  22. 47 KEY_A 32767;0 0;0
  23. After placing the file at the correct location, it can be read into Yuzu with the (default) hotkey
  24. CTRL+F6 (refresh). In the bottom left corner, it will display the amount of frames the script file
  25. has. Playback can be started or stopped using CTRL+F5.
  26. However, for playback to actually work, the correct input device has to be selected: In the Controls
  27. menu, select TAS from the device list for the controller that the script should be played on.
  28. Recording a new script file is really simple: Just make sure that the proper device (not TAS) is
  29. connected on P1, and press CTRL+F7 to start recording. When done, just press the same keystroke
  30. again (CTRL+F7). The new script will be saved at the location previously selected, as the filename
  31. record.txt.
  32. For debugging purposes, the common controller debugger can be used (View -> Debugging -> Controller
  33. P1).
  34. */
  35. namespace InputCommon::TasInput {
  36. constexpr size_t PLAYER_NUMBER = 10;
  37. enum class TasButton : u64 {
  38. BUTTON_A = 1U << 0,
  39. BUTTON_B = 1U << 1,
  40. BUTTON_X = 1U << 2,
  41. BUTTON_Y = 1U << 3,
  42. STICK_L = 1U << 4,
  43. STICK_R = 1U << 5,
  44. TRIGGER_L = 1U << 6,
  45. TRIGGER_R = 1U << 7,
  46. TRIGGER_ZL = 1U << 8,
  47. TRIGGER_ZR = 1U << 9,
  48. BUTTON_PLUS = 1U << 10,
  49. BUTTON_MINUS = 1U << 11,
  50. BUTTON_LEFT = 1U << 12,
  51. BUTTON_UP = 1U << 13,
  52. BUTTON_RIGHT = 1U << 14,
  53. BUTTON_DOWN = 1U << 15,
  54. BUTTON_SL = 1U << 16,
  55. BUTTON_SR = 1U << 17,
  56. BUTTON_HOME = 1U << 18,
  57. BUTTON_CAPTURE = 1U << 19,
  58. };
  59. struct TasAnalog {
  60. float x{};
  61. float y{};
  62. };
  63. enum class TasState {
  64. Running,
  65. Recording,
  66. Stopped,
  67. };
  68. class Tas final : public InputCommon::InputEngine {
  69. public:
  70. explicit Tas(const std::string& input_engine_);
  71. ~Tas();
  72. /**
  73. * Changes the input status that will be stored in each frame
  74. * @param buttons Bitfield with the status of the buttons
  75. * @param left_axis Value of the left axis
  76. * @param right_axis Value of the right axis
  77. */
  78. void RecordInput(u64 buttons, TasAnalog left_axis, TasAnalog right_axis);
  79. // Main loop that records or executes input
  80. void UpdateThread();
  81. // Sets the flag to start or stop the TAS command execution and swaps controllers profiles
  82. void StartStop();
  83. // Stop the TAS and reverts any controller profile
  84. void Stop();
  85. // Sets the flag to reload the file and start from the beginning in the next update
  86. void Reset();
  87. /**
  88. * Sets the flag to enable or disable recording of inputs
  89. * @returns true if the current recording status is enabled
  90. */
  91. bool Record();
  92. /**
  93. * Saves contents of record_commands on a file
  94. * @param overwrite_file Indicates if player 1 should be overwritten
  95. */
  96. void SaveRecording(bool overwrite_file);
  97. /**
  98. * Returns the current status values of TAS playback/recording
  99. * @returns A Tuple of
  100. * TasState indicating the current state out of Running ;
  101. * Current playback progress ;
  102. * Total length of script file currently loaded or being recorded
  103. */
  104. std::tuple<TasState, size_t, size_t> GetStatus() const;
  105. private:
  106. enum class TasAxis : u8;
  107. struct TASCommand {
  108. u64 buttons{};
  109. TasAnalog l_axis{};
  110. TasAnalog r_axis{};
  111. };
  112. /// Loads TAS files from all players
  113. void LoadTasFiles();
  114. /**
  115. * Loads TAS file from the specified player
  116. * @param player_index Player number to save the script
  117. * @param file_index Script number of the file
  118. */
  119. void LoadTasFile(size_t player_index, size_t file_index);
  120. /**
  121. * Writes a TAS file from the recorded commands
  122. * @param file_name Name of the file to be written
  123. */
  124. void WriteTasFile(std::u8string file_name);
  125. /**
  126. * Parses a string containing the axis values. X and Y have a range from -32767 to 32767
  127. * @param line String containing axis values with the following format "x;y"
  128. * @returns A TAS analog object with axis values with range from -1.0 to 1.0
  129. */
  130. TasAnalog ReadCommandAxis(const std::string& line) const;
  131. /**
  132. * Parses a string containing the button values. Each button is represented by it's text format
  133. * specified in text_to_tas_button array
  134. * @param line string containing button name with the following format "a;b;c;d..."
  135. * @returns A u64 with each bit representing the status of a button
  136. */
  137. u64 ReadCommandButtons(const std::string& line) const;
  138. /**
  139. * Reset state of all players
  140. */
  141. void ClearInput();
  142. /**
  143. * Converts an u64 containing the button status into the text equivalent
  144. * @param buttons Bitfield with the status of the buttons
  145. * @returns A string with the name of the buttons to be written to the file
  146. */
  147. std::string WriteCommandButtons(u64 buttons) const;
  148. /**
  149. * Converts an TAS analog object containing the axis status into the text equivalent
  150. * @param analog Value of the axis
  151. * @returns A string with the value of the axis to be written to the file
  152. */
  153. std::string WriteCommandAxis(TasAnalog analog) const;
  154. /// Sets an axis for a particular pad to the given value.
  155. void SetTasAxis(const PadIdentifier& identifier, TasAxis axis, f32 value);
  156. size_t script_length{0};
  157. bool is_recording{false};
  158. bool is_running{false};
  159. bool needs_reset{false};
  160. std::array<std::vector<TASCommand>, PLAYER_NUMBER> commands{};
  161. std::vector<TASCommand> record_commands{};
  162. size_t current_command{0};
  163. TASCommand last_input{}; // only used for recording
  164. };
  165. } // namespace InputCommon::TasInput