AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Enemy distance in M (https://forums.alliedmods.net/showthread.php?t=283058)

EFFx 05-23-2016 20:52

Enemy distance in M
 
Man, i was searching for the distance between me and the enemy but i dont found a good format.
Appearing in hud the player closer and its distance in meters.

Like this:

Enemy: EFFx - 17 meters

I looked more wdo I thought were definitive codes for X distance, and I want to sample the distance from the closer, and I did not find it.

Can you guys help me?

wickedd 05-23-2016 23:18

Re: Enemy distance in M
 
Quote:

Originally Posted by EFFx (Post 2421569)
I looked more wdo I thought were definitive codes for X distance, and I want to sample the distance from the closer, and I did not find it.

I'm not sure what you're trying to say but here's one way to get the distance. Of course it's not the complete code because I'm not sure what you want.

PHP Code:

public whatever()
{
    
    new 
Float:kOrigin], Float:vOrigin];
     
    
pevyour_indexpev_originkOrigin )
    
pevenemy_indexpev_originvOrigin )
    
    new 
Float:iDist get_distance_fkOriginvOrigin )   
    new 
Float:iMeters iDist 0.0254  // This will convert it to meters.
    
    //Add your hud here



EFFx 05-24-2016 18:31

Re: Enemy distance in M
 
1 Attachment(s)
What i made:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

#define PLUGIN "Enemy distance"
#define VERSION "1.0"
#define AUTHOR "EFFx"

public plugin_init() 
{
 
register_plugin(PLUGINVERSIONAUTHOR)
}
public 
client_putinserver(id)
 
set_task(1.0,"ShowEnemyDHud",id,_,_,"b")
 
public 
ShowEnemyDHud(id)
{
 if(!
is_user_alive(id) && get_user_team(id) != 1)
 return 
PLUGIN_HANDLED
 
 
new Float:kOrigin], Float:vOrigin];
 
 new 
enemy
 
 pev
idpev_originkOrigin )
 
pevenemypev_originvOrigin )
 
 new 
Float:iDist get_distance_fkOriginvOrigin )   
 new 
Float:iMeters iDist 0.0254  // This will convert it to meters.
 
 
if(get_user_team(id) == get_user_team(enemy))
 return 
PLUGIN_HANDLED
 
 
new szHudEnemyD[50]
 
formatex(szHudEnemyD,charsmax(szHudEnemyD),"Enemy: %d meters",iMeters)
 
set_hudmessage(0,255,0,-1.0,0.70,0,1.0,1.0)
 
show_hudmessage(id,szHudEnemyD)
 return 
PLUGIN_CONTINUE


And like every format that i've searched, the result is:

Black Rose 05-24-2016 19:35

Re: Enemy distance in M
 
%d -> %.0f
The number decides hos many decimals.

EFFx 05-24-2016 19:44

Re: Enemy distance in M
 
2 Attachment(s)
Yes, your format made the plugin running successfly.
But this format is to all enemy, i want only one ( the closest )

Because when i'm by side the enemy, the distance is 56 meters/9 meters, wtf?

Here's the screenshot:
The first is when i'm alone, just me on the server.
The second photo is when i'm by side the enemy.

wickedd 05-24-2016 21:05

Re: Enemy distance in M
 
Quote:

Originally Posted by EFFx (Post 2421803)
What i made:

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

#define PLUGIN "Enemy distance"
#define VERSION "1.0"
#define AUTHOR "EFFx"

public plugin_init() 
{
 
register_plugin(PLUGINVERSIONAUTHOR)
}
public 
client_putinserver(id)
 
set_task(1.0,"ShowEnemyDHud",id,_,_,"b")
 
public 
ShowEnemyDHud(id)
{
 if(!
is_user_alive(id) && get_user_team(id) != 1)
 return 
PLUGIN_HANDLED
 
 
new Float:kOrigin], Float:vOrigin];
 
 new 
enemy
 
 pev
idpev_originkOrigin )
 
pevenemypev_originvOrigin )
 
 new 
Float:iDist get_distance_fkOriginvOrigin )   
 new 
Float:iMeters iDist 0.0254  // This will convert it to meters.
 
 
if(get_user_team(id) == get_user_team(enemy))
 return 
PLUGIN_HANDLED
 
 
new szHudEnemyD[50]
 
formatex(szHudEnemyD,charsmax(szHudEnemyD),"Enemy: %d meters",iMeters)
 
set_hudmessage(0,255,0,-1.0,0.70,0,1.0,1.0)
 
show_hudmessage(id,szHudEnemyD)
 return 
PLUGIN_CONTINUE


And like every format that i've searched, the result is:

Post the code you're using because this code would never work.

Edit: If you want to get the closet player near you, then you should use find_ent_in_sphere or find_ent_class. Then use the code I gave you.

EFFx 05-24-2016 21:37

Re: Enemy distance in M
 
And how will be? Can you make aa example?

Black Rose 05-25-2016 05:30

Re: Enemy distance in M
 
There's no way around it. Loop through all enemies and compare the distance. Save the one with the lowest value.
find_ent_in_sphere will not give you the result you are looking for.

siriusmd99 05-25-2016 08:13

Re: Enemy distance in M
 
hhaa, you are creating variable enemy and after that you use pev origin. You basically are getting server origin because it has ID 0. You shall loop all players, I will come with a loop soon:

Try :

PHP Code:


#include <amxmodx>
#include <fakemeta>

#define PLUGIN "Enemy distance"
#define VERSION "1.0"
#define AUTHOR "EFFx"

public plugin_init() 
{
 
register_plugin(PLUGINVERSIONAUTHOR)
}
public 
client_putinserver(id)
 
set_task(1.0,"ShowEnemyDHud",id,_,_,"b")
 
public 
ShowEnemyDHud(id)
{
 if(!
is_user_alive(id) && get_user_team(id) != 1)
 return 
PLUGIN_HANDLED
 
 
new Float:myOrigin], Float:hisOrigin];
 new 
Float:iDistFloat:tmpdist;
 new 
players[32], pnum;
 
 
pevidpev_originmyOrigin )
 
 
get_playersplayerspnum"ae""CT" );
 
 if(!
pnum
 return 
PLUGIN_HANDLED
 
 
for(new ipnumi++)
 {
 
   
pevipev_originhisOrigin );
   
tmpdist get_distance_fmyOriginhisOrigin );
   if(
tmpdist iDist)
     
iDist tmpdist;
 
 }
 
    
 
iDist *= 0.0254;
 
 new 
szHudEnemyD[50]
 
formatex(szHudEnemyD,charsmax(szHudEnemyD),"Enemy: %d meters"iDist)
 
set_hudmessage(0,255,0,-1.0,0.70,0,1.0,1.0)
 
show_hudmessage(id,szHudEnemyD)
 return 
PLUGIN_CONTINUE


I removed amxmisc include from the top, not necessary..
And also i suggest you to set task on player spawn and remove it on death or disconnect, it will be more efficient because in your way task is running on dead player , disconnected player, on ct,spectate and or player.. It's not effiecient....

Like this one, it's more better (but uses hamsandwich module for player spawn hook):

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define PLUGIN "Enemy distance"
#define VERSION "1.0"
#define AUTHOR "EFFx"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
RegisterHam(Ham_Spawn"player""set_the_task"1)
    
register_event("DeathMsg""eDeathMsg""a");
}
public 
set_the_task(id)
    if(
is_user_connected(id) && is_user_alive(id) && get_user_team(id) == 1)
    
set_task1.0"ShowEnemyDHud"id, .flags "b" )


public 
client_disconnect(id)
    if(
task_exists(id)) 
    
remove_task(id)

public 
eDeathMsg()
{
    new 
Victim read_data(2);
    if(
task_exists(Victim)) 
        
remove_task(Victim)
}    

public 
ShowEnemyDHud(id)
{
    
    new 
players[32], pnum;
    
    
get_playersplayerspnum"ae""CT" );
    
    if(!
pnum
        return 
PLUGIN_HANDLED
    
    
new Float:myOrigin], Float:hisOrigin];
    new 
Float:iDistFloat:tmpdist;
    
    
pevidpev_originmyOrigin )
    
    for(new 
ipnumi++)
    {
        
        
pevipev_originhisOrigin );
        
tmpdist get_distance_fmyOriginhisOrigin );
        if(
tmpdist iDist)
            
iDist tmpdist;
        
    }
    
    
set_hudmessage(0,255,0,-1.0,0.70,0,1.0,1.0)
    
show_hudmessage(id"Enemy: %d meters"iDist 0.0254)
    
    return 
PLUGIN_CONTINUE



EFFx 05-25-2016 16:01

Re: Enemy distance in M
 
1 Attachment(s)
The results from your codes:


All times are GMT -4. The time now is 18:42.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.