Jump to content
Sign in to follow this  
Von Quest

Script to ADD Random Items and Random Pistol to Units from List?

Recommended Posts

I have another challenge for you wizards out there. I've found several Random systems out there

but can not get this working for ADDING Random things into the Units Loadout at creation.

I'm trying add Random Items from a set list. It needs to be a random range from 0-5 items in their

inventory. Also using same idea, add a random pistol to each (or class) Unit as well with this.

Would be great for searching the dead Units and checking for items and intel they may have.

I'm using Scorch's Inventory & Robert Hammer's Pistol's as an example. This one is just out of reach!

[also, this may be the kicker though, it NEEDS to work in MP over my LAN so the Items can match]

randomItems.sqf

//Random Pistol (or weapon) per each Unit, PLUS Random Items from Zero to Five
//WIP - Using RobertHammer's Pistol Pack & Scorch's Inventory - WIP EXAMPLE
///////////////////////////////////////////////////////////////////////////////
_unit = _this;

_weapons = ["RH_deagle","RH_Mak","RH_usp","RH_g19"];
_magazines = ["RH_7Rnd_50_AE","RH_8Rnd_9x18_Mak","RH_12Rnd_45cal_usp","RH_17Rnd_9x19g17"];
_items = ["sc_cellphone","sc_transmitter","sc_cigarettepack","sc_money","sc_lighter"]

_i = random floor count _weapons;
_c = 3; // Magazine Count
_t = random floor count _items;
_m = random (0 - 5);  //???? need it to pick random between 0 and 5 items ???

//Matching Magazine for Random Pistol???
removeSecondaryWeapon _unit;
while{_c > 0}do{
 _unit addMagazine (_magazines select _i);
 _c = _c - 1;
};

//Add 1 Pistol and the Random Items here?
_unit addScondaryWeapon (_weapons select _i);
_unit addItems (_items select _t);

Then to call I guess you would just run in the init.sqf at startup?

execVM "scripts\mission\randomItems.sqf";

Edited by Goblin
spelling

Share this post


Link to post
Share on other sites

Here's what I've used before. Might find something useful.

private ["_muzzles","_weapon","_pw","_item","_unit","_weaponChance","_itemChance","_clearAll","_weaponRoll","_itemRoll","_weaponList","_itemList","_defaultItems"];

// Called via: nul = [this] execVM "randomWeapons.sqf";
// Default 60% chance for weapons and items and clears previous items replacing them with Map, Watch, Compass and Radio.  Adjust _defaultItems to change that.
// Optional fields: nul = [this, 100, 30, false] execVM "randomWeapons.sqf" would always give a weapon, 30% give items and let you keep items you had before.
//
// Code based on ideas from SuMatra and Xeno.

// Declare unit and options from input arguments.
_unit = _this select 0;
_weaponChance = if (count _this > 1) then {_this select 1} else {60};
_itemChance = if (count _this > 2) then {_this select 2} else {60};
_clearAll = if (count _this > 3) then {_this select 3} else {true};

// Make the rolls to see if an item is given.
_weaponRoll = ceil (random 100);
_itemRoll = ceil (random 100);
//player sideChat format["Weapon: %1/%3 Item: %2/%4", _weaponRoll, _itemRoll, _weaponChance, _itemChance];

// List of weapons matched up with their ammo.
_weaponList = [
   ["arifle_Katiba_ARCO_F", "30Rnd_65x39_caseless_green"],
   ["arifle_Katiba_C_F", "30Rnd_65x39_caseless_green"],
   ["arifle_MK20_GL_F", "30Rnd_556x45_Stanag_Tracer_Red"],
   ["arifle_MX_SW_F", "100Rnd_65x39_caseless_mag_Tracer"],
   ["arifle_TRG20_Holo_F", "30Rnd_556x45_Stanag_Tracer_Red"]
];

// List of possible reward items.
_itemList = ["FirstAidKit","B_Uavterminal","muzzle_snds_M", "NVGoggles_INDEP"];

// List of default items to always have.
_defaultItems = ["ItemMap", "ItemWatch", "ItemCompass", "ItemRadio"];

// Clear weapons from the unit.
removeAllWeapons _unit;

// By default we'll remove all items as well to clear previous reward items and give back _defaultItems.
if (_clearAll) then { 
   removeAllItems _unit;
   removeAllAssignedItems _unit;
   {_unit addItem _x; _unit assignItem _x;} forEach _defaultItems;
};

// If we're less than or equal to the weapon chance
if (_weaponRoll <= _weaponChance) then {
   // Pick a random weapon/mag pair from _weaponList.
   _weapon = _weaponList select floor(random(count _weaponList));

   //player groupChat format["Adding %1 and 2x %2", (_weapon select 0), (_weapon select 1)];

   // Add two of the selected mags
   for "_i" from 0 to 1 do {
       _unit addMagazine (_weapon select 1);
   };  

   // Add the selected weapon
   _unit addWeapon (_weapon select 0);

   // Ready weapon if it's got a GL muzzle.
   _pw = primaryWeapon _unit;
   if (_pw != "") then {
       _unit selectWeapon _pw;
       // Fix for weapons with grenade launcher
       _muzzles = getArray(configFile>>"cfgWeapons" >> _pw >> "muzzles");
       _unit selectWeapon (_muzzles select 0);
   } else {
       // If all they have is a pistol ready that.
       _unit selectWeapon (_weapon select 0);
   };
};

// If we're less than or equal to the item chance
if (_itemRoll <= _itemChance) then {
   // Pick a random item from the _itemList.
   _item = _itemList select floor(random(count _itemList));

    //player groupChat format["Adding %1", _item];

   // Give the item
   _unit addItem _item;
};  

Edited by kylania

Share this post


Link to post
Share on other sites

This probably isn't even complete but it's based off of a [404] ArmA II Wasteland script.

private ["_unit","_Weapons","_items","_pWeapon","_pItem","_mag"];
_unit = player;
_Weapons =["RH_deagle","RH_Mak","RH_usp","RH_g19"];
_items = ["sc_cellphone","sc_transmitter","sc_cigarettepack","sc_money","sc_lighter"];

_pWeapon = _Weapons select (random (count _Weapons - 1));
_pItem = _items select (random (count _items - 1));
_mag = (getArray (configFile >> "Cfgweapons" >> _pWeapon >> "magazines")) select 0;



_unit addItem _pItem;
{_unit addMagazine _mag} forEach [1,2,3];
_unit addWeapon _pWeapon;

Share this post


Link to post
Share on other sites

@kylania

Looks promising, but could not get anything working. Like the percent chance option roll.

The code looks quite daunting and a bit out of my depth...

@bob100101

Had better luck with yours. Weird results though. It gave me (player) a random Pistol

from the list. Did not add any Magazines. And the Item was either the Money or nothing.

Odd. The code looks the same for each but why always the Money? hmmm. Getting

close. Need a range of several Items, and not just the 1. I like the idea of kylania's

where it's a user config percent of even running BEFORE it picks the 0-5 Items.

Would be cool if maybe even use different Lists for different Units (or classes) to

distinguish "casual" Items from "Intel" Items that perhaps ONLY Officers would have a chance

of carrying vs the mundane everyday stuff you'd expect from the ground-pounders.

Tried changing _units = player to _units = allUnits with no luck.

Need every other Unit (maybe just the OPFOR) to use the code. Not me the "player".

What would be all the options to use instead of "player"?

Share this post


Link to post
Share on other sites

Need every other Unit (maybe just the OPFOR) to use the code. Not me the "player".

What would be all the options to use instead of "player"?

So Just OPFOR needs the random weapons? And are these OPFOR "players" going to be real players, or just AI?

Share this post


Link to post
Share on other sites

Yes, just OPFOR. And they are all going to be AI.

I only play over my home LAN with a buddy.

We have our oun custom loadouts based on the

Mission we are doing. Since we play Black-Ops

style, we would love the option to search the

bodies for various Intel. Also the random Items

just helps immensely with the immersion part.

Right now I have to build each Unit specifically

then randomize where they spawn in at.

Share this post


Link to post
Share on other sites
/*

put this in the AI's init field:

nul =  this execVM "randomGuns_AI.sqf";

*/
private ["_Weapons","_items","_pWeapon","_pItem","_mag"]; 
_Weapons =["RH_deagle","RH_Mak","RH_usp","RH_g19"]; 
_items = ["sc_cellphone","sc_transmitter","sc_cigarettepack","sc_money","sc_lighter"]; 

_pWeapon = _Weapons select (random (count _Weapons)); 
_pItem = _items select (random (count _items)); 
_mag = (getArray (configFile >> "Cfgweapons" >> _pWeapon >> "magazines")) select 0; 

_this addItem _pItem; 
{_this addMagazine _mag} forEach [1,2,3]; 
_this addWeapon _pWeapon; 

Share this post


Link to post
Share on other sites
@kylania

Looks promising, but could not get anything working. Like the percent chance option roll.

The code looks quite daunting and a bit out of my depth...

I edited the script to use ArmA3 instead of ACE items and fixed the "adding items via addWeapon" thing. Worked fine in ArmA2! Should be working fine now though. If you have questions with the script just ask it's not that complex really. Just a 60% chance of a weapon and a 60% chance of an Item each time. But you can adjust that with different arguments.

Share this post


Link to post
Share on other sites

@bob100101

That's fantastic mate! Thank You. Very, very, very close. Small, simple, works. BUT...

Anyway we can adjust it so it will give more than just the 1 Item? Looking for a range.

OR a creative workaround. This will work really good as-is. I can leave most Units field

blank for the 'regular' guys walking around who are by-the-book, with nothing on them.

For High Command, Officers, etc I can setup multiple SQF files for the Lists. But to get

that extra bit of polish, I need some with just the 1 Item when searched; but others

may have 5 Items when searching those vile-dictator-loving scum from the one SQF.

From what I understand, this of course will not work for anyone spawning-in after the game

starts. Which is OK, for now. Just thinking ahead for when all the action moves over to Altis

and we're going to use a lot more spawn options. Anyway... Great so far, thanks again!

Share this post


Link to post
Share on other sites

Anyway we can adjust it so it will give more than just the 1 Item? Looking for a range.

OR a creative workaround. This will work really good as-is. I can leave most Units field

blank for the 'regular' guys walking around who are by-the-book, with nothing on them.

For High Command, Officers, etc I can setup multiple SQF files for the Lists. But to get

that extra bit of polish, I need some with just the 1 Item when searched; but others

may have 5 Items when searching those vile-dictator-loving scum from the one SQF.

From what I understand, this of course will not work for anyone spawning-in after the game

starts. Which is OK, for now. Just thinking ahead for when all the action moves over to Altis

and we're going to use a lot more spawn options. Anyway... Great so far, thanks again!

So, you mean like adding a random number of items from the _items array?

Share this post


Link to post
Share on other sites

Yes.

Example: Me an my Navy SEAL swim buddy are in-country behind enemy lines and while

trying to hack into the Iranian DRAC Network, a patrol stumbles across us and we have

to take them out. After a small fire-fight, we now have the option to search the 3 dead

bad guys that spotted us for other valuable intelligence the CIA may be interested in.

Of the 3 guys, one had 2 random Items, another had 4 Items, and the third had 2

Items as well. Each one could have several Items from my List array from 1 - 5 Items per Unit.

Share this post


Link to post
Share on other sites

Well, Currently I cannot think of an easy way to fix your problem, so I'll have a go at it in the morning. (It's really late here)

Share this post


Link to post
Share on other sites

Here's bob's code with your latest request:

private ["_Weapons","_items","_pWeapon","_pItem","_mag"]; 

//_weapons = ["arifle_Katiba_ARCO_F", "arifle_Katiba_C_F", "arifle_MK20_GL_F", "arifle_MX_SW_F", "arifle_TRG20_Holo_F"];
_Weapons =["RH_deagle","RH_Mak","RH_usp","RH_g19"]; 
_items = ["sc_cellphone","sc_transmitter","sc_cigarettepack","sc_money","sc_lighter"]; 

_pWeapon = _Weapons select floor (random (count _Weapons)); 
_mag = (getArray (configFile >> "Cfgweapons" >> _pWeapon >> "magazines")) select 0; 
{_this addMagazine _mag} forEach [1,2,3]; 
_this addWeapon _pWeapon;  

for "_i" from 1 to (ceil (random 5)) do {
_pItem = _items select floor (random (count _items)); 
//player sideChat format["Adding %1 with %2 left", _pitem, count _items];
_items = _items - [_pItem];
_this addItem _pItem; 
};

Also had to add floor to his random selects otherwise it could occasionally result in a null object being selected.

Share this post


Link to post
Share on other sites
Here's bob's code with your latest request:

private ["_Weapons","_items","_pWeapon","_pItem","_mag"]; 

//_weapons = ["arifle_Katiba_ARCO_F", "arifle_Katiba_C_F", "arifle_MK20_GL_F", "arifle_MX_SW_F", "arifle_TRG20_Holo_F"];
_Weapons =["RH_deagle","RH_Mak","RH_usp","RH_g19"]; 
_items = ["sc_cellphone","sc_transmitter","sc_cigarettepack","sc_money","sc_lighter"]; 

_pWeapon = _Weapons select floor (random (count _Weapons)); 
_mag = (getArray (configFile >> "Cfgweapons" >> _pWeapon >> "magazines")) select 0; 
{_this addMagazine _mag} forEach [1,2,3]; 
_this addWeapon _pWeapon;  

for "_i" from 1 to (ceil (random 5)) do {
_pItem = _items select floor (random (count _items)); 
//player sideChat format["Adding %1 with %2 left", _pitem, count _items];
_items = _items - [_pItem];
_this addItem _pItem; 
};

Ooh I see Someones beaten me to the punch :D

Also had to add floor to his random selects otherwise it could occasionally result in a null object being selected.

Good Idea using floor, It hadn't crossed my mind that a null object could be selected, but that's probably just because I'm tired :wink_o:

Edited by bob100101
Different smilie rofl

Share this post


Link to post
Share on other sites

Great! Have yet to test in MP, but it looks perfect in several runs I just did.

Bob came out swinging and thought he might have you kyl... Bob scored some

good points early in the round. Kyl had the finishing blow. Judges decision?

Share this post


Link to post
Share on other sites

I love stumbling upon threads like this that teach me things I never even knew to ask about. One question, what exactly does defining stuff as private do?

Share this post


Link to post
Share on other sites
One question, what exactly does defining stuff as private do?

Read about scope here.

As an example with the above code, if you added this line at the end:

hint format["%1", _pItem];

You'd get a hint popup of the last item you added.

However, if you didn't declare: private ["_pItem"]; at the top of the script first, that same hint command would result in an undefined variable: _pItem error.

This is because the local variable _pItem is first initialized (given a value) within the for loop (a control structure so therefore a different scope), which is a different scope than the main script itself. By using the private command it allows us to initialize it within the for loop yet also use it in the main script, as in the hint command.

Share this post


Link to post
Share on other sites

So it will pull its definition from within the for loop throughout the rest of the script?

Share this post


Link to post
Share on other sites

In that example, yeah. See the links in my post for scope and private (read the notes at the bottom) for more detailed examples.

Share this post


Link to post
Share on other sites

Thanks a ton! You're help, whether directly to me or something I've read that you've told someone else, is greatly appreciated.

Share this post


Link to post
Share on other sites

Old thread, but I wonder if anyone has done something like this in reverse. As in randomly remove items from a players current inventory...

Share this post


Link to post
Share on other sites

Great question! I would like to know myself...

I have a EOD System on the drawing board. One Idea was to have EMP Mines that randomly

disable a piece of electronic equipment. Right now I just have random Triggers that delete the

equipment that's pre-selected scattered about the Map. Kinda cool.

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  

×