Pixel Bot Development C# Diablo menu

User Tag List

Results 1 to 2 of 2
  1. #1
    Nazban's Avatar Member
    Reputation
    1
    Join Date
    May 2013
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pixel Bot Development C# Diablo

    So I'm attempting to develop a pixel bot for Diablo 2 or any game for that matter that can run in windowed mode. I currently have a working prototype based off this thread:
    https://www.ownedcore.com/forums/dia...how-bot-c.html ([Source] How to BOT in C#)

    Let me start off with sharing my intentions and goals for this project then we can get to the real meat the C# code and development.
    The intention is to further grow my skills and anyone else who wants to help in this journey. Also do everything without memory peaking or anything that could possibly get you banned or detected.
    This project is NOT intended for commercial use, or the fastest non client bot, or even designed to resell for major bot farms.
    My goal is to create a diablo 2 bot that will be able to do these features fully through pixel detection and mouse clicks:
    - Kill Monsters
    - Detect health
    - Mouse and Keyboard output to windowed Diablo 2
    - Looting items
    - Auto pathing maybe?
    - Other features like auto leveling, skilling etc will be more advanced features and come late in the project.

    Now for the code and concept I currently have based off the thread I posted above:
    I'm using VS2015 C# .Net 4.5.2 - Console Application (Since windows forms goes full retard on while loops)

    First thing we need to do is get the handle of the diablo window
    In the code below we are looking for a Process by the name of "Game" (which is d2's process name)
    If we have more than 1 process we need to throw an error since we don't know which process to use.
    If we have just 1 process we are good and we return the handle: return processes[0].MainWindowHandle; (we use 0 in the process array)
    All the other outcomes will return a IntPtr.Zero letting our code know we haven't found the d2 process.
    Code:
            public static IntPtr getD2WinHandle()
            {
                Process[] processes = Process.GetProcessesByName("Game");
    
                if (processes.Count() > 1)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("ERROR: More than 1 process detected.");
                    Console.ForegroundColor = ConsoleColor.White;
                    return IntPtr.Zero;
                }
    
                if (processes.Count() == 0)
                {
                    Console.WriteLine("Open Diablo 2...");
                    Thread.Sleep(2000);
                    return IntPtr.Zero;
                }
    
                if (processes.Count() == 1)
                {
                    return processes[0].MainWindowHandle;
                }
    
                return IntPtr.Zero;
            }
    Lets now focus on our controls a mouse click:
    We need to simulate a real mouse click, each click has a Down and Up event. If we just called the down event then the computer will think we are dragging the mouse so we call both.
    Anywhere in our code we can call the MouseClick() function to simulate a click.
    Code:
            private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
            [DllImport("user32.dll")]
            static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInfo);
    
            private static void MouseClick()
            {
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
    Our next control is mouse/cursor movement and position.
    We can call the MoveMouseToPosition() function anywhere in our code we just need to define 3 things.
    d2 process handle then x & y coordinates, the reason we need the d2handle is so we are referencing coordinates only on that window and not the entire desktop.
    Example call of the function: MoveMouseToPosition(D2handle, 150, 30);
    Code:
            using System.Drawing;    // be sure to add this at the top with the rest of your usings this is needed for Point
    
            [DllImport("user32.dll")]
            private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
            static private Point ConvertToScreenPixel(Point point, IntPtr hwnd)
            {
                Rectangle rect;
    
                GetWindowRect(hwnd, out rect);
    
                Point ret = new Point();
    
                ret.X = rect.Location.X + point.X;
                ret.Y = rect.Location.Y + point.Y;
    
                return ret;
            }
    
            [DllImport("user32.dll")]
            private static extern bool SetCursorPos(int x, int y);
    
            private static void MoveMouseToPosition(IntPtr hwnd, int x, int y)
            {
                Point point = new Point(0, 0);
                Point windowPoint = ConvertToScreenPixel(point, hwnd);
                Console.WriteLine("Ret: " + ConvertToScreenPixel(point, hwnd));
                SetCursorPos(windowPoint.X + x, windowPoint.Y + y);
            }
    Now that we have our d2handle(diablo window) and our controls ready to go we need to detect pixels and color:
    Similar to the MousePosition function we are defining our d2handle, x & y coordinates for our GetPixelColor() function which returns a color based on the x&y coords.

    Code:
            [DllImport("user32.dll")]
            static extern IntPtr GetDC(IntPtr hwnd);
    
            [DllImport("user32.dll")]
            static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
    
            [DllImport("gdi32.dll")]
            static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
    
            static public Color GetPixelColor(IntPtr hwnd, int x, int y)
            {
                IntPtr hdc = GetDC(hwnd);
                uint pixel = GetPixel(hdc, x, y);
                ReleaseDC(hwnd, hdc);
                Color color = Color.FromArgb((int)(pixel & 0x000000FF),
                                (int)(pixel & 0x0000FF00) >> 8,
                                (int)(pixel & 0x00FF0000) >> 16);
                return color;
            }
    Example of using GetPixelColor():
    Code:
                        Color pixColor = GetPixelColor(D2handle, 150, 30);
                        Console.WriteLine(pixColor.Name);
                        // pixColor.Name is a string and would look like this: "ff404040"
    Here is the testing code I have in its entirety:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Windows.Input;
    
    namespace D2Bot
    {
        class Program
        {
            [DllImport("user32.dll")]
            static extern IntPtr GetDC(IntPtr hwnd);
    
            [DllImport("user32.dll")]
            static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
    
            [DllImport("gdi32.dll")]
            static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
    
            private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
            [DllImport("user32.dll")]
            static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInfo);
    
            [DllImport("user32.dll")]
            private static extern bool SetCursorPos(int x, int y);
    
            [DllImport("user32.dll")]
            private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
            static private Point ConvertToScreenPixel(Point point, IntPtr hwnd)
            {
                Rectangle rect;
    
                GetWindowRect(hwnd, out rect);
    
                Point ret = new Point();
    
                ret.X = rect.Location.X + point.X;
                ret.Y = rect.Location.Y + point.Y;
    
                return ret;
            }
    
            static void Main(string[] args)
            {
                IntPtr D2handle = IntPtr.Zero;
                bool IsD2Open = false;
    
                while (!IsD2Open)
                {
                    D2handle = getD2WinHandle();
    
                    if (D2handle != IntPtr.Zero)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Found D2 Handle: " + D2handle.ToString());
                        Console.ForegroundColor = ConsoleColor.White;
                        IsD2Open = true;
                    }
                }
    
                while(IsD2Open)
                {
                    D2handle = getD2WinHandle();
    
                    if (D2handle != IntPtr.Zero)
                    {
                        //Bitmap bitmap = new Bitmap(Screen.)
    
                        Color pixColor = GetPixelColor(D2handle, 150, 30);
                        Console.WriteLine(D2handle.ToString());
                        Console.WriteLine("Color @ 150x150: " + pixColor.Name);
    
                        switch (pixColor.Name)
                        {
                            case "ff404040":    // Intro
                                Console.WriteLine("Intro.");
                                MoveMouseToPosition(D2handle, 150, 30 + 25);
                                Thread.Sleep(1000);
                                MouseClick();
                                break;
                            case "ff241c10":    // Main Menu
                                Console.WriteLine("Main Menu.");
                                Console.WriteLine("Move Cursor.");
                                MoveMouseToPosition(D2handle, 450, 300 + 25);
                                Thread.Sleep(50);
                                Console.WriteLine("Mouse Click");
                                MouseClick();
                                Thread.Sleep(2000);
                                break;
                            case "ff1c1410":    // Single Player - Char Select
                                Console.WriteLine("Character Select");
                                Thread.Sleep(1000);
                                MoveMouseToPosition(D2handle, 450, 300 + 25);
                                MouseClick();
                                break;
                            default:
                                Console.WriteLine("Color @ 150x150: " + pixColor.Name);
                                break;
                        }
                        //Thread.Sleep(1000);
                        //Console.ReadLine();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("D2 has been closed. Reopen D2.");
                        Console.ForegroundColor = ConsoleColor.White;
                        IsD2Open = false;
                    }
                }
    
                Console.ReadLine();
            }
    
            private static void MoveMouseToPosition(IntPtr hwnd, int x, int y)
            {
                Point point = new Point(0, 0);
                Point windowPoint = ConvertToScreenPixel(point, hwnd);
                Console.WriteLine("Ret: " + ConvertToScreenPixel(point, hwnd));
                SetCursorPos(windowPoint.X + x, windowPoint.Y + y);
            }
    
            private static void MouseClick()
            {
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
    
            public static IntPtr getD2WinHandle()
            {
                Process[] processes = Process.GetProcessesByName("Game");
    
                if (processes.Count() > 1)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("ERROR: More than 1 process detected.");
                    Console.ForegroundColor = ConsoleColor.White;
                    return IntPtr.Zero;
                }
    
                if (processes.Count() == 0)
                {
                    Console.WriteLine("Open Diablo 2...");
                    Thread.Sleep(2000);
                    return IntPtr.Zero;
                }
    
                if (processes.Count() == 1)
                {
                    return processes[0].MainWindowHandle;
                }
    
                return IntPtr.Zero;
            }
    
            static public Color GetPixelColor(IntPtr hwnd, int x, int y)
            {
                IntPtr hdc = GetDC(hwnd);
                uint pixel = GetPixel(hdc, x, y);
                ReleaseDC(hwnd, hdc);
                Color color = Color.FromArgb((int)(pixel & 0x000000FF),
                                (int)(pixel & 0x0000FF00) >> 8,
                                (int)(pixel & 0x00FF0000) >> 16);
                return color;
            }
        }
    }
    So if you have followed along until now we currently can do the following:
    - Find the Diablo 2 Window
    - Detect pixel color based off x&y position
    - Move cursor to x&y position
    - Click a mouse

    Now the hard part... development comes in, we need to detect enemy monsters, player health and mana and lots of other things but we have the tools to do it. We will focus on this for now and later maybe path finding using game seed etc.
    If anyone has any questions, ideas or needs help, more explanation of the code or how to get this working please post!
    I will be updating this at the very least weekly to share my latest build and whatever I learn from this.
    Last edited by Nazban; 01-30-2020 at 02:37 PM.

    Pixel Bot Development C# Diablo
  2. #2
    Grablin's Avatar Member
    Reputation
    1
    Join Date
    Jul 2019
    Posts
    3
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how are things going there?

Similar Threads

  1. online game bot development
    By igoldcity in forum Programming
    Replies: 2
    Last Post: 08-11-2022, 11:50 AM
  2. [Bot] I have developed a fully functioning pixel bot and I am looking for a partner.
    By abromide in forum WoW Classic Bots and Programs
    Replies: 47
    Last Post: 05-16-2022, 07:50 PM
  3. [Buying] Paying very well for EverQuest Bot development (pixel bot, or open to any ideas)
    By rubberguard in forum General MMO Buy Sell Trade
    Replies: 1
    Last Post: 06-03-2019, 09:50 AM
  4. Replies: 20
    Last Post: 11-24-2013, 04:49 PM
  5. Replies: 18
    Last Post: 10-19-2012, 03:46 PM
All times are GMT -5. The time now is 10:25 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search