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

Lua Function Player can/can not use spells/runes

VirrageS

←•†ĿuĀ && ©¤¤•→
Joined
May 30, 2010
Messages
984
Reaction score
63
Location
Poland
[LUA FUNCTION] Can/can not cast spell/runes



Description:
This function set for player that can/can not use spells/runes.
Simple but usefull function.


Example:

spells.xml
XML:
	<instant name="Silence" words="silence" lvl="40" mana="500" range="5" needtarget="1" blockwalls="1" exhaustion="10000" needlearn="0" event="script" value="silence.lua">
		<vocation id="1"/>
		<vocation id="2"/>
		<vocation id="5"/>
		<vocation id="6"/>
	</instant>

silence.lua
Lua:
local config = {
	silenceTime = 5000 -- in miliseconds
}

function onCastSpell(cid, var)
	local target = variantToNumber(var)

	doPlayerSetNoCastSpell(target, true) -- NEW function
	doSendMagicEffect(getThingPos(target), CONST_ME_MAGIC_BLUE)
	doSendAnimatedText(getThingPos(target), 'Silenced', COLOR_RED)
	doPlayerSendCancel(target, 'You can not use spells and runes for ' .. config.silenceTime / 1000 .. ' seconds.')

	addEvent(canCastSpell, config.silenceTime, target)
	return true
end

function canCastSpell(cid)
	if isPlayer(cid) then
		doPlayerSetNoCastSpell(cid, false) -- NEW function
	end
end

Warning:
If you don't know what you are doing, don't change anything and just leave from this theard.

Test:
I tested it on TFS 0.4 trunk 3884 (it should work on better versions).




Edit:


spells.cpp

Below:
[cpp]
bool Spell::checkSpell(Player* player) const
{
if(player->hasFlag(PlayerFlag_CannotUseSpells))
return false;
[/cpp]

Paste:
[cpp]
if(player->getNoCastSpell())
return false;
[/cpp]



Below:
[cpp]
bool InstantSpell::canCast(const Player* player) const
{
if(player->hasFlag(PlayerFlag_CannotUseSpells))
return false;
[/cpp]

Paste:
[cpp]
if(player->getNoCastSpell())
return false;
[/cpp]



Below:
[cpp]
ReturnValue RuneSpell::canExecuteAction(const Player* player, const Position& toPos)
{
if(player->hasFlag(PlayerFlag_CannotUseSpells))
return RET_CANNOTUSETHISOBJECT;
[/cpp]

Paste:
[cpp]
if(player->getNoCastSpell())
return RET_CANNOTUSETHISOBJECT;
[/cpp]


luascript.cpp

Below:
[cpp]
void LuaInterface::registerFunctions()
{
//example(...)
//lua_register(L, "name", C_function);
[/cpp]

Paste:
[cpp]
//doPlayerSetNoCastSpell(cid, block)
lua_register(m_luaState, "doPlayerSetNoCastSpell", LuaInterface::luaDoPlayerSetNoCastSpell);

//getPlayerNoCastSpell(cid)
lua_register(m_luaState, "getPlayerNoCastSpell", LuaInterface::luaGetPlayerNoCastSpell);
[/cpp]



Below:
[cpp]
int32_t LuaInterface::luaGetPlayerTradeState(lua_State* L)
{
return internalGetPlayerInfo(L, PlayerInfoTradeState);
}
[/cpp]

Paste:
[cpp]
int32_t LuaInterface::luaDoPlayerSetNoCastSpell(lua_State* L)
{
//doPlayerSetNoCastSpell(cid, block)
bool block = popNumber(L);

ScriptEnviroment* env = getEnv();
if(Player* player = env->getPlayerByUID(popNumber(L)))
{
player->setNoCastSpell(block);
lua_pushboolean(L, true);
}
else
{
errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushboolean(L, false);
}

return 1;
}

int32_t LuaInterface::luaGetPlayerNoCastSpell(lua_State* L)
{
//getPlayerNoCastSpell(cid)
ScriptEnviroment* env = getEnv();
if(Player* player = env->getPlayerByUID(popNumber(L)))
lua_pushboolean(L, player->getNoCastSpell());
else
{
errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushboolean(L, false);
}

return 1;
}
[/cpp]


luascript.h

Below:
[cpp]
//lua functions
[/cpp]

Paste:
[cpp]
static int32_t luaDoPlayerSetNoCastSpell(lua_State* L);
static int32_t luaGetPlayerNoCastSpell(lua_State* L);
[/cpp]


player.cpp

Below:
[cpp]
bool Player::hasLearnedInstantSpell(const std::string& name) const
{
if(hasFlag(PlayerFlag_CannotUseSpells))
return false;
[/cpp]

Paste:
[cpp]
if(getNoCastSpell())
return false;
[/cpp]



Below:
[cpp]
pzLocked = isConnecting = addAttackSkillPoint = requestedOutfit = false;
saving = true;
[/cpp]

Paste:
[cpp]
cannotCastSpell = false;
[/cpp]


player.h

Below:
[cpp]
//combat functions
[/cpp]

Paste:
[cpp]
void setNoCastSpell(bool _cannotCastSpell)
{
cannotCastSpell = _cannotCastSpell;
}
bool getNoCastSpell() const {return cannotCastSpell;}
[/cpp]



Below:
[cpp]
bool outfitAttributes;
bool addAttackSkillPoint;
[/cpp]

Paste:
[cpp]
bool cannotCastSpell;
[/cpp]
 
@up: yeah, by muting player or adding storage check in every spell

condition would be even better, no need to addEvent :D
 
i think this can be done in pure lua, but thanks anyway :)

Yes of course. It can be done in onCast event and return false. But I think it is more easier to use it on function, because no need to make a new event and check storages always when you want to make a simple spell or other script.
 
Back
Top