Advertisement
Parranoia

npc_enchantment

May 24th, 2013
3,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.15 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2.  
  3. class npc_enchantment : public CreatureScript
  4. {
  5.     public:
  6.         npc_enchantment() : CreatureScript("npc_enchantment") { }
  7.  
  8.     bool OnGossipHello(Player* player, Creature* creature)
  9.     {
  10.         player->ADD_GOSSIP_ITEM(0, "[Enchant Weapon]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_MAINHAND);
  11.         player->ADD_GOSSIP_ITEM(0, "[Enchant Off-Hand]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_OFFHAND);
  12.         player->ADD_GOSSIP_ITEM(0, "[Enchant Cloak]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_BACK);
  13.         player->ADD_GOSSIP_ITEM(0, "[Enchant Chest]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_CHEST);
  14.         player->ADD_GOSSIP_ITEM(0, "[Enchant Bracers]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_WRISTS);
  15.         player->ADD_GOSSIP_ITEM(0, "[Enchant Gloves]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_HANDS);
  16.         player->ADD_GOSSIP_ITEM(0, "[Enchant Feet]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_FEET);
  17.  
  18.         player->SEND_GOSSIP_MENU(1, creature->GetGUID());
  19.         return true;
  20.     }
  21.  
  22.     void Enchant(Player* player, uint8 equipSlot, uint32 enchantID)
  23.     {
  24.         Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, equipSlot);
  25.         if (!item)
  26.         {
  27.             player->CLOSE_GOSSIP_MENU();
  28.             return;
  29.         }
  30.  
  31.         // Must re-check that the currently equipped item is valid
  32.         uint32 i;
  33.         for (i = 0; i < sSpellMgr->GetSpellInfoStoreSize(); ++i)
  34.         {
  35.             SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(i);
  36.             if (!spellInfo)
  37.                 continue;
  38.             if (spellInfo->Effects[0].Effect != SPELL_EFFECT_ENCHANT_ITEM)
  39.                 continue;
  40.             if (spellInfo->Effects[0].MiscValue != enchantID)
  41.                 continue;
  42.  
  43.             uint32 spellID = 0;
  44.             SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellInfo->Id);
  45.             for (SkillLineAbilityMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
  46.             {
  47.                 // Not from the enchanting skill
  48.                 if (itr->second->skillId != 333 || itr->second->spellId != spellInfo->Id)
  49.                     continue;
  50.                 spellID = itr->second->id;
  51.                 break;
  52.             }
  53.             if (spellID == 0)
  54.                 continue;
  55.  
  56.             int32 invType = (int32)pow((float)2, (float)item->GetTemplate()->InventoryType);
  57.             int32 subClass = (int32)pow((float)2, (float)item->GetTemplate()->SubClass);
  58.             if (item->GetTemplate()->ItemLevel < spellInfo->BaseLevel)
  59.                 continue;
  60.             else if (spellInfo->EquippedItemClass != item->GetTemplate()->Class)
  61.                 continue;
  62.             else if (!(spellInfo->EquippedItemSubClassMask & subClass))
  63.                 continue;
  64.             else if (item->GetTemplate()->Class == ITEM_CLASS_ARMOR && item->GetTemplate()->SubClass != ITEM_SUBCLASS_ARMOR_SHIELD)
  65.             {
  66.                 if (!(spellInfo->EquippedItemInventoryTypeMask & invType))
  67.                     continue;
  68.                 else // Passed all checks
  69.                     break;
  70.             }
  71.             else
  72.                 break;
  73.            
  74.         }
  75.         if (i == sSpellMgr->GetSpellInfoStoreSize())
  76.         {
  77.             ChatHandler(player->GetSession()).PSendSysMessage("Failed to enchant item.");
  78.             return;
  79.         }
  80.  
  81.         player->ApplyEnchantment(item, PERM_ENCHANTMENT_SLOT, false);
  82.         item->SetEnchantment(PERM_ENCHANTMENT_SLOT, enchantID, 0, 0);
  83.         player->ApplyEnchantment(item, PERM_ENCHANTMENT_SLOT, true);
  84.  
  85.         std::string color = "|cff";
  86.         switch (item->GetTemplate()->Quality)
  87.         {
  88.             case 0: color += "9d9d9d"; break;
  89.             case 1: color += "ffffff"; break;
  90.             case 2: color += "1eff00"; break;
  91.             case 3: color += "0070dd"; break;
  92.             case 4: color += "a335ee"; break;
  93.             case 5: color += "ff8000"; break;
  94.         }
  95.         ChatHandler(player->GetSession()).PSendSysMessage("|cffFFFFFF[%s%s|cffFFFFFF] |cffFF0000succesfully enchanted!", color.c_str(), item->GetTemplate()->Name1.c_str());
  96.     }
  97.  
  98.     void ShowPage(Player* player, uint32 page, uint8 equipSlot)
  99.     {
  100.         Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, equipSlot);
  101.         if (!item)
  102.         {
  103.             player->CLOSE_GOSSIP_MENU();
  104.             return;
  105.         }
  106.  
  107.         ItemTemplate const* itemTemplate = item->GetTemplate();
  108.         if (!itemTemplate)
  109.         {
  110.             player->CLOSE_GOSSIP_MENU();
  111.             return;
  112.         }
  113.  
  114.         player->PlayerTalkClass->ClearMenus();
  115.         uint32 startPos = (page - 1) * 10;
  116.         uint32 currentPos = 0;
  117.  
  118.         uint32 i;
  119.         for (i = 0; i < sSpellMgr->GetSpellInfoStoreSize(); ++i)
  120.         {
  121.             SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(i);
  122.             if (!spellInfo)
  123.                 continue;
  124.             if (spellInfo->Effects[0].Effect != SPELL_EFFECT_ENCHANT_ITEM)
  125.                 continue;
  126.             if (spellInfo->Id == 62257) // Ignore Titanguard
  127.                 continue;
  128.             if (itemTemplate->ItemLevel < spellInfo->BaseLevel)
  129.                 continue;
  130.             if (spellInfo->EquippedItemClass != itemTemplate->Class)
  131.                 continue;
  132.             int32 subClass = (int32)pow((float)2, (float)itemTemplate->SubClass);
  133.             if (!(spellInfo->EquippedItemSubClassMask & subClass))
  134.                 continue;
  135.             int32 invType = (int32)pow((float)2, (float)itemTemplate->InventoryType);
  136.             if (itemTemplate->Class == ITEM_CLASS_ARMOR && itemTemplate->SubClass != ITEM_SUBCLASS_ARMOR_SHIELD)
  137.                 if (!(spellInfo->EquippedItemInventoryTypeMask & invType))
  138.                     continue;
  139.  
  140.             uint32 spellID = 0;
  141.             SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellInfo->Id);
  142.             for (SkillLineAbilityMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
  143.             {
  144.                 // Not from the enchanting skill
  145.                 if (itr->second->skillId != 333 || itr->second->spellId != spellInfo->Id)
  146.                     continue;
  147.                 spellID = itr->second->id;
  148.                 break;
  149.             }
  150.             if (spellID == 0)
  151.                 continue;
  152.             if (SpellItemEnchantmentEntry const* enchant = sSpellItemEnchantmentStore.LookupEntry(spellInfo->Effects[0].MiscValue))
  153.             {
  154.                 if (enchant->requiredLevel > player->getLevel())
  155.                     continue;
  156.                 if (currentPos >= startPos)
  157.                     player->ADD_GOSSIP_ITEM(GOSSIP_ICON_INTERACT_1, enchant->description[0], equipSlot, enchant->ID);
  158.  
  159.                 currentPos++;
  160.                 // Stop looping once we are 10 positions ahead
  161.                 if (currentPos == startPos + 10)
  162.                     break;
  163.             }
  164.         }
  165.  
  166.         // Make sure there is actually another page
  167.         if (currentPos == startPos + 10 && i != sSpellMgr->GetSpellInfoStoreSize())
  168.             player->ADD_GOSSIP_ITEM(0, "Next Page", 100 + page + 1, equipSlot);  // Have to offset by 100 to ensure it gets pushed to the default case
  169.         player->ADD_GOSSIP_ITEM(0, "Back", 100 + page - 1, equipSlot);
  170.     }
  171.  
  172.     bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
  173.     {
  174.         player->PlayerTalkClass->ClearMenus();
  175.         switch(sender)
  176.         {
  177.             case GOSSIP_SENDER_MAIN:
  178.                 ShowPage(player, 1, action);
  179.                 player->SEND_GOSSIP_MENU(1, creature->GetGUID());
  180.                 break;
  181.             default:
  182.                 if (sender < 100)
  183.                 {
  184.                     Enchant(player, sender, action);
  185.                     player->CLOSE_GOSSIP_MENU();
  186.                     break;
  187.                 }
  188.                 if (sender == 100)
  189.                     OnGossipHello(player, creature);
  190.                 else
  191.                 {
  192.                     ShowPage(player, sender - 100, action);
  193.                     player->SEND_GOSSIP_MENU(1, creature->GetGUID());
  194.                 }
  195.                 break;
  196.         }
  197.         return true;
  198.     }
  199. };
  200.  
  201. void AddSC_npc_enchantment()
  202. {
  203.     new npc_enchantment();
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement