• 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 [TFS 1.3] Changing chance type

E

Evil Puncker

Guest
Hello everyone, I'm planing on adding the lots of missing items to TFS that works like a "surprise bag", I've found a nice code to use as base from ORTS, the problem is that the chances are kinda troublesome to deal with, so I need help from someone to change the chances in the following script to work like our tfs rust remover script...


surprise bags:
Lua:
local config = {
    [13670] = { -- belonging of a deceased
        chances = {
            {from = 1, to = 1442, itemId = 2238},
            {from = 1443, to = 2856, itemId = 2237},
            {from = 2857, to = 4262, itemId = 2115},
            {from = 4263, to = 4819, itemId = 2148, count = 6},
            {from = 4820, to = 5325, itemId = 2787},
            {from = 5326, to = 5813, itemId = 5890},
            {from = 5814, to = 6283, itemId = 3976, count = 4},
            {from = 6284, to = 6751, itemId = 2328},
            {from = 6752, to = 7175, itemId = 5899},
            {from = 7176, to = 7576, itemId = 5894},
            {from = 7577, to = 7949, itemId = 10606},
            {from = 7950, to = 8315, itemId = 2403},
            {from = 8316, to = 8673, itemId = 8859},
            {from = 8674, to = 8972, itemId = 5902},
            {from = 8973, to = 9187},
            {from = 9188, to = 9328, itemId = 2661},
            {from = 9329, to = 9428, itemId = 2199},
            {from = 9429, to = 9515, itemId = 13926},
            {from = 9516, to = 9594, itemId = 2143},
            {from = 9595, to = 9666, itemId = 5879},
            {from = 9667, to = 9732, itemId = 2114},
            {from = 9733, to = 9791, itemId = 13925},
            {from = 9792, to = 9845, itemId = 10563},
            {from = 9846, to = 9891, itemId = 2110},
            {from = 9892, to = 9929, itemId = 5895},
            {from = 9930, to = 9967, itemId = 5880},
            {from = 9968, to = 9998, itemId = 13508},
            {from = 9999, to = 10001, itemId = 2195}
        },
        effect = CONST_ME_POFF
    },
    [15572] = { -- gooey mass
        chances = {
            {from = 1, to = 2},
            {from = 3, to = 2167, itemId = 15487, count = 10},
            {from = 2168, to = 4243, itemId = 2152, count = 2},
            {from = 4244, to = 6196, itemId = 2144, count = 2},
            {from = 6197, to = 8149, itemId = 7591, count = 2},
            {from = 8150, to = 9823, itemId = 7590, count = 2},
            {from = 9824, to = 9923, itemId = 9971},
            {from = 9924, to = 9990, itemId = 15546},
            {from = 9991, to = 10001, itemId = 15492}
        },
        effect = CONST_ME_HITBYPOISON
    },
    [18215] = { -- gnomish supply package
        chances = {
            {from = 1, to = 1440, itemId = 2787, count = 20},
            {from = 1441, to = 2434, itemId = 18397},
            {from = 2435, to = 3270, itemId = 18437, count = 15},
            {from = 3271, to = 4085, itemId = 18304, count = 15},
            {from = 4086, to = 4836, itemId = 18457},
            {from = 4837, to = 5447, itemId = 7588, count = 2},
            {from = 5448, to = 6047, itemId = 7589, count = 2},
            {from = 6048, to = 6576, itemId = 7618, count = 4},
            {from = 6577, to = 7094, itemId = 7620, count = 4},
            {from = 7095, to = 7559, itemId = 7590},
            {from = 7560, to = 7963, itemId = 7591},
            {from = 7964, to = 8317, itemId = 7443},
            {from = 8318, to = 8628, itemId = 7439},
            {from = 8629, to = 8932, itemId = 2152, count = 5},
            {from = 8933, to = 9232, itemId = 5911},
            {from = 9233, to = 9511, itemId = 7440},
            {from = 9512, to = 9636, itemId = 18455},
            {from = 9637, to = 9747, itemId = 18522},
            {from = 9748, to = 9836, itemId = 18519},
            {from = 9837, to = 9893, itemId = 2160},
            {from = 9894, to = 9929, itemId = 2156},
            {from = 9930, to = 9958, itemId = 18509},
            {from = 9959, to = 9987, itemId = 2154},
            {from = 9988, to = 9994, itemId = 2158},
            {from = 9995, to = 10001, itemId = 2155}
        },
        effect = CONST_ME_CRAPS
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local useId = config[item.itemid]
    if not useId then
        return false
    end

    local chance = math.random(10001)
    for i = 1, #useId.chances do
        local randomItem = useId.chances[i]
        if chance >= randomItem.from and chance <= randomItem.to then
            if randomItem.itemId then
                local itemId, count = randomItem.itemId, randomItem.count or 1
                player:addItem(itemId, count)
                if item.itemid == 13670 then
                    local itemType = ItemType(itemId)
                    player:say('You found ' .. (count > 1 and count or (itemType:getArticle() ~= '' and itemType:getArticle() or '')) .. ' ' .. (count > 1 and itemType:getPluralName() or itemType:getName()) .. ' in the bag.', TALKTYPE_MONSTER_SAY)
                end
            else
                player:say('You found nothing useful.', TALKTYPE_MONSTER_SAY)
            end

            item:getPosition():sendMagicEffect(useId.effect)
            item:remove(1)
            break
        end
    end
    return true
end

rust remover:
Lua:
local config = {
    [9808] = { -- Rusty Armor (Common)
        [1] = {id = 2464, chance = 6994}, -- Chain Armor
        [2] = {id = 2483, chance = 3952}, -- Scale Armor
        [3] = {id = 2465, chance = 1502}, -- Brass Armor
        [4] = {id = 2463, chance = 197} -- Plate Armor
    },
    [9809] = { -- Rusty Armor (Semi-rare)
        [1] = {id = 2483, chance = 6437}, -- Scale Armor
        [2] = {id = 2464, chance = 4606}, -- Chain Armor
        [3] = {id = 2465, chance = 3029}, -- Brass Armor
        [4] = {id = 2463, chance = 1559}, -- Plate Armor
        [5] = {id = 2476, chance = 595}, -- Knight Armor
        [6] = {id = 8891, chance = 283}, -- Paladin Armor
        [7] = {id = 2487, chance = 49} -- Crown Armor
    },
    [9810] = { -- Rusty Armor (Rare)
        [1] = {id = 2465, chance = 6681}, -- Brass Armor
        [2] = {id = 2463, chance = 3767}, -- Plate Armor
        [3] = {id = 2476, chance = 1832}, -- Knight Armor
        [4] = {id = 2487, chance = 177}, -- Crown Armor
        [5] = {id = 8891, chance = 31}, -- Paladin Armor
        [6] = {id = 2466, chance = 10} -- Golden Armor
    },
    [9811] = { -- Rusty Legs (Common)
        [1] = {id = 2648, chance = 6949}, -- Chain Legs
        [2] = {id = 2468, chance = 3692}, -- Studded Legs
        [3] = {id = 2478, chance = 1307}, -- Brass Legs
        [4] = {id = 2647, chance = 133} -- Plate Legs
    },
    [9812] = { -- Rusty Legs (Semi-Rare)
        [1] = {id = 2468, chance = 5962}, -- Studded Legs
        [2] = {id = 2648, chance = 4037}, -- Chain Legs
        [3] = {id = 2478, chance = 2174}, -- Brass Legs
        [4] = {id = 2647, chance = 1242}, -- Plate Legs
        [5] = {id = 2477, chance = 186}, -- Knight Legs
    },
    [9813] = { -- Rusty Legs (Rare)
        [1] = {id = 2478, chance = 6500}, -- Brass Legs
        [2] = {id = 2647, chance = 3800}, -- Plate Legs
        [3] = {id = 2477, chance = 200}, -- Knight Legs
        [4] = {id = 2488, chance = 52}, -- Crown Legs
        [5] = {id = 2470, chance = 30} -- Golden Legs
    },
    [9814] = { -- Heavily Rusted Shield
    },
    [9815] = { -- Rusted Shield
    },
    [9816] = { -- Slightly Rusted Shield
        [1] = {id = 2510, chance = 3137}, -- Plate Shield
        [2] = {id = 2532, chance = 2887}, -- Ancient Shield
        [3] = {id = 7460, chance = 929}, -- Norse Shield
        [4] = {id = 2519, chance = 23}, -- Crown Shield
        [5] = {id = 2534, chance = 10} -- Vampire Shield
    },
    [9820] = { -- Heavily Rusted Helmet
    },
    [9821] = { -- Rusted Helmet
        [1] = {id = 2460, chance = 2200}, -- Brass Helmet
        [2] = {id = 2482, chance = 1870}, -- Studded Helmet
        [3] = {id = 2459, chance = 1490}, -- Iron Helmet
        [4] = {id = 2457, chance = 1010}, -- Steel Helmet
        [5] = {id = 2491, chance = 190}, -- Crown Helmet
        [6] = {id = 2497, chance = 10} -- Crusader Helmet
    },
    [9822] = { -- Slightly Rusted Helmet
        [1] = {id = 2459, chance = 3156}, -- Iron Helmet
        [2] = {id = 2457, chance = 2976}, -- Steel Helmet
        [3] = {id = 2491, chance = 963}, -- Crown Helmet
        [4] = {id = 2497, chance = 210}, -- Crusader Helmet
        [5] = {id = 2498, chance = 7} -- Royal Helmet
    },
    [9817] = { -- Heavily Rusted Boots
    },
    [9818] = { -- Rusted Boots
    },
    [9819] = { -- Slightly Rusted Boots
    },
}

local rustRemover = Action()

function rustRemover.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = config[target.itemid]
    if not targetItem then
        return true
    end

    local randomChance = math.random(10000)
    local index = false

    if targetItem[1].chance >= randomChance then -- implying first item in the table index always has the highest chance.
        while not index do
            randomIndex = math.random(#targetItem)
            if targetItem[randomIndex].chance >= randomChance then
                index = randomIndex
            end
        end
    end

    if not index then
        if table.contains({9808, 9809, 9810}, target.itemid) then
            msg = "The armor was already damaged so badly that it broke when you tried to clean it."
        end
        if table.contains({9811, 9812, 9813}, target.itemid) then
            msg = "The legs were already damaged so badly that they broke when you tried to clean them."
        end
        if table.contains({9814, 9815, 9816}, target.itemid) then
            msg = "The shield was already damaged so badly that it broke when you tried to clean it."
        end
        if table.contains({9817, 9818, 9819}, target.itemid) then
            msg = "The boots were already damaged so badly that they broke when you tried to clean them."
        end
        if table.contains({9820, 9821, 9822}, target.itemid) then
            msg = "The helmet was already damaged so badly that it broke when you tried to clean it."
        end
        player:say(msg, TALKTYPE_MONSTER_SAY)
        target:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT)
        target:remove()
    else
        target:transform(targetItem[index].id)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
    return item:remove(1)
end

rustRemover:id(9930)
rustRemover:register()
 
Last edited by a moderator:
Solution
Hello everyone, I'm planing on adding the lots of missing items to TFS that works like a "surprise bag", I've found a nice code to use as base from ORTS, the problem is that the chances are kinda troublesome to deal with, so I need help from someone to change the chances in the following script to work like our tfs rust remover script...


surprise bags:
Lua:
local config = {
    [13670] = { -- belonging of a deceased
        chances = {
            {from = 1, to = 1442, itemId = 2238},
            {from = 1443, to = 2856, itemId = 2237},
            {from = 2857, to = 4262, itemId = 2115},
            {from = 4263, to = 4819, itemId = 2148, count = 6},
            {from = 4820, to = 5325, itemId = 2787},
            {from = 5326, to =...
See?
this:
{id = 2464, chance = 6994},

is way simpler than:
{from = 8674, to = 8972, itemId = 5902},

any help is appreciated 😢
 
Hello everyone, I'm planing on adding the lots of missing items to TFS that works like a "surprise bag", I've found a nice code to use as base from ORTS, the problem is that the chances are kinda troublesome to deal with, so I need help from someone to change the chances in the following script to work like our tfs rust remover script...


surprise bags:
Lua:
local config = {
    [13670] = { -- belonging of a deceased
        chances = {
            {from = 1, to = 1442, itemId = 2238},
            {from = 1443, to = 2856, itemId = 2237},
            {from = 2857, to = 4262, itemId = 2115},
            {from = 4263, to = 4819, itemId = 2148, count = 6},
            {from = 4820, to = 5325, itemId = 2787},
            {from = 5326, to = 5813, itemId = 5890},
            {from = 5814, to = 6283, itemId = 3976, count = 4},
            {from = 6284, to = 6751, itemId = 2328},
            {from = 6752, to = 7175, itemId = 5899},
            {from = 7176, to = 7576, itemId = 5894},
            {from = 7577, to = 7949, itemId = 10606},
            {from = 7950, to = 8315, itemId = 2403},
            {from = 8316, to = 8673, itemId = 8859},
            {from = 8674, to = 8972, itemId = 5902},
            {from = 8973, to = 9187},
            {from = 9188, to = 9328, itemId = 2661},
            {from = 9329, to = 9428, itemId = 2199},
            {from = 9429, to = 9515, itemId = 13926},
            {from = 9516, to = 9594, itemId = 2143},
            {from = 9595, to = 9666, itemId = 5879},
            {from = 9667, to = 9732, itemId = 2114},
            {from = 9733, to = 9791, itemId = 13925},
            {from = 9792, to = 9845, itemId = 10563},
            {from = 9846, to = 9891, itemId = 2110},
            {from = 9892, to = 9929, itemId = 5895},
            {from = 9930, to = 9967, itemId = 5880},
            {from = 9968, to = 9998, itemId = 13508},
            {from = 9999, to = 10001, itemId = 2195}
        },
        effect = CONST_ME_POFF
    },
    [15572] = { -- gooey mass
        chances = {
            {from = 1, to = 2},
            {from = 3, to = 2167, itemId = 15487, count = 10},
            {from = 2168, to = 4243, itemId = 2152, count = 2},
            {from = 4244, to = 6196, itemId = 2144, count = 2},
            {from = 6197, to = 8149, itemId = 7591, count = 2},
            {from = 8150, to = 9823, itemId = 7590, count = 2},
            {from = 9824, to = 9923, itemId = 9971},
            {from = 9924, to = 9990, itemId = 15546},
            {from = 9991, to = 10001, itemId = 15492}
        },
        effect = CONST_ME_HITBYPOISON
    },
    [18215] = { -- gnomish supply package
        chances = {
            {from = 1, to = 1440, itemId = 2787, count = 20},
            {from = 1441, to = 2434, itemId = 18397},
            {from = 2435, to = 3270, itemId = 18437, count = 15},
            {from = 3271, to = 4085, itemId = 18304, count = 15},
            {from = 4086, to = 4836, itemId = 18457},
            {from = 4837, to = 5447, itemId = 7588, count = 2},
            {from = 5448, to = 6047, itemId = 7589, count = 2},
            {from = 6048, to = 6576, itemId = 7618, count = 4},
            {from = 6577, to = 7094, itemId = 7620, count = 4},
            {from = 7095, to = 7559, itemId = 7590},
            {from = 7560, to = 7963, itemId = 7591},
            {from = 7964, to = 8317, itemId = 7443},
            {from = 8318, to = 8628, itemId = 7439},
            {from = 8629, to = 8932, itemId = 2152, count = 5},
            {from = 8933, to = 9232, itemId = 5911},
            {from = 9233, to = 9511, itemId = 7440},
            {from = 9512, to = 9636, itemId = 18455},
            {from = 9637, to = 9747, itemId = 18522},
            {from = 9748, to = 9836, itemId = 18519},
            {from = 9837, to = 9893, itemId = 2160},
            {from = 9894, to = 9929, itemId = 2156},
            {from = 9930, to = 9958, itemId = 18509},
            {from = 9959, to = 9987, itemId = 2154},
            {from = 9988, to = 9994, itemId = 2158},
            {from = 9995, to = 10001, itemId = 2155}
        },
        effect = CONST_ME_CRAPS
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local useId = config[item.itemid]
    if not useId then
        return false
    end

    local chance = math.random(10001)
    for i = 1, #useId.chances do
        local randomItem = useId.chances[i]
        if chance >= randomItem.from and chance <= randomItem.to then
            if randomItem.itemId then
                local itemId, count = randomItem.itemId, randomItem.count or 1
                player:addItem(itemId, count)
                if item.itemid == 13670 then
                    local itemType = ItemType(itemId)
                    player:say('You found ' .. (count > 1 and count or (itemType:getArticle() ~= '' and itemType:getArticle() or '')) .. ' ' .. (count > 1 and itemType:getPluralName() or itemType:getName()) .. ' in the bag.', TALKTYPE_MONSTER_SAY)
                end
            else
                player:say('You found nothing useful.', TALKTYPE_MONSTER_SAY)
            end

            item:getPosition():sendMagicEffect(useId.effect)
            item:remove(1)
            break
        end
    end
    return true
end

rust remover:
Lua:
local config = {
    [9808] = { -- Rusty Armor (Common)
        [1] = {id = 2464, chance = 6994}, -- Chain Armor
        [2] = {id = 2483, chance = 3952}, -- Scale Armor
        [3] = {id = 2465, chance = 1502}, -- Brass Armor
        [4] = {id = 2463, chance = 197} -- Plate Armor
    },
    [9809] = { -- Rusty Armor (Semi-rare)
        [1] = {id = 2483, chance = 6437}, -- Scale Armor
        [2] = {id = 2464, chance = 4606}, -- Chain Armor
        [3] = {id = 2465, chance = 3029}, -- Brass Armor
        [4] = {id = 2463, chance = 1559}, -- Plate Armor
        [5] = {id = 2476, chance = 595}, -- Knight Armor
        [6] = {id = 8891, chance = 283}, -- Paladin Armor
        [7] = {id = 2487, chance = 49} -- Crown Armor
    },
    [9810] = { -- Rusty Armor (Rare)
        [1] = {id = 2465, chance = 6681}, -- Brass Armor
        [2] = {id = 2463, chance = 3767}, -- Plate Armor
        [3] = {id = 2476, chance = 1832}, -- Knight Armor
        [4] = {id = 2487, chance = 177}, -- Crown Armor
        [5] = {id = 8891, chance = 31}, -- Paladin Armor
        [6] = {id = 2466, chance = 10} -- Golden Armor
    },
    [9811] = { -- Rusty Legs (Common)
        [1] = {id = 2648, chance = 6949}, -- Chain Legs
        [2] = {id = 2468, chance = 3692}, -- Studded Legs
        [3] = {id = 2478, chance = 1307}, -- Brass Legs
        [4] = {id = 2647, chance = 133} -- Plate Legs
    },
    [9812] = { -- Rusty Legs (Semi-Rare)
        [1] = {id = 2468, chance = 5962}, -- Studded Legs
        [2] = {id = 2648, chance = 4037}, -- Chain Legs
        [3] = {id = 2478, chance = 2174}, -- Brass Legs
        [4] = {id = 2647, chance = 1242}, -- Plate Legs
        [5] = {id = 2477, chance = 186}, -- Knight Legs
    },
    [9813] = { -- Rusty Legs (Rare)
        [1] = {id = 2478, chance = 6500}, -- Brass Legs
        [2] = {id = 2647, chance = 3800}, -- Plate Legs
        [3] = {id = 2477, chance = 200}, -- Knight Legs
        [4] = {id = 2488, chance = 52}, -- Crown Legs
        [5] = {id = 2470, chance = 30} -- Golden Legs
    },
    [9814] = { -- Heavily Rusted Shield
    },
    [9815] = { -- Rusted Shield
    },
    [9816] = { -- Slightly Rusted Shield
        [1] = {id = 2510, chance = 3137}, -- Plate Shield
        [2] = {id = 2532, chance = 2887}, -- Ancient Shield
        [3] = {id = 7460, chance = 929}, -- Norse Shield
        [4] = {id = 2519, chance = 23}, -- Crown Shield
        [5] = {id = 2534, chance = 10} -- Vampire Shield
    },
    [9820] = { -- Heavily Rusted Helmet
    },
    [9821] = { -- Rusted Helmet
        [1] = {id = 2460, chance = 2200}, -- Brass Helmet
        [2] = {id = 2482, chance = 1870}, -- Studded Helmet
        [3] = {id = 2459, chance = 1490}, -- Iron Helmet
        [4] = {id = 2457, chance = 1010}, -- Steel Helmet
        [5] = {id = 2491, chance = 190}, -- Crown Helmet
        [6] = {id = 2497, chance = 10} -- Crusader Helmet
    },
    [9822] = { -- Slightly Rusted Helmet
        [1] = {id = 2459, chance = 3156}, -- Iron Helmet
        [2] = {id = 2457, chance = 2976}, -- Steel Helmet
        [3] = {id = 2491, chance = 963}, -- Crown Helmet
        [4] = {id = 2497, chance = 210}, -- Crusader Helmet
        [5] = {id = 2498, chance = 7} -- Royal Helmet
    },
    [9817] = { -- Heavily Rusted Boots
    },
    [9818] = { -- Rusted Boots
    },
    [9819] = { -- Slightly Rusted Boots
    },
}

local rustRemover = Action()

function rustRemover.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItem = config[target.itemid]
    if not targetItem then
        return true
    end

    local randomChance = math.random(10000)
    local index = false

    if targetItem[1].chance >= randomChance then -- implying first item in the table index always has the highest chance.
        while not index do
            randomIndex = math.random(#targetItem)
            if targetItem[randomIndex].chance >= randomChance then
                index = randomIndex
            end
        end
    end

    if not index then
        if table.contains({9808, 9809, 9810}, target.itemid) then
            msg = "The armor was already damaged so badly that it broke when you tried to clean it."
        end
        if table.contains({9811, 9812, 9813}, target.itemid) then
            msg = "The legs were already damaged so badly that they broke when you tried to clean them."
        end
        if table.contains({9814, 9815, 9816}, target.itemid) then
            msg = "The shield was already damaged so badly that it broke when you tried to clean it."
        end
        if table.contains({9817, 9818, 9819}, target.itemid) then
            msg = "The boots were already damaged so badly that they broke when you tried to clean them."
        end
        if table.contains({9820, 9821, 9822}, target.itemid) then
            msg = "The helmet was already damaged so badly that it broke when you tried to clean it."
        end
        player:say(msg, TALKTYPE_MONSTER_SAY)
        target:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT)
        target:remove()
    else
        target:transform(targetItem[index].id)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
    return item:remove(1)
end

rustRemover:id(9930)
rustRemover:register()

This?

Lua:
-- Order rewards from highest chance [TOP] to lowest chance [BOTTOM]
-- Total chance in rewards table must match math.random value, i.e. 85 + 10 + 5 = math.random(100)
local rewards = {
    [13670] = {
    Effect = CONST_ME_POFF,
    Items = {
        {ItemID = 2148, Chance = 5000, Amount = 100}, -- 50%
        {ItemID = 2152, Chance = 4000, Amount = 15}, -- 40%
        {ItemID = 2160, Chance = 1000, Amount = 1} -- 10%
    }},
    [13671] = {
    Effect = CONST_ME_HITBYPOISON,
    Items = {
        {ItemID = 600, Chance = 5000, Amount = 100},
        {ItemID = 700, Chance = 4000, Amount = 15},
        {ItemID = 800, Chance = 1000, Amount = 1}
    }}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)   
    local chance = math.random(10000) -- 1 to 10000

    local itemId = item:getId()
    for _, v in pairs(rewards[itemId].Items) do
        if chance <= v.Chance then
            if item then
                player:addItem(v.ItemID, v.Amount)
                item:getPosition():sendMagicEffect(rewards[itemId].Effect)
                item:remove()
                return true
            end
        else
            chance = chance - v.Chance
        end
    end
end
 
Solution
This?

Lua:
-- Order rewards from highest chance [TOP] to lowest chance [BOTTOM]
-- Total chance in rewards table must match math.random value, i.e. 85 + 10 + 5 = math.random(100)
local rewards = {
    [13670] = {
    Effect = CONST_ME_POFF,
    Items = {
        {ItemID = 2148, Chance = 5000, Amount = 100}, -- 50%
        {ItemID = 2152, Chance = 4000, Amount = 15}, -- 40%
        {ItemID = 2160, Chance = 1000, Amount = 1} -- 10%
    }},
    [13671] = {
    Effect = CONST_ME_HITBYPOISON,
    Items = {
        {ItemID = 600, Chance = 5000, Amount = 100},
        {ItemID = 700, Chance = 4000, Amount = 15},
        {ItemID = 800, Chance = 1000, Amount = 1}
    }}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)  
    local chance = math.random(10000) -- 1 to 10000

    local itemId = item:getId()
    for _, v in pairs(rewards[itemId].Items) do
        if chance <= v.Chance then
            if item then
                player:addItem(v.ItemID, v.Amount)
                item:getPosition():sendMagicEffect(rewards[itemId].Effect)
                item:remove()
                return true
            end
        else
            chance = chance - v.Chance
        end
    end
end
exactly what I needed! one last thing tho, is it possible to make a print for debugging purposes to print the total sum of chance from each item? so I don't need to check them with the calculator manually when adding them?
 
exactly what I needed! one last thing tho, is it possible to make a print for debugging purposes to print the total sum of chance from each item? so I don't need to check them with the calculator manually when adding them?

you can easily use nested loop like this

Lua:
-- Order rewards from highest chance [TOP] to lowest chance [BOTTOM]
-- Total chance in rewards table must match math.random value, i.e. 85 + 10 + 5 = math.random(100)
local rewards = {
    [13670] = {
    Effect = CONST_ME_POFF,
    Items = {
        {ItemID = 2148, Chance = 5000, Amount = 100}, -- 50%
        {ItemID = 2152, Chance = 4000, Amount = 15}, -- 40%
        {ItemID = 2160, Chance = 1000, Amount = 1} -- 10%
    }},
    [13671] = {
    Effect = CONST_ME_HITBYPOISON,
    Items = {
        {ItemID = 600, Chance = 5000, Amount = 100},
        {ItemID = 700, Chance = 4000, Amount = 15},
        {ItemID = 800, Chance = 1000, Amount = 1}
    }}
}

-- Do not use in production
local function printDebugMsg()
    for i, j in pairs(rewards) do
        local totalSum = 0
        for k = 1, #j.Items do
            totalSum = totalSum + j.Items[k].Chance
        end
        print("Total sum for item " .. i .. " is " .. totalSum)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    printDebugMsg()
    
    local chance = math.random(10000) -- 1 to 10000

    local itemId = item:getId()
    for _, v in pairs(rewards[itemId].Items) do
        if chance <= v.Chance then
            if item then
                player:addItem(v.ItemID, v.Amount)
                item:getPosition():sendMagicEffect(rewards[itemId].Effect)
                item:remove()
                return true
            end
        else
            chance = chance - v.Chance
        end
    end
end
 
Back
Top