Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
cfgplayerrestrictedareahandler.c
Go to the documentation of this file.
2{
3 private static bool m_Initialized;
4
5 static ref TStringArray m_PRAFiles = new TStringArray();
7
8 //----------------------------------------------------------
9 //load, inits, validation
10
11 static bool LoadData()
12 {
13 if (m_Initialized)
14 return true;
15
16 m_Initialized = false;
17
18 if (GetGame().ServerConfigGetInt( "enableCfgGameplayFile" )) //only reach into the CfgGameplayHandler if properly loaded!
19 m_PRAFiles = CfgGameplayHandler.GetPlayerRestrictedAreaFiles();
20 else
21 m_PRAFiles = g_Game.GetMission().GetWorldData().GetDefaultPRAPaths();
22
23 if (!m_PRAFiles)
24 return true;
25
26 string worldName;
27 GetGame().GetWorldName(worldName);
28
29 foreach (string filename : m_PRAFiles)
30 {
32
33 string errorMessage, path;
34
35 path = string.Format("$mission:%1", filename);
36 if (!JsonFileLoader<PlayerRestrictedAreaInstance>.LoadFile(path, area, errorMessage))
37 {
38 if (!FileExist(filename))
39 path = string.Format("dz/worlds/%1/ce/%2", worldName, filename);
40
41 if (!JsonFileLoader<PlayerRestrictedAreaInstance>.LoadFile(path, area, errorMessage))
42 {
43 ErrorEx(errorMessage);
44 continue;
45 }
46 }
47
48 if (area != null)
49 m_Data.m_Areas.Insert(area);
50 }
51
52 m_Initialized = m_Data.InitializeAreas();
53
54 return true;
55 }
56
57 static bool IsInitialized()
58 {
59 return m_Initialized;
60 }
61
62 //----------------------------------------------------------
63 //synchronization
64
65 static void SyncDataSend(notnull PlayerIdentity identity)
66 {
67 GetGame().RPCSingleParam(null, ERPCs.RPC_PLAYERRESTRICTEDAREAS_SYNC, new Param1<CfgPlayerRestrictedAreaJsonData>(m_Data), true, identity);
68 }
69
70 static void OnRPC(ParamsReadContext ctx)
71 {
72 Param1<CfgPlayerRestrictedAreaJsonData> data = new Param1<CfgPlayerRestrictedAreaJsonData>(null);
73
74 if (ctx.Read(data))
75 {
76 m_Data = data.param1;
77 m_Initialized = m_Data.m_ValidatedAreas.Count() > 0;
78 }
79 else
80 {
81 ErrorEx("CfgPlayerRestrictedAreaHandler - client failed to read incoming data");
82 }
83 }
84
85 //----------------------------------------------------------
86 //main area checking methods
87
88 static bool IsPointInPlayerRestrictedAreaClosest(vector point, out PlayerRestrictedAreaInstance hitArea)
89 {
90 if (!IsInitialized())
91 return false;
92
93 PlayerRestrictedAreaInstance area = GetClosestArea(point);
94 if (area && IsCylinderInAreaBox(area,point) || IsPointInAreaPolygon(area,point))
95 {
96 hitArea = area;
97 return true;
98 }
99 return false;
100 }
101
102 static bool IsPointInPlayerRestrictedArea(vector point, out PlayerRestrictedAreaInstance hitArea)
103 {
104 if (!IsInitialized())
105 return false;
106
107 foreach (PlayerRestrictedAreaInstance area : m_Data.m_ValidatedAreas)
108 {
109 if (IsCylinderInAreaBox(area,point) || IsPointInAreaPolygon(area,point))
110 {
111 hitArea = area;
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119 //----------------------------------------------------------
120 //support methods
121
122 private static PlayerRestrictedAreaInstance GetClosestArea(vector point)
123 {
125 float closestDist = float.MAX;
126 float currentDist;
127
128 foreach (PlayerRestrictedAreaInstance area : m_Data.m_ValidatedAreas)
129 {
130 currentDist = vector.DistanceSq(point,area.GetCenterPos2D());
131 if (currentDist < closestDist)
132 {
133 ret = area;
134 closestDist = currentDist;
135 }
136 }
137
138 return ret;
139 }
140
142 private static bool IsCylinderInAreaBox(notnull PlayerRestrictedAreaInstance area, vector point, float cylinderRadius = 0.25, float cylinderHeight = 1)
143 {
144 foreach (PRAShapeBoxData boxData : area.m_PRABoxDataTranslated)
145 {
146 vector matPlayer4[4];
147 Math3D.MatrixIdentity4(matPlayer4);
148 matPlayer4[3] = point;
149 if (Math3D.IntersectCylinderOBB(boxData.m_Mins,boxData.m_Maxs,boxData.m_Mat4,matPlayer4,cylinderRadius,cylinderHeight))
150 return true;
151 }
152
153 return false;
154 }
155
156 private static bool IsPointInAreaPolygon(notnull PlayerRestrictedAreaInstance area, vector point)
157 {
158 array<float> translatedDta = new array<float>;
159 foreach (array<ref array<float>> polygonData : area.PRAPolygons)
160 {
161 translatedDta.Clear();
162
163 foreach (array<float> vertexPos : polygonData)
164 {
165 translatedDta.Insert(vertexPos[0]);
166 translatedDta.Insert(vertexPos[1]);
167 }
168
169 if (Math2D.IsPointInPolygon(translatedDta,point[0],point[2]))
170 return true;
171 }
172
173 return false;
174 }
175
176 //----------------------------------------------------------
177 //debugs
178
179 static private ref array<Shape> m_DbgShapesBoxes;
180 static private ref array<Shape> m_DbgShapesPolygonLines;
181
182 static void DrawBoxesDebug(bool draw)
183 {
184 if (!IsInitialized())
185 return;
186
187 if (!m_DbgShapesBoxes)
188 m_DbgShapesBoxes = {};
189
190 if (draw && m_DbgShapesBoxes.Count() == 0)
191 {
192 foreach (PlayerRestrictedAreaInstance area : m_Data.m_ValidatedAreas)
193 {
194 foreach (PRAShapeBoxData boxData : area.m_PRABoxDataTranslated)
195 {
196 Shape shp = Debug.DrawBox(boxData.m_Mins,boxData.m_Maxs);
197 shp.SetMatrix(boxData.m_Mat4);
198 m_DbgShapesBoxes.Insert(shp);
199 }
200 }
201 }
202 else if (!draw && m_DbgShapesBoxes.Count() > 0)
203 {
204 foreach (Shape box : m_DbgShapesBoxes)
205 {
206 box.Destroy();
207 }
208
209 m_DbgShapesBoxes.Clear();
210 }
211 }
212
213 static void DrawPolygonLinesDebug(bool draw)
214 {
215 if (!IsInitialized())
216 return;
217
218 if (!m_DbgShapesPolygonLines)
219 m_DbgShapesPolygonLines = {};
220
221 if (draw && m_DbgShapesPolygonLines.Count() == 0)
222 {
223 array<vector> polygonVectors = {};
224 foreach (PlayerRestrictedAreaInstance area : m_Data.m_ValidatedAreas)
225 {
226 foreach (array<ref array<float>> polygonData : area.PRAPolygons)
227 {
228 vector first;
229 vector current;
230 vector last;
231
232 foreach (array<float> vertexPos : polygonData)
233 {
234 polygonVectors.Insert(Vector(vertexPos[0],GetGame().SurfaceY(vertexPos[0],vertexPos[1]) + 2,vertexPos[1]));
235 }
236
237 int count = polygonVectors.Count();
238 for (int i = 0; i < count; ++i)
239 {
240 current = polygonVectors[i];
241
242 if (i == 0)
243 first = polygonVectors[i];
244 else
245 Debug.DrawLine(last,current,COLOR_WHITE,ShapeFlags.TRANSP|ShapeFlags.NOZWRITE);
246
247 last = current;
248 }
249
250 m_DbgShapesPolygonLines.Insert(Debug.DrawLine(current,first,COLOR_RED,ShapeFlags.TRANSP|ShapeFlags.NOZWRITE));
251
252 polygonVectors.Clear();
253 }
254 }
255 }
256 else if (!draw && m_DbgShapesPolygonLines.Count() > 0)
257 {
258 foreach (Shape item : m_DbgShapesPolygonLines)
259 {
260 item.Destroy();
261 }
262
263 m_DbgShapesPolygonLines.Clear();
264 }
265 }
266}
bool m_Initialized
Definition debug.c:2
The class that will be instanced (moddable)
Definition gameplay.c:389
Serialization general interface. Serializer API works with:
Definition serializer.c:56
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
DayZGame g_Game
Definition dayzgame.c:3868
ERPCs
Definition erpcs.c:2
proto native CGame GetGame()
const int COLOR_RED
Definition constants.c:64
const int COLOR_WHITE
Definition constants.c:63
enum ShapeType ErrorEx
ShapeFlags
Definition endebug.c:126
class DiagMenu Shape
don't call destructor directly. Use Destroy() instead
array< string > TStringArray
Definition enscript.c:709
proto bool FileExist(string name)
Check existence of file.
enum WindingOrder Math2D()
proto native vector Vector(float x, float y, float z)
Vector constructor from components.