Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. local PlayerLoot = {};
  2. local PrepreparedLootTables = {};
  3. math.randomseed(os.time()); -- nice random numbers kgo
  4.  
  5. -- NewPlayerLootTable([maxdrops, pKilledGUID, pKillerGUID])
  6. -- Returns a PlayerLootTable object.
  7. -- Parameters
  8. --  * maxdrops (int)        - the maximum number of drops at any one time (if not specified, 2).
  9. --  * pKilledGUID (userdata)        - the player who was killed.
  10. --  * pKillerGUID (userdata)        - the player who was the killer.
  11. function NewPlayerLootTable(maxdrops, pKilledGUID, pKillerGUID)
  12.     local o = {
  13.         m_looter = pKillerGUID or 0,
  14.         m_owner = pKilledGUID or 0,
  15.         m_maxdrops = maxdrops or 2,
  16.         loot = {}
  17.     }
  18.     setmetatable(o, {__index = PlayerLoot});
  19.     return o;
  20. end
  21.  
  22. function PlayerLoot:SetLootedPlayer(pPlayer)
  23.     self.m_owner = pPlayer;
  24. end
  25. function PlayerLoot:SetLooter(pPlayer)
  26.     self.m_looter = pPlayer;
  27. end
  28.  
  29. -- PlayerLoot:AddItem(...)
  30. -- Parameters for loot objects.
  31. --  * m_itemId              - the ID of the item to drop.
  32. --  * m_maxQuantity         - the Max Quantity of the item that can drop at any one time.
  33. --  * m_dropChance          - the chance for the item to drop. 1 = 100% chance.
  34. --  * m_rewardLvlVarience   - if the player that killed this person is not within the level range of the player killed then they will be ineligible for rewards.
  35. --  * m_classReq            - the player's class required for this item to drop.
  36. --  * m_raceReq             - the player's race required for this item to drop.
  37. --  * m_minLevel            - the minimum level of the player for this to drop.
  38. --  * m_maxLevel            - the maximum level of the player for this to drop.
  39. --  * m_killedClassReq      - the killed class that is required for this item to drop.
  40. --  * m_killedRaceReq       - the killed race that is required for this item to drop.
  41. --  * m_killedMinLevel      - the minimum level of the player killed for this to drop.
  42. --  * m_killedMaxLevel      - the maximum level of the player killed for this to drop.
  43. -- Returns 0 on successful add, 1 on unsuccessful add.
  44. function PlayerLoot:AddItem(...)
  45.     -- Create a table full of default values
  46.     local o = {
  47.         m_itemId = 0,
  48.         m_maxQuantity = 1,
  49.         m_dropChance = 0,
  50.         m_rewardLvlVarience = 5,
  51.         m_classReq = -1,
  52.         m_raceReq = -1,
  53.         m_minLevel = 0,
  54.         m_maxLevel = 60,
  55.         m_killedClassReq = -1,
  56.         m_killedRaceReq = -1,
  57.         m_killedMinLevel = 0,
  58.         m_killedMaxLevel = 60  
  59.     };
  60.     for k, v in pairs(arg) do
  61.         o[k] = v;
  62.     end
  63.     if tostring(self) ~= tostring(PlayerLoot) then
  64.         table.insert(self.loot, o);
  65.         return 0;
  66.     else
  67.         return 1;
  68.     end
  69. end
  70.  
  71.  
  72. -- PlayerLoot:GenerateDrops()
  73. -- Returns a table full of the items that should drop.
  74. function PlayerLoot:GenerateDrops()
  75.     if not self.loot then return 1; end
  76.     local phatloots = {};
  77.     local pOwner, pLooter = self.m_owner, self.m_looter;
  78.    
  79.     -- Check to make sure we exist
  80.     if not pLooter:IsInWorld() then return 1; end
  81.    
  82.     -- Create a table for "potential loot". This is just a table of self.loot but this one we remove from.
  83.     local potentialLoot = {};
  84.     for k, v in ipairs(self.loot) do potentialLoot[k] = v; end
  85.    
  86.     -- Requirement testing
  87.     -- If Player matches req (or req = -1) and Player2 matches req2 (or req = -2) then return true
  88.     local function MatchClassReq(Player,req,Player2,req2)
  89.         if req ~= -1 then
  90.             if Player:GetPlayerClass() ~= req then
  91.                 return false;
  92.             end
  93.         end
  94.         if req2 ~= -1 then
  95.             if Player2:GetPlayerClass() ~= req2 then
  96.                 return false;
  97.             end
  98.         end
  99.         return true;
  100.     end
  101.     local function MatchRaceReq(Player,req,Player2,req2)
  102.         if req ~= -1 then
  103.             if Player:GetPlayerRace() ~= req then
  104.                 return false;
  105.             end
  106.         end
  107.         if req2 ~= -1 then
  108.             if Player2:GetPlayerRace() ~= req2 then
  109.                 return false;
  110.             end
  111.         end
  112.         return true;
  113.     end
  114.    
  115.     -- Might need to do this recursively..
  116.     -- Now for the actual math
  117.     for n = 1, #potentialLoot do
  118.         local r = math.random(1, #potentialLoot);
  119.        
  120.         -- I'm going to check for the race/class after selecting the item.. maybe we should do that before? idk.
  121.         local i = potentialLoot[r];
  122.        
  123.         -- Requirement testing
  124.         if MatchClassReq(pLooter, i.m_classReq, pOwner, i.m_killedClassReq) then
  125.             if MatchRaceReq(pLooter, i.m_raceReq, pOwner, i.m_killedRaceReq) then
  126.                 -- Passed race/class checks
  127.                 if pLooter:GetPlayerLevel() >= i.m_minLevel and pLooter:GetPlayerLevel() <= i.m_maxLevel then
  128.                     if pOwner:GetPlayerLevel() >= i.m_killedMinLevel and pOwner:GetPlayerLevel() <= i.m_killedMaxLevel then
  129.                         if pOwner:GetPlayerLevel() + i.m_rewardLvlVariance >= pLooter:GetPlayerLevel() then
  130.                             if pOwner:GetPlayerLevel() - i.m_rewardLvlVariance <= pLooter:GetPlayerLevel() then
  131.                                 -- passed ALL those checks LOL
  132.                                 local chance = math.random();
  133.                                 if i.m_dropChance <= chance then -- if we get 0.3 and the chance is 0.3 or 0.2 or 0.1 then drop it
  134.                                     local q = i.m_maxQuantity ~= 1 and math.random(1, i.m_maxQuantity) or 1;
  135.                                     table.insert(phatloots, {["item"] = i, ["quantity"] = q});
  136.                                     table.remove(potentialLoot, n);
  137.                                 end
  138.                             end
  139.                         end
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.        
  145.         -- finally, check if we've generated enough rewards
  146.         if #phatloots >= self.m_maxDrops then
  147.             break;
  148.         end
  149.     end
  150.    
  151.     return phatloots;
  152. end
  153.  
  154. -- PlayerLoot:SendLootPrompt(loottable)
  155. -- Pass the return from PlayerLoot:GenerateDrops()!
  156. function PlayerLoot:SendLootPrompt(loottable)
  157.     for k, v in pairs(loottable) do
  158.         self.m_looter:AddLoot(v.item.m_entryId, v.quantity, v.quantity, false);
  159.     end
  160.     self.m_looter:SendLootWindow(self.m_looter:GetGUID(), 1);
  161. end
  162.  
  163.  
  164. --[[
  165. ------------------------
  166. -- USAGE
  167. ------------------------
  168. -- Creating a loot table
  169. local t = NewPlayerLootTable();
  170. -- Add Items to it
  171. t:AddItem{
  172.     m_entryId = 100,
  173.     m_maxQuantity = 2
  174. };
  175. -- Now you can either put this in a table for later use or use it right away.
  176.  
  177. -- Using the loot table
  178. local loottable = ... (in a table or use one you just created.)
  179. loottable:SetLootedPlayer(pKilled);
  180. loottable:SetLooter(pKiller);
  181. local preparedTable = loottable:GenerateLoot();
  182. if preparedTable and #preparedTable>0 then
  183.     loottable:SendLootPrompt(preparedTable);
  184. end
  185.  
  186. ]]
  187.  
  188. -- Announce we're good.
  189. do
  190.     logcol(1+8);
  191.     print("Loaded Player Loot system successfully.");
  192.     logcol(7);
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement