Jump to content
Sign in to follow this  
Big Dawg KS

BD Building Patrol Script v2.0

Recommended Posts

I don't think I released version 1.0 for ArmA, but I recently revised this script for use in ArmA2 and figured some of you might appreciate it.

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.

Here's the 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

You can download it now at Armaholic (includes demo mission):

BD Building Patrol Script

Enjoy.

Edited by Big Dawg KS
Fixed small typo in the code

Share this post


Link to post
Share on other sites

Here's a nice demo mission for you guys to see how it works.

(download in first post)

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites

Isn't this doing more or less what Tophe's Random House Patrol script is doing?

I guess your posting this because of the talk in the OA thread. The talk was about AI patrolling buildings on their own :)

Nice script though :)

Share this post


Link to post
Share on other sites
Isn't this doing more or less what Tophe's Random House Patrol script is doing?

I dunno what his script does. The whole point of this script is not to be random. Why? Because that's what I needed. This script is useful for people who want to arrange AI in a building the way they want, so you can get a lot more meaning than it just being random. Good for missions where you want your enemy to hold/patrol a specific structure in a well-thought-out manner. Check out the demo mission if you want to see it in action (if the d/l link is still active, I'm waiting on another so be patient).

Of course, if you want you can easily write another script to call this with random waypoints (or even random buildings). As I see it, making it random would make it geared more towards use large buit-up areas with lots of enterable structures, which isn't what I wrote this for, but it certainly doesn't mean you can't adapt it for that.

Also, from a technical standpoint this script has a lot of interesting features. I figured those who were new to scripting could use this to learn some new tricks.

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites

Random House Patrol script simply makes units random patrol a defined buildings positions.

Like i said it's a nice script you have written there :cool:

Share this post


Link to post
Share on other sites
Random House Patrol script simply makes units random patrol a defined buildings positions.

I see. Well then you can think of this as a more advanced version of that, with more options.

Share this post


Link to post
Share on other sites

Looks handy.

Indeed, it is a different flavor than the random house patrol, which can also be useful.

Share this post


Link to post
Share on other sites

I put up a download link to Armaholic but I'm not sure what it contains (whether it's just the script or the demo mission).

Can someone tell me? :o

Share this post


Link to post
Share on other sites
Guest
I put up a download link to Armaholic but I'm not sure what it contains (whether it's just the script or the demo mission).

Can someone tell me? :o

Its the script "patrol.sqf" together with an example mission. That is correct right?

Share this post


Link to post
Share on other sites

Saw this on Armaholic, so I just had to take a look since I did another script like this.

This seems like a great complement to my random script. Good job.

In my script I automated the building selection and counting of building pos's. Might be an idea for an update in your version. Instead of having the user first define a building, putting it in a variable and then pass it to the script, the script simply chooses the building closest to the unit. You could have an option to do that or use a user defined building. Just an idea to make it quicker and easier to implement.

Have you found any workaround for the missing building positions or undefined buildings in Arma 2?

Share this post


Link to post
Share on other sites
Its the script "patrol.sqf" together with an example mission. That is correct right?

Thanks. Yea that should be fine.

---------- Post added at 03:40 PM ---------- Previous post was at 03:34 PM ----------

In my script I automated the building selection and counting of building pos's. Might be an idea for an update in your version. Instead of having the user first define a building, putting it in a variable and then pass it to the script, the script simply chooses the building closest to the unit. You could have an option to do that or use a user defined building. Just an idea to make it quicker and easier to implement.

Well, like I mentioned earlier, I didn't design it to be easy or automated. I designed it to be flexible. I guess this would be better for more advanced mission makers. Like I already mentioned, all you need is another script that automatically finds a building and generates a random set of waypoints, then call my script from it.

Have you found any workaround for the missing building positions or undefined buildings in Arma 2?

No, I'm not even sure what the problem is. I'm not doing any checking for valid building positions or anything in my script. I wrote it for my own use, so I guess I figured I'd never give it any invalid input. :rolleyes:

Share this post


Link to post
Share on other sites
I wrote it for my own use, so I guess I figured I'd never give it any invalid input. :rolleyes:

hehe

You are a much better scripter than I am, then. :D

I find myself passing invalid data to my own scripts far more frequently than I like!

But, that is a digression....

Share this post


Link to post
Share on other sites

Well, there really is no point in adding any error checking. If the user doesn't already know what he's doing, they're not going to get the script to work. And if they do pass it invalid input, I'd prefer the ArmA2 interpreter warn them when they test their mission then simply have it not do anything.

Share this post


Link to post
Share on other sites

Big Dawg, I'm confused about what you're using for the second parameter, i.e. the building to patrol.

How are you identifying specific buildings without using their numeric IDs? I saw that in your example mission, the industrial building was referred to as insBldg in your script, so I did a quick Google search for this variable but came up with nothing.

If this is a silly question, I apologise for digging up an old thread; if not, I hope it helps others out. Your script sounds really powerful - thank you for sharing it! I'm definitely very eager to utilise it.

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
Sign in to follow this  

×