• 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] how to make script select more than one reward

E

Evil Puncker

Guest
Hi everyone, I want to make a PR once again to TFS with this and I don't know how to make the item generate 7 itens from the reward table, any help is appreciated:

Lua:
local presents = {
    [6507] = { -- red christmas bundle
        {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6504, 6388
    },
    [6508] = { -- blue christmas bundle
        {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6505, 6388
    },
    [6509] = { -- green christmas bundle
        {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6503, 6388
    }
}

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

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        gift = gift[1]
        count = gift[2]
    end

    player:addItem(gift, count)
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end
 
Solution
yes, player get 7 random items from the table
can you test this one?

Lua:
local test = Action()

function test.onUse(player, item, fromPosition, target, toPosition, isHotkey)
   
    local presents = {
        [6507] = { -- red christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6504, 6388
        },
        [6508] = { -- blue christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6505, 6388
        },
        [6509] = { -- green christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6503, 6388
        }
    }

    local targetItem = presents[item.itemid]
    if not targetItem then...
yes, player get 7 random items from the table
can you test this one?

Lua:
local test = Action()

function test.onUse(player, item, fromPosition, target, toPosition, isHotkey)
   
    local presents = {
        [6507] = { -- red christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6504, 6388
        },
        [6508] = { -- blue christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6505, 6388
        },
        [6509] = { -- green christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6503, 6388
        }
    }

    local targetItem = presents[item.itemid]
    if not targetItem then
        return true
    end

    local rewards = {}
    repeat
        local count = 1
        local rand = math.random(#targetItem)
        local gift = targetItem[rand]
        if type(gift) == "table" then
            count = gift[2]
            gift = gift[1]
        end
        rewards[#rewards + 1] = {gift, count}
        table.remove(targetItem, rand)
    until #rewards == 7
   
    for i = 1, #rewards do
        player:addItem(rewards[i][1], rewards[i][2])
    end
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

test:id(6507, 6508, 6509)
test:register()
 
Solution
can u stop filling tfs github with these things? before i could come and check it for useful new things, now it's filled of PRs that are adding very minor stuff to mimic things in real tibia. why are we copying real tibia in the official github repository? didnt mark sayd years ago we shouldnt be copying real tibia? and wibbenz just merges these things without thinking. some things arent even correct behaviour like wall mirror script
 
can u stop filling tfs github with these things? before i could come and check it for useful new things, now it's filled of PRs that are adding very minor stuff to mimic things in real tibia. why are we copying real tibia in the official github repository? didnt mark sayd years ago we shouldnt be copying real tibia? and wibbenz just merges these things without thinking. some things arent even correct behaviour like wall mirror script

Getting more sample scripts into the TFS repo is a good thing in my opinion. I have frequently been frustrated at the lack of sample codes in the default repo and annoyed that I would need to grab them from a rlmap datapack like ORTS, which also has modified lib files to accommodate for their adjustments.

If you identify something wrong in a PR, please review it, or create a separate PR that resolves the issue. I appreciate the efforts done by @Evil Puncker to invigorate some life into our repository datapack with some fresh codes.

While I wouldn't want 20 duplicate boat NPC files in our repo(as in copying real tibia), I would appreciate one good fleshed out one for the sake of good reference code. In the same fashion, this code appears to work as a good reference code for any multiple reward quests. (and/or random reward generation).
 
Last edited:
I'm surprised something like this doesn't exist in the code-base:
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local primarySkillList = {
        ["SKILL_SWORD"] = player:getSkillLevel(SKILL_SWORD),
        ["SKILL_AXE"] = player:getSkillLevel(SKILL_AXE),
        ["SKILL_CLUB"] = player:getSkillLevel(SKILL_CLUB)}

    -- Get Knight primary weapon type
    local max_val, skillname = -math.huge
    for k, v in pairs(primarySkillList) do
        if v > max_val then
            max_val, skillname = v, k
        end
    end

    local primarySkill = skillname
    local vocation = player:getVocation():getId()
    local reward = 2160

    -- Calculate reward based of Vocation and primary weapon type if Knight.
    if vocation == 1 or vocation == 5 then
        reward = 2187
    elseif vocation == 2 or vocation == 6 then
        reward = 2183
    elseif vocation == 3 or vocation == 7 then
        reward = 8891
    elseif vocation == 4 or vocation == 8 then
        if primarySkill == "SKILL_SWORD" then
            reward = 2392
        elseif primarySkill == "SKILL_AXE" then
            reward = 7454
        elseif primarySkill == "SKILL_CLUB" then
            reward = 7437
        end
    else
        reward = 23531
    end

    local itemType = ItemType(reward)
    local itemWeight = itemType:getWeight()
    local playerCap = player:getFreeCapacity()

    if player:getStorageValue(reward) == -1 then
        if playerCap >= itemWeight then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. itemType:getName() .. '.')
            player:addItem(reward, 1)
            player:setStorageValue(reward, 1)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. itemType:getName() .. ' weighing ' ..(itemWeight / 100).. ' oz. It\'s too heavy.')
        end
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end
    return true
end

Reward based on voc.
If knight reward based on highest SKILL_XXXX
 
Last edited:
can u stop filling tfs github with these things? before i could come and check it for useful new things, now it's filled of PRs that are adding very minor stuff to mimic things in real tibia. why are we copying real tibia in the official github repository? didnt mark sayd years ago we shouldnt be copying real tibia? and wibbenz just merges these things without thinking. some things arent even correct behaviour like wall mirror script
doing nothing and flaming ppl who is doing something, is this LoL or am I wrong?
 
can u stop filling tfs github with these things? before i could come and check it for useful new things, now it's filled of PRs that are adding very minor stuff to mimic things in real tibia. why are we copying real tibia in the official github repository? didnt mark sayd years ago we shouldnt be copying real tibia? and wibbenz just merges these things without thinking. some things arent even correct behaviour like wall mirror script
sorry I don't have enough knowledge to make something more advanced, but no, I won't stop trying to add missing items actions or whatsoever to TFS, we don't need to be forced to use datapacks to make items work that should be working by default, my goal is to make TFS "vanilla" datapack more than enough to be used for creating any OT that you like, without the need to add basic working systems/items, not trying to mimic real tibia, just making things work
 
can you test this one?

Lua:
local test = Action()

function test.onUse(player, item, fromPosition, target, toPosition, isHotkey)
  
    local presents = {
        [6507] = { -- red christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6504, 6388
        },
        [6508] = { -- blue christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6505, 6388
        },
        [6509] = { -- green christmas bundle
            {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6503, 6388
        }
    }

    local targetItem = presents[item.itemid]
    if not targetItem then
        return true
    end

    local rewards = {}
    repeat
        local count = 1
        local rand = math.random(#targetItem)
        local gift = targetItem[rand]
        if type(gift) == "table" then
            count = gift[2]
            gift = gift[1]
        end
        rewards[#rewards + 1] = {gift, count}
        table.remove(targetItem, rand)
    until #rewards == 7
  
    for i = 1, #rewards do
        player:addItem(rewards[i][1], rewards[i][2])
    end
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

test:id(6507, 6508, 6509)
test:register()

it works, thanks! someone noticed that the table is the same for all 3 bundles, except for items 6503 to 6505, could you please help merging the table into one and making these items the only one that are different between each bundle? thanks
 
Lua:
local test = Action()

function test.onUse(player, item, fromPosition, target, toPosition, isHotkey)
 
    local bundleTypes = {
        [6507] = { -- red christmas bundle
            6504
        },
        [6508] = { -- blue christmas bundle
            6505
        },
        [6509] = { -- green christmas bundle
            6503
        }
    }

    local common = {
        {6569, 15}, {2687, 20}, {2688, 10}, {2675, 10}, {2674, 5}, 6501, 6502, 6490, 6388
    }
   
    local targetItem = bundleTypes[item.itemid]
    if not targetItem then
        return true
    end
   
    targetItem = common

    -- In case there's going to be more than one unique item per bundle
    for i = 1, #bundleTypes[item.itemid] do
        table.insert(targetItem, bundleTypes[item.itemid][i])
    end
   
    local rewards = {}
    repeat
        local count = 1
        local rand = math.random(#targetItem)
        local gift = targetItem[rand]
        if type(gift) == "table" then
            count = gift[2]
            gift = gift[1]
        end
        rewards[#rewards + 1] = {gift, count}
        table.remove(targetItem, rand)
    until #rewards == 7
 
    for i = 1, #rewards do
        player:addItem(rewards[i][1], rewards[i][2])
    end
    item:remove(1)
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    return true
end

test:id(6507, 6508, 6509)
test:register()
 
Back
Top