Jump to content
Sign in to follow this  
MrPineapple

garrison units script - random place units in buildings

Recommended Posts

Here is a simple script that spawns units in buildings randomly.  I got tired of hand placing units with the 3d editor.

 

// populates building positions with units according to parameters
/*
	0: center position to search for houses (position)
	1: radius to search for (number)
	2: spawn chance to spawn unit in house position  (0.0-1.0)
	3: max number of units to spawn (number)
	4: array of classnames to spawn  (array of strings)
	5: side of units to spawn (side)
	6: OPTIONAL: DisableAI "MOVE"  (bool)  // stops ai from moving
	7: OPTIONAL: SetUnitPos "UP"  (bool) // keeps unit standing 
*/

Private ["_basePos", "_radius", "_spawnChance", "_maxUnits", "_aryManClass", "_side", "_disableMove", "_enableStanding"];
Private ["_spawnedUnits", "_aryHouse", "_aryHousePos", "_aryUnits", "_grp"];

Params [["_basePos",[0,0,0],[[]],3], ["_radius",100,[0]], ["_spawnChance",0.5,[0.0]], ["_maxUnits",10,[0]], ["_aryManClass",["I_Soldier_F"],[[]]], ["_side",resistance,[west]], ["_disableMove",false,[false]], ["_enableStanding",false,[false]]];

_spawnedUnits = 0;  // holds total number of spawned units


// get array of nearby houses
_aryHouse = [];

_aryHouse = NearestObjects [_basePos, ["House"], _radius];
_aryHousePos = [];

// get array of house positions for all houses
{
	Private ["_house", "_ary"];
	_house = _x;
	_ary = [_x] Call BIS_fnc_BuildingPositions;
	if (Count _ary > 0) then { _aryHousePos = _aryHousePos + _ary; };

} foreach _aryHouse;

// shuffle house positions
_aryHousePos = _aryHousePos Call BIS_fnc_ArrayShuffle;

_aryUnits = [];

// create random units for each of the remaining house positions
_grp = CreateGroup _side;
_grp EnableAttack false;  // leader won't issue attack commands

{
	Private ["_pos", "_manClass", "_man"];
	_pos = _x;
	if ( ((Random 100)/100) < _spawnChance ) then
	{
		if (_spawnedUnits < _maxUnits) then
		{
			_spawnedUnits = _spawnedUnits + 1;
			_manClass = _aryManClass Call BIS_fnc_SelectRandom;
			_man = _grp CreateUnit [_manClass, _pos, [], 0, "NONE"];
			_man SetPos _pos;
			_man SetDir (Random 360);
			DoStop _man;
			if (_disableMove) then {_man DisableAI "MOVE"};
			if (_enableStanding) then {_man SetUnitPos "UP"};
			_aryUnits Set [(Count _aryUnits), _man];
			//SystemChat str(_spawnedUnits);
		};
	};
	// terminate extra iterations if max units reached
	if (_spawnedUnits >= _maxUnits) Exitwith {};
	
} foreach _aryHousePos;


_aryUnits

 

here is how to execute the script:

assuming you named the script "garrison_units.sqf"

 

Parameters:

0: center position to search for houses (position)

1: radius to search for (number)

2: spawn chance to spawn unit in house position  (0.0-1.0)

3: max number of units to spawn (number)

4: array of classnames to spawn  (array of strings)

5: side of units to spawn (side)

6: OPTIONAL: DisableAI "MOVE"  (bool)  // stops ai from moving

7: OPTIONAL: SetUnitPos "UP"  (bool) // keeps unit standing

 

Returns: array of spawned units

if (IsServer) then
{
	_units = [GetPos Player, 100, 0.5, 50, ["I_Soldier_F","I_Soldier_AR_F","I_Soldier_GL_F"], resistance] Call Compile 			PreprocessFileLineNumbers "garrison_units.sqf";

	{ _x SetSkill["AimingAccuracy",0.0]; } foreach _units;
};

 

I believe you should run the script server side only

  • Like 1

Share this post


Link to post
Share on other sites

Params already privates your variables, so this:

Private ["_basePos", "_radius", "_spawnChance", "_maxUnits", "_aryManClass", "_side", "_disableMove", "_enableStanding"];  //  REDUNDANT
Private ["_spawnedUnits", "_aryHouse", "_aryHousePos", "_aryUnits", "_grp"];

Params [["_basePos",[0,0,0],[[]],3], ["_radius",100,[0]], ["_spawnChance",0.5,[0.0]], ["_maxUnits",10,[0]], ["_aryManClass",["I_Soldier_F"],[[]]], ["_side",resistance,[west]], ["_disableMove",false,[false]], ["_enableStanding",false,[false]]];

can be this:

Params [
["_basePos",[0,0,0],[[]],3], 
["_radius",100,[0]], 
["_spawnChance",0.5,[0.0]], 
["_maxUnits",10,[0]], 
["_aryManClass",["I_Soldier_F"],[[]]], 
["_side",resistance,[west]], 
["_disableMove",false,[false]], 
["_enableStanding",false,[false]],
"_spawnedUnits", 
"_aryHouse", 
"_aryHousePos", 
"_aryUnits", 
"_grp"
];

(expanded vertically for clarity, I prefer to write complex arrays this way)

Share this post


Link to post
Share on other sites

Zens occupy house script works great. Try his, it places units facing out at windows and in kneeling position in random positions even rooftop if necessary. 

Share this post


Link to post
Share on other sites

When I try to use it gives "wrong number in expression" error. 

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  

×