Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PlayerLoot = {};
- local PrepreparedLootTables = {};
- math.randomseed(os.time()); -- nice random numbers kgo
- -- NewPlayerLootTable([maxdrops, pKilledGUID, pKillerGUID])
- -- Returns a PlayerLootTable object.
- -- Parameters
- -- * maxdrops (int) - the maximum number of drops at any one time (if not specified, 2).
- -- * pKilledGUID (userdata) - the player who was killed.
- -- * pKillerGUID (userdata) - the player who was the killer.
- function NewPlayerLootTable(maxdrops, pKilledGUID, pKillerGUID)
- local o = {
- m_looter = pKillerGUID or 0,
- m_owner = pKilledGUID or 0,
- m_maxdrops = maxdrops or 2,
- loot = {}
- }
- setmetatable(o, {__index = PlayerLoot});
- return o;
- end
- function PlayerLoot:SetLootedPlayer(pPlayer)
- self.m_owner = pPlayer;
- end
- function PlayerLoot:SetLooter(pPlayer)
- self.m_looter = pPlayer;
- end
- -- PlayerLoot:AddItem(...)
- -- Parameters for loot objects.
- -- * m_itemId - the ID of the item to drop.
- -- * m_maxQuantity - the Max Quantity of the item that can drop at any one time.
- -- * m_dropChance - the chance for the item to drop. 1 = 100% chance.
- -- * 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.
- -- * m_classReq - the player's class required for this item to drop.
- -- * m_raceReq - the player's race required for this item to drop.
- -- * m_minLevel - the minimum level of the player for this to drop.
- -- * m_maxLevel - the maximum level of the player for this to drop.
- -- * m_killedClassReq - the killed class that is required for this item to drop.
- -- * m_killedRaceReq - the killed race that is required for this item to drop.
- -- * m_killedMinLevel - the minimum level of the player killed for this to drop.
- -- * m_killedMaxLevel - the maximum level of the player killed for this to drop.
- -- Returns 0 on successful add, 1 on unsuccessful add.
- function PlayerLoot:AddItem(...)
- -- Create a table full of default values
- local o = {
- m_itemId = 0,
- m_maxQuantity = 1,
- m_dropChance = 0,
- m_rewardLvlVarience = 5,
- m_classReq = -1,
- m_raceReq = -1,
- m_minLevel = 0,
- m_maxLevel = 60,
- m_killedClassReq = -1,
- m_killedRaceReq = -1,
- m_killedMinLevel = 0,
- m_killedMaxLevel = 60
- };
- for k, v in pairs(arg) do
- o[k] = v;
- end
- if tostring(self) ~= tostring(PlayerLoot) then
- table.insert(self.loot, o);
- return 0;
- else
- return 1;
- end
- end
- -- PlayerLoot:GenerateDrops()
- -- Returns a table full of the items that should drop.
- function PlayerLoot:GenerateDrops()
- if not self.loot then return 1; end
- local phatloots = {};
- local pOwner, pLooter = self.m_owner, self.m_looter;
- -- Check to make sure we exist
- if not pLooter:IsInWorld() then return 1; end
- -- Create a table for "potential loot". This is just a table of self.loot but this one we remove from.
- local potentialLoot = {};
- for k, v in ipairs(self.loot) do potentialLoot[k] = v; end
- -- Requirement testing
- -- If Player matches req (or req = -1) and Player2 matches req2 (or req = -2) then return true
- local function MatchClassReq(Player,req,Player2,req2)
- if req ~= -1 then
- if Player:GetPlayerClass() ~= req then
- return false;
- end
- end
- if req2 ~= -1 then
- if Player2:GetPlayerClass() ~= req2 then
- return false;
- end
- end
- return true;
- end
- local function MatchRaceReq(Player,req,Player2,req2)
- if req ~= -1 then
- if Player:GetPlayerRace() ~= req then
- return false;
- end
- end
- if req2 ~= -1 then
- if Player2:GetPlayerRace() ~= req2 then
- return false;
- end
- end
- return true;
- end
- -- Might need to do this recursively..
- -- Now for the actual math
- for n = 1, #potentialLoot do
- local r = math.random(1, #potentialLoot);
- -- I'm going to check for the race/class after selecting the item.. maybe we should do that before? idk.
- local i = potentialLoot[r];
- -- Requirement testing
- if MatchClassReq(pLooter, i.m_classReq, pOwner, i.m_killedClassReq) then
- if MatchRaceReq(pLooter, i.m_raceReq, pOwner, i.m_killedRaceReq) then
- -- Passed race/class checks
- if pLooter:GetPlayerLevel() >= i.m_minLevel and pLooter:GetPlayerLevel() <= i.m_maxLevel then
- if pOwner:GetPlayerLevel() >= i.m_killedMinLevel and pOwner:GetPlayerLevel() <= i.m_killedMaxLevel then
- if pOwner:GetPlayerLevel() + i.m_rewardLvlVariance >= pLooter:GetPlayerLevel() then
- if pOwner:GetPlayerLevel() - i.m_rewardLvlVariance <= pLooter:GetPlayerLevel() then
- -- passed ALL those checks LOL
- local chance = math.random();
- 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
- local q = i.m_maxQuantity ~= 1 and math.random(1, i.m_maxQuantity) or 1;
- table.insert(phatloots, {["item"] = i, ["quantity"] = q});
- table.remove(potentialLoot, n);
- end
- end
- end
- end
- end
- end
- end
- -- finally, check if we've generated enough rewards
- if #phatloots >= self.m_maxDrops then
- break;
- end
- end
- return phatloots;
- end
- -- PlayerLoot:SendLootPrompt(loottable)
- -- Pass the return from PlayerLoot:GenerateDrops()!
- function PlayerLoot:SendLootPrompt(loottable)
- for k, v in pairs(loottable) do
- self.m_looter:AddLoot(v.item.m_entryId, v.quantity, v.quantity, false);
- end
- self.m_looter:SendLootWindow(self.m_looter:GetGUID(), 1);
- end
- --[[
- ------------------------
- -- USAGE
- ------------------------
- -- Creating a loot table
- local t = NewPlayerLootTable();
- -- Add Items to it
- t:AddItem{
- m_entryId = 100,
- m_maxQuantity = 2
- };
- -- Now you can either put this in a table for later use or use it right away.
- -- Using the loot table
- local loottable = ... (in a table or use one you just created.)
- loottable:SetLootedPlayer(pKilled);
- loottable:SetLooter(pKiller);
- local preparedTable = loottable:GenerateLoot();
- if preparedTable and #preparedTable>0 then
- loottable:SendLootPrompt(preparedTable);
- end
- ]]
- -- Announce we're good.
- do
- logcol(1+8);
- print("Loaded Player Loot system successfully.");
- logcol(7);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement