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

CreatureEvent Monster Counter

Status
Not open for further replies.

Evil Hero

Legacy Member
TFS Developer
Joined
Dec 12, 2007
Messages
1,256
Solutions
28
Reaction score
728
Location
Germany
Hello Peoples,

Since I saw this requested by a guy I'll just release it here since it's nothing really hard to do.

but you need 2 certain functions to get it work.

Credits: Remere for the functions
Code:
_warpzone = 2147483648 -- start storing strings here (THIS IS THE ABSOLUTE MAXIMUM VALUE FOR THIS)
_maxlength = 1024 -- multiply by 3 to get the true length.
 
setPlayerStorageInteger = setPlayerStorageValue
getPlayerStorageInteger = getPlayerStorageValue
 
function setPlayerStorageString(cid, key, value)
    if #value > (_maxlength-1) * 3 - 1 then -- Last word is reserved for 0 termination of the string.
        error("Storage string is too long")
    end
    if key > _warpzone / _maxlength then
        error("Storage string key is too large (" .. key .. ")")
    end
    key = _warpzone + key * _maxlength
 
    local word = 0
    local wordwrap = 0
    local wordcount = 0
    local i = 1
    while i <= #value do
        local byte = string.byte(string.sub(value, i, i))
        word = bit.bor(word, bit.lshift(byte, wordwrap))
        wordwrap = wordwrap + 8
        if wordwrap == 24 then
            --[[
                In the ideal world we would be able to store 4 characters per word,
                however, as the default return value for getPlayerStorageValue is
                -1, we can't use the last bit.
            ]]--
            setPlayerStorageInteger(cid, key + wordcount, word)
            word = 0
            wordwrap = 0
            wordcount = wordcount + 1
        end
        i = i + 1
    end
    -- store the last word
    setPlayerStorageInteger(cid, key + wordcount, word)
end
 
function getPlayerStorageString(cid, key)
    if key > _warpzone / _maxlength then
        error("Storage string key is too large (" .. key .. ")")
    end
    key = _warpzone + key * _maxlength
 
    local wordcount = 0
    local str = ""
    while true do
        if wordcount >= _maxlength then
            break
        end
        local word = getPlayerStorageInteger(cid, key + wordcount)
        if word == -1 then
            -- end of string
            break
        else 
            -- Extract the 3 characters from the value
            byte = bit.band(word, 255)
            if byte == 0 then break else str = str .. string.char(byte) end
            byte = bit.rshift(bit.band(word, 65280), 8)
            if byte == 0 then break else str = str .. string.char(byte) end
            byte = bit.rshift(bit.band(word, 16711680), 16)
            if byte == 0 then break else str = str .. string.char(byte) end
        end
        wordcount = wordcount + 1
    end
    return str
end
Copy paste those at the bottom of your functions/global.lua

Here is the main Script

Code:
-- Monster Counter
-- Made by Evil Hero, thanks to Remere for the functions
function onKill(cid, target)
local monster = getCreatureName(target)
local killed = getPlayerStorageString(cid,monster)
	if isMonster(target) == TRUE then
		if getPlayerStorageString(cid,monster) < 1 then
			setPlayerStorageString(cid,monster,1)
			doPlayerSendTextMessage(cid,22,"Congratulations, You killed your first ".. monster ..".")
		else
			setPlayerStorageString(cid,monster,killed + 1)
			doPlayerSendTextMessage(cid,22,"You have killed ".. killed .." ".. monster .."s.")
		end
	end
	return TRUE
end

this is what you have to add into creaturescripts.xml

Code:
<event type="kill" name="Monster Counter" script="monstercounter.lua"/>

This one goes into login.lua

Code:
registerCreatureEvent(cid, "Monster Counter")


If there are any Questions, feel free to ask.


kind regards, Evil Hero
 
Last edited:
This is my script:

Code:
function onKill(cid, target)
	if isPlayer(cid) == TRUE and isCreature(target) == TRUE then
		local monsters_killed = getPlayerStorageValue(cid, getCreatureName(target))
		setPlayerStorageValue(cid, getCreatureName(target), monsters_killed+1)
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have killed " .. getPlayerStorageValue(cid, getCreatureName(target))+1 .. " monsters named: " .. string.lower(getCreatureName(target)) .. ".")
	end
end

:D
 
data/global.lua:714: attempt to compare number with string
stack traceback:
data/global.lua:714: in function 'getPlayerStorageString'
data/creaturescripts/scripts/monsterdeath.lua:3: in function'

Help! TFS 0.2pl23!
 
data/global.lua:714: attempt to compare number with string
stack traceback:
data/global.lua:714: in function 'getPlayerStorageString'
data/creaturescripts/scripts/monsterdeath.lua:3: in function'
Help! TFS 0.2pl23!
Evil Hero, example of use setPlayerStorageString is setPlayerStorageString(cid, value, string), not setPlayerStorageString(cid, string, value).
 
im using TFS 0.3.1 And there is no global.lua

when i paste it to functions.lua i got error when kill monster.
 
Last edited:
i have this wrong in engin tfs 0.3.1
PHP:
[16/02/2009 21:41:36] Lua Script Error: [CreatureScript Interface] 
[16/02/2009 21:41:36] data/creaturescripts/scripts/monster.lua:onKill

[16/02/2009 21:41:36] data/lib/constant.lua:704: attempt to compare number with string
[16/02/2009 21:41:36] stack traceback:
[16/02/2009 21:41:36] 	data/lib/constant.lua:704: in function 'getPlayerStorageString'
[16/02/2009 21:41:36] 	data/creaturescripts/scripts/monster.lua:5: in function <data/creaturescripts/scripts/monster.lua:3>
 
i have this wrong in engin tfs 0.3.1
PHP:
[16/02/2009 21:41:36] Lua Script Error: [CreatureScript Interface] 
[16/02/2009 21:41:36] data/creaturescripts/scripts/monster.lua:onKill

[16/02/2009 21:41:36] data/lib/constant.lua:704: attempt to compare number with string
[16/02/2009 21:41:36] stack traceback:
[16/02/2009 21:41:36] 	data/lib/constant.lua:704: in function 'getPlayerStorageString'
[16/02/2009 21:41:36] 	data/creaturescripts/scripts/monster.lua:5: in function <data/creaturescripts/scripts/monster.lua:3>

i have same error :S
 
This is my script:

Code:
function onKill(cid, target)
	if isPlayer(cid) == TRUE and isCreature(target) == TRUE then
		local monsters_killed = getPlayerStorageValue(cid, getCreatureName(target))
		setPlayerStorageValue(cid, getCreatureName(target), monsters_killed+1)
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have killed " .. getPlayerStorageValue(cid, getCreatureName(target))+1 .. " monsters named: " .. string.lower(getCreatureName(target)) .. ".")
	end
end

:D
when i use this script monster die but this mayby wronk
13:51 You have killed 10 monsters named: rotworm.
13:52 You have killed 11 monsters named: rotworm.
13:55 You have killed 12 monsters named: bat.
Why are not many monsters one at a time.
 
i have error in function 'getPlayerStorageString'
 
Sorry people, totaly forgot about this, yea got informed that the function doesn't work in the way that I thought it was...

I'll try to update it later...


kind regards, Evil Hero
 
Status
Not open for further replies.
Back
Top