Skip to content

Commit

Permalink
Added key handler system
Browse files Browse the repository at this point in the history
Added key handler system
  • Loading branch information
luadebug authored Aug 1, 2024
1 parent 1853624 commit 6e42372
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 287 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cargo-features = ["edition2024", "profile-rustflags"]

[package]
name = "rusted-assault-cube"
version = "2.2.0"
version = "2.3.0"
edition = "2024"
authors = ["luadebug Lin Evelynn [email protected]"]
publish = false
Expand Down
127 changes: 0 additions & 127 deletions src/aimbot.rs
Original file line number Diff line number Diff line change
@@ -1,127 +0,0 @@
use std::sync::atomic::Ordering::SeqCst;

use windows::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState;

use crate::angle::Angle;
use crate::entity::Entity;
use crate::getclosestentity::get_closest_entity;
use crate::hotkey_widget::to_win_key;
use crate::offsets::offsets::{LOCAL_PLAYER_OFFSET, PITCH_OFFSET, YAW_OFFSET};
use crate::settings::AppSettings;
use crate::utils::{read_memory, write_memory};
use crate::vars::game_vars::{LOCAL_PLAYER, SMOOTH};
use crate::vars::handles::AC_CLIENT_EXE_HMODULE;
use crate::vars::ui_vars::{IS_AIMBOT, IS_SMOOTH};
use crate::vec_structures::Vec3;

pub unsafe fn aimbot(app_settings: &AppSettings) {
unsafe {
if !IS_AIMBOT.load(SeqCst) {
return;
}

let local_player_addr =
match read_memory::<usize>(AC_CLIENT_EXE_HMODULE + LOCAL_PLAYER_OFFSET) {
Ok(addr) => addr,
Err(err) => {
println!("Error reading local player address: {}", err);
return;
}
};

LOCAL_PLAYER = Entity::from_addr(local_player_addr);

if GetAsyncKeyState(to_win_key(app_settings.aim_key.as_ref().unwrap().key).0 as i32) & 1
== 1
{
let enemy = get_closest_entity();
if LOCAL_PLAYER.entity_starts_at_addr == 0 || enemy.entity_starts_at_addr == 0 {
return; // Didn't find player or enemy
}

// Handle health and team checks
if enemy.health().unwrap_or(0) < 0 || enemy.team() == LOCAL_PLAYER.team() {
return; // Skipping dead or ally
}

// Safely read player and enemy positions
let player_head_pos = match LOCAL_PLAYER.head_position() {
Ok(pos) => pos,
Err(err) => {
println!("Error reading player head position: {}", err);
return;
}
};

let enemy_head_pos = match enemy.head_position() {
Ok(pos) => pos,
Err(err) => {
println!("Error reading enemy head position: {}", err);
return;
}
};

// Calculate angle
let angle = Angle::get_angle(
Vec3::new(player_head_pos.x, player_head_pos.y, player_head_pos.z),
Vec3::new(enemy_head_pos.x, enemy_head_pos.y, enemy_head_pos.z),
);

// Safely read and update yaw and pitch
let update_view_angle = |offset: usize, value: f32| {
let address = LOCAL_PLAYER.entity_starts_at_addr + offset;
match read_memory::<f32>(address) {
Ok(current_value) => {
// Only write if the value is different to avoid unnecessary writes
if current_value != value {
if let Err(err) = write_memory::<f32>(address, value) {
println!(
"Error writing to address {:x} storing value {}: {}",
address, current_value, err
);
}
}
}
Err(err) => {
println!("Error reading from address {:x}: {}", address, err);
}
}
};

// Read yaw and pitch with error handling
let (local_player_yaw, local_player_pitch) =
match (LOCAL_PLAYER.yaw(), LOCAL_PLAYER.pitch()) {
(Ok(y), Ok(p)) => (y as f32, p as f32),
(Err(err), _) => {
println!("Error reading yaw: {}", err);
return;
}
(_, Err(err)) => {
println!("Error reading pitch: {}", err);
return;
}
};

// Calculate the angle difference
let angle_diff_yaw = angle.yaw - local_player_yaw;
let angle_diff_pitch = angle.pitch - local_player_pitch;

let smooth = Angle::new(angle_diff_yaw, angle_diff_pitch);
let smooth_value = SMOOTH.load(SeqCst) as f32;

if IS_SMOOTH.load(SeqCst) {
if smooth_value > 0.0 {
let new_yaw = local_player_yaw + (smooth.yaw / smooth_value);
let new_pitch = local_player_pitch + (smooth.pitch / smooth_value);
update_view_angle(YAW_OFFSET, new_yaw);
update_view_angle(PITCH_OFFSET, new_pitch);
} else {
return;
}
} else {
update_view_angle(YAW_OFFSET, angle.yaw);
update_view_angle(PITCH_OFFSET, angle.pitch);
}
}
}
}
80 changes: 66 additions & 14 deletions src/hotkey_widget.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
use std::collections::HashSet;

use std::sync::atomic::Ordering::SeqCst;
use hudhook::imgui;
use hudhook::imgui::{Io, MouseButton, Window};
use imgui::Key;
use serde::{de::Visitor, Deserialize, Serialize};
use windows::Win32::UI::Input::KeyboardAndMouse::{
VK__none_, VIRTUAL_KEY, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9, VK_A,
VK_ADD, VK_APPS, VK_B, VK_BACK, VK_C, VK_CAPITAL, VK_CONTROL, VK_D, VK_DECIMAL, VK_DELETE,
VK_DIVIDE, VK_DOWN, VK_E, VK_END, VK_ESCAPE, VK_F, VK_F1, VK_F10, VK_F11, VK_F12, VK_F2, VK_F3,
VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_G, VK_H, VK_HOME, VK_I, VK_INSERT, VK_J, VK_K,
VK_L, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, VK_M, VK_MENU, VK_MULTIPLY, VK_N,
VK_NEXT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5,
VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_O, VK_OEM_1, VK_OEM_2, VK_OEM_3, VK_OEM_4,
VK_OEM_5, VK_OEM_6, VK_OEM_7, VK_OEM_COMMA, VK_OEM_MINUS, VK_OEM_PERIOD, VK_OEM_PLUS, VK_P,
VK_PAUSE, VK_PRIOR, VK_Q, VK_R, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN,
VK_S, VK_SCROLL, VK_SNAPSHOT, VK_SPACE, VK_SUBTRACT, VK_T, VK_TAB, VK_U, VK_UP, VK_V, VK_W,
VK_X, VK_Y, VK_Z,
};
use windows::Win32::UI::Input::KeyboardAndMouse::{VK__none_, VIRTUAL_KEY, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9, VK_A, VK_ADD, VK_APPS, VK_B, VK_BACK, VK_C, VK_CAPITAL, VK_CONTROL, VK_D, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_E, VK_END, VK_ESCAPE, VK_F, VK_F1, VK_F10, VK_F11, VK_F12, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_G, VK_H, VK_HOME, VK_I, VK_INSERT, VK_J, VK_K, VK_L, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, VK_M, VK_MENU, VK_MULTIPLY, VK_N, VK_NEXT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_O, VK_OEM_1, VK_OEM_2, VK_OEM_3, VK_OEM_4, VK_OEM_5, VK_OEM_6, VK_OEM_7, VK_OEM_COMMA, VK_OEM_MINUS, VK_OEM_PERIOD, VK_OEM_PLUS, VK_P, VK_PAUSE, VK_PRIOR, VK_Q, VK_R, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN, VK_S, VK_SCROLL, VK_SNAPSHOT, VK_SPACE, VK_SUBTRACT, VK_T, VK_TAB, VK_U, VK_UP, VK_V, VK_W, VK_X, VK_Y, VK_Z, GetAsyncKeyState};
use crate::vars::ui_vars::IS_SHOW_UI;

#[derive(Clone, Debug)]
pub struct HotKey {
Expand Down Expand Up @@ -277,6 +267,68 @@ pub fn render_button_key(
updated
}



/// Simple input system using the global mouse / keyboard state.
/// This does not require the need to process window messages or the imgui overlay to be active.
// Implement Default for [bool; VK_KEY_MAX]



#[derive(Debug)]
#[allow(unused)]
pub struct KeyboardInputSystem {
pub key_states: [bool; VK_KEY_MAX],
}

#[allow(unused)]
impl KeyboardInputSystem {
pub const fn new() -> Self {
Self {
key_states: [false; VK_KEY_MAX], // Initialize with all false
}
}

pub unsafe fn update(&mut self, io: &mut Io) {
for vkey in 0..VK_KEY_MAX {
let key_state = unsafe { GetAsyncKeyState(vkey as i32) as u16 };
let pressed = key_state & 1 == 1;
if self.key_states[vkey] == pressed {
continue;
}

self.key_states[vkey] = pressed;
let vkey = VIRTUAL_KEY(vkey as u16);

handle_key_modifier(io, vkey, pressed);

if !IS_SHOW_UI.load(SeqCst) //Do not handle mouse input in case menu is opened up
{
let mouse_button = match vkey {
VK_LBUTTON => Some(MouseButton::Left),
VK_RBUTTON => Some(MouseButton::Right),
VK_MBUTTON => Some(MouseButton::Middle),
VK_XBUTTON1 => Some(MouseButton::Extra1),
VK_XBUTTON2 => Some(MouseButton::Extra2),
_ => None,
};

if let Some(button) = mouse_button {
io.add_mouse_button_event(button, pressed);
}
}
if let Some(key) = to_imgui_key(vkey) {
println!("Key toogle {:?}: {}", key, pressed);
io.add_key_event(key, pressed);
} else {
println!("Missing ImGui key for {:?}", vkey);
}
}
}
}



const VK_KEY_MAX: usize = 256;

pub fn to_imgui_key(keycode: VIRTUAL_KEY) -> Option<Key> {
Expand Down
Loading

0 comments on commit 6e42372

Please sign in to comment.