23.04.2012, 23:15
totalVeh = -1 should be totalVeh = 0
Ah okay, it was just a rough example, I forgot about ALS (I need to read up on that again!)
And good point about the 'Ex' I'll remember that |
#define isNull(%0) \
(%0[0] == 0 || (%0[0] == 1 && %0[1] == 0))
stock
UnParams(params[],format[],{Float,_}:...)
{
if(isNull(params)) return 0;
new
formatPos = 0,
num = numargs(),
startPos = 2,
space = ' ',
startChar = 0,
pos = 0
;
while((startChar = params[pos]) && startChar == space)
{
pos++;
}
while(startPos < num)
{
switch(format[formatPos])
{
case 'd','D':
{
new
str[35],
ch,
posParam = pos+1
;
while((ch = params[posParam]) && ch != space)
{
if(ch > '9' || ch < '0')
{
return 0;
}
posParam++;
}
strmid(str,params,pos,pos + (posParam - pos));
pos = posParam;
setarg(startPos,0,strval(str));
formatPos++;
}
case 's','S':
{
new
str[128],
ch,
posParam = pos + 1,
index
;
while((ch = params[posParam]) && ch != space)
{
posParam++;
}
strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
for(new p,x = strlen(str);p < x;p++)
{
ch = str[p];
setarg(startPos,index,ch);
index++;
}
pos = posParam;
formatPos++;
}
case 'f','F':
{
new
str[35],
ch,
posParam = pos+1,
dots
;
while((ch = params[posParam]) && ch != space)
{
if(ch > '9' || ch < '0')
{
if(ch == '.' && dots < 1) dots++;
else return 0;
}
posParam++;
}
strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
pos = posParam;
setarg(startPos,0,_:floatstr(str));
formatPos++;
}
}
startPos++;
}
return 1;
}
#define isNull(%0) \
(%0[0] == 0 || (%0[0] == 1 && %0[1] == 0))
stock
UnParams(params[],format[],{Float,_}:...)
{
if(isNull(params)) return 0;
new
formatPos = 0,
num = numargs(),
startPos = 2,
space = ' ',
startChar = 0,
pos = 0
;
while((startChar = params[pos]) && startChar == space)
{
pos++;
}
while(startPos < num)
{
switch(format[formatPos])
{
case 'd','D','i','I':
{
new
str[35],
ch,
posParam = pos+1
;
while((ch = params[posParam]) && ch != space)
{
if(ch > '9' || ch < '0')
{
return 0;
}
posParam++;
}
strmid(str,params,pos,pos + (posParam - pos));
pos = posParam;
setarg(startPos,0,strval(str));
formatPos++;
}
case 's','S':
{
new
str[128],
ch,
posParam = pos + 1,
index
;
while((ch = params[posParam]) && ch != space)
{
posParam++;
}
strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
for(new p,x = strlen(str);p < x;p++)
{
ch = str[p];
setarg(startPos,index,ch);
index++;
}
pos = posParam;
formatPos++;
}
case 'f','F':
{
new
str[35],
ch,
posParam = pos+1,
dots
;
while((ch = params[posParam]) && ch != space)
{
if(ch > '9' || ch < '0')
{
if(ch == '.' && dots < 1) dots++;
else return 0;
}
posParam++;
}
strmid(str,params,pos + (((ch = params[pos]) && ch == space) ? 1 : 0),pos + (posParam - pos));
pos = posParam;
setarg(startPos,0,_:floatstr(str));
formatPos++;
}
default: return 0;
}
startPos++;
}
return 1;
}
/*
* strreplace - Replace occurrences of the search string with the replacement string.
*
* Supports both packed and unpacked strings.
*
* Parameters:
* string[] - The string to perform the replacing in.
* search[] - The string to look for.
* replacement[] - The string to put instead of "search".
* ignorecase - Whether the search for "search" should be case-insensitive. Defaults to false.
* pos - The position to start at. Defaults to 0 (the beginning).
* limit - Limit the number of replacements. Defaults to -1 (no limit).
* maxlength - The size of "string". Defaults to sizeof(string), which almost always is what you want.
*
* Returns:
* The number of replacements that were made.
*/
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
// No need to do anything if the limit is 0.
if (limit == 0)
return 0;
new
sublen = strlen(search),
replen = strlen(replacement),
bool:packed = ispacked(string),
maxlen = maxlength,
len = strlen(string),
count = 0
;
// "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
// Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
if (packed)
maxlen *= 4;
// If the length of the substring is 0, we have nothing to look for..
if (!sublen)
return 0;
// In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
// Delete the string we found
strdel(string, pos, pos + sublen);
len -= sublen;
// If there's anything to put as replacement, insert it. Make sure there's enough room first.
if (replen && len + replen < maxlen) {
strins(string, replacement, pos, maxlength);
pos += replen;
len += replen;
}
// Is there a limit of number of replacements, if so, did we break it?
if (limit != -1 && ++count >= limit)
break;
}
return count;
}
new string[200];
// Simple replacement
string = "You can't compare apples and oranges.";
strreplace(string, "apples", "oranges");
print(string);
// Case-insensitive replace
string = "LoremLoremLoremLorem ipsum dolor sit amet, consectetur adipiscing elit.";
strreplace(string, "lorem", "LOREM", .ignorecase = true);
strreplace(string, "DOLOR", "dooloor", .ignorecase = true);
print(string);
// Using an empty replacement string
string = "1111122222333334444455555";
strreplace(string, "2", "");
print(string);
// Using the "limit" argument
string = "bbbbbbbbbb";
strreplace(string, "b", "a", .limit = 5);
print(string);
// Using the "pos" argument
string = "---bbbbbbbbbb---";
strreplace(string, "b", "a", .pos = 5);
print(string);
main()
{
new string[100];
string = "* Kevin the noob is dumb *";
printf("%s", string);
strreplace(string, "Kevin", "Kwarde");
strreplace(string, "*", ">>", .limit = 1);
strreplace(string, "*", "<<", .pos = 1);
strreplace(string, "dumb", "just a regular scripter");
strreplace(string, "noob", "PAWN scripter");
printf("%s", string);
return 1;
}
/*
** Output **
[12:15:24] * Kevin the noob is dumb *
[12:15:24] >> Kwarde the PAWN scripter is just a regular scripter <<
** End Of Output **
*/
stock strDelete(szStr[], ...) {
new
iStart,
iAddr,
iLen,
iPos
;
#emit ADDR.PRI szStr
#emit ADD.C 4
#emit STOR.S.PRI iStart
for(new iEnd = iStart + ((numargs() - 1) << 2); iEnd != iStart; iStart += 4) {
#emit LOAD.S.PRI iStart
#emit LOAD.I
#emit STOR.S.PRI iAddr
#emit PUSH.S iAddr
#emit PUSH.C 4
#emit SYSREQ.C strlen
#emit INC.PRI
#emit STOR.S.PRI iLen
#emit STACK 8
LOOP:
#emit PUSH.C 0
#emit PUSH.C true
#emit PUSH.S iAddr
#emit PUSH.ADR szStr
#emit PUSH.C 16
#emit SYSREQ.C strfind
#emit STACK 20
#emit STOR.S.PRI iPos
if(iPos != -1) {
#emit LOAD.S.PRI iPos
#emit LOAD.S.ALT iLen
#emit ADD
#emit DEC.PRI
#emit PUSH.PRI
#emit PUSH.S iPos
#emit PUSH.ADR szStr
#emit PUSH.C 12
#emit SYSREQ.C strdel
#emit STACK 16
#emit JUMP LOOP
}
}
return 1;
}
new string[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
strDelete(string, "ipsum", "sit", ",", "consectetur", "elit");
print(string); // Prints: "Lorem dolor amet adipiscing ."
strDel(string[],...)
{
new
num = numargs();
for(new i=1;i<num;i++)
{
// GetString
new str[25],a,b;
while(!false)
{
b = getarg(i,a);
if(b == 0) break;
str[a] = b;
a++;
}
//
new pos;
while((pos = strfind(string,str,true)) != -1)
{
strdel(string,pos,pos+strlen(str));
}
}
return true;
}
I've never seen this, so pardon me if it's already been posted:
Converting a price number (e.g. "$1000") into a price string (e.g. "$1,000"): How this works: When you format a string or use SendClientMessageEx, you normally use "$%d" for a price. With this stock, you just use "%s" and it will convert it into currency format. Example: pawn Код:
pawn Код:
|
if(strcmp(tmp,"confirm",true) == 0)
{
new pricestring[32];
new price = PlayerInfo[playerid][hValue]/4;
new j = valstr(pricestring,price);
while(j >= 4) { j -= 3; strins(pricestring,",",j); }
strins(pricestring,"$",0);
GiveMoney(playerid, price);
PlayerPlaySound(playerid, Songs[1][0], 0.0, 0.0, 0.0);
SetTimerEx("StopSongTimer", 5000, false, "i", playerid);
format(string, sizeof(string), "Congratulations, You have sold your property and received 25 percent of the value, %s.", pricestring);
SendClientMessage(playerid, COLOR_YELLOW, string);
format(string, sizeof(string), "~w~Congratulations~n~ You have sold your property for ~n~~g~%s", pricestring);
GameTextForPlayer(playerid, string, 10000, 3);
RejectHouse(playerid);
OnPlayerSave(playerid);
}
Yet another version of quickSort implemented (from C++) to PAWN. I tested serveral times and it worked flawless (even a little bit faster than existing versions).
Output: Код:
-564 -541 -541 0 3 3 12 54 55 66 689 1554 1656 5468484 |
Sexy code SLice x;P
WELL pretty cool job , but Lets take an exaple : we try that code to sort KILLS and get name along with it from a DB or smthng ;and if 2 player has same kills It will mess won't it ? guess it will Give Player Name same for both Kills at that particular rows ? |
Slice just released an include which makes it easier and it's pretty much documented so I recommend checking that out. Click here.
|
new Text:td = TextDrawCreateBox(0x00000099, 100.0, 100.0, -100.0, -100.0);
TextDrawShowForAll(td);
stock Text:TextDrawCreateBox(color, Float:x1, Float:y1, Float:x2, Float:y2) {
new Text:td, Float:height;
// Coords less than 0 will be relative to the bottom-right corner
if (x1 < 0.0) x1 += 640.0;
if (x2 < 0.0) x2 += 640.0;
if (y1 < 0.0) y1 += 480.0;
if (y2 < 0.0) y2 += 480.0;
// Make sure the box is from top/left towards right/bottom
// If not, swap the variables (XOR swap).
if (x1 > x2) x1 ^= x2, x2 ^= x1, x1 ^= x2;
if (y1 > y2) y1 ^= y2, y2 ^= y1, y1 ^= y2;
height = y2 - y1;
td = TextDrawCreate(x1 * 1.003, y1 * 0.941, "_");
TextDrawUseBox(td, true);
TextDrawSetShadow(td, 0);
TextDrawAlignment(td, 1);
TextDrawSetOutline(td, 0);
TextDrawBoxColor(td, color);
TextDrawColor(td, 0);
TextDrawBackgroundColor(td, 0);
TextDrawTextSize(td, (x2 - 4.7) * 1.0025, 0.0);
TextDrawLetterSize(td, 0.0, height * 0.1045 - 0.55);
return td;
}