DayZ 1.24
Loading...
Searching...
No Matches
Totem.c
Go to the documentation of this file.
1class 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
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
16 {
17 m_RefresherActive = false;
18 m_RefresherActiveLocal = false;
19 m_RefresherInitialized = false;
20 m_RefresherTimeRemaining = 0;
21
22 if (GetCEApi())
24 RegisterNetSyncVariableBool("m_RefresherActive");
25 }
26
31
33 {
34 int frequency = GetCEApi().GetCEGlobalInt("FlagRefreshFrequency");
35 int max_duration = GetCEApi().GetCEGlobalInt("FlagRefreshMaxDuration");
36
37 if (frequency > 0)
38 m_FlagRefresherFrequency = frequency;
39 if (max_duration > 0)
40 m_FlagRefresherMaxDuration = max_duration;
41 m_RefresherInitialized = true;
42 }
43
44 override string GetConstructionKitType()
45 {
46 return "TerritoryFlagKit";
47 }
48
49 override int GetMeleeTargetType()
50 {
51 return EMeleeTargetType.NONALIGNABLE;
52 }
53
54 /*override bool NameOverride(out string output)
55 {
56 if ( m_GateState != GATE_STATE_NONE )
57 {
58 output = "#str_cfgvehicles_construction_part_gate"; //changes object displayed name if in 'gate' state
59 output.ToUpper();
60 return true;
61 }
62 return false;
63 }*/
64
65 //--- CONSTRUCTION KIT
67 {
68 if (MemoryPointExists("kit_spawn_position"))
69 {
71 position = GetMemoryPointPos("kit_spawn_position");
72
73 return ModelToWorld(position);
74 }
75
76 return GetPosition();
77 }
78
80 {
81 if (category_name == "Base" && !HasBase())
82 return true;
83 else if (category_name == "Support" && HasBase() && !GetConstruction().IsPartConstructed("support"))
84 return true;
85 else if (category_name == "Pole" && GetConstruction().IsPartConstructed("support") && !GetConstruction().IsPartConstructed("pole"))
86 return true;
87 else if (category_name == "Flag" && GetConstruction().IsPartConstructed("pole"))
88 return true;
89 else
90 return false;
91 }
92 // ---
93
94 // --- EVENTS
96 {
97 super.OnStoreSave(ctx);
98
99 ctx.Write(m_RefresherTimeRemaining);
100 ctx.Write(m_RefreshTimeCounter);
101 ctx.Write(m_FlagRefresherMaxDuration);
102 ctx.Write(m_RefresherActive);
103 }
104
105 override bool OnStoreLoad(ParamsReadContext ctx, int version)
106 {
107 if (!super.OnStoreLoad(ctx, version))
108 return false;
109
110 //int loaded_frequency;
112
113 if (!ctx.Read(m_RefresherTimeRemaining))
114 return false;
115
116 if (!ctx.Read(m_RefreshTimeCounter))
117 return false;
118
119 if (!ctx.Read(loaded_max_duration))
120 return false;
121
122 if (version >= 118 && !ctx.Read(m_RefresherActive))
123 return false;
124
127
128 return true;
129 }
130
131 override void AfterStoreLoad()
132 {
133 super.AfterStoreLoad();
134
135 if (!m_RefresherInitialized && GetCEApi())
137 SetSynchDirty();
139 }
140
141 override void OnCEUpdate()
142 {
143 super.OnCEUpdate();
144
145 int time_elapsed_rounded = Math.Round(m_ElapsedSinceLastUpdate);
146
147 if (m_RefresherTimeRemaining > 0)
148 {
149 m_RefreshTimeCounter += time_elapsed_rounded;
150 if (m_RefreshTimeCounter >= m_FlagRefresherFrequency)
151 {
152 GetCEApi().RadiusLifetimeReset(GetPosition(), GameConstants.REFRESHER_RADIUS);
153 m_RefresherTimeRemaining = Math.Clamp(m_RefresherTimeRemaining - m_RefreshTimeCounter, 0, m_FlagRefresherMaxDuration);
154 m_RefreshTimeCounter = 0; //possibly carry over the rest?
156 }
157 }
158
159 SetRefresherActive(m_RefresherTimeRemaining > 0);
160 }
161
164 {
165 Mission mission = GetGame().GetMission();
167 return;
168
170 if (m_RefresherActive && idx == -1)
172 else if (!m_RefresherActive && idx > -1)
174 }
175
177 {
178 if (m_RefresherActive != state)
179 {
180 m_RefresherActive = state;
181 SetSynchDirty();
182
183 //update on refresher activation / last update on refresher deactivation
184 GetCEApi().RadiusLifetimeReset(GetPosition(), GameConstants.REFRESHER_RADIUS); //TODO spammable!!!
185 }
186 }
187
189 {
190 Mission mission = GetGame().GetMission();
192 }
193
195 {
196 if (!GetGame() || (GetGame().IsMultiplayer() && GetGame().IsServer()))
197 return;
198
199 Mission mission = GetGame().GetMission();
201 return;
202
203 if (idx == -2)
205
206 if (idx > -1)
208 }
209
211 {
212 super.OnVariablesSynchronized();
213
214 if (m_RefresherActive != m_RefresherActiveLocal)
215 {
217 m_RefresherActiveLocal = m_RefresherActive;
218 }
219 }
220
221 //--- BUILD EVENTS
222 //CONSTRUCTION EVENTS
223 override void OnPartBuiltServer(notnull Man player, string part_name, int action_id)
224 {
225 //ConstructionPart constrution_part = GetConstruction().GetConstructionPart( part_name );
226
227 super.OnPartBuiltServer(player, part_name, action_id);
228
229 //update visuals (server)
231 }
232
234 {
236
237 super.OnPartDismantledServer(player, part_name, action_id);
238
239 //update visuals (server)
241 }
242
243 override void OnPartDestroyedServer(Man player, string part_name, int action_id, bool destroyed_by_connected_part = false)
244 {
245 super.OnPartDestroyedServer(player, part_name, action_id);
246
247 //update visuals (server)
249 }
250
251 //--- ATTACHMENT & CONDITIONS
253 {
254 if (!super.CanReceiveAttachment(attachment, slotId))
255 return false;
256
257 //manage action initiator (AT_ATTACH_TO_CONSTRUCTION)
258 if (!GetGame().IsDedicatedServer())
259 {
261 if (player)
262 {
263 ConstructionActionData construction_action_data = player.GetConstructionActionData();
264
265 //reset action initiator
266 construction_action_data.SetActionInitiator(NULL);
267 }
268 }
269
270 //conditions
272 //flag item attachment
273 if (slot_name == "Material_FPole_Flag")
274 {
276 return true;
277 else
278 return false;
279 }
280 //materials
283 int part_id = -1;
284 for (int i = 0; i < GetConstruction().GetConstructionParts().Count(); i++)
285 {
286 part = GetConstruction().GetConstructionParts().GetElement(i);
287 if (!part.IsBuilt())
288 {
289 if (part_id == -1 || part_id > part.GetId())
290 {
292 part_id = part.GetId();
293 }
294 }
295 }
296
297 if (part_lowest /*&& !part_lowest.IsBuilt()*/)
298 {
299 string cfg_path = "cfgVehicles " + GetType() + " Construction " + part_lowest.GetMainPartName() + " " + part_lowest.GetPartName() + " Materials";
300 string material_path;
301 if (GetGame().ConfigIsExisting(cfg_path))
302 {
303 string child_name;
304 string child_slot_name;
305 for (i = 0; i < GetGame().ConfigGetChildrenCount(cfg_path); i++)
306 {
307 GetGame().ConfigGetChildName(cfg_path, i, child_name);
308 material_path = "" + cfg_path + " " + child_name + " slot_name";
310 return true;
311 }
312 }
313 }
314
315 return false;
316 }
317
318 //hands
319 override bool CanPutIntoHands(EntityAI parent)
320 {
321 return false;
322 }
323
325 {
326 return true;
327 }
328
329 //--- ACTION CONDITIONS
330 override bool IsPlayerInside(PlayerBase player, string selection)
331 {
332 return true; //TODO
333
334 vector player_pos = player.GetPosition();
336 vector ref_dir = GetDirection();
337 ref_dir[1] = 0;
338 ref_dir.Normalize();
339
340 vector x[2];
341 vector b1, b2;
342 GetCollisionBox(x);
343 b1 = x[0];
344 b2 = x[1];
345
347 dir_to_fence[1] = 0;
348 float len = dir_to_fence.Length();
349
350 dir_to_fence.Normalize();
351
352 vector ref_dir_angle = ref_dir.VectorToAngles();
353 vector dir_to_fence_angle = dir_to_fence.VectorToAngles();
355
356 vector test_position = test_angles.AnglesToVector() * len;
357
358 if (test_position[0] < b1[0] || test_position[0] > b2[0] || test_position[2] < 0.2 || test_position[2] > 2.2)
359 return false;
360 else
361 return true;
362 }
363
364 override bool IsFacingPlayer(PlayerBase player, string selection)
365 {
366 return true; //TODO
367
369 vector player_pos = player.GetPosition();
370 vector ref_dir = GetDirection();
371
372 //vector fence_player_dir = player_pos - fence_pos;
373 vector fence_player_dir = player.GetDirection();
374 fence_player_dir.Normalize();
375 fence_player_dir[1] = 0; //ignore height
376
377 ref_dir.Normalize();
378 ref_dir[1] = 0; //ignore height
379
380 if (ref_dir.Length() != 0)
381 {
383
384 if (angle >= MAX_ACTION_DETECTION_ANGLE_RAD)
385 return true;
386 }
387
388 return false;
389 }
390
391 override bool IsFacingCamera(string selection)
392 {
393 return false; //TODO
394
395 vector ref_dir = GetDirection();
396 vector cam_dir = GetGame().GetCurrentCameraDirection();
397
398 //ref_dir = GetGame().GetCurrentCameraPosition() - GetPosition();
399 ref_dir.Normalize();
400 ref_dir[1] = 0; //ignore height
401
402 cam_dir.Normalize();
403 cam_dir[1] = 0; //ignore height
404
405 if (ref_dir.Length() != 0)
406 {
407 float angle = Math.Acos(cam_dir * ref_dir);
408
409 if (angle >= MAX_ACTION_DETECTION_ANGLE_RAD)
410 return true;
411 }
412
413 return false;
414 }
415
416 override bool HasProperDistance(string selection, PlayerBase player)
417 {
418 //TODO
419 if (MemoryPointExists(selection))
420 {
421 vector selection_pos = ModelToWorld(GetMemoryPointPos(selection));
422 float distance = vector.Distance(selection_pos, player.GetPosition());
423 if (distance >= MAX_ACTION_DETECTION_DISTANCE)
424 return false;
425 }
426
427 return true;
428 }
429
430 //================================================================
431 // SOUNDS
432 //================================================================
433 //TODO
434
435 override void SetActions()
436 {
437 super.SetActions();
438
442 }
443
444 /*override void UpdateVisuals()
445 {
446 super.UpdateVisuals();
447 }*/
448
449 //================================================================
450 // FLAG MANIPULATION + REFRESHER
451 //================================================================
453 {
454 float temp = Math.Clamp(delta, 0, 1);
455 float phase_new;
456 if (temp < 0.01 || temp > 0.99)
458 else
459 phase_new = temp;
460
461 SetAnimationPhase("flag_mast", phase_new);
462
463 if (player)
465
466 GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString("Material_FPole_Flag"), phase_new != 1);
467
468 }
469
470 void AnimateFlag(float delta)
471 {
473 }
474
476 {
477 PluginAdminLog logs = PluginAdminLog.Cast(GetPlugin(PluginAdminLog));
478 if (newPhase == 0)
479 {
480 // at the top(it's inverted)
481 logs.TotemFlagChange(true, player, this);
482 }
483 else if (newPhase == 1)
484 {
485 // at the bottom(it's inverted)
486 logs.TotemFlagChange(false, player, this);
487 }
488
489 }
490
492 {
493 float temp = Math.Clamp(m_FlagRefresherMaxDuration * fraction, 0, m_FlagRefresherMaxDuration);
494 m_RefresherTimeRemaining = Math.Round(temp);
495 }
496
497 /*void AddRefresherTimeFlat(float seconds) //seconds?
498 {
499 float temp = Math.Clamp(m_RefresherTimeRemaining + seconds, 0, m_FlagRefresherMaxDuration);
500 m_RefresherTimeRemaining = Math.Round(temp);
501 SetRefresherActive(m_RefresherTimeRemaining > 0);
502 }*/
503
505 {
506 float temp = Math.Clamp(m_RefresherTimeRemaining + (fraction * m_FlagRefresherMaxDuration), 0, m_FlagRefresherMaxDuration);
507 m_RefresherTimeRemaining = Math.Round(temp);
508 SetRefresherActive(m_RefresherTimeRemaining > 0);
509 //Print(m_RefresherTimeRemaining);
510 }
511
513 {
514 return m_RefresherTimeRemaining / m_FlagRefresherMaxDuration;
515 }
516
518 {
519 if (max_duration != m_FlagRefresherMaxDuration)
520 m_RefresherTimeRemaining = m_FlagRefresherMaxDuration * m_RefresherTimeRemaining / max_duration;
521 }
522
523 //================================================================
524 // DEBUG
525 //================================================================
526 /*
527 override void DebugCustomState()
528 {
529 //debug
530 m_SyncParts01 = 881; //full fence with gate
531 m_HasHinges = true;
532 m_HasBase = true;
533
534 OnVariablesSynchronized();
535 }
536 */
537
538 //Debug menu Spawn Ground Special
539 override void OnDebugSpawn()
540 {
541 super.OnDebugSpawn();
542
543 GetInventory().CreateInInventory("Flag_DayZ");
544 AnimateFlag(0);
546 }
547}
eBleedingSourceType GetType()
ActionFoldBaseBuildingObjectCB ActionContinuousBaseCB ActionFoldBaseBuildingObject()
void AddAction(typename actionName)
Construction GetConstruction()
bool HasBase()
proto native CEApi GetCEApi()
Get the CE API.
bool IsPartConstructed(string part_name)
Mission mission
EMeleeTargetType
Icon x
PlayerBase GetPlayer()
PluginBase GetPlugin(typename plugin_type)
class JsonUndergroundAreaTriggerData GetPosition
override bool CanDisplayAttachmentCategory(string category_name)
Definition Totem.c:79
void TerritoryFlag()
Definition Totem.c:15
override void OnDebugSpawn()
Definition Totem.c:539
override bool IsPlayerInside(PlayerBase player, string selection)
Definition Totem.c:330
bool m_RefresherInitialized
Definition Totem.c:8
override void OnVariablesSynchronized()
Definition Totem.c:210
void ~TerritoryFlag()
Definition Totem.c:27
void SetRefreshTimer01(float fraction)
Definition Totem.c:491
void RemoveRefresherPosition(int idx=-2)
Definition Totem.c:194
override string GetConstructionKitType()
Definition Totem.c:44
float GetRefresherTime01()
Definition Totem.c:512
int m_RefreshTimeCounter
Definition Totem.c:10
override bool CanPutIntoHands(EntityAI parent)
Definition Totem.c:319
override void UpdateVisuals()
Definition Watchtower.c:44
int m_RefresherTimeRemaining
Definition Totem.c:9
override void OnPartBuiltServer(notnull Man player, string part_name, int action_id)
Definition Totem.c:223
override void SetActions()
Definition Totem.c:435
void AddRefresherTime01(float fraction)
Definition Totem.c:504
void SetRefresherActive(bool state)
Definition Totem.c:176
int m_FlagRefresherMaxDuration
Definition Totem.c:13
override bool IsFacingCamera(string selection)
Definition Totem.c:391
override bool IsFacingPlayer(PlayerBase player, string selection)
Definition Totem.c:364
override void OnCEUpdate()
Definition Totem.c:141
override void AfterStoreLoad()
Definition Totem.c:131
override bool OnStoreLoad(ParamsReadContext ctx, int version)
Definition Totem.c:105
void LogAnimateFlag(float newPhase, notnull PlayerBase player)
Definition Totem.c:475
override int GetMeleeTargetType()
Definition Totem.c:49
override bool HasProperDistance(string selection, PlayerBase player)
Definition Totem.c:416
bool m_RefresherActive
Definition Totem.c:6
void HandleRefreshers()
Client-side, saves positions of active lifetime refreshers to MissionGameplay.
Definition Totem.c:163
void InitRefresherData()
Definition Totem.c:32
override void OnPartDestroyedServer(Man player, string part_name, int action_id, bool destroyed_by_connected_part=false)
Definition Totem.c:243
override bool CanReceiveAttachment(EntityAI attachment, int slotId)
Definition Totem.c:252
void CheckLoadedVariables(int max_duration)
Definition Totem.c:517
override void OnStoreSave(ParamsWriteContext ctx)
Definition Totem.c:95
void InsertRefresherPosition()
Definition Totem.c:188
override vector GetKitSpawnPosition()
Definition Totem.c:66
override bool CanBeRepairedToPristine()
Definition Totem.c:324
void AnimateFlagEx(float delta, PlayerBase player=null)
Definition Totem.c:452
void AnimateFlag(float delta)
Definition Totem.c:470
bool m_RefresherActiveLocal
Definition Totem.c:7
override void OnPartDismantledServer(notnull Man player, string part_name, int action_id)
Definition Totem.c:233
provides access to slot configuration
static proto native owned string GetSlotName(int id)
converts slot_id to string
static proto native int GetSlotIdFromString(string slot_name)
converts string to slot_id
Definition EnMath.c:7
Mission class.
Definition gameplay.c:668
ref array< vector > m_ActiveRefresherLocations
Definition gameplay.c:671
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
static proto native float Distance(vector v1, vector v2)
Returns the distance between tips of two 3D vectors.
proto native CGame GetGame()
const float REFRESHER_RADIUS
Definition constants.c:881
const int REFRESHER_MAX_DURATION_DEFAULT
Definition constants.c:879
const int REFRESHER_FREQUENCY_DEFAULT
Definition constants.c:880
static proto float Acos(float c)
Returns angle in radians from cosinus.
static proto float Round(float f)
Returns mathematical round of value.
static proto float Clamp(float value, float min, float max)
Clamps 'value' to 'min' if it is lower than 'min', or to 'max' if it is higher than 'max'.