Advertisement
Guest User

CreatureCommands.cpp

a guest
Jun 18th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 33.59 KB | None | 0 0
  1. /**
  2.  * MaNGOS is a full featured server for World of Warcraft, supporting
  3.  * the following clients: 1.12.x, 2.4.3, 3.3.5a, 4.3.4a and 5.4.8
  4.  *
  5.  * Copyright (C) 2005-2020 MaNGOS <https://getmangos.eu>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  *
  21.  * World of Warcraft, and all World of Warcraft or Warcraft art, images,
  22.  * and lore are copyrighted by Blizzard Entertainment, Inc.
  23.  */
  24.  
  25. #include "Chat.h"
  26. #include "Language.h"
  27. #include "World.h"
  28. #include "GridNotifiersImpl.h"
  29. #include "CellImpl.h"
  30. #include "TargetedMovementGenerator.h"                      // for HandleNpcUnFollowCommand
  31. #include "TemporarySummon.h"
  32. #include "WaypointManager.h"
  33. #include "PathFinder.h"                                     // for mmap commands
  34. #include "Totem.h"
  35.  
  36. #ifdef _DEBUG_VMAPS
  37. #include "VMapFactory.h"
  38. #endif
  39.  
  40.  /**********************************************************************
  41.      CommandTable : commandTable
  42.  /***********************************************************************/
  43.  
  44. /*
  45. ComeToMe command REQUIRED for 3rd party scripting library to have access to PointMovementGenerator
  46. Without this function 3rd party scripting library will get linking errors (unresolved external)
  47. when attempting to use the PointMovementGenerator
  48. */
  49. bool ChatHandler::HandleComeToMeCommand(char* /*args*/)
  50. {
  51.     Creature* caster = getSelectedCreature();
  52.  
  53.     if (!caster)
  54.     {
  55.         SendSysMessage(LANG_SELECT_CREATURE);
  56.         SetSentErrorMessage(true);
  57.         return false;
  58.     }
  59.  
  60.     Player* pl = m_session->GetPlayer();
  61.  
  62.     caster->GetMotionMaster()->MovePoint(0, pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ());
  63.     return true;
  64. }
  65.  
  66. bool ChatHandler::HandleRespawnCommand(char* /*args*/)
  67. {
  68.     Player* pl = m_session->GetPlayer();
  69.  
  70.     // accept only explicitly selected target (not implicitly self targeting case)
  71.     Unit* target = getSelectedUnit();
  72.     if (pl->GetSelectionGuid() && target)
  73.     {
  74.         if (target->GetTypeId() != TYPEID_UNIT)
  75.         {
  76.             SendSysMessage(LANG_SELECT_CREATURE);
  77.             SetSentErrorMessage(true);
  78.             return false;
  79.         }
  80.  
  81.         if (target->IsDead())
  82.         {
  83.             ((Creature*)target)->Respawn();
  84.         }
  85.         return true;
  86.     }
  87.  
  88.     MaNGOS::RespawnDo u_do;
  89.     MaNGOS::WorldObjectWorker<MaNGOS::RespawnDo> worker(u_do);
  90.     Cell::VisitGridObjects(pl, worker, pl->GetMap()->GetVisibilityDistance());
  91.     return true;
  92. }
  93.  
  94. // Edit Creature Faction
  95. bool ChatHandler::HandleModifyFactionCommand(char* args)
  96. {
  97.     Creature* chr = getSelectedCreature();
  98.     if (!chr)
  99.     {
  100.         SendSysMessage(LANG_SELECT_CREATURE);
  101.         SetSentErrorMessage(true);
  102.         return false;
  103.     }
  104.  
  105.     if (!*args)
  106.     {
  107.         if (chr)
  108.         {
  109.             uint32 factionid = chr->getFaction();
  110.             uint32 flag = chr->GetUInt32Value(UNIT_FIELD_FLAGS);
  111.             uint32 npcflag = chr->GetUInt32Value(UNIT_NPC_FLAGS);
  112.             uint32 dyflag = chr->GetUInt32Value(UNIT_DYNAMIC_FLAGS);
  113.             PSendSysMessage(LANG_CURRENT_FACTION, chr->GetGUIDLow(), factionid, flag, npcflag, dyflag);
  114.         }
  115.         return true;
  116.     }
  117.  
  118.     if (!chr)
  119.     {
  120.         SendSysMessage(LANG_NO_CHAR_SELECTED);
  121.         SetSentErrorMessage(true);
  122.         return false;
  123.     }
  124.  
  125.     uint32 factionid;
  126.     if (!ExtractUint32KeyFromLink(&args, "Hfaction", factionid))
  127.     {
  128.         return false;
  129.     }
  130.  
  131.     if (!sFactionTemplateStore.LookupEntry(factionid))
  132.     {
  133.         PSendSysMessage(LANG_WRONG_FACTION, factionid);
  134.         SetSentErrorMessage(true);
  135.         return false;
  136.     }
  137.  
  138.     uint32 flag;
  139.     if (!ExtractOptUInt32(&args, flag, chr->GetUInt32Value(UNIT_FIELD_FLAGS)))
  140.     {
  141.         return false;
  142.     }
  143.  
  144.     uint32 npcflag;
  145.     if (!ExtractOptUInt32(&args, npcflag, chr->GetUInt32Value(UNIT_NPC_FLAGS)))
  146.     {
  147.         return false;
  148.     }
  149.  
  150.     uint32  dyflag;
  151.     if (!ExtractOptUInt32(&args, dyflag, chr->GetUInt32Value(UNIT_DYNAMIC_FLAGS)))
  152.     {
  153.         return false;
  154.     }
  155.  
  156.     PSendSysMessage(LANG_YOU_CHANGE_FACTION, chr->GetGUIDLow(), factionid, flag, npcflag, dyflag);
  157.  
  158.     chr->setFaction(factionid);
  159.     chr->SetUInt32Value(UNIT_FIELD_FLAGS, flag);
  160.     chr->SetUInt32Value(UNIT_NPC_FLAGS, npcflag);
  161.     chr->SetUInt32Value(UNIT_DYNAMIC_FLAGS, dyflag);
  162.  
  163.     return true;
  164. }
  165.  
  166.  
  167. //-----------------------Npc Commands-----------------------
  168. // add spawn of creature
  169. //add npc
  170. bool ChatHandler::HandleNpcAddCommand(char* args)
  171. {
  172.     DEBUG_LOG("1");
  173.     Creature* pCreature;
  174.     DEBUG_LOG("1.2");
  175.     if (!*args)
  176.     {
  177.         pCreature = getSelectedCreature();
  178.         DEBUG_LOG("1.5");
  179.     }
  180.  
  181.     DEBUG_LOG("2");
  182.     uint32 id;
  183.     if (!ExtractUint32KeyFromLink(&args, "Hcreature_entry", id))
  184.     {
  185.         DEBUG_LOG("2.1");
  186.         if (!pCreature) {
  187.             return false;
  188.         }
  189.         else {
  190.             id = pCreature->GetEntry();
  191.             DEBUG_LOG("Got entry for creature");
  192.         }
  193.     }
  194.  
  195.     DEBUG_LOG("3");
  196.     CreatureInfo const* cinfo = ObjectMgr::GetCreatureTemplate(id);
  197.     if (!cinfo)
  198.     {
  199.         PSendSysMessage(LANG_COMMAND_INVALIDCREATUREID, id);
  200.         SetSentErrorMessage(true);
  201.         return false;
  202.     }
  203.  
  204.     DEBUG_LOG("4");
  205.     Player* chr = m_session->GetPlayer();
  206.     CreatureCreatePos pos(chr, chr->GetOrientation());
  207.     Map* map = chr->GetMap();
  208.  
  209.     DEBUG_LOG("Before redefining");
  210.     pCreature = new Creature;
  211.     DEBUG_LOG("After redefining");
  212.  
  213.     // used guids from specially reserved range (can be 0 if no free values)
  214.     uint32 lowguid = sObjectMgr.GenerateStaticCreatureLowGuid();
  215.     if (!lowguid)
  216.     {
  217.         SendSysMessage(LANG_NO_FREE_STATIC_GUID_FOR_SPAWN);
  218.         SetSentErrorMessage(true);
  219.         return false;
  220.     }
  221.  
  222.     if (!pCreature->Create(lowguid, pos, cinfo))
  223.     {
  224.         delete pCreature;
  225.         return false;
  226.     }
  227.  
  228.     pCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()));
  229.  
  230.     uint32 db_guid = pCreature->GetGUIDLow();
  231.  
  232.     // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
  233.     pCreature->LoadFromDB(db_guid, map);
  234.  
  235.     return true;
  236. }
  237.  
  238. // add item in vendorlist
  239. bool ChatHandler::HandleNpcAddVendorItemCommand(char* args)
  240. {
  241.     uint32 itemId;
  242.     if (!ExtractUint32KeyFromLink(&args, "Hitem", itemId))
  243.     {
  244.         SendSysMessage(LANG_COMMAND_NEEDITEMSEND);
  245.         SetSentErrorMessage(true);
  246.         return false;
  247.     }
  248.  
  249.     uint32 maxcount;
  250.     if (!ExtractOptUInt32(&args, maxcount, 0))
  251.     {
  252.         return false;
  253.     }
  254.  
  255.     uint32 incrtime;
  256.     if (!ExtractOptUInt32(&args, incrtime, 0))
  257.     {
  258.         return false;
  259.     }
  260.  
  261.     uint32 extendedcost;
  262.     if (!ExtractOptUInt32(&args, extendedcost, 0))
  263.     {
  264.         return false;
  265.     }
  266.  
  267.     Creature* vendor = getSelectedCreature();
  268.  
  269.     uint32 vendor_entry = vendor ? vendor->GetEntry() : 0;
  270.  
  271.     if (!sObjectMgr.IsVendorItemValid(false, "npc_vendor", vendor_entry, itemId, maxcount, incrtime, extendedcost, 0, m_session->GetPlayer()))
  272.     {
  273.         SetSentErrorMessage(true);
  274.         return false;
  275.     }
  276.  
  277.     sObjectMgr.AddVendorItem(vendor_entry, itemId, maxcount, incrtime, extendedcost);
  278.  
  279.     ItemPrototype const* pProto = ObjectMgr::GetItemPrototype(itemId);
  280.  
  281.     PSendSysMessage(LANG_ITEM_ADDED_TO_LIST, itemId, pProto->Name1, maxcount, incrtime, extendedcost);
  282.     return true;
  283. }
  284.  
  285. // del item from vendor list
  286. bool ChatHandler::HandleNpcDelVendorItemCommand(char* args)
  287. {
  288.     if (!*args)
  289.     {
  290.         return false;
  291.     }
  292.  
  293.     Creature* vendor = getSelectedCreature();
  294.     if (!vendor || !vendor->IsVendor())
  295.     {
  296.         SendSysMessage(LANG_COMMAND_VENDORSELECTION);
  297.         SetSentErrorMessage(true);
  298.         return false;
  299.     }
  300.  
  301.     uint32 itemId;
  302.     if (!ExtractUint32KeyFromLink(&args, "Hitem", itemId))
  303.     {
  304.         SendSysMessage(LANG_COMMAND_NEEDITEMSEND);
  305.         SetSentErrorMessage(true);
  306.         return false;
  307.     }
  308.  
  309.     if (!sObjectMgr.RemoveVendorItem(vendor->GetEntry(), itemId))
  310.     {
  311.         PSendSysMessage(LANG_ITEM_NOT_IN_LIST, itemId);
  312.         SetSentErrorMessage(true);
  313.         return false;
  314.     }
  315.  
  316.     ItemPrototype const* pProto = ObjectMgr::GetItemPrototype(itemId);
  317.  
  318.     PSendSysMessage(LANG_ITEM_DELETED_FROM_LIST, itemId, pProto->Name1);
  319.     return true;
  320. }
  321.  
  322. // show info about AI
  323. bool ChatHandler::HandleNpcAIInfoCommand(char* /*args*/)
  324. {
  325.     Creature* pTarget = getSelectedCreature();
  326.  
  327.     if (!pTarget)
  328.     {
  329.         SendSysMessage(LANG_SELECT_CREATURE);
  330.         SetSentErrorMessage(true);
  331.         return false;
  332.     }
  333.  
  334.     PSendSysMessage(LANG_NPC_AI_HEADER, pTarget->GetEntry());
  335.  
  336.     std::string strScript = pTarget->GetScriptName();
  337.     std::string strAI = pTarget->GetAIName();
  338.     char const* cstrAIClass = pTarget->AI() ? typeid(*pTarget->AI()).name() : " - ";
  339.  
  340.     PSendSysMessage(LANG_NPC_AI_NAMES,
  341.                     strAI.empty() ? " - " : strAI.c_str(),
  342.                     cstrAIClass ? cstrAIClass : " - ",
  343.                     strScript.empty() ? " - " : strScript.c_str());
  344.  
  345.     if (pTarget->AI())
  346.     {
  347.         pTarget->AI()->GetAIInformation(*this);
  348.     }
  349.  
  350.     return true;
  351. }
  352.  
  353. // change level of creature or pet
  354. bool ChatHandler::HandleNpcChangeLevelCommand(char* args)
  355. {
  356.     if (!*args)
  357.     {
  358.         return false;
  359.     }
  360.  
  361.     uint8 lvl = (uint8)atoi(args);
  362.     if (lvl < 1 || lvl > sWorld.getConfig(CONFIG_UINT32_MAX_PLAYER_LEVEL) + 3)
  363.     {
  364.         SendSysMessage(LANG_BAD_VALUE);
  365.         SetSentErrorMessage(true);
  366.         return false;
  367.     }
  368.  
  369.     Creature* pCreature = getSelectedCreature();
  370.     if (!pCreature)
  371.     {
  372.         SendSysMessage(LANG_SELECT_CREATURE);
  373.         SetSentErrorMessage(true);
  374.         return false;
  375.     }
  376.  
  377.     if (pCreature->IsPet())
  378.     {
  379.         ((Pet*)pCreature)->GivePetLevel(lvl);
  380.     }
  381.     else
  382.     {
  383.         pCreature->SetMaxHealth(100 + 30 * lvl);
  384.         pCreature->SetHealth(100 + 30 * lvl);
  385.         pCreature->SetLevel(lvl);
  386.  
  387.         if (pCreature->HasStaticDBSpawnData())
  388.         {
  389.             pCreature->SaveToDB();
  390.         }
  391.     }
  392.  
  393.     return true;
  394. }
  395.  
  396. // set npcflag of creature
  397. bool ChatHandler::HandleNpcFlagCommand(char* args)
  398. {
  399.     if (!*args)
  400.     {
  401.         return false;
  402.     }
  403.  
  404.     uint32 npcFlags = (uint32)atoi(args);
  405.  
  406.     Creature* pCreature = getSelectedCreature();
  407.  
  408.     if (!pCreature)
  409.     {
  410.         SendSysMessage(LANG_SELECT_CREATURE);
  411.         SetSentErrorMessage(true);
  412.         return false;
  413.     }
  414.  
  415.     pCreature->SetUInt32Value(UNIT_NPC_FLAGS, npcFlags);
  416.  
  417.     WorldDatabase.PExecuteLog("UPDATE `creature_template` SET `NpcFlags` = '%u' WHERE `entry` = '%u'", npcFlags, pCreature->GetEntry());
  418.  
  419.     SendSysMessage(LANG_VALUE_SAVED_REJOIN);
  420.  
  421.     return true;
  422. }
  423.  
  424. //removes creature
  425. bool ChatHandler::HandleNpcDeleteCommand(char* args)
  426. {
  427.     Creature* unit = NULL;
  428.     DEBUG_LOG("1");
  429.     if (*args)
  430.     {
  431.         DEBUG_LOG("ARGS");
  432.         // number or [name] Shift-click form |color|Hcreature:creature_guid|h[name]|h|r
  433.         uint32 lowguid;
  434.         if (!ExtractUint32KeyFromLink(&args, "Hcreature", lowguid))
  435.         {
  436.             return false;
  437.         }
  438.  
  439.         if (!lowguid)
  440.         {
  441.             return false;
  442.         }
  443.  
  444.         if (CreatureData const* data = sObjectMgr.GetCreatureData(lowguid))
  445.         {
  446.             unit = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
  447.         }
  448.     }
  449.     else
  450.     {
  451.         DEBUG_LOG("NO ARGS");
  452.         unit = getSelectedCreature();
  453.     }
  454.  
  455.     if (!unit)
  456.     {
  457.         DEBUG_LOG("3");
  458.         SendSysMessage(LANG_SELECT_CREATURE);
  459.         SetSentErrorMessage(true);
  460.         return false;
  461.     }
  462.  
  463.     switch (unit->GetSubtype())
  464.     {
  465.         case CREATURE_SUBTYPE_GENERIC:
  466.         {
  467.             unit->CombatStop();
  468.             if (CreatureData const* data = sObjectMgr.GetCreatureData(unit->GetGUIDLow()))
  469.             {
  470.                 Creature::AddToRemoveListInMaps(unit->GetGUIDLow(), data);
  471.                 Creature::DeleteFromDB(unit->GetGUIDLow(), data);
  472.             }
  473.             else
  474.             {
  475.                 unit->AddObjectToRemoveList();
  476.             }
  477.             break;
  478.         }
  479.         case CREATURE_SUBTYPE_PET:
  480.             ((Pet*)unit)->Unsummon(PET_SAVE_AS_CURRENT);
  481.             break;
  482.         case CREATURE_SUBTYPE_TOTEM:
  483.             ((Totem*)unit)->UnSummon();
  484.             break;
  485.         case CREATURE_SUBTYPE_TEMPORARY_SUMMON:
  486.             ((TemporarySummon*)unit)->UnSummon();
  487.             break;
  488.         default:
  489.             return false;
  490.     }
  491.  
  492.     SendSysMessage(LANG_COMMAND_DELCREATMESSAGE);
  493.  
  494.     return true;
  495. }
  496.  
  497. // move selected creature
  498. bool ChatHandler::HandleNpcMoveCommand(char* args)
  499. {
  500.     Creature* pCreature = getSelectedCreature();
  501.  
  502.     uint32 lowguid = 0;
  503.     if (pCreature) {
  504.         //DEBUG_LOG("Added creature");
  505.         //lowguid = pCreature->GetGUIDLow();
  506.         //CreatureInfo const* cInfo = sObjectMgr.GetCreatureTemplate(lowguid);
  507.         //DEBUG_LOG((char*)pCreature->GetEntry());
  508.         ChatHandler::HandleNpcAddCommand("");
  509.         DEBUG_LOG("Added creature");
  510.         ChatHandler::HandleNpcDeleteCommand("");
  511.         PSendSysMessage(LANG_COMMAND_CREATUREMOVED);
  512.         return true;
  513.     }
  514.     else {
  515.         return false;
  516.     }
  517.     /*
  518.     uint32 lowguid = 0;
  519.     Player* player = m_session->GetPlayer();
  520.  
  521.     if (!pCreature)
  522.     {
  523.         // number or [name] Shift-click form |color|Hcreature:creature_guid|h[name]|h|r
  524.         if (!ExtractUint32KeyFromLink(&args, "Hcreature", lowguid))
  525.         {
  526.             return false;
  527.         }
  528.  
  529.         CreatureData const* data = sObjectMgr.GetCreatureData(lowguid);
  530.         if (!data)
  531.         {
  532.             PSendSysMessage(LANG_COMMAND_CREATGUIDNOTFOUND, lowguid);
  533.             SetSentErrorMessage(true);
  534.             return false;
  535.         }
  536.  
  537.         if (player->GetMapId() != data->mapid)
  538.         {
  539.             PSendSysMessage(LANG_COMMAND_CREATUREATSAMEMAP, lowguid);
  540.             SetSentErrorMessage(true);
  541.             return false;
  542.         }
  543.  
  544.         pCreature = player->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
  545.     }
  546.     else
  547.     {
  548.         lowguid = pCreature->GetGUIDLow();
  549.     }
  550.  
  551.     float x = player->GetPositionX();
  552.     float y = player->GetPositionY();
  553.     float z = player->GetPositionZ();
  554.     float o = player->GetOrientation();
  555.  
  556.     if (pCreature)
  557.     {
  558.         if (CreatureData const* data = sObjectMgr.GetCreatureData(pCreature->GetGUIDLow()))
  559.         {
  560.             const_cast<CreatureData*>(data)->posX = x;
  561.             const_cast<CreatureData*>(data)->posY = y;
  562.             const_cast<CreatureData*>(data)->posZ = z;
  563.             const_cast<CreatureData*>(data)->orientation = o;
  564.         }
  565.         pCreature->GetMap()->CreatureRelocation(pCreature, x, y, z, o);
  566.         pCreature->GetMotionMaster()->Initialize();
  567.         ChatHandler::HandleReloadAllNpcCommand((char*)"");
  568.         if (pCreature->IsAlive())                           // dead creature will reset movement generator at respawn
  569.         {
  570.             pCreature->SetDeathState(JUST_DIED);
  571.             pCreature->Respawn();
  572.         }
  573.     }
  574.     */
  575.     //WorldDatabase.PExecuteLog("UPDATE `creature` SET `position_x` = '%f', `position_y` = '%f', `position_z` = '%f', `orientation` = '%f' WHERE `guid` = '%u'", x, y, z, o, lowguid);
  576.    
  577. }
  578.  
  579. /**HandleNpcSetMoveTypeCommand
  580.  * Set the movement type for an NPC.<br/>
  581.  * <br/>
  582.  * Valid movement types are:
  583.  * <ul>
  584.  * <li> stay - NPC wont move </li>
  585.  * <li> random - NPC will move randomly according to the spawndist </li>
  586.  * <li> way - NPC will move with given waypoints set </li>
  587.  * </ul>
  588.  * additional parameter: NODEL - so no waypoints are deleted, if you
  589.  *                       change the movement type
  590.  */
  591. bool ChatHandler::HandleNpcSetMoveTypeCommand(char* args)
  592. {
  593.     // 3 arguments:
  594.     // GUID (optional - you can also select the creature)
  595.     // stay|random|way (determines the kind of movement)
  596.     // NODEL (optional - tells the system NOT to delete any waypoints)
  597.     //        this is very handy if you want to do waypoints, that are
  598.     //        later switched on/off according to special events (like escort
  599.     //        quests, etc)
  600.  
  601.     uint32 lowguid;
  602.     Creature* pCreature;
  603.     if (!ExtractUInt32(&args, lowguid))                     // case .setmovetype $move_type (with selected creature)
  604.     {
  605.         pCreature = getSelectedCreature();
  606.         if (!pCreature || !pCreature->HasStaticDBSpawnData())
  607.         {
  608.             return false;
  609.         }
  610.         lowguid = pCreature->GetGUIDLow();
  611.     }
  612.     else                                                    // case .setmovetype #creature_guid $move_type (with guid)
  613.     {
  614.         CreatureData const* data = sObjectMgr.GetCreatureData(lowguid);
  615.         if (!data)
  616.         {
  617.             PSendSysMessage(LANG_COMMAND_CREATGUIDNOTFOUND, lowguid);
  618.             SetSentErrorMessage(true);
  619.             return false;
  620.         }
  621.  
  622.         Player* player = m_session->GetPlayer();
  623.  
  624.         if (player->GetMapId() != data->mapid)
  625.         {
  626.             PSendSysMessage(LANG_COMMAND_CREATUREATSAMEMAP, lowguid);
  627.             SetSentErrorMessage(true);
  628.             return false;
  629.         }
  630.  
  631.         pCreature = player->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
  632.     }
  633.  
  634.     MovementGeneratorType move_type;
  635.     char* type_str = ExtractLiteralArg(&args);
  636.     if (!type_str)
  637.     {
  638.         return false;
  639.     }
  640.  
  641.     if (strncmp(type_str, "stay", strlen(type_str)) == 0)
  642.     {
  643.         move_type = IDLE_MOTION_TYPE;
  644.     }
  645.     else if (strncmp(type_str, "random", strlen(type_str)) == 0)
  646.     {
  647.         move_type = RANDOM_MOTION_TYPE;
  648.     }
  649.     else if (strncmp(type_str, "way", strlen(type_str)) == 0)
  650.     {
  651.         move_type = WAYPOINT_MOTION_TYPE;
  652.     }
  653.     else
  654.     {
  655.         return false;
  656.     }
  657.  
  658.     bool doNotDelete = ExtractLiteralArg(&args, "NODEL") != NULL;
  659.     if (!doNotDelete && *args)                              // need fail if false in result wrong literal
  660.     {
  661.         return false;
  662.     }
  663.  
  664.     // now lowguid is low guid really existing creature
  665.     // and pCreature point (maybe) to this creature or NULL
  666.  
  667.     // update movement type
  668.     if (!doNotDelete)
  669.     {
  670.         sWaypointMgr.DeletePath(lowguid);
  671.     }
  672.  
  673.     if (pCreature)
  674.     {
  675.         pCreature->SetDefaultMovementType(move_type);
  676.         pCreature->GetMotionMaster()->Initialize();
  677.         if (pCreature->IsAlive())                           // dead creature will reset movement generator at respawn
  678.         {
  679.             pCreature->SetDeathState(JUST_DIED);
  680.             pCreature->Respawn();
  681.         }
  682.         pCreature->SaveToDB();
  683.     }
  684.  
  685.     if (doNotDelete)
  686.     {
  687.         PSendSysMessage(LANG_MOVE_TYPE_SET_NODEL, type_str);
  688.     }
  689.     else
  690.     {
  691.         PSendSysMessage(LANG_MOVE_TYPE_SET, type_str);
  692.     }
  693.  
  694.     return true;
  695. }
  696.  
  697. // set model of creature
  698. bool ChatHandler::HandleNpcSetModelCommand(char* args)
  699. {
  700.     if (!*args)
  701.     {
  702.         return false;
  703.     }
  704.  
  705.     uint32 displayId = (uint32)atoi(args);
  706.  
  707.     Creature* pCreature = getSelectedCreature();
  708.  
  709.     if (!pCreature || pCreature->IsPet())
  710.     {
  711.         SendSysMessage(LANG_SELECT_CREATURE);
  712.         SetSentErrorMessage(true);
  713.         return false;
  714.     }
  715.  
  716.     pCreature->SetDisplayId(displayId);
  717.     pCreature->SetNativeDisplayId(displayId);
  718.  
  719.     if (pCreature->HasStaticDBSpawnData())
  720.     {
  721.         pCreature->SaveToDB();
  722.     }
  723.  
  724.     return true;
  725. }
  726.  
  727. // set faction of creature
  728. bool ChatHandler::HandleNpcFactionIdCommand(char* args)
  729. {
  730.     if (!*args)
  731.     {
  732.         return false;
  733.     }
  734.  
  735.     uint32 factionId = (uint32)atoi(args);
  736.  
  737.     if (!sFactionTemplateStore.LookupEntry(factionId))
  738.     {
  739.         PSendSysMessage(LANG_WRONG_FACTION, factionId);
  740.         SetSentErrorMessage(true);
  741.         return false;
  742.     }
  743.  
  744.     Creature* pCreature = getSelectedCreature();
  745.  
  746.     if (!pCreature)
  747.     {
  748.         SendSysMessage(LANG_SELECT_CREATURE);
  749.         SetSentErrorMessage(true);
  750.         return false;
  751.     }
  752.  
  753.     pCreature->setFaction(factionId);
  754.  
  755.     // faction is set in creature_template - not inside creature
  756.  
  757.     // update in memory
  758.     if (CreatureInfo const* cinfo = pCreature->GetCreatureInfo())
  759.     {
  760.         const_cast<CreatureInfo*>(cinfo)->FactionAlliance = factionId;
  761.         const_cast<CreatureInfo*>(cinfo)->FactionHorde = factionId;
  762.     }
  763.  
  764.     // and DB
  765.     WorldDatabase.PExecuteLog("UPDATE `creature_template` SET `FactionAlliance` = '%u', `FactionHorde` = '%u' WHERE `entry` = '%u'", factionId, factionId, pCreature->GetEntry());
  766.  
  767.     return true;
  768. }
  769.  
  770. // set spawn dist of creature
  771. bool ChatHandler::HandleNpcSpawnDistCommand(char* args)
  772. {
  773.     if (!*args)
  774.     {
  775.         return false;
  776.     }
  777.  
  778.     float option = (float)atof(args);
  779.     if (option < 0.0f)
  780.     {
  781.         SendSysMessage(LANG_BAD_VALUE);
  782.         return false;
  783.     }
  784.  
  785.     MovementGeneratorType mtype = IDLE_MOTION_TYPE;
  786.     if (option > 0.0f)
  787.     {
  788.         mtype = RANDOM_MOTION_TYPE;
  789.     }
  790.  
  791.     Creature* pCreature = getSelectedCreature();
  792.     uint32 u_guidlow = 0;
  793.  
  794.     if (pCreature)
  795.     {
  796.         u_guidlow = pCreature->GetGUIDLow();
  797.     }
  798.     else
  799.     {
  800.         return false;
  801.     }
  802.  
  803.     pCreature->SetRespawnRadius((float)option);
  804.     pCreature->SetDefaultMovementType(mtype);
  805.     pCreature->GetMotionMaster()->Initialize();
  806.     if (pCreature->IsAlive())                               // dead creature will reset movement generator at respawn
  807.     {
  808.         pCreature->SetDeathState(JUST_DIED);
  809.         pCreature->Respawn();
  810.     }
  811.  
  812.     WorldDatabase.PExecuteLog("UPDATE `creature` SET `spawndist`=%f, `MovementType`=%i WHERE `guid`=%u", option, mtype, u_guidlow);
  813.     PSendSysMessage(LANG_COMMAND_SPAWNDIST, option);
  814.     return true;
  815. }
  816. // spawn time handling
  817. bool ChatHandler::HandleNpcSpawnTimeCommand(char* args)
  818. {
  819.     uint32 stime;
  820.     if (!ExtractUInt32(&args, stime))
  821.     {
  822.         return false;
  823.     }
  824.  
  825.     Creature* pCreature = getSelectedCreature();
  826.     if (!pCreature)
  827.     {
  828.         PSendSysMessage(LANG_SELECT_CREATURE);
  829.         SetSentErrorMessage(true);
  830.         return false;
  831.     }
  832.  
  833.     uint32 u_guidlow = pCreature->GetGUIDLow();
  834.  
  835.     WorldDatabase.PExecuteLog("UPDATE `creature` SET `spawntimesecs`=%i WHERE `guid`=%u", stime, u_guidlow);
  836.     pCreature->SetRespawnDelay(stime);
  837.     PSendSysMessage(LANG_COMMAND_SPAWNTIME, stime);
  838.  
  839.     return true;
  840. }
  841. // npc follow handling
  842. bool ChatHandler::HandleNpcFollowCommand(char* /*args*/)
  843. {
  844.     Player* player = m_session->GetPlayer();
  845.     Creature* creature = getSelectedCreature();
  846.  
  847.     if (!creature)
  848.     {
  849.         PSendSysMessage(LANG_SELECT_CREATURE);
  850.         SetSentErrorMessage(true);
  851.         return false;
  852.     }
  853.  
  854.     // Follow player - Using pet's default dist and angle
  855.     creature->GetMotionMaster()->MoveFollow(player, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
  856.  
  857.     PSendSysMessage(LANG_CREATURE_FOLLOW_YOU_NOW, creature->GetName());
  858.     return true;
  859. }
  860. // npc unfollow handling
  861. bool ChatHandler::HandleNpcUnFollowCommand(char* /*args*/)
  862. {
  863.     Player* player = m_session->GetPlayer();
  864.     Creature* creature = getSelectedCreature();
  865.  
  866.     if (!creature)
  867.     {
  868.         PSendSysMessage(LANG_SELECT_CREATURE);
  869.         SetSentErrorMessage(true);
  870.         return false;
  871.     }
  872.  
  873.     MotionMaster* creatureMotion = creature->GetMotionMaster();
  874.     if (creatureMotion->empty() ||
  875.         creatureMotion->GetCurrentMovementGeneratorType() != FOLLOW_MOTION_TYPE)
  876.     {
  877.         PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU, creature->GetName());
  878.         SetSentErrorMessage(true);
  879.         return false;
  880.     }
  881.  
  882.     FollowMovementGenerator<Creature> const* mgen = static_cast<FollowMovementGenerator<Creature> const*>(creatureMotion->top());
  883.  
  884.     if (mgen->GetTarget() != player)
  885.     {
  886.         PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU, creature->GetName());
  887.         SetSentErrorMessage(true);
  888.         return false;
  889.     }
  890.  
  891.     // reset movement
  892.     creatureMotion->MovementExpired(true);
  893.  
  894.     PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU_NOW, creature->GetName());
  895.     return true;
  896. }
  897. // npc tame handling
  898. bool ChatHandler::HandleNpcTameCommand(char* /*args*/)
  899. {
  900.     Creature* creatureTarget = getSelectedCreature();
  901.  
  902.     if (!creatureTarget || creatureTarget->IsPet())
  903.     {
  904.         PSendSysMessage(LANG_SELECT_CREATURE);
  905.         SetSentErrorMessage(true);
  906.         return false;
  907.     }
  908.  
  909.     Player* player = m_session->GetPlayer();
  910.  
  911.     if (player->GetPetGuid())
  912.     {
  913.         SendSysMessage(LANG_YOU_ALREADY_HAVE_PET);
  914.         SetSentErrorMessage(true);
  915.         return false;
  916.     }
  917.  
  918.     player->CastSpell(creatureTarget, 13481, true);         // Tame Beast, triggered effect
  919.     return true;
  920. }
  921.  
  922. // npc deathstate handling
  923. bool ChatHandler::HandleNpcSetDeathStateCommand(char* args)
  924. {
  925.     bool value;
  926.     if (!ExtractOnOff(&args, value))
  927.     {
  928.         SendSysMessage(LANG_USE_BOL);
  929.         SetSentErrorMessage(true);
  930.         return false;
  931.     }
  932.  
  933.     Creature* pCreature = getSelectedCreature();
  934.     if (!pCreature || !pCreature->HasStaticDBSpawnData())
  935.     {
  936.         SendSysMessage(LANG_SELECT_CREATURE);
  937.         SetSentErrorMessage(true);
  938.         return false;
  939.     }
  940.  
  941.     if (value)
  942.     {
  943.         pCreature->SetDeadByDefault(true);
  944.     }
  945.     else
  946.     {
  947.         pCreature->SetDeadByDefault(false);
  948.     }
  949.  
  950.     pCreature->SaveToDB();
  951.     pCreature->Respawn();
  952.  
  953.     return true;
  954. }
  955.  
  956. // TODO: NpcCommands that need to be fixed :
  957.  
  958. bool ChatHandler::HandleNpcNameCommand(char* /*args*/)
  959. {
  960.     /* Temp. disabled
  961.     if (!*args)
  962.     {
  963.         return false;
  964.     }
  965.  
  966.     if (strlen((char*)args)>75)
  967.     {
  968.         PSendSysMessage(LANG_TOO_LONG_NAME, strlen((char*)args)-75);
  969.         return true;
  970.     }
  971.  
  972.     for (uint8 i = 0; i < strlen(args); ++i)
  973.     {
  974.         if (!isalpha(args[i]) && args[i]!=' ')
  975.         {
  976.             SendSysMessage(LANG_CHARS_ONLY);
  977.             return false;
  978.         }
  979.     }
  980.  
  981.     ObjectGuid guid = m_session->GetPlayer()->GetSelectionGuid();
  982.     if (guid.IsEmpty())
  983.     {
  984.         SendSysMessage(LANG_NO_SELECTION);
  985.         return true;
  986.     }
  987.  
  988.     Creature* pCreature = sObjectAccessor.GetCreature(*m_session->GetPlayer(), guid);
  989.  
  990.     if (!pCreature)
  991.     {
  992.         SendSysMessage(LANG_SELECT_CREATURE);
  993.         return true;
  994.     }
  995.  
  996.     pCreature->SetName(args);
  997.     uint32 idname = sObjectMgr.AddCreatureTemplate(pCreature->GetName());
  998.     pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
  999.  
  1000.     pCreature->SaveToDB();
  1001.     */
  1002.  
  1003.     return true;
  1004. }
  1005.  
  1006. bool ChatHandler::HandleNpcSubNameCommand(char* /*args*/)
  1007. {
  1008.     /* Temp. disabled
  1009.  
  1010.     if (!*args)
  1011.     {
  1012.         args = "";
  1013.     }
  1014.  
  1015.     if (strlen((char*)args)>75)
  1016.     {
  1017.         PSendSysMessage(LANG_TOO_LONG_SUBNAME, strlen((char*)args)-75);
  1018.         return true;
  1019.     }
  1020.  
  1021.     for (uint8 i = 0; i < strlen(args); ++i)
  1022.     {
  1023.         if (!isalpha(args[i]) && args[i]!=' ')
  1024.         {
  1025.             SendSysMessage(LANG_CHARS_ONLY);
  1026.             return false;
  1027.         }
  1028.     }
  1029.  
  1030.     ObjectGuid guid = m_session->GetPlayer()->GetSelectionGuid();
  1031.     if (guid.IsEmpty())
  1032.     {
  1033.         SendSysMessage(LANG_NO_SELECTION);
  1034.         return true;
  1035.     }
  1036.  
  1037.     Creature* pCreature = sObjectAccessor.GetCreature(*m_session->GetPlayer(), guid);
  1038.  
  1039.     if (!pCreature)
  1040.     {
  1041.         SendSysMessage(LANG_SELECT_CREATURE);
  1042.         return true;
  1043.     }
  1044.  
  1045.     uint32 idname = sObjectMgr.AddCreatureSubName(pCreature->GetName(),args,pCreature->GetUInt32Value(UNIT_FIELD_DISPLAYID));
  1046.     pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
  1047.  
  1048.     pCreature->SaveToDB();
  1049.     */
  1050.     return true;
  1051. }
  1052.  
  1053. //-----------------------Npc Commands-----------------------
  1054. bool ChatHandler::HandleNpcAllowMovementCommand(char* /*args*/)
  1055. {
  1056.     if (sWorld.getAllowMovement())
  1057.     {
  1058.         sWorld.SetAllowMovement(false);
  1059.         SendSysMessage(LANG_CREATURE_MOVE_DISABLED);
  1060.     }
  1061.     else
  1062.     {
  1063.         sWorld.SetAllowMovement(true);
  1064.         SendSysMessage(LANG_CREATURE_MOVE_ENABLED);
  1065.     }
  1066.     return true;
  1067. }
  1068.  
  1069. bool ChatHandler::HandleNpcChangeEntryCommand(char* args)
  1070. {
  1071.     if (!*args)
  1072.     {
  1073.         return false;
  1074.     }
  1075.  
  1076.     uint32 newEntryNum = atoi(args);
  1077.     if (!newEntryNum)
  1078.     {
  1079.         return false;
  1080.     }
  1081.  
  1082.     Unit* unit = getSelectedUnit();
  1083.     if (!unit || unit->GetTypeId() != TYPEID_UNIT)
  1084.     {
  1085.         SendSysMessage(LANG_SELECT_CREATURE);
  1086.         SetSentErrorMessage(true);
  1087.         return false;
  1088.     }
  1089.     Creature* creature = (Creature*)unit;
  1090.     if (creature->UpdateEntry(newEntryNum))
  1091.     {
  1092.         SendSysMessage(LANG_DONE);
  1093.     }
  1094.     else
  1095.     {
  1096.         SendSysMessage(LANG_ERROR);
  1097.     }
  1098.     return true;
  1099. }
  1100.  
  1101. bool ChatHandler::HandleNpcInfoCommand(char* /*args*/)
  1102. {
  1103.     Creature* target = getSelectedCreature();
  1104.  
  1105.     if (!target)
  1106.     {
  1107.         SendSysMessage(LANG_SELECT_CREATURE);
  1108.         SetSentErrorMessage(true);
  1109.         return false;
  1110.     }
  1111.  
  1112.     uint32 faction = target->getFaction();
  1113.     uint32 npcflags = target->GetUInt32Value(UNIT_NPC_FLAGS);
  1114.     uint32 displayid = target->GetDisplayId();
  1115.     uint32 nativeid = target->GetNativeDisplayId();
  1116.     uint32 Entry = target->GetEntry();
  1117.     CreatureInfo const* cInfo = target->GetCreatureInfo();
  1118.  
  1119.     time_t curRespawnDelay = target->GetRespawnTimeEx() - time(NULL);
  1120.     if (curRespawnDelay < 0)
  1121.     {
  1122.         curRespawnDelay = 0;
  1123.     }
  1124.     std::string curRespawnDelayStr = secsToTimeString(curRespawnDelay, true);
  1125.     std::string defRespawnDelayStr = secsToTimeString(target->GetRespawnDelay(), true);
  1126.  
  1127.     // Send information dependend on difficulty mode
  1128.     CreatureInfo const* baseInfo = ObjectMgr::GetCreatureTemplate(Entry);
  1129.     if (baseInfo->HeroicEntry == target->GetCreatureInfo()->Entry)
  1130.         PSendSysMessage(LANG_NPCINFO_CHAR_DIFFICULTY, target->GetGuidStr().c_str(), faction, npcflags,
  1131.                         Entry, target->GetCreatureInfo()->Entry, 1,
  1132.                         displayid, nativeid);
  1133.     else
  1134.     {
  1135.         PSendSysMessage(LANG_NPCINFO_CHAR, target->GetGuidStr().c_str(), faction, npcflags, Entry, displayid, nativeid);
  1136.     }
  1137.     PSendSysMessage(LANG_NPCINFO_LEVEL, target->getLevel());
  1138.     PSendSysMessage(LANG_NPCINFO_HEALTH, target->GetCreateHealth(), target->GetMaxHealth(), target->GetHealth());
  1139.     PSendSysMessage(LANG_NPCINFO_FLAGS, target->GetUInt32Value(UNIT_FIELD_FLAGS), target->GetUInt32Value(UNIT_DYNAMIC_FLAGS), target->getFaction());
  1140.     PSendSysMessage(LANG_COMMAND_RAWPAWNTIMES, defRespawnDelayStr.c_str(), curRespawnDelayStr.c_str());
  1141.     PSendSysMessage(LANG_NPCINFO_LOOT, cInfo->LootId, cInfo->PickpocketLootId, cInfo->SkinningLootId);
  1142.     PSendSysMessage(LANG_NPCINFO_DUNGEON_ID, target->GetInstanceId());
  1143.     PSendSysMessage(LANG_NPCINFO_POSITION, float(target->GetPositionX()), float(target->GetPositionY()), float(target->GetPositionZ()));
  1144.  
  1145.     if ((npcflags & UNIT_NPC_FLAG_VENDOR))
  1146.     {
  1147.         SendSysMessage(LANG_NPCINFO_VENDOR);
  1148.     }
  1149.     if ((npcflags & UNIT_NPC_FLAG_TRAINER))
  1150.     {
  1151.         SendSysMessage(LANG_NPCINFO_TRAINER);
  1152.     }
  1153.  
  1154.     ShowNpcOrGoSpawnInformation<Creature>(target->GetGUIDLow());
  1155.     return true;
  1156. }
  1157.  
  1158. // play npc emote
  1159. bool ChatHandler::HandleNpcPlayEmoteCommand(char* args)
  1160. {
  1161.     uint32 emote = atoi(args);
  1162.  
  1163.     Creature* target = getSelectedCreature();
  1164.     if (!target)
  1165.     {
  1166.         SendSysMessage(LANG_SELECT_CREATURE);
  1167.         SetSentErrorMessage(true);
  1168.         return false;
  1169.     }
  1170.  
  1171.     target->HandleEmote(emote);
  1172.  
  1173.     return true;
  1174. }
  1175.  
  1176. // TODO: NpcCommands that needs to be fixed :
  1177. bool ChatHandler::HandleNpcAddWeaponCommand(char* /*args*/)
  1178. {
  1179.     /*if (!*args)
  1180.     return false;
  1181.  
  1182.     ObjectGuid guid = m_session->GetPlayer()->GetSelectionGuid();
  1183.     if (guid.IsEmpty())
  1184.     {
  1185.         SendSysMessage(LANG_NO_SELECTION);
  1186.         return true;
  1187.     }
  1188.  
  1189.     Creature *pCreature = sObjectAccessor.GetCreature(*m_session->GetPlayer(), guid);
  1190.  
  1191.     if(!pCreature)
  1192.     {
  1193.         SendSysMessage(LANG_SELECT_CREATURE);
  1194.         return true;
  1195.     }
  1196.  
  1197.     char* pSlotID = strtok((char*)args, " ");
  1198.     if (!pSlotID)
  1199.     {
  1200.         return false;
  1201.     }
  1202.  
  1203.     char* pItemID = strtok(NULL, " ");
  1204.     if (!pItemID)
  1205.     {
  1206.         return false;
  1207.     }
  1208.  
  1209.     uint32 ItemID = atoi(pItemID);
  1210.     uint32 SlotID = atoi(pSlotID);
  1211.  
  1212.     ItemPrototype* tmpItem = ObjectMgr::GetItemPrototype(ItemID);
  1213.  
  1214.     bool added = false;
  1215.     if(tmpItem)
  1216.     {
  1217.         switch(SlotID)
  1218.         {
  1219.             case 1:
  1220.                 pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY, ItemID);
  1221.                 added = true;
  1222.                 break;
  1223.             case 2:
  1224.                 pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY_01, ItemID);
  1225.                 added = true;
  1226.                 break;
  1227.             case 3:
  1228.                 pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY_02, ItemID);
  1229.                 added = true;
  1230.                 break;
  1231.             default:
  1232.                 PSendSysMessage(LANG_ITEM_SLOT_NOT_EXIST,SlotID);
  1233.                 added = false;
  1234.                 break;
  1235.         }
  1236.  
  1237.         if(added)
  1238.         {
  1239.             PSendSysMessage(LANG_ITEM_ADDED_TO_SLOT,ItemID,tmpItem->Name1,SlotID);
  1240.         }
  1241.     }
  1242.     else
  1243.     {
  1244.         PSendSysMessage(LANG_ITEM_NOT_FOUND,ItemID);
  1245.         return true;
  1246.     }
  1247.     */
  1248.     return true;
  1249. }
  1250. //----------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement