Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
trapspawnbase.c
Go to the documentation of this file.
1 class TrapSpawnBase extends ItemBase
2 {
3  int m_InitWaitTime; //After this time after deployment, the trap is activated
4  int m_UpdateWaitTime; //Time when timer will be called again until success or not succes catch is done
5  float m_DefectRate; //Added damage after trap activation
6  bool m_IsFoldable;
7  bool m_CanCatch = false;
8  float m_MinimalDistanceFromPlayersToCatch;
9 
10  float m_BaitCatchProb; // The probability (0-100) to catch something when bait is attached
11  float m_NoBaitCatchProb; // The probability (0-100) to catch something when no bait is attached
12  float m_FinalCatchProb; // The selected catch probability (0-100) -> Will be overriden no need to allocate
13 
14  vector m_PreyPos; // The position where prey will be spawned -> Will be overriden later
15 
16  protected EntityAI m_Bait;
17 
18  protected bool m_IsActive;
19  protected bool m_IsDeployed;
20  protected bool m_IsInProgress;
21 
22  ref Timer m_Timer;
23 
24  string m_AnimationPhaseSet;
26  string m_AnimationPhaseUsed;
27 
28  const string m_PlaceableWaterType
29  protected ref array<string> m_PlaceableWaterSurfaceList;
30 
31  bool m_WaterSurfaceForSetup; //if trap can be installed on water surface (cannot be detected via getsurfacetype)
32  ref multiMap<string, float> m_CatchesPond; //array of catches that can be catched in trap - string key, float catch_chance
33  ref multiMap<string, float> m_CatchesSea; //array of catches that can be catched in trap - string key, float catch_chance
34  ref multiMap<string, float> m_CatchesGroundAnimal; //array of catches that can be catched in trap - string key, float catch_chance
35 
36  protected ref EffectSound m_DeployLoopSound;
37 
38  // ===============================================================
39  // ===================== DEPRECATED ============================
40  // ===============================================================
41  ref Timer m_PrevTimer;
42  bool m_NeedInstalation;
43  bool m_BaitNeeded;
44  bool m_IsUsable;
45  private ItemBase m_Catch;
46  ref Timer m_AlignCatchTimer;
47  string m_InfoSetup;
48  // ===============================================================
49 
50  void TrapSpawnBase()
51  {
52  m_DefectRate = 10; //Added damage after trap activation
53  m_UpdateWaitTime = 10;
54  m_InitWaitTime = 10;
55  m_NeedInstalation = true;
56  m_BaitNeeded = true;
57  m_IsFoldable = false;
58  m_MinimalDistanceFromPlayersToCatch = 0;
59 
60  m_BaitCatchProb = 95;
61  m_NoBaitCatchProb = 50;
62 
65  m_AnimationPhaseUsed = "";
66 
67  m_CatchesPond = NULL;
68  m_CatchesSea = NULL;
69  m_CatchesGroundAnimal = NULL;
70 
71  m_PlaceableWaterSurfaceList = new array<string>();
72  m_PlaceableWaterSurfaceList.Insert(UAWaterType.SEA);
73  m_PlaceableWaterSurfaceList.Insert(UAWaterType.FRESH);
74 
75  RegisterNetSyncVariableBool("m_IsSoundSynchRemote");
76  RegisterNetSyncVariableBool("m_IsDeploySound");
77  RegisterNetSyncVariableBool("m_IsActive");
78  RegisterNetSyncVariableBool("m_IsDeployed");
79  }
80 
81  void ~TrapSpawnBase()
82  {
83  SEffectManager.DestroyEffect( m_DeployLoopSound );
84  }
85 
86  override void OnStoreSave( ParamsWriteContext ctx )
87  {
88  super.OnStoreSave( ctx );
89 
90  ctx.Write( m_IsActive );
91 
92  ctx.Write( m_IsInProgress );
93 
94  ctx.Write( m_IsDeployed );
95  }
96 
97  override bool OnStoreLoad( ParamsReadContext ctx, int version )
98  {
99  if ( !super.OnStoreLoad(ctx, version) )
100  return false;
101 
102  bool b_is_active = false;
103  if ( !ctx.Read( b_is_active ) )
104  b_is_active = false;
105 
106  bool b_is_in_progress = false;
107  if ( !ctx.Read( b_is_in_progress ) )
108  b_is_in_progress = false;
109 
110  bool b_is_deployed = false;
111  if ( !ctx.Read( b_is_deployed ) )
112  b_is_deployed = false;
113 
114  if ( b_is_active )
115  {
116  SetActive();
117  }
118 
119  if ( b_is_in_progress && !b_is_active )
120  {
121  StartActivate( NULL );
122  }
123 
124  SetDeployed( b_is_deployed );
125 
126  return true;
127  }
128 
130  override void OnVariablesSynchronized()
131  {
132  super.OnVariablesSynchronized();
133 
134  if ( IsDeploySound() && IsDeployed() )
135  {
136  PlayDeploySound();
137  }
138 
139  if ( CanPlayDeployLoopSound() )
140  {
142  }
143 
145  {
147  }
148  }
149 
150  bool IsActive()
151  {
152  return m_IsActive;
153  }
154 
155  bool IsDeployed()
156  {
157  return m_IsDeployed;
158  }
159 
160  void SetDeployed( bool newState )
161  {
162  m_IsDeployed = newState;
163 
164  if ( newState == true )
165  {
166  if ( m_AnimationPhaseSet != "" && m_AnimationPhaseTriggered != "" )
167  {
168  SetAnimationPhase( m_AnimationPhaseSet, 1 );
169  SetAnimationPhase( m_AnimationPhaseTriggered, 0 );
170  SetAnimationPhase( m_AnimationPhaseUsed, 1 );
171  }
172  }
173  else
174  {
175  if ( m_AnimationPhaseSet != "" && m_AnimationPhaseTriggered != "" )
176  {
177  SetAnimationPhase( m_AnimationPhaseSet, 0 );
178  SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
179  SetAnimationPhase( m_AnimationPhaseUsed, 1 );
180  }
181  }
182 
183  SetSynchDirty();
184  }
185 
186  override bool IsTakeable()
187  {
188  return true;
189  }
190 
191  bool IsPlaceableAtPosition( vector position )
192  {
193  string surface_type;
194  GetGame().SurfaceGetType3D( position[0], position[1], position[2], surface_type);
195 
196  // check surface
197  return GetGame().IsSurfaceDigable(surface_type);
198  }
199 
200  void SetupTrap()
201  {
202  if ( GetGame().IsServer() )
203  {
204  if ( GetHierarchyRootPlayer() && GetHierarchyRootPlayer().CanDropEntity( this ) )
205  {
206  SetupTrapPlayer( PlayerBase.Cast( GetHierarchyRootPlayer() ) );
207  }
208  }
209  }
210 
211  void SetupTrapPlayer( PlayerBase player, bool set_position = true )
212  {
213  if ( GetGame().IsServer() )
214  {
215  if ( set_position )
216  {
217  vector trapPos = player.GetPosition() + ( player.GetDirection() * 0.5 );
218  trapPos[1] = GetGame().SurfaceRoadY( trapPos[0], trapPos[2] );
219  SetPosition( trapPos );
220  }
221 
222  SetDeployed( true );
223  }
224  }
225 
226  void Fold()
227  {
228  if ( GetGame().IsServer() && m_IsFoldable == true )
229  {
230  SetInactive();
231  }
232  }
233 
234  // Deal damage to trap on specific events
235  void AddDefect()
236  {
237  if ( GetGame().IsServer() )
238  {
239  DecreaseHealth( "", "", m_DefectRate );
240  }
241  }
242 
243  void StartActivate( PlayerBase player ) { }
244 
245  // IsTakeable is used to hide tooltips as well, so we use a custom method instead
246  // Used to prevent players from taking traps which should be set for catching
247  bool CanBeTaken()
248  {
249  if ( !IsDeployed() || ( GetInventory().AttachmentCount() == 0 && IsDeployed() ) )
250  {
251  return true;
252  }
253 
254  return false;
255  }
256 
257  override bool CanPutInCargo( EntityAI parent )
258  {
259  super.CanPutInCargo( parent );
260  return CanBeTaken();
261  }
262 
263  override bool CanPutIntoHands( EntityAI parent )
264  {
265  super.CanPutIntoHands( parent );
266  return CanBeTaken();
267  }
268 
269  // Set animation phases according to state
270  void SetActive()
271  {
272  if ( GetGame().IsServer() && !IsActive() )
273  {
274  m_IsActive = true;
275 
276  if ( m_AnimationPhaseSet != "" && m_AnimationPhaseTriggered != "" )
277  {
278  SetAnimationPhase( m_AnimationPhaseSet, 1 );
279  SetAnimationPhase( m_AnimationPhaseTriggered, 0 );
280  SetAnimationPhase( m_AnimationPhaseUsed, 1 );
281  }
282 
283  // When activated we start timer for catch check
285  m_Timer.Run( m_InitWaitTime, this, "SpawnCatch" );
286 
287  SetSynchDirty();
288  }
289  }
290 
291  void SetInactive()
292  {
293  if ( GetGame().IsServer() )
294  {
295  // We stop timers as the trap is no longer active, then update visuals
296  m_IsActive = false;
297  if ( m_Timer )
298  {
299  m_Timer.Stop();
300  }
301 
302  SetDeployed( false );
303 
304  SetSynchDirty();
305  }
306  }
307 
308  void SetUsed()
309  {
310  if ( GetGame().IsServer() )
311  {
312  // We updated state, visuals and stop timers
313  m_IsActive = false;
314  m_IsDeployed = false;
315  if ( m_Timer )
316  {
317  m_Timer.Stop();
318  }
319 
320  if ( m_AnimationPhaseSet != "" && m_AnimationPhaseTriggered != "" )
321  {
322  SetAnimationPhase( m_AnimationPhaseSet, 1 );
323  SetAnimationPhase( m_AnimationPhaseTriggered, 1 );
324  SetAnimationPhase( m_AnimationPhaseUsed, 0 );
325  }
326 
327  SetSynchDirty();
328  }
329  }
330 
331  // Used to check catch conditions and spawn relevant prey
332  void SpawnCatch()
333  {
334  // Only server side, let's make sure
335  if ( GetGame().IsClient() )
336  return;
337 
338  // If we can't get CEApi we won't be able to test proximity
339  if ( !GetCEApi() )
340  return;
341 
342  int i;
343  m_Bait = null;
344 
345  // We check if we have bait and can catch
346  m_CanCatch = SetCanCatch( m_Bait );
347 
348  SetIsDeploySound( false );
349  SetSynchDirty();
350 
351  // We check the distance from players through CEApi if that check is enabled
352  if ( m_MinimalDistanceFromPlayersToCatch > 0 )
353  {
354  if ( !GetCEApi().AvoidPlayer( GetPosition(), m_MinimalDistanceFromPlayersToCatch ) && m_CanCatch )
355  {
356  m_CanCatch = false;
357 
358  // We could not catch yet, let's retest until we can
359  if ( m_Timer )
360  m_Timer.Stop();
361 
363  m_Timer.Run( m_UpdateWaitTime, this, "SpawnCatch" );
364 
365  return; // No need to go further, let's just wait for next check
366  }
367  }
368 
369  // We get the probability to catch depending on bait presence
370  if ( m_Bait )
371  m_FinalCatchProb = m_BaitCatchProb;
372  else
373  m_FinalCatchProb = m_NoBaitCatchProb;
374 
375  // We get the position of prey when it will spawn
376  m_PreyPos = GetPosition();
377  if ( MemoryPointExists("Prey_Position") )
378  {
379  m_PreyPos = ModelToWorld( GetMemoryPointPos("Prey_Position") );
380  }
381  }
382 
383  // Used to set if the given trap can catch a prey in it's current state
384  // Also outs the current bait to check for specific catches if required
385  bool SetCanCatch( out EntityAI bait )
386  {
387  int slotIdx = InventorySlots.GetSlotIdFromString("Trap_Bait");
388  bait = GetInventory().FindAttachment( slotIdx );
389 
390  if ( bait )
391  {
392  Edible_Base edibleBait = Edible_Base.Cast( bait );
393  if ( edibleBait )
394  {
395  if ( !edibleBait.GetFoodStage().IsFoodBurned() && !edibleBait.GetFoodStage().IsFoodRotten() )
396  return true;
397  else
398  return false; // We have invalid bait, no catch
399  }
400  }
401  // We are allowed to catch with no bait
402  return true;
403  }
404 
405  // Used to set the quantity of the catch ( later used when preparing fish )
406  void CatchSetQuant( ItemBase catch )
407  {
408  if ( catch.HasQuantity() )
409  {
410  // Random quantity between 50% and 100%
411  float coef = Math.RandomFloatInclusive(0.5, 1.0);
412  float item_quantity = catch.GetQuantityMax() * coef;
413  item_quantity = Math.Round(item_quantity);
414  catch.SetQuantity( item_quantity );
415  }
416  }
417 
418  override void OnItemLocationChanged( EntityAI old_owner, EntityAI new_owner )
419  {
420  super.OnItemLocationChanged( old_owner, new_owner );
421 
422  if ( GetGame().IsServer() )
423  {
424  // throw trap from vicinity if the trap does not need installation ( action required )
425  if ( new_owner == NULL && m_NeedInstalation == false )
426  {
427  SetActive();
428  }
429  else if ( old_owner == NULL && new_owner != NULL )
430  {
431  if ( m_IsFoldable )
432  {
433  Fold();
434  }
435  else
436  {
437  SetInactive();
438  }
439  }
440  }
441  }
442 
443  // Generic water check, no real distinction between pond or sea
444  bool IsSurfaceWater(vector position)
445  {
446  string surfaceType;
447  GetGame().SurfaceGetType3D(position[0], position[1], position[2], surfaceType);
448 
449  return Surface.AllowedWaterSurface(position[1] + 0.1, surfaceType, m_PlaceableWaterSurfaceList);
450  }
451 
452  // Can only receive attachment if deployed
453  override bool CanDisplayAttachmentSlot( int slot_id )
454  {
455  super.CanDisplayAttachmentSlot( slot_id );
456  return IsDeployed();
457  }
458 
459  override bool CanReceiveAttachment( EntityAI attachment, int slotId )
460  {
461  super.CanReceiveAttachment( attachment, slotId );
462  return IsDeployed();
463  }
464 
465  override void EEItemAttached( EntityAI item, string slot_name )
466  {
467  super.EEItemAttached( item, slot_name );
468 
469  // In the case the trap was still deployed, enable "quick re-arm" by attaching
470  if ( IsDeployed() && slot_name == "Trap_Bait" )
471  {
472  SetActive();
473  }
474  }
475 
476  //================================================================
477  // ADVANCED PLACEMENT
478  //================================================================
479 
480  override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
481  {
482  super.OnPlacementComplete(player, position, orientation);
483 
484  if (GetGame().IsServer())
485  {
486  vector rotation_matrix[3];
487  float direction[4];
488  Math3D.YawPitchRollMatrix(orientation, rotation_matrix);
489  Math3D.MatrixToQuat(rotation_matrix, direction);
491  InventoryLocation destination = new InventoryLocation;
492 
493  if (GetInventory().GetCurrentInventoryLocation(source))
494  {
495  destination.SetGroundEx(this, position, direction);
496  if (GetGame().IsMultiplayer())
497  player.ServerTakeToDst(source, destination);
498  else // singleplayer
499  player.GetInventory().TakeToDst(InventoryMode.LOCAL, source, destination);
500  }
501 
502  SetupTrapPlayer(PlayerBase.Cast(player), false);
503  SetIsDeploySound(true);
504  SetActive();
505  }
506  }
507 
508  void PlayDeployLoopSound()
509  {
510  if ( !GetGame().IsDedicatedServer() )
511  {
512  if ( !m_DeployLoopSound || !m_DeployLoopSound.IsSoundPlaying() )
513  {
514  m_DeployLoopSound = SEffectManager.PlaySound( GetLoopDeploySoundset(), GetPosition() );
515  }
516  }
517  }
518 
519  void StopDeployLoopSound()
520  {
521  if ( !GetGame().IsDedicatedServer() )
522  {
523  m_DeployLoopSound.SetSoundFadeOut(0.5);
524  m_DeployLoopSound.SoundStop();
525  }
526  }
527 
528  // We add the action to deploy a trap laid on ground
529  override void SetActions()
530  {
531  super.SetActions();
532 
535  }
536 
537 
538  // ===============================================================
539  // ===================== DEPRECATED ============================
540  // ===============================================================
541 
542  bool CanPutInInventory( EntityAI player ) { }
543 
544  void AlignCatch( ItemBase obj, string catch_name ) { }
545 
546  bool IsPlaceable()
547  {
548  if ( GetGame().IsServer() )
549  {
550  if ( GetHierarchyRootPlayer() != NULL && GetHierarchyRootPlayer().GetHumanInventory().GetEntityInHands() == this )
551  {
552  PlayerBase player = PlayerBase.Cast( GetHierarchyRootPlayer() );
553 
554  vector player_pos = player.GetPosition();
555  vector aim_pos = player.GetAimPosition();
556 
557  if ( vector.DistanceSq(player_pos, aim_pos) <= ( 1.5 * 1.5 ) )
558  {
559  return IsPlaceableAtPosition( aim_pos );
560  }
561  }
562  }
563 
564  return false;
565  }
566 
567  override bool CanBePlaced( Man player, vector position )
568  {
569  return IsPlaceableAtPosition(position);
570  }
571 
572  // ===============================================================
573 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
CALL_CATEGORY_GAMEPLAY
const int CALL_CATEGORY_GAMEPLAY
Definition: tools.c:10
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
m_IsInProgress
protected bool m_IsInProgress
Definition: trapbase.c:26
m_InfoSetup
string m_InfoSetup
Definition: trapbase.c:36
m_Timer
ref Timer m_Timer
Definition: dayzgame.c:690
SetIsDeploySound
void SetIsDeploySound(bool is_deploy_sound)
Definition: itembase.c:4293
SetActive
void SetActive()
Definition: trapbase.c:437
InventoryLocation
InventoryLocation.
Definition: inventorylocation.c:27
HasQuantity
bool HasQuantity()
Definition: itembase.c:3311
IsTakeable
override bool IsTakeable()
Definition: explosivesbase.c:197
ActionDeployHuntingTrap
Definition: actiondeployhuntingtrap.c:1
ActionActivateTrap
ActionActivateTrapCB ActionContinuousBaseCB ActionActivateTrap()
Definition: actionactivatetrap.c:18
PlayDeploySound
void PlayDeploySound()
Definition: itembase.c:4324
PlayDeployLoopSound
void PlayDeployLoopSound()
TrapSpawnBase
Definition: trap_fishnet.c:1
UAWaterType
Definition: actionconstants.c:144
m_AnimationPhaseSet
string m_AnimationPhaseSet
Definition: trapbase.c:33
IsDeploySound
bool IsDeploySound()
Definition: itembase.c:4298
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
SetupTrapPlayer
override void SetupTrapPlayer(PlayerBase player, bool set_position=true)
Definition: trap_tripwire.c:212
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
PlayerBase
Definition: playerbaseclient.c:1
CanPutIntoHands
override bool CanPutIntoHands(EntityAI parent)
Definition: explosivesbase.c:257
vector
Definition: enconvert.c:105
CanPutInCargo
override bool CanPutInCargo(EntityAI parent)
Definition: explosivesbase.c:247
m_IsActive
bool m_IsActive
Definition: modifierbase.c:20
InventoryMode
InventoryMode
NOTE: PREDICTIVE is not to be used at all in multiplayer.
Definition: inventory.c:21
SetInactive
override void SetInactive(bool stop_timer=true)
Definition: trap_tripwire.c:160
Surface
Definition: surface.c:1
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
AvoidPlayer
proto native bool AvoidPlayer(vector vPos, float fDistance)
Check if there is a player within a radius.
IsPlaceable
bool IsPlaceable()
Definition: trapbase.c:189
array< string >
StartActivate
override void StartActivate(PlayerBase player)
Definition: trap_landmine.c:44
m_DeployLoopSound
protected ref EffectSound m_DeployLoopSound
Definition: trapbase.c:47
SetPosition
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition: effect.c:436
OnStoreSave
void OnStoreSave(ParamsWriteContext ctx)
Definition: modifierbase.c:229
StopDeployLoopSound
void StopDeployLoopSound()
AddDefect
void AddDefect()
Definition: trapbase.c:429
CanPlayDeployLoopSound
bool CanPlayDeployLoopSound()
Definition: itembase.c:4360
IsActive
bool IsActive()
Definition: modifierbase.c:130
Timer
Definition: dayzplayerimplement.c:62
Math
Definition: enmath.c:6
EEItemAttached
override void EEItemAttached(EntityAI item, string slot_name)
Definition: basebuildingbase.c:520
m_DefectRate
float m_DefectRate
Definition: trapbase.c:19
m_AnimationPhaseTriggered
string m_AnimationPhaseTriggered
Definition: trapbase.c:34
OnItemLocationChanged
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition: combinationlock.c:75
SEffectManager
Manager class for managing Effect (EffectParticle, EffectSound)
Definition: effectmanager.c:5
m_InitWaitTime
float m_InitWaitTime
Definition: trapbase.c:17
OnStoreLoad
bool OnStoreLoad(ParamsReadContext ctx, int version)
Definition: modifiersmanager.c:270
EntityAI
Definition: building.c:5
Math3D
Definition: enmath3d.c:27
CanDisplayAttachmentSlot
override bool CanDisplayAttachmentSlot(int slot_id)
Definition: trap_tripwire.c:236
Edible_Base
Definition: bearsteakmeat.c:1
GetCEApi
proto native CEApi GetCEApi()
Get the CE API.