DayZ 1.24
Loading...
Searching...
No Matches
TrapBase.c
Go to the documentation of this file.
2{
4}
5
6class TrapBase extends ItemBase
7{
8#ifdef SERVER
9 protected const int SPAWN_FLAGS = ECE_CREATEPHYSICS;
10#else
11 protected const int SPAWN_FLAGS = ECE_LOCAL;
12#endif
13
14 protected const int DAMAGE_TRIGGER_MINE = 75;
15 protected const float UPDATE_TIMER_INTERVAL = 0.05;
16
17 float m_InitWaitTime; //After this time after deployment, the trap is activated
18 bool m_NeedActivation; //If activation of trap is needed
19 float m_DefectRate; //Added damage after trap activation
20 float m_DamagePlayers; //How much damage player gets when caught
21 float m_DamageOthers; //How much damage player gets when caught
22
23 bool m_AddActivationDefect; // Damage trap after activation
24 bool m_AddDeactivationDefect; // Damage trap after deactivation
25 protected bool m_IsActive; // True means that the trap is ready to detonate
26 protected bool m_IsInProgress;
27
28 protected bool m_Disarmed = false;
29
31
35
41
42 protected ref Timer m_Timer;
45
47 protected ref EffectSound m_DeployLoopSound; //DEPRECATED in favor of m_DeployLoopSoundEx
48
49 void TrapBase()
50 {
51 m_IsInProgress = false;
52 m_NeedActivation = true;
53 m_InitWaitTime = 5; //After this time after deployment, the trap is activated
54 m_DefectRate = 15; //Added damage after trap activation
55 m_DamagePlayers = 25; //How much damage player gets when caught
56 m_DamageOthers = 100; //How much damage others gets when caught
57
60
62
66
67 m_InfoSetup = "#STR_TrapBase0";
68 m_InfoDeactivated = "#STR_TrapBase1";
69 m_InfoDamageManipulation = "#STR_TrapBase2";
70 m_InfoDamage = "#STR_TrapBase3";
71 m_InfoActivationTime = "#STR_TrapBase4" + m_InitWaitTime.ToString() + "#STR_TrapBase5";
72
73 m_UpdateTimer = new ref Timer();
74
75 RegisterNetSyncVariableBool("m_IsActive");
76 RegisterNetSyncVariableBool("m_IsInProgress");
77 RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
78 RegisterNetSyncVariableBool("m_IsDeploySound");
79 }
80
85
87
92
95 {
96 super.OnVariablesSynchronized();
97
98 if (IsDeploySound())
100
103
106
107 if (GetGame().IsMultiplayer())
108 {
110 SetActive();
111
114 }
115 }
116
117 override void EEDelete(EntityAI parent)
118 {
119 super.EEDelete(parent);
120
121 if (GetGame() && m_TrapTrigger)
122 {
123 GetGame().ObjectDelete(m_TrapTrigger);
125 }
126 }
127
129 {
130 super.OnStoreSave(ctx);
131
132 ctx.Write(m_IsActive);
133 ctx.Write(m_IsInProgress);
134 }
135
136 //----------------------------------------------------------------
137 override bool OnStoreLoad(ParamsReadContext ctx, int version)
138 {
139 if (!super.OnStoreLoad(ctx, version))
140 return false;
141
142 bool b_is_active = false;
143 if (!ctx.Read(b_is_active))
144 b_is_active = false;
145
146 bool b_is_in_progress = false;
147 if (!ctx.Read(b_is_in_progress))
148 b_is_in_progress = false;
149
150 if (b_is_active)
151 SetActive();
152
155
156 return true;
157 }
158
159 bool IsActive()
160 {
161 return m_IsActive && m_IsInProgress == false && GetHierarchyRootPlayer() == null;
162 }
163
165 {
166 return !IsActive() && m_IsInProgress == false && GetHierarchyRootPlayer() == null;
167 }
168
169 // trap cannot be taken when is activated
170 override bool IsTakeable()
171 {
172 if (m_IsInProgress == false && !IsActive())
173 return true;
174
175 return false;
176 }
177
179 {
180 return !IsActive() && GetHierarchyRootPlayer() == null && GetHierarchyParent() == null && m_IsInProgress == false && !IsRuined() && m_NeedActivation;
181 }
182
184 {
185 if (GetHierarchyRootPlayer() != null && GetHierarchyRootPlayer().GetHumanInventory().GetEntityInHands() == this)
186 {
187 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
188
189 vector player_pos = player.GetPosition();
190 vector aim_pos = player.GetAimPosition();
191
194 }
195
196 return false;
197 }
198
200 {
201 if (GetGame().SurfaceIsSea(position[0], position[2]))
202 return false;
203 else if (GetGame().SurfaceIsPond(position[0], position[2]))
204 return false;
205
206 return true;
207 }
208
209 void Disarm()
210 {
211 SetInactive(false);
212 RefreshState();
213 GetGame().RPCSingleParam(this, ERPCs.RPC_TRAP_DISARM, null, true);
214
215 OnDisarm();
216 }
217
219 void OnDisarm();
220
222 {
223 if (GetGame().IsServer())
224 {
225 if (m_Timer)
226 m_Timer.Stop();
227
228 RefreshState();
229
230 if (m_DamagePlayers > 0)
231 {
232 if (victim)
233 {
234 if (victim.IsInherited(SurvivorBase))
235 victim.DecreaseHealth("", "", m_DamagePlayers);
236 else if (victim.IsInherited(DayZCreatureAI))
237 victim.DecreaseHealth("", "", m_DamageOthers);
238 else if (victim.IsInherited(ItemBase))
239 {
241 float damage_coef = 1;
242
243 if (victim_item.HasQuantity() && victim_item.GetQuantityMax() != 0 && victim_item.GetQuantity() > 0)
244 {
245 damage_coef = victim_item.GetQuantityMax() / victim_item.GetQuantity(); // Lower quantity increases damage exposure
246 }
247
248 if (damage_coef > 0)
249 {
250 int item_size_x = 1;
251 int item_size_y = 1;
252 GetGame().GetInventoryItemSize(victim_item, item_size_x, item_size_y);
253
254 float add_damage = 300 * damage_coef / Math.Clamp(item_size_x * item_size_y, 1, int.MAX);
255 victim_item.DecreaseHealth("", "", add_damage);
256 }
257 }
258 }
259 }
260
261 Synch(victim);
262 }
263
265 }
266
271
273 {
274 SetInactive(false);
275 }
276
278
279 // Synchronizes states
280 protected void Synch(EntityAI victim)
281 {
282 if (GetGame().IsServer())
283 {
284 if (victim && !victim.GetAllowDamage())
285 return;
286
288 GetGame().RPCSingleParam(this, ERPCs.RPC_TRAP_VICTIM, p, true);
289 }
290
291 }
292
293 // On server -> client synchronization
295 {
296 super.OnRPC(sender, rpc_type, ctx);
297
298 if (!GetGame().IsDedicatedServer())
299 {
300 switch (rpc_type)
301 {
302 case ERPCs.RPC_TRAP_VICTIM:
304
305 if (ctx.Read(victim))
306 {
307 if (victim.param1)
308 SnapOnObject(victim.param1);
309 }
310
311 break;
312
313 case ERPCs.RPC_TRAP_DISARM:
314 OnDisarm();
315 break;
316
317 case SoundTypeTrap.ACTIVATING:
318
319 Param1<bool> p = new Param1<bool>(false);
320
321 bool isActivating = false;
322 if (ctx.Read(p))
323 isActivating = p.param1;
324
325 if (isActivating)
327
328 if (!isActivating)
330
331 break;
332 }
333 }
334 }
335
337 {
339 return;
340
341 if (GetGame().IsServer())
342 {
343 // item is owned by player
344 if (GetHierarchyRootPlayer() != NULL && m_AnimationPhaseGrounded != "")
345 {
346 SetAnimationPhase(m_AnimationPhaseSet, 1);
348 SetAnimationPhase(m_AnimationPhaseTriggered, 1);
349 SetAnimationPhase(m_AnimationPhaseGrounded, 0);
350 }
351 // item is set active
352 else if (IsActive())
353 {
354 if (m_AnimationPhaseGrounded != "")
355 SetAnimationPhase(m_AnimationPhaseGrounded, 1);
357 {
358 SetAnimationPhase(m_AnimationPhaseTriggered, 1);
359 SetAnimationPhase(m_AnimationPhaseSet, 0);
360 }
361 }
362 // item is inactive and not owned by player (on the ground)
363 else if (IsInactive())
364 {
366 SetAnimationPhase(m_AnimationPhaseGrounded, 1);
368 {
369 SetAnimationPhase(m_AnimationPhaseSet, 1);
370 SetAnimationPhase(m_AnimationPhaseTriggered, 0);
371 }
372 }
373 }
374 }
375
377 {
378 if (GetGame().IsServer())
379 {
380 if (GetHierarchyRootPlayer() && GetHierarchyRootPlayer().CanDropEntity(this))
381 SetupTrapPlayer(PlayerBase.Cast(GetHierarchyRootPlayer()));
382 }
383 }
384
386 {
387 if (GetGame().IsServer())
388 {
389 if (set_position)
390 {
391 player.LocalDropEntity(this);
392
393 vector trapPos = player.GetDirection() * 1.5;
394 trapPos[1] = 0;
395 SetPosition(player.GetPosition() + trapPos);
396 }
397
398 if (m_NeedActivation == false)
399 SetActive();
400 }
401 }
402
404 {
405 if (GetGame().IsServer())
406 DecreaseHealth("", "", m_DefectRate);
407 }
408
410 {
412
413 m_IsInProgress = false;
414 m_IsActive = true;
415
417 AddDefect();
418
419 if (GetGame().IsServer())
420 {
422 RefreshState();
423 SetSynchDirty();
424 }
425
426 OnActivate();
427 }
428
429 void OnActivate();
430
432 {
433 if (GetGame().IsServer())
434 {
436 HideSelection("safety_pin");
437
438 if (m_InitWaitTime > 0)
439 {
440 m_IsInProgress = true;
441 m_Timer.Run(m_InitWaitTime, this, "SetActive");
442
443 SetSynchDirty();
444 }
445 else
446 SetActive();
447 }
448 }
449
451
452 void SetInactive(bool stop_timer = true)
453 {
455
456 m_IsActive = false;
457 if (m_Timer && stop_timer)
458 m_Timer.Stop();
459
461 AddDefect();
462
463 SetSynchDirty();
465 RefreshState();
466 }
467
469 {
470 if (Class.CastTo(m_TrapTrigger, GetGame().CreateObjectEx("TrapTrigger", GetPosition(), SPAWN_FLAGS)))
471 {
472 vector mins = "-0.01 -0.05 -0.01";
473 vector maxs = "0.01 0.5 0.01";
474 m_TrapTrigger.SetOrientation(GetOrientation());
478 }
479 }
480
482 {
483 if (m_TrapTrigger)
484 {
486 m_TrapTrigger.DeleteSafe();
487 }
488 }
489
495
497 {
498 super.OnItemLocationChanged(old_owner, new_owner);
499
500 if (GetGame().IsServer())
501 {
502 RefreshState();
503
504 // TAKE ACTIVE TRAP FROM VICINITY
505 if (old_owner == NULL && new_owner != NULL && IsActive())
506 {
507 // TAKE INTO HANDS
508 if (new_owner.IsPlayer())
510 else if (new_owner.GetHierarchyRootPlayer())
511 SnapOnObject(new_owner.GetHierarchyRootPlayer());
512 }
513 }
514
515 }
516
517 override void EEItemAttached(EntityAI item, string slot_name)
518 {
519 super.EEItemAttached(item, slot_name);
520
521 if (GetGame().IsServer())
522 RefreshState();
523 }
524
525 override void EEItemDetached(EntityAI item, string slot_name)
526 {
527 super.EEItemDetached(item, slot_name);
528
529 if (GetGame().IsServer())
530 RefreshState();
531 }
532
533 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
534 {
535 super.OnPlacementComplete(player, position, orientation);
536
537 if (GetGame().IsServer())
538 {
539 SetOrientation(orientation);
541 PlaceOnSurface();
542 SetSynchDirty();
543 }
544 }
545
546 override bool CanPutInCargo(EntityAI parent)
547 {
548 if (!super.CanPutInCargo(parent))
549 return false;
550
551 return IsTakeable();
552 }
553
554 override bool CanPutIntoHands(EntityAI parent)
555 {
556 if (!super.CanPutIntoHands(parent))
557 return false;
558
559 return IsTakeable();
560 }
561
562 override bool CanRemoveFromHands(EntityAI parent)
563 {
564 return IsTakeable();
565 }
566
567 override bool CanBePlaced(Man player, vector position)
568 {
570 }
571
574 {
575 return true;
576 }
577
578 //Set if trap can be disarmed using trap-specific action
580 {
581 return false;
582 }
583
586 {
588 }
589
592 {
593 return m_Disarmed;
594 }
595
596 //================================================================
597 // ADVANCED PLACEMENT
598 //================================================================
599
600 void PlayDeployLoopSound(); //deprecated
601
602 void StopDeployLoopSound(); //deprecated
603
604 override void SetActions()
605 {
606 super.SetActions();
607
609 }
610
611 // HELPERS
613 {
616 trapPosXZ[1] = 0;
617
618 GameInventory inv = victim.GetInventory();
619 for (int i = 0; i < inv.AttachmentCount(); i++)
620 {
622 EntityAI wheelEntity = inv.GetAttachmentFromIndex(i);
623 if (wheelEntity && wheelEntity.Type() == CarWheel_Ruined)
624 continue;
625
627 int slotId;
628 string slotName;
629 wheelEntity.GetInventory().GetCurrentAttachmentSlotInfo(slotId, slotName);
630 slotName.ToLower();
631 if (slotName.Contains("spare"))
632 {
633 continue
634 }
635
637 if (wheelEntity && wheelEntity.IsInherited(CarWheel))
638 {
639 vector entPosXZ = wheelEntity.GetPosition();
640 entPosXZ[1] = 0;
642 return wheelEntity;
643 }
644 }
645
646 return null;
647 }
648
650 {
651 //Array used to find all relevant information about currently equipped clothes
653
654 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("LEGS")));
655 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BACK")));
656 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("VEST")));
657 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("HeadGear")));
658 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("Mask")));
659 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BODY")));
660 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("FEET")));
661 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("GLOVES")));
662
663 //Damage all currently equipped clothes
664 for (int i = 0; i < equippedClothes.Count(); i++)
665 {
666 //If no item is equipped on slot, slot is ignored
667 if (equippedClothes[i] == null)
668 continue;
669
670 equippedClothes[i].DecreaseHealth(m_ClothingDmg[i], false);
671 }
672 }
673}
ActionActivateTrapCB ActionContinuousBaseCB ActionActivateTrap()
void AddAction(typename actionName)
override void OnVariablesSynchronized()
vector GetOrientation()
override void EEItemDetached(EntityAI item, string slot_name)
override void EEItemAttached(EntityAI item, string slot_name)
const int ECE_LOCAL
const int ECE_CREATEPHYSICS
PlayerSpawnPreset slotName
void Disarm()
Definition ClockBase.c:191
override void EEDelete(EntityAI parent)
ref Timer m_Timer
Definition DayZGame.c:675
void OnRPC(ParamsReadContext ctx)
ERPCs
Definition ERPCs.c:2
const int MAX
Definition EnConvert.c:27
override bool CanPutInCargo(EntityAI parent)
override bool CanPutIntoHands(EntityAI parent)
override bool IsTakeable()
override bool CanRemoveFromHands(EntityAI parent)
bool CanPlayDeployLoopSound()
Definition ItemBase.c:9039
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition ItemBase.c:5867
void PlayDeploySound()
Definition ItemBase.c:9003
bool IsDeploySound()
Definition ItemBase.c:8977
bool m_IsActive
bool IsActive()
void OnStoreSave(ParamsWriteContext ctx)
bool OnStoreLoad(ParamsReadContext ctx, int version)
ref Timer m_UpdateTimer
Definition RadialMenu.c:20
string m_AnimationPhaseTriggered
Definition TrapBase.c:34
float m_DefectRate
Definition TrapBase.c:19
void DeferredEnableTrigger()
Definition TrapBase.c:490
string m_InfoDamageManipulation
Definition TrapBase.c:38
bool m_WasActivatedOrDeactivated
DEPRECATED Used for explosive traps to prevent detonation after destroying through disarm action.
Definition TrapBase.c:30
ref array< int > m_ClothingDmg
Definition TrapBase.c:46
bool IsActivable()
Definition TrapBase.c:178
TrapTrigger GetTrapTrigger()
Definition TrapBase.c:88
enum SoundTypeTrap SPAWN_FLAGS
void PlayDeployLoopSound()
void StopDeployLoopSound()
void Synch(EntityAI victim)
keeping "step" here for consistency only
Definition TrapBase.c:280
string m_AnimationPhaseGrounded
Definition TrapBase.c:32
const float UPDATE_TIMER_INTERVAL
Definition TrapBase.c:15
override bool CanBePlaced(Man player, vector position)
Definition TrapBase.c:567
void AddDefect()
Definition TrapBase.c:403
void RefreshState()
Definition TrapBase.c:336
void RemoveFromObject(EntityAI victim)
Definition TrapBase.c:267
void SetActive()
Definition TrapBase.c:409
bool m_NeedActivation
Definition TrapBase.c:18
void SetupTrap()
Definition TrapBase.c:376
ref EffectSound m_DeployLoopSound
Definition TrapBase.c:47
const int DAMAGE_TRIGGER_MINE
Definition TrapBase.c:14
bool m_Disarmed
Definition TrapBase.c:28
EntityAI GetClosestCarWheel(EntityAI victim)
Definition TrapBase.c:612
string m_InfoActivationTime
Definition TrapBase.c:40
string m_AnimationPhaseSet
Definition TrapBase.c:33
bool m_IsInProgress
Definition TrapBase.c:26
bool IsInactive()
Definition TrapBase.c:164
void SnapOnObject(EntityAI victim)
Definition TrapBase.c:221
string m_InfoSetup
Definition TrapBase.c:36
void SetInactive(bool stop_timer=true)
Definition TrapBase.c:452
bool IsPlaceable()
Definition TrapBase.c:183
float m_InitWaitTime
Definition TrapBase.c:17
void DamageClothing(PlayerBase player)
Definition TrapBase.c:649
SoundTypeTrap
Definition TrapBase.c:2
@ ACTIVATING
Definition TrapBase.c:3
bool CanBeClapped()
DEPRECATED Set if trap can be disarmed using ActionClapBearTrapWithThisItem.
Definition TrapBase.c:573
TrapTrigger m_TrapTrigger
Definition TrapBase.c:44
string m_InfoDeactivated
Definition TrapBase.c:37
void SetupTrapPlayer(PlayerBase player, bool set_position=true)
Definition TrapBase.c:385
void SetDisarmed(bool disarmed)
DEPRECATED.
Definition TrapBase.c:585
bool GetDisarmed()
DEPRECATED.
Definition TrapBase.c:591
float m_DamagePlayers
Definition TrapBase.c:20
void TrapBase()
Definition TrapBase.c:49
bool IsPlaceableAtPosition(vector position)
Definition TrapBase.c:199
void StartActivate(PlayerBase player)
Definition TrapBase.c:431
string m_InfoDamage
Definition TrapBase.c:39
bool m_AddActivationDefect
Definition TrapBase.c:23
void DeleteTrigger()
Definition TrapBase.c:481
void ~TrapBase()
Definition TrapBase.c:81
bool m_AddDeactivationDefect
Definition TrapBase.c:24
float m_DamageOthers
Definition TrapBase.c:21
void StartDeactivate(PlayerBase player)
class JsonUndergroundAreaTriggerData GetPosition
Super root of all classes in Enforce script.
Definition EnScript.c:11
do not process rotations !
Definition DayZAnimal.c:573
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
script counterpart to engine's class Inventory
Definition Inventory.c:79
Definition EnMath.c:7
The class that will be instanced (moddable)
Definition gameplay.c:376
Manager class for managing Effect (EffectParticle, EffectSound)
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
override void OnSteppedOn(EntityAI victim)
Definition Trap_Bear.c:63
override void CreateTrigger()
Definition Trap_Bear.c:41
override void OnActivate()
Definition Trap_Bear.c:198
override void OnDisarm()
Definition Trap_Bear.c:205
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
Definition Trap_Bear.c:216
override bool CanBeDisarmed()
Definition Trap_Bear.c:25
override void SetActions()
Definition Trap_Bear.c:237
override void OnSteppedOut(EntityAI victim)
Definition Trap_Bear.c:119
Trigger used by traps.
Definition TrapTrigger.c:3
void SetEnabled()
prevents insider adding in the wrong position, HOTFIX
Definition TrapTrigger.c:46
void SetParentObject(TrapBase obj)
Definition TrapTrigger.c:18
void SetExtents(vector mins, vector maxs)
Set the size of the Trigger, avoid using SetCollisionBox directly.
Definition Trigger.c:116
proto string ToString()
static proto native float DistanceSq(vector v1, vector v2)
Returns the square distance between tips of two 3D vectors.
static proto native float Distance(vector v1, vector v2)
Returns the distance between tips of two 3D vectors.
proto native CGame GetGame()
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition Effect.c:420
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
static proto float SqrFloat(float f)
Returns squared value.
static proto float Clamp(float value, float min, float max)
Clamps 'value' to 'min' if it is lower than 'min', or to 'max' if it is higher than 'max'.
proto native void OnUpdate()
Definition tools.c:333
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8