summaryrefslogtreecommitdiff
path: root/src/server/scripts/Commands/cs_go.cpp
blob: 0e6bca42205b8df13bf1710ad556ee9275283122 (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
/*
 * 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 Affero General Public License as published by the
 * Free Software Foundation; either version 3 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 Affero 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 "Chat.h"
#include "CommandScript.h"
#include "GameGraveyard.h"
#include "Language.h"
#include "MapMgr.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "TicketMgr.h"

#include "boost/algorithm/string.hpp"
#include <regex>

using namespace Acore::ChatCommands;

class go_commandscript : public CommandScript
{
public:
    go_commandscript() : CommandScript("go_commandscript") { }

    ChatCommandTable GetCommands() const override
    {
        static ChatCommandTable goCommandTable =
        {
            { "creature",      HandleGoCreatureSpawnIdCommand,   SEC_MODERATOR,  Console::No },
            { "creature id",   HandleGoCreatureCIdCommand,       SEC_MODERATOR,  Console::No },
            { "creature name", HandleGoCreatureNameCommand,      SEC_MODERATOR,  Console::No },
            { "gameobject",    HandleGoGameObjectSpawnIdCommand, SEC_MODERATOR,  Console::No },
            { "gameobject id", HandleGoGameObjectGOIdCommand,    SEC_MODERATOR,  Console::No },
            { "graveyard",     HandleGoGraveyardCommand,         SEC_MODERATOR,  Console::No },
            { "grid",          HandleGoGridCommand,              SEC_MODERATOR,  Console::No },
            { "taxinode",      HandleGoTaxinodeCommand,          SEC_MODERATOR,  Console::No },
            { "trigger",       HandleGoTriggerCommand,           SEC_MODERATOR,  Console::No },
            { "zonexy",        HandleGoZoneXYCommand,            SEC_MODERATOR,  Console::No },
            { "xyz",           HandleGoXYZCommand,               SEC_MODERATOR,  Console::No },
            { "ticket",        HandleGoTicketCommand,            SEC_GAMEMASTER, Console::No },
            { "quest",         HandleGoQuestCommand,             SEC_MODERATOR,  Console::No },
        };

        static ChatCommandTable commandTable =
        {
            { "go", goCommandTable }
        };
        return commandTable;
    }

    static bool DoTeleport(ChatHandler* handler, Position pos, uint32 mapId = MAPID_INVALID)
    {
        Player* player = handler->GetSession()->GetPlayer();

        if (mapId == MAPID_INVALID)
            mapId = player->GetMapId();
        if (!MapMgr::IsValidMapCoord(mapId, pos) || sObjectMgr->IsTransportMap(mapId))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, pos.GetPositionX(), pos.GetPositionY(), mapId);
            return false;
        }

        // stop flight if need
        if (player->IsInFlight())
        {
            player->GetMotionMaster()->MovementExpired();
            player->CleanupAfterTaxiFlight();
        }
        // save only in non-flight case
        else
            player->SaveRecallPosition();

        player->TeleportTo({ mapId, pos });
        return true;
    }

    static bool HandleGoCreatureCIdCommand(ChatHandler* handler, Variant<Hyperlink<creature_entry>, uint32> cId, Optional<uint32> _pos)
    {
        uint32 pos = 1;
        if (_pos)
        {
            pos = *_pos;
            if (pos < 1)
            {
                handler->SendErrorMessage(LANG_COMMAND_FACTION_INVPARAM, pos);
                return false;
            }
        }

        std::vector<CreatureData const*> spawnpoints = GetCreatureDataList(*cId);

        if (spawnpoints.empty())
        {
            handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
            return false;
        }

        if (spawnpoints.size() < pos)
        {
            handler->SendErrorMessage(LANG_COMMAND_GONOTENOUGHSPAWNS, pos, spawnpoints.size());
            return false;
        }

        CreatureData const* spawnpoint = spawnpoints[--pos];

        return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
    }

    static bool HandleGoCreatureSpawnIdCommand(ChatHandler* handler, Variant<Hyperlink<creature>, ObjectGuid::LowType> spawnId)
    {
        CreatureData const* spawnpoint = sObjectMgr->GetCreatureData(spawnId);
        if (!spawnpoint)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
            return false;
        }

        return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
    }

    static bool HandleGoCreatureNameCommand(ChatHandler* handler, Tail name)
    {
        if (!name.data())
            return false;

        // Make sure we don't pass double quotes into the SQL query. Otherwise it causes a MySQL error
        std::string str = name.data(); // Making subtractions to the last character does not with in string_view
        if (str.front() == '"')
            str = str.substr(1);
        if (str.back() == '"')
            str = str.substr(0, str.size() - 1);

        QueryResult result = WorldDatabase.Query("SELECT entry FROM creature_template WHERE name = \"{}\" LIMIT 1", str);
        if (!result)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
            return false;
        }

        uint32 entry = result->Fetch()[0].Get<uint32>();
        CreatureData const* spawnpoint = GetCreatureData(handler, entry);
        if (!spawnpoint)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
            return false;
        }

        return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
    }

    static bool HandleGoGameObjectSpawnIdCommand(ChatHandler* handler, uint32 spawnId)
    {
        GameObjectData const* spawnpoint = sObjectMgr->GetGameObjectData(spawnId);
        if (!spawnpoint)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOOBJNOTFOUND);
            return false;
        }

        return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
    }

    static bool HandleGoGameObjectGOIdCommand(ChatHandler* handler, uint32 goId, Optional<uint32> _pos)
    {
        uint32 pos = 1;
        if (_pos)
        {
            pos = *_pos;
            if (pos < 1)
            {
                handler->SendErrorMessage(LANG_COMMAND_FACTION_INVPARAM, pos);
                return false;
            }
        }

        std::vector<GameObjectData const*> spawnpoints = GetGameObjectDataList(goId);

        if (spawnpoints.empty())
        {
            handler->SendErrorMessage(LANG_COMMAND_GOOBJNOTFOUND);
            return false;
        }

        if (spawnpoints.size() < pos)
        {
            handler->SendErrorMessage(LANG_COMMAND_GONOTENOUGHSPAWNS, pos, spawnpoints.size());
            return false;
        }

        GameObjectData const* spawnpoint = spawnpoints[--pos];

        return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
    }

    static bool HandleGoGraveyardCommand(ChatHandler* handler, uint32 gyId)
    {
        GraveyardStruct const* gy = sGraveyard->GetGraveyard(gyId);
        if (!gy)
        {
            handler->SendErrorMessage(LANG_COMMAND_GRAVEYARDNOEXIST, gyId);
            return false;
        }

        if (!MapMgr::IsValidMapCoord(gy->Map, gy->x, gy->y, gy->z))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, gy->x, gy->y, gy->Map);
            return false;
        }

        Player* player = handler->GetSession()->GetPlayer();
        // stop flight if need
        if (player->IsInFlight())
        {
            player->GetMotionMaster()->MovementExpired();
            player->CleanupAfterTaxiFlight();
        }
        // save only in non-flight case
        else
            player->SaveRecallPosition();

        player->TeleportTo(gy->Map, gy->x, gy->y, gy->z, player->GetOrientation());
        return true;
    }

    //teleport to grid
    static bool HandleGoGridCommand(ChatHandler* handler, float gridX, float gridY, Optional<uint32> oMapId)
    {
        Player* player = handler->GetSession()->GetPlayer();
        uint32 mapId = oMapId.value_or(player->GetMapId());

        // center of grid
        float x = (gridX - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
        float y = (gridY - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;

        if (!MapMgr::IsValidMapCoord(mapId, x, y))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
            return false;
        }

        // stop flight if need
        if (player->IsInFlight())
        {
            player->GetMotionMaster()->MovementExpired();
            player->CleanupAfterTaxiFlight();
        }
        // save only in non-flight case
        else
            player->SaveRecallPosition();

        Map const* map = sMapMgr->CreateBaseMap(mapId);
        float z = std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));

        player->TeleportTo(mapId, x, y, z, player->GetOrientation());
        return true;
    }

    static bool HandleGoTaxinodeCommand(ChatHandler* handler, Variant<Hyperlink<taxinode>, uint32> nodeId)
    {
        TaxiNodesEntry const* node = sTaxiNodesStore.LookupEntry(nodeId);
        if (!node)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOTAXINODENOTFOUND, uint32(nodeId));
            return false;
        }
        return DoTeleport(handler, { node->x, node->y, node->z }, node->map_id);
    }

    static bool HandleGoTriggerCommand(ChatHandler* handler, Variant<Hyperlink<areatrigger>, uint32> areaTriggerId)
    {
        AreaTrigger const* at = sObjectMgr->GetAreaTrigger(areaTriggerId);
        if (!at)
        {
            handler->SendErrorMessage(LANG_COMMAND_GOAREATRNOTFOUND, uint32(areaTriggerId));
            return false;
        }
        return DoTeleport(handler, { at->x, at->y, at->z }, at->map);
    }

    //teleport at coordinates
    static bool HandleGoZoneXYCommand(ChatHandler* handler, float x, float y, Optional<Variant<Hyperlink<area>, uint32>> areaIdArg)
    {
        Player* player = handler->GetSession()->GetPlayer();

        uint32 areaId = areaIdArg ? *areaIdArg : player->GetZoneId();

        AreaTableEntry const* areaEntry = sAreaTableStore.LookupEntry(areaId);

        if (x < 0 || x > 100 || y < 0 || y > 100 || !areaEntry)
        {
            handler->SendErrorMessage(LANG_INVALID_ZONE_COORD, x, y, areaId);
            return false;
        }

        // update to parent zone if exist (client map show only zones without parents)
        AreaTableEntry const* zoneEntry = areaEntry->zone ? sAreaTableStore.LookupEntry(areaEntry->zone) : areaEntry;
                ASSERT(zoneEntry);

        Map const* map = sMapMgr->CreateBaseMap(zoneEntry->mapid);

        if (map->Instanceable())
        {
            handler->SendErrorMessage(LANG_INVALID_ZONE_MAP, areaEntry->ID, areaEntry->area_name[handler->GetSessionDbcLocale()], map->GetId(), map->GetMapName());
            return false;
        }

        Zone2MapCoordinates(x, y, zoneEntry->ID);

        if (!MapMgr::IsValidMapCoord(zoneEntry->mapid, x, y))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, x, y, zoneEntry->mapid);
            return false;
        }

        // stop flight if need
        if (player->IsInFlight())
        {
            player->GetMotionMaster()->MovementExpired();
            player->CleanupAfterTaxiFlight();
        }
        // save only in non-flight case
        else
            player->SaveRecallPosition();

        float z = std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));

        player->TeleportTo(zoneEntry->mapid, x, y, z, player->GetOrientation());
        return true;
    }

    /**
     * @brief Teleports the GM to the specified world coordinates, optionally specifying map ID and orientation.
     *
     * @param handler The ChatHandler that is handling the command.
     * @param args The coordinates to teleport to in format "x y z [mapId [orientation]]".
     * @return true The command was successful.
     * @return false The command was unsuccessful (show error or syntax)
     */
    static bool HandleGoXYZCommand(ChatHandler* handler, Tail args)
    {
        std::wstring wInputCoords;
        if (!Utf8toWStr(args, wInputCoords))
        {
            return false;
        }

        // extract float and integer values from the input
        std::vector<float> locationValues;
        std::wregex floatRegex(L"(-?\\d+(?:\\.\\d+)?)");
        std::wsregex_iterator floatRegexIterator(wInputCoords.begin(), wInputCoords.end(), floatRegex);
        std::wsregex_iterator end;
        while (floatRegexIterator != end)
        {
            std::wsmatch match = *floatRegexIterator;
            std::wstring matchStr = match.str();

            // try to convert the match to a float
            try
            {
                locationValues.push_back(std::stof(matchStr));
            }
            // if the match is not a float, do not add it to the vector
            catch (std::invalid_argument const&){}

            ++floatRegexIterator;
        }

        // X and Y are required
        if (locationValues.size() < 2)
        {
            return false;
        }

        Player* player = handler->GetSession()->GetPlayer();

        uint32 mapId = locationValues.size() >= 4 ? uint32(locationValues[3]) : player->GetMapId();

        float x = locationValues[0];
        float y = locationValues[1];

        if (!sMapStore.LookupEntry(mapId) || !MapMgr::IsValidMapCoord(mapId, x, y))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
            return false;
        }

        Map const* map = sMapMgr->CreateBaseMap(mapId);

        float z = locationValues.size() >= 3 ? locationValues[2] : std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));
        // map ID (locationValues[3]) already handled above
        float o = locationValues.size() >= 5 ? locationValues[4] : player->GetOrientation();

        if (!MapMgr::IsValidMapCoord(mapId, x, y, z, o))
        {
            handler->SendErrorMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
            return false;
        }

        return DoTeleport(handler, { x, y, z, o }, mapId);
    }

    static bool HandleGoTicketCommand(ChatHandler* handler, uint32 ticketId)
    {
        GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
        if (!ticket)
        {
            handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
            return true;
        }

        Player* player = handler->GetSession()->GetPlayer();

        // stop flight if need
        if (player->IsInFlight())
        {
            player->GetMotionMaster()->MovementExpired();
            player->CleanupAfterTaxiFlight();
        }
        // save only in non-flight case
        else
            player->SaveRecallPosition();

        ticket->TeleportTo(player);
        return true;
    }

    static bool HandleGoQuestCommand(ChatHandler* handler, std::string_view type, Quest const* quest)
    {
        uint32 entry = quest->GetQuestId();

        if (type == "starter")
        {
            QuestRelations* qr = sObjectMgr->GetCreatureQuestRelationMap();

            for (auto itr = qr->begin(); itr != qr->end(); ++itr)
            {
                if (itr->second == entry)
                {
                    CreatureData const* spawnpoint = GetCreatureData(handler, itr->first);
                    if (!spawnpoint)
                    {
                        handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
                        return false;
                    }

                    // We've found a creature, teleport to it.
                    return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
                }
            }

            qr = sObjectMgr->GetGOQuestRelationMap();

            for (auto itr = qr->begin(); itr != qr->end(); ++itr)
            {
                if (itr->second == entry)
                {
                    GameObjectData const* spawnpoint = GetGameObjectData(handler, itr->first);
                    if (!spawnpoint)
                    {
                        handler->SendErrorMessage(LANG_COMMAND_GOOBJNOTFOUND);
                        return false;
                    }

                    return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
                }
            }
        }
        else if (type == "ender")
        {
            QuestRelations* qr = sObjectMgr->GetCreatureQuestInvolvedRelationMap();

            for (auto itr = qr->begin(); itr != qr->end(); ++itr)
            {
                if (itr->second == entry)
                {
                    CreatureData const* spawnpoint = GetCreatureData(handler, itr->first);
                    if (!spawnpoint)
                    {
                        handler->SendErrorMessage(LANG_COMMAND_GOCREATNOTFOUND);
                        return false;
                    }

                    // We've found a creature, teleport to it.
                    return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
                }
            }

            qr = sObjectMgr->GetGOQuestInvolvedRelationMap();

            for (auto itr = qr->begin(); itr != qr->end(); ++itr)
            {
                if (itr->second == entry)
                {
                    GameObjectData const* spawnpoint = GetGameObjectData(handler, itr->first);
                    if (!spawnpoint)
                    {
                        handler->SendErrorMessage(LANG_COMMAND_GOOBJNOTFOUND);
                        return false;
                    }

                    return DoTeleport(handler, { spawnpoint->posX, spawnpoint->posY, spawnpoint->posZ }, spawnpoint->mapid);
                }
            }
        }
        else
        {
            handler->SendErrorMessage(LANG_CMD_GOQUEST_INVALID_SYNTAX);
            return false;
        }

        return false;
    }

    static CreatureData const* GetCreatureData(ChatHandler* handler, uint32 entry)
    {
        CreatureData const* spawnpoint = nullptr;
        for (auto const& pair : sObjectMgr->GetAllCreatureData())
        {
            if (pair.second.id1 != entry)
            {
                continue;
            }

            if (!spawnpoint)
            {
                spawnpoint = &pair.second;
            }
            else
            {
                handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
                break;
            }
        }

        return spawnpoint;
    }

    static std::vector<CreatureData const*> GetCreatureDataList(uint32 entry)
    {
        std::vector<CreatureData const*> spawnpoints;
        for (auto const& pair : sObjectMgr->GetAllCreatureData())
        {
            if (pair.second.id1 != entry)
            {
                continue;
            }

            spawnpoints.emplace_back(&pair.second);
        }

        return spawnpoints;
    }

    static GameObjectData const* GetGameObjectData(ChatHandler* handler, uint32 entry)
    {
        GameObjectData const* spawnpoint = nullptr;
        for (auto const& pair : sObjectMgr->GetAllGOData())
        {
            if (pair.second.id != entry)
            {
                continue;
            }

            if (!spawnpoint)
            {
                spawnpoint = &pair.second;
            }
            else
            {
                handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
                break;
            }
        }

        return spawnpoint;
    }

    static std::vector<GameObjectData const*> GetGameObjectDataList(uint32 entry)
    {
        std::vector<GameObjectData const*> spawnpoints;
        for (auto const& pair : sObjectMgr->GetAllGOData())
        {
            if (pair.second.id != entry)
            {
                continue;
            }

            spawnpoints.emplace_back(&pair.second);
        }

        return spawnpoints;
    }
};

void AddSC_go_commandscript()
{
    new go_commandscript();
}