MAMEWorld >> EmuChat
View all threads Index   Threaded Mode Threaded  

Pages: 1

Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


MAME A.I.
#252540 - 04/22/11 08:46 PM


Well seeing as there are so few active discussions here my threshold for holding back for fear of adding noise to the boards has become low enough that I've decided to post to open discussion on a half-baked idea.

The half-baked idea is to implement a facility for supporting writing A.I. ('artificial intelligence') agents for playing arcade games using MAME.

There are many ways that one could approach this issue but I've already solidified my plans for some of it.

I already have patches to MAME that turn MAME into a library, allowing it to be linked into any program, giving that program easy facilities for starting and controlling emulated games, and receiving callbacks as the game is emulated. The API and callbacks will be familiar to the MAME developers as they do not deviate very far from what is provided to an OSD implementation:
Controlling the emulator:
- Start a game, running in the calling thread
- Provide callbacks to be made from the emulator as it runs
Callbacks, made frequently (essentially at the video refresh rate of the game):
- Startup percent complete (loading roms, initializing engine)
- Ask for current controllers state
- Provide a frame of video
- Provide a frame of audio
- Paused callback
- Callback that gives the code using the API an opportunity to make other
calls to change the game state:
- Pause
- Schedule exit
- Schedule hard/soft reset
- Save game state
- Load game state
- Alter a dipswitch setting

The only aspect of this that differs significantly from the callbacks that MAME makes into its OSD layer is the 'ask for current controllers state' callback. The API that MAME provides to its OSD layer is to allow the OSD to provide a callback per input device to be called to get the current input state for every device; libmame, like the OSD implementations, acquires the state to provide in these callbacks via a single poll done once in every 'update' call from the emulator, but whereas the OSD implementations internally poll this state, libmame makes a callback out to acquire this state, and the form of the query is in a single large structure containing every possible game input which the callback is expected to update with the current input state. The important criterion here being that I think that this provides a very simple way for any user of the library to provide controller state; it doesn't have to worry about N callbacks, one for each possible input device for the game, instead it has to worry about 1 callback, which expect to get an update of the state of all input devices for the game. libmame takes care of the gory details of mapping input device callbacks to code that pulls the data from the results of that single large 'poll all controllers state' callback made to the user of libmame.

This API provides a mechanism for any program to run a game via the MAME emulator, and to control it, get the video and audio that it produces, and provide controller inputs to it. The obvious use is to link this into a program that interfaces with a human to show them the video and audio and solicit their input. This is in fact what I am primarily using this for - an 'integrated' frontend where MAME runs in the frontend process itself and is under complete control of the frontend, which can more seamlessly interact with it than typical frontends which just launch MAME and then get out of the way.

But there is a secondary possibility, and that is to replace the human with an A.I. And the most useful way to do this would be to create another API that would add facilities for making it easy to write a program that provides an API to play a game. However, there is a very important caveat here that I haven't explored in great detail:

It is necessary to be able to tell the current game state for a given game on a frame by frame basis. One way to do this is to expect the A.I. to take video and audio frames and interpret the video pixels/audio frames directly and come up with their own internal representation of the game state from that. This would require very sophisticated image recognition techniques and it seems like it would be almost impossible for complex games. While it is an interesting aspect of game playing A.I., it's not one that I would expect every A.I. developer to want to have to deal with. So as an additional mechanism for getting game state, I would like to provide a definition of the game state for every game and a way to populate that game state from the running game. An example will help illustrate what I mean:

For Pac-man, the minimum game state that the A.I. would need is:
- Where on the screen is each ghost

The rest of it the A.I. could keep track of itself because it knows where Pac-man and each dot and power pill starts on the screen, and it knows what 'must have' happened on every game frame given its input. The only unknown is what the ghosts are doing because the game controls that, and so that is the only game state that would really need to be delivered to an A.I.

However, some other game state may be easy to deliver and may simplify some aspects of an A.I.:
- Where on the screen is Pac-man
- What dots are on the screen and where
- What power pills are on the screen and where
- What color are the ghosts (normal, blue, white)

Thus there is quite alot of variability in what one person may feel is game state that should be delivered to the A.I. versus what the A.I. should figure out for itself, even for a game as simple as Pac-man. My personal interests are not to write the 'eyes' and 'ears' of the A.I., but instead just the 'brains'. And so I personally would like the game state to tell me everything relevent that a human would normally know just from what their eyes and ears tell them. But others find the challenge of implementing the eyes and ears (i.e. turning pixels and audio into an ongoing accurate representation of the game state), so I would want to enable but not require that.

OK so for 'flavors' of the API that provide game state instead of video and audio to the A.I., where does this game state come from?

This is where I have uncertainty. I am just hoping and praying that it will be possible to deduce this information from values that can be extracted from the emulator in a knowable way each time a game is run. For example, for Pac-man:

- Would there be a specific set of memory addresses that contain the current locations of the ghosts that can be polled on every frame?

I am hoping that this is true. I know that the 'cheats' system of MAME exists and to me it demonstrates that there are certainly some aspects to games that can be intrusively read or written and consistently produce an expected result. I also know that these are very much on a game-by-game basis which would make adding 'game state' APIs a one-off for each game. This greatly adds to the work load of producing the framework for supporting an A.I.; but these can be done by people interested in writing the A.I.s so there can be an easy parallelism of the effort.

For complex games, this might be an intractible problem; I don't know. Especially if there are games that use lots of dynamic memory structured in ways that cannot be easily examined.

Here are some of the features that I would hope to be able to provide:

- libmame would be linked into a standalone program that would have a GUI that would:
- Allow the user to select a game to be run
- Allow the user to select the A.I. to be used to play the game
- Set up the game to be emulated:
- Configure dipswitches (difficulty, etc)
- Provide a canned input sequence to start the game
- For example, wait 5 seconds, add a coin, press 1 player start
- Would be very useful to provide further inputs to get the game into a known state to make things harder for the A.I. (for example, starting Pac-man with a few seconds of random play so that the A.I. could not just use a pattern for the level)
- Set up parameters controlling the "rules":
- Does the A.I. have to provide its input 'in real time', which means that has to calculate its next move in the interval between frames in the game run at real time speeds, or can it instead take as much time as it wants to, slowing the game down from real time speeds?
- Start the game
- Show the game as it is emulated and driven by the A.I., including showing the controller inputs that the A.I. is delivering on every frame
- Launch the A.I. in a separate thread and allow it to register a callback that gives it the current 'game state' and a 'controllers state' structure for the A.I. to fill in with the controller inputs that it wants to deliver as a result of that game state

In terms of what language the A.I. should be written in; the core is C++ since that is the language of libmame and MAME. And so an A.I. could be written as C++ methods, linked into a shared object library that would be dynamically loaded at runtime when the user selects that library as the A.I. to run. But wrappers could be made that would allow other languages to participate; the A.I. library could include, for example, a Java VM that would be run and the C++ callback to provide game state and query controller input would be turned into a call into the VM to provide these to a Java thread running within the VM. Then developers could write their A.I. as Java classes which would then be loaded by the VM to provide the game A.I. Similarly for other interpreted languages.

So what is the goal of all this? Well, mostly I just think it would be really fun to be able to write A.I. programs to play old arcade games. And the facilities I described here would allow anyone to focus on the A.I., with all of the details of controlling the emulator and displaying the results taken care of for them. I envision a few interesting possibilities:

- Contests to see who could write the best A.I. for a given game
- Head-to-head A.I. versus A.I. in any competitive 2 or more player game (fighting games would be good ones)
- 'Aftermarket' A.I.s to control the CPU player in player-vs-cpu games (for example, making different A.I.s to control the non-human opponent in e.g. Street Fighter II)
- Learning environemnts like A.I. classes at schools and universities
- Challenges like writing an A.I. that can beat the best human score in games like Donkey Kong etc.
- Advanced 'attract mode' for arcade cabinets where the games actually play themselves

Well anyway I'm open to any and all discussion on this topic. This is not just idle speculation as I intend to make real efforts on this project after I finish with my more immediate project of my integrated frontend program and subsequent MAME cabinet (which itself may take years at the current rate ...).



Heihachi_73
I am the Table!
Reged: 10/29/03
Posts: 1074
Loc: Melbourne, Australia
Send PM


MAME A.I.: Ultimate Mortal Kombat 3 new [Re: Bryan Ischo]
#252573 - 04/23/11 01:43 AM


This would be interesting to see in Ultimate Mortal Kombat 3, when the computer starts 'cheating' with its MK Walker mode.

Game AI example 1: Player repeatedly taps the stick towards the CPU; CPU perfectly mimics this ultra-fast movement.
Game AI example 2: Player jump kicks, CPU jump kicks and always hits first (Kabal will throw an air fireball instead).
Game AI example 3: Player attempts an uppercut (or high roundhouse kick), CPU immediately ducks and uppercuts you within 1/10th of a second.
Game AI example 4: Player attempts to throw the CPU, CPU throws them instead. This can also happen *after* you have defeated the CPU and it says FINISH HIM on the screen!
Game AI example 5: Sektor (and Smoke) can combo using multiple teleport uppercuts in succession. As a player, you have to wait for the move's timer to reset before it will work again.
Game AI example 6: Cyrax can throw multiple green nets at you without having to wait (if you attempt this, your Cyrax will simply do sweeps until the projectile timer is reset).
Game AI example 7: Jade can use her anti-projectile (flash) move multiple times in quick succession, even whilst running towards you. For the player, the button combination cannot actually be done whilst running, since you have to press back, forward and high kick to perform it (when you tap 'back', you temporarily stop running). Note that she cannot avoid Cyrax's net however.
Game AI example 8: Motaro can throw multiple fireballs on the screen at once, if one fireball hits, the player is juggled into a combo which can reach beyond 80% damage.
Game AI example 9: When a CPU player defeats you, they can perform one of their combos; if you defeat the CPU and attempt to perform a combo to finish them, only the first hit registers and your player does their winning pose.
Game AI example 10: The CPU can perform an Animality on you, with no Mercy code beforehand.



Naoki
Reged: 11/10/09
Posts: 1998
Loc: United Kingdom
Send PM


tl;dr new [Re: Bryan Ischo]
#252582 - 04/23/11 02:57 AM


What is the project in a sumerized term?

it's late and massive paragraphs about tech get me connfused very quickly..



----
On a quest for Digital 573 and Dancing Stage EuroMix 2

By gods I've found it!



CptGuapo
Beat'em-ups Lover
Reged: 03/18/08
Posts: 342
Loc: Off to Never Never Land
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252614 - 04/23/11 05:50 AM


Hmmm, Bryan...That one is so "techie"/"devie" talk...Sometimes indecipherable...



"Mythology is what grownups believe, folklore is what they tell children and religion is both."



jumpmaniac81
Donkey Kong Maniac
Reged: 10/13/10
Posts: 696
Loc: N.J.
Send PM


Re: tl;dr new [Re: Naoki]
#252615 - 04/23/11 05:59 AM


i understand him.he's trying to say he's smarter.



I’m convinced Mario is a hobo.
He wakes up everyday in the same clothes, runs around in sewers, and collects coins for a living.
At the end of the day, he uses the coins to buy mushrooms



krick
Get Fuzzy
Reged: 02/09/04
Posts: 4235
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252616 - 04/23/11 05:59 AM


> For Pac-man, the minimum game state that the A.I. would need is:
> - Where on the screen is each ghost

Actually, if the emulation is truly deterministic, you should be able to program the pac-man "patterns" and just have your program just blindly do the moves without caring where the ghosts are. As far as I know, nothing in pac-man is random. All the ghost movements and positions are directly determined by the moves the player makes. Theoretically, given enough time, it should be possible to create a "script" that plays a flawless game.

Kind of like this...



On the other hand, if you want to make it actually look at the screen and react to the ghosts, you want something like this...




GroovyMAME support forum on BYOAC



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: krick]
#252624 - 04/23/11 07:54 AM


Yes, I mentioned in my original post that to prevent an A.I. from being able to just use a pattern, the program that runs the A.I. could be configured to use some canned input (not known to the A.I. writer) to put Pac-man in a random state before the A.I. takes over. Then it couldn't rely on being able to run a pattern. Of course something like this would have to be done at the start of every level ...



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: CptGuapo]
#252625 - 04/23/11 07:56 AM


> Hmmm, Bryan...That one is so "techie"/"devie" talk...Sometimes indecipherable...

It is geared towards developers. I don't know how to address developers directly so I post on this board. It's the only place I know of to engage in discussion with devs. The MAME team does not, unfortunately, have any kind of discussion forum specifically for devs, or at least not one that I've been invited to. It would be so awesome if the new Ruler of MAME would be inclined to improve the communication channels; the whole hush-hush nature of MAME development is hard to deal with from the outside.



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: tl;dr new [Re: Naoki]
#252626 - 04/23/11 07:58 AM


> What is the project in a sumerized term?

A framework for allowing developers to write "artificial intelligence" to play MAME games automatically, both for the challenge of making a game playing 'bot' and also to enhance player-vs-CPU games.

> it's late and massive paragraphs about tech get me connfused very quickly..

Even if you don't have a tech background you can contribute ideas to things that a game playing A.I. could be good for. Give yourself some credit.



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: tl;dr new [Re: jumpmaniac81]
#252627 - 04/23/11 08:00 AM


> i understand him.he's trying to say he's smarter.

Surely you will appreciate the consistent use of capitalization and punctuation in my post. I used enough to balance out about 100 of your posts ...



StilettoAdministrator
They're always after me Lucky ROMS!
Reged: 03/07/04
Posts: 6472
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252672 - 04/23/11 09:08 PM


I swear there is some "prior art" on this but I forget where.

[EDIT] never mind, I just found it again.
http://organicrobot.com/mame/
http://organicrobot.com/mame/mamereport.html
Old thread: http://forums.bannister.org/ubbthreads.php?ubb=showflat&Number=31641

- Stiletto



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252822 - 04/25/11 07:01 PM


What I was really hoping for was to get some comments from developers on this issue:

Is it generally feasable to detect 'game state' for games by reading memory or CPU register state of the emulation? For example, can I tell where all of the ghosts, dots, power pills, etc, are in Pac-man easily from the emulated state?

What about for slightly more complex games, e.g. 1943, where there are a non-fixed number of 'sprites' on the screen at a time?

What about reading the video hardware directly? Can one look at, e.g., the tilemap and the tile-based video memory of Pac-man to deduce game state?

I just want to know whether it is feasable to use these techniques; otherwise writing an A.I. to play a game will involve detecting game state from pixels, and that is alot harder.



R. Belmont
Cuckoo for IGAvania
Reged: 09/21/03
Posts: 9716
Loc: ECV-197 The Orville
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252825 - 04/25/11 07:07 PM


You tell us.

Seriously, I'm not being flippant, that's just not something the developers spend time on. Check Don Hodges' excellent writeups on game bugs for that kind of thing.



Sune
Connected
Reged: 09/21/03
Posts: 5648
Loc: Lagoa Santa, Brasil
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252831 - 04/25/11 08:07 PM


> I just want to know whether it is feasable to use these techniques; otherwise writing
> an A.I. to play a game will involve detecting game state from pixels, and that is
> alot harder.

There was a competition held (was it in Germany?) a while back where the aim was to create the best Asteroids-playing AI. I remember watching a few videos from the competition, they were all using the original arcade version of Asteroids.

I googled for a bit just now but I can't find a trace of it.

S



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: R. Belmont]
#252832 - 04/25/11 08:27 PM


> You tell us.
>
> Seriously, I'm not being flippant, that's just not something the developers spend
> time on. Check Don Hodges' excellent writeups on game bugs for that kind of thing.

OK; I will tell you once I figure it out. I had assumed that there were developers who had some idea on this because for example the cheats system exists and although I have ever even looked at it in any way, I am aware of it and assumed that being able to understand game memory layout well enough to write cheats meant having a general familiarity with how game state is represented in the emulated system.

And I only wanted feedback because I want to understand if it's already known that my ideas will fail before I try to go and implement them.



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: Stiletto]
#252833 - 04/25/11 08:35 PM


> I swear there is some "prior art" on this but I forget where.
>
> [EDIT] never mind, I just found it again.
> http://organicrobot.com/mame/
> http://organicrobot.com/mame/mamereport.html
> Old thread: http://forums.bannister.org/ubbthreads.php?ubb=showflat&Number=31641
>
> - Stiletto

Thank you, that is super helpful. As with any idea I've ever had, someone has already though of it already!

The primary difference between that and what I am proposing is that I propose game-specific logic to extract game state from emulator state (memory, CPU registers, and possibly video hardware memory) in addition to allowing pixel interpretation by the 'agent'.

I believe that the challenge of detecting game state by reading pixels (and let's not forget audio!) is soo much harder than actually designing the decision logic for deciding what controller inputs to give, that it is prohibitive for most people to even being to get into writing A.I. 'agents' for playing games.

I think that if there were a framework that allowed anyone to come up with rules on how to control the game based on predefined game state, that it would encourage alot more people to write intelligent agents for playing games, so I hope my ideas would get some traction.

All of this is hinged upon being able to discern game state from emulator state which is why I was hoping for some feedback. But thank you for your response, I will get in contact with the author of that software and try to share some ideas.



Bart T.
Reged: 01/07/06
Posts: 196
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252839 - 04/25/11 09:13 PM


As far as I know, cheat engines are remarkably simple tools that sift through memory looking for particular values and changes. There's not much that can be gleaned from them.

For simple games, you could probably deduce the game state by looking at what's going on in RAM, but it's not going to be easy for anything more complicated than Pac Man. What you would really have to do is reverse engineer (disassemble, decompile) the entire game code and understand it.

In other words, it'll be very game-specific and a colossal waste of time.

Attempting to build a computer vision-based AI would be even more monumentally challenging, unless you wrote it specifically for particular games. In that case, it might not be too difficult, as far as computer vision problems are concerned. If you know the sprites or shapes you're looking for, it would be really easy to detect and track their positions on the screen.

IMHO, the effort put into such a project, and the prerequisite knowledge required, would be better spent on more productive (and profitable) applications of computer vision and AI. What's the best case here? An AI version of the jerk who always played as Chun Li and never let anyone have their own shot at Street Fighter 2?

Edited by Bart Trzynadlowski (04/25/11 09:14 PM)



Bart



Foxhack
Furry guy
Reged: 01/30/04
Posts: 2409
Loc: Spicy Canada
Send PM


Re: MAME A.I. new [Re: Bart T.]
#252841 - 04/25/11 09:22 PM


> What's the best case here? An AI version of the jerk who always played
> as Chun Li and never let anyone have their own shot at Street Fighter 2?

Oh god, he's going to create the ultimate SNK Cheese Boss, isn't he? SKYNET IS COMING! SKYNET IS COMING!



Bryan Ischo
MAME Fan
Reged: 03/28/10
Posts: 358
Send PM


Re: MAME A.I. new [Re: Bart T.]
#252852 - 04/25/11 10:17 PM


> For simple games, you could probably deduce the game state by looking at what's going
> on in RAM, but it's not going to be easy for anything more complicated than Pac Man.
> What you would really have to do is reverse engineer (disassemble, decompile) the
> entire game code and understand it.

What about reading video memory? Don't tilemap-based games use relatively simple layouts? Would it be possible to deduce from the value at each spot in video memory, for tilemap-based-games, what tile is being displayed there?

> In other words, it'll be very game-specific and a colossal waste of time.

I expected it to be very game-specific, that goes without saying.

> Attempting to build a computer vision-based AI would be even more monumentally
> challenging, unless you wrote it specifically for particular games. In that case, it
> might not be too difficult, as far as computer vision problems are concerned. If you
> know the sprites or shapes you're looking for, it would be really easy to detect and
> track their positions on the screen.
>
> IMHO, the effort put into such a project, and the prerequisite knowledge required,
> would be better spent on more productive (and profitable) applications of computer
> vision and AI. What's the best case here? An AI version of the jerk who always played
> as Chun Li and never let anyone have their own shot at Street Fighter 2?

I think it would be fun. If you do not (and it sounds like you do not) then you are welcome not to participate. Thank you for the information you have provided, though; it is useful.



R. Belmont
Cuckoo for IGAvania
Reged: 09/21/03
Posts: 9716
Loc: ECV-197 The Orville
Send PM


Re: MAME A.I. new [Re: Sune]
#252870 - 04/25/11 11:35 PM


> There was a competition held (was it in Germany?) a while back where the aim was to
> create the best Asteroids-playing AI. I remember watching a few videos from the
> competition, they were all using the original arcade version of Asteroids.

They used a pretty lightly modified version of SDLMAME if it's the one I'm thinking of. I don't recall seeing any results though.



Vas Crabb
BOFH
Reged: 12/13/05
Posts: 4462
Loc: Melbourne, Australia
Send PM


Re: MAME A.I. new [Re: Bryan Ischo]
#252892 - 04/26/11 04:26 AM


> What about reading video memory? Don't tilemap-based games use relatively simple
> layouts? Would it be possible to deduce from the value at each spot in video memory,
> for tilemap-based-games, what tile is being displayed there?

Prehistoric Isle would be a good candidate to do this way - one of the tilemaps is ROM-based, so you can work out where you are just from the offset into the tilemap. It always uses the same ranges of sprite RAM for player, player projectiles, enemies, explosions, etc.

Dooyong hardware shooters would be good candidates, too. There's relatively little interaction with the backgrounds (which are massive ROM-based tilemaps), and you can just pick up enemies and projectiles from sprite coordinates.



StilettoAdministrator
They're always after me Lucky ROMS!
Reged: 03/07/04
Posts: 6472
Send PM


Re: MAME A.I. new [Re: R. Belmont]
#252893 - 04/26/11 04:41 AM


> > There was a competition held (was it in Germany?) a while back where the aim was to
> > create the best Asteroids-playing AI. I remember watching a few videos from the
> > competition, they were all using the original arcade version of Asteroids.
>
> They used a pretty lightly modified version of SDLMAME if it's the one I'm thinking
> of. I don't recall seeing any results though.

I remember that project too, but not its name or anything else! LOL

- Stiletto



nuapete
Reged: 12/03/04
Posts: 1
Send PM


Re: MAME A.I. - asteroids client new [Re: Stiletto]
#252938 - 04/26/11 11:16 AM


Hi, this might be the one, someone posted it here way back and the website is still there!

http://www.heise.de/ct/creativ/08/02/details/

There's a downloadable sample client and all. As far as I recall, it used machine vision to play the game which struck me as something that'd be very intersting to look at. Some day.



couriersud
Reged: 03/27/07
Posts: 214
Send PM


Re: MAME A.I. - asteroids client new [Re: nuapete]
#253065 - 04/27/11 09:08 PM


The c't competition used vector ram contents.


Pages: 1

MAMEWorld >> EmuChat
View all threads Index   Threaded Mode Threaded  

Extra information Permissions
Moderator:  Robbbert, Tafoid 
1 registered and 317 anonymous users are browsing this forum.
You cannot start new topics
You cannot reply to topics
HTML is enabled
UBBCode is enabled
Thread views: 4819