Advertisement
Rochet2

cs_gobject

Sep 30th, 2012
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.40 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2008-2012 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: gobject_commandscript
  20. %Complete: 100
  21. Comment: All gobject related commands
  22. Category: commandscripts
  23. EndScriptData */
  24.  
  25. #include "ScriptMgr.h"
  26. #include "GameEventMgr.h"
  27. #include "ObjectMgr.h"
  28. #include "PoolMgr.h"
  29. #include "MapManager.h"
  30. #include "Chat.h"
  31.  
  32. class gobject_commandscript : public CommandScript
  33. {
  34. public:
  35.     gobject_commandscript() : CommandScript("gobject_commandscript") { }
  36.  
  37.     ChatCommand* GetCommands() const
  38.     {
  39.         static ChatCommand gobjectAddCommandTable[] =
  40.         {
  41.             { "temp",           SEC_GAMEMASTER,     false, &HandleGameObjectAddTempCommand,   "", NULL },
  42.             { "",               SEC_GAMEMASTER,     false, &HandleGameObjectAddCommand,       "", NULL },
  43.             { NULL,             0,                  false, NULL,                              "", NULL }
  44.         };
  45.         static ChatCommand gobjectSetCommandTable[] =
  46.         {
  47.             { "phase",          SEC_GAMEMASTER,     false, &HandleGameObjectSetPhaseCommand,  "", NULL },
  48.             { "state",          SEC_GAMEMASTER,     false, &HandleGameObjectSetStateCommand,  "", NULL },
  49.             { NULL,             0,                  false, NULL,                              "", NULL }
  50.         };
  51.         static ChatCommand gobjectCommandTable[] =
  52.         {
  53.             { "activate",       SEC_GAMEMASTER,     false, &HandleGameObjectActivateCommand,  "", NULL },
  54.             { "scale",          SEC_GAMEMASTER,     false, &HandleGameObjectScaleCommand,     "", NULL },
  55.             { "delete",         SEC_GAMEMASTER,     false, &HandleGameObjectDeleteCommand,    "", NULL },
  56.             { "info",           SEC_GAMEMASTER,     false, &HandleGameObjectInfoCommand,      "", NULL },
  57.             { "move",           SEC_GAMEMASTER,     false, &HandleGameObjectMoveCommand,      "", NULL },
  58.             { "near",           SEC_GAMEMASTER,     false, &HandleGameObjectNearCommand,      "", NULL },
  59.             { "target",         SEC_GAMEMASTER,     false, &HandleGameObjectTargetCommand,    "", NULL },
  60.             { "turn",           SEC_GAMEMASTER,     false, &HandleGameObjectTurnCommand,      "", NULL },
  61.             { "add",            SEC_GAMEMASTER,     false, NULL,            "", gobjectAddCommandTable },
  62.             { "set",            SEC_GAMEMASTER,     false, NULL,            "", gobjectSetCommandTable },
  63.             { NULL,             0,                  false, NULL,                              "", NULL }
  64.         };
  65.         static ChatCommand commandTable[] =
  66.         {
  67.             { "gobject",        SEC_GAMEMASTER,     false, NULL,                "", gobjectCommandTable },
  68.             { NULL,             0,                  false, NULL,                               "", NULL }
  69.         };
  70.         return commandTable;
  71.     }
  72.  
  73.     static bool HandleGameObjectScaleCommand(ChatHandler* handler, char const* args)
  74.     {
  75.         if (!*args)
  76.             return false;
  77.  
  78.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  79.         if (!id)
  80.             return false;
  81.  
  82.         uint32 guidLow = atoi(id);
  83.         if (!guidLow)
  84.             return false;
  85.  
  86.         GameObject* object = NULL;
  87.  
  88.         // by DB guid
  89.         if (GameObjectData const* goData = sObjectMgr->GetGOData(guidLow))
  90.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, goData->id);
  91.  
  92.         if (!object)
  93.         {
  94.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  95.             handler->SetSentErrorMessage(true);
  96.             return false;
  97.         }
  98.  
  99.         char* scale_temp = strtok (NULL, " ");
  100.         float scale = scale_temp ? atof(scale_temp) : 0.0f;
  101.         if (scale == 0.0f)
  102.         {
  103.             handler->SendSysMessage(LANG_BAD_VALUE);
  104.             handler->SetSentErrorMessage(true);
  105.             return false;
  106.         }
  107.  
  108.         // Get the entry of a proper object and set it to the new spawn
  109.  
  110.         // could have more checks..
  111.         QueryResult result = WorldDatabase.PQuery("SELECT entry FROM gameobject_template WHERE size = '%f' and type = %u and displayId = %u and  name = \"%s\" and faction = %u", scale, object->GetGOInfo()->type, object->GetGOInfo()->displayId, object->GetName(), object->GetGOInfo()->faction);
  112.  
  113.         uint32 new_entry = result ? result->Fetch()[0].GetUInt32() : 0;
  114.         if(new_entry == 0)
  115.         {
  116.             QueryResult result = WorldDatabase.Query("SELECT entry+1 FROM gameobject_template ORDER BY entry DESC LIMIT 1");
  117.             if(!result)
  118.                 return false;
  119.             new_entry = result->Fetch()[0].GetUInt32();
  120.             QueryResult result2 = WorldDatabase.PQuery("SELECT entry,  `type`,  `displayId`,  `name`,  `IconName`,  `castBarCaption`,  `unk1`,  `faction`,  `flags`,  size,  `questItem1`,  `questItem2`,  `questItem3`,  `questItem4`,  `questItem5`,  `questItem6`,  `data0`,  `data1`,  `data2`,  `data3`,  `data4`,  `data5`,  `data6`,  `data7`,  `data8`,  `data9`,  `data10`,  `data11`,  `data12`,  `data13`,  `data14`,  `data15`,  `data16`,  `data17`,  `data18`,  `data19`,  `data20`,  `data21`,  `data22`,  `data23`,  `AIName`,  `ScriptName`,  `WDBVerified` FROM gameobject_template WHERE entry = %u", object->GetEntry());
  121.             if(!result2)
  122.                 return false;
  123.             Field* fields = result2->Fetch();
  124.             WorldDatabase.PExecute("INSERT INTO gameobject_template (entry,  `type`,  `displayId`,  `name`,  `IconName`,  `castBarCaption`,  `unk1`,  `faction`,  `flags`,  size,  `questItem1`,  `questItem2`,  `questItem3`,  `questItem4`,  `questItem5`,  `questItem6`,  `data0`,  `data1`,  `data2`,  `data3`,  `data4`,  `data5`,  `data6`,  `data7`,  `data8`,  `data9`,  `data10`,  `data11`,  `data12`,  `data13`,  `data14`,  `data15`,  `data16`,  `data17`,  `data18`,  `data19`,  `data20`,  `data21`,  `data22`,  `data23`,  `AIName`,  `ScriptName`,  `WDBVerified`) VALUES (%u, %u, %u, \"%s\", \"%s\", \"%s\", \"%s\", %u, %u, '%f', %u, %u, %u, %u, %u, %u, %u, %i, %u, %u, %u, %u, %i, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, \"%s\", \"%s\", %u)", new_entry, fields[1].GetUInt32(), fields[2].GetUInt32(), fields[3].GetString().c_str(), fields[4].GetString().c_str(), fields[5].GetString().c_str(), fields[6].GetString().c_str(), fields[7].GetUInt32(), fields[8].GetUInt32(), scale, fields[10].GetUInt32(), fields[11].GetUInt32(), fields[12].GetUInt32(), fields[13].GetUInt32(), fields[14].GetUInt32(), fields[15].GetUInt32(), fields[16].GetUInt32(), fields[17].GetInt32(), fields[18].GetUInt32(), fields[19].GetUInt32(), fields[20].GetUInt32(), fields[21].GetUInt32(), fields[22].GetInt32(), fields[23].GetUInt32(), fields[24].GetUInt32(), fields[25].GetUInt32(), fields[26].GetUInt32(), fields[27].GetUInt32(), fields[28].GetUInt32(), fields[29].GetUInt32(), fields[30].GetUInt32(), fields[31].GetUInt32(), fields[32].GetUInt32(), fields[33].GetUInt32(), fields[34].GetUInt32(), fields[35].GetUInt32(), fields[36].GetUInt32(), fields[37].GetUInt32(), fields[38].GetUInt32(), fields[39].GetUInt32(), fields[40].GetString().c_str(), fields[41].GetString().c_str(), fields[42].GetUInt32());
  125.         }
  126.  
  127.         WorldDatabase.PExecute("UPDATE gameobject SET id = %u WHERE guid = %u", new_entry, object->GetGUIDLow());
  128.  
  129.         // set ingame scale
  130.         object->SetObjectScale(scale);
  131.         object->DestroyForNearbyPlayers();
  132.         object->UpdateObjectVisibility();
  133.  
  134.         return true;
  135.     }
  136.  
  137.     static bool HandleGameObjectActivateCommand(ChatHandler* handler, char const* args)
  138.     {
  139.         if (!*args)
  140.             return false;
  141.  
  142.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  143.         if (!id)
  144.             return false;
  145.  
  146.         uint32 guidLow = atoi(id);
  147.         if (!guidLow)
  148.             return false;
  149.  
  150.         GameObject* object = NULL;
  151.  
  152.         // by DB guid
  153.         if (GameObjectData const* goData = sObjectMgr->GetGOData(guidLow))
  154.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, goData->id);
  155.  
  156.         if (!object)
  157.         {
  158.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  159.             handler->SetSentErrorMessage(true);
  160.             return false;
  161.         }
  162.  
  163.         // Activate
  164.         object->SetLootState(GO_READY);
  165.         object->UseDoorOrButton(10000, false, handler->GetSession()->GetPlayer());
  166.  
  167.         handler->PSendSysMessage("Object activated!");
  168.  
  169.         return true;
  170.     }
  171.  
  172.     //spawn go
  173.     static bool HandleGameObjectAddCommand(ChatHandler* handler, char const* args)
  174.     {
  175.         if (!*args)
  176.             return false;
  177.  
  178.         // number or [name] Shift-click form |color|Hgameobject_entry:go_id|h[name]|h|r
  179.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject_entry");
  180.         if (!id)
  181.             return false;
  182.  
  183.         uint32 objectId = atol(id);
  184.         if (!objectId)
  185.             return false;
  186.  
  187.         char* spawntimeSecs = strtok(NULL, " ");
  188.  
  189.         const GameObjectTemplate* objectInfo = sObjectMgr->GetGameObjectTemplate(objectId);
  190.  
  191.         if (!objectInfo)
  192.         {
  193.             handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, objectId);
  194.             handler->SetSentErrorMessage(true);
  195.             return false;
  196.         }
  197.  
  198.         if (objectInfo->displayId && !sGameObjectDisplayInfoStore.LookupEntry(objectInfo->displayId))
  199.         {
  200.             // report to DB errors log as in loading case
  201.             sLog->outError(LOG_FILTER_SQL, "Gameobject (Entry %u GoType: %u) have invalid displayId (%u), not spawned.", objectId, objectInfo->type, objectInfo->displayId);
  202.             handler->PSendSysMessage(LANG_GAMEOBJECT_HAVE_INVALID_DATA, objectId);
  203.             handler->SetSentErrorMessage(true);
  204.             return false;
  205.         }
  206.  
  207.         Player* player = handler->GetSession()->GetPlayer();
  208.         float x = float(player->GetPositionX());
  209.         float y = float(player->GetPositionY());
  210.         float z = float(player->GetPositionZ());
  211.         float o = float(player->GetOrientation());
  212.         Map* map = player->GetMap();
  213.  
  214.         GameObject* object = new GameObject;
  215.         uint32 guidLow = sObjectMgr->GenerateLowGuid(HIGHGUID_GAMEOBJECT);
  216.  
  217.         if (!object->Create(guidLow, objectInfo->entry, map, player->GetPhaseMaskForSpawn(), x, y, z, o, 0.0f, 0.0f, 0.0f, 0.0f, 0, GO_STATE_READY))
  218.         {
  219.             delete object;
  220.             return false;
  221.         }
  222.  
  223.         if (spawntimeSecs)
  224.         {
  225.             uint32 value = atoi((char*)spawntimeSecs);
  226.             object->SetRespawnTime(value);
  227.         }
  228.  
  229.         // fill the gameobject data and save to the db
  230.         object->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), player->GetPhaseMaskForSpawn());
  231.  
  232.         // this will generate a new guid if the object is in an instance
  233.         if (!object->LoadGameObjectFromDB(guidLow, map))
  234.         {
  235.             delete object;
  236.             return false;
  237.         }
  238.  
  239.         // TODO: is it really necessary to add both the real and DB table guid here ?
  240.         sObjectMgr->AddGameobjectToGrid(guidLow, sObjectMgr->GetGOData(guidLow));
  241.  
  242.         handler->PSendSysMessage(LANG_GAMEOBJECT_ADD, objectId, objectInfo->name.c_str(), guidLow, x, y, z);
  243.         return true;
  244.     }
  245.  
  246.     // add go, temp only
  247.     static bool HandleGameObjectAddTempCommand(ChatHandler* handler, char const* args)
  248.     {
  249.         if (!*args)
  250.             return false;
  251.  
  252.         char* id = strtok((char*)args, " ");
  253.         if (!id)
  254.             return false;
  255.  
  256.         Player* player = handler->GetSession()->GetPlayer();
  257.  
  258.         char* spawntime = strtok(NULL, " ");
  259.         uint32 spawntm = 300;
  260.  
  261.         if (spawntime)
  262.             spawntm = atoi((char*)spawntime);
  263.  
  264.         float x = player->GetPositionX();
  265.         float y = player->GetPositionY();
  266.         float z = player->GetPositionZ();
  267.         float ang = player->GetOrientation();
  268.  
  269.         float rot2 = sin(ang/2);
  270.         float rot3 = cos(ang/2);
  271.  
  272.         uint32 objectId = atoi(id);
  273.  
  274.         player->SummonGameObject(objectId, x, y, z, ang, 0, 0, rot2, rot3, spawntm);
  275.  
  276.         return true;
  277.     }
  278.  
  279.     static bool HandleGameObjectTargetCommand(ChatHandler* handler, char const* args)
  280.     {
  281.         Player* player = handler->GetSession()->GetPlayer();
  282.         QueryResult result;
  283.         GameEventMgr::ActiveEvents const& activeEventsList = sGameEventMgr->GetActiveEventList();
  284.  
  285.         if (*args)
  286.         {
  287.             // number or [name] Shift-click form |color|Hgameobject_entry:go_id|h[name]|h|r
  288.             char* id = handler->extractKeyFromLink((char*)args, "Hgameobject_entry");
  289.             if (!id)
  290.                 return false;
  291.  
  292.             uint32 objectId = atol(id);
  293.  
  294.             if (objectId)
  295.                 result = WorldDatabase.PQuery("SELECT guid, id, position_x, position_y, position_z, orientation, map, phaseMask, (POW(position_x - '%f', 2) + POW(position_y - '%f', 2) + POW(position_z - '%f', 2)) AS order_ FROM gameobject WHERE map = '%i' AND id = '%u' ORDER BY order_ ASC LIMIT 1",
  296.                 player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), player->GetMapId(), objectId);
  297.             else
  298.             {
  299.                 std::string name = id;
  300.                 WorldDatabase.EscapeString(name);
  301.                 result = WorldDatabase.PQuery(
  302.                     "SELECT guid, id, position_x, position_y, position_z, orientation, map, phaseMask, (POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ "
  303.                     "FROM gameobject, gameobject_template WHERE gameobject_template.entry = gameobject.id AND map = %i AND name "_LIKE_" "_CONCAT3_("'%%'", "'%s'", "'%%'")" ORDER BY order_ ASC LIMIT 1",
  304.                     player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), player->GetMapId(), name.c_str());
  305.             }
  306.         }
  307.         else
  308.         {
  309.             std::ostringstream eventFilter;
  310.             eventFilter << " AND (eventEntry IS NULL ";
  311.             bool initString = true;
  312.  
  313.             for (GameEventMgr::ActiveEvents::const_iterator itr = activeEventsList.begin(); itr != activeEventsList.end(); ++itr)
  314.             {
  315.                 if (initString)
  316.                 {
  317.                     eventFilter  <<  "OR eventEntry IN (" << *itr;
  318.                     initString = false;
  319.                 }
  320.                 else
  321.                     eventFilter << ',' << *itr;
  322.             }
  323.  
  324.             if (!initString)
  325.                 eventFilter << "))";
  326.             else
  327.                 eventFilter << ')';
  328.  
  329.             result = WorldDatabase.PQuery("SELECT gameobject.guid, id, position_x, position_y, position_z, orientation, map, phaseMask, "
  330.                 "(POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ FROM gameobject "
  331.                 "LEFT OUTER JOIN game_event_gameobject on gameobject.guid = game_event_gameobject.guid WHERE map = '%i' %s ORDER BY order_ ASC LIMIT 10",
  332.                 handler->GetSession()->GetPlayer()->GetPositionX(), handler->GetSession()->GetPlayer()->GetPositionY(), handler->GetSession()->GetPlayer()->GetPositionZ(),
  333.                 handler->GetSession()->GetPlayer()->GetMapId(), eventFilter.str().c_str());
  334.         }
  335.  
  336.         if (!result)
  337.         {
  338.             handler->SendSysMessage(LANG_COMMAND_TARGETOBJNOTFOUND);
  339.             return true;
  340.         }
  341.  
  342.         bool found = false;
  343.         float x, y, z, o;
  344.         uint32 guidLow, id;
  345.         uint16 mapId, phase;
  346.         uint32 poolId;
  347.  
  348.         do
  349.         {
  350.             Field* fields = result->Fetch();
  351.             guidLow = fields[0].GetUInt32();
  352.             id =      fields[1].GetUInt32();
  353.             x =       fields[2].GetFloat();
  354.             y =       fields[3].GetFloat();
  355.             z =       fields[4].GetFloat();
  356.             o =       fields[5].GetFloat();
  357.             mapId =   fields[6].GetUInt16();
  358.             phase =   fields[7].GetUInt16();
  359.             poolId =  sPoolMgr->IsPartOfAPool<GameObject>(guidLow);
  360.             if (!poolId || sPoolMgr->IsSpawnedObject<GameObject>(guidLow))
  361.                 found = true;
  362.         } while (result->NextRow() && !found);
  363.  
  364.         if (!found)
  365.         {
  366.             handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, id);
  367.             return false;
  368.         }
  369.  
  370.         GameObjectTemplate const* objectInfo = sObjectMgr->GetGameObjectTemplate(id);
  371.  
  372.         if (!objectInfo)
  373.         {
  374.             handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, id);
  375.             return false;
  376.         }
  377.  
  378.         GameObject* target = handler->GetSession()->GetPlayer()->GetMap()->GetGameObject(MAKE_NEW_GUID(guidLow, id, HIGHGUID_GAMEOBJECT));
  379.  
  380.         handler->PSendSysMessage(LANG_GAMEOBJECT_DETAIL, guidLow, objectInfo->name.c_str(), guidLow, id, x, y, z, mapId, o, phase);
  381.  
  382.         if (target)
  383.         {
  384.             int32 curRespawnDelay = int32(target->GetRespawnTimeEx() - time(NULL));
  385.             if (curRespawnDelay < 0)
  386.                 curRespawnDelay = 0;
  387.  
  388.             std::string curRespawnDelayStr = secsToTimeString(curRespawnDelay, true);
  389.             std::string defRespawnDelayStr = secsToTimeString(target->GetRespawnDelay(), true);
  390.  
  391.             handler->PSendSysMessage(LANG_COMMAND_RAWPAWNTIMES, defRespawnDelayStr.c_str(), curRespawnDelayStr.c_str());
  392.         }
  393.         return true;
  394.     }
  395.  
  396.     //delete object by selection or guid
  397.     static bool HandleGameObjectDeleteCommand(ChatHandler* handler, char const* args)
  398.     {
  399.         // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
  400.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  401.         if (!id)
  402.             return false;
  403.  
  404.         uint32 guidLow = atoi(id);
  405.         if (!guidLow)
  406.             return false;
  407.  
  408.         GameObject* object = NULL;
  409.  
  410.         // by DB guid
  411.         if (GameObjectData const* gameObjectData = sObjectMgr->GetGOData(guidLow))
  412.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, gameObjectData->id);
  413.  
  414.         if (!object)
  415.         {
  416.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  417.             handler->SetSentErrorMessage(true);
  418.             return false;
  419.         }
  420.  
  421.         uint64 ownerGuid = object->GetOwnerGUID();
  422.         if (ownerGuid)
  423.         {
  424.             Unit* owner = ObjectAccessor::GetUnit(*handler->GetSession()->GetPlayer(), ownerGuid);
  425.             if (!owner || !IS_PLAYER_GUID(ownerGuid))
  426.             {
  427.                 handler->PSendSysMessage(LANG_COMMAND_DELOBJREFERCREATURE, GUID_LOPART(ownerGuid), object->GetGUIDLow());
  428.                 handler->SetSentErrorMessage(true);
  429.                 return false;
  430.             }
  431.  
  432.             owner->RemoveGameObject(object, false);
  433.         }
  434.  
  435.         object->SetRespawnTime(0);                                 // not save respawn time
  436.         object->Delete();
  437.         object->DeleteFromDB();
  438.  
  439.         handler->PSendSysMessage(LANG_COMMAND_DELOBJMESSAGE, object->GetGUIDLow());
  440.  
  441.         return true;
  442.     }
  443.  
  444.     //turn selected object
  445.     static bool HandleGameObjectTurnCommand(ChatHandler* handler, char const* args)
  446.     {
  447.         // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
  448.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  449.         if (!id)
  450.             return false;
  451.  
  452.         uint32 guidLow = atoi(id);
  453.         if (!guidLow)
  454.             return false;
  455.  
  456.         GameObject* object = NULL;
  457.  
  458.         // by DB guid
  459.         if (GameObjectData const* gameObjectData = sObjectMgr->GetGOData(guidLow))
  460.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, gameObjectData->id);
  461.  
  462.         if (!object)
  463.         {
  464.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  465.             handler->SetSentErrorMessage(true);
  466.             return false;
  467.         }
  468.  
  469.         char* orientation = strtok(NULL, " ");
  470.         float o;
  471.  
  472.         if (orientation)
  473.             o = (float)atof(orientation);
  474.         else
  475.         {
  476.             Player* player = handler->GetSession()->GetPlayer();
  477.             o = player->GetOrientation();
  478.         }
  479.  
  480.         object->Relocate(object->GetPositionX(), object->GetPositionY(), object->GetPositionZ(), o);
  481.         object->UpdateRotationFields();
  482.         object->DestroyForNearbyPlayers();
  483.         object->UpdateObjectVisibility();
  484.  
  485.         object->SaveToDB();
  486.         object->Refresh();
  487.  
  488.         handler->PSendSysMessage(LANG_COMMAND_TURNOBJMESSAGE, object->GetGUIDLow(), object->GetGOInfo()->name.c_str(), object->GetGUIDLow(), o);
  489.  
  490.         return true;
  491.     }
  492.  
  493.     //move selected object
  494.     static bool HandleGameObjectMoveCommand(ChatHandler* handler, char const* args)
  495.     {
  496.         // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
  497.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  498.         if (!id)
  499.             return false;
  500.  
  501.         uint32 guidLow = atoi(id);
  502.         if (!guidLow)
  503.             return false;
  504.  
  505.         GameObject* object = NULL;
  506.  
  507.         // by DB guid
  508.         if (GameObjectData const* gameObjectData = sObjectMgr->GetGOData(guidLow))
  509.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, gameObjectData->id);
  510.  
  511.         if (!object)
  512.         {
  513.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  514.             handler->SetSentErrorMessage(true);
  515.             return false;
  516.         }
  517.  
  518.         char* toX = strtok(NULL, " ");
  519.         char* toY = strtok(NULL, " ");
  520.         char* toZ = strtok(NULL, " ");
  521.  
  522.         if (!toX)
  523.         {
  524.             Player* player = handler->GetSession()->GetPlayer();
  525.             object->Relocate(player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), object->GetOrientation());
  526.             object->DestroyForNearbyPlayers();
  527.             object->UpdateObjectVisibility();
  528.         }
  529.         else
  530.         {
  531.             if (!toY || !toZ)
  532.                 return false;
  533.  
  534.             float x = (float)atof(toX);
  535.             float y = (float)atof(toY);
  536.             float z = (float)atof(toZ);
  537.  
  538.             if (!MapManager::IsValidMapCoord(object->GetMapId(), x, y, z))
  539.             {
  540.                 handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, object->GetMapId());
  541.                 handler->SetSentErrorMessage(true);
  542.                 return false;
  543.             }
  544.  
  545.             object->Relocate(x, y, z, object->GetOrientation());
  546.             object->DestroyForNearbyPlayers();
  547.             object->UpdateObjectVisibility();
  548.         }
  549.  
  550.         object->SaveToDB();
  551.         object->Refresh();
  552.  
  553.         handler->PSendSysMessage(LANG_COMMAND_MOVEOBJMESSAGE, object->GetGUIDLow(), object->GetGOInfo()->name.c_str(), object->GetGUIDLow());
  554.  
  555.         return true;
  556.     }
  557.  
  558.     //set phasemask for selected object
  559.     static bool HandleGameObjectSetPhaseCommand(ChatHandler* handler, char const* args)
  560.     {
  561.         // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
  562.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  563.         if (!id)
  564.             return false;
  565.  
  566.         uint32 guidLow = atoi(id);
  567.         if (!guidLow)
  568.             return false;
  569.  
  570.         GameObject* object = NULL;
  571.  
  572.         // by DB guid
  573.         if (GameObjectData const* gameObjectData = sObjectMgr->GetGOData(guidLow))
  574.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, gameObjectData->id);
  575.  
  576.         if (!object)
  577.         {
  578.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  579.             handler->SetSentErrorMessage(true);
  580.             return false;
  581.         }
  582.  
  583.         char* phase = strtok (NULL, " ");
  584.         uint32 phaseMask = phase ? atoi(phase) : 0;
  585.         if (phaseMask == 0)
  586.         {
  587.             handler->SendSysMessage(LANG_BAD_VALUE);
  588.             handler->SetSentErrorMessage(true);
  589.             return false;
  590.         }
  591.  
  592.         object->SetPhaseMask(phaseMask, true);
  593.         object->SaveToDB();
  594.         return true;
  595.     }
  596.  
  597.     static bool HandleGameObjectNearCommand(ChatHandler* handler, char const* args)
  598.     {
  599.         float distance = (!*args) ? 10.0f : (float)(atof(args));
  600.         uint32 count = 0;
  601.  
  602.         Player* player = handler->GetSession()->GetPlayer();
  603.  
  604.         PreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_GAMEOBJECT_NEAREST);
  605.         stmt->setFloat(0, player->GetPositionX());
  606.         stmt->setFloat(1, player->GetPositionY());
  607.         stmt->setFloat(2, player->GetPositionZ());
  608.         stmt->setUInt32(3, player->GetMapId());
  609.         stmt->setFloat(4, player->GetPositionX());
  610.         stmt->setFloat(5, player->GetPositionY());
  611.         stmt->setFloat(6, player->GetPositionZ());
  612.         stmt->setFloat(7, distance * distance);
  613.         PreparedQueryResult result = WorldDatabase.Query(stmt);
  614.  
  615.         if (result)
  616.         {
  617.             do
  618.             {
  619.                 Field* fields = result->Fetch();
  620.                 uint32 guid = fields[0].GetUInt32();
  621.                 uint32 entry = fields[1].GetUInt32();
  622.                 float x = fields[2].GetFloat();
  623.                 float y = fields[3].GetFloat();
  624.                 float z = fields[4].GetFloat();
  625.                 uint16 mapId = fields[5].GetUInt16();
  626.  
  627.                 GameObjectTemplate const* gameObjectInfo = sObjectMgr->GetGameObjectTemplate(entry);
  628.  
  629.                 if (!gameObjectInfo)
  630.                     continue;
  631.  
  632.                 handler->PSendSysMessage(LANG_GO_LIST_CHAT, guid, entry, guid, gameObjectInfo->name.c_str(), x, y, z, mapId);
  633.  
  634.                 ++count;
  635.             } while (result->NextRow());
  636.         }
  637.  
  638.         handler->PSendSysMessage(LANG_COMMAND_NEAROBJMESSAGE, distance, count);
  639.         return true;
  640.     }
  641.  
  642.     //show info of gameobject
  643.     static bool HandleGameObjectInfoCommand(ChatHandler* handler, char const* args)
  644.     {
  645.         uint32 entry = 0;
  646.         uint32 type = 0;
  647.         uint32 displayId = 0;
  648.         std::string name;
  649.         uint32 lootId = 0;
  650.  
  651.         if (!*args)
  652.         {
  653.             if (WorldObject* object = handler->getSelectedObject())
  654.                 entry = object->GetEntry();
  655.             else
  656.                 entry = atoi((char*)args);
  657.         }
  658.  
  659.         GameObjectTemplate const* gameObjectInfo = sObjectMgr->GetGameObjectTemplate(entry);
  660.  
  661.         if (!gameObjectInfo)
  662.             return false;
  663.  
  664.         type = gameObjectInfo->type;
  665.         displayId = gameObjectInfo->displayId;
  666.         name = gameObjectInfo->name;
  667.         if (type == GAMEOBJECT_TYPE_CHEST)
  668.             lootId = gameObjectInfo->chest.lootId;
  669.         else if (type == GAMEOBJECT_TYPE_FISHINGHOLE)
  670.             lootId = gameObjectInfo->fishinghole.lootId;
  671.  
  672.         handler->PSendSysMessage(LANG_GOINFO_ENTRY, entry);
  673.         handler->PSendSysMessage(LANG_GOINFO_TYPE, type);
  674.         handler->PSendSysMessage(LANG_GOINFO_LOOTID, lootId);
  675.         handler->PSendSysMessage(LANG_GOINFO_DISPLAYID, displayId);
  676.         handler->PSendSysMessage(LANG_GOINFO_NAME, name.c_str());
  677.  
  678.         return true;
  679.     }
  680.  
  681.     static bool HandleGameObjectSetStateCommand(ChatHandler* handler, char const* args)
  682.     {
  683.         // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
  684.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject");
  685.         if (!id)
  686.             return false;
  687.  
  688.         uint32 guidLow = atoi(id);
  689.         if (!guidLow)
  690.             return false;
  691.  
  692.         GameObject* object = NULL;
  693.  
  694.         if (GameObjectData const* gameObjectData = sObjectMgr->GetGOData(guidLow))
  695.             object = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(guidLow, gameObjectData->id);
  696.  
  697.         if (!object)
  698.         {
  699.             handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, guidLow);
  700.             handler->SetSentErrorMessage(true);
  701.             return false;
  702.         }
  703.  
  704.         char* type = strtok(NULL, " ");
  705.         if (!type)
  706.             return false;
  707.  
  708.         int32 objectType = atoi(type);
  709.         if (objectType < 0)
  710.         {
  711.             if (objectType == -1)
  712.                 object->SendObjectDeSpawnAnim(object->GetGUID());
  713.             else if (objectType == -2)
  714.                 return false;
  715.             return true;
  716.         }
  717.  
  718.         char* state = strtok(NULL, " ");
  719.         if (!state)
  720.             return false;
  721.  
  722.         int32 objectState = atoi(state);
  723.  
  724.         if (objectType < 4)
  725.             object->SetByteValue(GAMEOBJECT_BYTES_1, objectType, objectState);
  726.         else if (objectType == 4)
  727.         {
  728.             WorldPacket data(SMSG_GAMEOBJECT_CUSTOM_ANIM, 8+4);
  729.             data << object->GetGUID();
  730.             data << (uint32)(objectState);
  731.             object->SendMessageToSet(&data, true);
  732.         }
  733.         handler->PSendSysMessage("Set gobject type %d state %d", objectType, objectState);
  734.         return true;
  735.     }
  736. };
  737.  
  738. void AddSC_gobject_commandscript()
  739. {
  740.     new gobject_commandscript();
  741. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement