Page 1 of 1
Additions to player class(hasBuff and hasDebuff)
Posted: Wed Jun 23, 2010 12:34 am
by KillerTHC
I thought I would just add 2 functions to the player class, they are hasBuff(buff) and hasDebuff(deBuff). Both functions return true if the player currently has the buff active and false if the player doesn't. Both of these functions come in handy for using food items and using purifying spells when curse/poisoned.
Code: Select all
function CPlayer:hasBuff(buff)
local buffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('player', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
for i = 1,#buffs,2 do
local buffname = buffs[i];
local count = buffs[i+1] or 0;
if( count == 0 ) then count = 1; end;
if( buffname == buff) then
bool = true;
end
end
if( bool ) then return true; else return false; end
end
function CPlayer:hasDebuff(deBuff)
local deBuffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('player', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
for i = 1,#deBuffs,2 do
local debuffname = deBuffs[i];
local count = deBuffs[i+1] or 0;
if( count == 0 ) then count = 1; end;
if( debuffname == deBuff) then
bool = true;
end
end
if( bool ) then return true; else return false; end
end
I have tested this a bit but not too extensively, I think once optimized it should be added to either the player class or the pawn class so that it can be used to detect buffs on other targets. However I did not get this to work in the pawn class but in the player class it works.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Wed Jun 23, 2010 8:10 am
by rock5
I believe there is already a function to get buffs CPawn:updateBuffs(target) although it would be nice to have a function that searches for a buff or debuff after calling this function. You'd just need to search player.Buffs and/or player:Debuffs.
I was a bit confused by your code though. Why do you mess around with the "count" variable then not use it for anything?
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Wed Jun 23, 2010 11:03 am
by KillerTHC
I have tried searching the pawn buffs and debuffs but could not get it to work so I opted out for this method which doesn't need to run CPawn:updateBuffs(target) because it gets the most up to date info when it runs. I have the count variable their for future additions possibly making the function return the buff count as well as if the player has the buff.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Wed Jun 23, 2010 3:41 pm
by KillerTHC
Updated version of both methods to return both true or false if the buff is active and the count if it is active otherwise it returns 0.
Code: Select all
function CPlayer:hasBuff(buff)
local buffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('player', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
local count = 0;
for i = 1,#buffs,2 do
local buffname = buffs[i];
if( buffname == buff) then
bool = true;
count = buffs[i+1] or 0;
if( count == 0 ) then count = 1; end;
end
end
if( bool ) then return true,count; else return false,count; end
end
function CPlayer:hasDebuff(deBuff)
local deBuffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('player', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
local count = 0;
for i = 1,#deBuffs,2 do
local debuffname = deBuffs[i];
if( debuffname == deBuff) then
bool = true;
count = deBuffs[i+1] or 0;
if( count == 0 ) then count = 1; end;
end
end
if( bool ) then return true,count; else return false,count; end
end
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Wed Jun 23, 2010 11:43 pm
by rock5
Good work. Three changes I would make are; not use "local buffname = buffs
;" I'd just use "buff" itself, I'd assign 1 to count then get rid of the "if count == 0" staement and when returning the values I would get rid of the "if" statement and just use "return bool,count".
Also this really needs to be in pawn.lua as you might want to use it for mobs. I remember seeing code that checks the "Address" to see if it's a player so you could use "if self.Address == player.Address then".
Here's my version. Keep in mind it's untested.
Code: Select all
function CPawn:hasBuff(buff)
if self.Address == player.Address then
local pawn = "player"
else
local pawn = "target"
end
local buffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('"..pawn.."', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
local count = 0;
for i = 1,#buffs,2 do
if( buffs[i]== buff) then
bool = true;
count = buffs[i+1] or 1;
end
end
return bool ,count;
end
function CPawn:hasDebuff(deBuff)
if self.Address == player.Address then
local pawn = "player"
else
local pawn = "target"
end
local deBuffs = {RoMScript("} for i=1,16 do w,x,y,z=UnitBuff('"..pawn.."', i) table.insert(a,w) table.insert(a,y) end z={")};
local bool = false;
local count = 0;
for i = 1,#deBuffs,2 do
if( buffs[i]== deBuff) then
bool = true;
count = deBuffs[i+1] or 1;
end
end
return bool ,count;
end
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Thu Jun 24, 2010 1:13 am
by Administrator
Buff counts are, for certain buffs, returned as a stack of 0 when you should actually have 1 stack. That if statement is needed to correct that.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Thu Jun 24, 2010 1:29 am
by rock5
Administrator wrote:Buff counts are, for certain buffs, returned as a stack of 0 when you should actually have 1 stack. That if statement is needed to correct that.
Ok I think I see my mistake, when buffs[i+1] returns 0. Ok so you need the "if count == 0" statement.
If the buff exists is it possible to get a nil value for the count? Does it need the "or 0"?
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Thu Jun 24, 2010 4:52 am
by Administrator
I'm not entirely sure. I guess it was mostly put there to prevent any strange bugs in case nil was returned. I don't think it should return nil, though.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Fri Jun 25, 2010 11:23 am
by KillerTHC
So has rock5's version been added to the pawn class? My friend really wants to use these functions for curing himself and eating food buffs.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Fri Jun 25, 2010 11:35 am
by rock5
KillerTHC wrote:So has rock5's version been added to the pawn class? My friend really wants to use these functions for curing himself and eating food buffs.
No not yet. I'd like to hear from Administrator before adding it.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Fri Jun 25, 2010 2:58 pm
by Administrator
If it has been tested, go ahead and commit it. I don't see any problems.
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Sat Jun 26, 2010 12:28 am
by rock5
The problem is, I don't like that it reproduces code that already exists in updatesBuffs. If it ever needs to be changed it would need to be changed in 3 places. Better to use updateBuffs so if it needs to be changed you only need to change it in 1 location. It also makes the functions a lot smaller.
How about something like this?
Code: Select all
function CPawn:hasBuff(buff)
self:updateBuffs()
local bool = (self.Buffs[buff] ~= nil) -- exists or not, true or false
local count = (self.Buffs[buff] or 0) -- Buff count or 0
return bool, count
end
function CPawn:hasDebuff(deBuff)
self:updateBuffs()
local bool = (self.Debuffs[deBuff] ~= nil) -- exists or not, true or false
local count = (self.Debuffs[deBuff] or 0) -- Debuff count or 0
return bool, count
end
Actually when I see how simple it is, it hardly seems worth it.
In my test all I did was replace
Code: Select all
player:updateBuffs()
if player.Buffs["Wave Armor"] then
with
Code: Select all
if player:hasBuff("Wave Armor") then
Not much of a difference. Might still be worth doing I guess. What do you think?
Re: Additions to player class(hasBuff and hasDebuff)
Posted: Sat Jun 26, 2010 6:35 pm
by KillerTHC
I'd say add it so it can be better documented as a function.