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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
enum LockAction
{
	NONE,
	DIAL_NUMBER_CHANED,
	DIAL_INDEX_CHANGED,
	LOCKED,
	UNLOCKED,
	
	COUNT
}

class CombinationLock extends ItemBase
{
	int m_LockDigits;					//how many digits will the combination contain
	int m_Combination;					//actual combination that is dialed on lock
	int m_CombinationLocked;			//combination that was dialed on lock before the shuffle
	int m_DialIndex;					//index of current combination dial
	protected bool m_IsLocked;
	
	protected LockAction m_LockActionPerformed 		= LockAction.NONE;
	
	protected bool m_IsInitialized;
	
	//Sounds
	//build
	const string SOUND_LOCK_OPEN 			= "combinationlock_open_SoundSet";
	const string SOUND_LOCK_CLOSE 			= "combinationlock_close_SoundSet";
	const string SOUND_LOCK_CHANGE_NUMBER	= "combinationlock_changenumber_SoundSet";
	const string SOUND_LOCK_CHANGE_DIAL		= "combinationlock_changedial_SoundSet";

	protected EffectSound m_Sound;

	void CombinationLock()
	{
		SetBaseLockValues();
		
		//synchronized variables
		int combination_length = Math.Pow( 10, m_LockDigits );
		RegisterNetSyncVariableBool( "m_IsLocked" );
		RegisterNetSyncVariableInt( "m_Combination", 0, combination_length - 1 );
		RegisterNetSyncVariableInt( "m_DialIndex", 0, m_LockDigits - 1 );
		RegisterNetSyncVariableInt( "m_LockActionPerformed", 0, LockAction.COUNT );
	}
	
	protected void SetBaseLockValues()
	{
		//set lock init values
		m_LockDigits 			= 3;
		m_Combination		 	= 111;
		m_CombinationLocked 	= 999;		
		m_IsLocked 				= false;
	}

	override void EEInit()
	{
		super.EEInit();
		
		GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( SetInitialized, 1000, false );
		//SetInitialized();
		
		//set visual on init
		UpdateVisuals();
	}
	
	void SetInitialized()
	{
		m_IsInitialized = true;
	}
	
	override bool IsInitialized()
	{
		return m_IsInitialized;
	}
	
	override void OnItemLocationChanged( EntityAI old_owner, EntityAI new_owner ) 
	{
		super.OnItemLocationChanged( old_owner, new_owner );
		
		//Check combination lock
		if ( GetGame().IsServer() )
		{
			if ( IsInitialized() && new_owner && new_owner.IsInherited( BaseBuildingBase ) )
			{
				LockServer( new_owner );
			}
		}		
	}	
	
	// --- EVENTS
	override void OnStoreSave( ParamsWriteContext ctx )
	{   
		super.OnStoreSave( ctx );
		
		//write data
		ctx.Write( m_Combination );
		ctx.Write( m_CombinationLocked );
	}
	
	override bool OnStoreLoad( ParamsReadContext ctx, int version )
	{
		if ( !super.OnStoreLoad( ctx, version ) )
			return false;
		
		//--- Combination Lock data ---
		//combination
		if ( !ctx.Read( m_Combination ) )
		{
			m_Combination = 0;
			return false;
		}
		
		//combination locked
		if ( !ctx.Read( m_CombinationLocked ) )
		{
			m_CombinationLocked = 0;
			return false;
		}
		
		//is lock attached
		if ( version < 105 )					//removed in 105
		{
			bool is_lock_attached;
			if ( !ctx.Read( is_lock_attached ) )
			{
				return false;
			}
		}

		return true;
	}
	
	override void AfterStoreLoad()
	{	
		super.AfterStoreLoad();		
		
		//Check combination lock
		if ( GetGame().IsServer() )
		{
			EntityAI parent = GetHierarchyParent();
			if ( parent && parent.IsInherited( BaseBuildingBase ) )
			{
				LockServer( parent, true );
			}
		}		
		
		//synchronize
		Synchronize();
	}
	
	// --- SYNCHRONIZATION
	void Synchronize()
	{
		if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.Synchronize " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
		if ( GetGame().IsServer() )
		{
			SetSynchDirty();
			GetGame().GetCallQueue( CALL_CATEGORY_SYSTEM ).CallLater( ResetActionVar, 1000);//synced var used to trigger client sound needs to be reset after triggering the sound
			UpdateVisuals();
		}
	}

	
	void ResetActionVar()
	{
		m_LockActionPerformed = LockAction.NONE;
	}
	
	override void OnVariablesSynchronized()
	{
		super.OnVariablesSynchronized();
		//update visuals (client)
		UpdateVisuals();
		
		//update sound (client)
		if (m_LockActionPerformed)
			UpdateSound();
		
		if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.OnVariablesSynchronized " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
	}
	
	void SetCombination( int combination )
	{
		m_Combination = combination;
	}
	
	void SetCombinationLocked( int combination )
	{
		m_CombinationLocked = combination;
	}
	
	int GetCombination()
	{
		return m_Combination;
	}
	
	int GetLockDigits()
	{
		return m_LockDigits;
	}
	
	// --- ACTIONS
	void DialNextNumber()
	{
		string combination_text = m_Combination.ToString();
		string dialed_text;
		
		//insert zeros to dials with 0 value
		int length_diff = m_LockDigits - combination_text.Length();
		for ( int i = 0; i < length_diff; ++i )
		{
			combination_text = "0" + combination_text;
		}
		
		//assemble the whole combination with increased part
		for ( int j = 0; j < combination_text.Length(); ++j )
		{
			if ( j == m_DialIndex )
			{
				int next_dialed_number = combination_text.Get( j ).ToInt() + 1;
				if ( next_dialed_number > 9 )
				{
					next_dialed_number = 0;
				}
				
				dialed_text += next_dialed_number.ToString();
			}
			else
			{
				dialed_text += combination_text.Get( j );
			}
		}
		
		//set new number		
		SetCombination( dialed_text.ToInt() );
		m_LockActionPerformed = LockAction.DIAL_INDEX_CHANGED;
		CheckLockedStateServer();
		
		//synchronize
		Synchronize();
	}
	
	int GetDialIndex()
	{
		return m_DialIndex;
	}
	
	void SetNextDial()
	{
		if ( m_LockDigits > 1 )
		{
			if ( m_DialIndex <= m_LockDigits - 2 )
			{
				m_DialIndex++;
			}
			else if ( m_DialIndex >= m_LockDigits >  - 1 )
			{
				m_DialIndex = 0;
			}
		}
		else
		{
			m_DialIndex = 0;
		}
		
		//performed action
		m_LockActionPerformed = LockAction.DIAL_NUMBER_CHANED;
		//synchronize
		Synchronize();
	}	
	
	//Lock lock
	void LockServer( EntityAI parent, bool ignore_combination = false )
	{
		if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.LockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
		if ( IsLockAttached() )
		{
			if ( !ignore_combination )
			{
				SetCombinationLocked( m_Combination );
			
				//set slot lock
				InventoryLocation inventory_location = new InventoryLocation;
				GetInventory().GetCurrentInventoryLocation( inventory_location );		
				parent.GetInventory().SetSlotLock( inventory_location.GetSlot(), true );
			
				m_LockActionPerformed = LockAction.LOCKED;
			}
			ShuffleLock();
			SetTakeable(false);
			CheckLockedStateServer();
			//synchronize
			Synchronize();
		}
		
		//reset performed action
		//m_LockActionPerformed = LockAction.NONE;
	}
	
	void UnlockServer( EntityAI player, EntityAI parent )
	{
		if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] CombinationLock.UnlockServer " + " m_Combination=" + m_Combination + " m_CombinationLocked=" + m_CombinationLocked);
		if ( IsLockAttached() )
		{
			Fence fence = Fence.Cast( parent );
			
			//set slot unlock
			InventoryLocation inventory_location = new InventoryLocation;
			GetInventory().GetCurrentInventoryLocation( inventory_location );			
			fence.GetInventory().SetSlotLock( inventory_location.GetSlot(), false );			
	
			//drop entity from attachment slot
			if (player)
				player.ServerDropEntity( this );
			else
				parent.GetInventory().DropEntity(InventoryMode.SERVER, parent, this);
			SetPosition( fence.GetKitSpawnPosition() );
			PlaceOnSurface();
			
			m_LockActionPerformed = LockAction.UNLOCKED;
			SetTakeable(true);
			CheckLockedStateServer();
			//synchronize
			Synchronize();
		}
		
		//reset performed action
		//m_LockActionPerformed = LockAction.NONE;		
	}
		
	//Shuffle lock
	void ShuffleLock()
	{
		string combination_text = m_Combination.ToString();
		string shuffled_text;
		
		//insert zeros to dials with 0 value
		int length_diff = m_LockDigits - combination_text.Length();
		for ( int i = 0; i < length_diff; ++i )
		{
			combination_text = "0" + combination_text;
		}
		
		//assemble the whole combination with increased part
		for ( int j = 0; j < combination_text.Length(); ++j )
		{
			int dial_number = combination_text.Get( j ).ToInt();
			dial_number = ( dial_number + Math.RandomInt( 1, 9 ) ) % 10;
			shuffled_text = shuffled_text + dial_number.ToString();
		}
		
		SetCombination( shuffled_text.ToInt() );
	}
	
	bool IsLocked()
	{
		return m_IsLocked;
	}
	
	void CheckLockedStateServer()
	{
		m_IsLocked = m_Combination != m_CombinationLocked;
	}
	
	bool IsLockedOnGate()
	{
		Fence fence = Fence.Cast( GetHierarchyParent() );
		if ( fence )
		{
			if ( IsLocked() )
			{
				return true;
			}
		}
		
		return false;
	}
	
	bool IsLockAttached()
	{
		Fence fence = Fence.Cast( GetHierarchyParent() );
		if ( fence )
		{
			return true;
		}
		
		return false;
	}
	
	//destroy lock
	void DestroyLock()
	{
		GetGame().ObjectDelete( this );
	}
		
	// --- VISUALS
	void UpdateVisuals()
	{
		//Client/Server
		if ( IsLockedOnGate() )
		{
			GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( HideItem, 		0, false );
			GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( ShowAttached, 	0, false );
		}
		else
		{
			GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( ShowItem, 		0, false );
			GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( HideAttached, 	0, false );
		}
	}
	
	void UpdateSound()
	{
		//was locked
		if ( m_LockActionPerformed == LockAction.LOCKED )
		{
			SoundLockClose();
		}
		
		//was unlocked
		if ( m_LockActionPerformed == LockAction.UNLOCKED )
		{
			SoundLockOpen();
		}
		
		//next dial index
		if ( m_LockActionPerformed == LockAction.DIAL_INDEX_CHANGED )
		{
			SoundLockChangeDial();
		}		
		
		//dialed new number
		if ( m_LockActionPerformed == LockAction.DIAL_NUMBER_CHANED )
		{
			SoundLockChangeNumber();
		}
	}
	
	//Show/Hide anims
	protected void ShowItem()
	{
		SetAnimationPhase( "Combination_Lock_Item", 0 );
		SetAnimationPhase( "Lock_Item_1", 			0 );
		SetAnimationPhase( "Lock_Item_2", 			0 );
	}
	
	protected void HideItem()
	{
		SetAnimationPhase( "Combination_Lock_Item", 1 );
		SetAnimationPhase( "Lock_Item_1", 			1 );
		SetAnimationPhase( "Lock_Item_2", 			1 );
	}
	
	protected void ShowAttached()
	{
		SetAnimationPhase( "Combination_Lock_Attached", 0 );
		SetAnimationPhase( "Lock_Attached_1", 			0 );
		SetAnimationPhase( "Lock_Attached_2", 			0 );
	}
	
	protected void HideAttached()
	{
		SetAnimationPhase( "Combination_Lock_Attached", 1 );
		SetAnimationPhase( "Lock_Attached_1", 			1 );
		SetAnimationPhase( "Lock_Attached_2", 			1 );
	}	
	// ---	
	
	//================================================================
	// SOUNDS
	//================================================================
	protected void SoundLockOpen()
	{
		PlaySoundSet( m_Sound, SOUND_LOCK_OPEN, 0, 0 );
	}

	protected void SoundLockClose()
	{
		PlaySoundSet( m_Sound, SOUND_LOCK_CLOSE, 0, 0 );
	}
	
	void SoundLockChangeNumber()
	{
		PlaySoundSet( m_Sound, SOUND_LOCK_CHANGE_NUMBER, 0, 0 );
	}

	void SoundLockChangeDial()
	{
		PlaySoundSet( m_Sound, SOUND_LOCK_CHANGE_DIAL, 0, 0 );
	}	
	
	override void SetActions()
	{
		super.SetActions();

		AddAction(ActionAttachToConstruction);		
		AddAction(ActionNextCombinationLockDial);
		AddAction(ActionDialCombinationLock);
		AddAction(ActionNextCombinationLockDialOnTarget);
		AddAction(ActionDialCombinationLockOnTarget);
	}
}