From ce57432c8994154658c16ab944b644a4d8b3e215 Mon Sep 17 00:00:00 2001 From: Skaronator Date: Sat, 30 Nov 2013 22:03:53 +0100 Subject: [PATCH 1/3] Fix SpawnVehicle #821 --- SQF/dayz_server/init/server_functions.sqf | 35 +++++++++-------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/SQF/dayz_server/init/server_functions.sqf b/SQF/dayz_server/init/server_functions.sqf index a4dae01be..96a1902b0 100644 --- a/SQF/dayz_server/init/server_functions.sqf +++ b/SQF/dayz_server/init/server_functions.sqf @@ -211,38 +211,31 @@ BuildingList = []; } forEach (MarkerPosition nearObjects ["building",DynamicVehicleArea]); spawn_vehicles = { - private ["_weights","_isOverLimit","_isAbort","_counter","_index","_vehicle","_velimit","_qty","_isAir","_isShip","_position","_dir","_istoomany","_veh","_objPosition","_marker","_iClass","_itemTypes","_cntWeights","_itemType","_num","_allCfgLoots"]; + private ["_weights","_index","_vehicle","_velimit","_qty","_isAir","_isShip","_position","_dir","_istoomany","_veh","_objPosition","_marker","_iClass","_itemTypes","_cntWeights","_itemType","_num","_allCfgLoots"]; if (isDedicated) then { - - _isOverLimit = true; - _isAbort = false; - _counter = 0; - while {_isOverLimit} do { - waitUntil{!isNil "BIS_fnc_selectRandom"}; - _index = AllowedVehiclesList call BIS_fnc_selectRandom; + while {count AllowedVehiclesList > 0} do { + // BIS_fnc_selectRandom replaced because the index may be needed to remove the element + _index = floor random count AllowedVehiclesList; + _random = AllowedVehiclesList select _index; - _vehicle = _index select 0; - _velimit = _index select 1; + _vehicle = _random select 0; + _velimit = _random select 1; _qty = {_x == _vehicle} count serverVehicleCounter; // If under limit allow to proceed - if(_qty <= _velimit) then { - _isOverLimit = false; - }; + if (_qty <= _velimit) exitWith {}; - // counter to stop after 5 attempts - _counter = _counter + 1; - - if(_counter >= 5) then { - _isOverLimit = false; - _isAbort = true; - }; + // vehicle limit reached, remove vehicle from list + // since elements cannot be removed from an array, overwrite it with the last element and cut the last element of (as long as order is not important) + _lastIndex = (count AllowedVehiclesList) - 1; + AllowedVehiclesList set [_index, AllowedVehiclesList select _lastIndex]; + AllowedVehiclesList resize _lastIndex; }; - if (_isAbort) then { + if (count AllowedVehiclesList == 0) then { diag_log("DEBUG: unable to find suitable vehicle to spawn"); } else { From 9cd4995ef441302fda562a01f04ef5f08a21df8d Mon Sep 17 00:00:00 2001 From: Skaronator Date: Sat, 30 Nov 2013 22:59:39 +0100 Subject: [PATCH 2/3] Update Private Vars (ty @vos) --- SQF/dayz_server/init/server_functions.sqf | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/SQF/dayz_server/init/server_functions.sqf b/SQF/dayz_server/init/server_functions.sqf index 96a1902b0..e4ba0625b 100644 --- a/SQF/dayz_server/init/server_functions.sqf +++ b/SQF/dayz_server/init/server_functions.sqf @@ -211,7 +211,7 @@ BuildingList = []; } forEach (MarkerPosition nearObjects ["building",DynamicVehicleArea]); spawn_vehicles = { - private ["_weights","_index","_vehicle","_velimit","_qty","_isAir","_isShip","_position","_dir","_istoomany","_veh","_objPosition","_marker","_iClass","_itemTypes","_cntWeights","_itemType","_num","_allCfgLoots"]; + private ["_random","_lastIndex","_weights","_index","_vehicle","_velimit","_qty","_isAir","_isShip","_position","_dir","_istoomany","_veh","_objPosition","_marker","_iClass","_itemTypes","_cntWeights","_itemType","_num","_allCfgLoots"]; if (isDedicated) then { @@ -230,9 +230,11 @@ spawn_vehicles = { // vehicle limit reached, remove vehicle from list // since elements cannot be removed from an array, overwrite it with the last element and cut the last element of (as long as order is not important) - _lastIndex = (count AllowedVehiclesList) - 1; - AllowedVehiclesList set [_index, AllowedVehiclesList select _lastIndex]; - AllowedVehiclesList resize _lastIndex; + if (_lastIndex != _index) then { + _lastIndex = (count AllowedVehiclesList) - 1; + AllowedVehiclesList set [_index, AllowedVehiclesList select _lastIndex]; + AllowedVehiclesList resize _lastIndex; + }; }; if (count AllowedVehiclesList == 0) then { From 5f9a5cc31c7c893dcd84d99abf7bb444d314eaed Mon Sep 17 00:00:00 2001 From: Skaronator Date: Sat, 30 Nov 2013 23:05:32 +0100 Subject: [PATCH 3/3] Update if (ty @vos) --- SQF/dayz_server/init/server_functions.sqf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SQF/dayz_server/init/server_functions.sqf b/SQF/dayz_server/init/server_functions.sqf index e4ba0625b..cfa29879f 100644 --- a/SQF/dayz_server/init/server_functions.sqf +++ b/SQF/dayz_server/init/server_functions.sqf @@ -230,11 +230,11 @@ spawn_vehicles = { // vehicle limit reached, remove vehicle from list // since elements cannot be removed from an array, overwrite it with the last element and cut the last element of (as long as order is not important) + _lastIndex = (count AllowedVehiclesList) - 1; if (_lastIndex != _index) then { - _lastIndex = (count AllowedVehiclesList) - 1; AllowedVehiclesList set [_index, AllowedVehiclesList select _lastIndex]; - AllowedVehiclesList resize _lastIndex; }; + AllowedVehiclesList resize _lastIndex; }; if (count AllowedVehiclesList == 0) then {