Forum Jump :

Author Message


Posts: 28
Rank:


Level: Member

Country: uk
Location:
Occupation:
Age:
In-game name:

 
#1 Posted at 2016-04-17 12:58        
     
I have just finished a 6kb script, with the RHS mods, that basically allows you to put it in the init of any group/unit of anyside, and then you can select 4 options for random customizing of the group/unit. 1 is just normal protesters, 2 is protesters who have just been given a gun, 3 is like basic civilian levies, just locals raised by the rebels, and 4 is a full blown rebel. Random weapons, some have RPG, some have snipers, some have MGs. some may have helmets, or just headgear, and/or bandanas. Some may just have bandanas, and others may not have anything. They have random everything basically.

Anyway, my question is not about the scripting itself, but rather how to clean it up to make it more understandable and readable, and to just generally make it look neater. Anyway, this is my script:

_unit = _this select 0;
_type = _this select 1;
AKchoice = true;
_facewearPoss = random 1;
_headgearPoss = random 1;
_balaclavaPoss = random 1;
_machineGunPoss = random 1;
_sniperRiflePoss = random 1;
_launcherPoss = random 1;
_torchPoss = random 1;

_uniform = [
	"U_BG_Guerilla1_1",
	"U_BG_Guerrilla_6_1",
	"U_BG_leader",
	"rhs_chdkz_uniform_2",
	"rhs_chdkz_uniform_3",
	"rhs_chdkz_uniform_4"
] call BIS_fnc_selectRandom;

_grenades = [
	"HandGrenade",
	"HandGrenade",
	"HandGrenade",
	"Chemlight_green",
	"Chemlight_green",
	"MiniGrenade",
	"SmokeShell",
	"SmokeShell",
	"SmokeShell",
	"SmokeShellYellow",
	"Chemlight_red"
];

_magazines = [
	"rhs_30Rnd_545x39_AK",
	"rhs_30Rnd_545x39_AK_no_tracers",
	"rhs_30Rnd_545x39_7N10_AK",
	"rhs_30Rnd_545x39_7N22_AK",
	"rhs_45Rnd_545x39_AK",
	"rhs_45Rnd_545x39_7N10_AK",
	"rhs_45Rnd_545x39_7N22_AK"
];
 
_weapon = [
	"rhs_weap_ak74m",
	"rhs_weap_ak74m_camo",
	"rhs_weap_ak74m_desert",
	"rhs_weap_ak74m_folded",
	"rhs_weap_ak74m_camo_folded",
	"rhs_weap_ak74m_desert_folded",
	"rhs_weap_ak74m_2mag",
	"rhs_weap_ak74m_2mag_camo",
	"rhs_weap_ak105"
] call BIS_fnc_selectRandom;

_weaponMG = [
	"rhs_weap_pkm",
	"rhs_weap_pkp"
] call BIS_fnc_selectRandom;

_weaponDMR = [
	"rhs_weap_svdp",
	"rhs_weap_svdp_wd",
	"rhs_weap_svds"
] call BIS_fnc_selectRandom;

_backpack = [
	"B_AssaultPack_blk",
	"B_AssaultPack_rgr",
	"B_AssaultPack_dgtl",
	"B_FieldPack_khk",
	"B_FieldPack_cbr",
	"B_Kitbag_cbr",
	"B_TacticalPack_blk",
	"B_TacticalPack_oli",
	"rhs_sidor",
	"rhs_assault_umbts"
] call BIS_fnc_selectRandom;
 
_vest = [
	"V_BandollierB_cbr",
	"V_BandollierB_blk",
	"V_BandollierB_oli",
	"V_HarnessOGL_brn",
	"rhs_6sh46",
	"rhs_assault_umbts"
] call BIS_fnc_selectRandom;
 
_bandanna = [
	"G_Bandanna_blk",
	"G_Bandanna_oli",
	"G_Bandanna_beast",
	"G_Bandanna_tan",
	"G_Bandanna_khk",
	"rhs_scarf"
] call BIS_fnc_selectRandom;

_balaclava = [
	"G_Balaclava_oli",
	"G_Balaclava_blk",
	"H_Shemag_olive",
	"H_ShemagOpen_tan",
	"rhs_balaclava1_olive"
] call BIS_fnc_selectRandom;

_headgear = [
	"H_Bandanna_khk",
	"H_Bandanna_mcamo",
	"H_Booniehat_mcamo",
	"H_Booniehat_tan",
	"H_Booniehat_dgtl",
	"H_Cap_blk",
	"H_Cap_oli",
	"H_Hat_brown",
	"rhs_beanie_green",
	"rhs_Booniehat_flora",
	"rhs_Booniehat_marpatd"
] call BIS_fnc_selectRandom;

_helmet = [
	"rhs_6b26_green",
	"rhs_6b27m_green",
	"rhs_6b27m_digi",
	"rhs_6b28_green",
	"rhs_ssh68"
] call BIS_fnc_selectRandom;

 _magazine = _magazines call BIS_fnc_selectRandom;
 
_unit unassignItem "NVGoggles";
_unit removeItem "NVGoggles";
_unit unassignItem "NVGoggles_OPFOR";
_unit removeItem "NVGoggles_OPFOR";
_unit unassignItem "NVGoggles_INDEP";
_unit removeItem "NVGoggles_INDEP";
 
switch (_type) do {
	case 1 : {
		removeAllWeapons _unit;
		removeHeadgear _unit;
		removeGoggles _unit;
		
		if (_facewearPoss > 0.2) then {
			if (_balaclavaPoss < 0.2) then {_unit addGoggles _balaclava} else{_unit addGoggles _bandanna; _unit addHeadgear _headgear};
			};
		};
	case 2 : {
		removeAllWeapons _unit;
		_unit addMagazine _magazine;
		_unit addWeapon _weapon;
		for "_i" from 1 to 2 do {
		_unit addItem (_magazines call BIS_fnc_selectRandom);
		};
	};
	case 3 : {
		removeAllWeapons _unit;
		removeHeadgear _unit;
		removeGoggles _unit;
		_unit addMagazine _magazine;
		_unit addWeapon _weapon;
		_unit addVest _vest;
		for "_i" from 1 to 3 do {
			_unit addItem (_magazines call BIS_fnc_selectRandom);
		};
		
		if (_facewearPoss > 0.2) then {
			if (_balaclavaPoss < 0.2) then {_unit addGoggles _balaclava} else{_unit addGoggles _bandanna; _unit addHeadgear _headgear};
		};
	};
	case 4 : {
		removeAllWeapons _unit;
		removeHeadgear _unit;
		removeGoggles _unit;
		_unit addVest _vest;
		_unit forceAddUniform _uniform;

		if (_sniperRiflePoss < 0.1) then {
				AKchoice = false;
				_unit addMagazine "rhs_10Rnd_762x54mmR_7N1";
				_unit addWeapon _weaponDMR;
				_unit addBackpack _backpack;
				_unit addMagazine (_grenades call BIS_fnc_selectRandom);
				for "_i" from 1 to 3 do {
					_unit addItem "rhs_10Rnd_762x54mmR_7N1";
				};
			}
			else {if (_machineGunPoss < 0.1) then {
						AKchoice = false;
						_unit addMagazine "rhs_100Rnd_762x54mmR";
						_unit addWeapon _weaponMG;
						_unit addBackpack _backpack;
						_unit addMagazine (_grenades call BIS_fnc_selectRandom);
						for "_i" from 1 to 2 do {
							_unit addItem "rhs_100Rnd_762x54mmR";
						};
					} else {if (_launcherPoss < 0.1) then {
					_unit addMagazine "rhs_rpg7_PG7VL_mag";
					_unit addWeapon "rhs_weap_rpg7";
					_unit addBackpack "rhs_rpg_empty";
					_unit addItemToBackpack "rhs_rpg7_PG7VL_mag";
					};
				};
			};
		
		if (_facewearPoss > 0.35) then {
			if (_balaclavaPoss < 0.2) then {_unit addGoggles _balaclava} else{_unit addGoggles _bandanna; _unit addHeadgear _headgear};
		} else {_unit addHeadgear _helmet};
		
		if (AKchoice) then{
			_unit addMagazine _magazine;
			_unit addWeapon _weapon;
			for "_i" from 1 to 3 do {
				_unit addItem (_magazines call BIS_fnc_selectRandom);
			};
		};
		
			if (_torchPoss < 0.2) then{
			_unit addPrimaryWeaponItem "rhs_acc_2dpZenit"; 
			_unit assignItem "rhs_acc_2dpZenit";
			_unit setskill ["spotDistance",(0 + random 0.2)];
			_unit setskill ["spotTime",(0 + random 0.2)];
		};
	
		for "_i" from 1 to 2 do {
			_unit addMagazine (_grenades call BIS_fnc_selectRandom);
		};
	
	};
};


Author Message


Posts: 1189
Rank:


Level: Member

Country: tr
Location:
Occupation: Computer Science student
Age: 20
In-game name: Wak

 
#2 Posted at 2016-04-17 13:33        
     
// DON'T indent like this:
if (cond) then {

    }; //WRONG

//Because it turns into this:

case 2: {
    if (cond) then {
            _unit _unit addWeapon _weaponDMR;
        }
    }
case 3: {
    [...]

Here is a DIFF check of your script vs mine. I whouldn't say I made major progress for readability, but performance should improve a little bit and it definitely looks more compact now.


Sometimes I like to think as I started the whole "earplugs" thing.

W0lle: The only advice I can give you is: Do not try to understand BI. You will not succeed and it only makes your brain go boom. I would even go so far and say that not even they understand their own actions :-D.

#define getDamage getDammage

Advertisement


Author Message


Posts: 28
Rank:


Level: Member

Country: uk
Location:
Occupation:
Age:
In-game name:

 
#3 Posted at 2016-04-17 13:44        
     
Thanks 654wak654. The code works just fine and it does look cleaner and more organised.






Tags: Clean Up, Script, Tidy