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

C++ Creature CastSpell() right way

zxmatzx

Advanced OT User
Joined
Dec 1, 2010
Messages
311
Solutions
27
Reaction score
154
Location
Brazil
GitHub
Mateuso8
Hello,
I have this function to do Creatures cast spells, but isn't working in right way, if a monster(summon) cast a spell, the spell is damaging the player(master), if hit another player, the master don't get Skull, the master and the player that recive damage don't get Pz Locked. After search about i found this post where Delusion send a function to do Players cast spell, i noticed that he use another function to call the spell, and looking in this function all combat operations happens there, i want some help to do my function work in right way or adapt Delusion's function to work with Creatures(players, npcs and monsters), here is my function:

Luascript.cpp
C++:
registerMethod("Creature", "castSpell", LuaScriptInterface::luaCreatureCastSpell);
and
C++:
int LuaScriptInterface::luaCreatureCastSpell(lua_State* L)
{
    //creature:castSpell("name")
    const std::string& spellName = getString(L, 2);

    if(Creature* creature = getUserdata<Creature>(L, 1))
    {
        InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
        if(!spell)
        {
            lua_pushboolean(L, false);
            return 1;
        }
        
        Creature* target = creature->getAttackedCreature();

        if (spell->getNeedTarget() == true)
        {
            if (target)
            {
                //TODO -> change to !canThrowSpell and only break returning false
                if (spell->canThrowSpell(creature, target) == true)
                {
                    spell->castSpell(creature, target);
                    lua_pushboolean(L, true);
                    return 1;
                }
                else
                {
                    lua_pushboolean(L, false);
                    return 1;
                }
            }
            else
            {
                lua_pushboolean(L, false);
                return 1;
            }
        }
        spell->castSpell(creature, creature);

        lua_pushboolean(L, true);
    }
    else
    {
        lua_pushboolean(L, false);
    }
    return 1;
}
LuaScript.h
C++:
static int luaCreatureCastSpell(lua_State* L);

Im using TFS 1.2, i guess change Delusion's function is better, because it already have all code to handler all things that i need.
Thanks!
 
Back
Top