Jump to content
pierremgi

[released] MGI_fnc_nearestAirport

Recommended Posts

Hello all,

The command nearestAirport  doesn't exist.
Here is a little script for finding the nearest existing (native) airport or edited one (USS Freedom).

MGI_fnc_nearestAirport = {
  params [["_plane",player,[objNull,[]],[2,3]],"_airport_pos","_airport_inter","_checkValue","_airportName","_airport_dir","_airport_or"];
  private _world = (configFile / "cfgWorlds" / worldName);
  private _airports = [[0,getArray (_world / "ilsPosition"), getArray (_world / "ilsDirection")]];
  private _airport_dist = 10^6;
  private _dynAirports = (allAirports #1);
  {
    _airports pushBack [_forEachIndex,getArray (_x / "ilsPosition"),getArray (_x /"ilsDirection")]
  } forEach ("true" configClasses (_world / "secondaryAirports"));
  
  if (_airports isNotEqualTo [] && ((count _airports !=1) or (_airports #0#1#0 != _airports #0#1#1))) then {
    _airports = _airports apply {[(_x#1) distance2D _plane,_x]};
    _airports sort TRUE;
    _airport_dist = (_airports #0#0);
    _airport_pos = (_airports #0#1#1);
    _airport_dir = _plane getDir _airport_pos;
    _checkValue = (_airports #0#1#2);
    _airport_or = if ((_checkValue #2) != 0) then [{atan ((_checkValue #0)/(_checkValue #2))},{90}];
    _airportName = text (nearestLocation [_airport_pos,"NameLocal"]);
  };

  if (_dynAirports isNotEqualTo []) then {
    _airports = _dynAirports apply {[_x distance2D _plane,_x]};
    _airports sort TRUE;
    _airport_inter = (_airports #0#0);
    if (_airport_inter < _airport_dist) then {
      _airport_pos = getpos (_airports #0#1);
      _airport_dir = _plane getDir _airport_pos;
      _airport_dist = _airport_inter;
      _airportName = "CVN";
      _airport_or = (172+ getdir (_airports #0#1)) mod 360;
    };
  };
  [_airportName,round _airport_or,_airport_pos,round _airport_dist,round _airport_dir];
};

 

Usage:

just call it like:

call MGI_fnc_nearestAirport;  // refers to player by default

plane1 call MGI_fnc_nearestAirport;  // refers to object plane1
[anyPosition] call MGI_fnc_nearestAirport; // any position must be an array inside the array of parameter (like: [getpos player] or [[xxx,yyy]]...)

 

The function will return an array with:
[name of the airport (location), orientation of the main runway/angle deck for CVN, position of the airport, distance, azimuth]

 

Note: you can add it in cfgFunctions for better performance while calling it multiple times:
In your description.ext (or cpp ,hpp in addons)

class cfgFunctions
{
    class MGI
        {
            class anyCategoryYouWant {
              file = "yourSubFolder(s)"; // "\yourSubFolder(s) for addon"
              class nearestAirport{};
        };
    };
};


Then place the code in an sqf named fn_nearestAirport  inside your path (described as file). SQF starts with params line and ends with the returning array (must not be wrapped inside MGI_fnc_nearestAirport {...};)
And same usage in scripts: plane1 call MGI_fnc_nearestAirport;

 

  • Like 4

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

×