Jump to content
Sign in to follow this  
Lt Gunner

Kill all AI if certain distance away from game logic

Recommended Posts

I'm horrible with scripting and have tried to make something on my own, but I just don't get this stuff. Could somebody help me out and write a simple script that would delete any AI group and all members of that group if they were 600m or more away from a named game logic?

Any help would be much appreciated!

Share this post


Link to post
Share on other sites

Hi,

Put this into your init.sqf:

[font="Fixedsys"][] spawn {
if (isServer) then {
	while {true} do {
		{[_x] execVM "distancecheck.sqf";} forEach (nearestObjects [[color="Blue"]YourLogic[/color], ["Man"], 5000]);
		sleep 5;
	};
};
};[/font]

And create distancecheck.sqf:

[font="Fixedsys"]_Unit = _this select 0;
_GetVar = _Unit getVariable "DistanceMonitored";
if (_GetVar) exitWith {};
_Unit setVariable ["DistanceMonitored", true];
waitUntil {(!alive _Unit) || (_Unit distance [color="Blue"]YourLogic[/color] > 600)};
if (!alive _Unit) exitWith {};
deleteVehicle _Unit;[/font]

Not tested, but should work like that. :)

Edited by sxp2high
added setvariable to prevent unnecessary looping

Share this post


Link to post
Share on other sites

Thanks a ton, and for the fast reply too! I'll try it out and get back to ya.

Share this post


Link to post
Share on other sites

No problem. Just tested myself, it's working. :)

If you need any modifications or have questions, ask me.

Btw:

If you want to kill them (i used delete above) just change the last line of the distancecheck.sqf:

deleteVehicle _Unit;

to

_Unit setDamage 1;

Btww:

I just noticed that players are affected by this too. You can exclude units from this by putting this into their init line:

this setVariable ["DistanceMonitored", true];

Edited by sxp2high

Share this post


Link to post
Share on other sites

cool, that will be somewhat important, so that JIP guys can get in. one question, if players respawn that code will still be present right?

also, I'm not sure if its just how Im testing or not, but does this loop or check constantly? or it is just one time?

Edit: did another test and it does seem to loop, but it takes a long time to delete the guys when they meet the parameters. also it seems to cause a LOT of lag when i run this. would it be effective to run this script in a loop once a minute or once every 30 seconds? that would be more than adequate for what i need.

Edited by Lt Gunner

Share this post


Link to post
Share on other sites

As you can see here:

[font="Fixedsys"]while {true} do {
{[_x] execVM "distancecheck.sqf";} forEach (nearestObjects [YourLogic, ["Man"], 5000]);
sleep 5;
};[/font]

It executes the distancecheck.sqf for all soldiers that are within 5km around the logic, every 5 seconds, so yes it loops contantly. You can reduce to 1 second or so if that better fits your needs.

About respawn: No, setVariable will not remain after respawn. However, you can use this to protect units after respawn again:

[font="Fixedsys"]this addEventHandler ["killed",{ waitUntil {alive _this}; _this setVariable ["DistanceMonitored", true]; }];[/font]

Edit:

You can try to reduce the 5000 to 1000 or so and you can change the sleep to whatever you like. That defines the loop interval.

But i dont think that this script can cause lag. Heavy lag mostly comes from function(script) errors. Is there anything in your RPT file?

Here's a small demo mission, using a trigger instead of the loop.

Maybe that's better.

Edited by sxp2high

Share this post


Link to post
Share on other sites

there were some errors, but they were to do with the alice module and the script that spawns in my enemy squads. i disabled alice, and fixed the error in the spawning script and it seemed to go away, so thats good news!

On the other hand, I have put in a teleport script so i can quickly move about to test this script, and it works, but slowly. After about a minute or more, it will begin deleting the enemy squad members, and it seems to only delete them if im holding still (the game logic is set positioned to my position at all times). not sure if thats the case or if its just randomly slow at times.

one potential fix, could it be written to detect an enemy group, and do something like "deletevehicle _x for each units this group"? i've seen something like that before, and I'm thinking that would be a slightly more efficient way if possible.

Share this post


Link to post
Share on other sites

Okay, i gotta know the purpose of it to decide what solution is the best. You want to delete all enemies that are > 600m away from a specific unit ?

Share this post


Link to post
Share on other sites

exactly. I figured the easiest way to do this would be using the game logic, since it automatically moves with whomever is the group leader.

I have a single squad of blufor moving around a map, for whatever reason. They have access to a helicopter in order to provide them quick movement from one area to another based on what intel they get.

The mission isn't a high intensity operation, so the focus isn't on the combat, but the team needs to work together, provide security and watch their sectors... So I have a script that randomly spawns in an enemy squad, at a random distance and direction, and a somewhat random strength. But its always just one squad of Russian troops. That same script that spawns the group, also utilizes a game logic as a waypoint for that squad, and that game logic is automatically set positioned to the group leader of the blufor squad. The script will also not initialize if my group is airborne.

I have a four triggers setup to make the script run how i want.

first trigger is set to detect when all the blufor have left base for the first time. it sets one of two variables the last trigger needs, to true once everybody is gone.

the second and third trigger, detect if opfor are anywhere on the map. trigger #2 detects if there are no opfor on the map, it sets the second variable to true, enabling the 4th trigger to run the script. the third trigger detects if there are opfor on the map, and sets the second variable to false if there are. I found i needed that one, otherwise the 4th trigger would only trip the first time no opfor were detected.

the 4th trigger, simply waits for variable one and two to be true, then executes the script, with a random timeout.

so essentially, once blufor leaves base, the script will run at a random time, and place random units somewhere around my squad, and then make that enemy squad move to me.

Edit for clarity: the script will run once blufor has left base, one time. after that that variable is always true. so returning to base does not garuntee saftey. the other variable basically detects if there is already opfor or not, and switches the variable based on if there is, or isnt an enemy squad. if there is, the variable is false, if there is not the variable is true. once both variables are true, the script spawns in a group.

What i need this script to do, is detect when that enemy squad is more than 600m away from my squad, and delete them. I figure if they are that far away, my group broke contact, and loaded up in the chopper and are moving to wherever they think they need to go next. So i need the spawning script to be able to create a new group wherever we set down at, to investigate the intel we got. for that to happen, I'll need the old enemy squad deleted first

Edited by Lt Gunner

Share this post


Link to post
Share on other sites

Good, lets forget the game logic then, its maybe updating its position too slow.

Here's what i would use for that (pretty similar to my script above):

Init.sqf:

[font="Fixedsys"][] spawn {
if (isServer) then {
	while {true} do {
		_Leader = (leader [color="Blue"]NameOfYourGroup[/color]);
		{[_x] execVM "distancecheck.sqf";} forEach (nearestObjects [_Leader, ["Man"], 5000]);
		sleep 30;
	};
};
};[/font]

distancecheck.sqf:

[font="Fixedsys"]_Distance = 600;
_EnemySide = east;
_EnemySide2 = east;
_Leader = (leader [color="Blue"]NameOfYourGroup[/color]);

_Unit = _this select 0;
if ((side _Unit != _EnemySide) && (side _Unit != _EnemySide2)) exitWith {};
if (_Unit != leader (group _Unit)) exitWith {};
_GetVar = _Unit getVariable "DistanceMonitored";
if (_GetVar) exitWith {};
_Unit setVariable ["DistanceMonitored", true];
waitUntil {(!alive _Unit) || (_Unit distance _Leader > _Distance)};
if (!alive _Unit) exitWith { sleep 30 + (random 90); deleteVehicle _Unit; };
{deleteVehicle _x} forEach units (group _Unit);[/font]

Just change NameOfYourGroup to the groups name.

If you haven't set a group name, put this into the leader init line to define it:

[font="Fixedsys"]NameOfYourGroup = group this;[/font]

As you can see, you can now define 2 enemy sides. So west units are not affected by this at all.

Edited by sxp2high

Share this post


Link to post
Share on other sites

nice! let me give it a test.

EDIT: Works like a charm! Thanks a ton man!

Edited by Lt Gunner

Share this post


Link to post
Share on other sites

This is really cool!

Can it be made to work not just on infantry, but all enemy units and vehicles?

I've been trying to do it one group at a time, with a two trigger method for each spawned group, but this would be so much simpler.

Share this post


Link to post
Share on other sites

I have the opposite problem; I like playing on open maps and when the Ambient Combat Module is active, the units sometimes disappear right when I'm looking at them and have already engaged them in combat. Now I use the quick scenario editor that gives you the 4 basic scenario options, but have no idea how to fix the scripting to keep the units spawned alive.

Or is that even possible? I'm still quite the beginner as far as editing Arma2 CO missions.

Share this post


Link to post
Share on other sites
... So I have a script that randomly spawns in an enemy squad, at a random distance and direction, and a somewhat random strength. But its always just one squad of Russian troops.

...

, the script will run at a random time, and place random units somewhere around my squad, and then make that enemy squad move to me.

...

What i need this script to do, is detect when that enemy squad is more than 600m away from my squad, and delete them. I figure if they are that far away, my group broke contact, and loaded up in the chopper and are moving to wherever they think they need to go next. So i need the spawning script to be able to create a new group wherever we set down at, to investigate the intel we got. for that to happen, I'll need the old enemy squad deleted first

This sounds like an awesome script to have. I spend a lot of time with triggers and whatnot trying to fill a map like Fallujah with random enemies, so I can just roam around and fight bad guys. We have fun doing that. No objectives or anything, just roaming around and fighting, using norrins revive/respawn.

But it's quite a task to set it up, and even more so when you have a map as big as Fallujah and you have to start to worry about getting too many guys on the map loaded, slowing down cpu.

If you could be persuaded to share that script, I'm sure I wouldn't be the only one who would be very grateful. Maybe if it were shared, others could tweak it to add random motorized groups as well, maybe allow for other factions besides Russie, etc.

Just a thought, as that sounds like a very handy script to have. Please consider.

Share this post


Link to post
Share on other sites

What about this...

Is there any way to have distancecheck.sqf ignore a certain group(s), or any group(s) in a certain marker? Please?

Reason I ask is because I have about 4 UPSMON templates tucked way down in the corner of the map, which are used as templates for the rest of the map. There are triggers all over the map, which spawn copies of these templates as I approach certain areas.

It would be very nice to use distancecheck.sqf to 'clean up' when I leave certain areas, but as it is now, it also removes my templates down there in the corner of my map, because of course they are tucked way out of the way, and will always be far enough from me to be deleted according to this script.

Any help would be greatly appreciated.

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  

×