Advertisement
Rochet2

Trinity Core Professions NPC

Oct 20th, 2012
1,430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. /************************************
  2. *        Made by Sdyess             *
  3. *                                   *
  4. *      Edit By jameyboor            *
  5. *   Release by Ghostcrawler336      *
  6. *      Rewritten by Rochet2         *
  7. *                                   *
  8. *************************************
  9. */
  10.  
  11. #include "ScriptPCH.h"
  12.  
  13. class Professions_NPC : public CreatureScript
  14. {
  15. public:
  16.     Professions_NPC () : CreatureScript("Professions_NPC") {}
  17.  
  18.     bool OnGossipHello(Player *pPlayer, Creature* _creature)
  19.     {
  20.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Alchemy", GOSSIP_SENDER_MAIN, SKILL_ALCHEMY);
  21.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Blacksmithing", GOSSIP_SENDER_MAIN, SKILL_BLACKSMITHING);
  22.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Leatherworking", GOSSIP_SENDER_MAIN, SKILL_LEATHERWORKING);
  23.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Tailoring", GOSSIP_SENDER_MAIN, SKILL_TAILORING);
  24.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Engineering", GOSSIP_SENDER_MAIN, SKILL_ENGINEERING);
  25.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Enchanting", GOSSIP_SENDER_MAIN, SKILL_ENCHANTING);
  26.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Jewelcrafting", GOSSIP_SENDER_MAIN, SKILL_JEWELCRAFTING);
  27.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Inscription", GOSSIP_SENDER_MAIN, SKILL_INSCRIPTION);
  28.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Cooking", GOSSIP_SENDER_MAIN, SKILL_COOKING);
  29.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "First Aid", GOSSIP_SENDER_MAIN, SKILL_FIRST_AID);
  30.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Herbalism", GOSSIP_SENDER_MAIN, SKILL_HERBALISM);
  31.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Skinning", GOSSIP_SENDER_MAIN, SKILL_SKINNING);
  32.         pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Mining", GOSSIP_SENDER_MAIN, SKILL_MINING);
  33.  
  34.         pPlayer->PlayerTalkClass->SendGossipMenu(907, _creature->GetGUID());
  35.         return true;
  36.     }
  37.  
  38.     bool OnGossipSelect(Player* pPlayer, Creature* _creature, uint32 uiSender, uint32 SKILL)
  39.     {
  40.         pPlayer->PlayerTalkClass->ClearMenus();
  41.  
  42.         if (uiSender == GOSSIP_SENDER_MAIN)
  43.         {
  44.             if(pPlayer->HasSkill(SKILL))
  45.                 pPlayer->GetSession()->SendNotification("You already have that skill");
  46.             else
  47.                 CompleteLearnProfession(pPlayer, (SkillType)SKILL);
  48.         }
  49.         OnGossipHello(pPlayer, _creature);
  50.         return true;
  51.     }
  52.  
  53.     bool IsSecondarySkill(SkillType skill) const
  54.     {
  55.         return skill == SKILL_COOKING || skill == SKILL_FIRST_AID;
  56.     }
  57.  
  58.     void CompleteLearnProfession(Player *pPlayer, SkillType skill)
  59.     {
  60.         if (PlayerAlreadyHasTwoProfessions(pPlayer) && !IsSecondarySkill(skill))
  61.             pPlayer->GetSession()->SendNotification("You already know two professions!");
  62.         else
  63.         {
  64.             if (!LearnAllRecipesInProfession(pPlayer, skill))
  65.                 pPlayer->GetSession()->SendNotification("Internal error occured!");
  66.         }
  67.     }
  68.  
  69.     bool LearnAllRecipesInProfession(Player* pPlayer, SkillType skill)
  70.     {
  71.         SkillLineEntry const *SkillInfo = sSkillLineStore.LookupEntry(skill);
  72.  
  73.         if (!SkillInfo)
  74.         {
  75.             sLog->outError(LOG_FILTER_PLAYER_SKILLS, "Profession NPC: received non-valid skill ID (LearnAllRecipesInProfession)");
  76.             return false;
  77.         }
  78.         const char* skill_name = SkillInfo->name[ChatHandler(pPlayer->GetSession()).GetSessionDbcLocale()];
  79.  
  80.         LearnSkillRecipesHelper(pPlayer, SkillInfo->id);
  81.  
  82.         pPlayer->SetSkill(SkillInfo->id, pPlayer->GetSkillStep(SkillInfo->id), 450, 450);
  83.         ChatHandler(pPlayer->GetSession()).PSendSysMessage(LANG_COMMAND_LEARN_ALL_RECIPES, skill_name);
  84.  
  85.         return true;
  86.     }
  87.  
  88.     bool PlayerAlreadyHasTwoProfessions(Player *pPlayer)
  89.     {
  90.         uint32 skillCount = 0;
  91.  
  92.         if (pPlayer->HasSkill(SKILL_MINING))
  93.             skillCount++;
  94.         if (pPlayer->HasSkill(SKILL_SKINNING))
  95.             skillCount++;
  96.         if (pPlayer->HasSkill(SKILL_HERBALISM))
  97.             skillCount++;
  98.  
  99.         if (skillCount >= 2)
  100.             return true;
  101.  
  102.         for (uint32 i = 1; i < sSkillLineStore.GetNumRows(); ++i)
  103.         {
  104.             SkillLineEntry const *SkillInfo = sSkillLineStore.LookupEntry(i);
  105.             if (!SkillInfo)
  106.                 continue;
  107.  
  108.             if (SkillInfo->categoryId == SKILL_CATEGORY_SECONDARY)
  109.                 continue;
  110.  
  111.             if ((SkillInfo->categoryId != SKILL_CATEGORY_PROFESSION) || !SkillInfo->canLink)
  112.                 continue;
  113.  
  114.             const uint32 skillID = SkillInfo->id;
  115.             if (pPlayer->HasSkill(skillID))
  116.                 skillCount++;
  117.  
  118.             if (skillCount >= 2)
  119.                 return true;
  120.         }
  121.         return false;
  122.     }
  123.  
  124.     void LearnSkillRecipesHelper(Player *player, uint32 skill_id)
  125.     {
  126.         uint32 classmask = player->getClassMask();
  127.  
  128.         for (uint32 j = 0; j < sSkillLineAbilityStore.GetNumRows(); ++j)
  129.         {
  130.             SkillLineAbilityEntry const *skillLine = sSkillLineAbilityStore.LookupEntry(j);
  131.             if (!skillLine)
  132.                 continue;
  133.  
  134.             // wrong skill
  135.             if (skillLine->skillId != skill_id)
  136.                 continue;
  137.  
  138.             // not high rank
  139.             if (skillLine->forward_spellid)
  140.                 continue;
  141.  
  142.             // skip racial skills
  143.             if (skillLine->racemask != 0)
  144.                 continue;
  145.  
  146.             // skip wrong class skills
  147.             if (skillLine->classmask && (skillLine->classmask & classmask) == 0)
  148.                 continue;
  149.  
  150.             SpellInfo const * spellInfo = sSpellMgr->GetSpellInfo(skillLine->spellId);
  151.             if (!spellInfo || !SpellMgr::IsSpellValid(spellInfo, player, false))
  152.                 continue;
  153.  
  154.             player->learnSpell(skillLine->spellId, false);
  155.         }
  156.     }
  157. };
  158.  
  159. void AddSC_Professions_NPC()
  160. {
  161.     new Professions_NPC();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement