Advertisement
Rochet2

PreviewVendor

Oct 19th, 2013
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. // Scriptname to put into creature_template: PreviewVendor
  2. // dont forget to add vendor flags to the NPC (128)
  3.  
  4. #include "ScriptPCH.h"
  5.  
  6. static UNORDERED_MAP<uint32, std::vector<uint32> > itemList; // holds items from DB after startup
  7.  
  8. class PreviewLoader : public WorldScript // script that loads items from DB so you can customize the vendors without recompile and restart
  9. {
  10. public:
  11.     PreviewLoader() : WorldScript("PreviewLoader") { }
  12.  
  13.     void OnStartup()
  14.     {
  15.         itemList.clear(); // reload
  16.         QueryResult result = WorldDatabase.Query("SELECT entry, item FROM npc_vendor_preview");
  17.         if (!result)
  18.             return;
  19.         do
  20.         {
  21.             uint32 entry = (*result)[0].GetUInt32();
  22.             uint32 item = (*result)[1].GetUInt32();
  23.             if (sObjectMgr->GetItemTemplate(item))
  24.                 itemList[entry].push_back(item);
  25.         } while (result->NextRow());
  26.     }
  27.  
  28.     // you can reload config to reload the items.
  29.     // Too lazy to make a command since all the RBAC stuff changes all the time now and not sure how you have it
  30.     void OnConfigLoad(bool reload)
  31.     {
  32.         if (reload)
  33.             OnStartup();
  34.     }
  35. };
  36.  
  37.  
  38. class PreviewVendor : public CreatureScript
  39. {
  40. public:
  41.     PreviewVendor() : CreatureScript("PreviewVendor") { }
  42.  
  43.     bool OnGossipHello(Player* player, Creature* creature)
  44.     {
  45.         CustomSendListInventory(player, creature->GetGUID());
  46.         return true; // stop normal actions
  47.     }
  48.  
  49.     void CustomSendListInventory(Player* player, uint64 vendorGuid)
  50.     {
  51.         TC_LOG_DEBUG(LOG_FILTER_NETWORKIO, "WORLD: Sent custom SMSG_LIST_INVENTORY");
  52.  
  53.         WorldSession* session = player->GetSession();
  54.  
  55.         Creature* vendor = player->GetNPCIfCanInteractWith(vendorGuid, UNIT_NPC_FLAG_VENDOR);
  56.         if (!vendor)
  57.         {
  58.             TC_LOG_DEBUG(LOG_FILTER_NETWORKIO, "WORLD: SendListInventory - Unit (GUID: %u) not found or you can not interact with him.", uint32(GUID_LOPART(vendorGuid)));
  59.             player->SendSellError(SELL_ERR_CANT_FIND_VENDOR, NULL, 0);
  60.             return;
  61.         }
  62.  
  63.         // remove fake death
  64.         if (player->HasUnitState(UNIT_STATE_DIED))
  65.             player->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
  66.  
  67.         // Stop the npc if moving
  68.         if (vendor->HasUnitState(UNIT_STATE_MOVING))
  69.             vendor->StopMoving();
  70.  
  71.         // VendorItemData const* items = vendor->GetVendorItems();
  72.         if (itemList.find(vendor->GetEntry()) == itemList.end())
  73.         {
  74.             WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + 1);
  75.             data << uint64(vendorGuid);
  76.             data << uint8(0);                                   // count == 0, next will be error code
  77.             data << uint8(0);                                   // "Vendor has no inventory"
  78.             session->SendPacket(&data);
  79.             return;
  80.         }
  81.  
  82.         std::vector<uint32> items = itemList[vendor->GetEntry()];
  83.         uint8 itemCount = items.size();
  84.         uint8 count = 0;
  85.  
  86.         WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + itemCount * 8 * 4);
  87.         data << uint64(vendorGuid);
  88.  
  89.         size_t countPos = data.wpos();
  90.         data << uint8(count);
  91.  
  92.         // float discountMod = player->GetReputationPriceDiscount(vendor);
  93.  
  94.         for (uint8 slot = 0; slot < itemCount; ++slot)
  95.         {
  96.             if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(items[slot]))
  97.             {
  98.                 if (!(itemTemplate->AllowableClass & player->getClassMask()) && itemTemplate->Bonding == BIND_WHEN_PICKED_UP && !player->IsGameMaster())
  99.                     continue;
  100.                 // Only display items in vendor lists for the team the
  101.                 // player is on. If GM on, display all items.
  102.                 if (!player->IsGameMaster() && ((itemTemplate->Flags2 & ITEM_FLAGS_EXTRA_HORDE_ONLY && player->GetTeam() == ALLIANCE) || (itemTemplate->Flags2 == ITEM_FLAGS_EXTRA_ALLIANCE_ONLY && player->GetTeam() == HORDE)))
  103.                     continue;
  104.  
  105.                 // Items sold out are not displayed in list
  106.                 // uint32 leftInStock = !item->maxcount ? 0xFFFFFFFF : vendor->GetVendorItemCurrentCount(item);
  107.                 // if (!player->IsGameMaster() && !leftInStock)
  108.                 //     continue;
  109.                 uint32 leftInStock = 0xFFFFFFFF; // show all items
  110.  
  111.                 ConditionList conditions = sConditionMgr->GetConditionsForNpcVendorEvent(vendor->GetEntry(), items[slot]);
  112.                 if (!sConditionMgr->IsObjectMeetToConditions(player, vendor, conditions))
  113.                 {
  114.                     TC_LOG_DEBUG(LOG_FILTER_CONDITIONSYS, "SendListInventory: conditions not met for creature entry %u item %u", vendor->GetEntry(), items[slot]);
  115.                     continue;
  116.                 }
  117.  
  118.                 // reputation discount
  119.                 // int32 price = item->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->BuyPrice * discountMod)) : 0;
  120.                 int32 price = 0;
  121.  
  122.                 data << uint32(slot + 1);       // client expects counting to start at 1
  123.                 data << uint32(items[slot]);
  124.                 data << uint32(itemTemplate->DisplayInfoID);
  125.                 data << int32(leftInStock); // left in stock
  126.                 data << uint32(price); // price
  127.                 data << uint32(itemTemplate->MaxDurability);
  128.                 data << uint32(itemTemplate->BuyCount);
  129.                 data << uint32(0); // extended cost
  130.  
  131.                 if (++count >= MAX_VENDOR_ITEMS)
  132.                     break;
  133.             }
  134.         }
  135.  
  136.         if (count == 0)
  137.         {
  138.             data << uint8(0);
  139.             session->SendPacket(&data);
  140.             return;
  141.         }
  142.  
  143.         data.put<uint8>(countPos, count);
  144.         session->SendPacket(&data);
  145.     }
  146. };
  147.  
  148. void AddSC_PreviewVendor()
  149. {
  150.     new PreviewLoader;
  151.     new PreviewVendor;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement