• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua help in script loot block

ZeroSkyer

Member
Joined
May 17, 2019
Messages
62
Reaction score
9
Location
EEUU
Hello! I made this code to limit how many items can be looted per day, it works fine. But I would like to add many more items, any ideas? Thanks in advance.

Lua:
local falconcirclet = 32414
if lootBlock.itemId == falconcirclet then
    if Game.getStorageValue(falconcirclet) < 2 then
        lootBlock.itemId = falconcirclet
        Game.setStorageValue(falconcirclet, Game.getStorageValue(falconcirclet) + 1)
    else
        print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
        return lootTable
    end
end
 
Can you be more specific?
Not exactly sure what you're actually asking.

Anyway, My Immediate idea would be to create a table of itemId's and amounts, and loop through or check against the table.

Lua:
local limitedQuantityItems = {
    [1111] = {amountLootedToday = 0, maximumLootableAmount = 2},
    [2222] = {amountLootedToday = 0, maximumLootableAmount = 8},
    [3333] = {amountLootedToday = 0, maximumLootableAmount = 4}
}

---------

local itemId = 1111

local checkItem = limitedQuantityItems[itemId]

if checkItem then
    if checkItem.amountLootedToday >= checkItem.maximumLootableAmount then
        -- refuse item
    end
 
    -- another check for is stackable, and if amount in the stack is greater then checkItem.maximumLootableAmount...
    -- then reduce the amount being given as loot, to be equal to the amount remaining, until hit maximum
    checkItem.amountLootedToday = checkItem.amountLootedToday + checkItem.amount

    -- OR don't worry about the actual amount of items looted, but just count the occurrence of it happening. 
    checkItem.amountLootedToday = checkItem.amountLootedToday + 1

    -- then give loot
end

-- amountLootedToday can be used as a global storage instead, if you prefer. but is essentially the same thing

You could further expand on this idea, and make it so each player is stored in the table, instead of making it a global limit, and have it as a character/account limit.
 
Can you be more specific?
Not exactly sure what you're actually asking.

Anyway, My Immediate idea would be to create a table of itemId's and amounts, and loop through or check against the table.

Lua:
local limitedQuantityItems = {
    [1111] = {amountLootedToday = 0, maximumLootableAmount = 2},
    [2222] = {amountLootedToday = 0, maximumLootableAmount = 8},
    [3333] = {amountLootedToday = 0, maximumLootableAmount = 4}
}

---------

local itemId = 1111

local checkItem = limitedQuantityItems[itemId]

if checkItem then
    if checkItem.amountLootedToday >= checkItem.maximumLootableAmount then
        -- refuse item
    end
 
    -- another check for is stackable, and if amount in the stack is greater then checkItem.maximumLootableAmount...
    -- then reduce the amount being given as loot, to be equal to the amount remaining, until hit maximum
    checkItem.amountLootedToday = checkItem.amountLootedToday + checkItem.amount

    -- OR don't worry about the actual amount of items looted, but just count the occurrence of it happening.
    checkItem.amountLootedToday = checkItem.amountLootedToday + 1

    -- then give loot
end

-- amountLootedToday can be used as a global storage instead, if you prefer. but is essentially the same thing

You could further expand on this idea, and make it so each player is stored in the table, instead of making it a global limit, and have it as a character/account limit.
Thanks for your help, I couldn't get the code to work. I try to put the code here, the previous one works but only 1 item. I did in function function MonsterType.createLootItem(self, lootBlock, chance, lootTable)
Lua:
if not globalBosses then
    globalBosses = {}
end

function Monster.setReward(self, enable)
    if enable then
        if not self:getType():isRewardBoss() then
            error("Rewards can only be enabled to rewards bosses.")
            return false
        end
        globalBosses[self:getId()] = {}
        self:registerEvent("BossDeath")
        self:registerEvent("BossThink")
    else
        globalBosses[self:getId()] = nil
        self:unregisterEvent("BossDeath")
        self:unregisterEvent("BossThink")
    end
    return true
end

local function pushValues(buffer, sep, ...)
    local argv = {...}
    local argc = #argv
    for k, v in ipairs(argv) do
        table.insert(buffer, v)
        if k < argc and sep then
            table.insert(buffer, sep)
        end
    end
end

function Item.getNameDescription(self)
    local subType = self:getSubType()
    local itemType = self:getType()

    local buffer = {}

    local name = self:getName() or ''
    if(#name ~= 0) then
        if(itemType:isStackable() and subType > 1) then
            pushValues(buffer, ' ', subType, self:getPluralName())
        else
            local article = self:getArticle() or ''
            pushValues(buffer, ' ', select(#article ~= 0 and 1 or 2, article, name))
        end
    else
        pushValues(buffer, ' ', 'an item of type', self:getId())
    end

    return table.concat(buffer)
end

function Container.getContentDescription(self, outputBuffer)
    local firstItem = true
    local buffer = outputBuffer or {}
    for i = 1, self:getSize() do
        local item = self:getItem(i - 1)

        if(firstItem) then
            firstItem = false
        else
            table.insert(buffer, ", ")
        end

        table.insert(buffer, item:getNameDescription())
    end

    if firstItem then
        table.insert(buffer, "nothing")
    end

    if not outputBuffer then
        return table.concat(buffer)
    end
end

function Player.getRewardChest(self, autocreate)
    return self:getDepotChest(99, autocreate)
end

function Player.inBossFight(self)
    if not next(globalBosses) then
        return false
    end
    local playerGuid = self:getGuid()

    for _, info in pairs(globalBosses) do
        local stats = info[playerGuid]
        if stats and stats.active then
            return stats
        end
    end
    return false
end

-- by https://otland.net/members/cbrm.25752/ with some modifications
function MonsterType.createLootItem(self, lootBlock, chance, lootTable)
    local lootTable, itemCount = lootTable or {}, 0
    local randvalue = math.random(0, 100000) / (getConfigInfo("rateLoot") * chance)
    if randvalue < lootBlock.chance then
        if (ItemType(lootBlock.itemId):isStackable()) then
            itemCount = randvalue % lootBlock.maxCount + 1
        else
            itemCount = 1
        end
    end

    local itemType = ItemType(lootBlock.itemId)
    local decayTo = itemType:getDecayId()
    local decayTime = itemType:getDecayTime()
    if decayTo and decayTo >= 0 and decayTime and decayTime ~= 0 then
        local transformDeEquipId = itemType:getTransformDeEquipId()
        if transformDeEquipId and transformDeEquipId > 0 then
            print("[MonsterType.createLootItem] - Convert boss '" .. self:name() .. "' reward ID '" .. lootBlock.itemId .. "' to ID " .. transformDeEquipId .. ".")
            lootBlock.itemId = transformDeEquipId
        else
            print("[MonsterType.createLootItem] Cannot add item " .. lootBlock.itemId .. " as boss " .. self:name() .. " reward. It has decay.")
            return lootTable
        end
    end

    while itemCount > 0 do
        local n = math.min(itemCount, 100)
        itemCount = itemCount - n
        table.insert(lootTable, {lootBlock.itemId, n})
    end

    return lootTable
end

function MonsterType.getBossReward(self, lootFactor, topScore)
    local result = {}
    if getConfigInfo("rateLoot") > 0 then
        local loot = self:getLoot() or {}
        for i = #loot, 0, -1 do
            local lootBlock = loot[i]
            if lootBlock then
                if lootBlock.unique and not topScore then
                    --continue
                else
                    self:createLootItem(lootBlock, lootFactor, result)
                end
            end
        end
    end
    return result
end
Post automatically merged:

See, basically I want to optimize this script. If you could help me I would be grateful, anyway thanks!
Lua:
local falconcirclet = 32414
    local falconcoif = 32415
    local falconrod = 32416
    local falconwand = 32417
    local falconbow = 32418
    local falconplate = 32419
    local falcongreaves = 32420
    local falconshield = 32421
    local falconescutcheon = 32422
    local falconlongsword = 32423
    local falconbattleaxe = 32424
    local falconmace = 32425
    if lootBlock.itemId == falconcirclet then
        if Game.getStorageValue(falconcirclet) < 2 then
            lootBlock.itemId = falconcirclet
            Game.setStorageValue(falconcirclet, Game.getStorageValue(falconcirclet) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconcoif then
        if Game.getStorageValue(falconcoif) < 2 then
            lootBlock.itemId = falconcoif
            Game.setStorageValue(falconcoif, Game.getStorageValue(falconcoif) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconrod then
        if Game.getStorageValue(falconrod) < 2 then
            lootBlock.itemId = falconrod
            Game.setStorageValue(falconrod, Game.getStorageValue(falconrod) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconwand then
        if Game.getStorageValue(falconwand) < 2 then
            lootBlock.itemId = falconwand
            Game.setStorageValue(falconwand, Game.getStorageValue(falconwand) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconbow then
        if Game.getStorageValue(falconbow) < 2 then
            lootBlock.itemId = falconbow
            Game.setStorageValue(falconbow, Game.getStorageValue(falconbow) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconplate then
        if Game.getStorageValue(falconplate) < 2 then
            lootBlock.itemId = falconplate
            Game.setStorageValue(falconplate, Game.getStorageValue(falconplate) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falcongreaves then
        if Game.getStorageValue(falcongreaves) < 2 then
            lootBlock.itemId = falcongreaves
            Game.setStorageValue(falcongreaves, Game.getStorageValue(falcongreaves) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconshield then
        if Game.getStorageValue(falconshield) < 2 then
            lootBlock.itemId = falconshield
            Game.setStorageValue(falconshield, Game.getStorageValue(falconshield) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconescutcheon then
        if Game.getStorageValue(falconescutcheon) < 2 then
            lootBlock.itemId = falconescutcheon
            Game.setStorageValue(falconescutcheon, Game.getStorageValue(falconescutcheon) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconlongsword then
        if Game.getStorageValue(falconlongsword) < 2 then
            lootBlock.itemId = falconlongsword
            Game.setStorageValue(falconlongsword, Game.getStorageValue(falconlongsword) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconbattleaxe then
        if Game.getStorageValue(falconbattleaxe) < 2 then
            lootBlock.itemId = falconbattleaxe
            Game.setStorageValue(falconbattleaxe, Game.getStorageValue(falconbattleaxe) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    elseif lootBlock.itemId == falconmace then
        if Game.getStorageValue(falconmace) < 2 then
            lootBlock.itemId = falconmace
            Game.setStorageValue(falconmace, Game.getStorageValue(falconmace) + 1)
        else
            print("[MonsterType.createLootItem] - The limit of the following item has been reached: ".. lootBlock.itemId .. " ")
            return lootTable
        end
    end

I am giving the storage of the same item to make it simpler.
 
Last edited:
Back
Top