Build
Now that we are training our workers, eventually we'll be running out of supply.
To do this, we need to build some buildings that give us extra supply space.
Again for Zerg
this would be an Overlord
which is morphed from a Larva
larva.morph(UnitType.Zerg_Overlord);
Giving a command to build a building is as simple as
worker.build(building, location);
However, figuring out what, who, when and where you should build is not that trivial.
Something that gives supply
UnitType toBuild = self.getRace().getSupplyProvider();
A random worker that is not already doing something except gathering minerals (as to not disturb a potential other future builder or future scouter)
Unit builder = null;
for (Unit unit: self.getUnits()) {
if (unit.getType().isWorker() && (unit.isIdle() || unit.isGatheringMinerals())) {
builder = unit;
break;
}
}
We need to build supply buildings only if we are going to run out of supply space in the near future (and did not reach our supplycap of 200 units yet).
if (self.supplyTotal() - self.supplyUsed() <= 2 && self.supplyTotal() <= 400) {
}
Note: because of how StarCraft works, Zerglings
being only half a supply on the screen (0.5), supply in BWAPI is doubled compared to the number that the game displays. A Zergling costs 1 supply, a worker therefore costs 2 supply, and a supply building gives double of what you see on the screen. In the end it is the same, only this can be confusing for beginners when starting to work with BWAPI.
You can easily check for yourself with these handy methods
unitType.supplyProvided();
unitType.supplyRequired();
Building placement in StarCraft is very important but also quite complex (e.g. walls, canon placement, etc.).
For this tutorial however we'll cut some corners and just build around our starting location and use a built-in method to get a TilePosition
on where to build (which you shouldn't use for your real bot as it is quite unreliable).
TilePosition buildLocation = game.getBuildLocation(toBuild, self.getStartLocation());
builder.build(toBuild, buildLocation);
However have a problem right now, where this build condition will be true for a very long time, as long as the building is finished, so a lot of workers will try to build a supplyprovider at the same time. We can add a little hack by keeping track of our "queued" supply, e.g. the supply that we will have in the future.
NOTE: this is just a hack to keep the tutorial going, you should implement your own building building tracking system (e.g. my approach doesn't scale at all because it only works for buildings giving supply).