"Dynamic Recoil" (Fully Automatic Anti-Recoil Compensation)

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

"Dynamic Recoil" (Fully Automatic Anti-Recoil Compensation)

Postby USER101 » Wed Apr 17, 2019 3:34 am

"Dynamic Recoil"

Okay folks here it is. Dynamic Recoil / fully automatic anti-recoil compensation!!! I asked around and could not find any examples. So, with some help from “@don’t at me” and "@Buffy" I have come up with a method of automatically adjusting your recoil. Basically, it will auto adjust your vertical recoil each time you move your right thumb stick while firing your weapon. This method should produce better results the more you use a weapon. Its great for multiplayer matches where you use the same weapon. For other game modes like battle royal it should produce a medium between multiple weapons. Eventually I will most likely add the ability to store and switch the recoil value when you hit the button to switch weapons or lock the value all together. For now, there are two ways you can use this. Just keep it on and it should always try to compensate and improve. Or go to a range, then after you have a weapon dialed in, annotate the output value to use in other manual anti-recoil scripts. Such as this one:
https://www.consoletuner.com/forum/viewtopic.php?f=23&t=12205&wpureload=1
This code will most likely be incorporated as an option in that script anyway. The new anti recoil compensation value is output to the Gtuner output window every time you release the trigger. Just squeezing the trigger will not alter the value so long as your stick noise does not exceed 8. If it does just change that variable in the code and consider getting a new controller.

Technically speaking, while the trigger is depressed it records the position of your right thumb stick every 50 milliseconds. Then every time your thumb stick goes back to a neutral position or is released, it sums and averages the last 100 positions and modifies your vertical anti-recoil based on that average. For now the result is not persistent so if you switch slots or reload the script it will reset. There is a balance between usage time and amount of information captured which leads to accuracy. With that being said I slowed down the rate of compensation for more constant accuracy. Meaning it will take you longer to get it dialed in but once its there it should stay pretty consistent. So please go test this on a wall somewhere before jumping into a match. If you notice some crazy forces just try to compensate or reset the script. This hasn't happened to me yet but I have only tested it today and there is definitely room for improvement. This is a beta so it will most likely change. If you use this code please consider leaving feedback so we can continue to improve. To drag and drop just search for “Dynamic Recoil” in the online resources of Gtuner.

Thanks

Licensing/Usage:
Open source code.
Still in beta and unoptimized.
Free to use and modify.
Please DO NOT distribute for profit.

If you use this code please credit:
@User101 - for concept and creation of "Dynamic Recoil"
@Don't at me - for syntax contributions
@Buffy - for syntax contributions

Code: Select all
#pragma METAINFO("Dynamic Recoil", 1, 0, "USER101")
 
#define float  fix32
 
bool Modify = FALSE;
bool run = FALSE;
bool brun = FALSE;
 
uint8 timer = 0;
 
uint16 Modifier;
 
float AntiRecoilVertical;
float AntiRecoilVerticalOld;
float ModifierSum;
float RecoilModifier[100];
float StickNoise = 8.0;
float MinARecoilPercent = 0.0;
 
init{
  pmem_load();
  pmem_read(0, &AntiRecoilVertical);
  printf("%f", abs(AntiRecoilVertical));
  AntiRecoilVerticalOld = AntiRecoilVertical;
}
 
main {
    if (get_val(STICK_2_Y) <  -90.0) run = TRUE;
 
    if (!brun && run){
        combo_run(Sprint);
        brun = TRUE;
    }
    if (get_val(STICK_2_Y) >= -90.0) brun = FALSE;
 
    if (get_actual(BUTTON_5)){
        if (abs(get_actual(STICK_1_X)) < StickNoise) set_val(STICK_1_X, 0.0);
        if (abs(get_actual(STICK_1_Y)) < StickNoise) set_val(STICK_1_Y, 0.0);
        if (abs(get_actual(STICK_2_X)) < StickNoise) set_val(STICK_2_X, 0.0);
        if (abs(get_actual(STICK_2_Y)) < StickNoise) set_val(STICK_2_Y, 0.0);
 
        if (get_val(BUTTON_5)){
            AntiRecoil(STICK_1_Y, AntiRecoilVertical);
        }
    }
 
/****************************START DYNAMIC RECOIL CODE***********************************
    Open source code.
    Still in beta and unoptimized.
    Free to use and modify.
    Please DO NOT distribute for profit.
 
    If you use this code please credit:
 
    @User101 - for concept and creation of "Dynamic Recoil"
    @Don't at me - for syntax contributions
    @Buffy - for syntax contributions
 
*****************************************************************************************/

 
    if(is_active(BUTTON_5)){
        if(elapsed_time()){
          ++timer;
          if(timer==50){
            ++Modifier;
            if(Modifier > 50) Modifier = 0;
            if(abs(get_actual(STICK_1_Y)) >= StickNoise) RecoilModifier[Modifier] = (get_actual(STICK_1_Y));
//              printf("<b>RecoilModifier = %f", RecoilModifier[Modifier]);
            timer = 0;
          }
        }
    }
 
    if(event_release(STICK_1_Y))Modify = TRUE;
 
    if(Modify && is_active(BUTTON_5)){
    uint8 i;
 
        for(i=0; i<100; i++){
            ModifierSum += RecoilModifier[i];
            RecoilModifier[i] = 0.0;
        }
 
    ModifierSum = ModifierSum / 100.0;
//    printf("<b>ModifierSum = %f", ModifierSum);
    AntiRecoilVertical += ModifierSum;
    printf("<b>Anti Recoil Value = %f", AntiRecoilVertical);
    Modifier = 0;
    ModifierSum = 0.00;
    memset(&RecoilModifier, (uint8)0.00, sizeof(RecoilModifier));
    Modify = FALSE;
    }
 
      if(event_release(BUTTON_5))printf("<b>Anti Recoil Value = %f", AntiRecoilVertical);
 
/******************************END DYNAMIC RECOIL CODE************************************/   
 
//Save changes to AntiRecoilVertical on weapon switch (STILL TESTING DO NOT USE AUTO SAVE ON MICRO SD SLOT) writes alot
//     if(event_active(BUTTON_14) && AntiRecoilVerticalOld != AntiRecoilVertical) {
//       pmem_write(0, AntiRecoilVertical);
//       pmem_save();
//       AntiRecoilVerticalOld = AntiRecoilVertical;
//     }
 
 
}
combo Sprint {
    wait(40);
    set_val(BUTTON_9,100);
    wait(40);
    set_val(BUTTON_9,0);
    wait(0);
    run = FALSE;
}
/*****************Recoil function provided by Bonefisher and AryanX***********************/
void AntiRecoil(uint8 AxisToApply, float ARecoilToApply)
{
    float CurrentX = get_val(STICK_1_X);
    float CurrentY = get_val(STICK_1_Y);
    float MinARecoilFactor = MinARecoilPercent / 100.0;
    float MinARecoilToApply = MinARecoilFactor * ARecoilToApply;
    float MovementARecoilToApply = (1.0 - MinARecoilFactor) * ((ARecoilToApply * (100.0 - sqrt(CurrentX*CurrentX + CurrentY*CurrentY))) / (100.0 + abs(CurrentX) + (CurrentY*CurrentY*0.5)));
    set_val(AxisToApply,clamp(MinARecoilToApply + MovementARecoilToApply + get_val(AxisToApply),-100.00,100.00 - MinARecoilToApply));
}
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby Jalal » Wed Apr 17, 2019 7:14 am

thank you sir will try it tonight, sorry for my question but which value that i need to change ? is it this one float RecoilModifier[100];
User avatar
Jalal
Sergeant Major
Sergeant Major
 
Posts: 73
Joined: Wed Apr 26, 2017 7:42 am

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby bonefisher » Wed Apr 17, 2019 8:13 am

Code: Select all
 
#define float  fix32
 
bool Modify = FALSE;
bool run = FALSE;
bool brun = FALSE;
 
uint8 timer = 0;
 
uint16 Modifier;
 
float AntiRecoilVertical;
float AntiRecoilVerticalOld;
float ModifierSum;
float RecoilModifier[100];
float StickNoise = 8.0;
float MinARecoilPercent = 0.0;
 
init{
  pmem_load();
  pmem_read(0, &AntiRecoilVertical);
  printf("%f", abs(AntiRecoilVertical));
  AntiRecoilVerticalOld = AntiRecoilVertical;
}
 
main {
    if (get_val(STICK_2_Y) <  -90.0) run = TRUE;
 
    if (!brun && run){
        combo_run(Sprint);
        brun = TRUE;
    }
    if (get_val(STICK_2_Y) >= -90.0) brun = FALSE;
 
    if (get_actual(BUTTON_5)){
        if (abs(get_actual(STICK_1_X)) < StickNoise) set_val(STICK_1_X, 0.0);
        if (abs(get_actual(STICK_1_Y)) < StickNoise) set_val(STICK_1_Y, 0.0);
        if (abs(get_actual(STICK_2_X)) < StickNoise) set_val(STICK_2_X, 0.0);
        if (abs(get_actual(STICK_2_Y)) < StickNoise) set_val(STICK_2_Y, 0.0);
 
        if (get_val(BUTTON_5)){
            AntiRecoil(STICK_1_Y, AntiRecoilVertical);
        }
    }
 
/****************************START DYNAMIC RECOIL CODE***********************************
    Open source code.
    Still in beta and unoptimized.
    Free to use and modify.
    Please DO NOT distribute for profit.
 
    If you use this code please credit:
 
    @User101 - for concept and creation of "Dynamic Recoil"
    @Don't at me - for syntax contributions
    @Buffy - for syntax contributions
 
*****************************************************************************************/

 
    if(is_active(BUTTON_5)){
        set_val(BUTTON_9, 0.0);
      if(is_release(BUTTON_9) && time_active(BUTTON_9) <= 5000){
        if(elapsed_time()){
          ++timer;
          if(timer==50){
            ++Modifier;
            if(Modifier > 50) Modifier = 0;
            if(abs(get_actual(STICK_1_Y)) >= StickNoise) RecoilModifier[Modifier] = (get_actual(STICK_1_Y));
//              printf("<b>RecoilModifier = %f", RecoilModifier[Modifier]);
            timer = 0;
          }
        }
      }
    }
 
    if(event_release(STICK_1_Y))Modify = TRUE;
 
    if(Modify && is_active(BUTTON_5)){
    uint8 i;
 
        for(i=0; i<100; i++){
            ModifierSum += RecoilModifier[i];
            RecoilModifier[i] = 0.0;
        }
 
    ModifierSum = ModifierSum / 100.0;
//    printf("<b>ModifierSum = %f", ModifierSum);
    AntiRecoilVertical += ModifierSum;
    printf("<b>Anti Recoil Value = %f", AntiRecoilVertical);
    Modifier = 0;
    ModifierSum = 0.00;
    memset(&RecoilModifier, (uint8)0.00, sizeof(RecoilModifier));
    Modify = FALSE;
    }
 
      if(event_release(BUTTON_5))printf("<b>Anti Recoil Value = %f", AntiRecoilVertical);
 
/******************************END DYNAMIC RECOIL CODE************************************/   
 
//Save changes to AntiRecoilVertical on weapon switch (STILL TESTING DO NOT USE AUTO SAVE ON MICRO SD SLOT) writes alot
//     if(event_active(BUTTON_14) && AntiRecoilVerticalOld != AntiRecoilVertical) {
//       pmem_write(0, AntiRecoilVertical);
//       pmem_save();
//       AntiRecoilVerticalOld = AntiRecoilVertical;
//     }
 
 
}
combo Sprint {
    wait(40);
    set_val(BUTTON_9,100);
    wait(40);
    set_val(BUTTON_9,0);
    wait(0);
    run = FALSE;
}
/*****************Recoil function provided by Bonefisher and AryanX***********************/
void AntiRecoil(uint8 AxisToApply, float ARecoilToApply)
{
    float CurrentX = get_val(STICK_1_X);
    float CurrentY = get_val(STICK_1_Y);
    float MinARecoilFactor = MinARecoilPercent / 100.0;
    float MinARecoilToApply = MinARecoilFactor * ARecoilToApply;
    float MovementARecoilToApply = (1.0 - MinARecoilFactor) * ((ARecoilToApply * (100.0 - sqrt(CurrentX*CurrentX + CurrentY*CurrentY))) / (100.0 + abs(CurrentX) + (CurrentY*CurrentY*0.5)));
    set_val(AxisToApply,clamp(MinARecoilToApply + MovementARecoilToApply + get_val(AxisToApply),-100.00,100.00 - MinARecoilToApply));
}
 

Here give this a try! If your holding fire trigger click sprint button which will give you 5 seconds to adjust then it locks in! If not it just constantly adjusts while playing!
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby Rayman413 » Fri Apr 19, 2019 4:59 am

Can u add auto sprint when holding the left analog up i know there are two type where u always click it up or when u just press it up I want to where i press up on the analog it sprints thank you
User avatar
Rayman413
First Sergeant
First Sergeant
 
Posts: 51
Joined: Wed Aug 31, 2016 1:16 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby jelly44 » Fri Apr 19, 2019 6:01 am

Thanks for the script, USER101. Would it be possible to add anti-recoil for horizontal, too?
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby USER101 » Fri Apr 19, 2019 10:37 pm

Rayman413 wrote:Can u add auto sprint when holding the left analog up i know there are two type where u always click it up or when u just press it up I want to where i press up on the analog it sprints thank you


its there, just change the bool run to TRUE and auto run will work
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby USER101 » Fri Apr 19, 2019 10:40 pm

jelly44 wrote:Thanks for the script, USER101. Would it be possible to add anti-recoil for horizontal, too?

The issue with horizontal is that people could run either way and if for example they tend to run left a lot you will have some weird unwanted right compensation.. I will eventually probably just make a "training" mode.
User avatar
USER101
Sergeant Major
Sergeant Major
 
Posts: 100
Joined: Mon Dec 03, 2018 5:45 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby jelly44 » Sat Apr 20, 2019 6:26 am

USER101 wrote:
jelly44 wrote:Thanks for the script, USER101. Would it be possible to add anti-recoil for horizontal, too?

The issue with horizontal is that people could run either way and if for example they tend to run left a lot you will have some weird unwanted right compensation.. I will eventually probably just make a "training" mode.


understood - thank you. I'm yet to try your script.
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby Orta » Sun Apr 21, 2019 1:29 pm

this is what ive been looking for... ggz.. can this be added to your open script COD_BO4 over the original anti recoil. thanks for this again 5/5*
User avatar
Orta
Private
Private
 
Posts: 1
Joined: Wed Jan 02, 2019 7:26 am

Re: "Dynamic Recoil" (Fully Automatic Anti-Recoil Compensati

Postby jelly44 » Sun May 05, 2019 5:21 pm

It is actually working pretty well on SWBFII. Would you mind adding this script to your User Rapid Fire one, too?
- My recommendation for learning C programming: 'C Programming Absolute Beginner's Guide'
- Create your own scripts with GPC2 Scripting
User avatar
jelly44
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 220
Joined: Tue Feb 05, 2019 3:49 pm

Next

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 95 guests