bleedingsourcesmanagerserver.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. class BleedingSourcesManagerServer extends BleedingSourcesManagerBase
  2. {
  3. const float TICK_INTERVAL_SEC = 3;
  4. float m_Tick;
  5. bool m_DisableBloodLoss = false;
  6. ref array<int> m_DeleteList = new array<int>;
  7. const int STORAGE_VERSION = 103;
  8. protected BleedingSourceZone GetBleedingSourceZone(int bit)
  9. {
  10. return m_BleedingSourceZone.Get(GetSelectionNameFromBit(bit));
  11. }
  12. int GetStorageVersion()
  13. {
  14. return STORAGE_VERSION;
  15. }
  16. void RequestDeletion(int bit)
  17. {
  18. m_DeleteList.Insert(bit);
  19. }
  20. override protected void AddBleedingSource(int bit)
  21. {
  22. #ifdef DEVELOPER
  23. if (m_Player && !m_Player.GetAllowDamage())//full invincibility prevents bleeding source creation
  24. return;
  25. #endif
  26. m_Player.SetBleedingBits(m_Player.GetBleedingBits() | bit );
  27. super.AddBleedingSource(bit);
  28. m_Player.OnBleedingSourceAdded();
  29. }
  30. override protected bool RemoveBleedingSource(int bit)
  31. {
  32. if(!super.RemoveBleedingSource(bit))
  33. {
  34. Error("Failed to remove bleeding source:" + bit);
  35. }
  36. else
  37. {
  38. m_Player.OnBleedingSourceRemovedEx(m_Item);
  39. }
  40. int inverse_bit_mask = ~bit;
  41. m_Player.SetBleedingBits(m_Player.GetBleedingBits() & inverse_bit_mask );
  42. //infection moved here to allow proper working in singleplayer
  43. float chanceToInfect;
  44. if (m_Item)
  45. {
  46. chanceToInfect = m_Item.GetInfectionChance(0, CachedObjectsParams.PARAM1_BOOL);
  47. }
  48. else
  49. {
  50. chanceToInfect = PlayerConstants.BLEEDING_SOURCE_CLOSE_INFECTION_CHANCE;
  51. }
  52. float diceRoll = Math.RandomFloat01();
  53. if (diceRoll < chanceToInfect)
  54. {
  55. m_Player.InsertAgent(eAgents.WOUND_AGENT);
  56. }
  57. m_Item = null;//reset, so that next call, if induced by self-healing, will have no item
  58. return true;
  59. }
  60. void RemoveAnyBleedingSource()
  61. {
  62. int bleeding_sources_bits = m_Player.GetBleedingBits();
  63. int rightmost_bit = bleeding_sources_bits & (-bleeding_sources_bits);
  64. RemoveBleedingSource(rightmost_bit);
  65. }
  66. void RemoveMostSignificantBleedingSource()
  67. {
  68. int bit = GetMostSignificantBleedingSource();
  69. if( bit != 0)
  70. RemoveBleedingSource(bit);
  71. }
  72. void RemoveMostSignificantBleedingSourceEx(ItemBase item)
  73. {
  74. SetItem(item);
  75. RemoveMostSignificantBleedingSource();
  76. }
  77. int GetMostSignificantBleedingSource()
  78. {
  79. int bleeding_sources_bits = m_Player.GetBleedingBits();
  80. float highest_flow;
  81. int highest_flow_bit;
  82. int bit_offset;
  83. for(int i = 0; i < BIT_INT_SIZE; i++)
  84. {
  85. int bit = 1 << bit_offset;
  86. if( (bit & bleeding_sources_bits) != 0 )
  87. {
  88. BleedingSourceZone meta = GetBleedingSourceMeta(bit);
  89. if(meta)
  90. {
  91. if( meta.GetFlowModifier() > highest_flow )
  92. {
  93. highest_flow = meta.GetFlowModifier();
  94. highest_flow_bit = bit;
  95. //Print(meta.GetSelectionName());
  96. }
  97. }
  98. }
  99. bit_offset++;
  100. }
  101. return highest_flow_bit;
  102. }
  103. void OnTick(float delta_time)
  104. {
  105. m_Tick += delta_time;
  106. if( m_Tick > TICK_INTERVAL_SEC )
  107. {
  108. while( m_DeleteList.Count() > 0 )
  109. {
  110. RemoveBleedingSource(m_DeleteList.Get(0));
  111. m_DeleteList.Remove(0);
  112. }
  113. float blood_scale = Math.InverseLerp(PlayerConstants.BLOOD_THRESHOLD_FATAL, PlayerConstants.BLEEDING_LOW_PRESSURE_BLOOD, m_Player.GetHealth( "GlobalHealth", "Blood" ));
  114. blood_scale = Math.Clamp( blood_scale, PlayerConstants.BLEEDING_LOW_PRESSURE_MIN_MOD, 1 );
  115. for(int i = 0; i < m_BleedingSources.Count(); i++)
  116. {
  117. m_BleedingSources.GetElement(i).OnUpdateServer( m_Tick, blood_scale, m_DisableBloodLoss );
  118. }
  119. m_Tick = 0;
  120. }
  121. }
  122. void ActivateAllBS()
  123. {
  124. for(int i = 0; i < m_BleedingSourceZone.Count(); i++)
  125. {
  126. int bit = m_BleedingSourceZone.GetElement(i).GetBit();
  127. if( CanAddBleedingSource(bit) )
  128. {
  129. AddBleedingSource(bit);
  130. }
  131. }
  132. }
  133. //damage must be to "Blood" healthType
  134. void ProcessHit(float damage, EntityAI source, int component, string zone, string ammo, vector modelPos)
  135. {
  136. float dmg_max = m_Player.GetMaxHealth(zone, "Blood");
  137. float bleed_threshold = GetGame().ConfigGetFloat("CfgAmmo " + ammo + " DamageApplied bleedThreshold");
  138. string damageTypeString = GetGame().ConfigGetTextOut("CfgAmmo " + ammo + " DamageApplied type");
  139. bleed_threshold = Math.Clamp(bleed_threshold,0,1);
  140. float bleedingChance;
  141. bool createBleedingSource = false;
  142. // 'true' only when the damageTypeString is handled there
  143. if (BleedChanceData.CalculateBleedChance(damageTypeString, damage, bleed_threshold,bleedingChance))
  144. {
  145. float roll = Math.RandomFloat01();
  146. createBleedingSource = bleedingChance != 0 && bleedingChance >= roll;
  147. #ifdef ENABLE_LOGGING
  148. if (LogManager.IsBleedingChancesLogEnable())
  149. {
  150. Debug.BleedingChancesLog(roll.ToString(), "BleedingSourcesManagerServer" , "n/a", "bleeding random roll:");
  151. }
  152. #endif
  153. }
  154. else if (source && source.IsZombie())
  155. {
  156. int chance = Math.RandomInt(0,100);
  157. if (chance <= damage)
  158. {
  159. createBleedingSource = true;
  160. }
  161. }
  162. else if (damage > (dmg_max * (1 - bleed_threshold)) )
  163. {
  164. createBleedingSource = true;
  165. }
  166. if (createBleedingSource)
  167. {
  168. #ifdef ENABLE_LOGGING
  169. if (LogManager.IsBleedingChancesLogEnable())
  170. {
  171. Debug.BleedingChancesLog("true", "BleedingSourcesManagerServer" , "n/a", "Attempting to create bleeding source");
  172. }
  173. #endif
  174. AttemptAddBleedingSource(component);
  175. }
  176. }
  177. void DebugActivateBleedingSource(int source)
  178. {
  179. RemoveAllSources();
  180. if(source >= m_BleedingSourceZone.Count() || !m_BleedingSourceZone.GetElement(source)) return;
  181. int bit = m_BleedingSourceZone.GetElement(source).GetBit();
  182. if( bit && CanAddBleedingSource(bit) )
  183. {
  184. AddBleedingSource(bit);
  185. }
  186. }
  187. void SetBloodLoss(bool status)
  188. {
  189. m_DisableBloodLoss = status;
  190. }
  191. void OnStoreSave( ParamsWriteContext ctx )
  192. {
  193. //int count = m_BleedingSources.Count();
  194. int active_bits = m_Player.GetBleedingBits();
  195. ctx.Write(active_bits);
  196. int bit_offset = 0;
  197. for(int i = 0; i < BIT_INT_SIZE; i++)
  198. {
  199. int bit = 1 << bit_offset;
  200. if( (bit & active_bits) != 0 )
  201. {
  202. int active_time = GetBleedingSourceActiveTime(bit);
  203. eBleedingSourceType type = GetBleedingSourceType(bit);
  204. ctx.Write(active_time);
  205. ctx.Write(type);
  206. }
  207. bit_offset++;
  208. }
  209. }
  210. bool OnStoreLoad( ParamsReadContext ctx, int version )
  211. {
  212. int active_bits;
  213. if(!ctx.Read(active_bits))
  214. {
  215. return false;
  216. }
  217. int bit_offset = 0;
  218. for(int i = 0; i < BIT_INT_SIZE; i++)
  219. {
  220. int bit = 1 << bit_offset;
  221. if( (bit & active_bits) != 0 && CanAddBleedingSource(bit))
  222. {
  223. AddBleedingSource(bit);
  224. int active_time = 0;
  225. if(!ctx.Read(active_time))
  226. {
  227. return false;
  228. }
  229. else
  230. {
  231. SetBleedingSourceActiveTime(bit,active_time);
  232. }
  233. if( version >= 121 )//type was added in this version
  234. {
  235. eBleedingSourceType type = eBleedingSourceType.NORMAL;
  236. if (!ctx.Read(type))
  237. {
  238. return false;
  239. }
  240. else
  241. {
  242. SetBleedingSourceType(bit,type);
  243. }
  244. }
  245. }
  246. bit_offset++;
  247. }
  248. return true;
  249. }
  250. void ~BleedingSourcesManagerServer()
  251. {
  252. if (m_Player && !m_Player.IsAlive())
  253. RemoveAllSources();
  254. }
  255. }