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

Some functions i made out of boredness

Scarlet Ayleid

Advanced OT User
Senator
Joined
Jul 7, 2007
Messages
4,049
Reaction score
238
Hey =)
I was bored today, so I've decided to do some small functions, some might be useful, other might be less useful, nevertheless, here they are =)

All tested on The Forgotten Server, version 0.3.5 (Crying Damson)

sendCancelMessage(cid,message,effect)
Dont you find it boring that whenever you want to send a cancel message with the poff effect having to time those 2 lines all the time? Now you can do it with only one and I've even added some extra customization =)
cid = Player that will recieve the message
message = Either the text the player recieves or the number of a default cancel message
effect = The number of the effect you want to use
Note: message and effect can be ignored or left at ""(check examples below)
Lua:
function sendCancelMessage(cid, message, effect)
    message = message or RETURNVALUE_NOTPOSSIBLE
 
    if(tonumber(message)) then
        doPlayerSendDefaultCancel(cid, math.max(1, math.min(63, tonumber(message))))
    elseif(#message > 0) then
        doPlayerSendCancel(cid, message)
    end
 
    doSendMagicEffect(getCreaturePosition(cid), math.max(1, math.min(75, effect or CONST_ME_POFF))))
    return true
end
With this function, you can use these in multiple ways:
sendCancelMessage(cid) - Will send the effect poff(white smoke) and the message "Sorry, not possible."
sendCancelMessage(cid,"Text") - Will send the effect poff(white smoke) and the message "Text"
sendCancelMessage(cid,41) - Will send the effect poff(white smoke) and the message "This action is not permitted on in a protection zone."
sendCancelMessage(cid,"",21) - Will send the yellow music effect and the message "Sorry, not possible."
sendCancelMessage(cid,"Text",21) - Will send the yellow music effect and the message "Text"
sendCancelMessage(cid,3,22) - Will send the purple music effect and the message "There is not enough room."

I think I did all the possible chances :p

getLevelbyExp(EXP)
This is a simple function, it'll return the level corresponding to the given Exp
Lua:
function getLevelbyExp(EXP)
    return math.floor((math.sqrt(3) * math.sqrt(243*(EXP+1)^2-48600*(EXP+1)+3680000)+27 * (EXP+1)-2700)^(1/3)/30^(2/3)-(5*10^(2/3))/(3^(1/3)*(math.sqrt(3)*math.sqrt(243*(EXP+1)^2-48600*(EXP+1)+3680000)+27*(EXP+1)-2700)^(1/3))+2)
end
(Formula for Level->Exp taken from TibiaWiki and reversed to give the Exp->Level with Wolfram(check Solutions for the variable x))
getLevelbyExp(102) - Would return 2
getLevelbyExp(8000) - Would return 8

isWalkable(cid,pos)
Just a quick function to see if it is possible to step on the specified tile =)
Lua:
function isWalkable(cid,pos)
    pos.stackpos = 253
    if doTileQueryAdd(cid, pos) == 1 and getTilePzInfo(pos) == FALSE and isCreature(getThingFromPos(pos).uid) == FALSE then
        return TRUE
    end
    return FALSE
end
It returns the distance effect based on the given weapon =)
getWeaponDistanceEffect(getPlayerWeapon(cid).uid) will return the effect based on the weapon the player is carrying =)

That's it for today people =)
Suggestions or Comments? Oh and dont forget to Rep++
 
Last edited:
At the first one, to create a POFF effect you could just enter -1 or 68 :p
 
if you want the poff effect simply dont write anything there

sendCancelMessage(cid,"TEXT")

he'll automatically use the poff effect if none is written =)
 
Code:
function isWalkable(cid, pos)
	pos.stackpos = 253
	if (doTileQueryAdd(cid, pos) == 1 and getTilePzInfo(pos) == FALSE and isCreature(getThingFromPos(pos).uid) == FALSE) then
		return TRUE
	end
	return FALSE
end

Code:
function sendCancelMessage(cid, message, effect)
	if (not message) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
		return true
	end
	if (not number) then
		number = CONST_ME_POFF
	end

	doPlayerSendCancel(cid, message)
	doSendMagicEffect(getCreaturePosition(cid), number)

	return true
end


getOppositeSidePos is too long I'm too lazy to check it.
 
the sendCancelMessage wont work

Lua:
function sendCancelMessage(cid, message, effect)
	if (not message) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
		return true --this shouldnt be here, otherwise no effect will happen
	end
	if (not number) then --its effect, not number
		number = CONST_ME_POFF --its effect, not number
	end

	doPlayerSendCancel(cid, message)
	doSendMagicEffect(getCreaturePosition(cid), number) --its effect, not number

	return true
end

also, this one wont work for default cancel messages
 
Ok, it's mistaken with variable number, i haven't seen in header it's effect, and and i put return there, cause if script use doPlayerSendCancel, console will display errors - message is empty. :p
 
PHP:
if message == "" or message == nil then
in my code prevents errors for no message and automatically return the not possible default error
 
Okay. I think finally it should look:
Code:
function sendCancelMessage(cid, message, effect)
	if (not message) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
	else
		doPlayerSendCancel(cid, message)
	end

	doSendMagicEffect(getCreaturePosition(cid), effect and effect or CONST_ME_POFF)
	return true
end
 
it wont work with default cancel messages and what if the effect is out of range? ie. 80, you arent checking that ;)

and on the doSendMagicEffect, there is no need for that and in your code :p
PHP:
doSendMagicEffect(getCreaturePosition(cid), effect or CONST_ME_POFF)
 
it wont work with default cancel messages and what if the effect is out of range? ie. 80, you arent checking that ;)

Just trying to write less code, but i think it's useless 'Cause many server owners don't writing own scripts, just copying others :p

@Edit:
lol, i miss 2 words in sentence ? o_O
 
think of it this way, people that really script themselves will find the variety of this function useful, while the so called "Noob Admins" will just as you said, copy code, so they wont even use this function unless another scripter uses it and if he does, he'll have taken care to make sure no errors are given :p
 
Ah, also scripters will not use this function for public scripts, because they want to scripts be compatible with defaul distro's functions :p
 
Lua:
function sendCancelMessage(cid, message, effect)
	message = message or RETURNVALUE_NOTPOSSIBLE

	if(tonumber(message)) then
		doPlayerSendDefaultCancel(cid, math.max(1, math.min(65, tonumber(message)))
	elseif(#message > 0) then
		doPlayerSendCancel(cid, message)
	end

	doSendMagicEffect(getCreaturePosition(cid), math.max(1, math.min(67, effect or CONST_ME_POFF)))
	return true
end

:p Nice ideas :)
 
Lua:
function sendCancelMessage(cid, message, effect)
	message = message or RETURNVALUE_NOTPOSSIBLE

	if(tonumber(message)) then
		doPlayerSendDefaultCancel(cid, math.max(1, math.min(65, tonumber(message)))
	elseif(#message > 0) then
		doPlayerSendCancel(cid, message)
	end

	doSendMagicEffect(getCreaturePosition(cid), math.max(1, math.min(67, effect or CONST_ME_POFF)))
	return true
end

:p Nice ideas :)

thanks and could you explain why using those math.min and math.max?
 
to make sure it wont go greater/less than that value.

math.max() returns biggest number, math.min() returns smallest number. That way if I use math.max(1, effect) it will mean if effect is 0 (or anything below 1) it will return 1, because it is the biggest. And math.min(67, effect) if effect is 80 it will return 67 because it take smallest number.

Combining them you can make min and max values. Also you could make:
Lua:
function limit(n, minv, maxv)
    return math.max(minv, math.min(maxv, n))
end

Which could be used as such:
Lua:
doSendMagicEffect(getCreaturePosition(cid), limit(effect or CONST_ME_POFF, 1, 67))

Would be a bit shorter (and easier to read) than this:
Lua:
doSendMagicEffect(getCreaturePosition(cid), math.max(1, math.min(67, effect or CONST_ME_POFF)))
 
to make sure it wont go greater/less than that value.

math.max() returns biggest number, math.min() returns smallest number. That way if I use math.max(1, effect) it will mean if effect is 0 (or anything below 1) it will return 1, because it is the biggest. And math.min(67, effect) if effect is 80 it will return 67 because it take smallest number.

Combining them you can make min and max values. Also you could make:
Lua:
function limit(n, minv, maxv)
    return math.max(minv, math.min(maxv, n))
end

Which could be used as such:
Lua:
doSendMagicEffect(getCreaturePosition(cid), limit(effect or CONST_ME_POFF, 1, 67))

Would be a bit shorter (and easier to read) than this:
Lua:
doSendMagicEffect(getCreaturePosition(cid), math.max(1, math.min(67, effect or CONST_ME_POFF)))

I get it now, nicely thought :) Makes total sense and its much better then making the ifs like I did
I've already learned something new today =)
 
Please helo i have this error in 50-fuction in lib

Lua:
[31/10/2012 14:17:02] [Error - LuaScriptInterface::loadFile] data/lib/050-function.lua:848: ')' expected (to close '(' at line 847) near 'elseif'
[31/10/2012 14:17:02] [Warning - LuaScriptInterface::initState] Cannot load data/lib/
 
Back
Top