Jump to content

Recommended Posts

Hi y'all!

 

So I'm working on a plugin for area farming (where looting every corpses looks bot as fuck).

 

I'm going to include the whole code (just in case there is something there causing the error.

 

You'll note I've commented out a lot of the code so that I'm mostly just focussing on why I can't seem to get the timers to work...I've been looking at other developer's code and I can't figure out how my usage is different.

using robotManager.Helpful;
using System.Threading;
using robotManager.Products;
using wManager.Wow.Enums;
using wManager.Wow.Helpers;
using wManager.Wow.ObjectManager;
using System;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using robotManager;


public class Main : wManager.Plugin.IPlugin
{
    private bool _isLaunched;
    private bool _isLooting;
       
    private bool _lootingTimedOut;
    private bool _killingTimedOut;
    
    private bool _lootingCompleted;
    private bool _killingCompleted;
    
    private bool _lootingDone;
    private bool _killingDone;

    private static int killingMaxTime;
    private static int killingMaxTimeMilli;
    private static int lootingMaxTime;
    private static int lootingMaxTimeMilli;

    //private var lootableCorpses;
    private static int maxCorpses;
//    private List<WoWObject> lootableCorpses;
    private static int lootableCorpseCount;
    private List<WoWUnit> lootableCorpses = new List<WoWUnit>();


    // = ObjectManager.ObjectList().Where(u => u != null && u.IsValid && u.IsDead && u.IsLootable);
//    private List<WoWUnit> 
    //var lootableCorpses = new List<WoWUnit unit>();

    public static void Log(string text)
    {
        Logging.Write("[Lootoggle] " + text);
    }

    private static List<WoWUnit> GetLootableCorpses() {
        List<WoWUnit> result = new List<WoWUnit>();
        try
        {
            foreach (WoWObject wo in ObjectManager.ObjectList)
            {
                WoWUnit o;
                switch (wo.Type)
                {
                    case WoWObjectType.Unit:
                      o = new WoWUnit(wo.GetBaseAddress);
                      break;
                    default: continue;
                }
                if (o != null && o.IsValid && o.IsDead && o.IsLootable)
                {
                    result.Add(o);  
                }
            }
        }
        catch (Exception arg)
        {
            Logging.WriteError("GetLootableCorpses error " + arg, true);
        }
        return result;
    }

    public void Initialize()
    {
        _isLaunched = true;
        _isLooting = true;
        LootoggleSettings.Load();
        
        maxCorpses = LootoggleSettings.CurrentSetting.maxCorpses;
        Log("Started.");
        pluginLoop();
    }

    public void Dispose()
    {
        _isLaunched = false;
        Log("Disposed.");
    }

    public void Settings()
    {
        LootoggleSettings.Load();
        LootoggleSettings.CurrentSetting.ToForm();
        LootoggleSettings.CurrentSetting.Save();
//        Log("[Lootoggle] Settings saved.");
    }

    static robotManager.Helpful.Timer KillingTimer = new robotManager.Helpful.Timer(killingMaxTimeMilli);
    static robotManager.Helpful.Timer LootingTimer = new robotManager.Helpful.Timer(lootingMaxTimeMilli);

    public void StartLooting()
    {
        wManager.wManagerSetting.CurrentSetting.LootMobs = _isLooting = true;
        Log("Resetting looting timer to " + lootingMaxTime + ".");
        LootingTimer.Reset();
        LootingTimer.Reset();
    }

    public void StopLooting()
    {
        wManager.wManagerSetting.CurrentSetting.LootMobs = _isLooting = false;
        Log("Resetting killing timer to " + killingMaxTime + ".");
        KillingTimer.Reset();
        LootingTimer.Reset();
    }

    public void LogLootingEvents()
    {
        Log("Corpses: " + lootableCorpseCount + "/" + maxCorpses + "      Countdown: " + KillingTimer + "Downtime" + "/" + killingMaxTime);
        Thread.Sleep(100);
    }


//    public void watchForEvents()
//    {
//        //  Adding this mostly just to keep track of loot in logs.
//        EventsLuaWithArgs.OnEventsLuaWithArgs += (LuaEventsId id, List<string> args) =>
//        {
//            if (id == "LOOT_CLOSED")
//            {
//                Log("Looting completed.");
//            }           
//        };
//    }
//

    public void pluginLoop()
    {
        KillingTimer = new robotManager.Helpful.Timer(killingMaxTimeMilli);
        KillingTimer.ForceReady();
        LootingTimer.Reset(lootingMaxTimeMilli);
        KillingTimer.Reset(killingMaxTimeMilli);
        while (Products.IsStarted && Conditions.InGameAndConnectedAndAliveAndProductStartedNotInPause)
        {
            lootableCorpses = GetLootableCorpses();
            lootableCorpseCount = lootableCorpses.Count;
            Log("There are " + lootableCorpseCount + " lootable corpses.");

            _lootingTimedOut = LootingTimer.IsReady;
            _lootingCompleted = (lootableCorpseCount < 1);
            _lootingDone = (_lootingTimedOut || _lootingCompleted);
            
            // Log(LootingTimer.IsReady);

            _killingTimedOut = KillingTimer.IsReady;
            _killingCompleted = (lootableCorpseCount >= maxCorpses);
            _killingDone = (_killingTimedOut || _killingCompleted);
            Log("Looting Time left: " + LootingTimer.TimeLeft());
            Log("Countdown Time: " + LootingTimer.CountDowntime);
            Log("Killing Time left: " + KillingTimer.TimeLeft());
            Thread.Sleep( 2000 );
/*
            while (_isLooting)
            {
                Log("Time left: " + LootingTimer.TimeLeft());

                Log("IS LOOTING");
            }
            while (!_isLooting)
            {
                Log("Time left: " + KillingTimer.TimeLeft());
                if (KillingTimer.IsReady)
                {
                    Log("Killing timer is ready");
                }
                if (!KillingTimer.IsReady)
                {
                    Log("Killing timer is NOT ready");
                } 

                Log("NO LOOTING");
            }
            //Log("Killing done: " + _killingDone);
        }
        //List<WoWObject> lootableCorpses = new List<WoWObject>() {};
        */
        }
    }
/*        while (Products.IsStarted && _isLaunched)
        {
            while (_isLooting)
            {
                if (_lootingDone)
                {
                    if (_lootingCompleted)
                    {
                        Log("Bot has finished looting available corpses.");
                    }
                    if (_lootingTimedOut)
                    {
                        Log("Bot has been looting for " + killingMaxTime + " minutes. (If you get this message often, you may want to adjust your settings.)");
                    }
                    //      Eventually I'd like to rework this to have the bot auto-adjust based on conditions.
                    StopLooting();
                }
            }

            while (!_isLooting) 
            {
                if (_killingDone)
                {
                    if (_killingCompleted)
                    {
                        Log("There are more than " + maxCorpses + " lootable mobs. Switching to looting.");
                    }
                    if (_killingTimedOut)
                    {
                        Log("Bot hasn't looted in over " + killingMaxTime + " minutes. Time to loot those corpses!");
                    }
                    StartLooting();
                }
            }
        }
        */

    [Serializable]
    public class LootoggleSettings : Settings
    {
        [DefaultValue(3)]
        [Category("General Settings")]
        [DisplayName("Max Kill Time")]
        [Description("Maximum time bot will spend in pure killing mode between loot breaks")]
        
        public int killingMaxTime { get; set; }

        [DefaultValue(3)]
        [Category("General Settings")]
        [DisplayName("Max Loot Time")]
        [Description("Maximum time bot will spend looting before switching to looting phase")]
        
        public int lootingMaxTime { get; set; }

    
        [DefaultValue(40)]
        [Category("General Settings")]
        [DisplayName("Max Corpses")]
        [Description("Maximum number of lootable mobs bot will let lie before auto switching to looting (particularly helpful with group-farming).")]
        
        public int maxCorpses { get; set; }
        
        private LootoggleSettings()
        {
            killingMaxTime = 3;
            lootingMaxTime = 1;
            maxCorpses = 40;
            ConfigWinForm(new System.Drawing.Point(300, 400), "Lootoggle " + Translate.Get("Settings"));
        }

        public static LootoggleSettings CurrentSetting { get; set; }
        
        public bool Save()
        {
            try
            {
                return Save(AdviserFilePathAndName("Lootoggle", ObjectManager.Me.Name + "." + Usefuls.RealmName));
            }
            catch (Exception e)
            {
                Logging.WriteError("LootoggleSettings > Save(): " + e);
                return false;
            }
        }
        
        public static bool Load()
        {
            try
            {
                if (File.Exists(AdviserFilePathAndName("Lootoggle", ObjectManager.Me.Name + "." + Usefuls.RealmName)))
                {
                    CurrentSetting = Load<LootoggleSettings>(AdviserFilePathAndName("Lootoggle", ObjectManager.Me.Name+"."+Usefuls.RealmName));
                    return true;
                }
                CurrentSetting = new LootoggleSettings();
            }
            catch (Exception e)
            {
                Logging.WriteError("LootoggleSettings > Load(): " + e);
            }
            return false;
        }
    }
}

 

Hello,

it is hard to help you, first problem for me is:

while (Products.IsStarted && Conditions.InGameAndConnectedAndAliveAndProductStartedNotInPause)
        {

replace it by:

        while (Products.IsStarted)
        {
            if (Conditions.InGameAndConnectedAndAliveAndProductStartedNotInPause)
            { 

try to clean your code (remove unused part) and give more detail about your problem

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...