Bohemia Interactive Forums  

Go Back   Bohemia Interactive Forums > BI MILITARY GAMES FORUMS > ARMA 2 EDITING > ArmA 2 & OA - MISSION EDITING & SCRIPTING

ArmA 2 & OA - MISSION EDITING & SCRIPTING For discussing the technical aspects of creating custom ArmA 2 & the standalone expansion Operation Arrowhead missions as well as scripting.

Reply
 
Thread Tools Display Modes
Old 09-23-2009, 03:56 PM   #1
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
Improved BIS_fnc_taskDefend

Updated to 1.1 - Added in a random repeating delayed waypoint swap between search & destroy and dismissed to confine micro patrol ranges to about 75m and increase overall activity. They will continuously switch between various activities such as sitting down, standing around, going on 1-4 man patrols, or doing an occasional coordinated sweep of their local area.
Updated to 1.2 - Typo fixed which could break the cycle and allow patrols to wander out of range.
Updated to 1.3 - Prevented units from running around while they are not in combat as it looked a bit off.
Updated to 1.3a - Fixed typo which shouldn't effect anything either way.

Alternative version added "BIN_taskDefendLoose.sqf". This one has simplified coding and will re-man defenses after completing each combat. However there is a downside with this script in that individual units may wander quite far from their defense point.

*****

I made some improvements to BIS_fnc_taskDefend. I know a lot of you are using this function as it's convenient so I thought I would share my optimized version as I've been very happy with it's behavior.

Basically more efficient and effective. This version will reliably man all nearby defenses (even buggy spawned BIS compositions) and will additionally send out micro patrols in it's vicinity and otherwise act fairly believably without using much CPU.

When enemies are detected the area is searched and cleared before resuming their normal activities.

This version will also man empty vehicle turrets so you can place vehicles as static defenses. Read the code notes to see how to prevent this behavior if not desired.

*****

Copy and paste this code into a new file called "BIN_taskDefend.sqf" and then put into your mission directory. After, in the mission editor copy the below line into the group leader's init field that you want to do the defending. Make sure he is in the right position.

null = [group this,(getPos this)] execVM "BIN_taskDefend.sqf"

Just use notepad to create this script and make sure you save the file as "BIN_taskDefend.sqf" and not "BIN_taskDefend.sqf.txt". To make sure go to Windows Explorer and then find folder options and see that "Hide extensions for known file types" is NOT checked.

BIN_taskDefend.sqf
Code:
/*
 =======================================================================================================================
Script: BIN_taskDefend.sqf v1.3a
Author(s): Binesi
Partly based on original code by BIS

Description:
Group will man all nearby static defenses and vehicle turrets and guard/patrol the position and surrounding area.

Parameter(s):
_this select 0: group (Group)
_this select 1: defense position (Array)
    
Returns:
Boolean - success flag

Example(s):
null = [group this,(getPos this)] execVM "BIN_taskDefend.sqf"

-----------------------------------------------------------------------------------------------------------------------
Notes:

To prevent this script from manning vehicle turrets find and replace "LandVehicle" with "StaticWeapon".

The "DISMISS" waypoint is nice but bugged in a few ways. The odd hacks in this code are used to force the desired behavior.
The ideal method would be to write a new FSM and I may attempt that in a future project if no one else does. 

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

private ["_grp", "_pos"];
_grp = _this select 0;
_pos = _this select 1;

_grp setBehaviour "SAFE";

private ["_list", "_units","_staticWeapons"];
_list = _pos nearObjects ["LandVehicle", 120];
_units = (units _grp) - [leader _grp]; // The leader should not man defenses
_staticWeapons = [];

// Find all nearby static defenses or vehicles without a gunner
{
    if ((_x emptyPositions "gunner") > 0) then 
    {
        _staticWeapons = _staticWeapons + [_x];    
    };
} forEach _list;

// Have the group man empty static defenses and vehicle turrets
{
    // Are there still units available?
    if ((count _units) > 0) then 
    {
        private ["_unit"];
        _unit = (_units select ((count _units) - 1));
    
        _unit assignAsGunner _x;
        [_unit] orderGetIn true;
        sleep 16; // Give gunner time to get in, otherwise force.
        _unit moveInGunner _x;
            
        _units resize ((count _units) - 1);
    };
} forEach _staticWeapons;

// Setup Waypoints
private ["_wp1","_wp2","_wp3","_wp4"];

_wp1 = _grp addWaypoint [_pos, 0];
_wp1 setWaypointType "MOVE";
[_grp, 1] setWaypointSpeed "LIMITED";
[_grp, 1] setWaypointBehaviour "SAFE";
[_grp, 1] setWaypointCombatMode "YELLOW";
[_grp, 1] setWaypointFormation "STAG COLUMN";
[_grp, 1] setWaypointCompletionRadius 50;
[_grp, 1] setWaypointStatements ["true", "null = [this] spawn {
        _grp = group (_this select 0);
        sleep (30+(random 60));
        {doStop _x} forEach units _grp;
        _grp setCurrentWaypoint [_grp,3];
        {_x doMove getPos _x} forEach units _grp;
        sleep 1;
        {_x forceSpeed 3} forEach units _grp;
        if ((random 3)> 2) then {sleep 3} else {sleep 30 + (random 30)};
        _grp setCurrentWaypoint [_grp, 1];
}; "];

_wp2 = _grp addWaypoint [_pos, 0];
_wp2 setWaypointType "DISMISS";
[_grp, 2] setWaypointStatements ["true", "null = [this] spawn {
        _grp = group (_this select 0);
        {_x forceSpeed -1 } forEach units _grp;
}; "];

_wp3 = _grp addWaypoint [_pos, 0];
_wp3 setWaypointType "SAD";
[_grp, 3] setWaypointSpeed "FULL";
[_grp, 3] setWaypointBehaviour "ALERT";
[_grp, 3] setWaypointCombatMode "RED";
[_grp, 3] setWaypointFormation "VEE";
[_grp, 3] setWaypointCompletionRadius 50;
[_grp, 3] setWaypointStatements ["true", "null = [this] spawn {
        _grp = group (_this select 0);
        _grp setCurrentWaypoint [_grp, 1];
}; "];

_wp4 = _grp addWaypoint [_pos, 0];
_wp4 setWaypointType "CYCLE"; // Redundant failsafe

true
BIN_taskDefendLoose.sqf
Code:
/*
============================================================================================================
Script: BIN_taskDefendLoose.sqf v1.0
Author(s): Binesi
Partly based on original code by BIS

Description:
Group will man all nearby static defenses and vehicle turrets and guard/patrol the position and surrounding area.

Parameter(s):
_this select 0: group (Group)
_this select 1: defense position (Array)
_this select 2: radius (Number)
    
Returns:
Boolean - success flag

Example(s):
null = [group this,(getPos this)] execVM "BIN\BIN_taskDefend.sqf"

------------------------------------------------------------------------------------------------------------
Notes:

To prevent this script from manning vehicle turrets find and replace "LandVehicle" with "StaticWeapon".

Beware that this script uses the "DISMISS" waypoint waypoint which allows some units to wander quite far from their defense position.

============================================================================================================
*/
private ["_grp", "_pos", "_radius", "_list", "_static_weapons", "_units", "_wp1","_wp2","_wp3"];

_grp = _this select 0;
_pos = _this select 1;
_radius = if ((count _this) > 2) then {_this select 2} else {100};
_grp setBehaviour "SAFE";
_list = _pos nearObjects ["LandVehicle", _radius];
_units = (units _grp) - [leader _grp]; // The leader should not man defenses
_static_weapons = [];

// Find all nearby static defenses or vehicles without a gunner
{
    if ((_x emptyPositions "gunner") > 0) then 
    {
        _static_weapons = _static_weapons + [_x];    
    };
} forEach _list;

// Have the group man empty static defenses and vehicle turrets
{
    // Are there still units available?
    if ((count _units) > 0) then 
    {
        private ["_unit"];
        _unit = (_units select ((count _units) - 1));
    
        _unit assignAsGunner _x;
        [_unit] orderGetIn true;
        // Give gunner time to get in, otherwise force.
        _unit spawn { sleep 16; _this moveInGunner _x; };        
        _units resize ((count _units) - 1);
    };
} forEach _static_weapons;

// Setup Waypoints
_wp1 = _grp addWaypoint [_pos, 0];
_wp1 setWaypointType "DISMISS";
[_grp, 1] setWaypointSpeed "LIMITED";
[_grp, 1] setWaypointBehaviour "SAFE";
[_grp, 1] setWaypointCombatMode "YELLOW";
[_grp, 1] setWaypointFormation "STAG COLUMN";
[_grp, 1] setWaypointCompletionRadius 50;
[_grp, 1] setWaypointStatements ["true", "null = [this] spawn
{
    _grp = group (_this select 0);
    _grp setBehaviour 'STEALTH';
    { _x setUnitPos 'DOWN'; } forEach units _grp;
    sleep (2+(random 3));
    { _x setUnitPos 'MIDDLE'; } forEach units _grp;
    sleep (20+(random 40));
    { _x setUnitPos 'AUTO'; } forEach units _grp;
}; "];

_wp2 = _grp addWaypoint [_pos, 0];
_wp2 setWaypointType "SAD";
[_grp, 2] setWaypointSpeed "FULL";
[_grp, 2] setWaypointBehaviour "ALERT";
[_grp, 2] setWaypointCombatMode "RED";
[_grp, 2] setWaypointFormation "VEE";
[_grp, 2] setWaypointCompletionRadius 50;
[_grp, 2] setWaypointStatements ["true", "null = [this] spawn
{
    _grp = group (_this select 0);
    _list = position (_this select 0) nearObjects ['LandVehicle', 120];
    _static_weapons = [];
    {
        if ((_x emptyPositions 'gunner') > 0) then 
        {
            _static_weapons = _static_weapons + [_x];    
        };
    } forEach _list;
    {
        _units = units _grp;
        if ((count _units) > 0) then 
        {
            _unit = (_units select ((count _units) - 1));
            _unit assignAsGunner _x;
            [_unit] orderGetIn true;
            _unit spawn { sleep 16; _this moveInGunner _x; };        
            _units resize ((count _units) - 1);
        };
    } forEach _static_weapons;
}; "];

_wp3 = _grp addWaypoint [_pos, 0];
_wp3 setWaypointType "CYCLE";

true

Last edited by Binesi; 10-11-2009 at 04:48 PM. Reason: More instructions. Updated to 1.3
Binesi is offline   Reply With Quote
Old 09-23-2009, 04:11 PM   #2
TRexian
Master Sergeant
 
Join Date: Jun 2007
Posts: 784
Excellent! Had planned on bastardizing my own version, but you look like you've done much the same thing. Look forward to trying it out!

Hey - do you have an OFPEC tag? Want to name it as "yours."
__________________
-----------<T>----------

Creator of JTD Ambient Civilian Traffic Module (beta ver. 2).
TRexian is offline   Reply With Quote
Old 09-23-2009, 04:49 PM   #3
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
Quote:
Originally Posted by TRexian View Post
Hey - do you have an OFPEC tag? Want to name it as "yours."
No, but good idea. I registered one:

BIN_

Binesi is offline   Reply With Quote
Old 09-23-2009, 06:47 PM   #4
TRexian
Master Sergeant
 
Join Date: Jun 2007
Posts: 784
Hey - you taken a look at the taskPatrol script? I've used it a bit, and while it serves its purpose it also has some room for improvement. The waypoints can end up in a tangled mess, since each one can be at any angle from the prior one. You can have a "patrol" that criss-crosses a relatively small area.

At one time, I had a wp-generation system that basically created a pentagon around a spot, so that it ended up being more of a sequence. Was also planning on doing something like that with the taskPatrol.... unless someone beats me to it....
__________________
-----------<T>----------

Creator of JTD Ambient Civilian Traffic Module (beta ver. 2).
TRexian is offline   Reply With Quote
Old 09-23-2009, 08:47 PM   #5
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
Your pentagon system is interesting TRexian.

I need to give that some thought - how would a human setup patrols? A circular perimeter patrol is what I usually use with centrally located static defense.

I'll take a look at that function as well. It did look a bit rough. It isn't something I've used yet as patrols is an area I usually like to setup on a case by case basis or use Kronzky's UPS (although the later is a bit CPU heavy).

I can see some potential for using extra intelligence like checking height and going to high ground and trying to stay near cover. Also something that would work better for air units would be nice.
Binesi is offline   Reply With Quote
Old 09-23-2009, 08:48 PM   #6
MattXR
First Lieutenant
 
MattXR's Avatar
 
Join Date: Jan 2005
Location: United Kingdom
Posts: 5,836
Awsome thanks for this!
__________________



Coming Soon for OA: Desert Stronghold, Desert Rats, Desert Rats II, Desert Wolves, Desert Dogs
MattXR is offline   Reply With Quote
Old 09-24-2009, 06:46 PM   #7
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
I finished up a first draft of the patrol version here.
Binesi is offline   Reply With Quote
Old 09-25-2009, 01:14 AM   #8
Rommel
Master Gunnery Sergeant
 
Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 1,293
Never ever use Dismiss. It has no restriction on how far units travel.

You will find 1, 2 man groups up to 4km away from that position if you leave it long enough (ie more than 10minutes).
__________________

Australian Armed Forces - Dedicated Tactical Coop Squad for ArmA2
Rommel is offline   Reply With Quote
Old 09-25-2009, 01:39 AM   #9
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
Quote:
Originally Posted by Rommel View Post
You will find 1, 2 man groups up to 4km away from that position if you leave it long enough (ie more than 10minutes).
Are you sure that this still happens in ArmaA 2? As much as I've watched this script they usually turn back toward center after getting about 100m out. Not saying it *never* happens, but I have yet to see such an extreme example.

For my purposes I like the effect of the small area patrols. It's a cheap way to get a bit more realism and non-predictability which would otherwise require more scripting overhead.

Last edited by Binesi; 09-25-2009 at 07:21 AM.
Binesi is offline   Reply With Quote
Old 09-25-2009, 07:23 AM   #10
Binesi
Corporal
 
Join Date: Jul 2009
Posts: 76
Just in case this problem with a dismissed waypoint still happens and to improve the script I've written in a waypoint reset (check first post for update). This keeps patrols within about 75m of center and increases the overall activity of the group for some added realism.

Last edited by Binesi; 09-25-2009 at 07:34 AM.
Binesi is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:34 AM.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.