Jump to content
Blitzen88

Need Help Adding a waypoint to the closest Enemy or Uncaptured Sector

Recommended Posts

Greetings everyone,

 

When I first got into Arma @Larrow helped me by writing a script (thread link) which would move a marker to the closest enemy or uncaptured sector.  After fooling around for awhile I took that script snippet and mashed it together with Iceman77's Helicopter reinforcement script to create a helicopter reinforcement script that is compatible with Arma's Spawn AI and AI Sector Tactics Modules.

 

The script, using the Spawn AI Module's expression field, takes spawned AI, places them in a spawned helicopter, drops them off near a sector and then returns back to base.  The script uses the script from @Larrow's script to pick the closest enemy or uncaptured sector.  Although it works 98% of the time I sometimes get an error that derails the script.

 

Here is the script:

 

/*==========================================================================================

				Arma III Helicopter Transport - CSAT

===========================================================================================

* Creates a helicopter which transports infantry to the nearest neutral or enemy sector

* The helicopter's spawn position and return location is the same position as the spawn module

* Call with: _this execVM "Scripts\SpawnAI_HelicopterTransport_CSAT.sqf"

===========================================================================================*/

//Define Variables
params[ "_group", "_module", "_groupData" ];

//Create Helicopter Group and Helicopter
_heloCrew = createGroup East;
_helo = [getPos _module, getDir _module, "O_Heli_Light_02_F", EAST] call BIS_FNC_spawnVehicle;


//Assign the crew to the Helicopter group & assign cargo to the Helicopter
{[_x] joinSilent _heloCrew;} forEach crew (_helo select 0);	
{_x assignAsCargo (_helo select 0); _x moveInCargo (_helo select 0);} forEach units _group;


//Find the Closest Neutral or Enemy Sector
_sectors = +BIS_fnc_moduleSector_sectors apply{ [ _x distanceSqr _module, _x ] };
_sectors sort true;

_index = _sectors findIf {
	_x params[ "", "_sector" ];
	
	_sectorOwner = _sector getVariable[ "owner", sideUnknown ];
	_sectorOwner isEqualTo sideUnknown || _sectorOwner isEqualTo West
};

//Find a flat position around the LZ marker & create an HPad there.
_flatPos = [getPos ( _sectors select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;
_hPad = createVehicle ["Land_HelipadEmpty_F", _flatPos, [], 0, "NONE"];


//Give the helicopter an unload waypoint onto the hpad

    _heloWp = _heloCrew addWaypoint [_hPad, 0];
    _heloWp setWaypointType "TR UNLOAD";
    _heloWp setWaypointBehaviour "CARELESS";
    _heloWp setWaypointCombatMode "BLUE";
    _heloWp setWaypointSpeed "FULL";
    _heloWp setWaypointStatements ["true", "(vehicle this) LAND 'LAND';"];

	
//Wait until the helicopter is touching the ground before ejecting the cargo	
WaitUntil {isTouchingGround (_helo select 0) || {!canMove (_helo select 0)}};


//Eject the Infantry	
{unAssignVehicle _x; _x action ["eject", vehicle _x]; sleep 0.5;} forEach units _group; //Eject the cargo


//Wait Until the infantry group is no longer in the helicopter before assigning a new WP to the helicopter
WaitUntil {{!alive _x || !(_x in (_helo select 0))} count (units _group) == count (units _group)};


//Delete the helipad
{deletevehicle _hPad};

    _heloWp = _heloCrew addWaypoint [getpos _module, 0];
    _heloWp setWaypointType "MOVE";
    _heloWp setWaypointBehaviour "AWARE";
    _heloWp setWaypointCombatMode "BLUE";
    _heloWp setWaypointSpeed "FULL";
    _heloWp setWaypointStatements ["true", "{deleteVehicle _x;} forEach crew (vehicle this) + [vehicle this];"];

//End

The error that I get has something to do with the sector selection process. Here is a picture of the error I get.  

 

Error:

_flatPos = [getPos ( _sectors #select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;
Error Zero Divisor

Anyone have any ideas how to fix this?

Share this post


Link to post
Share on other sites

Maybe an Order of Operations issue, maybe not?  If so, I would think it should occur consistently.

 

What it means is confusion might occur over which command to run first, when multiple commands are in an expression and precedence is not conditioned ( ).  Example:

_flatPos = [getPos ( _sectors select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

// Could get run as:
_flatPos = [getPos ( (_sectors select _index) select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;
// or:
_flatPos = [getPos ( _sectors select (_index select 1) ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

 

Share this post


Link to post
Share on other sites
2 hours ago, opusfmspol said:

Maybe an Order of Operations issue, maybe not?  If so, I would think it should occur consistently.

 

What it means is confusion might occur over which command to run first, when multiple commands are in an expression and precedence is not conditioned ( ).  Example:


_flatPos = [getPos ( _sectors select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

// Could get run as:
_flatPos = [getPos ( (_sectors select _index) select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;
// or:
_flatPos = [getPos ( _sectors select (_index select 1) ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

 

I will definitely try out those suggestions but I dont think its a syntax issue. Like I said, the script works 98% of the time. If it was an issue like that then I would think the script would mess up more often.

 

I was thinking it might be related to a scenario where one side controls all the sectors at once. I think the script “filters” out friendly sectors and only selects uncaptured or enemy sectors. If the friendly side controls all the sectors then there is no where to go. Havent been able to test that yet though

Share this post


Link to post
Share on other sites
8 hours ago, Blitzen88 said:

I was thinking it might be related to a scenario where one side controls all the sectors at once.

 

_index = _sectors findIf {
	_x params[ "", "_sector" ];
	
	_sectorOwner = _sector getVariable[ "owner", sideUnknown ];
	_sectorOwner isEqualTo sideUnknown || _sectorOwner isEqualTo West
};

//Find a flat position around the LZ marker & create an HPad there.
_flatPos = [getPos ( _sectors select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

Yeah, the _index findIf would return -1 as _index when Opfor or Independent holds all.  You could test for that.

 

If that's the case you could exit the script, if that's what you want to happen.  if (_index < 0) exitWith {};

 

Share this post


Link to post
Share on other sites
5 hours ago, opusfmspol said:

 


_index = _sectors findIf {
	_x params[ "", "_sector" ];
	
	_sectorOwner = _sector getVariable[ "owner", sideUnknown ];
	_sectorOwner isEqualTo sideUnknown || _sectorOwner isEqualTo West
};

//Find a flat position around the LZ marker & create an HPad there.
_flatPos = [getPos ( _sectors select _index select 1 ), 0, 550, 10, 0, 0.3, 0] call BIS_fnc_findSafePos;

Yeah, the _index findIf would return -1 as _index when Opfor or Independent holds all.  You could test for that.

 

If that's the case you could exit the script, if that's what you want to happen.  if (_index < 0) exitWith {};

 

 

I was able to test it this morning and it does return an error when the friendly side holds all of the sectors at once.  How can I fix/account for that in the script?  I cant exit the script if that condition is met because units are being spawned by the Spawn AI module and then the script is executing.  Stopping the script from running wont stop the units from spawning.  I would like for the helicopter to still fly off to a sector...I will start looking into that today.

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

×