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

TFS 1.X+ Deathlist damage map

E

Evil Puncker

Guest
Hi everyone, I have a death broadcast script but the damageMap part seems to not be working as it should, even if there is more than 1 killer the message is still "was killed by" instead of slain crushed etc, how can I fix it? also any optimization and/or tips, are welcome :) using latest TFS 1.3

Lua:
function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local damageMap = creature:getDamageMap()
    local str = creature:getName().." ["..creature:getLevel().."]"

    if(#damageMap <= 1) then
        str = str.." was killed by "
    elseif(#damageMap > 1 and #damageMap <= 4) then
        str = str.." was slain by "
    elseif(#damageMap > 4 and #damageMap <= 7) then
        str = str.." was crushed by "
    elseif(#damageMap > 7 and #damageMap <= 10) then
        str = str.." was eliminated by "
    elseif(#damageMap > 10) then
        str = str.." was annihilated by "
    end

    local i = 0
    for pid, _ in pairs(damageMap) do
        i = i + 1

        if (i == #damageMap) then
            str = str.." and "
        elseif (i ~= 1) then
            str = str..", "
        end

        if not(isPlayer(pid) or isMonster(pid)) then
            str = str.."a field item"
        elseif isSummon(pid) then
            str = str.."a "..getCreatureName(pid):lower().." summoned by "..(isPlayer(getCreatureMaster(pid)) and "" or "a ")..""..getCreatureName(getCreatureMaster(pid))
        elseif isPlayer(pid) then
            str = str..""..getCreatureName(pid)
        elseif isMonster(pid) then
            str = str.."a "..getCreatureName(pid):lower()
        end
    end

    str = str.."."
    --Game.broadcastMessage(str, MESSAGE_STATUS_WARNING)
    sendChannelMessage(9, TALKTYPE_CHANNEL_R1, str)
    print(str)
    return true
end
 
Solution
Because the damage map is unordered, the # operator does not work properly on tables that aren't indexed in order. Use a function such as this to determine the real size of a table:
Lua:
function table.size(t)
    local size = 0
    for k, v in pairs(t) do
        size = size + 1
    end
    return size
end
debug it with this and kill someone with 2+
print(#damageMap)

check this:
 
Last edited:
Because the damage map is unordered, the # operator does not work properly on tables that aren't indexed in order. Use a function such as this to determine the real size of a table:
Lua:
function table.size(t)
    local size = 0
    for k, v in pairs(t) do
        size = size + 1
    end
    return size
end
 
Solution
debug it with this and kill someone with 2+
print(#damageMap)

check this:

it is printing 0

Lucas has logged in.
0
Lucas [87] was killed by a ghoul, a ghoul, a ghoul, a hero, a ghoul, a ghoul, a hero, a ghoul, a ghoul, a ghoul.
Lucas has logged out.


thanks @Delusion as always

Lucas [85] was crushed by a ghoul, a ghoul, a ghoul, a ghoul, a ghoul.
 
Last edited by a moderator:
@Evil Puncker hi! sorry for revive this thread, but I would like to know how is the result of the script with Infernum's solution. Thanks in advance!
Something like that:
Code:
function realTableSize(t)
    local size = 0
    for k, v in pairs(t) do
        size = size + 1
    end
    return size
end

function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    local damageMap = creature:getDamageMap()
    local str = creature:getName().." ["..creature:getLevel().."]"

    local damageMapSize = realTableSize(damageMap)

    if(damageMapSize <= 1) then
        str = str.." was killed by "
    elseif(damageMapSize > 1 and damageMapSize <= 4) then
        str = str.." was slain by "
    elseif(damageMapSize > 4 and damageMapSize <= 7) then
        str = str.." was crushed by "
    elseif(damageMapSize > 7 and damageMapSize <= 10) then
        str = str.." was eliminated by "
    elseif(damageMapSize > 10) then
        str = str.." was annihilated by "
    end

    local i = 0
    for pid, _ in pairs(damageMap) do
        i = i + 1

        if (i == damageMapSize) then
            str = str.." and "
        elseif (i ~= 1) then
            str = str..", "
        end

        if not(isPlayer(pid) or isMonster(pid)) then
            str = str.."a field item"
        elseif isSummon(pid) then
            str = str.."a "..getCreatureName(pid):lower().." summoned by "..(isPlayer(getCreatureMaster(pid)) and "" or "a ")..""..getCreatureName(getCreatureMaster(pid))
        elseif isPlayer(pid) then
            str = str..""..getCreatureName(pid)
        elseif isMonster(pid) then
            str = str.."a "..getCreatureName(pid):lower()
        end
    end

    str = str.."."
    --Game.broadcastMessage(str, MESSAGE_STATUS_WARNING)
    sendChannelMessage(9, TALKTYPE_CHANNEL_R1, str)
    print(str)
    return true
end
 
Back
Top