DayZ 1.24
Loading...
Searching...
No Matches
Trigger.c
Go to the documentation of this file.
1
3{
4 ref OLinkT insider; // DEPRECATED
5
7 protected Object m_Object;
8
11
14
17
19 {
20 insider = new OLinkT(obj);
21 m_Object = obj;
22 }
23
25 {
26 }
27
29 {
30 return m_Object;
31 }
32};
33
34#ifdef DIAG_DEVELOPER
36#endif
37
40{
42 const int TIMEOUT = 1000;
45
46#ifdef DIAG_DEVELOPER
47 bool m_Local;//is this trigger spawning on client only ?
48 string m_DebugAreaType;
50#endif
51
53 private void Trigger()
54 {
55 SetEventMask(EntityEvent.INIT /*| EntityEvent.TOUCH*/ | EntityEvent.FRAME | EntityEvent.ENTER | EntityEvent.LEAVE);
56 SetFlags(EntityFlags.TRIGGER, false);
57
59 }
60
62 private void ~Trigger()
63 {
64#ifdef DIAG_DEVELOPER
65 CleanupDebugShapes(dbgTargets);
66#endif
67 }
68
74 override void EOnInit(IEntity other, int extra)
75 {
76 SetExtents("-2 -4 -2", "2 4 2");
77 }
78
80 /*override void EOnTouch(IEntity other, int extra)
81 {
82 Object obj;
83 if (Class.CastTo(obj, other) && CanAddObjectAsInsider(obj))
84 AddInsider(obj);
85 }*/
86
88 override void EOnFrame(IEntity other, float timeSlice)
89 {
91 }
92
94 override void EOnEnter(IEntity other, int extra)
95 {
96 Object obj;
97 if (Class.CastTo(obj, other) && CanAddObjectAsInsider(obj))
98 AddInsider(obj);
99 }
100
102 override void EOnLeave(IEntity other, int extra)
103 {
104 Object obj;
105 if (Class.CastTo(obj, other))
107 }
109
110
120
123 {
124 return GetCollisionRadius();
125 }
126
132
135 {
137
138 for (int n = 0; n < m_insiders.Count(); ++n)
139 {
140 ins = m_insiders[n];
141 if (ins.GetObject() == object)
142 return ins;
143 }
144
145 return null;
146 }
147
150 {
152
153 for (int n = 0; n < m_insiders.Count(); ++n)
154 {
155 ins = m_insiders[n];
156 if (ins.GetObject() == object)
157 return n;
158 }
159
160 return -1;
161 }
163
164
169 override protected void OnEnterBeginEvent(TriggerInsider insider)
170 {
171 // Call the old event for backwards compatibility
172 OnEnter(insider.GetObject());
173 }
174
175 override protected void OnLeaveBeginEvent(TriggerInsider insider)
176 {
177 // Call the old event for backwards compatibility
178 OnLeave(insider.GetObject());
179 }
181
182
187 void OnEnter(Object obj) {}
188
189 void OnLeave(Object obj) {}
191
192
198 protected bool CanAddObjectAsInsider(Object object)
199 {
200 return true;
201 }
202
204 protected bool ShouldRemoveInsider(TriggerInsider insider)
205 {
206 return false;
207 }
208
211 {
212 return false;
213 }
215
216
223 {
224 return new TriggerInsider(obj);
225 }
226
228 protected void AddInsider(Object obj)
229 {
230 if (!obj)
231 return;
232
233 // Already in?
234 if (GetInsiderForObject(obj))
235 {
236 Error(string.Format("[WARNING] :: [Trigger] :: [%1] :: Insider (%2) is already inside.", GetDebugName(this), GetDebugName(obj)));
237 return;
238 }
239
240 // New Object entered! Fill the data.
241 TriggerInsider insider = CreateInsider(obj);
242 insider.timeStamp = g_Game.GetTime();
243 insider.timeEntered = g_Game.GetTickTime();
244 insider.lastUpdated = insider.timeEntered;
245
246 // Don't add if it is going to be removed anyways..
247 if (ShouldRemoveInsider(insider) || ShouldRemoveInsiderNoLeave(insider))
248 return;
249
250 // Keep track of the Object as long as it is inside the Trigger
251 int index = m_insiders.Insert(insider);
252
253 // Call the enter event to signal this Object entered
254 Enter(insider);
255 obj.OnEnterTrigger(this);
256
257#ifdef TRIGGER_DEBUG_NORMAL
258 Debug.TriggerLog(string.Format("%1: inserted at index %2", GetDebugName(obj), index), "Trigger", "", "AddInsider", GetDebugName(this));
259#endif
260 }
261
263 protected void RemoveInsider(TriggerInsider insider, int index = -1)
264 {
265 Leave(insider);
266 insider.GetObject().OnLeaveTrigger(this);
267
268#ifdef TRIGGER_DEBUG_NORMAL
269 Debug.TriggerLog(string.Format("%1: removing at index %2", GetDebugName(insider.GetObject()), index), "Trigger", "", "RemoveInsider", GetDebugName(this));
270#endif
271
272 if (index >= 0)
273 m_insiders.Remove(index);
274 else
275 m_insiders.RemoveItemUnOrdered(insider);
276 }
277
279 protected void RemoveInsiderByObject(Object object)
280 {
282 for (int n = 0; n < m_insiders.Count(); ++n)
283 {
284 ins = m_insiders[n];
285 if (ins.GetObject() == object)
286 {
288 return;
289 }
290 }
291
292 // As EOnLeave can call this, it is perfectly valid that this Object is not found on Script side
293 // because of "ShouldRemoveInsider" and "ShouldRemoveInsiderNoLeave"
294 }
295
297 protected void UpdateInsiders(int timeout)
298 {
299#ifdef DIAG_DEVELOPER
301#endif
302
303 // Don't do anything if there aren't any insiders
304 if (m_insiders.Count() == 0)
305 return;
306
307 // Mark the beginning of the update loop
308 StayStart(m_insiders.Count());
309
310 // Iterate over the current insiders, backwards because we are deleting
311 for (int n = m_insiders.Count() - 1; n >= 0 ; --n)
312 {
313 TriggerInsider insider = m_insiders.Get(n);
314 Object obj = insider.GetObject();
315
316 // Check if the Object still exists or should be removed without calling OnLeaveEvent
317 if (!obj || ShouldRemoveInsiderNoLeave(insider))
318 {
319#ifdef TRIGGER_DEBUG_BASIC
320 Debug.TriggerLog(string.Format("%1: removed with no Leave.", GetDebugName(obj)), "Trigger", "", "UpdateInsiders", GetDebugName(this));
321#endif
322
323 m_insiders.Remove(n);
324 continue;
325 }
326
327 // Check if Object left the Trigger or should be removed regardless
328 if (ShouldRemoveInsider(insider))
329 {
330 RemoveInsider(insider, n);
331 continue;
332 }
333
334 // Call the OnStayEvent, Object is still inside the Trigger and can be updated
335 // Pass in the time since the Object was last updated (or entered)
336 float currentTime = g_Game.GetTickTime();
337 Stay(insider, currentTime - insider.lastUpdated);
338 insider.lastUpdated = currentTime;
339 }
340
341 // Mark the end of the update loop
342 StayFinish();
343 }
345
351 {
352 super.OnRPC(sender, rpc_type, ctx);
353
354#ifdef DIAG_DEVELOPER
355 switch (rpc_type)
356 {
357 case ERPCs.DIAG_TRIGGER_DEBUG:
359
360 if (ctx.Read(data))
361 DebugDmgTrigger(data.param1, data.param2, data.param3, data.param4, data.param5, data.param6, data.param7);
362 break;
363 }
364#endif
365 }
366
367#ifdef DIAG_DEVELOPER
369 {
370 vector minmax[2];
371 GetCollisionBox(minmax);
372
374 data.param1 = GetWorldPosition();
375 data.param2 = GetOrientation();
376 data.param3 = minmax[0];
377 data.param4 = minmax[1];
378 data.param5 = GetCollisionRadius();
379 data.param6 = m_DebugAreaType;
380 data.param7 = m_insiders;
381
382 if (GetGame().IsMultiplayer() && GetGame().IsServer())
383 PluginDiagMenuServer.SendDataToSubscribersServer(this, ESubscriberSystems.TRIGGERS, ERPCs.DIAG_TRIGGER_DEBUG, data, false);
384 else if (!GetGame().IsMultiplayer() || m_Local)
385 DebugDmgTrigger(data.param1, data.param2, data.param3, data.param4, data.param5, data.param6, data.param7);
386 }
387
388 protected ref array<Shape> dbgTargets = new array<Shape>();
389
391 {
392 CleanupDebugShapes(dbgTargets);
393
394 bool enableDebug = DiagMenu.GetBool(DiagMenuIDs.TRIGGER_DEBUG);
395 if (enableDebug)
396 {
397 if (GetGame().IsMultiplayer() && GetGame().IsServer())
398 return;
399
401
402 w_pos = pos;
403 // sphere pos tweaks
405 // line pos tweaks
407
408 //Find way to change colour of box depending on ammoType in a more elegant fashion
411
412 switch (m_DebugAreaType)
413 {
414 case "FireDamage":
415 dbgShape = DrawDebugShape(pos, min, max, radius, COLOR_RED_A);
416 break;
417
418 case "BarbedWireHit":
419 dbgShape = DrawDebugShape(pos, min, max, radius, COLOR_BLUE_A);
420 break;
421
422 default:
423 dbgShape = DrawDebugShape(pos, min, max, radius, COLOR_GREEN_A);
424 break;
425 }
426
427 if (GetGame().IsMultiplayer() || GetGame().IsServer())
429
430 if (m_dbgInsiders.Count() > 0)
431 {
432 //Change colour to make state clearer
433 dbgShape.SetColor(COLOR_YELLOW_A);
434
435 for (int i = 0; i < m_dbgInsiders.Count(); i++)
436 {
438 if (insider_EAI)
439 {
440 vector insiderPos = insider_EAI.GetWorldPosition() + "0 0.1 0";
442 }
443 }
444 }
445 }
446 }
447
448 protected Shape DrawDebugShape(vector pos, vector min, vector max, float radius, int color)
449 {
451
452 switch (GetTriggerShape())
453 {
454 case TriggerShape.BOX:
455 dbgShape = Debug.DrawBoxEx(min, max, color, ShapeFlags.TRANSP | ShapeFlags.NOZWRITE);
456
457 vector mat[4];
458 GetTransform(mat);
459 dbgShape.CreateMatrix(mat);
460 dbgShape.SetMatrix(mat);
461 break;
462 case TriggerShape.CYLINDER:
463 dbgShape = Debug.DrawCylinder(pos, radius, max[1], color, ShapeFlags.TRANSP | ShapeFlags.NOZWRITE);
464 break;
465 case TriggerShape.SPHERE:
466 dbgShape = Debug.DrawSphere(pos, radius, color, ShapeFlags.TRANSP | ShapeFlags.NOZWRITE);
467 break;
468 default:
469 ErrorEx("TriggerShape not found", ErrorExSeverity.WARNING);
470 break;
471 }
472
473 dbgTargets.Insert(dbgShape);
474
475 return dbgShape;
476 }
477
478 protected void CleanupDebugShapes(array<Shape> shapes)
479 {
480 foreach (Shape shape : shapes)
481 Debug.RemoveShape(shape);
482
483 shapes.Clear();
484 }
485
486#endif
488};
void DrawDebugShape()
Object GetObject()
vector GetOrientation()
DayZGame g_Game
Definition DayZGame.c:3528
DiagMenuIDs
Definition EDiagMenuIDs.c:2
ERPCs
Definition ERPCs.c:2
proto native void SetCollisionBox(vector mins, vector maxs)
Sets collision box for object.
proto native TriggerShape GetTriggerShape()
Get the current TriggerShape.
TriggerShape
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Debug.c:14
static Shape DrawCylinder(vector pos, float radius, float height=1, int color=0x1fff7f7f, ShapeFlags flags=ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE)
Definition Debug.c:450
static Shape DrawSphere(vector pos, float size=1, int color=0x1fff7f7f, ShapeFlags flags=ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE)
Definition Debug.c:434
static Shape DrawArrow(vector from, vector to, float size=0.5, int color=0xFFFFFFFF, int flags=0)
Definition Debug.c:511
static void TriggerLog(string message=LOG_DEFAULT, string plugin=LOG_DEFAULT, string author=LOG_DEFAULT, string label=LOG_DEFAULT, string entity=LOG_DEFAULT)
Definition Debug.c:223
static Shape DrawBoxEx(vector pos1, vector pos2, int color=0x1fff7f7f, ShapeFlags flags=ShapeFlags.TRANSP|ShapeFlags.NOZWRITE)
Definition Debug.c:406
The class that will be instanced (moddable)
Definition gameplay.c:376
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
void Enter(TriggerInsider insider)
void Leave(TriggerInsider insider)
void StayStart(int nrOfInsiders)
void Stay(TriggerInsider insider, float deltaTime)
Scripted Trigger.
Definition Hologram.c:1518
override void OnLeave(Object obj)
Definition Hologram.c:1533
void OnLeave(Object obj)
Definition Trigger.c:189
override void OnEnter(Object obj)
Definition Hologram.c:1523
TriggerInsider CreateInsider(Object obj)
Used for easily overriding TriggerInsider creation without rewriting AddInsider.
Definition Trigger.c:222
void OnEnter(Object obj)
Definition Trigger.c:187
override void EOnInit(IEntity other, int extra)
Set the default extents of the Trigger only once it is properly initialized.
Definition Trigger.c:74
void RemoveInsider(TriggerInsider insider, int index=-1)
Removing of TriggerInsider.
Definition Trigger.c:263
void OnLeaveBeginEvent(TriggerInsider insider)
Definition Trigger.c:175
override void EOnEnter(IEntity other, int extra)
When an Object enters the trigger add it to Insiders.
Definition Trigger.c:94
override void EOnFrame(IEntity other, float timeSlice)
When an Object touches the Trigger, we want to register it being inside the Trigger -> Replaced by EO...
Definition Trigger.c:88
TriggerInsider GetInsiderForObject(Object object)
Gets the TriggerInsider for the Object if it exists.
Definition Trigger.c:134
void Trigger()
ctor
Definition Trigger.c:53
bool ShouldRemoveInsider(TriggerInsider insider)
Condition whether a TriggerInsider should still be updated or not (checked in update loop and before ...
Definition Trigger.c:204
ref array< ref TriggerInsider > m_insiders
The objects and their metadata which are currently inside the Trigger.
Definition Trigger.c:44
void SetExtents(vector mins, vector maxs)
Set the size of the Trigger, avoid using SetCollisionBox directly.
Definition Trigger.c:116
void UpdateInsiders(int timeout)
Definition Hologram.c:1540
override void OnRPC(PlayerIdentity sender, int rpc_type, ParamsReadContext ctx)
Definition Trigger.c:350
bool CanAddObjectAsInsider(Object object)
Condition whether an Object can be added as TriggerInsider (checked before calling AddInsider)
Definition Trigger.c:198
void OnEnterBeginEvent(TriggerInsider insider)
Definition Trigger.c:169
void RemoveInsiderByObject(Object object)
Removing of TriggerInsider through Object.
Definition Trigger.c:279
void AddInsider(Object obj)
Adding of new TriggerInsider.
Definition Trigger.c:228
array< ref TriggerInsider > GetInsiders()
Get the current TriggerInsider array, left for backwards compatibility, moved down from ManTrigger.
Definition Trigger.c:128
float GetRadius(vector min, vector max)
Get the radius of the CollisionBox, simply left for backwards compatibility.
Definition Trigger.c:122
bool ShouldRemoveInsiderNoLeave(TriggerInsider insider)
Condition whether a TriggerInsider should still be updated or not, skips OnLeaveEvent (checked in upd...
Definition Trigger.c:210
int GetInsiderIndexForObject(Object object)
Gets the index in m_insiders for the Object.
Definition Trigger.c:149
void ~Trigger()
dtor
Definition Trigger.c:62
override void EOnLeave(IEntity other, int extra)
When an Object exits the trigger remove it from Insiders.
Definition Trigger.c:102
const int TIMEOUT
DEPRECATED.
Definition Trigger.c:42
The object which is in a trigger and its metadata.
Definition Trigger.c:3
int timeStamp
Last time the object was seen in ms.
Definition Trigger.c:10
Object m_Object
Object that data belongs to.
Definition Trigger.c:7
void ~TriggerInsider()
Definition Trigger.c:24
float lastUpdated
Last time the object was updated in seconds, is used for calculating deltaTime.
Definition Trigger.c:16
ref OLinkT insider
Definition Trigger.c:4
Object GetObject()
Definition Trigger.c:28
float timeEntered
Time the object was first seen in seconds.
Definition Trigger.c:13
void TriggerInsider(Object obj)
Definition Trigger.c:18
static const vector Zero
Definition EnConvert.c:110
override string GetDebugName()
Link< Object > OLinkT
Definition gameplay.c:1456
proto native CGame GetGame()
const int COLOR_BLUE_A
Definition constants.c:71
const int COLOR_RED_A
Definition constants.c:69
const int COLOR_YELLOW_A
Definition constants.c:72
const int COLOR_GREEN_A
Definition constants.c:70
ErrorExSeverity
Definition EnDebug.c:62
void Error(string err)
Messagebox with error message.
Definition EnDebug.c:90
enum ShapeType ErrorEx
proto native void SetFlags(ShapeFlags flags)
ShapeFlags
Definition EnDebug.c:126
static proto bool GetBool(int id, bool reverse=false)
Get value as bool from the given script id.
class DiagMenu Shape
don't call destructor directly. Use Destroy() instead
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition EnEntity.c:44
EntityFlags
Entity flags.
Definition EnEntity.c:114