SA-MP Forums Archive
how to respawn all cars - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: how to respawn all cars (/showthread.php?tid=418446)



how to respawn all cars - RiChArD_A - 24.02.2013

Hi, I want to know what can I do so that all vehicles in my server can respawn to the position they were when the server started after 5 minutes but not if they are occupied. how can I do it, please help me.


Re: how to respawn all cars - PabloDiCostanzo - 24.02.2013

You mind like a /arespawncars?

I have the CMD in my GM.

I'll give you the CMD, not now because I'm on cellphone.


Respuesta: Re: how to respawn all cars - RiChArD_A - 24.02.2013

Quote:
Originally Posted by PabloDiCostanzo
Посмотреть сообщение
You mind like a /arespawncars?

I have the CMD in my GM.

I'll give you the CMD, not now because I'm on cellphone.
Mmm yeah that will help too.

PD: I'm on my a cellphone too


Re: how to respawn all cars - Jakku - 24.02.2013

Quickly written, should do the job

pawn Код:
stock RespawnAllVehicles()
{
    for (new i=0;i<MAX_VEHICLES;i++)
    {
        if (CanVehicleBeRespawned(i)) // If this returns 1 (no factors preventing the vehicle from being respawned as stated in "CanVehicleBeRespawned"), the vehicle will respawn
        {
            SetVehicleToRespawn(i);
        }
    }
}

stock CanVehicleBeRespawned(vehicleid)
{
    new Float:X,Float:Y,Float:Z;
    GetVehiclePos(vehicleid, X,Y,Z);
    if (X == 0.0 && Y == 0.0 && Z == 0.0)
    {
        //Vehicle not created, returning 0
        return 0;
    }
   
    for (new i=0;i<MAX_PLAYERS;i++)
    {
        if (IsPlayerConnected(i))
        {
            if (GetPlayerVehicleID(i) == vehicleid) return 0; // Someone is inside the vehicle, returning 0
        }
    }
   
    return 1; // Vehicle is ready to be respawned, returning 1 :)
}
Now just use RespawnAllVehicles to respawn them to their original positions

EDIT: If you need a timer for it, here:

pawn Код:
forward RespawnVehicles();

public OnGameModeInit()
{
    SetTimer("RespawnVehicles", 300000, true);
}


public RespawnVehicles()
{
    RespawnAllVehicles();
    SendClientMessageToAll(-1, "All unoccupied vehicles successfully respawned");
}



Re: how to respawn all cars - tyler12 - 24.02.2013

pawn Код:
stock IsVehicleOccupied(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(GetPlayerVehicleID(i) == vehicleid) return 1;
    }
    return 0;
}
pawn Код:
forward RespawnVehicles();
Under OnGameModeInit()
pawn Код:
SetTimer("RespawnVehicles",300000,1);
pawn Код:
public RespawnVehicles()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(!IsVehicleOccupied(i))
        {
            SetVehicleToRespawn(i);
        }
    }
    SendClientMessageToAll(-1,"Vehicles respawned.");
    return 1;
}



Re: how to respawn all cars - Jakku - 24.02.2013

Your code will perform longer since you don't check the vehicle's existence and if the player is connected or not.


Respuesta: Re: how to respawn all cars - RiChArD_A - 24.02.2013

Quote:
Originally Posted by tyler12
Посмотреть сообщение
pawn Код:
stock IsVehicleOccupied(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(GetPlayerVehicleID(i) == vehicleid) return 1;
    }
    return 0;
}
pawn Код:
forward RespawnVehicles();
Under OnGameModeInit()
pawn Код:
SetTimer("RespawnVehicles",300000,1);
pawn Код:
public RespawnVehicles()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(!IsVehicleOccupied(i))
        {
            SetVehicleToRespawn(i);
        }
    }
    SendClientMessageToAll(-1,"Vehicles respawned.");
    return 1;
}
where do I add the 2 first codes and last one


Re: how to respawn all cars - Salim_Karaja - 24.02.2013

Quote:
Originally Posted by Jakku
Посмотреть сообщение
Quickly written, should do the job

pawn Код:
stock RespawnAllVehicles()
{
    for (new i=0;i<MAX_VEHICLES;i++)
    {
        if (CanVehicleBeRespawned(i)) // If this returns 1 (no factors preventing the vehicle from being respawned as stated in "CanVehicleBeRespawned"), the vehicle will respawn
        {
            SetVehicleToRespawn(i);
        }
    }
}

stock CanVehicleBeRespawned(vehicleid)
{
    new Float:X,Float:Y,Float:Z;
    GetVehiclePos(vehicleid, X,Y,Z);
    if (X == 0.0 && Y == 0.0 && Z == 0.0)
    {
        //Vehicle not created, returning 0
        return 0;
    }
   
    for (new i=0;i<MAX_PLAYERS;i++)
    {
        if (IsPlayerConnected(i))
        {
            if (GetPlayerVehicleID(i) == vehicleid) return 0; // Someone is inside the vehicle, returning 0
        }
    }
   
    return 1; // Vehicle is ready to be respawned, returning 1 :)
}
Now just use RespawnAllVehicles to respawn them to their original positions
its can be made much shorter:
pawn Код:
stock RespawnAllVehicles()
{
    for(new v = 1; v <= MAX_VEHICLES; v++)
        {
            for(new i = 0; i <= MAX_PLAYERS; i++;)  if(IsPlayerInVehicle(i, v)) continue;
            SetVehicleToRespawn(v);
        }
}



Respuesta: Re: how to respawn all cars - RiChArD_A - 24.02.2013

Quote:
Originally Posted by Jakku
Посмотреть сообщение
Your code will perform longer since you don't check the vehicle's existence and if the player is connected or not.
It doesn't matter if it takes 2-3 seconds to do the job, what really matters is that it does it.


Re: Respuesta: Re: how to respawn all cars - Jakku - 24.02.2013

Quote:
Originally Posted by Lauder
Посмотреть сообщение
It doesn't matter if it takes 2-3 seconds to do the job, what really matters is that it does it.
Are you claiming 2 or 3 seconds of wasted time doesn't matter?

EDIT: 30ms without checking the existence and 6ms with it which means it's 5x faster.