Useful Functions

@Luka P.:
Nice but the your code will work slower than a switch-case statement. To be honest, the code below works 2 times faster than your code.

Hope you don't mind.
pawn Код:
stock returnWeaponSlot(weaponid)
{
    switch(weaponid)
    {
        case 0..1: return 0;
        case 2..9: return 1;
        case 22..24: return 2;
        case 25..27: return 3;
        case 28..29: return 4;
        case 30..31: return 5;
        case 33..34: return 6;
        case 35..38: return 7;
        case 16..18, 39: return 8;
        case 41..43: return 9;
        case 11..15: return 10;
        case 44..46: return 11;
        case 40: return 12;
    }
    return -1;
}
Oh, and if you say, it's too long.. Well here, all in one line. But that's ugly.. Though still it shorter.
pawn Код:
stock returnWeaponSlot(weaponid) { switch(weaponid) { case 0..1: return 0; case 2..9: return 1; case 22..24: return 2; case 25..27: return 3; case 28..29: return 4; case 30..31: return 5; case 33..34: return 6; case 35..38: return 7; case 16..18, 39: return 8; case 41..43: return 9; case 11..15: return 10; case 44..46: return 11; case 40: return 12; } return -1; }
Reply

I'm actually glad you want to make the code faster, because I'm using that for my weapon anticheat. Well, if someone wants to know the "trick", you can 'register' weapons by the slots in the pvars and on weapon change you can look up is the player's new weapon inside the 'registred weapons' list.
Reply

The fastest method will surely be array lookup, but it will take a lot of memory.
Код:
new
    g_WeaponSlotTable[] = { /* ... */ };
#define WEAPON_SLOT_LOOKUP(%1) g_WeaponSlotTable[(%1)]
Reply

@******63:
You mean like this?
pawn Код:
stock returnWeaponSlot(weaponid)
{
    new
        weaponSlots[13][9] =
        {
            { 0, 1 },
            { 2, 3, 4, 5, 6, 7, 8, 9 },
            { 22, 23, 24 },
            { 25, 26, 27 },
            { 28, 29 },
            { 30, 31 },
            { 33, 34 },
            { 35, 36, 37, 38 },
            { 16, 17, 18, 39 },
            { 41, 42, 43 },
            { 10, 11, 12, 13, 14, 15 },
            { 44, 45, 46 },
            { 40 }
        }
    ;
    for(new x; x != 13; ++x)
    {
        for(new y; y != 9; ++y)
        {
            if(weaponSlots[x][y] == weaponid) return x;
        }
    }
    return -1;
}
If so, this is slower..
Reply

I think he meant something like that

pawn Код:
new
    weaponSlots[47] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, ...};
pawn Код:
//OnGameModeInit - you could go on above but I am just to lazy to add that number mess
    weaponSlots[22] = weaponSlots[23] = weaponSlots[24] = 2;
    weaponSlots[25] = weaponSlots[26] = weaponSlots[27] = 3;
    weaponSlots[28] = weaponSlots[29] = 4;
    weaponSlots[30] = weaponSlots[31] = 5;
    weaponSlots[33] = weaponSlots[34] = 6;
    weaponSlots[35] = weaponSlots[36] = weaponSlots[37] = weaponSlots[38] = 7;
    weaponSlots[16] = weaponSlots[17] = weaponSlots[18] = weaponSlots[39] = 8;
    weaponSlots[41] = weaponSlots[42] = weaponSlots[43] = 9;
    weaponSlots[10] = weaponSlots[11] = weaponSlots[12] = weaponSlots[13] = weaponSlots[14] = weaponSlots[15] = 10;
    weaponSlots[44] = weaponSlots[45] = weaponSlots[46] = 11;
    weaponSlots[40] = 12;
pawn Код:
//Didnt add any validation checks because I suppose that only valid ids get entered
#define WEAPON_SLOT_LOOKUP(%1) weaponSlots[(%1)]
Reply

I meant to say jump table.
EDIT: Nero_3D showed it.
Reply

@Nero_3D:
Thanks man, looks great
Reply

IsApproximately

pawn Код:
stock IsApproximately(Float:number1, Float:number2, Float:difference)
{
    if(number2 <= number1+difference && number2 >= number1-difference) return true;
    else return false;
}
Very simple though very useful piece of code.
I've made this a long time ago, and I have used it quite a lot.

You can use it to check if number 1 is approximately the same as number 2, instead of checking if it is number 2.

So instead of if(1 == 5) (which checks if 1 is 5,)
you now use if(IsApproximately(1, 5, 6)). Now you check if 1 is approximately the same as 5, with a possible difference of 6.

You can use it (for example) to see if someone's Angle is approximately the same as someone else's angle, if someone's money is approximately the same as someone else's and so on.
Reply

Thanks! I was thinking how to make this yesterday!
Reply

Info (DeletePlayerWeapon)
Deletes a specific weapon of a player.

Code
pawn Код:
stock DeletePlayerWeapon(playerid, weaponid)
{
    new
        gWeaponData[13][2]
    ;
   
    for(new i; i != sizeof(gWeaponData); ++i)
    {
        GetPlayerWeaponData(playerid, i, gWeaponData[i][0], gWeaponData[i][1]);
       
        gWeaponData[i][1] = gWeaponData[i][1] < 0 ? -gWeaponData[i][1] : gWeaponData[i][1];
    }
    ResetPlayerWeapons(playerid);

    for(new i; i != sizeof(gWeaponData); ++i)
    {
        if(gWeaponData[i][0] != weaponid)
        {
            GivePlayerWeapon(playerid, gWeaponData[i][0], gWeaponData[i][1]);
        }
    }
    return 1;
}
Example
pawn Код:
DeletePlayerWeapon(playerid, 28);
This will delete weapon ID 28 (UZI) from the player (if he has one).
Reply

nice one ryder
Reply

Ryder, you may also want to add this check under the first loop because GetPlayerAmmo is bugged, which is what GetPlayerWeaponData uses. (This is because if you give a player a negative amount of ammo, the weapon will show in their inventory, but they can't use it. Once you switch to it, it'll reset you to fist)
pawn Код:
if(gWeaponData[i][1] < 0)
{
   gWeaponData[i][1] = gWeaponData[i][1] * -1;
}
Reply

@Grim_

I thought that was fixed in 0.3b. Isn't it?

EDIT: I changed the code anyway.. =)
Reply

Well when I was fixing an anti-cheat system using that function (almost identical) to remove the cheated weapon, the rest of the weapon was just bugged. With that check it worked
Reply

RGBAToInt(red,green,blue,alpha) by kyoshiro aka kyosaur
What is it?

This define does exactly as it's name implies, it converts rgba values into a single integer which can be used for colors. I haven't seen one posted here yet, so i made 3 different versions and ran speed tests.


Here's the fastest (for those who dont care about the tests):


Code

pawn Код:
#define RGBAToInt(%0,%1,%2,%3) ((16777216 * (%0)) + (65536 * (%1)) + (256 * (%2)) + (%3))

Here are the tests and all three functions for those who were curious:


pawn Код:
#define RGBAToInt1(%0,%1,%2,%3) (((%0) << 24) | ((%1) << 16) | ((%2) << 8 | (%3)))
#define RGBAToInt2(%0,%1,%2,%3) ((16777216 * (%0)) + (65536 * (%1)) + (256 * (%2)) + (%3))
#define RGBAToInt3(%0,%1,%2,%3) ((16777216 * (%0)) | (65536 * (%1)) | (256 * (%2)) | (%3))
I ran all 3 of them 10,000,000 times with randomly generated RGBA values, here are the results:

pawn Код:
//test 1
RGBAToInt (1): Number = 946968831 || Time = 3006 ms
RGBAToInt (2): Number = 946968831 || Time = 2762 ms
RGBAToInt (3): Number = 946968831 || Time = 2894 ms

//test 2
RGBAToInt (1): Number = -2070156545 || Time = 3038 ms
RGBAToInt (2): Number = -2070156545 || Time = 2764 ms
RGBAToInt (3): Number = -2070156545 || Time = 2897 ms

//test 3
RGBAToInt (1): Number = 1266655999 || Time = 3029 ms
RGBAToInt (2): Number = 1266655999 || Time = 2764 ms
RGBAToInt (3): Number = 1266655999 || Time = 2900 ms
Reply

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
RGBAToInt(red,green,blue,alpha) by kyoshiro aka kyosaur
Such a macro isnt needed (in my opinion) since the compiler converts the hex by itself

And at 8F000000 the number turns negative (in the 32 bit version)

And here the best version

pawn Код:
RGBAToInt(%0) (0x%0)
Reply

Like Nero_3D said, compiler will convert the hex itself... You can simly use hex colors (0xRGBA) or if you want to print the color, then use something like this.

Код:
new output[64]; format(output, sizeof(output), "dark red color code: %i", (0xF00000AA));
Reply

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Such a macro isnt needed (in my opinion) since the compiler converts the hex by itself

And at 8F000000 the number turns negative (in the 32 bit version)

And here the best version

pawn Код:
RGBAToInt(%0) (0x%0)
You completely misunderstood what this function does. This converts RGBA into a single integer to be used for colors, its not for converting hex.

Picture a /color command that supports 3 integers as parameters (Red, Green, and Blue) with values from 0-255. With this define it converts it into a single integer which can be used with SetPlayerColor. Thats what this function is for (there are more uses, thats just an example). This is a LOT easier than working with HEX values (IMO).

Here's an online tool which does the same (Its RGBA to Hex, but if you convert hex to an decimal format (base 10) it would be the same as what my define gives you).

RGBA CONVERTER


Edit:

Here's an article i wrote about hex (its rather short, and was just a template for my binary post, but its still useful).

https://sampwiki.blast.hk/wiki/Hex

Hex is just a different numeral system, colors are just one use of hex.
Reply

you sure it is made by you?...

heres a code that i found on my server

pawn Код:
stock
    RGB(red, green, blue, alpha)
{
    /*  Combines a color and returns it, so it can be used in functions.
        @red:           Amount of red color.
        @green:         Amount of green color.
        @blue:          Amount of blue color.
        @alpha:         Amount of alpha transparency.

        -Returns:
        A integer with the combined color.
    */

    return (red * 16777216) + (green * 65536) + (blue * 256) + alpha;
}
i know its slower and all but it calculates the same method as you so...
Reply

good but even DracoBlue released a HexToInt function in his DUtils (ofc it contains other useful functions)
But with his function you can insert strings

pawn Код:
/**
 *  Return the value of an hex-string
 *  @param string
 */

stock HexToInt(string[]) {
  if (string[0]==0) return 0;
  new i;
  new cur=1;
  new res=0;
  for (i=strlen(string);i>0;i--) {
    if (string[i-1]<58) res=res+cur*(string[i-1]-48); else res=res+cur*(string[i-1]-65+10);
    cur=cur*16;
  }
  return res;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)