Update thirdperson.c, hooks.c, and 3 more files...

This commit is contained in:
2025-04-04 17:07:54 -04:00
parent 0c63d7c969
commit 07b05f02c4
5 changed files with 77 additions and 224 deletions

View File

@@ -13,24 +13,20 @@
#include "include/game_detection.h"
#include "features/thirdperson.h"
// For ImGui context access
#define IMGUI_IMPLEMENTATION
#include "include/imgui/imgui.h"
#include "include/imgui/backends/imgui_impl_opengl2.h"
// Include for mouse button constants
#include "include/sdk/public/keydefs.h"
#include <dlfcn.h>
#include <GL/gl.h>
#include <string.h>
#include <unistd.h> // usleep()
#include <unistd.h>
// For DetourFunction and DetourRemove
extern void* DetourFunction(void* orig, void* hook);
extern bool DetourRemove(void* orig, void* hook);
// Define BYTE as unsigned char for GL hooks
typedef unsigned char BYTE;
/* Forward declarations of hook functions */
@@ -91,23 +87,19 @@ bool hooks_init(void) {
HOOK(i_client, CalcRefdef);
HOOK(i_client, HUD_PostRunCmd);
/* Third-person hooks - use direct replacement approach */
ho_CL_IsThirdPerson = i_client->CL_IsThirdPerson;
i_client->CL_IsThirdPerson = h_CL_IsThirdPerson;
i_engine->Con_Printf("CL_IsThirdPerson hook installed at %p -> %p\n",
ho_CL_IsThirdPerson, h_CL_IsThirdPerson);
/* Camera offset hook - critical for third-person view */
ho_CL_CameraOffset = i_client->CL_CameraOffset;
i_client->CL_CameraOffset = h_CL_CameraOffset;
i_engine->Con_Printf("CL_CameraOffset hook installed at %p -> %p\n",
ho_CL_CameraOffset, h_CL_CameraOffset);
/* Manual hook for HUD_Key_Event - avoiding type cast issues */
ho_HUD_Key_Event = (key_event_func_t)i_client->HUD_Key_Event;
i_client->HUD_Key_Event = (key_event_func_t)h_HUD_Key_Event;
/* Manual OpenGL hooks - get functions from the game engine's GL context */
real_glColor4f = (void (*)(GLfloat, GLfloat, GLfloat, GLfloat))glColor4f;
if (real_glColor4f) {
void* result = DetourFunction((void*)real_glColor4f, (void*)h_glColor4f);
@@ -116,10 +108,6 @@ bool hooks_init(void) {
}
}
/* We don't hook swap buffers directly anymore - we use HUD_Redraw instead
which is more reliable in GoldSrc engine */
/* Detour hooks */
void* clmove_ptr = dlsym(hw, "CL_Move");
if (!clmove_ptr) {
i_engine->Con_Printf("Failed to find CL_Move\n");
@@ -194,36 +182,29 @@ void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) {
return;
}
/* Declared in globals.c */
localplayer = i_engine->GetLocalPlayer();
// First call original function to let the game set up movement commands
ORIGINAL(CL_CreateMove, frametime, cmd, active);
vec3_t old_angles = cmd->viewangles;
// Store original movement commands
float origForward = cmd->forwardmove;
float origSide = cmd->sidemove;
float origUp = cmd->upmove;
int origButtons = cmd->buttons;
// If menu is open and movement is not allowed, zero out movement commands
if (g_menu_open && !g_settings.menu_allow_movement) {
cmd->forwardmove = 0.0f;
cmd->sidemove = 0.0f;
cmd->upmove = 0.0f;
cmd->buttons = 0;
// If menu is open and movement is not allowed, only force angles
if (s_lock_initialized) {
vec_copy(cmd->viewangles, s_locked_view_angles);
}
}
else {
// Menu is closed OR movement is allowed, process all features
// Restore original movement values if menu is open with movement allowed
if (g_menu_open && g_settings.menu_allow_movement) {
cmd->forwardmove = origForward;
cmd->sidemove = origSide;
@@ -231,7 +212,6 @@ void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) {
cmd->buttons = origButtons;
}
// Always process features if movement is allowed or menu is closed
bhop(cmd);
aimbot(cmd);
bullet_tracers(cmd);
@@ -239,7 +219,6 @@ void h_CL_CreateMove(float frametime, usercmd_t* cmd, int active) {
check_namechanger_mode_and_execute(cmd);
fov_adjust(cmd);
// If menu is open with movement allowed, still need to lock view angles
if (g_menu_open && s_lock_initialized) {
vec_copy(cmd->viewangles, s_locked_view_angles);
}
@@ -263,44 +242,33 @@ rgb_t rainbow_color(float time) {
}
int h_HUD_Redraw(float time, int intermission) {
// Force set view angles every frame when menu is open
force_view_angles();
// Call original function to let the game draw
int ret = ORIGINAL(HUD_Redraw, time, intermission);
// Draw watermark if enabled
if (g_settings.watermark) {
rgb_t color = g_settings.watermark_rainbow ? rainbow_color(time) : (rgb_t){ 0, 255, 255 };
engine_draw_text(5, 5, "https://git.deadzone.lol/Wizzard/goldsrc-cheat", color);
}
// Draw ESP
esp();
// Draw custom crosshair
custom_crosshair();
// Handle cursor for ImGui when menu is open
if (g_menu_open && g_imgui_context) {
// Get screen dimensions
SCREENINFO scr_inf;
scr_inf.iSize = sizeof(SCREENINFO);
i_engine->pfnGetScreenInfo(&scr_inf);
// Get mouse position from engine
int mouse_x, mouse_y;
i_engine->GetMousePosition(&mouse_x, &mouse_y);
// Update ImGui mouse position
ImGui::SetCurrentContext(g_imgui_context);
ImGuiIO& io = ImGui::GetIO();
// Update mouse buttons - using our tracked state from key events
io.MouseDown[0] = g_mouse_down[0];
io.MouseDown[1] = g_mouse_down[1];
// Update mouse position (clamped to screen)
if (mouse_x >= 0 && mouse_x < scr_inf.iWidth &&
mouse_y >= 0 && mouse_y < scr_inf.iHeight) {
io.MousePos.x = (float)mouse_x;
@@ -308,7 +276,6 @@ int h_HUD_Redraw(float time, int intermission) {
}
}
// Render ImGui menu (if open)
menu_render();
return ret;
@@ -446,45 +413,48 @@ void h_CL_Move(void)
/*----------------------------------------------------------------------------*/
int h_HUD_Key_Event(int down, int keynum, const char* pszCurrentBinding) {
// Debug output but only for specific keys to avoid spam
if (keynum == 'C' || keynum == g_settings.thirdperson_key || keynum == K_INS) {
// Debug output for important keys
if (keynum == 'C' || keynum == 'c' || keynum == 67 || keynum == 99 ||
keynum == g_settings.thirdperson_key || keynum == K_INS) {
i_engine->Con_Printf("Key event: down=%d, keynum=%d, binding=%s\n",
down, keynum, pszCurrentBinding ? pszCurrentBinding : "none");
}
// Check if thirdperson feature wants to handle this key
if (thirdperson_key_event(down, keynum)) {
// Check if menu is in key binding mode - this must come first
extern bool g_waiting_for_key_bind;
if (g_waiting_for_key_bind && down) {
menu_key_event(keynum, down);
return 0;
}
// Then try thirdperson key handling
if (thirdperson_key_event(keynum, down)) {
i_engine->Con_Printf("Thirdperson key event handled successfully\n");
return 0; // The key was handled by thirdperson
}
// Toggle menu with Insert key or specific binding
if (down && (keynum == K_INS || (pszCurrentBinding && strcmp(pszCurrentBinding, "dz_menu") == 0))) {
i_engine->Con_Printf("Menu toggle key detected!\n");
menu_key_event(keynum, down);
return 0; // Block this key from reaching the game
return 0;
}
// ImGui gets priority on all input when menu is open
if (g_menu_open) {
// For mouse buttons, update our own tracking to help ImGui
if (keynum == K_MOUSE1 || keynum == K_MOUSE2) {
if (keynum == K_MOUSE1)
g_mouse_down[0] = down ? true : false;
else if (keynum == K_MOUSE2)
g_mouse_down[1] = down ? true : false;
return 0; // Block mouse buttons from game
return 0;
}
// Let ESC pass through to the game to close console/etc.
if (keynum == K_ESCAPE && down) {
if (ho_HUD_Key_Event)
return ho_HUD_Key_Event(down, keynum, pszCurrentBinding);
}
// Allow WASD movement keys if the setting is enabled
if (g_settings.menu_allow_movement) {
// Check for movement key bindings instead of specific keys
if (pszCurrentBinding && (
strstr(pszCurrentBinding, "+forward") ||
strstr(pszCurrentBinding, "+back") ||
@@ -499,11 +469,10 @@ int h_HUD_Key_Event(int down, int keynum, const char* pszCurrentBinding) {
return ho_HUD_Key_Event(down, keynum, pszCurrentBinding);
}
// Also allow WASD keys directly
if (keynum == 'W' || keynum == 'A' || keynum == 'S' || keynum == 'D' ||
keynum == ' ' || // Space for jump
keynum == K_CTRL || // Crouch
keynum == K_SHIFT) { // Walk
keynum == ' ' ||
keynum == K_CTRL ||
keynum == K_SHIFT) {
i_engine->Con_Printf("Passing direct movement key to game: %d\n", keynum);
if (ho_HUD_Key_Event)
@@ -511,11 +480,9 @@ int h_HUD_Key_Event(int down, int keynum, const char* pszCurrentBinding) {
}
}
// Block all other keys from reaching the game
return 0;
}
// When menu is closed, let all keys pass to the game
if (ho_HUD_Key_Event) {
return ho_HUD_Key_Event(down, keynum, pszCurrentBinding);
}
@@ -525,28 +492,21 @@ int h_HUD_Key_Event(int down, int keynum, const char* pszCurrentBinding) {
/*----------------------------------------------------------------------------*/
// Force set view angles through the engine when menu is open
static void force_view_angles(void) {
// Only do anything when menu is open/closed state changes
static bool s_was_menu_open = false;
// Get the current time for smoother transitions
float current_time = i_engine->GetClientTime();
static float s_menu_close_time = 0.0f;
static bool s_is_restoring = false;
// Handle menu state changes
if (g_menu_open != s_was_menu_open) {
// Menu state changed
if (g_menu_open) {
// Menu just opened - store current view angles
i_engine->GetViewAngles(s_locked_view_angles);
s_lock_initialized = true;
i_engine->Con_Printf("Stored view angles: %.1f %.1f %.1f\n",
s_locked_view_angles[0], s_locked_view_angles[1], s_locked_view_angles[2]);
s_is_restoring = false;
} else {
// Menu just closed - set up restoration timing
s_menu_close_time = current_time;
s_is_restoring = true;
@@ -561,19 +521,14 @@ static void force_view_angles(void) {
s_was_menu_open = g_menu_open;
}
// When menu is open, continuously force the locked view angles
if (g_menu_open && s_lock_initialized) {
// Force the engine angles to match our locked angles
i_engine->SetViewAngles(s_locked_view_angles);
}
// Continue restoring angles for a short time after menu close to prevent flicker
if (!g_menu_open && s_is_restoring) {
if (current_time - s_menu_close_time < 0.2f) {
// Still in restoration period, keep forcing angles
i_engine->SetViewAngles(s_locked_view_angles);
} else {
// Done restoring
s_is_restoring = false;
}
}