Coding an AI player

Joined
Apr 2, 2013
Messages
46,737
This idea came to me in the middle of an internal rant...the kind that doesn't make it out of my mouth or onto a keyboard but still may mean that I wasn't thinking with any great clarity.

I accept these things as facts:

1) The makers of Civ games think their games are not war games, so for them to code in any decent ability at managing wars in their AI would be unexpected, and after however many decades of observing that they haven't it seems unthinkable that they ever will.

2) Modders are limited in what they can do after the fact.

3) Despite the fact that they are absolutely terrible at it something in the AI coding makes them start wars they are not equipped to fight every time they turn around so even though it isn't a war game there's no way to play Civ without wars.

Now, here's the idea:

Would it be possible to code an actual AI player? As in you could set up a hotseat game and an AI completely outside the game code could play the game. Obviously input and output would have to be interfaced somehow, and of course coding a civ playing AI would be on par with a good chess AI as far as the coding challenge goes...but keep in mind that there are very good chess playing AIs.
 
How would it be economical to have AI as separate content? :)

I have to suppose that some not that heavy coding* might model the AI after the player's moves, when one has played a number of games. It would calculate decisions, with the setting estimated in code terms (ie obviously not actually simulate what the player decided and why, but more of mimicking strokes deemed in-code to be on specific situation).

*For a very crude and limited example: if the human player goes for building x wonder ALL times in the early part of the game, the AI will be set to do the same, which is actually a simple thing to set (Civ already has flags for that).
 
Obviously it wouldn't be economical...for the publisher. But as a Civ playing program external to the game itself it wouldn't be a copyright violation, so if someone did program it they could sell it.

By the way...what you described would be a learning AI, and that would be really hard to do. Which is not to say that creating an algorithm that could analyze all the 'board positions' available in Civ and apply some sort of weighted value would be easy. But adapting by observation of the human player seems even harder.
 
Would it be possible to code an actual AI player? As in you could set up a hotseat game and an AI completely outside the game code could play the game. Obviously input and output would have to be interfaced somehow, and of course coding a civ playing AI would be on par with a good chess AI as far as the coding challenge goes...but keep in mind that there are very good chess playing AIs.

In theory, why not.

You would need to code:

1. Sort of a system that takes in what's on the screen as input and builds its own internal representation of the game, mirroring it perfectly - This would be pretty hard to do and I would probably call it the "showstopper" problem in this scenario. Unless you're a large company like google or something and have access to software engineers and lots of time and resources and stuff. I recommend that the interface to the data is the modding API instead. I don't know anything about it so I have no idea what data you get access to and what you don't.. and besides it would mean that it would be an internal solution and not an external one, like you want. Alternatively you could try to build something that analyzes civ game saves and converts that data to an internal representation of the game. That might actually be possible but I have 0 experience processing Civ game saves so that is a complete guess on my part

2. Assuming that a system exists that loads in one civ game save per turn and converts it to an internal representation of the game, you'd probably then need to build a neural net. You would have to figure out good ways to weigh decisions, as in "Is this outcome better than this other outcome?" so that your neural net can run through the games through many iterations and adapt to the game and try new things, and learn, and so on. This is also a bit of a showstopper unless you have a large team of programmers and software engineers and computers set up to do the simulations on, and lots of time so that you can start over and try new models and approaches. IMO it would be a bit of crapshoot to try to build a neural net that can learn how to play Civ. There are so many moving pieces in the game, it would probably be very hard to program a system to know which outcome is better than another outcome, so that it can teach the in-game AI and prod it along in the right direction. Instead the best approach is probably (with well understood games like Civ IV or V) a manual encoding of strategies. Basically all the things we know about those games, and how to play them well, you encode that as a bunch of rules, and the computer mechanically follows it. Basically the way Civ in-game AI is coded now but with a lot more attention to detail. This is probably the only real way to do it.

3. Assuming you have the above two systems you would then need some sort of a mechanical device that can type on a keyboard, move a mouse, and relay the information back into the game. Some sort of a robot I guess. Maybe something like this
Spoiler :
 
Thanks for thinking Warpus!

On part one...yeah, working from a game save seems superior to working from screen data. A hot seat game, as I understand it, is basically turn by turn saved and "passed along" anyway. The "player" could interpret that save game while the running Civ program is happily converting it into the usual visual format, then "play it as it lies" without actually "looking" at it..

On part two...I was definitely thinking more of the encoding a bunch of rules, rather than trying to create a real learning intelligence. For the most part I think a lot of that is already done, though lifting it might break the boundary of copyright infringement. Mostly I think the AI (in Civ IV and SMAX, which are my usual turf) is pretty capable and makes descent decisions. It's just that they are so awful at war that they get buried in turn advantage too badly for any decision to turn out well.

On part three...everything you can do with a mouse can be done with a keystroke. I personally cannot be bothered learning the endless keyboard shortcuts and just mouse my way along, but that terminator fellow could probably just eat the quick reference card or something. Ultimately though a series of keystroke commands doesn't seem like it would be all that hard to simulate. My keyboard is plugged into a USB port, so a keyboard emulating AI player plugged into a USB port should be able to bypass Arnold and company pretty handily.
 
Obviously it wouldn't be economical...for the publisher. But as a Civ playing program external to the game itself it wouldn't be a copyright violation, so if someone did program it they could sell it.

By the way...what you described would be a learning AI, and that would be really hard to do. Which is not to say that creating an algorithm that could analyze all the 'board positions' available in Civ and apply some sort of weighted value would be easy. But adapting by observation of the human player seems even harder.

I didn't mean anything having to be difficult to code. Eg in AGS (some adventure-creating software) you can pretty easily use built-in code options so as to make the AI pick up your choice when you did X in room Y, and so the next time you go to that room the AI can do something against this so as to make your in-game life more difficult :) Eg if the 'good choice' was to open drawer #4, the AI can be coded to stand with its tentacles right in front of drawer #4 so you can't go there again. Now drawer #4 may not even mean anything else code-wise (ie it won't lead to any breakthrough), but if you stick to it then the AI will ruin your fun all the same. Think of it as telling someone what your plan is, and then that other person using it against you.

Now usually in a game you would see drawer X either be important or not from the start (eg there is a key or a note in it), but that is pretty linear. If all four drawers are empty, the player would choose (if he/she does open them again) out of less obvious reason. Surprise, though, the AI hates you, so your choice is not allowed now.

In a civ setting, though, the analogous drawer #4 would be trying to build wonder X. The AI will try the same, so as to ruin your fun. Info from past games can be easily stored anyway, so the code allows for making use of it, and this is just the most basic way it could.

Now on your part you seem to mean an AI specifically coded to be great in a specific game (eg chess, and analogously for civ). Yet a chess program AI doesn't learn anything about chess; it just makes moves due to coded relations of a relatively small pool of possible game developments. In a game like civ that pool of possible developments is far larger, if for no other reason then for size of the moving locations alone!
Keep in mind that current civ AI on higher difficulty levels just cheats (is given more production shields for the same terrain which gives you less, etc) ^^
 
The alpha go team is growing a Starcraft 2 player. There is talk about whether its actions should be throttled through a mechanical set of arms playing on a keyboard and mouse.

In the dev blogs firaxes has said they specifically don't design their AI to play like players because people don't enjoy it. I have mixed feelings but I definitely always preferred multiplayer...
 
Not possible in the way you want it. You need to still hook it into the code.
Which is in Civ4 (and later versions, I guess) possible.
In Civ4 we had the betterAI mod, so people tried ;).
But coding an AI is a tough thing. It's not so that you just apply 4 rules, and you're done.
I hooked in some AI in Python code once. It was basically a decision to override an AI's decision to give a specific promotion and to apply a custom promotion.
That code alone was already 30 lines, I think (and it probably messed the AI a bit up, since it wasn't taking the ongoing decisions into account).
 
Sure, you could, but at the point where I'm paying people to develop an entire API to read game data without access to the game code, I'd just spend a few million dollars to reimplement the entire game. (I'm assuming civ4, the best civ.) As long as you don't re-use artwork, sounds, and other copyrightable bits, you're fine on that front.

There's really nothing fundamentally different about a regular civ AI and an "AI player" as you call it, just an extra step to for every input and output.

For the most part I think a lot of that is already done, though lifting it might break the boundary of copyright infringement.

Doubtful, can't copyright game design and mechanics.

In the dev blogs firaxes has said they specifically don't design their AI to play like players because people don't enjoy it.

And if you're just trying to build the winningest AI, it's probably not going to play with players anyway, because it has different strengths than humans, and leans on those strengths to win. A good way to beat humans is to play in ways that drag the game in a direction that maximizes AI advantage. Helps to mitigate with better game design. (e.g. civ4 where there's overflow of resources typically can't be gamed much.)
 
Not possible in the way you want it. You need to still hook it into the code.
Which is in Civ4 (and later versions, I guess) possible.
In Civ4 we had the betterAI mod, so people tried ;).
But coding an AI is a tough thing. It's not so that you just apply 4 rules, and you're done.
I hooked in some AI in Python code once. It was basically a decision to override an AI's decision to give a specific promotion and to apply a custom promotion.
That code alone was already 30 lines, I think (and it probably messed the AI a bit up, since it wasn't taking the ongoing decisions into account).

:D I am sure it was fun :)
 
and of course coding a civ playing AI would be on par with a good chess AI as far as the coding challenge goes...
I think you are massively, MASSIVELY underestimating the complexity of the task here.
 
Can't we just throw a million games into some ML grinder?
 
Yes. Chess has a comparatively limited state space and there are already well known heuristics for what is a good and what a bad state.

Civ is more complex than Go by far. And we have no idea how to score what a good game state is beyond incredibly crude stuff.

Can't we just throw a million games into some ML grinder?
ML requires a model first. Creating the model is what is hard here.
 
Well on the topic of Civ4, Soren Johnson himself literally said once that its AI is "just high school programming". It's just an expert system programmed by developers who weren't even experts at playing the game they made.
 
And yet this Civ4 AI is by far the best the serie ever had :p
 
The killer for game AIs is that they are always programmed by people who have no experience playing the game...by definition. That's part of why I'm thinking an 'aftermarket' AI player might be a practical approach.
 
And yet this Civ4 AI is by far the best the serie ever had :p
I think this is because Tim is on to something here, warfare is among the hardest aspects of the game for AI (and also one where blunders have immediate consequences that are noticeable to a human). Since Civ5 took the direction of being a poor man's wargame, problems grew.

The killer for game AIs is that they are always programmed by people who have no experience playing the game...by definition. That's part of why I'm thinking an 'aftermarket' AI player might be a practical approach.
But there is no reason to put such a "late stage" AI beyond the peripherals of the game.

The problem is of course marketing and customer expectations. Good AI is not marketable, because its absence is not immediately noticeable. Prior to release, it's cheap to claim how great the AI is (favourite vacuous marketing phrase: "the AI will react dynamically to the player's actions". Well I hope it does! Will it also do so smartly?) but otherwise it's much easier to attract potential customers with graphics and additional features, tacked on or otherwise. Sound game design kind of shares a similar fate.

The whole thing is made worse is that by and large, game AI is bad everywhere, and has not substantially advanced for over a decade. So people are used to being mistreated here, leading to low expectations and not much sustained outrage or change in behaviour if it is disappointing once again.
 
The killer for game AIs is that they are always programmed by people who have no experience playing the game...by definition. That's part of why I'm thinking an 'aftermarket' AI player might be a practical approach.

I dunno, rigorous AI and multiplay throughout the development process is synergistic with optimizing the development process in general.
 
If games were developed as "AI first" that might actually work. Unfortunately I have not heard of any games developed in such a way.
 
Top Bottom