• 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 0.X spell remove atk mode to everybody who is attacking me

Solution
Sorry i have changed variable name, but didn't update this line.
It should be:
lua_pushboolean(L, attackedCreatureChanged);

And as I can see I missed the semicolon in this line:
Player *player = creature->getPlayer()
should be:
Player *player = creature->getPlayer();





and did u have any example of script using getSpectators?
Unfortunately not :) But I am sure that there's a lot of them here.
You can use the following function to get all creatures able to see a specific position.
getSpectators(centerPos, rangex, rangey[, multifloor = false])

Then iterate over each of these creatures and check if it is attacking the caster:
getCreatureTarget(cid)

And if any creature is targeting your caster then simply change their's target.
voilà


To do target changing you probably have to add a new lua function because I cannot find any "setPlayerTarget"-like method.
You can do this in a similar way to function doMonsterSetTarget:

Didn't test it but should work ;) (I called it doCreatureSetTarget, not so creative :D):
Lua:
int32_t LuaInterface::luaDoCreatureSetTarget(lua_State* L)
{
    //doCreatureSetTarget(cid, target)
    uint32_t targetId = popNumber(L);
    ScriptEnviroment* env = getEnv();

    Creature* creature = env->getCreatureByUID(popNumber(L));
    if(!creature)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    Creature* target = env->getCreatureByUID(targetId);
    if(!target && targetId != 0)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    lua_pushboolean(L, creature->setAttackedCreature(target));

    return 1;
}

Of course, you have to register such function somewhere here:

and add it's definition to the header file somewhere here:

Providing 0 as a target creature should stop attacker from targeting currently attacked creature.
doCreatureSetTarget(attacker, 0)
 
@esigma94 ty you so much!

i tried:
luascript.cpp
Code:
lua_register(m_luaState, "doCreatureSetTarget", LuaInterface::luaDoCreatureSetTarget);


Code:
int32_t LuaInterface::luaDoCreatureSetTarget(lua_State* L)
{
    //doCreatureSetTarget(cid, target)
    uint32_t targetId = popNumber(L);
    ScriptEnviroment* env = getEnv();

    Creature* creature = env->getCreatureByUID(popNumber(L));
    if(!creature)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    Creature* target = env->getCreatureByUID(targetId);
    if(!target && targetId != 0)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    lua_pushboolean(L, creature->setAttackedCreature(target));

    return 1;
}

luascript.h
Code:
static int32_t luaDoCreatureSetTarget(lua_State* L);

then on spell i tried:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_HEALING)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
setCombatParam(combat, COMBAT_PARAM_AGGRESSIVE, false)

function onCastSpell(cid, var)
    doCreatureSetTarget(cid, 0)
    return doCombat(cid, combat, var)
end

no errors, but didn't remove player target, it shouldn't work?
 
I think so. Maybe just red square suggesting that you are attacking a specific target (but you are not anymore) remains.

try to add sendCancelTarget() to lua method if target is null and creature is player. Like this:
Lua:
int32_t LuaInterface::luaDoCreatureSetTarget(lua_State* L)
{
    //doCreatureSetTarget(cid, target)
    uint32_t targetId = popNumber(L);
    ScriptEnviroment* env = getEnv();

    Creature* creature = env->getCreatureByUID(popNumber(L));
    if(!creature)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    Creature* target = env->getCreatureByUID(targetId);
    if(!target && targetId != 0)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    bool attackedCreatureChanged = creature->setAttackedCreature(target);
    
    // Remove red square in client if setting target to NULL
    Player *player = creature->getPlayer()
    if(player != NULL && targetId == 0 && attackedCreatureChanged) {
        player->sendCancelTarget();
    }
    
    lua_pushboolean(L, result);

    return 1;
}
 
I think so. Maybe just red square suggesting that you are attacking a specific target (but you are not anymore) remains.

try to add sendCancelTarget() to lua method if target is null and creature is player. Like this:
Lua:
int32_t LuaInterface::luaDoCreatureSetTarget(lua_State* L)
{
    //doCreatureSetTarget(cid, target)
    uint32_t targetId = popNumber(L);
    ScriptEnviroment* env = getEnv();

    Creature* creature = env->getCreatureByUID(popNumber(L));
    if(!creature)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    Creature* target = env->getCreatureByUID(targetId);
    if(!target && targetId != 0)
    {
        errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
        lua_pushboolean(L, false);
        return 1;
    }

    bool attackedCreatureChanged = creature->setAttackedCreature(target);
   
    // Remove red square in client if setting target to NULL
    Player *player = creature->getPlayer()
    if(player != NULL && targetId == 0 && attackedCreatureChanged) {
        player->sendCancelTarget();
    }
   
    lua_pushboolean(L, result);

    return 1;
}

Code:
luascript.cpp:6922:24: error: ‘result’ was not declared in this scope
     lua_pushboolean(L, result);

what is this result?
lua_pushboolean(L, result);
Post automatically merged:

and did u have any example of script using getSpectators?
 
Sorry i have changed variable name, but didn't update this line.
It should be:
lua_pushboolean(L, attackedCreatureChanged);

And as I can see I missed the semicolon in this line:
Player *player = creature->getPlayer()
should be:
Player *player = creature->getPlayer();





and did u have any example of script using getSpectators?
Unfortunately not :) But I am sure that there's a lot of them here.
 
Solution
remove target is working :) TY

every single example of getSpectators i saw using this function was 1.0+ with that
Code:
player:
code

did u know how to get that player variable i'm trying to use here:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_HEALING)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
setCombatParam(combat, COMBAT_PARAM_AGGRESSIVE, false)

function onCastSpell(cid, var)
    doCreatureSetTarget(cid, 0)
    local spectators = getSpectators(getPlayerPosition(cid), 15, 15)
    if #spectators > 0 then
        doCreatureSetTarget(player, 0)
    end
    return doCombat(cid, combat, var)
end

?
 
Back
Top