Advertisement
Rochet2

Teleport location saver

Nov 21st, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.16 KB | None | 0 0
  1. // Teleport location saver (not tested)
  2. // By Rochet2
  3.  
  4.  
  5. #include "GossipDef.h"
  6. #include "ScriptedGossip.h"
  7. #include "Common.h"
  8. #include "Player.h"
  9. #include "Creature.h"
  10. #include "ScriptMgr.h"
  11. #include "Object.h"
  12. #include "WorldSession.h"
  13. #include "DatabaseEnv.h"
  14.  
  15. struct Data
  16. {
  17.     std::string name;
  18.     WorldLocation loc;
  19. };
  20.  
  21. static const size_t maxteles = 5;
  22. static std::unordered_map<ObjectGuid, std::map<uint32, Data> > playerLocations;
  23.  
  24. class myscriptclass0 : public WorldScript
  25. {
  26. public:
  27.     myscriptclass0() : WorldScript("myscriptclass0")
  28.     {
  29.     }
  30.  
  31.     void OnStartup() override
  32.     {
  33.         CharacterDatabase.DirectExecute("CREATE TABLE IF NOT EXISTS `player_teleports` ("
  34.             " `guid` INT UNSIGNED NOT NULL,"
  35.             " `id` INT UNSIGNED NOT NULL,"
  36.             " `name` TEXT NULL,"
  37.             " `map` INT UNSIGNED NOT NULL,"
  38.             " `x` FLOAT NOT NULL,"
  39.             " `y` FLOAT NOT NULL,"
  40.             " `z` FLOAT NOT NULL,"
  41.             " `o` FLOAT NOT NULL,"
  42.             " PRIMARY KEY(`guid`, `id`)"
  43.             " )"
  44.             " COLLATE = 'utf8_general_ci'"
  45.             " ENGINE = InnoDB"
  46.             " ;");
  47.     }
  48. };
  49.  
  50. class myscriptclass1 : public PlayerScript
  51. {
  52. public:
  53.     myscriptclass1() : PlayerScript("myscriptclass1")
  54.     {
  55.     }
  56.  
  57.     void OnLogin(Player* player, bool /*loginFirst*/) override
  58.     {
  59.         QueryResult res = CharacterDatabase.PQuery("SELECT `id`, `name`, `map`, `x`, `y`, `z`, `o` FROM player_teleports WHERE `guid` = %u", player->GetGUID().GetCounter());
  60.         if (!res)
  61.             return;
  62.         auto& teles = playerLocations[player->GetGUID()];
  63.         teles.clear();
  64.         do
  65.         {
  66.             auto fields = res->Fetch();
  67.             auto& t = teles[fields[0].GetUInt32()];
  68.             t.name = fields[1].GetString();
  69.             t.loc.WorldRelocate(fields[2].GetUInt32(), fields[3].GetFloat(), fields[4].GetFloat(), fields[5].GetFloat(), fields[6].GetFloat());
  70.         } while (res->NextRow());
  71.     }
  72.  
  73.     void OnLogout(Player* player) override
  74.     {
  75.         playerLocations.erase(player->GetGUID());
  76.     }
  77. };
  78.  
  79. class myscriptclass2 : public CreatureScript
  80. {
  81. public:
  82.     myscriptclass2() : CreatureScript("myscriptclass2")
  83.     {
  84.     }
  85.  
  86.     bool OnGossipHello(Player* player, Creature* creature) override
  87.     {
  88.         player->PlayerTalkClass->ClearMenus();
  89.         player->ADD_GOSSIP_ITEM_EXTENDED(4, "Save Position", 1, 1, "Insert teleport name", 0, true);
  90.         player->ADD_GOSSIP_ITEM(3, "Tele Position", 1, 2);
  91.         player->ADD_GOSSIP_ITEM(3, "Delete Position", 1, 3);
  92.         player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  93.     }
  94.  
  95.     bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action) override
  96.     {
  97.         player->PlayerTalkClass->ClearMenus();
  98.         if (sender == 1)
  99.         {
  100.             if (action == 2)
  101.             {
  102.                 auto& teles = playerLocations[player->GetGUID()];
  103.                 for (auto& tele : teles)
  104.                 {
  105.                     player->ADD_GOSSIP_ITEM(2, "Teleport to " + tele.second.name, action, tele.first);
  106.                 }
  107.                 player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  108.                 return true;
  109.             }
  110.             else if (action == 3)
  111.             {
  112.                 auto& teles = playerLocations[player->GetGUID()];
  113.                 for (auto& tele : teles)
  114.                 {
  115.                     player->ADD_GOSSIP_ITEM(4, "Delete " + tele.second.name, action, tele.first);
  116.                 }
  117.                 player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  118.                 return true;
  119.             }
  120.         }
  121.         else if (sender == 2)
  122.         {
  123.             auto& teles = playerLocations[player->GetGUID()];
  124.             auto itr = teles.find(action);
  125.             if (itr != teles.end())
  126.                 player->TeleportTo(itr->second.loc);
  127.             OnGossipSelect(player, creature, 1, sender);
  128.             return true;
  129.         }
  130.         else if (sender == 3)
  131.         {
  132.             auto& teles = playerLocations[player->GetGUID()];
  133.             auto itr = teles.find(action);
  134.             if (itr != teles.end())
  135.             {
  136.                 teles.erase(itr);
  137.                 CharacterDatabase.PExecute("DELETE FROM player_teleports WHERE `guid` = %u and `id` = %u", player->GetGUID().GetCounter(), action);
  138.             }
  139.             OnGossipSelect(player, creature, 1, sender);
  140.             return true;
  141.         }
  142.  
  143.         OnGossipHello(player, creature);
  144.         return true;
  145.     }
  146.  
  147.     bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code) override
  148.     {
  149.         player->PlayerTalkClass->ClearMenus();
  150.         if (sender == 1 && action == 1)
  151.         {
  152.             auto& teles = playerLocations[player->GetGUID()];
  153.             if (teles.size() < maxteles)
  154.             {
  155.                 std::string escaped(code);
  156.                 CharacterDatabase.EscapeString(escaped);
  157.                 uint32 id = 0;
  158.                 for (auto& tele : teles)
  159.                 {
  160.                     if (tele.first > id)
  161.                         break;
  162.                     ++id;
  163.                 }
  164.  
  165.                 Data data = { code, player->GetWorldLocation() };
  166.                 playerLocations[player->GetGUID()][id] = data;
  167.                 CharacterDatabase.PExecute("INSERT INTO player_teleports (`guid`, `id`, `name`, `map`, `x`, `y`, `z`, `o`) VALUES (%u, %u, \'%s\', %u, %f, %f, %f, %f)",
  168.                     player->GetGUID().GetCounter(), id, escaped.c_str(), data.loc.GetMapId(), data.loc.GetPositionX(), data.loc.GetPositionY(), data.loc.GetPositionZ(), data.loc.GetOrientation());
  169.                 player->GetSession()->SendAreaTriggerMessage("Position saved");
  170.             }
  171.             else
  172.             {
  173.                 player->GetSession()->SendNotification("You have to delete a teleport to add a new one");
  174.             }
  175.         }
  176.         OnGossipHello(player, creature);
  177.         return true;
  178.     }
  179. };
  180.  
  181. void AddSC_myscript()
  182. {
  183.     new myscriptclass0();
  184.     new myscriptclass1();
  185.     new myscriptclass2();
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement