From 6c9a12cd564d1666b8ce5c77280567e98441992a Mon Sep 17 00:00:00 2001 From: eraser1 Date: Tue, 1 Sep 2015 18:23:32 -0500 Subject: [PATCH] Better locality management --- @ExileServer/addons/a3_dms/fn_DMS_preInit.sqf | 1 + .../a3_dms/scripts/AILocalityManager.sqf | 38 +++++++++++ .../addons/a3_dms/scripts/SetAILocality.sqf | 68 ++++++++++++++----- .../addons/a3_dms/scripts/SpawnAIGroup.sqf | 9 +-- 4 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 @ExileServer/addons/a3_dms/scripts/AILocalityManager.sqf diff --git a/@ExileServer/addons/a3_dms/fn_DMS_preInit.sqf b/@ExileServer/addons/a3_dms/fn_DMS_preInit.sqf index c5e1443..05b5a4f 100644 --- a/@ExileServer/addons/a3_dms/fn_DMS_preInit.sqf +++ b/@ExileServer/addons/a3_dms/fn_DMS_preInit.sqf @@ -34,6 +34,7 @@ DMS_CreateMarker = compileFinal preprocessFileLineNumbers "\x\addons\dms\sc DMS_FindSuppressor = compileFinal preprocessFileLineNumbers "\x\addons\dms\scripts\FindSuppressor.sqf"; DMS_SpawnCrate = compileFinal preprocessFileLineNumbers "\x\addons\dms\scripts\SpawnCrate.sqf"; DMS_SetAILocality = compileFinal preprocessFileLineNumbers "\x\addons\dms\scripts\SetAILocality.sqf"; +DMS_AILocalityManager = compileFinal preprocessFileLineNumbers "\x\addons\dms\scripts\AILocalityManager.sqf"; //Load config call compileFinal preprocessFileLineNumbers "\x\addons\dms\config.sqf"; \ No newline at end of file diff --git a/@ExileServer/addons/a3_dms/scripts/AILocalityManager.sqf b/@ExileServer/addons/a3_dms/scripts/AILocalityManager.sqf new file mode 100644 index 0000000..cb54a7f --- /dev/null +++ b/@ExileServer/addons/a3_dms/scripts/AILocalityManager.sqf @@ -0,0 +1,38 @@ +/* + DMS_AILocalityManager + Created by Defent and eraser1 + + Offloads AI groups to a nearby client in order to improve server performance. +*/ + + +if !(DMS_ai_offload_to_client) exitWith {}; + +{ + // Exile already has a group cleanup system, so we'll leave empty groups for it + if ((count (units _x))>1) then + { + private ["_leader", "_group", "_owner"]; + _leader = leader _x; + _group = _x; + if ((!isNull _leader) && {(alive _leader) && {!isPlayer _leader}}) then + { + _owner = objNull; + + { + if ((groupOwner _group) isEqualTo (owner _x)) exitWith + { + _owner = _x; + }; + + false; + } count allPlayers; + + if ((isNull _owner) || {(_owner distance2D _leader)>3500}) then + { + [_group,_leader] call DMS_SetAILocality; + }; + }; + }; + false; +} count allGroups; \ No newline at end of file diff --git a/@ExileServer/addons/a3_dms/scripts/SetAILocality.sqf b/@ExileServer/addons/a3_dms/scripts/SetAILocality.sqf index 11af5a9..77c364f 100644 --- a/@ExileServer/addons/a3_dms/scripts/SetAILocality.sqf +++ b/@ExileServer/addons/a3_dms/scripts/SetAILocality.sqf @@ -1,28 +1,62 @@ /* - Makes a random player within 3 KM of the AI the owner. + DMS_SetAILocality + Created by Defent and eraser1 + + Usage: + [ + _groupOrUnit, + _posOrObject // Does not have to be defined if element 1 is a unit + ] call DMS_SetAILocality; + + Makes a random player within 3 KM of the AI unit or group the owner. Offloading AI can increase server performance. Could however have negative effects if target player has a potato PC. - How To Use: - [_pos, _group] call DMS_SetAILocality; - Posistion of the player and the group that the AIs are in. - */ -private ["_group","_position","_exit","_randomPlayer"]; +private ["_AI", "_pos", "_exit", "_client"]; -_group = _this select 0; -_position = _this select 1; -_exit = false; +_AI = param [0,objNull,[objNull,grpNull]]; -while {!_exit} do +if (isNull _AI) exitWith { - _randomPlayer = call ExileServer_system_session_getRandomPlayer; - if((_randomPlayer distance2D _position) < 3000)then - { - _exit = true; - }; + diag_log format ["DMS ERROR :: Calling DMS_SetAILocality with null parameter; _this: %1",_this]; }; -ExileServerOwnershipSwapQueue pushBack [_group,_randomPlayer]; +if ((typeName _AI)=="OBJECT") then +{ + _pos = _AI; +} +else +{ + _pos = param [1,"",[objNull,[]],[2,3]]; +}; -true \ No newline at end of file +if (_pos isEqualTo "") exitWith +{ + diag_log format ["DMS ERROR :: Calling DMS_SetAILocality with invalid position; this: %1",_this]; +}; + +_client = objNull; + +{ + if ((alive _x) && {(_x distance2D _pos)<=3000}) exitWith + { + _client = _x; + }; + false; +} count allPlayers; + +if (!isNull _client) then{ + ExileServerOwnershipSwapQueue pushBack [_AI,_client]; + if (DMS_DEBUG) then + { + diag_log format ["DMS_DEBUG SetAILocality :: Ownership swap of %1 (%4) to %2 (%3) is added to ExileServerOwnershipSwapQueue.",_AI,name _client,getPlayerUID _client,typeName _AI]; + }; +} +else +{ + if (DMS_DEBUG) then + { + diag_log format ["DMS_DEBUG SetAILocality :: No viable client found for the ownership of %1!",_AI]; + }; +}; \ No newline at end of file diff --git a/@ExileServer/addons/a3_dms/scripts/SpawnAIGroup.sqf b/@ExileServer/addons/a3_dms/scripts/SpawnAIGroup.sqf index 60d7221..9fab97b 100644 --- a/@ExileServer/addons/a3_dms/scripts/SpawnAIGroup.sqf +++ b/@ExileServer/addons/a3_dms/scripts/SpawnAIGroup.sqf @@ -110,14 +110,7 @@ if(_pos_z == 0) then if (DMS_ai_offload_to_client) then -{ - /* - _client = (allPlayers call BIS_fnc_selectRandom); - ExileServerOwnershipSwapQueue pushBack [_group,_client]; - */ - - [_group,_pos] call DMS_SetAILocality; - +{ if(DMS_DEBUG) then { diag_log format["DMS_DEBUG SpawnAIGroup :: Swapping group ownership of %1 to clients.",_group];