Wednesday, June 30, 2010

Client-Server Optimization Progress

The server code is now optimized for getting unit and map information into shared memory for the client. To get a ballpark estimate of how much of a speed improvement was attained, I ran both the most recent version revision of BWAPI and the most recent release of BWAPI - 2.8 - on the same replay and measured the frames per second each version could achieve when playing with /speed 0 and the replay mode on Fastest x16. Before optimization, the client and server could run at 53 FPS. After optimization, the speed increased to 87 FPS. While these numbers are specific to my computer, other computers will likely get a similar 50-70% speed increase.

Also, the test framework now tests every special ability, using related unit information commands like isStimmed, isStasised, and isLockedDown to verify that the abilities were executed successfully.

Sunday, June 27, 2010

Client-Server Optimization

I've decided to take a break from the testing framework to work on the client server architecture.

The client-server architecture is a new alternative way of making AIs with BWAPI. Traditionally, people would write their AI in a DLL which would get loaded by BWAPI at the start of a match. In contrast, the client-server architecture lets users write their AI as a separate executable program which connects to their local BWAPI server (loaded in Broodwar memory), and communicates with it via shared memory.

This new architecture is almost fully functional, however up to this point it has been extremely slow due to the method that BWAPI uses to get information from Broodwar into the shared memory bank. The inefficient/simplistic implementation currently just calls relevant BWAPI::Unit member functions, and copies the results into shared memory. Since each unit's state is comprised of about 100 different pieces of information, this results in about 100 BWAPI function calls per unit. And each of these functions call other functions in order to generate the appropriate error codes when needed and determine if the user/AI is allowed to access the given unit (hidden units are not accessible unless Flag::CompleteMapInformation is enabled).

To optimize the process of getting information into shared memory, BWAPI will compute the UnitData struct directly from Broodwar memory, bypassing the BWAPI function calls. Thus the last few days I've been moving implementation details of each individual Unit member function to a single function, called Unit::update(), so that it computes the entire UnitData struct directly from Broodwar memory without using any unnecessary or redundant function calls. In addition, to prevent duplicate code I've been simplifying the Unit member functions so they just compute their answers from the UnitData struct, just like the client-side implementation of the Unit object does. This should also go a long way to ensuring that BWAPI behaves consistently whether you're writing you AI as a DLL or as a separate client process.

In short, I'm making a rather large internal change to how the BWAPI::Unit class works in order to optimize the client-server architecture, however the end-user functionality should go unchanged. Also, the test framework module is a DLL, so it can be run both as a DLL in Starcraft memory the traditional way or be loaded into a separate program such as BWAPI's AIModuleLoader and run in a separate client process, so the test framework will be able to test both implementations to make sure they behave correctly and indistinguishably once everything is complete.

Friday, June 25, 2010

Test Framework Update

As planned, I've created test cases that research each TechType and fully upgrade each UpgradeType. Like before, adding these test cases has revealed several bugs which I have since fixed:
  • Fixed a bug where Unit::getRemainingResearchTime would sometimes return 0 due to latency.
  • Fixed a bug where Unit::getRemainingUpgradeTime would sometimes return 0 due to latency.
  • Fixed a bug where Game::canResearch would return true while another unit was researching the same TechType.
  • Fixed a bug where Game::canUpgrade would return true while another unit was upgrading the same UpgradeType.
  • Fixed a bug with UpgradeType::Apial_Sensors.whatResearches() so that it now correctly returns UnitTypes::Protoss_Fleet_Beacon.
I've also added the following two new error codes:
  • Error::Currently_Researching - generated when you try to tell a unit to research a TechType that is already being researched (at that unit or another unit).
  • Error::Currently_Upgrading - generated when you try to tell a unit to upgrade an UpgradeType that is already being upgraded (at that unit or another unit).

Since each special ability affects the game state in a unique way, creating UseTech test cases for each special ability may take a few days. For example, to check that the Stasis ability works, I'll need to check the target unit to verify that it is Stasised, while to check that Dark Swarm works correctly I'll need to verify that a Dark Swarm unit is produced at the target position.

Earlier today I fixed the following bugs related to Protoss Interceptors:
  • Fixed Unit::getTransport so that it works for Protoss Interceptors
  • Fixed Unit::isLoaded so that it returns true if and only if Unit::getTransport!=NULL.
  • Fixed Unit::getLoadedUnits so that it also returns the Interceptors currently loaded inside a Protoss Carrier.
  • Fixed a bug where Unit::getPosition and Unit::getTilePosition would return incorrect positions for loaded units.
I also added the following functions to the interface:
  • Unit::getCarrier - return the Carrier that created this Protoss Interceptor
  • Unit::getInterceptors - returns the Interceptors controlled by this Protoss Carrier.
  • Unit::getNydusExit - should return the connect Nydus Canal Exit (untested)
The one remaining bug with Protoss Interceptors is that the interceptors cannot be detected until they leave the carrier they are loaded in. It should be possible to fix once I change unit detection so that it traverses the other linked lists of Units, not just the main one. Also once this is implemented it should be easy to fix bugs related to unit deaths/removals.

Monday, June 21, 2010

Test Framework Progress

I've been working on the test framework over the last few days and I've finished step two of the plan I outlined in my last post. The TestAIModule now produces every possible building and unit which doesn't require an upgrade or special ability for each of the 3 test maps (TerranTest.scm, ProtossTest.scm, and ZergTest.scm). While these units and buildings are created, lots of asserts are performed to make sure relevant functions return the correct information. By checking the results on every frame, I've been able to find and fix bugs that would only appear for a frame or two, and then disappear. While this test framework is still far from complete, creating these test cases has allowed me to discover and fix the following 13 bugs:

  1. Fixed a bug where Unit::getType would not immediately switch to Egg/Lurker Egg/Cocoon after issuing a morph command to a unit
  2. Fixed a bug where Unit::getBuildType would return UnitTypes::None for Terran buildings that were constructing add-ons
  3. Fixed a bug where Unit::getBuildType would sometimes return UnitTypes::None for morphing Zerg units
  4. Fixed a bug where Unit::getBuildUnit would return NULL for incomplete Terran add-ons that are being constructed.
  5. Fixed a bug where Unit::getRemainingBuildTime would sometimes return incorrect values for morphing Zerg units
  6. Fixed a bug where Unit::getRemainingTrainTime would incorrectly return 0 on some frames due to latency.
  7. Fixed a bug where Unit::isBeingConstructed would return false for morphing Zerg units
  8. Fixed a bug where Unit::isConstructing would return false for Terran buildings that are constructing add-ons
  9. Fixed a bug where Unit::isConstructing would return false for incomplete Terran add-ons that are being constructed.
  10. Fixed a bug where Unit::isConstructing would return false morphing Zerg units
  11. Fixed a bug where Unit::isIdle would sometimes return true when the unit is constructing
  12. Fixed a bug where Unit::isIdle would return true morphing Zerg units
  13. Fixed a bug where Unit::isTraining would not return true for Reavers or Carriers.

While writing these test cases was a pain, I'm starting to see how useful and important a proper test framework is for finding and fixing bugs :). My next goal will be to create test cases that research each upgrade and special ability. After that I will create test cases for using each special ability.

Tuesday, June 15, 2010

BWAPI Test Framework Plan

This week I'm at a family reunion at the Mahoney State Park in Nebraska. I brought my laptop but the internet connection is really flaky, so I can't really work on BWAPI this week. However I'll be coming back in two days and I've made a plan for how to create the BWAPI test framework.

First I will create test maps for Zerg and Protoss and simplify the test map for Terran. Once a test map for each race is completed, I'll then write test cases that proceed in the following chronological order from match start:

  1. Test initial game/force/player/unit information - make sure they all return the correct initial values for the given test map.
  2. Test unit production, construction, and (for Zerg) morphing by creating every possible unit for the given race. This will also include asserts to make sure functions like Unit::getBuildType and Unit::getTrainingQueue return the correct information while the units are produced.
  3. Re-test the game/player/unit information - make sure they all return the correct updated values for the given test map now that step 2 has completed. This will also verify that step 2 completed successfully.
  4. Test the Unit::upgrade and Unit::research functions by researching each upgrade and tech, making sure the relevant functions in the Player class return the correct information at each step along the way.
  5. Test the Unit::useTech functions for each TechType (special ability). This will verify that BWAPI issues each special ability correctly for each spell-casting unit, and also test to make sure the correct error codes are generated in cases where the special abilities cannot be executed.
  6. Test the other unit commands, making sure the the unit information functions return the correct values before, during, and after each command. Each command will be tested in isolation and return the unit to a non-cloaked, non-burrowed, non-sieged, non-lifted idle state.

Wednesday, June 9, 2010

BWAPI Beta 2.8 has been released

BWAPI Beta 2.8 has been released. This release adds menu automation, support for bullets, and fixes several bugs. The full list of changes are in the change log.

Tuesday, June 8, 2010

Progress toward bullet support

Over the last couple days I've added the following functions to the Bullet interface, which is now documented on the wiki:
I've implemented all of these functions except for getID using information found in Starcraft's memory, specifically in Starcraft's bullet array, which we define in the BW::Bullet struct. This struct has been updated many times over the last several days and contains everything we currently know about the 112 bytes that each bullet occupies in Starcraft memory.

The process of adding new functions to the BulletImpl class, such as getTargetPosition(), usually consists of searching through the bytes in this struct and looking for bytes that appear to contain information that define the bullet's target position, or where on the map it is heading. Once we're sure the bytes represent what we think it does, we can rename the field for those bytes from some generic name, like unknown_0x18, to something more descriptive, like targetPosition, and use that field in the implementation of the getTargetPosition() function.

The getID() function returns a unique ID for each bullet and is updated whenever a bullet switches from non-existent to existent. Since bullets are created and destroyed much more frequently than units, unlike BWAPI Units, new BWAPI Bullets are not created when a Bullet is destroyed, which means bullets do not have a unique address throughout the course of a match. Only 100 BulletImpl objects are created (1 for each element in Starcraft's bullet array), and the IDs of these are updated when the slots are re-used for new bullets.

Bullet::isVisible() is now implemented correctly and does not depend on the visibility of the source unit.

I've also added a Game::getBullets() function which returns the set of accessible bullets (all existing bullets if Flag::CompleteMapInformation is enabled, otherwise just visible bullets).

Bullet support has also been added to the client and server code, however it has not been tested yet.

Friday, June 4, 2010

RCOS Presentation

Today I gave a presentation about BWAPI at the Rensselaer Center for Open Source Software (RCOS). In the presentation I gave an overview of BWAPI, mentioned two other projects I've been working on (BWTA and BWSAL), outlined my plan for what I hope to accomplish this summer with the BWAPI project, described the Starcraft AI Competition that the Expressive Intelligence Studio at UC Santa Cruz is hosting at AIIDE 2010, and concluded with a video of the EISBot, which really shows what people have been able to accomplish using BWAPI.

The slides are available here.

My last few commits to the BWAPI svn have been working toward the goal of adding Bullet support to BWAPI. So far the exact size and location of the Bullet table have been determined, and the following functions have been added to a new BWAPI::Bullet interface and have been implemented in the new BWAPI::BulletImpl class:

  • getType()
  • getOwner()
  • getPosition()
  • getRemoveTimer()
  • exists()
  • isVisible()

isVisible() is currently implemented in a simple/naive way that should work correctly in most cases, but will need to be rewritten at some point to work with cloaked/burrowed units. The other functions should be implemented correctly but have not been tested very much yet.

Next I'm going to try to figure out how to read the Bullet's target, direction/heading, and velocity, and also start a wiki page that documents the Bullet interface.