defensive double tap" from the Nemesis cheat:
cpp:
#include <cstdint>
#include <cmath>
#include <algorithm>
namespace dt {
struct vec3 {
float x;
float y;
float z;
};
struct ang3 {
float pitch;
float yaw;
float roll;
};
struct CUserCmd {
int command_number;
int tick_count;
ang3 viewangles;
vec3 aimdirection;
float forwardmove;
float sidemove;
float upmove;
int buttons;
unsigned char impulse;
int weaponselect;
int weaponsubtype;
int random_seed;
short mousedx;
short mousedy;
bool hasbeenpredicted;
};
enum : int {
IN_ATTACK = 1 << 0
};
struct bindings {
void* (*local_player)() = nullptr;
bool (*alive)(void*) = nullptr;
int (*tickbase)(void*) = nullptr;
void (*set_tickbase)(void*, int) = nullptr;
void* (*active_weapon)(void*) = nullptr;
float (*cycle_time)(void*) = nullptr;
bool (*weapon_can_fire)(void*, float) = nullptr;
bool (*is_gun)(void*) = nullptr;
float (*curtime)() = nullptr;
float (*interval_per_tick)() = nullptr;
int (*server_tick)() = nullptr;
int (*choked_commands)() = nullptr;
bool (*on_ground)(void*) = nullptr;
float (*speed_2d)(void*) = nullptr;
bool (*aa_defensive)() = nullptr;
bool (*key_down)(int) = nullptr;
int (*max_process_ticks)() = nullptr;
};
struct config {
bool enabled = false;
bool defensive_only = true;
bool double_shot = true;
bool freeze_desync = true;
int max_choked_ticks = 14;
int key = 0;
float min_speed = 5.0f;
};
struct status {
int pending = 0;
int charge = 0;
int headroom = 16;
int last_server_tick = -1;
int decided_tick = -1;
bool shifted = false;
bool defensive = false;
bool second_shot = false;
bool applied = false;
int last_shift_tick = -1000;
};
static bindings g_bind;
static config g_cfg;
static status g_state;
static int clamp_int(int value, int low, int high) {
return value < low ? low : (value > high ? high : value);
}
static int process_limit() {
const int value = g_bind.max_process_ticks ? g_bind.max_process_ticks() : 16;
return clamp_int(value, 1, 16);
}
static bool ready() {
return g_bind.local_player && g_bind.tickbase && g_bind.set_tickbase
&& g_bind.curtime && g_bind.interval_per_tick && g_bind.server_tick
&& g_bind.choked_commands && g_bind.cycle_time && g_bind.active_weapon;
}
void attach(const bindings& value) {
g_bind = value;
g_state.headroom = process_limit();
}
bindings& attached() {
return g_bind;
}
config& cfg() {
return g_cfg;
}
status& state() {
return g_state;
}
void reset() {
g_state.pending = 0;
g_state.charge = 0;
g_state.headroom = process_limit();
g_state.last_server_tick = -1;
g_state.decided_tick = -1;
g_state.shifted = false;
g_state.defensive = false;
g_state.second_shot = false;
g_state.applied = false;
g_state.last_shift_tick = -1000;
}
int charge_percent() {
if (g_cfg.max_choked_ticks <= 0)
return 0;
return clamp_int(g_state.charge * 100 / g_cfg.max_choked_ticks, 0, 100);
}
bool lock_desync() {
return g_cfg.freeze_desync && g_state.pending > 0;
}
bool wants_second_shot() {
return g_cfg.double_shot && g_state.pending > 0;
}
bool is_defensive() {
if (!g_bind.local_player || !g_bind.on_ground || !g_bind.speed_2d)
return false;
void* player = g_bind.local_player();
if (!player)
return false;
if (!g_bind.on_ground(player))
return false;
if (g_bind.speed_2d(player) > g_cfg.min_speed)
return false;
return g_bind.aa_defensive ? g_bind.aa_defensive() : true;
}
static int ticks_until_next_shot() {
const float interval = g_bind.interval_per_tick();
if (interval <= 0.0f)
return 0;
void* player = g_bind.local_player();
if (!player)
return 0;
void* weapon = g_bind.active_weapon(player);
if (!weapon)
return 0;
if (g_bind.is_gun && !g_bind.is_gun(weapon))
return 0;
const float cycle = g_bind.cycle_time(weapon);
if (cycle <= 0.0f)
return 0;
return std::max(1, static_cast<int>(std::ceil(cycle / interval)));
}
static bool weapon_ready() {
if (!g_bind.weapon_can_fire)
return true;
void* player = g_bind.local_player();
if (!player)
return false;
void* weapon = g_bind.active_weapon(player);
if (!weapon)
return false;
return g_bind.weapon_can_fire(weapon, g_bind.curtime());
}
static void update_charge() {
void* player = g_bind.local_player();
if (!player)
return;
const int server = g_bind.server_tick();
if (g_state.last_server_tick < 0) {
g_state.last_server_tick = server;
g_state.charge = g_cfg.max_choked_ticks;
}
else if (server != g_state.last_server_tick) {
g_state.last_server_tick = server;
g_state.charge = std::min(g_state.charge + 1, g_cfg.max_choked_ticks);
}
const int lag = std::max(0, g_bind.tickbase(player) - server);
g_state.headroom = clamp_int(process_limit() - lag, 0, process_limit());
}
static int request_shift(int ticks) {
if (ticks <= 0 || g_cfg.max_choked_ticks <= 0)
return 0;
if (g_bind.choked_commands() >= process_limit())
return 0;
int granted = std::min(ticks, g_state.charge);
granted = std::min(granted, g_state.headroom);
granted = std::min(granted, g_cfg.max_choked_ticks);
if (granted <= 0)
return 0;
g_state.charge -= granted;
g_state.headroom -= granted;
return granted;
}
int on_create_move(CUserCmd* cmd) {
if (!ready() || !cmd)
return 0;
const int tick = g_bind.server_tick();
if (tick == g_state.decided_tick)
return g_state.pending;
g_state.decided_tick = tick;
g_state.pending = 0;
g_state.shifted = false;
g_state.second_shot = false;
g_state.applied = false;
void* player = g_bind.local_player();
if (!player)
return 0;
if (g_bind.alive && !g_bind.alive(player))
return 0;
update_charge();
g_state.defensive = is_defensive();
if (!g_cfg.enabled || g_cfg.max_choked_ticks <= 0)
return 0;
if (g_cfg.key != 0 && g_bind.key_down && !g_bind.key_down(g_cfg.key))
return 0;
if (g_cfg.defensive_only && !g_state.defensive)
return 0;
if (!(cmd->buttons & IN_ATTACK))
return 0;
if (!weapon_ready())
return 0;
const int granted = request_shift(ticks_until_next_shot());
if (granted <= 0)
return 0;
g_state.pending = granted;
g_state.shifted = true;
g_state.second_shot = g_cfg.double_shot;
g_state.last_shift_tick = tick;
return granted;
}
void on_run_command(CUserCmd* cmd) {
if (g_state.pending <= 0 || g_state.applied || !ready())
return;
void* player = g_bind.local_player();
if (!player)
return;
const int shifted = g_bind.tickbase(player) + g_state.pending;
g_bind.set_tickbase(player, shifted);
g_state.applied = true;
if (cmd)
cmd->tick_count = shifted;
}
void on_send_command(CUserCmd* cmd) {
if (g_state.pending <= 0)
return;
if (cmd && ready()) {
void* player = g_bind.local_player();
if (player)
cmd->tick_count = g_bind.tickbase(player);
}
g_state.pending = 0;
g_state.second_shot = false;
g_state.applied = false;
}
int run(CUserCmd* cmd) {
const int granted = on_create_move(cmd);
if (granted > 0)
on_run_command(cmd);
return granted;
}
void on_level_shutdown() {
reset();
}
bool can_double_tap() {
if (!ready() || !g_cfg.enabled || g_cfg.max_choked_ticks <= 0)
return false;
if (g_cfg.defensive_only && !is_defensive())
return false;
const int need = ticks_until_next_shot();
return need > 0 && g_state.charge >= need;
}
}