11import bwapi .*;
2+ import bwapi .point .Position ;
3+ import bwapi .point .TilePosition ;
4+ import bwapi .types .Race ;
25import bwapi .types .UnitType ;
36
47import java .util .Comparator ;
@@ -22,25 +25,58 @@ public void onStart() {
2225
2326 public void onFrame () {
2427 final Player self = game .self ();
28+ final Race race = self .getRace ();
29+
2530 // find the depot
2631 final Unit depot = self .getUnits ().stream ()
2732 .filter (u -> u .getType ().isResourceDepot ())
2833 .findFirst ().get ();
2934
3035 // train workers
3136 if (depot .isIdle ()) {
32- depot .train (self . getRace () .getWorker ());
37+ depot .train (race .getWorker ());
3338 }
3439
35-
36- //find accessible minerals
40+ // find accessible minerals
3741 final PriorityQueue <Unit > minerals = new PriorityQueue <>(Comparator .comparingInt (a -> a .getDistance (depot )));
3842
39- minerals .addAll (game .getMinerals ());
43+ Position avgMineralPos = Position .Origin ;
44+ int count = 0 ;
45+ for (final Unit mineral : game .getMinerals ()) {
46+ if (mineral .isVisible ()) {
47+ avgMineralPos = avgMineralPos .add (mineral .getInitialPosition ());
48+ count += 1 ;
49+ minerals .add (mineral );
50+ }
51+ }
52+ avgMineralPos = avgMineralPos .divide (count );
53+ System .out .println (avgMineralPos );
4054
55+ // make workers gather minerals if idle
4156 self .getUnits ().stream ()
4257 .filter (u -> u .getType ().isWorker () && u .isIdle ())
4358 .forEach (u -> u .gather (minerals .poll ()));
59+
60+
61+ if (self .supplyTotal () - self .supplyUsed () <= 2 ) {
62+ final UnitType supplyProvider = race .getSupplyProvider ();
63+ if (self .minerals () >= supplyProvider .mineralPrice ()) {
64+ final TilePosition depotTP = depot .getTilePosition ();
65+ final TilePosition avgMinTP = avgMineralPos .toTilePosition ();
66+ final UnitType builderType = supplyProvider .whatBuilds ().getKey ();
67+ self .getUnits ().stream ()
68+ .filter (u -> u .getType ().equals (builderType ) && !u .isCarrying ())
69+ .findFirst ().ifPresent (u -> {
70+ if (u .getType ().isWorker ()) {
71+ u .build (supplyProvider , depotTP .add (depotTP .subtract (avgMinTP )));
72+ }
73+ else {
74+ u .train (supplyProvider );
75+ }
76+ });
77+ }
78+ }
79+
4480 }
4581
4682 public static void main (String [] args ) {
0 commit comments