-
Notifications
You must be signed in to change notification settings - Fork 0
Gather
In this tutorial we will learn to draw information to the screen and to give orders to ingame units.
In the Setup we already saw how to draw Hello World!
to the screen.
public void onFrame() {
game.drawTextScreen(100, 100, "Hello World!");
}
This is, as the name suggests, executed every frame.
The game
variable is set during onStart
public void onStart() {
game = bwClient.getGame();
}
Which gives you access to all information available to you in the game.
To test this out let's change the code in onFrame to print out your name and current minerals.
public void onFrame() {
Player self = game.self();
game.drawTextScreen(20, 20, self.getName() + " has " + self.minerals() + " minerals");
}
You should see your name (in this case bwapi
) and the amount of minerals (in this case 50
)
The first thing you do in StarCraft is ofcourse gathering more minerals.
Let's order our workers to gather the mineral patch closest to them! When a unit completes (even at the start of the game!), they trigger an onUnitComplete
event. We will use this to order our new units to gather minerals if they are workers.
public void onUnitComplete(Unit unit) {
if (unit.getType().isWorker()) {
Unit closestMineral = null;
int closestDistance = Integer.MAX_VALUE;
for (Unit mineral : game.getMinerals()) {
int distance = unit.getDistance(mineral);
if (distance < closestDistance) {
closestMineral = mineral;
closestDistance = distance;
}
}
// Gather the closest mineral
unit.gather(closestMineral);
}
}
If all went well, the workers should now start gathering the mineral patches closest to them. Also the mineral amount number we draw should also increment.
Ofcourse this is not an optimal spread of workers over the available minerals patches, even randomly assigning would probably be better, but that is left as an exercise to the reader ;)
In the next tutorial we will Train some workers, to improve the gather production!