Raised This Month: $32 Target: $400
 8% 

distance between players


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 08-09-2022 , 00:12   distance between players
Reply With Quote #1

Hello, I wanted to know if there is a way to obtain the distance between players, I am doing it this way, but since it is done repeatedly, it does not seem very light to me to call the engine twice each time it is executed, is there another way?

PHP Code:
   static Float:OriginPlayer1[3], Float:OriginPlayer2[3], Float:Distance;

   
entity_get_vectorPlayer1 EV_VEC_originOriginPlayer1 );
   
entity_get_vectorPlayer2EV_VEC_originOriginPlayer2 ); 

   
Distance get_distance_fOriginPlayer1OriginPlayer2 ); 
Solved!( i Think this is better )

PHP Code:
native Float:entity_range(idaidb); 

Last edited by MrPickles; 08-09-2022 at 00:34.
MrPickles is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 08-09-2022 , 01:30   Re: distance between players
Reply With Quote #2

If you are interested, here's this native you've used inside engine.cpp:

Code:
static cell AMX_NATIVE_CALL entity_range(AMX *amx, cell *params)
{
	int idxa = params[1];
	int idxb = params[2];

	CHECK_ENTITY(idxa);
	CHECK_ENTITY(idxb);

	edict_t *pEntA = TypeConversion.id_to_edict(idxa);
	edict_t *pEntB = TypeConversion.id_to_edict(idxb);

	REAL fRet = (pEntA->v.origin - pEntB->v.origin).Length();

	return amx_ftoc(fRet);
}
Basically it does the same as the first option you used, but definitely faster.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 08-09-2022 , 01:34   Re: distance between players
Reply With Quote #3

Quote:
Originally Posted by EFFx View Post
If you are interested, here's this native you've used inside engine.cpp:

Code:
static cell AMX_NATIVE_CALL entity_range(AMX *amx, cell *params)
{
	int idxa = params[1];
	int idxb = params[2];

	CHECK_ENTITY(idxa);
	CHECK_ENTITY(idxb);

	edict_t *pEntA = TypeConversion.id_to_edict(idxa);
	edict_t *pEntB = TypeConversion.id_to_edict(idxb);

	REAL fRet = (pEntA->v.origin - pEntB->v.origin).Length();

	return amx_ftoc(fRet);
}
Basically it does the same as the first option you used, but definitely faster.
Thanks! very usefull!, how can i check it for get vector and get_distance_f ? by the way i think entity_range is better than take 2 vectors and use get_distance_f, right?, i need it faster and light, cause i will check it constantly

Last edited by MrPickles; 08-09-2022 at 01:37.
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-09-2022 , 01:40   Re: distance between players
Reply With Quote #4

Huh? EFFx confirmed that you found the correct function to solve your problem.
__________________
fysiks is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 08-09-2022 , 02:28   Re: distance between players
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
Huh? EFFx confirmed that you found the correct function to solve your problem.
yes i know, i just want to check how entity_get_vector and get_distance_f works, I will definitely use entity range, its just to learn

Last edited by MrPickles; 08-09-2022 at 02:30.
MrPickles is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 08-09-2022 , 03:31   Re: distance between players
Reply With Quote #6

There's a lot of informations about how natives looks like inside github. The engine.cpp is the one that you should search about.

Code:
static cell AMX_NATIVE_CALL entity_get_vector(AMX *amx, cell *params)
{
	int iEnt = params[1];
	int idx = params[2];
	cell *vRet = MF_GetAmxAddr(amx, params[3]);
	Vector vRetValue = Vector(0, 0, 0);

	CHECK_ENTITY_SIMPLE(iEnt);

	edict_t *pEnt = TypeConversion.id_to_edict(iEnt);

	switch (idx)
	{
		case origin:
			vRetValue = pEnt->v.origin;
			break;
		case oldorigin:
			vRetValue = pEnt->v.oldorigin;
			break;
		case velocity:
			vRetValue = pEnt->v.velocity;
			break;
		case basevelocity:
			vRetValue = pEnt->v.basevelocity;
			break;
		case clbasevelocity:
			vRetValue = pEnt->v.clbasevelocity;
			break;
		case movedir:
			vRetValue = pEnt->v.movedir;
			break;
		case angles:
			vRetValue = pEnt->v.angles;
			break;
		case avelocity:
			vRetValue = pEnt->v.avelocity;
			break;
		case punchangle:
			vRetValue = pEnt->v.punchangle;
			break;
		case v_angle:
			vRetValue = pEnt->v.v_angle;
			break;
		case endpos:
			vRetValue = pEnt->v.endpos;
			break;
		case startpos:
			vRetValue = pEnt->v.startpos;
			break;
		case absmin:
			vRetValue = pEnt->v.absmin;
			break;
		case absmax:
			vRetValue = pEnt->v.absmax;
			break;
		case mins:
			vRetValue = pEnt->v.mins;
			break;
		case maxs:
			vRetValue = pEnt->v.maxs;
			break;
		case size:
			vRetValue = pEnt->v.size;
			break;
		case rendercolor:
			vRetValue = pEnt->v.rendercolor;
			break;
		case view_ofs:
			vRetValue = pEnt->v.view_ofs;
			break;
		case vuser1:
			vRetValue = pEnt->v.vuser1;
			break;
		case vuser2:
			vRetValue = pEnt->v.vuser2;
			break;
		case vuser3:
			vRetValue = pEnt->v.vuser3;
			break;
		case vuser4:
			vRetValue = pEnt->v.vuser4;
			break;
		default:
			return 0;
			break;
	}

	vRet[0] = amx_ftoc(vRetValue.x);
	vRet[1] = amx_ftoc(vRetValue.y);
	vRet[2] = amx_ftoc(vRetValue.z);

	return 1;
}
As you can see in the first case, it converts the pEnt to v.origin, the same thing that happens inside the entity_range().

The get_distance_f() is provided by vector.cpp

Code:
static cell AMX_NATIVE_CALL get_distance_f(AMX *amx, cell *params)
{
	cell *cpVec1 = get_amxaddr(amx, params[1]);
	cell *cpVec2 = get_amxaddr(amx, params[2]);

	Vector vec1 = Vector((float)amx_ctof(cpVec1[0]), (float)amx_ctof(cpVec1[1]), (float)amx_ctof(cpVec1[2]));
	Vector vec2 = Vector((float)amx_ctof(cpVec2[0]), (float)amx_ctof(cpVec2[1]), (float)amx_ctof(cpVec2[2]));

	REAL fDist = (REAL) (vec1 - vec2).Length();

	return amx_ftoc(fDist);
}
And here, it does the same too: gets the difference between two points. Not sure about what REAL does, maybe the same goal as the abs().
-

The idea is to make the code the shortest possible, so it is easy to edit, fix or read. Keep in mind that entity_get_vector() wasn't meant just for origin information, as you can see in the code inside engine.cpp. So if your goal was only to get the distance and nothing else, using entity_range() is way faster. Different natives for different purposes.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 08-09-2022 at 06:20.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-09-2022 , 23:00   Re: distance between players
Reply With Quote #7

Quote:
Originally Posted by EFFx View Post
The idea is to make the code the shortest possible
This statement should never be said in the context of writing code, ever.
__________________
fysiks is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 08-09-2022 , 23:18   Re: distance between players
Reply With Quote #8

What makes you say that? Why would I write a large code to do something easy, if I can use a smaller one which is faster and easy to understand, but with the same end goal?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 08-09-2022 , 23:27   Re: distance between players
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
This statement should never be said in the context of writing code, ever.
shortest & fast != less memory used?
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-09-2022 , 23:40   Re: distance between players
Reply With Quote #10

Quote:
Originally Posted by EFFx View Post
What makes you say that? Why would I write a large code to do something easy, if I can use a smaller one which is faster and easy to understand, but with the same end goal?
The length of code has no direct correlation to being "easy to edit, fix or read". You can easily make "short" code that is hard to read, fix, and edit.

Similarly, the length of code, generally, has no direct correlation to any performance metric (execution speed or memory). Also similarly, you can make "short" code that is inefficient.

I made the reply to your post because many new coders get it in their head that "shorter code is better" when they need to, instead, be thinking about how to write coded that is efficient and easy to read/edit.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:59.


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