summaryrefslogtreecommitdiff
path: root/src/server/scripts/Northrend/zone_wintergrasp.cpp
blob: 0c8aae0027d80dbeb465f11dfd8c9657a963de06 (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
/*
 * This file is part of the AzerothCore 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/>.
 */

#include "AchievementCriteriaScript.h"
#include "Battlefield.h"
#include "BattlefieldMgr.h"
#include "BattlefieldWG.h"
#include "CombatAI.h"
#include "CreatureScript.h"
#include "GameGraveyard.h"
#include "GameObjectAI.h"
#include "GameObjectScript.h"
#include "GameTime.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "PoolMgr.h"
#include "ScriptedCreature.h"
#include "ScriptedGossip.h"
#include "SpellScript.h"
#include "SpellScriptLoader.h"
#include "Vehicle.h"
#include "World.h"
#include <cmath>

enum eWGqueuenpctext
{
    WG_NPCQUEUE_TEXT_H_NOWAR            = 14775,
    WG_NPCQUEUE_TEXT_H_QUEUE            = 14790,
    WG_NPCQUEUE_TEXT_H_WAR              = 14777,
    WG_NPCQUEUE_TEXT_A_NOWAR            = 14782,
    WG_NPCQUEUE_TEXT_A_QUEUE            = 14791,
    WG_NPCQUEUE_TEXT_A_WAR              = 14781,
    WG_NPCQUEUE_TEXTOPTION_JOIN         = -1850507,

    WG_GOSSIP_MENU_QUEUE                = 10662,
};

enum Spells
{
    // Demolisher engineers spells
    SPELL_BUILD_SIEGE_VEHICLE_FORCE_HORDE     = 61409, //
    SPELL_BUILD_SIEGE_VEHICLE_FORCE_ALLIANCE  = 56662, // Which faction uses which ?
    SPELL_BUILD_CATAPULT_FORCE                = 56664,
    SPELL_BUILD_DEMOLISHER_FORCE              = 56659,
    SPELL_ACTIVATE_CONTROL_ARMS               = 49899,

    SPELL_VEHICLE_TELEPORT                    = 49759,

    // Spirit guide
    SPELL_CHANNEL_SPIRIT_HEAL                 = 22011,

    // RP-GG
    SPELL_RP_GG_TRIGGER_MISSILE               = 49769,

    // Teleport to fortress
    SPELL_TELEPORT_TO_FORTRESS                = 59096,
};

enum CreatureIds
{
    NPC_GOBLIN_MECHANIC                 = 30400,
    NPC_GNOMISH_ENGINEER                = 30499,

    NPC_WINTERGRASP_CONTROL_ARMS        = 27852,

    NPC_WORLD_TRIGGER_LARGE_AOI_NOT_IMMUNE_PC_NPC = 23742,
};

enum QuestIds
{
    QUEST_BONES_AND_ARROWS_HORDE_ATT              = 13199,
    QUEST_JINXING_THE_WALLS_HORDE_ATT             = 13202,
    QUEST_SLAY_THEM_ALL_HORDE_ATT                 = 13180,
    QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT       = 13200,
    QUEST_HEALING_WITH_ROSES_HORDE_ATT            = 13201,
    QUEST_DEFEND_THE_SIEGE_HORDE_ATT              = 13223,

    QUEST_BONES_AND_ARROWS_HORDE_DEF              = 13193,
    QUEST_WARDING_THE_WALLS_HORDE_DEF             = 13192,
    QUEST_SLAY_THEM_ALL_HORDE_DEF                 = 13178,
    QUEST_FUELING_THE_DEMOLISHERS_HORDE_DEF       = 13191,
    QUEST_HEALING_WITH_ROSES_HORDE_DEF            = 13194,
    QUEST_TOPPLING_THE_TOWERS_HORDE_DEF           = 13539,
    QUEST_STOP_THE_SIEGE_HORDE_DEF                = 13185,

    QUEST_BONES_AND_ARROWS_ALLIANCE_ATT           = 13196,
    QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT       = 13198,
    QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_ATT = 13179,
    QUEST_DEFEND_THE_SIEGE_ALLIANCE_ATT           = 13222,
    QUEST_A_RARE_HERB_ALLIANCE_ATT                = 13195,
    QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT    = 13197,

    QUEST_BONES_AND_ARROWS_ALLIANCE_DEF           = 13154,
    QUEST_WARDING_THE_WARRIORS_ALLIANCE_DEF       = 13153,
    QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_DEF = 13177,
    QUEST_SHOUTHERN_SABOTAGE_ALLIANCE_DEF         = 13538,
    QUEST_STOP_THE_SIEGE_ALLIANCE_DEF             = 13186,
    QUEST_A_RARE_HERB_ALLIANCE_DEF                = 13156,
    QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_DEF    = 236,
};

uint8 const MAX_WINTERGRASP_VEHICLES = 4;

uint32 const vehiclesList[MAX_WINTERGRASP_VEHICLES] =
{
    NPC_WINTERGRASP_CATAPULT,
    NPC_WINTERGRASP_DEMOLISHER,
    NPC_WINTERGRASP_SIEGE_ENGINE_ALLIANCE,
    NPC_WINTERGRASP_SIEGE_ENGINE_HORDE
};

////////////////////////////////////////////////
/////// NPCs
////////////////////////////////////////////////

class npc_wg_demolisher_engineer : public CreatureScript
{
public:
    npc_wg_demolisher_engineer() : CreatureScript("npc_wg_demolisher_engineer") { }

    bool OnGossipHello(Player* player, Creature* creature) override
    {
        if (creature->IsQuestGiver())
        {
            player->PrepareQuestMenu(creature->GetGUID());
        }

        if (player->HasAura(SPELL_CORPORAL))
        {
            AddGossipItemFor(player, 9923, 0, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);
        }
        else if (player->HasAura(SPELL_LIEUTENANT))
        {
            AddGossipItemFor(player, 9923, 0, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);
            AddGossipItemFor(player, 9923, 1, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 1);
            AddGossipItemFor(player, 9923, 2, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 2);
        }

        SendGossipMenuFor(player, player->GetGossipTextId(creature), creature->GetGUID());
        return true;
    }

    bool OnGossipSelect(Player* player, Creature* creature, uint32 /*sender */, uint32 action) override
    {
        CloseGossipMenuFor(player);

        uint32 spellId = 0;
        switch (action - GOSSIP_ACTION_INFO_DEF)
        {
        case 0:
            spellId = SPELL_BUILD_CATAPULT_FORCE;
            break;
        case 1:
            spellId = SPELL_BUILD_DEMOLISHER_FORCE;
            break;
        case 2:
            spellId = player->GetTeamId() == TEAM_ALLIANCE ? SPELL_BUILD_SIEGE_VEHICLE_FORCE_ALLIANCE : SPELL_BUILD_SIEGE_VEHICLE_FORCE_HORDE;
            break;
        }

        if (canBuild(creature))
        {
            creature->CastSpell(player, spellId, true);
            creature->CastSpell(creature, SPELL_ACTIVATE_CONTROL_ARMS, true);
        }
        else
        {
            WorldPacket data(SMSG_CAST_FAILED, 1 + 4 + 1 + 4);
            data << uint8(0);
            data << spellId;
            data << uint8(SPELL_FAILED_CUSTOM_ERROR);
            data << uint32(SPELL_CUSTOM_ERROR_CANT_BUILD_MORE_VEHICLES);
            player->SendDirectMessage(&data);
        }
        return true;
    }

private:
    bool canBuild(Creature* creature)
    {
        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
            return false;

        switch (creature->GetEntry())
        {
            case NPC_GOBLIN_MECHANIC:
                return (wintergrasp->GetData(BATTLEFIELD_WG_DATA_MAX_VEHICLE_H) > wintergrasp->GetData(BATTLEFIELD_WG_DATA_VEHICLE_H));
            case NPC_GNOMISH_ENGINEER:
                return (wintergrasp->GetData(BATTLEFIELD_WG_DATA_MAX_VEHICLE_A) > wintergrasp->GetData(BATTLEFIELD_WG_DATA_VEHICLE_A));
            default:
                return false;
        }
    }
};

class npc_wg_spirit_guide : public CreatureScript
{
public:
    npc_wg_spirit_guide() : CreatureScript("npc_wg_spirit_guide") { }

    bool OnGossipHello(Player* player, Creature* creature) override
    {
        if (creature->IsQuestGiver())
            player->PrepareQuestMenu(creature->GetGUID());

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
            return true;

        GraveyardVect graveyard = wintergrasp->GetGraveyardVector();
        for (uint8 i = 0; i < graveyard.size(); i++)
            if (graveyard[i]->GetControlTeamId() == player->GetTeamId())
                AddGossipItemFor(player, GOSSIP_ICON_CHAT, sObjectMgr->GetAcoreStringForDBCLocale(((BfGraveyardWG*)graveyard[i])->GetTextId()), GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + i);

        SendGossipMenuFor(player, player->GetGossipTextId(creature), creature->GetGUID());
        return true;
    }

    bool OnGossipSelect(Player* player, Creature* /*creature */, uint32 /*sender */, uint32 action) override
    {
        CloseGossipMenuFor(player);

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (wintergrasp)
        {
            GraveyardVect gy = wintergrasp->GetGraveyardVector();
            for (uint8 i = 0; i < gy.size(); i++)
                if (action - GOSSIP_ACTION_INFO_DEF == i && gy[i]->GetControlTeamId() == player->GetTeamId())
                    if (GraveyardStruct const* safeLoc = sGraveyard->GetGraveyard(gy[i]->GetGraveyardId()))
                        player->TeleportTo(safeLoc->Map, safeLoc->x, safeLoc->y, safeLoc->z, 0);
        }
        return true;
    }

    struct npc_wg_spirit_guideAI : public ScriptedAI
    {
        npc_wg_spirit_guideAI(Creature* creature) : ScriptedAI(creature)
        {
            me->setActive(true);
        }

        void UpdateAI(uint32 /*diff*/) override
        {
            if (!me->HasUnitState(UNIT_STATE_CASTING))
                DoCast(me, SPELL_CHANNEL_SPIRIT_HEAL);
        }
    };

    CreatureAI*  GetAI(Creature* creature) const override
    {
        return new npc_wg_spirit_guideAI(creature);
    }
};

enum eWgQueue
{
    EVENT_ARCANIST_BRAEDIN_YELL     = 1,
    EVENT_MAGISTER_SURDIEL_YELL     = 2,
    EVENT_SPELL_FROST_ARMOR         = 3,

    SAY_ARCANIST_BRAEDIN            = 0,
    SAY_MAGISTER_SURDIEL            = 0,

    NPC_ARCANIST_BRAEDIN            = 32169,
    NPC_MAGISTER_SURDIEL            = 32170,

    SPELL_FROST_ARMOR               = 12544
};

class npc_wg_queue : public CreatureScript
{
public:
    npc_wg_queue() : CreatureScript("npc_wg_queue") { }

    bool OnGossipHello(Player* player, Creature* creature) override
    {
        if (!sWorld->getBoolConfig(CONFIG_MINIGOB_MANABONK))
            return false;

        if (creature->IsQuestGiver())
            player->PrepareQuestMenu(creature->GetGUID());

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
            return true;

        if (wintergrasp->IsWarTime())
        {
            AddGossipItemFor(player, WG_GOSSIP_MENU_QUEUE, 0, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);
            SendGossipMenuFor(player, wintergrasp->GetDefenderTeam() ? WG_NPCQUEUE_TEXT_H_WAR : WG_NPCQUEUE_TEXT_A_WAR, creature->GetGUID());
        }
        else
        {
            uint32 timer = wintergrasp->GetTimer() / 1000;
            player->SendUpdateWorldState(WORLD_STATE_BATTLEFIELD_WG_CLOCK_TEXTS, GameTime::GetGameTime().count() + timer);
            if (timer < 15 * MINUTE)
            {
                AddGossipItemFor(player, WG_GOSSIP_MENU_QUEUE, 0, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF);
                SendGossipMenuFor(player, wintergrasp->GetDefenderTeam() ? WG_NPCQUEUE_TEXT_H_QUEUE : WG_NPCQUEUE_TEXT_A_QUEUE, creature->GetGUID());
            }
            else
                SendGossipMenuFor(player, wintergrasp->GetDefenderTeam() ? WG_NPCQUEUE_TEXT_H_NOWAR : WG_NPCQUEUE_TEXT_A_NOWAR, creature->GetGUID());
        }
        return true;
    }

    bool OnGossipSelect(Player* player, Creature* /*creature */, uint32 /*sender */, uint32 /*action*/) override
    {
        CloseGossipMenuFor(player);

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
            return true;

        if (wintergrasp->IsWarTime())
            wintergrasp->InvitePlayerToWar(player);
        else
        {
            uint32 timer = wintergrasp->GetTimer() / 1000;
            if (timer < 15 * MINUTE)
                wintergrasp->InvitePlayerToQueue(player);
        }
        return true;
    }

    struct npc_wg_queueAI : public ScriptedAI
    {
        npc_wg_queueAI(Creature* creature) : ScriptedAI(creature)
        {
            if (creature->GetEntry() == NPC_ARCANIST_BRAEDIN)
                events.ScheduleEvent(EVENT_ARCANIST_BRAEDIN_YELL, 0ms);
            else if (creature->GetEntry() == NPC_MAGISTER_SURDIEL)
                events.ScheduleEvent(EVENT_MAGISTER_SURDIEL_YELL, 0ms);

            events.ScheduleEvent(EVENT_SPELL_FROST_ARMOR, 0ms);
        }

        EventMap events;

        void UpdateAI(uint32 diff) override
        {
            if (sWorld->getIntConfig(CONFIG_WINTERGRASP_ENABLE) != 1)
                return;

            ScriptedAI::UpdateAI(diff);

            events.Update(diff);
            switch (events.ExecuteEvent())
            {
                case EVENT_ARCANIST_BRAEDIN_YELL:
                    if (Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG))
                    {
                        if (wintergrasp->IsWarTime())
                        {
                            Talk(SAY_ARCANIST_BRAEDIN);
                            events.ScheduleEvent(EVENT_ARCANIST_BRAEDIN_YELL, 4min);
                            break;
                        }
                    }
                    events.ScheduleEvent(EVENT_ARCANIST_BRAEDIN_YELL, 5s);
                    break;
                case EVENT_MAGISTER_SURDIEL_YELL:
                    if (Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG))
                    {
                        uint32 timer = wintergrasp->GetTimer() / 1000;
                        if (!wintergrasp->IsWarTime() && timer < 5 * MINUTE && timer > 4 * MINUTE)
                        {
                            Talk(SAY_MAGISTER_SURDIEL);
                            events.ScheduleEvent(EVENT_MAGISTER_SURDIEL_YELL, 5min);
                            break;
                        }
                    }
                    events.ScheduleEvent(EVENT_MAGISTER_SURDIEL_YELL, 5s);
                    break;
                case EVENT_SPELL_FROST_ARMOR:
                    me->CastSpell(me, SPELL_FROST_ARMOR, true);
                    events.ScheduleEvent(EVENT_SPELL_FROST_ARMOR, 15min);
                    break;
            }
        }
    };

    CreatureAI* GetAI(Creature* creature) const override
    {
        return new npc_wg_queueAI(creature);
    }
};

class npc_wg_quest_giver : public CreatureScript
{
public:
    npc_wg_quest_giver() : CreatureScript("npc_wg_quest_giver") { }

    bool OnGossipHello(Player* player, Creature* creature) override
    {
        if (creature->IsQuestGiver())
        {
            player->PrepareQuestMenu(creature->GetGUID());
        }

        if (creature->IsVendor())
        {
            AddGossipItemFor(player, GOSSIP_ICON_VENDOR, GOSSIP_TEXT_BROWSE_GOODS, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_TRADE);
        }

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
        {
            return true;
        }

        if (creature->IsQuestGiver())
        {
            QuestRelationBounds objectQR = sObjectMgr->GetCreatureQuestRelationBounds(creature->GetEntry());
            QuestRelationBounds objectQIR = sObjectMgr->GetCreatureQuestInvolvedRelationBounds(creature->GetEntry());

            QuestMenu& qm = player->PlayerTalkClass->GetQuestMenu();
            qm.ClearMenu();

            for (QuestRelations::const_iterator i = objectQIR.first; i != objectQIR.second; ++i)
            {
                uint32 quest_id = i->second;
                QuestStatus status = player->GetQuestStatus(quest_id);
                if (status == QUEST_STATUS_COMPLETE)
                    qm.AddMenuItem(quest_id, 4);
                else if (status == QUEST_STATUS_INCOMPLETE)
                    qm.AddMenuItem(quest_id, 4);
                //else if (status == QUEST_STATUS_AVAILABLE)
                //    qm.AddMenuItem(quest_id, 2);
            }

            // xinef: add att/def doubles if this quest is spawned
            std::vector<uint32> questRelationVector;
            for (QuestRelations::const_iterator i = objectQR.first; i != objectQR.second; ++i)
            {
                uint32 questId = i->second;
                Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
                if (!quest)
                    continue;

                switch (questId)
                {
                    case QUEST_BONES_AND_ARROWS_ALLIANCE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_BONES_AND_ARROWS_ALLIANCE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_BONES_AND_ARROWS_ALLIANCE_ATT);
                        break;
                    case QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_WARDING_THE_WARRIORS_ALLIANCE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT);
                        break;
                    case QUEST_A_RARE_HERB_ALLIANCE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_A_RARE_HERB_ALLIANCE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_A_RARE_HERB_ALLIANCE_ATT);
                        break;
                    case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT);
                        break;
                    case QUEST_BONES_AND_ARROWS_HORDE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_BONES_AND_ARROWS_HORDE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_BONES_AND_ARROWS_HORDE_ATT);
                        break;
                    case QUEST_JINXING_THE_WALLS_HORDE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_WARDING_THE_WALLS_HORDE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_JINXING_THE_WALLS_HORDE_ATT);
                        break;
                    case QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_FUELING_THE_DEMOLISHERS_HORDE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT);
                        break;
                    case QUEST_HEALING_WITH_ROSES_HORDE_ATT:
                        if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_HEALING_WITH_ROSES_HORDE_DEF))
                            continue;
                        questRelationVector.push_back(QUEST_HEALING_WITH_ROSES_HORDE_ATT);
                        break;
                    default:
                        questRelationVector.push_back(questId);
                        break;
                }
            }

            for (std::vector<uint32>::const_iterator i = questRelationVector.begin(); i != questRelationVector.end(); ++i)
            {
                uint32 questId = *i;
                Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
                switch (questId)
                {
                    // Horde attacker
                    case QUEST_BONES_AND_ARROWS_HORDE_ATT:
                    case QUEST_JINXING_THE_WALLS_HORDE_ATT:
                    case QUEST_SLAY_THEM_ALL_HORDE_ATT:
                    case QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT:
                    case QUEST_HEALING_WITH_ROSES_HORDE_ATT:
                    case QUEST_DEFEND_THE_SIEGE_HORDE_ATT:
                        if (wintergrasp->GetAttackerTeam() == TEAM_HORDE)
                        {
                            QuestStatus status = player->GetQuestStatus(questId);

                            if (quest->IsAutoComplete() && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 4);
                            else if (status == QUEST_STATUS_NONE && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 2);
                        }
                        break;
                    // Horde defender
                    case QUEST_BONES_AND_ARROWS_HORDE_DEF:
                    case QUEST_WARDING_THE_WALLS_HORDE_DEF:
                    case QUEST_SLAY_THEM_ALL_HORDE_DEF:
                    case QUEST_FUELING_THE_DEMOLISHERS_HORDE_DEF:
                    case QUEST_HEALING_WITH_ROSES_HORDE_DEF:
                    case QUEST_TOPPLING_THE_TOWERS_HORDE_DEF:
                    case QUEST_STOP_THE_SIEGE_HORDE_DEF:
                        if (wintergrasp->GetDefenderTeam() == TEAM_HORDE)
                        {
                            QuestStatus status = player->GetQuestStatus(questId);

                            if (quest->IsAutoComplete() && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 4);
                            else if (status == QUEST_STATUS_NONE && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 2);
                        }
                        break;
                    // Alliance attacker
                    case QUEST_BONES_AND_ARROWS_ALLIANCE_ATT:
                    case QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT:
                    case QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_ATT:
                    case QUEST_DEFEND_THE_SIEGE_ALLIANCE_ATT:
                    case QUEST_A_RARE_HERB_ALLIANCE_ATT:
                    case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT:
                        if (wintergrasp->GetAttackerTeam() == TEAM_ALLIANCE)
                        {
                            QuestStatus status = player->GetQuestStatus(questId);

                            if (quest->IsAutoComplete() && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 4);
                            else if (status == QUEST_STATUS_NONE && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 2);
                        }
                        break;
                    // Alliance defender
                    case QUEST_BONES_AND_ARROWS_ALLIANCE_DEF:
                    case QUEST_WARDING_THE_WARRIORS_ALLIANCE_DEF:
                    case QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_DEF:
                    case QUEST_SHOUTHERN_SABOTAGE_ALLIANCE_DEF:
                    case QUEST_STOP_THE_SIEGE_ALLIANCE_DEF:
                    case QUEST_A_RARE_HERB_ALLIANCE_DEF:
                    case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_DEF:
                        if (wintergrasp->GetDefenderTeam() == TEAM_ALLIANCE)
                        {
                            QuestStatus status = player->GetQuestStatus(questId);

                            if (quest->IsAutoComplete() && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 4);
                            else if (status == QUEST_STATUS_NONE && player->CanTakeQuest(quest, false))
                                qm.AddMenuItem(questId, 2);
                        }
                        break;
                    default:
                        QuestStatus status = player->GetQuestStatus(questId);

                        if (quest->IsAutoComplete() && player->CanTakeQuest(quest, false))
                            qm.AddMenuItem(questId, 4);
                        else if (status == QUEST_STATUS_NONE && player->CanTakeQuest(quest, false))
                            qm.AddMenuItem(questId, 2);
                        break;
                }
            }
        }
        SendGossipMenuFor(player, player->GetGossipTextId(creature), creature->GetGUID());
        return true;
    }

    uint32 GetDialogStatus(Player* player, Creature* creature) override
    {
        QuestRelationBounds qr = sObjectMgr->GetCreatureQuestRelationBounds(creature->GetEntry());
        QuestRelationBounds qir = sObjectMgr->GetCreatureQuestInvolvedRelationBounds(creature->GetEntry());
        QuestGiverStatus result = DIALOG_STATUS_NONE;

        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);

        for (QuestRelations::const_iterator i = qir.first; i != qir.second; ++i)
        {
            QuestGiverStatus result2 = DIALOG_STATUS_NONE;
            uint32 questId = i->second;
            Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
            if (!quest)
                continue;

            ConditionList conditions = sConditionMgr->GetConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_QUEST_AVAILABLE, quest->GetQuestId());
            if (!sConditionMgr->IsObjectMeetToConditions(player, conditions))
                continue;

            QuestStatus status = player->GetQuestStatus(questId);
            if ((status == QUEST_STATUS_COMPLETE && !player->GetQuestRewardStatus(questId)) ||
                    (quest->IsAutoComplete() && player->CanTakeQuest(quest, false)))
            {
                if (quest->IsAutoComplete() && quest->IsRepeatable() && !quest->IsDailyOrWeekly())
                    result2 = DIALOG_STATUS_REWARD_REP;
                else
                    result2 = DIALOG_STATUS_REWARD;
            }
            else if (status == QUEST_STATUS_INCOMPLETE)
                result2 = DIALOG_STATUS_INCOMPLETE;

            if (result2 > result)
                result = result2;
        }

        for (QuestRelations::const_iterator i = qr.first; i != qr.second; ++i)
        {
            QuestGiverStatus result2 = DIALOG_STATUS_NONE;
            uint32 questId = i->second;
            Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
            if (!quest)
                continue;

            ConditionList conditions = sConditionMgr->GetConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_QUEST_AVAILABLE, quest->GetQuestId());
            if (!sConditionMgr->IsObjectMeetToConditions(player, conditions))
                continue;

            switch (questId)
            {
                case QUEST_BONES_AND_ARROWS_ALLIANCE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_BONES_AND_ARROWS_ALLIANCE_DEF))
                        continue;
                    break;
                case QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_WARDING_THE_WARRIORS_ALLIANCE_DEF))
                        continue;
                    break;
                case QUEST_A_RARE_HERB_ALLIANCE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_A_RARE_HERB_ALLIANCE_DEF))
                        continue;
                    break;
                case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_DEF))
                        continue;
                    break;
                case QUEST_BONES_AND_ARROWS_HORDE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_BONES_AND_ARROWS_HORDE_DEF))
                        continue;
                    break;
                case QUEST_JINXING_THE_WALLS_HORDE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_WARDING_THE_WALLS_HORDE_DEF))
                        continue;
                    break;
                case QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_FUELING_THE_DEMOLISHERS_HORDE_DEF))
                        continue;
                    break;
                case QUEST_HEALING_WITH_ROSES_HORDE_ATT:
                    if (!sPoolMgr->IsSpawnedObject<Quest>(QUEST_HEALING_WITH_ROSES_HORDE_DEF))
                        continue;
                    break;
            }

            if (wintergrasp)
            {
                // Certain quests are only available when attacking / defending
                bool hasCorrectZoneControl = false;
                switch (questId)
                {
                    // Horde attacker
                    case QUEST_BONES_AND_ARROWS_HORDE_ATT:
                    case QUEST_JINXING_THE_WALLS_HORDE_ATT:
                    case QUEST_SLAY_THEM_ALL_HORDE_ATT:
                    case QUEST_FUELING_THE_DEMOLISHERS_HORDE_ATT:
                    case QUEST_HEALING_WITH_ROSES_HORDE_ATT:
                    case QUEST_DEFEND_THE_SIEGE_HORDE_ATT:
                        if (wintergrasp->GetAttackerTeam() == TEAM_HORDE)
                        {
                            hasCorrectZoneControl = true;
                        }
                        break;
                    // Horde defender
                    case QUEST_BONES_AND_ARROWS_HORDE_DEF:
                    case QUEST_WARDING_THE_WALLS_HORDE_DEF:
                    case QUEST_SLAY_THEM_ALL_HORDE_DEF:
                    case QUEST_FUELING_THE_DEMOLISHERS_HORDE_DEF:
                    case QUEST_HEALING_WITH_ROSES_HORDE_DEF:
                    case QUEST_TOPPLING_THE_TOWERS_HORDE_DEF:
                    case QUEST_STOP_THE_SIEGE_HORDE_DEF:
                        if (wintergrasp->GetDefenderTeam() == TEAM_HORDE)
                        {
                            hasCorrectZoneControl = true;
                        }
                        break;
                    // Alliance attacker
                    case QUEST_BONES_AND_ARROWS_ALLIANCE_ATT:
                    case QUEST_WARDING_THE_WARRIORS_ALLIANCE_ATT:
                    case QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_ATT:
                    case QUEST_DEFEND_THE_SIEGE_ALLIANCE_ATT:
                    case QUEST_A_RARE_HERB_ALLIANCE_ATT:
                    case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_ATT:
                        if (wintergrasp->GetAttackerTeam() == TEAM_ALLIANCE)
                        {
                            hasCorrectZoneControl = true;
                        }
                        break;
                    // Alliance defender
                    case QUEST_BONES_AND_ARROWS_ALLIANCE_DEF:
                    case QUEST_WARDING_THE_WARRIORS_ALLIANCE_DEF:
                    case QUEST_NO_MERCY_FOR_THE_MERCILESS_ALLIANCE_DEF:
                    case QUEST_SHOUTHERN_SABOTAGE_ALLIANCE_DEF:
                    case QUEST_STOP_THE_SIEGE_ALLIANCE_DEF:
                    case QUEST_A_RARE_HERB_ALLIANCE_DEF:
                    case QUEST_FUELING_THE_DEMOLISHERS_ALLIANCE_DEF:
                        if (wintergrasp->GetDefenderTeam() == TEAM_ALLIANCE)
                        {
                            hasCorrectZoneControl = true;
                        }
                        break;
                    default:
                        hasCorrectZoneControl = true;
                        break;
                }

                if (!hasCorrectZoneControl)
                {
                    continue;
                }
            }

            QuestStatus status = player->GetQuestStatus(questId);
            if (status == QUEST_STATUS_NONE)
            {
                if (player->CanSeeStartQuest(quest))
                {
                    if (player->SatisfyQuestLevel(quest, false))
                    {
                        if (quest->IsAutoComplete())
                            result2 = DIALOG_STATUS_REWARD_REP;
                        else if (player->GetLevel() <= (player->GetQuestLevel(quest) + sWorld->getIntConfig(CONFIG_QUEST_LOW_LEVEL_HIDE_DIFF)))
                        {
                            if (quest->IsDaily())
                                result2 = DIALOG_STATUS_AVAILABLE_REP;
                            else
                                result2 = DIALOG_STATUS_AVAILABLE;
                        }
                        else
                            result2 = DIALOG_STATUS_LOW_LEVEL_AVAILABLE;
                    }
                    else
                        result2 = DIALOG_STATUS_UNAVAILABLE;
                }
            }

            if (result2 > result)
                result = result2;
        }

        return result;
    }

    bool OnGossipSelect(Player* player, Creature* creature, uint32 /*sender*/, uint32 action) override
    {
        ClearGossipMenuFor(player);
        switch (action)
        {
            case GOSSIP_ACTION_TRADE:
                player->GetSession()->SendListInventory(creature->GetGUID());
                break;
        }

        return true;
    }
};

class npc_wg_siege_machine : public CreatureScript
{
public:
    npc_wg_siege_machine() : CreatureScript("npc_wg_siege_machine") { }

    struct npc_wg_siege_machineAI : public VehicleAI
    {
        npc_wg_siege_machineAI(Creature* creature) : VehicleAI(creature)
        {
            checkTimer = 0;
        }

        uint32 checkTimer;

        bool CanControlVehicle(Unit* passenger)
        {
            if (passenger->HasAura(SPELL_LIEUTENANT))
                return true;

            if (me->GetEntry() == NPC_WINTERGRASP_CATAPULT && passenger->HasAura(SPELL_CORPORAL))
                return true;

            return false;
        }

        void UpdateAI(uint32 diff) override
        {
            VehicleAI::UpdateAI(diff);

            checkTimer += diff;
            if (checkTimer >= 1000)
            {
                checkTimer = 0;
                if (me->GetVehicleKit())
                    for (SeatMap::iterator itr = me->GetVehicleKit()->Seats.begin(); itr != me->GetVehicleKit()->Seats.end(); ++itr)
                        if (const VehicleSeatEntry* seatInfo = itr->second.SeatInfo)
                            if (seatInfo->m_flags & VEHICLE_SEAT_FLAG_CAN_CONTROL)
                                if (Unit* passenger = ObjectAccessor::GetUnit(*me, itr->second.Passenger.Guid))
                                    if (!CanControlVehicle(passenger))
                                    {
                                        passenger->ExitVehicle();
                                        return;
                                    }
            }
        }
    };

    CreatureAI* GetAI(Creature* creature) const override
    {
        return new npc_wg_siege_machineAI(creature);
    }
};

////////////////////////////////////////////////
/////// GOs
////////////////////////////////////////////////

class go_wg_vehicle_teleporter : public GameObjectScript
{
public:
    go_wg_vehicle_teleporter() : GameObjectScript("go_wg_vehicle_teleporter") { }

    struct go_wg_vehicle_teleporterAI : public GameObjectAI
    {
        go_wg_vehicle_teleporterAI(GameObject* gameObject) : GameObjectAI(gameObject),
            _checkTimer(0)
        {
        }

        bool IsFriendly(Unit* passenger)
        {
            return ((me->GetUInt32Value(GAMEOBJECT_FACTION) == WintergraspFaction[TEAM_HORDE] && passenger->getRaceMask() & RACEMASK_HORDE) ||
                    (me->GetUInt32Value(GAMEOBJECT_FACTION) == WintergraspFaction[TEAM_ALLIANCE] && passenger->getRaceMask() & RACEMASK_ALLIANCE));
        }

        Creature* IsValidVehicle(Creature* cVeh)
        {
            if (!cVeh->HasAura(SPELL_VEHICLE_TELEPORT))
                if (Vehicle* vehicle = cVeh->GetVehicleKit())
                    if (Unit* passenger = vehicle->GetPassenger(0))
                        if (IsFriendly(passenger))
                            if (Creature* teleportTrigger = passenger->SummonTrigger(me->GetPositionX() - 60.0f, me->GetPositionY(), me->GetPositionZ() + 1.0f, cVeh->GetOrientation(), 1000))
                                return teleportTrigger;

            return nullptr;
        }

        void UpdateAI(uint32 diff) override
        {
            _checkTimer += diff;
            if (_checkTimer >= 1000)
            {
                for (uint8 i = 0; i < MAX_WINTERGRASP_VEHICLES; i++)
                    if (Creature* vehicleCreature = me->FindNearestCreature(vehiclesList[i], 3.0f, true))
                        if (Creature* teleportTrigger = IsValidVehicle(vehicleCreature))
                            teleportTrigger->CastSpell(vehicleCreature, SPELL_VEHICLE_TELEPORT, true);

                _checkTimer = 0;
            }
        }
    private:
        uint32 _checkTimer;
    };

    GameObjectAI* GetAI(GameObject* go) const override
    {
        return new go_wg_vehicle_teleporterAI(go);
    }
};

////////////////////////////////////////////////
/////// SPELLs
////////////////////////////////////////////////

/* 56662, 61409 - Build Siege Vehicle (Force)
   56664 - Build Catapult (Force)
   56659 - Build Demolisher (Force) */
class spell_wintergrasp_force_building : public SpellScript
{
    PrepareSpellScript(spell_wintergrasp_force_building);

    bool Validate(SpellInfo const* /*spell*/) override
    {
        return ValidateSpellInfo(
            {
                SPELL_BUILD_CATAPULT_FORCE,
                SPELL_BUILD_DEMOLISHER_FORCE,
                SPELL_BUILD_SIEGE_VEHICLE_FORCE_HORDE,
                SPELL_BUILD_SIEGE_VEHICLE_FORCE_ALLIANCE
            });
    }

    void HandleScript(SpellEffIndex effIndex)
    {
        PreventHitDefaultEffect(effIndex);
        if (Unit* target = GetHitUnit())
            target->CastSpell(target, GetEffectValue(), false, nullptr, nullptr, target->GetGUID());
    }

    void Register() override
    {
        OnEffectHitTarget += SpellEffectFn(spell_wintergrasp_force_building::HandleScript, EFFECT_0, SPELL_EFFECT_SCRIPT_EFFECT);
    }
};

/* 56661, 61408 - Build Siege Engine
   56663 - Build Catapult
   56575 - Build Demolisher */
class spell_wintergrasp_create_vehicle : public SpellScript
{
    PrepareSpellScript(spell_wintergrasp_create_vehicle);

    void HandleSummon(SpellEffIndex effIndex)
    {
        PreventHitEffect(effIndex);

        if (Unit* caster = GetCaster())
        {
            Unit* originalCaster = GetOriginalCaster();
            if (!originalCaster)
            {
                return;
            }

            SummonPropertiesEntry const* properties = sSummonPropertiesStore.LookupEntry(GetSpellInfo()->Effects[effIndex].MiscValueB);
            if (!properties)
            {
                return;
            }

            uint32 entry = GetSpellInfo()->Effects[effIndex].MiscValue;
            int32 duration = GetSpellInfo()->GetDuration();
            if (TempSummon* summon = caster->GetMap()->SummonCreature(entry, *GetHitDest(), properties, duration, originalCaster, GetSpellInfo()->Id))
            {
                if (summon->IsInMap(caster))
                {
                    summon->SetCreatorGUID(originalCaster->GetGUID());
                    summon->HandleSpellClick(caster);
                }
            }
        }
    }

    void Register() override
    {
        OnEffectHit += SpellEffectFn(spell_wintergrasp_create_vehicle::HandleSummon, EFFECT_1, SPELL_EFFECT_SUMMON);
    }
};

// 49761 - Rocket-Propelled Goblin Grenade
class spell_wintergrasp_rp_gg : public SpellScript
{
    PrepareSpellScript(spell_wintergrasp_rp_gg);

    bool Validate(SpellInfo const* /*spellInfo*/) override
    {
        return ValidateSpellInfo({ SPELL_RP_GG_TRIGGER_MISSILE });
    }

    bool handled;
    bool Load() override
    {
        handled = false;
        return true;
    }

    void HandleFinish()
    {
        if (!GetExplTargetDest())
            return;

        GetCaster()->CastSpell(GetExplTargetDest()->GetPositionX(), GetExplTargetDest()->GetPositionY(), GetExplTargetDest()->GetPositionZ(), SPELL_RP_GG_TRIGGER_MISSILE, true);
    }

    void Register() override
    {
        AfterCast += SpellCastFn(spell_wintergrasp_rp_gg::HandleFinish);
    }
};

// 58622 - Teleport to Lake Wintergrasp
class spell_wintergrasp_portal : public SpellScript
{
    PrepareSpellScript(spell_wintergrasp_portal);

    bool Validate(SpellInfo const* /*spellInfo*/) override
    {
        return ValidateSpellInfo({ SPELL_TELEPORT_TO_FORTRESS });
    }

    void HandleScript(SpellEffIndex effIndex)
    {
        PreventHitDefaultEffect(effIndex);
        Player* target = GetHitPlayer();
        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp || !target || target->GetLevel() < 75 || (wintergrasp->GetDefenderTeam() != target->GetTeamId()))
            return;

        target->CastSpell(target, SPELL_TELEPORT_TO_FORTRESS, true);
    }

    void Register() override
    {
        OnEffectHitTarget += SpellEffectFn(spell_wintergrasp_portal::HandleScript, EFFECT_0, SPELL_EFFECT_SCRIPT_EFFECT);
    }
};

// 36444 - Wintergrasp Water
class spell_wintergrasp_water : public SpellScript
{
    PrepareSpellScript(spell_wintergrasp_water);

    SpellCastResult CheckCast()
    {
        Unit* target = GetCaster();
        if (!target || !target->IsVehicle())
            return SPELL_FAILED_DONT_REPORT;

        return SPELL_CAST_OK;
    }

    void Register() override
    {
        OnCheckCast += SpellCheckCastFn(spell_wintergrasp_water::CheckCast);
    }
};

// 52107 - (Spell not exist in DBC)
class spell_wintergrasp_hide_small_elementals_aura : public AuraScript
{
    PrepareAuraScript(spell_wintergrasp_hide_small_elementals_aura);

    void HandlePeriodicDummy(AuraEffect const*  /*aurEff*/)
    {
        Unit* target = GetTarget();
        Battlefield* Bf = sBattlefieldMgr->GetBattlefieldToZoneId(target->GetZoneId());
        bool enable = !Bf || !Bf->IsWarTime();
        target->SetPhaseMask(enable ? 1 : 512, true);
        PreventDefaultAction();
    }

    void Register() override
    {
        OnEffectPeriodic += AuraEffectPeriodicFn(spell_wintergrasp_hide_small_elementals_aura::HandlePeriodicDummy, EFFECT_0, SPELL_AURA_PERIODIC_DUMMY);
    }
};

/* 57610, 51422 - Cannon
   50999 - Boulder
   57607 - Plague Slime */
class spell_wg_reduce_damage_by_distance : public SpellScript
{
    PrepareSpellScript(spell_wg_reduce_damage_by_distance);

    void RecalculateDamage()
    {
        if (!GetExplTargetDest() || !GetHitUnit())
            return;

        float maxDistance = GetSpellInfo()->Effects[EFFECT_0].CalcRadius(GetCaster()); // Xinef: always stored in EFFECT_0
        float distance = std::min<float>(GetHitUnit()->GetDistance(*GetExplTargetDest()), maxDistance);

        int32 damage = std::max<int32>(0, int32(GetHitDamage() - std::floor(GetHitDamage() * (distance / maxDistance))));
        SetHitDamage(damage);
    }

    void Register() override
    {
        OnHit += SpellHitFn(spell_wg_reduce_damage_by_distance::RecalculateDamage);
    }
};

////////////////////////////////////////////////
/////// ACHIEVEMENTs
////////////////////////////////////////////////

class achievement_wg_didnt_stand_a_chance : public AchievementCriteriaScript
{
public:
    achievement_wg_didnt_stand_a_chance() : AchievementCriteriaScript("achievement_wg_didnt_stand_a_chance") { }

    bool OnCheck(Player* source, Unit* target, uint32 /*criteria_id*/) override
    {
        if (!target)
            return false;

        if (Player* victim = target->ToPlayer())
        {
            if (!victim->IsMounted())
                return false;

            if (Vehicle* vehicle = source->GetVehicle())
                if (vehicle->GetVehicleInfo()->m_ID == 244) // Wintergrasp Tower Cannon
                    return true;
        }

        return false;
    }
};

class achievement_wg_vehicular_gnomeslaughter : public AchievementCriteriaScript
{
public:
    achievement_wg_vehicular_gnomeslaughter() : AchievementCriteriaScript("achievement_wg_vehicular_gnomeslaughter") { }

    bool OnCheck(Player* source, Unit* target, uint32 /*criteria_id*/) override
    {
        if (!target)
            return false;

        if (Unit* vehicle = source->GetVehicleBase())
            if (vehicle->GetEntry() == NPC_WINTERGRASP_SIEGE_ENGINE_ALLIANCE || vehicle->GetEntry() == NPC_WINTERGRASP_SIEGE_ENGINE_HORDE ||
                    vehicle->GetEntry() == NPC_WINTERGRASP_CATAPULT || vehicle->GetEntry() == NPC_WINTERGRASP_DEMOLISHER ||
                    vehicle->GetEntry() == NPC_WINTERGRASP_TOWER_CANNON)
                return true;

        return false;
    }
};

class achievement_wg_within_our_grasp : public AchievementCriteriaScript
{
public:
    achievement_wg_within_our_grasp() : AchievementCriteriaScript("achievement_wg_within_our_grasp") { }

    bool OnCheck(Player*  /*source*/, Unit*  /*target*/, uint32 /*criteria_id*/) override
    {
        Battlefield* wintergrasp = sBattlefieldMgr->GetBattlefieldByBattleId(BATTLEFIELD_BATTLEID_WG);
        if (!wintergrasp)
            return false;

        return wintergrasp->GetTimer() >= (20 * MINUTE * IN_MILLISECONDS);
    }
};

void AddSC_wintergrasp()
{
    // NPCs
    new npc_wg_queue();
    new npc_wg_spirit_guide();
    new npc_wg_demolisher_engineer();
    new npc_wg_quest_giver();
    new npc_wg_siege_machine();

    // GOs
    new go_wg_vehicle_teleporter();

    // SPELLs
    RegisterSpellScript(spell_wintergrasp_force_building);
    RegisterSpellScript(spell_wintergrasp_create_vehicle);
    RegisterSpellScript(spell_wintergrasp_rp_gg);
    RegisterSpellScript(spell_wintergrasp_portal);
    RegisterSpellScript(spell_wintergrasp_water);
    RegisterSpellScript(spell_wintergrasp_hide_small_elementals_aura);
    RegisterSpellScript(spell_wg_reduce_damage_by_distance);

    // ACHIEVEMENTs
    new achievement_wg_didnt_stand_a_chance();
    new achievement_wg_vehicular_gnomeslaughter();
    new achievement_wg_within_our_grasp();
}