Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
totem.c
Go to the documentation of this file.
1 class TerritoryFlag extends BaseBuildingBase
2 {
3  const float MAX_ACTION_DETECTION_ANGLE_RAD = 1.3; //1.3 RAD = ~75 DEG
4  const float MAX_ACTION_DETECTION_DISTANCE = 2.0; //meters
5 
6  bool m_RefresherActive;
7  bool m_RefresherActiveLocal;
8  bool m_RefresherInitialized;
9  int m_RefresherTimeRemaining;
10  int m_RefreshTimeCounter;
11 
12  protected int m_FlagRefresherFrequency = GameConstants.REFRESHER_FREQUENCY_DEFAULT; //how often does the flag increase lifetimes
13  protected int m_FlagRefresherMaxDuration = GameConstants.REFRESHER_MAX_DURATION_DEFAULT; //how long will the refresher run; multiple of m_FlagRefresherFrequency by default
14 
15  void TerritoryFlag()
16  {
17  m_RefresherActive = false;
18  m_RefresherActiveLocal = false;
19  m_RefresherInitialized = false;
20  m_RefresherTimeRemaining = 0;
21 
22  if ( GetCEApi() )
23  {
24  InitRefresherData();
25  }
26  RegisterNetSyncVariableBool("m_RefresherActive");
27  }
28 
29  void ~TerritoryFlag()
30  {
31  RemoveRefresherPosition();
32  }
33 
34  void InitRefresherData()
35  {
36  int frequency = GetCEApi().GetCEGlobalInt("FlagRefreshFrequency");
37  int max_duration = GetCEApi().GetCEGlobalInt("FlagRefreshMaxDuration");
38 
39  if ( frequency > 0)
40  {
41  m_FlagRefresherFrequency = frequency;
42  }
43  if ( max_duration > 0 )
44  {
45  m_FlagRefresherMaxDuration = max_duration;
46  }
47  m_RefresherInitialized = true;
48  }
49 
50  override string GetConstructionKitType()
51  {
52  return "TerritoryFlagKit";
53  }
54 
55  override int GetMeleeTargetType()
56  {
57  return EMeleeTargetType.NONALIGNABLE;
58  }
59 
60  /*override bool NameOverride(out string output)
61  {
62  if ( m_GateState != GATE_STATE_NONE )
63  {
64  output = "#str_cfgvehicles_construction_part_gate"; //changes object displayed name if in 'gate' state
65  output.ToUpper();
66  return true;
67  }
68  return false;
69  }*/
70 
71  //--- CONSTRUCTION KIT
72  override vector GetKitSpawnPosition()
73  {
74  if ( MemoryPointExists( "kit_spawn_position" ) )
75  {
76  vector position;
77  position = GetMemoryPointPos( "kit_spawn_position" );
78 
79  return ModelToWorld( position );
80  }
81 
82  return GetPosition();
83  }
84 
85  override bool CanDisplayAttachmentCategory( string category_name )
86  {
87  if ( category_name == "Base" && !HasBase() )
88  return true;
89  else if ( category_name == "Support" && HasBase() && !GetConstruction().IsPartConstructed("support") )
90  return true;
91  else if ( category_name == "Pole" && GetConstruction().IsPartConstructed("support") && !GetConstruction().IsPartConstructed("pole") )
92  return true;
93  else if ( category_name == "Flag" && GetConstruction().IsPartConstructed("pole") )
94  return true;
95  else
96  return false;
97  }
98  // ---
99 
100  // --- EVENTS
101  override void OnStoreSave( ParamsWriteContext ctx )
102  {
103  super.OnStoreSave( ctx );
104 
105  ctx.Write( m_RefresherTimeRemaining );
106  ctx.Write( m_RefreshTimeCounter );
107  ctx.Write( m_FlagRefresherMaxDuration );
108  ctx.Write( m_RefresherActive );
109  }
110 
111  override bool OnStoreLoad( ParamsReadContext ctx, int version )
112  {
113  if ( !super.OnStoreLoad( ctx, version ) )
114  return false;
115 
116  //int loaded_frequency;
117  int loaded_max_duration;
118 
119  if ( !ctx.Read( m_RefresherTimeRemaining ) )
120  {
121  return false;
122  }
123 
124  if ( !ctx.Read( m_RefreshTimeCounter ) )
125  {
126  return false;
127  }
128 
129  if ( !ctx.Read( loaded_max_duration ) )
130  {
131  return false;
132  }
133 
134  if ( version >= 118 && !ctx.Read( m_RefresherActive ) )
135  {
136  return false;
137  }
138 
139  CheckLoadedVariables(loaded_max_duration);
140  AnimateFlag(1 - GetRefresherTime01());
141 
142  return true;
143  }
144 
145  override void AfterStoreLoad()
146  {
147  super.AfterStoreLoad();
148 
149  if (!m_RefresherInitialized && GetCEApi())
150  {
151  InitRefresherData();
152  }
153  SetSynchDirty();
154  UpdateVisuals();
155  }
156 
157  override void OnCEUpdate()
158  {
159  super.OnCEUpdate();
160 
161  int time_elapsed_rounded = Math.Round(m_ElapsedSinceLastUpdate);
162 
163  if ( m_RefresherTimeRemaining > 0 )
164  {
165  m_RefreshTimeCounter += time_elapsed_rounded;
166  if ( m_RefreshTimeCounter >= m_FlagRefresherFrequency )
167  {
168  GetCEApi().RadiusLifetimeReset(GetPosition(),GameConstants.REFRESHER_RADIUS);
169  m_RefresherTimeRemaining = Math.Clamp(m_RefresherTimeRemaining - m_RefreshTimeCounter,0,m_FlagRefresherMaxDuration);
170  m_RefreshTimeCounter = 0; //possibly carry over the rest?
171  AnimateFlag( 1 - GetRefresherTime01() );
172  }
173  }
174 
175  SetRefresherActive(m_RefresherTimeRemaining > 0);
176  }
177 
179  void HandleRefreshers()
180  {
181  Mission mission = GetGame().GetMission();
182  if (!mission.m_ActiveRefresherLocations)
183  return;
184 
185  int idx = mission.m_ActiveRefresherLocations.Find(GetPosition());
186  if ( m_RefresherActive && idx == -1 )
187  {
188  InsertRefresherPosition();
189  }
190  else if ( !m_RefresherActive && idx > -1 )
191  {
192  RemoveRefresherPosition(idx);
193  }
194  }
195 
196  void SetRefresherActive(bool state)
197  {
198  if ( m_RefresherActive != state )
199  {
200  m_RefresherActive = state;
201  SetSynchDirty();
202 
203  //update on refresher activation / last update on refresher deactivation
204  GetCEApi().RadiusLifetimeReset(GetPosition(),GameConstants.REFRESHER_RADIUS); //TODO spammable!!!
205  }
206  }
207 
208  void InsertRefresherPosition()
209  {
210  Mission mission = GetGame().GetMission();
211  mission.m_ActiveRefresherLocations.Insert(GetPosition());
212  }
213 
214  void RemoveRefresherPosition(int idx = -2)
215  {
216  if (!GetGame() || (GetGame().IsMultiplayer() && GetGame().IsServer()) )
217  return;
218 
219  Mission mission = GetGame().GetMission();
220  if (!mission || !mission.m_ActiveRefresherLocations)
221  return;
222 
223  if (idx == -2)
224  {
225  idx = mission.m_ActiveRefresherLocations.Find(GetPosition());
226  }
227 
228  if (idx > -1)
229  {
230  mission.m_ActiveRefresherLocations.Remove(idx);
231  }
232  }
233 
234  override void OnVariablesSynchronized()
235  {
236  super.OnVariablesSynchronized();
237 
238  if ( m_RefresherActive != m_RefresherActiveLocal )
239  {
240  HandleRefreshers();
241  m_RefresherActiveLocal = m_RefresherActive;
242  }
243  }
244 
245  //--- BUILD EVENTS
246  //CONSTRUCTION EVENTS
247  override void OnPartBuiltServer( notnull Man player, string part_name, int action_id )
248  {
249  //ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
250 
251  super.OnPartBuiltServer( player, part_name, action_id );
252 
253  //update visuals (server)
254  UpdateVisuals();
255  }
256 
257  override void OnPartDismantledServer( notnull Man player, string part_name, int action_id )
258  {
259  ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
260 
261  super.OnPartDismantledServer( player, part_name, action_id );
262 
263  //update visuals (server)
264  UpdateVisuals();
265  }
266 
267  override void OnPartDestroyedServer( Man player, string part_name, int action_id, bool destroyed_by_connected_part = false )
268  {
269  super.OnPartDestroyedServer( player, part_name, action_id );
270 
271  //update visuals (server)
272  UpdateVisuals();
273  }
274 
275  //--- ATTACHMENT & CONDITIONS
276  override bool CanReceiveAttachment( EntityAI attachment, int slotId ) //TODO
277  {
278  if ( !super.CanReceiveAttachment(attachment, slotId) )
279  return false;
280 
281  //manage action initiator (AT_ATTACH_TO_CONSTRUCTION)
282  if ( !GetGame().IsDedicatedServer() )
283  {
284  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
285  if ( player )
286  {
287  ConstructionActionData construction_action_data = player.GetConstructionActionData();
288 
289  //reset action initiator
290  construction_action_data.SetActionInitiator( NULL );
291  }
292  }
293 
294  //conditions
295  string slot_name = InventorySlots.GetSlotName(slotId);
296  //flag item attachment
297  if ( slot_name == "Material_FPole_Flag" )
298  {
299  if ( GetConstruction().IsPartConstructed("pole") )
300  return true;
301  else
302  return false;
303  }
304  //materials
305  ConstructionPart part;
306  ConstructionPart part_lowest;
307  int part_id = -1;
308  for (int i = 0; i < GetConstruction().GetConstructionParts().Count(); i++)
309  {
310  part = GetConstruction().GetConstructionParts().GetElement(i);
311  if (!part.IsBuilt())
312  {
313  if (part_id == -1 || part_id > part.GetId())
314  {
315  part_lowest = part;
316  part_id = part.GetId();
317  }
318  }
319  }
320 
321  if (part_lowest /*&& !part_lowest.IsBuilt()*/)
322  {
323  string cfg_path = "cfgVehicles " + GetType() + " Construction " + part_lowest.GetMainPartName() + " " + part_lowest.GetPartName() + " Materials";
324  string material_path;
325  if ( GetGame().ConfigIsExisting(cfg_path) )
326  {
327  string child_name;
328  string child_slot_name;
329  for ( i = 0; i < GetGame().ConfigGetChildrenCount( cfg_path ); i++ )
330  {
331  GetGame().ConfigGetChildName( cfg_path, i, child_name );
332  material_path = "" + cfg_path + " " + child_name + " slot_name";
333  if ( GetGame().ConfigGetText(material_path,child_slot_name) && child_slot_name == slot_name )
334  {
335  return true;
336  }
337  }
338  }
339  }
340 
341  return false;
342  }
343 
344  //hands
345  override bool CanPutIntoHands( EntityAI parent )
346  {
347  return false;
348  }
349 
350  override bool CanBeRepairedToPristine()
351  {
352  return true;
353  }
354 
355  //--- ACTION CONDITIONS
356  override bool IsPlayerInside( PlayerBase player, string selection )
357  {
358  return true; //TODO
359 
360  vector player_pos = player.GetPosition();
361  vector fence_pos = GetPosition();
362  vector ref_dir = GetDirection();
363  ref_dir[1] = 0;
364  ref_dir.Normalize();
365 
366  vector x[2];
367  vector b1,b2;
368  GetCollisionBox(x);
369  b1 = x[0];
370  b2 = x[1];
371 
372  vector dir_to_fence = fence_pos - player_pos;
373  dir_to_fence[1] = 0;
374  float len = dir_to_fence.Length();
375 
376  dir_to_fence.Normalize();
377 
378  vector ref_dir_angle = ref_dir.VectorToAngles();
379  vector dir_to_fence_angle = dir_to_fence.VectorToAngles();
380  vector test_angles = dir_to_fence_angle - ref_dir_angle;
381 
382  vector test_position = test_angles.AnglesToVector() * len;
383 
384  if(test_position[0] < b1[0] || test_position[0] > b2[0] || test_position[2] < 0.2 || test_position[2] > 2.2 )
385  {
386  return false;
387  }
388  else
389  {
390  return true;
391  }
392  }
393 
394  override bool IsFacingPlayer( PlayerBase player, string selection )
395  {
396  return true; //TODO
397 
398  vector fence_pos = GetPosition();
399  vector player_pos = player.GetPosition();
400  vector ref_dir = GetDirection();
401 
402  //vector fence_player_dir = player_pos - fence_pos;
403  vector fence_player_dir = player.GetDirection();
404  fence_player_dir.Normalize();
405  fence_player_dir[1] = 0; //ignore height
406 
407  ref_dir.Normalize();
408  ref_dir[1] = 0; //ignore height
409 
410  if ( ref_dir.Length() != 0 )
411  {
412  float angle = Math.Acos( fence_player_dir * ref_dir );
413 
414  if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
415  {
416  return true;
417  }
418  }
419 
420  return false;
421  }
422 
423  override bool IsFacingCamera( string selection )
424  {
425  return false; //TODO
426 
427  vector ref_dir = GetDirection();
428  vector cam_dir = GetGame().GetCurrentCameraDirection();
429 
430  //ref_dir = GetGame().GetCurrentCameraPosition() - GetPosition();
431  ref_dir.Normalize();
432  ref_dir[1] = 0; //ignore height
433 
434  cam_dir.Normalize();
435  cam_dir[1] = 0; //ignore height
436 
437  if ( ref_dir.Length() != 0 )
438  {
439  float angle = Math.Acos( cam_dir * ref_dir );
440 
441  if ( angle >= MAX_ACTION_DETECTION_ANGLE_RAD )
442  {
443  return true;
444  }
445  }
446 
447  return false;
448  }
449 
450  override bool HasProperDistance( string selection, PlayerBase player )
451  {
452  //TODO
453  if ( MemoryPointExists( selection ) )
454  {
455  vector selection_pos = ModelToWorld( GetMemoryPointPos( selection ) );
456  float distance = vector.Distance( selection_pos, player.GetPosition() );
457  if ( distance >= MAX_ACTION_DETECTION_DISTANCE )
458  {
459  return false;
460  }
461  }
462 
463  return true;
464  }
465 
466  //================================================================
467  // SOUNDS
468  //================================================================
469  //TODO
470 
471  override void SetActions()
472  {
473  super.SetActions();
474 
478  }
479 
480  /*override void UpdateVisuals()
481  {
482  super.UpdateVisuals();
483  }*/
484 
485  //================================================================
486  // FLAG MANIPULATION + REFRESHER
487  //================================================================
488  void AnimateFlagEx(float delta, PlayerBase player = null)
489  {
490  float temp = Math.Clamp( delta,0,1 );
491  float phase_new;
492  if (temp < 0.01 || temp > 0.99)
493  {
494  phase_new = Math.Round(temp);
495  }
496  else
497  phase_new = temp;
498 
499  SetAnimationPhase("flag_mast",phase_new);
500 
501  if (player)
502  LogAnimateFlag(phase_new, player);
503 
504  GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString("Material_FPole_Flag"),phase_new != 1);
505 
506  }
507 
508  void AnimateFlag(float delta)
509  {
510  AnimateFlagEx(delta);
511  }
512 
513  protected void LogAnimateFlag(float newPhase, notnull PlayerBase player)
514  {
515  PluginAdminLog logs = PluginAdminLog.Cast(GetPlugin(PluginAdminLog));
516  if (newPhase == 0)
517  {
518  // at the top(it's inverted)
519  logs.TotemFlagChange(true, player, this);
520  }
521  else if (newPhase == 1)
522  {
523  // at the bottom(it's inverted)
524  logs.TotemFlagChange(false, player, this);
525  }
526 
527  }
528 
529  void SetRefreshTimer01(float fraction)
530  {
531  float temp = Math.Clamp(m_FlagRefresherMaxDuration * fraction, 0, m_FlagRefresherMaxDuration);
532  m_RefresherTimeRemaining = Math.Round(temp);
533  }
534 
535  /*void AddRefresherTimeFlat(float seconds) //seconds?
536  {
537  float temp = Math.Clamp(m_RefresherTimeRemaining + seconds, 0, m_FlagRefresherMaxDuration);
538  m_RefresherTimeRemaining = Math.Round(temp);
539  SetRefresherActive(m_RefresherTimeRemaining > 0);
540  }*/
541 
542  void AddRefresherTime01(float fraction)
543  {
544  float temp = Math.Clamp(m_RefresherTimeRemaining + (fraction * m_FlagRefresherMaxDuration), 0, m_FlagRefresherMaxDuration);
545  m_RefresherTimeRemaining = Math.Round(temp);
546  SetRefresherActive(m_RefresherTimeRemaining > 0);
547  //Print(m_RefresherTimeRemaining);
548  }
549 
550  float GetRefresherTime01()
551  {
552  return m_RefresherTimeRemaining / m_FlagRefresherMaxDuration;
553  }
554 
555  void CheckLoadedVariables(int max_duration)
556  {
557  if (max_duration != m_FlagRefresherMaxDuration)
558  {
559  m_RefresherTimeRemaining = m_FlagRefresherMaxDuration * m_RefresherTimeRemaining / max_duration;
560  }
561  }
562 
563  //================================================================
564  // DEBUG
565  //================================================================
566  /*
567  override void DebugCustomState()
568  {
569  //debug
570  m_SyncParts01 = 881; //full fence with gate
571  m_HasHinges = true;
572  m_HasBase = true;
573 
574  OnVariablesSynchronized();
575  }
576  */
577 
578  //Debug menu Spawn Ground Special
579  override void OnDebugSpawn()
580  {
581  super.OnDebugSpawn();
582 
583  GetInventory().CreateInInventory("Flag_DayZ");
584  AnimateFlag(0);
585  AddRefresherTime01(1);
586  }
587 }
GetGame
proto native CGame GetGame()
mission
Mission mission
Definition: displaystatus.c:28
ConstructionActionData
Definition: constructionactiondata.c:1
OnPartBuiltServer
void OnPartBuiltServer(notnull Man player, string part_name, int action_id)
Definition: basebuildingbase.c:554
Mission
Mission class.
Definition: gameplay.c:670
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
OnCEUpdate
override void OnCEUpdate()
Definition: remotedetonator.c:87
ActionFoldBaseBuildingObject
ActionFoldBaseBuildingObjectCB ActionContinuousBaseCB ActionFoldBaseBuildingObject()
Definition: actionfoldbasebuildingobject.c:11
OnVariablesSynchronized
override void OnVariablesSynchronized()
Definition: anniversarymusicsource.c:42
GetConstructionKitType
protected string GetConstructionKitType()
Definition: basebuildingbase.c:375
HasProperDistance
bool HasProperDistance(string selection, PlayerBase player)
Definition: basebuildingbase.c:969
IsPartConstructed
bool IsPartConstructed(string part_name)
Definition: construction.c:601
IsFacingCamera
bool IsFacingCamera(string selection)
Definition: basebuildingbase.c:957
GetPlugin
PluginBase GetPlugin(typename plugin_type)
Definition: pluginmanager.c:316
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
OnDebugSpawn
class Hatchback_02_Blue extends Hatchback_02 OnDebugSpawn
Definition: hatchback_02.c:404
GetPosition
class JsonUndergroundAreaTriggerData GetPosition
Definition: undergroundarealoader.c:9
GetKitSpawnPosition
protected vector GetKitSpawnPosition()
Definition: basebuildingbase.c:370
PlayerBase
Definition: playerbaseclient.c:1
CanPutIntoHands
override bool CanPutIntoHands(EntityAI parent)
Definition: explosivesbase.c:257
vector
Definition: enconvert.c:105
AfterStoreLoad
void AfterStoreLoad()
Definition: emotemanager.c:577
ActionRaiseFlag
Definition: actionraiseflag.c:9
BaseBuildingBase
Definition: fence.c:1
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
SetActions
void SetActions()
Definition: advancedcommunication.c:79
OnPartDismantledServer
void OnPartDismantledServer(notnull Man player, string part_name, int action_id)
Definition: basebuildingbase.c:594
GetConstruction
Construction GetConstruction()
Definition: basebuildingbase.c:888
UpdateVisuals
void UpdateVisuals()
Definition: construction.c:188
x
Icon x
OnStoreSave
void OnStoreSave(ParamsWriteContext ctx)
Definition: modifierbase.c:229
GetPlayer
protected void GetPlayer()
Definition: crosshairselector.c:127
GameConstants
Definition: constants.c:612
ConstructionPart
Definition: constructionpart.c:1
OnPartDestroyedServer
void OnPartDestroyedServer(Man player, string part_name, int action_id, bool destroyed_by_connected_part=false)
Definition: basebuildingbase.c:634
CanDisplayAttachmentCategory
override bool CanDisplayAttachmentCategory(string category_name)
Definition: civiliansedan.c:135
IsFacingPlayer
override bool IsFacingPlayer(PlayerBase player, string selection)
Definition: basebuildingbase.c:940
HasBase
bool HasBase()
Definition: basebuildingbase.c:329
Math
Definition: enmath.c:6
CanReceiveAttachment
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
Definition: basebuildingbase.c:895
OnStoreLoad
bool OnStoreLoad(ParamsReadContext ctx, int version)
Definition: modifiersmanager.c:270
EntityAI
Definition: building.c:5
IsPlayerInside
override bool IsPlayerInside(PlayerBase player, string selection)
Definition: basebuildingbase.c:945
ActionLowerFlag
Definition: actionlowerflag.c:2
GetType
override int GetType()
Definition: huddebugwincharagents.c:49
GetCEApi
proto native CEApi GetCEApi()
Get the CE API.
EMeleeTargetType
EMeleeTargetType
Definition: emeleetargettype.c:1