Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: Kewun on Aug 18, 2016, 08:04 AM

Title: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 08:04 AM
In lu there is GetClosestPlayer() but in vcmp no? i want to add a bind key to my server if player presses X, tries to arrest the nearest  wanted player

how? i need getclosestplayer function
Title: Re: function Get nearest player?
Post by: KAKAN on Aug 18, 2016, 08:30 AM
Use a loop and check each player's distance.
for ex:-
local cplr = 0;
for( local i =0; i< 50; i++ )
{
local plr = FindPlayer( i );
if( !plr ) continue;
local dis = DistanceFromPoint( player.Pos.x, player.Pos.y, plr.Pos.x, plr.Pos.y ); //Correct it, it might be wrong, or use player.Pos.Distance( plr.Pos ) if you want to add z.
if( dis < cplr ) cplr = dis;
}
player.Msg( cplr );
Correct it, it might be wrong. Just an example.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 08:59 AM
hmhmhmmrhmhmhphm... okay ill test this thing out and mess with it.. ill mark this topic as solved if i will make it work..
Title: Re: function Get nearest player?
Post by: DizzasTeR on Aug 18, 2016, 12:42 PM
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}
// ----

local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m

Untested, if you want to get nearest player, specially inside cases that it calls the function everytime 'X' is pressed, then you should keep a max range, you don't want people in one corner of the map to arrest a guy in the opposite corner if that's the nearest to him.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:02 PM
if i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller
Title: Re: function Get nearest player?
Post by: Murdock on Aug 18, 2016, 01:03 PM
Quote from: Kewun on Aug 18, 2016, 01:02 PMif i tested, GetClosestPlayer returns myself ;c :( even if there are plrs on serv. in your script doomkiller

That sure must be hard to fix yourself..
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:09 PM
not hard, im trying to fix it with my self
Title: Re: function Get nearest player?
Post by: DizzasTeR on Aug 18, 2016, 01:11 PM
Did you test it with multiple people on the server? I won't be surprised to hear a negative answer.
Title: Re: function Get nearest player?
Post by: . on Aug 18, 2016, 01:14 PM
Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.

It's actually what was supposed to happen because the source player is the closest to itself and there's no guard against that.

The following code:
if( player ) {
Could be changed into:
if( player != null && player != source ) {
Which can probably be collapsed into:
if( player != source ) {
Assuming that `source` is not null.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:21 PM
Quote from: Doom_Kill3R on Aug 18, 2016, 01:11 PMDid you test it with multiple people on the server? I won't be surprised to hear a negative answer.

tested with multiple players.


even if ( player != source ) { didnt work

ill try to fix..
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:22 PM
well after using if ( player != source ) { i could'nt arrest anyone
Title: Re: function Get nearest player?
Post by: KAKAN on Aug 18, 2016, 01:40 PM
Quote from: Kewun on Aug 18, 2016, 01:22 PMwell after using if ( player != source ) { i could'nt arrest anyone
show us your updated code.
it should be:-
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player != null && player != source ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}
// ----

local plr = GetClosestPlayer( player, 40 ); // return the closest player in range of 40m
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:41 PM
i tried it. Shows myself.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 01:55 PM
damn shit fixing it is hard for me a bit
Title: Re: function Get nearest player?
Post by: EK.IceFlake on Aug 18, 2016, 04:44 PM
Use original code and compare their IDs. Comparing instances will not get you anywhere.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 05:22 PM
so i will have to use if ( player.ID != source.ID ) {

?
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 05:24 PM
not worked ;c
Title: Re: function Get nearest player?
Post by: KAKAN on Aug 18, 2016, 06:21 PM
Quote from: Kewun on Aug 18, 2016, 05:24 PMnot worked ;c
show me the function you used. And check for duplicates.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 06:24 PM
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if ( player.ID != source.ID ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}

and

function onArrestBind(player)
{
local plr = GetClosestPlayer(player, 40)
if ( !ReadIniBool("stats.ini","logged",player.Name) ) return false;
if ( player.Skin != 1) return false;
if (!plr) {
MessagePlayer("[#ff0000]Target does not exist",player)
return false;
}
if (plr.WantedLevel <1) {
MessagePlayer("[#ff0000]This player is not wanted",player)
return false;
}
if ( plr.Name == player.Name) {
MessagePlayer("[#ff0000]You cant arrest your self!",player)
return false;
}
if (plr.WantedLevel > 0) {
local distance = player.Pos.Distance(target.Pos)
if ( distance < 5 ) {
if ( plr.Vehicle ) plr.Eject();
MessagePlayer("[#ffffff]Arrested "+plr+" , you get $2500",player)
player.Cash += 2500;
//target.Pos = Vector(386.59, -502.732, 9.39561)
target.Pos = Vector(390.684, -507.166, 9.39561);
target.WantedLevel = 0
NewTimer("BailOut",30000,1,target.Name,target.ID)
MessagePlayer("[#00ff00]You will be bailed out of jail in 30 seconds..",plr)
plr.Skin = 7
plr.Disarm();
SaveAccount(plr)
Message("[#ffffff]Police "+player+ " has arrested "+plr)
}
else {
MessagePlayer("[#00ff00]You must be near wanted target to arrest him",player)
return false;
}
}
}
Title: Re: function Get nearest player?
Post by: Kewun on Aug 18, 2016, 06:43 PM
i also tried if ( source.ID != player.ID ) {
Title: Re: function Get nearest player?
Post by: Kewun on Aug 19, 2016, 12:34 PM
:/ i tried to fix, i failed
Title: Re: function Get nearest player?
Post by: KAKAN on Aug 19, 2016, 01:25 PM
try this:-
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            if( i == source.ID ) continue;
            local player = FindPlayer( i );
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
        }
    return tbl_CloestPlayer.Instance;
}
And too, tell me the following output:-
print( typeof source.ID );
print( typeof i );
print( typeof player.ID );
They should be integers
Title: Re: function Get nearest player?
Post by: Kewun on Aug 19, 2016, 02:02 PM
AN ERROR HAS OCCURED [the index 'Pos' does not exist]

CALLSTACK
*FUNCTION [GetClosestPlayer()] server.nut line [955]
*FUNCTION [onArrestBind()] server.nut line [895]
*FUNCTION [onKeyDown()] server.nut line [1444]

LOCALS
[player] NULL
[i] 2
[tbl_CloestPlayer] TABLE
[int_MaxPlayers] 50
[max_distance] 40
[source] INSTANCE
[this] TABLE
[player] INSTANCE
[this] TABLE
[key] 12
[player] INSTANCE
[this] TABLE

function on key down:

function onKeyDown(player,key)
{
    if ( key == action )
    {
       onArrestBind(player)
     }
}
line: onArrestBind(player)

on arrest bind:
function onArrestBind(player)
{
local plr = GetClosestPlayer(player, 40)
if ( !ReadIniBool("stats.ini","logged",player.Name) ) return false;
if ( player.Skin != 1) return false;
if (!plr) {
MessagePlayer("[#ff0000]Target does not exist",player)
return false;
}
if (plr.WantedLevel <1) {
MessagePlayer("[#ff0000]This player is not wanted",player)
return false;
}
if ( plr.Name == player.Name) {
MessagePlayer("[#ff0000]You cant arrest your self!",player)
return false;
}
if (plr.WantedLevel > 0) {
local distance = player.Pos.Distance(target.Pos)
if ( distance < 5 ) {
if ( plr.Vehicle ) plr.Eject();
MessagePlayer("[#ffffff]Arrested "+plr+" , you get $2500",player)
player.Cash += 2500;
//target.Pos = Vector(386.59, -502.732, 9.39561)
target.Pos = Vector(390.684, -507.166, 9.39561);
target.WantedLevel = 0
NewTimer("BailOut",30000,1,target.Name,target.ID)
MessagePlayer("[#00ff00]You will be bailed out of jail in 30 seconds..",plr)
plr.Skin = 7
plr.Disarm();
SaveAccount(plr)
Message("[#ffffff]Police "+player+ " has arrested "+plr)
}
else {
MessagePlayer("[#00ff00]You must be near wanted target to arrest him",player)
return false;
}
}
}
line: local plr = GetClosestPlayer(player, 40)
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            if( i == source.ID ) continue;
            local player = FindPlayer( i );
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
        }
    return tbl_CloestPlayer.Instance;
}
line: local distance = source.Pos.Distance( player.Pos );
Title: Re: function Get nearest player?
Post by: DizzasTeR on Aug 19, 2016, 02:12 PM
I'm still wondering why is this all being such a mess, the only mistake in my function was not adding an exception for the function caller, otherwise it just works perfectly. Final function:
function GetClosestPlayer( source, max_distance = 100 ) {
    local int_MaxPlayers = GetMaxPlayers(), tbl_CloestPlayer = { Instance = null, Distance = max_distance };
        for( local i = 0; i < int_MaxPlayers; ++i ) {
            local player = FindPlayer( i );
            if( player && player.ID != source.ID ) {
                local distance = source.Pos.Distance( player.Pos );
                if( distance < tbl_CloestPlayer.Distance ) {
                    tbl_CloestPlayer.Instance = player;
                    tbl_CloestPlayer.Distance = distance;
                }
            }
        }
    return tbl_CloestPlayer.Instance;
}

Tested and works.
Title: Re: function Get nearest player?
Post by: Kewun on Aug 19, 2016, 02:25 PM
thx bro it works <3 
Title: Re: function Get nearest player?
Post by: KAKAN on Aug 19, 2016, 05:02 PM
SLC said the same thing and Kewun said it didn't work :\
Title: Re: function Get nearest player?
Post by: Kewun on Aug 19, 2016, 05:05 PM
if it was same thing, then it would work for me