• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Useful Methods for your scripts
#1
Here you can post code segments of any implementation of racial skills or useful checks. I will start with some... (sorry for good/bad solutions... and not following code standards)
Some of my rotations gets very long that's why I have to skip using more lines then necessary. I've worked a lot on hunter methods... I've now shared my full hunter rotation on this forum.

In my scripts I use the timer functions for Global Cooldowns when using skills. Then add a return statement early so the scripts does nothing during that duration (to not spam abilities).

Undead  Racial Cannibalize:
function SetCannibalizeTarget(targetObj) SetVar('cannibTarget', targetObj); end -- Saves a target to Cannibalize later
function GetCannibalizeTarget() return GetVar('cannibTarget'); end -- Gets the target that should be Cannibalized

-- Set the current target as a "Cannibalize-target" if it's Humanoid or Undead
 if (GetCreatureType(targetObj) == 'Humanoid' or GetCreatureType(targetObj) == 'Undead') then SetCannibalizeTarget(targetObj); else SetCannibalizeTarget(0); end

-- Check: If we can use Cannibalize on a corpse
if (IsTimeGood() == 1 and IsInCombat() == 0 and localHealth < eatHealth and IsEating() == 0) then          
     if (IsMoving() == 1 and GetDistance(GetCannibalizeTarget()) < 3) then StopMoving(); return; end
      -- Cannibalize if possible
      if(HasSpell('Cannibalize') == 1 and IsSpellOnCD('Cannibalize') == 0 and GetCannibalizeTarget() ~= 0) then  
            SetSatusText('Cannibalize possible: ', 'Moving to corpse to feed...');
            if (GetDistance(GetCannibalizeTarget()) > 3) then
                  MoveToTarget(GetCannibalizeTarget());
                  return;
             else
                  StopMoving();
                  CastSpellByName('Cannibalize');
                  UpdateTimer();
                  return;
           end
     end
end

Customizable check if bags are full, e.g. with ignore hunter quiver (quiverBagNr = quiver bag index):
-- Check bags 1-5, except the quiver bag (quiverBagNr)
for i=1,5 do
     if (i ~= quiverBagNr) then
            for y=1,GetContainerNumSlots(i-1) do
                  texture, itemCount, locked, quality, readable = GetContainerItemInfo(i-1,y);
                  if (itemCount == 0 or itemCount == nil) then
                          inventoryFull = false;
                  end
            end
      end
end

if (inventoryFull) then
    -- do something, e.g. I use Hearthstone and stop the bot.
end

Use Hearthstone with check of CD, does HS if on CD then stops the bot:
hsBag = 1; -- local setting variable: bag 1 (index is 0 for bag 1!)
hsSlot = 1; -- local setting variable: slot 1 (index is 1 for slot 1)
hsWhenStop = true; -- local setting

SetSatusText('Inventory is full', 'Stopping the bot...');
StopMoving();
if (GetContainerItemCooldown(hsBag-1,hsSlot) == 0 and hsWhenStop and IsTimeGood() == 1) then
         UseItem('Hearthstone');
         UpdateTimer();
         return;
else
         StopBot();
         return;
end


First Aid, bandage:
bandageName = "Linen Bandage"; -- Name of the bandage to use

function UpdateBandageTimer() SetVar('bandageTimer', GetTimeX() + 62000); end -- Set the CD time for the bandage debuff (60s)
function CanBandage() if (GetVar('bandageTimer') < GetTimeX()) then return 1; end return 0; end -- Has the bandage debuff CD timer passed?

-- Check: Use Bandage if possible
if(HasItem(bandageName) == 1 and CanBandage() == 1 and IsInCombat() == 0 and localHealth < eatHealth and IsEating() == 0 and IsTimeGood() == 1) then
    SetSatusText('Bandage possible: ', 'Using bandage...');
    if (UseItem(bandageName) == 1)  then
           UpdateBandageTimer();
           UpdateTimer();
           return;
    end
    return; 
end

Display the player's HP, Mana, Energy, Rage, Pet's Hp and Target's HP on the screen

DrawText('HP:' .. GetHealth(localObj) .. ' (' .. round(GetHealthPercentage(localObj),2) .. '%)', 25, 150, 0, 255, 0);
DrawText('Mana:' .. GetMana(localObj) .. ' (' .. round(GetManaPercentage(localObj),2) .. '%)', 25,165, 0, 255, 255);
if (hasPet and UnitExists("pet")) then DrawText('PET HP:' .. UnitHealth("pet") .. ' (' .. round(UnitHealth("pet")*100/UnitHealthMax("pet"),2) .. '%)', 25, 180, 0, 255, 0); end

DrawText('Energy:' .. GetEnergy(localObj), 25,165, 255, 255, 0);
DrawText('Energy:' ..GetRage(localObj), 25,165, 255, 255, 0);

if (GetTarget() ~= 0) then DrawText('Target HP:' .. GetHealth(GetTarget()) .. ' (%)', 25, 180, 255, 0, 0); end


Hunter choose aspect function:
function chooseAspect()
    hasHawk, hasMonkey, hasCheetah  = HasSpell("Aspect of the Hawk"), HasSpell("Aspect of the Monkey"), HasSpell("Aspect of the Cheetah");
    if (hasMonkey and GetLevel(localObj) < 10) then if (HasBuff(localObj, 'Aspect of the Monkey') == 0) then SetSatusText('Choosing Aspect: ', 'Aspect of the Monkey...'); CastSpellByName('Aspect of the Monkey'); return; end    
    elseif (hasCheetah and IsInCombat() == 0 and GetNearestEnemy() == 0) then if (HasBuff(localObj, 'Aspect of the Cheetah') == 0) then SetSatusText('Choosing Aspect: ', 'Aspect of the Cheetah...'); CastSpellByName('Aspect of the Cheetah'); return; end
    elseif (hasHawk and IsInCombat() == 1) then if (HasBuff(localObj, 'Aspect of the Hawk') == 0) then SetSatusText('Choosing Aspect: ', 'Aspect of the Hawk...'); CastSpellByName('Aspect of the Hawk'); return; end end
end

Feed pet for hunters:
bagWithPetFood = 4; -- 4th bag slot has pet food in it (ALWAYS PUT PET FOOD IN THE LAST SLOT OF THE BAG!)

function SetFeedTimer() SetVar('feedTimer', GetTimeX() + 20000); end -- 20s duration on Feed Pet
function IsFeedPetDone() if (GetVar('feedTimer') < GetTimeX()) then return 1; end return 0; end

-- Check: If pet isn't happy, feed it (see the bagWithPetFood, pet food have to be in the last slot of the bag)
happiness, damagePercentage, loyaltyRate = GetPetHappiness();
if (UnitExists("Pet") and IsFeedPetDone() == 1 and IsInCombat() == 0 and hasPet and UnitHealth("pet") > 0) then
    if (happiness < 3 or loyaltyRate < 0) then
        SetSatusText('Pet is not happy!', 'Feeding the pet...');
        if (IsStanding() == 0) then SitOrStand(); return; end
        CastSpellByName("Feed Pet"); TargetUnit("Pet"); PickupContainerItem(bagWithPetFood-1, GetContainerNumSlots(bagWithPetFood-1));
        -- Set a 20 seconds timer for this check (Feed Pet duration)
        SetFeedTimer(); end
end

Check: If pet is dead/dismissed then Revive/Call pet:
if (UnitHealth("pet") == 0 and hasPet) then
    SetSatusText('Pet is missing', 'Calling/reviving pet...');
    if (not UnitExists("pet")) then CastSpellByName('Call Pet'); return; end
    if (IsMoving() == 1) then StopMoving(); return; else
        if (IsStanding() == 0) then SitOrStand(); return; end
        __, __, __, __, reviveCost = GetSpellInfoX('Revive Pet');
        if (GetMana(localObj) >= reviveCost) then CastSpellByName('Revive Pet'); else return; end end
end

Check: Dont pull if pet has lower than 70% HP and out of combat, use mend pet
if (hasPet and UnitHealth("pet")/UnitHealthMax("pet") < 0.70 and IsInCombat() == 0 and UnitHealth("pet") > 0) then
    __, __, __, __, mendCost = GetSpellInfoX('Mend Pet');
    SetSatusText('Pet has lower than 70 procent HP!', 'Mend pet if not in combat...');
    if (IsMoving(localObj)) then StopMoving(); end
    if (IsDrinking() == 0 and GetMana(localObj) >= mendCost) then CastSpellByName("Mend Pet"); return; end
end

Check: Mend the pet if it has less than 35% HP and we are in combat and pet isn't dead
if (hasPet and UnitHealth("pet")/UnitHealthMax("pet") < 0.35 and IsInCombat() == 1 and UnitHealth("pet") ~= nil) then
    SetSatusText('Pet has lower than 30 percent HP!', 'Mend pet...');
    __, __, __, __, mendCost = GetSpellInfoX('Mend Pet');
    -- Check: If in range to mend the pet
    if (GetDistance(targetObj) < 20 and GetMana(localObj) >= mendCost) then if (IsMoving(localObj) == 1 and GetDistance(targetObj) < 20) then StopMoving(); return; end CastSpellByName("Mend Pet"); return;
    elseif (GetMana(localObj) >= mendCost) then MoveToTarget(targetObj); return; end
end
  Reply
#2
Useful function for meele classes, the hard calculations (vectors) is taken from Beniamins Runaway function. Credits to him.

My function run backwards if there is at least 'count' nr of monsters with in 'range'.


function RunBackwards(count, range) -- Run backwards if there is atleast count of monsters within range
   local countUnitsInRange = 0;
   local currentObj, typeObj = GetFirstObject();
   local localObj = GetLocalPlayer();
   local closestEnemy = 0;
   while currentObj ~= 0 do
        if typeObj == 3 or typeObj == 4 then
           if CanAttack(currentObj) == 1 and GetDistance(currentObj) <= range and IsDead(currentObj) == 0 then
              countUnitsInRange = countUnitsInRange + 1;
              closestEnemy = (GetDistance(currentObj) < GetDistance(closestEnemy) or closestEnemy == 0) an d currentObj or closestEnemy;
           end
        end
     currentObj, typeObj = GetNextObject(currentObj);
     end
     if closestEnemy ~= 0 then
        local xT, yT, zT = GetPosition(closestEnemy); local xP, yP, zP = GetPosition(localObj); local distance = GetDistance(localTarget);
        local xV, yV, zV = xP - xT, yP - yT, zP - zT;
        local vectorLength = math.sqrt(xV^2 + yV^2 + zV^2);
        local xUV, yUV, zUV = (1/vectorLength)*xV, (1/vectorLength)*yV, (1/vectorLength)*zV;
        local moveX, moveY, moveZ = xT + xUV*100, yT + yUV*100, zT + zUV*100;
        if countUnitsInRange >= count then
           Move(moveX, moveY, moveZ);
           return true;
         end
      end
end
  Reply
#3
Extended the script used to delete grey items to also delete blacklisted items.

Code:
-- Auto delete grey items parameter
deleteGrey = true;

-- specific items to remove
itemsToDelete = {
 'Small Barnacled Clam',
 'Murloc Eye',
 'An Old History Book',
}


-- Check: Delete grey items and specific items
if (IsInCombat() == 0 and IsDead(localObj) == 0) then
    for bag = 0, 4 do
        for slot = 1, GetContainerNumSlots(bag) do
            local name = GetContainerItemLink(bag,slot)
            if name then
                if deleteGrey and string.find(name,"ff9d9d9d") then
                    PickupContainerItem(bag,slot);
                    DeleteCursorItem();
                end
                if itemsToDelete and table.getn(itemsToDelete) > 0 then
                    for index = 1, table.getn(itemsToDelete) do
                        if string.find(name, itemsToDelete[index]) then
                            PickupContainerItem(bag,slot);
                            DeleteCursorItem();
                        end
                    end
                end
            end    
        end
    end
end
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)