[Include] xskins.inc - Skins Info + Skin Check Optimizations
#1

Xtended Information - Skins
IsValidSkin,IsMaleSkin,IsPublicServiceSkin,IsLawEn forcementSkin & Team Skins

Introduction:
I wrote this include for my gamemode and would like to share it with you'll.This include basically is just a copy of the information given at Wiki Skins Page and stores that information as bits.
It gives information about skin validity,male or female,or was it listed under public service skins or law enforcement skins.It also has a feature to set team number for skins and retrieve it whenever you want.

Features:
  • Provides information regard various aspects of a skin
  • SetSkinTeam and GetSkinTeam for optimizing large if checks
  • All the information are stored in bits to reduce the memory consumption
  • Written using Bitwise Operators to make things fast.
Notes:
  • There are no checks to check invalid skinid.The script assumes that the user will give correct skinid.Invalid skinid will crash the server.
  • Since I use just 8 bits the other 24 bits go waste.If someone knows how do I get the 'char' keyword working here.Please let me know.
Functions
List of all the available functions and their usage.

IsValidSkin(skinid)
Description:
Tells if the given skinid is a valid skin or not.
The validity information is collected from the latest wiki and the old bad skin ids are valid because they are supported now.

Params:
skinid - ID of the skin to check

Returns
1 if the skin id is valid
0 if the skin id is invalid

IsMaleSkin(skinid)
Description:
Tells if the skin is a male skin or not.If not then its a female skin.

Params:
skinid - ID of the skin to check

Returns
1 if the skin id is male
0 if the skin id is female


IsPublicServiceSkin(skinid)
Description:
Tells if the given skin is a part of the public service list in SAMP Wiki Skins page.
This include Firefighters,Medics,Bouncer,MIB,Army,Cops,etc

Params:
skinid - ID of the skin to check

Returns
1 if the skin id is listed under public service skins.
0 if the skin id is not listed under public service skins.

IsLawEnforcementSkin(skinid)
Description:
Tells if the given skin is a part of the Law Enforcement list in SAMP Wiki Skins page.

Params:
skinid - ID of the skin to check

Returns
1 if the skin id is listed under law enforcement.
0 if the skin id is not listed under law enforcement.

SetSkinToTeam(skinid,teamid)
Description:
Sets the team id to the skin.Will be returned on GetSkinTeam.

Params:
skinid - ID of the skin
teamid - a team number of your wish ranging from 0-15(supports just 16 teams, must be enough :P )

Returns
Returns the current settings value for the given skin.

GetSkinTeam(skinid)
Description:
Returns the Skin Team number.

Params:
skinid - ID of the skin to retrieve the team number.

Returns
Returns the team number.
By default all the skins belong to team 0.

Examples:
IsValidSkin(skinid)
Код:
if(IsValidSkin(skinid)) 
        SetPlayerSkin(playerid,skinid);
else 
        return SendClientMessage(playerid,0xFF0000,"Invalid Skin ID");
IsMaleSkin(skinid)
Код:
if(IsMaleSkin(skinid)) 
        SendClientMessage(playerid,0x00FF00,"You have chosen a male skin");
else 
        return SendClientMessage(playerid,0x00FF00,"You have chosen a female skin");
IsPublicServiceSkin(skinid)
Код:
if(!IsPublicServiceSkin(skinid)) 
        SetWantedLevel(playerid,3); //Sets wanted level to 3 if the player is not a public service worker.
IsLawEnforcementSkin(skinid)
Код:
if(!IsLawEnforcementSkin(skinid)) 
        SendClientMessage(playerid,0xFF0000,"You are firing in the public.");
SetSkinToTeam
Код:
public OnGameModeInit()
{
    #define TEAM_CIVILIANS 1
    #define TEAM_COPS 2
    SetSkinToTeam(0,TEAM_CIVILIANS);
    SetSkinToTeam(288,TEAM_COPS); 
    //and so on...
    return 1;
}
SetSkinToTeam
Код:
public OnPlayerSpawn()
{
    switch(GetSkinTeam(GetPlayerSkin(playerid)))
    {
         case TEAM_CIVILIANS:
         {
                SendClientMessage(playerid,0xFFFFFF,"You are a civilian.You can rob banks and other buildings.");
         }
         case TEAM_COPS:
         {
                SendClientMessage(playerid,0xFFFFFF,"You are a cop.Go and arrest robbers");
         }

    }
    return 1;
}
Downloads:
Pastebin
Will add a dropbox link soon - Got to go :P

The Final Words:
Please let me know if you find any bug or if you have any suggestion.
If anyone knows how to implement the char keyword here, please contact me so that I can make it better.
All of the information may not be correct because I manually created the bits from the page.If you find any mistakes please tell.
Thanks
Reply
#2

Your script looks a bit advanced while some functions could have been scripted easier. Here's something I made.

pawn Код:
stock IsLawEnforcerSkin(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 265,266,267,280,281,282,283,284,285,286: return 1;
    }
    return 0;
}

stock IsGroveSkin(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 105,106,107,149: return 1;
    }
    return 0;
}

stock IsBallasSkin(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 102,103,104: return 1;
    }
    return 0;
}

stock IsVagosSkin(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 108,109,110: return 1;
    }
    return 0;
}

stock IsAztecasSkin(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 114,115,116: return 1;
    }
    return 0;
}

stock IsPlayerBouncer(playerid)
{
    switch(GetPlayerSkin(playerid))
    {
        case 164,164,165,166: return 1;
    }
    return 0;
}
Reply
#3

Oh LOL!Thats the same code that I was trying to avoid!
You know there are 300 skins.And 300 if checks for each.You don't use them all.Ok say at least 40-100??
And so many checks would take lots of cycles.
Where as mine is just fetching the team id from an array and checking the team id.
As you go more advanced the script gets more optimized.The whole point of keeping it advanced was to keep it fast.I used to use many if checks I made this script to increase the efficiency of my code.
Reply
#4

Quote:
Originally Posted by Yashas
Посмотреть сообщение
Oh LOL!Thats the same code that I was trying to avoid!
You know there are 300 skins.And 300 if checks for each.You don't use them all.Ok say at least 40-100??
And so many checks would take lots of cycles.
Where as mine is just fetching the team id from an array and checking the team id.
As you go more advanced the script gets more optimized.The whole point of keeping it advanced was to keep it fast.I used to use many if checks I made this script to increase the efficiency of my code.
I understand you tried to avoid this though, it isn't really fast and it can't be that advanced, this is a great job though.
Reply
#5

Quote:

it isn't really fast

which code?

added to the speed optimization, it also reduces the memory usage.Variables take 4 bytes(char takes 1) and if you make 300 * 5 * 4 = 300 * 20 = 6000 = 6KB of memory if used int separate variables and most of that memory is waste.Instead this script uses just 300 * 4 = 1200bytes = 1.2KB
And I am trying to make it into char so it would use just 300KB and thats 6000/300= 20x times memory saved.
Reply
#6

I don't think so, I think the default natives and switch in SA-MP is much faster than this include, I've made some speed-test base on my computer, and all the results I got is listed down below

Result
pawn Код:
//Test 1
[14:06:37] With xskins.inc took 83 ms
[14:06:37] Without xskins.inc took 63 ms

//Test 2
[14:08:56] With xskins.inc took 211 ms
[14:08:56] Without xskins.inc took 186 ms

//Test 3
[14:08:59] With xskins.inc took 132 ms
[14:08:59] Without xskins.inc took 91 ms

//Test 4
[14:09:08] With xskins.inc took 156 ms
[14:09:08] Without xskins.inc took 87 ms
The result are based on this code: http://pastebin.com/wngv8Lkr
Reply
#7

Ok,now thats shocking....

EDIT:but I remember I had this faster with BENCHing!Doing some speed tests again!
Reply
#8

Sorry!My code is slower than the native

[20:22:42] TestXSKins:25992661
[20:22:42] TestXSkinsEnd:25992683
[20:22:42] TestNativeStart:25992684
[20:22:42] TestNativeEnd:25992715

http://pastebin.com/9WuFgxim - test code

Shows xskins is faster!

I think its dependent on the skin id or something.If it was a skin that comes early then it would get done at the beginning of the switch case.

EDIT:with a skinid in the middle
[20:25:47] TestXSKins:26177620
[20:25:47] TestXSkinsEnd:26177642
[20:25:47] TestNativeStart:26177642
[20:25:47] TestNativeEnd:26177669
still xskins is faster

xskin faster even at the top - wth
[20:27:27] TestXSKins:26277030
[20:27:27] TestXSkinsEnd:26277052
[20:27:27] TestNativeStart:26277053
[20:27:27] TestNativeEnd:26277081

unexpected-leme check my code
I gave a value which is at the end of ur check.And this is the result.The code is dependent on skinid.
[20:31:15] With xskins.inc took 9 ms
[20:31:15] Without xskins.inc took 12 ms

[20:34:28] With xskins.inc took 13 ms
[20:34:28] Without xskins.inc took 12 ms
the native is fast if I put an id in the middle with your code.

Super Confusion!
I put GetSkinTeam and GetPlayerSkin inside the loop.
Ooops, my code has to get an entry from the array and perform and & operation to get it done!
[20:39:43] TestXSKins:27012889
[20:39:43] TestXSkinsEnd:27012934
[20:39:43] TestNativeStart:27012934
[20:39:43] TestNativeEnd:27012975
Skin ID at the start.Mine got slower.

[20:41:58] TestXSKins:27147975
[20:41:58] TestXSkinsEnd:27148022
[20:41:58] TestNativeStart:27148022
[20:41:58] TestNativeEnd:27148067
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)