• 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!

[C++ / LUA] Loot rate or chance

narko

vertrauenswürdig ~
Joined
Oct 19, 2008
Messages
1,317
Solutions
2
Reaction score
130
Location
Unknown
Hi there! I am working on a new custom server on a fresh TFS 1.2 and I'd like to make an item for a quest that gives you bonus loot rate or chance for a certain period of time. I've been looking around the forum and found that the loot has to be removed and recreated. Any idea is really appreciated.

Thanks in advance.
 
Last edited:
Solution
Code:
target:setDropLoot(newLoot)
will not work setDropLoot parameter is a boolean, you can't add loot with this function, my guess to you give more loot you will add the items on the corpse of the monster with a creaturescript
yeah I wasn't sure, to lazy to check just made assumptions
do this instead
XML:
      <event type="kill" name="bonuslootkill" script="bonusloot.lua"/>
    <event type="death" name="bonusloot" script="bonusloot.lua"/>

Lua:
local BONUS_RATE = 3 -- 1 normal loot, 2 double loot etc

function onKill(player, target, lastHit)
    if target:isMonster() then
        target:registerEvent('bonusloot')
    end
    return true
end

function onDeath(creature, corpse, killer, mostDamageKiller, unjustified...

Aled

Active Member (probably)
Joined
Mar 30, 2017
Messages
215
Solutions
17
Reaction score
130
Hi there! I am working on a new custom server on a fresh TFS 1.2 and I'd like to make an item for a quest that gives you bonus loot rate or chance for a certain period of time. I've been looking around the forum and found that the loot has to be removed and recreated. Any idea is really appreciated.

Thanks in advance.
use an onequip movement event to register this creature event with that item
Lua:
local BONUS_RATE = 3
function onKill(player, target, lastHit)
    if target:isMonster() then
        local newLoot = {}
        for i, k in pairs(Monster(target):getType():getLoot()) do
            newLoot[i] = k
            newLoot[i].chance = BONUS_RATE*k.chance
        end
        target:setDropLoot(newLoot)
    end
    return true
end
not tested
 
Last edited:

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,479
Solutions
9
Reaction score
209
use an onequip movement event to register this creature event with that item
Lua:
local BONUS_RATE = 3
function onKill(player, target, lastHit)
    if target:isMonster() then
        local newLoot = {}
        for i, k in pairs(Monster(target):getType():getLoot()) do
            newLoot[i] = k
            newLoot[i].chance = BONUS_RATE*k.chance
        end
        target:setDropLoot(newLoot)
    end
    return true
end
not tested
Code:
target:setDropLoot(newLoot)
will not work setDropLoot parameter is a boolean, you can't add loot with this function, my guess to you give more loot you will add the items on the corpse of the monster with a creaturescript
 

Aled

Active Member (probably)
Joined
Mar 30, 2017
Messages
215
Solutions
17
Reaction score
130
Code:
target:setDropLoot(newLoot)
will not work setDropLoot parameter is a boolean, you can't add loot with this function, my guess to you give more loot you will add the items on the corpse of the monster with a creaturescript
yeah I wasn't sure, to lazy to check just made assumptions
do this instead
XML:
      <event type="kill" name="bonuslootkill" script="bonusloot.lua"/>
    <event type="death" name="bonusloot" script="bonusloot.lua"/>

Lua:
local BONUS_RATE = 3 -- 1 normal loot, 2 double loot etc

function onKill(player, target, lastHit)
    if target:isMonster() then
        target:registerEvent('bonusloot')
    end
    return true
end

function onDeath(creature, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    if not creature then
        return true
    end
    if not creature:getType():getLoot() then
        return true
    end
    local tc = 0
    for i, k in pairs(creature:getType():getLoot()) do
        tc = tc + k.chance
    end
    for i = 1, BONUS_RATE-1 do
        for i, k in pairs(creature:getType():getLoot()) do
            if math.random() < k.chance/tc then
                local item = corpse:addItem(k.itemId,math.random(k.maxCount) or k.subType)
                if item then
                    if k.actionId then item:setActionId(k.actionId) end
                    if k.text then item:setAttribute(ITEM_ATTRIBUTE_TEXT, k.text) end
                end
            end
        end
    end
    return true
end
 
Last edited:
Solution
OP
OP
narko

narko

vertrauenswürdig ~
Joined
Oct 19, 2008
Messages
1,317
Solutions
2
Reaction score
130
Location
Unknown
yeah I wasn't sure, to lazy to check just made assumptions
do this instead
XML:
      <event type="kill" name="bonuslootkill" script="bonusloot.lua"/>
    <event type="death" name="bonusloot" script="bonusloot.lua"/>

Lua:
local BONUS_RATE = 3 -- 1 normal loot, 2 double loot etc

function onKill(player, target, lastHit)
    if target:isMonster() then
        targer:registerEvent('bonusloot')
    end
    return true
end

function onDeath(creature, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    if not creature then
        return true
    end
    if not creature:getType():getLoot() then
        return true
    end
    local tc = 0
    for i, k in pairs(creature:getType():getLoot()) do
        tc = tc + k.chance
    end
    for i = 1, BONUS_RATE-1 do
        for i, k in pairs(creature:getType():getLoot()) do
            if math.random() < k.chance/tc then
                local item = corpse:addItem(k.itemId,math.random(k.maxCount) or k.subType)
                if item then
                    if k.actionId then item:setActionId(k.actionId) end
                    if k.text then item:setAttribute(ITEM_ATTRIBUTE_TEXT, k.text) end
                end
            end
        end
    end
    return true
end

It worked perfectly btw there's a little misspelling in target:registerEvent('bonusloot'). Thank you very much Aled.
 
Top