Respawn at nearest Hospital script not working correctly
#1

Quote:

The script works, but when I die right next to one hospital, it will still respawn me at the other hospital. Changing distance doesn't really do anything except anything over 2000 and it will go to hospital 1, or under 2000 and it will go to hospital 2, or something like that.

pawn Код:
stock SpawnToHospital(playerid)
{
    dead[playerid] = 0;
    static const Float:hspawns[][4] =
    {
        {2034.1670,-1406.0623,17.2181,150.4297},
        {1177.3391,-1324.2300,14.0658,268.3749}
    };
    new Float:spawndist[2];  // Change to the number of hospital spawnpoints you use.
    new Float:distance = 1000.0, closestspawn;
    spawndist[0] = GetPlayerDistanceFromPoint(playerid, 2034.1670,-1406.0623,17.2181);
    spawndist[1] = GetPlayerDistanceFromPoint(playerid, 1177.3391,-1324.2300,14.0658);
    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]);
    SetCameraBehindPlayer(playerid);
}
My new issue is that I only spawn at one of the 2 hospitals after a script revision:
pawn Код:
stock SpawnToHospital(playerid)
{
    dead[playerid] = 0;
    static const Float:hspawns[][4] =
    {
        {2034.1670,-1406.0623,17.2181,150.4297},
        {1177.3391,-1324.2300,14.0658,268.3749}
    };
    new Float:spawndist[2];  // Change to the number of hospital spawnpoints you use.
    new closestspawn;
    spawndist[0] = GetPlayerDistanceFromPoint(playerid, 2034.1670,-1406.0623,17.2181);
    spawndist[1] = GetPlayerDistanceFromPoint(playerid, 1177.3391,-1324.2300,14.0658);
    if(spawndist[0] < spawndist[1])
    {
        closestspawn = 0;
    }
    else if (spawndist[0] > spawndist[1])
    {
        closestspawn = 1;
    }
    SetPlayerPos(playerid, hspawns[closestspawn][0], hspawns[closestspawn][1], hspawns[closestspawn][2]);
    SetPlayerFacingAngle(playerid, hspawns[closestspawn][3]);
    SetCameraBehindPlayer(playerid);
    return 1;
}


After yet another script revision, I still come up with the same results -- sending me to hospital 1, or the second hospital. I'm thinking of just searching for a new script bit since I can't seem to get this one to work. I checked my coords and they are not the issue:

pawn Код:
stock SpawnToHospital(playerid)
{
    dead[playerid] = 0;
    static const Float:hspawns[][4] =
    {
        {2034.1670,-1406.0623,17.2181,150.4297},
        {1177.3391,-1324.2300,14.0658,268.3749}
    };
    new Float:spawndist[2];  // Change to the number of hospital spawnpoints you use.
    new closestspawn;
    spawndist[0] = GetPlayerDistanceFromPoint(playerid, 2034.1670,-1406.0623,17.2181);
    spawndist[1] = GetPlayerDistanceFromPoint(playerid, 1177.3391,-1324.2300,14.0658);
    if (spawndist[0] <= spawndist[1])
    {
        spawndist[0] = closestspawn;
        closestspawn = 0;
        print("hos 0 is closer");
    }
    if (spawndist[1] <= spawndist[0])
    {
        spawndist[1] = closestspawn;
        closestspawn = 1;
        print("hos 1 is closer");
    }
    SetPlayerPos(playerid, hspawns[closestspawn][0], hspawns[closestspawn][1], hspawns[closestspawn][2]);
    SetPlayerFacingAngle(playerid, hspawns[closestspawn][3]);
    SetCameraBehindPlayer(playerid);
    return 1;
}
Reply
#2

Create 2 more checks that will check if he is in range at 100 or something for each hospital, if is not then will check with the code that you have above. This is will be a possible fix I think, not that great, but something untill you find the main problem.
Reply
#3

Instead of a check against 1000 units of distance, why don't you search for the closest hospital within THEMSELVES? Use sorting techniques or make your own, it's not difficult!

1) Get distance of each hospital from player
2) In a loop, get the smallest distance at index 0 by sorting (or simply temp vars)
3) Spawn at gArray[ 0 ][ 0 ]....
Reply
#4

Quote:
Originally Posted by ]Rafaellos[
Посмотреть сообщение
Create 2 more checks that will check if he is in range at 100 or something for each hospital, if is not then will check with the code that you have above. This is will be a possible fix I think, not that great, but something untill you find the main problem.
Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
Instead of a check against 1000 units of distance, why don't you search for the closest hospital within THEMSELVES? Use sorting techniques or make your own, it's not difficult!

1) Get distance of each hospital from player
2) In a loop, get the smallest distance at index 0 by sorting (or simply temp vars)
3) Spawn at gArray[ 0 ][ 0 ]....
I basically used Rajat's idea by dumbing the code down to something I understood. It's still only spawning me at 1 of the 2 hospitals. Updating the topic.
Reply
#5

Still need help, bump.
Reply
#6

The code seems fine only, it would only break if you are over 1000 units away

You could at least try to print both distances and check for yourself why your code did what it did

That would be a version which uses an additional function
Basically put everything into an function if you want to reuse it more than once
pawn Код:
stock SpawnToHospital(playerid) {
    static const Float: hspawns[][] = {
        {2034.1670,-1406.0623,17.2181,150.4297},
        {1177.3391,-1324.2300,14.0658,268.3749}
    };
    new
        closest = GetClosestFromPlayer(playerid, hspawns)
    ;
    if(closest != -1) {
        dead[playerid] = false;
        SetPlayerPos(playerid, hspawns[closest][0], hspawns[closest][1], hspawns[closest][2]);
        SetPlayerFacingAngle(playerid, hspawns[closest][3]);
        return SetCameraBehindPlayer(playerid);
    }
    return false;
}
pawn Код:
GetClosestFromPlayer(playerid, const {_, Float}: data[][], Float: distance = 0.0, size = sizeof data) {
    if((distance = GetPlayerDistanceFromPoint(playerid, data[0][0], data[0][1], data[0][2]))) {
        new
            closest,
            Float: tmp
        ;
        while(--size) {
            tmp = GetPlayerDistanceFromPoint(playerid, data[size][0], data[size][1], data[size][2]);

            if(tmp < distance) {
                distance = tmp;
                closest = size;
            }
        }
        return closest;
    }
    return -1;
}
Reply
#7

Ugh, why do you write your script in that style lol it makes my scripting OCD kick in. It also seems like an overuse of stocks if I only need 2 or 3 hospital spawns. Thanks for your help though.
Reply
#8

Updated Issue again.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)