Jump to content
Sign in to follow this  
Dramacius

Help on selecting random building to spawn group of Ai in

Recommended Posts

Hi all. I am working on a script that can be run with an addAction to fill a town with AI units (currently all Redfor).

The intent of the script is to:
get an array of all the buildings in an area around a gamelogic called "TrainingMission"
select a random building from that list
spawn units in that building

set units to CBA_fnc_taskDefend
Then Loop for the number of buildings in array.

So far I am able to get an array of all the buildings in an area using

TZBuildings = nearestObjects [TrainingZone, ["Building"], 425];

but when I try to select a random building from that array it just gives me a huge list of buildings again.
I have tried the following codes:

randTZBuilding = selectRandom [TZBuildings];

and I have also tried:

randTZBuilding = [TZBuildings] call BIS_fnc_selectRandom;

I used

hint str(randTZBuilding);

to display the seleced building but it just gave me a huge long list.

The rest of my code is a non functional mess for spawning the groups right now so I wont burn your eyes with that.

Thanks for any help you can provode.

For reference full code of the selecting random building so far:

TZBuildings = nearestObjects [TrainingZone, ["Building"], 425];
randTZBuilding = [TZBuildings] call BIS_fnc_selectRandom;
hint str(randTZBuilding);

or

TZBuildings = nearestObjects [TrainingZone, ["Building"], 425];
randTZBuilding = selectRandom [TZBuildings];
hint str(randTZBuilding);

 

Share this post


Link to post
Share on other sites
21 minutes ago, Dramacius said:

randTZBuilding = selectRandom [TZBuildings];

 

 

You are selecting from an array of arrays (or array, as there is only one).

 

randTZBuilding = selectRandom TZBuildings;

 

Share this post


Link to post
Share on other sites
3 minutes ago, Harzach said:

 

You are selecting from an array of arrays (or array, as there is only one).

 


randTZBuilding = selectRandom TZBuildings;

 

Thank you, I will make the change and give it a test run. Out of curiosity which is the better way to select a random, using the BIS_fnc_selectRandom or selectRandom?

It will be for a multiplayer server if that makes a difference.

Share this post


Link to post
Share on other sites

again thank you so much for your first bit of help I am now trying to get a group to spawn at that location and I have the following code but nothing spawns, I am sure I am doing something just a simple wrong again but of course it could all just be a cluster of bull.
 

/*Goals of Training Mission:
* Have a number of hostile groups spawn into the area = to the number of buildings in random buildings (some buildings containing multiple groups some buildings remain empty)
	** 70% of the groups to remain in the buildings and CBA_fnc_taskDefend 30% to leave the buildings and CBA_fnc_modulePatrol upto 150m from spawn location
*Have a number of hostile vehicle groups = 1/10 the number of buildings spawn in in random locations within the zone and CBA_fnc_modulePatrol upto 300m from spawn location
* Have a number of civilians randomly wandering around the area = 1/2 the number of buildings in the day and 1/4 the number of buildings at night.
* Spawn an objective in a random location, eventually random from a list of:
	** Hostage Rescue and extraction
	** Capture HVT
	** Search and Destroy object
	** Maybe others
*/

/* --------------------------------------------------------------------------------------- */

/* Place a marker on the map where you want the Training Zone to exist and set its variable name to "TrainingZone". */

//Start of Script

TZBuildings = nearestObjects [TrainingZone, ["Building"], 450];
TZBuildingsCount = count ITZBuildings;
TZSpawns = (TZBuildingsCount * 0.15);
// hint TZBuildingsCount
randTZBuilding = selectRandom TZBuildings;

JaffaGRY = ["sga_jaffa", "sga_jaffa_black", "sga_jaffa_serpent_guard_closed", "sga_jaffa_serpent_guard_open"];
JaffaBLK = ["sga_jaffa_black", "sga_jaffa_black_leader", "sga_jaffa_black_serpent_guard_closed", "sga_jaffa_black_serpent_guard_open"];
JaffaGLD = ["sga_jaffa_gold", "sga_jaffa_gold_leader", "sga_jaffa_gold_serpent_guard_closed", "sga_jaffa_gold_serpent_guard_open"];
JaffaRED = ["sga_jaffa_red", "sga_jaffa_red_leader", "sga_jaffa_red_serpent_guard_closed", "sga_jaffa_red_serpent_guard_open"];

JaffaGroups = ["JaffaGRY", "JaffaBLK", "JaffaGLD", "JaffaRED"];
randJaffaGroup = selectRandom JaffaGroups;

TZHostileTasks = ["getPos randTZBuilding, 100] call CBA_fnc_taskPatrol", "getPos randTZBuilding, 50] call CBA_fnc_taskDefend"];
randTZHostileTask = selectRandom TZHostileTasks;

// begin loop

for "_i" from 1 to TZSpawns do {

TZHostileGRP_i = [getPos randTZBuilding, East, [randJaffaGroup], [], [], [0.5, 0.75], [],[] , 0, false, 0] call BIS_fnc_spawnGroup;
[TZHostileGRP_i, getPos randTZBuilding, 100] call CBA_fnc_taskDefend;

};
// end loop
hint "Spawning Hostile Forces.";

Nothing spawns though

Share this post


Link to post
Share on other sites

If it's of help to anyone here is my working code as it stands.

 

I will still be making changes to it for my own use such as adding tasks and the like. A to-do list is at the bottom.

 

//Start of Script

TZBuildings = nearestTerrainObjects [TrainingZone, ["HOUSE", "BUILDING", "HOSPITAL", "CHAPEL", "CHURCH", "FORTRESS", "FUELSTATION", "RUIN"], 450];
TZBuildingsCount = count TZBuildings;
TZHostileSpawns = (TZBuildingsCount * 0.15);
TZCivSpawns = (TZBuildingsCount * 0.05);


//Hostile Spawns
hint "Spawning Hostile Forces!";
// begin loop

for [{_i = 0}, {_i < TZHostileSpawns}, {_i = _i + 1 }] do {
randTZBuilding = selectRandom TZBuildings;

_hostileGroup = ["sga_infantry_goauld_group_red4","sga_infantry_goauld_group_gold4","sga_infantry_goauld_group_black4","sga_infantry_goauld_group_grey4"] call BIS_fnc_selectRandom;
sleep 0.1;
TZHostileGRP = createGroup east;
TZHostileGRP = [getPos randTZBuilding, east, (configfile >> "CfgGroups" >> "East" >> "sga_goauld" >> "Infantry" >> _hostileGroup)] call BIS_fnc_spawnGroup;
[TZHostileGRP, getPos randTZBuilding, 50, 3, 0.75,0.5] call CBA_fnc_taskDefend;
sleep 1;
};

// end loop
sleep 1;

//Civilian Spawns
hint "Spawning Civilians, Check your shots!";
// begin loop

for [{_i = 0}, {_i < TZCivSpawns}, {_i = _i + 1 }] do {
randTZBuilding = selectRandom TZBuildings;

TZCivilian = createGroup Civilian;
TZCivilian = [getPos randTZBuilding, Civilian, ["C_man_1", "C_man_polo_1_F", "C_man_polo_2_F", "C_man_polo_4_F", "C_man_polo_5_F_euro", "C_man_polo_6_F_afro", "C_man_polo_6_F_asia", "C_man_p_beggar_F", "C_man_w_worker_F", "C_man_p_shorts_1_F_afro", "C_journalist_F", "C_Orestes", "C_Nikos", "C_Nikos_aged"]] call BIS_fnc_spawnGroup;
[TZCivilian, getPos randTZBuilding, 200, 10, "Move", "Careless", "Red", "Normal", "", "", [3, 6, 9]] call CBA_fnc_taskPatrol;
};

// end loop
sleep 1;

hint "Mission Ready!";
/* Todo:
    Create task(s) to complete:
        Spawn a random objective in a random location:
            Hostage Rescue and extraction
            Capture HVT
            Search and Destroy object
            Maybe others
*/

  • Like 1

Share this post


Link to post
Share on other sites

Obviously I am happy for anyone to support, or provide constructive criticisms to the above code. I am a complete noob and knew none of the coding language before writing this. I have had help from various people around the internet but have learnt a lot by doing it.

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  

×