Dayz Explorer 1.29.162510
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 (g_Game.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 (g_Game && m_TrapTrigger)
105 {
106 g_Game.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().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 ( g_Game.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 g_Game.RPCSingleParam(this, ERPCs.RPC_TRAP_DISARM, null, true);
209
210 OnDisarm();
211 }
212
214 void OnDisarm();
215
217 {
218 if ( g_Game.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 g_Game.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
274 {
275 SetInactive(false);
276 }
277
278 void OnSteppedOut(EntityAI victim);
279
280 // Synchronizes states
281 protected void Synch(EntityAI victim)
282 {
283 if (g_Game.IsServer())
284 {
285 if (victim && !victim.GetAllowDamage())
286 return;
287
288 Param1<EntityAI> p = new Param1<EntityAI>(victim);
289 g_Game.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 ( !g_Game.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 }
319 }
320
322 {
324 {
325 return;
326 }
327
328 if ( g_Game.IsServer() )
329 {
330 // item is owned by player
331 if ( GetHierarchyRootPlayer() != NULL && m_AnimationPhaseGrounded != "" )
332 {
333 SetAnimationPhase( m_AnimationPhaseSet, 1 );
335 {
336 SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
337 }
338 SetAnimationPhase( m_AnimationPhaseGrounded, 0 );
339 }
340 // item is set active
341 else if ( IsActive() )
342 {
343 if ( m_AnimationPhaseGrounded != "" )
344 {
345 SetAnimationPhase( m_AnimationPhaseGrounded, 1 );
346 }
348 {
349 SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
350 SetAnimationPhase( m_AnimationPhaseSet, 0 );
351 }
352 }
353 // item is inactive and not owned by player (on the ground)
354 else if ( IsInactive() )
355 {
357 {
358 SetAnimationPhase( m_AnimationPhaseGrounded, 1 );
359 }
361 {
362 SetAnimationPhase( m_AnimationPhaseSet, 1 );
363 SetAnimationPhase( m_AnimationPhaseTriggered, 0 );
364 }
365 }
366 }
367 }
368
370 {
371 if (g_Game.IsServer())
372 {
373 if (GetHierarchyRootPlayer() && GetHierarchyRootPlayer().CanDropEntity(this))
374 SetupTrapPlayer(PlayerBase.Cast(GetHierarchyRootPlayer()));
375 }
376 }
377
378 void SetupTrapPlayer( PlayerBase player, bool set_position = true )
379 {
380 if (g_Game.IsServer())
381 {
382 if (set_position)
383 {
384 player.LocalDropEntity(this);
385
386 vector trapPos = player.GetDirection() * 1.5;
387 trapPos[1] = 0;
388 SetPosition(player.GetPosition() + trapPos);
389 }
390
391 if (m_NeedActivation == false)
392 SetActive();
393 }
394 }
395
397 {
398 if ( g_Game.IsServer() )
399 {
400 DecreaseHealth( "", "", m_DefectRate );
401 }
402 }
403
405 {
407
408 m_IsInProgress = false;
409 m_IsActive = true;
410
412 {
413 AddDefect();
414 }
415
416 if (g_Game.IsServer())
417 {
419 RefreshState();
420 SetSynchDirty();
421 }
422
423 OnActivate();
424 }
425
426 void OnActivate();
427
429 {
430 if (g_Game.IsServer())
431 {
433 HideSelection("safety_pin");
434
435 if (m_InitWaitTime > 0)
436 {
437 m_IsInProgress = true;
438 m_Timer.Run(m_InitWaitTime, this, "SetActive");
439
440 SetSynchDirty();
441 }
442 else
443 SetActive();
444 }
445 }
446
447 void StartDeactivate(PlayerBase player);
448
449 void SetInactive(bool stop_timer = true)
450 {
452
453 m_IsActive = false;
454 if (m_Timer && stop_timer)
455 m_Timer.Stop();
456
458 AddDefect();
459
460 SetSynchDirty();
462 RefreshState();
463 }
464
466 {
467 if (Class.CastTo(m_TrapTrigger, g_Game.CreateObjectEx("TrapTrigger", GetPosition(), SPAWN_FLAGS)))
468 {
469 vector mins = "-0.01 -0.05 -0.01";
470 vector maxs = "0.01 0.5 0.01";
471 m_TrapTrigger.SetOrientation(GetOrientation());
472 m_TrapTrigger.SetExtents(mins, maxs);
473 m_TrapTrigger.SetParentObject(this);
475 }
476 }
477
479 {
480 if (m_TrapTrigger)
481 {
482 m_TrapTrigger.SetParentObject(null);
483 m_TrapTrigger.DeleteSafe();
484 }
485 }
486
488 {
489 if (m_TrapTrigger)
490 m_TrapTrigger.SetEnabled();
491 }
492
493 override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
494 {
495 super.OnItemLocationChanged(old_owner, new_owner);
496
497 if (g_Game.IsServer())
498 {
499 RefreshState();
500
501 // TAKE ACTIVE TRAP FROM VICINITY
502 if (old_owner == NULL && new_owner != NULL && IsActive())
503 {
504 // TAKE INTO HANDS
505 if ( new_owner.IsPlayer() )
506 {
507 SnapOnObject(new_owner);
508 }
509 else if (new_owner.GetHierarchyRootPlayer())
510 {
511 SnapOnObject(new_owner.GetHierarchyRootPlayer());
512 }
513 }
514 }
515
516 }
517
518 override void EEItemAttached(EntityAI item, string slot_name)
519 {
520 super.EEItemAttached(item, slot_name);
521
522 if (g_Game.IsServer())
523 RefreshState();
524 }
525
526 override void EEItemDetached(EntityAI item, string slot_name)
527 {
528 super.EEItemDetached(item, slot_name);
529
530 if (g_Game.IsServer())
531 RefreshState();
532 }
533
534 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
535 {
536 super.OnPlacementComplete(player, position, orientation);
537
538 if (g_Game.IsServer())
539 {
540 SetOrientation(orientation);
541 SetPosition(position);
542 PlaceOnSurface();
543 SetSynchDirty();
544 }
545 }
546
547 override bool CanPutInCargo( EntityAI parent )
548 {
549 if ( !super.CanPutInCargo(parent) )
550 {
551 return false;
552 }
553
554 return IsTakeable();
555 }
556
557 override bool CanPutIntoHands( EntityAI parent )
558 {
559 if ( !super.CanPutIntoHands( parent ) )
560 {
561 return false;
562 }
563
564 return IsTakeable();
565 }
566
567 override bool CanRemoveFromHands( EntityAI parent )
568 {
569 return IsTakeable();
570 }
571
572 override bool CanBePlaced( Man player, vector position )
573 {
574 return IsPlaceableAtPosition( position );
575 }
576
579 {
580 return true;
581 }
582
583 //Set if trap can be disarmed using trap-specific action
585 {
586 return false;
587 }
588
590 void SetDisarmed( bool disarmed )
591 {
592 m_Disarmed = disarmed;
593 }
594
597 {
598 return m_Disarmed;
599 }
600
601 //================================================================
602 // ADVANCED PLACEMENT
603 //================================================================
604
605 override void SetActions()
606 {
607 super.SetActions();
608
610 }
611
612 // HELPERS
614 {
616 vector trapPosXZ = GetPosition();
617 trapPosXZ[1] = 0;
618
619 GameInventory inv = victim.GetInventory();
620 for (int i = 0; i < inv.AttachmentCount(); i++)
621 {
623 EntityAI wheelEntity = inv.GetAttachmentFromIndex(i);
624 if (wheelEntity && wheelEntity.Type() == CarWheel_Ruined)
625 {
626 continue;
627 }
628
630 int slotId;
631 string slotName;
632 wheelEntity.GetInventory().GetCurrentAttachmentSlotInfo(slotId, slotName);
633 slotName.ToLower();
634 if (slotName.Contains("spare"))
635 {
636 continue
637 }
638
640 if (wheelEntity && wheelEntity.IsInherited(CarWheel))
641 {
642 vector entPosXZ = wheelEntity.GetPosition();
643 entPosXZ[1] = 0;
644 if (vector.Distance(trapPosXZ, entPosXZ) < 1)
645 {
646 return wheelEntity;
647 }
648 }
649 }
650
651 return null;
652 }
653
654 protected void DamageClothing(PlayerBase player)
655 {
656 //Array used to find all relevant information about currently equipped clothes
657 array<ClothingBase> equippedClothes = new array<ClothingBase>;
658
659 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("LEGS")));
660 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BACK")));
661 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("VEST")));
662 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("HeadGear")));
663 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("Mask")));
664 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("BODY")));
665 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("FEET")));
666 equippedClothes.Insert(ClothingBase.Cast(player.GetItemOnSlot("GLOVES")));
667
668 //Damage all currently equipped clothes
669 for (int i = 0; i < equippedClothes.Count(); i++)
670 {
671 //If no item is equipped on slot, slot is ignored
672 if (equippedClothes[i] == null)
673 {
674 continue;
675 }
676
677 equippedClothes[i].DecreaseHealth(m_ClothingDmg[i], false);
678 }
679 }
680
682 protected ref EffectSound m_DeployLoopSound; //DEPRECATED in favor of m_DeployLoopSoundEx
683
686}
class LogManager EntityAI
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:656
Wrapper class for managing sound through SEffectManager.
Definition effectsound.c:5
script counterpart to engine's class Inventory
Definition inventory.c:81
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
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
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:3942
ref Timer m_Timer
Definition dayzgame.c:707
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()
bool CanBeDisarmed()
override bool CanRemoveFromHands(EntityAI parent)
override bool CanBePlaced(Man player, vector position)
Serializer ParamsReadContext
Definition gameplay.c:15
Serializer ParamsWriteContext
Definition gameplay.c:16
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition effect.c:463
vector GetPosition()
Get the world position of the Effect.
Definition effect.c:473
void OnUpdate()
Definition tools.c:349
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:487
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 CreateTrigger()
Definition trapbase.c:465
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:396
void RefreshState()
Definition trapbase.c:321
void OnSteppedOut(EntityAI victim)
void RemoveFromObject(EntityAI victim)
Definition trapbase.c:268
void SetActive()
Definition trapbase.c:404
bool m_NeedActivation
Definition trapbase.c:18
void SetupTrap()
Definition trapbase.c:369
void OnSteppedOn(EntityAI victim)
Definition trapbase.c:273
ref EffectSound m_DeployLoopSound
DEPRECATED.
Definition trapbase.c:682
const int DAMAGE_TRIGGER_MINE
Definition trapbase.c:14
bool m_Disarmed
Definition trapbase.c:28
EntityAI GetClosestCarWheel(EntityAI victim)
Definition trapbase.c:613
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:449
bool IsPlaceable()
Definition trapbase.c:172
float m_InitWaitTime
Definition trapbase.c:17
void DamageClothing(PlayerBase player)
Definition trapbase.c:654
SoundTypeTrap
Definition trapbase.c:2
@ ACTIVATING
Definition trapbase.c:3
bool CanBeClapped()
DEPRECATED Set if trap can be disarmed using ActionClapBearTrapWithThisItem.
Definition trapbase.c:578
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:378
void SetDisarmed(bool disarmed)
DEPRECATED.
Definition trapbase.c:590
bool GetDisarmed()
DEPRECATED.
Definition trapbase.c:596
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:428
string m_InfoDamage
Definition trapbase.c:39
bool m_AddActivationDefect
Definition trapbase.c:23
void DeleteTrigger()
Definition trapbase.c:478
bool m_AddDeactivationDefect
Definition trapbase.c:24
float m_DamageOthers
Definition trapbase.c:21
void StartDeactivate(PlayerBase player)