Advertisement
Guest User

cs_go.cpp

a guest
Sep 27th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.27 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2008-2013 TrinityCore <http://www.trinitycore.org/>
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU General Public License as published by the
  6.  * Free Software Foundation; either version 2 of the License, or (at your
  7.  * option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but WITHOUT
  10.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11.  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12.  * more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. /* ScriptData
  19. Name: go_commandscript
  20. %Complete: 100
  21. Comment: All go related commands
  22. Category: commandscripts
  23. EndScriptData */
  24.  
  25. #include "ScriptMgr.h"
  26. #include "ObjectMgr.h"
  27. #include "MapManager.h"
  28. #include "TicketMgr.h"
  29. #include "Chat.h"
  30. #include "Language.h"
  31. #include "Player.h"
  32.  
  33. class go_commandscript : public CommandScript
  34. {
  35. public:
  36.     go_commandscript() : CommandScript("go_commandscript") { }
  37.  
  38.     ChatCommand* GetCommands() const OVERRIDE
  39.     {
  40.         static ChatCommand goCommandTable[] =
  41.         {
  42.             { "creature",       SEC_MODERATOR,      false, &HandleGoCreatureCommand,          "", NULL },
  43.             { "graveyard",      SEC_MODERATOR,      false, &HandleGoGraveyardCommand,         "", NULL },
  44.             { "grid",           SEC_MODERATOR,      false, &HandleGoGridCommand,              "", NULL },
  45.             { "object",         SEC_MODERATOR,      false, &HandleGoObjectCommand,            "", NULL },
  46.             { "taxinode",       SEC_MODERATOR,      false, &HandleGoTaxinodeCommand,          "", NULL },
  47.             { "trigger",        SEC_MODERATOR,      false, &HandleGoTriggerCommand,           "", NULL },
  48.             { "zonexy",         SEC_MODERATOR,      false, &HandleGoZoneXYCommand,            "", NULL },
  49.             { "xyz",            SEC_MODERATOR,      false, &HandleGoXYZCommand,               "", NULL },
  50.             { "ticket",         SEC_MODERATOR,      false, &HandleGoTicketCommand,            "", NULL },
  51.             { "",               SEC_MODERATOR,      false, &HandleGoXYZCommand,               "", NULL },
  52.             { NULL,             0,                  false, NULL,                              "", NULL }
  53.         };
  54.  
  55.         static ChatCommand commandTable[] =
  56.         {
  57.             { "go",             SEC_MODERATOR,      false, NULL,                     "", goCommandTable },
  58.             { NULL,             0,                  false, NULL,                               "", NULL }
  59.         };
  60.         return commandTable;
  61.     }
  62.  
  63.     /** \brief Teleport the GM to the specified creature
  64.     *
  65.     * .gocreature <GUID>      --> TP using creature.guid
  66.     * .gocreature azuregos    --> TP player to the mob with this name
  67.     *                             Warning: If there is more than one mob with this name
  68.     *                                      you will be teleported to the first one that is found.
  69.     * .gocreature id 6109     --> TP player to the mob, that has this creature_template.entry
  70.     *                             Warning: If there is more than one mob with this "id"
  71.     *                                      you will be teleported to the first one that is found.
  72.     */
  73.     //teleport to creature
  74.     static bool HandleGoCreatureCommand(ChatHandler* handler, char const* args)
  75.     {
  76.         if (!*args)
  77.             return false;
  78.  
  79.         Player* player = handler->GetSession()->GetPlayer();
  80.  
  81.         // "id" or number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
  82.         char* param1 = handler->extractKeyFromLink((char*)args, "Hcreature");
  83.         if (!param1)
  84.             return false;
  85.  
  86.         std::ostringstream whereClause;
  87.  
  88.         // User wants to teleport to the NPC's template entry
  89.         if (strcmp(param1, "id") == 0)
  90.         {
  91.             // Get the "creature_template.entry"
  92.             // number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
  93.             char* tail = strtok(NULL, "");
  94.             if (!tail)
  95.                 return false;
  96.             char* id = handler->extractKeyFromLink(tail, "Hcreature_entry");
  97.             if (!id)
  98.                 return false;
  99.  
  100.             int32 entry = atoi(id);
  101.             if (!entry)
  102.                 return false;
  103.  
  104.             whereClause << "WHERE id = '" << entry << '\'';
  105.         }
  106.         else
  107.         {
  108.             int32 guid = atoi(param1);
  109.  
  110.             // Number is invalid - maybe the user specified the mob's name
  111.             if (!guid)
  112.             {
  113.                 std::string name = param1;
  114.                 WorldDatabase.EscapeString(name);
  115.                 whereClause << ", creature_template WHERE creature.id = creature_template.entry AND creature_template.name "_LIKE_" '" << name << '\'';
  116.             }
  117.             else
  118.                 whereClause <<  "WHERE guid = '" << guid << '\'';
  119.         }
  120.  
  121.         QueryResult result = WorldDatabase.PQuery("SELECT position_x, position_y, position_z, orientation, map, guid, id FROM creature %s", whereClause.str().c_str());
  122.         if (!result)
  123.         {
  124.             handler->SendSysMessage(LANG_COMMAND_GOCREATNOTFOUND);
  125.             handler->SetSentErrorMessage(true);
  126.             return false;
  127.         }
  128.         if (result->GetRowCount() > 1)
  129.             handler->SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
  130.  
  131.         Field* fields = result->Fetch();
  132.         float x = fields[0].GetFloat();
  133.         float y = fields[1].GetFloat();
  134.         float z = fields[2].GetFloat();
  135.         float ort = fields[3].GetFloat();
  136.         int mapId = fields[4].GetUInt16();
  137.         uint32 guid = fields[5].GetUInt32();
  138.         uint32 id = fields[6].GetUInt32();
  139.  
  140.         // if creature is in same map with caster go at its current location
  141.         if (Creature* creature = sObjectAccessor->GetCreature(*player, MAKE_NEW_GUID(guid, id, HIGHGUID_UNIT)))
  142.         {
  143.             x = creature->GetPositionX();
  144.             y = creature->GetPositionY();
  145.             z = creature->GetPositionZ();
  146.             ort = creature->GetOrientation();
  147.         }
  148.  
  149.         if (!MapManager::IsValidMapCoord(mapId, x, y, z, ort))
  150.         {
  151.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
  152.             handler->SetSentErrorMessage(true);
  153.             return false;
  154.         }
  155.  
  156.         // stop flight if need
  157.         if (player->IsInFlight())
  158.         {
  159.             player->GetMotionMaster()->MovementExpired();
  160.             player->CleanupAfterTaxiFlight();
  161.         }
  162.         // save only in non-flight case
  163.         else
  164.             player->SaveRecallPosition();
  165.  
  166.         player->TeleportTo(mapId, x, y, z, ort);
  167.         return true;
  168.     }
  169.  
  170.     static bool HandleGoGraveyardCommand(ChatHandler* handler, char const* args)
  171.     {
  172.         Player* player = handler->GetSession()->GetPlayer();
  173.  
  174.         if (!*args)
  175.             return false;
  176.  
  177.         char* gyId = strtok((char*)args, " ");
  178.         if (!gyId)
  179.             return false;
  180.  
  181.         int32 graveyardId = atoi(gyId);
  182.  
  183.         if (!graveyardId)
  184.             return false;
  185.  
  186.         WorldSafeLocsEntry const* gy = sWorldSafeLocsStore.LookupEntry(graveyardId);
  187.         if (!gy)
  188.         {
  189.             handler->PSendSysMessage(LANG_COMMAND_GRAVEYARDNOEXIST, graveyardId);
  190.             handler->SetSentErrorMessage(true);
  191.             return false;
  192.         }
  193.  
  194.         if (!MapManager::IsValidMapCoord(gy->map_id, gy->x, gy->y, gy->z))
  195.         {
  196.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, gy->x, gy->y, gy->map_id);
  197.             handler->SetSentErrorMessage(true);
  198.             return false;
  199.         }
  200.  
  201.         // stop flight if need
  202.         if (player->IsInFlight())
  203.         {
  204.             player->GetMotionMaster()->MovementExpired();
  205.             player->CleanupAfterTaxiFlight();
  206.         }
  207.         // save only in non-flight case
  208.         else
  209.             player->SaveRecallPosition();
  210.  
  211.         player->TeleportTo(gy->map_id, gy->x, gy->y, gy->z, player->GetOrientation());
  212.         return true;
  213.     }
  214.  
  215.     //teleport to grid
  216.     static bool HandleGoGridCommand(ChatHandler* handler, char const* args)
  217.     {
  218.         if (!*args)
  219.             return false;
  220.  
  221.         Player* player = handler->GetSession()->GetPlayer();
  222.  
  223.         char* gridX = strtok((char*)args, " ");
  224.         char* gridY = strtok(NULL, " ");
  225.         char* id = strtok(NULL, " ");
  226.  
  227.         if (!gridX || !gridY)
  228.             return false;
  229.  
  230.         uint32 mapId = id ? (uint32)atoi(id) : player->GetMapId();
  231.  
  232.         // center of grid
  233.         float x = ((float)atof(gridX) - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
  234.         float y = ((float)atof(gridY) - CENTER_GRID_ID + 0.5f) * SIZE_OF_GRIDS;
  235.  
  236.         if (!MapManager::IsValidMapCoord(mapId, x, y))
  237.         {
  238.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
  239.             handler->SetSentErrorMessage(true);
  240.             return false;
  241.         }
  242.  
  243.         // stop flight if need
  244.         if (player->IsInFlight())
  245.         {
  246.             player->GetMotionMaster()->MovementExpired();
  247.             player->CleanupAfterTaxiFlight();
  248.         }
  249.         // save only in non-flight case
  250.         else
  251.             player->SaveRecallPosition();
  252.  
  253.         Map const* map = sMapMgr->CreateBaseMap(mapId);
  254.         float z = std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));
  255.  
  256.         player->TeleportTo(mapId, x, y, z, player->GetOrientation());
  257.         return true;
  258.     }
  259.  
  260.     //teleport to gameobject
  261.     static bool HandleGoObjectCommand(ChatHandler* handler, char const* args)
  262.     {
  263.         if (!*args)
  264.             return false;
  265.  
  266.         Player* player = handler->GetSession()->GetPlayer();
  267.  
  268.         // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
  269.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  270.         if (!id)
  271.             return false;
  272.  
  273.         int32 guid = atoi(id);
  274.         if (!guid)
  275.             return false;
  276.  
  277.         float x, y, z, ort;
  278.         int mapId;
  279.  
  280.         // by DB guid
  281.         if (GameObjectData const* goData = sObjectMgr->GetGOData(guid))
  282.         {
  283.             x = goData->posX;
  284.             y = goData->posY;
  285.             z = goData->posZ;
  286.             ort = goData->orientation;
  287.             mapId = goData->mapid;
  288.         }
  289.         else
  290.         {
  291.             handler->SendSysMessage(LANG_COMMAND_GOOBJNOTFOUND);
  292.             handler->SetSentErrorMessage(true);
  293.             return false;
  294.         }
  295.  
  296.         if (!MapManager::IsValidMapCoord(mapId, x, y, z, ort))
  297.         {
  298.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
  299.             handler->SetSentErrorMessage(true);
  300.             return false;
  301.         }
  302.  
  303.         // stop flight if need
  304.         if (player->IsInFlight())
  305.         {
  306.             player->GetMotionMaster()->MovementExpired();
  307.             player->CleanupAfterTaxiFlight();
  308.         }
  309.         // save only in non-flight case
  310.         else
  311.             player->SaveRecallPosition();
  312.  
  313.         player->TeleportTo(mapId, x, y, z, ort);
  314.         return true;
  315.     }
  316.  
  317.     static bool HandleGoTaxinodeCommand(ChatHandler* handler, char const* args)
  318.     {
  319.         Player* player = handler->GetSession()->GetPlayer();
  320.  
  321.         if (!*args)
  322.             return false;
  323.  
  324.         char* id = handler->extractKeyFromLink((char*)args, "Htaxinode");
  325.         if (!id)
  326.             return false;
  327.  
  328.         int32 nodeId = atoi(id);
  329.         if (!nodeId)
  330.             return false;
  331.  
  332.         TaxiNodesEntry const* node = sTaxiNodesStore.LookupEntry(nodeId);
  333.         if (!node)
  334.         {
  335.             handler->PSendSysMessage(LANG_COMMAND_GOTAXINODENOTFOUND, nodeId);
  336.             handler->SetSentErrorMessage(true);
  337.             return false;
  338.         }
  339.  
  340.         if ((node->x == 0.0f && node->y == 0.0f && node->z == 0.0f) ||
  341.             !MapManager::IsValidMapCoord(node->map_id, node->x, node->y, node->z))
  342.         {
  343.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, node->x, node->y, node->map_id);
  344.             handler->SetSentErrorMessage(true);
  345.             return false;
  346.         }
  347.  
  348.         // stop flight if need
  349.         if (player->IsInFlight())
  350.         {
  351.             player->GetMotionMaster()->MovementExpired();
  352.             player->CleanupAfterTaxiFlight();
  353.         }
  354.         // save only in non-flight case
  355.         else
  356.             player->SaveRecallPosition();
  357.  
  358.         player->TeleportTo(node->map_id, node->x, node->y, node->z, player->GetOrientation());
  359.         return true;
  360.     }
  361.  
  362.     static bool HandleGoTriggerCommand(ChatHandler* handler, char const* args)
  363.     {
  364.         Player* player = handler->GetSession()->GetPlayer();
  365.  
  366.         if (!*args)
  367.             return false;
  368.  
  369.         char* id = strtok((char*)args, " ");
  370.         if (!id)
  371.             return false;
  372.  
  373.         int32 areaTriggerId = atoi(id);
  374.  
  375.         if (!areaTriggerId)
  376.             return false;
  377.  
  378.         AreaTriggerEntry const* at = sAreaTriggerStore.LookupEntry(areaTriggerId);
  379.         if (!at)
  380.         {
  381.             handler->PSendSysMessage(LANG_COMMAND_GOAREATRNOTFOUND, areaTriggerId);
  382.             handler->SetSentErrorMessage(true);
  383.             return false;
  384.         }
  385.  
  386.         if (!MapManager::IsValidMapCoord(at->mapid, at->x, at->y, at->z))
  387.         {
  388.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, at->x, at->y, at->mapid);
  389.             handler->SetSentErrorMessage(true);
  390.             return false;
  391.         }
  392.  
  393.         // stop flight if need
  394.         if (player->IsInFlight())
  395.         {
  396.             player->GetMotionMaster()->MovementExpired();
  397.             player->CleanupAfterTaxiFlight();
  398.         }
  399.         // save only in non-flight case
  400.         else
  401.             player->SaveRecallPosition();
  402.  
  403.         player->TeleportTo(at->mapid, at->x, at->y, at->z, player->GetOrientation());
  404.         return true;
  405.     }
  406.  
  407.     //teleport at coordinates
  408.     static bool HandleGoZoneXYCommand(ChatHandler* handler, char const* args)
  409.     {
  410.         if (!*args)
  411.             return false;
  412.  
  413.         Player* player = handler->GetSession()->GetPlayer();
  414.  
  415.         char* zoneX = strtok((char*)args, " ");
  416.         char* zoneY = strtok(NULL, " ");
  417.         char* tail = strtok(NULL, "");
  418.  
  419.         char* id = handler->extractKeyFromLink(tail, "Harea");       // string or [name] Shift-click form |color|Harea:area_id|h[name]|h|r
  420.  
  421.         if (!zoneX || !zoneY)
  422.             return false;
  423.  
  424.         float x = (float)atof(zoneX);
  425.         float y = (float)atof(zoneY);
  426.  
  427.         // prevent accept wrong numeric args
  428.         if ((x == 0.0f && *zoneX != '0') || (y == 0.0f && *zoneY != '0'))
  429.             return false;
  430.  
  431.         uint32 areaId = id ? (uint32)atoi(id) : player->GetZoneId();
  432.  
  433.         AreaTableEntry const* areaEntry = GetAreaEntryByAreaID(areaId);
  434.  
  435.         if (x < 0 || x > 100 || y < 0 || y > 100 || !areaEntry)
  436.         {
  437.             handler->PSendSysMessage(LANG_INVALID_ZONE_COORD, x, y, areaId);
  438.             handler->SetSentErrorMessage(true);
  439.             return false;
  440.         }
  441.  
  442.         // update to parent zone if exist (client map show only zones without parents)
  443.         AreaTableEntry const* zoneEntry = areaEntry->zone ? GetAreaEntryByAreaID(areaEntry->zone) : areaEntry;
  444.  
  445.         Map const* map = sMapMgr->CreateBaseMap(zoneEntry->mapid);
  446.  
  447.         if (map->Instanceable())
  448.         {
  449.             handler->PSendSysMessage(LANG_INVALID_ZONE_MAP, areaEntry->ID, areaEntry->area_name[handler->GetSessionDbcLocale()], map->GetId(), map->GetMapName());
  450.             handler->SetSentErrorMessage(true);
  451.             return false;
  452.         }
  453.  
  454.         Zone2MapCoordinates(x, y, zoneEntry->ID); //line 454 in cs_go.cpp
  455.  
  456.         if (!MapManager::IsValidMapCoord(zoneEntry->mapid, x, y))
  457.         {
  458.             handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, zoneEntry->mapid);
  459.             handler->SetSentErrorMessage(true);
  460.             return false;
  461.         }
  462.  
  463.         // stop flight if need
  464.         if (player->IsInFlight())
  465.         {
  466.             player->GetMotionMaster()->MovementExpired();
  467.             player->CleanupAfterTaxiFlight();
  468.         }
  469.         // save only in non-flight case
  470.         else
  471.             player->SaveRecallPosition();
  472.  
  473.         float z = std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));
  474.  
  475.         player->TeleportTo(zoneEntry->mapid, x, y, z, player->GetOrientation());
  476.         return true;
  477.     }
  478.  
  479.     //teleport at coordinates, including Z and orientation
  480.     static bool HandleGoXYZCommand(ChatHandler* handler, char const* args)
  481.     {
  482.         if (!*args)
  483.             return false;
  484.  
  485.         Player* player = handler->GetSession()->GetPlayer();
  486.  
  487.         char* goX = strtok((char*)args, " ");
  488.         char* goY = strtok(NULL, " ");
  489.         char* goZ = strtok(NULL, " ");
  490.         char* id = strtok(NULL, " ");
  491.         char* port = strtok(NULL, " ");
  492.  
  493.         if (!goX || !goY)
  494.             return false;
  495.  
  496.         float x = (float)atof(goX);
  497.         float y = (float)atof(goY);
  498.         float z;
  499.         float ort = port ? (float)atof(port) : player->GetOrientation();
  500.         uint32 mapId = id ? (uint32)atoi(id) : player->GetMapId();
  501.  
  502.         if (goZ)
  503.         {
  504.             z = (float)atof(goZ);
  505.             if (!MapManager::IsValidMapCoord(mapId, x, y, z))
  506.             {
  507.                 handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
  508.                 handler->SetSentErrorMessage(true);
  509.                 return false;
  510.             }
  511.         }
  512.         else
  513.         {
  514.             if (!MapManager::IsValidMapCoord(mapId, x, y))
  515.             {
  516.                 handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, mapId);
  517.                 handler->SetSentErrorMessage(true);
  518.                 return false;
  519.             }
  520.             Map const* map = sMapMgr->CreateBaseMap(mapId);
  521.             z = std::max(map->GetHeight(x, y, MAX_HEIGHT), map->GetWaterLevel(x, y));
  522.         }
  523.  
  524.         // stop flight if need
  525.         if (player->IsInFlight())
  526.         {
  527.             player->GetMotionMaster()->MovementExpired();
  528.             player->CleanupAfterTaxiFlight();
  529.         }
  530.         // save only in non-flight case
  531.         else
  532.             player->SaveRecallPosition();
  533.  
  534.         player->TeleportTo(mapId, x, y, z, ort);
  535.         return true;
  536.     }
  537.  
  538.     static bool HandleGoTicketCommand(ChatHandler* handler, char const* args)
  539.     {
  540.         if (!*args)
  541.             return false;
  542.  
  543.         char* id = strtok((char*)args, " ");
  544.         if (!id)
  545.             return false;
  546.  
  547.         uint32 ticketId = atoi(id);
  548.         if (!ticketId)
  549.             return false;
  550.  
  551.         GmTicket* ticket = sTicketMgr->GetTicket(ticketId);
  552.         if (!ticket)
  553.         {
  554.             handler->SendSysMessage(LANG_COMMAND_TICKETNOTEXIST);
  555.             return true;
  556.         }
  557.  
  558.         Player* player = handler->GetSession()->GetPlayer();
  559.         if (player->IsInFlight())
  560.         {
  561.             player->GetMotionMaster()->MovementExpired();
  562.             player->CleanupAfterTaxiFlight();
  563.         }
  564.         else
  565.             player->SaveRecallPosition();
  566.  
  567.         ticket->TeleportTo(player);
  568.         return true;
  569.     }
  570. };
  571.  
  572. void AddSC_go_commandscript()
  573. {
  574.     new go_commandscript();
  575. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement