Advertisement
Erictemponi

NPC Professions 2

Dec 16th, 2017
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.56 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2. #include "Language.h"
  3. #include "World.h"
  4. #include "WorldSession.h"
  5. #include "ScriptedGossip.h"
  6. #include "ScriptedCreature.h"
  7. #include "Player.h"
  8. #include "Log.h"
  9. #include "SpellMgr.h"
  10. #include "Chat.h"
  11. #include "WorldPacket.h"
  12. #include "DBCStores.h"
  13.  
  14. class Professions_NPC : public CreatureScript
  15. {
  16. public:
  17.     Professions_NPC() : CreatureScript("Professions_NPC") {}
  18.  
  19.     void CreatureWhisperBasedOnBool(const char *text, Creature *_creature, Player *pPlayer, bool value)
  20.     {
  21.         if (value)
  22.             _creature->TextEmote(text, pPlayer);
  23.     }
  24.  
  25.     uint32 PlayerMaxLevel() const
  26.     {
  27.         return sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL);
  28.     }
  29.  
  30.     bool PlayerHasItemOrSpell(const Player *plr, uint32 itemId, uint32 spellId) const
  31.     {
  32.         return plr->HasItemCount(itemId, 1, true) || plr->HasSpell(spellId);
  33.     }
  34.  
  35.     static bool OnGossipHello(Player *pPlayer, Creature* _creature)
  36.     {
  37.         AddGossipItemFor(pPlayer, 9, "[Professions] ->", GOSSIP_SENDER_MAIN, 196);
  38.         pPlayer->PlayerTalkClass->SendGossipMenu(907, _creature->GetGUID());
  39.         return true;
  40.     }
  41.  
  42.     static bool PlayerAlreadyHasTwoProfessions(const Player *pPlayer)
  43.     {
  44.         uint32 skillCount = 0;
  45.  
  46.         if (pPlayer->HasSkill(SKILL_MINING))
  47.             skillCount++;
  48.         if (pPlayer->HasSkill(SKILL_SKINNING))
  49.             skillCount++;
  50.         if (pPlayer->HasSkill(SKILL_HERBALISM))
  51.             skillCount++;
  52.  
  53.         if (skillCount >= 2)
  54.             return true;
  55.  
  56.         for (uint32 i = 1; i < sSkillLineStore.GetNumRows(); ++i)
  57.         {
  58.             SkillLineEntry const *SkillInfo = sSkillLineStore.LookupEntry(i);
  59.             if (!SkillInfo)
  60.                 continue;
  61.  
  62.             if (SkillInfo->categoryId == SKILL_CATEGORY_SECONDARY)
  63.                 continue;
  64.  
  65.             if ((SkillInfo->categoryId != SKILL_CATEGORY_PROFESSION) || !SkillInfo->canLink)
  66.                 continue;
  67.  
  68.             const uint32 skillID = SkillInfo->id;
  69.             if (pPlayer->HasSkill(skillID))
  70.                 skillCount++;
  71.  
  72.             if (skillCount >= 2)
  73.                 return true;
  74.         }
  75.         return false;
  76.     }
  77.  
  78.     static bool LearnAllRecipesInProfession(Player *pPlayer, SkillType skill)
  79.     {
  80.         ChatHandler handler(pPlayer->GetSession());
  81.         char* skill_name;
  82.  
  83.         SkillLineEntry const *SkillInfo = sSkillLineStore.LookupEntry(skill);
  84.         skill_name = SkillInfo->name[handler.GetSessionDbcLocale()];
  85.  
  86.         if (!SkillInfo)
  87.         {
  88.             TC_LOG_ERROR("server.loading", "Profession NPC: received non-valid skill ID (LearnAllRecipesInProfession)");
  89.         }
  90.  
  91.         LearnSkillRecipesHelper(pPlayer, SkillInfo->id);
  92.  
  93.         pPlayer->SetSkill(SkillInfo->id, pPlayer->GetSkillStep(SkillInfo->id), 450, 450);
  94.         handler.PSendSysMessage(LANG_COMMAND_LEARN_ALL_RECIPES, skill_name);
  95.  
  96.         return true;
  97.     }
  98.  
  99.     static void LearnSkillRecipesHelper(Player *player, uint32 skill_id)
  100.     {
  101.         uint32 classmask = player->getClassMask();
  102.  
  103.         for (uint32 j = 0; j < sSkillLineAbilityStore.GetNumRows(); ++j)
  104.         {
  105.             SkillLineAbilityEntry const *skillLine = sSkillLineAbilityStore.LookupEntry(j);
  106.             if (!skillLine)
  107.                 continue;
  108.  
  109.             // wrong skill
  110.             if (skillLine->skillId != skill_id)
  111.                 continue;
  112.  
  113.             // not high rank
  114.             if (skillLine->forward_spellid)
  115.                 continue;
  116.  
  117.             // skip racial skills
  118.             if (skillLine->racemask != 0)
  119.                 continue;
  120.  
  121.             // skip wrong class skills
  122.             if (skillLine->classmask && (skillLine->classmask & classmask) == 0)
  123.                 continue;
  124.  
  125.             SpellInfo const * spellInfo = sSpellMgr->GetSpellInfo(skillLine->spellId);
  126.             if (!spellInfo || !SpellMgr::IsSpellValid(spellInfo, player, false))
  127.                 continue;
  128.  
  129.             player->LearnSpell(skillLine->spellId, false);
  130.         }
  131.     }
  132.  
  133.     static bool IsSecondarySkill(SkillType skill)
  134.     {
  135.         return skill == SKILL_COOKING || skill == SKILL_FIRST_AID;
  136.     }
  137.  
  138.     static void CompleteLearnProfession(Player *pPlayer, Creature *pCreature, SkillType skill)
  139.     {
  140.         if (PlayerAlreadyHasTwoProfessions(pPlayer) && !IsSecondarySkill(skill))
  141.             pCreature->TextEmote("You already know two professions!", pPlayer);
  142.         else
  143.         {
  144.             if (!LearnAllRecipesInProfession(pPlayer, skill))
  145.                 pCreature->TextEmote("Internal error occured!", pPlayer);
  146.         }
  147.     }
  148.  
  149.     static bool OnGossipSelect(Player* pPlayer, Creature* _creature, uint32 uiSender, uint32 uiAction)
  150.     {
  151.         pPlayer->PlayerTalkClass->ClearMenus();
  152.  
  153.         if (uiSender == GOSSIP_SENDER_MAIN)
  154.         {
  155.  
  156.             switch (uiAction)
  157.             {
  158.             case 196:
  159.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\trade_alchemy:30|t Alchemy.", GOSSIP_SENDER_MAIN, 1);
  160.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\INV_Ingot_05:30|t Blacksmithing.", GOSSIP_SENDER_MAIN, 2);
  161.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\INV_Misc_LeatherScrap_02:30|t Leatherworking.", GOSSIP_SENDER_MAIN, 3);
  162.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\INV_Fabric_Felcloth_Ebon:30|t Tailoring.", GOSSIP_SENDER_MAIN, 4);
  163.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\inv_misc_wrench_01:30|t Engineering.", GOSSIP_SENDER_MAIN, 5);
  164.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\trade_engraving:30|t Enchanting.", GOSSIP_SENDER_MAIN, 6);
  165.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\inv_misc_gem_01:30|t Jewelcrafting.", GOSSIP_SENDER_MAIN, 7);
  166.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\INV_Scroll_08:30|t Inscription.", GOSSIP_SENDER_MAIN, 8);
  167.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\INV_Misc_Herb_07:30|t Herbalism.", GOSSIP_SENDER_MAIN, 9);
  168.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\inv_misc_pelt_wolf_01:30|t Skinning.", GOSSIP_SENDER_MAIN, 10);
  169.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_INTERACT_2, "|TInterface\\icons\\trade_mining:30|t Mining.", GOSSIP_SENDER_MAIN, 11);
  170.                 AddGossipItemFor(pPlayer, GOSSIP_ICON_TALK, "|TInterface/ICONS/Thrown_1H_Harpoon_D_01Blue:30|t Nevermind!", GOSSIP_SENDER_MAIN, 12);
  171.                 pPlayer->PlayerTalkClass->SendGossipMenu(1, _creature->GetGUID());
  172.                 break;
  173.             case 1:
  174.                 if (pPlayer->HasSkill(SKILL_ALCHEMY))
  175.                 {
  176.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  177.                     break;
  178.                 }
  179.  
  180.                 CompleteLearnProfession(pPlayer, _creature, SKILL_ALCHEMY);
  181.  
  182.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  183.                 break;
  184.             case 2:
  185.                 if (pPlayer->HasSkill(SKILL_BLACKSMITHING))
  186.                 {
  187.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  188.                     break;
  189.                 }
  190.                 CompleteLearnProfession(pPlayer, _creature, SKILL_BLACKSMITHING);
  191.  
  192.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  193.                 break;
  194.             case 3:
  195.                 if (pPlayer->HasSkill(SKILL_LEATHERWORKING))
  196.                 {
  197.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  198.                     break;
  199.                 }
  200.                 CompleteLearnProfession(pPlayer, _creature, SKILL_LEATHERWORKING);
  201.  
  202.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  203.                 break;
  204.             case 4:
  205.                 if (pPlayer->HasSkill(SKILL_TAILORING))
  206.                 {
  207.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  208.                     break;
  209.                 }
  210.                 CompleteLearnProfession(pPlayer, _creature, SKILL_TAILORING);
  211.  
  212.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  213.                 break;
  214.             case 5:
  215.                 if (pPlayer->HasSkill(SKILL_ENGINEERING))
  216.                 {
  217.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  218.                     break;
  219.                 }
  220.                 CompleteLearnProfession(pPlayer, _creature, SKILL_ENGINEERING);
  221.  
  222.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  223.                 break;
  224.             case 6:
  225.                 if (pPlayer->HasSkill(SKILL_ENCHANTING))
  226.                 {
  227.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  228.                     break;
  229.                 }
  230.                 CompleteLearnProfession(pPlayer, _creature, SKILL_ENCHANTING);
  231.  
  232.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  233.                 break;
  234.             case 7:
  235.                 if (pPlayer->HasSkill(SKILL_JEWELCRAFTING))
  236.                 {
  237.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  238.                     break;
  239.                 }
  240.                 CompleteLearnProfession(pPlayer, _creature, SKILL_JEWELCRAFTING);
  241.  
  242.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  243.                 break;
  244.             case 8:
  245.                 if (pPlayer->HasSkill(SKILL_INSCRIPTION))
  246.                 {
  247.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  248.                     break;
  249.                 }
  250.                 CompleteLearnProfession(pPlayer, _creature, SKILL_INSCRIPTION);
  251.  
  252.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  253.                 break;
  254.             case 9:
  255.                 if (pPlayer->HasSkill(SKILL_HERBALISM))
  256.                 {
  257.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  258.                     break;
  259.                 }
  260.  
  261.                 CompleteLearnProfession(pPlayer, _creature, SKILL_HERBALISM);
  262.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  263.                 break;
  264.             case 10:
  265.                 if (pPlayer->HasSkill(SKILL_SKINNING))
  266.                 {
  267.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  268.                     break;
  269.                 }
  270.  
  271.                 CompleteLearnProfession(pPlayer, _creature, SKILL_SKINNING);
  272.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  273.                 break;
  274.             case 11:
  275.                 if (pPlayer->HasSkill(SKILL_MINING))
  276.                 {
  277.                     pPlayer->PlayerTalkClass->SendCloseGossip();
  278.                     break;
  279.                 }
  280.  
  281.                 CompleteLearnProfession(pPlayer, _creature, SKILL_MINING);
  282.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  283.                 break;
  284.             case 12:
  285.                 pPlayer->PlayerTalkClass->SendCloseGossip();
  286.                 break;
  287.             }
  288.  
  289.  
  290.         }
  291.         return true;
  292.     }
  293.     struct MyAI : public ScriptedAI
  294.     {
  295.         MyAI(Creature* creature) : ScriptedAI(creature) { }
  296.         bool GossipHello(Player* player) override
  297.         {
  298.             return OnGossipHello(player, me);
  299.         }
  300.         bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override
  301.         {
  302.             uint32 sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId);
  303.             uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
  304.             return OnGossipSelect(player, me, sender, action);
  305.         }
  306.     };
  307.  
  308.     CreatureAI* GetAI(Creature* creature) const override
  309.     {
  310.         return new MyAI(creature);
  311.     }
  312. };
  313.  
  314. void AddSC_Professions_NPC()
  315. {
  316.     new Professions_NPC();
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement