DayZ 1.24
Loading...
Searching...
No Matches
CombinationLock.c
Go to the documentation of this file.
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
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
58 GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(SetInitialized, 1000, false);
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
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))
84 }
85 }
86
87 // --- EVENTS
89 {
90 super.OnStoreSave(ctx);
91
92 //write data
93 ctx.Write(m_Combination);
95 }
96
97 override bool OnStoreLoad(ParamsReadContext ctx, int version)
98 {
99 if (!super.OnStoreLoad(ctx, version))
100 return false;
101
102 //--- Combination Lock data ---
103 //combination
104 if (!ctx.Read(m_Combination))
105 {
106 m_Combination = 0;
107 return false;
108 }
109
110 //combination locked
111 if (!ctx.Read(m_CombinationLocked))
112 {
114 return false;
115 }
116
117 //is lock attached
118 if (version < 105) //removed in 105
119 {
120 bool is_lock_attached;
121 if (!ctx.Read(is_lock_attached))
122 return false;
123 }
124
125 return true;
126 }
127
128 override void AfterStoreLoad()
129 {
130 super.AfterStoreLoad();
131
132 //Check combination lock
133 if (GetGame().IsServer())
134 {
135 EntityAI parent = GetHierarchyParent();
136 if (parent && parent.IsInherited(BaseBuildingBase))
137 LockServer(parent, true);
138 }
139
140 //synchronize
141 Synchronize();
142 }
143
144 // --- SYNCHRONIZATION
146 {
147 if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.Synchronize " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
148 if (GetGame().IsServer())
149 {
150 SetSynchDirty();
151 GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(ResetActionVar, 1000); //synced var used to trigger client sound needs to be reset after triggering the sound
153 }
154 }
155
156
158 {
160 }
161
163 {
164 super.OnVariablesSynchronized();
165 //update visuals (client)
167
168 //update sound (client)
170 UpdateSound();
171
172 if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.OnVariablesSynchronized " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
173 }
174
179
184
186 {
187 return m_Combination;
188 }
189
191 {
192 return m_LockDigits;
193 }
194
195 // --- ACTIONS
197 {
198 string combination_text = m_Combination.ToString();
199 string dialed_text;
200
201 //insert zeros to dials with 0 value
203 for (int i = 0; i < length_diff; ++i)
205
206 //assemble the whole combination with increased part
207 for (int j = 0; j < combination_text.Length(); ++j)
208 {
209 if (j == m_DialIndex)
210 {
211 int next_dialed_number = combination_text.Get(j).ToInt() + 1;
212 if (next_dialed_number > 9)
214
215 dialed_text += next_dialed_number.ToString();
216 }
217 else
219 }
220
221 //set new number
223 m_LockActionPerformed = LockAction.DIAL_INDEX_CHANGED;
225
226 //synchronize
227 Synchronize();
228 }
229
231 {
232 return m_DialIndex;
233 }
234
236 {
237 if (m_LockDigits > 1)
238 {
239 if (m_DialIndex <= m_LockDigits - 2)
240 m_DialIndex++;
241 else if (m_DialIndex >= m_LockDigits > - 1)
242 m_DialIndex = 0;
243 }
244 else
245 m_DialIndex = 0;
246
247 //performed action
248 m_LockActionPerformed = LockAction.DIAL_NUMBER_CHANED;
249 //synchronize
250 Synchronize();
251 }
252
253 //Lock lock
254 void LockServer(EntityAI parent, bool ignore_combination = false)
255 {
256 if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.LockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
257 if (IsLockAttached())
258 {
260 {
262
263 //set slot lock
265 GetInventory().GetCurrentInventoryLocation(inventory_location);
266 parent.GetInventory().SetSlotLock(inventory_location.GetSlot(), true);
267
269 }
270 ShuffleLock();
271 SetTakeable(false);
273 //synchronize
274 Synchronize();
275 }
276
277 //reset performed action
278 //m_LockActionPerformed = LockAction.NONE;
279 }
280
282 {
283 if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.UnlockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
284 if (IsLockAttached())
285 {
286 Fence fence = Fence.Cast(parent);
287
288 //set slot unlock
290 GetInventory().GetCurrentInventoryLocation(inventory_location);
291 fence.GetInventory().SetSlotLock(inventory_location.GetSlot(), false);
292
293 //drop entity from attachment slot
294 if (player)
295 player.ServerDropEntity(this);
296 else
297 parent.GetInventory().DropEntity(InventoryMode.SERVER, parent, this);
298 SetPosition(fence.GetKitSpawnPosition());
299 PlaceOnSurface();
300
302 SetTakeable(true);
304 //synchronize
305 Synchronize();
306 }
307
308 //reset performed action
309 //m_LockActionPerformed = LockAction.NONE;
310 }
311
312 //Shuffle lock
314 {
315 string combination_text = m_Combination.ToString();
316 string shuffled_text;
317
318 //insert zeros to dials with 0 value
320 for (int i = 0; i < length_diff; ++i)
322
323 //assemble the whole combination with increased part
324 for (int j = 0; j < combination_text.Length(); ++j)
325 {
326 int dial_number = combination_text.Get(j).ToInt();
327 dial_number = (dial_number + Math.RandomInt(1, 9)) % 10;
329 }
330
332 }
333
334 bool IsLocked()
335 {
336 return m_IsLocked;
337 }
338
343
345 {
346 Fence fence = Fence.Cast(GetHierarchyParent());
347 if (fence)
348 {
349 if (IsLocked())
350 return true;
351 }
352
353 return false;
354 }
355
357 {
358 Fence fence = Fence.Cast(GetHierarchyParent());
359 if (fence)
360 return true;
361
362 return false;
363 }
364
365 //destroy lock
367 {
368 GetGame().ObjectDelete(this);
369 }
370
371 // --- VISUALS
373 {
374 //Client/Server
375 if (IsLockedOnGate())
376 {
377 GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(HideItem, 0, false);
378 GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(ShowAttached, 0, false);
379 }
380 else
381 {
382 GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(ShowItem, 0, false);
383 GetGame().GetCallQueue(CALL_CATEGORY_GAMEPLAY).CallLater(HideAttached, 0, false);
384 }
385 }
386
388 {
389 //was locked
390 if (m_LockActionPerformed == LockAction.LOCKED)
392
393 //was unlocked
394 if (m_LockActionPerformed == LockAction.UNLOCKED)
396
397 //next dial index
398 if (m_LockActionPerformed == LockAction.DIAL_INDEX_CHANGED)
400
401 //dialed new number
402 if (m_LockActionPerformed == LockAction.DIAL_NUMBER_CHANED)
404 }
405
406 //Show/Hide anims
407 protected void ShowItem()
408 {
409 SetAnimationPhase("Combination_Lock_Item", 0);
410 SetAnimationPhase("Lock_Item_1", 0);
411 SetAnimationPhase("Lock_Item_2", 0);
412 }
413
414 protected void HideItem()
415 {
416 SetAnimationPhase("Combination_Lock_Item", 1);
417 SetAnimationPhase("Lock_Item_1", 1);
418 SetAnimationPhase("Lock_Item_2", 1);
419 }
420
421 protected void ShowAttached()
422 {
423 SetAnimationPhase("Combination_Lock_Attached", 0);
424 SetAnimationPhase("Lock_Attached_1", 0);
425 SetAnimationPhase("Lock_Attached_2", 0);
426 }
427
428 protected void HideAttached()
429 {
430 SetAnimationPhase("Combination_Lock_Attached", 1);
431 SetAnimationPhase("Lock_Attached_1", 1);
432 SetAnimationPhase("Lock_Attached_2", 1);
433 }
434 // ---
435
436 //================================================================
437 // SOUNDS
438 //================================================================
439 protected void SoundLockOpen()
440 {
441 PlaySoundSet(m_Sound, SOUND_LOCK_OPEN, 0, 0);
442 }
443
444 protected void SoundLockClose()
445 {
446 PlaySoundSet(m_Sound, SOUND_LOCK_CLOSE, 0, 0);
447 }
448
450 {
451 PlaySoundSet(m_Sound, SOUND_LOCK_CHANGE_NUMBER, 0, 0);
452 }
453
455 {
456 PlaySoundSet(m_Sound, SOUND_LOCK_CHANGE_DIAL, 0, 0);
457 }
458
469}
InventoryMode
NOTE: PREDICTIVE is not to be used at all in multiplayer.
Definition Inventory.c:22
ActionDialCombinationLockCB ActionContinuousBaseCB ActionDialCombinationLock()
ActionDialCombinationLockOnTargetCB ActionContinuousBaseCB ActionDialCombinationLockOnTarget()
void AddAction(typename actionName)
void SetActions()
override void OnVariablesSynchronized()
class BaseBuildingBase extends ItemBase bsbDebugPrint(string s)
override bool IsInitialized()
void HideAttached()
void SoundLockClose()
void SetInitialized()
void ShuffleLock()
void UpdateSound()
void ResetActionVar()
bool m_IsInitialized
const string SOUND_LOCK_CHANGE_NUMBER
int GetLockDigits()
bool IsLockedOnGate()
const string SOUND_LOCK_CHANGE_DIAL
void SetNextDial()
void SoundLockOpen()
void ShowAttached()
void LockServer(EntityAI parent, bool ignore_combination=false)
void SoundLockChangeNumber()
const string SOUND_LOCK_OPEN
enum LockAction m_LockDigits
void CheckLockedStateServer()
void CombinationLock()
void ShowItem()
void SetCombination(int combination)
void SetCombinationLocked(int combination)
LockAction
@ COUNT
@ DIAL_INDEX_CHANGED
@ UNLOCKED
@ LOCKED
@ DIAL_NUMBER_CHANED
@ NONE
int GetCombination()
void Synchronize()
void SoundLockChangeDial()
int m_Combination
const string SOUND_LOCK_CLOSE
void UnlockServer(EntityAI player, EntityAI parent)
int m_CombinationLocked
bool IsLockAttached()
int m_DialIndex
int GetDialIndex()
LockAction m_LockActionPerformed
void DialNextNumber()
void DestroyLock()
void HideItem()
void UpdateVisuals()
override void EEInit()
void AfterStoreLoad()
EffectSound m_Sound
override void SetTakeable(bool pState)
Definition ItemBase.c:8905
override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
Definition ItemBase.c:5867
bool m_IsLocked
bool IsLocked()
void OnStoreSave(ParamsWriteContext ctx)
bool OnStoreLoad(ParamsReadContext ctx, int version)
override void SetBaseLockValues()
Wrapper class for managing sound through SEffectManager.
Definition EffectSound.c:5
InventoryLocation.
static bool IsBaseBuildingLogEnable()
Definition Debug.c:779
Definition EnMath.c:7
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
proto native CGame GetGame()
proto native void SetPosition(vector position)
Set the world position of the Effect.
Definition Effect.c:420
static proto float Pow(float v, float power)
Return power of v ^ power.
static proto int RandomInt(int min, int max)
Returns a random int number between and min [inclusive] and max [exclusive].
const int CALL_CATEGORY_GAMEPLAY
Definition tools.c:10
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8