Jump to content

Add/remove automaton target with C#


craine

Recommended Posts

Hello,

Is there a way to add/remove a character target in the automaton "npc to kill" list using a C# line ?

If yes, does the bot have to be stopped or can i add the target then restart it ?

Link to comment
Share on other sites

Hello, i think you can use the already built-in blacklist tool in WRobot which npc it should not attack but i don't know exactly, what do you want to achieve ( a special case which the character shouldn't do this on x condition?)

Link to comment
Share on other sites

The thing is, i try to create a  system that make the bot user able to play the game while being assisted by the bot (like wRotation but with more bot part).

For that, i use the party chat plugin from droidz to get input from the user to the bot. One of the assissted part will be a macro that start the bot with the automaton product to farm the entity the user targeted while using the macro.

Since automaton don't use a profile i wonder how i can manage all the entity the bot will farm.

The best case scenario would be a C# methods triggered by a command in game that add the current target to the list and restart the bot (or something like that).

After some research i didn't found anything like that in the api.

 

I'm not sure if i could use a new thread to setup a new automaton and get the target in game, it would be way more dificult to do.

Link to comment
Share on other sites

A nice thought to use the automaton for this task. But i agree it would be much work to make it happen. By your plugin you could use "Automaton.Bot.AutomatonSetting.CurrentSetting.ObjectNpcHarvest" to add npcs(you must add new line for each name: \n). But at first you must load the product in order to access it. If you have written the names to the settings you must save them.

So you could design the tool as plugin or maybe customprofile(start thread only from clicking on settings or even make a winform gui if you need to adjust something or add start/stop buttons).

 

Stuff that may help you(doesn't include things what i've already mentioned on other posts):

 

load/switch product: 

https://wrobot.eu/forums/topic/5507-changing-botbase/

 

execute macro & other relevant tasks: 

    public void Settings()
    {
	//start thread1 with the classes i presented on the other post
    }



    public void Thread1
    {
	//other things to initialize
	EventsLuaWithArgs.OnEventsLuaWithArgs += EventsLuaWithArgsOnOnEventsLuaWithArgs; //don't forget to unsubscribe if you abort your thread (-=)


	
	//add here while loop if needed

    }



    private void EventsLuaWithArgsOnOnEventsLuaWithArgs(LuaEventsId id, List<string> args)
    {
        if (id == LuaEventsId.EXECUTE_CHAT_LINE)
        {
            robotManager.Helpful.Logging.Write(args[0]);//if a macro is pressed or something on chat line is printed
      
      
      
      		if (args[0] == "/add target")//   "/add target"  your macro ingame to add targets
      		{
      			//get npc name by "ObjectManager.Me.TargetObject.Name" and save string variable somewhere
      			//add new entries like this: namestokill + "\n" + newname
      			//load product, configure settings with your npcname, save
      		}
      
      		if (args[0] == "/clear targetlist")
      		{
      			//clears your string variable
      		}
      
      		if (args[0] == "/startfarm")
      		{
      			//load product, start product
      		}
      
		
		//you can also stop automatically the product if a certain condition is met on the while loop on thread1
            	if (args[0] == "/stopfarm")
      		{
      			//stop product
      			
      		}
      
        }
    }

 

Link to comment
Share on other sites

Alright, after hours of try and thanks to your help, i managed to get a part of the result i wanted.

I'm now able to start extra thread using an ingame command setup by the party chat plugin. It result in a easy way to switch from one products to another with some basics settings.

example: i started the bot with wRotation and switch the bot to the automaton products with a "switch" command in game.

new Thread(() => {
 robotManager.Products.Products.ProductStop();
robotManager.Products.Products.LoadProducts("automaton");
Thread.Sleep(200);
Automaton.Bot.AutomatonSetting.CurrentSetting.ObjectNpcHarvest = "\n the entity to farm \n";
Thread.Sleep(200);
robotManager.Products.Products.ProductStart();
}).Start();

Unfortunately, i'm not able to stop the thread once it's started wich is... annoying.

i looked up at your thread manager from this post but i don't realy understand how it work and how you are able to abort thread once they are started.

If i give a name to that thread is it possible to abort it with another command or with the manual stop button ?

 

The second thing that miss in that command is a target, i may be blind but i didn't find anything in the api to get the in game target as a string. Any ideas ?

Thanks for your help, it realy matters with my low level in C# :unsure:

Link to comment
Share on other sites

Hello again, i have to inform you that you unfortunately cannot access Automaton.Bot.AutomatonSetting. My first thought was, it would be like on the grinder bot which granted me access to profiles & everything else.

The thread related code would work as well loading the different products.

Under this circumstances you have a few options:

-to use WRotation and a plugin to block the fightevents til you allow access (but you need to write the own behavior like to move on random vector3 & attack specific npcs)

-use xml writer to override the settings which you need & reload the product while using the plugin to start & stop

 

I think the second option would easier to implement. If i have time, i can look after the writer.

 

Edit: This is how you can use the threadmanager & cachemanager(without xml writer) but a bit unclean

using System;
using System.Collections.Generic;
using System.Threading;
using System.Runtime.Caching;
using robotManager.Helpful;
using robotManager.Products;
using wManager.Plugin;
using wManager.Wow.Enums;
using wManager.Wow.Helpers;
using Timer = robotManager.Helpful.Timer;

public class Main : IPlugin
{
    #region Variables

    private ThreadManager _threadManager = new ThreadManager();

    #endregion




    #region Classes

    public class CacheManager
    {
        public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration)
        {
            if (!IsIncache(cacheKey))
            {
                MemoryCache.Default.Add(cacheKey, savedItem, absoluteExpiration);
            }
            else
            {
                MemoryCache.Default.Set(cacheKey, savedItem, absoluteExpiration);
            }
        }

        public static T GetFromCache<T>(string cacheKey) where T : class
        {
            return MemoryCache.Default[cacheKey] as T;
        }

        public static void RemoveFromCache(string cacheKey)
        {
            MemoryCache.Default.Remove(cacheKey);
        }

        public static bool IsIncache(string cacheKey)
        {
            return MemoryCache.Default[cacheKey] != null;
        }

        public static bool IsIncache(Action method)
        {
            return MemoryCache.Default[method.Method.Name] != null;
        }
    }

    public class ThreadManager
    {
        public int TableId = 1;
        Dictionary<string, Thread> _threads = new Dictionary<string, Thread>();

        /// <summary>
        /// Starts a thread
        /// </summary>
        /// <param name="method">The method to start on</param>
        /// <param name="threadName">Define thread name</param>
        /// <param name="cache">Write thread also to memory cache</param>
        /// <param name="cacheExpiration">when the cache expires in memory (not on current instance!)</param>
        public void Start(Action method, string threadName, bool cache = false, DateTime cacheExpiration = default(DateTime))
        {
            if (!cache)
            {
                if (!_threads.ContainsKey(threadName))
                {
                    Thread Thread = new Thread(() => method());
                    Thread.Name = Convert.ToString(TableId);
                    Thread.Start();
                    _threads.Add(threadName, Thread);
                    Logging.Write("Start Thread " + threadName + ".");
                }
                else
                {
                    Logging.Write("Thread " + threadName + " already executed.");
                }
            }
            else
            {
                if (!CacheManager.IsIncache(threadName))
                {
                    Thread Thread = new Thread(() => method());
                    Thread.Name = Convert.ToString(TableId);
                    Thread.Start();
                    if (cacheExpiration == default(DateTime))
                    {
                        CacheManager.SaveTocache(threadName, Thread, DateTime.Now.AddMinutes(30));
                        Logging.Write("Start Thread " + threadName + ".\nExpires at " + DateTime.Now.AddMinutes(30));
                    }
                    else
                    {
                        CacheManager.SaveTocache(threadName, Thread, cacheExpiration);
                        Logging.Write("Start Thread " + threadName + ".\nExpires at " + cacheExpiration);
                    }
                }
                else
                {
                    Logging.Write("Thread " + threadName + " already executed.");
                }
            }
        }

        /// <summary>
        /// Starts a thread
        /// </summary>
        /// <param name="method">The method to start on</param>
        /// <param name="cache">Write thread also to memory cache</param>
        /// <param name="cacheExpiration">when the cache expires in memory (not on current instance!)</param>
        public void Start(Action method, bool cache = false, DateTime cacheExpiration = default(DateTime))
        {
            if (!cache)
            {
                if (!_threads.ContainsKey(method.Method.Name))
                {
                    Thread Thread = new Thread(() => method());
                    Thread.Name = Convert.ToString(TableId);
                    Thread.Start();
                    _threads.Add(method.Method.Name, Thread);
                    Logging.Write("Start Thread " + method.Method.Name + ".");
                }
                else
                {
                    Logging.Write("Thread " + method.Method.Name + " already executed.");
                }
            }
            else
            {
                if (!CacheManager.IsIncache(method.Method.Name))
                {
                    Thread Thread = new Thread(() => method());
                    Thread.Name = Convert.ToString(TableId);
                    Thread.Start();
                    if (cacheExpiration == default(DateTime))
                    {
                        CacheManager.SaveTocache(method.Method.Name, Thread, DateTime.Now.AddMinutes(30));
                        Logging.Write("Start Thread " + method.Method.Name + ".\nExpires at " + DateTime.Now.AddMinutes(30));
                    }
                    else
                    {
                        CacheManager.SaveTocache(method.Method.Name, Thread, cacheExpiration);
                        Logging.Write("Start Thread " + method.Method.Name + ".\nExpires at " + cacheExpiration);
                    }
                }
                else
                {
                    Logging.Write("Thread " + method.Method.Name + " already executed.");
                }
            }
        }

        /// <summary>
        /// Aborts a thread
        /// </summary>
        /// <param name="threadName">Define thread name</param>
        /// <param name="cache">Determine whether if thread on cache</param>
        public void Abort(string threadName, bool cache = false)
        {
            if (!cache)
            {
                if (_threads.ContainsKey(threadName))
                {
                    _threads[threadName].Abort();
                    _threads.Remove(threadName);
                    Logging.Write("Abort Thread " + threadName + ".");
                }
            }
            else
            {
                if (CacheManager.IsIncache(threadName))
                {
                    CacheManager.GetFromCache<Thread>(threadName).Abort();
                    CacheManager.RemoveFromCache(threadName);
                    Logging.Write("Abort Thread " + threadName + ".");
                }
                else
                {
                    Logging.Write("Thread " + threadName + "could not be found in cache.");
                }
            }
        }

        /// <summary>
        /// Aborts a thread
        /// </summary>
        /// <param name="method">The method which was associated with the thread</param>
        /// <param name="cache">Determine whether if thread on cache</param>
        public void Abort(Action method, bool cache = false)
        {
            if (!cache)
            {
                if (_threads.ContainsKey(method.Method.Name))
                {
                    _threads[method.Method.Name].Abort();
                    _threads.Remove(method.Method.Name);
                    Logging.Write("Abort Thread " + method.Method.Name + ".");
                }
            }
            else
            {
                if (CacheManager.IsIncache(method.Method.Name))
                {
                    CacheManager.GetFromCache<Thread>(method.Method.Name).Abort();
                    CacheManager.RemoveFromCache(method.Method.Name);
                    Logging.Write("Abort Thread " + method.Method.Name + ".");
                }
                else
                {
                    Logging.Write("Thread " + method.Method.Name + "could not be found in cache.");
                }
            }
        }
    }

    #endregion




    #region WRobot Methods

    public void Initialize()
    {

    }

    public void Dispose()
    {

    }

    public void Settings()
    {
        if (!CacheManager.IsIncache("Thread1"))
        {
            _threadManager.Start(Thread1, true, DateTime.Now.AddMinutes(60));
            CacheManager.SaveTocache("EventsLuaWithArgsOnOnEventsLuaWithArgs", _handler, DateTime.Now.AddMinutes(60));
            EventsLuaWithArgs.OnEventsLuaWithArgs += CacheManager.GetFromCache<EventsLuaWithArgs.EventsLuaWithArgsHandler>("EventsLuaWithArgsOnOnEventsLuaWithArgs");
        }
        else
        {
            Logging.Write("Dispose");
            EventsLuaWithArgs.OnEventsLuaWithArgs -= CacheManager.GetFromCache<EventsLuaWithArgs.EventsLuaWithArgsHandler>("EventsLuaWithArgsOnOnEventsLuaWithArgs");
            CacheManager.RemoveFromCache("EventsLuaWithArgsOnOnEventsLuaWithArgs");
            _threadManager.Abort(Thread1, true);
        }
    }

    #endregion




    #region Handler
    
    private EventsLuaWithArgs.EventsLuaWithArgsHandler _handler = delegate (LuaEventsId id, List<string> args)
    {
        if (id == LuaEventsId.EXECUTE_CHAT_LINE)
        {
            Logging.Write(args[0]);
            if (args[0] == "/start")
            {
                Products.LoadProducts("Automaton");
                Products.ProductStart();
            }
            if (args[0] == "/stop")
            {

                Products.ProductStop();
                //need to resubscribe events
                EventsLuaWithArgs.OnEventsLuaWithArgs -= CacheManager.GetFromCache<EventsLuaWithArgs.EventsLuaWithArgsHandler>("EventsLuaWithArgsOnOnEventsLuaWithArgs");
                EventsLuaWithArgs.OnEventsLuaWithArgs += CacheManager.GetFromCache<EventsLuaWithArgs.EventsLuaWithArgsHandler>("EventsLuaWithArgsOnOnEventsLuaWithArgs");
            }
        }
    };

    #endregion




    #region Threads

    public void Thread1()
    {
        var timer = new Timer(1000);
        timer.ForceReady();
        Logging.Write("Initialized Thread1");
        while (true)
        {
            try
            {
                if (timer.IsReady)
                {
                    Logging.Write("Pulse");
                    timer.Reset();
                }
            }
            catch (Exception e)
            {
                Logging.WriteError(e.ToString());
            }
            Thread.Sleep(30);
        }
    }

    #endregion
}

 

Link to comment
Share on other sites

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...