From 2fbca0240f6b5db38a016ec134d8ae1cc5837128 Mon Sep 17 00:00:00 2001 From: Krzyhau Date: Sat, 4 Nov 2023 22:18:20 +0100 Subject: [PATCH] feat: add zoom, duck, stop and cmd tas tools --- src/Features/Tas/TasTool.cpp | 4 ++ src/Features/Tas/TasTools/CommandTool.cpp | 31 ++++++++++++++ src/Features/Tas/TasTools/CommandTool.hpp | 26 ++++++++++++ src/Features/Tas/TasTools/DuckTool.cpp | 51 +++++++++++++++++++++++ src/Features/Tas/TasTools/DuckTool.hpp | 29 +++++++++++++ src/Features/Tas/TasTools/StopTool.cpp | 35 ++++++++++++++++ src/Features/Tas/TasTools/StopTool.hpp | 23 ++++++++++ src/Features/Tas/TasTools/ZoomTool.cpp | 49 ++++++++++++++++++++++ src/Features/Tas/TasTools/ZoomTool.hpp | 32 ++++++++++++++ src/SourceAutoRecord.vcxproj | 8 ++++ src/SourceAutoRecord.vcxproj.filters | 24 +++++++++++ 11 files changed, 312 insertions(+) create mode 100644 src/Features/Tas/TasTools/CommandTool.cpp create mode 100644 src/Features/Tas/TasTools/CommandTool.hpp create mode 100644 src/Features/Tas/TasTools/DuckTool.cpp create mode 100644 src/Features/Tas/TasTools/DuckTool.hpp create mode 100644 src/Features/Tas/TasTools/StopTool.cpp create mode 100644 src/Features/Tas/TasTools/StopTool.hpp create mode 100644 src/Features/Tas/TasTools/ZoomTool.cpp create mode 100644 src/Features/Tas/TasTools/ZoomTool.hpp diff --git a/src/Features/Tas/TasTool.cpp b/src/Features/Tas/TasTool.cpp index 72144a280..976d50e69 100644 --- a/src/Features/Tas/TasTool.cpp +++ b/src/Features/Tas/TasTool.cpp @@ -17,7 +17,11 @@ TasTool *TasTool::GetInstanceByName(int slot, std::string name) { std::vector TasTool::priorityList = { "check", + "cmd", + "stop", "use", + "duck", + "zoom", "shoot", "setang", "autoaim", diff --git a/src/Features/Tas/TasTools/CommandTool.cpp b/src/Features/Tas/TasTools/CommandTool.cpp new file mode 100644 index 000000000..66a4f8a9e --- /dev/null +++ b/src/Features/Tas/TasTools/CommandTool.cpp @@ -0,0 +1,31 @@ +#include "CommandTool.hpp" + +#include "Modules/Engine.hpp" +#include "Modules/Server.hpp" +#include "Features/Tas/TasParser.hpp" +#include "Features/Tas/TasPlayer.hpp" + +CommandTool commandTool[2] = {{0}, {1}}; + +void CommandTool::Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo) { + if (params.enabled) { + engine->ExecuteCommand(params.command.c_str(), true); + params.enabled = false; + } +} + +std::shared_ptr CommandTool::ParseParams(std::vector vp) { + if (vp.size() == 0) + throw TasParserException(Utils::ssprintf("Wrong argument count for tool %s: %d", this->GetName(), vp.size())); + + std::string command; + + for (const std::string &str : vp) { + if (!command.empty()) { + command += " "; + } + command += str; + } + + return std::make_shared(true, command); +} diff --git a/src/Features/Tas/TasTools/CommandTool.hpp b/src/Features/Tas/TasTools/CommandTool.hpp new file mode 100644 index 000000000..0438497fe --- /dev/null +++ b/src/Features/Tas/TasTools/CommandTool.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "../TasTool.hpp" + +struct CommandToolParams : public TasToolParams { + CommandToolParams() + : TasToolParams() { + } + CommandToolParams(bool enabled, std::string command) + : TasToolParams(enabled) + , command(command) { + } + + std::string command; +}; + +class CommandTool : public TasToolWithParams { +public: + CommandTool(int slot) + : TasToolWithParams("cmd", slot) { + } + + virtual std::shared_ptr ParseParams(std::vector); + virtual void Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo); +}; + +extern CommandTool commandTool[2]; diff --git a/src/Features/Tas/TasTools/DuckTool.cpp b/src/Features/Tas/TasTools/DuckTool.cpp new file mode 100644 index 000000000..f5f32181d --- /dev/null +++ b/src/Features/Tas/TasTools/DuckTool.cpp @@ -0,0 +1,51 @@ +#include "DuckTool.hpp" + +#include "Modules/Engine.hpp" +#include "Modules/Server.hpp" +#include "Features/Tas/TasParser.hpp" +#include "Features/Tas/TasPlayer.hpp" + +DuckTool duckTool[2] = {{0}, {1}}; + +void DuckTool::Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo) { + if (!params.enabled) { + return; + } + + if (this->updated) { + elapsedTicks = 0; + this->updated = false; + } + + if (elapsedTicks >= params.time) { + params.enabled = false; + return; + } + + bulk.buttonStates[TasControllerInput::Crouch] = true; + + elapsedTicks++; +} + +std::shared_ptr DuckTool::ParseParams(std::vector vp) { + if (vp.size() != 1) + throw TasParserException(Utils::ssprintf("Wrong argument count for tool %s: %d", this->GetName(), vp.size())); + + bool enabled = true; + int time = INT32_MAX; + + if (vp[0] == "on") { + enabled = true; + } else if (vp[0] == "off") { + enabled = false; + time = 0; + } else{ + try { + time = std::stoi(vp[0]); + } catch (...) { + throw TasParserException(Utils::ssprintf("Incorrect parameter for tool %s: %s", this->GetName(), vp[0].c_str())); + } + } + + return std::make_shared(enabled, time); +} diff --git a/src/Features/Tas/TasTools/DuckTool.hpp b/src/Features/Tas/TasTools/DuckTool.hpp new file mode 100644 index 000000000..fc456776a --- /dev/null +++ b/src/Features/Tas/TasTools/DuckTool.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "../TasTool.hpp" + +struct DuckToolParams : public TasToolParams { + DuckToolParams() + : TasToolParams() { + } + DuckToolParams(bool enabled, int time) + : TasToolParams(enabled) + , time(time) { + } + + int time = 0; +}; + +class DuckTool : public TasToolWithParams { +public: + DuckTool(int slot) + : TasToolWithParams("duck", slot) { + } + + virtual std::shared_ptr ParseParams(std::vector); + virtual void Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo); + +private: + int elapsedTicks; +}; + +extern DuckTool duckTool[2]; diff --git a/src/Features/Tas/TasTools/StopTool.cpp b/src/Features/Tas/TasTools/StopTool.cpp new file mode 100644 index 000000000..86891ccb3 --- /dev/null +++ b/src/Features/Tas/TasTools/StopTool.cpp @@ -0,0 +1,35 @@ +#include "StopTool.hpp" + +#include "Modules/Engine.hpp" +#include "Modules/Server.hpp" +#include "Features/Tas/TasParser.hpp" +#include "Features/Tas/TasPlayer.hpp" + +StopTool stopTool[2] = {{0}, {1}}; + +void StopTool::Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo) { + if (params.enabled) { + for (TasTool *tool : TasTool::GetList(pInfo.slot)) { + + bool toolJustRequested = false; + for (auto toolCmd : bulk.toolCmds) { + if (tool == toolCmd.tool) { + toolJustRequested = true; + break; + } + } + + if (!toolJustRequested) { + tool->Reset(); + } + } + params.enabled = false; + } +} + +std::shared_ptr StopTool::ParseParams(std::vector vp) { + if (vp.size() > 0) + throw TasParserException(Utils::ssprintf("Wrong argument count for tool %s: %d", this->GetName(), vp.size())); + + return std::make_shared(true); +} diff --git a/src/Features/Tas/TasTools/StopTool.hpp b/src/Features/Tas/TasTools/StopTool.hpp new file mode 100644 index 000000000..6929a4a56 --- /dev/null +++ b/src/Features/Tas/TasTools/StopTool.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "../TasTool.hpp" + +struct StopToolParams : public TasToolParams { + StopToolParams() + : TasToolParams() { + } + StopToolParams(bool enabled) + : TasToolParams(enabled) { + } +}; + +class StopTool : public TasToolWithParams { +public: + StopTool(int slot) + : TasToolWithParams("stop", slot) { + } + + virtual std::shared_ptr ParseParams(std::vector); + virtual void Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo); +}; + +extern StopTool stopTool[2]; diff --git a/src/Features/Tas/TasTools/ZoomTool.cpp b/src/Features/Tas/TasTools/ZoomTool.cpp new file mode 100644 index 000000000..03c5253d1 --- /dev/null +++ b/src/Features/Tas/TasTools/ZoomTool.cpp @@ -0,0 +1,49 @@ +#include "ZoomTool.hpp" + +#include "Modules/Engine.hpp" +#include "Modules/Server.hpp" +#include "Features/Tas/TasParser.hpp" +#include "Features/Tas/TasPlayer.hpp" + +ZoomTool zoomTool[2] = {{0}, {1}}; + +void ZoomTool::Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo) { + if (params.enabled) { + params.enabled = false; + + void *player = server->GetPlayer(pInfo.slot + 1); + + if (player == nullptr || (int)player == -1) return; + + bool isZoomedIn = SE(player)->field("m_hZoomOwner"); + + if ( + (params.zoomType == ZoomType::In && !isZoomedIn) || + (params.zoomType == ZoomType::Out && isZoomedIn) || + params.zoomType == ZoomType::Toggle + ) { + bulk.buttonStates[TasControllerInput::Zoom] = true; + } + } +} + +std::shared_ptr ZoomTool::ParseParams(std::vector vp) { + if (vp.size() != 1) + throw TasParserException(Utils::ssprintf("Wrong argument count for tool %s: %d", this->GetName(), vp.size())); + + ZoomType type; + + if (vp[0] == "in") { + type = ZoomType::In; + } + else if (vp[0] == "out") { + type = ZoomType::Out; + } + else if (vp[0] == "toggle") { + type = ZoomType::Toggle; + } else { + throw TasParserException(Utils::ssprintf("Bad parameter for tool %s: %s", this->GetName(), vp[0].c_str())); + } + + return std::make_shared(true, type); +} diff --git a/src/Features/Tas/TasTools/ZoomTool.hpp b/src/Features/Tas/TasTools/ZoomTool.hpp new file mode 100644 index 000000000..ded053f43 --- /dev/null +++ b/src/Features/Tas/TasTools/ZoomTool.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "../TasTool.hpp" + +enum ZoomType { + In, + Out, + Toggle +}; + +struct ZoomToolParams : public TasToolParams { + ZoomToolParams() + : TasToolParams() { + } + ZoomToolParams(bool enabled, ZoomType zoomType) + : TasToolParams(enabled) + , zoomType(zoomType) { + } + + ZoomType zoomType = In; +}; + +class ZoomTool : public TasToolWithParams { +public: + ZoomTool(int slot) + : TasToolWithParams("zoom", slot) { + } + + virtual std::shared_ptr ParseParams(std::vector); + virtual void Apply(TasFramebulk &bulk, const TasPlayerInfo &pInfo); +}; + +extern ZoomTool zoomTool[2]; diff --git a/src/SourceAutoRecord.vcxproj b/src/SourceAutoRecord.vcxproj index 0b74d64ba..c2aad1bf1 100644 --- a/src/SourceAutoRecord.vcxproj +++ b/src/SourceAutoRecord.vcxproj @@ -145,10 +145,14 @@ + + + + @@ -338,10 +342,14 @@ + + + + diff --git a/src/SourceAutoRecord.vcxproj.filters b/src/SourceAutoRecord.vcxproj.filters index 2b2b42a13..503cf729b 100644 --- a/src/SourceAutoRecord.vcxproj.filters +++ b/src/SourceAutoRecord.vcxproj.filters @@ -508,6 +508,18 @@ SourceAutoRecord\Features\Routing + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools + @@ -1302,6 +1314,18 @@ SourceAutoRecord\Features\Routing + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools + + + SourceAutoRecord\Features\Tas\TasTools +