File size: 52,454 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
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
class TurnItemIntoItemLambda extends ReplaceItemWithNewLambda
{
	bool m_TransferAgents;
	bool m_TransferVariables;
	bool m_TransferHealth;
	bool m_ExcludeQuantity;
	float m_quantity_override;
	
	void TurnItemIntoItemLambda (EntityAI old_item, string new_item_type, PlayerBase player)
	{
		SetTransferParams();
	}
	
	void SetTransferParams (bool transfer_agents = true, bool transfer_variables = true, bool transfer_health = true, bool exclude_quantity = false, float quantity_override = -1)
	{
		m_TransferAgents = transfer_agents;
		m_TransferVariables = transfer_variables; 
		m_TransferHealth = transfer_health;
		m_ExcludeQuantity = exclude_quantity;
		m_quantity_override = quantity_override;
	}

	override void CopyOldPropertiesToNew (notnull EntityAI old_item, EntityAI new_item)
	{
		super.CopyOldPropertiesToNew(old_item, new_item);

		if (new_item) 
		{
			MiscGameplayFunctions.TransferItemProperties(old_item, new_item, m_TransferAgents, m_TransferVariables, m_TransferHealth, m_ExcludeQuantity);
			MiscGameplayFunctions.TransferInventory(old_item, new_item, m_Player);
			
			//quantity override
			if (ItemBase.Cast(new_item) && m_quantity_override != -1)
			{
				m_quantity_override = Math.Max(m_quantity_override,0);
				ItemBase.Cast(new_item).SetQuantity(m_quantity_override);
			}
		}
		else
		{
			Debug.LogError("TurnItemIntoItemLambda: failed to create new item","static");
		}
	}
	
	//! if attaching from att.slot to att.slot, skips the change to "ground" version. Allows for correct property transfers.
	override void VerifyItemTypeBySlotType ()
	{
		if (m_NewLocation.GetType() == InventoryLocationType.ATTACHMENT && m_OldItem.ConfigIsExisting("ChangeIntoOnAttach"))
		{
			string str;
			int idx = -1;
			TStringArray inventory_slots = new TStringArray;
			TIntArray inventory_slots_idx = new TIntArray;
			TStringArray attach_types = new TStringArray;
			
			m_OldItem.ConfigGetTextArray("ChangeInventorySlot",inventory_slots);
			if (inventory_slots.Count() < 1) //is string
			{
				inventory_slots_idx.Insert(InventorySlots.GetSlotIdFromString(m_OldItem.ConfigGetString("ChangeInventorySlot")));
				attach_types.Insert(m_OldItem.ConfigGetString("ChangeIntoOnAttach"));
			}
			else //is array
			{
				inventory_slots_idx.Clear();
				for (int i = 0; i < inventory_slots.Count(); i++)
				{
					inventory_slots_idx.Insert(InventorySlots.GetSlotIdFromString(inventory_slots.Get(i)));
				}
				m_OldItem.ConfigGetTextArray("ChangeIntoOnAttach",attach_types);
			}
		
			idx = m_NewLocation.GetSlot();
			str = attach_types.Get(inventory_slots_idx.Find(idx));
			if (str != "")
			{
				m_NewItemType = str;
			}
		}
	}
	
	override void OnSuccess (EntityAI new_item)
	{
		super.OnSuccess(new_item);
		if( m_Player )
		{
			m_Player.GetItemAccessor().OnItemInHandsChanged();
		}
	}
};

class TurnItemIntoItemLambdaAnimSysNotifyLambda extends TurnItemIntoItemLambda
{
	override void OnSuccess (EntityAI new_item)
	{
		super.OnSuccess(new_item);
		if( m_Player )
		{
			m_Player.GetItemAccessor().OnItemInHandsChanged();
		}
	}
}

class TurnItemIntoItemLambdaRestrainLambda extends TurnItemIntoItemLambdaAnimSysNotifyLambda
{
	override void OnSuccess (EntityAI new_item)
	{
		super.OnSuccess(new_item);
		m_Player.SetRestrained(true);
	}
}

/**@class		DropEquipAndDestroyRootLambda
 * @brief		this one is a bit special: it drops all items and destroys the ex-root of the hierarchy
 **/
class DropEquipAndDestroyRootLambda : ReplaceItemWithNewLambdaBase
{
	PlayerBase m_Player;

	void DropEquipAndDestroyRootLambda (EntityAI old_item, string new_item_type, PlayerBase player)
	{
		m_Player = player;
	}
	
	override void CopyOldPropertiesToNew (notnull EntityAI old_item, EntityAI new_item)
	{
		super.CopyOldPropertiesToNew(old_item, new_item);
		
		InventoryLocation understash_src = m_NewLocation; // m_NewLocation is a backup of original old_item's src before the operation started
		
		array<EntityAI> children = new array<EntityAI>;
		old_item.GetInventory().EnumerateInventory(InventoryTraversalType.LEVELORDER, children);
		int count = children.Count();
		for (int i = 0; i < count; i++)
		{
			EntityAI child = children.Get(i);
			if (child)
			{
				InventoryLocation child_src = new InventoryLocation;
				child.GetInventory().GetCurrentInventoryLocation(child_src);
				
				InventoryLocation child_dst = new InventoryLocation;
				//@TODO: modify _dst with place on gnd?
				
				vector m4[4];
				Math3D.MatrixIdentity4(m4);
				
				//! We don't care if a valid transform couldn't be found, we just want to preferably use it instead of placing on the old item
				GameInventory.PrepareDropEntityPos(old_item, child, m4, false, -1);
				
				child_dst.SetGround(child,m4);
				
				m_Player.LocalTakeToDst(child_src, child_dst);
												
				GetGame().RemoteObjectTreeCreate(child); // this forces server to send CreateVehicle Message to client. This is needed for preserving the appearance of network operations on client (so that DeleteObject(old) arrives before CreateVehicle(new)). @NOTE: this does not delete the object on server, only it's network representation.
			}
		}
	}
}

/**@class		MoveEquipToExistingItemAndDestroyOldRootLambda
 * @brief		this one is a also bit special: it moves all items to already existing item and destroys the ex-root of the hierarchy
 **/
class MoveEquipToExistingItemAndDestroyOldRootLambda : ReplaceItemWithNewLambdaBase
{
	PlayerBase m_Player;
	EntityAI m_NewItem;

	void MoveEquipToExistingItemAndDestroyOldRootLambda (EntityAI old_item, string new_item_type, PlayerBase player, EntityAI new_item)
	{
		m_Player = player;
		m_NewItem = new_item;
		if (new_item_type != string.Empty)
			Error("MoveEquipAndDestroyRootLambda expects new_item_type to be empty");
	}
	
	override protected void RemoveNetworkObjectInfo ()
	{
		super.RemoveNetworkObjectInfo();
		GetGame().RemoteObjectTreeDelete(m_NewItem);
	}
	override protected void UndoRemoveNetworkObjectInfo ()
	{
		super.UndoRemoveNetworkObjectInfo();
		GetGame().RemoteObjectTreeCreate(m_NewItem);
	}
	
	override void CopyOldPropertiesToNew (notnull EntityAI old_item, EntityAI new_item)
	{
		// @NOTE: new_item is NULL, this lambda does not create new entity
		super.CopyOldPropertiesToNew(old_item, new_item);
		
		MiscGameplayFunctions.TransferInventory(old_item, m_NewItem, m_Player);		
	}
	
	override protected void CreateNetworkObjectInfo (EntityAI new_item)
	{
		super.CreateNetworkObjectInfo(new_item);
		GetGame().RemoteObjectTreeCreate(m_NewItem);
	}
};

enum TransferInventoryResult
{
	Ok, DroppedSome
};

enum ThrowEntityFlags
{
	NONE = 0,
	SPLIT = 1, //< Splits the item when it has quantity, recommended to use when called on an attachment
}

class MiscGameplayFunctions
{
	//! truncate float to specified precision
	static float Truncate(float value, int decimals = 2)
	{
		int multiplier = Math.Pow(10,decimals);
		return Math.Clamp(Math.Floor(value * multiplier),float.LOWEST, float.MAX) / multiplier;
	}
	//! truncate float to specified precision, output as string
	static string TruncateToS(float value, int decimals = 2)
	{
		return Truncate(value, decimals).ToString();
	}
	//! truncate float to specified precision
	static vector TruncateVec(vector value, int decimals = 2)
	{
		int multiplier = Math.Pow(10,decimals);
		float v1 = Math.Clamp(Math.Floor(value[0] * multiplier),float.LOWEST, float.MAX) / multiplier;
		float v2 = Math.Clamp(Math.Floor(value[1] * multiplier),float.LOWEST, float.MAX) / multiplier;
		float v3 = Math.Clamp(Math.Floor(value[2] * multiplier),float.LOWEST, float.MAX) / multiplier;
		return Vector(v1,v2,v3);
	}
	
	static string TruncateVecToS(vector value,int decimals = 2, string delimiter = " ")
	{
		return MiscGameplayFunctions.TruncateToS(value[0],decimals) + delimiter + MiscGameplayFunctions.TruncateToS(value[1],decimals) +delimiter + MiscGameplayFunctions.TruncateToS(value[2],decimals));
	}
	
	static string GetColorString(float r, float g, float b, float a)
	{
		return string.Format("#(argb,8,8,3)color(%1,CO)", string.Format("%1,%2,%3,%4", r, g, b, a));
	}
	
	//! Produces ACII "progress bar" based on an 0..1 'value' input
	static string ValueToBar(float value, string bar = "[----------]", string mark = "x")
	{
		int length = bar.Length() - 2;
		float index = Math.Lerp(0,length, value);
		index = Math.Round(index);
		index = Math.Clamp(index,0,length);
		
		return InsertAtPos(bar,mark,index);
	}
	
	//! Insert 'insert' behind index 'pos' of the 'base' string
	static string InsertAtPos(string base, string insert, int pos)
	{
		int length_first = pos+1;
		int length_base = base.Length();
		int length_second = length_base - length_first;
		string first = base.Substring(0,length_first);
		string second = base.Substring(pos+1,length_second);
		return first + insert + second;
	}
	
	//! will transform item' variables, agents and other local scripted properties as well as any relevant non-scripted properties like health
	static void TransferItemProperties(EntityAI source, notnull EntityAI target, bool transfer_agents = true, bool transfer_variables = true, bool transfer_health = true, bool exclude_quantity = false)
	{
		ItemBase target_ib = ItemBase.Cast(target);
		ItemBase source_ib = ItemBase.Cast(source);
		
		if( transfer_agents ) target_ib.TransferAgents( source.GetAgents() );
		
		if( transfer_variables )
		{
			MiscGameplayFunctions.TransferItemVariables(source_ib, target_ib, exclude_quantity);
		}

		if (GetGame().IsServer() || !GetGame().IsMultiplayer())
		{
			if( transfer_health )
				target_ib.SetHealth01("", "", source.GetHealth01("",""));
		}
	}

	static void TransferItemVariables(ItemBase source, ItemBase target, bool exclude_quantity = false)
	{
		target.TransferVariablesFloat( source.GetVariablesFloat() );
		//target.TransferVariablesString( source.GetVariablesString() );
		if ( exclude_quantity )
		{
			target.RemoveItemVariable(VARIABLE_QUANTITY);
		}
		else if ( source.IsMagazine() && target.IsMagazine() )
		{
			Magazine source_mag = Magazine.Cast(source);
			Magazine target_mag = Magazine.Cast(target);
			
			target_mag.ServerSetAmmoCount( source_mag.GetAmmoCount() );
		}
		
		if ( source.IsWeapon() && target.IsWeapon() )
		{
			Weapon_Base source_wpn = Weapon_Base.Cast(source);
			Weapon_Base target_wpn = Weapon_Base.Cast(target);
			
			target_wpn.CopyWeaponStateFrom(source_wpn);
		}
		else if ( source.HasEnergyManager() && target.HasEnergyManager() )
		{
			ComponentEnergyManager ems = source.GetCompEM();
			ComponentEnergyManager emt = target.GetCompEM();
			
			emt.SetEnergy(ems.GetEnergy());

			if (ems.IsSwitchedOn())
				emt.SwitchOn();
		}
		else if ( source.CanDecay() && target.CanDecay() )
		{
			Edible_Base source_edb = Edible_Base.Cast(source);
			Edible_Base target_edb = Edible_Base.Cast(target);
			
			target_edb.TransferFoodStage(source_edb);
		}
	}

	static TransferInventoryResult TransferInventory( EntityAI sourceItem, EntityAI targetItem, PlayerBase player)
	{
		TransferInventoryResult result = TransferInventoryResult.Ok;

		array<EntityAI> children = new array<EntityAI>;
		sourceItem.GetInventory().EnumerateInventory(InventoryTraversalType.LEVELORDER, children);
		int count = children.Count();
		for (int i = 0; i < count; i++)
		{
			EntityAI child = children.Get(i);
			if (child)
			{
				InventoryLocation child_src = new InventoryLocation;
				child.GetInventory().GetCurrentInventoryLocation( child_src );
				
				InventoryLocation child_dst = new InventoryLocation;
				child_dst.Copy( child_src );
				child_dst.SetParent( targetItem );

				bool drop = false;

				if (GameInventory.LocationCanAddEntity(child_dst))
				{
					// try to move it to the same exact place in dst
					targetItem.GetInventory().TakeToDst(InventoryMode.LOCAL, child_src, child_dst);
				}
				else
				{
					drop = true; // or drop otherwise
				}

				if (drop)
				{
					player.LocalDropEntity(child);
					GetGame().RemoteObjectTreeCreate(child);
					result = TransferInventoryResult.DroppedSome;
				}
			}
		}
		return result;
	}
	
	static void UnlimitedAmmoDebugCheck(Weapon_Base weapon)
	{
		if ( ItemBase.GetDebugActionsMask() & DebugActionType.UNLIMITED_AMMO )
		{
			Magazine magazine;
			if ( GetGame().IsServer() )
			{
				magazine = weapon.GetMagazine(weapon.GetCurrentMuzzle());
			
				if (magazine)
				{
					if (magazine.GetAmmoCount() <= 5)
					{
						magazine.ServerSetAmmoMax();
					}
				}
			}
			else
			{
				magazine = weapon.GetMagazine(weapon.GetCurrentMuzzle());
			
				if (magazine)
				{
					if (magazine.GetAmmoCount() <= 5)
					{
						magazine.LocalSetAmmoMax();
					}
				}
			
			}
		}
	}
	

		
	static void TurnItemIntoItem (notnull ItemBase old_item, string new_item_type, notnull PlayerBase player)
	{
		TurnItemIntoItemEx(player, new TurnItemIntoItemLambda(old_item, new_item_type, player));
	}
		
	static void TurnItemIntoItemEx (notnull PlayerBase player, ReplaceItemWithNewLambdaBase lambda)
	{
		player.ServerReplaceItemWithNew(lambda);
	}

	static void TurnItemInHandsIntoItem (notnull ItemBase old_item, string new_item_type, notnull PlayerBase player)
	{
		TurnItemInHandsIntoItemEx(player, new TurnItemIntoItemLambda(old_item, new_item_type, player));
	}
	
	static void TurnItemInHandsIntoItemEx (notnull PlayerBase player, ReplaceItemWithNewLambdaBase lambda)
	{
		player.ServerReplaceItemInHandsWithNew(lambda);
	}

	//!Spawns multiple piles of stackable ItemBase objects on ground (intended for generic use)
	static array<ItemBase> CreateItemBasePiles(string item_name, vector ground_position, float quantity, float health, bool floaty_spawn = false)
	{
		array<ItemBase>	item_piles;
		float max_stack_size;
		ItemBase pile;
		
		item_piles = new array<ItemBase>;
		max_stack_size = g_Game.ConfigGetInt("cfgVehicles " + item_name + " varStackMax");
		if( max_stack_size < 1)
			max_stack_size = g_Game.ConfigGetInt("cfgVehicles " + item_name + " varQuantityMax");
		if( max_stack_size < 1)
			max_stack_size = 1;
		
		int full_piles_count = Math.Floor(quantity/max_stack_size);
		int rest = quantity - (full_piles_count*max_stack_size);
		
		for ( int i = 0; i < full_piles_count; i++ )
		{
			if (floaty_spawn)
				pile = ItemBase.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_CREATEPHYSICS|ECE_UPDATEPATHGRAPH));
			else
				pile = ItemBase.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
			pile.SetQuantity(max_stack_size);
			pile.SetHealth(health);
			item_piles.Insert(pile);
		}
		if ( rest > 0 )
		{
			if (floaty_spawn)
				pile = ItemBase.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_CREATEPHYSICS|ECE_UPDATEPATHGRAPH));
			else
				pile = ItemBase.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
			pile.SetQuantity(rest);
			pile.SetHealth(health);
			item_piles.Insert(pile);
		}
		return item_piles;
	}
	
	static array<Magazine> CreateMagazinePiles(string item_name, vector ground_position, float quantity,  float health )
	{
		array<Magazine>	items;
		float stack_size;
		Magazine pile;
		
		items = new array<Magazine>;
		stack_size = g_Game.ConfigGetInt("cfgMagazines " + item_name + " count");
		
		int piles_count = Math.Floor(quantity/stack_size);
		int rest = quantity - (piles_count*stack_size);
		
		for ( int i = 0; i < piles_count; i++ )
		{
			pile = Magazine.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
			pile.ServerSetAmmoCount(stack_size);
			items.Insert(pile);
		}
		if ( rest > 0)
		{
			pile = Magazine.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
			pile.ServerSetAmmoCount(rest);
			items.Insert(pile);
		}
		return items;
	}
	
	static array<Magazine> CreateMagazinePilesFromBullet(string bullet_type, vector ground_position, float quantity,  float health )
	{
		array<Magazine>	items;
		items = new array<Magazine>;
		float stack_size;
		Magazine pile;
		string item_name;
		if( !g_Game.ConfigGetText("cfgAmmo " + bullet_type + " spawnPileType", item_name) )
			return items;
		
		stack_size = g_Game.ConfigGetInt("cfgMagazines " + item_name + " count");
		
		if(stack_size > 0)
		{
			int piles_count = Math.Floor(quantity/stack_size);
			int rest = quantity - (piles_count*stack_size);
		
			for ( int i = 0; i < piles_count; i++ )
			{
				pile = Magazine.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
				pile.ServerSetAmmoCount(stack_size);
				items.Insert(pile);
			}
			if ( rest > 0)
			{
				pile = Magazine.Cast(GetGame().CreateObjectEx(item_name, ground_position, ECE_PLACE_ON_SURFACE));
				pile.ServerSetAmmoCount(rest);
				items.Insert(pile);
			}
		}
		return items;
	}
	
	static int GetHealthLevelForAmmo(string class_name, float health)
	{
		float health_normalized = health / 100;
		string config_path = CFG_WEAPONSPATH + " " + class_name + " DamageSystem" + " GlobalHealth" + " healthLabels";
		CachedObjectsArrays.ARRAY_FLOAT.Clear();
		GetGame().ConfigGetFloatArray(config_path, CachedObjectsArrays.ARRAY_FLOAT);
		for(int i = 0; i < CachedObjectsArrays.ARRAY_FLOAT.Count(); i++)
		{
			if( health_normalized >= CachedObjectsArrays.ARRAY_FLOAT.Get(i) )
			{
				return i;
			}
		}
		return -1;
	}
	
	static float GetTypeMaxGlobalHealth(string class_name, string health_type = "Health")
	{
		float max_health;
		string cfg_path;
		
		if ( GetGame().ConfigIsExisting(CFG_VEHICLESPATH+" "+class_name) )
		{
			cfg_path = CFG_VEHICLESPATH;
		}
		else if ( GetGame().ConfigIsExisting(CFG_WEAPONSPATH+" "+class_name) )
		{
			cfg_path = CFG_WEAPONSPATH;
		}
		else if ( GetGame().ConfigIsExisting(CFG_MAGAZINESPATH+" "+class_name) )
		{
			cfg_path = CFG_MAGAZINESPATH;
		}
		cfg_path = cfg_path + " "+class_name+" DamageSystem GlobalHealth " + health_type + " hitpoints";
		max_health = GetGame().ConfigGetFloat(cfg_path);
		
		return max_health;
	}
	
	static bool GetProjectedCursorPos3d (out vector position, Weapon_Base weapon)
	{
		vector usti_hlavne_position = weapon.GetSelectionPositionMS( "usti hlavne" );
		vector konec_hlavne_position = weapon.GetSelectionPositionMS( "konec hlavne" );
		vector end_point = weapon.ModelToWorld(usti_hlavne_position);
		vector begin_point = weapon.ModelToWorld(konec_hlavne_position);
		vector contact_dir;
		int contact_component;
		
		vector aim_point = end_point - begin_point;
		aim_point = aim_point.Normalized() * PROJECTED_CURSOR_DISTANCE;
		aim_point = aim_point + end_point;
		
		if (DayZPhysics.RaycastRV(begin_point, aim_point, position, contact_dir, contact_component, null, null, null, false, false, ObjIntersectFire))
			return true;
		return false;
	}
	
	static void GetHeadBonePos(notnull PlayerBase player, out vector pos)
	{
		// Get position of head for starting trace pos, otherwise use sane default
		int headBoneIdx = player.GetBoneIndexByName("Head");
		if ( headBoneIdx == -1 )
			{ pos = player.GetPosition()[1] + 1.6; }
		else
			{ pos = player.GetBonePositionWS(headBoneIdx); }	
	}

	static vector GetHeadingVector(notnull PlayerBase player)
	{
		vector dir = vector.Zero;
		float headingAngle = GetHeadingAngle(player);
		dir[0] = Math.Cos(headingAngle + Math.PI_HALF);
		dir[2] = Math.Sin(headingAngle + Math.PI_HALF);
		
		return dir.Normalized();
	}

	static float GetHeadingAngle(notnull DayZPlayerImplement player)
	{
		HumanInputController hic = player.GetInputController();
		float headingAngle = hic.GetHeadingAngle();
		
		return headingAngle;
	}
	
	static float GetEnergyMetabolicSpeed(int movement_speed)
	{
		float speed;
		switch (movement_speed)
		{
		case DayZPlayerConstants.MOVEMENTIDX_WALK:
			speed = PlayerConstants.METABOLIC_SPEED_ENERGY_WALK;
		break;
		case DayZPlayerConstants.MOVEMENTIDX_RUN:
			speed = PlayerConstants.METABOLIC_SPEED_ENERGY_JOG;
		break;
		case DayZPlayerConstants.MOVEMENTIDX_SPRINT:
			speed = PlayerConstants.METABOLIC_SPEED_ENERGY_SPRINT;
		break;
		default:
			speed = 0;
		break;
		}

		speed += PlayerConstants.METABOLIC_SPEED_ENERGY_BASAL;
		return speed;
	}
	
	static float GetWaterMetabolicSpeed(int movement_speed)
	{
		float speed;
		switch (movement_speed)
		{
		case DayZPlayerConstants.MOVEMENTIDX_WALK:
			speed = PlayerConstants.METABOLIC_SPEED_WATER_WALK;
		break;
		case DayZPlayerConstants.MOVEMENTIDX_RUN:
			speed = PlayerConstants.METABOLIC_SPEED_WATER_JOG;
		break;
		case DayZPlayerConstants.MOVEMENTIDX_SPRINT:
			speed = PlayerConstants.METABOLIC_SPEED_WATER_SPRINT;
		break;
		default:
			speed = 0;
		break;
		}

		speed += PlayerConstants.METABOLIC_SPEED_WATER_BASAL;
		return speed;
	}
	
	static string ObtainRestrainItemTargetClassname(notnull EntityAI entity)
	{
		return entity.ConfigGetString( "OnRestrainChange");
	}
	
	static void TransformRestrainItem(EntityAI current_item, EntityAI tool, PlayerBase player_source, PlayerBase player_target, bool destroy = false)
	{
		bool type;
		
		if ( tool )
		{
			//is unrestrain and not struggle
			type = tool.ConfigGetBool("RestrainUnlockType");
		}
		string new_item_name = current_item.ConfigGetString( "OnRestrainChange");
		
		if ( new_item_name != "" )
		{
			if ( player_target )
			{
				if (player_target.IsAlive())
					MiscGameplayFunctions.TurnItemIntoItemEx(player_target, new ReplaceAndDestroyLambdaEx(current_item, new_item_name, player_target, type));
				else
					MiscGameplayFunctions.TurnItemIntoItemEx(player_source, new DestroyItemInCorpsesHandsAndCreateNewOnGndLambda(current_item, new_item_name, player_target, type));
			}
			else
			{
				MiscGameplayFunctions.TurnItemIntoItemEx(player_target, new ReplaceAndDestroyLambdaEx(current_item, new_item_name, player_target, type));
			}
		}
		else
		{
			Error("current_item:" +current_item+ ", tool:" +tool +". No value for 'OnRestrainChange' config parameter");
		}
	}
	
	static bool IsValueInRange(float value, float from, float to)
	{
		return (value >= from) && (value <= to);
	}

	//! Check if player direction(based on cone of defined angle) is oriented to target position
	static bool IsPlayerOrientedTowardPos(notnull DayZPlayerImplement player, vector target_pos, float cone_angle)
	{
		if (player)
		{
			vector player_dir = player.GetDirection();
			vector to_target_dir = target_pos - player.GetPosition();
	
			player_dir[1] = 0;
			to_target_dir[1] = 0;
		
			player_dir.Normalize();
			to_target_dir.Normalize();
		
			float cos_fi = vector.Dot(player_dir, to_target_dir);
			vector cross = player_dir * to_target_dir;
		
			int dir = Math.Acos(cos_fi) * Math.RAD2DEG;
				
			if( cross[1] < 0 )
				dir = -dir;

			//! dir in cone or in a tip of cone 
			if( (dir <= cone_angle && dir >= -cone_angle) || Math.AbsFloat(dir) == 90 )
			{
				return true;
			}
		}
		
		return false;
	}
	
	//TODO
	static string SanitizeString(string input)
	{
		int max_length = 512;
		string output = input;
		
		output = output.Substring(0,Math.Clamp(max_length,0,output.Length()));
		return output;
	}
	
	// deprecated - dont use
	static bool ComplexBuildCollideCheckClient( PlayerBase player, ActionTarget target, ItemBase item, string partName = "" )
	{
		return true;
	}
	
	static bool ComplexBuildCollideCheckClient( PlayerBase player, ActionTarget target, ItemBase item, int constraction_index )
	{
		BaseBuildingBase base_building = BaseBuildingBase.Cast( target.GetObject() );
		if (base_building)
		{
			Construction construction = base_building.GetConstruction();
			if (construction && BuildCondition( player, target, item, false, constraction_index ))
			{
				ConstructionActionData construction_action_data = player.GetConstructionActionData();
				string partName;
				if ( item )
				{
					partName = construction_action_data.GetBuildPartAtIndex(constraction_index).GetPartName();
				}
				else
				{
					partName = construction_action_data.GetBuildPartNoToolAtIndex(constraction_index).GetPartName();
				}
				bool boo;
				if (base_building.PerformRoofCheckForBase(partName,player,boo) && boo)
					return false;
				if ( player.IsPlacingLocal() || player.IsPlacingServer() )
					return false;
				
				/*float distance_root = vector.DistanceSq(target.GetCursorHitPos(), player.GetPosition());

				if (!CfgGameplayHandler.GetDisableDistanceCheck() && distance_root < UAMaxDistances.BASEBUILDING_SHORT)
				{
					return false;
				} */
				return !construction.IsColliding( partName );
			}
		}
		return false;
	}
	
	// deprecated - dont use
	static bool BuildCondition( PlayerBase player, ActionTarget target, ItemBase item, bool camera_check )
	{
		return true;
	}
	
	static bool BuildCondition( PlayerBase player, ActionTarget target, ItemBase item, bool camera_check, int constraction_index )
	{	
		Object targetObject = target.GetObject();
		if ( targetObject && targetObject.CanUseConstruction() )
		{
			BaseBuildingBase base_building = BaseBuildingBase.Cast( targetObject );
			ConstructionActionData construction_action_data = player.GetConstructionActionData();
			construction_action_data.SetTarget( targetObject );
			
			ConstructionPart constrution_part;
			if ( item )
			{
				constrution_part = construction_action_data.GetBuildPartAtIndex(constraction_index);
			}
			else
			{
				constrution_part = construction_action_data.GetBuildPartNoToolAtIndex(constraction_index);
			}

			//Debug
			/*
			if ( constrution_part )
			{
				Construction construction = base_building.GetConstruction();	
				construction.IsColliding( constrution_part.GetPartName() );
			}
			*/
			
			if ( constrution_part )
			{
				//camera and position checks
				bool position_check = ( base_building.MustBeBuiltFromOutside() && !base_building.IsPlayerInside(player, constrution_part.GetMainPartName()) ) || ( !base_building.MustBeBuiltFromOutside() && base_building.IsPlayerInside(player, constrution_part.GetMainPartName()) );
				if ( position_check && !player.GetInputController().CameraIsFreeLook() )
				{
					//Camera check (client-only)
					if ( camera_check )
					{
						if ( GetGame() && ( !GetGame().IsDedicatedServer() ) )
						{
							return !base_building.IsFacingCamera( constrution_part.GetMainPartName() );
						}
					}
					
					return true;
				}
			}
		}
		
		return false;
	}
	
	static void IsUnderRoofFromToCalculation(EntityAI entity, out vector from, out vector to, float height = GameConstants.ROOF_CHECK_RAYCAST_DIST)
	{
		vector minMax[2];
		entity.GetCollisionBox(minMax);

		vector size = Vector(0,0,0);
		//size[1] = minMax[1][1] - minMax[0][1];
		float from_override = entity.HeightStartCheckOverride();
		if (from_override > 0.0)
		{
			size[1] = from_override;
		}
		else
		{
			size[1] = minMax[1][1] - minMax[0][1];
		}
		
		from = entity.GetPosition() + size;  
		if ( entity.HeightCheckOverride() > 0 )
		{
			to = entity.GetPosition() + Vector(0, entity.HeightCheckOverride(), 0);
		}
		else
		{
			vector ceiling = "0 0 0";
			ceiling[1] = height;
			to = from + ceiling; //+size ??? offset to cast same distance
		}
	}
	
	static bool IsUnderRoof(EntityAI entity, float height = GameConstants.ROOF_CHECK_RAYCAST_DIST) 
	{
		return IsUnderRoofEx(entity, height, ObjIntersectView);
	}
	
	static bool IsUnderRoofEx(EntityAI entity, float height = GameConstants.ROOF_CHECK_RAYCAST_DIST, int geometry = ObjIntersectView) 
	{
		vector from;
		vector to;
		
		IsUnderRoofFromToCalculation(entity, from, to, height);

		vector contact_pos;
		vector contact_dir;

		int contact_component;
		//set<Object> hit_object = new set<Object>;
		bool boo = DayZPhysics.RaycastRV( from, to, contact_pos, contact_dir, contact_component, /*hit_object*/NULL, NULL, entity, false, false, geometry,0.25 );
		
		/*if (boo)
		{
			Debug.DrawSphere(from , 0.8,Colors.YELLOW, ShapeFlags.ONCE);
			Debug.DrawSphere(to , 0.8,Colors.RED, ShapeFlags.ONCE);
		}
		else
		{
			Debug.DrawSphere(from , 0.8,Colors.GREEN, ShapeFlags.ONCE);
			Debug.DrawSphere(to , 0.8,Colors.RED, ShapeFlags.ONCE);
		}*/
		
		return boo;
	}

	// cooking equipment effects (get position for steam particle)
	static vector GetSteamPosition( EntityAI parent )
	{
		vector particle_pos;
		float steam_offset = 0;
		
		if ( parent )
		{
			particle_pos = parent.GetPosition();
			
			if ( parent.IsInherited( PortableGasStove ) )
			{
				steam_offset = 0.2;
			}
			else if ( parent.IsInherited( FireplaceBase ) )
			{
				FireplaceBase fireplace = FireplaceBase.Cast( parent );
				
				if ( fireplace.IsBaseFireplace() )
				{
					steam_offset = 0.8;
				}
				else if ( fireplace.IsBarrelWithHoles() )
				{
					steam_offset = 1.1;
				}
				else if ( fireplace.IsFireplaceIndoor() )
				{
					steam_offset = 0.45;
				}
				else if ( fireplace.IsIndoorOven() )
				{
					steam_offset = 0.9;
				}
			}
		}
		
		particle_pos[1] = particle_pos[1] + steam_offset;
		
		return particle_pos;
	}
	
	static void DropAllItemsInInventoryInBounds(ItemBase ib, vector halfExtents)
	{
		if (!GetGame().IsServer())
			return;
		array<EntityAI> items = new array<EntityAI>;
		ib.GetInventory().EnumerateInventory(InventoryTraversalType.LEVELORDER, items);
		
		vector direction = ib.GetDirection();
		float dot = vector.Dot(direction, vector.Forward);
		
		float angle = Math.Acos(dot);	
		if (direction[0] < 0)
			angle = -angle;	

		float cos = Math.Cos(angle);
		float sin = Math.Sin(angle);
		
		EntityAI item;
		int count = items.Count();
		for ( int i = 0; i < count; ++i )
		{
			item = items.Get(i);
			if ( item )
				ib.GetInventory().DropEntityInBounds(InventoryMode.SERVER, ib, item, halfExtents, angle, cos, sin);					
		}
	}
	
	static void ThrowAllItemsInInventory(notnull EntityAI parent, int flags)
	{
		vector position = parent.GetPosition();
		vector orientation = parent.GetOrientation();
		vector rotation_matrix[3];
		float direction[4];
		
		vector minmax[2];
		parent.GetCollisionBox(minmax);

		Math3D.YawPitchRollMatrix( orientation, rotation_matrix );
		Math3D.MatrixToQuat( rotation_matrix, direction );

		vector randomPos;
		for ( int i = 0; i < parent.GetInventory().AttachmentCount(); ++i )
		{
			randomPos = Vector(position[0] + Math.RandomFloat(minmax[0][0], minmax[1][0]),
								position[1] + Math.RandomFloat(minmax[0][1], minmax[1][1]),
								position[2] + Math.RandomFloat(minmax[0][2], minmax[1][2]));
			
			ThrowEntityFromInventory(parent.GetInventory().GetAttachmentFromIndex( i ), randomPos, direction, -GetVelocity(parent), ThrowEntityFlags.NONE);
		}

		for ( int j = 0; j < parent.GetInventory().GetCargo().GetItemCount(); ++j )
		{
			randomPos = Vector(position[0] + Math.RandomFloat(minmax[0][0], minmax[1][0]),
								position[1] + Math.RandomFloat(minmax[0][1], minmax[1][1]),
								position[2] + Math.RandomFloat(minmax[0][2], minmax[1][2]));
			
			ThrowEntityFromInventory(parent.GetInventory().GetCargo().GetItem( j ), randomPos, direction, -GetVelocity(parent), ThrowEntityFlags.NONE);
		}
	}
	
	static void ThrowEntityFromInventory(notnull EntityAI entity, vector position, float direction[4], vector force, int flags)
	{	
		InventoryMode invMode = InventoryMode.SERVER;
		if ( !GetGame().IsMultiplayer() )
			invMode = InventoryMode.LOCAL;
		
		ItemBase entityIB;
		if (CastTo(entityIB, entity))
		{
			InventoryLocation dst = new InventoryLocation;	
			dst.SetGroundEx(entity, position, direction);
			
			if ( (flags & ThrowEntityFlags.SPLIT) && entityIB.CanBeSplit() )
			{
				for (int l = 0; l < entityIB.GetQuantity(); ++l)
				{
					ItemBase new_item = ItemBase.Cast( GameInventory.LocationCreateEntity( dst, entityIB.GetType(), ECE_NONE, RF_DEFAULT ) );
		
					if ( new_item )
					{
						MiscGameplayFunctions.TransferItemProperties(entityIB, new_item);
						entityIB.AddQuantity( -1 );
						new_item.SetQuantity( 1 );
						new_item.ThrowPhysically(null, force, false);
					}
				}
			}
			else
			{
				float stackable = entityIB.GetTargetQuantityMax();
				if ( !(stackable == 0 || stackable >= entityIB.GetQuantity()) )
				{					
					while (entityIB.GetQuantity() > stackable)
					{
						InventoryLocation spltDst = new InventoryLocation;
						position[1] = position[1] + 0.1;
						spltDst.SetGroundEx(entity, position, direction);
						
						ItemBase splitItem = entityIB.SplitIntoStackMaxToInventoryLocationEx( spltDst );
						splitItem.ThrowPhysically(null, force, false);
					}
				}
				
				InventoryLocation src = new InventoryLocation;
				entity.GetInventory().GetCurrentInventoryLocation(src);
				
				entity.GetInventory().TakeToDst(invMode, src, dst);
				entityIB.ThrowPhysically(null, force, false);
			}
		}
		else
		{
			entity.GetInventory().DropEntity(invMode, entity.GetHierarchyRoot(), entity);
			dBodyApplyImpulse(entity, force);
		}
	}

	static float GetCurrentItemHeatIsolation( ItemBase pItem )
	{
		float wetFactor;
		float healthFactor;

		float heatIsolation = pItem.GetHeatIsolation(); 	//! item heat isolation (from cfg)
		float itemHealthLabel = pItem.GetHealthLevel();		//! item health (state)
		float itemWetness = pItem.GetWet();					//! item wetness
		
		//! wet factor selection
		if ( itemWetness >= GameConstants.STATE_DRY && itemWetness < GameConstants.STATE_DAMP )
		{
			wetFactor = GameConstants.ENVIRO_ISOLATION_WETFACTOR_DRY;
		}
		else if ( itemWetness >= GameConstants.STATE_DAMP && itemWetness < GameConstants.STATE_WET )
		{
			wetFactor = GameConstants.ENVIRO_ISOLATION_WETFACTOR_DAMP;
		}
		else if ( itemWetness >= GameConstants.STATE_WET && itemWetness < GameConstants.STATE_SOAKING_WET )
		{
			wetFactor = GameConstants.ENVIRO_ISOLATION_WETFACTOR_WET;
		}
		else if ( itemWetness >= GameConstants.STATE_SOAKING_WET && itemWetness < GameConstants.STATE_DRENCHED )
		{
			wetFactor = GameConstants.ENVIRO_ISOLATION_WETFACTOR_SOAKED;
		}
		else if ( itemWetness >= GameConstants.STATE_DRENCHED )
		{
			wetFactor = GameConstants.ENVIRO_ISOLATION_WETFACTOR_DRENCHED;
		}
		
		//! health factor selection
		switch (itemHealthLabel)
		{
			case GameConstants.STATE_PRISTINE:
				healthFactor = GameConstants.ENVIRO_ISOLATION_HEALTHFACTOR_PRISTINE;
			break;
			
			case GameConstants.STATE_WORN:
				healthFactor = GameConstants.ENVIRO_ISOLATION_HEALTHFACTOR_WORN;
			break;
			
			case GameConstants.STATE_DAMAGED:
				healthFactor = GameConstants.ENVIRO_ISOLATION_HEALTHFACTOR_DAMAGED;
			break;
			
			case GameConstants.STATE_BADLY_DAMAGED:
				healthFactor = GameConstants.ENVIRO_ISOLATION_HEALTHFACTOR_B_DAMAGED;
			break;
			
			case GameConstants.STATE_RUINED:
				healthFactor = GameConstants.ENVIRO_ISOLATION_HEALTHFACTOR_RUINED;
			break;
		}
		
		//! apply factors
		heatIsolation *= healthFactor;
		heatIsolation *= wetFactor;

		return heatIsolation;
	}
	
	static void FilterObstructingObjects(array<Object> potentiallyObstructingObjects, out array<Object> obstructingObjects)
	{
		if (!obstructingObjects)
			obstructingObjects = new array<Object>;
		
		for ( int i = 0; i < potentiallyObstructingObjects.Count(); ++i )
		{
			Object obj = potentiallyObstructingObjects[i];
			if ( obj && ( obj.CanObstruct() || obj.CanProxyObstruct() ) )
				obstructingObjects.Insert(obj);
		}
	}
	
	static bool CanIgnoreDistanceCheck(Object obj)
	{
	    return obj.IsTransport() || obj.CanUseConstruction();
	}
	
	//! group objects that are close to each other together	
	static void FilterObstructedObjectsByGrouping(vector origin, float range, float distanceDelta, array<Object> objects, array<Object> obstructingObjects, out array<Object> filteredObjects, bool doDistanceCheck = false, bool checkIfDistanceCanBeIgnored = false, float maxDist = 0)
	{
		array<Object> vicinityObjects= new array<Object>;
		vicinityObjects.Copy(objects);
		
		int i = 0;
		int j = 0;
		int k = 0;			
		int mCount = vicinityObjects.Count();
		
		if (!filteredObjects)
			filteredObjects = new array<Object>;
		
		// Remove objects that are too far from the player anyways
		if ( doDistanceCheck )
		{
			for ( i = vicinityObjects.Count() - 1; i >= 0; --i )
			{
				Object obj = vicinityObjects[i];
				if ( obj && !CanIgnoreDistanceCheck( obj ) && vector.DistanceSq(origin, obj.GetPosition()) > maxDist * maxDist )
					vicinityObjects.Remove(i);
			}
		}

		// Sort obstructingObjects to have the furthest one first
		array<Object> sortedObstructingObjects = new array<Object>;
		array<float> distanceHelper = new array<float>;
		array<float> distanceHelperUnsorted = new array<float>;
		float distance, dist1, dist2;

		for ( i = 0; i < obstructingObjects.Count(); ++i )
		{
			distance = vector.DistanceSq(obstructingObjects[i].GetWorldPosition(), origin);
			distanceHelper.Insert(distance);
		}

		distanceHelperUnsorted.Copy(distanceHelper);
		distanceHelper.Sort();

		for ( i = distanceHelper.Count() - 1; i >= 0; --i )
			sortedObstructingObjects.Insert(obstructingObjects[distanceHelperUnsorted.Find(distanceHelper[i])]);

		array<ref array<Object>> tempGroups = new array<ref array<Object>>;
		array<ref array<Object>> objectGroups = new array<ref array<Object>>;
		array<Object> group;
		
		float cos = Math.Cos(90);
		float sin = Math.Sin(90);

		// Iterate through sorted obstructingObjects
		for ( i = 0; i < sortedObstructingObjects.Count(); ++i )
		{
			Object obstrObj = sortedObstructingObjects[i];
			vector worldPos = obstrObj.GetWorldPosition();
			vector min, max;
			vector minMax[2];
			if ( obstrObj.GetCollisionBox(minMax) )
			{
				min = minMax[0];
				max = minMax[1];
				max = max * (obstrObj.GetOrientation() * range);

				vector center, dx, dy, dz, half;
				center = (min + max) * 0.5;
				dz = obstrObj.GetOrientation();
				dx = vector.RotateAroundZero(dz, vector.Up, cos, sin);
				dy = vector.RotateAroundZero(dz, vector.Aside, cos, sin);
				half = (max - min) * 0.5;
				half = Vector(Math.AbsFloat(half[0]), Math.AbsFloat(half[1]), Math.AbsFloat(half[2]));

				group = new array<Object>;

				// Group objects within the above box
				for ( j = vicinityObjects.Count() - 1; j >= 0; --j )
				{
					Object vicObj = vicinityObjects[j];
					if ( vicObj )
					{
						vector d = vicObj.GetWorldPosition() - worldPos + center;
						if ( Math.AbsFloat(vector.Dot(d, dx)) <= half[0] && Math.AbsFloat(vector.Dot(d, dy)) <= half[1] && Math.AbsFloat(vector.Dot(d, dz)) <= half[2] )
						{
							group.Insert(vicObj);
							vicinityObjects.Remove(j);
						}
					}
				}

				if ( group.Count() > 0 )
					tempGroups.Insert(group);
			}
		}

		// Go through the objects grouped by obstruction to split them by distance too
		for ( i = 0; i < tempGroups.Count(); ++i )
			SplitArrayIntoGroupsByDistance(tempGroups[i], objectGroups, distanceDelta);

		// Split initial groups by distance
		SplitArrayIntoGroupsByDistance(vicinityObjects, objectGroups, distanceDelta);

		// Raycast accordingly to groups		
		IsObjectObstructedCache cache = new IsObjectObstructedCache(origin, mCount);
		for ( i = 0; i < objectGroups.Count(); ++i )
		{
			array<Object> objectGroup = objectGroups[i];
			Object sampleObject = objectGroup[0];

			if ( !IsObjectObstructedEx(sampleObject, cache) )
				filteredObjects.InsertAll(objectGroup);

			cache.ClearCache();
		}
	}

	static void SplitArrayIntoGroupsByDistance(array<Object> objects, array<ref array<Object>> objectGroups, float squaredDistanceDelta)
	{
		array<Object> group;
		for ( int i = 0; i < objects.Count(); )
		{
			Object obj1 = objects[i];
			if ( obj1 )
			{
				group = new array<Object>;
				group.Insert(obj1);
				for ( int j = objects.Count() - 1; j > i; --j )
				{
					Object obj2 = objects[j];
					if ( obj1 && obj2 )
					{
						vector start = obj1.GetWorldPosition();
						vector end = obj2.GetWorldPosition();

						float distance = vector.DistanceSq(start, end);
						if ( distance < squaredDistanceDelta )
						{								
							group.Insert(obj2);
							objects.Remove(j);
						}
					}
				}
				objectGroups.Insert(group);
				objects.Remove(i);
				continue;
			}
			++i;
		}
	}
	
	static bool IsObjectObstructed(Object object, bool doDistanceCheck = false, vector distanceCheckPos = "0 0 0", float maxDist = 0)
	{
		vector rayStart;
		MiscGameplayFunctions.GetHeadBonePos(PlayerBase.Cast(GetGame().GetPlayer()), rayStart);
		IsObjectObstructedCache cache = new IsObjectObstructedCache(rayStart, 1);

		return IsObjectObstructedEx(object, cache, doDistanceCheck, distanceCheckPos, maxDist);
	}

	static bool IsObjectObstructedEx(Object object, IsObjectObstructedCache cache, bool doDistanceCheck = false, vector distanceCheckPos = "0 0 0", float maxDist = 0)
	{
		if (!object)
			return true;
		
		PlayerBase player = PlayerBase.Cast(g_Game.GetPlayer());
		if (doDistanceCheck && vector.DistanceSq(player.GetPosition(), distanceCheckPos) > maxDist * maxDist)
			return true;

		cache.ObjectCenterPos = object.GetCenter();
		
		return IsObjectObstructedFilterEx(object, cache, player);
	}
 	
	static bool IsObjectObstructedProxy(Object object, IsObjectObstructedCache cache, PlayerBase player)
	{
		if (object.CanProxyObstruct())
		{
			RaycastRVParams rayInput = new RaycastRVParams(cache.RaycastStart, cache.ObjectCenterPos, player);
			DayZPhysics.RaycastRVProxy(rayInput, cache.HitProxyObjects);				
			if (cache.HitProxyObjects)
			{
				if (cache.HitProxyObjects.Count() > 0)
				{
					if (cache.HitProxyObjects[0].hierLevel > 0)
					{
						// ignores attachments on player
						if (!cache.HitProxyObjects[0].parent.IsMan())
						{
							if (cache.HitProxyObjects[0].parent)
							{
								EntityAI proxyParent = EntityAI.Cast(cache.HitProxyObjects[0].parent);
								if (proxyParent.GetInventory() && proxyParent.GetInventory().GetCargo())
									return true;
							}
						}	
					}
				}
			}
		}
		return false;
	}
	
	static bool IsObjectObstructedFilter(Object object, IsObjectObstructedCache cache, PlayerBase player)
	{
		for ( int m = 0; m < cache.HitObjects.Count(); m++ )
		{
			Object hit_object = cache.HitObjects.Get(m);
			
			if ( hit_object.CanObstruct() )
				return true;

			//4.3. ignore item if items are big and heavy >= OBJECT_OBSTRUCTION_WEIGHT 
			//EntityAI eai;
			//if ( Class.CastTo( eai, hit_object ) )
			//{					
			//	if ( eai.GetWeight() >= OBJECT_OBSTRUCTION_WEIGHT )
			//	{
			//		if ( eai != filtered_object && eai.GetHierarchyRoot() != filtered_object )
			//		{
			//			//Print("!!!!!obstacle vaha: " + hit_object);
			//			is_obstructed = true;
			//		}
			//	}
			//}
		}
		
		return false;
	}
	
	//! groups 'RaycastRVProxy' and 'RaycastRV' approaches into one method, allowes for comprehensive geometry override, if desired
	static bool IsObjectObstructedFilterEx(Object object, IsObjectObstructedCache cache, PlayerBase player, int geometryTypeOverride = -1)
	{
		//first proxy geometries
		RaycastRVParams rayInput = new RaycastRVParams(cache.RaycastStart, cache.ObjectCenterPos, player);
		rayInput.flags = CollisionFlags.ALLOBJECTS;
		if (geometryTypeOverride != -1)
			rayInput.type = geometryTypeOverride; //default 'ObjIntersectView'
		DayZPhysics.RaycastRVProxy(rayInput, cache.HitProxyObjects);
		int count;
		int i;
		
		if (cache.HitProxyObjects)
		{
			count = cache.HitProxyObjects.Count();
			Object parent;
			for (i = 0; i < count; i++)
			{
				if (cache.HitProxyObjects[i].hierLevel > 0) //parent has to exist, skipping nullcheck
				{
					parent = cache.HitProxyObjects[i].parent;
					if (parent && !parent.IsMan() && parent.CanProxyObstruct())
					{
						if (parent != object || (parent == object && object.CanProxyObstructSelf()))
							return true;
					}	
				}
			}
		}
		
		//second, regular raycast
		int geometry = ObjIntersectFire; //default for the RV raycast
		if (geometryTypeOverride != -1)
			geometry = geometryTypeOverride;
		DayZPhysics.RaycastRV(cache.RaycastStart, cache.ObjectCenterPos, cache.ObjectContactPos, cache.ObjectContactDir, cache.ContactComponent, cache.HitObjects, object, GetGame().GetPlayer(), false, false, geometry, 0.0, CollisionFlags.ALLOBJECTS);
		count = cache.HitObjects.Count();
		for (i = 0; i < count; i++ )
		{
			if (cache.HitObjects[i].CanObstruct())
				return true;
		}
		
		return false;
	}

	//Inflict absolute damage to item (used on tools when completing actions)
	static void DealAbsoluteDmg(ItemBase item, float dmg)
	{
		item.DecreaseHealth(dmg, false);
	}
	
	//Function used to normailze values, enter the used value and the max value (max will become 1)
	static float Normalize(int val, int maxVal)
	{
		if (maxVal == 0)
		{
			Debug.LogError("Division by 0 is not allowed");
			return 0;
		}
		
		return val / maxVal;
	}
	
	static float Bobbing(float period, float amplitude, float elapsedTime)
	{
		//Prevent division by 0
		if ( period == 0 )
			period = 1;
		
		elapsedTime /= period;
		
		float cycle;
		cycle += elapsedTime;
		cycle = FModulus(cycle, 360);
		cycle = Math.Sin(cycle) * amplitude;
		
		return cycle;
	}
	
	// DEPRECATED, use Math.ModFloat directly instead
	static float FModulus(float x, float y)
	{
		return Math.ModFloat(x, y);
	}
	
	static void RemoveSplint( PlayerBase player )
	{
		EntityAI entity = player.GetInventory().CreateInInventory("Splint");
		if (!entity)
			entity = player.SpawnEntityOnGroundOnCursorDir("Splint", 0.5);
		
		EntityAI attachment;
		ItemBase new_item = ItemBase.Cast(entity);
		Class.CastTo(attachment, player.GetItemOnSlot("Splint_Right"));
		if ( attachment && attachment.GetType() == "Splint_Applied" )
		{
			if (new_item)
			{	
				MiscGameplayFunctions.TransferItemProperties(attachment,new_item);
				
				if (GetGame().IsServer())
				{
					//Lower health level of splint after use
					if (new_item.GetHealthLevel() < 4)
					{
						int newDmgLevel = new_item.GetHealthLevel() + 1;
						
						float max = new_item.GetMaxHealth("","");
						
						switch ( newDmgLevel )
						{
							case GameConstants.STATE_BADLY_DAMAGED:
								new_item.SetHealth( "", "", max * GameConstants.DAMAGE_BADLY_DAMAGED_VALUE );
								break;
		
							case GameConstants.STATE_DAMAGED:
								new_item.SetHealth( "", "", max * GameConstants.DAMAGE_DAMAGED_VALUE );
								break;
		
							case GameConstants.STATE_WORN:
								new_item.SetHealth( "", "", max * GameConstants.DAMAGE_WORN_VALUE );
								break;
							
							case GameConstants.STATE_RUINED:
								new_item.SetHealth( "", "", max * GameConstants.DAMAGE_RUINED_VALUE );
								break;
							
							default:
								break;
						}
					}
				}
			}
			
			attachment.Delete();
		}
	}
	
	//! checks if we should teleport the player to a safe location and if so, performs the teleportation
	static void TeleportCheck(notnull PlayerBase player, notnull array<ref array<float>> safe_positions)
	{
		if( player.GetSimulationTimeStamp() < 20 && !player.IsPersistentFlag(PersistentFlag.AREA_PRESENCE) )
		{
			//simulation time is bellow a threshold, which means the player has recently connected,
			//the player does not have the AREA_PRESENCE flag set, which means they were not inside the area when they disconnected,
			//that means they just spawned into a contaminated area, lets move them somewhere safe
			vector player_pos = player.GetPosition();
			vector closest_safe_pos = MiscGameplayFunctions.GetClosestSafePos(player_pos, safe_positions);
			
			if (player_pos!=closest_safe_pos)
			{
				closest_safe_pos[1] = GetGame().SurfaceY(closest_safe_pos[0], closest_safe_pos[2]);
				
				player.SetPosition( closest_safe_pos );//...so lets teleport them somewhere safe
				//DeveloperTeleport.SetPlayerPosition(player, closest_safe_pos);
				GetGame().RPCSingleParam(player, ERPCs.RPC_WARNING_TELEPORT, null, true, player.GetIdentity());
			}
			
			player.SetPersistentFlag(PersistentFlag.AREA_PRESENCE, false);
		}
	}
	
	
	static vector GetClosestSafePos(vector to_pos, notnull array<ref array<float>> positions)
	{
		vector closest_pos = to_pos;
		float smallest_dist = float.MAX;
		foreach( array<float> pos:positions)
		{
			vector vpos = "0 0 0";
			vpos[0] = pos[0];
			vpos[2] = pos[1];

			to_pos[1] = 0;
			float dist = vector.DistanceSq(to_pos, vpos);//2d dist sq
			if ( dist < smallest_dist)
			{
				smallest_dist = dist;
				closest_pos = vpos;
			}
		}
		return closest_pos;
	}
	
	static void GenerateAINoiseAtPosition(vector position, float lifeTime, NoiseParams noiseParams)
	{
		if (GetGame().IsServer())
		{
			NoiseSystem noise = GetGame().GetNoiseSystem();
			if (noise)
			{
				noise.AddNoiseTarget(position, lifeTime, noiseParams);
			}
		}
	}

	static float GetMinValue(array<float> pArray)
	{
		float minValue = 0.0;
		for (int i = 0; i < pArray.Count(); i++)
		{
			if (minValue == 0 || pArray.Get(i) < minValue)
			{
				minValue = pArray.Get(i);
			}
		}
		
		return minValue;
	}
	
	static float GetMaxValue(array<float> pArray)
	{
		float maxValue = 0.0;
		for (int i = 0; i < pArray.Count(); i++)
		{
			if (maxValue == 0 || pArray.Get(i) > maxValue)
			{
				maxValue = pArray.Get(i);
			}
		}
		
		return maxValue;
	}
		
	static string GetItemDisplayName(string type)
	{
		return GetGame().ConfigGetTextOut("CfgVehicles " + type + " displayName");
	}

	static bool IsComponentInSelection(array<Selection> pSelection, string pCompName)
	{
		if (pSelection.Count() == 0 || pCompName.Length() == 0)
		{
			return false;
		}

		for (int i = 0; i < pSelection.Count(); ++i)
		{
			pCompName.ToLower();
			if (pSelection[i] && pSelection[i].GetName() == pCompName)
			{
				return true;
			}
		}

		return false;
	}
	
	static int GetComponentIndex(array<Selection> pSelection, string pCompName)
	{
		if (!MiscGameplayFunctions.IsComponentInSelection(pSelection, pCompName))
		{
			return INDEX_NOT_FOUND;
		}

		for (int i = 0; i < pSelection.Count(); ++i)
		{
			pCompName.ToLower();
			if (pSelection[i] && pSelection[i].GetName() == pCompName)
			{
				return i;
			}
		}

		return INDEX_NOT_FOUND;
	}
	
	static void RemoveAllAttachedChildrenByTypename(notnull EntityAI parent, array<typename> listOfTypenames)
	{
		if (listOfTypenames.Count() > 0)
		{
			Object child = Object.Cast(parent.GetChildren());
			while (child)
			{
				Object childToRemove = child;
				child = Object.Cast(child.GetSibling());
	
				if (childToRemove.IsAnyInherited(listOfTypenames))
				{
					vector pos = childToRemove.GetPosition();			
					parent.RemoveChild(childToRemove, false);
	
					vector m4[4];
					Math3D.MatrixIdentity4(m4);
					m4[3] = pos;
					childToRemove.SetTransform(m4);
					childToRemove.PlaceOnSurface();
				}
			}
		}
	}
	//! Fills the provided array with all children entities in hierarchy of this entity
	static void GetAttachedChildren(IEntity parent, array<IEntity> outputObjects)
	{
		IEntity child = parent.GetChildren();
		while (child)
		{
			outputObjects.Insert(child);
			child = child.GetSibling();
		}
	}
	
	static void SoakItemInsideParentContainingLiquidAboveThreshold(notnull ItemBase item, notnull ItemBase parent, float liquidQuantityThresholdPercentage = 0.05)
	{
		if (g_Game.IsServer())
		{
			if (parent.GetLiquidType() != 0 && parent.GetQuantityNormalized() > liquidQuantityThresholdPercentage)
				item.SetWetMax();
		}
	}	
}

class DestroyItemInCorpsesHandsAndCreateNewOnGndLambda : ReplaceAndDestroyLambda
{
	// @NOTE m_Player == target player - i.e. restrained one
	void DestroyItemInCorpsesHandsAndCreateNewOnGndLambda(EntityAI old_item, string new_item_type, PlayerBase player, bool destroy = false)
	{
		InventoryLocation gnd = new InventoryLocation;
		vector mtx[4];
		if (old_item)
			old_item.GetTransform(mtx);
		else
			player.GetTransform(mtx);
		gnd.SetGround(NULL, mtx);
		OverrideNewLocation(gnd);
	}
	
	protected override void RemoveOldItemFromLocation()
	{
		super.RemoveOldItemFromLocation();
		m_Player.GetHumanInventory().OnEntityInHandsDestroyed(m_OldLocation);
	}
}

// This was created since IsObjectObstructed is often called multiple times in one frame
// And apparently it seems that keeping this data in one struct seems to be more efficient than creating the variables dozens of times per frame
class IsObjectObstructedCache // Pretending this is a struct
{
	// Outside data
	vector RaycastStart = "0 0 0";
	int TotalObjects = 0;
	
	// Inside data
	vector ObjectCenterPos = "0 0 0";
	vector ObjectContactPos = "0 0 0";
	vector ObjectContactDir = "0 0 0";
	int ContactComponent = -1;
	ref array<ref RaycastRVResult> HitProxyObjects = new array<ref RaycastRVResult>;	
	ref set<Object> HitObjects = new set<Object>;
	
	void IsObjectObstructedCache(vector rayCastStart, int totalObjects)
	{
		RaycastStart = rayCastStart;
		TotalObjects = totalObjects;
	}
	
	// Only inside data
	void ClearCache()
	{		
		ObjectCenterPos = "0 0 0";
		ObjectContactPos = "0 0 0";
		ObjectContactDir = "0 0 0";
		ContactComponent = -1;
		HitProxyObjects.Clear();
		HitObjects.Clear();
	}
}