Page 1 of 1

Running your BWAPI vs itself

Posted: Sun Jan 23, 2011 8:20 pm
by krasi0
Automating games restarts while battling your own AI

Setting up StarCraft

2 machines (or Virtual Machines of course), one that will create the game that we will call host, the other that will connect to it that we will call client.
The host should have a bwapi.ini with things like:

Code: Select all

ai_dll = C:StarCraftAIBroodwarBotQReleaseHostModule.dll; your host dll
auto_menu = LAN
lan_mode = Local Area Network (UDP)
auto_restart = ON
map = mapsBroodWarTournamentSC-t1mapsdragoons.scm; the map on which you want to play
The client should have a bwapi.ini with things like:
ai_dll = C:StarCraftAIBroodwarBotQReleaseClientModule.dll; your client dll
auto_menu = LAN
lan_mode = Local Area Network (UDP)
auto_restart = ON
map =; auto-joins games

Launch both and launch the game (host, then client, then you click OK on the game lobby menu). It should work but stop in the same state (game lobby). That's why we want to click on OK automatically.
Setting up AutoHotkey

On your host, Grab and install AutoHotkey (I took the Basic version and it works).
Use AutoIt3 Windows Spy (installed with AutoHotkey) to find the In Active Window coordinates of the OK button in the game lobby. It helps to have your starcraft either full screen of windowed and pinned down (in the upper right icons of W-Mode). You can freeze the current mouse position for AutoIt3 Windows Spy on Windows XP by doing Alt+Maj+Tab.
Create a text file ending in .ahk and paste something like this while changing the mouse coordinates to your "In Active Window" ones:

Code: Select all

Sleep 6000
WinGetActiveTitle, PreviouslyActive
IfWinExist Brood War
{
   WinActivate
   Click 517, 426
}
IfWinExist, %PreviouslyActive%
{
   WinActivate
}

This waits 6 secondes avec been called (in onEnd(bool isWinner), see next section) so that we can skip the menus and have the client joined back the game. You may need to change that for more if your network is slow.
You can right click on the .ahk file and create an executable with Compile Script (AutoHotkey compiles it). You will use this executable in the next section. I called mine startGame.exe.
Tuning your code

All that's left is calling the AutoHotkey process that will restart the game when the games end. This code has to be in the host code (HostModule.dll in the example above). The client will reconnect automatically (thanks to map = . Here is the snippet of code I use. I put myRestartGame() in my utils and then just call it in onEnd(bool isWinner).

Code: Select all

void myRestartGame()
{
   /////////// Launch an AutoHotkey that will click on "OK" in 10 sec
   char procName[200];
   sprintf_s(procName,
      "C:StarCraftAIBroodwarBotQscriptsstartGame.exe");
   // Initialize StartupInfo structure
   STARTUPINFO   StartupInfo;
   memset(&StartupInfo, 0, sizeof(StartupInfo));
   StartupInfo.cb = sizeof(StartupInfo);

   // This will contain the information about the newly created process
   PROCESS_INFORMATION ProcessInformation;

   BOOL results = CreateProcess(0,
             procName, // Process and arguments
             0, // Process Attributes
             0, // Thread Attributes
             FALSE, // Inherit Handles
             0, // CreationFlags,
             0, // Environment
             0, // Current Directory
             &StartupInfo, // StartupInfo
             &ProcessInformation // Process Information
             );
   // Cleanup
   CloseHandle(ProcessInformation.hProcess);
   CloseHandle(ProcessInformation.hThread);
}

Now, you only have to click on OK in the game lobby once, the first time you start the series of games. The bots will continue playing automatically, as long as you want (and don't crash ;)).

The tutorial wasn't written by me. The original author is [CsT]Snippy (could be found on #BWAPI @ freenode, his website: http://emotion.inrialpes.fr/people/synnaeve/)

Posted: Sun Aug 07, 2011 2:36 am
by TheBlueGnu
Tried the link but I got 404 Not Found.

Edit:
Also put void myRestartGame() in my TestAIModule.cpp and got the following error;

1>TestAIModule.cpp
1>.SourceTestAIModule.cpp(545) : error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'char [200]' to 'LPWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Edit 2:
Woops I'm being lazy, this was a googlable issue;

http://social.msdn.microsoft.com/forums ... ba3be1ddb/

Link is still broken tho

Posted: Sun Aug 07, 2011 7:15 am
by krasi0
Thanks! Fixed the link.
BTW why don't you join the IRC channel @ http://webchat.freenode.net/?channels=BWAPI
There

Posted: Sun Aug 07, 2011 11:07 am
by TheBlueGnu
[quote name='krasi0' date='07 August 2011 - 03:15 PM' timestamp='1312701350' post='10328']
Thanks! Fixed the link.
BTW why don't you join the IRC channel @ http://webchat.freenode.net/?channels=BWAPI
There
[/quote]

Thanks very much! Will do