local config = {
itemId = 1111,
removeItem = true,
soulAmount = 50,
duration = 60, -- in seconds
ticks = 5000, -- milliseconds (how often it heals)
healhPercent = 20,
manaPercent = 20,
regenerationEffect = CONST_ME_MAGIC_GREEN,
buffActiveEffect = CONST_ME_MAGIC_RED,
buffActiveEffectTicks = 1500
}
local potionUsers = {} -- don't touch
local function potion(playerId)
local player = Player(playerId)
if not player then
potionUsers[playerId] = nil
return
end
local sendEffect = false
local maxHealth = player:getMaxHealth()
local currentHealth = player:getHealth()
if currentHealth < maxHealth then
local healthToAdd = math.min(maxHealth - currentHealth, math.floor((maxHealth * config.healhPercent) / 100))
player:addHealth(healthToAdd)
sendEffect = true
end
local maxMana = player:getMaxMana()
local currentMana = player:getMana()
if currentMana < maxMana then
local manaToAdd = math.min(maxMana - currentMana, math.floor((maxMana * config.manaPercent) / 100))
player:addMana(manaToAdd)
sendEffect = true
end
if sendEffect then
player:getPosition():sendMagicEffect(config.regenerationEffect)
end
if potionUsers[playerId] > 0 then
potionUsers[playerId] = potionUsers[playerId] - config.ticks
addEvent(potion, config.ticks, playerId)
return
end
player:say("Potion deactivated!", TALKTYPE_MONSTER_SAY)
potionUsers[playerId] = nil
end
local function buffAnimation(playerId, lastKnownDuration)
local player = Player(playerId)
if not player then
return
end
if not potionUsers[playerId] then
return
end
if potionUsers[playerId] > lastKnownDuration then
return
end
player:getPosition():sendMagicEffect(config.buffActiveEffect)
addEvent(buffAnimation, config.buffActiveEffectTicks, playerId, potionUsers[playerId])
end
local potionOfTheGods = Action()
function potionOfTheGods.onUse(player, item, fromPosition, target, toPosition, isHotkey)
local playerId = player:getId()
if potionUsers[playerId] then
player:sendTextMessage(MESSAGE_STATUS_SMALL, "Potion is still active.")
player:getPosition():sendMagicEffect(CONST_ME_POFF, player) -- shows only for this player
return false
end
if player:getSoul() < config.soulAmount then
player:sendTextMessage(MESSAGE_STATUS_SMALL, "Insufficient soul.")
player:getPosition():sendMagicEffect(CONST_ME_POFF, player)
return false
end
potionUsers[playerId] = config.duration * 1000
player:addSoul(-config.soulAmount)
if config.removeItem then
item:remove(1)
end
buffAnimation(playerId, potionUsers[playerId])
potion(playerId)
player:say("Potion activated!", TALKTYPE_MONSTER_SAY)
return true
end
potionOfTheGods:id(config.itemId)
potionOfTheGods:register()