Additions to player class(hasBuff and hasDebuff)

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

Additions to player class(hasBuff and hasDebuff)

#1 Post by KillerTHC » Wed Jun 23, 2010 12:34 am

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.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Additions to player class(hasBuff and hasDebuff)

#2 Post by rock5 » Wed Jun 23, 2010 8:10 am

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?
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

Re: Additions to player class(hasBuff and hasDebuff)

#3 Post by KillerTHC » Wed Jun 23, 2010 11:03 am

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.

KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

Re: Additions to player class(hasBuff and hasDebuff)

#4 Post by KillerTHC » Wed Jun 23, 2010 3:41 pm

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

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Additions to player class(hasBuff and hasDebuff)

#5 Post by rock5 » Wed Jun 23, 2010 11:43 pm

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
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Additions to player class(hasBuff and hasDebuff)

#6 Post by Administrator » Thu Jun 24, 2010 1:13 am

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.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Additions to player class(hasBuff and hasDebuff)

#7 Post by rock5 » Thu Jun 24, 2010 1:29 am

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"?
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Additions to player class(hasBuff and hasDebuff)

#8 Post by Administrator » Thu Jun 24, 2010 4:52 am

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.

KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

Re: Additions to player class(hasBuff and hasDebuff)

#9 Post by KillerTHC » Fri Jun 25, 2010 11:23 am

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.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Additions to player class(hasBuff and hasDebuff)

#10 Post by rock5 » Fri Jun 25, 2010 11:35 am

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.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Additions to player class(hasBuff and hasDebuff)

#11 Post by Administrator » Fri Jun 25, 2010 2:58 pm

If it has been tested, go ahead and commit it. I don't see any problems.

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Additions to player class(hasBuff and hasDebuff)

#12 Post by rock5 » Sat Jun 26, 2010 12:28 am

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?
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

KillerTHC
Posts: 63
Joined: Tue May 25, 2010 8:49 pm

Re: Additions to player class(hasBuff and hasDebuff)

#13 Post by KillerTHC » Sat Jun 26, 2010 6:35 pm

I'd say add it so it can be better documented as a function.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest