• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

C++ imbuiment mods 8.6

kfalls

New Member
Joined
Jan 11, 2018
Messages
16
Reaction score
1
I would like a system where the player uses an item on it, and when it hits the monster or another player it fills up some of the mana and life as a percentage of the damage caused, and with a duration of two hours after that the player has to use the item again.

tfs 0.3.6
 
Step 1:
data/actions/scripts/leech_item.lua

LUA:
local config = {
    storage = 50000,
    duration = 2 * 60 * 60,
    itemId = 12345,
    effect = CONST_ME_MAGIC_BLUE
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if not isPlayer(cid) then
        return true
    end

    if item.itemid ~= config.itemId then
        return true
    end

    local current = getPlayerStorageValue(cid, config.storage)
    if current ~= -1 and current > os.time() then
        local left = current - os.time()
        local hours = math.floor(left / 3600)
        local minutes = math.floor((left % 3600) / 60)

        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR,
            "This item is already active. Time left: " .. hours .. " hour(s) and " .. minutes .. " minute(s).")
        return true
    end

    setPlayerStorageValue(cid, config.storage, os.time() + config.duration)
    doSendMagicEffect(getThingPos(cid), config.effect)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Life/Mana leech activated for 2 hours.")
    return true
end


Step 2:
data/creaturescripts/scripts/leech_buff.lua

LUA:
local config = {
    storage = 50000,            -- must be the same storage as in leech_item.lua
    healthPercent = 20,         -- % of damage returned as life
    manaPercent = 15,           -- % of damage returned as mana
    effect = CONST_ME_HOLYDAMAGE
}

function onStatsChange(cid, attacker, type, combat, value)
    if not attacker or not isCreature(attacker) then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end

    -- We only care when the target loses health
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    -- Ignore zero/negative weird values
    if value <= 0 then
        return true
    end

    -- Check if buff is active
    local expireTime = getPlayerStorageValue(attacker, config.storage)
    if expireTime == -1 or expireTime < os.time() then
        return true
    end

    -- Prevent self-hit abuse
    if cid == attacker then
        return true
    end

    -- Only apply if target is monster or player
    if not isMonster(cid) and not isPlayer(cid) then
        return true
    end

    local healAmount = math.floor((value * config.healthPercent) / 100)
    local manaAmount = math.floor((value * config.manaPercent) / 100)

    if healAmount > 0 then
        doCreatureAddHealth(attacker, healAmount)
    end

    if manaAmount > 0 then
        doPlayerAddMana(attacker, manaAmount)
    end

    if healAmount > 0 or manaAmount > 0 then
        doSendMagicEffect(getThingPos(attacker), config.effect)
    end

    return true
end

Step 3:
data/creaturescripts/creaturescripts.xml

XML:
<event type="statschange" name="LifeManaLeech" event="script" value="leech_buff.lua"/>

Step 4:
data/creaturescripts/scripts/login.lua

LUA:
function onLogin(cid)
    XXXXXX CODE
    registerCreatureEvent(cid, "LifeManaLeech")
    XXXXXX OTHER CODE
    return true
end

Step 5:
data/actions/actions.xml

LUA:
<action itemid="12345" script="leech_item.lua"/>
 
Back
Top