Jump to content
Jayd33

Enemy AI using artillery.

Recommended Posts

I'm making a mission with the friendlies as Bluefor.

At a certain point if the Bluefor squad enters a zone, a trigger needs to go off so the Opfor shoot artillery towards the squad. It might be real artillery of virtual.

I've read the forums all over, I can get the Bluefor to us artillery by using communications etc, but nothing about the enemy using artillery.

I'm very new to all of this, but thx in advance for a reply.

rgds,

Jayd33

Share this post


Link to post
Share on other sites

You can use commandArtilleryFire.

Put down an enemy mortar and a marker and give them names. Then from a script you can do this;

mortarName commandArtilleryFire [
				markerpos "MortarMarker",                      
				getArtilleryAmmo [mortarName] select 0,  
				5                             
			];

That will have the mortar fire 5 rounds at a rate of about 1 a second, and they'll drop pretty much on top of the marker. If you want the impact area to be a bit more random you can do the following (taken from the Infantry Showcase mission):


for "_i" from 0 to 5 do {  [color="#800000"]//--- 5 = how many rounds you want fired[/color]

_mortar = mortarName;                [color="#800000"]//--- name of the mortar[/color]
_center = markerPos "MortarMarker";  [color="#800000"]//--- central point around which the mortar rounds will hit[/color]
_radius = 100;                      [color="#800000"] //--- random radius from the center[/color]

_pos = [
(_center select 0) - _radius + (2 * random _radius),
(_center select 1) - _radius + (2 * random _radius),
0
];

_mortar commandArtilleryFire [
				_pos,
				getArtilleryAmmo [_mortar] select 0,
				1
			];

sleep 3; [color="#800000"]//--- delay between rounds[/color]

};

Edited by 2nd Ranger
  • Thanks 1

Share this post


Link to post
Share on other sites

As 2nd Ranger said, commandArtilleryFire or doArtilleryFire work nicely. I implemented this yesterday using the latter command.

On a somewhat related note, I also have a question: Is it possible to tell the AI mortar to switch the ranging mode? If the target position for the artillery barrage lies too far away, they won't fire--but it's only too far away if you are in the first two distance modes (close and medium or some such). If you are in "Far" mode, you can easily reach the target position. But I'm not sure if I can tell the AI to use that option. I can however do it manually if I get in the artillery piece and try to fire it myself. It seems like the AI defaults to Medium range, and refuses to fire at anything that would lay within the "Far" min/max ranges. Though it's possible it's just hard coded somewhere and impossible to change. I thought maybe I had to use a different type of ammunition, but it seems there is only one type of 82mm HE round.

Share this post


Link to post
Share on other sites

So would you just give the script a name, like mortar.sqf and use mortarName execVM "mortar.sqf"; in a trigger?

Share this post


Link to post
Share on other sites

Tried ranger's method out, as I was browsing the forums and thought, hmm this be a good idea for a mission I am doing. Tried it in-game, and to no luck. Of course changing names around, and throwing in a hint in the script to make sure its even working, and hint went off. But rounds never came down on the marker.

Do the enemy AI mortars need to be a certain x distance away? For friendly mortars, using support module, it almost doesn't seem to matter from what I found, but getting enemy AI to fire, I tried various distance's, and to no luck.

Share this post


Link to post
Share on other sites
Tried ranger's method out, as I was browsing the forums and thought, hmm this be a good idea for a mission I am doing. Tried it in-game, and to no luck. Of course changing names around, and throwing in a hint in the script to make sure its even working, and hint went off. But rounds never came down on the marker.

Do the enemy AI mortars need to be a certain x distance away? For friendly mortars, using support module, it almost doesn't seem to matter from what I found, but getting enemy AI to fire, I tried various distance's, and to no luck.

The default range of the AI mortars is fairly low I found. While they can hit me from about the distance of half of the airfield (i.e. them at one end and me in the middle), they cannot hit me from one end of the airfield if I am at the opposite end. I have yet to figure out a solution to this, because I know the mortars themselves can fire that distance if you set the range to "far".

P.S. - Add this bit of code to determine if the AI are in fact capable of reaching the target location.

if(_pos inRangeOfArtillery [[_mortar], "8Rnd_82mm_Mo_shells"]) then {
   hint "Target in range! Firing...";
} else {
   hint "Target out of range.";
};

Edited by MH6
  • Like 1

Share this post


Link to post
Share on other sites
You can use commandArtilleryFire.

Put down an enemy mortar and a marker and give them names. Then from a script you can do this;

mortarName commandArtilleryFire [
				markerpos "MortarMarker",                      
				getArtilleryAmmo [mortarName] select 0,  
				5                             
			];

That will have the mortar fire 5 rounds at a rate of about 1 a second, and they'll drop pretty much on top of the marker. If you want the impact area to be a bit more random you can do the following (taken from the Infantry Showcase mission):


for "_i" from 0 to 5 do {  [color="#800000"]//--- 5 = how many rounds you want fired[/color]

_mortar = mortarName;                [color="#800000"]//--- name of the mortar[/color]
_center = markerPos "MortarMarker";  [color="#800000"]//--- central point around which the mortar rounds will hit[/color]
_radius = 100;                      [color="#800000"] //--- random radius from the center[/color]

_pos = [
(_center select 0) - _radius + (2 * random _radius),
(_center select 1) - _radius + (2 * random _radius),
0
];

[b]_mortar[/b] commandArtilleryFire [
				_pos,
				getArtilleryAmmo [[b]_mortar[/b]] select 0,
				1
			];

sleep 3; [color="#800000"]//--- delay between rounds[/color]

};

If you can't get that part working it's just the calling of the variable that will fix it. The "_mortar" in the commandArtilleryFire function where named "mortar".

I changed mine up a little bit for more randomness on its targets. But here's a step by step for anyone who can't figure it out.

1. Make some mortar units, 1-3 don't matter name them all, mine are.

mortar1, mortar2, mortar 3

I'm using multiple placed at different angles to give more confusion as to where the mortar is coming from.

2. Place a trigger, set type to "Switch" I'm using radio alpha as my trigger. On your action field add:

script=[] execVM "mortar.sqf";

3. Make some friendly units, I made 3 and named them

unit1, unit2, unit3

4. Create a mortar.sqf file in your mission folder. Inside of it place either the above code from ranger, or if you want to shoot at multiple units from various mortars try mine out:

for "_i" from 10 to 15 do {  //--- set range of how many cycles you want the mortars to run


_mortar = [mortar1, mortar2, mortar3] call BIS_fnc_selectRandom;    //--- selects a random mortar unit out of array
_uni = [unit1, unit2, unit3] call BIS_fnc_selectRandom;				//--- selects a unit to target the round at
_center = getpos _uni;  											//--- central point around which the mortar rounds will hit
_radius = 25;                       								//--- random radius from the center turn lower for closer hits

_pos = [
(_center select 0) - _radius + (2 * random _radius),
(_center select 1) - _radius + (2 * random _radius),
0
];


_mortar commandArtilleryFire [
				_pos,
				getArtilleryAmmo [_mortar] select 0,
				1 //--- number of shots per turn per unit
			];							


sleep 1; //--- delay between rounds

};

5. Just plug in your unit names or mortar names and try it out.

I did test out a few of the other rounds listed in the config, but none seemed to work other then the stock. In the future when they are fixed, this should allow you to use the other rounds.

_
mortar commandArtilleryFire [
				_pos,
				"8Rnd_82mm_Mo_Flare_white", //---- Flares as example, its possible I missed something, I only tried the 82mm variants
				1 
			];			

Edited by Camaris
  • Like 1

Share this post


Link to post
Share on other sites

Now your's works like a charm Camaris, thank you! Love how its even more random too. Cheers mate

  • Like 1

Share this post


Link to post
Share on other sites

Thanks , works great !

one thing ...

my units spotted the morta every time after the splash.... in a radius of 2.5 k .... even with no line of sight .... can i stop that ?

  • Like 1

Share this post


Link to post
Share on other sites
Thanks , works great !

one thing ...

my units spotted the morta every time after the splash.... in a radius of 2.5 k .... even with no line of sight .... can i stop that ?

yup, I had this too. Almost like terrain didn't exist, they knew where the mortar was after the splashes. Up to about 2km away.

  • Like 1

Share this post


Link to post
Share on other sites

I'm trying to adapt the mortar scripts in this thread for a multiplayer mission, but i'm stuck with a couple of problems that i hope you can help me to solve.

The first script above is nice for firing on a marker position, but my problem with it is that i'd need more markers with a trigger set to each marker to rig a whole area with mortar zones.

The second script above will target a random target even if its far away from the mortar zone, making it impossible to use it in a multiplayer mission with respawn.

Can anyone help me with this?

The best thing would be the second solution, where the mortar targets a bluefor guy entering the zone for a volley of shots with a radius dispersion.

Share this post


Link to post
Share on other sites
yup, I had this too. Almost like terrain didn't exist, they knew where the mortar was after the splashes. Up to about 2km away.

If anything, use setCaptive on the mortars.

Share this post


Link to post
Share on other sites

for "_i" from 10 to 15 do {  //--- set range of how many cycles you want the mortars to run 


   _mortar = [mortar1, mortar2, mortar3] call BIS_fnc_selectRandom;    //--- selects a random mortar unit out of array 
   _uni = [unit1, unit2, unit3] call BIS_fnc_selectRandom;                //--- selects a unit to target the round at 
   _center = getpos _uni;                                              //--- central point around which the mortar rounds will hit 
   _radius = 25;                                                       //--- random radius from the center turn lower for closer hits 

_pos = [ 
   (_center select 0) - _radius + (2 * random _radius), 
   (_center select 1) - _radius + (2 * random _radius), 
   0 
   ]; 


_mortar commandArtilleryFire [ 
                   _pos, 
                   getArtilleryAmmo [_mortar] select 0, 
                   1 //--- number of shots per turn per unit 
               ];                             


sleep 1; //--- delay between rounds 

};

This is awesome so thanks for that. One question. What is or how long is a cycle?

for "_i" from 10 to 15 do {  //--- set range of how many cycles you want the mortars to run 

Share this post


Link to post
Share on other sites

Repeatable Trigger

Name: "triggerone"

BLUFOR detected by OFPOR

on Act: script=[] execVM "mortar.sqf";

mortar.sqf:

_mortar = [mortar1,mortar2] call BIS_fnc_selectRandom;    //--- selects a random mortar unit out of array
//---   find random unit inside trigger
_triglist = list triggerone;
_uni = _triglist call BIS_fnc_selectRandom; 
hint format["%1 is being targeted", _uni];
_center = getpos _uni;                                              //--- central point around which the mortar rounds will hit
_radius = 25;                                                       //--- random radius from the center turn lower for closer hits

_pos = [
   (_center select 0) - _radius + (2 * random _radius),
   (_center select 1) - _radius + (2 * random _radius),
   0
   ];

for "_i" from 0 to 8 do {  //--- set range of how many cycles you want the mortars to run

_mortar commandArtilleryFire [
                   _pos,
                   getArtilleryAmmo [_mortar] select 0,
                   1 //--- number of shots per turn per unit
               ];        //---hint format["%1 is being fired on", _uni];                     


sleep 10; //--- delay between rounds

};  

however the mortars seem to fire only 3 rounds. :(

I am not sure if the unit is being passed from the trigger correctly.

Edited by abdecken

Share this post


Link to post
Share on other sites

would this work for the new mobile artillery units as well or does this only apply to the M8 mortar units?

Share this post


Link to post
Share on other sites

On theDEV version I think it might be broken now - works ok in preview but doesnt work when I host a game. Odd cos it did work a few DEV revisions back

Share this post


Link to post
Share on other sites

Hi all,

Just thought I'd drop in and share how I managed to do this. First off, as of this post I can not get this to work with artillery units, only mortars.

This is a script I wrote that allows the mortar to fire at any enemy land target that enters the target zone. Basically, the effect is that the mortar will "lock on" to any enemy that strays into the target zone(s) you set up for it and will re-adjust it's targeting as the enemy moves around.

First, here's the script you'll need.

_mortar = _this select 0;
_targetArray = _this select 1;

_target  = _targetArray select 0;
_theAmmo  = getArtilleryAmmo [_mortar] select 0;
_idx = 0;

//-- prevents firing at aircraft
if(isTouchingGround _target) then {	

//-- The number of times you want the _mortar to check for range
while {(_idx < 10)} do{
	_tgtPos = position _target;
	_isInRange = _tgtPos inRangeOfArtillery [[_mortar], _theAmmo  ];

	if (_isInRange) then {
		_mortar commandArtilleryFire [_tgtPos, _theAmmo  , 1 ];			
	};

	sleep 5;
	_idx = _idx + 1;
};	
};

Of course you can tinker with the values to suit your taste.

Next, make a mortar and name it whatever you want. In this example I used mortar1.

Then make a trigger that goes off repeatedly any time an enemy enters and put this in the on act section:

script=[mortar1, thislist] execVM "fireMortar.sqf"; 

You can reuse this script with as many different mortars and as many different targets as you want, so:

script=[mortar1, thislist] execVM "fireMortar.sqf"; 

script=[mortar2, thislist] execVM "fireMortar.sqf"; 

script=[mortar3, thislist] execVM "fireMortar.sqf"; 

Would all be valid as actions for targets assuming you created mortars with those names.

I've only been tinkering around with the editor for a few days, but this is the first thing I wanted to script out and I thought I would share it.

EDIT: Prevent firing at aircraft

Edited by leecarter
  • Like 1

Share this post


Link to post
Share on other sites

I had to edit the script above to prevent the mortar from firing at aircraft, otherwise it seems to be testing out well.

Now to figure out the deal with artillery...

I've got the artillery working pretty well now, made a separate thread:

http://forums.bistudio.com/showthread.php?164194-Finally-got-Artillery-working-right!

I'll clean my mortar script up to work more like it sometime soon and post it here.

Edited by [FRL]Myke

Share this post


Link to post
Share on other sites

Following 2nd Ranger and Camaris' posts I was able to get this to work without any changes. Was even able to swap out the mortars and use rocket artillery and it works like a champ.

I do have a question though. I'd like to have this used as one of my objectives in a MP mission. Players have to blow up a factory facility. Basically I'm trying to figure out the mechanics on how to have them call in the artillery bombardment. Right now I just have a trigger set about half a kilometer away, they go to it, the mortars fire.

I'd love for them to have to radio the strike in, or better yet, use the laser designator. I'm just trying to figure out how to do that so they can only call the artillery in on when attacking this one objective and not other times in the mission. Any thoughts?

Share this post


Link to post
Share on other sites
Hi all,

Just thought I'd drop in and share how I managed to do this. First off, as of this post I can not get this to work with artillery units, only mortars.

This is a script I wrote that allows the mortar to fire at any enemy land target that enters the target zone. Basically, the effect is that the mortar will "lock on" to any enemy that strays into the target zone(s) you set up for it and will re-adjust it's targeting as the enemy moves around.

First, here's the script you'll need.

_mortar = _this select 0;
_targetArray = _this select 1;

_target  = _targetArray select 0;
_theAmmo  = getArtilleryAmmo [_mortar] select 0;
_idx = 0;

//-- prevents firing at aircraft
if(isTouchingGround _target) then {	

//-- The number of times you want the _mortar to check for range
while {(_idx < 10)} do{
	_tgtPos = position _target;
	_isInRange = _tgtPos inRangeOfArtillery [[_mortar], _theAmmo  ];

	if (_isInRange) then {
		_mortar commandArtilleryFire [_tgtPos, _theAmmo  , 1 ];			
	};

	sleep 5;
	_idx = _idx + 1;
};	
};

Of course you can tinker with the values to suit your taste.

Next, make a mortar and name it whatever you want. In this example I used mortar1.

Then make a trigger that goes off repeatedly any time an enemy enters and put this in the on act section:

script=[mortar1, thislist] execVM "fireMortar.sqf"; 

You can reuse this script with as many different mortars and as many different targets as you want, so:

script=[mortar1, thislist] execVM "fireMortar.sqf"; 

script=[mortar2, thislist] execVM "fireMortar.sqf"; 

script=[mortar3, thislist] execVM "fireMortar.sqf"; 

Would all be valid as actions for targets assuming you created mortars with those names.

I've only been tinkering around with the editor for a few days, but this is the first thing I wanted to script out and I thought I would share it.

EDIT: Prevent firing at aircraft

this looks/works great. Makes missions that much better. thanks

Share this post


Link to post
Share on other sites

I tried to adjust this script for a dedicated server, every time we get in the trigger area, even if we were there once, it cause FPS drop to 10, but mortar units do fire.

if (isServer)  then 

{

for "_i" from 10 to 15 do {  //--- set range of how many cycles you want the mortars to run


   _mortar = [mortar1, mortar2, mortar_1_1, mortar_2_1] call BIS_fnc_selectRandom;    //--- selects a random mortar unit out of array
   _uni = [KOTS, AlphaLead, BravoLead] call BIS_fnc_selectRandom;                //--- selects a unit to target the round at
   _center = getpos _uni;                                              //--- central point around which the mortar rounds will hit
   _radius = 125;                                                       //--- random radius from the center turn lower for closer hits

_pos = [
   (_center select 0) - _radius + (2 * random _radius),
   (_center select 1) - _radius + (2 * random _radius),
   0
   ];


_mortar commandArtilleryFire [
                   _pos,
                   getArtilleryAmmo [_mortar] select 0,
                   6//--- number of shots per turn per unit
               ];                            


sleep 8; //--- delay between rounds

};  
};

Original Script


for "_i" from 10 to 15 do {  //--- set range of how many cycles you want the mortars to run


   _mortar = [mortar1, mortar2, mortar_1_1, mortar_2_1] call BIS_fnc_selectRandom;    //--- selects a random mortar unit out of array
   _uni = [KOTS, AlphaLead, BravoLead] call BIS_fnc_selectRandom;                //--- selects a unit to target the round at
   _center = getpos _uni;                                              //--- central point around which the mortar rounds will hit
   _radius = 125;                                                       //--- random radius from the center turn lower for closer hits

_pos = [
   (_center select 0) - _radius + (2 * random _radius),
   (_center select 1) - _radius + (2 * random _radius),
   0
   ];


_mortar commandArtilleryFire [
                   _pos,
                   getArtilleryAmmo [_mortar] select 0,
                   6//--- number of shots per turn per unit
               ];                            


sleep 8; //--- delay between rounds
};

Share this post


Link to post
Share on other sites

Fixed it / delete

Edited by Npk

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

×