Jump to content
HAFGaming1

I can spawn items in buildings, though i want to only spawn one or two.

Recommended Posts

Hi everyone!

I was able to get a script working to spawn loot. It spawns like 5-6 items per building (so far weapons and mags) and it lags the crap out of my game. (this is within a 100m distance) is there anyway to make it spawn one or two per building so i dont lag as much?

also to keep my mission from being to easy to find loot.

here is the script::

 

_unit = _this select 0;
_addActionID = _this select 2;

_unit removeAction _addActionID;

_markerPos = getMarkerPos "lootMarker";

_weaponArray = [];

//Automation process
_cfgWeapons = "true" configClasses (configFile >> "cfgWeapons");

{
    _weaponString = configName (_x);
    _parents = [_x,true] call BIS_fnc_returnParents;

    if ("Rifle" in _parents) then{
        _weaponArray append [_weaponString];
    };

    if ("Pistol" in _parents) then{
        _weaponArray append [_weaponString];
    };
} forEach _cfgWeapons;
//Automation process

_itemBoxArray = [];

_houseArray = _markerPos nearObjects ["house",100];

{
    _buildingPositions = [_x] call BIS_fnc_buildingPositions;
    {
        _weapon = _weaponArray select (floor (random (count _weaponArray)));
        _itemBox = "WeaponHolderSimulated" createVehicle [0,0,0];
        _itemBox setPos _x;
        _itemBox addWeaponCargoGlobal [_weapon,1];
        _magazines = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines");
        _mag = _magazines select (floor (random (count _magazines)));
        _itemBox addMagazineCargoGlobal [_mag,5];
        _itemBoxArray = _itemBoxArray + [_itemBox];
    } forEach _buildingPositions;
} forEach _houseArray;

sleep 120;
{
    deleteVehicle _x;
} forEach _itemBoxArray;

Share this post


Link to post
Share on other sites

All credit for script >> https://www.youtube.com/channel/UCLjNP-gJSrn2uTvUAYlFroA Lax on youtube.

Lax replied to another person to put an if then line::
if (10 > random 100) then { code };

He says to put the spawning command in the place of "code" which im gonna assume is addWeaponCargoGlobal, though is still spawns in every buildingpos it finds. though i want it to spawn maybe one per building which is why im trying to do a 10% chance of spawning.

I also don't know where in the script to put this line.

SORRY FOR THE STUPIDITY I HAVE NO CLUE WHAT IM DOING LOL i have messed with scripts before but that was in the alpha of alpha of arma 3 that was a while ago.

Share this post


Link to post
Share on other sites

As far as I understand the script it spawns 1 weapon and 5 magazines on each position of each house inside a radius of 100 meters.

I see some points to reduce the performance impact.

1. using "GroundWeaponHolder" instead of "WeaponHolderSimulated" because the object "WeaponHolderSimulated" has physx enabled.

2. introducing a chance for spawning an itembox inside of a house.

3. introducing a chance for spawning an itembox on a house position.

 

this is all done with this script:

_unit = _this select 0;
_addActionID = _this select 2; 
_unit removeAction _addActionID; 
_markerPos = getMarkerPos "lootMarker";

// EDIT THE FOLLOWING FIVE LINES TO ADJUST BEHAVIOR
_chance_house = 50;            // chance in percent to spawn an itembox inside a house
_chance_position = 50;           // chance in percent to spawn an itembox on house position
_house_radius = 100;           // only houses inside of this radius will be handled
_box_classname = "GroundWeaponHolder";   //"WeaponHolderSimulated" 
_max_magazines = 5;
// EDIT THE FIVE LINES ABOVE TO ADJUST BEHAVIOR

_weaponArray = []; 

//Automation process
 _cfgWeapons = "true" configClasses (configFile >> "cfgWeapons"); 
{
    _weaponString = configName (_x);
    _parents = [_x,true] call BIS_fnc_returnParents; 

    if ("Rifle" in _parents) then{
         _weaponArray append [_weaponString];
     }; 

    if ("Pistol" in _parents) then{
         _weaponArray append [_weaponString];
     };
 } forEach _cfgWeapons;
 //Automation process 

_itemBoxArray = []; 

_houseArray = (_markerPos nearObjects ["house", _house_radius]) select {random 100 < _chance_house};

{
     _buildingPositions = ([_x] call BIS_fnc_buildingPositions) select {random 100 < _chance_position};
     {
         _weapon = _weaponArray select (floor (random (count _weaponArray)));
         _itemBox = _box_classname createVehicle [0,0,0];
         _itemBox setPos _x;
         _itemBox addWeaponCargoGlobal [_weapon,1];
         _magazines = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines");
         _mag = _magazines select (floor (random (count _magazines)));
         _itemBox addMagazineCargoGlobal [_mag, (ceil random _max_magazines)];
         _itemBoxArray = _itemBoxArray + [_itemBox];
     } forEach _buildingPositions;
 } forEach _houseArray; 

sleep 120;
 {
     deleteVehicle _x;
 } forEach _itemBoxArray; 

Now u have 5 variables for adjusting the behavior for spawning loot:

_chance_house  -> is the chance that a house will be handled by the script to spawn loot in it

                           ->if this check fails then there will no itembox spawned in this house (but maybe on other houses)

_chance_position -> is the chance to spawn loot at a specific position of that house

                             ->if this check fails then there will no itembox spawned on the specific house position (but maybe on other positions)

_house_radius  -> is the radius where in houses are recognized for spawning loot

_box_classname -> change this back to "WeaponHolderSimulated" if u have an issue with the itemboxes (like flying boxes or such)

_max_magazines -> the maximum of magazines spawned in an itembox.

Share this post


Link to post
Share on other sites

doesn't building pos have the ability to select the different memory points defined?
I guess it doesn't necessarily have to be random in your case :shrug: 

Share this post


Link to post
Share on other sites
22 minutes ago, Midnighters said:

doesn't building pos have the ability to select the different memory points defined?
I guess it doesn't necessarily have to be random in your case :shrug: 

good point. I didnt read the wiki entry for BIS_fnc_buildingPositions

 

I changed the script and substituted the chance for a house position with a maximum number of house positions used:

_unit = _this select 0;
_addActionID = _this select 2; 
_unit removeAction _addActionID; 
_markerPos = getMarkerPos "lootMarker";

// EDIT THE FOLLOWING FIVE LINES TO ADJUST BEHAVIOR
_chance_house = 50;            // chance in percent to spawn an itembox inside a house
_max_positions = 4;           // maximum number of building positions used
_house_radius = 100;           // only houses inside of this radius will be handled
_box_classname = "GroundWeaponHolder";   //"WeaponHolderSimulated" 
_max_magazines = 5;          //maximum number of magazins spawned in item box
// EDIT THE FIVE LINES ABOVE TO ADJUST BEHAVIOR

_weaponArray = []; 

//Automation process
 _cfgWeapons = "true" configClasses (configFile >> "cfgWeapons"); 
{
    _weaponString = configName (_x);
    _parents = [_x,true] call BIS_fnc_returnParents; 

    if ("Rifle" in _parents) then{
         _weaponArray append [_weaponString];
     }; 

    if ("Pistol" in _parents) then{
         _weaponArray append [_weaponString];
     };
 } forEach _cfgWeapons;
 //Automation process 

_itemBoxArray = []; 

_houseArray = (_markerPos nearObjects ["house", _house_radius]) select {random 100 < _chance_house};

{
     _buildingPositions = [_x, (random ceil _max_positions)] call BIS_fnc_buildingPositions;
     {
         _weapon = _weaponArray select (floor (random (count _weaponArray)));
         _itemBox = _box_classname createVehicle [0,0,0];
         _itemBox setPos _x;
         _itemBox addWeaponCargoGlobal [_weapon,1];
         _magazines = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines");
         _mag = _magazines select (floor (random (count _magazines)));
         _itemBox addMagazineCargoGlobal [_mag, (ceil random _max_magazines)];
         _itemBoxArray = _itemBoxArray + [_itemBox];
     } forEach _buildingPositions;
 } forEach _houseArray; 

sleep 120;
 {
     deleteVehicle _x;
 } forEach _itemBoxArray; 

_chance_house  -> is the chance that a house will be handled by the script to spawn loot in it

                           ->if this check fails then there will no itembox spawned in this house (but maybe on other houses)

_max_positions -> the maximum number of house positions used for spawning an itembox

                             ->if this check fails then there will no itembox spawned on the specific house position (but maybe on other positions)

_house_radius  -> is the radius where in houses are recognized for spawning loot

_box_classname -> change this back to "WeaponHolderSimulated" if u have an issue with the itemboxes (like flying boxes or such)

_max_magazines -> the maximum of magazines spawned in an itembox.

Share this post


Link to post
Share on other sites

Sarog

2 hours ago, sarogahtyp said:

good point. I didnt read the wiki entry for BIS_fnc_buildingPositions

 

I changed the script and substituted the chance for a house position with a maximum number of house positions used:


_unit = _this select 0;
_addActionID = _this select 2; 
_unit removeAction _addActionID; 
_markerPos = getMarkerPos "lootMarker";

// EDIT THE FOLLOWING FIVE LINES TO ADJUST BEHAVIOR
_chance_house = 50;            // chance in percent to spawn an itembox inside a house
_max_positions = 4;           // maximum number of building positions used
_house_radius = 100;           // only houses inside of this radius will be handled
_box_classname = "GroundWeaponHolder";   //"WeaponHolderSimulated" 
_max_magazines = 5;          //maximum number of magazins spawned in item box
// EDIT THE FIVE LINES ABOVE TO ADJUST BEHAVIOR

_weaponArray = []; 

//Automation process
 _cfgWeapons = "true" configClasses (configFile >> "cfgWeapons"); 
{
    _weaponString = configName (_x);
    _parents = [_x,true] call BIS_fnc_returnParents; 

    if ("Rifle" in _parents) then{
         _weaponArray append [_weaponString];
     }; 

    if ("Pistol" in _parents) then{
         _weaponArray append [_weaponString];
     };
 } forEach _cfgWeapons;
 //Automation process 

_itemBoxArray = []; 

_houseArray = (_markerPos nearObjects ["house", _house_radius]) select {random 100 < _chance_house};

{
     _buildingPositions = [_x, (random ceil _max_positions)] call BIS_fnc_buildingPositions;
     {
         _weapon = _weaponArray select (floor (random (count _weaponArray)));
         _itemBox = _box_classname createVehicle [0,0,0];
         _itemBox setPos _x;
         _itemBox addWeaponCargoGlobal [_weapon,1];
         _magazines = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines");
         _mag = _magazines select (floor (random (count _magazines)));
         _itemBox addMagazineCargoGlobal [_mag, (ceil random _max_magazines)];
         _itemBoxArray = _itemBoxArray + [_itemBox];
     } forEach _buildingPositions;
 } forEach _houseArray; 

sleep 120;
 {
     deleteVehicle _x;
 } forEach _itemBoxArray; 

_chance_house  -> is the chance that a house will be handled by the script to spawn loot in it

                           ->if this check fails then there will no itembox spawned in this house (but maybe on other houses)

_max_positions -> the maximum number of house positions used for spawning an itembox

                             ->if this check fails then there will no itembox spawned on the specific house position (but maybe on other positions)

_house_radius  -> is the radius where in houses are recognized for spawning loot

_box_classname -> change this back to "WeaponHolderSimulated" if u have an issue with the itemboxes (like flying boxes or such)

_max_magazines -> the maximum of magazines spawned in an itembox.

Dude I absolutely love people like you. Thank you so much I haven't tested it, though when I get home from work I will. I have one other question that maybe you or someone else could help me with.

 

How could I have this script run using a trigger? Such as nothing spawns until blufor shows up then all the loot spawns into the town you have just entered. When you leave the town all the loot despawns?

 

I'm trying make a zombie survival mission for me and some friends to play but like I said I havent written script in this game in soo long I've forgotten everything I knew.

Share this post


Link to post
Share on other sites
deleted

The script is completly untested and may contain bugs and errors. I ll test it myself at a later time but u can test it youself and report error or bug and i ll try to fight against.

 

How it should work:

The script checks which alive players are inside of a trigger of _trigger_array;

Then it gets the nearest building of each of that players and spawns the loot inside of it.

It will spawn more loot if more players are near the same building.

If a building wherin is already spawned loot is not the closest building to any player anymore then the script will delete the loot.

The script ensures that loot is spawned only in that houses which are closest to each of the players which are inside of a loot trigger.

The script is thought to run on server only and should be spawned or execVM'ed from initServer.sqf.

Then it runs all the time with delay between 20-30 seconds between spawning/deleting loot.

 

How to adjust/use the script:

_trigger_array   -> contains the names of triggers which were setup in editor. The script doesnt matters any trigger preferences except the trigger area.

_spawn_chance  ->  The chance to spawn loot on a specific house position. If the same house is the closest building to multiple players then the check runs for each player and the chance to spawn loot on a specific position will be much higher.

_box_classname  -> change this back to "WeaponHolderSimulated" if u have an issue with the itemboxes (like flying boxes or such)

_max_magazines -> the maximum of magazines spawned in an itembox.

 

EDIT:

deleted script. see next post for it

Share this post


Link to post
Share on other sites

Tested and working:

Spawns weapons, items and bags in buildings near to alive players inside a trigger.
Deletes stuff if players are not close enough anymore.
The script doesnt care about any trigger preferences except the trigger area.

 

initServer.sqf

fnc_spawn_loot = compileFinal preprocessFile "loot.sqf";
_d = [] spawn fnc_spawn_loot;

 

loot.sqf

/*
Author: Sarogahtyp

Description:
Spawns weapons, items and bags in buildings near to alive players inside a trigger.
Deletes stuff if players are not close enough anymore.
The script doesnt care about any trigger preferences except the trigger area.
Main while loop runs every 8-12 seconds.
Soft delayed item spawning to prevent performance impact.

How to adjust/use the script: 
_trigger_array   -> contains the names of triggers in which area loot should spawn.

_spawn_chance  ->  The chance to spawn lo																				ot on a specific house position. If the same house is the closest building to 

_item_chance -> chance to spawn an item instead of a weapon

_bag_chance -> chance to spawn a bag instead of a item

_max_magazines -> the maximum of magazines spawned in an itembox. 

_house_distance -> houses inside of this radius of a player will spawn loot

_exclude_loot -> you can add classnames there and those stuff will never spawn

_exclusive_loot -> add classnames here and nothing else will be spawned

_debug -> if true then u get hints about places were stuff was spawned or deleted and how many spawn places are active
*/

//***** EDIT BELOW TO ADJUST BEHAVIOR
_trigger_array = [trigger1, "marker1"];  //names of triggers or area markers in editor. if empty then spawning occures everywhere
_spawn_chance = 20;    //chance to spawn loot at specific house position
_launcher_chance = 5;  //chance to spawn a launcher as weapon instead of rifle, pistol or machine gun
_item_chance = 30;   //chance to spawn an item instead of a weapon
_bag_chance = 5; // chance to spawn a backpack instead of an item
_max_magazines = 6;    //maximum number of mags to spawn
_max_magazines_launcher = 2; //maximum number of ammo to spawn for rocket launchers
_max_magazines_gl = 5; //maximum number of ammo to spawn for grenade launchers
_house_distance = 50;  // houses with that distance to players will spawn loot
_exclude_loot = [];   //classnames of items which should never spawn
_exclusive_loot = []; //add classnames here and nothing else will be spawned
_debug = false;  //information about number of places where items were spawned or deleted
//***** EDIT ABOVE TO ADJUST BEHAVIOR

//***** init variables
_checked_positions = [];
_spawned_positions = [];
_weapons_mags_array = [];
_launchers_mags_array = [];
_items_array = [];
_bags_array = [];
_box_classname = "WeaponHolderSimulated_Scripted";

//***** get weapon and magazine classnames from config file
if (_spawn_chance > 0) then
{
 _cfgWeapons = ("true" configClasses (configFile >> "cfgWeapons")) select
 {
  _parents = [_x,true] call BIS_fnc_returnParents; 
  ("Rifle" in _parents) or ("MGun" in _parents) or ("Pistol" in _parents)
 }; 

 {
  _weaponString = configName (_x);
  _muzzle_class = (getArray (configFile >> "CfgWeapons" >> _weaponString >> "muzzles")) select 1;
  _muzzle_magazines = [];
  
  if !(isNil {_muzzle_class}) then
  {	
   _muzzle_magazines = getArray (configFile >> "CfgWeapons" >> _weaponString >> _muzzle_class >> "magazines")
  };


  if (!(_weaponString in _exclude_loot) and ((count _exclusive_loot == 0) or (_weaponString in _exclusive_loot))) then
  {
   _d = _weapons_mags_array pushBack [_weaponString, (getArray (configFile >> "CfgWeapons" >> _weaponString >> "magazines")), _muzzle_magazines];
  };
 } count _cfgWeapons;


 //***** get launchers and rocket/grenade classnames from config file
 if (_launcher_chance > 0) then
 {
  _cfgLaunchers = ("true" configClasses (configFile >> "cfgWeapons")) select
  {
   _parents = [_x,true] call BIS_fnc_returnParents; 
   ("Launcher" in _parents)
  }; 

  {
   _launcherString = configName (_x);
   if (!(_launcherString in _exclude_loot) and ((count _exclusive_loot == 0) or (_launcherString in _exclusive_loot))) then
   {
    _d = _launchers_mags_array pushBack [_launcherString, (getArray (configFile >> "CfgWeapons" >> _launcherString >> "magazines"))];
   };
  } count _cfgLaunchers;
 };

 //*****get items classnames from config file
 if (_item_chance > 0) then
 {
  _cfgItems = ("true" configClasses (configFile >> "cfgWeapons")) select
  {
   _parents = [_x,true] call BIS_fnc_returnParents; 
   ("ItemCore" in _parents)
  };

  {
   _itemString = configName (_x);
   if (!(_itemString in _exclude_loot) and ((count _exclusive_loot == 0) or (_itemString in _exclusive_loot))) then
   {
    _d = _items_array pushBack _itemString;
   };
  } count _cfgItems;
 };

 //*****get bag classnames from config file
 if (_bag_chance > 0) then
 {
  _cfgBags = ("true" configClasses (configFile >> "cfgVehicles")) select
  {
   _parents = [_x,true] call BIS_fnc_returnParents; 
   ("Bag_Base" in _parents)
  }; 

  {
   _bagString = configName (_x);
   if (!(_bagString in _exclude_loot) and ((count _exclusive_loot == 0) or (_bagString in _exclusive_loot))) then
   {
    _d = _bags_array pushBack _bagString;
   };
  } count _cfgBags;
 };

 while {true} do
 {
  _actual_positions = [];
  _new_positions = [];
  _loot_players = [];
  _justPlayers = (allPlayers - entities "HeadlessClient_F") select {alive _x};

  //***** get desired spawn positions for loot in the buildings close to players
  //***** which are inside of a loot trigger area
  if(count _trigger_array > 0) then
  {
   {
    {
     _loot_players pushBack _x;
	 true
    } count (_justPlayers inAreaArray _x);
	true
   } count _trigger_array;
  }
  else
  {
   _loot_players = _justPlayers;
  };
  
  {
   {
    {
     if (!(_x in _checked_positions) && (random 100 < _spawn_chance)) then 
     {
      _new_positions pushBackUnique _x;
      _spawned_positions pushBackUnique _x;
     };
     _checked_positions pushBackUnique _x;
     _actual_positions pushBackUnique _x;
     true
    } count (_x buildingPos -1); 
    true
   }count (nearestObjects [_x, ["house"], _house_distance]);
   true
  } count _loot_players;

  //***** delete loot out of range
  _checked_positions = _checked_positions select {_x in _actual_positions};
  _delete_positions = _spawned_positions select {!(_x in _actual_positions)};
  _spawned_positions = _spawned_positions - _delete_positions;

  _del_pos_num = count _delete_positions;

  {
   {
    deleteVehicle _x;
    true
   } count (nearestObjects [_x, [_box_classname], 3]);
   true
  } count _delete_positions;

  //***** spawn loot within 4 seconds (delay to prevent performance impact)
  _new_pos_num = count _new_positions;

  // debug things
  if(_debug) then
  {
   _spawned_num = count _spawned_positions;
   _checked_num = count _checked_positions;

   hint parseText format ["spawned new: %1, deleted old: %2 <br /> 
						spawned places: %3, overall places: %4", 
						_new_pos_num, _del_pos_num, _spawned_num, _checked_num];
  };

  if (_new_pos_num > 0) then
  {
   _sleep_delay = 4 / _new_pos_num;
   {
    _itembox = createVehicle [_box_classname, (_x vectorAdd [0, 0, 0.5]), [], 0.2, "NONE"];
   
    if (random 100 > _item_chance) then
    {
     if(random 100 > _launcher_chance) then
     {
      _weapon_mag = selectRandom _weapons_mags_array;
      _itembox addItemCargoGlobal [(_weapon_mag select 0), 1];
    
      for "_i" from 1 to (ceil random _max_magazines) do 
      {
       _itembox addItemCargoGlobal [(selectRandom (_weapon_mag select 1)), 1];
      };
	 
	  if (count (_weapon_mag select 2) > 0) then
	  {
	   for "_i" from 1 to (ceil random _max_magazines_gl) do 
       {
        _itembox addItemCargoGlobal [(selectRandom (_weapon_mag select 2)), 1];
       };
	  };
     }
     else
     {
      _launcher_mag = selectRandom _launchers_mags_array;
      _itembox addItemCargoGlobal [(_launcher_mag select 0), 1];
    
      for "_i" from 1 to (ceil random _max_magazines_launcher) do 
      {
       _itembox addItemCargoGlobal [(selectRandom (_launcher_mag select 1)), 1];
      };
     };
    }
    else
    {
     if(random 100 > _bag_chance) then
     {
      _item = selectRandom _items_array;
      _itembox addItemCargoGlobal [_item, 1];
     }
     else
     {
      _bag = selectRandom _bags_array;
      _itembox addBackpackCargoGlobal [_bag, 1];
     };
    };
    sleep _sleep_delay;
   } count _new_positions;
  };
  sleep (1 + random 2);
 };
};

 

  • Like 1

Share this post


Link to post
Share on other sites

Personally, i don't spawn with trigger, but in the nearest house(s) along with player's) positions. When a house is "treated", there is a timer to rearm the loot. I randomize the elected houses, the positions in houses and of course weapons/nbr of mags and/or items.

Share this post


Link to post
Share on other sites

Thats a really nice script thanks

Edited by FallujahMedic -FM-
No need to quote a massive wall of text.

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

×