Nearest Hospital Spawn Upon Death
#1

Hey,

Looked all over using the search function, found a few suggested solutions, however can't seem to find any that work.

Basically what I'm looking for is a piece of script that respawns the player to the nearest Hospital upon death. So far I have the following code, however this doesn't work as I constantly respawn at my initial spawn point in the server.

Any help would be greatly appreciated!

Код:
new Dead[MAX_PLAYERS];
Код:
stock SpawnToHospital(playerid)
{
    Dead[playerid] = 0;
    static const Float:hspawns[8][4] = 
    {
                {2027.77, -1420.52, 15.99, 137.0},
		{1180.85, -1325.57, 12.58, 271.4},
		{1244.437, 331.2261, 18.5547, 7.5465},
		{-2199.719, -2308.075, 29.6181, 322.8928},
		{-2670.285, 616.4364, 13.4531, 183.1042},
		{-316.3832, 1056.045, 18.7344, 1.6017},
		{-1514.823, 2527.119, 54.7443, 2.3546},
		{1578.446, 1770.682, 9.8358, 99.7567}
       
    };
    new Float:spawndist[8];  // Change to the number of hospital spawnpoints you use.
    new Float:distance = 10000.0, closestspawn;
        spawndist[0] = GetPlayerDistanceFromPoint(playerid,2027.77, -1420.52, 15.99); // 1st hospital spawn.
        spawndist[1] = GetPlayerDistanceFromPoint(playerid,1180.85, -1325.57, 12.58); // 2nd hospital spawn.
	spawndist[2] = GetPlayerDistanceFromPoint(playerid,1244.437, 331.2261, 18.5547); // 3rd hospital spawn.
	spawndist[4] = GetPlayerDistanceFromPoint(playerid,-2199.719, -2308.075, 29.6181); // 5th hospital spawn.
	spawndist[3] = GetPlayerDistanceFromPoint(playerid,-2670.285, 616.4364, 13.4531); // 4th hospital spawn.
	spawndist[5] = GetPlayerDistanceFromPoint(playerid,-316.3832, 1056.045, 18.7344); // 6th hospital spawn.
	spawndist[6] = GetPlayerDistanceFromPoint(playerid,-1514.823, 2527.119, 54.7443); // 7th hospital spawn.
	spawndist[7] = GetPlayerDistanceFromPoint(playerid,1578.446, 1770.682, 9.8358); // 8th hospital spawn.

    
    for(new i = 0; i < sizeof(spawndist); i++)
    {
        if(spawndist[i] < distance) distance = spawndist[i], closestspawn = i;
    }
    SetPlayerPos(playerid, hspawns[closestspawn][0], hspawns[closestspawn][1], hspawns[closestspawn][2]);
    SetPlayerFacingAngle(playerid, hspawns[closestspawn][3]);
}
Код:
public OnPlayerConnect(playerid)
{
	Dead[playerid]=0;
        return 1;
}
Код:
public OnPlayerSpawn(playerid)
{
	if(Dead[playerid] == 1) 
	{
		SpawnToHospital(playerid);
	}
	
	SetPlayerInterior(playerid,0);
	TogglePlayerClock(playerid,0);
	if(PlayerInfo[playerid][pInJail] == 1)
	{
	    new string[64];
	   	SetPlayerPos(playerid, 1492.2151,-1768.2281,3285.2859);
	   	format(string, sizeof(string), "* Your character is currently is jail for another %d seconds.", PlayerInfo[playerid][pInJailTime]);
	   	SendClientMessage(playerid, COLOR_ADMIN, string);
	}
    
}
Reply
#2

Try changing the loop to something like this (note that I didn't test this):

pawn Код:
new closestspawn = -1;
for(new i = 0; i < sizeof(spawndist); i++)
{
    if(closestspawn == -1 && spawndist[i] < distance)
    {
        closestspawn = i;
        continue;
    }
    else if(closestspawn != -1 && closestspawn != i)
    {
        if(spawndist[i] < spawndist[closestspawn])
        {
            closestspawn = i;
            continue;
        }
        else continue;
    }
}
Reply
#3

Hey,


Thanks for the reply things have developed a lil further.


So we've made progress, so now when I die it tps me to a hospital, but it isn't the nearest, and for some bizarre reason for a split second it tps me to the train station where the player starts before spawning properly at the wrong hosp

any suggestions?

if it helps this is what the system is based off, which didn't seem to work: https://sampforum.blast.hk/showthread.php?tid=602213
Reply
#4

Maybe changing this little bit will work:

pawn Код:
else if(closestspawn != -1 && closestspawn != i)
{
    if(spawndist[i] > spawndist[closestspawn]) continue;
    else if(spawndist[i] < spawndist[closestspawn])
    {
        closestspawn = i;
        continue;
    }
}
Reply
#5

Quote:
Originally Posted by marley
Посмотреть сообщение
for some bizarre reason for a split second it tps me to the train station where the player starts before spawning properly at the wrong hosp
That is because you are setting player position to hospital on OnPlayerSpawn. So player has spawned already to its initial spawn point before you set it to Hospital, you can try using SetSpawnInfo in OnPlayerdeath.

Quote:
Originally Posted by marley
Посмотреть сообщение
So we've made progress, so now when I die it tps me to a hospital, but it isn't the nearest,
The loop working FINE. The reason why this is happening is that you are getting distance when player spawns so it is tp'ing you to the nearest hospital to initial spawn point. For it to work you need player position when he dies store in global variable then use those position to get distance.
Reply
#6

Just do this, simple.

Somewhere in your code:
PHP код:
   #define MAX_HOSPITALS 8
    
static const Float:hspawns[MAX_HOSPITALS][4] = 
    {
                {
2027.77, -1420.5215.99137.0},
        {
1180.85, -1325.5712.58271.4},
        {
1244.437331.226118.55477.5465},
        {-
2199.719, -2308.07529.6181322.8928},
        {-
2670.285616.436413.4531183.1042},
        {-
316.38321056.04518.73441.6017},
        {-
1514.8232527.11954.74432.3546},
        {
1578.4461770.6829.835899.7567}
       
    }; 
OnPlayerDeath:
PHP код:
new Float:distance 99999.0,
        
Float:tmp_distance
        
closest = -1;
    for(new 
0MAX_HOSPITALSi++)
    {
        
tmp_distance GetPlayerDistanceFromPoint(playeridhspawnsr[i][0], hspawns[i][1], hspawns[i][2]);
        if (
tmp_distance distance)
        {
            
distance tmp_distance;
            
closest i;
        }
    }
    
SetSpawnInfo(playeridNO_TEAMGetPlayerSkin(playerid), hspawns[closest][0],hspawns[closest][1],hspawns[closest][2],hspawns[closest][3], 000000); 
Should Work!
Reply
#7

It works man, thx a lot!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)