File size: 6,173 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
class InjuryHandlerThresholds
{
	const float WORN = 0.5;
	const float DAMAGED = 0.3;
	const float BADLY_DAMAGED = 0.2;
	const float RUINED = 0.1;
};

class InjuryAnimValues
{
	const float LVL0 = 0;
	const float LVL1 = 0.3;
	const float LVL2 = 0.6;
	const float LVL3 = 0.8;
	const float LVL4 = 1;
};

enum eInjuryHandlerLevels
{
	PRISTINE,
	WORN,
	DAMAGED,
	BADLY_DAMAGED,
	RUINED,
}

enum eInjuryOverrides
{
	//! MUST BE POW2
	NONE = 0,
	MORPHINE = 1,
	PAIN_KILLERS_LVL0 = 2,
	PAIN_KILLERS_LVL1 = 4,
	BROKEN_LEGS = 8, //New
	BROKEN_LEGS_SPLINT = 16, //New
	PRONE_ANIM_OVERRIDE = 32, //Prevent non pristine animation when prone
}

class InjuryAnimationHandler
{

	const float VALUE_CHECK_INTERVAL	 	= 5;
	const float SENSITIVTY_PERCENTAGE 		= 1; //how much the value needs to change up/down from previous update to trigger a new update(in percent)

	ref ScriptInvoker m_ChangedStateInvoker	= new ScriptInvoker();
	
	float m_TimeSinceLastTick = VALUE_CHECK_INTERVAL + 1;
	float m_LastUpdate;
	eInjuryHandlerLevels m_LastHealthUpdate;
	float m_HealthMaxValue;
	
	
	private PlayerBase m_Player; //! owner
	private bool m_AnimationChange = false;
	private bool m_InjuryAnimEnabled = false;
	private float m_InjuryAnimDamageValue = 0;
	int m_ForceInjuryAnimMask;
	
	void InjuryAnimationHandler(PlayerBase player)
	{
		m_Player = player;
		m_HealthMaxValue = m_Player.GetMaxHealth("", "Health");
	}
	
	bool IsInjuryAnimEnabled()
	{
		return m_InjuryAnimEnabled;
	}
	
	float GetInjuryAnimValue()
	{
		return m_InjuryAnimDamageValue;
	}
	
	ScriptInvoker GetInvoker()
	{
		return m_ChangedStateInvoker;
	}
	
	void Update(float deltaT)
	{
		if ( m_AnimationChange )
		{
			m_Player.GetCommandModifier_Additives().SetInjured(m_InjuryAnimDamageValue, m_InjuryAnimEnabled);
			m_AnimationChange = false;
		}

		if ( GetGame().IsClient() ) 
			return;
		
		m_TimeSinceLastTick += deltaT;

		if ( m_TimeSinceLastTick > VALUE_CHECK_INTERVAL )
		{
			CheckValue();
			m_TimeSinceLastTick = 0;
		}
	}

	eInjuryHandlerLevels GetOverrideLevel(eInjuryHandlerLevels unchanged_level)
	{
		eInjuryHandlerLevels override_level = unchanged_level;
		
		//Whatever the case, prone will always use PRISTINE anim level as alternative anim level(s) are not interesting gameplaywise
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.PRONE_ANIM_OVERRIDE && override_level > eInjuryHandlerLevels.PRISTINE )
		{
			override_level = eInjuryHandlerLevels.PRISTINE;
			return override_level;
		}
		
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.MORPHINE && override_level > eInjuryHandlerLevels.PRISTINE )
		{
			override_level = eInjuryHandlerLevels.PRISTINE;
		}
		
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.PAIN_KILLERS_LVL1 && override_level > eInjuryHandlerLevels.WORN )
		{
			override_level = eInjuryHandlerLevels.WORN;
		}
		
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.PAIN_KILLERS_LVL0 && override_level > eInjuryHandlerLevels.DAMAGED )
		{
			override_level = eInjuryHandlerLevels.DAMAGED;
		}
		
		if ( m_ForceInjuryAnimMask && override_level > eInjuryHandlerLevels.DAMAGED )
		{
			override_level = eInjuryHandlerLevels.RUINED;
		}
		
		//Broken leg overrides
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.BROKEN_LEGS_SPLINT && override_level < eInjuryHandlerLevels.DAMAGED )
		{
			override_level = eInjuryHandlerLevels.DAMAGED;
		}
		
		if ( m_ForceInjuryAnimMask & eInjuryOverrides.BROKEN_LEGS && override_level < eInjuryHandlerLevels.RUINED )
		{
			//Average leg health used to determine which animation to use
			float avgLegHealth = m_Player.GetHealth("RightLeg","") + m_Player.GetHealth("LeftLeg","") + m_Player.GetHealth("RightFoot","") + m_Player.GetHealth("LeftFoot","");
			avgLegHealth *= 0.25; //divide by 4 to make the average leg health;
			if (avgLegHealth <= PlayerConstants.BROKEN_LEGS_LOW_HEALTH_THRESHOLD)
				override_level = eInjuryHandlerLevels.RUINED;
			else if (avgLegHealth >= PlayerConstants.BROKEN_LEGS_HIGH_HEALTH_THRESHOLD)
				override_level = eInjuryHandlerLevels.WORN;
			else
				override_level = eInjuryHandlerLevels.DAMAGED;
		}
		
		return override_level;
	
	}
	
	void CheckValue(bool forceUpdate = false)
	{
		float health_current_normalized = m_Player.GetHealth("","Health") / m_HealthMaxValue;
		eInjuryHandlerLevels injury_level = GetInjuryLevel(health_current_normalized);
		
		if ( m_ForceInjuryAnimMask )
		{
			injury_level = GetOverrideLevel(injury_level);
		}
		
		if ( m_LastHealthUpdate != injury_level || forceUpdate )
		{
			SendValue(injury_level);
			m_ChangedStateInvoker.Invoke(injury_level);
			m_LastHealthUpdate = injury_level;
			Synchronize(injury_level);
		}
	}
	
	void Synchronize(eInjuryHandlerLevels level)
	{
		m_Player.m_HealthLevel = level;
		//Print("m_Player.m_HealthLevel:" + m_Player.m_HealthLevel);
		m_Player.SetSynchDirty();
	}

	void SendValue(eInjuryHandlerLevels level)
	{
		DayZPlayerSyncJunctures.SendInjury(m_Player, true, level);
	}
	
	void SetInjuryCommandParams(bool enable, eInjuryHandlerLevels level)
	{
		m_AnimationChange = true;
		m_InjuryAnimEnabled = enable;
		m_InjuryAnimDamageValue = GetInjuryValue(level);
	}
	
	eInjuryHandlerLevels GetInjuryLevel(float health)
	{
		if ( health  < InjuryHandlerThresholds.RUINED ) 
		{
			return eInjuryHandlerLevels.RUINED;
		}
		
		if ( health  < InjuryHandlerThresholds.BADLY_DAMAGED ) 
		{
			return eInjuryHandlerLevels.BADLY_DAMAGED;
		}
		
		if ( health  < InjuryHandlerThresholds.DAMAGED ) 			
		{
			return eInjuryHandlerLevels.DAMAGED;
		}
		
		if ( health  < InjuryHandlerThresholds.WORN ) 		
		{
			return eInjuryHandlerLevels.WORN;
		}
		
		return eInjuryHandlerLevels.PRISTINE;
	}
	
	
	float GetInjuryValue(eInjuryHandlerLevels level)
	{
		switch ( level )
		{
			case eInjuryHandlerLevels.RUINED:
			{
				return InjuryAnimValues.LVL4;
			}
			case eInjuryHandlerLevels.BADLY_DAMAGED:
			{
				return InjuryAnimValues.LVL3;
			}
			case eInjuryHandlerLevels.DAMAGED:
			{
				return InjuryAnimValues.LVL2;
			}
			case eInjuryHandlerLevels.WORN:
			{
				return InjuryAnimValues.LVL1;
			}
			case eInjuryHandlerLevels.PRISTINE:
			{
				return InjuryAnimValues.LVL0;
			}
			default:
				Error("Undefined Injury level");
		}
		return 0;
	}
}