Jump to content
ArmaMan360

Enemy AI respawn and order function/script/addon

Recommended Posts

I am looking for something like the great AISSP's militarize and fill house function but with enemy respawn ability for SP use.

To make it simpler, ability to spawn enemies around player or marker in a radius and have them respawn and follow the attack/defend/fortify/recon patrol functions.

If any one can suggest a combination of scripts that too is welcomed. One of my SP projects is lying incomplete because of it.

I hope some one can help.

Share this post


Link to post
Share on other sites

I would use bis spawn functions mixed with CBA ai task system

I wish I could. Sigh.

I mean I am not a total scripting noob and can use them individually. Its this mixup which bowls me over. Sadly cant.

Share this post


Link to post
Share on other sites

Oh come on is not that hard.

Read some  wiki stuff and you can write something you want and even if that would not work well we can help you here.

Share this post


Link to post
Share on other sites

Oh come on is not that hard.

Read some  wiki stuff and you can write something you want and even if that would not work well we can help you here.

OkI use something like this for spawning groups vanilla way:

_units = [_wave1,_axis,5,_vehi,_mech,_arm,_air] call JSHK_fnc_patrols;

where the patrol function is:

/* ////////////////////////////////////////////// 
Author: J.Shock 

File: fn_patrols.sqf 

Description: Creates randomly positioned and sized patrols throughout a defined radius of an AO 
             using a marker as the center position. 

Parameters:  
        1- Center position: (array) (default: empty array) 
        2- Radius to spawn units: (integer) (default: 300) 
        3- Number of foot patrol groups: (integer) (default: 5) 
        4- Number of vehicle patrol groups: (integer) (default: 3) 
        5- Number of mechanized patrol groups: (integer) (default: 2) 
        6- Number of armor patrol groups: (integer) (default: 2) 
        7- Number of air patrol groups: (integer) (default: 1) 
        8- Side: (side) (default: EAST) 
         
Return: Spawned units. 

Example Call line: _units = ["mrkName",200,5,3,2,2,1,EAST] call JSHK_fnc_patrols; 

*/////////////////////////////////////////////// 
private [ 
            "_AOmarker","_radius","_numFootPatrols","_numVehPatrols","_center", 
            "_numArmorPatrols","_numMechPatrols","_numAirPatrols","_footUnits", 
            "_vehUnits","_armorUnits","_mechUnits","_airUnits","_units" 
        ]; 

_AOmarker = [_this, 0, [], [[]]] call BIS_fnc_param; 
_radius = [_this, 1, 300, [0]] call BIS_fnc_param; 
_numFootPatrols = [_this, 2, 20, [0]] call BIS_fnc_param; 
_numVehPatrols = [_this, 3, 3, [0]] call BIS_fnc_param; 
_numArmorPatrols = [_this, 4, 2, [0]] call BIS_fnc_param; 
_numMechPatrols = [_this, 5, 2, [0]] call BIS_fnc_param; 
_numAirPatrols = [_this, 6, 1, [0]] call BIS_fnc_param; 

_footUnits = ["BUS_InfSentry", "BUS_InfTeam", "BUS_InfTeam_AT", "BUS_InfSquad","BUS_SniperTeam"]; 
_vehUnits = ["B_MRAP_01_hmg_F","B_MRAP_01_gmg_F"]; 
_armorUnits = ["B_MBT_01_cannon_F","B_MBT_01_arty_F","B_APC_Tracked_01_rcws_F","B_APC_Tracked_01_CRV_F"];
_mechUnits = ["B_APC_Wheeled_01_cannon_F","B_APC_Tracked_01_AA_F"]; 
_airUnits = ["B_Heli_Light_01_armed_F","B_Heli_Transport_01_camo_F","B_Heli_Attack_01_F"];

_center = createCenter WEST; 

_units = []; 

if (_numFootPatrols > 0) then 
{ 
    for "_i" from 1 to (_numFootPatrols) step 1 do  
    { 
        _configGrp = _footUnits call BIS_fnc_selectRandom; 
        _rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
        _grp = [_rndPos, _center, (configfile >> "CfgGroups" >> "West" >> "BLU_F" >> "Infantry" >> (_configGrp))] call BIS_fnc_spawnGroup; 
        [_grp, (_AOmarker), _radius] call BIS_fnc_taskPatrol; 
        {_units pushBack _x} forEach units _grp; 
        sleep 0.05; 
    }; 
};     

if (_numVehPatrols > 0) then 
{ 
    for "_i" from 1 to (_numVehPatrols) step 1 do  
    { 
        _rndVeh = _vehUnits call BIS_fnc_selectRandom; 
        _rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
        _veh = [_rndPos,random(359),_rndVeh,_center] call BIS_fnc_spawnVehicle; 
		0 = [(_veh select 2),_radius] execVM "shk_patrol.sqf";		
        {_units pushBack _x} forEach (_veh select 1); 
        _units pushBack (_veh select 0); 
        sleep 0.05; 
    }; 
}; 

if (_numArmorPatrols > 0) then 
{ 
    for "_i" from 1 to (_numArmorPatrols) step 1 do  
    { 
        _rndVeh = _armorUnits call BIS_fnc_selectRandom; 
        _rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
        _veh = [_rndPos,random(359),_rndVeh,_center] call BIS_fnc_spawnVehicle; 
		0 = [(_veh select 2),_radius] execVM "shk_patrol.sqf";	
        {_units pushBack _x} forEach (_veh select 1); 
        _units pushBack (_veh select 0); 
        sleep 0.05; 
    }; 
}; 

if (_numMechPatrols > 0) then 
{ 
    for "_i" from 1 to (_numMechPatrols) step 1 do  
    { 
        _rndVeh = _mechUnits call BIS_fnc_selectRandom; 
        _rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
        _veh = [_rndPos,random(359),_rndVeh,_center] call BIS_fnc_spawnVehicle; 
		0 = [(_veh select 2),_radius] execVM "shk_patrol.sqf";	
        {_units pushBack _x} forEach (_veh select 1); 
        _units pushBack (_veh select 0); 
        sleep 0.05; 
    }; 
}; 

if (_numAirPatrols > 0) then 
{ 
    for "_i" from 1 to (_numAirPatrols) step 1 do  
    { 
        _rndVeh = _airUnits call BIS_fnc_selectRandom; 
        _rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
        _veh = createVehicle [_rndVeh,_rndPos,[],0,"FLY"]; 
        createVehicleCrew _veh; 
		0 = [group _veh,_radius] execVM "shk_patrol.sqf";	
        {_units pushBack _x} forEach (crew _veh); 
        _units pushBack _veh; 
        sleep 0.05; 
    }; 
};

_units;

But I honestly cant figure out how to detect the group as eliminated, then sleep counter of lets say 60 seconds, and BAM they spawn again with the same orders like before..

 

I can get them to respawn without any problems, but cant get them to accept new orders once they spawn. Why I cant use triggers is because I am trying to achieve a persistent mission where the enemy units will be spawning on the entire map as well as some in crucial areas to defend. So a common trigger wont serve the purpose here.

Share this post


Link to post
Share on other sites

Its nonsense to make units persistence which will be spawned at mission start  and even re spawned again when killed.

You can create a loop or even scripted trigger where you can count _units and fire again the functions when  count is equal to 0.

Share this post


Link to post
Share on other sites

Will keep developing.. 

Share this post


Link to post
Share on other sites

Ok this is the best I could come up with and its spawning the units but I cant get it to detect a enemy group eliminated and then spawn a group on a randomly generated position on the map again.

 

Init.sqf:

call compile preprocessfile "SHK_pos\shk_pos_init.sqf";

sleep 1;

[getMarkerPos "m1", 10, 1] call JSHK_fnc_patrols;

Code:

/* ////////////////////////////////////////////// 
Author: J.Shock 

File: fn_patrols.sqf 

Description: Creates randomly positioned and sized patrols throughout a defined radius of an AO 
             using a marker as the center position. 

Parameters:  
        1- Center position: (array) (default: empty array) 
        2- Radius to spawn units: (integer) (default: 300) 
        3- Number of foot patrol groups: (integer) (default: 5) 
        8- Side: (side) (default: EAST) 
         
Return: Spawned units. 

Example Call line: _units = ["mrkName",200,5,3,2,2,1,EAST] call JSHK_fnc_patrols; 

*/////////////////////////////////////////////// 

_AOmarker = [_this, 0, [], [[]]] call BIS_fnc_param; 
_radius = [_this, 1, 300, [0]] call BIS_fnc_param; 
_numFootPatrols = [_this, 2, 20, [0]] call BIS_fnc_param; 
_footUnits = ["IRG_InfSentry", "IRG_InfTeam", "IRG_InfTeam_AT", "IRG_InfSquad","IRG_SniperTeam_M"]; 

_center = createCenter WEST; 

_units = []; 

if (_numFootPatrols > 0) then 
	{ 
		for "_i" from 1 to (_numFootPatrols) step 1 do  
		{ 
			_configGrp = _footUnits call BIS_fnc_selectRandom; 
			_rndPos = [_AOmarker, [80,100], random 360, 0, [0], 100] call SHK_pos;
			_grp = [_rndPos, _center, (configfile >> "CfgGroups" >> "West" >> "Guerilla" >> "Infantry" >> (_configGrp))] call BIS_fnc_spawnGroup; 
			[_grp, (_AOmarker), _radius] call BIS_fnc_taskPatrol; 
			{_units pushBack _x} forEach units _grp; 
			sleep 1; 
		}; 
	};
_units; // works till here

respawn = [] spawn
	{
	waitUntil {{!alive _x} forEach _units};
};

I know its broken. Please help me repeat the creategroup lines again to spawn a new group once an enemy group is completely eliminated.

 

Much thanks :'(

 

P.S.: Thanks to JShock for the original code :)

Share this post


Link to post
Share on other sites

Twelfth day of continuous research and this is where I have reached.

 

This is my init.sqf:

init.sqf:

[getMarkerPos "m1", 10, 1] call JSHK_fnc_patrols;

fn_patrols: (this works very well):

_AOmarker = [_this, 0, [], [[]]] call BIS_fnc_param;
_radius = [_this, 1, 300, [0]] call BIS_fnc_param;
_numFootPatrols = [_this, 2, 20, [0]] call BIS_fnc_param;
_footUnits = ["IRG_InfSentry", "IRG_InfTeam", "IRG_InfTeam_AT", "IRG_InfSquad","IRG_SniperTeam_M"];
_center = createCenter WEST;
_units = [];
if (_numFootPatrols > 0) then
{
  for "_i" from 1 to (_numFootPatrols) step 1 do 
  {
   _configGrp = _footUnits call BIS_fnc_selectRandom;//_rndPos = [_AOmarker, [], random 360, 0, [0], 100] call SHK_pos;
   _grp = [_AOmarker, _center, (configfile >> "CfgGroups" >> "West" >> "Guerilla" >> "Infantry" >> (_configGrp))] call BIS_fnc_spawnGroup;
   [_grp, (_AOmarker), _radius] call BIS_fnc_taskPatrol;
   sleep 0.5;  
   [_grp] spawn
   {
   while {true} do
    {
    sleep 5;
     {
     _grp = _x select 0;
     if (({(alive _x)} count (units _gp)) < 1) then
      {
      deleteGroup _gp;
      [getMarkerPos "m1", 10] execVM "respawn.sqf";
      };
     };
    };
   };
   {_units pushBack _x} forEach units _grp;
   sleep 1;
  };
};
_units;

and Finally this is my respawn.sqf: (this is NOT executed):

_AOmarker = [_this, 0, [], [[]]] call BIS_fnc_param;
_radius = [_this, 1, 300, [0]] call BIS_fnc_param;
_footUnits = ["IRG_InfSentry", "IRG_InfTeam", "IRG_InfTeam_AT", "IRG_InfSquad","IRG_SniperTeam_M"];
_center = createCenter WEST;
_units = [];
{
_configGrp = _footUnits call BIS_fnc_selectRandom;
_grp = [_AOmarker, _center, (configfile >> "CfgGroups" >> "West" >> "Guerilla" >> "Infantry" >> (_configGrp))] call BIS_fnc_spawnGroup;
[_grp, (_AOmarker), 10] call BIS_fnc_taskPatrol;
};
_units; 

:'((

Share this post


Link to post
Share on other sites

Untested but something like..

params[
	[ "_AOmarker", [] ],
	[ "_radius", 300 ],
	[ "_numFootPatrols", 20 ],
	[ "_side", west ],
	[ "_isRespawn", false ],	//Is this a callback for respawn
	[ "_grpType", "" ]			//What was the group type used
];


_fnc_CreateGroup = {
	params[ "_markerPos", "_side", "_configGrp", "_radius" ];
	
	//Make sure center exists
	_center	= createCenter _side;
	
	//Create Group
	_grp = [ _markerPos, _side, (configFile >> "CfgGroups" >> str _side >> "Guerilla" >> "Infantry" >> _configGrp ) ] call BIS_fnc_spawnGroup;
	
	//Start patrol
	[ _grp, _markerPos, _radius ] call BIS_fnc_taskPatrol;
	
	//Apply EH for when each unit dies
	//If all members are dead recall script with the groups settings
	{
		_x addEventHandler [ "Killed", format[ "
			params[ '_unit' ];
			
			if ( { alive _x }count ( units group _unit ) isEqualTo 0 ) then {
				
				_units = [ %1, %2, nil, side group _unit, true, %3 ] call JSHK_fnc_patrols;
				[ missionnamespace, 'JSHK_fnc_patrols_respawn', [ _units ] ] call BIS_fnc_callScriptedEventHandler;
				
				deleteGroup group _unit;
			};
		", _markerPos, _radius, str _configGrp ]]; //format groups settings into callback for respawn
	}forEach units _grp;
	
	units _grp
};


//MAIN
_units = [];

if !( _isRespawn ) then {
	
	//Create initial groups
	if (_numFootPatrols	> 0) then {
		
		for "_i" from 1 to ( _numFootPatrols ) do {
			
			//Chose random group type
			_configGrp = selectRandom [ "IRG_InfSentry", "IRG_InfSquad", "IRG_InfTeam", "IRG_InfTeam_AT", "IRG_SniperTeam_M" ];
			
			//Create group
			_units = _units + ( [ _AOmarker, _side, _configGrp, _radius ] call _fnc_CreateGroup );
			
		};
		
	};
}else{
	//Respawn similar group
	_units = _units + ( [ _AOmarker, _side, _grpType, _radius ] call _fnc_CreateGroup );
};

_units
So similar to what you had except we dont use a while loop for checking the units, instead we use the killed EH.

Instead of another function for the respawns we split the original into two via a IF statement and an extra parameter(_isRespawn) to select what to do( create/respawn) a group.

I have also passed back the original group type spawned so the respawned group will be of the same type.(can be easily changed if not desired)

I have also included a scriptedEH so you can get a reference to respawned units if necessary.

[ missionNamespace, "JSHK_fnc_patrols_respawn", //code/Function ] call BIS_fnc_addStackedEventHandler;
Where the code/function will receive an array of units that where respawned.

This could easily be further expanded to spawn groups from different side/factions.

  • Like 1

Share this post


Link to post
Share on other sites

Larrow, saving Armaholics since whatever blessed year he was born in :D

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

×