EVENT_DETECT nearest target.

Posted By: mEnTaL

EVENT_DETECT nearest target. - 07/31/12 19:42

Ok, I have an entity which performs c_scan every frame and detects (throught EVENT_DETECT) only entities with skill2=2;
My question is how to make it detect the nearest one and set the "YOU" poiner to it?
Posted By: rayp

Re: EVENT_DETECT nearest target. - 07/31/12 19:51

One simple way would be

Code:
var min_dist = 100;

result = c_scan
if(you)if(vec_dist(my.x, you.x) < min_dist) _found_a_near_ent;
//if(result < 100) _found_a_neaR_ent; //could work


Or u save all found ents in an array then checking all distances to get the "real" nearest ent.

Edit: Manual says YOU is automatic set to nearest entity.
Posted By: mEnTaL

Re: EVENT_DETECT nearest target. - 07/31/12 20:03

The first way you suggested will just find a near entity within the scanning cone. I've already done that. The problem is that my scanning distance is much more than 100 quants and i rly need to find the NEAREST one. The second way you mentioned is exactly what I need, but unfortunately I don't know how to make it.
Some help will be appreciated. laugh
Posted By: rayp

Re: EVENT_DETECT nearest target. - 07/31/12 20:22

Quote:
:
var i;
for (i=0; i<5; i++) // repeat 5 times
x *= x; // calculate the 5th power of x


edit: Adding something to an array (if i remember right) looks like this:
Code:
var my_array[100];
var my_index;
var i;

//add
    c_scan
    if(you)
    {
      my_Array[my_Index] = handle (you);
      my_index += 1;
    }

But this is a long time ago i used a array with 3dgs

Edit2: theres an simple way to go through all ents with an "for" then using vec_dist, but i cant remember.

Edit3: This is what i meant. U could modify it with vec_dist etc. to check for all ents
Quote:

function hide_all_ents()
{
// repeat for all entities
for(you = ent_next(NULL); you; you = ent_next(you))
set(you,INVISIBLE); // make entity invisible
}
}

Posted By: mEnTaL

Re: EVENT_DETECT nearest target. - 07/31/12 21:48

Thanks for the reply, but that function will just hide all entities within the scan cone. I need something like that:
1. Use vec_dist to get the distances to all entities with skill2=1 within the scan cone
2. Compare the distances to each one of them
3. Determine which is the smallest distance
4. Set the "you" pointer to the nearest scanned entity

This is the sequence, but i just don't know how to make it work.
Posted By: rayp

Re: EVENT_DETECT nearest target. - 07/31/12 22:13

I know what this code above does it should show you just the basic. Here is the mod which i meant above. It works without c_scan. I did not test it but i think it finds the nearest one. Use it fex. like this find_nearest(player); This should find the nearest you to player.

Code:
// -------------------------------------------------------------------------------------------------------------------------------------------------
function find_nearest(ENTITY* ent)
{
 var you_handle; 
 var _dist=1000;
 var _ndist;
 var dist_to_nearest;
// repeat for all entities
 for(you = ent_next(NULL); you; you = ent_next(you)) 
  {
    if(you.skill2 == 1) 
    {
    	_ndist = vec_dist(ent.x, you.x);
    	if (_ndist < _dist) { _dist = _ndist; you_handle = handle(you); }
    }
  }
  dist_to_nearest = _dist; //nearest entity to "ent"
  you = ptr_for_handle(you_handle); //you to nearest found
 }
// -------------------------------------------------------------------------------------------------------------------------------------------------


Posted By: mEnTaL

Re: EVENT_DETECT nearest target. - 07/31/12 23:34

Looks like it works. Thanks, man! laugh
Posted By: rayp

Re: EVENT_DETECT nearest target. - 08/01/12 00:02

Your welcome !

Btw: I blame the manual:
Quote:


function hide_all_ents()
{
// repeat for all entities
for(you = ent_next(NULL); you; you = ent_next(you))
set(you,INVISIBLE); // make entity invisible
}
}


Edit2:
I now used my own function from above the first time. Worked well.
© 2024 lite-C Forums