File size: 31,536 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
class WeaponManager
{
	const float MAX_DROP_MAGAZINE_DISTANCE_SQ = 4;
	
	protected PlayerBase 					m_player;
	
	protected int							m_LastAcknowledgmentID;
	
	protected int 							m_PendingWeaponActionAcknowledgmentID;
	protected Magazine 						m_PendingTargetMagazine;
	protected ref InventoryLocation			m_TargetInventoryLocation;
	protected int 							m_PendingWeaponAction; 
	protected ref InventoryLocation			m_PendingInventoryLocation;
	
	protected bool							m_canEnd;
	protected bool							m_justStart;
	protected bool							m_InProgress;
	protected bool							m_IsEventSended;
	protected bool							m_WantContinue;
	protected bool							m_InIronSight;
	protected bool							m_InOptic;
	protected bool 							m_readyToStart;
	protected Weapon_Base					m_WeaponInHand;
	protected MagazineStorage				m_MagazineInHand;
	protected ActionBase					m_ControlAction;
	protected int 							m_ForceEjectBulletTimestamp;
	
	protected const int 					FORCE_EJECT_BULLET_TIMEOUT = 2000;
#ifdef DIAG_DEVELOPER	
	protected int 							m_BurstOption;
#endif
	//Reload
	protected ref array<Magazine>			m_MagazinePilesInInventory;
	protected ref array<MagazineStorage>	m_MagazineStorageInInventory;
	protected ref array<Magazine>			m_SuitableMagazines;
	protected Magazine						m_PreparedMagazine;
	
	//Jamming
	protected float m_NewJamChance;
	protected bool m_WaitToSyncJamChance;
	
	
	protected int m_AnimationRefreshCooldown;
	
	void WeaponManager(PlayerBase player)
	{
		m_ForceEjectBulletTimestamp = -1;
		m_player = player;
		m_PendingWeaponActionAcknowledgmentID = -1;
		m_PendingTargetMagazine = NULL;
		m_PendingInventoryLocation = NULL;
		m_WeaponInHand = NULL;
		m_MagazineInHand = NULL;
		m_ControlAction = NULL;
		m_PendingWeaponAction = -1;
		m_LastAcknowledgmentID = 1;
		m_InProgress = false;
		m_WantContinue = true;
		m_IsEventSended = false;
		m_canEnd = false;
		m_readyToStart = false;
		m_AnimationRefreshCooldown = 0;
		
		m_NewJamChance = -1;
		m_WaitToSyncJamChance = false;
		
		m_MagazinePilesInInventory = new array<Magazine>;
		m_MagazineStorageInInventory = new array<MagazineStorage>;
		m_SuitableMagazines = new array<Magazine>;
		m_PreparedMagazine = null;
		
#ifdef DEVELOPER	
		m_BurstOption = 0;
#endif
	}
//----------------------------------------------------------------------------	
// Weapon Action conditions
//----------------------------------------------------------------------------	
	bool CanFire(Weapon_Base wpn)
	{
		if( m_player.GetHumanInventory().GetEntityInHands() != wpn )
			return false;
		
		if( m_player.IsLiftWeapon() || !m_player.IsRaised() || wpn.IsDamageDestroyed() || m_player.GetDayZPlayerInventory().IsProcessing() || !m_player.IsWeaponRaiseCompleted() || m_player.IsFighting() ) 
			return false;
		
		return true;
		
	}
	
	
	bool CanAttachMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true )
	{	
		if ( !wpn || !mag )
			return false;
		
		if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
			return false;
		
		if ( wpn.IsDamageDestroyed())
			return false;
		
		//if ( mag.GetHierarchyRootPlayer() && mag.GetHierarchyRootPlayer() != m_player )
			//return false;
		
		if( m_player.IsItemsToDelete())
			return false;
		
		if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
			return false;
		
		InventoryLocation invLoc;
		invLoc = new InventoryLocation();
		
		mag.GetInventory().GetCurrentInventoryLocation(invLoc);
		Weapon_Base wnp2;
		

		//magazine is already in weapon
		if ( Class.CastTo(wnp2,  invLoc.GetParent()) )
			return false;
			
		int muzzleIndex = wpn.GetCurrentMuzzle();
		
		if (wpn.CanAttachMagazine(muzzleIndex, mag))
			return true;

		return false;
		
	}
//---------------------------------------------------------------------------	
	
	bool CanSwapMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
	{
		if ( !wpn || !mag )
			return false;
		
		if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
			return false;
		
		if ( mag.IsDamageDestroyed() || wpn.IsDamageDestroyed())
			return false;
		
		//if ( mag.GetHierarchyRootPlayer() && mag.GetHierarchyRootPlayer() != m_player )
			//return false;

		if( m_player.IsItemsToDelete())
			return false;
		
		if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
			return false;
		

		InventoryLocation invLoc;
		invLoc = new InventoryLocation();
		
		mag.GetInventory().GetCurrentInventoryLocation(invLoc);
		Weapon_Base wnp2;
		
		//second magazine is already in weapon
		if( Class.CastTo(wnp2,  invLoc.GetParent()) )
			return false;		
		
		int muzzleIndex = wpn.GetCurrentMuzzle();	
					
		Magazine mag2;
		if( !Class.CastTo(mag2, wpn.GetMagazine(muzzleIndex)) ) 
			return false;
		
		if( GameInventory.CanSwapEntitiesEx( mag, mag2 ) )
			return true;
		
		InventoryLocation il = new InventoryLocation();
		
		if( GameInventory.CanForceSwapEntitiesEx( mag, null, mag2, il ) )
			return true;
		
		return false;
	}
//----------------------------------------------------------------------------			
	bool CanDetachMagazine(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
	{
		if ( !wpn || !mag )
			return false;
		
		if ( m_player.GetHumanInventory().GetEntityInHands() != wpn )
			return false;
		
		if ( mag.GetHierarchyParent() != wpn )
			return false;
		
		if( m_player.IsItemsToDelete())
			return false;
		
		if ( reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
			return false;
		
		return true;
	}
//---------------------------------------------------------------------------
	bool CanLoadBullet(Weapon_Base wpn, Magazine mag, bool reservationCheck = true)
	{
		if (!wpn || !mag)
			return false;
		
		if (m_player.GetHumanInventory().GetEntityInHands() != wpn)
			return false;
		
		if (mag.IsDamageDestroyed() || wpn.IsDamageDestroyed())
			return false;
		
		if (wpn.IsJammed())
			return false;
	
		if (m_player.IsItemsToDelete())
			return false;
		
		if (reservationCheck && (m_player.GetInventory().HasInventoryReservation(wpn, null) || m_player.GetInventory().HasInventoryReservation(mag, null)))
			return false;
		
		for (int i = 0; i < wpn.GetMuzzleCount(); i++)
		{
			if (wpn.CanChamberBullet(i, mag))
				return true;
		}
		
		return false;
	}
//---------------------------------------------------------------------------	
	bool CanUnjam(Weapon_Base wpn, bool reservationCheck = true)
	{
		if( !wpn )
			return false;
		 
		if( m_player.GetHumanInventory().GetEntityInHands() != wpn)
			return false;
		
		if( wpn.IsDamageDestroyed())
			return false;
		
		if( m_player.IsItemsToDelete())
			return false;
		
		if ( reservationCheck && m_player.GetInventory().HasInventoryReservation(wpn, null))
			return false;

		
		if( !wpn.IsJammed(/*wpn.GetCurrentMuzzle()*/) )
			return false;
		 
		return true;
	}
	
	bool CanEjectBullet(Weapon_Base wpn, bool reservationCheck = true)
	{
		if( !wpn )
			return false;
		 
		if( m_player.GetHumanInventory().GetEntityInHands() != wpn)
			return false;
		
		if( m_player.IsItemsToDelete())
			return false;
		
		if( reservationCheck && m_player.GetInventory().HasInventoryReservation(wpn, null))
			return false;
		
		if( !wpn.CanEjectBullet() )
			return false;
		
		if( wpn.IsJammed(/*wpn.GetCurrentMuzzle()*/) )
			return false;
		 
		return true;
	}
	
	void SetEjectBulletTryTimestamp()
	{
		m_ForceEjectBulletTimestamp = GetGame().GetTime();
	}
//----------------------------------------------------------------------------
	bool InventoryReservation( Magazine mag, InventoryLocation invLoc)
	{
		Weapon_Base weapon;
		InventoryLocation ilWeapon = new InventoryLocation();
		if (Weapon_Base.CastTo(weapon, m_player.GetItemInHands()) )
		{
			weapon.GetInventory().GetCurrentInventoryLocation(ilWeapon);
			if ( m_player.GetInventory().HasInventoryReservation(weapon, ilWeapon) )
			{
				return false;
			}
			else
			{
				m_player.GetInventory().AddInventoryReservationEx(weapon,ilWeapon,GameInventory.c_InventoryReservationTimeoutMS);
			}
		}
		
		if( invLoc )
		{
			if ( m_player.GetInventory().HasInventoryReservation(invLoc.GetItem(),invLoc) )
			{
				m_player.GetInventory().ClearInventoryReservationEx(weapon, ilWeapon);
				return false;
			}
			else
			{
				m_player.GetInventory().AddInventoryReservationEx(invLoc.GetItem(),invLoc,GameInventory.c_InventoryReservationTimeoutMS);
			}
		}
		
		if( mag )
		{
			m_TargetInventoryLocation = new InventoryLocation();
			m_TargetInventoryLocation.SetAttachment( m_WeaponInHand, mag, InventorySlots.MAGAZINE);
			
			if ( m_player.GetInventory().HasInventoryReservation(mag, m_TargetInventoryLocation) )
			//if ( !m_player.GetInventory().AddInventoryReservationEx( mag, m_TargetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS) )
			{
				m_player.GetInventory().ClearInventoryReservationEx(weapon, ilWeapon);
				if (invLoc)
				{
					m_player.GetInventory().ClearInventoryReservationEx(invLoc.GetItem(), invLoc);
				}
				return false;
			}
			else
			{
				m_player.GetInventory().AddInventoryReservationEx(mag, m_TargetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
			}
		}
		m_PendingTargetMagazine = mag;
		m_PendingInventoryLocation = invLoc;
		
		return true;
	}
	
//----------------------------------------------------------------------------	
// Weapon Actions	
//----------------------------------------------------------------------------
	bool AttachMagazine( Magazine mag , ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_ATTACH_MAGAZINE, mag, NULL, control_action);
	}
	
	bool DetachMagazine( InventoryLocation invLoc, ActionBase control_action = NULL)
	{
		return StartAction(AT_WPN_DETACH_MAGAZINE, NULL, invLoc, control_action);
	}
	
	bool SwapMagazine( Magazine mag, ActionBase control_action = NULL )
	{
		InventoryLocation il = new InventoryLocation();
		if (PrepareInventoryLocationForMagazineSwap(m_WeaponInHand, mag, il) )
		{
			return StartAction(AT_WPN_SWAP_MAGAZINE, mag, il, control_action);
		}
		return false;
	}
	
	bool SwapMagazineEx( Magazine mag, InventoryLocation invLoc, ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_SWAP_MAGAZINE, mag, invLoc, control_action);
	}
	
	bool LoadBullet( Magazine mag, ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_LOAD_BULLET, mag, NULL, control_action);
	}
	
	bool LoadMultiBullet( Magazine mag, ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_LOAD_MULTI_BULLETS_START, mag, NULL, control_action);
	}
	
	void LoadMultiBulletStop( )
	{
		if(m_InProgress) m_WantContinue = false;
	}
	
	bool Unjam( ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_UNJAM, NULL, NULL, control_action);
	}

	bool EjectBullet( ActionBase control_action = NULL )
	{
		return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
	}
	
	bool CanEjectBulletVerified()
	{
		int mi = m_WeaponInHand.GetCurrentMuzzle();
		if( !m_WeaponInHand.IsChamberFiredOut(mi) && !m_WeaponInHand.IsChamberEmpty(mi) )
		{
			int actual_time = GetGame().GetTime();
			if ( actual_time - m_ForceEjectBulletTimestamp > FORCE_EJECT_BULLET_TIMEOUT )
			{
				return false;
			}
		}
		return true;
	}
	
	bool EjectBulletVerified( ActionBase control_action = NULL )
	{
		if ( m_WeaponInHand )
		{	
			int mi = m_WeaponInHand.GetCurrentMuzzle();
			if ( !m_WeaponInHand.IsChamberFiredOut(mi) && !m_WeaponInHand.IsChamberEmpty(mi) )
			{
				m_ForceEjectBulletTimestamp = GetGame().GetTime();
				return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
			}
			else
			{
				return StartAction(AT_WPN_EJECT_BULLET, NULL, NULL, control_action);
			}
		}	
		return false;
	}
	
	bool SetNextMuzzleMode ()
	{
		return StartAction(AT_WPN_SET_NEXT_MUZZLE_MODE, NULL, NULL, NULL);
	}
	
	void Fire(Weapon_Base wpn)
	{
		int mi = wpn.GetCurrentMuzzle();
		if ( wpn.IsChamberFiredOut(mi) || wpn.IsJammed() || wpn.IsChamberEmpty(mi) )
		{
			wpn.ProcessWeaponEvent(new WeaponEventTrigger(m_player));
			return;
		}
		
		if (wpn.JamCheck(0))
		{
			wpn.ProcessWeaponEvent(new WeaponEventTriggerToJam(m_player));
		}
		else
		{
			wpn.ProcessWeaponEvent(new WeaponEventTrigger(m_player));
		}
	}
	
#ifdef DIAG_DEVELOPER
	int GetBurstOption()
	{
		return m_BurstOption;
	}
	
	void SetBurstOption(int value)
	{
		m_BurstOption = value;
	}
#endif	
//-------------------------------------------------------------------------------------	
// Synchronize - initialize from client side
//-------------------------------------------------------------------------------------

	//Client
	private void Synchronize( )
	{
		if ( GetGame().IsClient() )
		{
			m_PendingWeaponActionAcknowledgmentID = ++m_LastAcknowledgmentID;
			ScriptInputUserData ctx = new ScriptInputUserData;
			
			ctx.Write(INPUT_UDT_WEAPON_ACTION);
			ctx.Write(m_PendingWeaponAction);
			ctx.Write(m_PendingWeaponActionAcknowledgmentID);
			
			
			switch (m_PendingWeaponAction)
			{
				case AT_WPN_ATTACH_MAGAZINE:
				{	
					ctx.Write(m_PendingTargetMagazine);
					break;
				}
				case AT_WPN_SWAP_MAGAZINE:
				{
					ctx.Write(m_PendingTargetMagazine);
					m_PendingInventoryLocation.WriteToContext(ctx);
					break;
				}
				case AT_WPN_DETACH_MAGAZINE:
				{
					m_PendingInventoryLocation.WriteToContext(ctx);
					break;
				}
				case AT_WPN_LOAD_BULLET:
				{
					ctx.Write(m_PendingTargetMagazine);
					break;
				}
				case AT_WPN_LOAD_MULTI_BULLETS_START:
				{
					ctx.Write(m_PendingTargetMagazine);
					break;
				}
				case AT_WPN_UNJAM:
				{
					break;
				}
				case AT_WPN_EJECT_BULLET:
				{
					break;
				}
				default: 
				break;
			}
			ctx.Send();
			//if( !m_player.GetDayZPlayerInventory().HasLockedHands() )
			//	m_player.GetDayZPlayerInventory().LockHands();
		}
	}
	

	
	void OnSyncJuncture(int pJunctureID, ParamsReadContext pCtx)
	{
		if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_SET_JAMMING_CHANCE )
		{
			pCtx.Read(m_NewJamChance);
		}
		else
		{
			int AcknowledgmentID;
			pCtx.Read(AcknowledgmentID);
			if ( AcknowledgmentID == m_PendingWeaponActionAcknowledgmentID)
			{
				if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_ACTION_ACK_ACCEPT)
				{ 
					m_readyToStart = true;
				}
				else if (pJunctureID == DayZPlayerSyncJunctures.SJ_WEAPON_ACTION_ACK_REJECT)
				{
					if(m_PendingWeaponAction >= 0 )
					{
						if(!(GetGame().IsServer() && GetGame().IsMultiplayer()))
						{
							InventoryLocation ilWeapon = new InventoryLocation();
							ItemBase weapon = m_player.GetItemInHands();
							weapon.GetInventory().GetCurrentInventoryLocation(ilWeapon);
							m_player.GetInventory().ClearInventoryReservationEx(m_player.GetItemInHands(),ilWeapon);
							
							if( m_PendingTargetMagazine )
							{
								m_PendingTargetMagazine.GetInventory().ClearInventoryReservationEx(m_PendingTargetMagazine, m_TargetInventoryLocation );
							}
							
							if( m_PendingInventoryLocation )
							{
								m_player.GetInventory().ClearInventoryReservationEx( NULL, m_PendingInventoryLocation );
							}
						}
						m_PendingWeaponActionAcknowledgmentID = -1;
						m_PendingTargetMagazine = NULL;
						m_PendingWeaponAction = -1;
						m_PendingInventoryLocation = NULL;
						m_InProgress = false;
					}
				}
			}
		}
	}
	

		//Server
	bool OnInputUserDataProcess(int userDataType, ParamsReadContext ctx)
	{
		Weapon_Base wpn;
		int mi;
		InventoryLocation il;
		int slotID;
		bool accepted = false;
		if( userDataType == INPUT_UDT_WEAPON_ACTION)
		{
			if (!ctx.Read(m_PendingWeaponAction))
				return false;
			
			if (!ctx.Read(m_PendingWeaponActionAcknowledgmentID))
				return false;
			
			if(m_PendingTargetMagazine)
			{
				GetGame().ClearJuncture(m_player, m_PendingTargetMagazine);
				m_PendingTargetMagazine = NULL;
			}
			m_InProgress = true;
			m_IsEventSended = false;
			Magazine mag = NULL;
			
			Weapon_Base.CastTo( wpn, m_player.GetItemInHands() );
			if ( wpn )
				mi = wpn.GetCurrentMuzzle();
			
			switch (m_PendingWeaponAction)
			{
				case AT_WPN_ATTACH_MAGAZINE:
				{
					if ( !ctx.Read(mag) )
						break;
					
					if ( !mag || !wpn )
						break;

					slotID = wpn.GetSlotFromMuzzleIndex(mi);
					il = new InventoryLocation();
					il.SetAttachment(wpn,mag,slotID);
					if( GetGame().AddInventoryJunctureEx(m_player, mag, il, false, 10000) )
						accepted = true;

					m_PendingTargetMagazine = mag;
					break;
				}
				case AT_WPN_SWAP_MAGAZINE:
				{
					if ( !ctx.Read(mag) )
						break;
					
					il = new InventoryLocation();
					if (!il.ReadFromContext(ctx))
						break;
					
					if ( !mag || !wpn )
						break;

					if ( !wpn.GetMagazine(mi) )
						break;
					
					if ( GetGame().AddActionJuncture(m_player,mag,10000) )
						accepted = true;
					m_PendingInventoryLocation = il;
					m_PendingTargetMagazine = mag;
					
					break;
				}
				case AT_WPN_DETACH_MAGAZINE:
				{
					il = new InventoryLocation();
					if ( !il.ReadFromContext(ctx) )
						break;
					
					if ( !il.IsValid() )
						break;
					
					if ( !wpn )
						break;
					
					Magazine det_mag = wpn.GetMagazine(mi);
					mag = Magazine.Cast(il.GetItem());
					if ( !det_mag || ( mag != det_mag) )
						break;
					
					if( GetGame().AddInventoryJunctureEx(m_player, il.GetItem(), il, true, 10000))
						accepted = true;
					m_PendingInventoryLocation = il;
					m_PendingTargetMagazine = mag;
					break;
				}
				case AT_WPN_LOAD_BULLET:
				{
					ctx.Read(mag);
					
					if ( !mag )
						break;
					
					if( GetGame().AddActionJuncture(m_player,mag,10000) )
						accepted = true;
					m_PendingTargetMagazine = mag;
					break;
				}
				case AT_WPN_LOAD_MULTI_BULLETS_START:
				{
					ctx.Read(mag);
					
					if ( !mag )
						break;
					
					if( GetGame().AddActionJuncture(m_player,mag,10000) )
						accepted = true;
					m_PendingTargetMagazine = mag;
					break;
				}
				case AT_WPN_UNJAM:
				{
					accepted = true;
					//Unjam();
					break;
				}
				case AT_WPN_EJECT_BULLET:
				{
					accepted = true;
					break;
				}
				case AT_WPN_SET_NEXT_MUZZLE_MODE:
				{
					accepted = true;
					break;
				}
				default:
					Error("unknown actionID=" + m_PendingWeaponAction);
					break;
			}
			DayZPlayerSyncJunctures.SendWeaponActionAcknowledgment(m_player, m_PendingWeaponActionAcknowledgmentID, accepted);
		}
		
		return accepted;
	}
	
	bool StartAction(int action, Magazine mag, InventoryLocation il, ActionBase control_action = NULL)
	{
		//if it is controled by action inventory reservation and synchronization provide action itself
		if(control_action)
		{
			m_ControlAction = ActionBase.Cast(control_action);
			m_PendingWeaponAction = action;
			m_InProgress = true;
			m_IsEventSended = false;
			m_PendingTargetMagazine = mag;
			m_PendingInventoryLocation = il;
			StartPendingAction();
			
			return true;
		}
		
		
		if (GetGame().IsMultiplayer() && GetGame().IsServer())
			return false;
		
		if ( !ScriptInputUserData.CanStoreInputUserData() )
			return false;
		if ( !InventoryReservation(mag, il) )
			return false;

		m_PendingWeaponAction = action;
		m_InProgress = true;
		m_IsEventSended = false;
		
		if ( !GetGame().IsMultiplayer() )
			m_readyToStart = true;
		else
			Synchronize();
		
		return true;
	}
	
	void StartPendingAction()
	{		
		m_WeaponInHand = Weapon_Base.Cast(m_player.GetItemInHands());
		if(!m_WeaponInHand)
		{
			OnWeaponActionEnd();
			return;
		}
		switch (m_PendingWeaponAction)
		{
			case AT_WPN_ATTACH_MAGAZINE:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventAttachMagazine(m_player, m_PendingTargetMagazine) );
				break;
			}
			case AT_WPN_SWAP_MAGAZINE:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventSwapMagazine(m_player, m_PendingTargetMagazine, m_PendingInventoryLocation) );
				break;
			}
			case AT_WPN_DETACH_MAGAZINE:
			{
				Magazine mag = Magazine.Cast(m_PendingInventoryLocation.GetItem());
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventDetachMagazine(m_player, mag, m_PendingInventoryLocation) );
				break;
			}
			case AT_WPN_LOAD_BULLET:
			{
				m_WantContinue = false;
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventLoad1Bullet(m_player, m_PendingTargetMagazine) );
				break;
			}
			case AT_WPN_LOAD_MULTI_BULLETS_START:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventLoad1Bullet(m_player, m_PendingTargetMagazine) );
				break;
			}
			case AT_WPN_LOAD_MULTI_BULLETS_END:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventContinuousLoadBulletEnd(m_player) );
				break;
			}
			case AT_WPN_UNJAM:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventUnjam(m_player, NULL) );
				break;
			}
			case AT_WPN_EJECT_BULLET:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventMechanism(m_player, NULL) );
				break;
			}
			case AT_WPN_SET_NEXT_MUZZLE_MODE:
			{
				m_player.GetDayZPlayerInventory().PostWeaponEvent( new WeaponEventSetNextMuzzleMode(m_player, NULL) );
				break;
			}
			default:
				m_InProgress = false;
				Error("unknown actionID=" + m_PendingWeaponAction);
		}	
		m_IsEventSended = true;
		m_canEnd = false;
	}
	
	bool IsRunning()
	{
		return m_InProgress;
	}
	
	void SetRunning( bool value)
	{
		m_InProgress = value;
	}
	
	void Refresh()
	{
		OnWeaponActionEnd();
	}
	
	void Update( float deltaT )
	{
		if (m_WeaponInHand != m_player.GetItemInHands())
		{
			if( m_WeaponInHand )
			{
				m_SuitableMagazines.Clear();
				OnWeaponActionEnd();
			}
			m_WeaponInHand = Weapon_Base.Cast(m_player.GetItemInHands());
			if ( m_WeaponInHand )
			{
				m_MagazineInHand = null;
				//SET new magazine
				SetSutableMagazines();
				m_WeaponInHand.SetSyncJammingChance(0);
			}
			m_AnimationRefreshCooldown = 0;
		}
		
		if (m_WeaponInHand)
		{
			if(m_AnimationRefreshCooldown)
			{
				m_AnimationRefreshCooldown--;
			
				if( m_AnimationRefreshCooldown == 0)
				{
					RefreshAnimationState();
				}
			}
		
			if (!GetGame().IsMultiplayer())
			{
				m_WeaponInHand.SetSyncJammingChance(m_WeaponInHand.GetChanceToJam());
			}
			else
			{
				if ( m_NewJamChance >= 0)
				{
					m_WeaponInHand.SetSyncJammingChance(m_NewJamChance);
					m_NewJamChance = -1;
					m_WaitToSyncJamChance = false;
				}
				if (GetGame().IsServer() && !m_WaitToSyncJamChance )
				{
					float actual_chance_to_jam;
					actual_chance_to_jam = m_WeaponInHand.GetChanceToJam();
					if ( Math.AbsFloat(m_WeaponInHand.GetSyncChanceToJam() - m_WeaponInHand.GetChanceToJam()) > 0.001 )
					{
						DayZPlayerSyncJunctures.SendWeaponJamChance(m_player, m_WeaponInHand.GetChanceToJam());
						m_WaitToSyncJamChance = true;
					}
				}
			}
			
			if(m_readyToStart)
			{
				StartPendingAction();
				m_readyToStart = false;
				return;
			}
		
			if( !m_InProgress || !m_IsEventSended )
				return;
		
			if(m_canEnd)
			{
				if(m_WeaponInHand.IsIdle())
				{
					OnWeaponActionEnd();
				}
				else if(m_justStart)
				{
					m_InIronSight = m_player.IsInIronsights();
					m_InOptic = m_player.IsInOptics();
		
					if(m_InIronSight || m_InOptic)
					{
						//'RequestResetADSSync' can be called here, if ADS reset is desired
						m_player.ExitSights();
					}
				
					m_justStart = false;
				}
			
			}
			else
			{
				m_canEnd = true;
				m_justStart = true;
			}
		}
		else
		{
			if ( m_MagazineInHand != m_player.GetItemInHands() )
			{
				m_MagazineInHand = MagazineStorage.Cast(m_player.GetItemInHands());
				if ( m_MagazineInHand )
				{
					SetSutableMagazines();
				}
			}
		
		
		}
	}

	void OnWeaponActionEnd()
	{
		if ( !m_InProgress )
			return;
		
		if (!m_ControlAction)
		{
			if(GetGame().IsServer() && GetGame().IsMultiplayer())
			{
				if(m_PendingTargetMagazine)
				{
					GetGame().ClearJuncture(m_player,m_PendingTargetMagazine);
				}
			}
			else
			{
				InventoryLocation il = new InventoryLocation();
				il.SetHands(m_player,m_player.GetItemInHands());
				m_player.GetInventory().ClearInventoryReservationEx(m_player.GetItemInHands(),il);
						
				if( m_PendingTargetMagazine )
				{
					m_player.GetInventory().ClearInventoryReservationEx(m_PendingTargetMagazine, m_TargetInventoryLocation );
				}
						
				if( m_PendingInventoryLocation )
				{
					m_player.GetInventory().ClearInventoryReservationEx( m_PendingInventoryLocation.GetItem(), m_PendingInventoryLocation );
				}
			}
		}
		
		m_ControlAction = NULL;
		m_PendingWeaponAction = -1;
		m_PendingTargetMagazine = NULL;
		m_PendingInventoryLocation = NULL;
		m_TargetInventoryLocation = NULL;
		m_PendingWeaponActionAcknowledgmentID = -1;
		//m_WeaponInHand = NULL;
		m_InProgress = false;
		m_readyToStart = false;
		m_WantContinue = true;
		
	}
	
	void DelayedRefreshAnimationState(int delay)
	{	
		if(m_WeaponInHand)
		{
			if(delay == 0)
			{
				RefreshAnimationState();
			}
			m_AnimationRefreshCooldown = delay;
		}
	}
	
	void RefreshAnimationState()
	{
		if(m_WeaponInHand)
		{
			WeaponStableState state = WeaponStableState.Cast( m_WeaponInHand.GetCurrentState() );
		
			if (state)
			{
				HumanCommandWeapons hcw = m_player.GetCommandModifier_Weapons();
				if (hcw)
				{
					hcw.SetInitState(state.m_animState);
				}
			}
		}
	}
	
	bool WantContinue()
	{
		return m_WantContinue;
	}
	
	Magazine GetPreparedMagazine()
	{
		for (int i = 0; i < m_SuitableMagazines.Count(); i++)
		{
			if(!m_SuitableMagazines.Get(i) || m_SuitableMagazines.Get(i).IsRuined() )
			{
				m_SuitableMagazines.Remove(i);
				i--;
				continue;
			}
			if (m_SuitableMagazines.Get(i).GetAmmoCount() > 0)
				return m_SuitableMagazines.Get(i);
		}
		
		return null;
	}
	
	Magazine GetNextPreparedMagazine( out int startIdx )
	{
		for (int i = startIdx; i < m_SuitableMagazines.Count(); i++)
		{
			if (m_SuitableMagazines.Get(i).GetAmmoCount() > 0)
			{
				startIdx = i;
				return m_SuitableMagazines.Get(i);
			}				
		}
		return null;
	}
	
	void OnMagazineInventoryEnter(Magazine mag)
	{
		if (mag)
		{
			Weapon_Base weapon = Weapon_Base.Cast(mag.GetHierarchyParent());
			
			if (weapon)
				return;
		}
		
		int i;
		MagazineStorage sMag = MagazineStorage.Cast(mag);
		if(sMag)
		{
			for(i = 0; i < m_MagazineStorageInInventory.Count(); i++ ) 
			{
				MagazineStorage s_mag_i = m_MagazineStorageInInventory[i];
				if(!s_mag_i)
				{
					m_MagazineStorageInInventory.RemoveOrdered(i);
					i--;
					continue;
				}
				
				if(CompareMagazinesSuitability(s_mag_i,sMag)<0)
					break;
			}
			m_MagazineStorageInInventory.InsertAt(sMag,i);
			
			SetSutableMagazines(); //TODO optimalize
			return;
		}
		
		if(mag)
		{

			for(i = 0; i < m_MagazinePilesInInventory.Count(); i++ ) 
			{
				Magazine mag_i = m_MagazinePilesInInventory[i];
				if(!mag_i)
				{
					m_MagazinePilesInInventory.RemoveOrdered(i);
					i--;
					continue;
				}
				
				
				if(CompareMagazinesSuitability(mag_i,mag)<0)
				{
					break;
				}
			}
			m_MagazinePilesInInventory.InsertAt(mag,i);
			SetSutableMagazines(); //TODO optimalize
		}
		
	}
	
	void OnMagazineInventoryExit(Magazine mag)
	{
		m_SuitableMagazines.RemoveItem(mag);

		MagazineStorage sMag = MagazineStorage.Cast(mag);
		if(sMag)
		{
			m_MagazineStorageInInventory.RemoveItem(sMag);
			return;
		}
		
		if(mag)
		{
			m_MagazinePilesInInventory.RemoveItem(mag);
		}
	}
	
	void OnMagazineAttach(Magazine mag)
	{
		OnMagazineInventoryExit(mag);
	}
	
	void OnMagazineDetach(Magazine mag)
	{
		OnMagazineInventoryEnter(mag);
	}
	
	int CompareMagazinesSuitability( Magazine mag1, Magazine mag2 )
	{
		return mag1.GetAmmoCount() - mag2.GetAmmoCount();
	}
	
	void SortMagazineAfterLoad()
	{
		array<MagazineStorage>	magazines = new array<MagazineStorage>;
		array<Magazine>	magazines_piles = new array<Magazine>;
		int low_mag1, high_mag1;
		int low_mag2, high_mag2;
		int i, j;
		
		for(i = 0; i < m_MagazineStorageInInventory.Count(); i++ ) 
		{
			MagazineStorage mag = m_MagazineStorageInInventory.Get(i);
			mag.GetNetworkID(low_mag1,high_mag1);
			for( j = 0; j < magazines.Count(); j++)
			{
				magazines.Get(j).GetNetworkID(low_mag2,high_mag2);
				if(low_mag1 > low_mag2)
				{
					break;
				}
				else if (low_mag1 == low_mag2)
				{
					if( high_mag1 > high_mag2 )
					{
						break;
					}
				}
			}
			magazines.InsertAt(mag,j);
		}
		
		m_MagazineStorageInInventory.Clear();
		m_MagazineStorageInInventory.Copy(magazines);
		
		for(i = 0; i < m_MagazinePilesInInventory.Count(); i++ ) 
		{
			Magazine pile = m_MagazinePilesInInventory.Get(i);
			pile.GetNetworkID(low_mag1,high_mag1);
			for( j = 0; j < magazines_piles.Count(); j++)
			{
				magazines_piles.Get(j).GetNetworkID(low_mag2,high_mag2);
				if(low_mag1 > low_mag2)
				{
					break;
				}
				else if (low_mag1 == low_mag2)
				{
					if( high_mag1 > high_mag2 )
					{
						break;
					}
				}
			}
			magazines_piles.InsertAt(pile,j);
		}
		
		m_MagazinePilesInInventory.Clear();
		m_MagazinePilesInInventory.Copy(magazines_piles);
	
		SetSutableMagazines();
	}
	
	void SetSutableMagazines()
	{
		m_SuitableMagazines.Clear();
		int i;
		
		if (m_WeaponInHand)
		{
			int mi = m_WeaponInHand.GetCurrentMuzzle();
			
			for (i = 0; i < m_MagazineStorageInInventory.Count(); i++ )
			{
				MagazineStorage s_mag = m_MagazineStorageInInventory[i];
				
				if (!s_mag)	
				{
					m_MagazineStorageInInventory.RemoveOrdered(i);
					i--;
					continue;
				}
				
				if ( m_WeaponInHand.TestAttachMagazine(mi, s_mag, false, true))
					m_SuitableMagazines.Insert(s_mag);
			}
			
			for (i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
			{
				Magazine mag = m_MagazinePilesInInventory[i];
				if (!mag)	
				{
					m_MagazinePilesInInventory.RemoveOrdered(i);
					i--;
					continue;
				}
				
				if (m_WeaponInHand.CanChamberFromMag(mi, mag))
					m_SuitableMagazines.Insert(mag);	
			}
//TODO m_MagazineStorageInInventory and m_MagazinePilesInInventory always sort
		}
		else if (m_MagazineInHand)
		{
			for (i = 0; i < m_MagazinePilesInInventory.Count(); i++ )
			{
				Magazine m_mag = m_MagazinePilesInInventory[i];
				if (!m_mag)	
				{
					m_MagazinePilesInInventory.RemoveOrdered(i);
					i--;
					continue;
				}
				if (m_MagazineInHand.IsCompatiableAmmo( m_mag ))
					m_SuitableMagazines.Insert(m_mag);
			}
		}
		else
		{
			m_PreparedMagazine = null;
		}
	
	}
	
	void OnLiftWeapon()
	{
		if( m_WeaponInHand )
			m_WeaponInHand.ResetBurstCount();
	}
	
	string GetCurrentModeName()
	{
		if( m_WeaponInHand )
		{
			int mi = m_WeaponInHand.GetCurrentMuzzle();
			return m_WeaponInHand.GetCurrentModeName(mi);
		}
		return "";
	}
	
	bool PrepareInventoryLocationForMagazineSwap( notnull Weapon_Base wpn, notnull Magazine new_mag, out InventoryLocation new_il )
	{
		int muzzleIndex = wpn.GetCurrentMuzzle();
		Magazine old_mag = Magazine.Cast(wpn.GetMagazine(muzzleIndex));
		InventoryLocation temp = new InventoryLocation();
		
		if (old_mag)
		{
			bool result = GameInventory.CanSwapEntitiesEx(new_mag, old_mag);
			if ( result )
			{
				new_mag.GetInventory().GetCurrentInventoryLocation(new_il);
				new_il.SetItem(old_mag);
			}
			else
			{
				result = GameInventory.CanForceSwapEntitiesEx(new_mag, null, old_mag, new_il);
				float dir[4];
				if ( !result )
				{
					new_il.SetGroundEx( old_mag,  m_player.GetPosition(), dir);
				}
			}
		}
		return true;
	}
}