Developing an AI using C++

Discussion of all aspects of the game engine, including development of new and existing features.

Moderators: Forum Moderators, Developers

Post Reply
User avatar
Posts: 29
Joined: April 9th, 2010, 11:40 am

Developing an AI using C++

Post by milwac »

Hi all,

I have been trying to develop an AI based on deep reinforcement learning and have had some interesting discussions regarding the same on this thread.

Right now I am trying to get some basic debugging stuff working - display game state information and deciding on what input features would be useful to train on - but I'm getting stuck with several setup related issues. I read in the wiki that for a custom AI, an external Lua addon is the way to go. I'm currently being helped by mattsc on understanding how to set this up as he has experience with his own AI-Demos addon.

My question is, being a C++ programmer, is it possible to write an AI in C++, which can be accessed from the multiplayer game setup just like default AI? I did look into the wiki for that as well, but the page (as it says) is terribly out of date. I looked at the description here where Talash says that s/he doesn't know how the test AI can be made accessible from within the game. Is there more documentation on this since?

Again, the general need is just to incorporate an input (game state)/output (AI controller) system working soon, so that I can focus more on the AI algorithm itself. Which is the best and/or ideal way to do this in your opinion(s)? Any pointers will be appreciated!


User avatar
Posts: 1658
Joined: August 3rd, 2012, 11:26 pm
Location: Canada

Re: Developing an AI using C++

Post by Celtic_Minstrel »

The short answer to this is yes, it's possible to create a custom Wesnoth AI in C++.

What you would need to do is create a custom stage class (inheriting from ai::stage which is defined in src/ai/composite/state.?pp), and then add it to the AI registry in src/ai/registry.cpp. Beyond that, I'm not entirely sure what you'd need to know. I would suggest reading through the existing AI code to get some idea of how things are done.

Once you have your custom stage implemented, you can then add a config entry for it in data/ai/ais which would basically just consist of an [ai] tag that contains a [stage] tag specifying the name of your custom stage. (Of course it also needs the id and description keys in [ai].)

Note that there is no capacity to distribute a C++ AI as an addon, though, so you wouldn't be able to easily give it to others to test. You'd need to either distribute a full custom Wesnoth build or get others to build the engine plus your custom AI themselves.


I guess the key thing that Talash missed was putting the config in ai/ais instead ai/dev.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

Posts: 282
Joined: June 3rd, 2004, 4:35 pm

Re: Developing an AI using C++

Post by shevegen »

The AI in the game is somewhat limited in that there are a few ways to trigger it, in order to make it easier to win.

For example, sacrificing certain units with low health or low level can be placed as cannon fodder to attract enemy units, which can then more easily be picked off as you baited them that way.

It would be kind of cool to have an AI that would "learn" from such behaviour or mistakes but I understand that AI is very limited in general.

Post Reply