External AI control
Moderator: Forum Moderators
External AI control
The question has come up a few times whether it is possible to have an external mechanism (such as a machine learning framework) interact with Wesnoth without having to change the C++ code. In other words, is it possible to receive the game state from Wesnoth and pass a move back to it, which is then executed by the AI, with the new game state being returned afterward and so on? While no direct interface mechanism is provided by the Wesnoth engine for this, it is nevertheless possible to achieve this via scripts that receive output sent to stdout by a Lua candidate action (CA) and modify files that are read by another Lua CA. It's admittedly a pretty hacky way to do this, but it does work and is quite simple.
I put together a very quick and dirty proof-of-concept example and added it to the AI-demos add-on. It can be accessed by starting the campaign from the 'Campaigns' menu item, or from the command line with
In brief, there are two Lua CAs. The first writes some information about units to stdout, which can then be read by, for example, an external script. This example only outputs a small subset of the game state information, but extending that is trivial. The second CA continuously reads a file and moves a unit according to that file's content, if it changes. Again, this is only an example of a simple move, but it can easily be extended to attacks, recruiting etc. Also, I have not built in any sanity checks, whether the move is actually possible or points to a valid unit or anything like that. As I said, this is meant as a quick proof-of-concept example only.
One more comment: Passing the information back to Wesnoth can probably be done in a "nicer" way by using the
I've included the files needed below with very brief explanations of what they do, just so that they are documented on the forum also.
I put together a very quick and dirty proof-of-concept example and added it to the AI-demos add-on. It can be accessed by starting the campaign from the 'Campaigns' menu item, or from the command line with
wesnoth -tmanual_ai
(or via the test scenarios hotkey). Note that this has not been uploaded to the add-ons server yet (some other parts of AI-demos are currently not releasable), but you can get it from the AI-demos github repository. The commit adding the example is here.In brief, there are two Lua CAs. The first writes some information about units to stdout, which can then be read by, for example, an external script. This example only outputs a small subset of the game state information, but extending that is trivial. The second CA continuously reads a file and moves a unit according to that file's content, if it changes. Again, this is only an example of a simple move, but it can easily be extended to attacks, recruiting etc. Also, I have not built in any sanity checks, whether the move is actually possible or points to a valid unit or anything like that. As I said, this is meant as a quick proof-of-concept example only.
One more comment: Passing the information back to Wesnoth can probably be done in a "nicer" way by using the
--unsafe-scripts
command line option (or via changes to the C++ code), but I was really only interested in testing if this can be done using only Lua CAs directly. If somebody has a more elegant solution, please let us know.I've included the files needed below with very brief explanations of what they do, just so that they are documented on the forum also.
The Files
SP campaigns: Galuldur's First Journey (1.12 & 1.14) & Grnk the Mighty (1.10 & 1.12)
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
- Pentarctagon
- Project Manager
- Posts: 5565
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: External AI control
Perhaps another potential option would be to write the gamestate out using PersistenceWML, convert it to JSON or XML with
wmlparser3
, and then use that.99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: External AI control
Another way is to communicate though (local) multiplayer server.
Related links - https://forums.wesnoth.org/viewtopic.php?f=10&t=43965 https://wiki.wesnoth.org/MultiplayerserverWML https://github.com/ProditorMagnus/wesbo ... ebotCON.py
Related links - https://forums.wesnoth.org/viewtopic.php?f=10&t=43965 https://wiki.wesnoth.org/MultiplayerserverWML https://github.com/ProditorMagnus/wesbo ... ebotCON.py
Re: External AI control
Something like this could be the core of a "Wesnoth in ncurses" implementation, too. Instead of the external program being a machine learning app, it would be a console app that lets an actual human player perform the next move.
Re: External AI control
Howdy! I just came to the forum looking for something like this. I would love to experiment with AI design using the Wesnoth engine. The other game I am looking at is the much clunkier Pioneers (http://pio.sourceforge.net/) game, which is attractive because of its standalone pioneersai (http://pio.sourceforge.net/manual/ch04.html), which connects to a persistent server over the network, and could therefore be more easily replaced by a custom AI.
I will look more closely at your LUA Candidate Actions. It's not something with which I am familiar, but if it's faster than HTTP, all the better!
I will look more closely at your LUA Candidate Actions. It's not something with which I am familiar, but if it's faster than HTTP, all the better!
Re: External AI control
There are at least 3 ideas of how to connect with an external AI listed here. But which one is the easiest/best? Are these methods still viable? Will they work in future Wesnoth versions?
I am looking for some way to do the following:
1) Make an external AI program. For now this would be a Windows .exe.
2) Connect with an existing Wesnoth installation.
3) Play automatically vs existing Wesnoth AI (for example on a random map).
4) Optionally also play vs a human using the Wesnoth installation for interfacing the human.
I am looking for some way to do the following:
1) Make an external AI program. For now this would be a Windows .exe.
2) Connect with an existing Wesnoth installation.
3) Play automatically vs existing Wesnoth AI (for example on a random map).
4) Optionally also play vs a human using the Wesnoth installation for interfacing the human.
Re: External AI control
All of these methods should still work, and continue to work with future versions. As for which one to use, that depends on what you want to accomplish, and how "permanent" a solution you want. The method in my original post works and is very easy to implement, but it's really a dirty hack that's not very nice. If I wanted something "cleaner", I might look into Ravana's method, but that's really without looking into the details at all.
SP campaigns: Galuldur's First Journey (1.12 & 1.14) & Grnk the Mighty (1.10 & 1.12)
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
AI experiments: Micro AIs (wiki, forum thread, known/fixed bugs), Fred, AI-demos add-on
Re: External AI control
Zrevnur wrote:
> 3) Play automatically vs existing Wesnoth AI (for example on a random map).
It would be kinda great if we could have adjusted A.I.s e. g. an orcish A.I. that would favour
brute force, or a rogue A.I for sneaky units that may try to surround more and avoid direct
confrontation and such. But I guess one also has to keep it simple, because otherwise who
may want to maintain complicated code ...![Razz :P](./images/smilies/icon_razz.gif)
May be worth to actually keep different A. I. styles and let the players vote on which variants
they think is best, in this or that scenario. I am fine with the default behaviour, but you can
also sort of induce the enemy A. I. into making mistakes, e. g. jabait them with weaker
(injured) units.
> 3) Play automatically vs existing Wesnoth AI (for example on a random map).
It would be kinda great if we could have adjusted A.I.s e. g. an orcish A.I. that would favour
brute force, or a rogue A.I for sneaky units that may try to surround more and avoid direct
confrontation and such. But I guess one also has to keep it simple, because otherwise who
may want to maintain complicated code ...
![Razz :P](./images/smilies/icon_razz.gif)
May be worth to actually keep different A. I. styles and let the players vote on which variants
they think is best, in this or that scenario. I am fine with the default behaviour, but you can
also sort of induce the enemy A. I. into making mistakes, e. g. jabait them with weaker
(injured) units.
- Lord-Knightmare
- Discord Moderator
- Posts: 2364
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Re: External AI control
We have micro AI for that.It would be kinda great if we could have adjusted A.I.s e. g. an orcish A.I. that would favour
brute force, or a rogue A.I for sneaky units that may try to surround more and avoid direct
confrontation and such. But I guess one also has to keep it simple, because otherwise who
may want to maintain complicated code ...
May be worth to actually keep different A. I. styles and let the players vote on which variants
they think is best, in this or that scenario. I am fine with the default behaviour, but you can
also sort of induce the enemy A. I. into making mistakes, e. g. jabait them with weaker
(injured) units.
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium