aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Scripting/ScriptMgr.h
blob: 31a5842a383d3454dc409ff3322c263f07fc79de (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
/*
 * 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 SC_SCRIPTMGR_H
#define SC_SCRIPTMGR_H

#include "Common.h"
#include "ObjectGuid.h"
#include "Tuples.h"
#include "Types.h"
#include <boost/preprocessor/punctuation/remove_parens.hpp>
#include <memory>
#include <vector>

class AccountMgr;
class AreaTrigger;
class AreaTriggerAI;
class AuctionHouseObject;
class Aura;
class AuraScript;
class Battlefield;
class Battleground;
class BattlegroundMap;
class BattlegroundScript;
class Channel;
class Conversation;
class Creature;
class CreatureAI;
class DynamicObject;
class GameObject;
class GameObjectAI;
class Guild;
class Group;
class InstanceMap;
class InstanceScript;
class Item;
class Map;
class ModuleReference;
class OutdoorPvP;
class Player;
class Quest;
class ScriptMgr;
class Spell;
class SpellInfo;
class SpellScript;
class SpellCastTargets;
class Transport;
class Unit;
class Vehicle;
class Weather;
class WorldPacket;
class WorldSocket;
class WorldObject;
class WorldSession;

struct AchievementEntry;
struct AreaTriggerEntry;
struct AuctionPosting;
struct ConditionSourceInfo;
struct Condition;
struct CreatureTemplate;
struct CreatureData;
struct ItemTemplate;
struct MapEntry;
struct Position;
struct QuestObjective;
struct SceneTemplate;
struct WorldStateTemplate;

namespace Trinity::ChatCommands { struct ChatCommandBuilder; }

enum BattlegroundTypeId : uint32;
enum Difficulty : uint8;
enum DuelCompleteType : uint8;
enum Emote : uint32;
enum QuestStatus : uint8;
enum RemoveMethod : uint8;
enum ShutdownExitCode : uint32;
enum ShutdownMask : uint32;
enum SpellEffIndex : uint8;
enum WeatherState : uint32;
enum XPColorChar : uint8;

#define VISIBLE_RANGE       166.0f                          //MAX visible range (size of grid)

/*
    @todo Add more script type classes.

    MailScript
    SessionScript
    CollisionScript
    ArenaTeamScript

*/

/*
    Standard procedure when adding new script type classes:

    First of all, define the actual class, and have it inherit from ScriptObject, like so:

    class MyScriptType : public ScriptObject
    {
        uint32 _someId;

        private:

            void RegisterSelf();

        protected:

            MyScriptType(char const* name, uint32 someId)
                : ScriptObject(name), _someId(someId)
            {
                ScriptRegistry<MyScriptType>::AddScript(this);
            }

        public:

            // If a virtual function in your script type class is not necessarily
            // required to be overridden, just declare it virtual with an empty
            // body. If, on the other hand, it's logical only to override it (i.e.
            // if it's the only method in the class), make it pure virtual, by adding
            // = 0 to it.
            virtual void OnSomeEvent(uint32 someArg1, std::string& someArg2) { }

            // This is a pure virtual function:
            virtual void OnAnotherEvent(uint32 someArg) = 0;
    }

    Next, you need to add a specialization for ScriptRegistry. Put this in the bottom of
    ScriptMgr.cpp:

    template class ScriptRegistry<MyScriptType>;

    Now, add a cleanup routine in ScriptMgr::~ScriptMgr:

    SCR_CLEAR(MyScriptType);

    Now your script type is good to go with the script system. What you need to do now
    is add functions to ScriptMgr that can be called from the core to actually trigger
    certain events. For example, in ScriptMgr.h:

    void OnSomeEvent(uint32 someArg1, std::string& someArg2);
    void OnAnotherEvent(uint32 someArg);

    In ScriptMgr.cpp:

    void ScriptMgr::OnSomeEvent(uint32 someArg1, std::string& someArg2)
    {
        FOREACH_SCRIPT(MyScriptType)->OnSomeEvent(someArg1, someArg2);
    }

    void ScriptMgr::OnAnotherEvent(uint32 someArg)
    {
        FOREACH_SCRIPT(MyScriptType)->OnAnotherEvent(someArg1, someArg2);
    }

    Now you simply call these two functions from anywhere in the core to trigger the
    event on all registered scripts of that type.
*/

class TC_GAME_API ScriptObject
{
    friend class ScriptMgr;

    public:

        ScriptObject(ScriptObject const& right) = delete;
        ScriptObject(ScriptObject&& right) = delete;
        ScriptObject& operator=(ScriptObject const& right) = delete;
        ScriptObject& operator=(ScriptObject&& right) = delete;

        std::string const& GetName() const;

    protected:

        ScriptObject(char const* name);
        virtual ~ScriptObject();

    private:

        std::string const _name;
};

class TC_GAME_API SpellScriptLoader : public ScriptObject
{
    protected:

        explicit SpellScriptLoader(char const* name);

    public:

        // Should return a fully valid SpellScript pointer.
        virtual SpellScript* GetSpellScript() const;

        // Should return a fully valid AuraScript pointer.
        virtual AuraScript* GetAuraScript() const;
};

class TC_GAME_API ServerScript : public ScriptObject
{
    protected:

        explicit ServerScript(char const* name);

    public:

        ~ServerScript();

        // Called when reactive socket I/O is started (WorldTcpSessionMgr).
        virtual void OnNetworkStart();

        // Called when reactive I/O is stopped.
        virtual void OnNetworkStop();

        // Called when a remote socket establishes a connection to the server. Do not store the socket object.
        virtual void OnSocketOpen(std::shared_ptr<WorldSocket> socket);

        // Called when a socket is closed. Do not store the socket object, and do not rely on the connection
        // being open; it is not.
        virtual void OnSocketClose(std::shared_ptr<WorldSocket> socket);

        // Called when a packet is sent to a client. The packet object is a copy of the original packet, so reading
        // and modifying it is safe.
        virtual void OnPacketSend(WorldSession* session, WorldPacket& packet);

        // Called when a (valid) packet is received by a client. The packet object is a copy of the original packet, so
        // reading and modifying it is safe. Make sure to check WorldSession pointer before usage, it might be null in case of auth packets
        virtual void OnPacketReceive(WorldSession* session, WorldPacket& packet);
};

class TC_GAME_API WorldScript : public ScriptObject
{
    protected:

        explicit WorldScript(char const* name);

    public:

        ~WorldScript();

        // Called when the open/closed state of the world changes.
        virtual void OnOpenStateChange(bool open);

        // Called after the world configuration is (re)loaded.
        virtual void OnConfigLoad(bool reload);

        // Called before the message of the day is changed.
        virtual void OnMotdChange(std::string& newMotd);

        // Called when a world shutdown is initiated.
        virtual void OnShutdownInitiate(ShutdownExitCode code, ShutdownMask mask);

        // Called when a world shutdown is cancelled.
        virtual void OnShutdownCancel();

        // Called on every world tick (don't execute too heavy code here).
        virtual void OnUpdate(uint32 diff);

        // Called when the world is started.
        virtual void OnStartup();

        // Called when the world is actually shut down.
        virtual void OnShutdown();
};

class TC_GAME_API FormulaScript : public ScriptObject
{
    protected:

        explicit FormulaScript(char const* name);

    public:

        ~FormulaScript();

        // Called after calculating honor.
        virtual void OnHonorCalculation(float& honor, uint8 level, float multiplier);

        // Called after gray level calculation.
        virtual void OnGrayLevelCalculation(uint8& grayLevel, uint8 playerLevel);

        // Called after calculating experience color.
        virtual void OnColorCodeCalculation(XPColorChar& color, uint8 playerLevel, uint8 mobLevel);

        // Called after calculating zero difference.
        virtual void OnZeroDifferenceCalculation(uint8& diff, uint8 playerLevel);

        // Called after calculating base experience gain.
        virtual void OnBaseGainCalculation(uint32& gain, uint8 playerLevel, uint8 mobLevel);

        // Called after calculating experience gain.
        virtual void OnGainCalculation(uint32& gain, Player* player, Unit* unit);

        // Called when calculating the experience rate for group experience.
        virtual void OnGroupRateCalculation(float& rate, uint32 count, bool isRaid);
};

template<class TMap>
class TC_GAME_API MapScript
{
        MapEntry const* _mapEntry;

    protected:

        explicit MapScript(MapEntry const* mapEntry);

    public:

        // Gets the MapEntry structure associated with this script. Can return NULL.
        MapEntry const* GetEntry() const;

        // Called when the map is created.
        virtual void OnCreate(TMap* map);

        // Called just before the map is destroyed.
        virtual void OnDestroy(TMap* map);

        // Called when a player enters the map.
        virtual void OnPlayerEnter(TMap* map, Player* player);

        // Called when a player leaves the map.
        virtual void OnPlayerLeave(TMap* map, Player* player);

        virtual void OnUpdate(TMap* map, uint32 diff);
};

class TC_GAME_API WorldMapScript : public ScriptObject, public MapScript<Map>
{
    protected:

        explicit WorldMapScript(char const* name, uint32 mapId);

    public:

        ~WorldMapScript();
};

class TC_GAME_API InstanceMapScript : public ScriptObject, public MapScript<InstanceMap>
{
    protected:

        explicit InstanceMapScript(char const* name, uint32 mapId);

    public:

        ~InstanceMapScript();

        // Gets an InstanceScript object for this instance.
        virtual InstanceScript* GetInstanceScript(InstanceMap* map) const;
};

class TC_GAME_API BattlegroundMapScript : public ScriptObject, public MapScript<BattlegroundMap>
{
    protected:

        explicit BattlegroundMapScript(char const* name, uint32 mapId);

    public:

        ~BattlegroundMapScript();

        // Gets an BattlegroundScript object for this battleground.
        virtual BattlegroundScript* GetBattlegroundScript(BattlegroundMap* map) const;
};

class TC_GAME_API ItemScript : public ScriptObject
{
    protected:

        explicit ItemScript(char const* name);

    public:

        ~ItemScript();

        // Called when a player accepts a quest from the item.
        virtual bool OnQuestAccept(Player* player, Item* item, Quest const* quest);

        // Called when a player uses the item.
        virtual bool OnUse(Player* player, Item* item, SpellCastTargets const& targets, ObjectGuid castId);

        // Called when the item expires (is destroyed).
        virtual bool OnExpire(Player* player, ItemTemplate const* proto);

        // Called when the item is destroyed.
        virtual bool OnRemove(Player* player, Item* item);

        // Called before casting a combat spell from this item (chance on hit spells of item template, can be used to prevent cast if returning false)
        virtual bool OnCastItemCombatSpell(Player* player, Unit* victim, SpellInfo const* spellInfo, Item* item);
};

class TC_GAME_API UnitScript : public ScriptObject
{
    protected:

        explicit UnitScript(char const* name);

    public:

        ~UnitScript();

        // Called when a unit deals healing to another unit
        virtual void OnHeal(Unit* healer, Unit* reciever, uint32& gain);

        // Called when a unit deals damage to another unit
        virtual void OnDamage(Unit* attacker, Unit* victim, uint32& damage);

        // Called when DoT's Tick Damage is being Dealt
        virtual void ModifyPeriodicDamageAurasTick(Unit* target, Unit* attacker, uint32& damage);

        // Called when Melee Damage is being Dealt
        virtual void ModifyMeleeDamage(Unit* target, Unit* attacker, uint32& damage);

        // Called when Spell Damage is being Dealt
        virtual void ModifySpellDamageTaken(Unit* target, Unit* attacker, int32& damage, SpellInfo const* spellInfo);
};

class TC_GAME_API CreatureScript : public ScriptObject
{
    protected:

        explicit CreatureScript(char const* name);

    public:

        ~CreatureScript();

        // Called when a CreatureAI object is needed for the creature.
        virtual CreatureAI* GetAI(Creature* creature) const = 0;
};

class TC_GAME_API GameObjectScript : public ScriptObject
{
    protected:

        explicit GameObjectScript(char const* name);

    public:

        ~GameObjectScript();

        // Called when a GameObjectAI object is needed for the gameobject.
        virtual GameObjectAI* GetAI(GameObject* go) const = 0;
};

class TC_GAME_API AreaTriggerScript : public ScriptObject
{
    protected:

        explicit AreaTriggerScript(char const* name);

    public:

        ~AreaTriggerScript();

        // Called when the area trigger is activated by a player.
        virtual bool OnTrigger(Player* player, AreaTriggerEntry const* trigger);

        // Called when the area trigger is left by a player.
        virtual bool OnExit(Player* player, AreaTriggerEntry const* trigger);
};

class TC_GAME_API OnlyOnceAreaTriggerScript : public AreaTriggerScript
{
        using AreaTriggerScript::AreaTriggerScript;

    public:

        ~OnlyOnceAreaTriggerScript();

        bool OnTrigger(Player* player, AreaTriggerEntry const* trigger) final;

    protected:
        // returns true if the trigger was successfully handled, false if we should try again next time
        virtual bool TryHandleOnce(Player* player, AreaTriggerEntry const* trigger) = 0;
        void ResetAreaTriggerDone(InstanceScript* instance, uint32 triggerId);
        void ResetAreaTriggerDone(Player const* player, AreaTriggerEntry const* trigger);
};

class TC_GAME_API BattlefieldScript : public ScriptObject
{
    protected:

        explicit BattlefieldScript(char const* name);

    public:

        ~BattlefieldScript();

        virtual Battlefield* GetBattlefield(Map* map) const = 0;
};

class TC_GAME_API OutdoorPvPScript : public ScriptObject
{
    protected:

        explicit OutdoorPvPScript(char const* name);

    public:

        ~OutdoorPvPScript();

        // Should return a fully valid OutdoorPvP object for the type ID.
        virtual OutdoorPvP* GetOutdoorPvP(Map* map) const = 0;
};

class TC_GAME_API CommandScript : public ScriptObject
{
    protected:

        explicit CommandScript(char const* name);

    public:

        ~CommandScript();

        // Should return a pointer to a valid command table (ChatCommand array) to be used by ChatHandler.
        virtual std::vector<Trinity::ChatCommands::ChatCommandBuilder> GetCommands() const = 0;
};

class TC_GAME_API WeatherScript : public ScriptObject
{
    protected:

        explicit WeatherScript(char const* name);

    public:

        ~WeatherScript();

        // Called when the weather changes in the zone this script is associated with.
        virtual void OnChange(Weather* weather, WeatherState state, float grade);

        virtual void OnUpdate(Weather* weather, uint32 diff);
};

class TC_GAME_API AuctionHouseScript : public ScriptObject
{
    protected:

        explicit AuctionHouseScript(char const* name);

    public:

        ~AuctionHouseScript();

        // Called when an auction is added to an auction house.
        virtual void OnAuctionAdd(AuctionHouseObject* ah, AuctionPosting* auction);

        // Called when an auction is removed from an auction house.
        virtual void OnAuctionRemove(AuctionHouseObject* ah, AuctionPosting* auction);

        // Called when an auction was succesfully completed.
        virtual void OnAuctionSuccessful(AuctionHouseObject* ah, AuctionPosting* auction);

        // Called when an auction expires.
        virtual void OnAuctionExpire(AuctionHouseObject* ah, AuctionPosting* auction);
};

class TC_GAME_API ConditionScript : public ScriptObject
{
    protected:

        explicit ConditionScript(char const* name);

    public:

        ~ConditionScript();

        // Called when a single condition is checked for a player.
        virtual bool OnConditionCheck(Condition const* condition, ConditionSourceInfo& sourceInfo);
};

class TC_GAME_API VehicleScript : public ScriptObject
{
    protected:

        explicit VehicleScript(char const* name);

    public:

        ~VehicleScript();

        // Called after a vehicle is installed.
        virtual void OnInstall(Vehicle* veh);

        // Called after a vehicle is uninstalled.
        virtual void OnUninstall(Vehicle* veh);

        // Called when a vehicle resets.
        virtual void OnReset(Vehicle* veh);

        // Called after an accessory is installed in a vehicle.
        virtual void OnInstallAccessory(Vehicle* veh, Creature* accessory);

        // Called after a passenger is added to a vehicle.
        virtual void OnAddPassenger(Vehicle* veh, Unit* passenger, int8 seatId);

        // Called after a passenger is removed from a vehicle.
        virtual void OnRemovePassenger(Vehicle* veh, Unit* passenger);
};

class TC_GAME_API DynamicObjectScript : public ScriptObject
{
    protected:

        explicit DynamicObjectScript(char const* name);

    public:

        ~DynamicObjectScript();

        virtual void OnUpdate(DynamicObject* obj, uint32 diff);
};

class TC_GAME_API TransportScript : public ScriptObject
{
    protected:

        explicit TransportScript(char const* name);

    public:

        ~TransportScript();

        // Called when a player boards the transport.
        virtual void OnAddPassenger(Transport* transport, Player* player);

        // Called when a creature boards the transport.
        virtual void OnAddCreaturePassenger(Transport* transport, Creature* creature);

        // Called when a player exits the transport.
        virtual void OnRemovePassenger(Transport* transport, Player* player);

        // Called when a transport moves.
        virtual void OnRelocate(Transport* transport, uint32 mapId, float x, float y, float z);

        virtual void OnUpdate(Transport* transport, uint32 diff);
};

class TC_GAME_API AchievementScript : public ScriptObject
{
    protected:

        explicit AchievementScript(char const* name);

    public:

        ~AchievementScript();

        // Called when an achievement is completed.
        virtual void OnCompleted(Player* player, AchievementEntry const* achievement);
};

class TC_GAME_API AchievementCriteriaScript : public ScriptObject
{
    protected:

        explicit AchievementCriteriaScript(char const* name);

    public:

        ~AchievementCriteriaScript();

        // Called when an additional criteria is checked.
        virtual bool OnCheck(Player* source, Unit* target) = 0;
};

class TC_GAME_API PlayerScript : public ScriptObject
{
    protected:

        explicit PlayerScript(char const* name);

    public:

        ~PlayerScript();

        // Called when a player kills another player
        virtual void OnPVPKill(Player* killer, Player* killed);

        // Called when a player kills a creature
        virtual void OnCreatureKill(Player* killer, Creature* killed);

        // Called when a player is killed by a creature
        virtual void OnPlayerKilledByCreature(Creature* killer, Player* killed);

        // Called when a player's level changes (after the level is applied)
        virtual void OnLevelChanged(Player* player, uint8 oldLevel);

        // Called when a player's free talent points change (right before the change is applied)
        virtual void OnFreeTalentPointsChanged(Player* player, uint32 points);

        // Called when a player's talent points are reset (right before the reset is done)
        virtual void OnTalentsReset(Player* player, bool noCost);

        // Called when a player's money is modified (before the modification is done)
        virtual void OnMoneyChanged(Player* player, int64& amount);

        // Called when a player's money is at limit (amount = money tried to add)
        virtual void OnMoneyLimit(Player* player, int64 amount);

        // Called when a player gains XP (before anything is given)
        virtual void OnGiveXP(Player* player, uint32& amount, Unit* victim);

        // Called when a player's reputation changes (before it is actually changed)
        virtual void OnReputationChange(Player* player, uint32 factionId, int32& standing, bool incremental);

        // Called when a duel is requested
        virtual void OnDuelRequest(Player* target, Player* challenger);

        // Called when a duel starts (after 3s countdown)
        virtual void OnDuelStart(Player* player1, Player* player2);

        // Called when a duel ends
        virtual void OnDuelEnd(Player* winner, Player* loser, DuelCompleteType type);

        // The following methods are called when a player sends a chat message.
        virtual void OnChat(Player* player, uint32 type, uint32 lang, std::string& msg);

        virtual void OnChat(Player* player, uint32 type, uint32 lang, std::string& msg, Player* receiver);

        virtual void OnChat(Player* player, uint32 type, uint32 lang, std::string& msg, Group* group);

        virtual void OnChat(Player* player, uint32 type, uint32 lang, std::string& msg, Guild* guild);

        virtual void OnChat(Player* player, uint32 type, uint32 lang, std::string& msg, Channel* channel);

        // Both of the below are called on emote opcodes.
        virtual void OnClearEmote(Player* player);

        virtual void OnTextEmote(Player* player, uint32 textEmote, uint32 emoteNum, ObjectGuid guid);

        // Called in Spell::Cast.
        virtual void OnSpellCast(Player* player, Spell* spell, bool skipCheck);

        // Called when a player logs in.
        virtual void OnLogin(Player* player, bool firstLogin);

        // Called when a player logs out.
        virtual void OnLogout(Player* player);

        // Called when a player is created.
        virtual void OnCreate(Player* player);

        // Called when a player is deleted.
        virtual void OnDelete(ObjectGuid guid, uint32 accountId);

        // Called when a player delete failed
        virtual void OnFailedDelete(ObjectGuid guid, uint32 accountId);

        // Called when a player is about to be saved.
        virtual void OnSave(Player* player);

        // Called when a player is bound to an instance
        virtual void OnBindToInstance(Player* player, Difficulty difficulty, uint32 mapId, bool permanent, uint8 extendState);

        // Called when a player switches to a new zone
        virtual void OnUpdateZone(Player* player, uint32 newZone, uint32 newArea);

        // Called when a player changes to a new map (after moving to new map)
        virtual void OnMapChanged(Player* player);

        // Called after a player's quest status has been changed
        virtual void OnQuestStatusChange(Player* player, uint32 questId);

        // Called when a player presses release when he died
        virtual void OnPlayerRepop(Player* player);

        // Called when a player completes a movie
        virtual void OnMovieComplete(Player* player, uint32 movieId);

        // Called when a player choose a response from a PlayerChoice
        virtual void OnPlayerChoiceResponse(Player* player, uint32 choiceId, uint32 responseId);
};

class TC_GAME_API AccountScript : public ScriptObject
{
    protected:

        explicit AccountScript(char const* name);

    public:

        ~AccountScript();

        // Called when an account logged in succesfully
        virtual void OnAccountLogin(uint32 accountId);

        // Called when an account login failed
        virtual void OnFailedAccountLogin(uint32 accountId);

        // Called when Email is successfully changed for Account
        virtual void OnEmailChange(uint32 accountId);

        // Called when Email failed to change for Account
        virtual void OnFailedEmailChange(uint32 accountId);

        // Called when Password is successfully changed for Account
        virtual void OnPasswordChange(uint32 accountId);

        // Called when Password failed to change for Account
        virtual void OnFailedPasswordChange(uint32 accountId);
};

class TC_GAME_API GuildScript : public ScriptObject
{
    protected:

        explicit GuildScript(char const* name);

    public:

        ~GuildScript();

        // Called when a member is added to the guild.
        virtual void OnAddMember(Guild* guild, Player* player, uint8 plRank);

        // Called when a member is removed from the guild.
        virtual void OnRemoveMember(Guild* guild, ObjectGuid guid, bool isDisbanding, bool isKicked);

        // Called when the guild MOTD (message of the day) changes.
        virtual void OnMOTDChanged(Guild* guild, std::string const& newMotd);

        // Called when the guild info is altered.
        virtual void OnInfoChanged(Guild* guild, std::string const& newInfo);

        // Called when a guild is created.
        virtual void OnCreate(Guild* guild, Player* leader, std::string const& name);

        // Called when a guild is disbanded.
        virtual void OnDisband(Guild* guild);

        // Called when a guild member withdraws money from a guild bank.
        virtual void OnMemberWitdrawMoney(Guild* guild, Player* player, uint64& amount, bool isRepair);

        // Called when a guild member deposits money in a guild bank.
        virtual void OnMemberDepositMoney(Guild* guild, Player* player, uint64& amount);

        // Called when a guild member moves an item in a guild bank.
        virtual void OnItemMove(Guild* guild, Player* player, Item* pItem, bool isSrcBank, uint8 srcContainer, uint8 srcSlotId,
            bool isDestBank, uint8 destContainer, uint8 destSlotId);

        virtual void OnEvent(Guild* guild, uint8 eventType, ObjectGuid::LowType playerGuid1, ObjectGuid::LowType playerGuid2, uint8 newRank);

        virtual void OnBankEvent(Guild* guild, uint8 eventType, uint8 tabId, ObjectGuid::LowType playerGuid, uint64 itemOrMoney, uint16 itemStackCount, uint8 destTabId);
};

class TC_GAME_API GroupScript : public ScriptObject
{
    protected:

        explicit GroupScript(char const* name);

    public:

        ~GroupScript();

        // Called when a member is added to a group.
        virtual void OnAddMember(Group* group, ObjectGuid guid);

        // Called when a member is invited to join a group.
        virtual void OnInviteMember(Group* group, ObjectGuid guid);

        // Called when a member is removed from a group.
        virtual void OnRemoveMember(Group* group, ObjectGuid guid, RemoveMethod method, ObjectGuid kicker, char const* reason);

        // Called when the leader of a group is changed.
        virtual void OnChangeLeader(Group* group, ObjectGuid newLeaderGuid, ObjectGuid oldLeaderGuid);

        // Called when a group is disbanded.
        virtual void OnDisband(Group* group);
};

class TC_GAME_API AreaTriggerEntityScript : public ScriptObject
{
    protected:

        explicit AreaTriggerEntityScript(char const* name);

    public:

        ~AreaTriggerEntityScript();

        // Called when a AreaTriggerAI object is needed for the areatrigger.
        virtual AreaTriggerAI* GetAI(AreaTrigger* at) const;
};

class TC_GAME_API ConversationScript : public ScriptObject
{
    protected:

        explicit ConversationScript(char const* name);

    public:

        ~ConversationScript();

        // Called when Conversation is created but not added to Map yet.
        virtual void OnConversationCreate(Conversation* conversation, Unit* creator);

        // Called when Conversation is started
        virtual void OnConversationStart(Conversation* conversation);

        // Called when player sends CMSG_CONVERSATION_LINE_STARTED with valid conversation guid
        virtual void OnConversationLineStarted(Conversation* conversation, uint32 lineId, Player* sender);

        // Called for each update tick
        virtual void OnConversationUpdate(Conversation* conversation, uint32 diff);
};

class TC_GAME_API SceneScript : public ScriptObject
{
    protected:

        explicit SceneScript(char const* name);

    public:

        ~SceneScript();

        // Called when a player start a scene
        virtual void OnSceneStart(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);

        // Called when a player receive trigger from scene
        virtual void OnSceneTriggerEvent(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate, std::string const& triggerName);

        // Called when a scene is canceled
        virtual void OnSceneCancel(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);

        // Called when a scene is completed
        virtual void OnSceneComplete(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);
};

class TC_GAME_API QuestScript : public ScriptObject
{
    protected:

        explicit QuestScript(char const* name);

    public:

        ~QuestScript();

        // Called when a quest status change
        virtual void OnQuestStatusChange(Player* player, Quest const* quest, QuestStatus oldStatus, QuestStatus newStatus);

        // Called for auto accept quests when player closes quest UI after seeing initial quest details
        virtual void OnAcknowledgeAutoAccept(Player* player, Quest const* quest);

        // Called when a quest objective data change
        virtual void OnQuestObjectiveChange(Player* player, Quest const* quest, QuestObjective const& objective, int32 oldAmount, int32 newAmount);
};

class TC_GAME_API WorldStateScript : public ScriptObject
{
    protected:

        explicit WorldStateScript(char const* name);

    public:

        ~WorldStateScript();

        // Called when worldstate changes value, map is optional
        virtual void OnValueChange(int32 worldStateId, int32 oldValue, int32 newValue, Map const* map);
};

class TC_GAME_API EventScript : public ScriptObject
{
    protected:

        explicit EventScript(char const* name);

    public:

        ~EventScript();

        // Called when a game event is triggered
        virtual void OnTrigger(WorldObject* object, WorldObject* invoker, uint32 eventId);
};

// Manages registration, loading, and execution of scripts.
class TC_GAME_API ScriptMgr
{
    friend class ScriptObject;

    private:
        ScriptMgr();
        ~ScriptMgr();

        ScriptMgr(ScriptMgr const& right) = delete;
        ScriptMgr(ScriptMgr&& right) = delete;
        ScriptMgr& operator=(ScriptMgr const& right) = delete;
        ScriptMgr& operator=(ScriptMgr&& right) = delete;

        void FillSpellSummary();
        void LoadDatabase();

        void IncreaseScriptCount() { ++_scriptCount; }
        void DecreaseScriptCount() { --_scriptCount; }

    public: /* Initialization */
        static ScriptMgr* instance();

        void Initialize();

        uint32 GetScriptCount() const { return _scriptCount; }

        typedef void(*ScriptLoaderCallbackType)();

        /// Sets the script loader callback which is invoked to load scripts
        /// (Workaround for circular dependency game <-> scripts)
        void SetScriptLoader(ScriptLoaderCallbackType script_loader_callback)
        {
            _script_loader_callback = script_loader_callback;
        }

    public: /* Updating script ids */
        /// Inform the ScriptMgr that an entity has a changed script id
        void NotifyScriptIDUpdate();
        /// Synchronize all scripts with their current ids
        void SyncScripts();

    public: /* Script contexts */
        /// Set the current script context, which allows the ScriptMgr
        /// to accept new scripts in this context.
        /// Requires a SwapScriptContext() call afterwards to load the new scripts.
        void SetScriptContext(std::string const& context);
        /// Returns the current script context.
        std::string const& GetCurrentScriptContext() const { return _currentContext; }
        /// Releases all scripts associated with the given script context immediately.
        /// Requires a SwapScriptContext() call afterwards to finish the unloading.
        void ReleaseScriptContext(std::string const& context);
        /// Executes all changed introduced by SetScriptContext and ReleaseScriptContext.
        /// It is possible to combine multiple SetScriptContext and ReleaseScriptContext
        /// calls for better performance (bulk changes).
        void SwapScriptContext(bool initialize = false);

        /// Returns the context name of the static context provided by the worldserver
        static std::string const& GetNameOfStaticContext();

        /// Acquires a strong module reference to the module containing the given script name,
        /// which prevents the shared library which contains the script from unloading.
        /// The shared library is lazy unloaded as soon as all references to it are released.
        std::shared_ptr<ModuleReference> AcquireModuleReferenceOfScriptName(
            std::string const& scriptname) const;

    public: /* Unloading */

        void Unload();

    public: /* SpellScriptLoader */

        void CreateSpellScripts(uint32 spellId, std::vector<SpellScript*>& scriptVector, Spell* invoker) const;
        void CreateAuraScripts(uint32 spellId, std::vector<AuraScript*>& scriptVector, Aura* invoker) const;
        SpellScriptLoader* GetSpellScriptLoader(uint32 scriptId);

    public: /* ServerScript */

        void OnNetworkStart();
        void OnNetworkStop();
        void OnSocketOpen(std::shared_ptr<WorldSocket> socket);
        void OnSocketClose(std::shared_ptr<WorldSocket> socket);
        void OnPacketReceive(WorldSession* session, WorldPacket const& packet);
        void OnPacketSend(WorldSession* session, WorldPacket const& packet);

    public: /* WorldScript */

        void OnOpenStateChange(bool open);
        void OnConfigLoad(bool reload);
        void OnMotdChange(std::string& newMotd);
        void OnShutdownInitiate(ShutdownExitCode code, ShutdownMask mask);
        void OnShutdownCancel();
        void OnWorldUpdate(uint32 diff);
        void OnStartup();
        void OnShutdown();

    public: /* FormulaScript */

        void OnHonorCalculation(float& honor, uint8 level, float multiplier);
        void OnGrayLevelCalculation(uint8& grayLevel, uint8 playerLevel);
        void OnColorCodeCalculation(XPColorChar& color, uint8 playerLevel, uint8 mobLevel);
        void OnZeroDifferenceCalculation(uint8& diff, uint8 playerLevel);
        void OnBaseGainCalculation(uint32& gain, uint8 playerLevel, uint8 mobLevel);
        void OnGainCalculation(uint32& gain, Player* player, Unit* unit);
        void OnGroupRateCalculation(float& rate, uint32 count, bool isRaid);

    public: /* MapScript */

        void OnCreateMap(Map* map);
        void OnDestroyMap(Map* map);
        void OnPlayerEnterMap(Map* map, Player* player);
        void OnPlayerLeaveMap(Map* map, Player* player);
        void OnMapUpdate(Map* map, uint32 diff);

    public: /* InstanceMapScript */

        InstanceScript* CreateInstanceData(InstanceMap* map);

    public: /* ItemScript */

        bool OnQuestAccept(Player* player, Item* item, Quest const* quest);
        bool OnItemUse(Player* player, Item* item, SpellCastTargets const& targets, ObjectGuid castId);
        bool OnItemExpire(Player* player, ItemTemplate const* proto);
        bool OnItemRemove(Player* player, Item* item);
        bool OnCastItemCombatSpell(Player* player, Unit* victim, SpellInfo const* spellInfo, Item* item);

    public: /* CreatureScript */

        bool CanCreateCreatureAI(uint32 scriptId) const;
        CreatureAI* GetCreatureAI(Creature* creature);

    public: /* GameObjectScript */

        bool CanCreateGameObjectAI(uint32 scriptId) const;
        GameObjectAI* GetGameObjectAI(GameObject* go);

    public: /* AreaTriggerScript */

        bool OnAreaTrigger(Player* player, AreaTriggerEntry const* trigger, bool entered);

    public: /* BattlefieldScript */

        Battlefield* CreateBattlefield(uint32 scriptId, Map* map);

    public: /* BattlegroundScript */

        BattlegroundScript* CreateBattlegroundData(BattlegroundMap* map);

    public: /* OutdoorPvPScript */

        OutdoorPvP* CreateOutdoorPvP(uint32 scriptId, Map* map);

    public: /* CommandScript */

        std::vector<Trinity::ChatCommands::ChatCommandBuilder> GetChatCommands();

    public: /* WeatherScript */

        void OnWeatherChange(Weather* weather, WeatherState state, float grade);
        void OnWeatherUpdate(Weather* weather, uint32 diff);

    public: /* AuctionHouseScript */

        void OnAuctionAdd(AuctionHouseObject* ah, AuctionPosting* auction);
        void OnAuctionRemove(AuctionHouseObject* ah, AuctionPosting* auction);
        void OnAuctionSuccessful(AuctionHouseObject* ah, AuctionPosting* auction);
        void OnAuctionExpire(AuctionHouseObject* ah, AuctionPosting* auction);

    public: /* ConditionScript */

        bool OnConditionCheck(Condition const* condition, ConditionSourceInfo& sourceInfo);

    public: /* VehicleScript */

        void OnInstall(Vehicle* veh);
        void OnUninstall(Vehicle* veh);
        void OnReset(Vehicle* veh);
        void OnInstallAccessory(Vehicle* veh, Creature* accessory);
        void OnAddPassenger(Vehicle* veh, Unit* passenger, int8 seatId);
        void OnRemovePassenger(Vehicle* veh, Unit* passenger);

    public: /* DynamicObjectScript */

        void OnDynamicObjectUpdate(DynamicObject* dynobj, uint32 diff);

    public: /* TransportScript */

        void OnAddPassenger(Transport* transport, Player* player);
        void OnAddCreaturePassenger(Transport* transport, Creature* creature);
        void OnRemovePassenger(Transport* transport, Player* player);
        void OnTransportUpdate(Transport* transport, uint32 diff);
        void OnRelocate(Transport* transport, uint32 mapId, float x, float y, float z);

    public: /* AchievementScript */

        void OnAchievementCompleted(Player* player, AchievementEntry const* achievement);

    public: /* AchievementCriteriaScript */

        bool OnCriteriaCheck(uint32 scriptId, Player* source, Unit* target);

    public: /* PlayerScript */

        void OnPVPKill(Player* killer, Player* killed);
        void OnCreatureKill(Player* killer, Creature* killed);
        void OnPlayerKilledByCreature(Creature* killer, Player* killed);
        void OnPlayerLevelChanged(Player* player, uint8 oldLevel);
        void OnPlayerFreeTalentPointsChanged(Player* player, uint32 newPoints);
        void OnPlayerTalentsReset(Player* player, bool noCost);
        void OnPlayerMoneyChanged(Player* player, int64& amount);
        void OnPlayerMoneyLimit(Player* player, int64 amount);
        void OnGivePlayerXP(Player* player, uint32& amount, Unit* victim);
        void OnPlayerReputationChange(Player* player, uint32 factionID, int32& standing, bool incremental);
        void OnPlayerDuelRequest(Player* target, Player* challenger);
        void OnPlayerDuelStart(Player* player1, Player* player2);
        void OnPlayerDuelEnd(Player* winner, Player* loser, DuelCompleteType type);
        void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg);
        void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Player* receiver);
        void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Group* group);
        void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Guild* guild);
        void OnPlayerChat(Player* player, uint32 type, uint32 lang, std::string& msg, Channel* channel);
        void OnPlayerClearEmote(Player* player);
        void OnPlayerTextEmote(Player* player, uint32 textEmote, uint32 emoteNum, ObjectGuid guid);
        void OnPlayerSpellCast(Player* player, Spell* spell, bool skipCheck);
        void OnPlayerLogin(Player* player, bool firstLogin);
        void OnPlayerLogout(Player* player);
        void OnPlayerCreate(Player* player);
        void OnPlayerDelete(ObjectGuid guid, uint32 accountId);
        void OnPlayerFailedDelete(ObjectGuid guid, uint32 accountId);
        void OnPlayerSave(Player* player);
        void OnPlayerBindToInstance(Player* player, Difficulty difficulty, uint32 mapid, bool permanent, uint8 extendState);
        void OnPlayerUpdateZone(Player* player, uint32 newZone, uint32 newArea);
        void OnQuestStatusChange(Player* player, uint32 questId);
        void OnPlayerRepop(Player* player);
        void OnMovieComplete(Player* player, uint32 movieId);
        void OnPlayerChoiceResponse(Player* player, uint32 choiceId, uint32 responseId);

    public: /* AccountScript */

        void OnAccountLogin(uint32 accountId);
        void OnFailedAccountLogin(uint32 accountId);
        void OnEmailChange(uint32 accountId);
        void OnFailedEmailChange(uint32 accountId);
        void OnPasswordChange(uint32 accountId);
        void OnFailedPasswordChange(uint32 accountId);

    public: /* GuildScript */

        void OnGuildAddMember(Guild* guild, Player* player, uint8 plRank);
        void OnGuildRemoveMember(Guild* guild, ObjectGuid guid, bool isDisbanding, bool isKicked);
        void OnGuildMOTDChanged(Guild* guild, const std::string& newMotd);
        void OnGuildInfoChanged(Guild* guild, const std::string& newInfo);
        void OnGuildCreate(Guild* guild, Player* leader, const std::string& name);
        void OnGuildDisband(Guild* guild);
        void OnGuildMemberWitdrawMoney(Guild* guild, Player* player, uint64 &amount, bool isRepair);
        void OnGuildMemberDepositMoney(Guild* guild, Player* player, uint64 &amount);
        void OnGuildItemMove(Guild* guild, Player* player, Item* pItem, bool isSrcBank, uint8 srcContainer, uint8 srcSlotId,
            bool isDestBank, uint8 destContainer, uint8 destSlotId);
        void OnGuildEvent(Guild* guild, uint8 eventType, ObjectGuid::LowType playerGuid1, ObjectGuid::LowType playerGuid2, uint8 newRank);
        void OnGuildBankEvent(Guild* guild, uint8 eventType, uint8 tabId, ObjectGuid::LowType playerGuid, uint64 itemOrMoney, uint16 itemStackCount, uint8 destTabId);

    public: /* GroupScript */

        void OnGroupAddMember(Group* group, ObjectGuid guid);
        void OnGroupInviteMember(Group* group, ObjectGuid guid);
        void OnGroupRemoveMember(Group* group, ObjectGuid guid, RemoveMethod method, ObjectGuid kicker, char const* reason);
        void OnGroupChangeLeader(Group* group, ObjectGuid newLeaderGuid, ObjectGuid oldLeaderGuid);
        void OnGroupDisband(Group* group);

    public: /* UnitScript */

        void OnHeal(Unit* healer, Unit* reciever, uint32& gain);
        void OnDamage(Unit* attacker, Unit* victim, uint32& damage);
        void ModifyPeriodicDamageAurasTick(Unit* target, Unit* attacker, uint32& damage);
        void ModifyMeleeDamage(Unit* target, Unit* attacker, uint32& damage);
        void ModifySpellDamageTaken(Unit* target, Unit* attacker, int32& damage, SpellInfo const* spellInfo);

    public: /* AreaTriggerEntityScript */

        bool CanCreateAreaTriggerAI(uint32 scriptId) const;
        AreaTriggerAI* GetAreaTriggerAI(AreaTrigger* areaTrigger);

    public: /* ConversationScript */

        void OnConversationCreate(Conversation* conversation, Unit* creator);
        void OnConversationStart(Conversation* conversation);
        void OnConversationLineStarted(Conversation* conversation, uint32 lineId, Player* sender);
        void OnConversationUpdate(Conversation* conversation, uint32 diff);

    public: /* SceneScript */

        void OnSceneStart(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);
        void OnSceneTrigger(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate, std::string const& triggerName);
        void OnSceneCancel(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);
        void OnSceneComplete(Player* player, uint32 sceneInstanceID, SceneTemplate const* sceneTemplate);

    public: /* QuestScript */

        void OnQuestStatusChange(Player* player, Quest const* quest, QuestStatus oldStatus, QuestStatus newStatus);
        void OnQuestAcknowledgeAutoAccept(Player* player, Quest const* quest);
        void OnQuestObjectiveChange(Player* player, Quest const* quest, QuestObjective const& objective, int32 oldAmount, int32 newAmount);

    public: /* WorldStateScript */

        void OnWorldStateValueChange(WorldStateTemplate const* worldStateTemplate, int32 oldValue, int32 newValue, Map const* map);

    public: /* EventScript */

        void OnEventTrigger(WorldObject* object, WorldObject* invoker, uint32 eventId);

    private:
        uint32 _scriptCount;
        bool _scriptIdUpdated;

        ScriptLoaderCallbackType _script_loader_callback;

        std::string _currentContext;
};

namespace Trinity::SpellScripts
{
    template<typename T>
    using is_SpellScript = std::is_base_of<SpellScript, T>;

    template<typename T>
    using is_AuraScript = std::is_base_of<AuraScript, T>;
}

template <typename... Ts>
class GenericSpellAndAuraScriptLoader : public SpellScriptLoader
{
    using SpellScriptType = typename Trinity::find_type_if_t<Trinity::SpellScripts::is_SpellScript, Ts...>;
    using AuraScriptType = typename Trinity::find_type_if_t<Trinity::SpellScripts::is_AuraScript, Ts...>;
    using ArgsType = typename Trinity::find_type_if_t<Trinity::is_tuple, Ts...>;

    static_assert(!std::conjunction_v<std::is_same<SpellScriptType, Trinity::find_type_end>, std::is_same<AuraScriptType, Trinity::find_type_end>>, "At least one of SpellScript/AuraScript arguments must be provided for GenericSpellAndAuraScriptLoader");

public:
    GenericSpellAndAuraScriptLoader(char const* name, ArgsType&& args) : SpellScriptLoader(name), _args(std::move(args)) { }

private:
    SpellScript* GetSpellScript() const override
    {
        if constexpr (!std::is_same_v<SpellScriptType, Trinity::find_type_end>)
            return Trinity::new_from_tuple<SpellScriptType>(_args);
        else
            return nullptr;
    }

    AuraScript* GetAuraScript() const override
    {
        if constexpr (!std::is_same_v<AuraScriptType, Trinity::find_type_end>)
            return Trinity::new_from_tuple<AuraScriptType>(_args);
        else
            return nullptr;
    }

    ArgsType _args;
};

#define RegisterSpellScriptWithArgs(spell_script, script_name, ...) new GenericSpellAndAuraScriptLoader<BOOST_PP_REMOVE_PARENS(spell_script), decltype(std::make_tuple(__VA_ARGS__))>(script_name, std::make_tuple(__VA_ARGS__))
#define RegisterSpellScript(spell_script) RegisterSpellScriptWithArgs(spell_script, #spell_script)
#define RegisterSpellAndAuraScriptPairWithArgs(script_1, script_2, script_name, ...) new GenericSpellAndAuraScriptLoader<BOOST_PP_REMOVE_PARENS(script_1), BOOST_PP_REMOVE_PARENS(script_2), decltype(std::make_tuple(__VA_ARGS__))>(script_name, std::make_tuple(__VA_ARGS__))
#define RegisterSpellAndAuraScriptPair(script_1, script_2) RegisterSpellAndAuraScriptPairWithArgs(script_1, script_2, #script_1)

template <class AI>
class GenericCreatureScript : public CreatureScript
{
    public:
        GenericCreatureScript(char const* name) : CreatureScript(name) { }
        CreatureAI* GetAI(Creature* me) const override { return new AI(me); }
};
#define RegisterCreatureAI(ai_name) new GenericCreatureScript<ai_name>(#ai_name)

template <class AI, AI* (*AIFactory)(Creature*)>
class FactoryCreatureScript : public CreatureScript
{
    public:
        FactoryCreatureScript(char const* name) : CreatureScript(name) { }
        CreatureAI* GetAI(Creature* me) const override { return AIFactory(me); }
};
#define RegisterCreatureAIWithFactory(ai_name, factory_fn) new FactoryCreatureScript<ai_name, &factory_fn>(#ai_name)

template <class AI>
class GenericGameObjectScript : public GameObjectScript
{
    public:
        GenericGameObjectScript(char const* name) : GameObjectScript(name) { }
        GameObjectAI* GetAI(GameObject* go) const override { return new AI(go); }
};
#define RegisterGameObjectAI(ai_name) new GenericGameObjectScript<ai_name>(#ai_name)

template <class AI, AI* (*AIFactory)(GameObject*)>
class FactoryGameObjectScript : public GameObjectScript
{
    public:
        FactoryGameObjectScript(char const* name) : GameObjectScript(name) { }
        GameObjectAI* GetAI(GameObject* me) const override { return AIFactory(me); }
};
#define RegisterGameObjectAIWithFactory(ai_name, factory_fn) new FactoryGameObjectScript<ai_name, &factory_fn>(#ai_name)

template <class AI>
class GenericAreaTriggerEntityScript : public AreaTriggerEntityScript
{
    public:
        GenericAreaTriggerEntityScript(char const* name) : AreaTriggerEntityScript(name) { }
        AreaTriggerAI* GetAI(AreaTrigger* at) const override { return new AI(at); }
};
#define RegisterAreaTriggerAI(ai_name) new GenericAreaTriggerEntityScript<ai_name>(#ai_name)

template<class Script>
class GenericBattlegroundMapScript : public BattlegroundMapScript
{
public:
    GenericBattlegroundMapScript(char const* name, uint32 mapId) : BattlegroundMapScript(name, mapId) { }

    BattlegroundScript* GetBattlegroundScript(BattlegroundMap* map) const override { return new Script(map); }
};
#define RegisterBattlegroundMapScript(script_name, mapId) new GenericBattlegroundMapScript<script_name>(#script_name, mapId)

#define sScriptMgr ScriptMgr::instance()

#endif