Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.50 KB | None | 0 0
  1. #include "Player.h"
  2. #include "DatabaseWorker.h"
  3. #include "WorldDatabase.h"
  4. #include "Field.h"
  5. #include "QueryResult.h"
  6. #include "Chat.h"
  7. #include "RBAC.h"
  8. #include "Item.h"
  9. #include "DatabaseEnv.h"
  10. #include "Spell.h"
  11. #include "Logger.h"
  12. #include "ObjectMgr.h"
  13. #include "Log.h"
  14. #include "WorldSession.h"
  15. #include "Player.h"
  16. #include "ScriptedGossip.h"
  17. constexpr int size = 6;
  18.  
  19. // This is the code which returns the fancy colors and the name of the item
  20. std::string GetItemLink(uint32 entry, WorldSession* session)
  21. {
  22.     TC_LOG_DEBUG("custom.transmog", "Transmogrification::GetItemLink");
  23.  
  24.     const ItemTemplate* temp = sObjectMgr->GetItemTemplate(entry);
  25.     LocaleConstant loc_idx = session->GetSessionDbLocaleIndex();
  26.     std::string name = temp->Name1;
  27.     if (ItemLocale const* il = sObjectMgr->GetItemLocale(entry))
  28.         ObjectMgr::GetLocaleString(il->Name, loc_idx, name);
  29.  
  30.     std::ostringstream oss;
  31.     oss << "|c" << std::hex << ItemQualityColors[temp->Quality] << std::dec <<
  32.         "|Hitem:" << entry << ":0:0:0:0:0:0:0:0:0|h[" << name << "]|h|r";
  33.  
  34.     return oss.str();
  35. }
  36.  
  37. // This is the structure of the ItemUpgrader, if you want to add more requirements do it here
  38. struct ItemUgpradeStructure
  39. {
  40.     uint32 itemEntry;
  41.     std::array<uint32, size> reqItems;
  42.     std::array<uint32, size> reqItemCounts;
  43.     uint32 upgradedItem;
  44. };
  45.  
  46. // this is the map in which we're storing all of the data related to the item upgrader
  47. std::unordered_map<uint32, ItemUgpradeStructure> ItemUpgradeMap;
  48.  
  49. // this is the function which loads the item upgrade information from the database and places it into our structure and then the map
  50. void LoadItemUpgrader()
  51. {
  52.     static_assert(decltype(ItemUgpradeStructure::reqItems)().size() == decltype(ItemUgpradeStructure::reqItemCounts)().size(), "reqItems and reqItemCounts size must be equal!");
  53.     ItemUpgradeMap.clear();
  54.     QueryResult result = WorldDatabase.PQuery("SELECT entry, reqItem1, reqItem2, reqItem3, reqItem4, reqItem5, reqItem6, reqItemCount1, reqItemCount2, reqItemCount3, reqItemCount4, reqItemCount5, reqItemCount6, upgradedItem FROM z_item_upgrader");
  55.     if (result)
  56.         do
  57.         {
  58.             Field* fields = result->Fetch();
  59.             ItemUgpradeStructure its;
  60.  
  61.             its.itemEntry = fields[0].GetUInt32();
  62.             for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
  63.             {
  64.                 its.reqItems[i] = fields[i + 1].GetUInt32();
  65.                 its.reqItemCounts[i] = fields[i + 7].GetUInt32();
  66.             }
  67.             // this part here places our info into the map
  68.  
  69.             ItemUpgradeMap.insert({ its.itemEntry, its });
  70.  
  71.         } while (result->NextRow());
  72. }
  73.  
  74. // this class loads our item upgrade table from the databasse on startup
  75. class item_upgrade_loader : public WorldScript
  76. {
  77. public:
  78.     item_upgrade_loader() : WorldScript("item_upgrade_loader") { }
  79.  
  80.     void OnStartup()
  81.     {
  82.         LoadItemUpgrader();
  83.     }
  84. };
  85.  
  86. // This class handles coommands for reloading the item upgrade info, use it whenever you edit soemthing in the database
  87. class item_upgrade_reload_command : public CommandScript
  88. {
  89. public:
  90.     item_upgrade_reload_command() : CommandScript("item_upgrade_reload_command") { }
  91.  
  92.     std::vector<ChatCommand> GetCommands() const
  93.     {
  94.         static std::vector<ChatCommand> reloadCommandTable =
  95.         {
  96.             { "item_upgrade_system",   rbac::RBAC_PERM_COMMAND_RELOAD, false, &HandleReloadItemUpgrader,   "" },
  97.         };
  98.  
  99.         static std::vector<ChatCommand> commandTable =
  100.         {
  101.             { "reload",  rbac::RBAC_PERM_COMMAND_RELOAD, false,  NULL, "", reloadCommandTable },
  102.         };
  103.         return commandTable;
  104.     }
  105.  
  106.     static bool HandleReloadItemUpgrader(ChatHandler * handler, const char * args)
  107.     {
  108.         handler->SendSysMessage("Reloading Item Upgrade System");
  109.         LoadItemUpgrader();
  110.         handler->SendSysMessage("Item Upgrade System Reloaded");
  111.         return true;
  112.     }
  113. };
  114.  
  115. // If you want to add more requirements in the future
  116. // You will need to edit this boolean to check if the players have what's required
  117. bool CanUpgrade(Player* player, uint32 actions)
  118. {
  119.     for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
  120.     {
  121.         if (ItemUpgradeMap[actions].reqItems[i] && !player->HasItemCount(ItemUpgradeMap[actions].reqItems[i], ItemUpgradeMap[actions].reqItemCounts[i]))
  122.             return false;
  123.     }
  124.     return true;
  125. }
  126.  
  127. // the code for the item upgrader itself
  128. class item_upgrader : public ItemScript
  129. {
  130. public:
  131.     item_upgrader() : ItemScript("item_upgrader") {}
  132.  
  133.     bool OnUse(Player* player, Item* item, SpellCastTargets const& targets) override
  134.     {
  135.         uint32 spell = item->GetSpell();
  136.  
  137.         if (targets.GetItemTarget() && targets.GetItemTarget()->GetTypeId() == TYPEID_ITEM)
  138.         {
  139.             if (ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].itemEntry)
  140.             {
  141.                 std::ostringstream upgradeText;
  142.                 upgradeText << "Are you sure you want to upgrade\n\n" << GetItemLink(targets.GetItemTarget()->GetEntry(), player->GetSession()) << " into " << GetItemLink(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].upgradedItem, player->GetSession());
  143.                 upgradeText << "\n\nIn order to upgrade your item you will need:\n\n";
  144.  
  145.                 for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
  146.                 {
  147.                     if (sObjectMgr->GetItemTemplate(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItems[i]))
  148.                         upgradeText << GetItemLink(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItems[i], player->GetSession()) << " x " << std::to_string(player->GetItemCount(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItemCounts[i])) << " / " << std::to_string(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItemCounts[i]) << "\n";
  149.                 }
  150.                 player->PlayerTalkClass->ClearMenus();
  151.                 AddGossipItemFor(player, -1, "", 1, targets.GetItemTarget()->GetEntry(), upgradeText.str().c_str(), 0, false);
  152.  
  153.                 SendGossipMenuFor(player, 1, item->GetGUID());
  154.             }
  155.             else
  156.                 ChatHandler(player->GetSession()).PSendSysMessage("|cff46def2[SYSTEM]: |r|cff5994f2This item is not upgradable.|r");
  157.         }
  158.         else
  159.             player->SendEquipError(EQUIP_ERR_CANT_DO_RIGHT_NOW, item, NULL);
  160.         player->RemoveAura(spell);
  161.         return true;
  162.     }
  163.  
  164.     // This is the part which actually removes the required items and gives the player the upgraded item
  165.     void OnGossipSelect(Player * player, Item * item, uint32 sender, uint32 actions)
  166.     {
  167.         if (!CanUpgrade(player, actions))
  168.         {
  169.             ChatHandler(player->GetSession()).PSendSysMessage("You do not have the required materials needed to upgrade this item.");
  170.             return;
  171.         }
  172.         for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
  173.         {
  174.             if (ItemUpgradeMap[actions].reqItems[i])
  175.                 player->DestroyItemCount(ItemUpgradeMap[actions].reqItems[i], ItemUpgradeMap[actions].reqItemCounts[i], true);
  176.         }
  177.         player->AddItem(ItemUpgradeMap[actions].upgradedItem, 1);
  178.         player->DestroyItemCount(actions, 1, true);
  179.        
  180.     }
  181. };
  182.  
  183. // Add to script loader
  184. void AddSC_item_upgrade_system()
  185. {
  186.     new item_upgrade_loader();
  187.     new item_upgrade_reload_command();
  188.     new item_upgrader();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement