DayZ 1.24
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
85
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 SetActive();
116
119
120 SetDeployed(b_is_deployed);
121
122 return true;
123 }
124
127 {
128 super.OnVariablesSynchronized();
129
130 if (IsDeploySound() && IsDeployed())
132
135
138 }
139
140 bool IsActive()
141 {
142 return m_IsActive;
143 }
144
146 {
147 return m_IsDeployed;
148 }
149
151 {
152 m_IsDeployed = newState;
153
154 if (newState == true)
155 {
157 {
158 SetAnimationPhase(m_AnimationPhaseSet, 1);
159 SetAnimationPhase(m_AnimationPhaseTriggered, 0);
160 SetAnimationPhase(m_AnimationPhaseUsed, 1);
161 }
162 }
163 else
164 {
166 {
167 SetAnimationPhase(m_AnimationPhaseSet, 0);
168 SetAnimationPhase(m_AnimationPhaseTriggered, 1);
169 SetAnimationPhase(m_AnimationPhaseUsed, 1);
170 }
171 }
172
173 SetSynchDirty();
174 }
175
176 override bool IsTakeable()
177 {
178 return true;
179 }
180
182 {
183 string surface_type;
184 GetGame().SurfaceGetType3D(position[0], position[1], position[2], surface_type);
185
186 // check surface
187 return GetGame().IsSurfaceDigable(surface_type);
188 }
189
191 {
192 if (GetGame().IsServer())
193 {
194 if (GetHierarchyRootPlayer() && GetHierarchyRootPlayer().CanDropEntity(this))
195 SetupTrapPlayer(PlayerBase.Cast(GetHierarchyRootPlayer()));
196 }
197 }
198
200 {
201 if (GetGame().IsServer())
202 {
203 if (set_position)
204 {
205 vector trapPos = player.GetPosition() + (player.GetDirection() * 0.5);
206 trapPos[1] = GetGame().SurfaceRoadY(trapPos[0], trapPos[2]);
208 }
209
210 SetDeployed(true);
211 }
212 }
213
214 void Fold()
215 {
216 if (GetGame().IsServer() && m_IsFoldable == true)
217 SetInactive();
218 }
219
220 // Deal damage to trap on specific events
222 {
223 if (GetGame().IsServer())
224 DecreaseHealth("", "", m_DefectRate);
225 }
226
228
229 // IsTakeable is used to hide tooltips as well, so we use a custom method instead
230 // Used to prevent players from taking traps which should be set for catching
232 {
233 if (!IsDeployed() || (GetInventory().AttachmentCount() == 0 && IsDeployed()))
234 return true;
235
236 return false;
237 }
238
239 override bool CanPutInCargo(EntityAI parent)
240 {
241 super.CanPutInCargo(parent);
242 return CanBeTaken();
243 }
244
245 override bool CanPutIntoHands(EntityAI parent)
246 {
247 super.CanPutIntoHands(parent);
248 return CanBeTaken();
249 }
250
251 // Set animation phases according to state
253 {
254 if (GetGame().IsServer() && !IsActive())
255 {
256 m_IsActive = true;
257
259 {
260 SetAnimationPhase(m_AnimationPhaseSet, 1);
261 SetAnimationPhase(m_AnimationPhaseTriggered, 0);
262 SetAnimationPhase(m_AnimationPhaseUsed, 1);
263 }
264
265 // When activated we start timer for catch check
267 m_Timer.Run(m_InitWaitTime, this, "SpawnCatch");
268
269 SetSynchDirty();
270 }
271 }
272
274 {
275 if (GetGame().IsServer())
276 {
277 // We stop timers as the trap is no longer active, then update visuals
278 m_IsActive = false;
279 if (m_Timer)
280 m_Timer.Stop();
281
282 SetDeployed(false);
283
284 SetSynchDirty();
285 }
286 }
287
288 void SetUsed()
289 {
290 if (GetGame().IsServer())
291 {
292 // We updated state, visuals and stop timers
293 m_IsActive = false;
294 m_IsDeployed = false;
295 if (m_Timer)
296 m_Timer.Stop();
297
299 {
300 SetAnimationPhase(m_AnimationPhaseSet, 1);
301 SetAnimationPhase(m_AnimationPhaseTriggered, 1);
302 SetAnimationPhase(m_AnimationPhaseUsed, 0);
303 }
304
305 SetSynchDirty();
306 }
307 }
308
309 // Used to check catch conditions and spawn relevant prey
311 {
312 // Only server side, let's make sure
313 if (GetGame().IsClient())
314 return;
315
316 // If we can't get CEApi we won't be able to test proximity
317 if (!GetCEApi())
318 return;
319
320 int i;
321 m_Bait = null;
322
323 // We check if we have bait and can catch
324 m_CanCatch = SetCanCatch(m_Bait);
325
326 SetIsDeploySound(false);
327 SetSynchDirty();
328
329 // We check the distance from players through CEApi if that check is enabled
330 if (m_MinimalDistanceFromPlayersToCatch > 0)
331 {
332 if (!GetCEApi().AvoidPlayer(GetPosition(), m_MinimalDistanceFromPlayersToCatch) && m_CanCatch)
333 {
334 m_CanCatch = false;
335
336 // We could not catch yet, let's retest until we can
337 if (m_Timer)
338 m_Timer.Stop();
339
341 m_Timer.Run(m_UpdateWaitTime, this, "SpawnCatch");
342
343 return; // No need to go further, let's just wait for next check
344 }
345 }
346
347 // We get the probability to catch depending on bait presence
348 if (m_Bait)
349 m_FinalCatchProb = m_BaitCatchProb;
350 else
351 m_FinalCatchProb = m_NoBaitCatchProb;
352
353 // We get the position of prey when it will spawn
354 m_PreyPos = GetPosition();
355 if (MemoryPointExists("Prey_Position"))
356 m_PreyPos = ModelToWorld(GetMemoryPointPos("Prey_Position"));
357 }
358
359 // Used to set if the given trap can catch a prey in it's current state
360 // Also outs the current bait to check for specific catches if required
362 {
363 int slotIdx = InventorySlots.GetSlotIdFromString("Trap_Bait");
364 bait = GetInventory().FindAttachment(slotIdx);
365
366 if (bait)
367 {
369 if (edibleBait)
370 {
371 if (!edibleBait.GetFoodStage().IsFoodBurned() && !edibleBait.GetFoodStage().IsFoodRotten())
372 return true;
373 else
374 return false; // We have invalid bait, no catch
375 }
376 }
377 // We are allowed to catch with no bait
378 return true;
379 }
380
381 // Used to set the quantity of the catch ( later used when preparing fish )
383 {
384 if (catch.HasQuantity())
385 {
386 // Random quantity between 50% and 100%
387 float coef = Math.RandomFloatInclusive(0.5, 1.0);
388 float item_quantity = catch.GetQuantityMax() * coef;
390 catch.SetQuantity(item_quantity);
391 }
392 }
393
395 {
396 super.OnItemLocationChanged(old_owner, new_owner);
397
398 if (GetGame().IsServer())
399 {
400 // throw trap from vicinity if the trap does not need installation ( action required )
401 if (new_owner == NULL && m_NeedInstalation == false)
402 SetActive();
403 else if (old_owner == NULL && new_owner != NULL)
404 {
405 if (m_IsFoldable)
406 Fold();
407 else
408 SetInactive();
409 }
410 }
411 }
412
413 // Generic water check, no real distinction between pond or sea
415 {
416 string surfaceType;
417 GetGame().SurfaceGetType3D(position[0], position[1], position[2], surfaceType);
418
419 return Surface.AllowedWaterSurface(position[1] + 0.1, surfaceType, m_PlaceableWaterSurfaceList);
420 }
421
422 // Can only receive attachment if deployed
424 {
425 super.CanDisplayAttachmentSlot(slot_id);
426 return IsDeployed();
427 }
428
430 {
431 super.CanReceiveAttachment(attachment, slotId);
432 return IsDeployed();
433 }
434
435 override void EEItemAttached(EntityAI item, string slot_name)
436 {
437 super.EEItemAttached(item, slot_name);
438
439 // In the case the trap was still deployed, enable "quick re-arm" by attaching
440 if (IsDeployed() && slot_name == "Trap_Bait")
441 SetActive();
442 }
443
444 //================================================================
445 // ADVANCED PLACEMENT
446 //================================================================
447
448 override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
449 {
450 super.OnPlacementComplete(player, position, orientation);
451
452 if (GetGame().IsServer())
453 {
455 float direction[4];
460
461 if (GetInventory().GetCurrentInventoryLocation(source))
462 {
463 destination.SetGroundEx(this, position, direction);
464 if (GetGame().IsMultiplayer())
465 player.ServerTakeToDst(source, destination);
466 else // singleplayer
467 player.GetInventory().TakeToDst(InventoryMode.LOCAL, source, destination);
468 }
469
470 SetupTrapPlayer(PlayerBase.Cast(player), false);
471 SetIsDeploySound(true);
472 SetActive();
473 }
474 }
475
477 {
478 if (!GetGame().IsDedicatedServer())
479 {
480 if (!m_DeployLoopSound || !m_DeployLoopSound.IsSoundPlaying())
482 }
483 }
484
486 {
487 if (!GetGame().IsDedicatedServer())
488 {
489 m_DeployLoopSound.SetSoundFadeOut(0.5);
490 m_DeployLoopSound.SoundStop();
491 }
492 }
493
494 // We add the action to deploy a trap laid on ground
495 override void SetActions()
496 {
497 super.SetActions();
498
501 }
502
503
504 // ===============================================================
505 // ===================== DEPRECATED ============================
506 // ===============================================================
507
509
510 void AlignCatch(ItemBase obj, string catch_name) { }
511
513 {
514 if (GetGame().IsServer())
515 {
516 if (GetHierarchyRootPlayer() != NULL && GetHierarchyRootPlayer().GetHumanInventory().GetEntityInHands() == this)
517 {
518 PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
519
520 vector player_pos = player.GetPosition();
521 vector aim_pos = player.GetAimPosition();
522
523 if (vector.DistanceSq(player_pos, aim_pos) <= (1.5 * 1.5))
525 }
526 }
527
528 return false;
529 }
530
531 override bool CanBePlaced(Man player, vector position)
532 {
534 }
535
536 // ===============================================================
537}
InventoryMode
NOTE: PREDICTIVE is not to be used at all in multiplayer.
Definition Inventory.c:22
ActionActivateTrapCB ActionContinuousBaseCB ActionActivateTrap()
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:675
void SetIsDeploySound(bool is_deploy_sound)
Definition ItemBase.c:8972
bool CanPlayDeployLoopSound()
Definition ItemBase.c:9039
bool HasQuantity()
Definition ItemBase.c:7990
void PlayDeploySound()
Definition ItemBase.c:9003
bool IsDeploySound()
Definition ItemBase.c:8977
bool m_IsActive
bool IsActive()
string m_AnimationPhaseTriggered
Definition TrapBase.c:34
float m_DefectRate
Definition TrapBase.c:19
void PlayDeployLoopSound()
void StopDeployLoopSound()
void SetActive()
Definition TrapBase.c:409
ref EffectSound m_DeployLoopSound
Definition TrapBase.c:47
string m_AnimationPhaseSet
Definition TrapBase.c:33
bool m_IsInProgress
Definition TrapBase.c:26
void SetInactive(bool stop_timer=true)
Definition TrapBase.c:452
float m_InitWaitTime
Definition TrapBase.c:17
void SetupTrapPlayer(PlayerBase player, bool set_position=true)
Definition TrapBase.c:385
void StartActivate(PlayerBase player)
Definition TrapBase.c:431
class JsonUndergroundAreaTriggerData GetPosition
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
InventoryLocation.
provides access to slot configuration
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
override void EEItemAttached(EntityAI item, string slot_name)
float m_NoBaitCatchProb
bool m_IsDeployed
bool IsSurfaceWater(vector position)
string m_AnimationPhaseTriggered
bool SetCanCatch(out EntityAI bait)
string m_InfoSetup
bool m_IsInProgress
override bool OnStoreLoad(ParamsReadContext ctx, int version)
ref Timer m_AlignCatchTimer
bool CanPutInInventory(EntityAI player)
void AlignCatch(ItemBase obj, string catch_name)
float m_MinimalDistanceFromPlayersToCatch
bool IsDeployed()
float m_FinalCatchProb
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
bool m_BaitNeeded
override bool CanPutIntoHands(EntityAI parent)
bool m_IsUsable
void PlayDeployLoopSound()
override void SetActions()
ref Timer m_PrevTimer
void SpawnCatch()
void SetInactive()
void SetupTrapPlayer(PlayerBase player, bool set_position=true)
ref multiMap< string, float > m_CatchesGroundAnimal
ref EffectSound m_DeployLoopSound
void TrapSpawnBase()
void Fold()
override void OnStoreSave(ParamsWriteContext ctx)
ref multiMap< string, float > m_CatchesSea
int m_InitWaitTime
void SetUsed()
void AddDefect()
void SetActive()
int m_UpdateWaitTime
override void OnVariablesSynchronized()
this event is called all variables are synchronized on client
bool IsPlaceable()
vector m_PreyPos
override bool CanDisplayAttachmentSlot(int slot_id)
void SetDeployed(bool newState)
ItemBase m_Catch
EntityAI m_Bait
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
override bool CanPutInCargo(EntityAI parent)
override void OnPlacementComplete(Man player, vector position="0 0 0", vector orientation="0 0 0")
void StartActivate(PlayerBase player)
void ~TrapSpawnBase()
override bool IsTakeable()
bool m_IsActive
bool IsActive()
float m_BaitCatchProb
bool IsPlaceableAtPosition(vector position)
void SetupTrap()
string m_AnimationPhaseSet
bool m_NeedInstalation
void StopDeployLoopSound()
override bool CanBePlaced(Man player, vector position)
float m_DefectRate
bool m_WaterSurfaceForSetup
bool CanBeTaken()
bool m_IsFoldable
ref multiMap< string, float > m_CatchesPond
void CatchSetQuant(ItemBase catch)
string m_AnimationPhaseUsed
ref array< string > m_PlaceableWaterSurfaceList
DEPRECATED.
Definition EnMath.c:7
Manager class for managing Effect (EffectParticle, EffectSound)
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.
static void DestroyEffect(Effect effect)
Unregisters, stops and frees the Effect.
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
static bool AllowedWaterSurface(float pHeight, string pSurface, array< string > pAllowedSurfaceList)
Definition Surface.c:8
override string GetLoopDeploySoundset()
override bool IsPlaceableAtPosition(vector position)
const string FRESH
fake
const string SEA
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:420
static proto void MatrixToQuat(vector mat[3], out float d[4])
Converts rotation matrix to quaternion.
static proto void YawPitchRollMatrix(vector ang, out vector mat[3])
Creates rotation matrix from angles.
static float RandomFloatInclusive(float min, float max)
Returns a random float number between and min [inclusive] and max [inclusive].
Definition EnMath.c:106
static proto float Round(float f)
Returns mathematical round of value.
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10