[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]