DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
CombinationLock.c
Go to the documentation of this file.
2{
8
11
12class 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
32
34 {
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;
51 m_IsLocked = false;
52 }
53
54 override void EEInit()
55 {
56 super.EEInit();
57
59 //SetInitialized();
60
61 //set visual on init
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 );
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
152 {
153 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
159 }
160 }
161
162
164 {
166 }
167
169 {
170 super.OnVariablesSynchronized();
171 //update visuals (client)
173
174 //update sound (client)
176 UpdateSound();
177
178 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
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 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 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
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
391 {
392 GetGame().ObjectDelete( this );
393 }
394
395 // --- VISUALS
397 {
398 //Client/Server
399 if ( IsLockedOnGate() )
400 {
403 }
404 else
405 {
408 }
409 }
410
412 {
413 //was locked
414 if ( m_LockActionPerformed == LockAction.LOCKED )
415 {
417 }
418
419 //was unlocked
420 if ( m_LockActionPerformed == LockAction.UNLOCKED )
421 {
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}
InventoryMode
Definition Inventory.c:20
ActionDialCombinationLockCB ActionContinuousBaseCB ActionDialCombinationLock()
ActionDialCombinationLockOnTargetCB ActionContinuousBaseCB ActionDialCombinationLockOnTarget()
void AddAction(typename actionName)
void SetActions()
override void EEInit()
class BaseBuildingBase extends ItemBase bsbDebugPrint(string s)
override bool IsInitialized()
void SetInitialized()
void ShuffleLock()
void UpdateSound()
void ResetActionVar()
const string SOUND_LOCK_CHANGE_NUMBER
int GetLockDigits()
bool IsLockedOnGate()
protected void SoundLockOpen()
const string SOUND_LOCK_CHANGE_DIAL
void SetNextDial()
void LockServer(EntityAI parent, bool ignore_combination=false)
void SoundLockChangeNumber()
const string SOUND_LOCK_OPEN
enum LockAction m_LockDigits
protected void ShowAttached()
protected void SoundLockClose()
void CheckLockedStateServer()
void SetCombination(int combination)
void SetCombinationLocked(int combination)
LockAction
@ COUNT
@ DIAL_INDEX_CHANGED
@ UNLOCKED
@ LOCKED
@ DIAL_NUMBER_CHANED
@ NONE
protected void ShowItem()
int GetCombination()
void Synchronize()
void SoundLockChangeDial()
int m_Combination
protected LockAction m_LockActionPerformed
const string SOUND_LOCK_CLOSE
void UnlockServer(EntityAI player, EntityAI parent)
int m_CombinationLocked
bool IsLockAttached()
protected void HideItem()
protected void HideAttached()
int m_DialIndex
int GetDialIndex()
void DialNextNumber()
void DestroyLock()
protected bool m_IsInitialized
void UpdateVisuals()
void AfterStoreLoad()
EffectSound m_Sound
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition ItemBase.c:5779
override void SetTakeable(bool pState)
Definition ItemBase.c:8843
override void OnVariablesSynchronized()
bool m_IsLocked
bool IsLocked()
void OnStoreSave(ParamsWriteContext ctx)
bool OnStoreLoad(ParamsReadContext ctx, int version)
proto native void ObjectDelete(Object obj)
override ScriptCallQueue GetCallQueue(int call_category)
Definition DayZGame.c:1153
override void SetBaseLockValues()
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
override bool ServerDropEntity(notnull EntityAI item)
Definition Man.c:113
InventoryLocation.
proto native int GetSlot()
returns slot id if current type is Attachment
Definition EnMath.c:7
proto void CallLater(func fn, int delay=0, bool repeat=false, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL)
adds call into the queue with given parameters and arguments (arguments are held in memory until the ...
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
proto bool Write(void value_out)
proto bool Read(void value_in)
proto native CGame GetGame()
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition Effect.c:427
static proto int RandomInt(int min, int max)
Returns a random int number between and min [inclusive] and max [exclusive].
static proto float Pow(float v, float power)
Return power of v ^ power.
proto native int ToInt()
Converts string to integer.
string Get(int index)
Gets n-th character from string.
Definition EnString.c:434
static proto string ToString(void var, bool type=false, bool name=false, bool quotes=true)
Return string representation of variable.
proto native int Length()
Returns length of string.
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8