File size: 7,040 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
class BleedingSourcesManagerServer extends BleedingSourcesManagerBase
{
	const float TICK_INTERVAL_SEC = 3;
	float 	m_Tick;
	bool	m_DisableBloodLoss = false;
	ref array<int> m_DeleteList = new array<int>;
	
	const int STORAGE_VERSION = 103;
	
	protected BleedingSourceZone GetBleedingSourceZone(int bit)
	{
		return m_BleedingSourceZone.Get(GetSelectionNameFromBit(bit));
	}
	
	int GetStorageVersion()
	{
		return STORAGE_VERSION;
	}
	
	void RequestDeletion(int bit)
	{
		m_DeleteList.Insert(bit);
	}
	
	override protected void AddBleedingSource(int bit)
	{
		#ifdef DEVELOPER
		if (m_Player && !m_Player.GetAllowDamage())//full invincibility prevents bleeding source creation
			return;
		#endif
		
		m_Player.SetBleedingBits(m_Player.GetBleedingBits() | bit );
		super.AddBleedingSource(bit);
		m_Player.OnBleedingSourceAdded();
	}
	
	override protected bool RemoveBleedingSource(int bit)
	{
		if(!super.RemoveBleedingSource(bit))
		{
			Error("Failed to remove bleeding source:" + bit);
		}
		else
		{
			m_Player.OnBleedingSourceRemovedEx(m_Item);
		}
		
		int inverse_bit_mask = ~bit;
		m_Player.SetBleedingBits(m_Player.GetBleedingBits() & inverse_bit_mask );
		
		
		//infection moved here to allow proper working in singleplayer
		float chanceToInfect;
		
		if (m_Item)
		{
			chanceToInfect = m_Item.GetInfectionChance(0, CachedObjectsParams.PARAM1_BOOL);
		}
		else
		{
			chanceToInfect = PlayerConstants.BLEEDING_SOURCE_CLOSE_INFECTION_CHANCE;
		}
		float diceRoll = Math.RandomFloat01();
		if (diceRoll < chanceToInfect)
		{
			m_Player.InsertAgent(eAgents.WOUND_AGENT);
		}
		
		m_Item = null;//reset, so that next call, if induced by self-healing, will have no item
		
		return true;
	}
	
	void RemoveAnyBleedingSource()
	{
		int bleeding_sources_bits = m_Player.GetBleedingBits();
		int rightmost_bit = bleeding_sources_bits & (-bleeding_sources_bits);
		
		RemoveBleedingSource(rightmost_bit);
	}
	
	void RemoveMostSignificantBleedingSource()
	{
		int bit = GetMostSignificantBleedingSource();
		if( bit != 0)
			RemoveBleedingSource(bit);
	}
	
	void RemoveMostSignificantBleedingSourceEx(ItemBase item)
	{
		SetItem(item);
		RemoveMostSignificantBleedingSource();
	}
	
	int GetMostSignificantBleedingSource()
	{
		int bleeding_sources_bits = m_Player.GetBleedingBits();
		
		float highest_flow;
		int highest_flow_bit;
		int bit_offset;
		
		for(int i = 0; i < BIT_INT_SIZE; i++)
		{
			int bit = 1 << bit_offset;
			
			if( (bit & bleeding_sources_bits) != 0 )
			{
				BleedingSourceZone meta = GetBleedingSourceMeta(bit);
				if(meta)
				{
					if( meta.GetFlowModifier() > highest_flow )
					{
						highest_flow = meta.GetFlowModifier();
						highest_flow_bit = bit;
						//Print(meta.GetSelectionName());
					}
				}
			}
			bit_offset++;
		}
		return highest_flow_bit;
	}
	
	void OnTick(float delta_time)
	{
		m_Tick += delta_time;
		if( m_Tick > TICK_INTERVAL_SEC )
		{
			while( m_DeleteList.Count() > 0 )
			{
				RemoveBleedingSource(m_DeleteList.Get(0));
				m_DeleteList.Remove(0);
			} 
			
			float blood_scale = Math.InverseLerp(PlayerConstants.BLOOD_THRESHOLD_FATAL, PlayerConstants.BLEEDING_LOW_PRESSURE_BLOOD, m_Player.GetHealth( "GlobalHealth", "Blood" ));
			blood_scale = Math.Clamp( blood_scale, PlayerConstants.BLEEDING_LOW_PRESSURE_MIN_MOD, 1 );

			for(int i = 0; i < m_BleedingSources.Count(); i++)
			{
				m_BleedingSources.GetElement(i).OnUpdateServer( m_Tick, blood_scale, m_DisableBloodLoss );
			}
			m_Tick = 0;
		}
	}
	
	void ActivateAllBS()
	{
		for(int i = 0; i < m_BleedingSourceZone.Count(); i++)
		{
			int bit = m_BleedingSourceZone.GetElement(i).GetBit();
			if( CanAddBleedingSource(bit) )
			{
				AddBleedingSource(bit);
			}
		}
	}
	
	//damage must be to "Blood" healthType
	void ProcessHit(float damage, EntityAI source, int component, string zone, string ammo, vector modelPos)
	{
		float dmg_max = m_Player.GetMaxHealth(zone, "Blood");
		float bleed_threshold = GetGame().ConfigGetFloat("CfgAmmo " + ammo + " DamageApplied bleedThreshold");
		string damageTypeString = GetGame().ConfigGetTextOut("CfgAmmo " + ammo + " DamageApplied type");
		bleed_threshold = Math.Clamp(bleed_threshold,0,1);
		float bleedingChance;
		bool createBleedingSource = false;
		
		// 'true' only when the damageTypeString is handled there
		if (BleedChanceData.CalculateBleedChance(damageTypeString, damage, bleed_threshold,bleedingChance))
		{
			float roll = Math.RandomFloat01();
			createBleedingSource = bleedingChance != 0 && bleedingChance >= roll;
			
			#ifdef DEVELOPER
			if (LogManager.IsBleedingChancesLogEnable())
			{	
				Debug.BleedingChancesLog(roll.ToString(), "BleedingSourcesManagerServer" , "n/a", "bleeding random roll:");
			}
			#endif
		}
		else if (source && source.IsZombie())
		{
			int chance = Math.RandomInt(0,100);
			if (chance <= damage)
			{
				createBleedingSource = true;
			}
		}
		else if (damage > (dmg_max * (1 - bleed_threshold)) )
		{
			createBleedingSource = true;
		}
		
		if (createBleedingSource)
		{
			#ifdef DEVELOPER
			if (LogManager.IsBleedingChancesLogEnable())
			{	
				Debug.BleedingChancesLog("true", "BleedingSourcesManagerServer" , "n/a", "Attempting to create bleeding source");
			}
			#endif
			AttemptAddBleedingSource(component);
		}
	}
	
	void DebugActivateBleedingSource(int source)
	{
		RemoveAllSources();
		
		if(source >= m_BleedingSourceZone.Count() || !m_BleedingSourceZone.GetElement(source)) return;
		
		int bit = m_BleedingSourceZone.GetElement(source).GetBit();
		
		if( bit && CanAddBleedingSource(bit) )
		{
			AddBleedingSource(bit);
		}
	}
	
	void SetBloodLoss(bool status)
	{
		m_DisableBloodLoss = status;
	}
	
	void OnStoreSave( ParamsWriteContext ctx )
	{
		//int count = m_BleedingSources.Count();
		int active_bits = m_Player.GetBleedingBits();
		ctx.Write(active_bits);
		
		int bit_offset = 0;
		for(int i = 0; i < BIT_INT_SIZE; i++)
		{
			int bit = 1 << bit_offset;
			if( (bit & active_bits) != 0 )
			{
				int active_time = GetBleedingSourceActiveTime(bit);
				eBleedingSourceType type = GetBleedingSourceType(bit);
				
				ctx.Write(active_time);
				ctx.Write(type);
			}
			bit_offset++;
		}
	}

	bool OnStoreLoad( ParamsReadContext ctx, int version )
	{
		int active_bits;
		if(!ctx.Read(active_bits))
		{
			return false;
		}
	
		int bit_offset = 0;
		for(int i = 0; i < BIT_INT_SIZE; i++)
		{
			int bit = 1 << bit_offset;
			if( (bit & active_bits) != 0 && CanAddBleedingSource(bit))
			{
				AddBleedingSource(bit);
				int active_time = 0;
				if(!ctx.Read(active_time))
				{
					return false;
				}
				else
				{
					SetBleedingSourceActiveTime(bit,active_time);
				}
				if( version >= 121 )//type was added in this version
				{
					eBleedingSourceType type = eBleedingSourceType.NORMAL;
					if (!ctx.Read(type))
					{
						return false;
					}
					else
					{
						SetBleedingSourceType(bit,type);
					}
				}
			}
			bit_offset++;
		}
		return true;
	}
	
	
	void ~BleedingSourcesManagerServer()
	{
		if (m_Player && !m_Player.IsAlive())
			RemoveAllSources();
	}
}