Send Player to Nearest Hospital

Started by Mötley, Jan 31, 2018, 03:50 AM

Previous topic - Next topic

Mötley

There's no real Island return in the wiki, So I had to work with what we already have.

I don't know who the originator of:
function GetDistance(pos1, pos2) {
    return sqrt(pow(pos1.x - pos2.x, 2) + pow(pos1.y - pos2.y, 2) + pow(pos1.z - pos2.z, 2));
}
But if someone could tell me I would like to leave credits to that individual ;)


The script is a real basic Island/nearest hospital return. It took a heck of a lot of testing One entire day to get it right, if you encounter any bugs please tell me as it's a huge map. I literally tested coordinates all over the map but I'm sure I missed something somewhere, somehow.

I haven't set the players angle on spawn

I don't really ever see anyone needing this as all I have seen is team death matches in vcmp, so something like this would be useless to these individual :c lol. 

I hope you enjoy it, as well if you happened to stumble upon it and really needed it I'm glad I was able to help you ;)
/*
    Server: The American Server
    Coding by: Motley
*/

// One equals first biggest Island/ Two equals all center Islands/ Three equals other big Island
Player_Island <- array( GetMaxPlayers(), 0 );
// North and South/one/two
Player_Locator <- array( GetMaxPlayers(), 0 );

// Death Spawn
Hospital_Spawn <- array( GetMaxPlayers(), false );
HospitalSpawnLocations <- [ Vector( -886.074,-470.278,13.1109 ), Vector( -783.051,1141.83,12.4111), Vector( 467.763,697.68,11.7033 ), Vector( -135.137,-981.579,10.4634 ) ];
Hospital_Locator <- array( GetMaxPlayers() );

District_ID <- array(120, 0);
District_Name <- array(120, "");

//-----------------------------------------------------------------------------------------

function SetPlayersDistrict(Name, player) {
    switch(Name) {
        /* First Island */
        case "Vice Point":                             return District_ID[player.ID] = 1;
        case "Ocean Beach":                            return District_ID[player.ID] = 2;
        case "Washington Beach":                       return District_ID[player.ID] = 3;
       
        /* Center Shit */
        case "Leaf Links":                             return District_ID[player.ID] = 4;
        case "Starfish Island":                        return District_ID[player.ID] = 5;
        case "Prawn Island":                           return District_ID[player.ID] = 6;

        /* Third Island */
        case "Escobar International Airport":          return District_ID[player.ID] = 7;
        case "Downtown Vice City":                     return District_ID[player.ID] = 8;
        case "Junkyard":                               return District_ID[player.ID] = 9;
        case "Little Haiti":                           return District_ID[player.ID] = 10;
        case "Little Havana":                          return District_ID[player.ID] = 11;
        case "Little Haiti":                           return District_ID[player.ID] = 12;
       
        case "Viceport":                               return District_ID[player.ID] = 13;
        case "Vice City":                              return District_ID[player.ID] = 14;
        default:                                       return District_ID[player.ID] = 0;
    }
}

//-----------------------------------------------------------------------------------------

function GetPlayerIsland(player) {
    if ( ( District_ID[player.ID] >= 1 ) && ( District_ID[player.ID] <= 3 ) ) {
        if (NearestHospital(player.Pos) == 1 ) {
            Player_Island[player.ID] = 1;
            Player_Locator[player.ID] = 1;
        }
        if (NearestHospital(player.Pos) == 2 ) {
            Player_Island[player.ID] = 1;
            Player_Locator[player.ID] = 2;
        }
    }

    if ( ( District_ID[player.ID] >= 4 ) && ( District_ID[player.ID] <= 6 ) ) {
         if (NearestHospital(player.Pos) == 1 ) {
            Player_Island[player.ID] = 2;
            Player_Locator[player.ID] = 1;
        }
         if (NearestHospital(player.Pos) == 2 ) {
            Player_Island[player.ID] = 2;
            Player_Locator[player.ID] = 2;
        }   
     }

    if ( ( District_ID[player.ID] >= 7 ) && ( District_ID[player.ID] <= 12 ) ) {
         if (NearestHospital(player.Pos) == 1 ) {
            Player_Island[player.ID] = 3;
            Player_Locator[player.ID] = 1;
        }         
        if (NearestHospital(player.Pos) == 2 ) {
            Player_Island[player.ID] = 3;
            Player_Locator[player.ID] = 2;   
        }
    }

    if ( ( District_ID[player.ID] == 13 ) || ( District_ID[player.ID] == 0 ) || ( District_ID[player.ID] == 14 )) {
         if ((GetIsland(player.Pos) == 1 ) && ( NearestHospital(player.Pos) == 1 )) {
            Player_Island[player.ID] = 1;
            Player_Locator[player.ID] = 1;
         }
         if ((GetIsland(player.Pos) == 1 ) && ( NearestHospital(player.Pos) == 2 )) {
            Player_Island[player.ID] = 1;
            Player_Locator[player.ID] = 2;
         }         
         if ((GetIsland(player.Pos) == 2 ) && ( NearestHospital(player.Pos) == 1 )) {
            Player_Island[player.ID] = 2;
            Player_Locator[player.ID] = 1;   
         }       
         if ((GetIsland(player.Pos) == 2 ) && ( NearestHospital(player.Pos) == 2 )) {
            if (player.Pos.x < -500 ) {
              Player_Island[player.ID] = 3;
              Player_Locator[player.ID] = 2;
            }
            else{
                Player_Island[player.ID] = 2;
                Player_Locator[player.ID] = 2;
            }
         }
         if ((GetIsland(player.Pos) == 3 ) && ( NearestHospital(player.Pos) == 1 )) {
            Player_Island[player.ID] = 3;
            Player_Locator[player.ID] = 1;
         }
         if ((GetIsland(player.Pos) == 3 ) && ( NearestHospital(player.Pos) == 2 )) {
              Player_Island[player.ID] = 3;
              Player_Locator[player.ID] = 2;
         }
    }
}

//-----------------------------------------------------------------------------------------

function GetNearestHospital(player) {
    Hospital_Spawn[player.ID] = true;
    Hospital_Locator[player.ID] = player.Pos;   

    District_Name[player.ID] = GetDistrictName(player.Pos.x, player.Pos.y);
   
    SetPlayersDistrict(District_Name[player.ID], player); //Do not remove player, LOL..

    GetPlayerIsland(player);
    if ( Player_Island[player.ID] == 1 ) {
        local
        pos = player.Pos,
        first_check = GetDistance(HospitalSpawnLocations[2], pos).tointeger(),
        second_check = GetDistance(HospitalSpawnLocations[3], pos).tointeger();

        if (first_check <= second_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[2];
        else if (second_check <= first_check )   Hospital_Locator[player.ID] = HospitalSpawnLocations[3];   
    }

    if ( Player_Island[player.ID] == 3 ) {
        local
        pos = player.Pos,
        first_check = GetDistance(HospitalSpawnLocations[0], pos).tointeger(),
        second_check = GetDistance(HospitalSpawnLocations[1], pos).tointeger();

        if (first_check <= second_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[0];
        else if (second_check <= first_check )   Hospital_Locator[player.ID] = HospitalSpawnLocations[1];
    }
   
    if ( Player_Island[player.ID] == 2 ) {
        if (Player_Locator[player.ID] == 1 ) {
            local
            pos = player.Pos,
            first_check = GetDistance(HospitalSpawnLocations[1], pos).tointeger(),
            second_check = GetDistance(HospitalSpawnLocations[2], pos).tointeger();

            if (first_check <= second_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[1];
            else if (second_check <= first_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[2]; 
        }

        if (Player_Locator[player.ID] == 2 ) {
            local
            pos = player.Pos,
            first_check = GetDistance(HospitalSpawnLocations[0], pos).tointeger(),
            second_check = GetDistance(HospitalSpawnLocations[3], pos).tointeger();

            if (first_check <= second_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[0];
            else if (second_check <= first_check ) Hospital_Locator[player.ID] = HospitalSpawnLocations[3];
        }
    }
}
//-----------------------------------------------------------------------------------------
function GetIsland(pos) {
 
  // First Island
  if (pos.x > -170) return 1;
  // Third Island
  if (pos.x < -716) return 3;
 
  // Center island BS
  return 2;
}

function NearestHospital(pos) {
    if (pos.y >= 0) return 1; // north hospitals
    if (pos.y <= 0) return 2; // south hospitals
}
//-----------------------------------------------------------------------------------------
function onPlayerRequestClass( player, classID, team, skin ) {
    if (Hospital_Spawn[player.ID] == true) { player.Spawn(); }
}

function onPlayerSpawn(player) {
    // Spawn data
    if (Hospital_Spawn[player.ID] == true) { player.Pos = Hospital_Locator[player.ID]; Hospital_Spawn[player.ID] = false; }
}

function onPlayerKill( killer, killed, reason, bodypart ) {
    GetNearestHospital(killed);
}
function onPlayerDeath( player, reason ) {
    GetNearestHospital(player);
}

function GetDistance(pos1, pos2)
{
    return sqrt(pow(pos1.x - pos2.x, 2) + pow(pos1.y - pos2.y, 2) + pow(pos1.z - pos2.z, 2));
}

function onPlayerPart(player, reason) {
  Player_Island[player.ID] = 0;
  Player_Locator[player.ID] = 0;
  Hospital_Spawn[player.ID] = false;
  Hospital_Locator[player.ID] = null;

  District_ID[player.ID] = 0;
  District_Name[player.ID] = "";
}

//-----------------------------------------------------------------------------------------

I'm actually getting ready to re-write this code, so have fun with it ;D

Yeah, maybe I'll be coming back with a server maybe not :P

Mötley

#1
Added a second fix, So Viceport goes all the way to the first island... And causes a lot of issues,

on GetPlayerIsland(player)
You might want to play with
         
if ((GetIsland(player.Pos) == 2 ) && ( NearestHospital(player.Pos) == 2 )) {
            if (player.Pos.x < -500 ) {
              Player_Island[player.ID] = 3;
              Player_Locator[player.ID] = 2;
            }
            else{
                Player_Island[player.ID] = 2;
                Player_Locator[player.ID] = 2;
            }
         }
-500 seems to be okay, just drive on that main bridge, dont worry about being that close to the center on the map as that entire area is patched, everything else island etc works fine :).

Just fiddle with that line if anything ;)



If you want to test it, to prevent insanity just use this:

// Server Loop
Server_Timer <- null;

function Server_Loop() {
   
  local Players = GetPlayers();
  if (!Players) return; // Skip the loop

  for (local i=0;i<GetMaxPlayers();i++) {
    local player = FindPlayer(i);
   
    if (player) {
      if (player.ID == i) {

        GetNearestHospital(player)

        Announce("~b~Island~w~: ~r~ " + Player_Island[i] + " ~b~Loacation~w~: ~r~ " + Player_Locator[i], player, 1);
      }
    }
  }
}

function onScriptLoad() {
Server_Timer = NewTimer( "Server_Loop", 1000, 0 );
}

function onScriptUnload() {
local timer = Server_Timer;
    if ( timer ) {
      timer.Delete();
      Server_Timer = null;
    }
}

Mohamed Boubekri

| What now ? | Not yet ! |
Morrocan:- [ 🇲🇦 ].