Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import bwapi.*;
  5. import bwta.BWTA;
  6. import bwta.BaseLocation;
  7.  
  8. public class TestBot1 extends DefaultBWListener {
  9.     static int scvCount = 0;
  10.     BaseLocation[] enemyBases;
  11.     private Mirror mirror = new Mirror();
  12.     static int barCount = 0;
  13.     int barLimit = 2;
  14.     private Game game;
  15.     static int marineCount = 0;
  16.     static int step = 0;
  17.     int currentSCV = 0;
  18.     int secondSCV = 0;
  19.     int thirdSCV = 0;
  20.     private Player self;
  21.     static int marineLimit = 0;
  22.     static int buildOrderPos = 0;
  23.     UnitType[] buildings;
  24.     List<Unit> badGuys;
  25.     List<Unit> badList;
  26.     boolean gotRef = false;
  27.    
  28.  
  29.     public void run() {
  30.         mirror.getModule().setEventListener(this);
  31.         mirror.startGame();
  32.     }
  33.  
  34.     // Returns a suitable TilePosition to build a given building type near
  35.     // specified TilePosition aroundTile, or null if not found. (builder
  36.     // parameter is our worker)
  37.     public TilePosition getBuildTile(Unit builder, UnitType buildingType, TilePosition aroundTile) {
  38.         TilePosition ret = null;
  39.         int maxDist = buildingType.tileWidth();
  40.         int stopDist = 40;
  41.  
  42.         // Refinery, Assimilator, Extractor
  43.         if (buildingType.isRefinery()) {
  44.             for (Unit n : game.neutral().getUnits()) {
  45.                 if ((n.getType() == UnitType.Resource_Vespene_Geyser)
  46.                         && (Math.abs(n.getTilePosition().getX() - aroundTile.getX()) < stopDist)
  47.                         && (Math.abs(n.getTilePosition().getY() - aroundTile.getY()) < stopDist))
  48.                     return n.getTilePosition();
  49.             }
  50.         }
  51.  
  52.         while ((maxDist < stopDist) && (ret == null)) {
  53.             for (int i = aroundTile.getX() - maxDist; i <= aroundTile.getX() + maxDist; i++) {
  54.                 for (int j = aroundTile.getY() - maxDist; j <= aroundTile.getY() + maxDist; j++) {
  55.                     if (game.canBuildHere(new TilePosition(i, j), buildingType, builder, false)) {
  56.                         // units that are blocking the tile
  57.                         boolean unitsInWay = false;
  58.                         for (Unit u : game.getAllUnits()) {
  59.                             if (u.getID() == builder.getID())
  60.                                 continue;
  61.                             if ((Math.abs(u.getTilePosition().getX() - i) < 4)
  62.                                     && (Math.abs(u.getTilePosition().getY() - j) < 4))
  63.                                 unitsInWay = true;
  64.                         }
  65.                         if (!unitsInWay) {
  66.                             return new TilePosition(i, j);
  67.                         }
  68.                         // creep for Zerg
  69.  
  70.                     }
  71.                 }
  72.             }
  73.             maxDist += 2;
  74.         }
  75.  
  76.         if (ret == null)
  77.             game.printf("Unable to find suitable build position for " + buildingType.toString());
  78.         return ret;
  79.     }
  80.  
  81.     @Override
  82.     public void onUnitCreate(Unit unit) {
  83.         System.out.println("New unit discovered " + unit.getType());
  84.         if (unit.getType() == UnitType.Terran_Barracks) {
  85.             barCount++;
  86.             buildOrderPos++;
  87.         }
  88.         if (unit.getType() == UnitType.Terran_Marine) {
  89.             marineCount++;
  90.         }
  91.         if (unit.getType() == UnitType.Terran_Supply_Depot) {
  92.             buildOrderPos++;
  93.         }
  94.        
  95.        
  96.     }
  97.  
  98.     @Override
  99.     public void onStart() {
  100.         game = mirror.getGame();
  101.         self = game.self();
  102.  
  103.         // Use BWTA to analyze map
  104.         // This may take a few minutes if the map is processed first time!
  105.         System.out.println("Analyzing map...");
  106.         BWTA.readMap();
  107.         BWTA.analyze();
  108.         System.out.println("Map data ready");
  109.  
  110.         int i = 0;
  111.         for (BaseLocation baseLocation : BWTA.getBaseLocations()) {
  112.             System.out.println("Base location #" + (++i) + ". Printing location's region polygon:");
  113.             for (Position position : baseLocation.getRegion().getPolygon().getPoints()) {
  114.                 System.out.print(position + ", ");
  115.             }
  116.             System.out.println();
  117.         }
  118.  
  119.     }
  120.  
  121.     @Override
  122.     public void onFrame() {
  123.         // game.setTextSize(10);
  124.         game.drawTextScreen(10, 10, "Playing as " + self.getName() + " - " + self.getRace());
  125.  
  126.         StringBuilder units = new StringBuilder("My units:\n");
  127.  
  128.         goMine();
  129.  
  130.         currentSCV = buildDepot();
  131.  
  132.         buildOrder();
  133.         buildRef();
  134.         // buildBarracks();
  135.         goodRefinery();
  136.        
  137.         buildMarine();
  138.         // secondSCV = build2Depot();
  139.         giverHell();
  140.         // thirdSCV = build3Depot();
  141.         totalMarineCount();
  142.         // draw my units on screen
  143.         game.drawTextScreen(10, 25, units.toString());
  144.     }
  145.     // public void runBuild(){
  146.     // for(int i = 0; i < buildings.length; i++){
  147.     //
  148.     // }
  149.     // }
  150.  
  151.     public UnitType marineRushBuild(int x) {
  152.         int y = x;
  153.         UnitType[] buildings = new UnitType[16];
  154.         buildings[0] = UnitType.Terran_Supply_Depot;
  155.         buildings[1] = UnitType.Terran_Barracks;
  156.         buildings[2] = UnitType.Terran_Supply_Depot;
  157.         buildings[3] = UnitType.Terran_Supply_Depot;
  158.         buildings[4] = UnitType.Terran_Barracks;
  159.         buildings[5] = UnitType.Terran_Barracks;
  160.         buildings[6] = UnitType.Terran_Supply_Depot;
  161.         buildings[7] = UnitType.Terran_Supply_Depot;
  162.         buildings[8] = UnitType.Terran_Supply_Depot;
  163.         buildings[9] = UnitType.Terran_Supply_Depot;
  164.         buildings[10] = UnitType.Terran_Barracks;
  165.         buildings[11] = UnitType.Terran_Supply_Depot;
  166.         buildings[12] = UnitType.Terran_Supply_Depot;
  167.         buildings[13] = UnitType.Terran_Barracks;
  168.         buildings[14] = UnitType.Terran_Supply_Depot;
  169.         buildings[15] = UnitType.Terran_Supply_Depot;
  170.         return buildings[y];
  171.     }
  172.  
  173.     // public void openFire(){
  174.     // badList = enemy.getUnits();
  175.     // for(Unit myUnit : self.getUnits()){
  176.     // if(myUnit.getType() == UnitType.Terran_Marine){
  177.     // badGuys = myUnit.getUnitsInRadius(1024);
  178.     // for(int i = 0; i < badGuys.size();i++){
  179.     // if(badGuys.get[i])
  180.     //
  181.     //
  182.     // }
  183.     // }
  184.     // }
  185.     // }
  186.     // }
  187.     public void giverHell() {
  188.         Position basePosition = new Position((game.mapWidth() - self.getStartLocation().getX()) * 32,
  189.                 (game.mapHeight() - self.getStartLocation().getY()) * 32);
  190.         Position homeBase = new Position(self.getStartLocation().getX() * 32, self.getStartLocation().getY() * 32);
  191.  
  192.        
  193.  
  194.         if (marineLimit > 26) {
  195.  
  196.             for (Unit myUnit : self.getUnits()) {
  197.  
  198.                 if (myUnit.getType() == UnitType.Terran_Marine) {
  199.  
  200.                     // myUnit.attack(new Position(enemyBase.getX()* 32,
  201.                     // enemyBase.getY() * 32));
  202.  
  203.                     if (myUnit.isIdle())
  204.  
  205.                         if (myUnit.getDistance(basePosition) < 175)
  206.  
  207.                             myUnit.attack(randomPosition());
  208.  
  209.                         else
  210.  
  211.                             myUnit.attack(basePosition);
  212.  
  213.                 }
  214.  
  215.             }
  216.  
  217.         }
  218.         else{
  219.             for (Unit myUnit : self.getUnits()) {
  220.                 if (myUnit.getType() == UnitType.Terran_Marine) {
  221.                     if (myUnit.isIdle()) {
  222.                         myUnit.attack(homeBase);
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.  
  228.     }
  229.  
  230.     public Position randomPosition() {
  231.  
  232.         int x = 32 * (int) (Math.random() * (float) game.mapWidth());
  233.  
  234.         int y = 32 * (int) (Math.random() * (float) game.mapHeight());
  235.  
  236.         return new Position(x, y);
  237.  
  238.     }
  239.     // public int build3Depot(){
  240.     // int x = 0;
  241.     // for (Unit myUnit : self.getUnits()) {
  242.     //
  243.     //
  244.     //
  245.     // if (myUnit.getType() == UnitType.Terran_SCV && self.supplyTotal() -
  246.     // self.supplyUsed() < 3 + barCount && self.minerals() >= 100 &&
  247.     // myUnit.getID() != currentSCV && myUnit.getID() != secondSCV ||
  248.     // self.supplyUsed() == 20 || self.supplyUsed() == 28|| self.supplyUsed() ==
  249.     // 39) {
  250.     // x = myUnit.getID();
  251.     // // get a nice place to build a supply depot
  252.     // TilePosition buildTile = getBuildTile(myUnit,
  253.     // UnitType.Terran_Supply_Depot, self.getStartLocation());
  254.     // // and, if found, send the worker to build it (and leave
  255.     // // others alone - break;)
  256.     //
  257.     // if (buildTile != null) {
  258.     // myUnit.build(UnitType.Terran_Supply_Depot, buildTile);
  259.     //
  260.     // break;
  261.     // }
  262.     // }
  263.     //
  264.     // }
  265.     // return x;
  266.     //
  267.     // }
  268.     // public int build2Depot(){
  269.     // int x = 0;
  270.     // for (Unit myUnit : self.getUnits()) {
  271.     //
  272.     //
  273.     //
  274.     // if (myUnit.getType() == UnitType.Terran_SCV && self.supplyTotal() -
  275.     // self.supplyUsed() < 5 + barCount && self.minerals() >= 100 &&
  276.     // myUnit.getID() != currentSCV) {
  277.     // // get a nice place to build a supply depot
  278.     // TilePosition buildTile = getBuildTile(myUnit,
  279.     // UnitType.Terran_Supply_Depot, self.getStartLocation());
  280.     // // and, if found, send the worker to build it (and leave
  281.     // // others alone - break;)
  282.     //
  283.     // if (buildTile != null) {
  284.     // x = myUnit.getID();
  285.     // myUnit.build(UnitType.Terran_Supply_Depot, buildTile);
  286.     //
  287.     // break;
  288.     // }
  289.     // }
  290.     //
  291.     // }
  292.     // return x;
  293.     //
  294.     // }
  295.  
  296.     public int buildDepot() {
  297.         int y = 0;
  298.         for (Unit myUnit : self.getUnits()) {
  299.  
  300.             if (myUnit.getType() == UnitType.Terran_SCV && self.minerals() >= 150) {
  301.                 // get a nice place to build a supply depot
  302.                 if(buildOrderPos == 2 && gotRef == true){
  303.                     break;
  304.                 }
  305.                 y = myUnit.getID();
  306.                 TilePosition buildTile = getBuildTile(myUnit, marineRushBuild(buildOrderPos), self.getStartLocation());
  307.                 // and, if found, send the worker to build it (and leave
  308.                 // others alone - break;)
  309.                
  310.                 if (buildTile != null) {
  311.  
  312.                     myUnit.build(marineRushBuild(buildOrderPos), buildTile);
  313.                    
  314.  
  315.                     break;
  316.                 }
  317.  
  318.             }
  319.  
  320.         }
  321.         return y;
  322.        
  323.  
  324.     }
  325.     public void buildRef(){
  326.         for (Unit myUnit : self.getUnits()) {
  327.             if (myUnit.getType() == UnitType.Terran_SCV){
  328.                 secondSCV = myUnit.getID();
  329.                 if(currentSCV != secondSCV){
  330.                     if(buildOrderPos > 2 && gotRef == false){
  331.                         TilePosition buildTile = getBuildTile(myUnit, UnitType.Terran_Refinery, self.getStartLocation());
  332.                         // and, if found, send the worker to build it (and leave
  333.                         // others alone - break;)
  334.                        
  335.                         if (buildTile != null) {
  336.  
  337.                             myUnit.build(UnitType.Terran_Refinery, buildTile);
  338.                            
  339.  
  340.                             break;
  341.                         }
  342.                     }
  343.                 }
  344.                
  345.             }
  346.         }
  347.     }
  348.     public void goodRefinery(){
  349.         for (Unit myUnit : self.getUnits()) {
  350.             if (myUnit.getType() == UnitType.Terran_Refinery){
  351.                 if(myUnit.isBeingConstructed()){
  352.                     gotRef = true;
  353.                     if(myUnit.isCompleted()){
  354.                         buildOrderPos++;
  355.                     }
  356.                 }
  357.             }
  358.         }
  359.     }
  360.     public void goMine() {
  361.         for (Unit myUnit : self.getUnits()) {
  362.             if (myUnit.getType().isWorker() && myUnit.isIdle()) {
  363.                 Unit closestMineral = null;
  364.  
  365.                 // find the closest mineral
  366.                 for (Unit neutralUnit : game.neutral().getUnits()) {
  367.                     if (neutralUnit.getType().isMineralField()) {
  368.                         if (closestMineral == null
  369.                                 || myUnit.getDistance(neutralUnit) < myUnit.getDistance(closestMineral)) {
  370.                             closestMineral = neutralUnit;
  371.                         }
  372.                     }
  373.                 }
  374.  
  375.                 // if a mineral patch was found, send the worker to gather it
  376.                 if (closestMineral != null) {
  377.                     myUnit.gather(closestMineral, false);
  378.                 }
  379.             }
  380.         }
  381.     }
  382.  
  383.     public int buildBarracks() {
  384.  
  385.         int scvID = 0;
  386.         for (Unit myUnit : self.getUnits()) {
  387.  
  388.             if (myUnit.getType() == UnitType.Terran_SCV && self.minerals() >= 200 && myUnit.getID() != currentSCV
  389.                     && self.supplyTotal() - self.supplyUsed() < 7) {
  390.                 // get a nice place to build a supply depot
  391.                 TilePosition buildTile = getBuildTile(myUnit, UnitType.Terran_Supply_Depot, self.getStartLocation());
  392.                 // and, if found, send the worker to build it (and leave
  393.                 // others alone - break;)
  394.  
  395.                 if (buildTile != null) {
  396.                     scvID = myUnit.getID();
  397.                     myUnit.build(UnitType.Terran_Supply_Depot, buildTile);
  398.  
  399.                     break;
  400.  
  401.                 }
  402.  
  403.             }
  404.  
  405.         }
  406.  
  407.         return scvID;
  408.  
  409.     }
  410.     // public void checkBar(){
  411.     // for (Unit myUnit : self.getUnits()) {
  412.     // if (myUnit.getType() == UnitType.Terran_Barracks)
  413.  
  414.     // if(myUnit.isCompleted())
  415.  
  416.     // barCount++;
  417.     // }
  418.     // }
  419.     public void buildOrder() {
  420.         for (Unit myUnit : self.getUnits()) {
  421.             if (myUnit.getType() == UnitType.Terran_Command_Center && self.minerals() >= 50 && scvCount < 15
  422.                     && self.supplyTotal() - self.supplyUsed() > 1 && myUnit.isTraining() == false) {
  423.                 System.out.print(scvCount);
  424.                 myUnit.train(UnitType.Terran_SCV);
  425.                 scvCount++;
  426.             }
  427.  
  428.         }
  429.     }
  430.  
  431.     public void buildMarine() {
  432.         for (Unit myUnit : self.getUnits()) {
  433.             if (myUnit.getType() == UnitType.Terran_Barracks && self.minerals() >= 50
  434.                     && self.supplyTotal() - self.supplyUsed() >= 1 && myUnit.isTraining() == false) {
  435.                 myUnit.train(UnitType.Terran_Marine);
  436.  
  437.             }
  438.         }
  439.  
  440.     }
  441.     public void totalMarineCount(){
  442.         marineLimit = 0;
  443.         ArrayList<Integer> IDS = new ArrayList<Integer>();
  444.         for(Unit myUnit : self.getUnits()){
  445.             if(myUnit.getType() == UnitType.Terran_Marine){
  446.                 IDS.add(myUnit.getID());
  447.             }
  448.         }
  449.         for(int i : IDS){
  450.             marineLimit++;
  451.         }
  452.         System.out.println("Marine count is : " + marineLimit);
  453.         System.out.println(buildOrderPos);
  454.     }
  455.  
  456.     public static void main(String[] args) {
  457.  
  458.         new TestBot1().run();
  459.     }
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement