Jump to content
Tankbuster

set a unit to look out of window function?

Recommended Posts

I want to be able to place AI in buildings and have them look out of the nearest window or door.

I'm creating units within buildings but they all face north. I've made them face the position of the building which at least stops most of them staring at walls, although it only seems to work reliably with units on the ground floor.

I fiddled with lineintersects and random directions to try to get the unit to look in a direction where his eyeline is least obstructed, ie, out of a window or door, but it was too cpu intensive.

Does anyone have any thoughts?

Share this post


Link to post
Share on other sites

Is it even possible to detect window positions, if it can't I can't see it happening unless you go into the building and log the position.

Share this post


Link to post
Share on other sites

^What he says - you need to work off the existing building positions or create arrays of new buiding positions for each building. Do you have that building position script from Arma2 that places helper objects at all the building positions? I used existing positions and this worked well:

/*
Requirements: Opfor player on map - probability of presence0%

examples - [building type,[window positions]]
_array = ["Land_i_House_Big_01_V1_F",[3,5,6,7,9]];
_array = ["Land_i_House_Small_03_V1_F",[1,2,3,4,5]];
*/
_array = ["Land_i_House_Big_02_V1_F",[2,5,7]];

_bldType = _array select 0;
_bldPosArr = _array select 1;

_nBld = nearestObject [player, _bldType];
_bldLoc = getPosATL _nBld;

for "_i" from 0 to (count _bldPosArr)-1 do
{
_bldPos = ((_bldPosArr select _i)-1);

_grp = createGroup east;
_unit = _grp createUnit ["O_Soldier_F", [0,0,0], [], 0, "CAN_COLLIDE"];
_unit setPosATL (_nBld buildingPos _bldPos);
_unit setDir (([_unit, _bldLoc] call BIS_fnc_dirTo) + 180);
_unit setUnitPos "UP";
sleep 1;
};

If you setup the player with

this allowDamage false;

you get sprayed with fire from the windows using the above. But you will have to build a database of arrays to get it to work.

The other way that I have used successfully in the past is to get AI squad members to move randomly to different positions in the building. It's less predictable and more lifelike. They appear at windows and doors when you don't expect it.

Edited by Mattar_Tharkari

Share this post


Link to post
Share on other sites

Here you go mate - just worked this out for you :)

private ["_model_pos","_world_pos","_armor","_cfg_entry","_veh","_house","_window_pos_arr","_cfgHitPoints","_cfgDestEff","_brokenGlass","_selection_name"];

_house = nearestObject [player , "House"];

hint format ["%1", _house];

_window_pos_arr = [];

_cfgHitPoints = (configFile >> "cfgVehicles" >> (typeOf _house) >> "HitPoints");

for "_i" from 0 to count _cfgHitPoints - 1 do 
{
_cfg_entry = _cfgHitPoints select _i;

if (isClass _cfg_entry) then
{
	_armor = getNumber (_cfg_entry / "armor");

	if (_armor < 0.5) then
	{
		_cfgDestEff = (_cfg_entry / "DestructionEffects");
		_brokenGlass = _cfgDestEff select 0;
		_selection_name = getText (_brokenGlass / "position");
		_model_pos = _house selectionPosition _selection_name;
		_world_pos = _house modelToWorld _model_pos;
		_window_pos_arr set [count _window_pos_arr, _world_pos];
	};
};
}; 

{ 
_veh = createVehicle ["Sign_Sphere100cm_F", _x, [], 0, "none"];
_veh setObjectTexture [0,'#(argb,8,8,3)color(0.55,0,0.1,1)'];
_veh setPosATL _x;     
} foreach _window_pos_arr;

It looks like this:

https://dl.dropboxusercontent.com/u/101800212/arma3%202013-11-23%2023-25-33-33.png

It's quite basic, but I'm sure you can modify it for your needs. At the moment it doesn't detect armoured glass (you can just increase the number that _armor is compared to).

Also, it doesn't currently filter for the size of window (but you could look at the radius config parameter to check this). Plus, it doesn't discriminate against height of window (so it returns windows that are mounted too high to shoot through).

I guess you could compare the eyePos of your AI units to the ASL of the windows position so they only look through windows at approx head height.

Let me know how you get on with it.

EDIT:

This detects doors and hatches as well:

private ["_model_pos","_world_pos","_armor","_cfg_entry","_veh","_house","_window_pos_arr","_cfgHitPoints","_cfgDestEff","_brokenGlass","_selection_name","_display_name","_anim_source_pos_arr","_cfgUserActions"];

_house = nearestObject [player , "House"];

hint format ["%1", _house];

_window_pos_arr = [];
_anim_source_pos_arr = [];

_cfgHitPoints = (configFile >> "cfgVehicles" >> (typeOf _house) >> "HitPoints");

for "_i" from 0 to count _cfgHitPoints - 1 do 
{
_cfg_entry = _cfgHitPoints select _i;

if (isClass _cfg_entry) then
{
	_armor = getNumber (_cfg_entry / "armor");

	if (_armor < 10) then
	{
		_cfgDestEff = (_cfg_entry / "DestructionEffects");
		_brokenGlass = _cfgDestEff select 0;
		_selection_name = getText (_brokenGlass / "position");
		_model_pos = _house selectionPosition _selection_name;
		_world_pos = _house modelToWorld _model_pos;
		_window_pos_arr set [count _window_pos_arr, _world_pos];
	};
};
}; 

_cfgUserActions = (configFile >> "cfgVehicles" >> (typeOf _house) >> "UserActions");

for "_i" from 0 to count _cfgUserActions - 1 do 
{
_cfg_entry = _cfgUserActions select _i;

if (isClass _cfg_entry) then
{
	_display_name = getText (_cfg_entry / "displayname");
	if (_display_name == "Open hatch" or {_display_name == "Open door"}) then
	{
		_selection_name = getText (_cfg_entry / "position");
		_model_pos = _house selectionPosition _selection_name;
		_world_pos = _house modelToWorld _model_pos;
		_anim_source_pos_arr set [count _anim_source_pos_arr, _world_pos];
	};
};
};

{ 
_veh = createVehicle ["Sign_Sphere25cm_F", _x, [], 0, "none"];
_veh setObjectTexture [0,'#(argb,8,8,3)color(0.7,0,0.1,1)'];
_veh setPosATL _x;     
} foreach _window_pos_arr;

{ 
_veh = createVehicle ["Sign_Sphere25cm_F", _x, [], 0, "none"];
_veh setObjectTexture [0,'#(argb,8,8,3)color(0.1,0,0.55,1)'];
_veh setPosATL _x;     
} foreach _anim_source_pos_arr;

Picture.

Having problems finding out how to detect glass in mil buildings. The selections definitely exist, but no reference to them in the config as far as I can see. Maybe when BIS gutted the map objects, these were a casualty as well. Will continue to investigate though.

Edited by Das Attorney

Share this post


Link to post
Share on other sites

Holy cow, guys! I really thought this was going to be completely unachievable!

I've still been wrestling with a slowly rotating unit and playing objects 6 meters in front of him as he turns and using lineintersects to see if he could see the object, but it was dreadfully slow and quite tough on the CPU. I was resigned to manually recceying each building and gathering a database of locations and directions.

So to be given a wodge of code to do it automatically is brilliant. Thank you so much. I'll plug it into my mission and scripts later and report back.

---------- Post added at 12:24 ---------- Previous post was at 12:22 ----------

Weirdly, Das, I can never get nearestbuilding to pick up the control tower. :) Even standing in it and doing a nearestbuilding returns the office building next door. LOL.

Edited by Tankbuster

Share this post


Link to post
Share on other sites

Tankbuster, did you get this thing working ??? iam trying to do the same thing, maybe you got any pointers ?

Share this post


Link to post
Share on other sites

I admit I have not got it working because I haven't tried it yet.:o

Share this post


Link to post
Share on other sites

i did manage to get my MG looking away from the building by doing this:

first i spawn my units on pre choosen locations in the building.. then i spawn the MG facing towards the center of the building.

Then i spawn in my gunner. setting his watch direction 180 degres from the MG direction (wich is turned towards the center of the building

_grp = createGroup EAST;

_buildposarray = (_buildsel select 1);

_ranbuildpos = _buildposarray select (floor(random count _buildposarray));

_buildpos = _build buildingpos _ranbuildpos;

//Spawn MG

_gunsel = _gunlist call BIS_fnc_selectRandom;

_mgun = createVehicle [_gunsel, (_buildpos), [], 0, "NONE"];

_centerpos = nearestObject [_build,(_buildsel select 0)];

_currentbuild = getpos _centerpos;

_mgun setposatl _buildpos;

_Watchdir = (([_mgun, _currentbuild] call BIS_fnc_relativeDirTo));

_mgun setDir _Watchdir;

_mgun setposatl _buildpos;

_MG = _MG + [_mgun];

//spawn Unit for MG

_mansel = _menlist call BIS_fnc_selectRandom;

_mgman = _grp createUnit [_mansel,_buildpos, [], 0, "NONE"];

_mgman moveingunner _mgun;

_mgman setFormDir ((getdir _mgun) + 180);

Credits to ghost for the orignial script (ghst_roofmgs.sqf)..

now i always get my MG facing outwards from the buildings... woorks good on Roof positions.... and note, i have to spawn each gunner as a seperate group otherwise it wont work...

Maybe someone else has had the same problem..

Edited by Hansson0728

Share this post


Link to post
Share on other sites

I've finally had a chance to use this code and plug it into my mission and have come up against a number of problems.

Firstly, the majority of buildings in the towns don't have glass windows or opening doors, so DA's excellent script doesn't work there. The buildings that are classname land _i_* work fine plus the office and military buildings.

But for civilian buildings (where I wanted my CQB to happen) there are addon buildings that are (not mods) garages, verandahs and lean-tos. None of these have windows or doors. Furthermore, all of the damaged buildings, the unfinished ones and the land_d_* and land_i_* have no doors or glass windows either, so the script doesn't work for them.

This isn't DA's fault, he made a script that worked as advertised. It's brilliant and I'm grateful, but because I didn't ask quite the right question, it doesn't quite cover all the bases.

There's more problems when this is plugged into a mission. When you create troops to put in these buildings and make them turn to look at the windows and doors the script finds, the setdir is ignored by most of the troops because they all follow the direction of the group leader. I could make a load of 1 man groups for the blokes, but there's an engine limit of 144 groups so I'd worry about hitting that.

Also, if they do look out of the windows, the moment the AI see you coming, they go prone and then they either shoot into the wall below the window, or they glitch through the wall.

Lastly, AI that spawn on balconies look inwards not outwards.

So, I'd like to again thank DA for a lovely script that I couldn't have written in a million years, but I have to admit defeat because of engine limitations.

Share this post


Link to post
Share on other sites

"setdir is ignored by most of the troops because they all follow the direction of the group leader."

Not if you doStop them, which separates them from their formation, so you can handle them separate.

You'll need to freeze them with _x disableAI "MOVE"; _x setUnitPos "UP"; so that they won't go prone.

I am definetely sure that what you are after is achievable, although it does need some precious FSM scripting. I am saying this because I myself have been thinking on this for a long time, and got some parts working. I'll let you know if I am at this point in my mission and got it working. :)

The window part probably saved me 2 months of swearing, so giga thanks for that!!

Share this post


Link to post
Share on other sites
"setdir is ignored by most of the troops because they all follow the direction of the group leader."

Not if you doStop them, which separates them from their formation, so you can handle them separate.

You'll need to freeze them with _x disableAI "MOVE"; _x setUnitPos "UP"; so that they won't go prone.

I am definetely sure that what you are after is achievable, although it does need some precious FSM scripting. I am saying this because I myself have been thinking on this for a long time, and got some parts working. I'll let you know if I am at this point in my mission and got it working. :)

The window part probably saved me 2 months of swearing, so giga thanks for that!!

Holy cow, really? It is doable? I spent two days on this and was gutted when I came to the conclusion it wasn't going to happen.

The other issues remain, of course. In most of the towns, less than half of the buildings can use DA's script, but it's a start, I guess. :)

Share this post


Link to post
Share on other sites

Do you place them on-the-fly (time sensitive calculations) or at init (you get all the time you need)?

I've been working on a part of a script, which finds walls (I didn't want my men to go too close to a wall). My trouble was windows. :) So we can turn that around and find the holes in the walls. I went from outside to inside, checking lineintersectswith house with nearestbuilding in 1meter height. Where it returned the house, those were the walls.

To turn it around we somehow need from center of the house to outside, and use house longestdimension length line and checks if it intersects with the house in chest height (1.4m?) if not, there is an opening in the house. This is a rough mashup of an idea, but it can start you at least.

Plus the closed doors and the windows by DasAttorneys script.

Then make an FSM for the units to be able to use this information. This is not a 2 day, but like a 2 week project I guess if you want to make it work flawlessly.

Share this post


Link to post
Share on other sites

They are created at the start of a CQB secondary operation so time is not of the essence. I too was fiddling with lineintersects and had it working more or less. As you say, this is a big project and as it's actually quite a small part of my larger mission, I'm not sure it should have this much resources put into it.

I think, for now at least, I'm going to use DA's script. I'm passing a house to it and creating the spheres. I create them locally (only on the server) so players won't see them and then get the AI to setdir towards the nearest one. If the distance is more than 10m or not found then I'll assume the sphere it's finding is in a different building and setdir the unit 180 from the centre of the building.

Share this post


Link to post
Share on other sites

@Tankbuster, I am glad you made this request. Its always worth it when knowledge is shared like here. Thanks to all the inputs. I will be trying some of these scripts myself

Share this post


Link to post
Share on other sites

Not if you doStop them, which separates them from their formation, so you can handle them separate.

You'll need to freeze them with _x disableAI "MOVE"; _x setUnitPos "UP"; so that they won't go prone.

!

This is where I am. I've tested doStop and it works as you said. It 'decouples' the units from formation so they can be given individual setdirs. This works - I've seen all the AI standing in the rooms looking towards the windows. :)

They still go prone when they see enemy though. Sometimes, they go prone and their guns clips through the wall below thw window and they actually shoot through it. I'm not sure disableAI move is the way forward, because this means they'll stare out of the window while players bundle in the door and shoot them in the arse. Fun? Yes. Authentic. No. :) Ive not yet experinented with setunitpos. I'll have a bash with that in a while and report back.

Share this post


Link to post
Share on other sites

I'm having too many problems with this and inconsistent results so I'm moving this down the priority list and moving on. :(

Share this post


Link to post
Share on other sites

@DASAttorney, is there a way to use closed windows with glass like a lot of Zargabad buildings have?

Share this post


Link to post
Share on other sites
I'm having too many problems with this and inconsistent results so I'm moving this down the priority list and moving on. :(

Yes, this was foreseeable... Another normal day in the Arma office. There is no impossible if you have 7000 hours to figure it out how. :) I'll report back if I have something useable.

About unit staying at the window: I guess we'll need to find a method where they stay up but still can rotate. This way they will be able to fire out of the windows in a better radius and turn for the player if he enters the house. I don't think they'll need to move, only rotate in place: inside CQB is over in 2 secs anyways.

Share this post


Link to post
Share on other sites

Tankbuster: its easier than I expected. A simple

_unit setUnitPos "UP";

doStop _unit;

does the trick. Unit can rotate, and stays up (they seem to be tracking you so they wait facing the door direction when you enter the house). No need for additional FSM imho. Yes, its kinda rigid and stupid but if you need more immersive AI you need to come up with a good story instead: like all soliders are robots, or braindead from a recent chemattack. :D

I made some initial tests, but I think a simple buildingPosition check is enough. I will make a circle poll with eg. 36 pieces (every 10 degrees) 10 m long rays on like 1.5 m height from bpos.

If there is roof above (buildingpos select 2 and buildingpos select 2 + 20 intersectswith house) AND there is one ray in the circle that does not intersectswith house, then it is a windowed buildingpos.

This + DA"s genial script should be enough. It is possible to find ALL holes on the building, but there is so little gain from it and soo much work of completely scanning a house interior it just doesn't worth it for randomly placing some windowambushes in a city.

Share this post


Link to post
Share on other sites
Tankbuster: its easier than I expected. A simple

_unit setUnitPos "UP";

doStop _unit;

I can't make it all work. Even after a dostop, the units ignore setform dir and if I use setdir, the briefly turn to the dir I give them, but then turn back to where they were before. doWatch is much better though and is working great. Now to find the windows and doors in houses that DA's script can't use.

I thought of the intersects method to sense if a unit is outside. This works great for units on balconies who face inwards to the nearest window where it would be much better if they faced outwards.

Share this post


Link to post
Share on other sites

Yes, they can turn around, but will spot you as a normal outside unit and face you then. By disableAI move, you completely disable their rotation, but that's unwanted I guess.

Share this post


Link to post
Share on other sites

We've made some progress, albeit limited to the types of buildings that DA's script can use. I'll post a YT later.

Share this post


Link to post
Share on other sites
@DASAttorney, is there a way to use closed windows with glass like a lot of Zargabad buildings have?

Maybe - I don't have A2 installed anymore , but will see if possible.

This is a very interesting project you have. You plan to release it as a mod?

Yes - won't be for a while but definitely something is on it's way.

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

×