Jump to content
Sign in to follow this  
hogmason

respawning a dynamic ai group using randomgroup

Recommended Posts

ok so i need to set up a respawn of a group once all are killed. this is easy to do for a unit but i dont know how for a random group.

this is what i have got

patrols init.sqf

waituntil {!isnil "bis_fnc_init"};
if isserver then {
sleep 10;

   for "_i" from 0 to 100 do 
   {
//hint "spawning patrol";

_SPAWNPOS = ["AO",false,["safe_zone"]] call SHK_pos;
	_types = ["Infantry"] call BIS_fnc_selectrandom;
	_patrol = [_SPAWNPOS, _types, 1000,"BIS_TK_INS"] execVM "HOG_scripts\HOG_patrols\crB_taskPatrol.sqf";	[color="#FF0000"]// this is the group	[/color]	
                             //_patrol addeventhandler ["killed", {null = this execVM 'HOG_scripts\HOG_patrols\respawn.sqf';}]; /////// [color="#FF0000"]i tried this but it doesnt work[/color]	
    sleep 1;			 
   }; 	

};


crB_taskPatrol.sqf


if(!isServer) exitWith{};

private["_pos","_type","_dist","_fac","_grp","_code"];
_pos = _this select 0;
_type = _this select 1;
_dist = _this select 2;
_fac = nil;
if(count _this > 3) then {
_fac = _this select 3;
};
_code = "";
if(count _this > 4) then {
_code = _this select 4;
};

_grp = [_pos, _type, _fac] call compile preprocessFileLineNumbers "HOG_scripts\HOG_patrols\crB_randomGroup.sqf";
call compile _code;
sleep 5;
call{[_grp, position leader _grp, _dist] execVM "HOG_scripts\HOG_patrols\BIN_taskPatrol.sqf";};


crB_randomGroup.sqf


if(!isServer) exitWith{};
private["_pos","_type","_fac","_facs","_sidex","_side","_grpx","_grps","_grp","_radius"];

_pos = _this select 0;
_type = _this select 1;
_fac = nil;
// setup default param
if (count _this > 2) then { _fac = _this select 2; };
if (isNil "_fac") then { _fac = east; };
//hint str _fac;

// init BIS funcs and ACE
waitUntil {!isNil "bis_fnc_init"}; 

_facs = [];
_side = nil;
// get all factions
_allfacs = [] call BIS_fnc_getFactions;
// if default or selection by side
if(typeName _fac == "ANY" || typeName _fac == "SIDE") then {
if(typeName _fac == "SIDE") then {
	_side = _fac;
	_fac = nil;
};
switch(_side) do {
	case east: {
		_sidex = 0;
	};
	case west: {
		_sidex = 1;
	};
	case resistance: {
		_sidex = 2;
	};
	case civilian: {
		_sidex = 3;
	};
};

{
	_fx = getNumber(configFile >> "CfgFactionClasses" >> _x >> "side");
	if (_fx == _sidex) then {
		_facs = _facs + [_x];
	};
} forEach _allfacs;
};
//hint str _facs;
// if single faction
if(typeName _fac == "STRING") then {
_facs = [_fac];
_fac = nil;
};

// if multiple factions
if(typeName _fac == "ARRAY") then {
_facs = _fac;
_fac = nil;
};
if(isNil "_fac" && !isNil "_side") then {
_s = switch(_side) do {
	case resistance: {"Guerrila";};
	case civilian: {"Civilian";};
	default {str _side;};
};
//hint str _s;
_facx = [];
{
	_grpx = count(configFile >> "CfgGroups" >> _s >> _x >> _type);
	for "_y" from 1 to _grpx - 1 do {
		if (!(_x in _facx)) then {
			_facx = _facx + [_x];
		};
	};
} forEach _facs;
_facs = _facx;
};
//hint str _facs;
// pick random faction and validate
_fac = _facs select floor(random count _facs);
//if(!(_fac in _allfacs)) exitWith{player GlobalChat format["crB_randomGroup - ""%1"" not valid - %2", _fac, _allfacs];};

if(isNil "_side") then {
_sidex = getNumber(configFile >> "CfgFactionClasses" >> _fac >> "side");
_side = nil;
switch(_sidex) do {
	case 0: {
		_side = east;
	};
	case 1: {
		_side = west;
	};
	case 2: {
		_side = resistance;
	};
	case 3: {
		_side = civilian;
	};
};
};

//player globalChat format["Side %1 %2 Fctns: %3 This %4 Type %5", _side, _sidex, _facs, _fac, _type];

_grps = [];
_s = switch(_side) do {
	case resistance: {"Guerrila";};
	case civilian: {"Civilian";};
	default {str _side;};
};
//hint str _s;
_grpx = count(configFile >> "CfgGroups" >> _s >> _fac >> _type);
for "_y" from 0 to _grpx - 1 do {
_grps = _grps + [(configFile >> "CfgGroups" >> _s >> _fac >> _type) select _y];
};
//hint str _grps;
_grp = _grps select floor(random count _grps);

//hint format["%1 %2 %3", _pos, _side, _grp];
_spawnGrp = [_pos, _side, _grp] call BIS_fnc_spawnGroup;

if(_side == civilian) then {
[_spawnGrp, _pos] spawn {
	_spawnGrp = _this select 0;
	_pos = _this select 1;		
	while{true} do {
		_wp = _spawnGrp addWaypoint [_pos, 0];
		_wp setWaypointType "DISMISS";
		_wp setWaypointBehaviour "SAFE";
		sleep 300;
	};
};
};

//player globalChat format["type: %1 fac: %2", _type, _fac];
_spawnGrp;

BIN_taskPatrol.sqf


_grp = _this select 0;
_pos = _this select 1;
_max_dist = _this select 2;
_debug = if ((count _this) > 3) then {_this select 3} else {0};
_blacklist = if ((count _this) > 4) then {_blacklist = _this select 4} else {[]};

_mode = ["YELLOW", "RED", "GREEN"] call BIS_fnc_selectRandom;
_formation = ["LINE"] call BIS_fnc_selectRandom;

_grp setBehaviour "SAFE";
_grp setSpeedMode "NORMAL";
_grp setCombatMode _mode;
_grp setFormation _formation;

_center_x = (_pos) select 0;
_center_y = (_pos) select 1;
_center_z = (_pos) select 2;
if(isNil "_center_z")then{_center_z = 0;};

_wp_count = 4 + (floor random 3) + (floor (_max_dist / 100 ));
_angle = (360 / (_wp_count -1));

_new_angle = 0;
_wp_array = [];
_slack = _max_dist / 5.5;
if ( _slack < 20 ) then { _slack = 20 };

_angle_offset = random 360;
while {count _wp_array < _wp_count} do 
{
   private ["_x1","_y1","_wp_pos", "_prepos","_bldgpos","_bldgs"];

   _newangle = (count _wp_array * _angle) + _angle_offset;

   _x1 = _center_x - (sin _newangle * _max_dist);
   _y1 = _center_y - (cos _newangle * _max_dist);

   _prepos = [_x1, _y1, _center_z];
   if ( isNil "_center_z" ) then {
       _prepos = [_x1, _y1];
   };

   _wp_pos = [_prepos, 0, _slack, 6, 0, 50 * (pi / 180), 0, _blacklist] call BIS_fnc_findSafePos;

if (leader _grp isKindOf "Man") then {
    //////////////////////////////////////////////////////////////////
    // The following code is an extract from Random Building Position Script v1.0 by Tophe of Östgöta Ops
    //////////////////////////////////////////////////////////////////
    _bldgpos = [];
    _bldgs = nearestObjects [_wp_pos, ["Building"], 50];
    {
	  private["_i","_y"];
        _i = 0;
        _y = _x buildingPos _i;
        while {format["%1", _y] != "[0,0,0]"} do {
			_bldgpos = _bldgpos + [_y];
			_i = _i + 1;
			_y = _x buildingPos _i;
        };
    } forEach _bldgs;
    if(count _bldgpos != 0) then {_wp_pos = _bldgpos call BIS_fnc_selectRandom;};
    _wp_array = _wp_array + [_wp_pos];

    sleep 0.5;
};
};

sleep 1;

for "_i" from 1 to (_wp_count - 1) do
{
   private ["_wp","_cur_pos","_marker","_marker_name"];

   _cur_pos = (_wp_array select _i);

   // Create waypoints based on array of positions
   _wp = _grp addWaypoint [_cur_pos, 0];
   _wp setWaypointType "MOVE";
   _wp setWaypointCompletionRadius (5 + _slack);
   [_grp,_i] setWaypointTimeout [0, 2, 16];
   // When completing waypoint have 33% chance to choose a random next wp
   [_grp,_i] setWaypointStatements ["true", "if ((random 3) > 2) then { group this setCurrentWaypoint [(group this), (floor (random (count (waypoints (group this)))))];};"];

   if (_debug > 0) then {
       _marker_name = str(_wp_array select _i);
       _marker = createMarker[_marker_name,[_cur_pos select 0,_cur_pos select 1]];
       _marker setMarkerShape "ICON";
       _marker_name setMarkerType "DOT";
   };

   sleep 0.5;
};

// End back near start point and then pick a new random point
_wp1 = _grp addWaypoint [_pos, 0];
_wp1 setWaypointType "SAD";
_wp1 setwaypointCombatMode "GREEN";
_wp1 setWaypointBehaviour "SAFE";
_wp1 setWaypointCompletionRadius (random (_max_dist));
[_grp,(count waypoints _grp)] setWaypointStatements ["true", "group this setCurrentWaypoint [(group this), (round (random 2) + 1)];"];

// Cycle in case we reach the end
_wp2 = _grp addWaypoint [_pos, 0];
_wp2 setWaypointType "CYCLE";
_wp2 setWaypointCompletionRadius 100;

true

Share this post


Link to post
Share on other sites

you thought about adding the random patrol to an array and then count that every now and then? just a thought

Share this post


Link to post
Share on other sites

You need to add the killed eventHandler to each unit of the group, then do your respawn when all are dead. Or, are you trying to respawn each unit individually as they are killed?

Something along these lines:

//Add event Handler to each unit of group, do something when all are dead
{
  _x addEventHandler ["killed", 
  {  
     _grp = group (_this select 0);    
     if ({ alive _x } count units _grp < 1)
     {
        do your respawn stuff here;
     };
  }];
} forEach units _patrol;  

Edited by panther42

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  

×