dayzplayerimplementaiming.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. DayZPlayerImplement
  3. this file is implemenation of dayzPlayer in script
  4. - logic of movement
  5. - camera switching logic
  6. */
  7. enum eSwayStates
  8. {
  9. DEFAULT,
  10. HOLDBREATH_IN,
  11. HOLDBREATH_STABLE,
  12. HOLDBREATH_EXHAUSTED,
  13. MAX
  14. }
  15. class PlayerSwayConstants
  16. {
  17. static const float SWAY_MULTIPLIER_DEFAULT = 1.0;
  18. static const float SWAY_MULTIPLIER_STABLE = 0.05;
  19. static const float SWAY_MULTIPLIER_EXHAUSTED = 0.6;
  20. static const float SWAY_TIME_IN = 1.5;
  21. static const float SWAY_TIME_STABLE = 3.0;
  22. static const float SWAY_TIME_EXHAUSTED = 1.5;
  23. static const float SWAY_TIME_OUT = 0.5;
  24. static const float SWAY_ROLL = 3;
  25. }
  26. class DayZPlayerImplementAiming
  27. {
  28. //-------------------------------------------------------------
  29. //!
  30. //! This HeadingModel - Clamps heading
  31. //!
  32. protected const float SWAY_WEIGHT_SCALER = 1;//use this to scale the sway effect up/down
  33. protected float m_HorizontalNoise;
  34. protected float m_HorizontalTargetValue;
  35. protected float m_HorizontalNoiseVelocity[1] = {0};
  36. protected DayZPlayerImplement m_PlayerDpi;
  37. protected PlayerBase m_PlayerPb;
  38. protected float m_TotalTime;
  39. protected float m_ReferenceTime = 0;
  40. protected float m_SwayStateStartTime;
  41. //protected float m_SwayStateStartTime[eSwayStates.MAX];
  42. protected float m_LastSwayMultiplier = PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT;
  43. protected float m_StateStartSwayMultiplier;
  44. protected float m_HorizontalNoiseXAxisOffset;
  45. protected float m_BreathingXAxisOffset;
  46. protected float m_BreathingYAxisOffset;
  47. protected bool m_HoldingBreathSet;
  48. protected bool m_AimNoiseAllowed = true;
  49. protected bool m_ProceduralRecoilEnabled = true;
  50. protected ref RecoilBase m_CurrentRecoil;
  51. protected int m_ShakeCount;
  52. protected float m_SwayWeight;
  53. protected float m_MaxVelocity;
  54. protected ref KuruShake m_KuruShake;
  55. protected float m_CamShakeX;
  56. protected float m_CamShakeY;
  57. protected vector m_SwayModifier = "1 1 1";//"max_speed_noise radius_noise overall_speed"
  58. protected int m_SwayState = -1;
  59. protected float m_StealthAimY_Last;
  60. protected float m_FilterVelocityStealthAimY[1] = {0};
  61. protected static float m_AimXClampRanges[] = { -180, -20, 90, 0, -50, 90, 180, -20, 90 };
  62. void DayZPlayerImplementAiming(DayZPlayerImplement player)
  63. {
  64. m_PlayerDpi = player;
  65. Class.CastTo(m_PlayerPb, player);
  66. UpdateSwayState(eSwayStates.DEFAULT);
  67. }
  68. void SetRecoil(Weapon_Base weapon)
  69. {
  70. if (m_ProceduralRecoilEnabled)
  71. {
  72. m_CurrentRecoil = weapon.SpawnRecoilObject();
  73. }
  74. }
  75. void RequestKuruShake(float amount)
  76. {
  77. if (!m_KuruShake)
  78. m_KuruShake = new KuruShake(m_PlayerPb, amount);
  79. }
  80. void OnRaiseBegin(DayZPlayerImplement player)
  81. {
  82. Weapon_Base weapon = Weapon_Base.Cast(player.GetHumanInventory().GetEntityInHands());
  83. if (weapon)
  84. {
  85. m_SwayModifier = weapon.GetPropertyModifierObject().m_SwayModifiers;
  86. }
  87. }
  88. void OnFinisherBegin(float currentAimY)
  89. {
  90. m_StealthAimY_Last = currentAimY;
  91. m_FilterVelocityStealthAimY[0] = 0;
  92. }
  93. void OnSwayStateChange(int state)
  94. {
  95. switch (state)
  96. {
  97. case eSwayStates.HOLDBREATH_EXHAUSTED:
  98. m_PlayerPb.OnHoldBreathExhausted();
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. float GetSwayWeight()
  105. {
  106. return m_SwayWeight;
  107. }
  108. void SetAimNoiseAllowed(bool state)
  109. {
  110. m_AimNoiseAllowed = state;
  111. }
  112. bool IsAimNoiseAllowed()
  113. {
  114. return m_AimNoiseAllowed;
  115. }
  116. void SetProceduralRecoilEnabled(bool state)
  117. {
  118. m_ProceduralRecoilEnabled = state;
  119. }
  120. bool IsProceduralRecoilEnabled()
  121. {
  122. return m_ProceduralRecoilEnabled;
  123. }
  124. void SetCamShakeValues(float x_axis, float y_axis)
  125. {
  126. m_CamShakeX = x_axis;
  127. m_CamShakeY = y_axis;
  128. }
  129. bool ProcessStealthFilters(float pDt, SDayZPlayerAimingModel pModel)
  130. {
  131. m_StealthAimY_Last = Math.SmoothCD(m_StealthAimY_Last, 0, m_FilterVelocityStealthAimY, 0.3, 1000, pDt);
  132. pModel.m_fAimYMouseShift = -(pModel.m_fCurrentAimY - m_StealthAimY_Last);
  133. return true;
  134. }
  135. bool ProcessAimFilters(float pDt, SDayZPlayerAimingModel pModel, int stance_index)
  136. {
  137. float breathing_offset_x;
  138. float breathing_offset_y;
  139. float noise_offset_x;
  140. float noise_offset_y;
  141. float shake_offset_x;
  142. float shake_offset_y;
  143. float recoil_offset_mouse_x;
  144. float recoil_offset_mouse_y;
  145. float recoil_offset_hands_x;
  146. float recoil_offset_hands_y;
  147. float kuru_offset_x;
  148. float kuru_offset_y;
  149. float player_stamina = m_PlayerPb.GetStaminaHandler().GetSyncedStaminaNormalized();
  150. #ifdef DEVELOPER
  151. DbgPrintAimingImplement("Player: " + m_PlayerPb + " | ProcessAimFilters | timestamp: " + m_PlayerPb.GetSimulationTimeStamp());
  152. #endif
  153. //negates stamina effect during hold breath
  154. if (m_PlayerPb.IsHoldingBreath())
  155. {
  156. player_stamina = 1;
  157. }
  158. float speed = CalculateSpeedMultiplier(player_stamina);
  159. m_TotalTime += pDt * speed;
  160. if (m_PlayerPb.IsHoldingBreath() && !m_HoldingBreathSet)
  161. {
  162. m_ReferenceTime = m_TotalTime;
  163. }
  164. else if (!m_PlayerPb.IsHoldingBreath() && m_HoldingBreathSet)
  165. {
  166. m_ReferenceTime = m_TotalTime;
  167. }
  168. float adjusted_sway_multiplier = CalculateSwayMultiplier();
  169. m_LastSwayMultiplier = adjusted_sway_multiplier;
  170. m_SwayWeight = CalculateWeight( stance_index, player_stamina, 0.5/*m_PlayerPb.m_CameraSwayModifier*/, m_PlayerPb.IsHoldingBreath()) * adjusted_sway_multiplier;
  171. //! get sway
  172. ApplyBreathingPattern(breathing_offset_x, breathing_offset_y, 3.0, m_TotalTime, m_SwayWeight);
  173. ApplyHorizontalNoise(noise_offset_x, noise_offset_y, 0.2, 0.5, 3.0 * m_SwayModifier[0], speed, 3 * m_SwayModifier[1], m_SwayWeight, pDt);
  174. int shake_level = m_PlayerPb.GetShakeLevel();
  175. if (shake_level != 0)
  176. {
  177. ApplyShakes(shake_offset_x, shake_offset_y, shake_level);
  178. }
  179. //! get recoil
  180. if (m_CurrentRecoil)
  181. {
  182. m_CurrentRecoil.Update(pModel, recoil_offset_mouse_x, recoil_offset_mouse_y, recoil_offset_hands_x, recoil_offset_hands_y, pDt);
  183. }
  184. if (m_KuruShake)
  185. {
  186. m_KuruShake.Update(pDt, kuru_offset_x, kuru_offset_y);
  187. }
  188. //! hands offset
  189. pModel.m_fAimXHandsOffset = breathing_offset_x + noise_offset_x + recoil_offset_hands_x + shake_offset_x + kuru_offset_x;
  190. pModel.m_fAimYHandsOffset = breathing_offset_y + noise_offset_y + recoil_offset_hands_y + shake_offset_y + kuru_offset_y;
  191. #ifdef DEVELOPER
  192. DbgPrintAimingImplement("breathing_offset_y: " + breathing_offset_y);
  193. DbgPrintAimingImplement("noise_offset_y: " + noise_offset_y);
  194. DbgPrintAimingImplement("recoil_offset_hands_y: " + recoil_offset_hands_y);
  195. DbgPrintAimingImplement("shake_offset_y: " + shake_offset_y);
  196. DbgPrintAimingImplement("kuru_offset_y: " + kuru_offset_y);
  197. DbgPrintAimingImplement("pModel.m_fAimYHandsOffset: " + pModel.m_fAimYHandsOffset);
  198. #endif
  199. //! cam offset
  200. pModel.m_fAimXCamOffset = -shake_offset_x - recoil_offset_hands_x - kuru_offset_x + m_CamShakeX;
  201. pModel.m_fAimYCamOffset = -shake_offset_y - recoil_offset_hands_y - kuru_offset_y + m_CamShakeY;
  202. #ifdef DEVELOPER
  203. DbgPrintAimingImplement("m_CamShakeY: " + m_CamShakeY);
  204. DbgPrintAimingImplement("pModel.m_fAimYCamOffset: " + pModel.m_fAimYCamOffset);
  205. #endif
  206. //! clamp aim ranges
  207. if (stance_index == DayZPlayerConstants.STANCEIDX_RAISEDPRONE)
  208. {
  209. float newVal = DayZPlayerUtils.LinearRangeClamp(pModel.m_fCurrentAimX, pModel.m_fCurrentAimY, m_AimXClampRanges);
  210. pModel.m_fAimYHandsOffset += newVal - pModel.m_fCurrentAimY;
  211. }
  212. float absAimY = Math.AbsFloat(pModel.m_fCurrentAimY);
  213. pModel.m_fAimYHandsOffset = Math.Clamp(pModel.m_fAimYHandsOffset,absAimY - 89.9,89.9 - absAimY); //'90' leads to rounding errors down the line
  214. if (m_PlayerDpi.IsInOptics() && m_KuruShake)
  215. {
  216. //TODO - do not offset mouse
  217. }
  218. //! mouse offset
  219. pModel.m_fAimXMouseShift = recoil_offset_mouse_x -kuru_offset_x / 10;
  220. pModel.m_fAimYMouseShift = recoil_offset_mouse_y + kuru_offset_y / 10;
  221. #ifdef DEVELOPER
  222. DbgPrintAimingImplement("recoil_offset_mouse_y: " + recoil_offset_mouse_y);
  223. DbgPrintAimingImplement("pModel.m_fAimYMouseShift: " + pModel.m_fAimYMouseShift);
  224. #endif
  225. if (m_PlayerPb.IsHoldingBreath() && !m_HoldingBreathSet)
  226. {
  227. m_HoldingBreathSet = true;
  228. m_HorizontalNoiseXAxisOffset = noise_offset_x;
  229. m_BreathingXAxisOffset = breathing_offset_x;
  230. m_BreathingYAxisOffset = breathing_offset_y;
  231. }
  232. else if (!m_PlayerPb.IsHoldingBreath() && m_HoldingBreathSet)
  233. {
  234. m_HoldingBreathSet = false;
  235. }
  236. if (!m_PlayerPb.IsHoldingBreath() && m_LastSwayMultiplier == PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT && m_HorizontalNoiseXAxisOffset != 0)
  237. {
  238. m_HorizontalNoiseXAxisOffset = 0;
  239. m_BreathingXAxisOffset = 0;
  240. m_BreathingYAxisOffset = 0;
  241. }
  242. if (m_PlayerPb.IsHoldingBreath())
  243. {
  244. m_PlayerPb.DepleteStamina(EStaminaModifiers.HOLD_BREATH,pDt*speed);
  245. }
  246. #ifdef DEVELOPER
  247. DbgPrintAimingImplement("----------------------------");
  248. #endif
  249. return true;
  250. }
  251. protected float CalculateSwayMultiplier()
  252. {
  253. float max;
  254. float time;
  255. float time_clamped;
  256. float ret;
  257. if (m_PlayerPb.IsHoldingBreath())
  258. {
  259. time = m_TotalTime - m_ReferenceTime;
  260. if (time < PlayerSwayConstants.SWAY_TIME_IN)
  261. {
  262. UpdateSwayState(eSwayStates.HOLDBREATH_IN);
  263. max = PlayerSwayConstants.SWAY_TIME_IN;
  264. time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
  265. ret = Math.Lerp(m_LastSwayMultiplier,PlayerSwayConstants.SWAY_MULTIPLIER_STABLE,time_clamped/max);
  266. }
  267. else if (time >= PlayerSwayConstants.SWAY_TIME_IN && time < (PlayerSwayConstants.SWAY_TIME_IN + PlayerSwayConstants.SWAY_TIME_STABLE))
  268. {
  269. UpdateSwayState(eSwayStates.HOLDBREATH_STABLE);
  270. ret = PlayerSwayConstants.SWAY_MULTIPLIER_STABLE;
  271. }
  272. else
  273. {
  274. UpdateSwayState(eSwayStates.HOLDBREATH_EXHAUSTED);
  275. max = PlayerSwayConstants.SWAY_TIME_EXHAUSTED;
  276. time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
  277. ret = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_STABLE,PlayerSwayConstants.SWAY_MULTIPLIER_EXHAUSTED,(time_clamped/max));
  278. }
  279. }
  280. else
  281. {
  282. UpdateSwayState(eSwayStates.DEFAULT);
  283. max = PlayerSwayConstants.SWAY_TIME_OUT;
  284. time_clamped = Math.Clamp((m_TotalTime - m_SwayStateStartTime),0,max);
  285. ret = Math.Lerp(m_LastSwayMultiplier,1,time_clamped/max);
  286. }
  287. return ret;
  288. }
  289. float CalculateSpeedMultiplier(float stamina)
  290. {
  291. return (((1.0 - stamina) * 3.0) + 1.0) * m_SwayModifier[2]; // just 'm_SwayModifier[2]' for HoldBreath
  292. }
  293. protected bool UpdateSwayState(int state)
  294. {
  295. if (state != m_SwayState)
  296. {
  297. m_SwayState = state;
  298. m_SwayStateStartTime = m_TotalTime;
  299. m_StateStartSwayMultiplier = m_LastSwayMultiplier;
  300. OnSwayStateChange(state);
  301. return true;
  302. }
  303. return false;
  304. }
  305. int GetCurrentSwayState()
  306. {
  307. return m_SwayState;
  308. }
  309. protected void ApplyBreathingPattern(out float x_axis, out float y_axis, float pAmplitude, float pTotalTime, float weight)
  310. {
  311. float multiplier = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT,0,m_LastSwayMultiplier); //TODO revise
  312. #ifdef DEVELOPER
  313. DbgPrintAimingImplement("m_LastSwayMultiplier: " + m_LastSwayMultiplier);
  314. DbgPrintAimingImplement("pAmplitude: " + pAmplitude);
  315. DbgPrintAimingImplement("pTotalTime: " + pTotalTime);
  316. DbgPrintAimingImplement("weight: " + weight);
  317. DbgPrintAimingImplement("multiplier: " + multiplier);
  318. #endif
  319. x_axis = (Math.Sin(pTotalTime) * pAmplitude / 4) * weight;
  320. y_axis = (Math.Sin((pTotalTime) * 0.8 + 0.6) * pAmplitude) * weight;
  321. #ifdef DEVELOPER
  322. DbgPrintAimingImplement("y_axis_midproduct: " + y_axis);
  323. #endif
  324. x_axis += m_BreathingXAxisOffset * multiplier;
  325. y_axis += m_BreathingYAxisOffset * multiplier;
  326. }
  327. protected void ApplyHorizontalNoise(out float x_axis, out float y_axis, float smooth_time,float max_velocity_low, float max_velocity_high, float velocity_modifier, float max_distance, float weight, float pDt)
  328. {
  329. if (Math.AbsFloat(m_HorizontalTargetValue - m_HorizontalNoise) < 0.01)
  330. {
  331. //acquire new target
  332. m_MaxVelocity = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, max_velocity_low, max_velocity_high);
  333. float r = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
  334. m_HorizontalTargetValue = (r - 0.5) * 2 * max_distance;
  335. m_HorizontalNoiseVelocity[0] = 0;
  336. }
  337. m_HorizontalNoise = Math.SmoothCD(m_HorizontalNoise, m_HorizontalTargetValue, m_HorizontalNoiseVelocity, smooth_time, m_MaxVelocity * velocity_modifier, pDt);
  338. x_axis = m_HorizontalNoise * weight;
  339. float multiplier = Math.Lerp(PlayerSwayConstants.SWAY_MULTIPLIER_DEFAULT,0,m_LastSwayMultiplier); //TODO revise
  340. x_axis += m_HorizontalNoiseXAxisOffset * multiplier;
  341. }
  342. protected void ApplyShakes(out float x_axis, out float y_axis, int level)
  343. {
  344. float weight = level / PlayerBase.SHAKE_LEVEL_MAX;
  345. m_ShakeCount++;
  346. int shakes_threshold = Math.Round(m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 2, 4));
  347. if (m_ShakeCount > shakes_threshold)
  348. {
  349. m_ShakeCount = 0;
  350. float modifier = m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0.45, 0.9);
  351. x_axis = modifier * weight * m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
  352. y_axis = modifier * weight * m_PlayerPb.GetRandomGeneratorSyncManager().GetRandomInRange(RandomGeneratorSyncUsage.RGSAimingModel, 0, 1);
  353. }
  354. }
  355. protected float CalculateWeight(int stance_index, float current_stamina, float camera_sway_modifier, bool holding_breath)
  356. {
  357. if (m_PlayerDpi.GetCommand_Move() && m_PlayerDpi.GetCommand_Move().IsInRoll())//when the player is rolling, set a constant and disregard everything else
  358. {
  359. return PlayerSwayConstants.SWAY_ROLL;
  360. }
  361. float stance_modifier;
  362. switch (stance_index)
  363. {
  364. case DayZPlayerConstants.STANCEIDX_RAISEDERECT:
  365. stance_modifier = 0.5;
  366. break;
  367. case DayZPlayerConstants.STANCEIDX_RAISEDCROUCH:
  368. stance_modifier = 0.75;
  369. break;
  370. case DayZPlayerConstants.STANCEIDX_RAISEDPRONE:
  371. stance_modifier = 0.9;
  372. break;
  373. default:
  374. stance_modifier = 0.75;
  375. //Debug.LogError("stance mask out of definition");
  376. break;
  377. }
  378. #ifdef DEVELOPER
  379. DbgPrintAimingImplement("current_stamina: " + current_stamina);
  380. DbgPrintAimingImplement("camera_sway_modifier: " + camera_sway_modifier);
  381. DbgPrintAimingImplement("holding_breath: " + holding_breath);
  382. #endif
  383. return (1 - stance_modifier) * m_AimNoiseAllowed * camera_sway_modifier * SWAY_WEIGHT_SCALER;
  384. }
  385. void DbgPrintAimingImplement(string val)
  386. {
  387. #ifdef DEVELOPER
  388. if (GetDayZGame().IsAimLogEnabled())
  389. Print("DayZPlayerImplementAiming | " + val);
  390. #endif
  391. }
  392. }