Using old AI in the current release?

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

Moderator: Forum Moderators

Post Reply
Talash
Posts: 6
Joined: May 8th, 2013, 8:54 pm

Using old AI in the current release?

Post by Talash »

I am taking over an AI project that someone did in a much older version of Battle for Wesnoth, which is effectively 1.7.12. From the timestamps he grabbed 1.7.11 from the main dev repo about a week before 1.7.12 came out, and the only code difference with that release is that he's missing some comment changes.

I am trying to figure out how feasible it would be to pull this old AI into the most recent version of BfW. Am I correct in reading that the old "default ai" is still included in newer releases, even though it may no longer be the actual default? Can it still be used in an ai vs ai match with the arguments like controller# and algorithm#?

I know that BfW now includes more ways that AI can be written for the game, but if the default AI is still in then it sounds hopeful that the AI made by this other student (which is coded in a similar way) could also potentially still work on newer versions as well. Have there been any important AI related changes that I should be aware of that may make such a change less possible?

Game changes to maps, balancing, unit types added/removed should not make a difference as those don't matter to the AI, but if the "default ai" has been changed significantly in how it interacts with the game and what methods it uses to do so, then those same changes would likely be required for this other AI. I wouldn't have the time to do that though, and so I'd have to stick with the old version of BfW.
Talash
Posts: 6
Joined: May 8th, 2013, 8:54 pm

Re: Using old AI in the current release?

Post by Talash »

So I did some poking around, and it looks like things have not changed too significantly. The biggest change was the relocation of a lot of elements from game_info to resources, which was easy to correct for.

It seems things may have also changed for how you add a cpp AI to the game though, and run it through the command line. Previously, the AI was added in simply by editing registry.cpp to include the following in the AIs section (as well as the needed include):

Code: Select all

static register_ai_factory<TestAI> testAI("TestAI");
The AI was then run with the following set of command line args:

Code: Select all

--nosound --nogui --multiplayer --scenario=multiplayer_The_Freelands --side1=Rebels --side2=Rebels --controller1=ai --controller2=ai --algorithm1=TestAI --algorithm2=default_ai --turns=40 --exit-at-end
I have seen that the command line format has changed slightly (such as "--side1=Rebels" becoming "--side=1:Rebels") but accounting for that this no longer works, and both AI's in the match are simply the default RCA AI. What am I doing wrong? What is the proper way to include a new AI so that it can be run from the command line?
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: Using old AI in the current release?

Post by iceiceice »

Talash: see my old post about this... I struggled to figure this out one time like 6 months ago or so. It is very poorly documented but custom C++ ai's are still possible.

Edit: http://forums.wesnoth.org/viewtopic.php?f=10&t=39660

However, I should also mention that I did eventually get in contact with Crab. He told me that the intended way to add new custom C++ behavior is to define a new stage, which is added to the "composite AI" by configuring the wml to use that stage exclusively. If you can get that to work then you won't have to rewrite the AI registry code. However my post could still be helpful to you, there are various pitfalls about version numbers recorded in the [ai] tag that can cause you to inexplicably get no results.
Talash
Posts: 6
Joined: May 8th, 2013, 8:54 pm

Re: Using old AI in the current release?

Post by Talash »

Thanks for the info Iceiceice! I'll try first looking into what Crab said is the intended method, and then if there are any issues I can fall back to how you managed to do it.
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: Using old AI in the current release?

Post by iceiceice »

By the way, if you figure out how to make it work by deriving from stage, it would be really great if you could write something on the wiki about it. I think the only devs who actually know how to do that are more or less on wesbreak.
Talash
Posts: 6
Joined: May 8th, 2013, 8:54 pm

Re: Using old AI in the current release?

Post by Talash »

I was able to get my old c++ AI working through stages, though the method is a bit hacky. Thanks for pointing me in the right direction iceiceice!

While I got things working, I'm feeling like what I did may still not be the complete intended way, so some additional input from those who know this part of the code could still be helpful before a wiki addition? The process is basically:
  1. The old AI was derived from the AI interface class (src/ai/interface.hpp). It has been changed to instead be derived from stage (in src/ai/composite/stage.hpp), and the old play_turn() function (this was the only wesnoth code needed to run the AI every turn before) was renamed to do_play_stage() accordingly.
  2. Add your new stage to the registry (src/ai/registry.cpp). For example if your stage derived class is named TestAI, you could enter:

    Code: Select all

    static register_stage_factory<TestAI>
        test_ai_factory("test_ai");
    In this example, the "test_ai_factory" part can be named whatever you like. You will not need to refer to it again, so I was just following the existing convention. The text in quotes, "test_ai" can be anything, but is important because this string is what you will use to reference and load your stage.
  3. Create a new cfg file for your ai. You can store this anywhere, but putting it somewhere in resources/data/ai is the most convenient. To continue the TestAI example, you could place the following file in resources/data/ai/dev/test_ai.cfg:

    Code: Select all

    #textdomain wesnoth-ai
    
    [ai]
        id=ai_test_ai
        description=_"Multiplayer_AI^Test AI" # wmllint: no spellcheck
        version=11710
        [stage]
            id=main_loop
            name=test_ai
        [/stage]
    [/ai]
    
    This file was created simply by taking the cfg file for the RCA AI and stripping out everything that the C++ AI did not need. The most important thing here seems to be that the name= line uses the same string that you added to the registry, in this case "test_ai" in both places. I don't know if it matters, but I also bumped up the version number from 10710 to 11710 in case there might be issues like those mentioned in iceiceice's thread which was linked to earlier in this thread.
  4. I only needed to be able to run the AI in command-line multiplayer AI vs AI matches, which this works for. You will not see your AI in any of the in-game menus though, and I still have no idea how to do that. To run your AI using command-line arguments, you have to specifically tell the game to use the cfg file you created. For example to make the C++ ai take over player 2 in a game, you require these arguments along with all the other usual ones for starting a multiplayer match:

    Code: Select all

    --controller=2:ai --ai-config=2:ai/dev/ai_test_ai.cfg
    
    Placing your new cfg file in with the existing ones means you only need to use a relative path, as I did above. If you have the cfg file outside of the game's data directories then you will of course need to use an absolute path.
The AI will still be named "Default AI (RCA)" in-game and I don't know how to change this, but it'll definitely be running your AI through the stage you created.
User avatar
iceiceice
Posts: 1056
Joined: August 23rd, 2013, 2:10 am

Re: Using old AI in the current release?

Post by iceiceice »

Talash:

Thanks again for writing this :D

I added to link to this post from one of the AI pages on the wiki, hopefully it can be expanded upon and incorporated into the wiki someday by an AI dev. But in the meantime I think it's a big help.
Post Reply