Jump to content
Sign in to follow this  
merlin

Random Quantities

Recommended Posts

Apologies in advance if this is a very rudimentary question.

I'm trying to randomize the contents of some weapons caches.

What i want is a random quantity of one, or in some cases several different weapons. I'm quite amateurish at scripting and this was my first attempt.

while {alive _this} do
{
clearweaponcargo _this;
clearmagazinecargo _this;

   _rNumber = random 10


_this addWeaponCargo ["ACE_M72",_rNumber;];

sleep 8800;
};

my second attempt looked like this

_this addWeaponCargo ["ACE_M72", %1, _rNumber;];

My question is

1. What am I doing wrong?

2. (Assuming this would be done though the addition of variables) Is there an efficient way to set individual random numbers for a decent amount of weapon/ammo types?

Thanks very much!

Share this post


Link to post
Share on other sites

Your first attempt might have worked with this instead of _this if it was placed in the ammobox's init.

But try it this way...

Create a little script and call the script from the ammobox's init....instead of trying to cram a bunch of text in the init.

Create a script in your mission folder called fillammobox.sqf or whatever you want to call it.

fillammobox.sqf

_ammobox = _this select 0;

while {alive _ammobox} do {

       //clear the ammo box
clearweaponcargo _ammobox;
clearmagazinecargo _ammobox;

       //create a random number
_rNumber = ceil (random 10);

       //add _rNumber * 10 magazines....so 10 for each gun
_ammobox addMagazineCargo ["20Rnd_556x45_Stanag",_rNumber * 10];

       //add _rNumber weapons to the box 
_ammobox addWeaponCargo ["m16a4",_rNumber];

sleep 8800;
}

Put this in the ammoboxes init:-

nul = [this] execVM "fillammobox.sqf";

That will run the little script instead of struggling with the space in the init.

Edited by twirly
Changes

Share this post


Link to post
Share on other sites

I appreciate hte prompt response Twirly,

The script works great and the magazine multiplication idea is brilliant.

_number = ceil (random 10);

the _number variable in this should be _rnumber though.

thanks again!

Share this post


Link to post
Share on other sites

Glad it helped. Yeah I fixed the _rNumber thing in the post above.

Share this post


Link to post
Share on other sites

I've been trying to flesh out randomized loadouts once more and have run into some more problems while trying to solve a seemingly simple scripting problem.

I have several varying loadouts for a single ammo crate, what i want is for a random ammo script to be selected and initialized.

my first attempt used a single script and looked something like this

(i used this as a reference http://forums.bistudio.com/showthread.php?t=106956 )

_ammobox = _this select 0;
while {alive _ammobox} do {
clearweaponcargo _ammobox;
clearmagazinecargo _ammobox;


switch (floor (random 2)) do {

case 0: 
{
//weapons
_ammobox addWeaponCargo ["Binocular",1];
//mags
_ammobox addMagazineCargo ["HandGrenade_Stone",98];
};

case 1: 
{
//weapons
_ammobox addWeaponCargo ["ACE_Flaregun",1];
//mags
_ammobox addMagazineCargo ["ACE_SSGreen_FG",10];
};

case 2: 
{
//weapons
_ammobox addWeaponCargo ["revolver_EP1",1];
//mags
_ammobox addMagazineCargo ["6Rnd_45ACP",16];
};
};

I then tried throwing together some kind of primitive if>then script to no avail, looking something like

_ammobox = _this select 0;
while {alive _ammobox} do {
clearweaponcargo _ammobox;
clearmagazinecargo _ammobox;

_rnumber = floor (random 2);

if _rnumber = 0 do {
_ammobox addWeaponCargo ["ACE_Flaregun",1];
_ammobox addMagazineCargo ["ACE_SSGreen_FG",10];
};

if _rnumber = 1 do { ...etc

i used this thread as reference for my final attempt, http://forums.bistudio.com/showpost.php?p=1872767&postcount=3

with indivudual scripts for different ammocrate loadouts and a scriptsel.sqf to select which one to initialize

scriptsel.sqf:

_ammos = ["ammo1.sqf","ammo2.sqf","ammo3.sqf"];
_ammo = _ammos select (round (random ((count _ammos) - 1)));

// ammocrate is named crate1 
[crate1] execVM _ammo;

right now i'm fairly certain that any one of these three techniques could work but i've butchered the formatting on every one. any guidance would be appreciated.

Share this post


Link to post
Share on other sites

For the first one try this. I added an extra bracket and a sleep for 60 seconds.... also changed the random 2 to random 3....so floor can actually give you a 2!

_ammobox = _this select 0;

while {alive _ammobox} do {

clearweaponcargo _ammobox;
clearmagazinecargo _ammobox;

switch (floor (random [b][color="Red"]3[/color][/b])) do {

	case 0: {

	//weapons
	_ammobox addWeaponCargo ["Binocular",1];

	//mags
	_ammobox addMagazineCargo ["HandGrenade_Stone",98];
	};

	case 1: {

		//weapons
		_ammobox addWeaponCargo ["ACE_Flaregun",1];

		//mags
		_ammobox addMagazineCargo ["ACE_SSGreen_FG",10];
	};

	case 2: {

		//weapons
		_ammobox addWeaponCargo ["revolver_EP1",1];

		//mags
		_ammobox addMagazineCargo ["6Rnd_45ACP",16];

	};
};

[b][color="Red"]sleep 60;[/color][/b]
[b][color="Red"]};[/color][/b]

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Thanks once more Twirly, i really do appreciate the quick response.

one problem i have observed stems from the sleep command, it seems to cycle the loadout every 60 seconds, which really conflicts with my intentions for the script which are to give the players limited quantities of unexpected equipment and perhaps solicit some degree of cleverness/improvisation from them.

my first instinct would be to change 60 to some large number, however i must ask is there any command to only initialize the script once?

I tried removing the sleep command as well as adding an "exit" line ot the end, however they both resulted in the script running every tick(?).

Share this post


Link to post
Share on other sites

I thought you wanted to cycle it because the while was there.... lol!

Just take out the line with the while.... take out the line with the sleep.... and remove the last bracket in the script and it should be good to go.

This approach looks cool too....

_ammos = ["ammo1.sqf","ammo2.sqf","ammo3.sqf"];
_ammo = _ammos select [b][color="Red"](floor (random (count _ammos)));[/color][/b]

// ammocrate is named crate1 
[color="Red"][b]nul = [/b][/color][crate1] execVM _ammo;

Edited by twirly
Added stuff

Share this post


Link to post
Share on other sites

Sorry to bump this thread once more but i've encountered something of a multiplayer problem.

When running a local server only the host can see the randomized loadout and take weapons from the crate. Dedicated servers on the other hand seem to produce different loadouts for each player and will not let anyone take weapons from the crate. I presume this is because the random number generation that i had in the ammo1.sqf was run by the individual clients. I assumed i could fix this by making the random number a public variable. this resulted in nothing happening in mission editor tests, I have yet to test it on a dedicated server but i feel like i'll get the same result (unless publicvariables do not work in SP missions).

init.sqf

num1 = {floor (random  10)}
publicVariable "num1";

init line of weapon crate

nul = [this] execVM "ammo1.sqf";

ammo1.sqf

_ammobox = _this select 0;

clearweaponcargo _ammobox;
clearmagazinecargo _ammobox;

switch (num1)) do {
	case 0: {

	_ammobox addWeaponCargo ["Binocular",2];
	_ammobox addWeaponCargo ["ACE_RPG22",1];
	_ammobox addWeaponCargo ["Sa61_EP1",1];



	_ammobox addMagazineCargo ["10Rnd_B_765x17_Ball",8];
	_ammobox addMagazineCargo ["PMC_ied_v4",1];
	_ammobox addMagazineCargo ["ACE_MON50_M",2];
	_ammobox addMagazineCargo ["HandGrenade_Stone",30];

	};
	case 1: {

		_ammobox addWeaponCargo ["ACE_Javelin_CLU",1];
		_ammobox addWeaponCargo ["ACE_Javelin_Direct",2];
		_ammobox addWeaponCargo ["UZI_EP1",3];
		_ammobox addWeaponCargo ["glock17_EP1",1];


		_ammobox addMagazineCargo ["ACE_30Rnd_9x19_S_UZI",20];
		_ammobox addMagazineCargo ["ACE_33Rnd_9x19_G18",10];

	};
	case 2: {

	_ammobox addWeaponCargo ["AA12_PMC",4];

		_ammobox addMagazineCargo ["20Rnd_B_AA12_74Slug",30];
		_ammobox addMagazineCargo ["20Rnd_B_AA12_HE",30];
		_ammobox addMagazineCargo ["20Rnd_B_AA12_Pellets",30];
	};

[color="#FF0000"][... case 3-8 exempted for brevity][/color]

	case 9: {

		_ammobox addWeaponCargo ["ACE_TOWLauncherSingle_Tri",4];
		_ammobox addWeaponCargo ["TOWLauncherSingle",4];

		_ammobox addMagazineCargo ["ACE_TOW_CSWDM",8];

	};
};

Also is this the best approach for a "slot machine" style ammo crate filler? I've also considered making seperate crates for each case and setting the condition of presence for the randomized publicvariable, although i think i might still have the same problem if i'm doing that incorrectly in the above code.

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  

×