Optimize loot init file

Optimizing the code in this file. It compiled in half the time of the original. Compiles have been added to the top because there is not very many.
This commit is contained in:
worldwidesorrow
2020-01-09 16:46:30 -06:00
committed by GitHub
parent abb4046281
commit f55e268606

View File

@@ -1,54 +1,17 @@
#include "\z\addons\dayz_code\util\math.hpp"
#include "Loot.hpp"
//If defined, minimizes memory usage at the expense of initialization time.
//#define MINIMIZE_MEMORY
private
[
"_cfgGroups",
"_cfg",
"_lootGroup",
"_weight",
"_weighted",
"_index",
"_indices",
"_count",
"_gcd"
];
#ifdef MINIMIZE_MEMORY
private ["_lootDefCompare", "_y", "_a", "_b", "_r"];
_lootDefCompare =
{
_a = _this select 0;
_b = _this select 1;
if ((_a select 0) != (_b select 0)) exitWith { false };
if ((_a select 1) != (_b select 1)) exitWith { false };
_r = true;
switch (_a select 0) do
{
case Loot_PILE:
{
_r = ((_a select 2) == (_b select 2)) &&
{(_a select 3) == (_b select 3)};
};
case Loot_CONTAINER:
{
_r = ((_a select 2) == (_b select 2)) &&
{(_a select 3) == (_b select 3)} &&
{(_a select 4) == (_b select 4)};
};
};
_r
// Compiles
dz_fn_loot_select = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\select.sqf";
dz_fn_loot_spawnGroup = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\spawnGroup.sqf";
dz_fn_loot_insert = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\insert.sqf";
dz_fn_loot_insertCargo = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\insertCargo.sqf";
if (isServer) then {
dz_fn_loot_spawn = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\spawn_server.sqf";
} else {
dz_fn_loot_spawn = compile preprocessFileLineNumbers "\z\addons\dayz_code\loot\spawn.sqf";
};
#endif
#include "\z\addons\dayz_code\Configs\CfgLoot\LootDefines.hpp"
private ["_cfgGroups","_cfg","_lootGroup","_weight","_weighted","_index","_count"];
dz_loot_groups = [];
dz_loot_weighted = [];
@@ -56,86 +19,45 @@ dz_loot_definitions = [];
_cfgGroups = missionConfigFile >> "CfgLoot" >> "Groups";
for "_i" from 0 to (count _cfgGroups) - 1 do
{
for "_i" from 0 to (count _cfgGroups) - 1 do {
_cfg = _cfgGroups select _i;
dz_loot_groups set [_i, configName _cfg];
};
dz_loot_weighted resize count dz_loot_groups;
for "_i" from 0 to (count _cfgGroups) - 1 do
{
_lootGroup = getArray (_cfgGroups select _i);
_weighted = [];
if ((count _lootGroup) > 0) then
_lootGroup = getArray _cfg;
_weighted = [];
_count = 0;
{
_indices = [];
_indices resize count _lootGroup;
// Round the weight just in case somebody doesn't use a whole number.
_weight = round(_x select 1);
{
_weight = _x select 1;
//Remove weight from _x
for "_j" from 1 to (count _x) - 2 do { _x set [_j, _x select (_j + 1)]; };
_x resize ((count _x) - 1);
_index = -1;
//Search for an existing identical loot definition
#ifdef MINIMIZE_MEMORY
_y = _x;
{
if ([_y, _x] call _lootDefCompare) exitWith
{
_index = _forEachIndex;
};
}
foreach dz_loot_definitions;
#endif
//Existing loot definition not found, add it and set the index to point to the new definition
if (_index == -1) then
{
_index = count dz_loot_definitions;
dz_loot_definitions set [_index, _x];
};
_lootGroup set [_forEachIndex, round(_weight * 100)];
_indices set [_forEachIndex, _index];
}
foreach _lootGroup;
//Remove weight from _x
for "_j" from 1 to (count _x) - 2 do { _x set [_j, _x select (_j + 1)]; };
_x resize ((count _x) - 1);
//Calculate GCD of all the weights
_gcd = Math_GCDArray(_lootGroup);
//_gcd = _lootGroup call math_gcdx;
// Add resized group array to definitions array
_index = count dz_loot_definitions;
dz_loot_definitions set [_index, _x];
_count = 0;
{
_weight = _x / _gcd;
_weighted resize (_count + _weight);
for "_j" from _count to (_count + _weight - 1) do
{
_weighted set [_j, _indices select _forEachIndex];
};
_count = _count + _weight;
}
foreach _lootGroup;
};
// Build weighted array for the group
for "_j" from _count to (_count + _weight - 1) do {
_weighted set [_j, _index];
};
_count = _count + _weight;
} count _lootGroup;
dz_loot_weighted set [_i, _weighted];
};
{
switch (_x select 0) do
{
case Loot_GROUP: { _x set [1, dz_loot_groups find (_x select 1)]; };
case Loot_PILE: { _x set [1, dz_loot_groups find (_x select 1)]; };
case Loot_CONTAINER: { _x set [2, dz_loot_groups find (_x select 2)]; };
case Loot_CUSTOM: { _x set [1, compile (_x select 1)]; };
if !((_x select 0) in [0,2,3,5,6]) then { // skip types other than listed below
switch (_x select 0) do {
case Loot_GROUP: { _x set [1, dz_loot_groups find (_x select 1)]; };
case Loot_PILE: { _x set [1, dz_loot_groups find (_x select 1)]; };
case Loot_CONTAINER: { _x set [2, dz_loot_groups find (_x select 2)]; };
case Loot_CUSTOM: { _x set [1, compile (_x select 1)]; };
};
};
}
foreach dz_loot_definitions;
} count dz_loot_definitions;