Dayz Explorer 1.28.160049
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;
43 protected ref Timer m_UpdateTimer;
45
47
48 void TrapBase()
49 {
50 m_IsInProgress = false;
51 m_NeedActivation = true;
52 m_InitWaitTime = 5; //After this time after deployment, the trap is activated
53 m_DefectRate = 15; //Added damage after trap activation
54 m_DamagePlayers = 25; //How much damage player gets when caught
55 m_DamageOthers = 100; //How much damage others gets when caught
56
59
61
65
66 m_InfoSetup = "#STR_TrapBase0";
67 m_InfoDeactivated = "#STR_TrapBase1";
68 m_InfoDamageManipulation = "#STR_TrapBase2";
69 m_InfoDamage = "#STR_TrapBase3";
70 m_InfoActivationTime = "#STR_TrapBase4" + m_InitWaitTime.ToString() + "#STR_TrapBase5";
71
72 m_UpdateTimer = new Timer();
73
74 RegisterNetSyncVariableBool("m_IsActive");
75 RegisterNetSyncVariableBool("m_IsInProgress");
76 }
77
78 void OnUpdate(EntityAI victim);
79
84
87 {
88 super.OnVariablesSynchronized();
89
90 if (GetGame().IsMultiplayer())
91 {
93 SetActive();
94
96 StartActivate(null);
97 }
98 }
99
100 override void EEDelete(EntityAI parent)
101 {
102 super.EEDelete(parent);
103
104 if (GetGame() && m_TrapTrigger)
105 {
106 GetGame().ObjectDelete(m_TrapTrigger);
107 m_TrapTrigger = null;
108 }
109 }
110
112 {
113 super.OnStoreSave(ctx);
114
115 ctx.Write(m_IsActive);
116 ctx.Write(m_IsInProgress);
117 }
118
119 //----------------------------------------------------------------
120 override bool OnStoreLoad(ParamsReadContext ctx, int version)
121 {
122 if ( !super.OnStoreLoad(ctx, version) )
123 return false;
124
125 bool b_is_active = false;
126 if ( !ctx.Read( b_is_active ) )
127 b_is_active = false;
128
129 bool b_is_in_progress = false;
130 if ( !ctx.Read( b_is_in_progress ) )
131 b_is_in_progress = false;
132
133 if ( b_is_active )
134 {
135 SetActive();
136 }
137
138 if (b_is_in_progress && !b_is_active)
139 {
140 StartActivate(null);
141 }
142
143 return true;
144 }
145
146 bool IsActive()
147 {
148 return m_IsActive && m_IsInProgress == false && GetHierarchyRootPlayer() == null;
149 }
150
152 {
153 return !IsActive() && m_IsInProgress == false && GetHierarchyRootPlayer() == null;
154 }
155
156 // trap cannot be taken when is activated
157 override bool IsTakeable()
158 {
159 if ( m_IsInProgress == false && !IsActive() )
160 {
161 return true;
162 }
163
164 return false;
165 }
166
168 {
169 return !IsActive() && GetHierarchyRootPlayer() == null && GetHierarchyParent() == null && m_IsInProgress == false && !IsRuined() && m_NeedActivation;
170 }
171
173 {
174 if ( GetHierarchyRootPlayer() != null && GetHierarchyRootPlayer().GetHumanInventory().GetEntityInHands() == this )
175 {
176 PlayerBase player = PlayerBase.Cast( GetHierarchyRootPlayer() );
177
178 vector player_pos = player.GetPosition();
179 vector aim_pos = player.GetAimPosition();
180
181 if ( vector.DistanceSq( player_pos, aim_pos ) <= ( Math.SqrFloat( 1.5 ) ) )
182 {
183 return IsPlaceableAtPosition( aim_pos );
184 }
185 }
186
187 return false;
188 }
189
191 {
192 if ( position[1] < g_Game.SurfaceGetSeaLevelMax() + 0.03 )
193 {
194 return false;
195 }
196 else if ( GetGame().SurfaceIsPond( position[0], position[2] ) )
197 {
198 return false;
199 }
200
201 return true;
202 }
203
204 void Disarm()
205 {
206 SetInactive(false);
207 RefreshState();
208 GetGame().RPCSingleParam(this, ERPCs.RPC_TRAP_DISARM, null, true);
209
210 OnDisarm();
211 }
212
214 void OnDisarm();
215
217 {
218 if ( GetGame().IsServer() )
219 {
220 if ( m_Timer )
221 {
222 m_Timer.Stop();
223 }
224
225 RefreshState();
226
227 if (m_DamagePlayers > 0)
228 {
229 if (victim)
230 {
231 if ( victim.IsInherited(SurvivorBase))
232 {
233 victim.DecreaseHealth("", "", m_DamagePlayers);
234 }
235 else if (victim.IsInherited(DayZCreatureAI))
236 {
237 victim.DecreaseHealth("", "", m_DamageOthers);
238 }
239 else if (victim.IsInherited(ItemBase))
240 {
241 ItemBase victim_item = ItemBase.Cast(victim);
242 float damage_coef = 1;
243
244 if (victim_item.HasQuantity() && victim_item.GetQuantityMax() != 0 && victim_item.GetQuantity() > 0)
245 {
246 damage_coef = victim_item.GetQuantityMax() / victim_item.GetQuantity(); // Lower quantity increases damage exposure
247 }
248
249 if (damage_coef > 0)
250 {
251 int item_size_x = 1;
252 int item_size_y = 1;
253 GetGame().GetInventoryItemSize(victim_item, item_size_x, item_size_y);
254
255 float add_damage = 300 * damage_coef / Math.Clamp(item_size_x * item_size_y, 1, int.MAX);
256 victim_item.DecreaseHealth("", "", add_damage);
257 }
258 }
259 }
260 }
261
262 Synch(victim);
263 }
264
265 OnSteppedOn(victim);
266 }
267
269 {
270 OnSteppedOut(victim);
271 }
272
273 void OnSteppedOn(EntityAI victim)
274 {
275 SetInactive(false);
276 }
277
278 void OnSteppedOut(EntityAI victim);
279
280 // Synchronizes states
281 protected void Synch(EntityAI victim)
282 {
283 if (GetGame().IsServer())
284 {
285 if (victim && !victim.GetAllowDamage())
286 return;
287
288 Param1<EntityAI> p = new Param1<EntityAI>(victim);
289 GetGame().RPCSingleParam(this, ERPCs.RPC_TRAP_VICTIM, p, true);
290 }
291
292 }
293
294 // On server -> client synchronization
295 override void OnRPC(PlayerIdentity sender, int rpc_type, ParamsReadContext ctx)
296 {
297 super.OnRPC(sender, rpc_type, ctx);
298
299 if ( !GetGame().IsDedicatedServer() )
300 {
301 switch (rpc_type)
302 {
303 case ERPCs.RPC_TRAP_VICTIM:
304 Param1<EntityAI> victim = new Param1<EntityAI>(null);
305
306 if (ctx.Read(victim))
307 {
308 if (victim.param1)
309 SnapOnObject(victim.param1);
310 }
311
312 break;
313
314 case ERPCs.RPC_TRAP_DISARM:
315 OnDisarm();
316 break;
317
318 case SoundTypeTrap.ACTIVATING:
319
320 Param1<bool> p = new Param1<bool>(false);
321
322 bool isActivating = false;
323 if (ctx.Read(p))
324 isActivating = p.param1;
325
326 break;
327 }
328 }
329 }
330
332 {
334 {
335 return;
336 }
337
338 if ( GetGame().IsServer() )
339 {
340 // item is owned by player
341 if ( GetHierarchyRootPlayer() != NULL && m_AnimationPhaseGrounded != "" )
342 {
343 SetAnimationPhase( m_AnimationPhaseSet, 1 );
345 {
346 SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
347 }
348 SetAnimationPhase( m_AnimationPhaseGrounded, 0 );
349 }
350 // item is set active
351 else if ( IsActive() )
352 {
353 if ( m_AnimationPhaseGrounded != "" )
354 {
355 SetAnimationPhase( m_AnimationPhaseGrounded, 1 );
356 }
358 {
359 SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
360 SetAnimationPhase( m_AnimationPhaseSet, 0 );
361 }
362 }
363 // item is inactive and not owned by player (on the ground)
364 else if ( IsInactive() )
365 {
367 {
368 SetAnimationPhase( m_AnimationPhaseGrounded, 1 );
369 }
371 {
372 SetAnimationPhase( m_AnimationPhaseSet, 1 );
373 SetAnimationPhase( m_AnimationPhaseTriggered, 0 );
374 }
375 }
376 }
377 }
378
380 {
381 if (GetGame().IsServer())
382 {
383 if (GetHierarchyRootPlayer() && GetHierarchyRootPlayer().CanDropEntity(this))
384 SetupTrapPlayer(PlayerBase.Cast(GetHierarchyRootPlayer()));
385 }
386 }
387
388 void SetupTrapPlayer( PlayerBase player, bool set_position = true )
389 {
390 if (GetGame().IsServer())
391 {
392 if (set_position)
393 {
394 player.LocalDropEntity(this);
395
396 vector trapPos = player.GetDirection() * 1.5;
397 trapPos[1] = 0;
398 SetPosition(player.GetPosition() + trapPos);
399 }
400
401 if (m_NeedActivation == false)
402 SetActive();
403 }
404 }
405
407 {
408 if ( GetGame().IsServer() )
409 {
410 DecreaseHealth( "", "", m_DefectRate );
411 }
412 }
413
415 {
417
418 m_IsInProgress = false;
419 m_IsActive = true;
420
422 {
423 AddDefect();
424 }
425
426 if (GetGame().IsServer())
427 {
428 CreateTrigger();
429 RefreshState();
430 SetSynchDirty();
431 }
432
433 OnActivate();
434 }
435
436 void OnActivate();
437
439 {
440 if (GetGame().IsServer())
441 {
443 HideSelection("safety_pin");
444
445 if (m_InitWaitTime > 0)
446 {
447 m_IsInProgress = true;
448 m_Timer.Run(m_InitWaitTime, this, "SetActive");
449
450 SetSynchDirty();
451 }
452 else
453 SetActive();
454 }
455 }
456
457 void StartDeactivate(PlayerBase player);
458
459 void SetInactive(bool stop_timer = true)
460 {
462
463 m_IsActive = false;
464 if (m_Timer && stop_timer)
465 m_Timer.Stop();
466
468 AddDefect();
469
470 SetSynchDirty();
472 RefreshState();
473 }
474
475 void CreateTrigger()
476 {
477 if (Class.CastTo(m_TrapTrigger, GetGame().CreateObjectEx("TrapTrigger", GetPosition(), SPAWN_FLAGS)))
478 {
479 vector mins = "-0.01 -0.05 -0.01";
480 vector maxs = "0.01 0.5 0.01";
481 m_TrapTrigger.SetOrientation(GetOrientation());
482 m_TrapTrigger.SetExtents(mins, maxs);
483 m_TrapTrigger.SetParentObject(this);
485 }
486 }
487
489 {
490 if (m_TrapTrigger)
491 {
492 m_TrapTrigger.SetParentObject(null);
493 m_TrapTrigger.DeleteSafe();
494 }
495 }
496
502
503 override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
504 {
505 super.OnItemLocationChanged(old_owner, new_owner);
506
507 if (GetGame().IsServer())
508 {
509 RefreshState();
510
511 // TAKE ACTIVE TRAP FROM VICINITY
512 if (old_owner == NULL && new_owner != NULL && IsActive())
513 {
514 // TAKE INTO HANDS
515 if ( new_owner.IsPlayer() )
516 {
517 SnapOnObject(new_owner);
518 }
519 else if (new_owner.GetHierarchyRootPlayer())
520 {
521 SnapOnObject(new_owner.GetHierarchyRootPlayer());
522 }
523 }
524 }
525
526 }
527
528 override void EEItemAttached(EntityAI item, string slot_name)
529 {
530 super.EEItemAttached(item, slot_name);
531
532 if (GetGame().IsServer())
533 RefreshState();
534 }
535
536 override void EEItemDetached(EntityAI item, string slot_name)
537 {
538 super.EEItemDetached(item, slot_name);
539
540 if (GetGame().IsServer())
541 RefreshState();
542 }
543
544 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
545 {
546 super.OnPlacementComplete(player, position, orientation);
547
548 if (GetGame().IsServer())
549 {
550 SetOrientation(orientation);
551 SetPosition(position);
552 PlaceOnSurface();
553 SetSynchDirty();
554 }
555 }
556
557 override bool CanPutInCargo( EntityAI parent )
558 {
559 if ( !super.CanPutInCargo(parent) )
560 {
561 return false;
562 }
563
564 return IsTakeable();
565 }
566
567 override bool CanPutIntoHands( EntityAI parent )
568 {
569 if ( !super.CanPutIntoHands( parent ) )
570 {
571 return false;
572 }
573
574 return IsTakeable();
575 }
576
577 override bool CanRemoveFromHands( EntityAI parent )
578 {
579 return IsTakeable();
580 }
581
582 override bool CanBePlaced( Man player, vector position )
583 {
584 return IsPlaceableAtPosition( position );
585 }
586
589 {
590 return true;
591 }
592
593 //Set if trap can be disarmed using trap-specific action
594 bool CanBeDisarmed()
595 {
596 return false;
597 }
598
600 void SetDisarmed( bool disarmed )
601 {
602 m_Disarmed = disarmed;
603 }
604
607 {
608 return m_Disarmed;
609 }
610
611 //================================================================
612 // ADVANCED PLACEMENT
613 //================================================================
614
615 override void SetActions()
616 {
617 super.SetActions();
618
620 }
621
622 // HELPERS
624 {
626 vector trapPosXZ = GetPosition();
627 trapPosXZ[1] = 0;
628
629 GameInventory inv = victim.GetInventory();
630 for (int i = 0; i < inv.AttachmentCount(); i++)
631 {
633 EntityAI wheelEntity = inv.GetAttachmentFromIndex(i);
634 if (wheelEntity && wheelEntity.Type() == CarWheel_Ruined)
635 {
636 continue;
637 }
638
640 int slotId;
641 string slotName;
642 wheelEntity.GetInventory().GetCurrentAttachmentSlotInfo(slotId, slotName);
643 slotName.ToLower();
644 if (slotName.Contains("spare"))
645 {
646 continue
647 }
648
650 if (wheelEntity && wheelEntity.IsInherited(CarWheel))
651 {
652 vector entPosXZ = wheelEntity.GetPosition();
653 entPosXZ[1] = 0;
654 if (vector.Distance(trapPosXZ, entPosXZ) < 1)
655 {
656 return wheelEntity;
657 }
658 }
659 }
660
661 return null;
662 }
663
664 protected void DamageClothing(PlayerBase player)
665 {
666 //Array used to find all relevant information about currently equipped clothes
667 array<ClothingBase> equippedClothes = new array<ClothingBase>;
668
669 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("LEGS")));
670 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BACK")));
671 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("VEST")));
672 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("HeadGear")));
673 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("Mask")));
674 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BODY")));
675 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("FEET")));
676 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("GLOVES")));
677
678 //Damage all currently equipped clothes
679 for (int i = 0; i < equippedClothes.Count(); i++)
680 {
681 //If no item is equipped on slot, slot is ignored
682 if (equippedClothes[i] == null)
683 {
684 continue;
685 }
686
687 equippedClothes[i].DecreaseHealth(m_ClothingDmg[i], false);
688 }
689 }
690
692 protected ref EffectSound m_DeployLoopSound; //DEPRECATED in favor of m_DeployLoopSoundEx
693
696}
ActionActivateTrapCB ActionContinuousBaseCB ActionActivateTrap()
void AddAction(typename actionName)
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
Super root of all classes in Enforce script.
Definition enscript.c:11
do not process rotations !
Definition dayzanimal.c:654
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
script counterpart to engine's class Inventory
Definition inventory.c:79
proto native EntityAI GetAttachmentFromIndex(int index)
proto native int AttachmentCount()
Returns count of attachments attached to this item.
Definition enmath.c:7
The class that will be instanced (moddable)
Definition gameplay.c:389
Serialization general interface. Serializer API works with:
Definition serializer.c:56
override void OnActivate()
Definition trap_bear.c:210
override void OnDisarm()
Definition trap_bear.c:217
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
Definition trap_bear.c:228
override void SetActions()
Definition trap_bear.c:249
Trigger used by traps.
Definition traptrigger.c:3
void SetEnabled()
prevents insider adding in the wrong position, HOTFIX
Definition traptrigger.c:52
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
void Disarm()
Definition clockbase.c:199
override void EEDelete(EntityAI parent)
override void OnVariablesSynchronized()
DayZGame g_Game
Definition dayzgame.c:3868
ref Timer m_Timer
Definition dayzgame.c:705
const int MAX
Definition enconvert.c:27
ERPCs
Definition erpcs.c:2
override bool CanPutInCargo(EntityAI parent)
override bool CanPutIntoHands(EntityAI parent)
override bool IsTakeable()
override bool CanRemoveFromHands(EntityAI parent)
override bool CanBePlaced(Man player, vector position)
proto native CGame GetGame()
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition effect.c:463
class JsonUndergroundAreaTriggerData GetPosition
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8
bool m_IsActive
bool IsActive()
void OnStoreSave(ParamsWriteContext ctx)
bool OnStoreLoad(ParamsReadContext ctx, int version)
override void OnRPC(ParamsReadContext ctx)
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:497
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:167
TrapTrigger GetTrapTrigger()
Definition trapbase.c:80
enum SoundTypeTrap SPAWN_FLAGS
void PlayDeployLoopSound()
void StopDeployLoopSound()
DEPRECATED.
void Synch(EntityAI victim)
keeping "step" here for consistency only
Definition trapbase.c:281
string m_AnimationPhaseGrounded
Definition trapbase.c:32
const float UPDATE_TIMER_INTERVAL
Definition trapbase.c:15
void AddDefect()
Definition trapbase.c:406
void RefreshState()
Definition trapbase.c:331
void RemoveFromObject(EntityAI victim)
Definition trapbase.c:268
void SetActive()
Definition trapbase.c:414
bool m_NeedActivation
Definition trapbase.c:18
void SetupTrap()
Definition trapbase.c:379
ref EffectSound m_DeployLoopSound
DEPRECATED.
Definition trapbase.c:692
const int DAMAGE_TRIGGER_MINE
Definition trapbase.c:14
bool m_Disarmed
Definition trapbase.c:28
EntityAI GetClosestCarWheel(EntityAI victim)
Definition trapbase.c:623
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:151
void SnapOnObject(EntityAI victim)
Definition trapbase.c:216
string m_InfoSetup
Definition trapbase.c:36
void SetInactive(bool stop_timer=true)
Definition trapbase.c:459
bool IsPlaceable()
Definition trapbase.c:172
float m_InitWaitTime
Definition trapbase.c:17
void DamageClothing(PlayerBase player)
Definition trapbase.c:664
SoundTypeTrap
Definition trapbase.c:2
@ ACTIVATING
Definition trapbase.c:3
bool CanBeClapped()
DEPRECATED Set if trap can be disarmed using ActionClapBearTrapWithThisItem.
Definition trapbase.c:588
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:388
void SetDisarmed(bool disarmed)
DEPRECATED.
Definition trapbase.c:600
bool GetDisarmed()
DEPRECATED.
Definition trapbase.c:606
float m_DamagePlayers
Definition trapbase.c:20
void TrapBase()
Definition trapbase.c:48
bool IsPlaceableAtPosition(vector position)
Definition trapbase.c:190
void StartActivate(PlayerBase player)
Definition trapbase.c:438
string m_InfoDamage
Definition trapbase.c:39
bool m_AddActivationDefect
Definition trapbase.c:23
void DeleteTrigger()
Definition trapbase.c:488
bool m_AddDeactivationDefect
Definition trapbase.c:24
float m_DamageOthers
Definition trapbase.c:21
void StartDeactivate(PlayerBase player)