Advertisement
Rochet2

-- moving

Jun 2nd, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.61 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2. #include "ScriptMgr.h"
  3. #include "GameEventMgr.h"
  4. #include "ObjectMgr.h"
  5. #include "PoolMgr.h"
  6. #include "MapManager.h"
  7. #include "Chat.h"
  8. // #include "Chat.h"
  9.  
  10. struct GOMove_Data
  11. {
  12.     GameObject* pGob;bool Face;uint32 Entry;float x;float y;float z;float o;float scale;uint32 Phase;
  13. };
  14. static std::map<uint64, GOMove_Data> GOMove;
  15. static std::map<uint64, GameObject*> GOSaved;
  16. static std::map<uint64, bool> GOSpawned;
  17.  
  18. class GOMove_commandscript : public CommandScript
  19. {
  20. public:
  21.     GOMove_commandscript() : CommandScript("GOMove_commandscript") { }
  22.  
  23.     ChatCommand* GetCommands() const
  24.     {
  25.         static ChatCommand GOMoveCommandTable[] =
  26.         {
  27.             { "gomove",         SEC_GAMEMASTER,     true,   &GOMove_Command,                "", NULL },
  28.             { NULL,             0,                  falseNULL,                           "", NULL }
  29.         };
  30.         return GOMoveCommandTable;
  31.     }
  32.  
  33.     static bool GO(uint64 GUID,uint32 e,float x,float y,float z,float o,uint32 p,Player* pPlayer,GameObject* OLD,ChatHandler* handler)
  34.     {
  35.         if(GUID == 0)
  36.             return false;
  37.         if(GOMove[GUID].Face) // Face north
  38.             pPlayer->SetOrientation(0);
  39.  
  40.         if(!OLD) // create a new object totally
  41.         {
  42.             handler->PSendSysMessage("CREATED NEW");
  43.             if(e == 0 || p == 0)
  44.                 return false;
  45.             // Getting gameobject info and checking if the object exists
  46.             const GameObjectTemplate* objectInfo = sObjectMgr->GetGameObjectTemplate(e);
  47.             if (!objectInfo)
  48.             {
  49.                 handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, e);
  50.                 handler->SetSentErrorMessage(true);
  51.                 return false;
  52.             }
  53.             if (objectInfo->displayId && !sGameObjectDisplayInfoStore.LookupEntry(objectInfo->displayId))
  54.             {
  55.                 sLog->outErrorDb("Gameobject (Entry %u GoType: %u) have invalid displayId (%u), not spawned.", e, objectInfo->type, objectInfo->displayId);
  56.                 handler->PSendSysMessage(LANG_GAMEOBJECT_HAVE_INVALID_DATA, e);
  57.                 handler->SetSentErrorMessage(true);
  58.                 return false;
  59.             }
  60.             Map* map = pPlayer->GetMap();
  61.             GameObject* object = new GameObject;
  62.             uint32 guidLow = sObjectMgr->GenerateLowGuid(HIGHGUID_GAMEOBJECT);
  63.  
  64.             if (!object->Create(guidLow, objectInfo->entry, map, p, x, y, z, o, 0.0f, 0.0f, 0.0f, 0.0f, 0, GO_STATE_READY))
  65.             {
  66.                 delete object;
  67.                 return false;
  68.             }
  69.             // fill the gameobject data and save to the db
  70.             object->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), pPlayer->GetPhaseMaskForSpawn());
  71.  
  72.             // this will generate a new guid if the object is in an instance
  73.             if (!object->LoadGameObjectFromDB(guidLow, map))
  74.             {
  75.                 delete object;
  76.                 return false;
  77.             }
  78.             sObjectMgr->AddGameobjectToGrid(guidLow, sObjectMgr->GetGOData(guidLow));
  79.             handler->PSendSysMessage(LANG_GAMEOBJECT_ADD, e, objectInfo->name.c_str(), guidLow, x, y, z);
  80.             GOSaved[object->GetGUID()] = object;
  81.             GOMove[GUID].pGob = object;
  82.             return true;
  83.         }
  84.         else // move the old object
  85.         {
  86.             uint64 OGuid = OLD->GetGUID();
  87.             if (!MapManager::IsValidMapCoord(OLD->GetMapId(), x, y, z))
  88.             {
  89.                 handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, OLD->GetMapId());
  90.                 handler->SetSentErrorMessage(true);
  91.                 return false;
  92.             }
  93.             // OLD->LoadFromDB();
  94.             OLD->RemoveFromWorld();
  95.             float rot2 = sin(o/2);
  96.             float rot3 = cos(o/2);
  97.             GOSaved[OGuid]->Relocate(x, y, z, o);
  98.             if(p != 0 && p != NULL)
  99.                 GOSaved[OGuid]->SetPhaseMask(p, true);
  100.             GOSaved[OGuid]->SaveToDB();
  101.             GameObject* NEW = pPlayer->SummonGameObject(OLD->GetEntry(), x, y, z, o, 0, 0, rot2, rot3, 300); // phase?
  102.             GOSaved[NEW->GetGUID()] = GOSaved[OGuid];
  103.             GOSaved.erase(OGuid);
  104.             GOMove[GUID].pGob = NEW;
  105.             //OLD->SetRespawnTime(0); // not save respawn time
  106.             //OLD->Delete();
  107.             /*
  108.             if (!MapManager::IsValidMapCoord(OLD->GetMapId(), x, y, z))
  109.             {
  110.                 handler->PSendSysMessage(LANG_INVALID_TARGET_COORD, x, y, OLD->GetMapId());
  111.                 handler->SetSentErrorMessage(true);
  112.                 return false;
  113.             }
  114.             OLD->Relocate(x, y, z, o);
  115.             if(p != 0 && p != NULL)
  116.                 OLD->SetPhaseMask(p, true);
  117.             OLD->SaveToDB();
  118.             */
  119.         }
  120.         return true;
  121.     }
  122.  
  123. static bool GOMove_Command(ChatHandler* handler, const char* args)
  124. {
  125.     // GO(GUID, entry, pPlayer->GetPositionX(), pPlayer->GetPositionY(), pPlayer->GetPositionZ(), pPlayer->GetOrientation(), pPlayer->GetPhaseMask(), pPlayer, pOld, handler);
  126.     enum eEnums
  127.     {
  128.         TEST = 0 ,
  129.         SELECTNEAR = 1 ,
  130.         DELET = 2 ,
  131.         X = 3 ,
  132.         Y = 4 ,
  133.         Z = 5 ,
  134.         O = 6 ,
  135.  
  136.         SPAWN = 7 ,
  137.         NORTH = 8 ,
  138.         EAST = 9 ,
  139.         SOUTH = 10 ,
  140.         WEST = 11 ,
  141.         NORTHEAST = 12 ,
  142.         NORTHWEST = 13 ,
  143.         SOUTHEAST = 14 ,
  144.         SOUTHWEST = 15 ,
  145.         UP = 16 ,
  146.         DOWN = 17 ,
  147.         LEFT = 18 ,
  148.         RIGHT = 19 ,
  149.     };
  150.  
  151.     WorldSession* Session = handler->GetSession();
  152.     Player* pPlayer = Session->GetPlayer();
  153.     uint64 GUID = pPlayer->GetGUID();
  154.  
  155.     if(!args)
  156.         return false;
  157.  
  158.     char* ID_t = strtok((char*)args, " "); // change (char*)args incase of a crash
  159.     if(!ID_t)
  160.         return false;
  161.     uint32 ID = atoi(ID_t);
  162.  
  163.     char* ARG_t = strtok(NULL, " ");
  164.     uint32 ARG = 0;
  165.     if(ARG_t)
  166.         ARG = atoi(ARG_t);
  167.  
  168.     if(GOMove.find(GUID) == GOMove.end())
  169.         GOMove[GUID].Face = false;
  170.  
  171.     if(ARG == 0 && ID <= 6) // no args
  172.     {
  173.         switch(ID)
  174.         {
  175.         case TEST:
  176.             {
  177.                 Session->SendAreaTriggerMessage(pPlayer->GetName());
  178.             }
  179.             break;
  180.  
  181.         case SELECTNEAR:
  182.             {
  183.                 QueryResult& result = WorldDatabase.PQuery("SELECT gameobject.guid, id, position_x, position_y, position_z, orientation, map, phaseMask, "
  184.                     "(POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ FROM gameobject "
  185.                     "WHERE map = '%i' ORDER BY order_ ASC LIMIT 10",
  186.                     handler->GetSession()->GetPlayer()->GetPositionX(), handler->GetSession()->GetPlayer()->GetPositionY(), handler->GetSession()->GetPlayer()->GetPositionZ(),
  187.                     handler->GetSession()->GetPlayer()->GetMapId());
  188.  
  189.                 if (!result)
  190.                 {
  191.                     handler->SendSysMessage(LANG_COMMAND_TARGETOBJNOTFOUND);
  192.                     return true;
  193.                 }
  194.  
  195.                 bool found = false;
  196.                 uint32 guidLow, id,poolId;
  197.  
  198.                 do
  199.                 {
  200.                     Field* fields = result->Fetch();
  201.                     guidLow = fields[0].GetUInt32();
  202.                     id =      fields[1].GetUInt32();
  203.                     poolId =  sPoolMgr->IsPartOfAPool<GameObject>(guidLow);
  204.                     if (!poolId || sPoolMgr->IsSpawnedObject<GameObject>(guidLow))
  205.                         found = true;
  206.                 } while (result->NextRow() && !found);
  207.  
  208.                 if (!found)
  209.                 {
  210.                     handler->PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST, id);
  211.                     return true;
  212.                 }
  213.                 GameObject * Target = pPlayer->GetMap()->GetGameObject(MAKE_NEW_GUID(guidLow, id, HIGHGUID_GAMEOBJECT));
  214.                 GOMove[GUID].pGob = Target;
  215.                 if(GOSaved.find(Target->GetGUID()) == GOSaved.end())
  216.                     GOSaved[Target->GetGUID()] = Target;
  217.                 // SEND_USED(pPlayer, str)
  218.                 Session->SendAreaTriggerMessage("Selected %s", Target->GetName());
  219.             }
  220.             break;
  221.  
  222.         case DELET:
  223.             {
  224.                 if(GOMove[GUID].pGob)
  225.                 {
  226.                     GameObject* Target = GOMove[GUID].pGob;
  227.                     uint64 ownerGuid = Target->GetOwnerGUID();
  228.                     if (ownerGuid)
  229.                     {
  230.                         Unit* owner = ObjectAccessor::GetUnit(*handler->GetSession()->GetPlayer(), ownerGuid);
  231.                         if (!owner || !IS_PLAYER_GUID(ownerGuid))
  232.                         {
  233.                             handler->PSendSysMessage(LANG_COMMAND_DELOBJREFERCREATURE, GUID_LOPART(ownerGuid), Target->GetGUIDLow());
  234.                             handler->SetSentErrorMessage(true);
  235.                             return false;
  236.                         }
  237.                         owner->RemoveGameObject(Target, false);
  238.                     }
  239.  
  240.                     Target->SetRespawnTime(0);                                 // not save respawn time
  241.                     Target->Delete();
  242.                     Target->DeleteFromDB();
  243.  
  244.                     handler->PSendSysMessage(LANG_COMMAND_DELOBJMESSAGE, Target->GetGUIDLow());
  245.  
  246.                     GOMove[GUID].pGob = NULL;
  247.                 }
  248.                 else
  249.                     Session->SendNotification("No object selected");
  250.             }
  251.             break;
  252.  
  253.         case X:
  254.             {
  255.                 if(GOMove[GUID].pGob)
  256.                 {
  257.                     GameObject* Target = GOMove[GUID].pGob;
  258.                     float x,y,z,o;
  259.                     Target->GetPosition(x,y,z,o);
  260.                     GO(GUID,NULL,pPlayer->GetPositionX(),y,z,o,0,pPlayer,Target, handler);
  261.                 }
  262.                 else
  263.                     Session->SendNotification("No object selected");
  264.             }
  265.             break;
  266.  
  267.         case Y:
  268.             {
  269.                 if(GOMove[GUID].pGob)
  270.                 {
  271.                     GameObject* Target = GOMove[GUID].pGob;
  272.                     float x,y,z,o;
  273.                     Target->GetPosition(x,y,z,o);
  274.                     GO(GUID,NULL,x,pPlayer->GetPositionY(),z,o,0,pPlayer,Target, handler);
  275.                 }
  276.                 else
  277.                     Session->SendNotification("No object selected");
  278.             }
  279.             break;
  280.  
  281.         case Z:
  282.             {
  283.                 if(GOMove[GUID].pGob)
  284.                 {
  285.                     GameObject* Target = GOMove[GUID].pGob;
  286.                     float x,y,z,o;
  287.                     Target->GetPosition(x,y,z,o);
  288.                     GO(GUID,NULL,x,y,pPlayer->GetPositionZ(),o,0,pPlayer,Target, handler);
  289.                 }
  290.                 else
  291.                     Session->SendNotification("No object selected");
  292.             }
  293.             break;
  294.  
  295.         case O:
  296.             {
  297.                 if(GOMove[GUID].pGob)
  298.                 {
  299.                     GameObject* Target = GOMove[GUID].pGob;
  300.                     float x,y,z,o;
  301.                     Target->GetPosition(x,y,z,o);
  302.                     GO(GUID,NULL,x,y,z,pPlayer->GetOrientation(),0,pPlayer,Target, handler);
  303.                 }
  304.                 else
  305.                     Session->SendNotification("No object selected");
  306.             }
  307.             break;
  308.  
  309.         default:
  310.             {
  311.                 Session->SendNotification("ID not found: or ID, argument combination was wrong: %u, %u", ID, ARG);
  312.             }
  313.         }
  314.         return true;
  315.     }
  316.     else if(ARG != 0 && ID >= 7)
  317.     {
  318.         switch(ID)
  319.         {
  320.         case SPAWN:
  321.             {
  322.                 bool Sel = GO(GUID, ARG, pPlayer->GetPositionX(), pPlayer->GetPositionY(), pPlayer->GetPositionZ(),  pPlayer->GetOrientation(), pPlayer->GetPhaseMaskForSpawn(), pPlayer, NULL, handler);
  323.                 if(!Sel)
  324.                     Session->SendNotification("No gameobject found with entry %u", ARG);
  325.                 else
  326.                 {
  327.                     Session->SendAreaTriggerMessage("%s spawned", GOMove[GUID].pGob->GetName());
  328.                     // SEND_USED(pPlayer, str)
  329.                 }
  330.             }
  331.             break;
  332.  
  333.         case NORTH:
  334.             {
  335.                 if(GOMove[GUID].pGob)
  336.                 {
  337.                     GameObject* Target = GOMove[GUID].pGob;
  338.                     float x,y,z,o;
  339.                     Target->GetPosition(x,y,z,o);
  340.                     GO(GUID,NULL,x+(ARG/100),y,z,o,0,pPlayer,Target, handler);
  341.                     std::string test;
  342.                     test += (ARG/100);
  343.                     Session->SendNotification(test.c_str());
  344.                 }
  345.                 else
  346.                     Session->SendNotification("No object selected");
  347.             }
  348.             break;
  349.  
  350.         case EAST: // Move east
  351.             {
  352.                 if(GOMove[GUID].pGob)
  353.                 {
  354.                     GameObject* Target = GOMove[GUID].pGob;
  355.                     float x,y,z,o;
  356.                     Target->GetPosition(x,y,z,o);
  357.                     GO(GUID,NULL,x,y-(ARG/100),z,o,0,pPlayer,Target, handler);
  358.                 }
  359.                 else
  360.                     Session->SendNotification("No object selected");
  361.             }
  362.             break;
  363.  
  364.         case SOUTH: // Move south
  365.             {
  366.                 if(GOMove[GUID].pGob)
  367.                 {
  368.                     GameObject* Target = GOMove[GUID].pGob;
  369.                     float x,y,z,o;
  370.                     Target->GetPosition(x,y,z,o);
  371.                     GO(GUID,NULL,x-(ARG/100),y,z,o,0,pPlayer,Target, handler);
  372.                 }
  373.                 else
  374.                     Session->SendNotification("No object selected");
  375.             }
  376.             break;
  377.  
  378.         case WEST: // Move west
  379.             {
  380.                 if(GOMove[GUID].pGob)
  381.                 {
  382.                     GameObject* Target = GOMove[GUID].pGob;
  383.                     float x,y,z,o;
  384.                     Target->GetPosition(x,y,z,o);
  385.                     GO(GUID,NULL,x,y+(ARG/100),z,o,0,pPlayer,Target, handler);
  386.                 }
  387.                 else
  388.                     Session->SendNotification("No object selected");
  389.             }
  390.             break;
  391.  
  392.         case NORTHEAST: // Move Northeast
  393.             {
  394.                 if(GOMove[GUID].pGob)
  395.                 {
  396.                     GameObject* Target = GOMove[GUID].pGob;
  397.                     float x,y,z,o;
  398.                     Target->GetPosition(x,y,z,o);
  399.                     GO(GUID,NULL,x+(ARG/100),y-(ARG/100),z,o,0,pPlayer,Target, handler);
  400.                 }
  401.                 else
  402.                     Session->SendNotification("No object selected");
  403.             }
  404.             break;
  405.  
  406.         case SOUTHEAST: // Move southeast
  407.             {
  408.                 if(GOMove[GUID].pGob)
  409.                 {
  410.                     GameObject* Target = GOMove[GUID].pGob;
  411.                     float x,y,z,o;
  412.                     Target->GetPosition(x,y,z,o);
  413.                     GO(GUID,NULL,x-(ARG/100),y-(ARG/100),z,o,0,pPlayer,Target, handler);
  414.                 }
  415.                 else
  416.                     Session->SendNotification("No object selected");
  417.             }
  418.             break;
  419.  
  420.         case SOUTHWEST: // Move southwest
  421.             {
  422.                 if(GOMove[GUID].pGob)
  423.                 {
  424.                     GameObject* Target = GOMove[GUID].pGob;
  425.                     float x,y,z,o;
  426.                     Target->GetPosition(x,y,z,o);
  427.                     GO(GUID,NULL,x-(ARG/100),y+(ARG/100),z,o,0,pPlayer,Target, handler);
  428.                 }
  429.                 else
  430.                     Session->SendNotification("No object selected");
  431.             }
  432.             break;
  433.  
  434.         case NORTHWEST: // Move northwest
  435.             {
  436.                 if(GOMove[GUID].pGob)
  437.                 {
  438.                     GameObject* Target = GOMove[GUID].pGob;
  439.                     float x,y,z,o;
  440.                     Target->GetPosition(x,y,z,o);
  441.                     GO(GUID,NULL,x+(ARG/100),y+(ARG/100),z,o,0,pPlayer,Target, handler);
  442.                 }
  443.                 else
  444.                     Session->SendNotification("No object selected");
  445.             }
  446.             break;
  447.  
  448.         case UP: // Move up
  449.             {
  450.                 if(GOMove[GUID].pGob)
  451.                 {
  452.                     GameObject* Target = GOMove[GUID].pGob;
  453.                     float x,y,z,o;
  454.                     Target->GetPosition(x,y,z,o);
  455.                     GO(GUID,NULL,x,y,z+(ARG/100),o,0,pPlayer,Target, handler);
  456.                 }
  457.                 else
  458.                     Session->SendNotification("No object selected");
  459.             }
  460.             break;
  461.  
  462.         case DOWN: // Move down
  463.             {
  464.                 if(GOMove[GUID].pGob)
  465.                 {
  466.                     GameObject* Target = GOMove[GUID].pGob;
  467.                     float x,y,z,o;
  468.                     Target->GetPosition(x,y,z,o);
  469.                     GO(GUID,NULL,x,y,z-(ARG/100),o,0,pPlayer,Target, handler);
  470.                 }
  471.                 else
  472.                     Session->SendNotification("No object selected");
  473.             }
  474.             break;
  475.  
  476.         case RIGHT:
  477.             {
  478.                 if(GOMove[GUID].pGob)
  479.                 {
  480.                     GameObject* Target = GOMove[GUID].pGob;
  481.                     float x,y,z,o;
  482.                     Target->GetPosition(x,y,z,o);
  483.                     GO(GUID,NULL,x,y,z,o-(ARG/100),0,pPlayer,Target, handler);
  484.                 }
  485.                 else
  486.                     Session->SendNotification("No object selected");
  487.             }
  488.             break;
  489.  
  490.         case LEFT:
  491.             {
  492.                 if(GOMove[GUID].pGob)
  493.                 {
  494.                     GameObject* Target = GOMove[GUID].pGob;
  495.                     float x,y,z,o;
  496.                     Target->GetPosition(x,y,z,o);
  497.                     GO(GUID,NULL,x,y,z,o+(ARG/100),0,pPlayer,Target, handler);
  498.                 }
  499.                 else
  500.                     Session->SendNotification("No object selected");
  501.             }
  502.             break;
  503.  
  504.         default:
  505.             {
  506.                 Session->SendNotification("ID not found: or ID, argument combination was wrong: %u, %u", ID, ARG);
  507.             }
  508.         }
  509.         return true;
  510.     }
  511.     return false;
  512. }
  513. };
  514.  
  515. void AddSC_GOMove_commandscript()
  516. {
  517.     new GOMove_commandscript();
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement