aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Grids/Notifiers/GridNotifiers.h
blob: 497db5d219a0b6ccc08eb0e143acc79ef1d23fb7 (plain)
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
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITY_GRIDNOTIFIERS_H
#define TRINITY_GRIDNOTIFIERS_H

#include "AreaTrigger.h"
#include "Creature.h"
#include "Corpse.h"
#include "Conversation.h"
#include "DynamicObject.h"
#include "GameObject.h"
#include "Player.h"
#include "SceneObject.h"
#include "Spell.h"
#include "SpellInfo.h"
#include "UnitAI.h"
#include "UpdateData.h"

namespace Trinity
{
    template<typename ObjectType>
    struct GridMapTypeMaskForType : std::integral_constant<GridMapTypeMask, GridMapTypeMask(0)> { };

    template<> struct GridMapTypeMaskForType<Corpse> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_CORPSE> { };
    template<> struct GridMapTypeMaskForType<Creature> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_CREATURE> { };
    template<> struct GridMapTypeMaskForType<DynamicObject> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_DYNAMICOBJECT> { };
    template<> struct GridMapTypeMaskForType<GameObject> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_GAMEOBJECT> { };
    template<> struct GridMapTypeMaskForType<Player> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_PLAYER> { };
    template<> struct GridMapTypeMaskForType<AreaTrigger> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_AREATRIGGER> { };
    template<> struct GridMapTypeMaskForType<SceneObject> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_SCENEOBJECT> { };
    template<> struct GridMapTypeMaskForType<Conversation> : std::integral_constant<GridMapTypeMask, GRID_MAP_TYPE_MASK_CONVERSATION> { };

    struct TC_GAME_API VisibleNotifier
    {
        Player &i_player;
        UpdateData i_data;
        std::set<WorldObject*> i_visibleNow;
        GuidUnorderedSet vis_guids;

        VisibleNotifier(Player &player);
        ~VisibleNotifier();
        template<class T> void Visit(GridRefManager<T> &m);
        void SendToSelf(void);

        VisibleNotifier(VisibleNotifier const&) = delete;
        VisibleNotifier(VisibleNotifier&&) = delete;
        VisibleNotifier& operator=(VisibleNotifier const&) = delete;
        VisibleNotifier& operator=(VisibleNotifier&&) = delete;
    };

    struct VisibleChangesNotifier
    {
        IteratorPair<WorldObject**> i_objects;

        explicit VisibleChangesNotifier(IteratorPair<WorldObject**> objects) : i_objects(objects) { }
        template<class T> void Visit(GridRefManager<T> &) { }
        void Visit(PlayerMapType &);
        void Visit(CreatureMapType &);
        void Visit(DynamicObjectMapType &);
    };

    struct TC_GAME_API PlayerRelocationNotifier : public VisibleNotifier
    {
        PlayerRelocationNotifier(Player &player) : VisibleNotifier(player) { }

        template<class T> void Visit(GridRefManager<T> &m) { VisibleNotifier::Visit(m); }
        void Visit(CreatureMapType &);
        void Visit(PlayerMapType &);
    };

    struct TC_GAME_API CreatureRelocationNotifier
    {
        Creature &i_creature;
        CreatureRelocationNotifier(Creature &c) : i_creature(c) { }
        template<class T> void Visit(GridRefManager<T> &) { }
        void Visit(CreatureMapType &);
        void Visit(PlayerMapType &);
    };

    struct TC_GAME_API DelayedUnitRelocation
    {
        Map &i_map;
        Cell &cell;
        CellCoord &p;
        const float i_radius;
        DelayedUnitRelocation(Cell &c, CellCoord &pair, Map &map, float radius) :
            i_map(map), cell(c), p(pair), i_radius(radius) { }
        template<class T> void Visit(GridRefManager<T> &) { }
        void Visit(CreatureMapType &);
        void Visit(PlayerMapType   &);
    };

    struct TC_GAME_API AIRelocationNotifier
    {
        Unit &i_unit;
        bool isCreature;
        explicit AIRelocationNotifier(Unit &unit) : i_unit(unit), isCreature(unit.GetTypeId() == TYPEID_UNIT)  { }
        template<class T> void Visit(GridRefManager<T> &) { }
        void Visit(CreatureMapType &);
    };

    struct TC_GAME_API CreatureAggroGracePeriodExpiredNotifier
    {
        Creature& i_creature;
        CreatureAggroGracePeriodExpiredNotifier(Creature& c) : i_creature(c) { }
        template<class T> void Visit(GridRefManager<T>&) { }
        void Visit(CreatureMapType&);
        void Visit(PlayerMapType&);
    };

    struct GridUpdater
    {
        GridType &i_grid;
        uint32 i_timeDiff;
        GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) { }

        template<class T> void updateObjects(GridRefManager<T> &m)
        {
            for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
                iter->GetSource()->Update(i_timeDiff);
        }

        void Visit(PlayerMapType &m) { updateObjects<Player>(m); }
        void Visit(CreatureMapType &m){ updateObjects<Creature>(m); }
        void Visit(GameObjectMapType &m) { updateObjects<GameObject>(m); }
        void Visit(DynamicObjectMapType &m) { updateObjects<DynamicObject>(m); }
        void Visit(CorpseMapType &m) { updateObjects<Corpse>(m); }
        void Visit(AreaTriggerMapType &m) { updateObjects<AreaTrigger>(m); }
        void Visit(SceneObjectMapType &m) { updateObjects<SceneObject>(m); }
        void Visit(ConversationMapType &m) { updateObjects<Conversation>(m); }
    };

    struct PacketSenderRef
    {
        WorldPacket const* Data;

        PacketSenderRef(WorldPacket const* message) : Data(message) { }

        void operator()(Player const* player) const
        {
            player->SendDirectMessage(Data);
        }
    };

    template<typename Packet>
    struct PacketSenderOwning
    {
        Packet Data;

        void operator()(Player const* player) const
        {
            player->SendDirectMessage(Data.GetRawPacket());
        }
    };

    template<typename PacketSender>
    struct MessageDistDeliverer
    {
        WorldObject const* i_source;
        PacketSender& i_packetSender;
        PhaseShift const* i_phaseShift;
        float i_distSq;
        Team team;
        Player const* skipped_receiver;
        bool required3dDist;
        MessageDistDeliverer(WorldObject const* src, PacketSender& packetSender, float dist, bool own_team_only = false, Player const* skipped = nullptr, bool req3dDist = false)
            : i_source(src), i_packetSender(packetSender), i_phaseShift(&src->GetPhaseShift()), i_distSq(dist * dist)
            , team(TEAM_OTHER)
            , skipped_receiver(skipped)
            , required3dDist(req3dDist)
        {
            if (own_team_only)
                if (Player const* player = src->ToPlayer())
                    team = player->GetEffectiveTeam();
        }

        void Visit(PlayerMapType &m) const;
        void Visit(CreatureMapType &m) const;
        void Visit(DynamicObjectMapType &m) const;
        template<class SKIP> void Visit(GridRefManager<SKIP> &) const { }

        void SendPacket(Player const* player) const
        {
            // never send packet to self
            if (player == i_source || (team && player->GetEffectiveTeam() != team) || skipped_receiver == player)
                return;

            if (!player->HaveAtClient(i_source))
                return;

            i_packetSender(player);
        }
    };

    template<typename PacketSender>
    struct MessageDistDelivererToHostile
    {
        Unit* i_source;
        PacketSender& i_packetSender;
        PhaseShift const* i_phaseShift;
        float i_distSq;

        MessageDistDelivererToHostile(Unit* src, PacketSender& packetSender, float dist)
            : i_source(src), i_packetSender(packetSender), i_phaseShift(&src->GetPhaseShift()), i_distSq(dist * dist)
        {
        }

        void Visit(PlayerMapType &m) const;
        void Visit(CreatureMapType &m) const;
        void Visit(DynamicObjectMapType &m) const;
        template<class SKIP> void Visit(GridRefManager<SKIP> &) const { }

        void SendPacket(Player const* player) const
        {
            // never send packet to self
            if (player == i_source || !player->HaveAtClient(i_source) || player->IsFriendlyTo(i_source))
                return;

            i_packetSender(player);
        }
    };

    struct ObjectUpdater
    {
        uint32 i_timeDiff;
        explicit ObjectUpdater(const uint32 diff) : i_timeDiff(diff) { }
        template<class T> void Visit(GridRefManager<T> &m);
        void Visit(PlayerMapType &) { }
        void Visit(CorpseMapType &) { }
    };

    // SEARCHERS & LIST SEARCHERS & WORKERS

    // WorldObject searchers & workers
    enum class WorldObjectSearcherContinuation
    {
        Continue,
        Return
    };

    template<typename Type>
    class SearcherFirstObjectResult
    {
        Type& result;

    protected:
        explicit SearcherFirstObjectResult(Type& ref_) : result(ref_) { }

        WorldObjectSearcherContinuation ShouldContinue() const
        {
            return result ? WorldObjectSearcherContinuation::Return : WorldObjectSearcherContinuation::Continue;
        }

        void Insert(Type object)
        {
            result = object;
        }
    };

    template<typename Type>
    class SearcherLastObjectResult
    {
        Type& result;

    protected:
        explicit SearcherLastObjectResult(Type& ref_) : result(ref_) { }

        WorldObjectSearcherContinuation ShouldContinue() const
        {
            return WorldObjectSearcherContinuation::Continue;
        }

        void Insert(Type object)
        {
            result = object;
        }
    };

    // Generic base class to insert elements into arbitrary containers using push_back
    template<typename Type>
    class SearcherContainerResult
    {
        using InserterType = void(*)(void*, Type&&);

        void* ref;
        InserterType inserter;

    protected:
        template<typename T>
        explicit SearcherContainerResult(T& ref_) : ref(&ref_)
        {
            inserter = [](void* containerRaw, Type&& object)
            {
                T* container = reinterpret_cast<T*>(containerRaw);
                container->insert(container->end(), std::move(object));
            };
        }

        WorldObjectSearcherContinuation ShouldContinue() const
        {
            return WorldObjectSearcherContinuation::Continue;
        }

        void Insert(Type object)
        {
            inserter(ref, std::move(object));
        }
    };

    struct DynamicGridMapTypeMaskCheck
    {
        DynamicGridMapTypeMaskCheck(uint32 mask) : MaskValue(mask) { }

        static constexpr bool IsStatic = false;

        uint32 MaskValue;

        constexpr bool Includes(uint32 mapTypeMask) const
        {
            return (MaskValue & mapTypeMask) != 0;
        }
    };

    template <uint32 MapTypeMask>
    struct StaticGridMapTypeMaskCheck
    {
        StaticGridMapTypeMaskCheck(uint32) { }

        static constexpr bool IsStatic = true;

        static constexpr bool Includes(uint32 mapTypeMask)
        {
            return (MapTypeMask & mapTypeMask) != 0;
        }
    };

    template<class Check, class Result, class MapTypeMaskCheck = DynamicGridMapTypeMaskCheck>
    struct WorldObjectSearcherBase : Result
    {
        MapTypeMaskCheck i_mapTypeMask;
        PhaseShift const* i_phaseShift;
        Check& i_check;

        template<class T>
        void Visit(GridRefManager<T>& m)
        {
            if constexpr (MapTypeMaskCheck::IsStatic)
            {
                if constexpr (MapTypeMaskCheck::Includes(GridMapTypeMaskForType<T>::value))
                    VisitImpl(m);
            }
            else
            {
                if (i_mapTypeMask.Includes(GridMapTypeMaskForType<T>::value))
                    VisitImpl(m);
            }
        }

    protected:
        template<typename Container>
        WorldObjectSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : Result(result), i_mapTypeMask(mapTypeMask), i_phaseShift(&phaseShift), i_check(check) { }

    private:
        template<class T>
        void VisitImpl(GridRefManager<T>&);
    };

    template<class Work, class MapTypeMaskCheck = DynamicGridMapTypeMaskCheck>
    struct WorldObjectWorkerBase
    {
        MapTypeMaskCheck i_mapTypeMask;
        PhaseShift const* i_phaseShift;
        Work& i_work;

        template<class T>
        void Visit(GridRefManager<T> const& m)
        {
            if constexpr (MapTypeMaskCheck::IsStatic)
            {
                if constexpr (MapTypeMaskCheck::Includes(GridMapTypeMaskForType<T>::value))
                    VisitImpl(m);
            }
            else
            {
                if (i_mapTypeMask.Includes(GridMapTypeMaskForType<T>::value))
                    VisitImpl(m);
            }
        }

    protected:
        WorldObjectWorkerBase(PhaseShift const& phaseShift, Work& work, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : i_mapTypeMask(mapTypeMask), i_phaseShift(&phaseShift), i_work(work) { }

    private:
        template<class T>
        inline void VisitImpl(GridRefManager<T> const& m)
        {
            for (GridReference<T> const& ref : m)
                if (ref.GetSource()->InSamePhase(*i_phaseShift))
                    this->i_work(ref.GetSource());
        }
    };

    template<class Check>
    struct WorldObjectSearcher : WorldObjectSearcherBase<Check, SearcherFirstObjectResult<WorldObject*>>
    {
        WorldObjectSearcher(PhaseShift const& phaseShift, WorldObject*& result, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectSearcherBase<Check, SearcherFirstObjectResult<WorldObject*>>(phaseShift, result, check, mapTypeMask) { }

        WorldObjectSearcher(WorldObject const* searcher, WorldObject*& result, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectSearcher(searcher->GetPhaseShift(), result, check, mapTypeMask) { }
    };

    template<class Check>
    struct WorldObjectLastSearcher : WorldObjectSearcherBase<Check, SearcherLastObjectResult<WorldObject*>>
    {
        WorldObjectLastSearcher(PhaseShift const& phaseShift, WorldObject*& result, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectSearcherBase<Check, SearcherLastObjectResult<WorldObject*>>(phaseShift, result, check, mapTypeMask) { }

        WorldObjectLastSearcher(WorldObject const* searcher, WorldObject*& result, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectLastSearcher(searcher->GetPhaseShift(), result, check, mapTypeMask) { }
    };

    template<class Check>
    struct WorldObjectListSearcher : WorldObjectSearcherBase<Check, SearcherContainerResult<WorldObject*>>
    {
        template<typename Container>
        WorldObjectListSearcher(PhaseShift const& phaseShift, Container& container, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectSearcherBase<Check, SearcherContainerResult<WorldObject*>>(phaseShift, container, check, mapTypeMask) { }

        template<typename Container>
        WorldObjectListSearcher(WorldObject const* searcher, Container& container, Check& check, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectListSearcher(searcher->GetPhaseShift(), container, check, mapTypeMask) { }
    };

    template<class Check, typename Container>
    WorldObjectListSearcher(PhaseShift const&, Container&, Check const&) -> WorldObjectListSearcher<Check const>;

    template<class Check, typename Container>
    WorldObjectListSearcher(WorldObject const*, Container&, Check const&) -> WorldObjectListSearcher<Check const>;

    template<class Work>
    struct WorldObjectWorker : WorldObjectWorkerBase<Work>
    {
        WorldObjectWorker(PhaseShift const& phaseShift, Work& work, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectWorkerBase<Work>(phaseShift, work, mapTypeMask) { }

        WorldObjectWorker(WorldObject const* searcher, Work& work, uint32 mapTypeMask = GRID_MAP_TYPE_MASK_ALL)
            : WorldObjectWorker(searcher->GetPhaseShift(), work, mapTypeMask) { }
    };

    // Gameobject searchers

    template<class Check, class Result>
    struct GameObjectSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_GAMEOBJECT>>
    {
        template<typename Container>
        GameObjectSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_GAMEOBJECT>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct GameObjectSearcher : GameObjectSearcherBase<Check, SearcherFirstObjectResult<GameObject*>>
    {
        GameObjectSearcher(PhaseShift const& phaseShift, GameObject*& result, Check& check)
            : GameObjectSearcherBase<Check, SearcherFirstObjectResult<GameObject*>>(phaseShift, result, check) { }

        GameObjectSearcher(WorldObject const* searcher, GameObject*& result, Check& check)
            : GameObjectSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    // Last accepted by Check GO if any (Check can change requirements at each call)
    template<class Check>
    struct GameObjectLastSearcher : GameObjectSearcherBase<Check, SearcherLastObjectResult<GameObject*>>
    {
        GameObjectLastSearcher(PhaseShift const& phaseShift, GameObject*& result, Check& check)
            : GameObjectSearcherBase<Check, SearcherLastObjectResult<GameObject*>>(phaseShift, result, check) { }

        GameObjectLastSearcher(WorldObject const* searcher, GameObject*& result, Check& check)
            : GameObjectLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct GameObjectListSearcher : GameObjectSearcherBase<Check, SearcherContainerResult<GameObject*>>
    {
        template<typename Container>
        GameObjectListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : GameObjectSearcherBase<Check, SearcherContainerResult<GameObject*>>(phaseShift, container, check) { }

        template<typename Container>
        GameObjectListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : GameObjectListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Check, typename Container>
    GameObjectListSearcher(PhaseShift const&, Container&, Check const&) -> GameObjectListSearcher<Check const>;

    template<class Check, typename Container>
    GameObjectListSearcher(WorldObject const*, Container&, Check const&) -> GameObjectListSearcher<Check const>;

    template<class Work>
    struct GameObjectWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_GAMEOBJECT>>
    {
        GameObjectWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_GAMEOBJECT>>(phaseShift, work) { }

        GameObjectWorker(WorldObject const* searcher, Work& work)
            : GameObjectWorker(searcher->GetPhaseShift(), work) { }
    };

    // Unit searchers

    template<class Check, class Result>
    struct UnitSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE | GRID_MAP_TYPE_MASK_PLAYER>>
    {
        template<typename Container>
        UnitSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE | GRID_MAP_TYPE_MASK_PLAYER>>(phaseShift, result, check) { }
    };

    // First accepted by Check Unit if any
    template<class Check>
    struct UnitSearcher : UnitSearcherBase<Check, SearcherFirstObjectResult<Unit*>>
    {
        UnitSearcher(PhaseShift const& phaseShift, Unit*& result, Check& check)
            : UnitSearcherBase<Check, SearcherFirstObjectResult<Unit*>>(phaseShift, result, check) { }

        UnitSearcher(WorldObject const* searcher, Unit*& result, Check& check)
            : UnitSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    // Last accepted by Check Unit if any (Check can change requirements at each call)
    template<class Check>
    struct UnitLastSearcher : UnitSearcherBase<Check, SearcherLastObjectResult<Unit*>>
    {
        UnitLastSearcher(PhaseShift const& phaseShift, Unit*& result, Check& check)
            : UnitSearcherBase<Check, SearcherLastObjectResult<Unit*>>(phaseShift, result, check) { }

        UnitLastSearcher(WorldObject const* searcher, Unit*& result, Check& check)
            : UnitLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    // All accepted by Check units if any
    template<class Check>
    struct UnitListSearcher : UnitSearcherBase<Check, SearcherContainerResult<Unit*>>
    {
        template<typename Container>
        UnitListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : UnitSearcherBase<Check, SearcherContainerResult<Unit*>>(phaseShift, container, check) { }

        template<typename Container>
        UnitListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : UnitListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct UnitWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE | GRID_MAP_TYPE_MASK_PLAYER>>
    {
        UnitWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE | GRID_MAP_TYPE_MASK_PLAYER>>(phaseShift, work) { }

        UnitWorker(WorldObject const* searcher, Work& work)
            : UnitWorker(searcher->GetPhaseShift(), work) { }
    };

    // Creature searchers

    template<class Check, class Result>
    struct CreatureSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE>>
    {
        template<typename Container>
        CreatureSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct CreatureSearcher : CreatureSearcherBase<Check, SearcherFirstObjectResult<Creature*>>
    {
        CreatureSearcher(PhaseShift const& phaseShift, Creature*& result, Check& check)
            : CreatureSearcherBase<Check, SearcherFirstObjectResult<Creature*>>(phaseShift, result, check) { }

        CreatureSearcher(WorldObject const* searcher, Creature*& result, Check& check)
            : CreatureSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    // Last accepted by Check Creature if any (Check can change requirements at each call)
    template<class Check>
    struct CreatureLastSearcher : CreatureSearcherBase<Check, SearcherLastObjectResult<Creature*>>
    {
        CreatureLastSearcher(PhaseShift const& phaseShift, Creature*& result, Check& check)
            : CreatureSearcherBase<Check, SearcherLastObjectResult<Creature*>>(phaseShift, result, check) { }

        CreatureLastSearcher(WorldObject const* searcher, Creature*& result, Check& check)
            : CreatureLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct CreatureListSearcher : CreatureSearcherBase<Check, SearcherContainerResult<Creature*>>
    {
        template<typename Container>
        CreatureListSearcher(PhaseShift const& phaseShift, Container& container, Check & check)
            : CreatureSearcherBase<Check, SearcherContainerResult<Creature*>>(phaseShift, container, check) { }

        template<typename Container>
        CreatureListSearcher(WorldObject const* searcher, Container& container, Check & check)
            : CreatureListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct CreatureWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE>>
    {
        CreatureWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CREATURE>>(phaseShift, work) { }

        CreatureWorker(WorldObject const* searcher, Work& work)
            : CreatureWorker(searcher->GetPhaseShift(), work) { }
    };

    // Player searchers

    template<class Check, class Result>
    struct PlayerSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_PLAYER>>
    {
        template<typename Container>
        PlayerSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_PLAYER>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct PlayerSearcher : PlayerSearcherBase<Check, SearcherFirstObjectResult<Player*>>
    {
        PlayerSearcher(PhaseShift const& phaseShift, Player*& result, Check& check)
            : PlayerSearcherBase<Check, SearcherFirstObjectResult<Player*>>(phaseShift, result, check) { }

        PlayerSearcher(WorldObject const* searcher, Player*& result, Check& check)
            : PlayerSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct PlayerLastSearcher : PlayerSearcherBase<Check, SearcherLastObjectResult<Player*>>
    {
        PlayerLastSearcher(PhaseShift const& phaseShift, Player*& result, Check& check)
            : PlayerSearcherBase<Check, SearcherLastObjectResult<Player*>>(phaseShift, result, check) { }

        PlayerLastSearcher(WorldObject const* searcher, Player*& result, Check& check)
            : PlayerLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct PlayerListSearcher : PlayerSearcherBase<Check, SearcherContainerResult<Player*>>
    {
        template<typename Container>
        PlayerListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : PlayerSearcherBase<Check, SearcherContainerResult<Player*>>(phaseShift, container, check) { }

        template<typename Container>
        PlayerListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : PlayerListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct PlayerWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_PLAYER>>
    {
        PlayerWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_PLAYER>>(phaseShift, work) { }

        PlayerWorker(WorldObject const* searcher, Work& work)
            : PlayerWorker(searcher->GetPhaseShift(), work) { }
    };

    template<class Work>
    struct PlayerDistWorker
    {
        WorldObject const* i_searcher;
        float i_dist;
        Work& i_work;

        PlayerDistWorker(WorldObject const* searcher, float _dist, Work& _do)
            : i_searcher(searcher), i_dist(_dist), i_work(_do) { }

        void Visit(PlayerMapType const& m) const
        {
            for (GridReference<Player> const& ref : m)
                if (ref.GetSource()->InSamePhase(i_searcher) && ref.GetSource()->IsWithinDist(i_searcher, i_dist))
                    i_work(ref.GetSource());
        }

        template<class NOT_INTERESTED> void Visit(GridRefManager<NOT_INTERESTED> const&) const { }
    };

    // AreaTrigger searchers
    template<class Check, class Result>
    struct AreaTriggerSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_AREATRIGGER>>
    {
        template<typename Container>
        AreaTriggerSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_AREATRIGGER>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct AreaTriggerSearcher : AreaTriggerSearcherBase<Check, SearcherFirstObjectResult<AreaTrigger*>>
    {
        AreaTriggerSearcher(PhaseShift const& phaseShift, AreaTrigger*& result, Check& check)
            : AreaTriggerSearcherBase<Check, SearcherFirstObjectResult<AreaTrigger*>>(phaseShift, result, check) { }

        AreaTriggerSearcher(WorldObject const* searcher, AreaTrigger*& result, Check& check)
            : AreaTriggerSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct AreaTriggerLastSearcher : AreaTriggerSearcherBase<Check, SearcherLastObjectResult<AreaTrigger*>>
    {
        AreaTriggerLastSearcher(PhaseShift const& phaseShift, AreaTrigger*& result, Check& check)
            : AreaTriggerSearcherBase<Check, SearcherLastObjectResult<AreaTrigger*>>(phaseShift, result, check) { }

        AreaTriggerLastSearcher(WorldObject const* searcher, AreaTrigger*& result, Check& check)
            : AreaTriggerLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct AreaTriggerListSearcher : AreaTriggerSearcherBase<Check, SearcherContainerResult<AreaTrigger*>>
    {
        template<typename Container>
        AreaTriggerListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : AreaTriggerSearcherBase<Check, SearcherContainerResult<AreaTrigger*>>(phaseShift, container, check) { }

        template<typename Container>
        AreaTriggerListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : AreaTriggerListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct AreaTriggerWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_AREATRIGGER>>
    {
        AreaTriggerWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_AREATRIGGER>>(phaseShift, work) { }

        AreaTriggerWorker(WorldObject const* searcher, Work& work)
            : AreaTriggerWorker(searcher->GetPhaseShift(), work) { }
    };

    // SceneObject searchers
    template<class Check, class Result>
    struct SceneObjectSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_SCENEOBJECT>>
    {
        template<typename Container>
        SceneObjectSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_SCENEOBJECT>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct SceneObjectSearcher : SceneObjectSearcherBase<Check, SearcherFirstObjectResult<SceneObject*>>
    {
        SceneObjectSearcher(PhaseShift const& phaseShift, SceneObject*& result, Check& check)
            : SceneObjectSearcherBase<Check, SearcherFirstObjectResult<SceneObject*>>(phaseShift, result, check) { }

        SceneObjectSearcher(WorldObject const* searcher, SceneObject*& result, Check& check)
            : SceneObjectSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct SceneObjectLastSearcher : SceneObjectSearcherBase<Check, SearcherLastObjectResult<SceneObject*>>
    {
        SceneObjectLastSearcher(PhaseShift const& phaseShift, SceneObject*& result, Check& check)
            : SceneObjectSearcherBase<Check, SearcherLastObjectResult<SceneObject*>>(phaseShift, result, check) { }

        SceneObjectLastSearcher(WorldObject const* searcher, SceneObject*& result, Check& check)
            : SceneObjectLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct SceneObjectListSearcher : SceneObjectSearcherBase<Check, SearcherContainerResult<SceneObject*>>
    {
        template<typename Container>
        SceneObjectListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : SceneObjectSearcherBase<Check, SearcherContainerResult<SceneObject*>>(phaseShift, container, check) { }

        template<typename Container>
        SceneObjectListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : SceneObjectListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct SceneObjectWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_SCENEOBJECT>>
    {
        SceneObjectWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_SCENEOBJECT>>(phaseShift, work) { }

        SceneObjectWorker(WorldObject const* searcher, Work& work)
            : SceneObjectWorker(searcher->GetPhaseShift(), work) { }
    };

    // Conversation searchers
    template<class Check, class Result>
    struct ConversationSearcherBase : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CONVERSATION>>
    {
        template<typename Container>
        ConversationSearcherBase(PhaseShift const& phaseShift, Container& result, Check& check)
            : WorldObjectSearcherBase<Check, Result, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CONVERSATION>>(phaseShift, result, check) { }
    };

    template<class Check>
    struct ConversationSearcher : ConversationSearcherBase<Check, SearcherFirstObjectResult<Conversation*>>
    {
        ConversationSearcher(PhaseShift const& phaseShift, Conversation*& result, Check& check)
            : ConversationSearcherBase<Check, SearcherFirstObjectResult<Conversation*>>(phaseShift, result, check) { }

        ConversationSearcher(WorldObject const* searcher, Conversation*& result, Check& check)
            : ConversationSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct ConversationLastSearcher : ConversationSearcherBase<Check, SearcherLastObjectResult<Conversation*>>
    {
        ConversationLastSearcher(PhaseShift const& phaseShift, Conversation*& result, Check& check)
            : ConversationSearcherBase<Check, SearcherLastObjectResult<Conversation*>>(phaseShift, result, check) { }

        ConversationLastSearcher(WorldObject const* searcher, Conversation*& result, Check& check)
            : ConversationLastSearcher(searcher->GetPhaseShift(), result, check) { }
    };

    template<class Check>
    struct ConversationListSearcher : ConversationSearcherBase<Check, SearcherContainerResult<Conversation*>>
    {
        template<typename Container>
        ConversationListSearcher(PhaseShift const& phaseShift, Container& container, Check& check)
            : ConversationSearcherBase<Check, SearcherContainerResult<Conversation*>>(phaseShift, container, check) { }

        template<typename Container>
        ConversationListSearcher(WorldObject const* searcher, Container& container, Check& check)
            : ConversationListSearcher(searcher->GetPhaseShift(), container, check) { }
    };

    template<class Work>
    struct ConversationWorker : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CONVERSATION>>
    {
        ConversationWorker(PhaseShift const& phaseShift, Work& work)
            : WorldObjectWorkerBase<Work, StaticGridMapTypeMaskCheck<GRID_MAP_TYPE_MASK_CONVERSATION>>(phaseShift, work) { }

        ConversationWorker(WorldObject const* searcher, Work& work)
            : ConversationWorker(searcher->GetPhaseShift(), work) { }
    };

    // CHECKS && DO classes

    // CHECK modifiers
    class InRangeCheckCustomizer
    {
    public:
        explicit InRangeCheckCustomizer(WorldObject const& obj, float range) : i_obj(obj), i_range(range) { }

        bool Test(WorldObject const* o) const
        {
            return i_obj.IsWithinDist(o, i_range);
        }

        void Update(WorldObject const* /*o*/) { }

    private:
        WorldObject const& i_obj;
        float i_range;
    };

    class NearestCheckCustomizer
    {
    public:
        explicit NearestCheckCustomizer(WorldObject const& obj, float range) : i_obj(obj), i_range(range) { }

        bool Test(WorldObject const* o) const
        {
            return i_obj.IsWithinDist(o, i_range);
        }

        void Update(WorldObject const* o)
        {
            i_range = i_obj.GetDistance(o);
        }

    private:
        WorldObject const& i_obj;
        float i_range;
    };

    // WorldObject check classes

    class TC_GAME_API AnyDeadUnitObjectInRangeCheck
    {
        public:
            AnyDeadUnitObjectInRangeCheck(WorldObject* searchObj, float range) : i_searchObj(searchObj), i_range(range) { }
            bool operator()(Player* u);
            bool operator()(Corpse* u);
            bool operator()(Creature* u);
            template<class NOT_INTERESTED> bool operator()(NOT_INTERESTED*) { return false; }
        protected:
            WorldObject const* const i_searchObj;
            float i_range;
    };

    class TC_GAME_API AnyDeadUnitSpellTargetInRangeCheck : public AnyDeadUnitObjectInRangeCheck, public WorldObjectSpellTargetCheck
    {
        public:
            AnyDeadUnitSpellTargetInRangeCheck(WorldObject* searchObj, float range, SpellInfo const* spellInfo, SpellTargetCheckTypes check, SpellTargetObjectTypes objectType)
                : AnyDeadUnitObjectInRangeCheck(searchObj, range), WorldObjectSpellTargetCheck(searchObj, searchObj, spellInfo, check, nullptr, objectType)
            { }
            bool operator()(Player* u);
            bool operator()(Corpse* u);
            bool operator()(Creature* u);
            template<class NOT_INTERESTED> bool operator()(NOT_INTERESTED*) { return false; }
    };

    // WorldObject do classes

    class RespawnDo
    {
        public:
            RespawnDo() { }
            void operator()(Creature* u) const { u->Respawn(); }
            void operator()(GameObject* u) const { u->Respawn(); }
            void operator()(WorldObject const*) const { }
    };

    // GameObject checks

    class GameObjectFocusCheck
    {
        public:
            GameObjectFocusCheck(WorldObject const* caster, uint32 focusId) : _caster(caster), _focusId(focusId) { }

            bool operator()(GameObject* go) const
            {
                if (go->GetGOInfo()->GetSpellFocusType() != _focusId)
                    return false;

                if (!go->isSpawned())
                    return false;

                float const dist = go->GetGOInfo()->GetSpellFocusRadius();
                return go->IsWithinDist(_caster, dist);
            }

        private:
            WorldObject const* _caster;
            uint32 _focusId;
    };

    // Find the nearest Fishing hole and return true only if source object is in range of hole
    class NearestGameObjectFishingHole
    {
        public:
            NearestGameObjectFishingHole(WorldObject const& obj, float range) : i_obj(obj), i_range(range) { }

            bool operator()(GameObject* go)
            {
                if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_FISHINGHOLE && go->isSpawned() && i_obj.IsWithinDist(go, i_range) && i_obj.IsWithinDist(go, (float)go->GetGOInfo()->fishingHole.radius))
                {
                    i_range = i_obj.GetDistance(go);
                    return true;
                }
                return false;
            }

        private:
            WorldObject const& i_obj;
            float i_range;

            // prevent clone
            NearestGameObjectFishingHole(NearestGameObjectFishingHole const&) = delete;
    };

    class NearestGameObjectCheck
    {
        public:
            NearestGameObjectCheck(WorldObject const& obj) : i_obj(obj), i_range(999.f) { }

            bool operator()(GameObject* go)
            {
                if (i_obj.IsWithinDist(go, i_range))
                {
                    i_range = i_obj.GetDistance(go);        // use found GO range as new range limit for next check
                    return true;
                }
                return false;
            }

        private:
            WorldObject const& i_obj;
            float i_range;

            // prevent clone this object
            NearestGameObjectCheck(NearestGameObjectCheck const&) = delete;
    };

    // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO)
    class NearestGameObjectEntryInObjectRangeCheck
    {
        public:
            NearestGameObjectEntryInObjectRangeCheck(WorldObject const& obj, uint32 entry, float range, bool spawnedOnly = true) : i_obj(obj), i_entry(entry), i_range(range), i_spawnedOnly(spawnedOnly) { }

            bool operator()(GameObject* go)
            {
                if ((!i_spawnedOnly || go->isSpawned()) && go->GetEntry() == i_entry && go->GetGUID() != i_obj.GetGUID() && i_obj.IsWithinDist(go, i_range))
                {
                    i_range = i_obj.GetDistance(go);        // use found GO range as new range limit for next check
                    return true;
                }
                return false;
            }

        private:
            WorldObject const& i_obj;
            uint32 i_entry;
            float  i_range;
            bool   i_spawnedOnly;

            // prevent clone this object
            NearestGameObjectEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&) = delete;
    };

    // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest unspawned GO)
    class NearestUnspawnedGameObjectEntryInObjectRangeCheck
    {
    public:
        NearestUnspawnedGameObjectEntryInObjectRangeCheck(WorldObject const& obj, uint32 entry, float range) : i_obj(obj), i_entry(entry), i_range(range) { }

        bool operator()(GameObject* go)
        {
            if (!go->isSpawned() && go->GetEntry() == i_entry && go->GetGUID() != i_obj.GetGUID() && i_obj.IsWithinDist(go, i_range))
            {
                i_range = i_obj.GetDistance(go);        // use found GO range as new range limit for next check
                return true;
            }
            return false;
        }

    private:
        WorldObject const& i_obj;
        uint32 i_entry;
        float  i_range;

        // prevent clone this object
        NearestUnspawnedGameObjectEntryInObjectRangeCheck(NearestUnspawnedGameObjectEntryInObjectRangeCheck const&) = delete;
    };

    // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO with a certain type)
    class NearestGameObjectTypeInObjectRangeCheck
    {
        public:
            NearestGameObjectTypeInObjectRangeCheck(WorldObject const& obj, GameobjectTypes type, float range) : i_obj(obj), i_type(type), i_range(range) { }

            bool operator()(GameObject* go)
            {
                if (go->GetGoType() == i_type && i_obj.IsWithinDist(go, i_range))
                {
                    i_range = i_obj.GetDistance(go);        // use found GO range as new range limit for next check
                    return true;
                }
                return false;
            }

        private:
            WorldObject const& i_obj;
            GameobjectTypes i_type;
            float i_range;

            // prevent clone this object
            NearestGameObjectTypeInObjectRangeCheck(NearestGameObjectTypeInObjectRangeCheck const&) = delete;
    };

    // Unit checks

    class MostHPMissingInRange
    {
        public:
            MostHPMissingInRange(Unit const* obj, float range, uint32 hp) : i_obj(obj), i_range(range), i_hp(hp) { }

            bool operator()(Unit* u)
            {
                if (u->IsAlive() && u->IsInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDist(u, i_range) && u->GetMaxHealth() - u->GetHealth() > i_hp)
                {
                    i_hp = u->GetMaxHealth() - u->GetHealth();
                    return true;
                }
                return false;
            }

        private:
            Unit const* i_obj;
            float i_range;
            uint64 i_hp;
    };

    class MostHPPercentMissingInRange
    {
    public:
        MostHPPercentMissingInRange(Unit const* obj, float range, uint32 minHpPct, uint32 maxHpPct) : i_obj(obj), i_range(range), i_minHpPct(minHpPct), i_maxHpPct(maxHpPct), i_hpPct(101.f) { }

        bool operator()(Unit* u)
        {
            if (u->IsAlive() && u->IsInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDist(u, i_range) && i_minHpPct <= u->GetHealthPct() && u->GetHealthPct() <= i_maxHpPct && u->GetHealthPct() < i_hpPct)
            {
                i_hpPct = u->GetHealthPct();
                return true;
            }
            return false;
        }

    private:
        Unit const* i_obj;
        float i_range;
        float i_minHpPct, i_maxHpPct, i_hpPct;
    };

    class FriendlyBelowHpPctEntryInRange
    {
        public:
            FriendlyBelowHpPctEntryInRange(Unit const* obj, uint32 entry, float range, uint8 pct, bool excludeSelf) : i_obj(obj), i_entry(entry), i_range(range), i_pct(pct), i_excludeSelf(excludeSelf) { }

            bool operator()(Unit* u)
            {
                if (i_excludeSelf && i_obj->GetGUID() == u->GetGUID())
                    return false;
                if (u->GetEntry() == i_entry && u->IsAlive() && u->IsInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDist(u, i_range) && u->HealthBelowPct(i_pct))
                    return true;
                return false;
            }

        private:
            Unit const* i_obj;
            uint32 i_entry;
            float i_range;
            uint8 i_pct;
            bool i_excludeSelf;
    };

    class FriendlyCCedInRange
    {
        public:
            FriendlyCCedInRange(Unit const* obj, float range) : i_obj(obj), i_range(range) { }

            bool operator()(Unit* u) const
            {
                if (u->IsAlive() && u->IsInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDist(u, i_range) &&
                    (u->IsFeared() || u->IsCharmed() || u->HasRootAura() || u->HasUnitState(UNIT_STATE_STUNNED) || u->HasUnitState(UNIT_STATE_CONFUSED)))
                {
                    return true;
                }
                return false;
            }

        private:
            Unit const* i_obj;
            float i_range;
    };

    class FriendlyMissingBuffInRange
    {
        public:
            FriendlyMissingBuffInRange(Unit const* obj, float range, uint32 spellid) : i_obj(obj), i_range(range), i_spell(spellid) { }

            bool operator()(Unit* u) const
            {
                if (u->IsAlive() && u->IsInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDist(u, i_range) && !u->HasAura(i_spell))
                    return true;

                return false;
            }

        private:
            Unit const* i_obj;
            float i_range;
            uint32 i_spell;
    };

    class AnyUnfriendlyUnitInObjectRangeCheck
    {
        public:
            AnyUnfriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) { }

            bool operator()(Unit* u) const
            {
                if (u->IsAlive() && i_obj->IsWithinDist(u, i_range) && !i_funit->IsFriendlyTo(u))
                    return true;

                return false;
            }

        private:
            WorldObject const* i_obj;
            Unit const* i_funit;
            float i_range;
    };

    class NearestAttackableNoTotemUnitInObjectRangeCheck
    {
        public:
            NearestAttackableNoTotemUnitInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) { }

            bool operator()(Unit* u)
            {
                if (!u->IsAlive())
                    return false;

                if (u->GetCreatureType() == CREATURE_TYPE_NON_COMBAT_PET)
                    return false;

                if (u->GetTypeId() == TYPEID_UNIT && u->ToCreature()->IsTotem())
                    return false;

                if (!u->isTargetableForAttack(false))
                    return false;

                if (!i_obj->IsWithinDist(u, i_range) || !i_obj->IsValidAttackTarget(u))
                    return false;

                i_range = i_obj->GetDistance(*u);
                return true;
            }

        private:
            WorldObject const* i_obj;
            float i_range;
    };

    class AnyFriendlyUnitInObjectRangeCheck
    {
        public:
            AnyFriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range, bool playerOnly = false, bool incOwnRadius = true, bool incTargetRadius = true)
                : i_obj(obj), i_funit(funit), i_range(range), i_playerOnly(playerOnly), i_incOwnRadius(incOwnRadius), i_incTargetRadius(incTargetRadius) { }

            bool operator()(Unit* u) const
            {
                if (!u->IsAlive())
                    return false;

                float searchRadius = i_range;
                if (i_incOwnRadius)
                    searchRadius += i_obj->GetCombatReach();
                if (i_incTargetRadius)
                    searchRadius += u->GetCombatReach();

                if (!u->IsInMap(i_obj) || !u->InSamePhase(i_obj) || !u->IsWithinVerticalCylinder(*i_obj, searchRadius, searchRadius, true))
                    return false;

                if (!i_funit->IsFriendlyTo(u))
                    return false;

                return !i_playerOnly || u->GetTypeId() == TYPEID_PLAYER;
            }

        private:
            WorldObject const* i_obj;
            Unit const* i_funit;
            float i_range;
            bool i_playerOnly;
            bool i_incOwnRadius;
            bool i_incTargetRadius;
    };

    class AnyGroupedUnitInObjectRangeCheck
    {
        public:
            AnyGroupedUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range, bool raid, bool playerOnly = false, bool incOwnRadius = true, bool incTargetRadius = true)
                : _source(obj), _refUnit(funit), _range(range), _raid(raid), _playerOnly(playerOnly), i_incOwnRadius(incOwnRadius), i_incTargetRadius(incTargetRadius) { }

            bool operator()(Unit* u) const
            {
                if (_playerOnly && u->GetTypeId() != TYPEID_PLAYER)
                    return false;

                if (_raid)
                {
                    if (!_refUnit->IsInRaidWith(u))
                        return false;
                }
                else if (!_refUnit->IsInPartyWith(u))
                    return false;

                if (_refUnit->IsHostileTo(u))
                    return false;

                if (!u->IsAlive())
                    return false;

                float searchRadius = _range;
                if (i_incOwnRadius)
                    searchRadius += _source->GetCombatReach();
                if (i_incTargetRadius)
                    searchRadius += u->GetCombatReach();

                return u->IsInMap(_source) && u->InSamePhase(_source) && u->IsWithinVerticalCylinder(*_source, searchRadius, searchRadius, true);
            }

        private:
            WorldObject const* _source;
            Unit const* _refUnit;
            float _range;
            bool _raid;
            bool _playerOnly;
            bool i_incOwnRadius;
            bool i_incTargetRadius;
    };

    class AnyUnitInObjectRangeCheck
    {
        public:
            AnyUnitInObjectRangeCheck(WorldObject const* obj, float range, bool check3D = true, bool reqAlive = true) : i_obj(obj), i_range(range), i_check3D(check3D), i_reqAlive(reqAlive) { }

            bool operator()(Unit* u) const
            {
                if (i_reqAlive && !u->IsAlive())
                    return false;

                if (!i_obj->IsWithinDist(u, i_range, i_check3D))
                    return false;

                return true;
            }

        private:
            WorldObject const* i_obj;
            float i_range;
            bool i_check3D;
            bool i_reqAlive;
    };

    // Success at unit in range, range update for next check (this can be use with UnitLastSearcher to find nearest unit)
    class NearestAttackableUnitInObjectRangeCheck
    {
        public:
            NearestAttackableUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) { }

            bool operator()(Unit* u)
            {
                if (u->isTargetableForAttack() && i_obj->IsWithinDist(u, i_range) &&
                    (i_funit->IsInCombatWith(u) || i_funit->IsHostileTo(u)) && i_obj->CanSeeOrDetect(u))
                {
                    i_range = i_obj->GetDistance(u);        // use found unit range as new range limit for next check
                    return true;
                }

                return false;
            }

        private:
            WorldObject const* i_obj;
            Unit const* i_funit;
            float i_range;

            // prevent clone this object
            NearestAttackableUnitInObjectRangeCheck(NearestAttackableUnitInObjectRangeCheck const&) = delete;
    };

    class AnyAoETargetUnitInObjectRangeCheck
    {
        public:
            AnyAoETargetUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range, SpellInfo const* spellInfo = nullptr, bool incOwnRadius = true, bool incTargetRadius = true)
                : i_obj(obj), i_funit(funit), _spellInfo(spellInfo), i_range(range), i_incOwnRadius(incOwnRadius), i_incTargetRadius(incTargetRadius)
            {
            }

            bool operator()(Unit* u) const
            {
                // Check contains checks for: live, uninteractible, non-attackable flags, flight check and GM check, ignore totems
                if (u->GetTypeId() == TYPEID_UNIT && u->IsTotem())
                    return false;

                if (_spellInfo)
                {
                    if (!u->IsPlayer())
                    {
                        if (_spellInfo->HasAttribute(SPELL_ATTR3_ONLY_ON_PLAYER))
                            return false;

                        if (_spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER_CONTROLLED_NPC) && u->IsControlledByPlayer())
                            return false;
                    }
                    else if (_spellInfo->HasAttribute(SPELL_ATTR5_NOT_ON_PLAYER))
                        return false;
                }

                if (!i_funit->IsValidAttackTarget(u, _spellInfo))
                    return false;

                float searchRadius = i_range;
                if (i_incOwnRadius)
                    searchRadius += i_obj->GetCombatReach();
                if (i_incTargetRadius)
                    searchRadius += u->GetCombatReach();

                return u->IsInMap(i_obj) && u->InSamePhase(i_obj) && u->IsWithinVerticalCylinder(*i_obj, searchRadius, searchRadius, true);
            }

        private:
            WorldObject const* i_obj;
            Unit const* i_funit;
            SpellInfo const* _spellInfo;
            float i_range;
            bool i_incOwnRadius;
            bool i_incTargetRadius;
    };

    // do attack at call of help to friendly crearture
    class CallOfHelpCreatureInRangeDo
    {
        public:
            CallOfHelpCreatureInRangeDo(Unit* funit, Unit* enemy, float range)
                : i_funit(funit), i_enemy(enemy), i_range(range) { }

            void operator()(Creature* u) const
            {
                if (u == i_funit)
                    return;

                if (!u->CanAssistTo(i_funit, i_enemy, false))
                    return;

                // too far
                // Don't use combat reach distance, range must be an absolute value, otherwise the chain aggro range will be too big
                if (!u->IsWithinDist(i_funit, i_range, true, false, false))
                    return;

                // only if see assisted creature's enemy
                if (!u->IsWithinLOSInMap(i_enemy))
                    return;

                u->EngageWithTarget(i_enemy);
            }
        private:
            Unit* const i_funit;
            Unit* const i_enemy;
            float i_range;
    };

    struct AnyDeadUnitCheck
    {
        bool operator()(Unit* u) const
        {
            return !u->IsAlive();
        }
    };

    // Creature checks

    class NearestHostileUnitCheck
    {
        public:
            explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0.f, bool playerOnly = false) : me(creature), i_playerOnly(playerOnly)
            {
                m_range = (dist == 0.f ? 9999.f : dist);
            }

            bool operator()(Unit* u)
            {
                if (!me->IsWithinDist(u, m_range))
                    return false;

                if (!me->IsValidAttackTarget(u))
                    return false;

                if (i_playerOnly && u->GetTypeId() != TYPEID_PLAYER)
                    return false;

                m_range = me->GetDistance(u);   // use found unit range as new range limit for next check
                return true;
            }

        private:
            Creature const* me;
            float m_range;
            bool i_playerOnly;
            NearestHostileUnitCheck(NearestHostileUnitCheck const&) = delete;
    };

    class NearestHostileUnitInAttackDistanceCheck
    {
        public:
            explicit NearestHostileUnitInAttackDistanceCheck(Creature const* creature, float dist = 0.f) : me(creature)
            {
                m_range = (dist == 0.f ? 9999.f : dist);
                m_force = (dist == 0.f ? false : true);
            }

            bool operator()(Unit* u)
            {
                if (!me->IsWithinDist(u, m_range))
                    return false;

                if (!me->CanSeeOrDetect(u))
                    return false;

                if (m_force)
                {
                    if (!me->IsValidAttackTarget(u))
                        return false;
                }
                else if (!me->CanStartAttack(u, false))
                    return false;

                m_range = me->GetDistance(u);   // use found unit range as new range limit for next check
                return true;
            }

        private:
            Creature const* me;
            float m_range;
            bool m_force;
            NearestHostileUnitInAttackDistanceCheck(NearestHostileUnitInAttackDistanceCheck const&) = delete;
    };

    class NearestHostileUnitInAggroRangeCheck
    {
        public:
            explicit NearestHostileUnitInAggroRangeCheck(Creature const* creature, bool useLOS = false, bool ignoreCivilians = false) : _me(creature), _useLOS(useLOS), _ignoreCivilians(ignoreCivilians) { }

            bool operator()(Unit* u) const
            {
                if (!u->IsHostileTo(_me))
                    return false;

                if (!u->IsWithinDist(_me, _me->GetAggroRange(u)))
                    return false;

                if (!_me->IsValidAttackTarget(u))
                    return false;

                if (_useLOS && !u->IsWithinLOSInMap(_me))
                    return false;

                // pets in aggressive do not attack civilians
                if (_ignoreCivilians)
                    if (Creature* c = u->ToCreature())
                        if (c->IsCivilian())
                            return false;

                return true;
            }

        private:
            Creature const* _me;
            bool _useLOS;
            bool _ignoreCivilians;
            NearestHostileUnitInAggroRangeCheck(NearestHostileUnitInAggroRangeCheck const&) = delete;
    };

    class AnyAssistCreatureInRangeCheck
    {
        public:
            AnyAssistCreatureInRangeCheck(Unit* funit, Unit* enemy, float range)
                : i_funit(funit), i_enemy(enemy), i_range(range) { }

            bool operator()(Creature* u) const
            {
                if (u == i_funit)
                    return false;

                if (!u->CanAssistTo(i_funit, i_enemy))
                    return false;

                // too far
                // Don't use combat reach distance, range must be an absolute value, otherwise the chain aggro range will be too big
                if (!i_funit->IsWithinDist(u, i_range, true, false, false))
                    return false;

                // only if see assisted creature
                if (!i_funit->IsWithinLOSInMap(u))
                    return false;

                return true;
            }

        private:
            Unit* const i_funit;
            Unit* const i_enemy;
            float i_range;
    };

    class NearestAssistCreatureInCreatureRangeCheck
    {
        public:
            NearestAssistCreatureInCreatureRangeCheck(Creature* obj, Unit* enemy, float range)
                : i_obj(obj), i_enemy(enemy), i_range(range) { }

            bool operator()(Creature* u)
            {
                if (u == i_obj)
                    return false;
                if (!u->CanAssistTo(i_obj, i_enemy))
                    return false;

                // Don't use combat reach distance, range must be an absolute value, otherwise the chain aggro range will be too big
                if (!i_obj->IsWithinDist(u, i_range, true, false, false))
                    return false;

                if (!i_obj->IsWithinLOSInMap(u))
                    return false;

                i_range = i_obj->GetDistance(u);            // use found unit range as new range limit for next check
                return true;
            }

        private:
            Creature* const i_obj;
            Unit* const i_enemy;
            float i_range;

            // prevent clone this object
            NearestAssistCreatureInCreatureRangeCheck(NearestAssistCreatureInCreatureRangeCheck const&) = delete;
    };

    // Success at unit in range, range update for next check (this can be use with CreatureLastSearcher to find nearest creature)
    class NearestCreatureEntryWithLiveStateInObjectRangeCheck
    {
        public:
            NearestCreatureEntryWithLiveStateInObjectRangeCheck(WorldObject const& obj, uint32 entry, bool alive, float range)
                : i_obj(obj), i_entry(entry), i_alive(alive), i_range(range) { }

            bool operator()(Creature* u)
            {
                if (u->getDeathState() != DEAD
                    && u->GetEntry() == i_entry
                    && u->IsAlive() == i_alive
                    && u->GetGUID() != i_obj.GetGUID()
                    && i_obj.IsWithinDist(u, i_range)
                    && u->CheckPrivateObjectOwnerVisibility(&i_obj))
                {
                    i_range = i_obj.GetDistance(u);         // use found unit range as new range limit for next check
                    return true;
                }
                return false;
            }

        private:
            WorldObject const& i_obj;
            uint32 i_entry;
            bool   i_alive;
            float  i_range;

            // prevent clone this object
            NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&) = delete;
    };

    template <typename Customizer = InRangeCheckCustomizer>
    class CreatureWithOptionsInObjectRangeCheck
    {
        public:
            CreatureWithOptionsInObjectRangeCheck(WorldObject const& obj, Customizer& customizer, FindCreatureOptions const& args)
                : i_obj(obj), i_args(args), i_customizer(customizer) { }

            bool operator()(Creature const* u) const
            {
                if (u->getDeathState() == DEAD) // Despawned
                    return false;

                if (u->GetGUID() == i_obj.GetGUID())
                    return false;

                if (!i_customizer.Test(u))
                    return false;

                if (i_args.CreatureId && u->GetEntry() != i_args.CreatureId)
                    return false;

                if (i_args.StringId && !u->HasStringId(*i_args.StringId))
                    return false;

                if (i_args.IsAlive.has_value())
                {
                    switch (*i_args.IsAlive)
                    {
                        case FindCreatureAliveState::Alive:
                        {
                            if (!u->IsAlive())
                                return false;
                            break;
                        }
                        case FindCreatureAliveState::Dead:
                        {
                            if (u->IsAlive())
                                return false;
                            break;
                        }
                        case FindCreatureAliveState::EffectivelyAlive:
                        {
                            if (!u->IsAlive() || u->HasUnitFlag2(UNIT_FLAG2_FEIGN_DEATH))
                                return false;
                            break;
                        }
                        case FindCreatureAliveState::EffectivelyDead:
                        {
                            if (u->IsAlive() && !u->HasUnitFlag2(UNIT_FLAG2_FEIGN_DEATH))
                                return false;
                            break;
                        }
                        default:
                            break;
                    }
                }

                if (i_args.IsSummon.has_value() && u->IsSummon() != i_args.IsSummon)
                    return false;

                if (i_args.IsInCombat.has_value() && u->IsInCombat() != i_args.IsInCombat)
                    return false;

                if ((i_args.OwnerGuid && u->GetOwnerGUID() != i_args.OwnerGuid)
                    || (i_args.CharmerGuid && u->GetCharmerGUID() != i_args.CharmerGuid)
                    || (i_args.CreatorGuid && u->GetCreatorGUID() != i_args.CreatorGuid)
                    || (i_args.DemonCreatorGuid && u->GetDemonCreatorGUID() != i_args.DemonCreatorGuid)
                    || (i_args.PrivateObjectOwnerGuid && u->GetPrivateObjectOwner() != i_args.PrivateObjectOwnerGuid))
                    return false;

                if (i_args.IgnorePrivateObjects && u->IsPrivateObject())
                    return false;

                if (i_args.IgnoreNotOwnedPrivateObjects && !u->CheckPrivateObjectOwnerVisibility(&i_obj))
                    return false;

                if (i_args.AuraSpellId && !u->HasAura(*i_args.AuraSpellId))
                    return false;

                i_customizer.Update(u);
                return true;
            }

        private:
            WorldObject const& i_obj;
            FindCreatureOptions const& i_args;
            Customizer& i_customizer;
    };

    template <typename Customizer = InRangeCheckCustomizer>
    class GameObjectWithOptionsInObjectRangeCheck
    {
    public:
        GameObjectWithOptionsInObjectRangeCheck(WorldObject const& obj, Customizer& customizer, FindGameObjectOptions const& args)
            : i_obj(obj), i_args(args), i_customizer(customizer) { }

        bool operator()(GameObject const* go) const
        {
            if (i_args.IsSpawned.has_value() && i_args.IsSpawned != go->isSpawned()) // Despawned
                return false;

            if (go->GetGUID() == i_obj.GetGUID())
                return false;

            if (!i_customizer.Test(go))
                return false;

            if (i_args.GameObjectId && go->GetEntry() != i_args.GameObjectId)
                return false;

            if (i_args.StringId && !go->HasStringId(*i_args.StringId))
                return false;

            if (i_args.IsSummon.has_value() && (go->GetSpawnId() == 0) != i_args.IsSummon)
                return false;

            if ((i_args.OwnerGuid && go->GetOwnerGUID() != i_args.OwnerGuid)
                || (i_args.PrivateObjectOwnerGuid && go->GetPrivateObjectOwner() != i_args.PrivateObjectOwnerGuid))
                return false;

            if (i_args.IgnorePrivateObjects && go->IsPrivateObject())
                return false;

            if (i_args.IgnoreNotOwnedPrivateObjects && !go->CheckPrivateObjectOwnerVisibility(&i_obj))
                return false;

            if (i_args.GameObjectType && go->GetGoType() != i_args.GameObjectType)
                return false;

            i_customizer.Update(go);
            return true;
        }

    private:
        WorldObject const& i_obj;
        FindGameObjectOptions const& i_args;
        Customizer& i_customizer;
    };

    class AnyPlayerInPositionRangeCheck
    {
    public:
        AnyPlayerInPositionRangeCheck(Position const* pos, float range, bool reqAlive = true) : _pos(pos), _range(range), _reqAlive(reqAlive) { }
        bool operator()(Player* u)
        {
            if (_reqAlive && !u->IsAlive())
                return false;

            if (!u->IsWithinDist3d(_pos, _range))
                return false;

            return true;
        }

    private:
        Position const* _pos;
        float _range;
        bool _reqAlive;
    };

    class NearestPlayerInObjectRangeCheck
    {
        public:
            NearestPlayerInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) { }

            bool operator()(Player* u)
            {
                if (u->IsAlive() && i_obj->IsWithinDist(u, i_range))
                {
                    i_range = i_obj->GetDistance(u);
                    return true;
                }

                return false;
            }
        private:
            WorldObject const* i_obj;
            float i_range;

            NearestPlayerInObjectRangeCheck(NearestPlayerInObjectRangeCheck const&) = delete;
    };

    class AllFriendlyCreaturesInGrid
    {
        public:
            AllFriendlyCreaturesInGrid(Unit const* obj) : unit(obj) { }

            bool operator()(Unit* u) const
            {
                if (u->IsAlive() && u->IsVisible() && u->IsFriendlyTo(unit))
                    return true;

                return false;
            }

        private:
            Unit const* unit;
    };

    class AllGameObjectsWithEntryInRange
    {
        public:
            AllGameObjectsWithEntryInRange(WorldObject const* object, uint32 entry, float maxRange) : m_pObject(object), m_uiEntry(entry), m_fRange(maxRange) { }

            bool operator()(GameObject* go) const
            {
                if ((!m_uiEntry || go->GetEntry() == m_uiEntry) && m_pObject->IsWithinDist(go, m_fRange, false))
                    return true;

                return false;
            }

        private:
            WorldObject const* m_pObject;
            uint32 m_uiEntry;
            float m_fRange;
    };

    class AllCreaturesOfEntryInRange
    {
        public:
            AllCreaturesOfEntryInRange(WorldObject const* object, uint32 entry, float maxRange = 0.0f) : m_pObject(object), m_uiEntry(entry), m_fRange(maxRange) { }

            bool operator()(Unit* unit) const
            {
                if (m_uiEntry)
                {
                    if (unit->GetEntry() != m_uiEntry)
                        return false;
                }

                if (m_fRange)
                {
                    if (m_fRange > 0.0f && !m_pObject->IsWithinDist(unit, m_fRange, false))
                        return false;
                    if (m_fRange < 0.0f && m_pObject->IsWithinDist(unit, m_fRange, false))
                        return false;
                }

                return true;
            }

        private:
            WorldObject const* m_pObject;
            uint32 m_uiEntry;
            float m_fRange;
    };

    class PlayerAtMinimumRangeAway
    {
        public:
            PlayerAtMinimumRangeAway(Unit const* unit, float fMinRange) : unit(unit), fRange(fMinRange) { }

            bool operator()(Player* player) const
            {
                //No threat list check, must be done explicit if expected to be in combat with creature
                if (!player->IsGameMaster() && player->IsAlive() && !unit->IsWithinDist(player, fRange, false))
                    return true;

                return false;
            }

        private:
            Unit const* unit;
            float fRange;
    };

    class GameObjectInRangeCheck
    {
        public:
            GameObjectInRangeCheck(float _x, float _y, float _z, float _range, uint32 _entry = 0) :
              x(_x), y(_y), z(_z), range(_range), entry(_entry) { }

            bool operator()(GameObject* go) const
            {
                if (!entry || (go->GetGOInfo() && go->GetGOInfo()->entry == entry))
                    return go->IsInRange(x, y, z, range);
                else return false;
            }

        private:
            float x, y, z, range;
            uint32 entry;
    };

    class AllWorldObjectsInRange
    {
        public:
            AllWorldObjectsInRange(WorldObject const* object, float maxRange) : m_pObject(object), m_fRange(maxRange) { }

            bool operator()(WorldObject* go) const
            {
                return m_pObject->IsWithinDist(go, m_fRange, false) && m_pObject->InSamePhase(go);
            }

        private:
            WorldObject const* m_pObject;
            float m_fRange;
    };

    class ObjectTypeIdCheck
    {
        public:
            ObjectTypeIdCheck(TypeID typeId, bool equals) : _typeId(typeId), _equals(equals) { }

            bool operator()(WorldObject* object) const
            {
                return (object->GetTypeId() == _typeId) == _equals;
            }

        private:
            TypeID _typeId;
            bool _equals;
    };

    class ObjectGUIDCheck
    {
        public:
            ObjectGUIDCheck(ObjectGuid GUID) : _GUID(GUID) { }

            bool operator()(WorldObject* object) const
            {
                return object->GetGUID() == _GUID;
            }

        private:
            ObjectGuid _GUID;
    };

    class HeightDifferenceCheck
    {
    public:
        HeightDifferenceCheck(WorldObject* go, float diff, bool reverse)
            : _baseObject(go), _difference(diff), _reverse(reverse)
        {
        }

        bool operator()(WorldObject* unit) const
        {
            return (unit->GetPositionZ() - _baseObject->GetPositionZ() > _difference) != _reverse;
        }

    private:
        WorldObject* _baseObject;
        float _difference;
        bool _reverse;
    };

    class UnitAuraCheck
    {
        public:
            UnitAuraCheck(bool present, uint32 spellId, ObjectGuid casterGUID = ObjectGuid::Empty) : _present(present), _spellId(spellId), _casterGUID(casterGUID) { }

            bool operator()(Unit* unit) const
            {
                return unit->HasAura(_spellId, _casterGUID) == _present;
            }

            bool operator()(WorldObject* object) const
            {
                return object->ToUnit() && object->ToUnit()->HasAura(_spellId, _casterGUID) == _present;
            }

        private:
            bool _present;
            uint32 _spellId;
            ObjectGuid _casterGUID;
    };

    class ObjectEntryAndPrivateOwnerIfExistsCheck
    {
    public:
        ObjectEntryAndPrivateOwnerIfExistsCheck(ObjectGuid ownerGUID, uint32 entry) : _ownerGUID(ownerGUID), _entry(entry) { }

        bool operator()(WorldObject* object) const
        {
            return object->GetEntry() == _entry && (!object->IsPrivateObject() || object->GetPrivateObjectOwner() == _ownerGUID);
        }

    private:
        ObjectGuid _ownerGUID;
        uint32 _entry;
    };

    class NearestAreaTriggerEntryInObjectRangeCheck
    {
    public:
        NearestAreaTriggerEntryInObjectRangeCheck(WorldObject const& obj, uint32 entry, float range, bool spawnedOnly = false) : i_obj(obj), i_entry(entry), i_range(range), i_spawnedOnly(spawnedOnly) { }

        bool operator()(AreaTrigger const* at)
        {
            if ((!i_spawnedOnly || at->IsStaticSpawn()) && at->GetEntry() == i_entry && at->GetGUID() != i_obj.GetGUID() && i_obj.IsWithinDist(at, i_range))
            {
                i_range = i_obj.GetDistance(at);
                return true;
            }
            return false;
        }

    private:
        WorldObject const& i_obj;
        uint32 i_entry;
        float  i_range;
        bool   i_spawnedOnly;

        // prevent clone this object
        NearestAreaTriggerEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&) = delete;
    };

    // Player checks and do

    // Prepare using Builder localized packets with caching and send to player
    template<typename Localizer>
    class LocalizedDo
    {
        using LocalizedAction = std::remove_pointer_t<decltype(std::declval<Localizer>()(LocaleConstant{}))>;

    public:
        explicit LocalizedDo(Localizer& localizer) : _localizer(localizer) { }

        void operator()(Player const* p);

    private:
        Localizer& _localizer;
        std::vector<std::unique_ptr<LocalizedAction>> _localizedCache;         // 0 = default, i => i-1 locale index
    };
}
#endif