Jump to content

Recommended Posts

I'm currently working on a loot script ( done before i know, but i enjoy learning )...

And all seems to work correctly.

Player distance from "House" is checked, each building in range is selected and depending on the variable assigned will start to spawn loot in building positions and debug markers created....

 

What im struggling with, is the ability to run a check for loot deletion..

I want to check that (1) the player is at least (say 100m) away from building position, and time since the loot was spawned has passed a certain amount, but only de-spawn loot when both are true....

Hope that makes sense....

 

Here is what i have thus far:

 

if !(isServer) exitWith {};

RAZ_fnc_lootArray = [

	[
		// Uniforms
		"U_C_IDAP_Man_cargo_F"
	],

	[
		// Backpacks
		"B_AssaultPack_blk"
	],

	[
		// Vests
		"V_PlateCarrierGL_blk"
	],

	[
		// Headgear
		"H_HelmetSpecO_blk"
	],

	[
		// Items
		"FirstAidKit"
	],

	[
		// Survival
		"ItemCompass"
	]

];

RAZ_fnc_spawnLoot = {

	_spawnDistance = 80; // Set the distance to building for loot spawn...

	_spawnChance = 5; // Set the % chance of loot spawning ( 0.5 - 5 is recommended)...

	_enableDebug = true; // Enable debug loot markers...

	_buildings = player nearObjects [ "House", _spawnDistance ]; // Get buildings close to player...

	{

		if ( ( _x getVariable ["hasLoot", 0] ) == 0 ) then {

			_buildingPositions = [ _x ] call BIS_fnc_buildingPositions; // Get positions in buildings if variable is false...

			{

				if ( player distance _x <= _spawnDistance && _spawnChance > random 100 ) then {

					_lootSelection = floor ( random 7 );

					if ( _lootSelection == 0 ) then {

						_rifleConfigs = "getNumber( _x >> 'scope' ) isEqualTo 2 && { getNumber( _x >> 'type' ) isEqualTo 1 && getText( _x >> 'cursor' ) == 'arifle' }"configClasses( configFile >> "CfgWeapons" ) apply { configName _x };
						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addWeaponCargoGlobal [ (selectRandom ( _rifleConfigs ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn weapon loot

					if ( _lootSelection == 1 ) then {

						_rifleConfigs = "getNumber( _x >> 'scope' ) isEqualTo 2 && { getNumber( _x >> 'type' ) isEqualTo 1 && getText( _x >> 'cursor' ) == 'arifle' }"configClasses( configFile >> "CfgWeapons" ) apply { configName _x };
						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_item = ( selectRandom ( _rifleConfigs ) );
						_mags = getArray ( configFile >> "CfgWeapons" >> _item >> "magazines" );
						_mag = ( selectRandom ( _mags ) );
						_itemHolder addMagazineCargoGlobal [_mag, (random 4)];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn ammo loot

					if ( _lootSelection == 2 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 0 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn uniform loot

					if ( _lootSelection == 3 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addBackpackCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 1 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn backpack loot

					if ( _lootSelection == 4 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 2 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn vest loot

					if ( _lootSelection == 5 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 3 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn headgear loot

					if ( _lootSelection == 6 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 4 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn item loot

					if ( _lootSelection == 7 ) then {

						_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
						_itemHolder setPos _x;
						_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 5 ) ), 1];
						if ( _enableDebug ) then {
							_markerID = format [ "%1", _x ];
							_markerText = format [ "%1", _lootSelection ];
							_debug = createMarker [_markerID, _x];
							_debug setMarkerType "mil_dot";
							_debug setMarkerColor "ColorRed";
							_debug setMarkerText _markerText;
						}

					}; // Spawn survival loot

				}; // End spawn distance and chance check...

			} forEach _buildingPositions; // End for each building position...

			_x setVariable ["hasLoot", 1]; // Set the building variable to true...

		}; // Enf building variavble check...

	} forEach _buildings; // End for each building...

}; // End RAZ_fnc_spawnLoot...

//====================================//
// LETS CALL THE FUNCTION FOR TESTING //
//====================================//

_spawnLoot = [] spawn {

	_spawnLoot = true;

	while { _spawnLoot } do {

		_lootSpawner = [] call RAZ_fnc_spawnLoot;

		sleep 1;

	};

};

 

Share this post


Link to post
Share on other sites

Just wondering... Why do you need to delete the loot when player is far away? FPS? Not sure if this matters as you aren't in the area anyway but I'm not sure so here:

_spawnedLoot = [];
// Add each loot to the _spawnedLoot array in your forEach loop.
// Example:
{
	_spawnedLoot pushBack _weaponPlaceholder;
} forEach blahblahblah; // This is where you spawn the loot... Weapon placeholder would be what you add the array.
{
	deleteVehicle _x;
} forEach _spawnedLoot;
// setVariable on every weapon placeholder.
{
	_weaponPlaceholder setVariable ["loot", true, true]; // You shouldn't need the last parameter "true" as you would create and delete the loot on the server I assume...
} forEach blahblahblah;
// Search through weapon placeholders with getVariable check...
// I don't recommend doing it this way and I'm not sure why I told you about it either lol! :D

 

Share this post


Link to post
Share on other sites

This is what i have so far & all is working fine!

But, is there any advice as to optimization etc for my code?

I'm learning to code in Arma3 and am loving the experience and learning curve...

 

I also need to start looking into *multiplayer - izing* it if anyone has any pointers? 

 

RAZ_fnc_lootArray = [

	[
		// Weapons
		"arifle_MXC_F"
	],

	[
		// Uniforms
		"U_C_IDAP_Man_cargo_F"
	],

	[
		// Backpacks
		"B_AssaultPack_blk"
	],

	[
		// Vests
		"V_PlateCarrierGL_blk"
	],

	[
		// Headgear
		"H_HelmetSpecO_blk"
	],

	[
		// Items
		"FirstAidKit"
	],

	[
		// Survival
		"ItemCompass"
	],

	[
		// Weapon attachments
	]

];

_RAZ_fnc_spawnLoot = [] spawn {

	_runScript = true;

	_spawnDistance = 30; // Set the distance to building for loot spawn...

	_spawnChance = 100; // Set the % chance of loot spawning ( 0.5 - 5 is recommended)...

	_deletionTime = 1 * 60; // mins before loot deleted ready for respawn (will only delete when player is ?? far away and timer is over ??) (30 mins recommended)...

	_deletionDistance = 500; // Remove building variable when player >= 500m away...

	_buildingCoolOff = 1 * 60; // Time between fresh loot spawning / replacing into a building (1 hour recommended)

	_enableDebug = true; // Enable debug loot markers...

	_spawnedLoot = []; // Array- netId of spawned item holders

	_buildingsArray = []; // Array of all buildings checked...

	while { _runScript } do {

		_buildings = player nearObjects [ "House", _spawnDistance ]; // Get buildings close to player...

		{

			if (_x getVariable ["buildingEmpty", true]) then {

				_coolOfPeriod = time + _buildingCoolOff;
				_buildingsArray pushBack [_x call BIS_fnc_netId, _coolOfPeriod];

				_buildingPositions = [ _x ] call BIS_fnc_buildingPositions; // Get positions in buildings if variable is false...

				{

					if ( player distance _x <= _spawnDistance && _spawnChance >= random 100 ) then {

						_lootSelection = Ceil random 8;

						if ( _lootSelection == 1 ) then {
							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_item = ( selectRandom ( RAZ_fnc_lootArray select 0 ) );
							_itemHolder addWeaponCargoGlobal [ _item, 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Weapon";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn weapon loot

						if ( _lootSelection == 2 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_item = ( selectRandom ( RAZ_fnc_lootArray select 0 ) );
							_mags = getArray ( configFile >> "CfgWeapons" >> _item >> "magazines" );
							_mag = ( selectRandom ( _mags ) );
							_itemHolder addMagazineCargoGlobal [_mag, (random 4)];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Ammo";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn ammo loot

						if ( _lootSelection == 3 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 1 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Uniform";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn uniform loot

						if ( _lootSelection == 4 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addBackpackCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 2 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Backpack";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn backpack loot

						if ( _lootSelection == 5 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 3 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Vest";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn vest loot

						if ( _lootSelection == 6 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 4 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Headgear";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn headgear loot

						if ( _lootSelection == 7 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 5 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Inventory Item";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn item loot

						if ( _lootSelection == 8 ) then {

							_itemHolder = "WeaponHolderSimulated" createVehicle [0,0,0];
							_itemHolder setPos _x;
							_itemHolder addItemCargoGlobal [ (selectRandom ( RAZ_fnc_lootArray select 6 ) ), 1];
							_timer = time + _deletionTime;
							if ( _enableDebug ) then {
								_markerID = format [ "%1", _x ];
								_debug = createMarker [_markerID, _x];
								_debug setMarkerType "mil_dot";
								_debug setMarkerColor "ColorRed";
								_debug setMarkerText "Survival Item";
							};
							if !(isNull _itemHolder) then { _spawnedLoot pushBack [_itemHolder call BIS_fnc_netId, _timer, _x] };

						}; // Spawn survival loot...

					}; // End spawn distance and chance check...

				} forEach _buildingPositions; // End for each building position...

				_x setVariable ["buildingEmpty", false]; // Set the building variable to false...

			}; // End building variable check...

		} forEach _buildings; // End for each building...

		{
			_obj = _x select 0 call BIS_fnc_objectFromNetId;

			_getMarkerName = _x select 2;

			_markerName = format [ "%1", _getMarkerName ];

			if ( player distance _obj > _deletionDistance && time > _x select 1 ) then { deleteVehicle _obj; deleteMarker _markerName; _spawnedLoot deleteAt (_spawnedLoot find _x) };

		} forEach _spawnedLoot; // Foreach loot item in array...

		{

			_building = _x select 0 call BIS_fnc_objectFromNetId;

			if ( player distance _building > _deletionDistance && time > _x select 1 ) then { _building setVariable [ "buildingEmpty", true ] };

		} forEach _buildingsArray; // Foreach building in array...

		hint str _spawnedLoot;

		sleep 2; // Time between spawn checks...

	}; // End while loop...

}; // End RAZ_fnc_spawnLoot...

 

Share this post


Link to post
Share on other sites

Coming back to this script after working on something else.

I would like to set this up for multiplayer...
This code will be run on the dedi server and i was just wondering if Player is referenced on dedi? I have a feeling it is not.
Would it be advisable to wrap this up with:

 

_players = allPlayers - switchableUnits;

{
	// Loot code
}forEach _players;

Kind regards
Rory

Share this post


Link to post
Share on other sites

switchableUnits returns [] in MP. Keep that for SP switch units compatibility.

playableUnits returns:

- all the playable units IF enabled in lobby

- only players (like allPlayers) if units are disabled in lobby

 

allPlayers - switchableUnits has no sense in any context.

 

And yes, there is no player on dedicated, by definition.

 

For loots, you just have to run your code in/from initServer.sqf then do your stuff for allPlayers.

Share this post


Link to post
Share on other sites
17 hours ago, pierremgi said:

switchableUnits returns [] in MP. Keep that for SP switch units compatibility.

playableUnits returns:

- all the playable units IF enabled in lobby

- only players (like allPlayers) if units are disabled in lobby

 

allPlayers - switchableUnits has no sense in any context.

 

And yes, there is no player on dedicated, by definition.

 

For loots, you just have to run your code in/from initServer.sqf then do your stuff for allPlayers.


I figured as much.

I currently have initPlayerLocal running the building checks locally and once returned true then fire remoteExec to server for all clients.
Im still learning sqf but getting better as the days roll by.
Thanks for you help :)

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

×