Jump to content
  • 0

Random numbers of magazines


BetterDeadThanZed

Question

I'm working on an AI mission and I want to have a random number of magazines in the for the weapon that is in the box. The mission is a sniper mission and there's only one rifle in the box, with ammo. Here's what I have so far:

 

Random rifle is selected:

_rifle = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1","SVD","M24","M24_des_EP1","vil_SV_98","vil_G3sg1b","vil_G3sg1","vil_SVDK","vil_SVD_63","vil_SVD_M","vil_SVD_N","vil_SVD_P21","vil_SVD_S","USSR_cheytacM200","USSR_cheytacM200","RH_m21","vil_M21G"] call loadout_fnc_selectRandom;

Magazine list is generated based on the weapon that is selected:

_magazinelist = _rifle call find_suitable_ammunition;

This is the part I'm not too sure about. I'm using someone's code as an example, but it's a little outside my experience. To generate a random number of magazines (defined by _magazinelist), I am going to use this code:

_weaponrnd = floor random (count _magazinelist);
_ammo = _magazinelist select _weaponrnd;

If I understand this correct, _weaponrnd will generate a random number of magazines and _ammo can be used in the mission as loot in the box and will put _weaponrnd number of _ammo magazines in the box. Is that correct?

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

hey,

 

I do not know if it helps you, is for arma 3, but actually the scripts are still the same...

here:

//special for magazines: spawn 1-6 // from the lootspawner // full part magazine

                                    };
                                    //special for magazines: spawn 1-6
                                    if(_lootType == 2) exitWith {
                                        _lootholder = createVehicle ["GroundWeaponHolder", _tmpPos, [], 0, "CAN_COLLIDE"];
                                        _randChance = 1 + floor(random(5));
                                        for "_rm" from 0 to _randChance do {
                                            _selecteditem = (floor(random(count((lootMagazine_list select _lootClass) select 1))));
                                            _loot = (((lootMagazine_list select _lootClass) select 1) select _selecteditem);
                                            _lootholder addMagazineCargoGlobal [_loot, 1];
                                        };
                                        _lootholder setdir (random 360);
                                        _lootholder setPosATL _spwnPos;
                                    };

 

or special for crates:

 

    _box addItemCargoGlobal ["MortarBucket", 4 + (random 2)];  // between 4-6
    _box addItemCargoGlobal ["Cinderblocks", 8 + (random 4)]; // between 8-12
    _box addItemCargoGlobal ["CircuitParts", (random 1)]; // between 0-1

 

hope it helps at all :D

 

Suppe

Link to comment
Share on other sites

  • 0

if you are not sure what is the outcome then just simply let it display to you for script on client side:

systemChat format['_myvariable: %1',_myvariable];

or to get it to your rpt:

diag_log format['_myvariable: %1',_myvariable];

Otherwise simply add a number for your own at let pick a random from it:

_mags = round (random 10);

or you can use this instead which checks the weapon of the unit and gives the right ammo:

 


_mags = getArray (configFile >> 'CfgWeapons' >> primaryWeapon _unit >> 'magazines');
				_mag = _mags select 0;
				for "_i" from 0 to (random 3) do {_unit addMagazine _mag;};

or instead of primaryWeapon you could use currentWeapon _unit

Link to comment
Share on other sites

  • 0

Thanks, SchwEde. I will try the round (random 10); The other code you posted seems to be intended for giving magazines to units. Right now I am working on filling a supply box with a random primary and secondary weapon, along with a random amount of ammo for those weapons. This is for a sniper mission where they have a box with a spare sniper rifle and ammo for that rifle.

Link to comment
Share on other sites

  • 0

Hi,

You do not mention which AI Missions system you are using.

If you are using DZMS then you could add this to DZMSBox.sqf:

if (_type == "sniper_crate") then {
        _sniperriflearray = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1"];
        _sniperrifle = _sniperriflearray select (floor (random (count _sniperriflearray)));  //select a rifle from the array
        _snipermagazine = getArray (configFile >> "CfgWeapons" >> _sniperrifle >> "magazines") select 0;  //select the mag for rifle
        _magcount = floor(random 5) + 3;  //range min 3 max 7
        _crate addWeaponCargoGlobal [_sniperrifle,1];  //add the rifle
        _crate addMagazineCargoGlobal [_snipermagazine,_magcount];  //adds the rnd num mags
    };



and call it from a mission with:

[_crate,"sniper_crate"] ExecVM DZMSBoxSetup;

I hope this helps.

Link to comment
Share on other sites

  • 0

Ok, been working on this some more based on the feedback. Here's the arrays that determine what sniper rifle is in the box:

_primarylistarray = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1","SVD","M24","M24_des_EP1","vil_SV_98","vil_G3sg1b","vil_G3sg1","vil_SVDK","vil_SVD_63","vil_SVD_M","vil_SVD_N","vil_SVD_P21","vil_SVD_S","USSR_cheytacM200","USSR_cheytacM200","RH_m21","vil_M21G"] call loadout_fnc_selectRandom;
_primaryrifle = _primarylistarray select (floor (random (count _primarylistarray)));  //select a rifle from the array
_primarymagazine = getArray (configFile >> "CfgWeapons" >> _primaryrifle >> "magazines") select 0;  //select the mag for rifle
_magcount  = floor(random 5) + 3;  //range min 3 max 7

The loot box spawner for fmission is this:

_loot_lists = [
[
[_primary,_assorted],
[_magazine,_shelter,_food]
]
];

How would I get the number of magazines, as determined by _magcount to be put in the box? Would it be _magazine count _magcount?

Link to comment
Share on other sites

  • 0

Ok, altered more code to make it easier for me to understand. Here's the arrays that determine the weapon, ammo and number of mags:

_primarylist = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1","SVD","M24","M24_des_EP1","vil_SV_98","vil_G3sg1b","vil_G3sg1","vil_SVDK","vil_SVD_63","vil_SVD_M","vil_SVD_N","vil_SVD_P21","vil_SVD_S","USSR_cheytacM200","USSR_cheytacM200","RH_m21","vil_M21G"];
_primaryrifle = _primarylist select (floor (random (count _primarylist)));  //select a rifle from the array
_primarymagazine = getArray (configFile >> "CfgWeapons" >> _primaryrifle >> "magazines") select 0;  //select the mag for rifle
_primarymagcount = floor(random 5) + 3;  //range min 3 max 7
_primary = _primaryrifle;
_magazine = _primarymagazine select _primarymagcount;

The server log is showing this error:

13:50:02 Error in expression <maryrifle;
_magazine = _primarymagazine select _primarymagcount;

_assortedlist >
13:50:02   Error position: <select _primarymagcount;

_assortedlist >
13:50:02   Error select: Type String, expected Array,Config entry
13:50:02 File z\addons\dayz_server\addons\Fmission\Missions\M1\Foamy\FMission_M1_Sniper.sqf, line 85

Line 85 is this:

_magazine = _primarymagazine select _primarymagcount;

Thanks in advance for any help you guys can provide!

Link to comment
Share on other sites

  • 0
Maybe you can try something like this for this purpose:
 

 

_primarylist = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1","SVD","M24","M24_des_EP1","vil_SV_98","vil_G3sg1b","vil_G3sg1","vil_SVDK","vil_SVD_63","vil_SVD_M","vil_SVD_N","vil_SVD_P21","vil_SVD_S","USSR_cheytacM200","USSR_cheytacM200","RH_m21","vil_M21G"];
_primaryrifle = _primarylist select (floor (random (count _primarylist)));  //select a rifle from the array
_primarymagazine = getArray (configFile >> "CfgWeapons" >> _primaryrifle >> "magazines");  //select the mag for rifle
_primarymagazine = _primarymagazines select 0; //keep it simple

 

for "_i" from 3 to (random 6) do {_crate addMagazine _primarymagazines;}; //set your crate in here | maybe change the numbers could be more 4-7 then actually 3-7
Link to comment
Share on other sites

  • 0

try this:

_primarymagazine = (getArray (configFile >> "CfgWeapons" >> _primaryrifle >> "magazines")) select 0;

not sure though

 

Are you trying to add a random amount of ammo for each weapon which is in the box?

 

Yes, I am creating a random number of magazines to go in the crate. This is where the crate is filled:

_loot_lists = [
[
[_primary,_assorted],
[_magazine,_shelter,_food]
]
];
Link to comment
Share on other sites

  • 0

so i guess this should a solution for every mission, seems pretty comlicated for me to be honest.

I would make it a little bit simpler, but that is your choice ^^

 

so you want this array filled with stuff and then the outcome should be something like:

_loot_lists = [
[
[["Weapone1,"Weapon2","Weapon3"],["GPS","Map","Toolbox"]],
[["MagforWeap1","","",],["","","",""],["","",""]]
]
];
Link to comment
Share on other sites

  • 0

try this:

_primarymagazine = (getArray (configFile >> "CfgWeapons" >> _primaryrifle >> "magazines")) select 0;

 

Gave this a shot and it still didn't make a difference. Hopefully someone that has messed with AI systems can chime in with what I can do. I don't want to create a set of X loadouts for the box and have one selected. I want the box to be truely random.

Link to comment
Share on other sites

  • 0

Hi,

 

The error in Line 85 is because the code is expecting an array, with the select command, but it is reporting _primarymagazine as a string (classname).

 

I have not really used Fmission and this code is not tested but might be worth a try. It is where I would start if I was doing the edit I think you are trying to do.

 

/*
	File Name: FMission_M1_Assassination.sqf
	File Created: 2/26/2014
	File Version: 1.2
	File Author: Foamy 
	File Last Edit Date: 3/26/2014
	File Description: Assassination Mission (M1)
*/

// CHECK FOR ANOTHER MISSION 1 RUNNING START ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	if (isNil "EPOCH_MISSION1_RUNNING") then 
	{
	EPOCH_MISSION1_RUNNING = false;
	};

	if (EPOCH_MISSION1_RUNNING) exitWith 
	{
	diag_log("MISSION 1 already running");
	};

// CHECK FOR ANOTHER MISSION 1 RUNNING END ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

private ["_missionSpawnChance","_spawnMarker","_spawnRadius","_markerRadius","_markerColor","_loot","_loot_lists","_loot_box","_wait_time","_spawnRoll","_position","_loot_pos","_base","_aiunit_spawn_pos","_aivehicle_spawn_pos","_aiheli_spawn_pos","_targetunitsAlive","_targetgroup","_target1","_attackgroup","_attacker1","_attacker2","_aiunit1","_aiunit2","_aiunit3","_aiunit4","_aiunit5","_timeout"];

diag_log("MISSION 1: Assassination - Script Started");

_missionSpawnChance =  .40;
_spawnRoll = random 1;

diag_log("MISSION 1: Assassination - Checking MissionSpawnChance");
if (_spawnRoll > _missionSpawnChance) exitWith 
{
diag_log format ["MISSION 1: Assassination - Failed with %1 it needed to be lower than %2",_spawnRoll,_missionSpawnChance];
};

sleep .5;
diag_log("MISSION 1: Assassination - Mission SpawnChance Success");

sleep .5;
diag_log("MISSION 1: Assassination - Mission Script Started");

EPOCH_MISSION1_RUNNING = true;
/*
//_loot_box = "GuerillaCacheBox";
//_loot_lists = [
[
["M9SD","ItemMachete","Binocular"],
["15Rnd_9x19_M9SD","15Rnd_9x19_M9SD","15Rnd_9x19_M9SD","ItemGoldBar","ItemGoldBar","ItemGoldBar","ItemGoldBar","ItemTrashToiletpaper"],
["DZ_CivilBackpack_EP1"]
],
[
["DMR","NVGoggles","Binocular_Vector"],
["20Rnd_762x51_DMR","20Rnd_762x51_DMR","20Rnd_762x51_DMR","ItemSodaMdew","PipeBomb","ItemAntibiotic","ItemGoldBar10oz"],
["DZ_GunBag_EP1"]
],
[
["MP5SD","ItemFlashlightRed","ItemCompass"],
["30Rnd_9x19_MP5SD","30Rnd_9x19_MP5SD","30Rnd_9x19_MP5SD","ItemSodaMdew","ItemWire","ItemSilverBar10oz","ItemSilverBar10oz","ItemSilverBar10oz"],
["DZ_TerminalPack_EP1"]
],
[
["SVD_CAMO","ItemWatch","Binocular"],
["10Rnd_762x54_SVD","10Rnd_762x54_SVD","10Rnd_762x54_SVD","ItemSodaMdew","ItemBurlap","ItemGoldBar","ItemGoldBar","ItemGoldBar"],
["DZ_Backpack_EP1"]
],
[
["M4A1_AIM_SD_CAMO","ItemGPS","Binocular"],
["30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","30Rnd_556x45_StanagSD","ItemSodaRbull","ItemGoldBar10oz","ItemGoldBar10oz"],
["DZ_ALICE_Pack_EP1"]
]
];
_loot = _loot_lists call BIS_fnc_selectRandom;
*/

_sniperriflearray = ["SVD_CAMO","M40A3","M14_EP1","SVD_des_EP1"];
_sniperrifle = _sniperriflearray select (floor (random (count _sniperriflearray)));  //select a rifle from the array
_snipermagazine = getArray (configFile >> "CfgWeapons" >> _sniperrifle >> "magazines") select 0;  //select the mag for rifle

mission_despawn_timer = 1200;
_wait_time = 900;
_spawnRadius = 5000;
_spawnMarker = 'center';
_markerRadius = 350; // Radius the loot can spawn and used for the marker
_markerColor = "ColorBlue";
_textMarker_IconType = "mil_objective";
_markerText = "Mission: Assassination";
_markerBrush = "SOLID";

// Random location
_position = [getMarkerPos _spawnMarker,0,_spawnRadius,10,0,20,0] call BIS_fnc_findSafePos;
_loot_pos = [_position,0,(_markerRadius - 100),10,0,20,0] call BIS_fnc_findSafePos;

// FMarker Launch
[_position,_loot_pos,_markerRadius,_markerColor,false,_textMarker_IconType,_markerText,_markerBrush] execVM "\z\addons\dayz_server\addons\FMission\FMarker\FMarker1.sqf";

diag_log(format["MISSION 1: Assassination - Spawning loot event at %1", _position]);
 
_aiunit_spawn_pos = [_loot_pos, 30, 50, 30, 0, 20, 0] call BIS_fnc_findSafePos;
_aivehicle_spawn_pos = [_loot_pos, 30, 50, 30, 0, 20, 0] call BIS_fnc_findSafePos;
_aiheli_spawn_pos = [_loot_pos, 30, 50, 30, 0, 20, 0] call BIS_fnc_findSafePos;
	
_base = createVehicle ["Land_fortified_nest_big",_loot_pos,[], 0, "CAN_COLLIDE"];
_base setPos _loot_pos;

sleep 1;

diag_log("MISSION 1: Assassination - Loading -=FAI Units=- Started");

_aiunit1 = [_aiunit_spawn_pos,50,4,"Bandit",_loot_pos,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Unit.sqf";

_aiunit2 = [_aiunit_spawn_pos,50,4,"Sniper",_loot_pos,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Unit.sqf";

_aiunit3 = [_aiunit_spawn_pos,50,4,"Medic",_loot_pos,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Unit.sqf";

_aiunit4 = [_aiunit_spawn_pos,50,4,"Bandit",_loot_pos,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Unit.sqf";

_aiunit5 = [_aiunit_spawn_pos,50,4,"Bandit",_loot_pos,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Unit.sqf";

sleep 10;
diag_log("MISSION 1: Assassination - Loading -=FAI=- Completed");

sleep .5;
diag_log("MISSION 1: Assassination - Loading -=FAI Vehicle=- Started");
_aiVehicle = [_aivehicle_spawn_pos,_loot_pos,150,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Vehicle.sqf";
sleep 1;
diag_log("MISSION 1: Assassination - Loading -=FAI Vehicle=- Completed");

sleep .5;
diag_log("MISSION 1: Assassination - Loading -=FAI Heli=- Started");
_aiHeli = [_aiheli_spawn_pos,_loot_pos,250,"M1"] execVM "\z\addons\dayz_server\addons\FMission\FAI\FAI_Create_Heli.sqf";
sleep 1;
diag_log("MISSION 1: Assassination - Loading -=FAI Heli=- Completed");

sleep 1;

diag_log("MISSION 1: Assassination - Creating Assassination Target");
// Add Assassination Target
_targetgroup = createGroup civilian;
		_target1 = _targetgroup createUnit ["Hooker4", _base, [], 0, "Form"];
			_target1 setUnitPos "DOWN";
			_target1 disableAI "MOVE";
			_target1 setCaptive true;
			removeAllItems _target1;
			
// Add Interior Guards
diag_log("MISSION 1: Assassination - Spawning Interior Attackers");
	_attackgroup = createGroup east;
	
		_attacker1 = _attackgroup createUnit ["TK_INS_Soldier_3_EP1", _base, [], 0, "Form"];
			_attacker1 setUnitPos "AUTO";
			_null = [_attacker1] execVM "\z\addons\dayz_server\addons\FMission\FLoot\FLoot_Bandit.sqf";
			_attacker1 setUnitPos "Middle";
		
		_attacker2 = _attackgroup createUnit ["TK_Soldier_Medic_EP1", _base, [], 0, "Form"];
			_attacker2 setUnitPos "AUTO";
			_null = [_attacker2] execVM "\z\addons\dayz_server\addons\FMission\FLoot\FLoot_Medic.sqf";
			_attacker2 setUnitPos "Middle";
			
			_attackgroup setCombatMode "RED";
			_attackgroup setBehaviour "STEALTH";

// Add a chance to spawn a Mine Field
[_loot_pos,east,.50] execVM "\z\addons\dayz_server\addons\FMission\FMinefield\FMinefield.sqf";

[nil,nil,rTitleText,"MISSION: Assassination - A high profile target has been located at a temporary camp. Assassinate the target before they leave the area!", "PLAIN",10] call RE;			
			
diag_log("MISSION 1: Assassination - Waiting...");
_timeout = time + mission_despawn_timer;
waitUntil 
{
_targetunitsAlive = {alive _x} count (units _targetgroup);
((time > _timeout) || (_targetunitsAlive < 1))
};

_targetunitsAlive = {alive _x} count (units _targetgroup);
if (_targetunitsAlive < 1) then
{
[nil,nil,rTitleText,"MISSION: Target Eliminated, Good Job.", "PLAIN",10] call RE;

		// Create loot box
		diag_log("MISSION 1: Assassination - Target eliminated creating loot box");
		_loot_box = createVehicle [_loot_box,_loot_pos,[], 0, "NONE"];
		_loot_box setPos _loot_pos;
		clearMagazineCargoGlobal _loot_box;
		clearWeaponCargoGlobal _loot_box;
		clearBackpackCargoGlobal _loot_box;
				 
		// Add loot
		diag_log("MISSION 1: Assassination - Loot box created, adding loot to loot box");
		/*
		{
		_loot_box addWeaponCargoGlobal [_x,1];
		} forEach (_loot select 0);
		{
		_loot_box addMagazineCargoGlobal [_x,1];
		} forEach (_loot select 1);
		{
		_loot_box addBackpackCargoGlobal [_x,1];
		} forEach (_loot select 2);
		*/
		_loot_box addWeaponCargoGlobal [_sniperrifle,1];
		_loot_box addMagazineCargoGlobal [_snipermagazine,(floor(random 5) + 3)];	
		// Wait
		sleep _wait_time;
 
		// Clean up
		EPOCH_MISSION1_RUNNING = false;
		deleteVehicle _base;
		deleteVehicle _loot_box;
		{ deleteVehicle _x } forEach units _targetgroup;
		deleteGroup _targetgroup;
		{ deleteVehicle _x } forEach units _attackgroup;
		deleteGroup _attackgroup;
		diag_log("MISSION 1: Assassination - Script Finished");
}			
else
{
[nil,nil,rTitleText,"MISSION: Assassination Failed - The target escaped.", "PLAIN",10] call RE;
		// Clean up
		EPOCH_MISSION1_RUNNING = false;
		deleteVehicle _base;
		{ deleteVehicle _x } forEach units _targetgroup;
		deleteGroup _targetgroup;
		{ deleteVehicle _x } forEach units _attackgroup;
		deleteGroup _attackgroup;
		diag_log("MISSION 1: Assassination - Script Finished");
};

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...