stock IsVehicleInRangeOfPoint(vehicleid, Float:range, Float:x, Float:y, Float:z)
{
new Float:x1,
Float:y1,
Float:z1;
CreateCircle(0, x, y, z, range*0.01, x1, y1, z1);
GetVehiclePos(vehicleid, x, y, z);
new Float:dist = GetDistanceBetween2Points(x, y, z, x1, y1, z1);
if(dist < range)
return true;
else
return false;
}
stock GetNearestVehicleByCoord(Float:x, Float:y, Float:z, Float:precision = 2.0)
{
new vehicle = INVALID_VEHICLE_ID;
for(new i=1; i<MAX_VEHICLES; i++)
{
new Float:vehDist = GetVehicleDistanceFromPoint(i, x, y, z);
if(vehDist == 0.0)
{
if(i > GetVehiclePoolSize()) return vehicle;
else continue;
}
if(vehDist > precision) continue;
else precision = vehDist, vehicle = i;
}
return vehicle;
}
stock Float:GetDistanceBetween2Points(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
return VectorSize(x1-x2, y1-y2, z1-z2);
}
CMD:exemple(playerid)
{
new Float:x,
Float:y,
Float:z;
GetVehiclePos(GetPlayerVehicleID(playerid), x, y, z);
if(IsVehicleInRangeOfPoint(1, 10, x, y, z))
return SCM(playerid, GREEN, "In range!");
else
return SCM(playerid, RED, "Out of range");
}
#define IsVehicleInRangeOfPoint(%0,%1, %1 > GetVehicleDistanceFromPoint(%0,
static const NATOPhonetic[][9 char] = { // https://en.wikipedia.org/wiki/NATO_phonetic_alphabet#Letters !"Alfa", !"Bravo", !"Charlie", !"Delta", !"Echo", !"Foxtrot", !"Golf", !"Hotel", !"India", !"Juliett", !"Kilo", !"Lima", !"Mike", !"November", !"Oscar", !"Papa", !"Quebec", !"Romeo", !"Sierra", !"Tango", !"Uniform", !"Victor", !"Whiskey", !"X-ray", !"Yankee", !"Zulu" }; ReturnPhonetic(letter) { new idx = toupper(letter) - 65, string[9]; if(!(0 <= idx <= 25)) { format(string, sizeof(string), "Invalid"); return string; } strunpack(string, NATOPhonetic[idx], sizeof(string)); return string; }
new string[32]; format(string, sizeof(string), "We lost objective %s!", ReturnPhonetic('c')); SendClientMessageToAll(-1, string); // output: We lost objective Charlie!
You should use natives because they are faster
IsVehicleInRangeOfPoint could be done with GetVehicleDistanceFromPoint PHP Code:
For GetDistanceBetween2Points use the native VectorSize |
Float:EuclideanDivision(dividend, divisor, &rest, &Float:quotient, precision = 5)
{
new temp;
quotient = dividend / divisor;
rest = dividend % divisor;
while(rest != 0)
{
if(temp++ > precision)
break;
dividend *= 10;
quotient = quotient + (dividend / divisor) * 0.10;
rest = dividend % divisor;
}
return quotient;
}
split(const source[], destination[][], delim, destsize, destlen) { for(new i, li, aNum, len, s = strlen(source); i <= s && aNum < destsize; i++) { if(source[i] == delim || i == s) { len = strmid(destination[aNum], source, li, i, destlen); destination[aNum][len] = EOS; li = i + 1; aNum++; } } return 1; }
new pos[3][9]; new strtmp[] = "1958.3783, 1343.1572, 15.3746 ||| Other s,t,u,f,f"; split(strtmp, pos, ',', sizeof pos, sizeof pos[]); SetPlayerPos(playerid, floatstr(pos[0]), floatstr(pos[1]), floatstr(pos[2]));
strfindchar(const string[], sub) { for(new i; string[i] != '\0'; i++) { if(string[i] == sub) return i; } return -1; }
new strtmp[] = "Question?"; if(strfindchar(strtmp, '?') != -1) //Pay attention to '' (not "") { print("Yes"); } else print("No");
bool: IsAParticle(objectid) { if(objectid >= 18668 && objectid <= 18748) return true; return false; }
Code:
bool: IsAParticle(objectid) { if(objectid >= 18668 && objectid <= 18748) return true; return false; } |
bool:IsAParticle(objectid) return objectid >= 18668 && objectid <= 18748 ? true : false;
bool:IsAParticle(modelid)
return (18668 <= modelid <= 18748);
static const wepDataArray[] =
{
331, 333, 334, 335, 336, 337, 338, 339, 341, 321,
322, 323, 324, 325, 326, 342, 343, 344, 346, 347,
348, 349, 350, 351, 352, 353, 355, 356, 372, 357,
358, 359, 360, 361, 362, 363, 364, 365, 366, 367,
368, 369, 371
};
This probably isn't any better but it is more compact imo.
Код:
WeaponToModel ( wepid ) { new wepDataArray [ ] [ ] = { { 331 } , { 333 } , { 334 } , { 335 } , { 336 } , { 337 } , { 338 } , { 339 } , { 341 } , { 321 } , { 322 } , { 323 } , { 324 } , { 325 } , { 326 } , { 342 } , { 343 } , { 344 } , { 346 } , { 347 } , { 348 } , { 349 } , { 350 } , { 351 } , { 352 } , { 353 } , { 355 } , { 356 } , { 372 } , { 357 } , { 358 } , { 359 } , { 360 } , { 361 } , { 362 } , { 363 } , { 364 } , { 365 } , { 366 } , { 367 } , { 368 } , { 369 } , { 371 } } ; if ( wepid > sizeof ( wepDataArray ) || wepid < sizeof ( wepDataArray ) ) return INVALID_OBJECT_ID ; return wepDataArray [ wepid ] [ 0 ] ; } |
if ( wepid > sizeof ( wepDataArray ) || wepid < sizeof ( wepDataArray ) )
new
g_WeaponModels[] = {
-1,
331,
333,
334,
335,
336,
337,
338,
339,
341,
321,
322,
323,
324,
325,
326,
342,
343,
344,
-1,
-1,
-1,
346,
347,
348,
349,
350,
351,
352,
353,
355,
356,
372,
357,
358,
359,
360,
361,
362,
363,
364,
365,
366,
367,
368,
369,
371
};
stock GetWeaponModelByID(weaponid) {
if(weaponid < 0 || weaponid > sizeof(g_WeaponModels))
return -1; //run time check
return g_WeaponModels[weaponid];
}
stock GetWeaponIDByModel(weapon_model) {
for(new i = 1; i< sizeof(g_WeaponModels); i++) {
if(g_WeaponModels[i] == weapon_model)
return i;
}
return -1;
}
stock bool:isValidDate(dStr[], delim = '/', dFormat[] = "ymd", size = sizeof(dStr)) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_Year,
temp_Month,
temp_Day,
temp_Index = 0,
temp_Pos = 0,
temp_ePos = 0,
temp_shortStr[5]
;
do {
while(dStr[temp_ePos] != delim) {
if(++temp_ePos >= size) {
temp_ePos = size - 1;
break;
}
}
strmid(temp_shortStr, dStr, temp_Pos, temp_ePos, sizeof(temp_shortStr));
temp_Pos = ++temp_ePos;
switch(dFormat[temp_Index++]) {
case 'm', 'M':
temp_Month = strval(temp_shortStr);
case 'd', 'D':
temp_Day = strval(temp_shortStr);
case 'y', 'Y':
temp_Year = strval(temp_shortStr);
default:
return false;
}
}
while(temp_Index < 3);
if((temp_Year % 400) == 0 || ((temp_Year % 100) != 0 && (temp_Year % 4) == 0))
monthDays[1] = 29;
if(temp_Month < 1 || temp_Month > 12)
return false;
if(temp_Day < 1 || temp_Day > monthDays[temp_Month - 1])
return false;
return true;
}
public OnFilterScriptInit() {
new date[15] = "06/29/2016"; //MM/DD/YYYY "mdy"
printf("IsValid : %d", isValidDate(date, '/', "mdy"));
date = "29/02/2016"; //DD/MM/YYYY "dmy"
printf("IsValid : %d", isValidDate(date, '/', "dmy"));
date = "2016/02/29"; //YYYY/MM/DD "ymd"
printf("IsValid : %d", isValidDate(date, '/', "ymd"));
//invalid tests.
//February doesn't have 29 days in 2017. So the further results should be 0.
date = "02/29/2017"; //MM/DD/YYYY "mdy"
printf("IsValid : %d", isValidDate(date, '/', "mdy"));
date = "29/02/2017"; //DD/MM/YYYY "dmy"
printf("IsValid : %d", isValidDate(date, '/', "dmy"));
date = "2017/02/29"; //YYYY/MM/DD "ymd"
printf("IsValid : %d", isValidDate(date, '/', "ymd"));
date = "01/32/2016"; //MM/DD/YYYY "mdy" //January doesn't have 32 days.
printf("IsValid : %d", isValidDate(date, '/', "mdy"));
return 1;
}
stock bool:isValidDate(dStr[], dFormat[] = "ymd", size = sizeof(dStr)) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_Year,
temp_Month,
temp_Day,
temp_Index = 0,
temp_Pos = 0,
temp_ePos = 0,
temp_shortStr[5]
;
do {
while(dStr[temp_ePos] >= 48 && dStr[temp_ePos] <= 57) {
if(++temp_ePos >= size) {
temp_ePos = size - 1;
break;
}
}
strmid(temp_shortStr, dStr, temp_Pos, temp_ePos, sizeof(temp_shortStr));
temp_Pos = ++temp_ePos;
switch(dFormat[temp_Index++]) {
case 'm', 'M':
temp_Month = strval(temp_shortStr);
case 'd', 'D':
temp_Day = strval(temp_shortStr);
case 'y', 'Y':
temp_Year = strval(temp_shortStr);
default:
return false;
}
}
while(temp_Index < 3);
if((temp_Year % 400) == 0 || ((temp_Year % 100) != 0 && (temp_Year % 4) == 0))
monthDays[1] = 29;
if(temp_Month < 1 || temp_Month > 12)
return false;
if(temp_Day < 1 || temp_Day > monthDays[temp_Month - 1])
return false;
return true;
}
//I'm testing on different date separators.
new date[15] = "06.29.2016"; //MM/DD/YYYY "mdy"
printf("IsValid : %d", isValidDate(date, "mdy"));
date = "29/02/2016"; //DD/MM/YYYY "dmy"
printf("IsValid : %d", isValidDate(date, "dmy"));
date = "2016-02-29"; //YYYY/MM/DD "ymd"
printf("IsValid : %d", isValidDate(date, "ymd"));
//invalid tests.
//February doesn't have 29 days in 2017. So the further results should be 0.
date = "02>29>2017"; //MM/DD/YYYY "mdy"
printf("IsValid : %d", isValidDate(date, "mdy"));
date = "29!02!2017"; //DD/MM/YYYY "dmy"
printf("IsValid : %d", isValidDate(date, "dmy"));
date = "2017\02\29"; //YYYY/MM/DD "ymd"
printf("IsValid : %d", isValidDate(date, "ymd"));
date = "01~32~2016"; //MM/DD/YYYY "mdy" //January doesn't have 32 days.
printf("IsValid : %d", isValidDate(date, "mdy"));
IsValid : 1 IsValid : 1 IsValid : 1 IsValid : 0 IsValid : 0 IsValid : 0 IsValid : 0
bool:isValidDate(str[])//by Sreyas
{
new count,i,j;
new daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new parts[3][5],Index_Beg;
new bool:legit;
while(str[i]!='\0')
{
if(str[i] < 48 || str[i] > 57)
{
count++;
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
Index_Beg = i+1;
j++;
}
i++;
if(str[i]=='\0')
{
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
}
}
if(count != 2) return false;
new number[3];
for(i=0;i<3;i++)
{
number[i] = strval(parts[i]);
if(number[i] == 0)return false;
}
new y,m,d;
y = m = d = -1;
for(i=0;i<3;i++)
{
if(number[i]>31)
{
if(y != -1) return false;
y = i;
}
}
for(i=0;i<3;i++)
{
if(i == y) continue;
if(number[i] < 13 && number[i] > 0)
{
if(m == -1)
{
m = i;
}
else
{
d = i;
}
}
}
if(y==-1)
{
for(i=0;i<3;i++)
{
if(i==d||i==m) continue;
y = i;
}
}
if(d==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==m) continue;
d = i;
}
}
if(m==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==d) continue;
m = i;
}
}
if(number[y] % 400 == 0 || (number[y] % 100 != 0 && number[y] % 4 == 0))
daysinmonth[1]=29;
if( number[m]<13 && number[d] <= daysinmonth[number[m]-1])
legit=true;
if (!legit) return false;
return true;
}
isValidDate
Okay this function is created on the behalf of this discussion.It validates a date which is passed as string to the function.It doesn't need any delimiter or format to specify actually.The function will identify date,month and year automatically. Parameters: str - date in string PHP код:
|
stock bool:IsValidDate(dStr[], size = sizeof(dStr)) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_YMD[3],
temp_Pos = 0,
temp_ePos = 0,
temp_Index = 0,
temp_shortStr[5]
;
do {
while(dStr[temp_ePos] >= 48 && dStr[temp_ePos] <= 57) {
if(++temp_ePos >= size) {
temp_ePos = size - 1;
break;
}
}
strmid(temp_shortStr, dStr, temp_Pos, temp_ePos, sizeof(temp_shortStr));
temp_YMD[temp_Index++] = strval(temp_shortStr);
temp_Pos = ++temp_ePos;
}
while(dStr[temp_ePos] != '\0' && temp_Index < 3);
temp_ePos = temp_Pos = -1;
for(temp_Index = 0; temp_Index < 3; temp_Index++) {
if(temp_YMD[temp_Index] > 1750) {
if((temp_YMD[temp_Index] % 400) == 0 || ((temp_YMD[temp_Index] % 100) != 0 && (temp_YMD[temp_Index] % 4) == 0))
monthDays[1] = 29;
}
else if(temp_YMD[temp_Index] > 12)
temp_Pos = temp_Index;
else
if(temp_ePos == -1)
temp_ePos = temp_Index;
else
temp_Pos = temp_Index;
}
if(temp_ePos == -1 || temp_Pos == -1)
return false;
if(temp_YMD[temp_ePos] < 1 || temp_YMD[temp_ePos] > 12)
return false;
if(temp_YMD[temp_Pos] < 1 || temp_YMD[temp_Pos] > monthDays[temp_YMD[temp_ePos] - 1])
return false;
return true;
}
stock bool:IsValidDate(dStr[]) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_YMD[3],
temp_Pos = 0,
temp_ePos = 0,
temp_Index = 0
;
//Thought about implementing SSCANF_QUIET, but it requires spaces to work "currently".
for(temp_Index = strlen(dStr) - 1; temp_Index != 0; temp_Index--) {
if(!(dStr[temp_Index] >= 48 && dStr[temp_Index] <= 57))
dStr[temp_Index] = ' ';
}
if(sscanf(dStr, "iii", temp_YMD[0], temp_YMD[1], temp_YMD[2]))
return false;
temp_ePos = temp_Pos = -1;
for(temp_Index = 0; temp_Index < 3; temp_Index++) {
if(temp_YMD[temp_Index] > 1750) {
if((temp_YMD[temp_Index] % 400) == 0 || ((temp_YMD[temp_Index] % 100) != 0 && (temp_YMD[temp_Index] % 4) == 0))
monthDays[1] = 29;
}
else if(temp_YMD[temp_Index] > 12)
temp_Pos = temp_Index;
else
if(temp_ePos == -1)
temp_ePos = temp_Index;
else
temp_Pos = temp_Index;
}
if(temp_ePos == -1 || temp_Pos == -1)
return false;
if(temp_YMD[temp_ePos] < 1 || temp_YMD[temp_ePos] > 12)
return false;
if(temp_YMD[temp_Pos] < 1 || temp_YMD[temp_Pos] > monthDays[temp_YMD[temp_ePos] - 1])
return false;
return true;
}
Good idea, but you should check your function failing at some invalid dates which causes array index out of bounds.
|