Jump to content

Forums Announcement

Read-Only Mode for Announcements & Changelogs

Dear Survivors, we'd like to inform you that this forum will transition to read-only mode. From now on, it will serve exclusively as a platform for official announcements and changelogs.

For all community discussions, debates, and engagement, we encourage you to join us on our social media platforms: Discord, Twitter/X, Facebook.

Thank you for being a valued part of our community. We look forward to connecting with you on our other channels!

Stay safe out there,
Your DayZ Team

Sign in to follow this  
Publik

Update to fn_selectRandomWeighted for more efficient loot spawning

Recommended Posts

DayZ mod uses a "ticket" style random weighted selection algorithm. To be polite, it's inefficient and inflexible, allowing only 100 different levels of rarity for items. I'd pointed this out in the mod forums near when I left DayZ a year or whatever ago, and lo and behold the code's still here. I'm gonna repost it here since "it's been in the pipeline" for over a year now and would probably help out a bunch if the current spawning system takes too much time. Maybe it's already been fixed, just not on the client side.

 

Edit: Now, this wouldn't be as good of a fit if they're still planning to do loot spawning in huge batches, though I don't have access to whatever the new equivalent of building_spawnLoot.sqf is now to tell. Still doesn't change the fact that the ticket system is ugly and makes me cry. In the mod, the array generated from fnc_buildWeightedArray was built every time something was spawned, so hopefully they're building the array once then doing a loop on it, but they're going to have to switch to incremental loot spawns sooner or later.

 

compiles.sqf:

// Original method:BIS_fnc_selectRandom = compile preprocessFileLineNumbers "\z\addons\dayz_code\compile\fn_selectRandom.sqf"; //Checks which actions for nearby casualty// Add:PUB_fnc_selectRandom = compile preprocessFileLineNumbers "\z\addons\dayz_code\compile\fn_selectRandomlyWithWeights.sqf"; //PUBLIK'S VERSION

In building_spawnLoot.sqf and wherever weighted arrays are used:

// Replace:_weights = [_itemType,_itemChance] call fnc_buildWeightedArray;_index = _weights call BIS_fnc_selectRandom;// With:_index = _itemChance call PUB_fnc_selectRandom;

fn_selectRandomlyWithWeights.sqf

/*Author: Jeff RosenbergTakes: [_weights]_weights: The array of number weights. Items array doesn't matter, as long as the arrays match indexes.Returns: The index of an item selected from a weighted list of passed length*/private["_sum", "_current", "_index", "_target"];// Get the sum of the weights_sum = 0;for "_number" from 0 to ((count _this) - 1) do{_sum = _sum + (_this select _number);};_current = 0;_index = -1;_target = random _sum; // Pick a number between 0 and _sum// Select the itemwhile { _current < _target; } do{_index = _index + 1; // Increment the index_current = _current + (_this select _index); // Add another weight. If we've passed _target, we've selected the index _target resides in};_index

I really think you guys should open up DayZ to the modding community while there's still interest.

Edited by Publik
  • Like 1

Share this post


Link to post
Share on other sites
Sign in to follow this  

×