From 9118ebe770fcb106679a1ae4acbf8c0f34062026 Mon Sep 17 00:00:00 2001 From: Oleksandr Nemesh Date: Thu, 18 Apr 2024 20:25:35 +0300 Subject: [PATCH] add "Auto Kill" --- resources/hacks/level.json | 4 + src/shared/hacks/auto-kill/auto-kill.cpp | 99 ++++++++++++++++++++++++ src/shared/hacks/auto-kill/auto-kill.hpp | 21 +++++ 3 files changed, 124 insertions(+) create mode 100644 src/shared/hacks/auto-kill/auto-kill.cpp create mode 100644 src/shared/hacks/auto-kill/auto-kill.hpp diff --git a/resources/hacks/level.json b/resources/hacks/level.json index 568ae81..605a66c 100644 --- a/resources/hacks/level.json +++ b/resources/hacks/level.json @@ -301,6 +301,10 @@ { "type": "embedded", "hack": "smart_startpos" + }, + { + "type": "embedded", + "hack": "auto_kill" } ] } diff --git a/src/shared/hacks/auto-kill/auto-kill.cpp b/src/shared/hacks/auto-kill/auto-kill.cpp new file mode 100644 index 0000000..2424c26 --- /dev/null +++ b/src/shared/hacks/auto-kill/auto-kill.cpp @@ -0,0 +1,99 @@ +#include "auto-kill.hpp" +#include "../../menu/menu.hpp" + +namespace openhack::hacks { + + inline float getCurrentPercent(gd::PlayLayer *playLayer) { + float percent; + auto *level = playLayer->m_level(); + auto timestamp = level->m_timestamp(); + if (timestamp > 0) { + percent = static_cast(playLayer->m_gameState().m_stepSpeed()) / timestamp * 100.f; + } else { + percent = playLayer->m_player1()->m_position().x / playLayer->m_levelLength() * 100.f; + } + + if (percent >= 100.f) return 100.f; + else if (percent <= 0.f) return 0.f; + else return percent; + } + + void AutoKill::onInit() { + // Set the default value + config::setIfEmpty("hack.auto_kill.enabled", false); + config::setIfEmpty("hack.auto_kill.use_percentage", true); + config::setIfEmpty("hack.auto_kill.percentage", 75.0f); + config::setIfEmpty("hack.auto_kill.use_time", false); + config::setIfEmpty("hack.auto_kill.time", 90.0f); + + // Initialize keybind + menu::keybinds::setKeybindCallback("auto_kill.enabled", []() { + bool enabled = !config::get("hack.auto_kill.enabled"); + config::set("hack.auto_kill.enabled", enabled); + }); + } + + void AutoKill::onDraw() { + gui::callback([]() { + gui::tooltip("Kills the player at a certain time/percentage."); + menu::keybinds::addMenuKeybind("auto_kill.enabled", "Auto Kill", []() { + bool enabled = !config::get("hack.auto_kill.enabled", false); + config::set("hack.auto_kill.enabled", enabled); + }); + }); + gui::toggleSetting("Auto Kill", "hack.auto_kill.enabled", []() { + gui::width(120); + + gui::checkbox("Use Percentage", "hack.auto_kill.use_percentage"); + gui::inputFloat("Percentage", "hack.auto_kill.percentage", 0.f, 100.f, "%.1f%%"); + gui::tooltip("The percentage at which the player will be killed"); + + gui::checkbox("Use time", "hack.auto_kill.use_time"); + gui::inputFloat("Time", "hack.auto_kill.time", 0.f, FLT_MAX, "%.2f sec"); + gui::tooltip("Time since the start of the level at which the player will be killed"); + + gui::width(); + }, ImVec2(0, 0), 150); + } + + void AutoKill::killPlayer() { + auto *playLayer = gd::PlayLayer::get(); + if (playLayer == nullptr) return; + + // Toggle noclip off to make sure the player dies + hacks::getHack("level.noclip")->applyPatch(false); + auto *player = playLayer->m_player1(); + if (player != nullptr && !player->m_isDead()) { + playLayer->destroyPlayer(player, player); + } + hacks::getHack("level.noclip")->applyPatch(); + } + + void AutoKill::update() { + if (!config::get("hack.auto_kill.enabled", false)) return; + + // Get PlayLayer + auto *playLayer = gd::PlayLayer::get(); + if (playLayer == nullptr) return; + + float percentage = getCurrentPercent(playLayer); + double time = playLayer->m_dTime(); + + bool checkPercentage = config::get("hack.auto_kill.use_percentage"); + bool checkTime = config::get("hack.auto_kill.use_time"); + bool kill = false; + + if (checkPercentage) { + auto deathPercentage = config::get("hack.auto_kill.percentage"); + kill |= percentage >= deathPercentage; + } + + if (checkTime) { + auto deathTime = config::get("hack.auto_kill.time"); + kill |= time >= deathTime; + } + + if (kill) killPlayer(); + } + +} \ No newline at end of file diff --git a/src/shared/hacks/auto-kill/auto-kill.hpp b/src/shared/hacks/auto-kill/auto-kill.hpp new file mode 100644 index 0000000..4b8757c --- /dev/null +++ b/src/shared/hacks/auto-kill/auto-kill.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "../hacks.hpp" + +namespace openhack::hacks { + + /// @brief Kills the player at a certain time/percentage. + class AutoKill : public EmbeddedHack { + public: + AutoKill() : EmbeddedHack("Auto Kill", "auto_kill") {} + + void onInit() override; + void onDraw() override; + void update() override; + bool isCheating() override { return false; } + + /// @brief Kills the player if exists + static void killPlayer(); + }; + +} \ No newline at end of file