Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
add "Auto Kill"
Browse files Browse the repository at this point in the history
  • Loading branch information
Prevter committed Apr 18, 2024
1 parent b55e70f commit 9118ebe
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/hacks/level.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@
{
"type": "embedded",
"hack": "smart_startpos"
},
{
"type": "embedded",
"hack": "auto_kill"
}
]
}
99 changes: 99 additions & 0 deletions src/shared/hacks/auto-kill/auto-kill.cpp
Original file line number Diff line number Diff line change
@@ -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<float>(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<bool>("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<bool>("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<bool>("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<bool>("hack.auto_kill.use_percentage");
bool checkTime = config::get<bool>("hack.auto_kill.use_time");
bool kill = false;

if (checkPercentage) {
auto deathPercentage = config::get<float>("hack.auto_kill.percentage");
kill |= percentage >= deathPercentage;
}

if (checkTime) {
auto deathTime = config::get<float>("hack.auto_kill.time");
kill |= time >= deathTime;
}

if (kill) killPlayer();
}

}
21 changes: 21 additions & 0 deletions src/shared/hacks/auto-kill/auto-kill.hpp
Original file line number Diff line number Diff line change
@@ -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();
};

}

0 comments on commit 9118ebe

Please sign in to comment.