Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. local PlayerLoot = {};
  2. math.randomseed(os.time()); -- nice random numbers kgo
  3.  
  4. -- NewPlayerLootTable(player, player[, maxdrops])
  5. -- Returns a PlayerLootTable object.
  6. -- Parameters
  7. --  * pKilledGUID (userdata)        - the player who was killed.
  8. --  * pKillerGUID (userdata)        - the player who was the killer.
  9. --  * maxdrops (int)        - the maximum number of drops at any one time (if not specified, 2).
  10. function NewPlayerLootTable(pKilledGUID, pKillerGUID, maxdrops)
  11.     local o = {
  12.         m_looter = pKillerGUID,
  13.         m_owner = pKilledGUID,
  14.         m_maxdrops = maxdrops or 2,
  15.         loot = {}
  16.     }
  17.     setmetatable(o, {__index = PlayerLoot});
  18.     return o;
  19. end
  20.  
  21.  
  22. -- PlayerLoot:AddItem(...)
  23. -- Parameters for loot objects.
  24. --  * m_itemId              - the ID of the item to drop.
  25. --  * m_maxQuantity         - the Max Quantity of the item that can drop at any one time.
  26. --  * m_dropChance          - the chance for the item to drop. 1 = 100% chance.
  27. --  * 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.
  28. --  * m_classReq            - the player's class required for this item to drop.
  29. --  * m_raceReq             - the player's race required for this item to drop.
  30. --  * m_minLevel            - the minimum level of the player for this to drop.
  31. --  * m_maxLevel            - the maximum level of the player for this to drop.
  32. --  * m_killedClassReq      - the killed class that is required for this item to drop.
  33. --  * m_killedRaceReq       - the killed race that is required for this item to drop.
  34. --  * m_killedMinLevel      - the minimum level of the player killed for this to drop.
  35. --  * m_killedMaxLevel      - the maximum level of the player killed for this to drop.
  36. -- Returns 0 on successful add, 1 on unsuccessful add.
  37. function PlayerLoot:AddItem(...)
  38.     -- Create a table full of default values
  39.     local o = {
  40.         m_itemId = 0,
  41.         m_maxQuantity = 1,
  42.         m_dropChance = 0,
  43.         m_rewardLvlVarience = 5,
  44.         m_classReq = -1,
  45.         m_raceReq = -1,
  46.         m_minLevel = 0,
  47.         m_maxLevel = 60,
  48.         m_killedClassReq = -1,
  49.         m_killedRaceReq = -1,
  50.         m_killedMinLevel = 0,
  51.         m_killedMaxLevel = 60  
  52.     };
  53.     for k, v in pairs(arg) do
  54.         o[k] = v;
  55.     end
  56.     if tostring(self) ~= tostring(PlayerLoot) then
  57.         table.insert(self.loot, o);
  58.         return 0;
  59.     else
  60.         return 1;
  61.     end
  62. end
  63.  
  64.  
  65. -- PlayerLoot:GenerateDrops()
  66. -- Returns a table full of the items that should drop.
  67. function PlayerLoot:GenerateDrops()
  68.     if not self.loot then return 1; end
  69.     local phatloots = {};
  70.     local pOwner, pLooter = self.m_owner, self.m_looter;
  71.    
  72.     -- Check to make sure we exist
  73.     if not pLooter:IsInWorld() then return 1; end
  74.    
  75.     -- Create a table for "potential loot". This is just a table of self.loot but this one we remove from.
  76.     local potentialLoot = {};
  77.     for k, v in ipairs(self.loot) do potentialLoot[k] = v; end
  78.    
  79.     -- Requirement testing
  80.     -- If Player matches req (or req = -1) and Player2 matches req2 (or req = -2) then return true
  81.     local function MatchClassReq(Player,req,Player2,req2)
  82.         if req ~= -1 then
  83.             if Player:GetPlayerClass() ~= req then
  84.                 return false;
  85.             end
  86.         end
  87.         if req2 ~= -1 then
  88.             if Player2:GetPlayerClass() ~= req2 then
  89.                 return false;
  90.             end
  91.         end
  92.         return true;
  93.     end
  94.     local function MatchRaceReq(Player,req,Player2,req2)
  95.         if req ~= -1 then
  96.             if Player:GetPlayerRace() ~= req then
  97.                 return false;
  98.             end
  99.         end
  100.         if req2 ~= -1 then
  101.             if Player2:GetPlayerRace() ~= req2 then
  102.                 return false;
  103.             end
  104.         end
  105.         return true;
  106.     end
  107.    
  108.     -- Might need to do this recursively..
  109.     -- Now for the actual math
  110.     for i = 1, #potentialLoot do
  111.         local r = math.random(1, #potentialLoot);
  112.        
  113.         -- I'm going to check for the race/class after selecting the item.. maybe we should do that before? idk.
  114.         local i = potentialLoot[r];
  115.        
  116.         -- Requirement testing
  117.         if MatchClassReq(pLooter, i.m_classReq, pOwner, i.m_killedClassReq) then
  118.             if MatchRaceReq(pLooter, i.m_raceReq, pOwner, i.m_killedRaceReq) then
  119.                 -- Passed race/class checks
  120.                 if pLooter:GetPlayerLevel() >= i.m_minLevel and pLooter:GetPlayerLevel() <= i.m_maxLevel then
  121.                     if pOwner:GetPlayerLevel() >= i.m_killedMinLevel and pOwner:i.m_killedMaxLevel then
  122.                         if pOwner:GetPlayerLevel() + i.m_rewardLvlVariance >= pLooter:GetPlayerLevel() then
  123.                             if pOwner:GetPlayerLevel() - i.m_rewardLvlVariance <= pLooter:GetPlayerLevel() then
  124.                                 -- passed ALL those checks LOL
  125.                                 local chance = math.random();
  126.                                 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
  127.                                     local q = i.m_maxQuantity ~= 1 and math.random(1, i.m_maxQuantity) or 1;
  128.                                     table.insert(phatloots, {["item"] = i, ["quantity"] = q});
  129.                                     table.remove(potentialLoot, i);
  130.                                 end
  131.                             end
  132.                         end
  133.                     end
  134.                 end
  135.             end
  136.         end
  137.        
  138.         -- finally, check if we've generated enough rewards
  139.         if #phatloots >= self.m_maxDrops then
  140.             break;
  141.         end
  142.     end
  143.    
  144.     return phatloots;
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement