Bots, how do they work? Do they tell the video game a key was pressed or the mouse was clicked?
机器人,他们是如何工作的?他们是否告诉视频游戏按下了键或点击了鼠标?
If not is there a way to have your program tell another program a key was pressed? I would like to make a program to beat some game. So any resources or examples are appreciated.
如果没有,是否有办法让你的程序告诉另一个程序按下了一个键?我想做一个程序来击败一些游戏。因此,任何资源或示例都受到赞赏。
Update: So one way is to emulate keystrokes, so what are some methods to do this (in any language)?
更新:所以一种方法是模拟击键,那么有什么方法可以做到这一点(用任何语言)?
12 个解决方案
#1
I've written a bunch of bots at one time or another (from Pogo games to Yohoho Puzzle Pirates). For windows, you're usually going to either be sending Win32 events to simulate mouse movements, or spoof the actually low-level messages sent between windows when the mouse is actually clicked. A lot of it really depends on how the program reacts (by accepting the message with the coordinates, or, in Java's case, immediately reading the mouse coordinates). The "automation" part usually involves reading the screen and writing heuristics or algorithms for determining the state, but can also be as nice as packet sniffing (a lot of information there in poor poker implementations) or as hacky as reading memory locations directly. Pretty big "field", and poorly documented as it's pretty profitable and not hard to get into.
我曾经一次写过一堆机器人(从Pogo游戏到Yohoho Puzzle Pirates)。对于Windows,您通常要么发送Win32事件来模拟鼠标移动,要么在实际点击鼠标时欺骗窗口之间发送的实际低级别消息。很多它实际上取决于程序如何反应(通过接受带有坐标的消息,或者在Java的情况下,立即读取鼠标坐标)。 “自动化”部分通常涉及读取屏幕并编写用于确定状态的启发式算法或算法,但也可以像数据包嗅探一样好(扑克实施中的大量信息)或直接读取内存位置的hacky。相当大的“领域”,并且记录不佳,因为它非常有利可图并且不难进入。
Sending Input
C/C++ (in Windows)
For keys, try CodeProject:
对于密钥,请尝试CodeProject:
http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx
And messages:
http://www.codeproject.com/KB/threads/sendmsg.aspx
Your best bet is to learn to send messages using the Win32 API, then use something like Spy++ or its derivatives to "reverse engineer" how KeyPresses and mouse movements are sent to the window.
最好的办法是学习使用Win32 API发送消息,然后使用类似Spy ++或其衍生物的东西来“逆向工程”KeyPresses和鼠标移动如何发送到窗口。
Java
Java has an amazingly portable Robot class that is able to:
Java有一个非常便携的Robot类,它能够:
- Read Pixels from the screen.
- Control the mouse.
- Send keys.
从屏幕上读取像素。
控制鼠标。
I'd give that a shot if you're looking for quick and easy.
如果你正在寻找快捷方便,我会给你一个机会。
Basic Logic
This is described elsewhere on the internet in depth, but most bots follow a simple state-machine program flow. You read the screen (or packets, or memory), find out what "state" you're in based on your readings and past data, do calculations, and send the result back out to the program.
这在互联网上的其他地方进行了深入的描述,但大多数机器人遵循简单的状态机程序流程。您阅读屏幕(或数据包或内存),根据您的读数和过去的数据找出您所处的“状态”,进行计算,并将结果发送回程序。
Reading the screen can be difficult, but can be made easier if you consider that a lot of times, there are a few "lucky" pixels relative to the window that will give you an idea of what state the program is in. The process of finding these pixels can be automated.
阅读屏幕可能很困难,但如果你多次考虑,可以使这个屏幕相对于窗口有一些“幸运”像素可以让你更容易理解程序所在的状态。找到这些像素可以自动化。
#2
Some programs (such as ones that grind for you in an MMORPG) are simply emulating keystrokes. A tool to do this is AutoIt.
某些程序(例如在MMORPG中为您研磨的程序)只是模拟击键。执行此操作的工具是AutoIt。
As for bots that actually play the games themselves, I've not really done anything with them, but I'm assuming they would use some sort of predefined set of actions, maybe a heuristic, but not likely.
对于实际上自己玩游戏的机器人,我并没有真正对它们做任何事情,但我假设它们会使用某种预定义的动作集,可能是启发式的,但不太可能。
#3
There are a couple of kinds of bots. From what I remember when I used to play CS (a long, long time ago)
有几种机器人。从我记得以前我曾经玩CS(很久很久以前)
Some bots use the libraries of the application itself to inspect the environment (the map, where players are etc...) The bots also use the library to control movement. I doubt there are many bot implementations that invoke mouse messages to tell the game how to move etc...
一些机器人使用应用程序本身的库来检查环境(地图,玩家等等......)机器人也使用库来控制移动。我怀疑有很多机器人实现调用鼠标消息告诉游戏如何移动等...
Some bots emulate the application level protocol (if it's multi-player). So in other words, the game doesn't even need to run. A bot can run in a virtual game.
一些机器人模拟应用程序级协议(如果它是多人游戏)。换句话说,游戏甚至不需要运行。机器人可以在虚拟游戏中运行。
I'm going to run counter to what one other poster has suggested: writing a game bot is probably a more valuable exercise than actually playing the game, and being curious about how they work is a good sign. :)
我将与另一张海报提出的建议背道而驰:编写一个游戏机器人可能比实际玩游戏更有价值,并且对它们如何工作感到好奇是一个好兆头。 :)
#4
The biggest flaw in this concept is not the input, but the game output. Your bot can't just randomly press keys, it needs to know what is "happening". Very few games allow you to query this data which would leave your bot to solve a very tricky problem - Converting 60 frames per second of 2D visual data into a virtual space.
这个概念中最大的缺陷不是输入,而是游戏输出。你的机器人不能随便按键,它需要知道什么是“正在发生”。很少有游戏允许您查询这些数据,这将使您的机器人解决一个非常棘手的问题 - 将每秒60帧的2D视觉数据转换为虚拟空间。
There are actuially games out there that not only allow bot players, but encourage them. Open RTS is one of those but there are also simpler ones like GUN-TACTYX and crobots which are good starting points.
那里有精确的游戏,不仅允许机器人玩家,而且鼓励他们。开放式RTS是其中之一,但也有更简单的,如GUN-TACTYX和crobots,它们是很好的起点。
#5
Bots will be acting as clients to the game (server). Then, they can send appropriate data just as a user may be sending manually. The bot(client) will be analysing the data from the server ("someone is about to attack from the left"), et cetera. The client should be then able to compute the best move and send the appropriate data to the server to execute it.
Bots将充当游戏(服务器)的客户端。然后,他们可以发送适当的数据,就像用户可能手动发送一样。机器人(客户端)将分析来自服务器的数据(“某人即将从左侧攻击”),等等。然后,客户端应该能够计算最佳移动并将适当的数据发送到服务器以执行它。
I am not sure if this will work with all games.
我不确定这是否适用于所有游戏。
#6
It very much depends on the game. For example I made a bot for Travian (which is an online game) that used the internet explorer activeX to automate moves. I worte the application in c# and to get it to do stuff took about 30 minute. It's simple to call methods on the activeX an make it do things like fill in text fields or click on buttons. For login for example i used the folowing :
这在很大程度上取决于游戏。例如,我为Travian(这是一个在线游戏)制作了一个机器人,它使用Internet Explorer activeX自动化移动。我在c#中使用该应用程序,并让它做了大约30分钟的事情。在activeX上调用方法很简单,它可以执行填写文本字段或单击按钮等操作。对于登录例如我使用了以下内容:
var inputs = web.Document.GetElementsByTagName("input").
Cast<HtmlElement>();
var nume = inputs.First(h => h.GetAttribute("type") == "text");
var pass = inputs.First(h => h.GetAttribute("type") == "password");
var login = inputs.First(h => h.GetAttribute("type") == "image");
var form = web.Document.GetElementsByTagName("form")[0];
nume.SetAttribute("value", "user");
pass.SetAttribute("value", "pass");
login.InvokeMember("Click");
For those games that are desktop based things get more complicated. Windows allows for one aplication to send messages to another application so i guess you could use that to send messages to a game (although wheather this works for games that use directX for input i don't know)
对于那些基于桌面的游戏来说,事情变得更加复杂。 Windows允许一个应用程序将消息发送到另一个应用程序,所以我猜你可以使用它来发送消息到游戏(虽然这适用于使用directX输入的游戏我不知道)
#7
A bot like you describe it will simply emulate key presses and mouse movements/clicks, so the game cannot tell if a human is playing or another program. It would not have to interface with the game at all.
像你描述的机器人将简单地模仿按键和鼠标移动/点击,因此游戏无法判断人类是在玩还是其他程序。它根本不必与游戏接口。
#8
You can emulate mouse movements/clicks and keyboard input in Java with the Robot class. It also lets you take screen shots.
您可以使用Robot类模拟Java中的鼠标移动/单击和键盘输入。它还可以让您拍摄屏幕截图。
#9
I think AutoIT is a great language for doing this sort of software. Even though I haven't used it to build bots, sending clicks and keys was very easy.
我认为AutoIT是一种很好的语言来做这种软件。即使我没有用它来构建机器人,发送点击和按键也很容易。
#10
My understanding is that most high-end gaming bots use system calls to map themselves into the game client's memory space, reverse engineer its layout, and directly manipulate the client program's internal variables. That's way more work than I'd be up for just for fun.
我的理解是,大多数高端游戏机器人使用系统调用将自己映射到游戏客户端的内存空间,对其布局进行反向工程,并直接操纵客户端程序的内部变量。这比我为了好玩而做的工作更多。
#11
When you say "control a program" think about how programs are controlled. Most accept keyboard/mouse input.
当你说“控制程序”时,想一想如何控制程序。大多数人接受键盘/鼠标输入。
If you write a program to simulate keyboard/mouse input, there you go.
如果你编写一个程序来模拟键盘/鼠标输入,你去吧。
A corollary to this would be that if a program accepted arguments passed through an input box, write a program to send arguments as if they were sent through an input box.
这样做的必然结果是,如果程序接受通过输入框传递的参数,则编写一个程序来发送参数,就好像它们是通过输入框发送的一样。
This is how all bots work. Hope it sheds some light for you!
这就是所有机器人的工作方式。希望它为你带来一些亮点!
Edit: This is how the vast majority of bots work :-)
编辑:这是绝大多数机器人的工作方式:-)
#12
For this you need to know programming. However, there are programs that allow you to program with visual means. For them, you do not need to be able to program. An example is the program Kibor. It is designed specifically for your purposes and has a visual editor with which you can create bots with block diagrams. Description of the program Kibor
为此,您需要了解编程。但是,有些程序允许您使用可视方式进行编程。对他们来说,你不需要能够编程。一个例子是程序Kibor。它专为您的目的而设计,并具有可视化编辑器,您可以使用它来创建带有方框图的机器人。程序描述Kibor
It has many ready-made functions for bots. There is even a built-in text recognition module Screenshot Kibor. Text recognising
它有许多现成的机器人功能。甚至还有一个内置的文本识别模块Screenshot Kibor。文字识别
#1
I've written a bunch of bots at one time or another (from Pogo games to Yohoho Puzzle Pirates). For windows, you're usually going to either be sending Win32 events to simulate mouse movements, or spoof the actually low-level messages sent between windows when the mouse is actually clicked. A lot of it really depends on how the program reacts (by accepting the message with the coordinates, or, in Java's case, immediately reading the mouse coordinates). The "automation" part usually involves reading the screen and writing heuristics or algorithms for determining the state, but can also be as nice as packet sniffing (a lot of information there in poor poker implementations) or as hacky as reading memory locations directly. Pretty big "field", and poorly documented as it's pretty profitable and not hard to get into.
我曾经一次写过一堆机器人(从Pogo游戏到Yohoho Puzzle Pirates)。对于Windows,您通常要么发送Win32事件来模拟鼠标移动,要么在实际点击鼠标时欺骗窗口之间发送的实际低级别消息。很多它实际上取决于程序如何反应(通过接受带有坐标的消息,或者在Java的情况下,立即读取鼠标坐标)。 “自动化”部分通常涉及读取屏幕并编写用于确定状态的启发式算法或算法,但也可以像数据包嗅探一样好(扑克实施中的大量信息)或直接读取内存位置的hacky。相当大的“领域”,并且记录不佳,因为它非常有利可图并且不难进入。
Sending Input
C/C++ (in Windows)
For keys, try CodeProject:
对于密钥,请尝试CodeProject:
http://www.codeproject.com/KB/cpp/sendkeys_cpp_Article.aspx
And messages:
http://www.codeproject.com/KB/threads/sendmsg.aspx
Your best bet is to learn to send messages using the Win32 API, then use something like Spy++ or its derivatives to "reverse engineer" how KeyPresses and mouse movements are sent to the window.
最好的办法是学习使用Win32 API发送消息,然后使用类似Spy ++或其衍生物的东西来“逆向工程”KeyPresses和鼠标移动如何发送到窗口。
Java
Java has an amazingly portable Robot class that is able to:
Java有一个非常便携的Robot类,它能够:
- Read Pixels from the screen.
- Control the mouse.
- Send keys.
从屏幕上读取像素。
控制鼠标。
I'd give that a shot if you're looking for quick and easy.
如果你正在寻找快捷方便,我会给你一个机会。
Basic Logic
This is described elsewhere on the internet in depth, but most bots follow a simple state-machine program flow. You read the screen (or packets, or memory), find out what "state" you're in based on your readings and past data, do calculations, and send the result back out to the program.
这在互联网上的其他地方进行了深入的描述,但大多数机器人遵循简单的状态机程序流程。您阅读屏幕(或数据包或内存),根据您的读数和过去的数据找出您所处的“状态”,进行计算,并将结果发送回程序。
Reading the screen can be difficult, but can be made easier if you consider that a lot of times, there are a few "lucky" pixels relative to the window that will give you an idea of what state the program is in. The process of finding these pixels can be automated.
阅读屏幕可能很困难,但如果你多次考虑,可以使这个屏幕相对于窗口有一些“幸运”像素可以让你更容易理解程序所在的状态。找到这些像素可以自动化。
#2
Some programs (such as ones that grind for you in an MMORPG) are simply emulating keystrokes. A tool to do this is AutoIt.
某些程序(例如在MMORPG中为您研磨的程序)只是模拟击键。执行此操作的工具是AutoIt。
As for bots that actually play the games themselves, I've not really done anything with them, but I'm assuming they would use some sort of predefined set of actions, maybe a heuristic, but not likely.
对于实际上自己玩游戏的机器人,我并没有真正对它们做任何事情,但我假设它们会使用某种预定义的动作集,可能是启发式的,但不太可能。
#3
There are a couple of kinds of bots. From what I remember when I used to play CS (a long, long time ago)
有几种机器人。从我记得以前我曾经玩CS(很久很久以前)
Some bots use the libraries of the application itself to inspect the environment (the map, where players are etc...) The bots also use the library to control movement. I doubt there are many bot implementations that invoke mouse messages to tell the game how to move etc...
一些机器人使用应用程序本身的库来检查环境(地图,玩家等等......)机器人也使用库来控制移动。我怀疑有很多机器人实现调用鼠标消息告诉游戏如何移动等...
Some bots emulate the application level protocol (if it's multi-player). So in other words, the game doesn't even need to run. A bot can run in a virtual game.
一些机器人模拟应用程序级协议(如果它是多人游戏)。换句话说,游戏甚至不需要运行。机器人可以在虚拟游戏中运行。
I'm going to run counter to what one other poster has suggested: writing a game bot is probably a more valuable exercise than actually playing the game, and being curious about how they work is a good sign. :)
我将与另一张海报提出的建议背道而驰:编写一个游戏机器人可能比实际玩游戏更有价值,并且对它们如何工作感到好奇是一个好兆头。 :)
#4
The biggest flaw in this concept is not the input, but the game output. Your bot can't just randomly press keys, it needs to know what is "happening". Very few games allow you to query this data which would leave your bot to solve a very tricky problem - Converting 60 frames per second of 2D visual data into a virtual space.
这个概念中最大的缺陷不是输入,而是游戏输出。你的机器人不能随便按键,它需要知道什么是“正在发生”。很少有游戏允许您查询这些数据,这将使您的机器人解决一个非常棘手的问题 - 将每秒60帧的2D视觉数据转换为虚拟空间。
There are actuially games out there that not only allow bot players, but encourage them. Open RTS is one of those but there are also simpler ones like GUN-TACTYX and crobots which are good starting points.
那里有精确的游戏,不仅允许机器人玩家,而且鼓励他们。开放式RTS是其中之一,但也有更简单的,如GUN-TACTYX和crobots,它们是很好的起点。
#5
Bots will be acting as clients to the game (server). Then, they can send appropriate data just as a user may be sending manually. The bot(client) will be analysing the data from the server ("someone is about to attack from the left"), et cetera. The client should be then able to compute the best move and send the appropriate data to the server to execute it.
Bots将充当游戏(服务器)的客户端。然后,他们可以发送适当的数据,就像用户可能手动发送一样。机器人(客户端)将分析来自服务器的数据(“某人即将从左侧攻击”),等等。然后,客户端应该能够计算最佳移动并将适当的数据发送到服务器以执行它。
I am not sure if this will work with all games.
我不确定这是否适用于所有游戏。
#6
It very much depends on the game. For example I made a bot for Travian (which is an online game) that used the internet explorer activeX to automate moves. I worte the application in c# and to get it to do stuff took about 30 minute. It's simple to call methods on the activeX an make it do things like fill in text fields or click on buttons. For login for example i used the folowing :
这在很大程度上取决于游戏。例如,我为Travian(这是一个在线游戏)制作了一个机器人,它使用Internet Explorer activeX自动化移动。我在c#中使用该应用程序,并让它做了大约30分钟的事情。在activeX上调用方法很简单,它可以执行填写文本字段或单击按钮等操作。对于登录例如我使用了以下内容:
var inputs = web.Document.GetElementsByTagName("input").
Cast<HtmlElement>();
var nume = inputs.First(h => h.GetAttribute("type") == "text");
var pass = inputs.First(h => h.GetAttribute("type") == "password");
var login = inputs.First(h => h.GetAttribute("type") == "image");
var form = web.Document.GetElementsByTagName("form")[0];
nume.SetAttribute("value", "user");
pass.SetAttribute("value", "pass");
login.InvokeMember("Click");
For those games that are desktop based things get more complicated. Windows allows for one aplication to send messages to another application so i guess you could use that to send messages to a game (although wheather this works for games that use directX for input i don't know)
对于那些基于桌面的游戏来说,事情变得更加复杂。 Windows允许一个应用程序将消息发送到另一个应用程序,所以我猜你可以使用它来发送消息到游戏(虽然这适用于使用directX输入的游戏我不知道)
#7
A bot like you describe it will simply emulate key presses and mouse movements/clicks, so the game cannot tell if a human is playing or another program. It would not have to interface with the game at all.
像你描述的机器人将简单地模仿按键和鼠标移动/点击,因此游戏无法判断人类是在玩还是其他程序。它根本不必与游戏接口。
#8
You can emulate mouse movements/clicks and keyboard input in Java with the Robot class. It also lets you take screen shots.
您可以使用Robot类模拟Java中的鼠标移动/单击和键盘输入。它还可以让您拍摄屏幕截图。
#9
I think AutoIT is a great language for doing this sort of software. Even though I haven't used it to build bots, sending clicks and keys was very easy.
我认为AutoIT是一种很好的语言来做这种软件。即使我没有用它来构建机器人,发送点击和按键也很容易。
#10
My understanding is that most high-end gaming bots use system calls to map themselves into the game client's memory space, reverse engineer its layout, and directly manipulate the client program's internal variables. That's way more work than I'd be up for just for fun.
我的理解是,大多数高端游戏机器人使用系统调用将自己映射到游戏客户端的内存空间,对其布局进行反向工程,并直接操纵客户端程序的内部变量。这比我为了好玩而做的工作更多。
#11
When you say "control a program" think about how programs are controlled. Most accept keyboard/mouse input.
当你说“控制程序”时,想一想如何控制程序。大多数人接受键盘/鼠标输入。
If you write a program to simulate keyboard/mouse input, there you go.
如果你编写一个程序来模拟键盘/鼠标输入,你去吧。
A corollary to this would be that if a program accepted arguments passed through an input box, write a program to send arguments as if they were sent through an input box.
这样做的必然结果是,如果程序接受通过输入框传递的参数,则编写一个程序来发送参数,就好像它们是通过输入框发送的一样。
This is how all bots work. Hope it sheds some light for you!
这就是所有机器人的工作方式。希望它为你带来一些亮点!
Edit: This is how the vast majority of bots work :-)
编辑:这是绝大多数机器人的工作方式:-)
#12
For this you need to know programming. However, there are programs that allow you to program with visual means. For them, you do not need to be able to program. An example is the program Kibor. It is designed specifically for your purposes and has a visual editor with which you can create bots with block diagrams. Description of the program Kibor
为此,您需要了解编程。但是,有些程序允许您使用可视方式进行编程。对他们来说,你不需要能够编程。一个例子是程序Kibor。它专为您的目的而设计,并具有可视化编辑器,您可以使用它来创建带有方框图的机器人。程序描述Kibor
It has many ready-made functions for bots. There is even a built-in text recognition module Screenshot Kibor. Text recognising
它有许多现成的机器人功能。甚至还有一个内置的文本识别模块Screenshot Kibor。文字识别