SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 07/05/2019, 09:15 AM   #1
GnT
Big Clucker
 
Join Date: Oct 2017
Location: Los Santos
Posts: 63
Reputation: 0
Default Loop

Hello. I'm not sure if these loops are the issue with the server restart, I would be grateful if you could look to see if they are done correctly.
Code:
stock GetClosestVehicle2(playerid, Float:dis)
{
	new Float:X,
		Float:Y,
		Float:Z;
	if(GetPlayerPos(playerid, X, Y, Z))
	{
		new vehicleid = INVALID_VEHICLE_ID;
		for(new v, Float:temp, Float:VX, Float:VY, Float:VZ; v != MAX_VEHICLES; v++)
		{
			if(GetVehiclePos(v, VX, VY, VZ))
			{
				VX -= X, VY -= Y, VZ -= Z;
				temp = VX * VX + VY * VY + VZ * VZ;
				if(temp < dis) dis = temp, vehicleid = v;
			}
		}
		dis = floatpower(dis, 0.5);
		return vehicleid;
	}
	return INVALID_VEHICLE_ID;
}
stock GetClosestVehicle(playerid, exception = INVALID_VEHICLE_ID) { // closestcar
    new
		Float:Distance,
		target = -1;

    for(new v; v < MAX_VEHICLES; v++) if(doesVehicleExist(v)) {
        if(v != exception && (target < 0 || Distance > GetDistancePlayerVeh(playerid, v))) {
            target = v;
            Distance = GetDistancePlayerVeh(playerid, v);
        }
    }
    return target;
}
function RACtime(playerid)
{
	for(new car = 1; car < MAX_VEHICLES; car++)
	{
		new nu;
		if(!IsVehicleOccupied(car))
		{
			foreach(Player, i)
			{
				if(Trailer[i] == car) nu = 1;
				for(new v; v < MAX_PERSONAL_VEHICLES; v++)
				{
					if(PlayerInfo[i][pCarID][v] == car) nu = 1;
				}
			}
		}
		if(nu == 0) SetVehicleToRespawn(car);
	}
	return 1;
}
CMD:va(playerid, params[]) {
	if(gPlayerLogged[playerid] == 0) return SendClientMessage(playerid, COLOR_LIGHTRED, "You need to log in first.");
	if(PlayerInfo[playerid][pAdmin] < 1) return SCM(playerid, COLOR_RED2, AdminOnly);
	new range,Distance2,string[64];
	new Float: Distance, Float: posX, Float: posY, Float: posZ;
	if(sscanf(params, "d", range)) return SCM(playerid, COLOR_GREY, "{CF0000}Usage: {FFFFFF}/va <Range>");
	if(range <= 0 || range >= 301) return SendClientMessage(playerid, COLOR_GREY, "Range must be between 1 and 300.");
	GetPlayerPos(playerid, posX, posY, posZ);
	for(new x = 1; x < MAX_VEHICLES; x ++)
	{
		if(!IsVehicleOccupied(x))
		{
			Distance = GetVehicleDistanceFromPoint(x, posX, posY, posZ);
			Distance2 = floatround(Distance, floatround_round);
			if(Distance2 <= range)
			{
				SetVehicleToRespawn(x);
			}
		}
	}
	format(string, sizeof(string), "You respawned all vehicles near you(%d game units).", range);
	SendClientMessage(playerid, COLOR_GREY, string);
	return 1;
}
CMD:vre(playerid, params[])
{
	if(gPlayerLogged[playerid] == 0) return SendClientMessage(playerid, COLOR_LIGHTRED, "You need to log in first.");
	if(PlayerInfo[playerid][pAdmin] > 0)
	{
		new string[128],sendername[30];
		GetPlayerName(playerid, sendername, sizeof(sendername));
		if(IsPlayerInAnyVehicle(playerid))
		{
			new car = GetPlayerVehicleID(playerid), count;
			for(new x; x < MAX_VEHICLES; x++)
			{
				if(Carspawn[x] == car)
				{
					DestroyVehicle(x);
					Carspawn[x] = 0;
					count++;
					format(string,sizeof(string),"({CC0000}Admin Info{FFFFFF}) Admin %s[%d] respawned vehicle %d.",sendername,playerid,x);
					ABroadCast(COLOR_WHITE,string,1);
					break;
				}
			}
			if(count == 0)
			{
				SetVehicleToRespawn(car);
				format(string,sizeof(string),"({CC0000}Admin Info{FFFFFF}) Admin %s[%d] respawned vehicle %d.",sendername,playerid,car);
				ABroadCast(COLOR_WHITE,string,1);
			}
		}
		else
		{
			new car, count;
			if(sscanf(params, "d",car)) return SCM(playerid, COLOR_GREY, "{CF0000}Usage: {FFFFFF}/vre <Car ID>");
			new bool:unwanted[MAX_VEHICLES];
		    foreach(Player, i)
	     	{
	             if(IsPlayerInAnyVehicle(i))
				 {
				 	unwanted[GetPlayerVehicleID(i)]=true;
				 }
	     	}
	     	if(!unwanted[car])
			{
				for(new x; x < MAX_VEHICLES; x++)
				{
					if(Carspawn[x] == car)
					{
						if(!unwanted[car])
						{
							DestroyVehicle(x);
							Carspawn[x] = 0;
							count++;
							format(string,sizeof(string),"({CC0000}Admin Info{FFFFFF}) Admin %s[%d] respawned vehicle %d.",sendername,playerid,x);
							ABroadCast(COLOR_WHITE,string,1);
							break;
						}
					}
				}
				if(count == 0)
				{
					if(!unwanted[car])
					{
						SetVehicleToRespawn(car);
						format(string,sizeof(string),"({CC0000}Admin Info{FFFFFF}) Admin %s[%d] respawned vehicle %d.",sendername,playerid,car);
						ABroadCast(COLOR_WHITE,string,1);
					}
				}
			}
		}
	}
	else return SendClientMessage(playerid, COLOR_LIGHTGREEN3, AdminOnly);
	return 1;
}
CMD:removeallspawncar(playerid, params[]) {
	if(gPlayerLogged[playerid] == 0) return SendClientMessage(playerid, COLOR_LIGHTRED, "You need to login first.");
	if(PlayerInfo[playerid][pAdmin] >= 4) {
		new string[128];
		for(new i = 1; i < MAX_VEHICLES; i++)
		{
			if(Carspawn[i] != 0)
			{
				DestroyVehicle(Carspawn[i]);
			    Carspawn[i] = 0;
			}
		}
		format(string,sizeof(string),"({CC0000}Admin Info{FFFFFF}) %s[%d] has despawned all vehicles.",GetName(playerid),playerid);
		ABroadCast(COLOR_WHITE,string,1);
	}
	else return SendClientMessage(playerid, COLOR_RED2, AdminOnly);
	return 1;
}
CMD:checkspawncar(playerid, params[]) {
	if(gPlayerLogged[playerid] == 0) return SendClientMessage(playerid, COLOR_LIGHTRED, "You need to login first.");
	if(PlayerInfo[playerid][pAdmin] >= 4) {
		new string[64];
		for(new i = 1; i < MAX_VEHICLES; i++)
		{
			if(Carspawn[i] != 0)
			{
				SendClientMessage(playerid, COLOR_TEAL, "--------------------------------------------------------------------------------------------------------------------------------");
			    format(string,sizeof(string),"Car ID: %d",Carspawn[i]);
				SCM(playerid,COLOR_WHITE,string);
				SendClientMessage(playerid, COLOR_TEAL, "--------------------------------------------------------------------------------------------------------------------------------");
			}
		}
	}
	else return SendClientMessage(playerid, COLOR_RED2, AdminOnly);
	return 1;
}
public OnVehicleStreamIn(vehicleid, forplayerid)
{
	for(new v; v < MAX_PERSONAL_VEHICLES; v++)
	{
		if(PlayerInfo[forplayerid][pCarID][v] == vehicleid)
		{
			SetVehicleParamsForPlayer(vehicleid,forplayerid,0,0);
		}
	}
	for(new vv; vv < MAX_VEHICLES; vv++)
	{
		if(vv == HireCar[forplayerid])
		{
			SetVehicleParamsForPlayer(vv,forplayerid,0,0);
		}
	}
	return 1;
}
Thank you.
GnT is offline   Reply With Quote
Old 07/05/2019, 12:29 PM   #2
raydx
Huge Clucker
 
raydx's Avatar
 
Join Date: Feb 2017
Posts: 205
Reputation: 73
Default Re: Loop

Always check if vehicle is valid before using vehicleid in arrays. Most of these loops can crash your server.
raydx is online now   Reply With Quote
Old 07/05/2019, 12:35 PM   #3
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,112
Reputation: 67
Default Re: Loop

Yeah there is a multiple possibilities for a crash from that loops, consider to re-write these loops, especially these ones: GetClosestVehicle2, OnVehicleStreamIn.

p.s. you don't need loop in OnVehicleStreamIn.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 07/05/2019, 02:02 PM   #4
jasperschellekens
Gangsta
 
jasperschellekens's Avatar
 
Join Date: Dec 2016
Location: Gehenna
Posts: 723
Reputation: 48
Default Re: Loop

Might want to use this include instead of your getcloestvehicle functions.
https://forum.sa-mp.com/showthread.php?t=321575

I was having trouble with those too and now I just use the include above. Works absolutely perfect.
Code example:
PHP Code:
// GetNearest(playerid, type, distance);
new nearvehicleid GetNearest(playeridVEHICLE5.0);
new 
nearobjectid GetNearest(playeridOBJECT5.0); 
new 
nearobjectid GetNearest(playeridDYNAMIC_OBJECT5.0); 
new 
nearplayerid GetNearest(playeridPLAYER5.0); 

    if(
nearobjectid)
    {
        
format(strsizeof(str), "Object: %d"nearobjectid);
        
SendClientMessage(playerid, -1str);
    } 
jasperschellekens is offline   Reply With Quote
Old 07/05/2019, 06:18 PM   #5
GnT
Big Clucker
 
Join Date: Oct 2017
Location: Los Santos
Posts: 63
Reputation: 0
Default Re: Loop

How is the correct form for loop through all vehicles? It's corect this form: for(new i = 1; i < MAX_VEHICLES; i ++)?
GnT is offline   Reply With Quote
Old 07/05/2019, 06:26 PM   #6
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,112
Reputation: 67
Default Re: Loop

Quote:
Originally Posted by GnT View Post
How is the correct form for loop through all vehicles? It's corect this form: for(new i = 1; i < MAX_VEHICLES; i ++)?
yes, that's correct.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A loop (for) inside a loop (for) E_Meec Scripting Help 3 20/08/2014 07:50 AM
[Ajuda] Loop parando a funcao (meio que nao continua depois do loop) Axll PortuguÍs/Portuguese 16 15/02/2014 06:34 PM
loop inside of loop? newbienoob Scripting Help 3 17/12/2013 05:06 AM
Help with loop inside loop Salim_Karaja Scripting Help 4 24/02/2013 08:02 PM
for( ) loop vs while( ) loop | While( ) loop is 143x faster? zgintasz Scripting Help 17 03/07/2012 01:57 PM


All times are GMT. The time now is 11:03 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.