Author: RAVEN
Author Website: http://www.armedassault.com.ar/

Requirements: No addons required

Version: 1.1

Short description: This script makes the Anti Aircraft units search for ammo when they run out of ammo.

Date: 2009-08-29 06:30


Comments: (0)
Rating:



 

3-autoreload.jpg

AutoReload
by
RAVEN


Description:
This script makes the Anti Aircraft units search for ammo when they run out of ammo. They will search both in ammo trucks and ammo depots, but they will do it first in mobile ones. They will search which one is able to assist them (checking if they have a driver, if it's not damaged, if it has fuel ...) and call it to receive the ammo. Once the transfer is completed the ammo supplier will move back to its position, protected from enemy fire or any other zone waiting for another call.

If there is no vehicles near to supply the ammo and the unit can move, it will go to the nearest vehicle to rearm. And if there is no vehicle it will move to the closest non-mobile rearming point and then move back to its defending position.

The script can be setup to limit the rearming time (for example 10 seconds). This way you can make it go back to action quickly - but with less ammo - and you can also set the distance the vehicle can search for the suppliers. You can also limit the rearming processes or leave it unlimited using the value -1.

The AI in ArmA2 is better than in ArmA and when it runs out of ammo the commander sends it to rearm in the nearest supplier vehicle. But it only searches in a fixed distance (around 200 meters) and after rearming it doesn't come back to it's position.


Parameters:
1- Name of the vehicle.
2- Time in seconds, of the allowed rearming process.
3- Number of allowed rearming processes (-1: infinite)
4- List of unit names -or unit classnames- which will be searched to be the ammo suppliers.
5- Distance in meters of the zone the vehicle will search for the suppliers.

The parameter #4 can be used in two ways. You can add the unit classnames allowed to rearm (ex: ["KamazReammo","MtvrReammo"]) or you can use the names of the units in the editor (ex: [Taller, RecargadorWest, Caja1]) and the script will convert them in their classname.


Usage:
Place this code in the init line of the unit:
null = [AA, 5, -1, [Taller,ReammoMovil], 250] execVM "AutoReload.sqf";


Script:
// *******************************************************************************
// **       Funcion: AutoReload.sqf
// **   Descripcion: Abastecimiento de armamento a unidades.
// *******************************************************************************
// **         Autor: RAVEN
// **          Site: www.ArmedAssault.com.ar
// **         Forum: http://www.armedassault.com.ar/forums/viewtopic.php?f=9&t=2598
// ** Use: null = [AA, 10, -1, ["KamazReammo","MtvrReammo"], 500] execVM "AutoReload.sqf";
// *******************************************************************************

private ["_veh","_tmpReload","_canReload","_reloader","_dist","_guns",
   "_mags","_list","_vehR","_posV","_posR","_rearm","_px","_py",
   "_listMovil","_listFijo","_tFin","_ULset"];

if (not isServer) exitWith {};

_veh       = _this select 0;
_tmpReload = _this select 1;
_canReload = _this select 2;
_reloader  = _this select 3;
_dist      = _this select 4;

_guns = weapons _veh;
_mags = magazines _veh;

_reloader = [];
{
   if (typeName _x == "OBJECT") then
   {
      _reloader = _reloader + [format ["%1",typeOf _x]];
   }
   else
   {
      _reloader = _reloader + [_x];
   };
} foreach (_this select 3);

_ULset = "logic" createvehiclelocal [0,0,0];
_ULset setpos (position _veh);
_veh disableAi "move";

while {alive _veh} do
{
   waitUntil {_veh distance _ULset < 20};
   _veh disableAi "move";
   waitUntil {not(someAmmo _veh) or not(canfire _veh)};

   if ((_canReload != 0) and (alive _veh)) then
   {
      _list = nearestObjects [_veh, _reloader, _dist];
      sleep 0.5;

      if ((count _list) > 0 and (count _reloader) > 0) then
      {
         _listMovil = []; _listFijo = [];
         {
            if ((canmove _x) and (fuel _x > 0) and not isnull(driver _x) and (speed _x == 0)) then
            {
               _listMovil = _listMovil + [_x];
            }
            else
            {
               _listFijo = _listFijo + [_x];
            };
         } foreach _list;
         _list = [];
         {if (alive _x) then {_list = _list + [_x]}} foreach _listMovil + _listFijo;
         _vehR = _list select 0;
         _posV = position _veh;
         _posR = position _vehR;
         _rearm = true;
         _mov = false;

         _vehR setAmmoCargo 1.00;
         if ((canmove _vehR) and (fuel _vehR) > 0 and not isnull(driver _vehR)) then
         {
            _px = (_posV select 0) + (5 * sin((direction _veh) + 180));
            _py = (_posV select 1) + (5 * cos((direction _veh) + 180));
            _vehR move [_px, _py, 0];
            waitUntil {(_veh distance _vehR < 10) or (not canmove _vehR) or (someAmmo _veh)};
            if (not (alive _vehR) or not (canmove _vehR)) then {_rearm = false};
         }
         else
         {
            if ((canmove _veh) and (fuel _veh) > 0 and not isnull(driver _veh)) then
            {
               _mov = true;
               _px = (_posR select 0) + (5 * sin((direction _vehR) + 180));
               _py = (_posR select 1) + (5 * cos((direction _vehR) + 180));
               _veh enableAi "move";
               _veh move [_px, _py, 0];
               waitUntil {(_veh distance _vehR < 10) or (not canmove _veh) or (someAmmo _veh)};
               if (not (alive _veh) or not (canmove _veh)) then {_rearm = false};
            };
         };

         if (_rearm) then
         {
            _aborta = false; _repet = false;
            while {not _aborta} do
            {
               _veh action ["REARM", _vehR];
               _tFin = (time + _tmpReload);
               while {(time < _tFin) and (alive _veh) and (alive _vehR)} do {sleep 1};
               if ((not(someAmmo _veh) or not(canfire _veh)) and _mov) then
               {
                  _px = ((position _veh) select 0) + (10 * sin(direction _vehR));
                  _py = ((position _veh) select 1) + (10 * cos(direction _vehR));
                  _veh move [_px, _py, 0];
                  _repet = true;
                  waitUntil {unitready _veh};
               } else {_aborta = true};
            };
            _vehR setAmmoCargo 0.00;
            _veh move (position _ULset);
            _vehR move _posR;
            if (_canReload > 0) then {_canReload = _canReload - 1};
         };
      };
   };
   sleep 5;
   _vehR setAmmoCargo 1.00;
};
deletevehicle _ULset;


Notes:
The script can be used to assist any vehicle (tanks, trucks, boats, etc.), but it might need some modifications for aircrafts which could make it impossible to be used in those cases.

The same procedure used by AutoReload.sqf can be used to repair and refuel vehicles. (Possibly added in the future).


Change log:
v1.1
- improved assisting vehicles availability to be more effective.


Forums Topic:
- RavGamers forums (In Spanish)




Enable javascript to be able to download from Armaholic please!



Tags: No tags