Hello everyone! I'm trying to apply elemental damage to mana using onManaChange on my TFS 1.4 server, but it's not working.
onHealthChange works fine, but with onManaChange, the elemental damage is never applied.
onHealthChange works fine, but with onManaChange, the elemental damage is never applied.
LUA:
local onManaChangeDamage = CreatureEvent("onManaDamage")
function onManaChangeDamage.onManaChange(target, source, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
print("[DEBUG] onManaChange START")
print("[DEBUG] Target:", target and target:getName() or "nil")
print("[DEBUG] Source:", source and source:getName() or "nil")
print("[DEBUG] Primary:", primaryDamage, primaryType, "Secondary:", secondaryDamage, secondaryType, "Origin:", origin)
local weapon
if source and source:isPlayer() then
weapon = source:getSlotItem(CONST_SLOT_LEFT) or source:getSlotItem(CONST_SLOT_RIGHT)
end
print("[DEBUG] Equipped weapon:", weapon and weapon:getName() or "none")
local elementTypes = {
{combatType = COMBAT_FIREDAMAGE, storage = 977554},
{combatType = COMBAT_ICEDAMAGE, storage = 977555},
{combatType = COMBAT_ENERGYDAMAGE, storage = 977556},
{combatType = COMBAT_EARTHDAMAGE, storage = 977557},
{combatType = COMBAT_DEATHDAMAGE, storage = 977558},
{combatType = COMBAT_WATERDAMAGE, storage = 977559},
{combatType = COMBAT_HOLYDAMAGE, storage = 977560},
{combatType = COMBAT_ARCANEDAMAGE, storage = 977561}
}
local totalElementPercent = 0
local finalElementType = COMBAT_NONE
if weapon then
local itemType = weapon:getType()
local weaponElement = itemType:getElementDamage() or 0
local weaponElementType = itemType:getElementType() or COMBAT_NONE
print("[DEBUG] Weapon element:", weaponElement, weaponElementType)
if weaponElement > 0 and weaponElementType ~= COMBAT_NONE then
totalElementPercent = weaponElement
finalElementType = weaponElementType
print("[DEBUG] Base element found:", finalElementType, "with", totalElementPercent, "%")
for _, elem in ipairs(elementTypes) do
if elem.combatType == weaponElementType then
local storageValue = source and source:getStorageValue(elem.storage) or 0
print("[DEBUG] Additional element storage:", storageValue)
if storageValue > 0 then
totalElementPercent = totalElementPercent + storageValue
end
break
end
end
end
end
if totalElementPercent == 0 then
print("[DEBUG] No weapon element, checking storages...")
for _, elem in ipairs(elementTypes) do
local storageValue = source and source:getStorageValue(elem.storage) or 0
print("[DEBUG] Storage", elem.combatType, "=", storageValue)
if storageValue > totalElementPercent then
totalElementPercent = storageValue
finalElementType = elem.combatType
end
end
end
print("[DEBUG] Total Element %:", totalElementPercent, "Final type:", finalElementType)
if totalElementPercent > 0 and finalElementType ~= COMBAT_NONE then
local mainValue = math.abs(primaryDamage)
local elementalDamage = -math.floor(mainValue * totalElementPercent / 100)
print("[DEBUG] Base value:", mainValue, "Calculated elemental damage:", elementalDamage)
if target:isMonster() and monsterImmunities and monsterImmunities.isImmune then
if monsterImmunities.isImmune(target, finalElementType) then
print("[DEBUG] Immunity detected for final element:", finalElementType)
target:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT)
elementalDamage = 0
end
end
if secondaryType == finalElementType then
secondaryDamage = elementalDamage
else
secondaryType = finalElementType
secondaryDamage = (secondaryDamage < 0 and secondaryDamage or 0) + elementalDamage
end
end
print("[DEBUG] Final return:", primaryDamage, primaryType, secondaryDamage, secondaryType)
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
onManaChangeDamage:register()


