Author: Big Dawg KS
Author Website:

Requirements: No addons required

Version: 2.0

Short description: The purpose of this script is to allow AI units to patrol inside of a building.

Date: 2010-06-23 17:07


Comments: (0)
Rating:



 

BD Building Patrol Script
by
Big Dawg KS


Description:
The purpose of this script is to allow AI units to patrol inside of a building. Contrary to popular belief, the ArmA2 AI works very well indoors. This script allows you to set up a path for them to patrol using buildingPos's. When the unit reaches a waypoint, he will wait there for a short period of time before moving on. In addition, (optionally) he will watch a specified direction at that waypoint. The unit will move to the waypoints in order, and will loop when the last waypoint is reached. When the unit becomes alerted they will move more cautiously, and (optionally) will move to a specified position until they resume SAFE mode, at which point they resume the patrol. You can also cease the patrol by passing the script a condition. By ceasing the patrol you can allow the unit to move freely, leaving the building, whatever you want. Once the patrol is ceased the script terminates, so you have to execute it again to resume the patrol afterward.


Parameters::
1. Object - patrolling unit
2. Object - building to patrol
3. Array - buildingPos waypoints;
each waypoint may consist of either just a buildingPos (number), or an array of a buildingPos & direction to face
4. Boolean - true to setPos unit to first WP (used to start unit inside building)
5. Array/Number (Optional) - break to WP(buildingPos)/dir pair, or just a buildingPos, unit will move here when alerted
6. Number (Optional) - probability unit has to crouch after being alerted, otherwise


Examples:
 [soldier1,structure,[[3,20],[4,90]],true] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[5,7,1,4],false,4,0] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[2,[3,15],5],false,[2,10],1] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[[1,45],5,[2,5],3,[4,60]],true,[2,5],0.8,"player in list BLUFORDetected"] execVM "buildPatrol.sqf"; 


Installation:
It comes with a mission as a demo version. Extract the folder to your ArmA2\other Profiles\Yournick\Missions and load it in the editor.


Script:
/* Building Patrol Script v2.0 
Big Dawg KS 
 6/19/10 

Paramaters: 
 1. Object - patroling unit 
 2. Object - building to patrol 
 3. Array - buildingPos waypoints; 
    each waypoint may consist of either just a buildingPos (number), or an array of a buildingPos & direction to face 
 4. Boolean - true to setPos unit to first WP (used to start unit inside building) 
 5. Array/Number (Optional) - break to WP(buildingPos)/dir pair, or just a buildingPos, unit will move here when alerted 
 6. Number (Optional) - probablity unit has to crouch after being alerted, otherwise unit will stand 
 7. String (Optional) - condition for which to cease patrol; exits the script 

Description: 
 Give a unit a path to patrol inside of a building. Path consists of an array of waypoints. A waypoint consists of either a 
 buildingPos index, or an array containing a buildingPos index and a direction. An additional waypoint can be passed 
 that the unit will move to when alerted. Once the unit returns to SAFE, they will resume their patrol. Additional 
 parameters determine whether or not to teleport the unit to their first waypoint (useful to initialize unit on their patrol) 
 and the probability that the unit will crouch when alerted 

Examples: 
 [soldier1,structure,[[3,20],[4,90]],true] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[5,7,1,4],false,4,0] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[2,[3,15],5],false,[2,10],1] execVM "buildPatrol.sqf"; 
 [soldier1,structure,[[1,45],5,[2,5],3,[4,60]],true,[2,5],0.8,"player in list BLUFORDetected"] execVM "buildPatrol.sqf"; 

-----------------------------------------------------------------------------*/ 

// start initializing params 
_unit = _this select 0; 
if(local _unit)then{ 
_building = _this select 1; 
_wps = _this select 2; 
_start = _this select 3; 

// unit will start in the building 
if(_start)then{ 
    _wpp = _wps select 0; 
    if(typeName _wpp == "ARRAY")then{ 
        _unit setPos (_building buildingPos (_wpp select 0)); 
        _unit setDir (_wpp select 1); 
    }else{ 
        _unit setPos (_building buildingPos _wpp); 
    }; 
    _unit setBehaviour "SAFE"; 
}; 

// finish initializing varaibles & params 
_awp = "Null"; 
_crouch = 0.5; 
_cond = "false"; 
if(count _this >= 5)then{_awp = _this select 4}; 
if(count _this >= 6)then{_crouch = _this select 5}; 
if(count _this == 7)then{_cond = _this select 6}; 

// ensure that the condition evaluates to a boolean 
_testCond = { 
    private["_test","_testType"]; 
    _test = call compile _this; 
    _testType = typeName _test; 
    if(format ["%1",_testType] != "BOOL")then{_test = false}; 
    _test 
}; 

_unit setUnitPos "UP"; 
_cwp = 0; 
_pos = _building buildingPos 0; 
_dir = 0; 
while{alive _unit && !(_cond call _testCond)}do{ 

    if(unitPos _unit == "MIDDLE")then{_unit setUnitPos "UP"}; 

    // move to next WP 
    if(count _wps > 1 || !_start)then{ 
        _wpp = _wps select _cwp; 
        _wp = _wpp; 
        _dir = "null"; 
        if(typeName _wpp == "ARRAY")then{ 
            _wp = _wpp select 0; 
            _dir = _wpp select 1; 
        }; 
        _pos =  (_building buildingPos _wp); 
        _unit doMove _pos; 
        _unit doWatch objNull; 
    }; 

    // wait until unit has arrived at WP 
    waitUntil{ 
        ((_unit modelToWorld [0,0,0]) distance _pos <= 1.5) 
|| (behaviour _unit != "SAFE" && format ["%1",_awp] != "Null") || (!alive _unit)||(_cond call _testCond) 
    }; 

    // tell unit to watch specified direction 
    if(format ["%1",_dir] != "null")then{ 
        _watchPos = [(_pos select 0)+(10*sin _dir),(_pos select 1)+(10*cos _dir),_pos select 2]; 
        _unit doWatch _watchPos; 
    }; 

    // break out of patrol if a break-out WP is defined 
    if(alive _unit && behaviour _unit != "SAFE" && format ["%1",_awp] != "Null" && !(_cond call _testCond))
 then { 
        _wp = _awp; 
        _dir = "null"; 
        if(typeName _awp == "ARRAY")then{ 
            _wp = _awp select 0; 
            _dir = _awp select 1; 
        }; 
        _pos = (_building buildingPos _wp); 
        _unit doMove _pos; 
        _unit doWatch objNull; 
        waitUntil{((_unit modelToWorld [0,0,0]) distance _pos <= 1.5) || !(alive _unit)||(_cond call _testCond)}; 

        // if unit returns to SAFE, resume the patrol 
        while{alive _unit && behaviour _unit != "SAFE" && !(_cond call _testCond)}do{ 
            if((_crouch > random 1)&&(unitPos _unit != "MIDDLE"))then{_unit setUnitPos "MIDDLE"}; 
            if(format ["%1",_dir] != "null")then{ 
                _watchPos = [(_pos select 0)+(10*sin _dir),(_pos select 1)+(10*cos _dir),_pos select 2]; 
                _unit doWatch _watchPos; 
            }; 
            sleep (15 + random 25); 
            if(behaviour _unit == "SAFE")then{sleep (7 + random 5)}; 
        }; 
    }; 
    if((_crouch > random 1)&&(unitPos _unit != "MIDDLE")&&(behaviour _unit != "SAFE"))
 then {_unit setUnitPos "MIDDLE"; sleep (5 + random 3)}; 
    sleep (4 + random 13); 
    if(_cwp < ((count _wps)-1))then{_cwp = _cwp + 1}else{_cwp = 0}; 
}; // while alive 

// cease patrol 
if(alive _unit)then{ 
    _unit doFollow (leader group _unit); 
    _unit setUnitPos "AUTO"; 
    _unit doWatch objNull; 
}; 

}; // local unit 


Forum topic:
- BI forums




Enable javascript to be able to download from Armaholic please!