SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 11/03/2020, 10:54 AM   #4011
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: Useful Functions

pawn Code:
Color_GetBrightness(color)
{
    new r = ((color >> 24) & 0xFF) * 1000 / 255,
        g = ((color >> 16) & 0xFF) * 1000 / 255,
        b = ((color >> 8) & 0xFF) * 1000 / 255,
       
        max_value = max(max(r, g), b),
        min_value = min(min(r, g), b);
   
    return (max_value + min_value) / 20;
}
or
pawn Code:
RGB_GetBrightness(red_value, green_value, blue_value)
{
    new r = red_value * 1000 / 255,
        g = green_value * 1000 / 255,
        b = blue_value * 1000 / 255,

        max_value = max(max(r, g), b),
        min_value = min(min(r, g), b);
   
    return (max_value + min_value) / 20;
}
returns percentage (0-100) of brightness (ignoring alpha value, only used for client messages).

Example use:
pawn Code:
CMD:color(playerid, params[])
{
    new R, G, B;

    if (sscanf(params, "ddd", R, G, B)) return SendClientMessage(playerid, -1, "USAGE: /color <Red: 0-255> <Green: 0-255> <Blue: 0-255>");
    if (RGB_GetBrightness(R, G, B) <= 10) return SendClientMessage(playerid, -1, "ERROR: Choose a lighter color.");
 
    // create color and set for player
    return 1;
}
__________________
Calisthenics is offline   Reply With Quote
Old 11/03/2020, 11:41 AM   #4012
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 918
Reputation: 89
Default Re: Useful Functions

Quote:
Originally Posted by Calisthenics View Post
//code
With this code can i set color darker or lighter?
Symon is offline   Reply With Quote
Old 11/03/2020, 12:49 PM   #4013
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: Useful Functions

Quote:
Originally Posted by Symon View Post
With this code can i set color darker or lighter?
There are different implementations, I like this one: https://stackoverflow.com/a/13532993

pawn Code:
Color_ChangeShadow(color, percent)
{
    new r = clamp(((color >> 24) & 0xFF) * (100 + percent) / 100, 0, 255),
        g = clamp(((color >> 16) & 0xFF) * (100 + percent) / 100, 0, 255),
        b = clamp(((color >> 8) & 0xFF) * (100 + percent) / 100, 0, 255),
        a = color & 0xFF;
       
    return (r << 24) | (g << 16) | (b << 8) | a;
}
Returns the new color.

percent > 0 (lighter)
percent = 0 (same)
percent < 0 (darker)
__________________
Calisthenics is offline   Reply With Quote
Old 11/03/2020, 02:05 PM   #4014
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 918
Reputation: 89
Default Re: Useful Functions

So can i use it directly with SetPlayerColor?
Symon is offline   Reply With Quote
Old 15/03/2020, 07:34 PM   #4015
OstGot
Gangsta
 
OstGot's Avatar
 
Join Date: Mar 2013
Location: Gomel, Belarus
Posts: 953
Reputation: 333
Default Re: Useful Functions

Another useful vehicle function in addition to the previous post

DoesVehicleHaveTurret

pawn Code:
DoesVehicleHaveTurret(modelid)
{
    switch(modelid)
    {
        case 407, 432, 564, 601: return 1;
    }
    return 0;
}
__________________
aka Nexius
Don't want to always check for updates of my scripts?
Include it the last, after all others
Nexius's Update Checker
OstGot is offline   Reply With Quote
Old 07/04/2020, 11:09 AM   #4016
OstGot
Gangsta
 
OstGot's Avatar
 
Join Date: Mar 2013
Location: Gomel, Belarus
Posts: 953
Reputation: 333
Default Re: Useful Functions

CreateParticle and CreateParticleForPlayer functions:
pawn Code:
stock CreateParticle(type, Float:X, Float:Y, Float:Z, Float:DrawDistance = 0.0, time = 0)
{
    new particleid = CreateObject(type, X, Y, Z, 0.0, 0.0, 0.0, DrawDistance);
    if(time > 0) SetTimerEx("DestroyParticle", time, 0, "i", particleid);
    return particleid;
}

forward DestroyParticle(particleid);
public DestroyParticle(particleid) return DestroyObject(particleid);

stock CreateParticleForPlayer(playerid, type, Float:X, Float:Y, Float:Z, Float:DrawDistance = 0.0, time = 0)
{
    new particleid = CreatePlayerObject(playerid, type, X, Y, Z, 0.0, 0.0, 0.0, DrawDistance);
    if(time > 0) SetTimerEx("DestroyParticleForPlayer", time, 0, "ii", playerid, particleid);
    return particleid;
}

forward DestroyParticleForPlayer(playerid, particleid);
public DestroyParticleForPlayer(playerid, particleid) return DestroyPlayerObject(playerid, particleid);

List of particles (to be used as "type" parameter):
pawn Code:
#define WATERSPLASH             18720
#define FIRE_BIKE               18689
#define FLAMETHROWERP           18694
#define WATER_HYDRANT           18740
#define EXHALE                  18677
#define BLOODPOOL1              19836
#define SMOKE30LIT              18725
#define SHOOTLIGHT              18724
#define RIOT_SMOKE              18723
#define PUKE                    18722
#define PRT_WHEELDIRT           18721
#define PRT_WAKE                18719
#define SMOKE50LIT              18727
#define PRT_SPARK_2             18718
#define PRT_SPARK               18717
#define PRT_SMOKE_EXPAND        18716
#define PRT_SMOKE_HUGE          18715
#define PRT_SAND                18714
#define PRT_SAND2               18713
#define PRT_GUNSHELL            18712
#define PRT_GLASS               18711
#define SMOKE30M                18726
#define SPRAYCANP               18729
#define SMOKE_FLARE             18728
#define WATER_FOUNTAIN          18739
#define WATERFALL_END           18747
#define WATER_SWIM              18746
#define WATER_SPLSH_SML         18745
#define WATER_SPLASH_BIG        18744
#define WATER_SPLASH            18743
#define WATER_SPEED             18742
#define WATER_RIPPLES           18741
#define WATER_FNT_TME           18738
#define PRT_CARDEBRIS           18709
#define WALLBUST                18737
#define VENT                    18736
#define VENT2                   18735
#define TREE_HIT_PALM           18734
#define TREE_HIT_FIR            18733
#define TEARGASAD               18732
#define TEARGAS99               18731
#define TANK_FIRE               18730
#define PRT_COLLISIONSMOKE      18710
#define PRT_BLOOD               18706
#define PRT_BUBBLE              18708
#define COKE_PUFF               18675
#define EXPLOSION_MEDIUM        18683
#define EXPLOSION_LARGE         18682
#define EXPLOSION_FUEL_CAR      18681
#define EXPLOSION_DOOR          18680
#define EXPLOSION_CRATE         18679
#define EXPLOSION_BARREL        18678
#define COKE_TRAIL              18676
#define CLOUDFAST               18674
#define EXPLOSION_SMALL         18685
#define CIGARETTE_SMOKE         18673
#define CEMENTP                 18672
#define CARWASHSPRAY            18671
#define CAMFLASH                18670
#define BOAT_PROP               18669
#define BLOOD_HELI              18668
#define EXPLOSION_MOLOTOV       18684
#define EXPLOSION_TINY          18686
#define PRT_BOATSPLASH          18707
#define JETPACKP                18699
#define PETROLCAN               18705
#define OVERHEAT_CAR_ELEC       18704
#define OVERHEAT_CAR            18703
#define NITROP                  18702
#define MOLOTOV_FLAME           18701
#define JETTHRUST               18700
#define INSECTS                 18698
#define EXTINGUISHER            18687
#define HELI_DUST               18697
#define GUNSMOKE                18696
#define GUNFLASH                18695
#define FLAME99                 18693
#define FIRE_MED                18692
#define FIRE_LARGE              18691
#define FIRE_CAR                18690
#define FIRE                    18688
#define WS_FACTORYSMOKE         18748

Example:
pawn Code:
CreateParticle(PRT_SMOKE_HUGE, 1958.3783, 1343.1572, 15.3746, 0.0, 1500); //Create huge smoke particle for 1500 ms
__________________
aka Nexius
Don't want to always check for updates of my scripts?
Include it the last, after all others
Nexius's Update Checker
OstGot is offline   Reply With Quote
Old 17/05/2020, 09:04 PM   #4017
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,739
Reputation: 1686
Default Re: Useful Functions

Couldn't find a function for this so figured I created one: similar_hexes.
It compares two hex colors (in 0xRRGGBB format, not any other format such as 0xRRGGBBAA or 0xAARRGGBB) and decides if they are similar colors or not:

pawn Code:
similar_hexes(hex_a, hex_b, difference = 150)
{
    new R[2], G[2], B[2], diff[3];
    R[0] = (hex_a >>> 16) & 0xFF;
    G[0] = (hex_a >>> 8) & 0xFF;
    B[0] = (hex_a) & 0xFF;
    R[1] = (hex_b >>> 16) & 0xFF;
    G[1] = (hex_b >>> 8) & 0xFF;
    B[1] = (hex_b) & 0xFF;

    diff[0] = floatround(difference * 0.14);
    diff[1] = floatround(difference * 0.2);
    diff[2] = floatround(difference * 0.48);

    return //returns 1(true) if they are similar and 0(false) if they are not.
    (
        (R[0] - diff[0] <= R[1] && R[1] <= R[0] + diff[0]) &&
        (G[0] - diff[1] <= G[1] && G[1] <= G[0] + diff[1]) &&
        (B[0] - diff[2] <= B[1] && B[1] <= B[0] + diff[2])
    );
}

It is not always totally accurate (eg. 5566FF and 6655CC don't look alot like each other but it is returned as "similar" anyway -just like eg. E7E7E7 and F0F0B0) but I got the best results with most tests so far with "difference = 150".
Had to tweak differences for R,G and B a bit because otherwise it would return "not similar" if the colors were actually similar (most being 'faulty' in the BB value) - and the R value would often return "similar" while they weren't.

Feel free to improve it.

If there's a better function around I'd like to know aswell


Here's a small (and very poorly scripted) testscript (.html) to test it if you'd like; https://pastebin.com/mhp0pa5g
Insert RRGGBB color (without 0x) in the two boxes and press "Check".
It will show both colors you entered and will print "YES" if they are similar and "NO" if they are not.
__________________
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009

Last edited by Kwarde; 18/05/2020 at 01:36 AM.
Kwarde is offline   Reply With Quote
Old 18/05/2020, 05:26 AM   #4018
StevenLV
Big Clucker
 
StevenLV's Avatar
 
Join Date: Apr 2017
Location: Pakistan
Posts: 144
Reputation: 11
Default Re: Useful Functions

A poorly coded number checker, what does it do?
It gets me the number written in a string firstly. like Stringab12cd456gh returns 12 !
i wanted it in some process so did this shit:

like:
PHP Code:
printf("ANDEBUG: %i"GetNumberFromString("ezpz12r3")); 
prints 12.

PHP Code:
GetNumberFromString(string[])
{
    new 
str[10], cnt = -1tmps[2];
    for(new 
0len strlen(string); leni++)
    {
        if(
isNumber(stringi) && cnt == -1)
        {
            
format(tmps2string[i]);
            
strcat(strtmps);
            
cnt i;
        }
        if(
isNumber(stringi) && cnt == i-1)
        {
            
format(tmps2string[i]);
            
strcat(strtmps);
            
cnt i;
        }
    }
    new 
intt;
    
intt strval(str);
    return 
intt;
}

isNumber(c[], index)
{
    switch(
c[index])
    {
        case 
'0' .. '9': return true;
        default: return 
false;
    }
    return 
false;

__________________
Quote:
Eternal Life Roleplay - An upcoming unique RP server based in Palomino Creek
https://discord.gg/JWzE78r
Quote:
Originally Posted by !damo!spiderman View Post
You're pathetic. Stand for something that's going to help the world rather than fill it with more hate. Give someone a hug, tell someone you care... Do something, do anything but do it with love not hate.
StevenLV is offline   Reply With Quote
Old 18/05/2020, 11:00 AM   #4019
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,885
Reputation: 1337
Default Re: Useful Functions

@Nexius I like that you used global objects for temp objects.
Pottus is offline   Reply With Quote
Old 29/05/2020, 09:39 PM   #4020
OstGot
Gangsta
 
OstGot's Avatar
 
Join Date: Mar 2013
Location: Gomel, Belarus
Posts: 953
Reputation: 333
Default Re: Useful Functions

Quote:
Originally Posted by Pottus View Post
@Nexius I like that you used global objects for temp objects.
You can easily use streamer's dynamic objects as well, but I prefer not to tow dependencies in any code I see which might not even need them for some people in their scripts.
__________________
aka Nexius
Don't want to always check for updates of my scripts?
Include it the last, after all others
Nexius's Update Checker
OstGot is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 14 21/04/2018 04:06 AM
Functions? sciman001 Help Archive 7 19/03/2011 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 01:56 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.