Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Alistar @ AC-WEB.ORG | With love to Whitemane DEVs.
- */
- #include "ScriptedGossip.h"
- #include "DatabaseEnv.h"
- #include "ObjectMgr.h"
- #include "SpellMgr.h"
- #include "Chat.h"
- #include "Log.h"
- // If you wish to charge an item enter itemId and ItemAmount otherwise keep at them both 0
- static constexpr uint32 itemId = 0; // Item Id
- static constexpr uint8 itemCount = 0; // Item Amount
- static std::unordered_map<uint8, std::unordered_map<uint8, std::vector<uint32>>> racialSpellMap;
- static std::unordered_map<uint64, uint32> racialMap;
- class World_RacialSwitch : public WorldScript
- {
- public:
- World_RacialSwitch()
- : WorldScript("World_RacialSwitch") { }
- void StoreRacialData()
- {
- QueryResult result = CharacterDatabase.Query("SELECT race, class, spellId FROM racialswitch_data");
- if (result)
- {
- do
- {
- Field* fields = result->Fetch();
- uint8 race = fields[0].GetUInt8();
- uint8 Class = fields[1].GetUInt8();
- uint32 spellId = fields[2].GetUInt32();
- racialSpellMap[race][Class].push_back(spellId);
- } while (result->NextRow());
- }
- }
- void LoadRacials()
- {
- uint8 count = 0;
- QueryResult result = CharacterDatabase.Query("SELECT guid, raceId FROM racialswitch");
- if (result)
- {
- do
- {
- Field* field = result->Fetch();
- uint64 guid = field[0].GetUInt32();
- uint8 raceId = field[1].GetUInt8();
- racialMap[guid] = raceId;
- ++count;
- } while (result->NextRow());
- }
- TC_LOG_INFO("server.loading", ">> Loaded %u custom racials.", count);
- }
- // Store / Load custom racials on server start.
- void OnStartup() override
- {
- StoreRacialData();
- LoadRacials();
- }
- // Save racials on server shutdown.
- void OnShutdown() override
- {
- for (const auto& i : racialMap)
- CharacterDatabase.PExecute("REPLACE INTO racialswitch VALUES ('%u', '%u')", i.first, i.second);
- }
- };
- class Npc_RacialSwitch : public CreatureScript
- {
- public:
- Npc_RacialSwitch()
- : CreatureScript("Npc_RacialSwitch") { }
- struct Npc_RacialSwitchAI : public ScriptedAI
- {
- Npc_RacialSwitchAI(Creature* creature) : ScriptedAI(creature) { }
- static void UpdateRacials(Player* player, uint8 race, bool learn)
- {
- for (std::unordered_map<uint8, std::vector<uint32>>::const_iterator it = racialSpellMap[race].begin(); it != racialSpellMap[race].end(); ++it)
- {
- for (const auto& spellId : it->second)
- {
- if (!learn)
- player->RemoveSpell(spellId, false, false);
- else if (it->first == player->GetClass())
- player->LearnSpell(spellId, false);
- }
- }
- }
- static void Racials(Player* player, uint8 raceId)
- {
- uint64 playerGuid = player->GetGUID().GetRawValue();
- // check if player has already changed their racials
- if (racialMap.find(playerGuid) != racialMap.end())
- {
- // don't allow learning an already active race
- if (raceId == racialMap[playerGuid])
- return;
- // remove previous racials
- UpdateRacials(player, racialMap[playerGuid], false);
- }
- else // first time changing racials
- {
- // remove own race racials
- UpdateRacials(player, player->GetRace(), false);
- }
- // learn new racials
- UpdateRacials(player, raceId, true);
- // update racial map
- racialMap[playerGuid] = raceId;
- }
- bool OnGossipHello(Player* player) override
- {
- for (uint8 i = RACE_HUMAN; i < MAX_RACES; ++i)
- {
- if (i == 9) continue; // goblins...
- AddGossipItemFor(player, GOSSIP_ICON_TRAINER, EnumUtils::ToTitle(Races(i)), GOSSIP_SENDER_MAIN, i);
- }
- SendGossipMenuFor(player, 1, me->GetGUID());
- return true;
- }
- bool OnGossipSelect(Player* player, uint32 /*menu_id*/, uint32 gossipListId) override
- {
- uint32 sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId);
- uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
- return OnGossipSelect(player, me, sender, action);
- }
- bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
- {
- ClearGossipMenuFor(player);
- if (itemId != 0 && itemCount != 0)
- {
- if (!player->HasItemCount(itemId, itemCount))
- {
- if (const ItemTemplate* it = sObjectMgr->GetItemTemplate(itemId))
- ChatHandler(player->GetSession()).PSendSysMessage("You need: [%s] x%u", it->Name1.c_str(), itemCount);
- player->PlayerTalkClass->SendCloseGossip();
- return true;
- }
- else
- player->DestroyItemCount(itemId, itemCount, true);
- }
- Racials(player, action);
- player->PlayerTalkClass->SendCloseGossip();
- return true;
- }
- };
- CreatureAI* GetAI(Creature* creature) const override
- {
- return new Npc_RacialSwitchAI(creature);
- }
- };
- class Player_RacialSwitch : public PlayerScript
- {
- public:
- Player_RacialSwitch() : PlayerScript("Player_RacialSwitch") { }
- void OnLogin(Player* player, bool /*firstLogin*/) override
- {
- if (racialMap.find(player->GetGUID().GetRawValue()) != racialMap.end())
- {
- ChatHandler(player->GetSession()).PSendSysMessage("|cffff0000[Racial Switch]:|r You're using %s's racials.", EnumUtils::ToTitle(Races(racialMap[player->GetGUID().GetRawValue()])));
- Npc_RacialSwitch::Npc_RacialSwitchAI::UpdateRacials(player, player->GetRace(), false);
- }
- }
- };
- void AddSC_Npc_RacialSwitch()
- {
- new World_RacialSwitch();
- new Npc_RacialSwitch();
- new Player_RacialSwitch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement