stock strCopyEx(szSrc[], szDest[], const szDelim[], iSize = sizeof(szDest)) {
new
iPos
;
if((iPos = strfind(szSrc, szDelim[0], true)) != -1) {
szSrc[iPos] = EOS;
iSize = strcat(szDest, szSrc, iSize);
szSrc[iPos] = szDelim[0];
}
else {
strcat(szDest, szSrc, iSize);
}
return iSize;
}
new
szStr[] = "This-is-a-test-string-!",
szDest[7]
;
strCopyEx(szStr[5], szDest, "-");
print(szDest); // Prints "is"
szDest[0] = EOS;
strCopyEx(szStr[15], szDest, "-");
print(szDest); // Prints "string"
stock strExplode(szStr[], const szDelim[], szOutput[][], const iSize = sizeof(szOutput)) {
new
i,
iPos,
iLen = strlen(szStr)
;
do {
iPos += strCopyEx(szStr[iPos], szOutput[i], szDelim[0], iLen) + 1;
}
while(iPos < iLen && ++i < iSize);
return i;
}
new
szStr[] = "This-is-a-test-string-!",
szOutput[6][7] // We have 6 string parts and the biggest length is 7
;
strExplode(szStr, "-", szOutput);
print(szOutput[0]); // This
print(szOutput[1]); // is
print(szOutput[2]); // a
print(szOutput[3]); // test
print(szOutput[4]); // string
print(szOutput[5]); // !
I'm still wondering why you write "sz" before every variable/array you make.
Nice functions, however. |
new
szStr[] = "This-is-a-test-stringgggggg-!",
szOutput[6][7] // We have 6 string parts and the biggest length is 7
;
strExplode(szStr, "-", szOutput);
Hungarian notation. The variables are prefixed with their "type".
szTest[] = string, zero-terminated (all strings in PAWN are what's called zero-terminated, i.e. the text ends at the first 0 in that array). fTest = float iTest = integer Also, RyDeR`, you should make sure the length of the 2nd dimension in the output for your explode function has boundary checks. Right now this will cause strange behavior: pawn Код:
|
Hungarian notation. The variables are prefixed with their "type".
szTest[] = string, zero-terminated (all strings in PAWN are what's called zero-terminated, i.e. the text ends at the first 0 in that array). fTest = float iTest = integer |
stock IsWeaponInAnySlot(playerid, weaponid)
{
static
szWeapon, szAmmo
;
GetPlayerWeaponData(playerid, GetWeaponSlot(weaponid), szWeapon, szAmmo);
if(szWeapon == weaponid) return true;
#pragma unused szAmmo
return false;
}
stock GetPlayerWeaponInSlot(playerid, slot)
{
static
szWeapon, szAmmo
;
GetPlayerWeaponData(playerid, slot, szWeapon, szAmmo);
#pragma unused szAmmo
return szWeapon;
}
stock GetWeaponSlot(weaponid)
{
switch(weaponid)
{
case WEAPON_BRASSKNUCKLE:
return 0;
case WEAPON_GOLFCLUB .. WEAPON_CHAINSAW:
return 1;
case WEAPON_COLT45 .. WEAPON_DEAGLE:
return 2;
case WEAPON_SHOTGUN .. WEAPON_SHOTGSPA:
return 3;
case WEAPON_UZI, WEAPON_MP5, WEAPON_TEC9:
return 4;
case WEAPON_AK47, WEAPON_M4:
return 5;
case WEAPON_RIFLE, WEAPON_SNIPER:
return 6;
case WEAPON_ROCKETLAUNCHER .. WEAPON_MINIGUN:
return 7;
case WEAPON_GRENADE .. WEAPON_MOLTOV, WEAPON_SATCHEL:
return 8;
case WEAPON_SPRAYCAN .. WEAPON_CAMERA:
return 9;
case WEAPON_DILDO .. WEAPON_FLOWER:
return 10;
case 44, 45, WEAPON_PARACHUTE:
return 11;
case WEAPON_BOMB:
return 12;
}
return -1;
}
LMAO, I use it even on strings/floats/ints now since it does make code look cooler :O
pawn Код:
|
#define setnull(%1) (%1[0]='\0') // similar to slices - Make a string "NULL"
#define inttobool(%1,%2) (%2=!!%1) // INT to BOOL
#define Bit8:%1<%2> %1[(%2) char] // Creates a "CHAR" array, aka a 8bit array
#define Bit8_Get(%1,%2) (%1{%2}) // Gets the "CHAR" array value (pretty self explanatory)
#define SetValue_2D(%1) {_:%1,...} // Sets a value on a "2D" array
new
Text: Hi[5] = SetValue_2D(INVALID_TEXT_DRAW),
Bit8: Yo <5>
;
if(Bit8_Get(Yo, playerid) == 25) return true;
I found a little problem with strdel, it seems like it just sets the pos cell to '\0' and still leaves the rest.
Because I was writing a function and in it, it looped backwards through a string to find the last of something, and even though strdel had supposedly deleted from pos 40 it still found the character after this position. I did a quick print test and deleted part of a string str[20]; str = "a string that's 20."; strdel(str, 10, 20); Then printed the remaining part, that was supposed to be deleted: print(str[11]); "hat's 20." So I made my own version that loops from the start to end and sets them all to 0. Not sure if this is a function bug or I have a crappy version defined somewhere. |
new test[] = {'h', 'e', 'l', 'l', 'o', 0, 'w', 'o', 'r', 'l', 'd'};
print(test);
stock SetVehicleNumberPlateEx(vehicleid, numberplate[])
{
static
iLoopIdx,
iReturnValue,
iDamageStatus[4],
iComponents[14],
Float: fPos[3],
Float: fAngle,
Float: fVelocity[3],
Float: fHealth
;
if ( !GetVehicleModel(vehicleid) )
return 0;
if ( strlen(numberplate) > 32 )
numberplate[32] = 0;
GetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
GetVehicleZAngle(vehicleid, fAngle);
GetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
GetVehicleHealth(vehicleid, fHealth);
GetVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
for ( iLoopIdx = 0; iLoopIdx < 14; iLoopIdx++ )
{
iComponents[iLoopIdx] = GetVehicleComponentInSlot(vehicleid, iLoopIdx);
}
new bPlayerData[MAX_PLAYERS] = { -1, ... };
for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
{
if ( IsPlayerInVehicle(iLoopIdx, vehicleid) )
{
bPlayerData[iLoopIdx] = GetPlayerVehicleSeat(iLoopIdx);
RemovePlayerFromVehicle(iLoopIdx);
SetPlayerPos(iLoopIdx, fPos[0], fPos[1], fPos[2]); // if RPFV would not work
}
}
iReturnValue = SetVehicleNumberPlate(vehicleid, numberplate);
SetVehicleToRespawn(vehicleid);
for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
{
if ( bPlayerData[iLoopIdx] != -1 )
{
PutPlayerInVehicle(iLoopIdx, vehicleid, bPlayerData[iLoopIdx]);
}
}
SetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
SetVehicleZAngle(vehicleid, fAngle);
SetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
SetVehicleHealth(vehicleid, fHealth);
UpdateVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
for ( iLoopIdx = 0; iLoopIdx < 14; iLoopIdx++ )
{
if (iComponents[iLoopIdx] > 999) AddVehicleComponent(vehicleid, iComponents[iLoopIdx]);
}
return iReturnValue;
}
pawn Код:
|
#define Packed:%1<%2[%3]> %1[%2 char]=!(%3)
new
Packed: Hey <25["Hi All"]>,
Packed: Hey1 <25["Hi All"]>
;
How is:
pawn Код:
pawn Код:
|
new DBResult:result = db_query(db, "SELECT * FROM blabla");
db_print_result(result);
db_free_result(result);
/ type | name | tbl_name | rootpage | sql \ |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_users | lw_lvdm_users | 2 | CREATE TABLE lw_lvdm_users( | | | | | | userid integer primary key, | | | | | | name text, | | | | | | password | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_bans | lw_lvdm_bans | 4 | CREATE TABLE lw_lvdm_bans( | | | | | | banid integer primary key, | | | | | | name text, | | | | | | reporter t | |-------|-----------------|-----------------|----------|------------------------------------------------| | index | bans_type | lw_lvdm_bans | 36886 | CREATE INDEX "bans_type" ON lw_lvdm_bans(type) | |-------|-----------------|-----------------|----------|------------------------------------------------| | index | bans_time | lw_lvdm_bans | 40587 | CREATE INDEX "bans_time" ON lw_lvdm_bans(time) | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_lvdm_ip_bans | lw_lvdm_ip_bans | 46040 | CREATE TABLE `lw_lvdm_ip_bans` | | | | | | ( | | | | | | BANID INTEGER PRIMARY KEY, | | | | | | ip TEXT, | | | | | | pla | |-------|-----------------|-----------------|----------|------------------------------------------------| | table | lw_temp_data | lw_temp_data | 20549 | CREATE TABLE "lw_temp_data" ( | | | | | | name text PRIMARY KEY, | | | | | | money INTEGER, | | | | | | bank INTEGER, | | | | | | bount | \-------------------------------------------------------------------------------------------------------/
stock db_print_result(DBResult:dbrResult, iMaxFieldLength = 20) {
const
MAX_ROWS = 100,
MAX_FIELDS = 20,
MAX_FIELD_LENGTH = 88
;
static
s_aaszFields[MAX_ROWS + 1][MAX_FIELDS][MAX_FIELD_LENGTH char],
s_aiFieldMaxLength[MAX_FIELDS],
s_szBuffer[(MAX_FIELD_LENGTH + 4) * MAX_FIELDS]
;
static const
szcSpacePadding[MAX_FIELD_LENGTH] = {' ', ...},
szcDashPadding[MAX_FIELD_LENGTH] = {'-', ...}
;
if (iMaxFieldLength == -1)
iMaxFieldLength = MAX_FIELD_LENGTH;
print(!" ");
print(!"Query result:");
if (!dbrResult)
print(!"\t- No result.");
else if (!db_num_rows(dbrResult))
print(!"\t- No rows.");
else {
new
iRow = 0,
iRows,
iFields = db_num_fields(dbrResult),
iField,
iLength,
bool:bHasMoreLines,
iPos,
iNextPos
;
if (iMaxFieldLength > MAX_FIELD_LENGTH) {
printf("\t- The longest possible field length is %d. Change MAX_FIELD_LENGTH for larger values.", MAX_FIELD_LENGTH);
iMaxFieldLength = MAX_FIELD_LENGTH;
}
if (iFields > MAX_FIELDS) {
printf("\t- There are %d, but only %d of them will be visible.", iFields, MAX_FIELDS);
print(!"\t- Increase MAX_FIELDS if you want to see all fields.");
iFields = MAX_FIELDS;
}
for (iField = 0; iField < iFields; iField++) {
db_field_name(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
s_szBuffer[iPos] = ' ';
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
s_szBuffer[iPos] = ' ';
strins(s_szBuffer, " ", iPos, iMaxFieldLength);
iPos += 4;
}
iPos = 0;
do {
iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;
if (!iNextPos)
iLength = strlen(s_szBuffer[iPos]);
else
iLength = iNextPos - iPos - 1;
s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
} while ((iPos = iNextPos));
strpack(s_aaszFields[0][iField], s_szBuffer, iMaxFieldLength char);
}
do {
for (iField = 0; iField < iFields; iField++) {
db_get_field(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
s_szBuffer[iPos] = ' ';
iPos = 0;
while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
s_szBuffer[iPos] = ' ';
strins(s_szBuffer, " ", iPos, iMaxFieldLength);
iPos += 4;
}
iPos = 0;
do {
iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;
if (!iNextPos)
iLength = strlen(s_szBuffer[iPos]);
else
iLength = iNextPos - iPos - 1;
s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
} while ((iPos = iNextPos));
strpack(s_aaszFields[iRow + 1][iField], s_szBuffer, iMaxFieldLength char);
}
if (++iRow >= MAX_ROWS) {
iRows = iRow;
while (db_next_row(dbrResult))
iRows++;
printf("\t- Only the first %d rows are displayed; there are %d remaining.", MAX_ROWS, iRows);
break;
}
} while (db_next_row(dbrResult));
print(!" ");
for (iRows = iRow, iRow = 0; iRow <= iRows; iRow++) {
do {
bHasMoreLines = false;
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, " | ");
iLength = strlen(s_szBuffer);
if (-1 != (iPos = strfind(s_aaszFields[iRow][iField], "\n"))) {
strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], strlen(s_szBuffer[iLength]) + iPos + 1);
strdel(s_aaszFields[iRow][iField], 0, iPos + 1);
bHasMoreLines = true;
} else {
if (s_aaszFields[iRow][iField]{0}) {
strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], sizeof(s_szBuffer) - iLength);
s_aaszFields[iRow][iField]{0} = 0;
}
}
iLength = strlen(s_szBuffer[iLength]);
strcat(s_szBuffer, szcSpacePadding, strlen(s_szBuffer) + (s_aiFieldMaxLength[iField] - iLength + 1));
}
if (bHasMoreLines)
printf("\t| %s |", s_szBuffer);
} while (bHasMoreLines);
if (iRow == 0) {
printf("\t/ %s \\", s_szBuffer);
} else {
printf("\t| %s |", s_szBuffer);
}
if (iRow == iRows) {
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, "---");
strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
}
printf("\t\\-%s-/", s_szBuffer);
} else {
s_szBuffer[0] = 0;
for (iField = 0; iField < iFields; iField++) {
if (iField)
strcat(s_szBuffer, "-|-");
strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
}
printf("\t|-%s-|", s_szBuffer);
}
}
}
print(!" ");
}
#define TimeStampPassed(%1,%2) ((GetTickCount()-%1)>(%2)) // TimeStampPassed(timestampVar, seconds
public OnPlayerUpdate(playerid)
{
if(TimeStampPassed(p_WeaponUpdate[playerid], 250))
{
p_WeaponUpdate[playerid] = GetTickCount();
SendClientMessage(playerid, -1, "Fart");
}
return 1;
}
stock encrypt(string[],shift)
{
for(new i=0; string[i] != '\0'; ++i)
string[i]+=shift;
}
stock decrypt(string[],shift)
{
for(new i=0; string[i] != '\0'; ++i)
string[i]-=shift;
}
main()
{
new string[] = "This is a string.";
encrypt(string, 13);
printf("Encrypted String: %s", string);
decrypt(string, 13);
printf("Decrypted String: %s", string);
}
Encrypt and Decrypt
Introduction Simple encryption technique based on Caesar's cipher. How does it work? Replaces a character according to the shift parameter. Functions pawn Код:
pawn Код:
pawn Код:
|
#define DecreasePvar(%0,%1,%2) SetPVarInt(%0, %1, GetPVarInt(%0, %1) - %2)
#define IncreasePVar(%0,%1,%2) SetPVarInt(%0, %1, GetPVarInt(%0, %1) + %2)
#define DecreasePVar(%0,%1,%2) IncreasePVar(%0, %1, -(%2))