Advertisement
Guest User

Untitled

a guest
Feb 13th, 2021
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. /*
  2. Alistar @ AC-WEB.ORG | With love to Whitemane DEVs.
  3. */
  4.  
  5. #include "ScriptedGossip.h"
  6. #include "DatabaseEnv.h"
  7. #include "ObjectMgr.h"
  8. #include "SpellMgr.h"
  9. #include "Chat.h"
  10. #include "Log.h"
  11.  
  12. // If you wish to charge an item enter itemId and ItemAmount otherwise keep at them both 0
  13. static constexpr uint32 itemId = 0;   // Item Id
  14. static constexpr uint8 itemCount = 0; // Item Amount
  15.  
  16. static std::unordered_map<uint8, std::unordered_map<uint8, std::vector<uint32>>> racialSpellMap;
  17. static std::unordered_map<uint64, uint32> racialMap;
  18.  
  19. class World_RacialSwitch : public WorldScript
  20. {
  21. public:
  22.     World_RacialSwitch()
  23.         : WorldScript("World_RacialSwitch") { }
  24.  
  25.     void StoreRacialData()
  26.     {
  27.         QueryResult result = CharacterDatabase.Query("SELECT race, class, spellId FROM racialswitch_data");
  28.         if (result)
  29.         {
  30.             do
  31.             {
  32.                 Field* fields = result->Fetch();
  33.                 uint8 race = fields[0].GetUInt8();
  34.                 uint8 Class = fields[1].GetUInt8();
  35.                 uint32 spellId = fields[2].GetUInt32();
  36.  
  37.                 racialSpellMap[race][Class].push_back(spellId);
  38.  
  39.             } while (result->NextRow());
  40.         }
  41.     }
  42.  
  43.     void LoadRacials()
  44.     {
  45.         uint8 count = 0;
  46.         QueryResult result = CharacterDatabase.Query("SELECT guid, raceId FROM racialswitch");
  47.         if (result)
  48.         {
  49.             do
  50.             {
  51.                 Field* field = result->Fetch();
  52.                 uint64 guid = field[0].GetUInt32();
  53.                 uint8 raceId = field[1].GetUInt8();
  54.  
  55.                 racialMap[guid] = raceId;
  56.                 ++count;
  57.             } while (result->NextRow());
  58.         }
  59.         TC_LOG_INFO("server.loading", ">> Loaded %u custom racials.", count);
  60.     }
  61.  
  62.     // Store / Load custom racials on server start.
  63.     void OnStartup() override
  64.     {
  65.         StoreRacialData();
  66.         LoadRacials();
  67.     }
  68.  
  69.     // Save racials on server shutdown.
  70.     void OnShutdown() override
  71.     {
  72.         for (const auto& i : racialMap)
  73.             CharacterDatabase.PExecute("REPLACE INTO racialswitch VALUES ('%u', '%u')", i.first, i.second);
  74.     }
  75. };
  76.  
  77. class Npc_RacialSwitch : public CreatureScript
  78. {
  79. public:
  80.     Npc_RacialSwitch()
  81.         : CreatureScript("Npc_RacialSwitch") { }
  82.  
  83.     struct Npc_RacialSwitchAI : public ScriptedAI
  84.     {
  85.         Npc_RacialSwitchAI(Creature* creature) : ScriptedAI(creature) { }
  86.  
  87.         static void UpdateRacials(Player* player, uint8 race, bool learn)
  88.         {
  89.             for (std::unordered_map<uint8, std::vector<uint32>>::const_iterator it = racialSpellMap[race].begin(); it != racialSpellMap[race].end(); ++it)
  90.             {
  91.                 for (const auto& spellId : it->second)
  92.                 {
  93.                     if (!learn)
  94.                         player->RemoveSpell(spellId, false, false);
  95.                     else if (it->first == player->GetClass())
  96.                         player->LearnSpell(spellId, false);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         static void Racials(Player* player, uint8 raceId)
  102.         {
  103.             uint64 playerGuid = player->GetGUID().GetRawValue();
  104.  
  105.             // check if player has already changed their racials
  106.             if (racialMap.find(playerGuid) != racialMap.end())
  107.             {
  108.                 // don't allow learning an already active race
  109.                 if (raceId == racialMap[playerGuid])
  110.                     return;
  111.  
  112.                 // remove previous racials
  113.                 UpdateRacials(player, racialMap[playerGuid], false);
  114.  
  115.             }
  116.             else // first time changing racials
  117.             {
  118.                 // remove own race racials
  119.                 UpdateRacials(player, player->GetRace(), false);
  120.             }
  121.  
  122.             // learn new racials
  123.             UpdateRacials(player, raceId, true);
  124.  
  125.             // update racial map
  126.             racialMap[playerGuid] = raceId;
  127.         }
  128.  
  129.         bool OnGossipHello(Player* player) override
  130.         {
  131.             for (uint8 i = RACE_HUMAN; i < MAX_RACES; ++i)
  132.             {
  133.                 if (i == 9) continue; // goblins...
  134.                 AddGossipItemFor(player, GOSSIP_ICON_TRAINER, EnumUtils::ToTitle(Races(i)), GOSSIP_SENDER_MAIN, i);
  135.             }
  136.  
  137.             SendGossipMenuFor(player, 1, me->GetGUID());
  138.             return true;
  139.         }
  140.  
  141.         bool OnGossipSelect(Player* player, uint32 /*menu_id*/, uint32 gossipListId) override
  142.         {
  143.             uint32 sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId);
  144.             uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
  145.             return OnGossipSelect(player, me, sender, action);
  146.         }
  147.  
  148.         bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
  149.         {
  150.             ClearGossipMenuFor(player);
  151.  
  152.             if (itemId != 0 && itemCount != 0)
  153.             {
  154.                 if (!player->HasItemCount(itemId, itemCount))
  155.                 {
  156.                     if (const ItemTemplate* it = sObjectMgr->GetItemTemplate(itemId))
  157.                         ChatHandler(player->GetSession()).PSendSysMessage("You need: [%s] x%u", it->Name1.c_str(), itemCount);
  158.                     player->PlayerTalkClass->SendCloseGossip();
  159.                     return true;
  160.                 }
  161.                 else
  162.                     player->DestroyItemCount(itemId, itemCount, true);
  163.             }
  164.  
  165.             Racials(player, action);
  166.  
  167.             player->PlayerTalkClass->SendCloseGossip();
  168.             return true;
  169.         }
  170.     };
  171.  
  172.     CreatureAI* GetAI(Creature* creature) const override
  173.     {
  174.         return new Npc_RacialSwitchAI(creature);
  175.     }
  176. };
  177.  
  178. class Player_RacialSwitch : public PlayerScript
  179. {
  180. public:
  181.     Player_RacialSwitch() : PlayerScript("Player_RacialSwitch") { }
  182.  
  183.     void OnLogin(Player* player, bool /*firstLogin*/) override
  184.     {
  185.         if (racialMap.find(player->GetGUID().GetRawValue()) != racialMap.end())
  186.         {
  187.             ChatHandler(player->GetSession()).PSendSysMessage("|cffff0000[Racial Switch]:|r You're using %s's racials.", EnumUtils::ToTitle(Races(racialMap[player->GetGUID().GetRawValue()])));
  188.             Npc_RacialSwitch::Npc_RacialSwitchAI::UpdateRacials(player, player->GetRace(), false);
  189.         }
  190.     }
  191. };
  192.  
  193. void AddSC_Npc_RacialSwitch()
  194. {
  195.     new World_RacialSwitch();
  196.     new Npc_RacialSwitch();
  197.     new Player_RacialSwitch();
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement