Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.99 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. import bwapi.*;
  7.  
  8. import bwta.BWTA;
  9.  
  10. import bwta.BaseLocation;
  11.  
  12.  
  13.  
  14. public class TestBot11 extends DefaultBWListener {
  15.  
  16.  
  17.  
  18.     private Mirror mirror = new Mirror();
  19.  
  20.  
  21.  
  22.     private Game game;
  23.  
  24.  
  25.  
  26.     private Player self;
  27.  
  28.    
  29.  
  30.     private int greatestProbeCount = 0;
  31.  
  32.     private int maxProbesAllowed = 12;
  33.  
  34.    
  35.  
  36.     private int zealotCount = 0;
  37.  
  38.     private int zealotAttackThreshold = 4;
  39.  
  40.    
  41.  
  42.     private ArrayList<UnitType> buildOrder;
  43.  
  44.     private UnitType scheduledBuilding = null;
  45.  
  46.     private int scheduledBuildingCount = 0;
  47.  
  48.     private int promisedMinerals = 0;
  49.  
  50.    
  51.  
  52.     private int pylonCount = 0;
  53.  
  54.  
  55.  
  56.     public void run() {
  57.  
  58.         mirror.getModule().setEventListener(this);
  59.  
  60.         mirror.startGame();
  61.  
  62.     }
  63.  
  64.  
  65.  
  66.     @Override
  67.  
  68.     public void onUnitCreate(Unit unit) {
  69.  
  70.         System.out.println("New unit " + unit.getType());
  71.  
  72.     }
  73.  
  74.  
  75.  
  76.     @Override
  77.  
  78.     public void onStart() {
  79.  
  80.         game = mirror.getGame();
  81.  
  82.         self = game.self();
  83.  
  84.        
  85.  
  86.         game.enableFlag(1);
  87.  
  88.         game.setLocalSpeed(30);
  89.  
  90.  
  91.  
  92.         //Use BWTA to analyze map
  93.  
  94.         //This may take a few minutes if the map is processed first time!
  95.  
  96.         System.out.println("Analyzing map...");
  97.  
  98.         BWTA.readMap();
  99.  
  100.         BWTA.analyze();
  101.  
  102.         System.out.println("Map data ready");
  103.  
  104.        
  105.  
  106.         int i = 0;
  107.  
  108.         for(BaseLocation baseLocation : BWTA.getBaseLocations()){
  109.  
  110.             System.out.println("Base location #" + (++i) +". Printing location's region polygon:");
  111.  
  112.             for(Position position: baseLocation.getRegion().getPolygon().getPoints()){
  113.  
  114.                 System.out.print(position + ", ");
  115.  
  116.             }
  117.  
  118.             System.out.println();
  119.  
  120.         }
  121.  
  122.        
  123.  
  124.         buildOrder = new ArrayList<UnitType>();
  125.  
  126.         buildOrder.add(UnitType.Protoss_Pylon);
  127.  
  128.         buildOrder.add(UnitType.Protoss_Gateway);
  129.  
  130.         buildOrder.add(UnitType.Protoss_Pylon);
  131.  
  132.         buildOrder.add(UnitType.Protoss_Gateway);
  133.  
  134.         buildOrder.add(UnitType.Protoss_Pylon);
  135.  
  136.     }
  137.  
  138.  
  139.  
  140.     @Override
  141.  
  142.     public void onFrame() {
  143.  
  144.        
  145.  
  146.         UnitHousekeeping();
  147.  
  148.          
  149.  
  150.         StartNextBuildInOrder();
  151.  
  152.         AssignNextBuildInOrderToWorker();
  153.  
  154.         SignOffOnBuild();
  155.  
  156.        
  157.  
  158.         MarshalZealots();
  159.  
  160.  
  161.  
  162.         ManageFood();
  163.  
  164.      
  165.  
  166.     }
  167.  
  168.  
  169.  
  170. private void UnitHousekeeping(){
  171.  
  172.  
  173.  
  174.     game.drawTextScreen(10, 10, "Playing as " + self.getName() + " - " + self.getRace());
  175.  
  176.  
  177.  
  178.     StringBuilder units = new StringBuilder("My units:\n");
  179.  
  180.    
  181.  
  182.     int probeCount = 0;
  183.  
  184.     pylonCount = 0;
  185.  
  186.     zealotCount = 0;
  187.  
  188.    
  189.  
  190.    
  191.  
  192.     //iterate through my units
  193.  
  194.     for (Unit myUnit : self.getUnits()) {
  195.  
  196.         units.append(myUnit.getType()).append(" ").append(myUnit.getTilePosition()).append("\n");
  197.  
  198.  
  199.  
  200.         //if there's enough minerals, train an SCV
  201.  
  202.         if (myUnit.getType() == UnitType.Protoss_Nexus && self.minerals() >= UnitType.Protoss_Probe.mineralPrice()-promisedMinerals && greatestProbeCount < maxProbesAllowed) {
  203.  
  204.             if(myUnit.isIdle() && (scheduledBuilding == null))
  205.  
  206.                 myUnit.train(UnitType.Protoss_Probe);
  207.  
  208.         }
  209.  
  210.  
  211.  
  212.         if (myUnit.getType() == UnitType.Protoss_Gateway && self.minerals() >= UnitType.Protoss_Zealot.mineralPrice()) {
  213.  
  214.             if(myUnit.isIdle())
  215.  
  216.                 myUnit.train(UnitType.Protoss_Zealot);
  217.  
  218.         }
  219.  
  220.         if (myUnit.getType() == UnitType.Protoss_Forge && self.minerals() >= UpgradeType.Protoss_Ground_Weapons.mineralPrice()) {
  221.  
  222.             if(myUnit.isIdle())
  223.  
  224.                 myUnit.upgrade(UpgradeType.Protoss_Ground_Weapons);
  225.  
  226.         }
  227.  
  228.        
  229.  
  230.         if (myUnit.getType() == UnitType.Protoss_Probe)
  231.  
  232.             probeCount++;
  233.  
  234.        
  235.  
  236.         if (myUnit.getType() == UnitType.Protoss_Zealot)
  237.  
  238.             if(myUnit.isCompleted())
  239.  
  240.                 zealotCount++;
  241.  
  242.        
  243.  
  244.         if (myUnit.getType() == UnitType.Protoss_Pylon){
  245.  
  246.             if(myUnit.isCompleted())
  247.  
  248.                 pylonCount++;
  249.  
  250.         }
  251.  
  252.        
  253.  
  254.         //if it's a drone and it's idle, send it to the closest mineral patch
  255.  
  256.         if (myUnit.getType().isWorker() && myUnit.isIdle()) {
  257.  
  258.             Unit closestMineral = null;
  259.  
  260.  
  261.  
  262.             //find the closest mineral
  263.  
  264.             for (Unit neutralUnit : game.neutral().getUnits()) {
  265.  
  266.                 if (neutralUnit.getType().isMineralField()) {
  267.  
  268.                     if (closestMineral == null || myUnit.getDistance(neutralUnit) < myUnit.getDistance(closestMineral)) {
  269.  
  270.                         closestMineral = neutralUnit;
  271.  
  272.                     }
  273.  
  274.                 }
  275.  
  276.             }
  277.  
  278.  
  279.  
  280.             //if a mineral patch was found, send the drone to gather it
  281.  
  282.             if (closestMineral != null) {
  283.  
  284.                 myUnit.gather(closestMineral, false);
  285.  
  286.             }
  287.  
  288.         }
  289.  
  290.     }
  291.  
  292.     if (probeCount > greatestProbeCount)
  293.  
  294.         greatestProbeCount = probeCount;   
  295.  
  296.    
  297.  
  298.     //draw my units on screen
  299.  
  300.     game.drawTextScreen(10, 25, units.toString());
  301.  
  302. }
  303.  
  304.  
  305.  
  306. //Identify the enemy position. If we have enough zealots, attack
  307.  
  308. private void MarshalZealots(){
  309.  
  310.     Position basePosition = new Position( (game.mapWidth()- self.getStartLocation().getX())* 32, (game.mapHeight()-self.getStartLocation().getY()) * 32);
  311.  
  312.     if (zealotCount > zealotAttackThreshold){
  313.  
  314.         for (Unit myUnit : self.getUnits()) {
  315.  
  316.             if (myUnit.getType() == UnitType.Protoss_Zealot){
  317.  
  318.             //  myUnit.attack(new Position(enemyBase.getX()* 32, enemyBase.getY() * 32));
  319.  
  320.                 if (myUnit.isIdle())
  321.  
  322.                     if (myUnit.getDistance(basePosition) < 100)
  323.  
  324.                         myUnit.attack(randomPosition());
  325.  
  326.                     else
  327.  
  328.                         myUnit.attack(basePosition);       
  329.  
  330.             }
  331.  
  332.         }
  333.  
  334.     }
  335.  
  336. }
  337.  
  338.  
  339.  
  340. //Get a valid random position in pixel space
  341.  
  342. public Position randomPosition(){
  343.  
  344.     int x = 32*(int)(Math.random() * (float)game.mapWidth());
  345.  
  346.     int y = 32*(int)(Math.random() * (float)game.mapHeight());
  347.  
  348.    
  349.  
  350.     return new Position(x, y);
  351.  
  352. }
  353.  
  354. //Build the next object in the build order, if possible
  355.  
  356. private void StartNextBuildInOrder(){
  357.  
  358.     //If the build order is not complete
  359.  
  360.     if (buildOrder.size() > 0){
  361.  
  362.         //If we are not already building something
  363.  
  364.         if (scheduledBuilding == null){
  365.  
  366.             UnitType unitToBuild = buildOrder.get(0);
  367.  
  368.            
  369.  
  370.             //If we can currently afford the next building in the order
  371.  
  372.             if (self.minerals() >= unitToBuild.mineralPrice()){
  373.  
  374.                 //Only build a non-pylon building if we have pylons
  375.  
  376.                 if (unitToBuild == UnitType.Protoss_Pylon || pylonCount > 0){
  377.  
  378.                 scheduledBuilding = unitToBuild;
  379.  
  380.                 promisedMinerals = unitToBuild.mineralPrice();
  381.  
  382.                 //Count the number of buildings of this type currently in existence, so that we can
  383.  
  384.                 //detect when construction starts on this building
  385.  
  386.                 scheduledBuildingCount = 0;
  387.  
  388.                 for (Unit myUnit : self.getUnits()) {
  389.  
  390.                     if (myUnit.getType() == scheduledBuilding){
  391.  
  392.                         scheduledBuildingCount++;
  393.  
  394.                     }
  395.  
  396.                 }
  397.  
  398.                 }
  399.  
  400.             }
  401.  
  402.         }
  403.  
  404.        
  405.  
  406.     }
  407.  
  408. }
  409.  
  410.  
  411.  
  412. //If a building is planned, assign to a worker
  413.  
  414. private void AssignNextBuildInOrderToWorker(){
  415.  
  416.     if (scheduledBuilding != null)
  417.  
  418.     {
  419.  
  420.         for (Unit myUnit : self.getUnits()) {
  421.  
  422.             if (myUnit.getType() == UnitType.Protoss_Probe){
  423.  
  424.                 TilePosition buildTile =
  425.  
  426.                         getBuildTile(myUnit, scheduledBuilding, self.getStartLocation());
  427.  
  428.                     //and, if found, send the worker to build it (and leave others alone - break;)
  429.  
  430.                     if (buildTile != null) {
  431.  
  432.                         myUnit.build(scheduledBuilding, buildTile);
  433.  
  434.                         break;
  435.  
  436.                     }
  437.  
  438.             }
  439.  
  440.         }
  441.  
  442.     }
  443.  
  444. }
  445.  
  446.  
  447.  
  448. //Detect when construction has started on next building, and set scheduledBuilding to null
  449.  
  450. private void SignOffOnBuild(){
  451.  
  452.     if (scheduledBuilding!= null){
  453.  
  454.     int newCountOfScheduledBuildingType = 0;
  455.  
  456.     for (Unit myUnit : self.getUnits()) {
  457.  
  458.         if (myUnit.getType() == scheduledBuilding){
  459.  
  460.             newCountOfScheduledBuildingType++;
  461.  
  462.         }
  463.  
  464.     }
  465.  
  466.    
  467.  
  468.     if (newCountOfScheduledBuildingType > scheduledBuildingCount ){
  469.  
  470.         buildOrder.remove(0);
  471.  
  472.         scheduledBuilding = null;
  473.  
  474.         promisedMinerals = 0;
  475.  
  476.     }
  477.  
  478.     }
  479.  
  480. }
  481.  
  482.  // Returns a suitable TilePosition to build a given building type near
  483.  
  484.  // specified TilePosition aroundTile, or null if not found. (builder parameter is our worker)
  485.  
  486.  public TilePosition getBuildTile(Unit builder, UnitType buildingType, TilePosition aroundTile) {
  487.  
  488.     TilePosition ret = null;
  489.  
  490.     int maxDist = buildingType.tileWidth();
  491.  
  492.     int stopDist = 40;
  493.  
  494.    
  495.  
  496.     // Refinery, Assimilator, Extractor
  497.  
  498.     if (buildingType.isRefinery()) {
  499.  
  500.         for (Unit n : game.neutral().getUnits()) {
  501.  
  502.             if ((n.getType() == UnitType.Resource_Vespene_Geyser) &&
  503.  
  504.                     ( Math.abs(n.getTilePosition().getX() - aroundTile.getX()) < stopDist ) &&
  505.  
  506.                     ( Math.abs(n.getTilePosition().getY() - aroundTile.getY()) < stopDist )
  507.  
  508.                     ) return n.getTilePosition();
  509.  
  510.         }
  511.  
  512.     }
  513.  
  514.    
  515.  
  516.     while ((maxDist < stopDist) && (ret == null)) {
  517.  
  518.         for (int i=aroundTile.getX()-maxDist; i<=aroundTile.getX()+maxDist; i++) {
  519.  
  520.             for (int j=aroundTile.getY()-maxDist; j<=aroundTile.getY()+maxDist; j++) {
  521.  
  522.                 if (game.canBuildHere(new TilePosition(i,j), buildingType)) {
  523.  
  524.                     // units that are blocking the tile
  525.  
  526.                     boolean unitsInWay = false;
  527.  
  528.                     for (Unit u : game.getAllUnits()) {
  529.  
  530.                         if (u.getID() == builder.getID()) continue;
  531.  
  532.                         if ((Math.abs(u.getTilePosition().getX()-i) < 4) && (Math.abs(u.getTilePosition().getY()-j) < 4)) unitsInWay = true;
  533.  
  534.                     }
  535.  
  536.                     if (!unitsInWay) {
  537.  
  538.                         return new TilePosition(i, j);
  539.  
  540.                     }
  541.  
  542.                     // creep for Zerg
  543.  
  544.                     if (buildingType.requiresCreep()) {
  545.  
  546.                         boolean creepMissing = false;
  547.  
  548.                         for (int k=i; k<=i+buildingType.tileWidth(); k++) {
  549.  
  550.                             for (int l=j; l<=j+buildingType.tileHeight(); l++) {
  551.  
  552.                                 if (!game.hasCreep(k, l)) creepMissing = true;
  553.  
  554.                                 break;
  555.  
  556.                             }
  557.  
  558.                         }
  559.  
  560.                         if (creepMissing) continue;
  561.  
  562.                     }
  563.  
  564.                 }
  565.  
  566.             }
  567.  
  568.         }
  569.  
  570.         maxDist += 2;
  571.  
  572.     }
  573.  
  574.    
  575.  
  576.     if (ret == null) game.printf("Unable to find suitable build position for "+buildingType.toString());
  577.  
  578.     return ret;
  579.  
  580.  }
  581.  
  582.  
  583.  
  584.  private void ManageFood(){
  585.  
  586.  
  587.  
  588.      if ((self.supplyTotal() - self.supplyUsed() < 4) && (self.minerals() >= 400) && scheduledBuilding == null) {
  589.  
  590.        
  591.  
  592.         //iterate over units to find a worker
  593.  
  594.         for (Unit myUnit : self.getUnits()) {
  595.  
  596.             if (myUnit.getType() == UnitType.Protoss_Probe) {
  597.  
  598.                 //get a nice place to build a supply depot
  599.  
  600.                 TilePosition buildTile =
  601.  
  602.                     getBuildTile(myUnit, UnitType.Protoss_Pylon, self.getStartLocation());
  603.  
  604.                 //and, if found, send the worker to build it (and leave others alone - break;)
  605.  
  606.                 if (buildTile != null) {
  607.  
  608.                     myUnit.build(UnitType.Protoss_Pylon, buildTile);
  609.  
  610.                     break;
  611.  
  612.                 }
  613.  
  614.             }
  615.  
  616.         }
  617.  
  618.      }
  619.  
  620.  }
  621.  
  622.  
  623.  
  624.     public static void main(String[] args) {
  625.  
  626.         new TestBot11().run();
  627.  
  628.     }
  629.  
  630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement