Lua prevent summon attacks

Discussion in 'Support' started by God Of Pain, Jun 8, 2018.

  1. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    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:

    Code (Lua):
    1.     local Master = self:getMaster()
    2.     if Master then
    3.         if Master:isPlayer() then
    4.             if (self:getName():lower() == 'monk vip') then
    5.                 if target and target:isPlayer() then
    6.                     -- no works here cuz my target is monster
    7.                     if self:getMaster():hasSecureMode() then
    8.                         return RETURNVALUE_TURNSECUREMODETOATTACKUNMARKEDPLAYERS
    9.                     end
    10.                 end
    11.             end
    12.         end
    13.     end
    [​IMG]
     
    Last edited: Jun 8, 2018
  2. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    235
    Likes Received:
    93
    Best Answers:
    14
    remember to check in your "luascript.cpp" if you have this function Player.hasSecureMode()
    Code (Text):
    1.  
    2. local master = self:getMaster()
    3.     if master and master:isPlayer() and target and target:isPlayer() and master:hasSecureMode() then
    4.         return RETURNVALUE_NOTPOSSIBLE
    5.     end
    me like please :eek:
    Edit for ( and target )
     
    Last edited: Jun 9, 2018
    bayview and God Of Pain like this.
  3. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    thanks but the same problem are happening, you did the same code only in diff order and not checking the self name o/
     
  4. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    235
    Likes Received:
    93
    Best Answers:
    14
    in TFS 1.X+ it works perfectly.
    attack ( melee or expansive spell ) is equal, i use it like this on my server

    [​IMG][​IMG]
     
    Last edited: Jun 8, 2018
  5. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    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
     
  6. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    235
    Likes Received:
    93
    Best Answers:
    14
    [​IMG]
    the summon not casue damage in player with secureMode
     
    God Of Pain likes this.
  7. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    then my sourcer or something are bugged (here still not working), can you provide your function ?
    Code (Lua):
    1. function Creature:onTargetCombat(target)
     
  8. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    235
    Likes Received:
    93
    Best Answers:
    14
    [​IMG]
    Code (Text):
    1.  
    2. ReturnValue Events::eventCreatureOnTargetCombat(Creature* creature, Creature* target)
    3. {
    4.     // Creature:onTargetCombat(target) or Creature.onTargetCombat(self, target)
    5.     if (creatureOnTargetCombat == -1) {
    6.         return RETURNVALUE_NOERROR;
    7.     }
    8.  
    9.     if (!scriptInterface.reserveScriptEnv()) {
    10.         std::cout << "[Error - Events::eventCreatureOnTargetCombat] Call stack overflow" << std::endl;
    11.         return RETURNVALUE_NOTPOSSIBLE;
    12.     }
    13.  
    14.     ScriptEnvironment* env = scriptInterface.getScriptEnv();
    15.     env->setScriptId(creatureOnTargetCombat, &scriptInterface);
    16.  
    17.     lua_State* L = scriptInterface.getLuaState();
    18.     scriptInterface.pushFunction(creatureOnTargetCombat);
    19.  
    20.     if (creature) {
    21.         LuaScriptInterface::pushUserdata<Creature>(L, creature);
    22.         LuaScriptInterface::setCreatureMetatable(L, -1, creature);
    23.     } else {
    24.         lua_pushnil(L);
    25.     }
    26.  
    27.     LuaScriptInterface::pushUserdata<Creature>(L, target);
    28.     LuaScriptInterface::setCreatureMetatable(L, -1, target);
    29.  
    30.     ReturnValue returnValue;
    31.     if (scriptInterface.protectedCall(L, 2, 1) != 0) {
    32.         returnValue = RETURNVALUE_NOTPOSSIBLE;
    33.         LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
    34.     } else {
    35.         returnValue = LuaScriptInterface::getNumber<ReturnValue>(L, -1);
    36.         lua_pop(L, 1);
    37.     }
    38.  
    39.     scriptInterface.resetScriptEnv();
    40.     return returnValue;
    41. }
     
    Shadow_ likes this.
  9. Static_

    Static_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,718
    Likes Received:
    1,641
    Best Answers:
    274
    do you have it enabled="1" in events.xml?
     
    Sarah Wesker likes this.
  10. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    yes and @Sarah Wesker in ur code have a crash bug, need check target...
    btw this dont working here o/ omg.
     
  11. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    235
    Likes Received:
    93
    Best Answers:
    14
    it does not give me problems, in all cases, however, it can be used as well:
    Code (Text):
    1.  
    2. [LIST=1]
    3. [*]if (creature) {
    4. [*]        LuaScriptInterface::pushUserdata<Creature>(L, creature);
    5. [*]        LuaScriptInterface::setCreatureMetatable(L, -1, creature);
    6. [*]    } else {
    7. [*]        lua_pushnil(L);
    8. [*]    }
    [/LIST]
    This has never been taken into account, on my case.
     
  12. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    full script, any error? I didn't see:
    Code (Lua):
    1. function Creature:onTargetCombat(target)
    2.     if not self then
    3.         return true
    4.     end
    5.     if not __picif[target.uid] then
    6.         if target:isMonster() then
    7.             target:registerEvent("RewardSystemSlogan")
    8.             __picif[target.uid] = {}
    9.         end
    10.     end
    11.     local targetSummer = self:getTarget()
    12.     if self:isPlayer() then
    13.         if self:getStorageValue(923013) == 1 and Game.getStorageValue(923012) == 1 then
    14.             if target:getName() == staminaBonusASS.target1 or target:getName() == staminaBonusASS.target2 or target:getName() == staminaBonusASS.target3 or target:getName() == staminaBonusASS.target4 then
    15.                 return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE              
    16.             end
    17.         end
    18.     end  
    19.     if self:isPlayer() then
    20.         if target and target:getName() == staminaBonus.target then
    21.             local name = self:getName()
    22.             if not staminaBonus.events[name] then
    23.                 staminaBonus.events[name] = addEvent(addStamina, staminaBonus.period, name)
    24.             end
    25.         end
    26.     end
    27.  
    28.     if self:isPlayer() then
    29.         if target and target:getName() == staminaBonusCoh.targetCoh then
    30.             local name = self:getName()
    31.             if not staminaBonusCoh.eventsCoh[name] then
    32.                 staminaBonusCoh.eventsCoh[name] = addEvent(addStaminaCoh, staminaBonusCoh.periodCoh, name)
    33.             end
    34.         end
    35.     end
    36.  
    37.     if (not target) then
    38.         return true
    39.     end  
    40.  
    41.     local master = self:getMaster()
    42.     if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
    43.         return RETURNVALUE_NOTPOSSIBLE
    44.     end  
    45.  
    46.     return true
    47. end
     
  13. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
  14. bayview

    bayview Discord: Arthur#8852

    Joined:
    Jan 25, 2018
    Messages:
    527
    Likes Received:
    266
    Best Answers:
    21
    @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:
    Code (Lua):
    1. function Creature:onTargetCombat(target)
    2.     if not self then
    3.         print('not self')
    4.         return true
    5.     end
    6.     if not __picif[target.uid] then
    7.         print('not picif', target.uid)
    8.         if target:isMonster() then
    9.             print('target is monster')
    10.             target:registerEvent("RewardSystemSlogan")
    11.             __picif[target.uid] = {}
    12.         end
    13.     end
    14.     local targetSummer = self:getTarget()
    15.     if self:isPlayer() then
    16.         print('self is player')
    17.         if self:getStorageValue(923013) == 1 and Game.getStorageValue(923012) == 1 then
    18.             print('storage equals 1')
    19.             if target:getName() == staminaBonusASS.target1 or target:getName() == staminaBonusASS.target2 or target:getName() == staminaBonusASS.target3 or target:getName() == staminaBonusASS.target4 then
    20.                 print('you may not attack this creature')
    21.                 return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE          
    22.             end
    23.         end
    24.     end
    25.     if self:isPlayer() then
    26.         print('self is player')
    27.         if target and target:getName() == staminaBonus.target then
    28.             print(target:getName(), 'stamina bonus')
    29.             local name = self:getName()
    30.             if not staminaBonus.events[name] then
    31.                 print(target:getName(), ' not stamina bonus')
    32.                 staminaBonus.events[name] = addEvent(addStamina, staminaBonus.period, name)
    33.             end
    34.         end
    35.     end
    36.     if self:isPlayer() then
    37.         print('is player')
    38.         if target and target:getName() == staminaBonusCoh.targetCoh then
    39.             print(target:getName(), 'stamina bonus coh')
    40.             local name = self:getName()
    41.             if not staminaBonusCoh.eventsCoh[name] then
    42.                 print(target:getName(), 'not stamina bonus coh')
    43.                 staminaBonusCoh.eventsCoh[name] = addEvent(addStaminaCoh, staminaBonusCoh.periodCoh, name)
    44.             end
    45.         end
    46.     end
    47.     if (not target) then
    48.         print('not target')
    49.         return true
    50.     end
    51.     local master = self:getMaster()
    52.     if master and master:isPlayer() and target:isPlayer() and master:hasSecureMode() then
    53.         print('master not possible')
    54.         return RETURNVALUE_NOTPOSSIBLE
    55.     end
    56.     return true
    57. end
    See how I used print underneath all the if statements?
     
    Sarah Wesker likes this.
  15. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8
    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
     
  16. God Of Pain

    God Of Pain Member

    Joined:
    Aug 3, 2017
    Messages:
    393
    Likes Received:
    8
    Best Answers:
    8

Share This Page

Loading...