DayZ 1.30
DayZ Explorer by KGB
 
Загрузка...
Поиск...
Не найдено
CodeLockComponent.c
См. документацию.
1//#define CODELOCKCOMPONENT_LOG
2
3// Code lock state enum for specific states
12
13enum CodeLockUIAction
14{
15 NONE,
18 DIGIT_INPUT
19}
20
22{
23 const int MIN_LOCK_PIN_LENGTH = 4; // min amount of digits the combination needs to contain
24 const int MAX_LOCK_PIN_LENGTH = 6; // max amount of digits the combination can contain
25
26 private const float DEFAULT_BRUTEFORCE_TIME = 30.0;
27 private const float DEFAULT_BRUTEFORCE_RESET_TIME = 3600.0;
28
29 protected int m_MinLockPinLength;
30
31 protected string m_InputPIN; // combination that is dialed on lock
32 protected string m_LockPIN; // actual combination of the lock
33
34 protected bool m_IsLocked; // lock state
35 protected EntityAI m_Parent; // code lock parent entity
36 protected int m_DoorIndex = -1; // code lock door index for parent building if code lock belongs to a door
37
38 protected bool m_HasPIN; // code state
39
41 protected float m_BruteForceResetTime;
42
43 protected int m_Stage;
46 protected int m_WrongCodeCount;
49 protected CodeLockUIAction m_UIAction;
50
75
77 {
78 m_Stage = stage;
79 #ifdef CODELOCKCOMPONENT_LOG
80 ErrorEx(string.Format("Lock stage=%1", typename.EnumToString(CodeLockStage, m_Stage)), ErrorExSeverity.INFO);
81 #endif
82 }
83
85 {
86 return m_Stage;
87 }
88
89 void SetMinimalPinCodeLength(int length)
90 {
91 m_MinLockPinLength = length;
92 }
93
94 bool SetInputCode(string pin, bool force = true)
95 {
96 if (!force)
97 {
98 int pinLength = pin.Length();
99 if (pinLength < m_MinLockPinLength || pinLength > MAX_LOCK_PIN_LENGTH)
100 return false;
101 }
102
103 m_InputPIN = pin;
104
105 #ifdef CODELOCKCOMPONENT_LOG
106 ErrorEx("Set input pin=" + m_InputPIN, ErrorExSeverity.INFO);
107 #endif
108
109 return true;
110 }
111
112 protected bool ValidatePIN(string pin)
113 {
114 if (pin == "")
115 return false;
116
117 int pinLength = pin.Length();
118 return pinLength >= m_MinLockPinLength && pinLength <= MAX_LOCK_PIN_LENGTH;
119 }
120
121 bool SetLockCode(string pin, bool sync = true)
122 {
123 if (!ValidatePIN(pin))
124 {
125 #ifdef CODELOCKCOMPONENT_LOG
126 ErrorEx(string.Format("Invalid PIN length: %1 (min: %2, max: %3)", pin.Length(), m_MinLockPinLength, MAX_LOCK_PIN_LENGTH), ErrorExSeverity.WARNING);
127 #endif
128 return false;
129 }
130
131 m_LockPIN = pin;
132 m_HasPIN = true;
133
134 SetStage(CodeLockStage.NORMAL);
135 SetLockState(true, false);
136
137 #ifdef CODELOCKCOMPONENT_LOG
138 ErrorEx("Set lock pin=" + m_LockPIN, ErrorExSeverity.INFO);
139 #endif
140
141 if (sync)
143
144 return true;
145 }
146
147 void ResetPIN(bool sync = true)
148 {
149 m_LockPIN = "";
150 m_HasPIN = false;
151 SetStage(CodeLockStage.SETTING);
152
153 if (sync)
155 }
156
157 void SetLockState(bool state, bool sync = true)
158 {
159 if (m_IsLocked == state)
160 return;
161
162 m_IsLocked = state;
163
164 if (!state)
165 {
167
171
172 if (m_HasPIN)
173 {
174 if (m_Stage >= CodeLockStage.PROTECTION_STAGE_ONE)
175 {
176 SetStage(CodeLockStage.PROTECTION_STAGE_TWO);
177 }
178 else
179 {
180 SetStage(CodeLockStage.NORMAL);
181 }
182 }
183 }
184
185 if (m_DoorIndex > -1)
187
188 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
189 {
190 ItemBase parentIB = ItemBase.Cast(m_Parent);
191 if (parentIB)
192 {
193 if (state)
194 {
195 parentIB.StartItemSoundServer(SoundConstants.ITEM_DIGITAL_LOCK_LOCK);
196 }
197 else
198 {
199 parentIB.StartItemSoundServer(SoundConstants.ITEM_DIGITAL_LOCK_UNLOCK);
200 }
201 }
202 }
203
204 if (sync)
206
207 #ifdef CODELOCKCOMPONENT_LOG
208 ErrorEx("Set lock state=" + m_IsLocked, ErrorExSeverity.INFO);
209 #endif
210 }
211
212 protected void HandleHierarchyRootLock(bool state)
213 {
214 if (m_DoorIndex == -1)
215 {
216 #ifdef CODELOCKCOMPONENT_LOG
217 ErrorEx(string.Format("Error! Invalid call of HandleHierarchyRootLock without valid door index! Parent=%1 | Door index=%2", m_Parent, m_DoorIndex), ErrorExSeverity.ERROR);
218 #endif
219 return;
220 }
221
222 Building building = Building.Cast(m_Parent.GetHierarchyRoot());
223 if (!building || !building.CanUseConstruction())
224 {
225 #ifdef CODELOCKCOMPONENT_LOG
226 ErrorEx(string.Format("Error! Invalid hierarchy root parent for lock! Parent=%1 | Hierarchy root parent=%2", m_Parent, m_Parent.GetHierarchyRoot()), ErrorExSeverity.ERROR);
227 #endif
228 return;
229 }
230
231 if (m_DoorIndex > building.GetDoorCount() - 1)
232 {
233 #ifdef CODELOCKCOMPONENT_LOG
234 ErrorEx(string.Format("Warning! Given door index is out of bounds! Door index=%1 | Doors count=%2", m_DoorIndex, building.GetDoorCount()), ErrorExSeverity.ERROR);
235 #endif
236 return;
237 }
238
239 if (state)
240 {
241 building.LockDoor(m_DoorIndex);
242
243 #ifdef CODELOCKCOMPONENT_LOG
244 ErrorEx(string.Format("Locked door! Door index=%1 | Doors count=%2", m_DoorIndex, building.GetDoorCount()), ErrorExSeverity.INFO);
245 #endif
246 }
247 else
248 {
249 building.UnlockDoor(m_DoorIndex);
250
251 #ifdef CODELOCKCOMPONENT_LOG
252 ErrorEx(string.Format("Unlocked door! Door index=%1 | Doors count=%2", m_DoorIndex, building.GetDoorCount()), ErrorExSeverity.INFO);
253 #endif
254 }
255 }
256
258 {
259 ctx.Write(m_LockPIN);
260 ctx.Write(m_IsLocked);
261 ctx.Write(m_DoorIndex);
262 }
263
264 bool OnStoreLoad(ParamsReadContext ctx, int version)
265 {
266 if (!ctx.Read(m_LockPIN))
267 {
268 m_LockPIN = "";
269 m_HasPIN = false;
270 return false;
271 }
272
273 if (!ctx.Read(m_IsLocked))
274 return false;
275
276 if (!ctx.Read(m_DoorIndex))
277 return false;
278
279 #ifdef CODELOCKCOMPONENT_LOG
280 ErrorEx(string.Format("Loaded lock pin=%1", m_LockPIN), ErrorExSeverity.INFO);
281 ErrorEx(string.Format("Lock pin length=%1", m_LockPIN.Length()), ErrorExSeverity.INFO);
282 #endif
283
284 if (m_LockPIN != "" && m_LockPIN.Length() >= m_MinLockPinLength)
285 {
286 m_HasPIN = true;
287 }
288
289 #ifdef CODELOCKCOMPONENT_LOG
290 ErrorEx(string.Format("Has pin=%1", m_HasPIN), ErrorExSeverity.INFO);
291 ErrorEx(string.Format("Is locked=%1", m_IsLocked), ErrorExSeverity.INFO);
292 #endif
293
294 SetLockState(m_IsLocked, false);
295
296 return true;
297 }
298
299 protected void CheckUIActionServer(CodeLockUIAction uiAction)
300 {
301 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
302 {
303 #ifdef CODELOCKCOMPONENT_LOG
304 ErrorEx("UI Action=" + typename.EnumToString(CodeLockUIAction, uiAction), ErrorExSeverity.INFO);
305 #endif
306
307 m_UIAction = uiAction;
309
310 g_Game.GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(ResetUIActionServer, 100);
311 }
312 }
313
314 protected void ResetUIActionServer()
315 {
316 m_UIAction = CodeLockUIAction.NONE;
318 }
319
321 {
322 #ifdef CODELOCKCOMPONENT_LOG
323 ErrorEx("UI Action=" + typename.EnumToString(CodeLockUIAction, m_UIAction), ErrorExSeverity.INFO);
324 #endif
325
326 ItemBase parentIB = ItemBase.Cast(m_Parent);
327 switch(m_UIAction)
328 {
329 case CodeLockUIAction.CLEAR_INPUT:
330 {
331 parentIB.StartItemSoundClient(SoundConstants.ITEM_DIGITAL_LOCK_CLEAR_INPUT);
332 break;
333 }
334 case CodeLockUIAction.CONFIRM_INPUT:
335 {
336 parentIB.StartItemSoundClient(SoundConstants.ITEM_DIGITAL_LOCK_CONFIRM_INPUT);
337 break;
338 }
339 case CodeLockUIAction.DIGIT_INPUT:
340 {
341 parentIB.StartItemSoundClient(SoundConstants.ITEM_DIGITAL_LOCK_DIGIT_INPUT);
342 break;
343 }
344 }
345
346 m_UIAction = CodeLockUIAction.NONE;
347 }
348
349 // Called on server on RPC request
350 protected void CheckCodeRequest(string pin, PlayerIdentity identity)
351 {
352 bool success;
353
354 if (g_Game.IsServer() || !g_Game.IsMultiplayer())
355 {
356 if (!SetInputCode(pin))
357 return;
358
359 if (m_HasPIN)
360 {
361 // Check lock state and see if pin is valid
362 if (m_IsLocked)
363 {
364 #ifdef CODELOCKCOMPONENT_LOG
365 ErrorEx("Lock pin=" + GetInputPIN(), ErrorExSeverity.INFO);
366 ErrorEx("Actual pin=" + GetLockPIN(), ErrorExSeverity.INFO);
367 #endif
368
369 if (m_InputPIN == m_LockPIN)
370 {
371 // Lock is locked and given pin is same as lock pin. Unlock lock
372 // SetLockState will handle clearing brute force protection
373 SetLockState(false, false);
374 success = true;
375 }
376 else
377 {
378 // Handle brute force protection
379 ItemBase parentIB = ItemBase.Cast(m_Parent);
380 if (parentIB)
381 parentIB.StartItemSoundServer(SoundConstants.ITEM_DIGITAL_LOCK_ERROR);
383 success = false;
384 }
385 }
386 }
387 else
388 {
389 // Lock has no pin. Set pin!
390 SetLockCode(pin, false);
391 success = true;
392 }
393 }
394
395 if (g_Game.IsDedicatedServer())
396 {
397 m_Parent.SetSynchDirty();
398 }
399
400 // Force visual update after setting code
401 DigitalCodeLock codeLock;
402 if (Class.CastTo(codeLock, m_Parent) && codeLock.GetVisualManager())
403 {
404 codeLock.GetVisualManager().UpdateVisuals();
405 }
406
407 Param1<bool> response = new Param1<bool>(success);
408 GetGame().RPCSingleParam(m_Parent, ERPCs.RPC_CODE_LOCK_RESPONSE, response, true, identity);
409 }
410
425
426 private void ActivateBruteForceProtection(float protectionTime, float protectionResetTime)
427 {
428 if (m_Stage < CodeLockStage.PROTECTION_STAGE_ONE)
429 {
430 SetStage(CodeLockStage.PROTECTION_STAGE_ONE);
431 }
432 else if (m_Stage == CodeLockStage.PROTECTION_STAGE_ONE)
433 {
434 SetStage(CodeLockStage.PROTECTION_STAGE_TWO);
435 }
436
439
441 m_BruteForceUpdateTimer.Run(protectionTime, this, "CheckBruteForceState");
442
444 m_BruteForceResetTimer.Run(protectionResetTime, this, "CheckBruteForceReset");
445
448
449 if (g_Game.IsDedicatedServer())
450 m_Parent.SetSynchDirty();
451 }
452
454 {
456 {
459 }
460
462 {
465 }
466 }
467
469 {
470 switch(m_Stage)
471 {
472 case CodeLockStage.NORMAL:
473 {
475 }
476 case CodeLockStage.PROTECTION_STAGE_ONE:
477 case CodeLockStage.PROTECTION_STAGE_TWO:
478 {
480 }
481 }
482 return false;
483 }
484
485 protected void CheckBruteForceState()
486 {
488 {
491 }
492
495
497 }
498
499 protected void CheckBruteForceReset()
500 {
502 {
505 }
506
508 {
511 }
512
516
517 if (m_HasPIN)
518 SetStage(CodeLockStage.NORMAL);
519
521 }
522
523 void OnParentDetached(bool sync = true)
524 {
525 m_DoorIndex = -1;
526 OnParentReset(false);
527
528 if (sync)
530 }
531
532 void OnParentReset(bool sync = true)
533 {
534 SetLockState(false, false);
535 ResetPIN(false);
539
541 {
544 }
545
547 {
550 }
551
552 if (sync)
554 }
555
556 void OnRPC(PlayerIdentity sender, int type, ParamsReadContext ctx)
557 {
558 switch (type)
559 {
560 case ERPCs.RPC_CODE_LOCK_REQUEST:
561 {
562 Param1<string> code = new Param1<string>("");
563 if (!ctx.Read(code))
564 return;
565
566 string dialedCode = code.param1;
567 CheckCodeRequest(dialedCode, sender);
568 SetInputCode("", true);
569 break;
570 }
571 case ERPCs.RPC_CODE_LOCK_RESPONSE:
572 {
573 Param1<bool> response = new Param1<bool>(false);
574 if (!ctx.Read(response))
575 return;
576
577 DigitalCodeLockUIBase codeLockUI = DigitalCodeLockUIBase.Cast(GetGame().GetUIManager().FindMenu(MENU_DIGITAL_CODELOCK));
578 if (codeLockUI)
579 {
580 bool codeResponse = response.param1;
581 codeLockUI.CodeResponse(codeResponse);
582 SetInputCode("", true);
583 }
584 break;
585 }
586 case ERPCs.RPC_CODE_LOCK_UI_ACTION:
587 {
588 Param1<int> action = new Param1<int>(CodeLockUIAction.NONE);
589 if (!ctx.Read(action))
590 return;
591
592 CodeLockUIAction uiAction = action.param1;
593 if (uiAction != CodeLockUIAction.NONE)
594 CheckUIActionServer(uiAction);
595 break;
596 }
597 }
598 }
599
600 protected void UpdateParentVisuals()
601 {
602 if (!m_Parent)
603 return;
604
605 DigitalCodeLock codeLock;
606 if (Class.CastTo(codeLock, m_Parent) && codeLock.GetVisualManager())
607 {
608 codeLock.GetVisualManager().MarkDirty();
609 codeLock.GetVisualManager().UpdateVisuals();
610 }
611 }
612
613 protected void SyncAndMarkDirty()
614 {
615 // Always update parent visuals (will be marked dirty or updated depending on context)
617
618 if (g_Game.IsDedicatedServer())
619 m_Parent.SetSynchDirty();
620 }
621
623 {
624 return m_Parent;
625 }
626
627 void SetDoorIndex(int index)
628 {
629 m_DoorIndex = index;
630 }
631
633 {
634 return m_DoorIndex;
635 }
636
639 {
640 return m_Parent.GetPosition();
641 }
642
643 string GetInputPIN()
644 {
645 return m_InputPIN;
646 }
647
648 bool HasPIN()
649 {
650 return m_HasPIN;
651 }
652
653 string GetLockPIN()
654 {
655 return m_LockPIN;
656 }
657
658 bool IsLocked()
659 {
660 return m_IsLocked;
661 }
662
664 {
665 return m_Parent || m_DoorIndex > -1;
666 }
667
672
677
679 {
680 return m_WrongCodeCount;
681 }
682
684 {
685 return m_MinLockPinLength;
686 }
687
688 CodeLockUIAction GetUIAction()
689 {
690 return m_UIAction;
691 }
692}
693
695{
696 const string SELECTION_INTERACT = "codelockinteract";
697
699 {
700 vector position = m_Parent.GetSelectionBasePositionLS(SELECTION_INTERACT);
701 return m_Parent.ModelToWorld(position);
702 }
703}
void OnRPC(PlayerIdentity sender, int rpcType, ParamsReadContext ctx)
Определения BunkerBroadcastComponent.c:182
Entity m_Parent
Определения CachedEquipmentStorageBase.c:17
void ResetPIN()
Определения CodeLock.c:925
void CheckBruteForceReset()
Определения CodeLockComponent.c:499
void CheckCodeRequest(string pin, PlayerIdentity identity)
Определения CodeLockComponent.c:350
float m_BruteForceResetTime
Определения CodeLockComponent.c:41
void ResetUIActionServer()
Определения CodeLockComponent.c:314
CodeLockUIAction m_UIAction
Определения CodeLockComponent.c:49
void SetStage(CodeLockStage stage)
Определения CodeLockComponent.c:76
void CheckUIActionClient()
Определения CodeLockComponent.c:320
int GetDoorIndex()
Определения CodeLockComponent.c:632
int m_MinLockPinLength
Определения CodeLockComponent.c:29
void CheckUIActionServer(CodeLockUIAction uiAction)
Определения CodeLockComponent.c:299
string GetLockPIN()
Определения CodeLockComponent.c:653
bool HasPIN()
Определения CodeLockComponent.c:648
bool ValidatePIN(string pin)
Определения CodeLockComponent.c:112
void StopAllTimers()
Определения CodeLockComponent.c:453
float m_BruteForceProtectionTime
Определения CodeLockComponent.c:40
int m_WrongCodeCount
Определения CodeLockComponent.c:46
bool m_HasBruteForceProtection
Определения CodeLockComponent.c:47
enum CodeLockStage CONFIRM_INPUT
ref Timer m_BruteForceUpdateTimer
Определения CodeLockComponent.c:44
void ActivateBruteForceProtection(float protectionTime, float protectionResetTime)
Определения CodeLockComponent.c:426
bool ShouldTriggerProtectionStage()
Определения CodeLockComponent.c:468
bool SetInputCode(string pin, bool force=true)
Определения CodeLockComponent.c:94
bool m_IsLocked
Определения CodeLockComponent.c:34
void HandleHierarchyRootLock(bool state)
Определения CodeLockComponent.c:212
void SetMinimalPinCodeLength(int length)
Определения CodeLockComponent.c:89
void OnParentDetached(bool sync=true)
Определения CodeLockComponent.c:523
bool SetLockCode(string pin, bool sync=true)
Определения CodeLockComponent.c:121
void HandleBruteForceProtectionServer()
Определения CodeLockComponent.c:411
int m_Stage
Определения CodeLockComponent.c:43
void CodeLockComponent(notnull EntityAI parent, int minLockPinLength=MIN_LOCK_PIN_LENGTH)
Определения CodeLockComponent.c:51
EntityAI m_Parent
Определения CodeLockComponent.c:35
bool IsLocked()
Определения CodeLockComponent.c:658
int m_DoorIndex
Определения CodeLockComponent.c:36
ref Timer m_BruteForceResetTimer
Определения CodeLockComponent.c:45
bool HasBruteForceProtection()
Определения CodeLockComponent.c:668
string m_LockPIN
Определения CodeLockComponent.c:32
string m_InputPIN
Определения CodeLockComponent.c:31
void OnParentReset(bool sync=true)
Определения CodeLockComponent.c:532
bool HasParent()
Определения CodeLockComponent.c:663
enum CodeLockStage CLEAR_INPUT
enum CodeLockStage MIN_LOCK_PIN_LENGTH
bool OnStoreLoad(ParamsReadContext ctx, int version)
Определения CodeLockComponent.c:264
CodeLockStage
Определения CodeLockComponent.c:5
@ SETTING
Определения CodeLockComponent.c:8
@ PROTECTION_STAGE_ONE
Определения CodeLockComponent.c:9
@ PROTECTION_STAGE_TWO
Определения CodeLockComponent.c:10
bool m_HasPIN
Определения CodeLockComponent.c:38
void SetLockState(bool state, bool sync=true)
Определения CodeLockComponent.c:157
int GetStage()
Определения CodeLockComponent.c:84
bool ShouldHandleBruteForce()
Определения CodeLockComponent.c:673
CodeLockUIAction GetUIAction()
Определения CodeLockComponent.c:688
void SetDoorIndex(int index)
Определения CodeLockComponent.c:627
void OnStoreSave(ParamsWriteContext ctx)
Определения CodeLockComponent.c:257
int GetWrongCodeCount()
Определения CodeLockComponent.c:678
int GetMinLockPINLength()
Определения CodeLockComponent.c:683
const float DEFAULT_BRUTEFORCE_RESET_TIME
Определения CodeLockComponent.c:27
void SyncAndMarkDirty()
Определения CodeLockComponent.c:613
string GetInputPIN()
Определения CodeLockComponent.c:643
const float DEFAULT_BRUTEFORCE_TIME
Определения CodeLockComponent.c:26
void CheckBruteForceState()
Определения CodeLockComponent.c:485
const int MAX_LOCK_PIN_LENGTH
Определения CodeLockComponent.c:24
void UpdateParentVisuals()
Определения CodeLockComponent.c:600
bool m_HandleBruteForceProtection
Определения CodeLockComponent.c:48
DayZGame g_Game
Определения DayZGame.c:4074
vector position
Определения DustKickupEffects.c:20
@ NORMAL
Определения ECameraZoomType.c:4
ERPCs
Определения ERPCs.c:2
proto native void LockDoor(int index, bool force=false)
Locks the door if not already locked, resets the door health. 'force = true' will close the door if o...
proto native int GetDoorCount()
Returns the number of the doors in the building.
proto native void UnlockDoor(int index, bool animate=true)
Unlocks the door if locked, AJAR animation optional.
static float GetExternalLockProtectionResetTime()
Определения CfgGameplayHandler.c:518
static float GetExternalLockProtectionCountStageTwo()
Определения CfgGameplayHandler.c:528
static float GetExternalLockProtectionTime()
Определения CfgGameplayHandler.c:513
static float GetExternalLockProtectionCountStageOne()
Определения CfgGameplayHandler.c:523
Super root of all classes in Enforce script.
Определения EnScript.c:11
const string SELECTION_INTERACT
Определения CodeLockComponent.c:696
override vector GetPosition()
used on static objects
Определения CodeLockComponent.c:698
CodeLockVisualManager GetVisualManager()
Определения CodeLock.c:435
Определения CodeLock.c:2
void CodeResponse(bool response)
TODO doc.
Определения EnScript.c:118
The class that will be instanced (moddable)
Определения gameplay.c:389
proto bool Write(void value_out)
proto bool Read(void value_in)
Определения DayZPlayerImplement.c:39
Определения EnConvert.c:119
Serializer ParamsReadContext
Определения gameplay.c:15
Serializer ParamsWriteContext
Определения gameplay.c:16
DayZGame GetGame()
Определения gameplay.c:636
ErrorExSeverity
Определения EnDebug.c:62
enum ShapeType ErrorEx
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
const int MENU_DIGITAL_CODELOCK
Определения 3_Game/DayZ/constants.c:222
@ NONE
body is not in simulation, nor in collision world
Определения SimulationState.c:15
vector GetPosition()
Get the world position of the Effect.
Определения Effect.c:473
proto native int Length()
Returns length of string.
const int CALL_CATEGORY_SYSTEM
Определения 3_Game/DayZ/tools/tools.c:8
proto native Widget GetParent()
Get parent of the Effect.
Определения Effect.c:422