Jump to content
Gekkibi

Random AA Fire script

Recommended Posts

It seems it's this time again... The release of new Arma means I have to update my old scripts once more.

Because I had a request for this old script, I decided to publish it after tweaking it a little bit & making sure it works in Arma 3.

Ladies and gentlemen, I give you GKB_fnc_randomAAFire. The same one rexehuk praised so much many years ago.

GKB_fnc_randomAAFire.jpg

Old video from Arma 2 version (go to 0:18, this video contains other projects I created as well).

Here's an Arma 2 version in action. An actual mission, not just a showcase. Thanks goes to Havoc Company for using it.

/* ----------------------------------------------------------------------------------------------------
Function:
GKB_fnc_randomAAFire

Author:
Gekkibi

Terms for copying, distribution and modification:
Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
http://creativecommons.org/licenses/by-nc-sa/3.0/

Description:
Defined AA-batteries shoot coherently to a randomly selected position, simulating anti-air fire
without creating any in-game aircraft. Script will make sure they will not run out of ammunition.
AA-batteries can be stationary or mobile. Batteries will stop firing at the fake target if they
spot a real one, and continues normally afterwards.

Parameters:
1st	Objects in Array	AA-units
2nd	Arrays in array		Target is created between these coordinates
				[[x1, x2], [y1, y2], [z1, z2]]
3rd	Number			Delay between targets
4th	Number			Secondary weapon chance (from 0 to 1). Use 0 if no such weapon
5th	Code			When true new targets are calculated

Returns:
Nil

Example:
[
	[GKB_aa1, GKB_aa2, GKB_aa3],
	[[0, 4500], [7000, 8000], [1000, 500]],
	20 + random 100,
	0.1,
	{alive GKB_radar}
] spawn GKB_fnc_randomAAFire.sqf;
---------------------------------------------------------------------------------------------------- */

if (isServer) then {
private ["_positionArray", "_runCondition", "_secondaryChance", "_sleepTime", "_unitArray"];
_unitArray = _this select 0;
_positionArray = _this select 1;
_sleepTime = _this select 2;
_secondaryChance = _this select 3;
_runCondition = _this select 4;
{
	_x lock 3;
} forEach _unitArray;
while _runCondition do {
	private ["_direction", "_weapon"];
	{
		if (! alive gunner _x) then {
			_unitArray = _unitArray - [_x];
		};
	} forEach _unitArray;
	if (count _unitArray == 0) exitWith {};
	_direction = [
		(_positionArray select 0 select 0) - random ((_positionArray select 0 select 0) - (_positionArray select 0 select 1)),
		(_positionArray select 1 select 0) - random ((_positionArray select 1 select 0) - (_positionArray select 1 select 1)),
		(_positionArray select 2 select 0) - random ((_positionArray select 2 select 0) - (_positionArray select 2 select 1))
	];
	if (random 1 > _secondaryChance) then {
		_weapon = "cannon";
	} else {
		_weapon = "missile";
	};
	{
		if (alive gunner _x && isNull assignedTarget _x) then {
			[_x, _direction, _weapon] spawn {
				private ["_direction", "_unit", "_weapon"];
				_unit = _this select 0;
				_direction = _this select 1;
				_weapon = _this select 2;
				_unit doWatch _direction;
				sleep 2;
				switch (_weapon) do {
					case "cannon": {
						for "_x" from 1 to 1 + ceil random 5 do {
							sleep random 2;
							for "_x" from 1 to 10 do {
								if (! isNull assignedTarget _unit) exitWith {};
								_unit fire (_unit weaponsTurret [0] select 0);
								sleep 0.05;
							};
							if (! isNull assignedTarget _unit) exitWith {};
							_unit doWatch _direction;
						};
					};
					case "missile": {
						sleep (2 + random 5);
						_unit fire (_unit weaponsTurret [0] select 1);
					};
				};
				if (isNull assignedTarget _unit) then {
					_unit doWatch objNull;
				} else {
					_unit doWatch assignedTarget _unit;
				};
				_unit setVehicleAmmo 1;
			};
		};
	} forEach _unitArray;
	sleep _sleepTime;
};
};

You can download an example mission by clicking here.

As always, feedback, suggestions, questions, bug reports and outright insults are welcome. Feel free to use in your missions (CC BY-NC-SA 3.0).

Edited by Gekkibi

Share this post


Link to post
Share on other sites

Oh, can't believe I never noticed your scripts in ArmA2.

Looks very nice.

Share this post


Link to post
Share on other sites
Oh, can't believe I never noticed your scripts in ArmA2.

That's probably because I publicly shared only a handful of scripts/missions, I have made them since OFP 1.0. I take requests and provide missions / scripts / help as needed for teams and groups, privately.

Share this post


Link to post
Share on other sites
7 hours ago, zombiemanx12 said:

How do you get this to work?

Fairy dust. :rofl:

Quote

Example: [ [GKB_aa1, GKB_aa2, GKB_aa3], [[0, 4500], [7000, 8000], [1000, 500]], 20 + random 100, 0.1, {alive GKB_radar} ] spawn GKB_fnc_randomAAFire.sqf;

Quote

Parameters:
1st	Objects in Array	AA-units
2nd	Arrays in array		Target is created between these coordinates
				[[x1, x2], [y1, y2], [z1, z2]]
3rd	Number			Delay between targets
4th	Number			Secondary weapon chance (from 0 to 1). Use 0 if no such weapon
5th	Code			When true new targets are calculated

 

 

  • Haha 1

Share this post


Link to post
Share on other sites

How do I use this? There's nowhere explaining how to make an AA that fires randomly, just here. There's nothing explaining where each thing goes or how ...
I think that...
This larger code has to create a text file in the mission folder, copy and paste there, change the name to "GKB_fnc_randomAAFire.sqf". Within the game create a trigger with the smaller code and put it in, then take AA batteries and change the gunner's name to GKB_aaX for example ... would that be enough? I really don't understand well

Share this post


Link to post
Share on other sites

Greetings!
 

I'd like to add myself to this list. And if the moderators rate this thread as dead (and us as "Necro-ing") then that is fine and I will leave this alone.

The test mission is no longer downloadable. I have tried to replicate it, but I am also not able to get this specific function to work.
I've (of course) added the function to the description.ext . I've defined the GKB_AAx's using either Praetorian 1C's and Mk49 Spartans (just in case the syntax for "cannon" or "missile" was specific), along with a Cronus radar. And have specified positions around them.

However, when executing the function, I receive a generic error on line 49 of the function:

'...forEach _unitArray;
while _runCondition |#|do {
private ["_direction", "_weapon"];
...'
Error Generic error in expression
File functions\fn_randomAAFire.sqf [GKB_fnc_randomAAFire]..., line 49

Line 49 indeed matches up with the start of the function.
Generic error in my case often means typo or syntax error. 
Function has compiled correctly, but doesn't seem to accept arguments. I guessed it would be the 3 AA units. But it does this with any unit. 

Anyone still on the forum who has used this, with a specific tip?

Share this post


Link to post
Share on other sites

It works (tested),

It's all about the parameters you're passing:

On 9/19/2013 at 5:46 AM, Gekkibi said:

Parameters:

1st Objects in Array AA-units

2nd Arrays in array Target is created between these coordinates [[x1, x2], [y1, y2], [z1, z2]]

3rd Number Delay between targets

4th Number Secondary weapon chance (from 0 to 1). Use 0 if no such weapon

5th Code When true new targets are calculated

Make sure 1st parameter is an array with the NAMES of your AA guns.

Make sure 5th parameter returns true. E.g. alive yourgunname1, or just true.

Share this post


Link to post
Share on other sites

Thanks for the reply. 

2 hours ago, RCA3 said:

Make sure 1st parameter is an array with the NAMES of your AA guns.

Make sure 5th parameter returns true. E.g. alive yourgunname1, or just true.


I assume you mean giving each AA gun a variable name in Eden, which is sort of what I meant with "defined by ...".
Just to check: GKB_aa1 through GKB_aa3 can be Praetorians, yes?
And the GKB_radar an "AN/MPQ 105 Radar" object, yes?

 

Share this post


Link to post
Share on other sites

Yes, and you can name them whatever, not necessarily GKB_*: gun1, radar1, etc.

Choose them from NATO side though, not empty objects.

  • Like 1

Share this post


Link to post
Share on other sites

Appreciate the continued responses.

On 2/7/2021 at 1:05 AM, RCA3 said:

Yes, and you can name them whatever, not necessarily GKB_*: gun1, radar1, etc.

I'm aware. I use the parameters mentioned in the script file example to simplify troubleshooting.

 

On 2/7/2021 at 1:05 AM, RCA3 said:

Choose them from NATO side though, not empty objects.

Praetorians and the AN/MPQ 105 Radar are Blufor objects. Just in case, I retested by creating them with the "place vehicles with crew" box unchecked, and manually adding AI units to the turrets.  

I'm thankful you are helping me with this, but what would be the next most likely problem, if not these?

Share this post


Link to post
Share on other sites
2 hours ago, Melody_Mike said:

I'm thankful you are helping me with this

It's no problem.

Please paste the syntax you're calling the function with.

 

Share this post


Link to post
Share on other sites

I copy pasted the (commented out) example call from the top of the fn_randomAAFire.sqf file:

Example:
[
	[GKB_aa1, GKB_aa2, GKB_aa3],
	[[0, 4500], [7000, 8000], [1000, 500]],
	20 + random 100,
	0.1,
	{alive GKB_radar}
] spawn GKB_fnc_randomAAFire.sqf;

=======

...

 

I fixed the problem. 
I had added the function to the description.ext CfgFunctions. Of course, spawning the function with GKB_fnc_randomAAFire.sqf; would not work then. I had copied fn_randomAAFire.sqf to the mission root and changed the spawn to GNK_fnc_randomAAFire.sqf (to test if my description.ext was correct), but ArmA would give me the "missing ;" error then. Interesting...

So the given example in the script file has a typo. It should end with " ] spawn GKB_fnc_randomAAFire; "

To make it easier for future users, my example description.ext :

class CfgFunctions 
{
	class GKB
		{
        class MyCategory 
			{
			file = "functions";
			class randomAAFire {};
			};
		};
};

(make a folder in your mission folder called "functions" and place the script file fn_randomAAFire.sqf in there. Read this BIKI article for more details)

Appreciate the hand holding. Have a good one!

  • Like 1

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

×