Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
combinationlock.c
Go to the documentation of this file.
2 {
3  NONE,
8 
10 }
11 
12 class CombinationLock extends ItemBase
13 {
14  int m_LockDigits; //how many digits will the combination contain
15  int m_Combination; //actual combination that is dialed on lock
16  int m_CombinationLocked; //combination that was dialed on lock before the shuffle
17  int m_DialIndex; //index of current combination dial
18  protected bool m_IsLocked;
19 
21 
22  protected bool m_IsInitialized;
23 
24  //Sounds
25  //build
26  const string SOUND_LOCK_OPEN = "combinationlock_open_SoundSet";
27  const string SOUND_LOCK_CLOSE = "combinationlock_close_SoundSet";
28  const string SOUND_LOCK_CHANGE_NUMBER = "combinationlock_changenumber_SoundSet";
29  const string SOUND_LOCK_CHANGE_DIAL = "combinationlock_changedial_SoundSet";
30 
31  protected EffectSound m_Sound;
32 
34  {
35  SetBaseLockValues();
36 
37  //synchronized variables
38  int combination_length = Math.Pow( 10, m_LockDigits );
39  RegisterNetSyncVariableBool( "m_IsLocked" );
40  RegisterNetSyncVariableInt( "m_Combination", 0, combination_length - 1 );
41  RegisterNetSyncVariableInt( "m_DialIndex", 0, m_LockDigits - 1 );
42  RegisterNetSyncVariableInt( "m_LockActionPerformed", 0, LockAction.COUNT );
43  }
44 
45  protected void SetBaseLockValues()
46  {
47  //set lock init values
48  m_LockDigits = 3;
49  m_Combination = 111;
50  m_CombinationLocked = 999;
51  m_IsLocked = false;
52  }
53 
54  override void EEInit()
55  {
56  super.EEInit();
57 
58  GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( SetInitialized, 1000, false );
59  //SetInitialized();
60 
61  //set visual on init
62  UpdateVisuals();
63  }
64 
66  {
67  m_IsInitialized = true;
68  }
69 
70  override bool IsInitialized()
71  {
72  return m_IsInitialized;
73  }
74 
75  override void OnItemLocationChanged( EntityAI old_owner, EntityAI new_owner )
76  {
77  super.OnItemLocationChanged( old_owner, new_owner );
78 
79  //Check combination lock
80  if ( GetGame().IsServer() )
81  {
82  if ( IsInitialized() && new_owner && new_owner.IsInherited( BaseBuildingBase ) )
83  {
84  LockServer( new_owner );
85  }
86  }
87  }
88 
89  // --- EVENTS
90  override void OnStoreSave( ParamsWriteContext ctx )
91  {
92  super.OnStoreSave( ctx );
93 
94  //write data
95  ctx.Write( m_Combination );
96  ctx.Write( m_CombinationLocked );
97  }
98 
99  override bool OnStoreLoad( ParamsReadContext ctx, int version )
100  {
101  if ( !super.OnStoreLoad( ctx, version ) )
102  return false;
103 
104  //--- Combination Lock data ---
105  //combination
106  if ( !ctx.Read( m_Combination ) )
107  {
108  m_Combination = 0;
109  return false;
110  }
111 
112  //combination locked
113  if ( !ctx.Read( m_CombinationLocked ) )
114  {
116  return false;
117  }
118 
119  //is lock attached
120  if ( version < 105 ) //removed in 105
121  {
122  bool is_lock_attached;
123  if ( !ctx.Read( is_lock_attached ) )
124  {
125  return false;
126  }
127  }
128 
129  return true;
130  }
131 
132  override void AfterStoreLoad()
133  {
134  super.AfterStoreLoad();
135 
136  //Check combination lock
137  if ( GetGame().IsServer() )
138  {
139  EntityAI parent = GetHierarchyParent();
140  if ( parent && parent.IsInherited( BaseBuildingBase ) )
141  {
142  LockServer( parent, true );
143  }
144  }
145 
146  //synchronize
147  Synchronize();
148  }
149 
150  // --- SYNCHRONIZATION
151  void Synchronize()
152  {
153  if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.Synchronize " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
154  if ( GetGame().IsServer() )
155  {
156  SetSynchDirty();
157  GetGame().GetCallQueue( CALL_CATEGORY_SYSTEM ).CallLater( ResetActionVar, 1000);//synced var used to trigger client sound needs to be reset after triggering the sound
158  UpdateVisuals();
159  }
160  }
161 
162 
164  {
166  }
167 
168  override void OnVariablesSynchronized()
169  {
170  super.OnVariablesSynchronized();
171  //update visuals (client)
172  UpdateVisuals();
173 
174  //update sound (client)
176  UpdateSound();
177 
178  if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.OnVariablesSynchronized " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
179  }
180 
181  void SetCombination( int combination )
182  {
183  m_Combination = combination;
184  }
185 
186  void SetCombinationLocked( int combination )
187  {
188  m_CombinationLocked = combination;
189  }
190 
192  {
193  return m_Combination;
194  }
195 
197  {
198  return m_LockDigits;
199  }
200 
201  // --- ACTIONS
203  {
204  string combination_text = m_Combination.ToString();
205  string dialed_text;
206 
207  //insert zeros to dials with 0 value
208  int length_diff = m_LockDigits - combination_text.Length();
209  for ( int i = 0; i < length_diff; ++i )
210  {
211  combination_text = "0" + combination_text;
212  }
213 
214  //assemble the whole combination with increased part
215  for ( int j = 0; j < combination_text.Length(); ++j )
216  {
217  if ( j == m_DialIndex )
218  {
219  int next_dialed_number = combination_text.Get( j ).ToInt() + 1;
220  if ( next_dialed_number > 9 )
221  {
222  next_dialed_number = 0;
223  }
224 
225  dialed_text += next_dialed_number.ToString();
226  }
227  else
228  {
229  dialed_text += combination_text.Get( j );
230  }
231  }
232 
233  //set new number
234  SetCombination( dialed_text.ToInt() );
235  m_LockActionPerformed = LockAction.DIAL_INDEX_CHANGED;
237 
238  //synchronize
239  Synchronize();
240  }
241 
243  {
244  return m_DialIndex;
245  }
246 
247  void SetNextDial()
248  {
249  if ( m_LockDigits > 1 )
250  {
251  if ( m_DialIndex <= m_LockDigits - 2 )
252  {
253  m_DialIndex++;
254  }
255  else if ( m_DialIndex >= m_LockDigits > - 1 )
256  {
257  m_DialIndex = 0;
258  }
259  }
260  else
261  {
262  m_DialIndex = 0;
263  }
264 
265  //performed action
266  m_LockActionPerformed = LockAction.DIAL_NUMBER_CHANED;
267  //synchronize
268  Synchronize();
269  }
270 
271  //Lock lock
272  void LockServer( EntityAI parent, bool ignore_combination = false )
273  {
274  if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.LockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
275  if ( IsLockAttached() )
276  {
277  if ( !ignore_combination )
278  {
280 
281  //set slot lock
282  InventoryLocation inventory_location = new InventoryLocation;
283  GetInventory().GetCurrentInventoryLocation( inventory_location );
284  parent.GetInventory().SetSlotLock( inventory_location.GetSlot(), true );
285 
287  }
288  ShuffleLock();
289  SetTakeable(false);
291  //synchronize
292  Synchronize();
293  }
294 
295  //reset performed action
296  //m_LockActionPerformed = LockAction.NONE;
297  }
298 
299  void UnlockServer( EntityAI player, EntityAI parent )
300  {
301  if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.UnlockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
302  if ( IsLockAttached() )
303  {
304  Fence fence = Fence.Cast( parent );
305 
306  //set slot unlock
307  InventoryLocation inventory_location = new InventoryLocation;
308  GetInventory().GetCurrentInventoryLocation( inventory_location );
309  fence.GetInventory().SetSlotLock( inventory_location.GetSlot(), false );
310 
311  //drop entity from attachment slot
312  if (player)
313  player.ServerDropEntity( this );
314  else
315  parent.GetInventory().DropEntity(InventoryMode.SERVER, parent, this);
316  SetPosition( fence.GetKitSpawnPosition() );
317  PlaceOnSurface();
318 
320  SetTakeable(true);
322  //synchronize
323  Synchronize();
324  }
325 
326  //reset performed action
327  //m_LockActionPerformed = LockAction.NONE;
328  }
329 
330  //Shuffle lock
331  void ShuffleLock()
332  {
333  string combination_text = m_Combination.ToString();
334  string shuffled_text;
335 
336  //insert zeros to dials with 0 value
337  int length_diff = m_LockDigits - combination_text.Length();
338  for ( int i = 0; i < length_diff; ++i )
339  {
340  combination_text = "0" + combination_text;
341  }
342 
343  //assemble the whole combination with increased part
344  for ( int j = 0; j < combination_text.Length(); ++j )
345  {
346  int dial_number = combination_text.Get( j ).ToInt();
347  dial_number = ( dial_number + Math.RandomInt( 1, 9 ) ) % 10;
348  shuffled_text = shuffled_text + dial_number.ToString();
349  }
350 
351  SetCombination( shuffled_text.ToInt() );
352  }
353 
354  bool IsLocked()
355  {
356  return m_IsLocked;
357  }
358 
360  {
362  }
363 
365  {
366  Fence fence = Fence.Cast( GetHierarchyParent() );
367  if ( fence )
368  {
369  if ( IsLocked() )
370  {
371  return true;
372  }
373  }
374 
375  return false;
376  }
377 
379  {
380  Fence fence = Fence.Cast( GetHierarchyParent() );
381  if ( fence )
382  {
383  return true;
384  }
385 
386  return false;
387  }
388 
389  //destroy lock
390  void DestroyLock()
391  {
392  GetGame().ObjectDelete( this );
393  }
394 
395  // --- VISUALS
397  {
398  //Client/Server
399  if ( IsLockedOnGate() )
400  {
401  GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( HideItem, 0, false );
402  GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( ShowAttached, 0, false );
403  }
404  else
405  {
406  GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( ShowItem, 0, false );
407  GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( HideAttached, 0, false );
408  }
409  }
410 
411  void UpdateSound()
412  {
413  //was locked
414  if ( m_LockActionPerformed == LockAction.LOCKED )
415  {
416  SoundLockClose();
417  }
418 
419  //was unlocked
420  if ( m_LockActionPerformed == LockAction.UNLOCKED )
421  {
422  SoundLockOpen();
423  }
424 
425  //next dial index
426  if ( m_LockActionPerformed == LockAction.DIAL_INDEX_CHANGED )
427  {
429  }
430 
431  //dialed new number
432  if ( m_LockActionPerformed == LockAction.DIAL_NUMBER_CHANED )
433  {
435  }
436  }
437 
438  //Show/Hide anims
439  protected void ShowItem()
440  {
441  SetAnimationPhase( "Combination_Lock_Item", 0 );
442  SetAnimationPhase( "Lock_Item_1", 0 );
443  SetAnimationPhase( "Lock_Item_2", 0 );
444  }
445 
446  protected void HideItem()
447  {
448  SetAnimationPhase( "Combination_Lock_Item", 1 );
449  SetAnimationPhase( "Lock_Item_1", 1 );
450  SetAnimationPhase( "Lock_Item_2", 1 );
451  }
452 
453  protected void ShowAttached()
454  {
455  SetAnimationPhase( "Combination_Lock_Attached", 0 );
456  SetAnimationPhase( "Lock_Attached_1", 0 );
457  SetAnimationPhase( "Lock_Attached_2", 0 );
458  }
459 
460  protected void HideAttached()
461  {
462  SetAnimationPhase( "Combination_Lock_Attached", 1 );
463  SetAnimationPhase( "Lock_Attached_1", 1 );
464  SetAnimationPhase( "Lock_Attached_2", 1 );
465  }
466  // ---
467 
468  //================================================================
469  // SOUNDS
470  //================================================================
471  protected void SoundLockOpen()
472  {
473  PlaySoundSet( m_Sound, SOUND_LOCK_OPEN, 0, 0 );
474  }
475 
476  protected void SoundLockClose()
477  {
478  PlaySoundSet( m_Sound, SOUND_LOCK_CLOSE, 0, 0 );
479  }
480 
482  {
483  PlaySoundSet( m_Sound, SOUND_LOCK_CHANGE_NUMBER, 0, 0 );
484  }
485 
487  {
488  PlaySoundSet( m_Sound, SOUND_LOCK_CHANGE_DIAL, 0, 0 );
489  }
490 
491  override void SetActions()
492  {
493  super.SetActions();
494 
500  }
501 }
ItemBase
Definition: inventoryitem.c:730
GetDialIndex
int GetDialIndex()
Definition: combinationlock.c:242
DialNextNumber
void DialNextNumber()
Definition: combinationlock.c:202
GetGame
proto native CGame GetGame()
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
SetNextDial
void SetNextDial()
Definition: combinationlock.c:247
EEInit
override void EEInit()
Definition: combinationlock.c:54
ActionDialCombinationLockOnTarget
ActionDigGardenPlotCB ActionDialCombinationLockOnTarget
IsLockAttached
bool IsLockAttached()
Definition: combinationlock.c:378
SoundLockChangeNumber
void SoundLockChangeNumber()
Definition: combinationlock.c:481
CALL_CATEGORY_GAMEPLAY
const int CALL_CATEGORY_GAMEPLAY
Definition: tools.c:10
LogManager
Definition: debug.c:734
COUNT
@ COUNT
Definition: combinationlock.c:9
CombinationLock
Definition: combinationlock4.c:1
DIAL_NUMBER_CHANED
@ DIAL_NUMBER_CHANED
Definition: combinationlock.c:4
UNLOCKED
@ UNLOCKED
Definition: combinationlock.c:7
ActionNextCombinationLockDial
Definition: actionnextcombinationlockdial.c:1
m_Combination
int m_Combination
Definition: combinationlock.c:15
ActionAttachToConstruction
Definition: actionattachtoconstruction.c:1
m_Sound
protected EffectSound m_Sound
Definition: combinationlock.c:31
SetActions
override void SetActions()
Definition: combinationlock.c:491
CheckLockedStateServer
void CheckLockedStateServer()
Definition: combinationlock.c:359
m_IsInitialized
protected bool m_IsInitialized
Definition: combinationlock.c:22
SOUND_LOCK_OPEN
const string SOUND_LOCK_OPEN
Definition: combinationlock.c:26
SetCombinationLocked
void SetCombinationLocked(int combination)
Definition: combinationlock.c:186
GetCombination
int GetCombination()
Definition: combinationlock.c:191
InventoryLocation
InventoryLocation.
Definition: inventorylocation.c:27
LOCKED
@ LOCKED
Definition: combinationlock.c:6
m_IsLocked
protected bool m_IsLocked
Definition: combinationlock.c:18
m_LockDigits
enum LockAction m_LockDigits
SOUND_LOCK_CHANGE_DIAL
const string SOUND_LOCK_CHANGE_DIAL
Definition: combinationlock.c:29
ShowAttached
protected void ShowAttached()
Definition: combinationlock.c:453
m_LockActionPerformed
protected LockAction m_LockActionPerformed
Definition: combinationlock.c:20
SetInitialized
void SetInitialized()
Definition: combinationlock.c:65
GetLockDigits
int GetLockDigits()
Definition: combinationlock.c:196
SetTakeable
override void SetTakeable(bool pState)
Definition: itembase.c:4226
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
UpdateVisuals
void UpdateVisuals()
Definition: combinationlock.c:396
Synchronize
void Synchronize()
Definition: combinationlock.c:151
ShowItem
protected void ShowItem()
Definition: combinationlock.c:439
DIAL_INDEX_CHANGED
@ DIAL_INDEX_CHANGED
Definition: combinationlock.c:5
EffectSound
Wrapper class for managing sound through SEffectManager.
Definition: effectsound.c:4
ActionDialCombinationLock
ActionDialCombinationLockCB ActionContinuousBaseCB ActionDialCombinationLock()
Definition: actiondialcombinationlock.c:13
InventoryMode
InventoryMode
NOTE: PREDICTIVE is not to be used at all in multiplayer.
Definition: inventory.c:21
SetCombination
void SetCombination(int combination)
Definition: combinationlock.c:181
BaseBuildingBase
Definition: fence.c:1
m_CombinationLocked
int m_CombinationLocked
Definition: combinationlock.c:16
DestroyLock
void DestroyLock()
Definition: combinationlock.c:390
ActionNextCombinationLockDialOnTarget
Definition: actionnextcombinationlockdialontarget.c:1
CombinationLock
void CombinationLock()
Definition: combinationlock.c:33
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
HideAttached
protected void HideAttached()
Definition: combinationlock.c:460
IsInitialized
override bool IsInitialized()
Definition: combinationlock.c:70
IsLockedOnGate
bool IsLockedOnGate()
Definition: combinationlock.c:364
m_DialIndex
int m_DialIndex
Definition: combinationlock.c:17
OnVariablesSynchronized
override void OnVariablesSynchronized()
Definition: combinationlock.c:168
bsbDebugPrint
Chemlight_ColorBase bsbDebugPrint
NONE
@ NONE
Definition: combinationlock.c:3
SetPosition
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition: effect.c:436
SoundLockClose
protected void SoundLockClose()
Definition: combinationlock.c:476
SOUND_LOCK_CLOSE
const string SOUND_LOCK_CLOSE
Definition: combinationlock.c:27
LockServer
void LockServer(EntityAI parent, bool ignore_combination=false)
Definition: combinationlock.c:272
UnlockServer
void UnlockServer(EntityAI player, EntityAI parent)
Definition: combinationlock.c:299
AfterStoreLoad
override void AfterStoreLoad()
Definition: combinationlock.c:132
SOUND_LOCK_CHANGE_NUMBER
const string SOUND_LOCK_CHANGE_NUMBER
Definition: combinationlock.c:28
UpdateSound
void UpdateSound()
Definition: combinationlock.c:411
SoundLockChangeDial
void SoundLockChangeDial()
Definition: combinationlock.c:486
ShuffleLock
void ShuffleLock()
Definition: combinationlock.c:331
OnStoreLoad
override bool OnStoreLoad(ParamsReadContext ctx, int version)
Definition: combinationlock.c:99
Math
Definition: enmath.c:6
HideItem
protected void HideItem()
Definition: combinationlock.c:446
LockAction
LockAction
Definition: combinationlock.c:1
OnItemLocationChanged
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition: combinationlock.c:75
EntityAI
Definition: building.c:5
OnStoreSave
override void OnStoreSave(ParamsWriteContext ctx)
Definition: combinationlock.c:90
ResetActionVar
void ResetActionVar()
Definition: combinationlock.c:163
IsLocked
bool IsLocked()
Definition: combinationlock.c:354
SoundLockOpen
protected void SoundLockOpen()
Definition: combinationlock.c:471