Anti-Recoil Public Release

GPC2 script programming for Titan Two. Code examples, questions, requests.

Anti-Recoil Public Release

Postby turmoil_manpower » Fri May 12, 2023 7:20 am

This is still a work in progress the original code belongs to DrNefario and I used the Dead zone Removal from Antithesis.
The code has been heavily modified, features I plan on adding Randomization On Shot and a more customizable Anti-Recoil.
I'm not able to test the code for in-game functionality at the moment, but it compiles, if anyone wants to test it and give feedback that would be cool. It has comments throughout, so people can see what each part does.

Code: Select all
// Define constants for vertical and horizontal recoil, and magnitude of adjustment
const fix32 RECOIL_V = 5.0;
const fix32 RECOIL_H = 1.0;
const fix32 Stick_Noise = 13.0;
 
// Define settings for Two Part Anti-Recoil
const fix32 SPLIT_PARAMETER = 0.3; // Radius of inner circle
const fix32 MOVEMENT_THRESHOLD = 0.9; // Radius of outer ring
 
// Declare scaling factor variable
fix32 scale_factor;
 
// Main loop
main {
  if (is_active(BUTTON_5) && is_active(BUTTON_8)) { // Activates the anti-recoil system
    fix32 stick_x = get_val(STICK_1_X);
    fix32 stick_y = get_val(STICK_1_Y);
 
    // Check if right stick position is within inner circle or outer ring
    fix32 stick_dist = sqrt(sq(stick_x) + sq(stick_y));
    if (stick_dist < SPLIT_PARAMETER) {
 
      // No anti-recoil compensation applied within inner circle
      set_val(STICK_1_X, stick_x);
      set_val(STICK_1_Y, stick_y);
    } else if (stick_dist < MOVEMENT_THRESHOLD) {
 
      // Apply anti-recoil compensation to outer ring
      AntiRecoil(STICK_1_Y, STICK_1_X, RECOIL_V, RECOIL_H);
 
      // Get actual stick values
      fix32 stick1_x = get_actual(STICK_1_X);
      fix32 stick1_y = get_actual(STICK_1_Y);
      fix32 stick2_x = get_actual(STICK_2_X);
      fix32 stick2_y = get_actual(STICK_2_Y);
 
      // Deadzone remover
      set_val(STICK_1_X, abs(stick1_x) < Stick_Noise ? 0.0 : get_val(STICK_1_X));
      set_val(STICK_1_Y, abs(stick1_y) < Stick_Noise ? 0.0 : get_val(STICK_1_Y));
      set_val(STICK_2_X, abs(stick2_x) < Stick_Noise ? 0.0 : get_val(STICK_2_X));
      set_val(STICK_2_Y, abs(stick2_y) < Stick_Noise ? 0.0 : get_val(STICK_2_Y));
    }
  }
}
 
// Function to apply anti-recoil to the right stick
void AntiRecoil(uint8 axisY, uint8 axisX, fix32 recoil_Y, fix32 recoil_X)
{
  // Check if recoil exists
  if (recoil_Y + recoil_X)
  {
    // Get actual values of right stick Y and X axis
    fix32 RY = get_actual(axisY);
    fix32 RX = get_actual(axisX);
 
    // Calculate magnitude of current recoil vector
    fix32 RXRY = sq(RY) + sq(RX);
 
    // If recoil vector magnitude is smaller than or equal to adjustment magnitude,
    // apply simple adjustment to stick value based on recoil and input values
    if (RXRY <= sq(Stick_Noise))
    {
      // Calculate scaling factor
      scale_factor = sqrt(sq(recoil_Y) + sq(recoil_X));
 
      // Scale input values based on scaling factor and adjust stick value based on input value magnitude
      set_val(axisY, (recoil_Y / scale_factor) * (scale_factor + Stick_Noise));
      set_val(axisX, (recoil_X / scale_factor) * (scale_factor + Stick_Noise));
    }
    // If recoil vector magnitude is larger than adjustment magnitude,
    // apply more complex adjustment to stick value based on recoil and input values
    else
    {
      // Calculate true radius of current recoil vector and set scaling factor
      fix32 true_radius = min(sqrt(RXRY), 100.0);
      scale_factor = 100.0 - Stick_Noise;
 
      // Scale input values based on current recoil vector radius
      RY *= ((true_radius - Stick_Noise) / true_radius);
      RX *= ((true_radius - Stick_Noise) / true_radius);
 
      // Apply scaling factor to recoil values and adjust stick value based on input value magnitude
      recoil_Y -= ((recoil_Y * 0.80) / scale_factor) * true_radius;
      recoil_X -= ((recoil_X * 0.80) / scale_factor) * true_radius;
      RY += recoil_Y - ((recoil_Y / scale_factor) * abs(RY));
      RX += recoil_X - ((recoil_X / scale_factor) * abs(RX));
 
      // Adjust stick value based on final adjusted input value and adjustment magnitude
      scale_factor = sqrt(sq(RY) + sq(RX));
      RY = (RY / scale_factor) * (scale_factor + Stick_Noise);
      RX = (RX / scale_factor) * (scale_factor + Stick_Noise);
 
      // Set clamped stick value based on final adjusted input value
      set_val(axisY, clamp(RY, -100f, 100f));
      set_val(axisX, clamp(RX, -100f, 100f));
    }
  }
  return;
}
 
Last edited by turmoil_manpower on Tue May 23, 2023 6:07 am, edited 1 time in total.
User avatar
turmoil_manpower
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Sat May 06, 2023 7:41 pm

Re: Anti-Recoil Public Release

Postby Bagura32rus » Thu May 18, 2023 9:41 am

thanks for your work, today I will try it in pubg and give comments
User avatar
Bagura32rus
Sergeant Major
Sergeant Major
 
Posts: 73
Joined: Fri Dec 21, 2018 9:37 pm
Location: RUSSIA

Re: Anti-Recoil Public Release

Postby turmoil_manpower » Tue May 23, 2023 5:55 am

Update 1.1

FEATURES:
• Undetectable
• Adjustable On-The-Fly Anti-Recoil
• Smart Anti-Recoil

Code: Select all
const fix32 verticalCompensation = 5.0;
const fix32 verticalCompensationInc = 0.5;
const fix32 verticalCompensationDec = 0.5;
 
const fix32 horizontalCompensation = 1.0;
 
const fix32 verticalRandomMin = 0.0;
const fix32 verticalRandomMax = 2.0;
const fix32 horizontalRandomMin = 0.0;
const fix32 horizontalRandomMax = 2.0;
 
const fix32 stickNoise = 19.0;
 
const fix32 splitParameter = 0.3;
const fix32 movementThreshold = 0.9;
 
fix32 scaleFactor;
 
main {
    if (get_actual(BUTTON_8) && event_active(BUTTON_10)) verticalCompensation += verticalCompensationInc;
 
    if (get_actual(BUTTON_8) && event_active(BUTTON_11)) verticalCompensation -= verticalCompensationDec;
 
    if (is_active(BUTTON_5) && is_active(BUTTON_8)) {
        fix32 stick_x = get_val(STICK_1_X);
        fix32 stick_y = get_val(STICK_1_Y);
 
        fix32 stick_dist = sqrt(sq(stick_x) + sq(stick_y));
        if (stick_dist < splitParameter) {
 
            set_val(STICK_1_X, stick_x);
            set_val(STICK_1_Y, stick_y);
        } else if (stick_dist < movementThreshold) {
 
            AntiRecoil(STICK_1_Y, STICK_1_X, verticalCompensation, horizontalCompensation);
 
            fix32 stick1_x = get_actual(STICK_1_X);
            fix32 stick1_y = get_actual(STICK_1_Y);
            fix32 stick2_x = get_actual(STICK_2_X);
            fix32 stick2_y = get_actual(STICK_2_Y);
 
            set_val(STICK_1_X, abs(stick1_x) < stickNoise ? 0.0 : get_val(STICK_1_X));
            set_val(STICK_1_Y, abs(stick1_y) < stickNoise ? 0.0 : get_val(STICK_1_Y));
            set_val(STICK_2_X, abs(stick2_x) < stickNoise ? 0.0 : get_val(STICK_2_X));
            set_val(STICK_2_Y, abs(stick2_y) < stickNoise ? 0.0 : get_val(STICK_2_Y));
        }
    }
}
 
void AntiRecoil(uint8 axisY, uint8 axisX, fix32 recoil_Y, fix32 recoil_X)
{
  fix32 randomFactorRY = frand(verticalRandomMin, verticalRandomMax);
  fix32 randomFactorRX = frand(horizontalRandomMin, horizontalRandomMax);
 
  recoil_Y += randomFactorRY;
  recoil_X += randomFactorRX;
 
  if (recoil_Y + recoil_X)
  {
    fix32 RY = get_actual(axisY);
    fix32 RX = get_actual(axisX);
 
    fix32 RXRY = sq(RY) + sq(RX);
 
    if (RXRY <= sq(stickNoise))
    {
      scaleFactor = sqrt(sq(recoil_Y) + sq(recoil_X));
 
      set_val(axisY, (recoil_Y / scaleFactor) * (scaleFactor + stickNoise));
      set_val(axisX, (recoil_X / scaleFactor) * (scaleFactor + stickNoise));
    }
    else
    {
      fix32 true_radius = min(sqrt(RXRY), 100.0);
      scaleFactor = 100.0 - stickNoise;
 
      RY *= ((true_radius - stickNoise) / true_radius);
      RX *= ((true_radius - stickNoise) / true_radius);
 
      recoil_Y -= ((recoil_Y * 0.80) / scaleFactor) * true_radius;
      recoil_X -= ((recoil_X * 0.80) / scaleFactor) * true_radius;
      RY += recoil_Y - ((recoil_Y / scaleFactor) * abs(RY));
      RX += recoil_X - ((recoil_X / scaleFactor) * abs(RX));
 
      scaleFactor = sqrt(sq(RY) + sq(RX));
      RY = (RY / scaleFactor) * (scaleFactor + stickNoise);
      RX = (RX / scaleFactor) * (scaleFactor + stickNoise);
 
      set_val(axisY, clamp(RY, -100f, 100f));
      set_val(axisX, clamp(RX, -100f, 100f));
    }
  }
  return;
}
 
fix32 frand(fix32 vmin, fix32 vmax) {
    return (rand() * (vmax - vmin) + vmin);
}
Attachments
_AntiRecoil_PR.gpc
(3.29 KiB) Downloaded 113 times
User avatar
turmoil_manpower
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Sat May 06, 2023 7:41 pm

Re: Anti-Recoil Public Release

Postby MrBrgv » Wed May 24, 2023 5:17 am

//const fix32 verticalCompensationInc = 0.5;
//const fix32 verticalCompensationDec = 0.5;


if (get_actual(BUTTON_8) && event_active(BUTTON_10)) verticalCompensation += 0.5;

if (get_actual(BUTTON_8) && event_active(BUTTON_11)) verticalCompensation -= 0.5;

save you a few bytes, but over all very nice work
User avatar
MrBrgv
Sergeant First Class
Sergeant First Class
 
Posts: 16
Joined: Sun Dec 27, 2020 9:32 pm

Re: Anti-Recoil Public Release

Postby turmoil_manpower » Fri May 26, 2023 4:05 am

Hi MrBrgv, I initially had the script written that way, but I made a change to use a constant instead so that people can modify the script more easily. However, I have reverted the change in the newest update since it will save some bytes.
User avatar
turmoil_manpower
Staff Sergeant
Staff Sergeant
 
Posts: 11
Joined: Sat May 06, 2023 7:41 pm

Re: Anti-Recoil Public Release

Postby тебяя » Fri Aug 25, 2023 8:44 pm

i try this on pubg,but anti recoil not active. which buttons i need press to active anti recoil?
тебяя
Private First Class
Private First Class
 
Posts: 2
Joined: Wed Mar 22, 2023 7:27 am


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 114 guests