Jump to content
phronk

[SOLVED] Nearest Occupied Vehicles

Recommended Posts

I'm having a brain-fart with finding all nearest Blufor units around the player within a radius, INCLUDING units INSIDE vehicles. (Whether it be driver, gunner, passenger, etc.)

 

I'm searching for nearby units like this:

BLU=["B_Soldier_base_F","MRAP_01_base_F","Truck_01_base_F","MBT_01_base_F","LSV_01_base_F","B_APC_Wheeled_01_base_F","B_APC_Tracked_01_base_F","Heli_Light_01_base_F","Heli_Transport_01_base_F","Heli_Transport_03_base_F","Plane_CAS_01_base_F","Parachute_02_base_F","B_Boat_Transport_01_F","B_Boat_Armed_01_minigun_F","B_SDV_01_F"];

 

_n=nearestObjects[player,BLU,8192];_c=[];

{if((isAbleToBreathe _x)&&{(alive _x)&&(_x!=player)})then{_c set[(count _c),_x];};}forEach _n;


The above code, even if "B_Soldier_base_F" is the only object being searched for, will not return units inside vehicles.  It's possible that once a unit enters a vehicle, he is no longer "B_Soldier_base_F" until he dismounts, but I could definitely be wrong and overlooking something...

 

[EDIT]

Figured out a possible work around:

BLU=["B_Soldier_base_F","MRAP_01_base_F","Truck_01_base_F","MBT_01_base_F","LSV_01_base_F","B_APC_Wheeled_01_base_F","B_APC_Tracked_01_base_F","Heli_Light_01_base_F","Heli_Transport_01_base_F","Heli_Transport_03_base_F","Plane_CAS_01_base_F","Parachute_02_base_F","B_Boat_Transport_01_F","B_Boat_Armed_01_minigun_F","B_SDV_01_F"];

 
_n=nearestObjects[player,BLU,WS];_c=[];_v=[];
{if((isAbleToBreathe _x)&&{(alive _x)&&(_x!=player)})then{_c set[(count _c),_x];};}forEach _n;
{if((count(crew _x)>1)&&{(alive _x)&&(_x!=player)})then{_v set[(count _v),_x];};}forEach _n;

_c=_c+_v;

_c select 0;

I think this should work.  Still need to test it more.

Share this post


Link to post
Share on other sites
11 hours ago, phronk said:

[EDIT]

Figured out a possible work around:


BLU=["B_Soldier_base_F","MRAP_01_base_F","Truck_01_base_F","MBT_01_base_F","LSV_01_base_F","B_APC_Wheeled_01_base_F","B_APC_Tracked_01_base_F","Heli_Light_01_base_F","Heli_Transport_01_base_F","Heli_Transport_03_base_F","Plane_CAS_01_base_F","Parachute_02_base_F","B_Boat_Transport_01_F","B_Boat_Armed_01_minigun_F","B_SDV_01_F"];

 
_n=nearestObjects[player,BLU,WS];_c=[];_v=[];
{if((isAbleToBreathe _x)&&{(alive _x)&&(_x!=player)})then{_c set[(count _c),_x];};}forEach _n;
{if((count(crew _x)>1)&&{(alive _x)&&(_x!=player)})then{_v set[(count _v),_x];};}forEach _n;

_c=_c+_v;

_c select 0;

I think this should work.  Still need to test it more.

 

Is there a case where a unit would satisfy isAbleToBreath but not alive? Your edited code should find vehicles, but I don't think it will return the units in them.

 

Additionally, I think it is possible to substitute nearestObjects with nearEntities: would remove the need to enumerate all BLUFOR CfgVehicles.

 

This is what I came up with:

_n = player nearEntities(WS);
_units = [];

{
    if (side(_x) == west) then
    {
        // Need to find each unit in a vehicle
        if (vehicle(_x) == _x) then
        {
            {
                if (alive(_x) && _x != player) then
                {
                    _units append [_x];
                };
                
            } forEach(crew(_x));
        }
        
        else
        {
            if (alive(_x) && _x != player) then
            {
                _units append [_x];
            };
        };
    };

} forEach(_n);

hint str(_units);

Note: I have not tested the code above.

Share this post


Link to post
Share on other sites

While nearEntities is faster, it doesn't organize the entities from nearest to farthest (In fact, it seems like it's ordered randomly) and I need that for a script I'm writing, to gauge the distance between the player and the nearest object/entity.

I haven't been able to test if just using (isPlayer _x) would work, but would that return players inside vehicles?  Or at least the driver or random player inside the vehicle?  If that's the case, the isPlayer check would suit my needs perfectly.

 

[EDIT]

According to the Kronzky on the wiki: (in regards to isPlayer)
 

Quote

This is not the same as testing object == player, because in MP it tests for any player, not only for the local one. If object is a vehicle, the test is done for the vehicle commander.

Sounds like isPlayer could be my solution after all.

Share this post


Link to post
Share on other sites

Not sure about using isPlayer to get units inside vehicles, but the code I posted should take care of getting all alive units in a vehicle.

 

However, it seems you intend on grabbing only one of the alive units inside the vehicle?

Share this post


Link to post
Share on other sites

How about something like...

_foundUnits = [];
{
    if ( typeOf _x isKindOf "Man" ) then {
        _nul = _foundUnits pushBack _x;
    }else{
        _foundUnits append ( fullCrew _x apply{ _x select 0 }select{ !isPlayer _x } );
    };
}forEach (( nearestObjects[ player, [ "AllVehicles" ], worldSize ] select{
    alive _x && { side group _x isEqualTo west }
}) - allPlayers );

Will return all west units even inside vehicles that are alive and are not players in order of units vehicle distance from player.

  • Like 1

Share this post


Link to post
Share on other sites
Quote

I'm having a brain-fart with finding all nearest Blufor units around the player within a radius, INCLUDING units INSIDE vehicles. (Whether it be driver, gunner, passenger, etc.)

 

(entities [["SoldierWB"], [], true, true]) inAreaArray [position player, 8192, 8192, 360, false, -1];

 

  • Like 1

Share this post


Link to post
Share on other sites

Seems to work well also, thanks!  Doesn't return empty vehicles (which is good), but return units and vehicles occupied by friendlies (extra good).  If I end up using this, I'll add you to the credits of my script.  I think it's actually more optimized than my original approach too.

Problem solved.  Thanks guys!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×