Jump to content
Sign in to follow this  
bolbies

Random Unit Weapons?

Recommended Posts

Is there a way to spawn, let's say, a weapons squad but have the game read through your addons and randomly pick a weapon to give to the unit? So a vanilla rifleman would have an MX but with this he might spawn with an L85A2, M4, etc.

Share this post


Link to post
Share on other sites

Search for a mod called "All the Weapons"-I think that does what you're asking, if you want it as a script you could ask if he wouldn't mind letting you edit it.

Share this post


Link to post
Share on other sites

Yeah that's perfect! Too bad it isn't MP compatible yet though. I'll definitely keep an eye on it thank you!

Share this post


Link to post
Share on other sites

Here is a quick test piece...

//Get all available primary weapons from config
allPrimaryWeapons = "
( getNumber ( _x >> 'scope' ) isEqualTo 2
&&
{ getText ( _x >> 'simulation' ) isEqualTo 'Weapon'
&&
{ getNumber ( _x >> 'type' ) isEqualTo 1 } } )
"configClasses ( configFile >> "cfgWeapons" );


fnc_randomWeapon = {
_unit = _this;

//Get a random weapon from the list
_rndWeapon = configName ( allPrimaryWeapons select (floor (random (count allPrimaryWeapons ))) );

//If new weapon is not the same as the current weapon
if !( _rndWeapon isEqualTo primaryWeapon _unit ) then {

	//Get current weapons magazine types
	_currentMagazine = getArray ( configFile >> "CfgWeapons" >> primaryWeapon _unit >> "magazines" );

	//Remove all current weapon magazines
	{
		_unit removeMagazines _x;
	}forEach _currentMagazine;

	//Find new weapon default magazine type
	_magazine = ( getArray ( configFile >> "CfgWeapons" >> _rndWeapon >> "magazines" )) select 0 ;

	//Can the new mag type fit in the uniform ( large mg mags can not )
	if !( _unit canAddItemToUniform _magazine ) then {

		//If not
		//Move as many items as possible from vest to uniform
		//to make room for adding large magazines into vest

		//For evey item in the units vest
		{
			//If item can fit in uniform
			if ( _unit canAddItemToUniform _x ) then {
				//Remove the item
				_unit removeMagazineGlobal _x;
				//Add item to uniform
				_unit addItemToUniform _x;
			};
		}forEach vestItems _unit;

	};

   	//Add new mags before weapon so it get preloaded
	_unit addMagazines [ _magazine, 6 ];

   	//Add new weapon
	_unit addWeaponGlobal _rndWeapon;

	//If not a player
	if !( isPlayer _unit ) then {
		//Make unit swap back to their primary weapon
		_unit selectWeapon ( primaryWeapon _unit );
	};
};
};


//*****************************
//FOR TESTING
player addAction [ "New Weapon", { player call fnc_randomWeapon } ];

player addAction [ "Spawn Group", {
_groupCfg = configFile >> "CfgGroups" >> "West" >> "BLU_F" >> "Infantry" >> "BUS_InfSquad_Weapons";
_pos = [ player, 20, getDir player ] call BIS_fnc_relPos;
_newGroup = [ _pos, west, _groupCfg ] call BIS_fnc_spawnGroup;
{
	_x call fnc_randomWeapon;
}forEach units _newGroup;
}];

TEST MISSION

This could be enhanced quite alot. TBH this will break groups as in, you could have ammo bearers carrying ammo for a weapon the group does not posses, you could have a missile specialist over weight because they also carrying a HMG etc.

Ive just made as much room as possible for some mags by moving items from vest to uniform, you could maybe add backpacks to these units to increase mag counts etc.

I suppose the proper way to go about this would be, to not use cfgGroup groups and make your own by spawning certain unit types e.g autoriflemen, missle specialist and assistants and then rather than choosing from a whole list of primary weapons seperate them into types.

Edited by Larrow
intellisense fail

Share this post


Link to post
Share on other sites
Yeah that's perfect! Too bad it isn't MP compatible yet though. I'll definitely keep an eye on it thank you!

I've made changes that should make All The Weapons MP compatible and quite a bit faster. I'll update it tonight. It also sorts weapons by side so east gets the aks, west m4, etc. And assualt gets assault rifles, machine gunners get machine guns, snipers get the right weapons, etc. Plus I've introduced variables that let you access these lists in your own missions easily.

But you can't go wrong with larrows suggestion, he's a source of tons of great info and scripts. Absolute legend.

Edited by twisted

Share this post


Link to post
Share on other sites

Oh please do it! I ve been searching for an automated "all the weapons in an array depending on if they are assault rifles or sniper etc.." snippet for a long time. It's a pity isKindOf does not work with weapons!

Share this post


Link to post
Share on other sites
Oh please do it! I ve been searching for an automated "all the weapons in an array depending on if they are assault rifles or sniper etc.." snippet for a long time. It's a pity isKindOf does not work with weapons!

well you can actually do this quite easily thanks to a command by bis

_weaponType = [_primweapon] call BIS_fnc_itemType;

https://community.bistudio.com/wiki/BIS_fnc_itemType

pretty damn useful. i use it to categorise weapons and allocate to the right kinds of units.

edit - if you'd like to try out the update feel free to at http://forums.bistudio.com/showthread.php?189122-All-The-Weapons

Edited by twisted

Share this post


Link to post
Share on other sites

Oh my god it's great to hear from the guy who made the addon! I can't wait to test it out. Thank you all for your help!

Share this post


Link to post
Share on other sites

Is there any automated way to know the faction which belongs the weapon, you know, Katibas are CSAT weapons etc...

Share this post


Link to post
Share on other sites

Not that i can think of (it was something i experimented with before for Icemans DLRS), the closest i think you can get and this is for vanilla BIS weapons only is to check through the soldier classes for their default weapons. Even then it will not include all available weapon classes. Plus it takes quite a while to iterate around the CfgVehicles class to do this.

It is something i wish BIS would include as a standard class property much like they do for uniforms in their itemInfo where you can interrogate the default soldier class to get the side. Would make this kind of filtering so much easier than having to manually do it like twisted has (and then your job is never done as your always having to add mods).

Quick tester just for assault rifles

allPrimaryWeapons = [
[],
[],
[],
[],
[]
];
"
if ( getNumber ( _x >> 'scope' ) isEqualTo 2
&&
{ getText ( _x >> 'simulation' ) isEqualTo 'Weapon'
&&
{ getNumber ( _x >> 'type' ) isEqualTo 1 } } ) then {
	_cursor = tolower getText ( _x >> 'cursor' );
	_index = switch ( _cursor ) do {
		case 'arifle' : { 0 };
		case 'mg' : { 1 };
		case 'smg' : { 2 };
		case 'srifle' : { 3 };
		default { 4 };
	};
	_tmp = allPrimaryWeapons select _index;
	_tmp pushback configName _x;
	allPrimaryWeapons set [ _index, _tmp ];
};
"configClasses ( configFile >> "cfgWeapons" );



assaultRiflesBySide = [
[],
[],
[],
[]
];

{
_weapon = _x;
"
	if ( getNumber ( _x >> 'scope' ) isEqualTo 2
		&&
		{ getText ( _x >> 'vehicleClass') isEqualTo 'Men' }
	) then {
		_soldierClass = _x;
		if ( _weapon in getArray ( _soldierClass >> 'weapons' ) ) then {
			_side = getNumber ( _soldierClass >> 'side' );
			if !( _weapon in ( assaultRiflesBySide select _side ) ) then {
				_tmp = assaultRiflesBySide select _side;
				_tmp pushBack _weapon;
				assaultRiflesBySide set [ _side, _tmp ];
			};
		};
	};
	false
"configClasses ( configFile >> "CfgVehicles" );
}forEach ( allPrimaryWeapons select 0 );

Share this post


Link to post
Share on other sites
Is there any automated way to know the faction which belongs the weapon, you know, Katibas are CSAT weapons etc...

:yay: wikipedia :yay:

Share this post


Link to post
Share on other sites

You could do it a quite simplistic way but its pretty sloppy, put all west weapon classnames into an array, same for csat and aaf, then simply do a search through all three to figure out what side a weapon belongs to based on what array it is in.

Something like this could rip all the names for you, though you'll probably want to do a check to see if its a weapon such as 'arifle' using BIS_fnc_trimString so it doesn't get all the extra crap you don't need thats been dumped inside cfgWeapons like hats, uniforms, and vehicle turret weapons.

myVar = [];
myvar2 = []; 
myValStr = "";
myVar = (configfile >> "cfgWeapons") call BIS_fnc_getcfgsubclasses; 
{	
myvar2 set[count myvar2,_x];
} foreach myVar;
myvarStr = format["%1",myvar2];
hint format ["%1",myvarStr];
copytoclipboard myVarStr;

Edited by austin_medic

Share this post


Link to post
Share on other sites

Yes, I got it, the problem with trimString is my server init is now very loaded, and make that check will take a bit more time, I was wondering, as my init is messing now with the weapon configs, there was some param on them to know the side of the weapon.

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  

×