SA-MP Forums Archive
Changing the timer [HELP ASAP] - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Changing the timer [HELP ASAP] (/showthread.php?tid=521673)



Changing the timer [HELP ASAP] - [Cali]ChrOnic_T - 24.06.2014

How to change the timer so it can detect if the weapon is shooting in 2 milliseconds or lest


pawn Код:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    if(Lrapid_oldticks[playerid] == 0) Lrapid_oldticks[playerid] = GetTickCount();
    else {
        new Lrapid_intervals,
            Lrapid_checkreturn = 1;
        if((Lrapid_intervals = GetTickCount() - Lrapid_oldticks[playerid]) <= 35 &&(GetPlayerWeapon(playerid) != 38
            && GetPlayerWeapon(playerid) != 28 && GetPlayerWeapon(playerid) != 32 && GetPlayerWeapon(playerid) != 31)) { //Submachines such as 32, 28 and minigun got a higher fire rate.
            Lrapid_checkreturn = CallLocalFunction("OnPlayerRapidFire", "iii", playerid, weaponid, Lrapid_intervals);
        }
        else if((Lrapid_intervals = GetTickCount() - Lrapid_oldticks[playerid]) <= 370 && (GetPlayerWeapon(playerid) == 34 ||
        GetPlayerWeapon(playerid) == 33)) {
            Lrapid_checkreturn = CallLocalFunction("OnPlayerRapidFire", "iii", playerid, weaponid, Lrapid_intervals);
        }
        Lrapid_oldticks[playerid] = GetTickCount();
        if(!Lrapid_checkreturn) return 0; //Doesn't allow to call the rest 'OnPlayerWeaponShot' works anymore.
    }
    #if defined LRAPID_OnPlayerWeaponShot
    return LRAPID_OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ);
    #else
    return 1;
    #endif
}



Re: Changing the timer [HELP ASAP] - Corekt - 24.06.2014

This
pawn Код:
else if((Lrapid_intervals = GetTickCount() - Lrapid_oldticks[playerid]) <= 370 && (GetPlayerWeapon(playerid) == 34 ||
        GetPlayerWeapon(playerid) == 33)) {
            Lrapid_checkreturn = CallLocalFunction("OnPlayerRapidFire", "iii", playerid, weaponid, Lrapid_intervals);
        }
}
will never get called since the conditions for having a rifle/sniper rifle are already fulfilled for the previous one. You should also assign GetTickCount to a variable so you don't have to keep calling that function. Also no need for GetPlayerWeapon when you have the weaponid already as a parameter.

I've optimized your code below. See the comments for explanation.

pawn Код:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    new
        tickcount = GetTickCount();

    if(Lrapid_oldticks[playerid] == 0) Lrapid_oldticks[playerid] = tickcount;
    else {

        if (weaponid != 28 && weaponid != 31 && weaponid != 32 && weaponid != 38) {
            // Not a submachine or minigun
            new
                Lrapid_checkreturn = 1,
                Lrapid_intervals = tickcount - Lrapid_oldticks[playerid]; // Set only once instead of multiple times

            if ((weaponid != 33 && weaponid != 34 && Lrapid_intervals < 36) || Lrapid_intervals < 371) {
                // pass if interval is less than 36ms, UNLESS weapon is (sniper) rifle then interval can be less than 371ms
                // This will already detect if the interval is 2ms or less, but you can lower the value (36) if needed
                Lrapid_checkreturn = CallLocalFunction("OnPlayerRapidFire", "iii", playerid, weaponid, Lrapid_intervals);
            }
            Lrapid_oldticks[playerid] = tickcount; // Placed here since no need to save tickcount for submachine or minigun
            if(!Lrapid_checkreturn) return 0; // WeaponShot damage cancelled if OnPlayerRapidFire returns 0
        }
    }
    #if defined LRAPID_OnPlayerWeaponShot
    return LRAPID_OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ);
    #else
    return 1;
    #endif
}
Also I should add that 36 milliseconds is low enough to detect rapid fire cheats for non-submachine/non-highrate guns.