OTX 3 Event Script

Discussion in 'Requests' started by athenso, Jan 12, 2018.

  1. athenso

    athenso Average Coder Premium User

    Joined:
    May 31, 2011
    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    I have an event script for Events -> creature.lua The script is supposed to be working, so a player can not attack another player that is 30% less than the attacker. In this case its for my reborn storages.

    Code (Text):
    1. function Creature:onTargetCombat(target)
    2. local percent = 0.3   --- devide the percent by 100 , so 0.3 = 30%
    3. local player = Player(cid)
    4. local reb = 85987
    5.         local level, tlevel = getStorageValue(cid, reb), getStorageValue(target, reb)
    6.         if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
    7.              player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. percent .."% level difference.")
    8.                 return false
    9.         else
    10.         return true
    11.     end
    12. end

    The script is not working, and it is not throwing any errors. Some outside eyes on it would be nice. Thanks
     
  2. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    87
    Likes Received:
    33
    Best Answers:
    4
    Code (Lua):
    1.  
    2.     function Creature:onTargetCombat(target)
    3.     local percent = 130 -- 100 is default, 130 is 100 + 30%
    4.     local reb = 85987
    5.     if self:isPlayer() and target:isPlayer() then
    6.     local level = player:getStorageValue(reb)
    7.     local tlevel = target:getStorageValue(reb)
    8.     local diff = ((level / tlevel) * 100)
    9.     if diff >= percent then
    10.         player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
    11.         return false
    12.     end end
    13.         return true
    14.     end
     
  3. Xikini

    Xikini I whore myself out for likes

    Joined:
    Nov 17, 2010
    Messages:
    3,403
    Likes Received:
    1,717
    Best Answers:
    60
    Is onTargetCombat the same as onStatsChange in 0.3.7?
    If it's not.. this only stops the player from selecting the target.
    Area damage is still gonna murder each other.
     
  4. athenso

    athenso Average Coder Premium User

    Joined:
    May 31, 2011
    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Tested this and it has the same outcome as my script had.

    I knew i would overlook something, but 1 step at a time lol. Im just trying to keep a reborn 30 from destroying a level 400 non reborn.
     
  5. Xikini

    Xikini I whore myself out for likes

    Joined:
    Nov 17, 2010
    Messages:
    3,403
    Likes Received:
    1,717
    Best Answers:
    60
    Because of OtLand outage, I had to save this in a notepad for like 5 hours. xD
    -----------
    Well let's look at your math for a second.
    Let's say we have a level 100 rebirth and a level 200 rebirth.
    Code (Lua):
    1. if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
    2. -- if 100 < 200 + 200 * - 0.3 / 100 or 200 > 100 + 100 * 0.3 / 100 then
    3.    -- if 100 < 200 + -60 / 100 or 200 > 100 + 30 / 100 then
    4.    -- if 100 < 200 + -0.6 or 200 > 100 + 0.3 then
    5.    -- if 100 < 199.4 or 200 > 100.3 then
    If you actually write out your equation, you can see it makes no sense.

    So let's try to think about what we want to do.

    Alright, we want to check if these people can attack each other.
    Since we KNOW that cid is the person initiating the attack..
    We only need to know if person X can attack person Y.
    Since we don't know if x is lower or higher then y to start, we need to check both ways.

    So, let's start here.
    Code (Lua):
    1. function()
    2.     local x = cid_rebirth
    3.     local y = target_rebirth
    4.  
    5.     if x > y then
    6.         -- do this
    7.     elseif x < y then
    8.         -- do this
    9.     end
    10.     return true
    11. end
    Alright, now we want to find out what 30% of number x .
    For clarity, let's assign x to 100 and y to 200 like before.
    Code (Lua):
    1. function()
    2.     local x = 100
    3.     local y = 200
    4.     local x_range = x * 0.3 -- 30
    5.  
    6.     if x > y then
    7.        -- do this
    8.     elseif x < y then
    9.         -- do this
    10.     end
    11.     return true
    12. end
    Let's start with x being a lower value then y.
    We know x's range is 30% , we just need to see if y is within that range.
    We also know we want this statement to return false, we will ask the question backwards.
    Code (Lua):
    1. function()
    2.    local x = 100
    3.    local y = 200
    4.    local x_range = x * 0.3 -- 30
    5.  
    6.    if x > y then
    7.        if x + x_range < y then
    8.        --if 100 + 30 < 200 then
    9.            return false
    10.        end
    11.    elseif x < y then
    12.        -- do this
    13.    end
    14.    return true
    15. end
    Alright, so we got some working math.
    At this point we need to start working on if x is higher then y.
    In this case we'll want to swap it around, and see what the target's range is, instead of our range, since they are the smaller level.
    So let's add y_range into the equation.
    And since we don't need to check x_range inside the second check, let's move these locals too.
    Code (Lua):
    1. function()
    2.    local x = 200
    3.    local y = 100
    4.  
    5.    if x > y then
    6.        local x_range = x * 0.3
    7.        if x + x_range < y then
    8.            return false
    9.        end
    10.    elseif x < y then
    11.        local y_range = y * 0.3 -- 30
    12.        if y + y_range < x then
    13.        --if 100 + 30 < 200 then
    14.            return false
    15.        end
    16.    end
    17.    return true
    18. end
    Alright still working.
    Now let's try some edge cases in both directions.
    Let's just check the equations, no need to post entire code.
    Code (Lua):
    1. if x + x_range < y then
    2. --if 100 + 30 < 130 then
    3.    return false
    4. end
    5.  
    6. if y + y_range < x then
    7. --if 100 + 30 < 130 then
    8.    return false
    9. end
    Still working.
    Alright, let's turn this into a real function you can use..
    and plug it into the script.

    ----------------------------
    I'm not very good with OTX or 1.0+ scripting..
    but isn't
    Code (Text):
    1. local player = Player(cid)
    this incorrect?
    Shouldn't it be like this?
    Code (Text):
    1. local self = Player(Creature)
    I'm gonna write the script as if my version is correct.
    Someone is gonna have to fix up this part if it's wrong, but my math and reasoning is gonna be correct inside the function.
    ----------------------------
    Code (Lua):
    1. local function isInRebirthLevelRange(cid, target)
    2.    local reb = 85987
    3.    local x = getStorageValue(cid, reb)
    4.    local y = getStorageValue(target, reb)
    5.    local range = 0.3
    6.  
    7.    if x > y then
    8.        local x_range = x * range
    9.        if x + x_range < y then
    10.            return false
    11.        end
    12.    elseif x < y then
    13.        local y_range = y * range
    14.        if y + y_range < x then
    15.            return false
    16.        end
    17.    end
    18.    return true
    19. end
    20.  
    21. function Creature:onTargetCombat(target)
    22.    -- this extra check is so that monsters and npc's don't get their storages checked
    23.    if Creature:isPlayer() and target:isPlayer() then
    24.        local player = Player(Creature)
    25.        if not isInRebirthLevelRange(player, target) then
    26.            player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than 30% level difference.")
    27.            return false
    28.        end
    29.    end
    30.    return true
    31. end
    Well anyways, hope that works.
    If not, at least you know the math is correct. xP
     
  6. athenso

    athenso Average Coder Premium User

    Joined:
    May 31, 2011
    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    This script looks amazing. I tried it and nothing happened. I was looking at it last night, and it looks as though the script is never getting called being put where it is. So i will have to find a different method to perform the check. I thought for sure Events would be the place to try this, as in OTX there is no "onCombat" for creature scripts. Thanks anyways
     
  7. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    87
    Likes Received:
    33
    Best Answers:
    4
    Code (Lua):
    1.  
    2.     function Creature:onTargetCombat(target)
    3.    
    4.     if not self then
    5.     return true
    6.     end
    7.  
    8.     local percent = 130 -- 100 is default, 130 is 100 + 30%
    9.     local reb = 85987
    10.     if self:isPlayer() and target:isPlayer() then
    11.     local level = player:getLevel()
    12.     local tlevel = target:getLevel()
    13.     local diff = ((level / tlevel) * 100)
    14.     if diff >= percent then
    15.         player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
    16.         return false
    17.     end end
    18.  
    19.     return true
    20.     end
     
  8. Xikini

    Xikini I whore myself out for likes

    Joined:
    Nov 17, 2010
    Messages:
    3,403
    Likes Received:
    1,717
    Best Answers:
    60
    Code (Text):
    1. level = 100
    2. tlevel = 200
    3. diff = (100 / 200) * 100
    4. -- 2 * 100
    5. if 200 >= 130 then
    The math doesn't add up.
     
  9. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    87
    Likes Received:
    33
    Best Answers:
    4
    Code (Lua):
    1.  
    2. 1.3 == 130 ( 1.3 * 100 )
    3. 2.0 == 200 ( 2.0 * 100 )
    4.  
    What are you talking about?
    it's the same comparison
    just that it's better not to use decimals
     
    Last edited: Jan 14, 2018
  10. Xikini

    Xikini I whore myself out for likes

    Joined:
    Nov 17, 2010
    Messages:
    3,403
    Likes Received:
    1,717
    Best Answers:
    60
    if you if you actually read your script instead of quoting me and telling me bullshit you would know you're wrong
     
    Sarah Wesker likes this.
  11. imkingran

    imkingran Learning everyday. Premium User

    Joined:
    Jan 15, 2014
    Messages:
    1,212
    Likes Received:
    368
    Best Answers:
    10
    Make sure you enabled it in events.xml
    Code (Lua):
    1. <event class="Creature" method="onTargetCombat" enabled="1" />
    Code (Lua):
    1. -- local variables
    2. local rebirthStorage = 85987
    3. local percent = 0.3 -- 30%
    4.  
    5. function Player:canAttack(target)
    6.     local pLvl, tLvl = self:getStorageValue(rebirthStorage), target:getStorageValue(rebirthStorage)
    7.     -- if the target is equal/higher level then he can attack automatically
    8.     if tLvl >= pLvl then
    9.         return true
    10.     end
    11.  
    12.     -- if the diff between the lower target and player is less than or equal to 30% of player level then he can attack
    13.     local diff = pLvl - tLvl
    14.     if diff <= (pLvl * percent) then
    15.         return true
    16.     end
    17. return false
    18. end
    19.  
    20. function Creature:onTargetCombat(target)
    21.     if not self then
    22.         return true
    23.     end
    24.  
    25.     if self:isPlayer() and target:isPlayer() then
    26.         if not self:canAttack(target) then
    27.             self:sendTextMessage(MESSAGE_INFO_DESCR, "You may not attack players with more than " .. percent * 100 .. "% rebirth level difference.")
    28.             return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
    29.         end
    30.     end
    31. return true
    32. end
     
    Last edited: Jan 14, 2018

Share This Page

Loading...