Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
pluginuniversaltemperaturesourceclient.c
Go to the documentation of this file.
1 class PluginUniversalTemperatureSourceClient extends PluginBase
2 {
3  const int MAX_SIMULTANEOUS_UTS = 10;
4 
5  protected float m_UTSAverageTemperature;
6 
7  protected ref array<ref UTemperatureSourceDebug> m_UTemperatureSourceDebugs;
8 
9  protected ref Widget m_RootWidget[MAX_SIMULTANEOUS_UTS];
10  protected TextListboxWidget m_StatListWidgets[MAX_SIMULTANEOUS_UTS];
11  protected TextWidget m_HeaderWidget[MAX_SIMULTANEOUS_UTS];
12 
13  protected PlayerBase m_Player;
14 
15  void PluginUniversalTemperatureSourceClient()
16  {
17  m_UTemperatureSourceDebugs = new array<ref UTemperatureSourceDebug>();
18  }
19 
20  override void OnInit()
21  {
22  #ifndef NO_GUI
23  InitWidgets();
24  #endif
25  }
26 
27  override void OnUpdate(float delta_time)
28  {
29  #ifndef NO_GUI
30  if (!m_Player)
31  {
32  return;
33  }
34 
35  UpdateStatWidgets();
36  DrawDebugs();
37  #endif
38  }
39 
40  void InitWidgets()
41  {
42  for (int i = 0; i < MAX_SIMULTANEOUS_UTS; i++)
43  {
44  m_RootWidget[i] = GetGame().GetWorkspace().CreateWidgets("gui/layouts/debug/day_z_debug_remoteinfo.layout");
45  m_StatListWidgets[i] = TextListboxWidget.Cast(m_RootWidget[i].FindAnyWidget("TextListboxWidget0"));
46  m_HeaderWidget[i] = TextWidget.Cast(m_RootWidget[i].FindAnyWidget("TextWidget0"));
47  }
48  }
49 
50  void DrawDebugs()
51  {
52  foreach (UTemperatureSourceDebug utsd : m_UTemperatureSourceDebugs)
53  {
54  float fullRange = utsd.GetValue(1).ToFloat();
55  float maxRange = utsd.GetValue(2).ToFloat();
56  float temp = utsd.GetValue(3).ToFloat();
57  vector sphPos = utsd.GetValue(0).ToVector();
58 
59  int fullRangeColor = COLOR_RED_A;
60  int maxRangeColor = COLOR_YELLOW_A;
61  if (temp < 0)
62  {
63  fullRangeColor = COLOR_GREEN_A;
64  maxRangeColor = COLOR_BLUE_A;
65  }
66 
67  Debug.DrawCylinder(sphPos, fullRange, fullRange, fullRangeColor, ShapeFlags.ONCE|ShapeFlags.TRANSP);
68  Debug.DrawCylinder(sphPos, maxRange, maxRange, maxRangeColor, ShapeFlags.ONCE|ShapeFlags.TRANSP);
69  Debug.DrawArrow(m_Player.GetPosition(), sphPos, 1.0, 0xffffffff, ShapeFlags.ONCE|ShapeFlags.TRANSP);
70  }
71 
72  ProcessUniversalTemperatureSources();
73 
74  if (m_UTemperatureSourceDebugs.Count() > 0)
75  {
76  DbgUI.Begin("Universal Temp Sources", 10, 300);
77  DbgUI.Text(string.Format("Lookup radius: %1m (server-side)", PluginUniversalTemperatureSourceServer.LOOKUP_RADIUS));
78  DbgUI.Text(string.Format("Count: %1", m_UTemperatureSourceDebugs.Count()));
79  DbgUI.Text(string.Format("Avg. temp: %1 °C", m_UTSAverageTemperature));
80  DbgUI.End();
81  }
82  }
83 
84  protected void ProcessUniversalTemperatureSources()
85  {
86  if (m_UTemperatureSourceDebugs.Count() == 0)
87  {
88  m_UTSAverageTemperature = 0;
89 
90  return;
91  }
92 
93  array<float> utsTemperatures = new array<float>();
94 
95  // get temperature from the source (based on distance), save it for min/max filtering
96  foreach (UniversalTemperatureSourceDebug utsd : m_UTemperatureSourceDebugs)
97  {
99  if (vector.DistanceSq(m_Player.GetPosition(), utsd.GetValue(0).ToVector()) > Math.SqrFloat(utsd.GetValue(2).ToFloat()))
100  {
101  continue;
102  }
103 
104  utsTemperatures.Insert(CalcTemperatureFromTemperatureSource(utsd));
105  }
106 
107  float min = MiscGameplayFunctions.GetMinValue(utsTemperatures);
108  float max = MiscGameplayFunctions.GetMaxValue(utsTemperatures);
109 
110  if (max > 0 && min < 0)
111  {
112  m_UTSAverageTemperature = (max + min) * 0.5;
113  }
114  else
115  {
116  m_UTSAverageTemperature = max;
117  }
118 
119  }
120 
121  protected float CalcTemperatureFromTemperatureSource(notnull UTemperatureSourceDebug utsd)
122  {
123  float distance = vector.Distance(m_Player.GetPosition(), utsd.GetValue(0).ToVector());
124  distance = Math.Max(distance, 0.1); //min distance cannot be 0 (division by zero)
125  float temperature = 0;
126 
128  if (distance > utsd.GetValue(1).ToFloat())
129  {
130  float distFactor = 1 - (distance / utsd.GetValue(2).ToFloat());
131  distFactor = Math.Max(distFactor, 0.0);
132  temperature = utsd.GetValue(3).ToFloat() * distFactor;
133  }
134  else
135  {
136  temperature = utsd.GetValue(3).ToFloat();
137  }
138 
139  //Print(temperature);
140 
141  return temperature;
142  }
143 
144  void EnableWidgets(bool enable)
145  {
146  for (int i = 0; i < MAX_SIMULTANEOUS_UTS; i++)
147  {
148  m_RootWidget[i].Show(enable);
149  }
150  }
151 
152  void UpdateStatWidgets()
153  {
154  int i = 0;
155  int utsDebugCount = m_UTemperatureSourceDebugs.Count();
156  for (; i < utsDebugCount && i < MAX_SIMULTANEOUS_UTS; ++i)
157  {
158  UTemperatureSourceDebug utsd = m_UTemperatureSourceDebugs[i];
159  vector pos = utsd.GetValue(0).ToVector();
160  vector screen_pos_stats = GetGame().GetScreenPos(pos + "0 0 0");
161  vector screen_pos_damage = GetGame().GetScreenPos(pos + "0 2 0");
162  m_RootWidget[i].SetPos(screen_pos_stats[0], screen_pos_stats[1]);
163 
164  if (screen_pos_stats[2] > 0 && screen_pos_stats[0] > 0 && screen_pos_stats[1] > 0)
165  {
166  m_RootWidget[i].Show(true);
167  UpdateStatWidget(i, utsd);
168  }
169  else
170  {
171  m_RootWidget[i].Show(false);
172  }
173  }
174 
175  for (; i < MAX_SIMULTANEOUS_UTS; ++i)
176  {
177  if (m_RootWidget[i])
178  {
179  m_RootWidget[i].Show(false);
180  }
181  }
182  }
183 
184  void UpdateStatWidget(int rowIndex, UTemperatureSourceDebug utsd)
185  {
186  m_StatListWidgets[rowIndex].ClearItems();
187 
188  m_HeaderWidget[rowIndex].SetText(utsd.GetHeader());
189 
190  int numPairs = utsd.PairsCount();
191  for (int i = 0; i < numPairs; ++i)
192  {
193  m_StatListWidgets[rowIndex].AddItem(utsd.GetName(i), null, 0, i);
194  m_StatListWidgets[rowIndex].SetItem(i, utsd.GetValue(i), null, 1);
195  }
196 
197  // manually add value for distance (client only)
198  m_StatListWidgets[rowIndex].AddItem("distance", null, 0, numPairs);
199  m_StatListWidgets[rowIndex].SetItem(numPairs, vector.Distance(m_Player.GetPosition(), utsd.GetValue(0).ToVector()).ToString(), null, 1);
200  }
201 
202  void RequestUniversalTemperatureSources(PlayerBase player, int enable)
203  {
204  //Debug.Log("RequestUniversalTemperatureSources called", "PluginUniversalTemperatureSourceClient");
205 
206  if (!enable)
207  {
208  m_UTemperatureSourceDebugs.Clear();
209  m_Player = null;
210  EnableWidgets(false);
211  return;
212  }
213 
214  ScriptRPC rpc = new ScriptRPC();
215  rpc.Write(enable);
216  rpc.Send(player, ERPCs.DEV_REQUEST_UTS_DEBUG, true, player.GetIdentity());
217 
218  m_Player = player;
219  }
220 
221  void PrintedDebug()
222  {
223  foreach (UTemperatureSourceDebug utsd : m_UTemperatureSourceDebugs)
224  {
225  PrintString("-------------------------------------");
226  utsd.Debug();
227  PrintString("-------------------------------------");
228  }
229  }
230 
231  void OnRPC(ParamsReadContext ctx)
232  {
233  //Debug.Log("OnRPC called", "PluginUniversalTemperatureSourceClient");
234  //PrintedDebug();
235  ctx.Read(m_UTemperatureSourceDebugs);
236  }
237 }
EnableWidgets
void EnableWidgets(bool enable)
Definition: pluginremoteplayerdebugclient.c:47
GetGame
proto native CGame GetGame()
DbgUI
Definition: dbgui.c:59
m_RootWidget
ref Widget m_RootWidget[MAX_SIMULTANIOUS_PLAYERS]
Definition: pluginremoteplayerdebugclient.c:14
m_StatListWidgets
ref TextListboxWidget m_StatListWidgets[MAX_SIMULTANIOUS_PLAYERS]
Definition: pluginremoteplayerdebugclient.c:16
COLOR_YELLOW_A
const int COLOR_YELLOW_A
Definition: constants.c:72
UniversalTemperatureSourceDebug
Definition: universaltemperaturesource.c:53
COLOR_RED_A
const int COLOR_RED_A
Definition: constants.c:69
PluginBase
Definition: pluginadminlog.c:1
FindAnyWidget
proto native Widget FindAnyWidget(string pathname)
InitWidgets
void InitWidgets()
Definition: pluginremoteplayerdebugclient.c:35
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
OnRPC
void OnRPC(ParamsReadContext ctx)
Definition: displaystatus.c:216
PlayerBase
Definition: playerbaseclient.c:1
vector
Definition: enconvert.c:105
TextWidget
Definition: enwidgets.c:219
OnUpdate
proto native void OnUpdate()
Definition: tools.c:349
ShapeFlags
ShapeFlags
Definition: endebug.c:125
ScriptRPC
Definition: gameplay.c:104
m_Player
DayZPlayer m_Player
Definition: hand_events.c:42
PrintString
void PrintString(string s)
Helper for printing out string expression. Example: PrintString("Hello " + var);.
Definition: enscript.c:345
COLOR_BLUE_A
const int COLOR_BLUE_A
Definition: constants.c:71
array< ref UTemperatureSourceDebug >
Debug
Definition: debug.c:13
ERPCs
ERPCs
Definition: erpcs.c:1
OnInit
void OnInit()
Definition: aibehaviour.c:49
Widget
Definition: enwidgets.c:189
Math
Definition: enmath.c:6
COLOR_GREEN_A
const int COLOR_GREEN_A
Definition: constants.c:70