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.
Lua:
if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
-- if 100 < 200 + 200 * - 0.3 / 100 or 200 > 100 + 100 * 0.3 / 100 then
-- if 100 < 200 + -60 / 100 or 200 > 100 + 30 / 100 then
-- if 100 < 200 + -0.6 or 200 > 100 + 0.3 then
-- 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.
Lua:
function()
local x = cid_rebirth
local y = target_rebirth
if x > y then
-- do this
elseif x < y then
-- do this
end
return true
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.
Lua:
function()
local x = 100
local y = 200
local x_range = x * 0.3 -- 30
if x > y then
-- do this
elseif x < y then
-- do this
end
return true
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.
Lua:
function()
local x = 100
local y = 200
local x_range = x * 0.3 -- 30
if x > y then
if x + x_range < y then
--if 100 + 30 < 200 then
return false
end
elseif x < y then
-- do this
end
return true
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.
Lua:
function()
local x = 200
local y = 100
if x > y then
local x_range = x * 0.3
if x + x_range < y then
return false
end
elseif x < y then
local y_range = y * 0.3 -- 30
if y + y_range < x then
--if 100 + 30 < 200 then
return false
end
end
return true
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.
Lua:
if x + x_range < y then
--if 100 + 30 < 130 then
return false
end
if y + y_range < x then
--if 100 + 30 < 130 then
return false
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:
local player = Player(cid)
this incorrect?
Shouldn't it be like this?
Code:
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.
----------------------------
Lua:
local function isInRebirthLevelRange(cid, target)
local reb = 85987
local x = getStorageValue(cid, reb)
local y = getStorageValue(target, reb)
local range = 0.3
if x > y then
local x_range = x * range
if x + x_range < y then
return false
end
elseif x < y then
local y_range = y * range
if y + y_range < x then
return false
end
end
return true
end
function Creature:onTargetCombat(target)
-- this extra check is so that monsters and npc's don't get their storages checked
if Creature:isPlayer() and target:isPlayer() then
local player = Player(Creature)
if not isInRebirthLevelRange(player, target) then
player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than 30% level difference.")
return false
end
end
return true
end
Well anyways, hope that works.
If not, at least you know the math is correct. xP