02.07.2013, 04:56
Useful Functions
03.07.2013, 00:11
This is for all old strtok uses.
I build this function (strtok) from the beginning and i could optimize it by more than one and a half the original function:
In addition there's the option to insert the result into an array and not just return it.
Speed results:
Enjoy
I build this function (strtok) from the beginning and i could optimize it by more than one and a half the original function:
PHP код:
stock strtok(str[], &index, array[20] = ' ', dilimiter[2] = ' ') {
format(array, 20, str);
strdel(array, 0, index);
new pos = strfind(array, dilimiter, false);
if(pos == -1) array[index] = EOS;
else {
array[pos] = EOS;
index += pos;
}
index++;
return array;
}
Speed results:
Код:
< speed > Ron's strtok: 1100 < speed > Original strtok: 1791
03.07.2013, 00:26
Few ms of difference, almost everyone use sscanf instead.
03.07.2013, 00:57
03.07.2013, 05:03
03.07.2013, 11:04
(
Последний раз редактировалось Mmartin; 03.07.2013 в 16:12.
)
Thought I'd share even though I wouldn't put my hand in fire that it couldn't be made simpler.
Код:
stock IsRPName(name[]) { new upos=strfind(name, "_"); if(isnull(name)) return false; if(strlen(name)-2<upos<2) return false; for(new i = 0; i < 24; i++) { if(!name[i]) break; if(!i && 65 > name[i] > 90) return false; if(name[i] == 95 && i!=upos) return false; if(upos && i-upos == 1) { if(65 > name[i] > 90) return false; } if(65 <= name[i] <= 90) { if(!(!i || i==upos+1 || (i==2 && upos>5) || (i==upos+3 && strlen(name)-upos>5))) return false; } if(97 > name[i] > 122) return false; } if(upos==-1) return false; if(strlen(name)-upos<3) return false; return true; }
03.07.2013, 13:31
This function checks if two line segments intersect.
pawn Код:
/*
Function:
SegmentsIntersect
Parameters:
Float:x1 - The X start point of the first line segment.
Float:y1 - The Y start point of the first line segment.
Float:x2 - The X end point of the first line segment.
Float:y2 - The Y end point of the first line segment.
Float:x3 - The X start point of the second line segment.
Float:y3 - The Y start point of the second line segment.
Float:x4 - The X end point of the second line segment.
Float:y4 - The Y end point of the second line segment.
Float:x - A float to store the X intersection point in, passed by reference.
Float:y - A float to store the Y intersection point in, passed by reference.
Returns:
0 - line segments do not intersect
1 - line segments intersect
*/
stock SegmentsIntersect(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:x4, Float:y4, &Float:x, &Float:y)
{
new
Float:xA = x2 - x1,
Float:yA = y2 - y1,
Float:xB = x4 - x3,
Float:yB = y4 - y3,
Float:d = xA * yB - yA * xB;
if (!d)
{
// Lines are parallel, or one or both segments are zero-length
return 0;
}
new
Float:xC = x3 - x1,
Float:yC = y3 - y1,
Float:pA = (xC * yB - yC * xB) / d,
Float:pB = (xC * yA - yC * xA) / d;
if (pA < 0 || pA > 1 || pB < 0 || pB > 1)
{
return 0;
}
// Compute the intersection point
x = x1 + pA * xA
y = y1 + pA * yA
return 1;
}
03.07.2013, 14:45
Stylock are you able to make a 3D version of that function?
It will be very useful, seeing as this is a 3D game.
It will be very useful, seeing as this is a 3D game.
03.07.2013, 22:12
04.07.2013, 09:20
05.07.2013, 10:11
It's true, but people that worked already with strtok, in most situation they have to work a lot to change it to sscanf.
Anyway i builded it as a challenge optimize it for me (not for use), i just wanted to see how much hard it to do that.
Anyway i builded it as a challenge optimize it for me (not for use), i just wanted to see how much hard it to do that.
07.07.2013, 17:06
Quote:
For example, using "random(100)" you can get a number ranging from 0-100(max, parameter in random), but with "randomNumber()" you choose how many digits you want your number to have.
pawn Код:
Код:
random(100): 1 random(100): 5 random(100): 57 random(100): 98 pawn Код:
Код:
Random number: 123 Random number: 359 Random number: 811 Random number: 925 |
07.07.2013, 17:17
Quote:
pawn Код:
pawn Код:
Код:
Random string: aTzuiJKLoFdacVb Random number: 665 |
pawn Код:
stock randomString()
{
new str[RND_STR_SIZE];
for(new i = 0; i != RND_STR_SIZE; i++) str[i] = random(2) ? random(26) + 65 : random(26) + 97;
return str;
}
07.07.2013, 19:44
07.07.2013, 21:27
15.07.2013, 11:21
pawn Код:
decode_doors(src, &bonnet, &boot, &driver_door, &passenger_door)
{
new int[1];
int[0] = src;
bonnet = int{3};
boot = int{2};
driver_door = int{1};
passenger_door = int{0};
return 1;
}
EXAMPLE:
pawn Код:
encode_doors(bonnet, boot, driver_door, passenger_door, behind_driver_door, behind_passenger_door)
{
#pragma unused behind_driver_door
#pragma unused behind_passenger_door
return bonnet | (boot << 8) | (driver_door << 16) | (passenger_door << 24);
}
decode_doors(src, &bonnet, &boot, &driver_door, &passenger_door)
{
new int[1];
int[0] = src;
bonnet = int{3};
boot = int{2};
driver_door = int{1};
passenger_door = int{0};
return 1;
}
main()
{
new bonnet, boot, driver_door, passenger_door;
decode_doors(encode_doors(1, 2, 3, 4, 0, 0), bonnet, boot, driver_door, passenger_door);
printf("%d | %d | %d | %d", bonnet, boot, driver_door, passenger_door); // Result: "1 | 2 | 3 | 4"
}
16.07.2013, 22:21
(
Последний раз редактировалось Emmet_; 17.07.2013 в 20:47.
)
whoever -repped me
shall be sent to jail for life
shall be sent to jail for life
16.07.2013, 23:07
[] GetPlayerSex
[] Information:
Character sex verify the server.
[] function:
[] example:
[] Credits:
OTACON
[] Information:
Character sex verify the server.
[] function:
pawn Код:
native GetPlayerSex(playerid, bool:sex);;
pawn Код:
stock GetPlayerSex(playerid, bool:sex) {
if(sex==true) { //man
switch(GetPlayerSkin(playerid)) {
case 0..8,14..30,32..38,42..52,57..62,66..68,70..73,78..84,86,94..128,132..137,142..144: return true;
case 146..147,149,153..156,158..168,170..177,179..189,200,202..204,206,208..209,210,212,213,217: return true;
case 220..223,227..230,234..236,239..242,247..250,252..255,264..297,299,258..262: return true; }
} else { //woman
switch(GetPlayerSkin(playerid)) {
case 9..13,31,39..41,53..56,63..65,69,74..77,85,87..93,129..131,138..141: return true;
case 145,148,150..152,157,169,172,178,190..199,201,205,207,211,214..216,218,219,224..226,231..233: return true;
case 237..238,243..246,251,256,257,263,298: return true; }
} return false;
}
pawn Код:
COMMAND:test(playerid, params[]) {
if(GetPlayerSex(playerid, false)) { //Man
SendClientMessage(playerid, -1, "[TEST] You Man");
}
else if(GetPlayerSex(playerid, true) { //woman
SendClientMessage(playerid, -1, "[TEST] You woman");
}
return true;
}
OTACON
16.07.2013, 23:17
16.07.2013, 23:47
(
Последний раз редактировалось OTACON; 17.07.2013 в 01:46.
)
[] GetPlayerSex v2
[] Information:
Character sex verify the server.
[] function:
[] example:
[] Credits:
OTACON
RyDeR (suggestion)
__________________________________________________ ______________
[] GetPlayerSkinColour
[] Information:
Check the Character Skin color on the Server.
[] Function:
[] example::
[] Creditos:
OTACON
[] Information:
Character sex verify the server.
[] function:
pawn Код:
native GetPlayerSex(playerid);
pawn Код:
stock GetPlayerSex(playerid) {
switch(GetPlayerSkin(playerid)) { //man
case 0..8,14..30,32..38,42..52,57..62,66..68,70..73,78..84,86,94..128,132..137,142..144: return true;
case 146..147,149,153..156,158..168,170..177,179..189,200,202..204,206,208..209,210,212,213,217: return true;
case 220..223,227..230,234..236,239..242,247..250,252..255,264..297,299,258..262: return true;
} return false;
}
pawn Код:
COMMAND:test(playerid, params[]) {
if(GetPlayerSex(playerid)) { //Man
SendClientMessage(playerid, -1, "[TEST] You Man");
} else { //woman
SendClientMessage(playerid, -1, "[TEST] You woman");
} return true;
}
OTACON
RyDeR (suggestion)
__________________________________________________ ______________
[] GetPlayerSkinColour
[] Information:
Check the Character Skin color on the Server.
[] Function:
pawn Код:
native GetPlayerSkinColour(playerid);
pawn Код:
stock GetPlayerSkinColour(playerid) {
switch(!GetPlayerSkin(playerid)) { //black
case 0,4..22,24,25,28,30,35,36,40,46..48,50,51,54,55,58,63..67,69,74,76,78..80,83,84,86: return true;
case 91,98,102..107,131,134,136,139,140,142..146,149..150,156,163,166,168,176,180,182,183: return true;
case 185,187,190,193,195,207,211,214,215,218..223,230,233,238,239,243..245,249,253,256,260: return true;
case 262,263,265,267,269,270,271,274,275,278,279,284,293,296..298: return true;
} return false;
}
pawn Код:
COMMAND:test(playerid, params[]) {
if(GetPlayerSkinColour(playerid)) { //white
SendClientMessage(playerid, -1, "You white");
} else { //black
SendClientMessage(playerid, -1, "You black");
} return true;
}
OTACON
« Next Oldest | Next Newest »
Users browsing this thread: 36 Guest(s)