Advertisement
Guest User

[C++]: Phasing System, By Tommy/QQrofl

a guest
Apr 1st, 2012
2,897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 32.45 KB | None | 0 0
  1. /*******************************************************************************************
  2. *              _   _                                      __   _                           *
  3. *              | \ | |   ___    _ __ ___    ___    ___    / _| | |_                        *
  4. *              |  \| |  / _ \  | '_ ` _ \  / __|  / _ \  | |_  | __|                       *
  5. *              | |\  | | (_) | | | | | | | \__ \ | (_) | |  _| | |_                        *
  6. *              |_| \_|  \___/  |_| |_| |_| |___/  \___/  |_|    \__|                       *
  7. *                               The policy of Nomsoftware states: Releasing our software   *
  8. *                               or any other files are protected. You cannot re-release    *
  9. *                               anywhere unless you were given permission.                 *
  10. *                           (C) Nomsoftware 2011-2012. All rights reserved.                *
  11. ********************************************************************************************/
  12. #include "ScriptPCH.h"
  13. #include "Chat.h"
  14. #include <stdarg.h>
  15. #include "GameObject.h"
  16. #include "PoolMgr.h"
  17. #include "ObjectAccessor.h"
  18. #include "Transport.h"
  19. using namespace std;
  20.  
  21. void CreatePhase(Player * player, bool IsMember, uint32 phase)
  22. {
  23.     ostringstream ss;
  24.     if(IsMember == false)
  25.     {
  26.         ss << "INSERT INTO phase VALUES("
  27.             << "'" << player->GetGUID() << "',"
  28.             << "'" << player->GetName() << "',"
  29.             << "'" << phase << "',"
  30.             << "'" << phase << "',"
  31.             << "'" << phase << "',"
  32.             << "'" << "0" << "');";
  33.     }
  34.     else
  35.     {
  36.         ss << "INSERT INTO phase_members VALUES("
  37.             << "'" << player->GetGUID() << "',"
  38.             << "'" << player->GetName() << "',"
  39.             << "'" << phase << "',"
  40.             << "'" << phase << "');";
  41.     }
  42.  
  43.     SQLTransaction transat = CharacterDatabase.BeginTransaction();
  44.     transat->Append(ss.str().c_str());
  45.     CharacterDatabase.CommitTransaction(transat);
  46. }
  47.  
  48. class phasing_system : public CommandScript
  49. {
  50.    public:
  51.        phasing_system() : CommandScript("phasing_system") { }
  52.  
  53.       ChatCommand * GetCommands() const
  54.       {
  55.         static ChatCommand phaseCmdTable[] =
  56.         {
  57.             { "create",         SEC_PLAYER,         false, &HandlePhaseCreateCommand,                      "", NULL },
  58.             { "join",           SEC_PLAYER,         false, &HandlePhaseJoinCommand,                        "", NULL },
  59.             { "go",             SEC_PLAYER,         false, &HandlePhaseGoCommand,                          "", NULL },
  60.             { "gotarget",       SEC_PLAYER,         false, &HandlePhaseGoTargetCommand,                    "", NULL },
  61.             { "godelete",       SEC_PLAYER,         false, &HandlePhaseGoDeleteCommand,                    "", NULL },
  62.             { "help",           SEC_PLAYER,         false, &HandlePhaseHelpCommand,                        "", NULL },
  63.             { "delete",         SEC_PLAYER,         false, &HandlePhaseDeleteCommand,                      "", NULL },
  64.             { "kick",           SEC_PLAYER,         false, &HandlePhaseKickCommand,                        "", NULL },
  65.             { "get",            SEC_PLAYER,         false, &HandlePhaseGetCommand,                         "", NULL },
  66.             { "complete",       SEC_PLAYER,         false, &HandlePhaseCompleteCommand,                    "", NULL },
  67.             { "addmember",      SEC_PLAYER,         false, &HandlePhaseAddMemberCommand,                   "", NULL },
  68.             { "npcadd",         SEC_PLAYER,         false, &HandlePhaseAddNpcCommand,                      "", NULL },
  69.             { "npcdelete",      SEC_PLAYER,         false, &HandlePhaseDeleteNpcCommand,                   "", NULL },
  70.             { NULL,             0,                  false, NULL,                                           "", NULL }
  71.         };
  72.  
  73.         static ChatCommand phasecommandTable[] =
  74.         {
  75.             { "phase",          SEC_PLAYER,         false, NULL,                               "", phaseCmdTable },
  76.             { NULL,             0,                  false, NULL,                               "", NULL }
  77.         };
  78.         return phasecommandTable;
  79.       }
  80.  
  81.       static bool HandlePhaseCreateCommand(ChatHandler * chat, const char * args)
  82.       {
  83.           if(!*args)
  84.               return false;
  85.  
  86.           Player * player = chat->GetSession()->GetPlayer();
  87.  
  88.           uint32 phase = atoi((char*)args);
  89.  
  90.           if(phase == 0) // If someone even joins on 0, then it will crash the server
  91.               return false;
  92.           else if(phase == 1) // Default is 1, if someone owned that, then everyone would be screwed xD
  93.               return false;
  94.  
  95.           QueryResult get_phase = CharacterDatabase.PQuery("SELECT phase_owned FROM phase WHERE phase='%u'", phase);
  96.           QueryResult check = CharacterDatabase.PQuery("SELECT phase_owned FROM phase WHERE guid='%u'", player->GetGUID());
  97.  
  98.           if(check)
  99.           {
  100.               do
  101.               {
  102.                   Field * mfields = check->Fetch();
  103.                   if(mfields[0].GetInt32() != 0)
  104.                   {
  105.                       chat->SendSysMessage("|cffFF0000You already have a phase!|r");
  106.                       return false;
  107.                   }
  108.                   else
  109.                   {
  110.                       return true;
  111.                   }
  112.               }while(check->NextRow());
  113.           }
  114.  
  115.           if(get_phase)
  116.           {
  117.               do
  118.               {
  119.                   Field * fields = get_phase->Fetch();
  120.                   if(phase == fields[0].GetInt32())
  121.                   {
  122.                       chat->SendSysMessage("|cffFF0000This phase has already been taken!|r");
  123.                       return false;
  124.                   }
  125.                   else
  126.                   {
  127.                       return true;
  128.                   }
  129.               }while(get_phase->NextRow());
  130.           }
  131.  
  132.           CreatePhase(player, false, phase);
  133.           CreatePhase(player, true, phase);
  134.           player->SetPhaseMask(phase, true);
  135.           chat->SendSysMessage("|cff4169E1You have created your phase!|r \n |cffbbbbbb.phase join #|r - |cff00FF00is to join a phase.|r \n |cffbbbbbb.phase complete|r - |cff00FF00completes your phase for the public to join and see.|r");
  136.           return true;
  137.       };
  138.  
  139.       static bool HandlePhaseJoinCommand(ChatHandler * chat, const char * args)
  140.       {
  141.           if(!*args)
  142.               return false;
  143.  
  144.           Player * player = chat->GetSession()->GetPlayer();
  145.           uint32 phase = (uint32)atoi((char*)args);
  146.           if(phase == 0)
  147.               return false;
  148.           else if (phase == 1)
  149.           {
  150.               player->SetPhaseMask(1, true);
  151.               chat->PSendSysMessage("|cff4169E1You are now entering phase 1.|r (Default Phase)");
  152.           }
  153.  
  154.           QueryResult hasPhase = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase WHERE guid='%u'", player->GetGUID());
  155.           if(hasPhase)
  156.           {
  157.              do
  158.              {
  159.                  Field * fields = hasPhase->Fetch();
  160.                  if(fields[0].GetInt32() == 0)
  161.                  {
  162.                       chat->PSendSysMessage("|cffFF0000You need to create a phase before joining one!|r", phase);
  163.                       return false;
  164.                  }
  165.              }while(hasPhase->NextRow());
  166.           }
  167.  
  168.           QueryResult myPhase = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", player->GetGUID());
  169.  
  170.           if(myPhase)
  171.           {
  172.               do
  173.               {
  174.                   Field * mfield = myPhase->Fetch();
  175.                   if(mfield[0].GetUInt32() == phase)
  176.                   {
  177.                       chat->PSendSysMessage("|cffFF0000You are already in phase %u!|r", phase);
  178.                       return false;
  179.                   }
  180.               }while(myPhase->NextRow());
  181.           }
  182.  
  183.           QueryResult isCompleted = CharacterDatabase.PQuery("SELECT has_completed,guid,phase FROM phase WHERE phase='%u'", phase);
  184.           if(!isCompleted)
  185.               return false;
  186.  
  187.           if(isCompleted)
  188.           {
  189.               do
  190.               {
  191.                   Field * fields = isCompleted->Fetch();
  192.                   if(fields[0].GetUInt16() == 1) // if the phase is completed
  193.                   {
  194.                     player->SetPhaseMask(phase, true);
  195.                     CharacterDatabase.PExecute("UPDATE phase SET get_phase='%u' WHERE guid='%u'", phase, player->GetGUID());
  196.                     chat->PSendSysMessage("|cff4169E1You are now entering phase %u.|r", phase);
  197.                   }
  198.                   else if(player->GetGUID() == fields[1].GetUInt64() && fields[2].GetUInt32() == phase)
  199.                   {
  200.                     player->SetPhaseMask(phase, true);
  201.                     CharacterDatabase.PExecute("UPDATE phase SET get_phase='%u' WHERE guid='%u'", phase, player->GetGUID());
  202.                     chat->PSendSysMessage("|cffffffffYou are now entering your own phase %u.|r", phase);
  203.                   }
  204.                   else // if the phase isn't completed
  205.                   {
  206.                     chat->PSendSysMessage("|cffFF0000This phase isn't completed yet!|r \n Phase: %u", phase);
  207.                     return false;
  208.                   }
  209.               }while(isCompleted->NextRow());
  210.           }
  211.           return true;
  212.       };
  213.  
  214.       static bool HandlePhaseHelpCommand(ChatHandler * chat, const char * args)
  215.       {
  216.           Player * player = chat->GetSession()->GetPlayer();
  217.           char msg[1024];
  218.           snprintf(msg, 1024, "\n |cff00FF00 ~ Phase commands and information is below ~|r \n |cff008000.phase npcadd:|r |cff4169E1 Adds a npc.|r \n |cff008000.phase npcdelete:|r |cff4169E1 Deletes a npc.|r \n |cff008000.phase godelete:|r |cff4169E1 Deletes a gobject.|r \n |cff008000.phase gotarget:|r |cff4169E1 Targets a gobject.|r \n |cff008000.phase go:|r |cff4169E1 Spawns a gobject if you're allowed to.|r \n |cff008000.phase changeowner:|r |cff4169E1 Changes the phase's owner.|r \n |cff008000.phase addmember:|r |cff4169E1 Enables that player to build in your phase.|r \n |cff008000.phase create:|r |cff4169E1 Allows you to create a phase.|r \n |cff008000.phase join:|r |cff4169E1 Join a specified/completed phase.|r \n |cff008000.phase delete:|r |cff4169E1 Delete your created phase.|r \n |cff008000.phase kick:|r |cff4169E1 Kick a player from your phase.|r \n |cff008000.phase leave:|r |cff4169E1 Leave any phase to the default 0 phase.|r \n |cff008000.phase help:|r |cff4169E1 Shows you the commands and what they do.|r \n |cff008000.phase get:|r |cff4169E1 Gets the current phase you're in.|r \n |cff008000.phase kick:|r |cff4169E1 Kick a player from your phase.|r \n |cff008000.phase complete:|r |cff4169E1 After you finish your phase, completing it will set your phase to public.|r \n");
  219.           chat->SendSysMessage(msg);
  220.           return true;
  221.       };
  222.  
  223.       static bool HandlePhaseDeleteCommand(ChatHandler * chat, const char * args)
  224.       {
  225.           Player * player = chat->GetSession()->GetPlayer();
  226.  
  227.           player->SetPhaseMask(0, true);
  228.           QueryResult res = CharacterDatabase.PQuery("SELECT * FROM phase WHERE guid='%u' LIMIT 1", player->GetGUID());
  229.           if(!res)
  230.               return false;
  231.  
  232.           CharacterDatabase.PExecute("DELETE FROM phase WHERE (guid='%u')", player->GetGUID());
  233.           CharacterDatabase.PExecute("DELETE FROM phase_members WHERE (guid='%u')", player->GetGUID());
  234.           chat->SendSysMessage("|cffFFFF00Your phase has now been deleted.|r");
  235.           return true;
  236.       };
  237.  
  238.       static bool HandlePhaseKickCommand(ChatHandler * chat, const char * args)
  239.       {
  240.           if(!*args)
  241.               return false;
  242.  
  243.           Player * player = chat->GetSession()->GetPlayer();
  244.  
  245.           QueryResult phase = CharacterDatabase.PQuery("SELECT phase FROM phase WHERE guid='%u'", player->GetGUID());
  246.           if(!phase)
  247.               return false;
  248.  
  249.           if(phase)
  250.           {
  251.               do
  252.               {
  253.                   Field * fields = phase->Fetch();
  254.                   QueryResult getplayer = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE player_name='%s'", args);
  255.                   if(!getplayer)
  256.                       return false;
  257.  
  258.                   char msg[255];
  259.  
  260.                   Field * mfields = getplayer->Fetch();
  261.                   if(mfields[0].GetInt32() == fields[0].GetInt32())
  262.                   {
  263.                       Player * target = ObjectAccessor::FindPlayerByName(args);
  264.                       target->SetPhaseMask(0, true);
  265.                       snprintf(msg, 255, "|cffFF0000You were kicked from phase %u by %s!|r", fields[0].GetInt32(), player->GetName());
  266.                       player->Whisper(msg, LANG_UNIVERSAL, player->GetGUID());
  267.                       CharacterDatabase.PExecute("UPDATE phases SET get_phase='0' AND get_phase='0' WHERE (guid='%u')", player->GetGUID());
  268.                   }
  269.                   else
  270.                   {
  271.                      snprintf(msg, 255, "|cffFF0000%s is not currently in your phase!|r", args);
  272.                      chat->SendSysMessage(msg);
  273.                      return false;
  274.                   }
  275.               }while(phase->NextRow());
  276.           }
  277.           return true;
  278.       };
  279.  
  280.       static bool HandlePhaseGetCommand(ChatHandler * chat, const char * args)
  281.       {
  282.           Player * player = chat->GetSession()->GetPlayer();
  283.           QueryResult getPhaseAndOwnedPhase = CharacterDatabase.PQuery("SELECT COUNT(*),get_phase,phase FROM phase WHERE guid='%u'", player->GetGUID());
  284.           if(!getPhaseAndOwnedPhase)
  285.               return false;
  286.  
  287.           if(getPhaseAndOwnedPhase)
  288.           {
  289.               do
  290.               {
  291.                   Field * fields = getPhaseAndOwnedPhase->Fetch();
  292.                   if(fields[0].GetInt32() == 0)
  293.                   {
  294.                       chat->SendSysMessage("|cffFF0000Could not get phase or owned phase.|r");
  295.                       return false;
  296.                   }
  297.                   chat->PSendSysMessage("|cffADD8E6Current Phase: %u \n Owned Phase: %u|r", fields[1].GetUInt32(), fields[2].GetUInt32());
  298.               }while(getPhaseAndOwnedPhase->NextRow());
  299.           }
  300.           return true;
  301.       };
  302.  
  303.       static bool HandlePhaseCompleteCommand(ChatHandler * chat, const char * args)
  304.       {
  305.           if(!*args)
  306.               return false;
  307.  
  308.           Player * player = chat->GetSession()->GetPlayer();
  309.           uint8 yesorno = (uint8)atoi((char*)args);
  310.           if(!yesorno)
  311.               return false;
  312.  
  313.           if(yesorno > 1)
  314.               return false;
  315.  
  316.           QueryResult isOwnerOfAPhase = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase WHERE guid='%u'", player->GetGUID());
  317.           if(isOwnerOfAPhase)
  318.           {
  319.               do
  320.               {
  321.                   Field * fields = isOwnerOfAPhase->Fetch();
  322.                   if(fields[0].GetInt32() == 0)
  323.                   {
  324.                       chat->SendSysMessage("|cffFF0000Could not complete phase.|r");
  325.                       return false;
  326.                   }
  327.               }while(isOwnerOfAPhase->NextRow());
  328.           }
  329.           if(yesorno == 1)
  330.           {
  331.               chat->SendSysMessage("|cffFFA500You have now completed your phase! It is open for public.|r");
  332.               CharacterDatabase.PExecute("UPDATE phase SET has_completed='1' WHERE guid='%u'", player->GetGUID());
  333.           }
  334.           else if(yesorno == 0)
  335.           {
  336.               chat->SendSysMessage("|cffADD8E6Please take your time.");
  337.           }
  338.           return true;
  339.       };
  340.  
  341.       static bool HandlePhaseAddMemberCommand(ChatHandler * chat, const char * args)
  342.       {
  343.           if(!*args)
  344.               return false;
  345.  
  346.           uint32 ph = atoi((char*)args);
  347.           Player * player = chat->GetSession()->GetPlayer();
  348.           Player * target = chat->getSelectedPlayer();
  349.  
  350.           if (!target)
  351.           {
  352.              chat->SendSysMessage(LANG_NO_CHAR_SELECTED);
  353.              chat->SetSentErrorMessage(true);
  354.              return false;
  355.           }
  356.  
  357.           if(target == player)
  358.             return false;
  359.  
  360.             QueryResult isOwnerOfAPhase = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase WHERE player_name='%s'", target->GetName());
  361.             if(isOwnerOfAPhase)
  362.             {
  363.                 do
  364.                 {
  365.                     Field * fields = isOwnerOfAPhase->Fetch();
  366.                     if(fields[0].GetInt32() == 0)
  367.                     {
  368.                         chat->PSendSysMessage("|cffFF0000 %s does not own a phase; therefore, he/she cannot be added to your phase.|r", target->GetName());
  369.                         return false;
  370.                     }
  371.                 }while(isOwnerOfAPhase->NextRow());
  372.             }
  373.             QueryResult isMember = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE player_name='%s' AND phase='%u'", target->GetName(), ph);
  374.             if(isMember)
  375.             {
  376.                 do
  377.                 {
  378.                     Field * field = isMember->Fetch();
  379.                     if(field[0].GetInt32() == 1)
  380.                     {
  381.                         chat->PSendSysMessage("|cffFF0000 %s is already a member of your phase!|r", target->GetName());
  382.                         return false;
  383.                     }
  384.                 }while(isMember->NextRow());
  385.             }
  386.             QueryResult isOwner = CharacterDatabase.PQuery("SELECT phase_owned FROM phase WHERE guid='%u'", player->GetGUID());
  387.             if(isOwner)
  388.             {
  389.                 do
  390.                 {
  391.                     Field * mfield = isOwner->Fetch();
  392.                     if(ph != mfield[0].GetUInt32())
  393.                     {
  394.                         chat->PSendSysMessage("|cffFF0000You cannot add yourself or anyone else to someone elses phase.|r");
  395.                         return false;
  396.                     }
  397.                 }while(isOwner->NextRow());
  398.             }
  399.             chat->PSendSysMessage("|cffFFA500You successfully added %s to your phase %u.|r", target->GetName(), ph);
  400.             CreatePhase(target, true, ph);
  401.          return true;
  402.       };
  403.  
  404. #ifndef _PHASE_GO_COMMANDS_
  405.       static bool HandlePhaseGoCommand(ChatHandler * handler, const char * args)
  406.       {
  407.         if (!*args)
  408.             return false;
  409.  
  410.         // number or [name] Shift-click form |color|Hgameobject_entry:go_id|h[name]|h|r
  411.         char* id = handler->extractKeyFromLink((char*)args, "Hgameobject_entry");
  412.         if (!id)
  413.             return false;
  414.  
  415.         uint32 objectId = atol(id);
  416.         if (!objectId)
  417.             return false;
  418.  
  419.         char* phasing = strtok(NULL, " ");
  420.  
  421.         const GameObjectTemplate* objectInfo = sObjectMgr->GetGameObjectTemplate(objectId);
  422.  
  423.         if (!objectInfo)
  424.         {
  425.             handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, objectId);
  426.             handler->SetSentErrorMessage(true);
  427.             return false;
  428.         }
  429.  
  430.         if (objectInfo->displayId && !sGameObjectDisplayInfoStore.LookupEntry(objectInfo->displayId))
  431.         {
  432.             // report to DB errors log as in loading case
  433.             sLog->outErrorDb("Gameobject (Entry %u GoType: %u) have invalid displayId (%u), not spawned.", objectId, objectInfo->type, objectInfo->displayId);
  434.             handler->PSendSysMessage(LANG_GAMEOBJECT_HAVE_INVALID_DATA, objectId);
  435.             handler->SetSentErrorMessage(true);
  436.             return false;
  437.         }
  438.  
  439.         Player* player = handler->GetSession()->GetPlayer();
  440.         float x = float(player->GetPositionX());
  441.         float y = float(player->GetPositionY());
  442.         float z = float(player->GetPositionZ());
  443.         float o = float(player->GetOrientation());
  444.         Map* map = player->GetMap();
  445.  
  446.         GameObject* object = new GameObject;
  447.         uint32 guidLow = sObjectMgr->GenerateLowGuid(HIGHGUID_GAMEOBJECT);
  448.  
  449.         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))
  450.         {
  451.             delete object;
  452.             return false;
  453.         }
  454.  
  455.         if (phasing)
  456.         {
  457.             uint32 value = atoi((char*)phasing);
  458.  
  459.             if(value == 0)
  460.                 return false;
  461.  
  462.             QueryResult res;
  463.             res = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", player->GetGUID());
  464.             if(res)
  465.             {
  466.                 do
  467.                 {
  468.                     Field * fields = res->Fetch();
  469.                     uint32 val = fields[0].GetUInt32();
  470.                     if(val != value)
  471.                     {
  472.                         handler->SendSysMessage("You are not in this phase!");
  473.                         return false;
  474.                     }
  475.  
  476.                     QueryResult result;
  477.                     result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE guid='%u' AND phase='1' LIMIT 1",
  478.                         player->GetGUID(), (uint32)val);
  479.  
  480.                     if(result)
  481.                     {
  482.                         do
  483.                         {
  484.                             Field * fields = result->Fetch();
  485.                             if(fields[0].GetInt32() == 0)
  486.                             {
  487.                                 handler->SendSysMessage("You must be added to this phase before you can build.");
  488.                                 return false;
  489.                             }
  490.                         }while(result->NextRow());
  491.                     }
  492.                 }while(res->NextRow());
  493.             }
  494.         }
  495.  
  496.         // fill the gameobject data and save to the db
  497.         object->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), player->GetPhaseMaskForSpawn());
  498.  
  499.         // this will generate a new guid if the object is in an instance
  500.         if (!object->LoadGameObjectFromDB(guidLow, map))
  501.         {
  502.             delete object;
  503.             return false;
  504.         }
  505.  
  506.         // TODO: is it really necessary to add both the real and DB table guid here ?
  507.         sObjectMgr->AddGameobjectToGrid(guidLow, sObjectMgr->GetGOData(guidLow));
  508.  
  509.         handler->PSendSysMessage(LANG_GAMEOBJECT_ADD, objectId, objectInfo->name.c_str(), guidLow, x, y, z);
  510.         return true;
  511.     }
  512.  
  513.       static bool HandlePhaseGoTargetCommand(ChatHandler * handler, const char * args)
  514.       {
  515.           Player* pl = handler->GetSession()->GetPlayer();
  516.  
  517.           QueryResult result;
  518.           GameEventMgr::ActiveEvents const& activeEventsList = sGameEventMgr->GetActiveEventList();
  519.           if (*args)
  520.           {
  521.             char* cId = handler->extractKeyFromLink((char*)args, "Hgameobject_entry");
  522.             if (!cId)
  523.                 return false;
  524.  
  525.             uint32 id = atol(cId);
  526.             char* phasing = strtok(NULL, " ");
  527.             if(!phasing)
  528.                 return false;
  529.  
  530.             if (id)
  531.                 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",
  532.                 pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(), id);
  533.             else
  534.             {
  535.                 std::string name = cId;
  536.                 WorldDatabase.EscapeString(name);
  537.                 result = WorldDatabase.PQuery(
  538.                     "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_ "
  539.                     "FROM gameobject, gameobject_template WHERE gameobject_template.entry = gameobject.id AND map = %i AND name "_LIKE_" "_CONCAT3_("'%%'", "'%s'", "'%%'")" ORDER BY order_ ASC LIMIT 1",
  540.                     pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(), name.c_str());
  541.             }
  542.  
  543.             if (phasing)
  544.             {
  545.                 uint32 value = atoi((char*)phasing);
  546.  
  547.                 if(value == 0)
  548.                     return false;
  549.  
  550.                 QueryResult res;
  551.                 res = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", pl->GetGUID());
  552.                 if(res)
  553.                 {
  554.                     do
  555.                     {
  556.                         Field * fields = res->Fetch();
  557.                         uint32 val = fields[0].GetUInt32();
  558.                         if(val != value)
  559.                         {
  560.                             handler->SendSysMessage("You are not in this phase!");
  561.                             return false;
  562.                         }
  563.  
  564.                         QueryResult result;
  565.                         result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE guid='%u' AND phase='1' LIMIT 1",
  566.                             pl->GetGUID(), (uint32)val);
  567.  
  568.                         if(result)
  569.                         {
  570.                             do
  571.                             {
  572.                                 Field * fields = result->Fetch();
  573.                                 if(fields[0].GetInt32() == 0)
  574.                                 {
  575.                                     handler->SendSysMessage("You must be added to this phase before you can target a go.");
  576.                                     return false;
  577.                                 }
  578.                             }while(result->NextRow());
  579.                         }
  580.                     }while(res->NextRow());
  581.                 }
  582.             }
  583.         }
  584.         else
  585.         {
  586.             std::ostringstream eventFilter;
  587.             eventFilter << " AND (eventEntry IS NULL ";
  588.             bool initString = true;
  589.  
  590.             for (GameEventMgr::ActiveEvents::const_iterator itr = activeEventsList.begin(); itr != activeEventsList.end(); ++itr)
  591.             {
  592.                 if (initString)
  593.                 {
  594.                     eventFilter  <<  "OR eventEntry IN (" <<*itr;
  595.                     initString =false;
  596.                 }
  597.                 else
  598.                     eventFilter << ", " << *itr;
  599.             }
  600.  
  601.             if (!initString)
  602.                 eventFilter << "))";
  603.             else
  604.                 eventFilter << ")";
  605.  
  606.             result = WorldDatabase.PQuery("SELECT gameobject.guid, id, position_x, position_y, position_z, orientation, map, phaseMask, "
  607.                 "(POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ FROM gameobject "
  608.                 "LEFT OUTER JOIN game_event_gameobject on gameobject.guid = game_event_gameobject.guid WHERE map = '%i' %s ORDER BY order_ ASC LIMIT 10",
  609.                 handler->GetSession()->GetPlayer()->GetPositionX(), handler->GetSession()->GetPlayer()->GetPositionY(), handler->GetSession()->GetPlayer()->GetPositionZ(),
  610.                 handler->GetSession()->GetPlayer()->GetMapId(), eventFilter.str().c_str());
  611.         }
  612.  
  613.           if (!result)
  614.           {
  615.              handler->SendSysMessage(LANG_COMMAND_TARGETOBJNOTFOUND);
  616.              return true;
  617.           }
  618.  
  619.           bool found = false;
  620.           float x, y, z, o;
  621.           uint32 lowguid, id;
  622.           uint16 mapid, phase;
  623.           uint32 pool_id;
  624.  
  625.           do
  626.           {
  627.              Field *fields = result->Fetch();
  628.              lowguid = fields[0].GetUInt32();
  629.              id =      fields[1].GetUInt32();
  630.              x =       fields[2].GetFloat();
  631.              y =       fields[3].GetFloat();
  632.              z =       fields[4].GetFloat();
  633.              o =       fields[5].GetFloat();
  634.              mapid =   fields[6].GetUInt16();
  635.              phase =   fields[7].GetUInt16();
  636.              pool_id = sPoolMgr->IsPartOfAPool<GameObject>(lowguid);
  637.              if (!pool_id || sPoolMgr->IsSpawnedObject<GameObject>(lowguid))
  638.                 found = true;
  639.           } while (result->NextRow() && (!found));
  640.  
  641.           if (!found)
  642.           {
  643.              handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, id);
  644.              return false;
  645.           }
  646.  
  647.           GameObjectTemplate const* goI = sObjectMgr->GetGameObjectTemplate(id);
  648.  
  649.           if (!goI)
  650.           {
  651.               handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, id);
  652.               return false;
  653.           }
  654.  
  655.           GameObject* target = handler->GetSession()->GetPlayer()->GetMap()->GetGameObject(MAKE_NEW_GUID(lowguid, id, HIGHGUID_GAMEOBJECT));
  656.  
  657.           handler->PSendSysMessage(LANG_GAMEOBJECT_DETAIL, lowguid, goI->name.c_str(), lowguid, id, x, y, z, mapid, o, phase);
  658.  
  659.           if (target)
  660.           {
  661.               int32 curRespawnDelay = int32(target->GetRespawnTimeEx()-time(NULL));
  662.               if (curRespawnDelay < 0)
  663.                  curRespawnDelay = 0;
  664.  
  665.              std::string curRespawnDelayStr = secsToTimeString(curRespawnDelay, true);
  666.              std::string defRespawnDelayStr = secsToTimeString(target->GetRespawnDelay(), true);
  667.  
  668.              handler->PSendSysMessage(LANG_COMMAND_RAWPAWNTIMES, defRespawnDelayStr.c_str(), curRespawnDelayStr.c_str());
  669.           }
  670.         return true;
  671.       };
  672.  
  673.       static bool HandlePhaseGoDeleteCommand(ChatHandler * handler, const char * args)
  674.       {
  675.           if(!*args)
  676.               return false;
  677.  
  678.           Player * player = handler->GetSession()->GetPlayer();
  679.  
  680.           char* cId = handler->extractKeyFromLink((char*)args, "Hgameobject");
  681.           if (!cId)
  682.             return false;
  683.  
  684.           uint32 lowguid = atoi(cId);
  685.           if (!lowguid)
  686.              return false;
  687.  
  688.           char* phasing = strtok(NULL, " ");
  689.           if(!phasing)
  690.             return false;
  691.  
  692.           GameObject* obj = NULL;
  693.  
  694.           if (GameObjectData const* go_data = sObjectMgr->GetGOData(lowguid))
  695.               obj = handler->GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid, go_data->id);
  696.  
  697.           if (!obj)
  698.           {
  699.               handler->PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
  700.               handler->SetSentErrorMessage(true);
  701.               return false;
  702.           }
  703.  
  704.             if (phasing)
  705.             {
  706.                 uint32 value = atoi((char*)phasing);
  707.  
  708.                 if(value == 0)
  709.                     return false;
  710.  
  711.                 QueryResult res;
  712.                 res = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", player->GetGUID());
  713.                 if(res)
  714.                 {
  715.                     do
  716.                     {
  717.                         Field * fields = res->Fetch();
  718.                         uint32 val = fields[0].GetUInt32();
  719.                         if(val != value)
  720.                         {
  721.                             handler->SendSysMessage("You are not in this phase!");
  722.                             return false;
  723.                         }
  724.  
  725.                         QueryResult result;
  726.                         result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE guid='%u' AND phase='1' LIMIT 1",
  727.                             player->GetGUID(), (uint32)val);
  728.  
  729.                         if(result)
  730.                         {
  731.                             do
  732.                             {
  733.                                 Field * fields = result->Fetch();
  734.                                 if(fields[0].GetInt32() == 0)
  735.                                 {
  736.                                     handler->SendSysMessage("You must be added to this phase before you can delete an object.");
  737.                                     return false;
  738.                                 }
  739.                             }while(result->NextRow());
  740.                         }
  741.                     }while(res->NextRow());
  742.                 }
  743.             }
  744.  
  745.           uint64 owner_guid = obj->GetOwnerGUID();
  746.           if (owner_guid)
  747.           {
  748.               Unit* owner = ObjectAccessor::GetUnit(*handler->GetSession()->GetPlayer(), owner_guid);
  749.               if (!owner || !IS_PLAYER_GUID(owner_guid))
  750.               {
  751.                   handler->PSendSysMessage(LANG_COMMAND_DELOBJREFERCREATURE, GUID_LOPART(owner_guid), obj->GetGUIDLow());
  752.                   handler->SetSentErrorMessage(true);
  753.                   return false;
  754.               }
  755.               owner->RemoveGameObject(obj, false);
  756.           }
  757.  
  758.           obj->SetRespawnTime(0);                                 // not save respawn time
  759.           obj->Delete();
  760.           obj->DeleteFromDB();
  761.           handler->PSendSysMessage(LANG_COMMAND_DELOBJMESSAGE, obj->GetGUIDLow());
  762.           return true;
  763.       };
  764. #endif
  765.  
  766. #ifndef _PHASE_NPC_COMMANDS_
  767.     static bool HandlePhaseAddNpcCommand(ChatHandler* handler, const char* args)
  768.     {
  769.         if (!*args)
  770.             return false;
  771.         char* charID = handler->extractKeyFromLink((char*)args, "Hcreature_entry");
  772.         if (!charID)
  773.             return false;
  774.  
  775.         char * phase = strtok(NULL, " ");
  776.  
  777.         char* team = strtok(NULL, " ");
  778.         int32 teamval = 0;
  779.         if (team) { teamval = atoi(team); }
  780.         if (teamval < 0) { teamval = 0; }
  781.  
  782.         uint32 id  = atoi(charID);
  783.  
  784.         Player* chr = handler->GetSession()->GetPlayer();
  785.         float x = chr->GetPositionX();
  786.         float y = chr->GetPositionY();
  787.         float z = chr->GetPositionZ();
  788.         float o = chr->GetOrientation();
  789.         Map* map = chr->GetMap();
  790.  
  791.         if (chr->GetTransport())
  792.         {
  793.             uint32 tguid = chr->GetTransport()->AddNPCPassenger(0, id, chr->GetTransOffsetX(), chr->GetTransOffsetY(), chr->GetTransOffsetZ(), chr->GetTransOffsetO());
  794.             if (tguid > 0)
  795.                 WorldDatabase.PQuery("INSERT INTO creature_transport (guid, npc_entry, transport_entry,  TransOffsetX, TransOffsetY, TransOffsetZ, TransOffsetO) values (%u, %u, %f, %f, %f, %f, %u)", tguid, id, chr->GetTransport()->GetEntry(), chr->GetTransOffsetX(), chr->GetTransOffsetY(), chr->GetTransOffsetZ(), chr->GetTransOffsetO());
  796.  
  797.             return true;
  798.         }
  799.  
  800.         Creature* creature = new Creature;
  801.         if (!creature->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), id, 0, (uint32)teamval, x, y, z, o))
  802.         {
  803.             delete creature;
  804.             return false;
  805.         }
  806.  
  807.         if(phase)
  808.         {
  809.             uint32 value = atoi((char*)phase);
  810.  
  811.             if(value == 0)
  812.                 return false;
  813.  
  814.             QueryResult res;
  815.             res = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", chr->GetGUID());
  816.             if(res)
  817.             {
  818.                 do
  819.                 {
  820.                     Field * fields = res->Fetch();
  821.                     uint32 val = fields[0].GetUInt32();
  822.                     if(val != value)
  823.                     {
  824.                         handler->SendSysMessage("You are not in this phase!");
  825.                         return false;
  826.                     }
  827.  
  828.                     QueryResult result;
  829.                     result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE guid='%u' AND phase='1' LIMIT 1",
  830.                         chr->GetGUID(), (uint32)val);
  831.  
  832.                     if(result)
  833.                     {
  834.                         do
  835.                         {
  836.                             Field * fields = result->Fetch();
  837.                             if(fields[0].GetInt32() == 0)
  838.                             {
  839.                                 handler->SendSysMessage("You must be added to this phase before you can build.");
  840.                                 return false;
  841.                             }
  842.                         }while(result->NextRow());
  843.                     }
  844.                 }while(res->NextRow());
  845.             }
  846.         }
  847.  
  848.         creature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
  849.  
  850.         uint32 db_guid = creature->GetDBTableGUIDLow();
  851.  
  852.         // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
  853.         if (!creature->LoadCreatureFromDB(db_guid, map))
  854.         {
  855.             delete creature;
  856.             return false;
  857.         }
  858.  
  859.         sObjectMgr->AddCreatureToGrid(db_guid, sObjectMgr->GetCreatureData(db_guid));
  860.         return true;
  861.     }
  862.  
  863.     static bool HandlePhaseDeleteNpcCommand(ChatHandler * handler, const char * args)
  864.     {
  865.         Creature* unit = NULL;
  866.         Player * pl = handler->GetSession()->GetPlayer();
  867.         if (*args)
  868.         {
  869.             char * phase = strtok(NULL, " ");
  870.             if(!phase)
  871.                 return false;
  872.  
  873.             if(phase)
  874.             {
  875.                 uint32 value = atoi((char*)phase);
  876.                 if(value == 0)
  877.                     return false;
  878.  
  879.                 QueryResult res;
  880.                 res = CharacterDatabase.PQuery("SELECT get_phase FROM phase WHERE guid='%u'", pl->GetGUID());
  881.                 if(res)
  882.                 {
  883.                     do
  884.                     {
  885.                         Field * fields = res->Fetch();
  886.                         uint32 val = fields[0].GetUInt32();
  887.                         if(val != value)
  888.                         {
  889.                             handler->SendSysMessage("You are not in this phase!");
  890.                             return false;
  891.                         }
  892.  
  893.                         QueryResult result;
  894.                         result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM phase_members WHERE guid='%u' AND phase='1' LIMIT 1",
  895.                             pl->GetGUID(), (uint32)val);
  896.  
  897.                         if(result)
  898.                         {
  899.                             do
  900.                             {
  901.                                 Field * fields = result->Fetch();
  902.                                 if(fields[0].GetInt32() == 0)
  903.                                 {
  904.                                     handler->SendSysMessage("You must be added to this phase before you can target a go.");
  905.                                     return false;
  906.                                 }
  907.                             }while(result->NextRow());
  908.                         }
  909.                     }while(res->NextRow());
  910.                 }
  911.             }
  912.         }
  913.         else
  914.             unit = handler->getSelectedCreature();
  915.  
  916.         if (!unit || unit->isPet() || unit->isTotem())
  917.         {
  918.             handler->SendSysMessage(LANG_SELECT_CREATURE);
  919.             handler->SetSentErrorMessage(true);
  920.             return false;
  921.         }
  922.  
  923.         // Delete the creature
  924.         unit->CombatStop();
  925.         unit->DeleteFromDB();
  926.         unit->AddObjectToRemoveList();
  927.  
  928.         handler->SendSysMessage(LANG_COMMAND_DELCREATMESSAGE);
  929.  
  930.         return true;
  931.     }
  932. #endif
  933. };
  934.  
  935. void AddSC_system_phase()
  936. {
  937.     new phasing_system;
  938. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement