Advertisement
Rochet2

Automatic gossip menu creation - recommended

Dec 15th, 2011
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | None | 0 0
  1. -- The Gossip NPC's entry and the amount of options on the screen.
  2. local NPC_Entry = 180002
  3. local Options = 12 -- Please do not use values over 29 or the gossip menu might have too many options (29 + 3 = 32 options. 31 is max.)
  4. local NoRefresh = true -- Incase you want to update the T.Data table on Gossip Hello, change NoRefresh to false.
  5.  
  6.  
  7. -- initialize the main table and the subtables.
  8. local T =
  9. {
  10. Page = {},
  11. }
  12.  
  13. -- This defines how many intids are left untouched. 5 would mean intid's: 0,1,2,3,4, not 1,2,3,4,5.
  14. -- Defining this for later addtition / reduction of additional functions, like Jump to page etc.
  15. local Space = 4
  16.  
  17. -- This function loads the data from the database by default. You can mold it, but it should populate the T.Data table with the amount of options at least.
  18. -- Currently this function has an example script, that will load the name and entry of items from the items table. (randomized amount of them for testing purposes)
  19. function T.LoadData(pPlayer)
  20.     local Table
  21.     if(not pPlayer) then
  22.         T.Data = {}
  23.         Table = T.Data
  24.     else
  25.         T.Data[tostring(pPlayer)] = {}
  26.         Table = T.Data[tostring(pPlayer)]
  27.     end
  28.     local Query = WorldDBQuery("SELECT entry, name1 FROM items LIMIT "..math.random(100)..";")
  29.     if(Query ~= nil) then
  30.         for i = Space, Query:GetRowCount()+Space-1 do
  31.             Table[i] = {Query:GetColumn(0):GetLong(), Query:GetColumn(1):GetString()}
  32.             Query:NextRow()
  33.         end
  34.         local Amount
  35.         local R, D = #Table%Options, #Table/Options
  36.         if(R == 0) then
  37.             Amount = D - 1
  38.         elseif(R <= 5) then
  39.             Amount = math.floor(D)
  40.         else
  41.             Amount = math.ceil(D) - 1
  42.         end
  43.         Table.Amount = Amount
  44.     end
  45. end
  46. if(NoRefresh) then
  47.     T.LoadData()
  48. end
  49.  
  50. function T.GetMenu(pUnit, e, pPlayer, Page)
  51.     local Table
  52.     if(NoRefresh) then
  53.         Table = T.Data
  54.     else
  55.         if(e ~= nil) then
  56.             T.LoadData(pPlayer)
  57.         end
  58.         Table = T.Data[tostring(pPlayer)]
  59.     end
  60.     if(Page == nil or Page < 0) then -- On hello and faulty page, set to main menu.
  61.         T.Page[tostring(pPlayer)] = 0
  62.         Page = 0
  63.     end
  64.     pUnit:GossipCreateMenu(100, pPlayer, 0)
  65.     local Rows, Count = #Table, T.Count(Page)
  66.     local Max, Next = T.Max(Count, Rows)
  67.    
  68.     -- These 3 options can also be moved to the botton of the list, just put them under the for loop (above GossipSendMenu)
  69.     -- I left them to the top for an easy access and use. If the option texts are long (like some item names are),
  70.     -- the buttons will jump up and down if they are at the bottom.
  71.     if(Next) then
  72.         pUnit:GossipMenuAddItem(7, "Next page", 1, 0, '', 0)
  73.     end
  74.     if(Page > 0) then
  75.         pUnit:GossipMenuAddItem(7, "Previous page", 2, 0, '', 0)
  76.     end
  77.     if(Rows > Options) then
  78.         pUnit:GossipMenuAddItem(2, "Jump to page", 3, 1, 'Jump to a page 1-'..Table.Amount, 0)
  79.     end
  80.     for i = Count, Max do
  81.         -- This loop creates our gossip options. Currently it takes the name of the option from T.Data column 2 (item name)
  82.         pUnit:GossipMenuAddItem(4, Table[i][2], i, 0, '', 0)
  83.     end
  84.     pUnit:GossipSendMenu(pPlayer)
  85. end
  86.  
  87. function T.Count(Page)
  88.     if(Page == 0) then
  89.         return Space
  90.     else
  91.         return Space+(Page*Options)
  92.     end
  93. end
  94.  
  95. function T.Max(Count, LData)
  96.     if(LData - Count >= Options) then
  97.         return Count+Options-1, true
  98.     else
  99.         return LData, false
  100.     end
  101. end
  102.  
  103. function T.Select(pUnit, event, pPlayer, id, intid, code)
  104.     local str = tostring(pPlayer)
  105.     local Table
  106.     if(NoRefresh) then
  107.         Table = T.Data
  108.     else
  109.         Table = T.Data[tostring(pPlayer)]
  110.     end
  111.     if(intid < Space) then
  112.         -- This if statement contains the extra functions
  113.         if(intid == 1) then -- Next page
  114.             T.Page[str] = T.Page[str] + 1
  115.         elseif(intid == 2) then -- Previous page
  116.             T.Page[str] = T.Page[str] - 1
  117.         elseif(intid == 3) then -- Jump to page
  118.             local code = math.ceil(tonumber(code))
  119.             if(code ~= nil and code >= 1 and code <= Table.Amount) then -- Check that the "code" (page) is suitable
  120.                 T.Page[str] = code-1
  121.             end
  122.         end
  123.     else
  124.         -- Place your script here, which will execute when a player clicks a normal option.
  125.        
  126.         -- example:
  127.         -- Add item with the entry in T.Data table (column 1) based on the option we click
  128.         pPlayer:AddItem(Table[intid][1], 1)
  129.        
  130.     end
  131.     T.GetMenu(pUnit, nil, pPlayer, T.Page[str])
  132. end
  133.  
  134. RegisterUnitGossipEvent(NPC_Entry, 1, T.GetMenu)
  135. RegisterUnitGossipEvent(NPC_Entry, 2, T.Select)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement