Useful Functions

I wrote a bunch of those here: https://github.com/Southclaw/Scaveng...eapon/misc.pwn

Though users would probably want to alter the function names slightly from my item/weapon abstractions.
Reply

ConvertTimeToString
I made this function quite a while ago, mostly because all the functions available to me didn't suit my OCD as a grammar freak. It basically converts units as small as seconds, into units as large as weeks. Useful in situations such as ban duration.

pawn Код:
stock ConvertTimeToString(weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0)
{
    while(seconds >= 60) minutes++, seconds -= 60;
    while(minutes >= 60) hours++, minutes -= 60;
    while(hours >= 24) days++, hours -= 24;
    while(days >= 7) weeks++, days -= 7;
    new string[70], fstr[20];
    if(weeks) { format(fstr, sizeof(fstr), (weeks == 1) ? ("%d week") : ("%d weeks"), weeks); if(days || hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, 0); }
    if(days) { format(fstr, sizeof(fstr), (days == 1) ? ("%d day") : ("%d days"), days); if(hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(hours) { format(fstr, sizeof(fstr), (hours == 1) ? ("%d hour") : ("%d hours"), hours); if(minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(minutes) { format(fstr, sizeof(fstr), (minutes == 1) ? ("%d minute") : ("%d minutes"), minutes); if(seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(seconds) { format(fstr, sizeof(fstr), (seconds == 1) ? ("%d second") : ("%d seconds"), seconds), strins(string, fstr, strlen(string)); }
    return string;
}
Example of it's usage:
If you wanted 5 days, 49 hours, 17 minutes and 14 seconds:
pawn Код:
printf("%s", ConvertTimeToString(.days = 5, .hours = 49, .minutes = 17, .seconds = 14));
//Outcome: 1 week, 1 hour, 17 minutes, 14 seconds
If you wanted 3 days, 3 hours and 18 minutes:
pawn Код:
printf("%s", ConvertTimeToString(.days = 3, .hours = 3, .minutes = 18));
//Outcome: 3 days, 3 hours, 18 minutes
Another example...
When a player logs in:
pawn Код:
new string[100];
format(string, sizeof(string), "You last logged in: %s ago", ConvertTimeToString(.seconds = (gettime() - LastLoggedIn[playerid])));
SendClientMessage(playerid, -1, string);
LastLoggedIn[playerid] = gettime();
Where 'LastLoggedIn[playerid]' would be the variable you use to store a player's last login time.

--
Honestly, I am just posting this here because I always have trouble finding it when I want to look at it for references, but maybe someone finds this helpful...
Reply

Quote:
Originally Posted by ]Rafaellos[
Посмотреть сообщение
It could be helpful, I guess.

Код:
isBulletWeapon(weaponid)
{
    switch(weaponid)
    {
        case 16..18, 22..39, 41..43: return 1;
    }
    return 0;
}
Function name is wrong. Did you mean "IsWeaponWithAmmo" or something similar?
Reply

strtrunc

Truncates a string. If the string is longer than the max chars specified, the excess characters are removed and replaced with "...".

Example:

pawn Код:
new
    string[24];

strtrunc("This is a test string.", 10, string);
print(string);
Outputs:

pawn Код:
This is a ...
Areas of use:
  • Tab list dialogs where you don't want to show the entire string in the column (especially if it's long).
  • Places where long strings can't normally fit and need to be truncated.
  • And lots more!
Function:

pawn Код:
strtrunc(const source[], chars, dest[], maxlength=sizeof(dest))
{
    if(strlen(source) > chars)
    {
        if(ispacked(source))
        {
            strmid(dest, source, 0, chars, maxlength);
        strcat(dest, "...", maxlength);
    }
    else
    {
        format(dest, maxlength, "%.*s...", chars, source);
    }
    }
    else
    {
        if(ispacked(source))
    {
        strpack(dest, source, maxlength);
        }
    else
    {
        strcat(((dest[0] = 0), dest), source, maxlength);
    }
    }
   
    return 1;
}
How you would normally do it without using the function:

pawn Код:
if(strlen(banReason) > 24)
{
    format(string, sizeof(string), "%s\t%.24s...\t%s", username, banReason, date);
}
else
{
    format(string, sizeof(string), "%s\t%s\t%s", username, banReason, date);
}
How it looks like with this function:

pawn Код:
new
    truncatedReason[24];

strtrunc(banReason, 24, truncatedReason);

format(string, sizeof(string), "%s\t%s\t%s", username, truncatedReason, date);
Reply

strcensor

Censors "bad words" in a string.

Examples:
Код:
input = "fuck you";
strcensor(input); // output = f*** you
input = "fuck you";
strcensor(input, 0); // output = **** you
input = "fuck you";
strcensor(input, 1, 1); // output = f**k you
input = "fuck you bitch";
strcensor(input); // output = f*** you b****
Код:
strcensor(input[], allowed_beginning_chars = 1, allowed_end_chars = 0) {
    static const badwords[][] = {
        "4r5e", "5h1t", "5hit", "a55", "anal", "anus", "ar5e", "arrse", "arse", "ass",
        "ass-fucker", "asses", "assfucker", "assfukka", "asshole", "assholes", "asswhole", "a_s_s", "b!tch", "b00bs",
        "b17ch", "b1tch", "ballbag", "balls", "ballsack", "bastard", "beastial", "beastiality", "bellend", "bestial",
        "bestiality", "bi+ch", "biatch", "bitch", "bitcher", "bitchers", "bitches", "bitchin", "bitching", "bloody",
        "blow job", "blowjob", "blowjobs", "boiolas", "bollock", "bollok", "boner", "boob", "boobs", "booobs",
        "boooobs", "booooobs", "booooooobs", "breasts", "buceta", "bugger", "bum", "bunny fucker", "butt", "butthole",
        "buttmuch", "buttplug", "c0ck", "c0cksucker", "carpet muncher", "cawk", "chink", "cipa", "cl1t", "clit",
        "clitoris", "clits", "cnut", "cock", "cock-sucker", "cockface", "cockhead", "cockmunch", "cockmuncher", "cocks",
        "cocksuck", "cocksucked", "cocksucker", "cocksucking", "cocksucks", "cocksuka", "cocksukka", "cok", "cokmuncher", "coksucka",
        "coon", "cox", "crap", "cum", "cummer", "cumming", "cums", "cumshot", "cunilingus", "cunillingus",
        "cunnilingus", "cunt", "cuntlick", "cuntlicker", "cuntlicking", "cunts", "cyalis", "cyberfuc", "cyberfuck", "cyberfucked",
        "cyberfucker", "cyberfuckers", "cyberfucking", "d1ck", "damn", "dick", "dickhead", "dildo", "dildos", "dink",
        "dinks", "dirsa", "dlck", "dog-fucker", "doggin", "dogging", "donkeyribber", "doosh", "duche", "dyke",
        "ejaculate", "ejaculated", "ejaculates", "ejaculating", "ejaculatings", "ejaculation", "ejakulate", "f u c k", "f u c k e r", "f4nny",
        "fag", "fagging", "faggitt", "faggot", "faggs", "fagot", "fagots", "fags", "fanny", "fannyflaps",
        "fannyfucker", "fanyy", "fatass", "fcuk", "fcuker", "fcuking", "feck", "fecker", "felching", "fellate",
        "fellatio", "fingerfuck", "fingerfucked", "fingerfucker", "fingerfuckers", "fingerfucking", "fingerfucks", "fistfuck", "fistfucked", "fistfucker",
        "fistfuckers", "fistfucking", "fistfuckings", "fistfucks", "flange", "fook", "fooker", "fuck", "fucka", "fucked",
        "fucker", "fuckers", "fuckhead", "fuckheads", "fuckin", "fucking", "fuckings", "fuckingshitmotherfucker", "fuckme", "fucks",
        "fuckwhit", "fuckwit", "fudge packer", "fudgepacker", "fuk", "fuker", "fukker", "fukkin", "fuks", "fukwhit",
        "fukwit", "fux", "fux0r", "f_u_c_k", "gangbang", "gangbanged", "gangbangs", "gaylord", "gaysex", "goatse",
        "God", "god-dam", "god-damned", "goddamn", "goddamned", "hardcoresex", "hell", "heshe", "hoar", "hoare",
        "hoer", "homo", "hore", "horniest", "horny", "hotsex", "jack-off", "jackoff", "jap", "jerk-off",
        "jism", "jiz", "jizm", "jizz", "kawk", "knob", "knobead", "knobed", "knobend", "knobhead",
        "knobjocky", "knobjokey", "kock", "kondum", "kondums", "kum", "kummer", "kumming", "kums", "kunilingus",
        "l3i+ch", "l3itch", "labia", "lust", "lusting", "m0f0", "m0fo", "m45terbate", "ma5terb8", "ma5terbate",
        "masochist", "master-bate", "masterb8", "masterbat*", "masterbat3", "masterbate", "masterbation", "masterbations", "masturbate", "mo-fo",
        "mof0", "mofo", "mothafuck", "mothafucka", "mothafuckas", "mothafuckaz", "mothafucked", "mothafucker", "mothafuckers", "mothafuckin",
        "mothafucking", "mothafuckings", "mothafucks", "mother fucker", "motherfuck", "motherfucked", "motherfucker", "motherfuckers", "motherfuckin", "motherfucking",
        "motherfuckings", "motherfuckka", "motherfucks", "muff", "mutha", "muthafecker", "muthafuckker", "muther", "mutherfucker", "n1gga",
        "n1gger", "nazi", "nigg3r", "nigg4h", "nigga", "niggah", "niggas", "niggaz", "nigger", "niggers",
        "nob", "nob jokey", "nobhead", "nobjocky", "nobjokey", "numbnuts", "nutsack", "orgasim", "orgasims", "orgasm",
        "orgasms", "p0rn", "pawn", "pecker", "penis", "penisfucker", "phonesex", "phuck", "phuk", "phuked",
        "phuking", "phukked", "phukking", "phuks", "phuq", "pigfucker", "pimpis", "piss", "pissed", "pisser",
        "pissers", "pisses", "pissflaps", "pissin", "pissing", "pissoff", "poop", "porn", "porno", "pornography",
        "pornos", "prick", "pricks", "pron", "pube", "pusse", "pussi", "pussies", "pussy", "pussys",
        "rectum", "retard", "rimjaw", "rimming", "s hit", "s.o.b.", "sadist", "schlong", "screwing", "scroat",
        "scrote", "scrotum", "semen", "sex", "sh!+", "sh!t", "sh1t", "shag", "shagger", "shaggin",
        "shagging", "shemale", "shi+", "shit", "shitdick", "shite", "shited", "shitey", "shitfuck", "shitfull",
        "shithead", "shiting", "shitings", "shits", "shitted", "shitter", "shitters", "shitting", "shittings", "shitty",
        "skank", "slut", "sluts", "smegma", "smut", "snatch", "son-of-a-bitch", "spac", "spunk", "s_h_i_t",
        "t1tt1e5", "t1tties", "teets", "teez", "testical", "testicle", "tit", "titfuck", "tits", "titt",
        "tittie5", "tittiefucker", "titties", "tittyfuck", "tittywank", "titwank", "tosser", "turd", "tw4t", "twat",
        "twathead", "twatty", "twunt", "twunter", "v14gra", "v1gra", "vagina", "viagra", "vulva", "w00se",
        "wang", "wank", "wanker", "wanky", "whoar", "whore", "willies", "willy", "xrated", "xxx"
    };

    new badword_count;
    
    for (new pos, i = sizeof badwords - 1; i != 0 ; i--) {
        while ((pos = strfind(input, badwords[i], true, pos), pos) != -1) {
            for (new p; p < strlen(badwords[i]) - allowed_beginning_chars - allowed_end_chars; p++)
                input[pos + p + allowed_beginning_chars] = '*';
            
            badword_count++;
        }
    }
    return badword_count;
}
Reply

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Censors "bad words" in a string.
What about "words-exceptions"?
I'm not sure how in English, but in Russian (for example) there are some words that have a partial match with some bad words, although they are not a bad words.
Reply

Quote:
Originally Posted by OstGot
Посмотреть сообщение
What about "words-exceptions"?
I'm not sure how in English, but in Russian (for example) there are some words that have a partial match with some bad words, although they are not a bad words.
An alternative would be to split the spring into words and compare each words, then join it back together.
Reply

Quote:
Originally Posted by Crayder
Посмотреть сообщение
An alternative would be to split the spring into words and compare each words, then join it back together.
Too not optimal, I think
A better solution (here only the operation principle):
Код:
public OnPlayerText(playerid, text[])
{
	for(new i, j, p, s, t; i < num_s; ++i)
	{
		t = 0;
		while((j = strfind(text, Swears[i], true, t)) != -1)
		{
			s = j + strlen(Swears[i]);
			for(p = 0; p < num_e; ++p)
			{
				if(!(-1 < strfind(text, Exceptions[p], true, t) < s) || strfind(Exceptions[p], Swears[i], true) == -1)
				{
					if(p == num_e - 1)
					{
						for(; j < s; ++j) text[j] = '*';
					}
					continue;
				}
				else
				{
					t = s;
					break;
				}
			}
		}
	}
	return 1;
}
So, you can add a key part of the bad word (and not the whole bad word), and when it finds the part of word within other word-exception, it not will false detect
Reply

IsObstacleOnPath


PHP код:
IsObstacleOnPath(Float:a[], Float:b[], Float:tolerance 60.0) { 
    new 
Float:c[3]; 
    new 
Float:d[3]; 
    new 
Floatretn
    new 
Float:angle = (atan2(a[0]-b[0], a[1]-b[1]))+180.0
    if(
angle 360.0angle -= 360.0
    new 
Float1.0
    new 
FloatVectorSize(a[0]-b[0], a[1]-b[1], 0.0); 
    new 
Floatx; new Floaty
    while( 
<= (j) ) { 
        
c[0]=a[0]+(i*floatsin(angle,degrees)); 
        
c[1]=a[1]+(i*floatcos(angle,degrees)); 
        
d[0]=a[0]+((i-1)*floatsin(angle,degrees)); 
        
d[1]=a[1]+((i-1)*floatcos(angle,degrees)); 
        
CA_FindZ_For2DCoord(c[0],c[1],c[2]); 
        
CA_FindZ_For2DCoord(d[0],d[1],d[2]); 
        
+= 1.0
        
xVectorSize(c[0]-d[0], c[1]-d[1], 0.0); 
        
floatabs(d[2]-c[2]); 
        
retn atan2(y,x); 
        if(
retn tolerance) { 
            return 
true
        } 
    } 
    return 
false

- http://forum.sa-mp.com/showpost.php?...&postcount=415
Reply

Quote:
Originally Posted by Crayder
Посмотреть сообщение
strcensor

Censors "bad words" in a string.

Examples:
Код:
input = "fuck you";
strcensor(input); // output = f*** you
input = "fuck you";
strcensor(input, 0); // output = **** you
input = "fuck you";
strcensor(input, 1, 1); // output = f**k you
input = "fuck you bitch";
strcensor(input); // output = f*** you b****
Код:
strcensor(input[], allowed_beginning_chars = 1, allowed_end_chars = 0) {
    static const badwords[][] = {
        "4r5e", "5h1t", "5hit", "a55", "anal", "anus", "ar5e", "arrse", "arse", "ass",
        "ass-fucker", "asses", "assfucker", "assfukka", "asshole", "assholes", "asswhole", "a_s_s", "b!tch", "b00bs",
        "b17ch", "b1tch", "ballbag", "balls", "ballsack", "bastard", "beastial", "beastiality", "bellend", "bestial",
        "bestiality", "bi+ch", "biatch", "bitch", "bitcher", "bitchers", "bitches", "bitchin", "bitching", "bloody",
        "blow job", "blowjob", "blowjobs", "boiolas", "bollock", "bollok", "boner", "boob", "boobs", "booobs",
        "boooobs", "booooobs", "booooooobs", "breasts", "buceta", "bugger", "bum", "bunny fucker", "butt", "butthole",
        "buttmuch", "buttplug", "c0ck", "c0cksucker", "carpet muncher", "cawk", "chink", "cipa", "cl1t", "clit",
        "clitoris", "clits", "cnut", "cock", "cock-sucker", "cockface", "cockhead", "cockmunch", "cockmuncher", "cocks",
        "cocksuck", "cocksucked", "cocksucker", "cocksucking", "cocksucks", "cocksuka", "cocksukka", "cok", "cokmuncher", "coksucka",
        "coon", "cox", "crap", "cum", "cummer", "cumming", "cums", "cumshot", "cunilingus", "cunillingus",
        "cunnilingus", "cunt", "cuntlick", "cuntlicker", "cuntlicking", "cunts", "cyalis", "cyberfuc", "cyberfuck", "cyberfucked",
        "cyberfucker", "cyberfuckers", "cyberfucking", "d1ck", "damn", "dick", "dickhead", "dildo", "dildos", "dink",
        "dinks", "dirsa", "dlck", "dog-fucker", "doggin", "dogging", "donkeyribber", "doosh", "duche", "dyke",
        "ejaculate", "ejaculated", "ejaculates", "ejaculating", "ejaculatings", "ejaculation", "ejakulate", "f u c k", "f u c k e r", "f4nny",
        "fag", "fagging", "faggitt", "faggot", "faggs", "fagot", "fagots", "fags", "fanny", "fannyflaps",
        "fannyfucker", "fanyy", "fatass", "fcuk", "fcuker", "fcuking", "feck", "fecker", "felching", "fellate",
        "fellatio", "fingerfuck", "fingerfucked", "fingerfucker", "fingerfuckers", "fingerfucking", "fingerfucks", "fistfuck", "fistfucked", "fistfucker",
        "fistfuckers", "fistfucking", "fistfuckings", "fistfucks", "flange", "fook", "fooker", "fuck", "fucka", "fucked",
        "fucker", "fuckers", "fuckhead", "fuckheads", "fuckin", "fucking", "fuckings", "fuckingshitmotherfucker", "fuckme", "fucks",
        "fuckwhit", "fuckwit", "fudge packer", "fudgepacker", "fuk", "fuker", "fukker", "fukkin", "fuks", "fukwhit",
        "fukwit", "fux", "fux0r", "f_u_c_k", "gangbang", "gangbanged", "gangbangs", "gaylord", "gaysex", "goatse",
        "God", "god-dam", "god-damned", "goddamn", "goddamned", "hardcoresex", "hell", "heshe", "hoar", "hoare",
        "hoer", "homo", "hore", "horniest", "horny", "hotsex", "jack-off", "jackoff", "jap", "jerk-off",
        "jism", "jiz", "jizm", "jizz", "kawk", "knob", "knobead", "knobed", "knobend", "knobhead",
        "knobjocky", "knobjokey", "kock", "kondum", "kondums", "kum", "kummer", "kumming", "kums", "kunilingus",
        "l3i+ch", "l3itch", "labia", "lust", "lusting", "m0f0", "m0fo", "m45terbate", "ma5terb8", "ma5terbate",
        "masochist", "master-bate", "masterb8", "masterbat*", "masterbat3", "masterbate", "masterbation", "masterbations", "masturbate", "mo-fo",
        "mof0", "mofo", "mothafuck", "mothafucka", "mothafuckas", "mothafuckaz", "mothafucked", "mothafucker", "mothafuckers", "mothafuckin",
        "mothafucking", "mothafuckings", "mothafucks", "mother fucker", "motherfuck", "motherfucked", "motherfucker", "motherfuckers", "motherfuckin", "motherfucking",
        "motherfuckings", "motherfuckka", "motherfucks", "muff", "mutha", "muthafecker", "muthafuckker", "muther", "mutherfucker", "n1gga",
        "n1gger", "nazi", "nigg3r", "nigg4h", "nigga", "niggah", "niggas", "niggaz", "nigger", "niggers",
        "nob", "nob jokey", "nobhead", "nobjocky", "nobjokey", "numbnuts", "nutsack", "orgasim", "orgasims", "orgasm",
        "orgasms", "p0rn", "pawn", "pecker", "penis", "penisfucker", "phonesex", "phuck", "phuk", "phuked",
        "phuking", "phukked", "phukking", "phuks", "phuq", "pigfucker", "pimpis", "piss", "pissed", "pisser",
        "pissers", "pisses", "pissflaps", "pissin", "pissing", "pissoff", "poop", "porn", "porno", "pornography",
        "pornos", "prick", "pricks", "pron", "pube", "pusse", "pussi", "pussies", "pussy", "pussys",
        "rectum", "retard", "rimjaw", "rimming", "s hit", "s.o.b.", "sadist", "schlong", "screwing", "scroat",
        "scrote", "scrotum", "semen", "sex", "sh!+", "sh!t", "sh1t", "shag", "shagger", "shaggin",
        "shagging", "shemale", "shi+", "shit", "shitdick", "shite", "shited", "shitey", "shitfuck", "shitfull",
        "shithead", "shiting", "shitings", "shits", "shitted", "shitter", "shitters", "shitting", "shittings", "shitty",
        "skank", "slut", "sluts", "smegma", "smut", "snatch", "son-of-a-bitch", "spac", "spunk", "s_h_i_t",
        "t1tt1e5", "t1tties", "teets", "teez", "testical", "testicle", "tit", "titfuck", "tits", "titt",
        "tittie5", "tittiefucker", "titties", "tittyfuck", "tittywank", "titwank", "tosser", "turd", "tw4t", "twat",
        "twathead", "twatty", "twunt", "twunter", "v14gra", "v1gra", "vagina", "viagra", "vulva", "w00se",
        "wang", "wank", "wanker", "wanky", "whoar", "whore", "willies", "willy", "xrated", "xxx"
    };

    new badword_count;
    
    for (new pos, i = sizeof badwords - 1; i != 0 ; i--) {
        while ((pos = strfind(input, badwords[i], true, pos), pos) != -1) {
            for (new p; p < strlen(badwords[i]) - allowed_beginning_chars - allowed_end_chars; p++)
                input[pos + p + allowed_beginning_chars] = '*';
            
            badword_count++;
        }
    }
    return badword_count;
}
Quote:
Originally Posted by Crayder
Посмотреть сообщение
"pawn",
Why the hell would you censor that?

Quote:
Originally Posted by Crayder
Посмотреть сообщение
"lust",
Are you censoring text for sunday church or something?

Quote:
Originally Posted by Crayder
Посмотреть сообщение
"f_u_c_k",
Clearly the player could just try f__u_c_k instead, which would work. Your could make a custom strfind that ignores non-alphanumerics, which would cut down on most of this.

It seems rather inefficient to add many combinations of a word (such as shit, shitdick, shiting, etc.) rather than just having 1 word where you specify whether the beginning and the end has to be an anchor or not (i.e. next to whitespace/bounds).
You should also check for things like using a capital i for a small L:
pawn Код:
strcensor("your mother gives the best bIowjobs") == 0
Edit:
Here's what I mean about a custom strfind:
pawn Код:
stock strfuzzyfind(const string[], const sub[], pos = 0, &start = -1, &end = -1)
{
    new len = strlen(string);
    new sublen = strlen(sub);
    new s[2];
    s[0] = sub[0];
   
    pos -= 1;
   
    while (-1 != (pos = strfind(string, s, true, ++pos))) {
        for (new i = pos, j = 0; i < len; i++) {
            switch (string[i]) {
                case 'a' .. 'z', 'A' .. 'Z': {
                    if (string[i] != sub[j++]) {
                        break;
                    }
                   
                    if (j == sublen) {
                        start = pos;
                        end = i;
                        return pos;
                    }
                }
            }
        }
    }
   
    return -1;
}

main() {
    new pos = -1;
    new input[] = "h e l l o h-e-l-l-o world";
    new sub[] = "hello";
    new start, end;
    while (-1 != (pos = strfuzzyfind(input, sub, pos + 1, start, end))) {
        for (new i = start + 1; i < end; i++) {
            input[i] = '*';
        }
        printf("found at %d", pos);
    }
    printf("%s", input);
}
Output:
Код:
found at 0
found at 10
h*******o h*******o world
I just wrote this, though, so you'll need to test it a bit more..
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Why the hell would you censor that?

Are you censoring text for sunday church or something?

Clearly the player could just try f__u_c_k instead, which would work. Your could make a custom strfind that ignores non-alphanumerics, which would cut down on most of this.

It seems rather inefficient to add many combinations of a word (such as shit, shitdick, shiting, etc.) rather than just having 1 word where you specify whether the beginning and the end has to be an anchor or not (i.e. next to whitespace/bounds).
Lol. I just copied that list off from some random repo on GitHub.
Reply

Function allows you to get a constant value (see enum), defining the ban type what you want to do. (ip, pseudo or the twice).

PHP код:
enum
{
    
INVALID_BAN_TYPE,
    
PSEUDO_BAN_TYPE,
    
IP_BAN_TYPE,
    
TWICE_BAN_TYPE
}
stock GetBanType(const buf[])
{
    new
        
strlen(buf);
    if(
buf[0] == '+' || buf[l-1] == '+') return INVALID_BAN_TYPE;
    new
        
bool:gbt@p,
        
bool:gbt@i,
        
bool:gbt@s;
    for(new 
0li++)
    {
        switch(
buf[i])
        {
            case 
'p''P': {if(gbt@p) return INVALID_BAN_TYPEgbt@true;}
            case 
'i''I': {if(gbt@i) return INVALID_BAN_TYPEgbt@true;}
            case 
'+': {if(gbt@s) return INVALID_BAN_TYPEgbt@true;}
            
            default: return 
INVALID_BAN_TYPE;
        }
    }
    return (
gbt@? (gbt@? (gbt@TWICE_BAN_TYPE INVALID_BAN_TYPE) : PSEUDO_BAN_TYPE) : (gbt@IP_BAN_TYPE INVALID_BAN_TYPE));

Code test :
PHP код:
main()
{        
    new const
        
a[][] =
        {
            
"p+i",
            
"i",
            
"p",
            
"P+I",
            
"I",
            
"P",
            
"pi",
            
"p+",
            
"i+",
            
"+p",
            
"+i",
            
"a+p",
            
"a+a",
            
"p+++i"
        
};
    for(new 
0sizeof(a); i++)
    {
        new
            
bt GetBanType(a[i]);
    
        switch(
bt)
        {
            case 
INVALID_BAN_TYPEprintf("%s -> INVALID_BAN_TYPE"a[i]);
            case 
PSEUDO_BAN_TYPEprintf("%s -> PSEUDO_BAN_TYPE"a[i]);
            case 
IP_BAN_TYPEprintf("%s -> IP_BAN_TYPE"a[i]);
            case 
TWICE_BAN_TYPEprintf("%s -> TWICE_BAN_TYPE"a[i]);
        }
    }

Result :
Код:
p+i -> TWICE_BAN_TYPE
i -> IP_BAN_TYPE
p -> PSEUDO_BAN_TYPE
P+I -> TWICE_BAN_TYPE
I -> IP_BAN_TYPE
P -> PSEUDO_BAN_TYPE
pi -> INVALID_BAN_TYPE
p+ -> INVALID_BAN_TYPE
i+ -> INVALID_BAN_TYPE
+p -> INVALID_BAN_TYPE
+i -> INVALID_BAN_TYPE
a+p -> INVALID_BAN_TYPE
a+a -> INVALID_BAN_TYPE
p+++i -> INVALID_BAN_TYPE
Reply

Wouldn't it be a better idea to use bit masks for that?
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Wouldn't it be a better idea to use bit masks for that?
Seriously, the function isn't allowed to be used several times in little time.
But I don't see, how we can make this function, with bit masks.
Reply

I'm a bit unsure of the function's purpose, but my suggestion was not related to performance.

I mean you could do something like this:
pawn Код:
enum (<<= 1) {
    INVALID_BAN_TYPE = 0b000,
    PSEUDO_BAN_TYPE = 0b001,
    IP_BAN_TYPE // 0b010
}


stock Ban(ban_type) {
    if (ban_type & PSEUDO_BAN_TYPE) {
        DoPseudoBan(...);
    }

    if (ban_type & IP_BAN_TYPE) {
        DoIpBan(...);
    }
}


// only IP ban
Ban(IP_BAN_TYPE);
// this is the "twice" type
Ban(PSEUDO_BAN_TYPE | IP_BAN_TYPE);
Reply

Yes I thought this, but the purpose is to consider the string "p+i", "i" or "p" (+ uppercase) to get a value about this.

After, a simple switch to execute the correct code.
Reply

IsObstacleOnPath

The function is very heavy.

If you know how optimize it, help me

Anyway, here a colission detect between two points

Diference between RayCastLine. IsObstacleOnPath detect only Colissions with 63.5 angle or above. Useful for NPCS path and other.

This version of IsObstacleOnPath works with Bridges and others "Coverage" .


Thank to Crayder. He taught me tips of CA_RayCastLineNormal



PHP код:

IsObstacleOnPath
(Float:a[], Float:b[], Float:tolerance 63.5Floatpitch 0.5) {
    static 
Float:c[3];
    if(
floatabs(a[2] - b[2]) < 0.25) {
        
// if is same Z ground, only check CA_RayCastLine have simple colission
        
return CA_RayCastLine(
        
a[0], a[1], a[2],
        
b[0], b[1], b[2],
        
c[0], c[1], c[2]
        );
    }
    static 
Float:d[3];
    static 
Floatretn;
    static 
Float:angle;
    
// angle between two points
    
angle = (atan2(a[0]-b[0], a[1]-b[1]))+180.0;
    if(
angle 360.0angle -= 360.0;
    static 
Floatipitch;
    
// distance between two points 3d
    
static FloatjVectorSize(a[0]-b[0], a[1]-b[1], 0.0);
    new 
Floatx; new Floaty;
    while( 
<= (j) ) {
        
// check pitch by bitch to detect anormalite of Z ground (obstacle)
        
c[0]=a[0]+(i*floatsin(angle,degrees));
        
c[1]=a[1]+(i*floatcos(angle,degrees));
        
d[0]=a[0]+((i-pitch)*floatsin(angle,degrees));
        
d[1]=a[1]+((i-pitch)*floatcos(angle,degrees));
        
// get find z coord
        
CA_FindZ_For2DCoord(c[0],c[1],c[2]);
        
CA_FindZ_For2DCoord(d[0],d[1],d[2]);
        
+= pitch;
        
// get distance from 2d point
        
VectorSize(c[0]-d[0], c[1]-d[1], 0.0);
        static 
Floatslope;
        static 
Floate[3];
        static 
;
        if(
d[2]>=c[2]) {
            
// check if have possible colission
            
= (d[2]-c[2]);
            
// get angle between z angle
            
retn atan2(y,x);
            
// if have a colission
            
if(retn tolerance) {
                
CA_RayCastLineNormal(
                    
c[0], c[1], c[2],
                    
d[0], d[1], c[2]+0.5,
                    
e[0],e[1],e[2],
                    
slopeslopeslope
                
);
                
// check if is bridge or not
                
return (&& acos(slope) > tolerance);
            }
        }
        else {
            
floatabs(c[2]-d[2]);
            
retn atan2(y,x);
            if(
retn tolerance) {
                
CA_RayCastLineNormal(
                    
d[0], d[1], d[2],
                    
c[0], c[1], d[2]+0.5,
                    
e[0],e[1],e[2],
                    
slopeslopeslope
                
);
                return (
&& acos(slope) > tolerance);
            }
        }
    }
    return 
false;

Ps. My English is Terrible
Reply

Very simple GetFightingStyleName(style, string[], len) function

Gets the name of the specified fight style and writes it to a string.

Код:
static const FightingStyleNames[][] =
{
	"Normal", "Boxing", "Kung fu", "Knee Head",
	"", "", "", "", "", "", "",
	"Grab Kick", "Elbow"
};

stock GetFightingStyleName(style, string[], len)
{
	if(!(3 < style < 8) && !(14 < style < 17)) return 0;
	strmid(string, FightingStyleNames[style - 4], 0, strlen(FightingStyleNames[style - 4]), len);
	return 1;
}
Reply

Quote:
Originally Posted by XO
Посмотреть сообщение
Good function, no memory usage at all, works well.
You should know tricking people into using things like that isn't just going to be allowed.

If anybody sees this, DON'T USE THAT FUCKING FUNCTION.

It repeatedly sets more and more infinite timers each time it is run. It's going to wreck your computer.
Reply

Quote:
Originally Posted by Crayder
Посмотреть сообщение
You should know tricking people into using things like that isn't just going to be allowed.

If anybody sees this, DON'T USE THAT FUCKING FUNCTION.

It repeatedly sets more and more infinite timers each time it is run. It's going to wreck your computer.
This function came in like a wrecking ball..
Anyhow, how is in any kind of sense, someone would ever use my function and what for.
It's just a simple sense of humor, Have a nice day.
Reply


Forum Jump:


Users browsing this thread: 13 Guest(s)