• 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 prevent summon attacks

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
What I'm trying to do: prevent spells from my summon from affecting a player I did not attack or affect (take pk) if I'm not on secureMode.
I mean, if player are with secureMode/have a summon/his summon attack one monster, the spell will shoot the target:isPlayer() but like non-pvp (no damage)

tfs 1.3
current script:

Lua:
    local Master = self:getMaster()
    if Master then
        if Master:isPlayer() then
            if (self:getName():lower() == 'monk vip') then
                if target and target:isPlayer() then
                    -- no works here cuz my target is monster
                    if self:getMaster():hasSecureMode() then
                        return RETURNVALUE_TURNSECUREMODETOATTACKUNMARKEDPLAYERS
                    end
                end
            end
        end
    end

cksX5BN.png
 
Last edited by a moderator:
Solution
remember to check in your "luascript.cpp" if you have this function Player.hasSecureMode()
Code:
local master = self:getMaster()
    if master and master:isPlayer() and target and target:isPlayer() and master:hasSecureMode() then
        return RETURNVALUE_NOTPOSSIBLE
    end
me like please :eek:
Edit for ( and target )
remember to check in your "luascript.cpp" if you have this function Player.hasSecureMode()
Code:
local master = self:getMaster()
    if master and master:isPlayer() and target and target:isPlayer() and master:hasSecureMode() then
        return RETURNVALUE_NOTPOSSIBLE
    end
me like please :eek:
Edit for ( and target )
 
Last edited:
Solution
remember to check in your "luascript.cpp" if you have this function Player.hasSecureMode()
Code:
local master = self:getMaster()
    if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
        return RETURNVALUE_NOTPOSSIBLE
    end
me like please :eek:

thanks but the same problem are happening, you did the same code only in diff order and not checking the self name o/
 
in TFS 1.X+ it works perfectly.
attack ( melee or expansive spell ) is equal, i use it like this on my server

goWuyQNlQIyuoYSg_WBFIg.png
7AS9ZvCUSu6reQnQH5CTjg.png
hm? I dont understand this picture, who is attacking the monk? I didn't see any summon attacking, I see you (player attacking), please tell me
 
aiMiSWbOQTKpWBAXK4GWhw.png

Code:
ReturnValue Events::eventCreatureOnTargetCombat(Creature* creature, Creature* target)
{
    // Creature:onTargetCombat(target) or Creature.onTargetCombat(self, target)
    if (creatureOnTargetCombat == -1) {
        return RETURNVALUE_NOERROR;
    }

    if (!scriptInterface.reserveScriptEnv()) {
        std::cout << "[Error - Events::eventCreatureOnTargetCombat] Call stack overflow" << std::endl;
        return RETURNVALUE_NOTPOSSIBLE;
    }

    ScriptEnvironment* env = scriptInterface.getScriptEnv();
    env->setScriptId(creatureOnTargetCombat, &scriptInterface);

    lua_State* L = scriptInterface.getLuaState();
    scriptInterface.pushFunction(creatureOnTargetCombat);

    if (creature) {
        LuaScriptInterface::pushUserdata<Creature>(L, creature);
        LuaScriptInterface::setCreatureMetatable(L, -1, creature);
    } else {
        lua_pushnil(L);
    }

    LuaScriptInterface::pushUserdata<Creature>(L, target);
    LuaScriptInterface::setCreatureMetatable(L, -1, target);

    ReturnValue returnValue;
    if (scriptInterface.protectedCall(L, 2, 1) != 0) {
        returnValue = RETURNVALUE_NOTPOSSIBLE;
        LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
    } else {
        returnValue = LuaScriptInterface::getNumber<ReturnValue>(L, -1);
        lua_pop(L, 1);
    }

    scriptInterface.resetScriptEnv();
    return returnValue;
}
 
it does not give me problems, in all cases, however, it can be used as well:
remember to check in your "luascript.cpp" if you have this function Player.hasSecureMode()
Code:
local master = self:getMaster()
    if master and master:isPlayer() and target and target:isPlayer() and master:hasSecureMode() then
        return RETURNVALUE_NOTPOSSIBLE
    end
me like please :eek:
Edit for ( and target )

Code:
[LIST=1]
[*]if (creature) {
[*]        LuaScriptInterface::pushUserdata<Creature>(L, creature);
[*]        LuaScriptInterface::setCreatureMetatable(L, -1, creature);
[*]    } else {
[*]        lua_pushnil(L);
[*]    }
[/LIST]
This has never been taken into account, on my case.
 
full script, any error? I didn't see:
Lua:
function Creature:onTargetCombat(target)
    if not self then
        return true
    end
    if not __picif[target.uid] then
        if target:isMonster() then
            target:registerEvent("RewardSystemSlogan")
            __picif[target.uid] = {}
        end
    end
    local targetSummer = self:getTarget()
    if self:isPlayer() then
        if self:getStorageValue(923013) == 1 and Game.getStorageValue(923012) == 1 then
            if target:getName() == staminaBonusASS.target1 or target:getName() == staminaBonusASS.target2 or target:getName() == staminaBonusASS.target3 or target:getName() == staminaBonusASS.target4 then
                return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE               
            end
        end
    end  
    if self:isPlayer() then
        if target and target:getName() == staminaBonus.target then
            local name = self:getName()
            if not staminaBonus.events[name] then
                staminaBonus.events[name] = addEvent(addStamina, staminaBonus.period, name)
            end
        end
    end
  
    if self:isPlayer() then
        if target and target:getName() == staminaBonusCoh.targetCoh then
            local name = self:getName()
            if not staminaBonusCoh.eventsCoh[name] then
                staminaBonusCoh.eventsCoh[name] = addEvent(addStaminaCoh, staminaBonusCoh.periodCoh, name)
            end
        end
    end
  
    if (not target) then
        return true
    end  
  
    local master = self:getMaster()
    if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
        return RETURNVALUE_NOTPOSSIBLE
    end  
  
    return true
end
 
@God Of Pain when we write scripts or if we don't if the script isn't performing as it should a real simple way to trouble-shoot the script is to use print on the values at different stages so we can see why it isn't working. I think this is something you should start learning how to do instead of waiting on someone for the answer.

Eventually after doing this enough you will learn how to program because you will force yourself to read up on what is what and want to change the values on your own.
For instance:
Lua:
function Creature:onTargetCombat(target)
    if not self then
        print('not self')
        return true
    end
    if not __picif[target.uid] then
        print('not picif', target.uid)
        if target:isMonster() then
            print('target is monster')
            target:registerEvent("RewardSystemSlogan")
            __picif[target.uid] = {}
        end
    end
    local targetSummer = self:getTarget()
    if self:isPlayer() then
        print('self is player')
        if self:getStorageValue(923013) == 1 and Game.getStorageValue(923012) == 1 then
            print('storage equals 1')
            if target:getName() == staminaBonusASS.target1 or target:getName() == staminaBonusASS.target2 or target:getName() == staminaBonusASS.target3 or target:getName() == staminaBonusASS.target4 then
                print('you may not attack this creature')
                return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE           
            end
        end
    end
    if self:isPlayer() then
        print('self is player')
        if target and target:getName() == staminaBonus.target then
            print(target:getName(), 'stamina bonus')
            local name = self:getName()
            if not staminaBonus.events[name] then
                print(target:getName(), ' not stamina bonus')
                staminaBonus.events[name] = addEvent(addStamina, staminaBonus.period, name)
            end
        end
    end
    if self:isPlayer() then
        print('is player')
        if target and target:getName() == staminaBonusCoh.targetCoh then
            print(target:getName(), 'stamina bonus coh')
            local name = self:getName()
            if not staminaBonusCoh.eventsCoh[name] then
                print(target:getName(), 'not stamina bonus coh')
                staminaBonusCoh.eventsCoh[name] = addEvent(addStaminaCoh, staminaBonusCoh.periodCoh, name)
            end
        end
    end
    if (not target) then
        print('not target')
        return true
    end
    local master = self:getMaster()
    if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
        print('master not possible')
        return RETURNVALUE_NOTPOSSIBLE
    end
    return true
end
See how I used print underneath all the if statements?
 
@God Of Pain when we write scripts or if we don't if the script isn't performing as it should a real simple way to trouble-shoot the script is to use print on the values at different stages so we can see why it isn't working. I think this is something you should start learning how to do instead of waiting on someone for the answer.

Eventually after doing this enough you will learn how to program because you will force yourself to read up on what is what and want to change the values on your own.
For instance:
Lua:
function Creature:onTargetCombat(target)
    if not self then
        print('not self')
        return true
    end
    if not __picif[target.uid] then
        print('not picif', target.uid)
        if target:isMonster() then
            print('target is monster')
            target:registerEvent("RewardSystemSlogan")
            __picif[target.uid] = {}
        end
    end
    local targetSummer = self:getTarget()
    if self:isPlayer() then
        print('self is player')
        if self:getStorageValue(923013) == 1 and Game.getStorageValue(923012) == 1 then
            print('storage equals 1')
            if target:getName() == staminaBonusASS.target1 or target:getName() == staminaBonusASS.target2 or target:getName() == staminaBonusASS.target3 or target:getName() == staminaBonusASS.target4 then
                print('you may not attack this creature')
                return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE        
            end
        end
    end
    if self:isPlayer() then
        print('self is player')
        if target and target:getName() == staminaBonus.target then
            print(target:getName(), 'stamina bonus')
            local name = self:getName()
            if not staminaBonus.events[name] then
                print(target:getName(), ' not stamina bonus')
                staminaBonus.events[name] = addEvent(addStamina, staminaBonus.period, name)
            end
        end
    end
    if self:isPlayer() then
        print('is player')
        if target and target:getName() == staminaBonusCoh.targetCoh then
            print(target:getName(), 'stamina bonus coh')
            local name = self:getName()
            if not staminaBonusCoh.eventsCoh[name] then
                print(target:getName(), 'not stamina bonus coh')
                staminaBonusCoh.eventsCoh[name] = addEvent(addStaminaCoh, staminaBonusCoh.periodCoh, name)
            end
        end
    end
    if (not target) then
        print('not target')
        return true
    end
    local master = self:getMaster()
    if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
        print('master not possible')
        return RETURNVALUE_NOTPOSSIBLE
    end
    return true
end
See how I used print underneath all the if statements?

The result would obviously be: it will not print with the function in which we think we should print/work.
So, for what is the prints?

Edit: this print('master not possible') not print in this case
but thanks for your answer
 
Did u have the same problem? :S I still not fixed too
Try:
events/scripts/creature.lua
Lua:
function Creature:onTargetCombat(target)
    if not self then
        return RETURNVALUE_NOERROR
    end
    local master = self:getMaster()
    if master and master == target then
        return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
    end
    if self:isPlayer() and target:isPlayer() then
        local party = self:getParty()
        if party then
            local targetParty = target:getParty()
            if targetParty and targetParty == party then
                return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
            end
        end
    end
    return RETURNVALUE_NOERROR
end
 
Try:
events/scripts/creature.lua
Lua:
function Creature:onTargetCombat(target)
    if not self then
        return RETURNVALUE_NOERROR
    end
    local master = self:getMaster()
    if master and master == target then
        return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
    end
    if self:isPlayer() and target:isPlayer() then
        local party = self:getParty()
        if party then
            local targetParty = target:getParty()
            if targetParty and targetParty == party then
                return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
            end
        end
    end
    return RETURNVALUE_NOERROR
end

same, but u solved with this?
 
Back
Top