DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
TrapSpawnBase.c
Go to the documentation of this file.
1class 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
7 bool m_CanCatch = false;
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
23
27
28 const string m_PlaceableWaterType
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
37
38 // ===============================================================
39 // ===================== DEPRECATED ============================
40 // ===============================================================
48 // ===============================================================
49
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
82 {
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
131 {
132 super.OnVariablesSynchronized();
133
134 if ( IsDeploySound() && IsDeployed() )
135 {
137 }
138
140 {
142 }
143
145 {
147 }
148 }
149
150 bool IsActive()
151 {
152 return m_IsActive;
153 }
154
156 {
157 return m_IsDeployed;
158 }
159
160 void SetDeployed( bool newState )
161 {
162 m_IsDeployed = newState;
163
164 if ( newState == true )
165 {
167 {
168 SetAnimationPhase( m_AnimationPhaseSet, 1 );
169 SetAnimationPhase( m_AnimationPhaseTriggered, 0 );
170 SetAnimationPhase( m_AnimationPhaseUsed, 1 );
171 }
172 }
173 else
174 {
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
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
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
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
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
271 {
272 if ( GetGame().IsServer() && !IsActive() )
273 {
274 m_IsActive = true;
275
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
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
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
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 )
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 SetupTrapPlayer( PlayerBase.Cast( player ), false );
487 SetIsDeploySound( true );
488 SetActive();
489 }
490 }
491
493 {
494 if ( !GetGame().IsDedicatedServer() )
495 {
497 {
499 }
500 }
501 }
502
504 {
505 if ( !GetGame().IsDedicatedServer() )
506 {
509 }
510 }
511
512 // We add the action to deploy a trap laid on ground
513 override void SetActions()
514 {
515 super.SetActions();
516
519 }
520
521
522 // ===============================================================
523 // ===================== DEPRECATED ============================
524 // ===============================================================
525
526 bool CanPutInInventory( EntityAI player ) { }
527
528 void AlignCatch( ItemBase obj, string catch_name ) { }
529
531 {
532 if ( GetGame().IsServer() )
533 {
534 if ( GetHierarchyRootPlayer() != NULL && GetHierarchyRootPlayer().GetHumanInventory().GetEntityInHands() == this )
535 {
536 PlayerBase player = PlayerBase.Cast( GetHierarchyRootPlayer() );
537
538 vector player_pos = player.GetPosition();
539 vector aim_pos = player.GetAimPosition();
540
541 if ( vector.DistanceSq(player_pos, aim_pos) <= ( 1.5 * 1.5 ) )
542 {
543 return IsPlaceableAtPosition( aim_pos );
544 }
545 }
546 }
547
548 return false;
549 }
550
551 override bool CanBePlaced( Man player, vector position )
552 {
553 return IsPlaceableAtPosition(position);
554 }
555
556 // ===============================================================
557}
void AddAction(typename actionName)
proto native bool AvoidPlayer(vector vPos, float fDistance)
Check if there is a player within a radius.
proto native CEApi GetCEApi()
Get the CE API.
ref Timer m_Timer
Definition DayZGame.c:687
void PlayDeploySound()
Definition ItemBase.c:8920
bool HasQuantity()
Definition ItemBase.c:7955
void SetIsDeploySound(bool is_deploy_sound)
Definition ItemBase.c:8910
bool IsDeploySound()
Definition ItemBase.c:8915
bool CanPlayDeployLoopSound()
Definition ItemBase.c:8947
bool m_IsActive
bool IsActive()
string m_AnimationPhaseTriggered
Definition TrapBase.c:34
protected ref EffectSound m_DeployLoopSound
Definition TrapBase.c:47
float m_DefectRate
Definition TrapBase.c:19
void PlayDeployLoopSound()
Definition TrapBase.c:651
void StopDeployLoopSound()
Definition TrapBase.c:661
void SetActive()
Definition TrapBase.c:463
string m_AnimationPhaseSet
Definition TrapBase.c:33
void SetInactive(bool stop_timer=true)
Definition TrapBase.c:510
int m_InitWaitTime
Definition TrapBase.c:17
void SetupTrapPlayer(PlayerBase player, bool set_position=true)
Definition TrapBase.c:433
void StartActivate(PlayerBase player)
Definition TrapBase.c:487
protected bool m_IsInProgress
Definition TrapBase.c:26
class JsonUndergroundAreaTriggerData GetPosition
proto void SurfaceGetType3D(float x, float y, float z, out string type)
bool IsSurfaceDigable(string surface)
Checks if the surface is digable.
Definition Game.c:1087
proto native float SurfaceRoadY(float x, float z)
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
bool IsSoundPlaying()
Get whether EffectSound is currently playing.
void SetSoundFadeOut(float fade_out)
Set the sound fade out duration.
void SoundStop()
Stops sound.
provides access to slot configuration
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
float m_NoBaitCatchProb
string m_AnimationPhaseTriggered
protected ref EffectSound m_DeployLoopSound
void SetDeployed(bool newState)
float m_BaitCatchProb
float m_DefectRate
protected bool m_IsActive
ref Timer m_AlignCatchTimer
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
private ItemBase m_Catch
void SetInactive()
bool CanPutInInventory(EntityAI player)
bool CanBeTaken()
bool m_BaitNeeded
protected bool m_IsDeployed
bool SetCanCatch(out EntityAI bait)
float m_FinalCatchProb
override void OnStoreSave(ParamsWriteContext ctx)
int m_UpdateWaitTime
ref multiMap< string, float > m_CatchesPond
const string m_PlaceableWaterType protected ref array< string > m_PlaceableWaterSurfaceList
DEPRECATED.
ref Timer m_PrevTimer
bool m_IsFoldable
void PlayDeployLoopSound()
void StopDeployLoopSound()
override bool CanPutInCargo(EntityAI parent)
override bool CanBePlaced(Man player, vector position)
void AddDefect()
void SetActive()
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
ref multiMap< string, float > m_CatchesGroundAnimal
void SetupTrap()
override bool CanDisplayAttachmentSlot(int slot_id)
bool m_NeedInstalation
string m_AnimationPhaseSet
bool m_IsUsable
string m_InfoSetup
override bool CanPutIntoHands(EntityAI parent)
bool IsPlaceable()
override bool OnStoreLoad(ParamsReadContext ctx, int version)
int m_InitWaitTime
string m_AnimationPhaseUsed
void SetUsed()
void AlignCatch(ItemBase obj, string catch_name)
void SetupTrapPlayer(PlayerBase player, bool set_position=true)
bool IsSurfaceWater(vector position)
void ~TrapSpawnBase()
override bool IsTakeable()
override void EEItemAttached(EntityAI item, string slot_name)
vector m_PreyPos
float m_MinimalDistanceFromPlayersToCatch
bool IsActive()
bool IsPlaceableAtPosition(vector position)
void StartActivate(PlayerBase player)
override void OnVariablesSynchronized()
this event is called all variables are synchronized on client
protected bool m_IsInProgress
protected EntityAI m_Bait
void Fold()
ref Timer m_Timer
ref multiMap< string, float > m_CatchesSea
void CatchSetQuant(ItemBase catch)
bool m_WaterSurfaceForSetup
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
void SpawnCatch()
bool IsDeployed()
void TrapSpawnBase()
override void SetActions()
Definition EnMath.c:7
Manager class for managing Effect (EffectParticle, EffectSound)
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
static EffectSound PlaySound(string sound_set, vector position, float play_fade_in=0, float stop_fade_out=0, bool loop=false)
Create and play an EffectSound.
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
proto bool Write(void value_out)
proto bool Read(void value_in)
static bool AllowedWaterSurface(float pHeight, string pSurface, array< string > pAllowedSurfaceList)
Definition Surface.c:8
override void Stop()
override string GetLoopDeploySoundset()
override bool IsPlaceableAtPosition(vector position)
const string SEA
const string FRESH
fake
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
static proto native float DistanceSq(vector v1, vector v2)
Returns the square 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:427
static proto float Round(float f)
Returns mathematical round of value.
static float RandomFloatInclusive(float min, float max)
Returns a random float number between and min [inclusive] and max [inclusive].
Definition EnMath.c:86
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10