New simple anti-recoil for all around!

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

Re: New simple anti-recoil for all around!

Postby bonefisher » Thu Aug 09, 2018 1:44 am

Also if you wanted the anti-recoil to run with hold time only with rapid fire change the get_actual to get_val that runs the combo. I myself like it running through to run smoother....
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: New simple anti-recoil for all around!

Postby antithesis » Thu Aug 09, 2018 5:24 am

Very simple AR script bf! I'll give it a bash on the Xim and see how it performs.
Official Australian retailer for Titan One, Titan Two and XIM APEX at Mod Squad
User avatar
antithesis
Colonel
Colonel
 
Posts: 1912
Joined: Sat May 28, 2016 10:45 pm

Re: New simple anti-recoil for all around!

Postby Scachi » Thu Aug 09, 2018 7:19 am

bonefisher wrote:Also if you wanted the anti-recoil to run with hold time only with rapid fire change the get_actual to get_val that runs the combo. I myself like it running through to run smoother....

It looks like this depends very much in which order the T2 executes the combos/functions.
I tried it with the example from antithesis "antithesis Antirecoil & Autofire" after I had problems with almost identical code in my own script and it doesn't work (or doesn't work anymore) to be active on fire hold only.

Reading that code I thought, nice..it will only do anti recoil when RapidHold is active..
but that is not true. It looks like the combo runs after the recoil function and so get_val always return 100 when you are holding down R2, no matter what rapid fire combo does set R2 to.

Parts of that code:
Code: Select all
main {
 
    if (get_val (BUTTON_5)) // only active when firing to allow for microaim adjustments without the input being filtered
    {   
        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 only active when firing
    {   
        combo_run(RapidFire);
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
}
 
combo RapidFire
{   
    set_val(BUTTON_5, 100);
    wait(RapidHold);
    set_val(BUTTON_5, 0);
    wait(RapidRest);
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
 
     RY = get_actual(STICK_1_Y);
     RX = get_actual(STICK_1_X);
 
    if (get_val(BUTTON_5) && (sqrt(RX*RX + RY*RY)) <= abs(recoil))
    {
        if(abs(RY) <= abs(recoil))
        {
            set_val(axis,(recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
        }
    }
}
 


rapid_recoil.png
rapid_recoil.png (21.54 KiB) Viewed 1566 times


To get this working to be active on hold only one have to call the AntiRecoil function from within the combo itself:
Code: Select all
set_val(BUTTON_5, 100);
AntiRecoil(STICK_1_Y,RECOIL_V);
AntiRecoil(STICK_1_X,RECOIL_H);
wait(RapidHold);



Github issue added to get J2Kbr's attention :innocent_smile_1: : https://github.com/J2Kbr/TitanTwo/issues/157
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: New simple anti-recoil for all around!

Postby Jalal » Thu Aug 09, 2018 8:16 am

where i can put the value for stick noise here fix32 noise; ??
init{noise = 5.0;} or this one
User avatar
Jalal
Sergeant Major
Sergeant Major
 
Posts: 73
Joined: Wed Apr 26, 2017 7:42 am

Re: New simple anti-recoil for all around!

Postby Sillyasskid » Thu Aug 09, 2018 8:59 am

Code: Select all
fix32 RECOIL_V, RECOIL_H;
int RapidHold, RapidRest;
#define RY get_actual(STICK_1_Y)
#define RX get_actual(STICK_1_X) 
 
#define StickNoise 4.3 
init {
  RECOIL_V = 30.0;
  RECOIL_H = 0.0;
 
  RapidHold = 50;
  RapidRest = 50;
}
main {
  if(is_active(BUTTON_5)) {   
    AntiRecoil(STICK_1_Y,RECOIL_V);
    AntiRecoil(STICK_1_X,RECOIL_H);
    combo_run(RapidFire);
  }
}
combo RapidFire {   
  set_val(BUTTON_5, 100);
  wait(RapidHold);
  set_val(BUTTON_5, 0);
  wait(RapidRest);
}
void AntiRecoil (uint8 axis, fix32 recoil) {
  if(sqrt(sq(RX)+sq(RY)) < abs(recoil)) {
    set_val(axis, (recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
  }
  if(abs(RY) > recoil || abs(RX) > 20f) set_val(STICK_1_X, RX), set_val(STICK_1_Y, RY);
  if(abs(RX) < StickNoise) set_val(STICK_1_X, 0);
  if(abs(RY) < StickNoise) set_val(STICK_1_Y, 0);
  return;
}


I guess give this a try.
I've not tried this in any game, but the device manager shows it working.
User avatar
Sillyasskid
Captain
Captain
 
Posts: 574
Joined: Sat May 14, 2016 3:07 am

Re: New simple anti-recoil for all around!

Postby bonefisher » Thu Aug 09, 2018 10:32 am

Scachi wrote:
bonefisher wrote:Also if you wanted the anti-recoil to run with hold time only with rapid fire change the get_actual to get_val that runs the combo. I myself like it running through to run smoother....

It looks like this depends very much in which order the T2 executes the combos/functions.
I tried it with the example from antithesis "antithesis Antirecoil & Autofire" after I had problems with almost identical code in my own script and it doesn't work (or doesn't work anymore) to be active on fire hold only.

Reading that code I thought, nice..it will only do anti recoil when RapidHold is active..
but that is not true. It looks like the combo runs after the recoil function and so get_val always return 100 when you are holding down R2, no matter what rapid fire combo does set R2 to.

Parts of that code:
Code: Select all
main {
 
    if (get_val (BUTTON_5)) // only active when firing to allow for microaim adjustments without the input being filtered
    {   
        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 only active when firing
    {   
        combo_run(RapidFire);
        AntiRecoil(STICK_1_Y,RECOIL_V);
        AntiRecoil(STICK_1_X,RECOIL_H);
    }
}
 
combo RapidFire
{   
    set_val(BUTTON_5, 100);
    wait(RapidHold);
    set_val(BUTTON_5, 0);
    wait(RapidRest);
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
 
     RY = get_actual(STICK_1_Y);
     RX = get_actual(STICK_1_X);
 
    if (get_val(BUTTON_5) && (sqrt(RX*RX + RY*RY)) <= abs(recoil))
    {
        if(abs(RY) <= abs(recoil))
        {
            set_val(axis,(recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
        }
    }
}
 


rapid_recoil.png


To get this working to be active on hold only one have to call the AntiRecoil function from within the combo itself:
Code: Select all
set_val(BUTTON_5, 100);
AntiRecoil(STICK_1_Y,RECOIL_V);
AntiRecoil(STICK_1_X,RECOIL_H);
wait(RapidHold);



Github issue added to get J2Kbr's attention :innocent_smile_1: : https://github.com/J2Kbr/TitanTwo/issues/157

Code: Select all
 
#pragma METAINFO("BLUEPRINT FIRE", 1, 0, "bonefisher")
 
/* *****************************************************************************
<cfgdesc>
[BLUEPRINT FIRE]
color       = #349510
shortdesc    = Switching P=primary and S=secondary weapons enabled.
control        = info
 
[ONE CLICK CONFIGURATIONS]
color       = #349510
collapsible = 2
shortdesc    = One click predefined configurations can be incorporated in the Interactive Configuration interface.
control        = config
item        = SLEDGE:02000000090000000000000002001900000000000000000096010000000000000800000000000000020018000000000000000000A0
item        = GIVICFG:03000001680000000000000003000000A0001800000000000001000000000001C20000000000000002000000A00018000000000000
item        = ASH:020000000C0000000000000003001900000003000000000096010000000000000800000000000000020017E66700000000000000A0
item        = THERMITE:020000000C0000000000000004001900000003000000000096010000000000000800000000000000020017E66700000000000000A0
item        = TWITCH:01000000080000000000000003001900000002000000000096010000000000000800000000000000020017E66700000000000000A0
 
[PRIMARY WEAPONS FIRING OPTIONS]
color       = #349510
collapsible = 2
shortdesc    = Enable options on fire trigger.
byteoffset    = 0
bitsize        = 8
control        = radiobox
default        = 0
item        = Disabled for automatics
item        = Enable rapid fire for semi-automatics.
item        = Enable burst firing for automatics.
item        = Enable burst firing for semi-automatics.
 
[PRIMARY WEAPON ROUNDS PER MINUTE]
group        = true
shortdesc    = Adjust rounds per minute.
byteoffset    = 1
bitsize        = 32
control        = spinbox
default        = 360
minimum        = 60
maximum        = 1200
step        = 1
 
[PRIMARY WEAPON SHOTS PER BURST]
group        = true
shortdesc    = Apply burst shots by quick tap of fire trigger or for burst firing.
byteoffset    = 9
bitsize        = 32
control        = spinbox
default        = 1
minimum        = 1
maximum        = 20
step        = 1
 
[PRIMARY WEAPON BURST FIRE]
group        = true
shortdesc    = Adjust release time for burst firing semi-automatics or automatics.
byteoffset    = 13
bitsize        = 32
control        = spinbox
default        = 160
minimum        = 1
maximum        = 1200
step        = 1
 
[PRIMARY WEAPON ANTI-RECOIL VERTICAL SETTINGS]
group        = true
shortdesc    = Vertical compensation settings for primary weapon.
byteoffset    = 17
bitsize        = 32
control        = spinboxf
default        = 0000
minimum        = -10000
maximum        = 10000
step        = 1
decimals    = 2
 
[PRIMARY WEAPON ANTI-RECOIL HORIZONTAL SETTINGS]
group        = true
shortdesc    = Horizontal compensation settings for primary weapon.
byteoffset    = 21
bitsize        = 32
control        = spinboxf
default        = 00000
minimum        = -10000
maximum        = 10000
step        = 1
decimals    = 2
 
[SECONDARY WEAPONS FIRING OPTIONS]
color       = #349510
collapsible = 2
shortdesc    = Enable options on fire trigger.
byteoffset    = 25
bitsize        = 8
control        = radiobox
default        = 0
item        = Disabled for automatics
item        = Enable rapid fire for semi-automatics.
item        = Enable burst firing for automatics.
item        = Enable burst firing for semi-automatics.
 
[SECONDARY WEAPON ROUNDS PER MINUTE]
group        = true
shortdesc    = Adjust rounds per minute.
byteoffset    = 29
bitsize        = 32
control        = spinbox
default        = 450
minimum        = 60
maximum        = 1200
step        = 1
 
[SECONDARY WEAPON SHOTS PER BURST]
group        = true
shortdesc    = Apply burst shots by quick tap of fire trigger or for burst firing.
byteoffset    = 37
bitsize        = 32
control        = spinbox
default        = 1
minimum        = 1
maximum        = 20
step        = 1
 
[SECONDARY WEAPON BURST FIRE]
group        = true
shortdesc    = Adjust release time for burst firing semi-automatics or automatics.
byteoffset    = 41
bitsize        = 32
control        = spinbox
default        = 160
minimum        = 1
maximum        = 1200
step        = 1
 
[SECONDARY WEAPON ANTI-RECOIL VERTICAL SETTINGS]
group        = true
shortdesc    = Vertical compensation settings for primary weapon.
byteoffset    = 45
bitsize        = 32
control        = spinboxf
default        = 0000
minimum        = -10000
maximum        = 10000
step        = 1
decimals    = 2
 
[SECONDARY WEAPON ANTI-RECOIL HORIZONTAL SETTINGS]
group        = true
shortdesc    = Horizontal compensation settings for primary weapon.
byteoffset    = 49
bitsize        = 32
control        = spinboxf
default        = 00000
minimum        = -10000
maximum        = 10000
step        = 1
decimals    = 2
</cfgdesc>
***************************************************************************** */

 
#include <display.gph>
#define WEAPON_0   0
#define WEAPON_1   1
 
uint8  primary_fire;
uint8  secondary_fire;
uint8  weapon = 0;
fix32  noise;
fix32  vertical;
fix32  horizontal;
fix32  vertical2;
fix32  horizontal2;
uint32 rpm;
uint32 rpm2;
uint32 burst;
uint32 burst2;
uint32 tap_fire;
uint32 tap_fire2;
uint32 burst_adjustment;
uint32 burst_adjustment2;
uint32    IND[] = {
    _0_,                  // 0
    _1_,                  // 1
    _2_,                  // 2
    _3_,                  // 3
    _P_,                  // 4
    _S_,                  // 5
    _A_                   // 6
};
init
{
    pmem_load();
    primary_fire = pmem_read(0);
    pmem_read(1, &rpm);
    pmem_read(9, &burst);
    pmem_read(13, &burst_adjustment);
    pmem_read(17, &vertical);
    pmem_read(21, &horizontal);
    secondary_fire = pmem_read(25);
    pmem_read(29, &rpm2);
    pmem_read(37, &burst2);
    pmem_read(41, &burst_adjustment2);
    pmem_read(45, &vertical2);
    pmem_read(49, &horizontal2);
 
    printf("primary_fire: %u", primary_fire);
    printf("rpm: %d", rpm);
    printf("burst: %d", burst);
    printf("burst_adjustment: %d", burst_adjustment);
    printf("vertical: %.04f", vertical);
    printf("horizontal: %.04f", horizontal);
    printf("secondary_fire: %u", secondary_fire);
    printf("rpm2: %d", rpm2);
    printf("burst2: %d", burst2);
    printf("burst_adjustment2: %d", burst_adjustment2);
    printf("vertical2: %.04f", vertical2);
    printf("horizontal2: %.04f", horizontal2);
 
    tap_fire = burst * (1000 / (rpm / 60));
    tap_fire2 = burst2 * (1000 / (rpm2 / 60));
    noise = 5.0;
}
main {
//-------------------------Remove stick noise---------------------------------//
    if(abs(get_actual(STICK_1_X)) < noise) set_val(STICK_1_X, 0.0);
    if(abs(get_actual(STICK_1_Y)) < noise) set_val(STICK_1_Y, 0.0);
    if(abs(get_actual(STICK_2_X)) < noise) set_val(STICK_2_X, 0.0);
    if(abs(get_actual(STICK_2_Y)) < noise) set_val(STICK_2_Y, 0.0);
//--------------------------WEAPON-SWITCHING----------------------------------//
    if(event_release(BUTTON_14) && time_active(BUTTON_14) < 250) {
        if(weapon == WEAPON_0) set_weapon(WEAPON_1);
        else if(weapon == WEAPON_1) set_weapon(WEAPON_0);
    }
//--------------------------RESET-WEAPON-TRACK--------------------------------//
    if(!is_active(BUTTON_12) && !is_active(BUTTON_13) &&
    event_active(BUTTON_11) && time_release(BUTTON_11) < 250) {
        set_weapon(WEAPON_0);
    }
 
    switch(weapon) {
        case WEAPON_0: {
    /* Automatic weapons */
    if(primary_fire == 0) {
    if(get_actual(BUTTON_5)) {
        set_val(BUTTON_5, 100.0);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire) {
        tap_fire = burst * (1000 / (rpm / 60));
        set_val(BUTTON_5, 100.0);
        combo_run(anti_recoil);
    }
    }
    /* Semi- Automatic weapons */
    if(primary_fire == 1) {
    if(get_actual(BUTTON_5)) {
        turbo(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire) {
        turbo(BUTTON_5);
        combo_run(anti_recoil);
    }
    }
     /* Burst for Automatic weapons */
    if(primary_fire == 2) {
        set_val(BUTTON_5, 0.0);
    if(is_active(BUTTON_5)) {
        combo_run(burst_auto);
        turbo(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire) {
        turbo(BUTTON_5);
        combo_run(anti_recoil);
    }
    }
    /* Burst for Semi- Automatic weapons */
    if(primary_fire == 3) {
        set_val(BUTTON_5, 0.0);
    if(is_active(BUTTON_5)) {
        combo_run(burst_rapid);
        turbo(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire) {
        turbo(BUTTON_5);
        combo_run(anti_recoil);
    }
    }
    if(get_val(BUTTON_5))combo_run(anti_recoil);
    if(is_active(STICK_1_Y))combo_stop(anti_recoil);
    if(is_active(STICK_1_X))combo_stop(anti_recoil);
    } break;
        case WEAPON_1: {
    /* Automatic weapons */
    if(secondary_fire == 0) {
    if(get_actual(BUTTON_5)) {
        set_val(BUTTON_5, 100.0);
        combo_run(anti_recoil2);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire2) {
        tap_fire2 = burst2 * (1000 / (rpm2 / 60));
        set_val(BUTTON_5, 100.0);
        combo_run(anti_recoil2);
    }
    }
    /* Semi- Automatic weapons */
    if(secondary_fire == 1) {
    if(get_actual(BUTTON_5)) {
        turbos(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire2) {
        turbos(BUTTON_5);
        combo_run(anti_recoil2);
    }
    }
     /* Burst for Automatic weapons */
    if(secondary_fire == 2) {
        set_val(BUTTON_5, 0.0);
    if(is_active(BUTTON_5)) {
        combo_run(burst_auto2);
        turbos(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire2) {
        turbos(BUTTON_5);
        combo_run(anti_recoil2);
    }
    }
    /* Burst for Semi- Automatic weapons */
    if(secondary_fire == 3) {
        set_val(BUTTON_5, 0.0);
    if(is_active(BUTTON_5)) {
        combo_run(burst_rapid2);
        turbos(BUTTON_5);
    }else if(is_release(BUTTON_5) &&
     time_active(BUTTON_5) <= tap_fire2) {
        turbos(BUTTON_5);
        combo_run(anti_recoil2);
    }
    }
    if(get_actual(BUTTON_5))combo_run(anti_recoil2);
    if(is_active(STICK_1_Y))combo_stop(anti_recoil2);
    if(is_active(STICK_1_X))combo_stop(anti_recoil2);
    } break;
    }
}
 
combo burst_auto
{
    turbo(BUTTON_5);
    set_val(BUTTON_5, 100.0);
    wait(tap_fire);
    set_val(BUTTON_5, 0.0);
    wait(burst_adjustment);
}
 
combo burst_rapid
{
    turbo(BUTTON_5);
    wait(tap_fire);
    set_val(BUTTON_5, 0.0);
    wait(burst_adjustment);
}
 
combo burst_auto2
{
    turbos(BUTTON_5);
    set_val(BUTTON_5, 100.0);
    wait(tap_fire2);
    set_val(BUTTON_5, 0.0);
    wait(burst_adjustment2);
}
 
combo burst_rapid2
{
    turbos(BUTTON_5);
    wait(tap_fire2);
    set_val(BUTTON_5, 0.0);
    wait(burst_adjustment2);
}
 
combo anti_recoil
{
    set_val(STICK_1_Y, vertical);
    set_val(STICK_1_X, horizontal);
}
 
combo anti_recoil2
{
    set_val(STICK_1_Y, vertical2);
    set_val(STICK_1_X, horizontal2);
}
 
void turbo(uint8 button)
{
    uint32 sps = rpm/ 60;
    uint32 rate = 1000 / sps;
    uint32 hold = rate / 2;
 
    uint32 ta = time_active(button);
    set_val(button, 100.0);
 
   while(ta > rate) {
        ta -= rate;
    }
    tap_fire = burst * (1000 / (rpm / 60));
 
    if(ta > hold) {
        set_val(button, 0.0);
    }
}
 
void turbos(uint8 button)
{
    uint32 sps2 = rpm2/ 60;
    uint32 rate2 = 1000 / sps2;
    uint32 hold2 = rate2 / 2;
 
    uint32 ta2 = time_active(button);
    set_val(button, 100.0);
 
   while(ta2 > rate2) {
        ta2 -= rate2;
    }
    tap_fire2 = burst2 * (1000 / (rpm2 / 60));
 
    if(ta2 > hold2) {
        set_val(button, 0.0);
    }
}
 
void set_weapon(uint8 new_weapon)
{
    static uint8 old_weapon;
 
    if(new_weapon == old_weapon) return;
    old_weapon = new_weapon;
    weapon = new_weapon;
 
    static uint32 timestamp;
    uint32 elapsed = system_time() - timestamp;
 
    if(!elapsed || elapsed >= 10) {
    // Set LED
    switch(weapon) {
        case WEAPON_0: { // BLUE LED
            led_set(LED_1, 100.0, 0);
            led_set(LED_2, 0.0, 0);
            led_set(LED_3, 0.0, 0);
            led_set(LED_4, 0.0, 0);
            display (4);
        } break;
        case WEAPON_1: { // RED LED
            led_set(LED_1, 0.0, 0);
            led_set(LED_2, 100.0, 0);
            led_set(LED_3, 0.0, 0);
            led_set(LED_4, 0.0, 0);
            display (5);
        } break;
    }
    timestamp = system_time();
    }
    return;
}
 
 // Display Numbers
void display (uint8 indc)
{
    uint8 VIE;
    VIE = IND[indc];
    display_overlay(VIE, 2000 );
}
 
 

Must be only happening in my function then.. I put in the primary side the get_val and the secondary side I put get_actual..for testing!
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: New simple anti-recoil for all around!

Postby Scachi » Thu Aug 09, 2018 10:44 am

Ah... okay... it seems to work if both are put into a combo, a separate for rapid and recoil works, thank you bonefisher.
I edited Antithesis code only a little bit, I put the recoil function calls in its own combo, right after the RapidFire combo and it works. This is like you did it as it works fine in your code and now it works here too:

if (get_val (BUTTON_5)) //antirecoil only active when firing
{
combo_run(RapidFire);
combo_run(cAntiRecoil);
// AntiRecoil(STICK_1_Y,RECOIL_V);
// AntiRecoil(STICK_1_X,RECOIL_H);
}
}

Complete code:
Code: Select all
#pragma METAINFO("antithesis Antirecoil & Autofire", 1, 00, "modified")
 
 
 //------------------------------------------------------------------------------
// ANTI-RECOIL
fix32 RECOIL_V = 30.0;
fix32 RECOIL_H = 0.0;
fix32 RY;
fix32 RX;
 
//------------------------------------------------------------------------------
// APEX DEADZONE REMOVER
fix32 StickNoise = 4.32;
 
//------------------------------------------------------------------------------
// RAPID-FIRE
 
    // Fine-tune the time between single shots, set to 10 shots / sec by default (100ms)
    uint8 RapidHold = 50; // Hold the button in ms
    uint8 RapidRest = 50; // Release the button in ms
 
main {
 
    if (get_val (BUTTON_5)) // only active when firing to allow for microaim adjustments without the input being filtered
    {   
        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 only active when firing
    {   
        combo_run(RapidFire);
    combo_run(cAntiRecoil);
     //   AntiRecoil(STICK_1_Y,RECOIL_V);
     //  AntiRecoil(STICK_1_X,RECOIL_H);
    }
}
 
combo RapidFire
{   
    set_val(BUTTON_5, 100);
    wait(RapidHold);
    set_val(BUTTON_5, 0);
    wait(RapidRest);
}
 
combo cAntiRecoil {
  AntiRecoil(STICK_1_Y,RECOIL_V);
  AntiRecoil(STICK_1_X,RECOIL_H);
}
 
void AntiRecoil (uint8 axis, fix32 recoil)
{
 
     RY = get_actual(STICK_1_Y);
     RX = get_actual(STICK_1_X);
 
    if (get_val(BUTTON_5) && (sqrt(RX*RX + RY*RY)) <= abs(recoil))
    {
        if(abs(RY) <= abs(recoil))
        {
            set_val(axis,(recoil * (100.0 - abs(get_val(axis)))) / 100.0 + get_val(axis));
        }
    }
}
 
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: New simple anti-recoil for all around!

Postby bonefisher » Thu Aug 09, 2018 10:52 am

Your welcome! Only thing in that code that it stacks when using XIM which I had fixed it in some script around here. Mine doesn't have any drag at all free wheeling.
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: New simple anti-recoil for all around!

Postby paname » Thu Aug 09, 2018 11:49 am

I always prefered to use get_actual vs get_val for this kind of thing.
because if your condition is fire button press, combined with a rapid fire combo, the get_val() state change during the main_loop while get_actual remain constant.



here is one of my anti_recoil include gph file:
you have to define and set s_hipantirecoil_* and s_antirecoi_* variables outside of this script (dependant of weapon selection )
I do this in a menu config with a table for each weapon. same for weapon type tracking.. it is done in another include.



Code: Select all
 
#ifndef ANTIRECOIL_GPH_
#define ANTIRECOIL_GPH_
 
 
 
 
 
// generic antirecoil
accum Yar;
accum Xar;
 
 
// init
init {
 
}
// main
main {
    if (!CONFIG_MODE) {   
        if (get_actual(SHOOT_BUTTON) &&  get_actual(SCOPE_BUTTON)) {
            do_ads_recoil_stuff();
        } else if (get_actual(SHOOT_BUTTON) &&  !get_actual(SCOPE_BUTTON)) {
            do_hip_recoil_stuff();
        }
    }
 
}
// Combos
 
 
combo GenericRecoil {
// RX
    if (Selected_Weapon == Secondary_Weapon) {
        Xar = s_antirecoil_rx ;
    }
    else Xar = antirecoil_rx;
    if (Selected_Weapon == Heavy_Weapon) Xar = 0.00;
 
    AntiRecoilNG(PS4_RX, Xar);
// RY
    if (Selected_Weapon == Secondary_Weapon) {
        Yar = s_antirecoil_ry ;
    }
    else Yar = antirecoil_ry;
    if (Selected_Weapon == Heavy_Weapon) Yar = 0.00;
 
    AntiRecoilNG(PS4_RY, Yar);
 
}
 
combo GenericHipRecoil {
 
// RX
 
    if (Selected_Weapon == Secondary_Weapon) {
        Xar = s_hipantirecoil_rx ;
    }
    else Xar = hipantirecoil_rx;
 
    if (Selected_Weapon == Heavy_Weapon) Xar = 0.00;
 
    AntiRecoilNG(PS4_RX, Xar);
// RY
    if (Selected_Weapon == Secondary_Weapon) {
        Yar = s_hipantirecoil_ry ;
    }
    else Yar = hipantirecoil_ry;
    if (Selected_Weapon == Heavy_Weapon) Yar = 0.00;
    AntiRecoilNG(PS4_RY, Yar);
 
}
 
// Functions
 
 
void do_ads_recoil_stuff() {
 
 
    if ((Selected_Weapon == Secondary_Weapon && RecoilSecRapidFireOn) ||(Selected_Weapon == Primary_Weapon && RecoilPrimRapidFireOn) ) {
        combo_run(GenericRecoil);
    }
 
    return;
}
 
void do_hip_recoil_stuff() {
 
 
    if ((Selected_Weapon == Secondary_Weapon && RecoilSecRapidFireOn) ||(Selected_Weapon == Primary_Weapon && RecoilPrimRapidFireOn) ) {
        combo_run(GenericHipRecoil);
    }
 
 
    return;
}
void AntiRecoilNG(uint8 io, fix32 recoil){
    accum io_value = get_actual(io);
    if (abs(io_value) <6f) {
        io_value = 0.00;
    }
 
    accum result = recoil * (100.00-abs(io_value))/100.00 + io_value;
    accum output = clamp(result,-100.00,100.00);
    set_val(io,output);
    return;
    }
#endif
 
User avatar
paname
Sergeant Major of the Army
Sergeant Major of the Army
 
Posts: 204
Joined: Wed Jan 07, 2015 3:02 pm

Re: New simple anti-recoil for all around!

Postby bonefisher » Thu Aug 09, 2018 12:34 pm

Thanks for sharing paname I'll give it a go! :smile0517:
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

PreviousNext

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 212 guests