summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/tas/tas_input.cpp47
-rw-r--r--src/input_common/tas/tas_input.h59
2 files changed, 33 insertions, 73 deletions
diff --git a/src/input_common/tas/tas_input.cpp b/src/input_common/tas/tas_input.cpp
index 6efa1234a..baeb18c22 100644
--- a/src/input_common/tas/tas_input.cpp
+++ b/src/input_common/tas/tas_input.cpp
@@ -2,13 +2,8 @@
2// Licensed under GPLv2+ 2// Licensed under GPLv2+
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <chrono>
6#include <cstring> 5#include <cstring>
7#include <functional>
8#include <random>
9#include <regex> 6#include <regex>
10#include <thread>
11#include <boost/asio.hpp>
12 7
13#include "common/fs/file.h" 8#include "common/fs/file.h"
14#include "common/fs/fs_types.h" 9#include "common/fs/fs_types.h"
@@ -43,27 +38,25 @@ constexpr std::array<std::pair<std::string_view, TasButton>, 20> text_to_tas_but
43}; 38};
44 39
45Tas::Tas() { 40Tas::Tas() {
41 if (!Settings::values.tas_enable) {
42 return;
43 }
46 LoadTasFiles(); 44 LoadTasFiles();
47} 45}
48 46
49Tas::~Tas() { 47Tas::~Tas() = default;
50 update_thread_running = false;
51}
52 48
53void Tas::RefreshTasFile() {
54 refresh_tas_fle = true;
55}
56void Tas::LoadTasFiles() { 49void Tas::LoadTasFiles() {
57 script_length = 0; 50 script_length = 0;
58 for (size_t i = 0; i < PLAYER_NUMBER; i++) { 51 for (size_t i = 0; i < commands.size(); i++) {
59 LoadTasFile(i); 52 LoadTasFile(i);
60 if (commands[i].size() > script_length) { 53 if (commands[i].size() > script_length) {
61 script_length = commands[i].size(); 54 script_length = commands[i].size();
62 } 55 }
63 } 56 }
64} 57}
58
65void Tas::LoadTasFile(size_t player_index) { 59void Tas::LoadTasFile(size_t player_index) {
66 LOG_DEBUG(Input, "LoadTasFile()");
67 if (!commands[player_index].empty()) { 60 if (!commands[player_index].empty()) {
68 commands[player_index].clear(); 61 commands[player_index].clear();
69 } 62 }
@@ -110,7 +103,6 @@ void Tas::LoadTasFile(size_t player_index) {
110} 103}
111 104
112void Tas::WriteTasFile() { 105void Tas::WriteTasFile() {
113 LOG_DEBUG(Input, "WriteTasFile()");
114 std::string output_text; 106 std::string output_text;
115 for (size_t frame = 0; frame < record_commands.size(); frame++) { 107 for (size_t frame = 0; frame < record_commands.size(); frame++) {
116 if (!output_text.empty()) { 108 if (!output_text.empty()) {
@@ -131,13 +123,13 @@ void Tas::WriteTasFile() {
131 } 123 }
132} 124}
133 125
134static std::pair<float, float> FlipY(std::pair<float, float> old) { 126std::pair<float, float> Tas::FlipAxisY(std::pair<float, float> old) {
135 auto [x, y] = old; 127 auto [x, y] = old;
136 return {x, -y}; 128 return {x, -y};
137} 129}
138 130
139void Tas::RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes) { 131void Tas::RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes) {
140 last_input = {buttons, FlipY(axes[0]), FlipY(axes[1])}; 132 last_input = {buttons, FlipAxisY(axes[0]), FlipAxisY(axes[1])};
141} 133}
142 134
143std::tuple<TasState, size_t, size_t> Tas::GetStatus() const { 135std::tuple<TasState, size_t, size_t> Tas::GetStatus() const {
@@ -155,21 +147,21 @@ std::tuple<TasState, size_t, size_t> Tas::GetStatus() const {
155 return {state, current_command, script_length}; 147 return {state, current_command, script_length};
156} 148}
157 149
158static std::string DebugButtons(u32 buttons) { 150std::string Tas::DebugButtons(u32 buttons) const {
159 return fmt::format("{{ {} }}", TasInput::Tas::ButtonsToString(buttons)); 151 return fmt::format("{{ {} }}", TasInput::Tas::ButtonsToString(buttons));
160} 152}
161 153
162static std::string DebugJoystick(float x, float y) { 154std::string Tas::DebugJoystick(float x, float y) const {
163 return fmt::format("[ {} , {} ]", std::to_string(x), std::to_string(y)); 155 return fmt::format("[ {} , {} ]", std::to_string(x), std::to_string(y));
164} 156}
165 157
166static std::string DebugInput(const TasData& data) { 158std::string Tas::DebugInput(const TasData& data) const {
167 return fmt::format("{{ {} , {} , {} }}", DebugButtons(data.buttons), 159 return fmt::format("{{ {} , {} , {} }}", DebugButtons(data.buttons),
168 DebugJoystick(data.axis[0], data.axis[1]), 160 DebugJoystick(data.axis[0], data.axis[1]),
169 DebugJoystick(data.axis[2], data.axis[3])); 161 DebugJoystick(data.axis[2], data.axis[3]));
170} 162}
171 163
172static std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) { 164std::string Tas::DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const {
173 std::string returns = "[ "; 165 std::string returns = "[ ";
174 for (size_t i = 0; i < arr.size(); i++) { 166 for (size_t i = 0; i < arr.size(); i++) {
175 returns += DebugInput(arr[i]); 167 returns += DebugInput(arr[i]);
@@ -180,8 +172,17 @@ static std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) {
180 return returns + "]"; 172 return returns + "]";
181} 173}
182 174
175std::string Tas::ButtonsToString(u32 button) const {
176 std::string returns;
177 for (auto [text_button, tas_button] : text_to_tas_button) {
178 if ((button & static_cast<u32>(tas_button)) != 0)
179 returns += fmt::format(", {}", text_button.substr(4));
180 }
181 return returns.empty() ? "" : returns.substr(2);
182}
183
183void Tas::UpdateThread() { 184void Tas::UpdateThread() {
184 if (!update_thread_running) { 185 if (!Settings::values.tas_enable) {
185 return; 186 return;
186 } 187 }
187 188
@@ -206,9 +207,9 @@ void Tas::UpdateThread() {
206 } 207 }
207 if (is_running) { 208 if (is_running) {
208 if (current_command < script_length) { 209 if (current_command < script_length) {
209 LOG_INFO(Input, "Playing TAS {}/{}", current_command, script_length); 210 LOG_DEBUG(Input, "Playing TAS {}/{}", current_command, script_length);
210 size_t frame = current_command++; 211 size_t frame = current_command++;
211 for (size_t i = 0; i < PLAYER_NUMBER; i++) { 212 for (size_t i = 0; i < commands.size(); i++) {
212 if (frame < commands[i].size()) { 213 if (frame < commands[i].size()) {
213 TASCommand command = commands[i][frame]; 214 TASCommand command = commands[i][frame];
214 tas_data[i].buttons = command.buttons; 215 tas_data[i].buttons = command.buttons;
diff --git a/src/input_common/tas/tas_input.h b/src/input_common/tas/tas_input.h
index 49ef10ff9..e011e559e 100644
--- a/src/input_common/tas/tas_input.h
+++ b/src/input_common/tas/tas_input.h
@@ -5,8 +5,6 @@
5#pragma once 5#pragma once
6 6
7#include <array> 7#include <array>
8#include <mutex>
9#include <thread>
10 8
11#include "common/common_types.h" 9#include "common/common_types.h"
12#include "core/frontend/input.h" 10#include "core/frontend/input.h"
@@ -65,53 +63,6 @@ public:
65 Tas(); 63 Tas();
66 ~Tas(); 64 ~Tas();
67 65
68 static std::string ButtonsToString(u32 button) {
69 std::string returns;
70 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_A)) != 0)
71 returns += ", A";
72 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_B)) != 0)
73 returns += ", B";
74 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_X)) != 0)
75 returns += ", X";
76 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_Y)) != 0)
77 returns += ", Y";
78 if ((button & static_cast<u32>(TasInput::TasButton::STICK_L)) != 0)
79 returns += ", STICK_L";
80 if ((button & static_cast<u32>(TasInput::TasButton::STICK_R)) != 0)
81 returns += ", STICK_R";
82 if ((button & static_cast<u32>(TasInput::TasButton::TRIGGER_L)) != 0)
83 returns += ", TRIGGER_L";
84 if ((button & static_cast<u32>(TasInput::TasButton::TRIGGER_R)) != 0)
85 returns += ", TRIGGER_R";
86 if ((button & static_cast<u32>(TasInput::TasButton::TRIGGER_ZL)) != 0)
87 returns += ", TRIGGER_ZL";
88 if ((button & static_cast<u32>(TasInput::TasButton::TRIGGER_ZR)) != 0)
89 returns += ", TRIGGER_ZR";
90 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_PLUS)) != 0)
91 returns += ", PLUS";
92 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_MINUS)) != 0)
93 returns += ", MINUS";
94 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_LEFT)) != 0)
95 returns += ", LEFT";
96 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_UP)) != 0)
97 returns += ", UP";
98 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_RIGHT)) != 0)
99 returns += ", RIGHT";
100 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_DOWN)) != 0)
101 returns += ", DOWN";
102 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_SL)) != 0)
103 returns += ", SL";
104 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_SR)) != 0)
105 returns += ", SR";
106 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_HOME)) != 0)
107 returns += ", HOME";
108 if ((button & static_cast<u32>(TasInput::TasButton::BUTTON_CAPTURE)) != 0)
109 returns += ", CAPTURE";
110 return returns.empty() ? "" : returns.substr(2);
111 }
112
113 void RefreshTasFile();
114 void LoadTasFiles();
115 void RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes); 66 void RecordInput(u32 buttons, const std::array<std::pair<float, float>, 2>& axes);
116 void UpdateThread(); 67 void UpdateThread();
117 68
@@ -137,6 +88,7 @@ private:
137 TasAnalog l_axis{}; 88 TasAnalog l_axis{};
138 TasAnalog r_axis{}; 89 TasAnalog r_axis{};
139 }; 90 };
91 void LoadTasFiles();
140 void LoadTasFile(size_t player_index); 92 void LoadTasFile(size_t player_index);
141 void WriteTasFile(); 93 void WriteTasFile();
142 TasAnalog ReadCommandAxis(const std::string& line) const; 94 TasAnalog ReadCommandAxis(const std::string& line) const;
@@ -144,9 +96,16 @@ private:
144 std::string WriteCommandButtons(u32 data) const; 96 std::string WriteCommandButtons(u32 data) const;
145 std::string WriteCommandAxis(TasAnalog data) const; 97 std::string WriteCommandAxis(TasAnalog data) const;
146 98
99 std::pair<float, float> FlipAxisY(std::pair<float, float> old);
100
101 std::string DebugButtons(u32 buttons) const;
102 std::string DebugJoystick(float x, float y) const;
103 std::string DebugInput(const TasData& data) const;
104 std::string DebugInputs(const std::array<TasData, PLAYER_NUMBER>& arr) const;
105 std::string ButtonsToString(u32 button) const;
106
147 size_t script_length{0}; 107 size_t script_length{0};
148 std::array<TasData, PLAYER_NUMBER> tas_data; 108 std::array<TasData, PLAYER_NUMBER> tas_data;
149 bool update_thread_running{true};
150 bool refresh_tas_fle{false}; 109 bool refresh_tas_fle{false};
151 bool is_recording{false}; 110 bool is_recording{false};
152 bool is_running{false}; 111 bool is_running{false};