Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
transfervalues.c
Go to the documentation of this file.
1 class TransferValues extends Managed
2 {
3  const int TYPE_HEALTH = 1;
4  const int TYPE_BLOOD = 2;
5 
6  const float VALUE_CHECK_INTERVAL = 5;
7  const float SENSITIVTY_PERCENTAGE = 1;//how much the value needs to change up/down from previous update to trigger a new update(in percent)
8 
9  const int BLOOD_THRESHOLD_LOW = 3000;
12 
13  float m_LastHealthUpdate;
14  float m_LastBloodUpdate;
15 
16  //float m_CumulatedHealthDiff;
17  //float m_CumulatedBloodDiff;
18 
19  float m_HealthMaxValue;
20  float m_BloodMaxValue;
21 
22  float m_BloodClient;
23  float m_HealthClient;
24 
25  protected bool m_InitialSyncSent;
26 
27  void TransferValues(PlayerBase player)
28  {
29  m_Player = player;
30  m_InitialSyncSent = false;
31  Init();
32  }
33 
34  void Init()
35  {
37  m_LastBloodUpdate = 0;
38  m_HealthMaxValue = m_Player.GetMaxHealth("", "Health");
39  m_BloodMaxValue = m_Player.GetMaxHealth("", "Blood");
40  m_BloodClient = 0;
41  m_HealthClient = 0;
42  }
43 
44  void OnScheduledTick(float deltatime)
45  {
46  #ifdef DIAG_DEVELOPER
47  #ifndef SERVER
48  ShowDebugValues(DiagMenu.GetBool(DiagMenuIDs.TRANSFER_VALUES_SHOW));
49  #endif
50  #endif
51 
52  if ( GetGame().IsClient() ) return;
53 
54  m_TimeSinceLastTick += deltatime;
55 
56  if (!m_InitialSyncSent)
57  {
58  SendInitValues();
59  }
61  {
62  /*
63  Print(m_TimeSinceLastTick.ToString());
64  Print(VALUE_CHECK_INTERVAL.ToString());
65  Print("--------------");
66  */
68  CheckValues();
69 
70  // send sync junctures if necessary
71  // TODO: !!!! event is sent too often, please fix it
72  /*
73  float damage = 1 - m_Player.GetHealth("", "") / 100;
74  DayZPlayerSyncJunctures.SendInjury(m_Player, true, damage);
75  */
76  }
77  }
78 
79  void CheckValues()
80  {
81  CheckHealth();
82  CheckBlood();
83  }
84 
85  float GetBlood()
86  {
87  return m_BloodClient;
88 
89  }
90 
91  float GetHealth()
92  {
93  return m_HealthClient;
94  }
95 
96  void CheckHealth()
97  {
98  float health_current = m_Player.GetHealth("","Health");
99  float health_normalized = health_current / m_HealthMaxValue;
100  float difference_normalized = health_normalized - m_LastHealthUpdate;
101  float diff_abs = Math.AbsFloat(difference_normalized);
102 
103  if ( diff_abs > ( SENSITIVTY_PERCENTAGE / 100 ) )
104  {
105  SendValue(TYPE_HEALTH, health_normalized);
106  m_LastHealthUpdate = health_normalized;
107  }
108  }
109 
110  /*void CheckBlood()
111  {
112  float blood_current = m_Player.GetHealth("","Blood");
113  //float blood_normalized = blood_current / m_BloodMaxValue;
114  float blood_normalized = Math.InverseLerp(BLOOD_THRESHOLD_LOW, m_BloodMaxValue, blood_current);
115  blood_normalized = Math.Clamp(blood_normalized,0,1);
116  float difference_normalized = blood_normalized - m_LastBloodUpdate;
117  float diff_abs = Math.AbsFloat(difference_normalized);
118  m_CumulatedBloodDiff += diff_abs;
119 
120  //Print("DbgBlood | CheckBlood | m_CumulatedBloodDiff: " + m_CumulatedBloodDiff);
121  if( m_CumulatedBloodDiff > ( SENSITIVTY_PERCENTAGE /100 ) )
122  {
123  SendValue(TYPE_BLOOD, blood_normalized);
124  m_LastBloodUpdate = blood_normalized;
125  //Print("DbgBlood | CheckBlood | Sending blood: " + blood_normalized);
126  m_CumulatedBloodDiff = 0;
127  }
128  else
129  {
130  //Print("DbgBlood | CheckBlood | unsent values blood: " + blood_normalized);
131  }
132  }*/
133 
134  void CheckBlood()
135  {
136  float blood_current = m_Player.GetHealth("","Blood");
137  //float blood_normalized = blood_current / m_BloodMaxValue;
138  float blood_normalized = Math.InverseLerp(BLOOD_THRESHOLD_LOW, m_BloodMaxValue, blood_current);
139  blood_normalized = Math.Clamp(blood_normalized,0,1);
140  float difference_normalized = blood_normalized - m_LastBloodUpdate;
141  float diff_abs = Math.AbsFloat(difference_normalized);
142 
143  if ( diff_abs > ( SENSITIVTY_PERCENTAGE /100 ) )
144  {
145  SendValue(TYPE_BLOOD, blood_normalized);
146  m_LastBloodUpdate = blood_normalized;
147  }
148  }
149 
151  void SendInitValues()
152  {
153  m_InitialSyncSent = true;
154 
155  //HP
156  float health_current = m_Player.GetHealth("","Health");
157  float health_normalized = health_current / m_HealthMaxValue;
158  SendValue(TYPE_HEALTH, health_normalized);
159  m_LastHealthUpdate = health_normalized;
160 
161  //Blood
162  float blood_current = m_Player.GetHealth("","Blood");
163  float blood_normalized = Math.InverseLerp(BLOOD_THRESHOLD_LOW, m_BloodMaxValue, blood_current);
164  blood_normalized = Math.Clamp(blood_normalized,0,1);
165  //Print("DbgBlood | SendInitValues | blood_normalized: " + blood_normalized);
166  SendValue(TYPE_BLOOD, blood_normalized);
167  m_LastBloodUpdate = blood_normalized;
168  }
169 
170  void SendValue(int value_type, float value)
171  {
172  CachedObjectsParams.PARAM2_INT_FLOAT.param1 = value_type;
173  CachedObjectsParams.PARAM2_INT_FLOAT.param2 = value;
174 
175  GetGame().RPCSingleParam(m_Player, ERPCs.RPC_DAMAGE_VALUE_SYNC, CachedObjectsParams.PARAM2_INT_FLOAT, true, m_Player.GetIdentity());
176  }
177 
178  void ReceiveValue(int value_type, float value)
179  {
180  if ( value_type == TYPE_HEALTH )
181  {
182  m_HealthClient = value;
183  }
184  else if ( value_type == TYPE_BLOOD )
185  {
186  m_BloodClient = value;
187  }
188  }
189 
190  void OnRPC(ParamsReadContext ctx)
191  {
192  ctx.Read(CachedObjectsParams.PARAM2_INT_FLOAT);
193 
194  int value_type = CachedObjectsParams.PARAM2_INT_FLOAT.param1;
195  float value = CachedObjectsParams.PARAM2_INT_FLOAT.param2;
196 
197  ReceiveValue(value_type, value);
198  }
199 
200  void ShowDebugValues(bool show)
201  {
202  #ifdef DIAG_DEVELOPER
203  if ( show )
204  {
205  DbgUI.BeginCleanupScope();
206  DbgUI.Begin("Values", 50, 50);
207 
208  DbgUI.Text("Blood: " + m_BloodClient.ToString());
209  DbgUI.Text("Health: " + m_HealthClient.ToString());
210 
211  DbgUI.End();
212  DbgUI.EndCleanupScope();
213  }
214  else
215  {
216  DbgUI.BeginCleanupScope();
217  DbgUI.Begin("Values", 50, 50);
218  DbgUI.End();
219  DbgUI.EndCleanupScope();
220  }
221  #endif
222  }
223 }
GetGame
proto native CGame GetGame()
m_TimeSinceLastTick
float m_TimeSinceLastTick
Definition: injuryhandler.c:47
DbgUI
Definition: dbgui.c:59
DiagMenu
Definition: endebug.c:232
Managed
TODO doc.
Definition: enscript.c:117
VALUE_CHECK_INTERVAL
enum eInjuryHandlerLevels VALUE_CHECK_INTERVAL
CachedObjectsParams
Definition: utilityclasses.c:9
OnScheduledTick
void OnScheduledTick(float delta_time)
Definition: modifiersmanager.c:200
m_HealthMaxValue
float m_HealthMaxValue
Definition: injuryhandler.c:50
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
OnRPC
void OnRPC(ParamsReadContext ctx)
Definition: displaystatus.c:216
SendValue
void SendValue(eInjuryHandlerLevels level)
Definition: injuryhandler.c:180
DiagMenuIDs
DiagMenuIDs
Definition: ediagmenuids.c:1
PlayerBase
Definition: playerbaseclient.c:1
SENSITIVTY_PERCENTAGE
const float SENSITIVTY_PERCENTAGE
Definition: injuryhandler.c:43
Init
class InventoryGridController extends ScriptedWidgetEventHandler Init
Definition: uihintpanel.c:46
m_Player
DayZPlayer m_Player
Definition: hand_events.c:42
m_LastHealthUpdate
eInjuryHandlerLevels m_LastHealthUpdate
Definition: injuryhandler.c:49
ERPCs
ERPCs
Definition: erpcs.c:1
Math
Definition: enmath.c:6