Advertisement
Guest User

Untitled

a guest
Nov 15th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. /*
  2. ________ ____ ____ ____ __
  3. /\_____ \ /\ _`\ /\ _`\ /\ _`\ /\ \ __
  4. \/____//'/'\ \ \L\ \\ \ \L\_\ \ \ \/\_\ __ ___\ \ \___ /\_\ ___ __
  5. //'/' \ \ _ <'\ \ _\L \ \ \/_/_ /'__`\ /'___\ \ _ `\/\ \ /' _ `\ /'_ `\
  6. //'/'___ \ \ \L\ \\ \ \L\ \ \ \ \L\ \/\ \L\.\_/\ \__/\ \ \ \ \ \ \/\ \/\ \/\ \L\ \
  7. /\_______\\ \____/ \ \____/ \ \____/\ \__/.\_\ \____\\ \_\ \_\ \_\ \_\ \_\ \____ \
  8. \/_______/ \/___/ \/___/ _______\/___/ \/__/\/_/\/____/ \/_/\/_/\/_/\/_/\/_/\/___L\ \
  9. /\______\ /\____/
  10. \/______/ \_/__/
  11.  
  12. Based on NOU_Cache originally written for MSO, modified and redistributed with permission from NOU.
  13. Today, 06:27 PM (5/2/2014)
  14. NouberNou
  15. Re: Nou_Caching
  16. "Do whatever you please with it, I barely had any contribution to the MSO module besides the original proof of concept a long time ago. No need to attribute in any special way."
  17.  
  18. Code had credit to Nou ---> "// Thanks to Nou for this code" in MSO commit 743cfe7f169602b707e6c61488db15cd85992d05 in which I have his explicit permission to modify and redistribute. If explicit permission from original author is invalid, MSO's Apache license applies.
  19.  
  20. Exceptions:
  21. Vehicle caching FSM - Inspired by CEP_Caching
  22. */
  23.  
  24. //Config switches call: [1000,true]execvm "ZBE_Caching\main.sqf"; in init.sqf
  25. ZBE_cache_dist = _this select 0;
  26. ZBE_cache_debug = _this select 1;
  27. ZBE_cache_vehicle = _this select 2;
  28.  
  29. //Caching functions
  30. ZBE_cached = 0;
  31. ZBE_suspended = 0;
  32. ZBE_stats = "";
  33.  
  34. ZBE_cacheGroup = {
  35.  
  36. if(!(_this getVariable ["Cached", false])) then {
  37. _this setVariable ["Cached", true];
  38. {
  39.  
  40. private["_pos"];
  41. if(_x != leader _this && {!(isPlayer _x)} && {!("driver" in assignedVehicleRole _x)}) then {
  42. /*_x disableAI "TARGET";
  43. _x disableAI "AUTOTARGET";
  44. _x disableAI "MOVE";
  45. _x disableAI "ANIM";
  46. _x disableAI "FSM";*/
  47. //_x setSpeedMode "FULL"; //Fix for leader walking (slow) after his buddies are cached
  48. _x enableSimulation false;
  49. _x allowDamage false;
  50. _x hideobject true;
  51. /*if (vehicle _x == _x) then {
  52. _pos = getPosATL _x;
  53. _pos set [2, -100];
  54. _x setPosATL _pos;
  55. };*/
  56. ZBE_cached = ZBE_cached + 1;
  57. } else {
  58. _x allowDamage true;
  59. _x hideobject false;
  60. _x enableSimulation true;
  61. //_x setSpeedMode "NORMAL"; //Fix for leader walking (slow) after his buddies are cached
  62. /*_x enableAI "TARGET";
  63. _x enableAI "AUTOTARGET";
  64. _x enableAI "MOVE";
  65. _x enableAI "ANIM";
  66. _x enableAI "FSM";*/
  67. };
  68.  
  69. } forEach units _this;
  70. if (ZBE_cache_debug) then {publicVariable "ZBE_cached";};
  71. };
  72. };
  73.  
  74. ZBE_uncacheGroup = {
  75.  
  76. if(_this getVariable ["Cached",true]) then {
  77. _this setVariable ["Cached", false];
  78. {
  79. if(_x != leader _this && {!(isPlayer _x)} && {!("driver" in assignedVehicleRole _x)}) then {
  80.  
  81. if (vehicle _x == _x) then {
  82. _x setPosATL (formationPosition _x);
  83. };
  84. [_x]spawn {sleep 3;(_this select 0) allowDamage true;};//Spawned AI on leader dropped to death on inclines. This short sleep should be enough for uncached units to drop to ground without dying. Put in a spawn as to not halt function execution, performance impact should be minimal.
  85. _x enableSimulation true;
  86. _x hideobject false;
  87. /*_x setSpeedMode "NORMAL";
  88. _x enableAI "TARGET";
  89. _x enableAI "AUTOTARGET";
  90. _x enableAI "MOVE";
  91. _x enableAI "ANIM";
  92. _x enableAI "FSM";*/
  93.  
  94. if(ZBE_cached > 0) then {ZBE_cached = ZBE_cached - 1;};
  95. };
  96. } forEach units _this;
  97. if (ZBE_cache_debug) then {publicVariable "ZBE_cached";};
  98. };
  99. };
  100.  
  101. ZBE_syncleader = {
  102. if (!(simulationEnabled (leader _this))) then {
  103. private ["_x"];
  104. _x = leader _this;
  105.  
  106. _x allowDamage true;
  107. _x enableSimulation true;
  108. _x hideobject false;
  109. /*_x enableAI "TARGET";
  110. _x enableAI "AUTOTARGET";
  111. _x enableAI "MOVE";
  112. _x enableAI "ANIM";
  113. _x enableAI "FSM";*/
  114. if (ZBE_cache_debug) then {
  115. player sidechat format ["Synced %1 TL!",_x];
  116. };
  117. };
  118. };
  119.  
  120. ZBE_suspendGroup = {
  121.  
  122. if(!(_this getVariable ["Suspended", false])) then {
  123. _this setVariable ["Suspended", true];
  124. {
  125. _x enableSimulation false;
  126. ZBE_suspended = ZBE_suspended + 1;
  127. } forEach units _this - [leader _this];
  128. };
  129. };
  130.  
  131. ZBE_unsuspendGroup = {
  132.  
  133. if(_this getVariable ["Suspended", true]) then {
  134. _this setVariable ["Suspended", false];
  135. {
  136. _x enableSimulation true;
  137. if(ZBE_suspended > 0) then {ZBE_suspended = ZBE_suspended - 1;};
  138. } forEach units _this - [leader _this];
  139. };
  140. };
  141.  
  142. ZBE_deleteunitsnotleaderfnc = {
  143. {
  144. deleteVehicle _x;
  145. } forEach units _this - [leader _this];
  146. };
  147.  
  148. ZBE_deleteunitsnotleader = {
  149. {
  150. _x call ZBE_deleteunitsnotleaderfnc;
  151. } forEach allGroups;
  152. };
  153.  
  154. ZBE_closestUnit = {
  155.  
  156. private["_units", "_unit", "_dist", "_udist"];
  157. _units = _this select 0;
  158. _unit = _this select 1;
  159. _dist = 10^5;
  160.  
  161. {
  162. _udist = _x distance _unit;
  163. if (_udist < _dist) then {_dist = _udist;};
  164. } forEach _units;
  165. _dist;
  166. };
  167.  
  168. ZBE_triggerUnits = {
  169.  
  170. private ["_ZBE_leader","_trigUnits"];
  171. _ZBE_leader = _this select 0;
  172. _trigUnits = [];
  173. {
  174. if ((((side _x) getFriend (side _ZBE_leader)) <= 0.6)) then {
  175. _trigUnits set [count _trigUnits, leader _x];
  176. };
  177. } forEach allGroups;
  178. _trigUnits = _trigUnits + ([] call BIS_fnc_listPlayers);
  179. _trigUnits;
  180. };
  181.  
  182. ZBE_los = {
  183. //code for line of sight check WIP
  184. };
  185.  
  186. fn_centerpos = {
  187. private ["_center","_return"];
  188. switch toLower(worldName) do {
  189. case "altis": {
  190. _return = [15101.8,16846.1,0.00143814];
  191. };
  192. default {
  193. _center = getArray (configFile >> "CfgWorlds" >> worldName >> "centerPosition");
  194. _return = _center;
  195. };
  196. };
  197. _return;
  198. };
  199.  
  200. waitUntil{typeName allGroups == "ARRAY"};
  201. waitUntil{typeName allUnits == "ARRAY"};
  202. waitUntil{typeName playableUnits == "ARRAY"};
  203.  
  204. //Client loop
  205. if(!isServer) then {
  206. diag_log format["%1 ZBE Caching (%2) Starting", time, name player];
  207. [] spawn {
  208. while {true} do {
  209. {
  210. private["_closest"];
  211. _closest = [units player, leader _x] call ZBE_closestUnit;
  212. if (_closest > (ZBE_cache_dist * 1.1)) then {
  213. _x call ZBE_suspendGroup;
  214. };
  215.  
  216. if (_closest < ZBE_cache_dist) then {
  217. _x call ZBE_unsuspendGroup;
  218. };
  219. } forEach allGroups;
  220.  
  221. if(ZBE_cache_debug) then {if(ZBE_stats != format["%1 Groups %2/%3/%4 All/Suspended/Cached Units %5/%6 All/Cached Vehicles", count allGroups, count allUnits, ZBE_suspended, ZBE_cached, allvehicleszbe, allvehiclescachedzbe]) then {
  222. ZBE_stats = format["%1 Groups %2/%3/%4 All/Suspended/Cached Units %5/%6 All/Cached Vehicles", count allGroups, count allUnits, ZBE_suspended, ZBE_cached, allvehicleszbe, allvehiclescachedzbe];
  223. diag_log format["%1 ZBE Caching (%2) # %3", time, name player, ZBE_stats];
  224. hint format["%1 ZBE Caching # %2", time, ZBE_stats];};};
  225. sleep 0.01;
  226. };
  227. };
  228. };
  229. //Server loop
  230. if(isServer) then {
  231. diag_log format["%1 ZBE Caching (%2) Starting", time,"SERVER"];
  232. []spawn {
  233. while {true} do {
  234. {
  235. private["_closest"];
  236. _closest = [([leader _x] call ZBE_triggerUnits), leader _x] call ZBE_closestUnit;
  237. //if (diag_fps < 40 && {_closest > (ZBE_cache_dist * 1.1)}) then {
  238. if (_closest > (ZBE_cache_dist * 1.1)) then {
  239. _x call ZBE_cacheGroup;
  240. }; //No use caching units if serverFPS is greater then 40. Also used lazy eval for some more performance savings.
  241.  
  242. if (_closest < ZBE_cache_dist) then {
  243. _x call ZBE_uncacheGroup;
  244. }; //Needs to uncache regardless of server FPS.
  245. _x call ZBE_syncleader; //Resyncs group leader if one dies (uncaches)
  246. } forEach allGroups;
  247.  
  248. if(ZBE_cache_debug) then {if(ZBE_stats != format["%1 Groups %2/%3 Active/Cached Units %4/%5 All/Cached Vehicles", count allGroups, (count allUnits) - ZBE_cached, ZBE_cached,allvehicleszbe, allvehiclescachedzbe]) then {
  249. ZBE_stats = format["%1 Groups %2/%3 Active/Cached Units %4/%5 All/Cached Vehicles", count allGroups, (count allUnits) - ZBE_cached, ZBE_cached,allvehicleszbe, allvehiclescachedzbe];
  250. diag_log format["%1 ZBE Caching (%2) # %3", time,"SERVER", ZBE_stats];
  251. hint format["%1 ZBE Caching # %2", time, ZBE_stats];};}; //A bit spammy to the RPT. May add ZBE_cache_debug switch later on.
  252. sleep 0.01;
  253. };
  254. };
  255. };
  256.  
  257. //Vehicle Caching Alpha inspired by CEP_Caching
  258. allvehicleszbe = 0;
  259. allvehiclescachedzbe = 0;
  260. [ZBE_cache_vehicle, 0] spawn {
  261. private ["_timex","_Dist","_dly","_assets"];
  262. zbe_cached_vehs = [];
  263. _Dist = _this select 0;
  264. _dly = _this select 1;
  265. while {true} do {
  266. _assets = (call fn_centerpos) nearEntities [["LandVehicle","Air","Ship"],50000];
  267. if(_dly > 0) then {sleep _dly;};
  268. {
  269. if !(_x in zbe_cached_vehs) then {
  270. zbe_cached_vehs = zbe_cached_vehs + [_x];
  271. if (isDedicated) then {} else {[_x, _Dist] execFSM "ZBE_Caching\zbe_vehiclecaching.fsm";};
  272. };
  273. } forEach _assets;
  274.  
  275. {
  276. if (!(_x in _assets)) then {
  277. zbe_cached_vehs = zbe_cached_vehs - [_x];
  278. };
  279. } forEach zbe_cached_vehs;
  280.  
  281.  
  282. _timex = time + 25;
  283. allvehicleszbe = count _assets;
  284. waitUntil{time > _timex};
  285. };
  286. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement