Пример #1
0
 public static void fireCircle(RobotController rc, int radius, MapLocation center) {
   for (int k = 0; k < directions.length; k++) {
     while (!rc.isActive()) {}
     MapLocation toFire = center.add(directions[k], radius);
     try {
       if (toFire.x >= 0
           && toFire.x < rc.getMapWidth()
           && toFire.y >= 0
           && toFire.y < rc.getMapHeight()) {
         rc.attackSquare(toFire);
         rc.yield();
       }
     } catch (Exception e) {
     }
     while (!rc.isActive()) {}
     toFire = center;
     for (int a = 0; a < radius / 2; a++) {
       toFire = toFire.add(directions[k]);
       toFire = toFire.add(directions[(k + 1) % directions.length]);
     }
     try {
       if (toFire.x >= 0
           && toFire.x < rc.getMapWidth()
           && toFire.y >= 0
           && toFire.y < rc.getMapHeight()) {
         rc.attackSquare(toFire);
         rc.yield();
       }
     } catch (Exception e) {
     }
   }
 }
Пример #2
0
  // All combat units (Soldiers, Bashers, Tanks, Drones, Launchers, Commander)
  private static void runCombat() {
    while (true) {
      threats.update();
      myLoc = rc.getLocation();

      // Move if we can and want to
      if (rc.isCoreReady()) {
        boolean ignoreThreat = overwhelms();

        if (!ignoreThreat && shouldRetreat()) {
          if (rc.isWeaponReady() && myType.loadingDelay == 0) attackWeakest();
          doRetreatMove(); // Pull back if in range of the enemy guns
        } else {
          boolean engaged = false;
          if (rc.isCoreReady() && inCombat(4)) engaged = doCloseWithEnemyMove(ignoreThreat);
          if (rc.isCoreReady()
              && !engaged) // Close with enemy might not do a move if the enemy is a drone out of
                           // reach
          doAdvanceMove();
        }
      }

      // Attack if there is an enemy in sight
      if (myType == RobotType.LAUNCHER) doLaunch();
      else if (rc.isWeaponReady()) attackWeakest();

      doTransfer();

      rc.yield();
    }
  }
Пример #3
0
  // Drones
  private static void runDrone() {
    moveDir = Direction.NORTH;
    droneMoveCurrent = 1;
    droneMoveMax = 2;
    patrolClockwise = true;
    droneCentred = false; // We haven't made it to the centre of our spiral yet

    while (true) {
      threats.update();
      myLoc = rc.getLocation();

      // Attack if there is an enemy in sight
      if (rc.isWeaponReady()) attackWeakest();

      // Move if we can and want to
      if (rc.isCoreReady()) {
        if (shouldRetreat()) {
          doRetreatMove(); // Pull back if in range of the enemy guns
        } else if (Clock.getRoundNum() < 600) {
          doPatrol();
        } else {
          doSupply();
        }
      }

      doTransfer();

      rc.yield();
    }
  }
Пример #4
0
  private static void runTower() {
    while (true) {
      threats.update();

      // Attack if there is an enemy in sight
      if (rc.isWeaponReady()) attackWeakest();

      rc.yield();
    }
  }
Пример #5
0
  private static void runMissile() {
    int lastTurn = Clock.getRoundNum() + GameConstants.MISSILE_LIFESPAN;
    int[] damageRange = {0, 8, 15, 24, 35, 48};
    MapLocation target = null;
    boolean targetMoves = true;

    while (true) {
      myLoc = rc.getLocation();
      int turns = lastTurn - Clock.getRoundNum();

      if (targetMoves) { // Re-acquire the target's location
        RobotInfo[] inRange = rc.senseNearbyRobots(damageRange[turns], enemyTeam);
        if (inRange.length > 0) { // No units to target
          target = inRange[0].location;
          targetMoves = inRange[0].type.canMove();
          rc.setIndicatorString(0, "Missile targetting " + inRange[0].type + "@" + target);
        } else {
          targetMoves = false; // Pick a tower or the HQ
          MapLocation[] enemyTowers = rc.senseEnemyTowerLocations();
          for (MapLocation t : enemyTowers) {
            if (myLoc.distanceSquaredTo(t) <= damageRange[turns]) {
              target = t;
              rc.setIndicatorString(0, "Missile targetting Tower @" + target);
              break;
            }
          }
          if (target == null) {
            target = rc.senseEnemyHQLocation();
            rc.setIndicatorString(0, "Missile targetting HQ @" + target);
          }
        }
      }

      try {
        if (target != null) {
          if (myLoc.distanceSquaredTo(target) <= GameConstants.MISSILE_RADIUS_SQUARED) rc.explode();
          else {
            Direction d = myLoc.directionTo(target);
            if (rc.canMove(d)) rc.move(d);
            else if (rc.canMove(d.rotateLeft())) rc.move(d.rotateLeft());
            else if (rc.canMove(d.rotateRight())) rc.move(d.rotateRight());
          }
        }
      } catch (GameActionException e) {
        System.out.println("Missile exception");
        // e.printStackTrace();
      }
      rc.yield();
    }
  }
Пример #6
0
  // Factories and supply depots
  private static void runBuilding() { // Most builds spawn units
    if (myType.canSpawn()) strategy = new BuildStrategy(rc);

    while (true) {
      if (rc.isCoreReady() && myType.canSpawn()) {
        threats.update();
        RobotType build = strategy.getBuildOrder();
        if (build != null) trySpawn(rc.getLocation().directionTo(threats.enemyHQ), build);
      }

      doTransfer();

      rc.yield();
    }
  }
Пример #7
0
  // Beavers
  private static void runBeaver() {
    strategy = new BuildStrategy(rc);
    rand = new Random(rc.getID());

    while (true) {
      threats.update();
      myLoc = rc.getLocation();

      if (rc.isCoreReady()) {
        RobotType build = strategy.getBuildOrder();
        if (build != null) tryBuild(rc.getLocation().directionTo(threats.enemyHQ), build);
      }

      // Attack if there is an enemy in sight
      if (rc.isWeaponReady()) attackWeakest();

      double ore = rc.senseOre(rc.getLocation());

      // Move if we can and want to
      if (rc.isCoreReady()) {
        boolean ignoreThreat = overwhelms();

        if (!ignoreThreat && shouldRetreat()) {
          doRetreatMove(); // Pull back if in range of the enemy guns
        } else {
          doMinerMove();
          if (ore == 0 && rc.isCoreReady()) { // We didn't find ore nearby
            doSearchMove();
          }
        }
      }

      // Mine if possible
      if (rc.isCoreReady() && ore > 0) {
        try {
          rc.mine();
        } catch (GameActionException e) {
          System.out.println("Mining Exception");
          // e.printStackTrace();
        }
      }

      doTransfer();

      rc.yield();
    }
  }
Пример #8
0
 public static void expandIndividual() throws GameActionException {
   MapLocation expandLocation =
       new MapLocation(
           rc.readBroadcast(Constants.singleExpandXChannel),
           rc.readBroadcast(Constants.singleExpandYChannel));
   rc.broadcast(Constants.commandChannel, Constants.commandRally);
   while (true) {
     if (rc.getLocation().distanceSquaredTo(expandLocation)
         < 1) // if we are at the location of the rally point
     {
       if (rc.isActive()) // if we are allowed to capture
       {
         if (rc.senseCaptureCost() + 1.8 * getNumberOfAlliedRobosAfterMe()
             < rc.getTeamPower()) // if we have enough power to capture
         {
           int readIn = rc.readBroadcast(Constants.campChannel);
           if (readIn == Constants.campGen) {
             rc.broadcast(Constants.campChannel, Constants.campGenInProduction);
             rc.captureEncampment(RobotType.GENERATOR);
           } else if (readIn == Constants.campGenInProduction) {
             rc.captureEncampment(RobotType.SUPPLIER);
           } else if (readIn == Constants.campSupplier) {
             rc.captureEncampment(RobotType.SUPPLIER);
           } else // TODO: transmissions may be being scrambled, for now just make supplier
           {
             rc.captureEncampment(RobotType.SUPPLIER);
           }
           break;
         }
       }
     } else if (rc.senseNearbyGameObjects(Robot.class, expandLocation, 0, rc.getTeam()).length
         > 0) // if there is an allied robot on our rally point
     {
       expandLocation = findClosestEmptyCamp();
       if (expandLocation == null) {
         expandLocation = findRallyPoint();
       }
       goToLocation(expandLocation);
     } else {
       goToLocation(expandLocation);
     }
     rc.yield();
   }
 }
Пример #9
0
  public static void soldierCode(RobotController myRC) throws GameActionException {
    rc = myRC;
    rallyPoint = findRallyPoint();
    while (true) {
      try {
        // returns all enemy robots within our sight range (aka that we can actually fight right now
        // or very soon)
        Robot[] closeEnemyRobots =
            rc.senseNearbyGameObjects(Robot.class, rc.getLocation(), 14, rc.getTeam().opponent());

        // returns all enemy robots reasonably close to us (aka that we should move toward and
        // potentially assist our allies in fighting)
        Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, 63, rc.getTeam().opponent());

        if (closeEnemyRobots.length == 0 && enemyRobots.length == 0) // no enemies are nearby
        {
          int command = HQCommand();
          switch (command) {
            case Constants.commandExpand:
              expand();
              break;
            case Constants.commandRally:
              rally();
              break;
            case Constants.commandEnemyNukeHalfDone:
              rally();
            case Constants.commandBuildIndividual: // TODO- implement this
              break;
          }
        } else {

          // enemy spotted
          localscan = false;
          MapLocation closestEnemy = findClosestRobot(enemyRobots);
          goToLocation(closestEnemy);
        }
      } catch (Exception e) {
        System.out.println("Soldier Exception");
        e.printStackTrace();
      }
      rc.yield();
    }
  }
Пример #10
0
  public static void run(RobotController myRC) {
    rc = myRC;
    barracks = findBarracks();
    while (true) {
      try {
        if (rc.getType() == RobotType.SOLDIER) {
          Robot[] enemyRobots =
              rc.senseNearbyGameObjects(
                  Robot.class, 50000, rc.getTeam().opponent()); // list of enemy robots
          if (enemyRobots.length == 0) {
            Clock.getRoundNum(); // gets the round number
            if (Clock.getRoundNum() < 250) {
              goToLocation(
                  barracks); // if you are before round 250 travel to the "barracks" or meeting
                             // place.
            } else {
              goToLocation(rc.senseEnemyHQLocation());
            }
          } else { // else attack the closest enemy
            int closestDist = 50000;
            MapLocation closestEnemy = null;
            for (int i = 0; i < enemyRobots.length; i++) {
              Robot enemyBot = enemyRobots[i];
              RobotInfo arobotInfo = rc.senseRobotInfo(enemyBot);
              int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation());
              if (dist < closestDist) {
                closestDist = dist;
                closestEnemy = arobotInfo.location;
              }
            }
            goToLocation(closestEnemy);
          }
        } else {
          HqCommand();
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
      rc.yield(); // end turn
    }
  }
Пример #11
0
  // Computers
  private static void runOther() {
    int numTowers = -1;
    while (true) {
      threats.update();
      myLoc = rc.getLocation();

      // Move if we can and want to
      if (rc.isCoreReady() && myType.canMove()) {
        if (shouldRetreat()) doRetreatMove(); // Pull back if in range of the enemy guns
        else doAdvanceMove(); // Move towards our HQ
      }

      doTransfer();

      // Perform a background breadth first search to the enemy HQ
      if (myType == RobotType.COMPUTER && Clock.getBytecodesLeft() > 1000) {
        bfs.work(threats.enemyHQ, Bfs.PRIORITY_HIGH, 1000, numTowers != threats.enemyTowers.length);
      }
      numTowers = threats.enemyTowers.length;
      rc.yield();
    }
  }
Пример #12
0
 public static void pullInto(RobotController rc, int radius, MapLocation center) {
   for (int k = 0; k < directions.length; k++) {
     while (!rc.isActive()) {}
     MapLocation toFire = center.add(directions[k], radius);
     try {
       while (toFire.distanceSquaredTo(center) > 3) {
         if (toFire.x >= 0
             && toFire.x < rc.getMapWidth()
             && toFire.y >= 0
             && toFire.y < rc.getMapHeight()) {
           try {
             rc.attackSquare(toFire);
             rc.yield();
           } catch (Exception e) {
           }
         }
         toFire = toFire.add(directions[k].opposite());
       }
     } catch (Exception e) {
     }
   }
 }
Пример #13
0
  public static void run(RobotController rc) {
    int type = 0;

    while (true) {
      try {
        if (rc.getType() == RobotType.HQ) {
          new SmartHQ(rc).run();
        } else if (rc.getType() == RobotType.NOISETOWER) {
          new GenericTower(rc, false).run();
        } else if (rc.getType() == RobotType.PASTR) {
          rc.yield();
        } else {
          if (type == 0) {
            type = rc.readBroadcast(0);
          } else if (type == SmartHQ.DURAN) {
            // new Duran(rc).run();
          } else if (type == SmartHQ.GHOST) {
            // new Ghost(rc).run();
          } else if (type == SmartHQ.GOLIATH) {
            // new Goliath(rc).run();
          } else if (type == SmartHQ.MARINE) {
            // new Marines(rc).run();
          } else if (type == SmartHQ.MULE) {
            new fastMULE(rc, true).run();
          } else if (type == SmartHQ.TOWER) {
            new SmartTower(rc, true).run();
          } else if (type == SmartHQ.TROLL) {
            // new Scout(rc).run();
          } else if (type == SmartHQ.OPTOWER) {
            new SmartTower(rc, false).run();
          } else if (type == SmartHQ.OPMULE) {
            new fastMULE(rc, false).run();
          }
        }
      } catch (Exception e) {
      }
    }
  }
Пример #14
0
 private static void rally() throws GameActionException {
   rallyPoint = findRallyPoint();
   // if we are fairly close to the rally point and we have the necessary soldier counts to make up
   // a wave, gogogogogo
   if (rc.getLocation().distanceSquaredTo(rallyPoint) < rallyRadius) {
     if (rc.readBroadcast(Constants.attackChannel) == Constants.attackAllIn) {
       while (true) {
         Robot[] closeEnemyRobots =
             rc.senseNearbyGameObjects(Robot.class, rc.getLocation(), 14, rc.getTeam().opponent());
         if (closeEnemyRobots.length == 0) {
           goToLocation(rc.senseEnemyHQLocation());
         } else {
           goToLocation(Util.findClosestRobot(rc, closeEnemyRobots));
         }
         rc.yield();
       }
     } else {
       goToLocation(rallyPoint);
     }
   } else {
     goToLocation(rallyPoint);
   }
 }
Пример #15
0
  public static void run(RobotController rcIn) throws GameActionException {
    rc = rcIn;
    randall.setSeed(rc.getRobot().getID());

    if (rc.getType() == RobotType.HQ) {
      tryToSpawn();
    } else {
      BreadthFirst.init(rc, bigBoxSize);
      MapLocation goal = getRandomLocation();
      path =
          BreadthFirst.pathTo(
              VectorFunctions.mldivide(rc.getLocation(), bigBoxSize),
              VectorFunctions.mldivide(goal, bigBoxSize),
              100000);
      // VectorFunctions.printPath(path,bigBoxSize);
    }

    // generate a coarsened map of the world
    // TODO only HQ should do this. The others should download it.
    //		MapAssessment.assessMap(4);
    //		MapAssessment.printBigCoarseMap();
    //		MapAssessment.printCoarseMap();

    while (true) {
      try {
        if (rc.getType() == RobotType.HQ) {
          runHQ();
        } else if (rc.getType() == RobotType.SOLDIER) {
          runSoldier();
        }
      } catch (Exception e) {
        // e.printStackTrace();
      }
      rc.yield();
    }
  }
Пример #16
0
  // HQ is responsible for collating unit counts and broadcasting them each turn
  // It also needs to pass on its supply each turn and fire if there are enemies in range
  private static void runHQ() {
    // double lastOre = 500;
    strategy = new BuildStrategy(rc);

    while (true) {
      threats.update();
      strategy.broadcast();
      // double oreIncome = rc.getTeamOre() - lastOre + strategy.oreSpent();
      // System.out.println("Ore income " + oreIncome + " per miner = " + (oreIncome-5) /
      // (strategy.units(RobotType.MINER) + strategy.units(RobotType.BEAVER)));

      // See if we need to spawn a beaver
      if (rc.isCoreReady()) {
        RobotType build = strategy.getBuildOrder();
        if (build != null) {
          trySpawn(rc.getLocation().directionTo(threats.enemyHQ), build);
        }
      }

      // Attack if there is an enemy in sight
      if (rc.isWeaponReady()) {
        int senseRange = RobotType.HQ.attackRadiusSquared;
        MapLocation[] myTowers = rc.senseTowerLocations();
        if (myTowers.length >= 5) {
          senseRange =
              52; // This is slightly larger than the real range but does include all possible
                  // enemies that can be hit with splash
          attackRange = GameConstants.HQ_BUFFED_ATTACK_RADIUS_SQUARED;
        } else if (myTowers.length >= 2) {
          attackRange = GameConstants.HQ_BUFFED_ATTACK_RADIUS_SQUARED;
        } else {
          attackRange = senseRange;
        }
        RobotInfo[] enemies = rc.senseNearbyRobots(senseRange, enemyTeam);
        // Pick the first valid target
        MapLocation best = null;
        for (RobotInfo e : enemies) {
          int range = e.location.distanceSquaredTo(myLoc);
          if (range <= attackRange) {
            best = e.location;
            break;
          }
          if (myTowers.length
              >= 5) { // Check for tiles adjacent to the enemy as they might be in splash range
            Direction d = e.location.directionTo(myLoc);
            if (e.location.add(d).distanceSquaredTo(myLoc) <= attackRange) {
              best = e.location.add(d);
              break;
            }
            if (e.location.add(d.rotateLeft()).distanceSquaredTo(myLoc) <= attackRange) {
              best = e.location.add(d.rotateLeft());
              break;
            }
            if (e.location.add(d.rotateRight()).distanceSquaredTo(myLoc) <= attackRange) {
              best = e.location.add(d.rotateRight());
              break;
            }
          }
        }
        if (best != null) {
          try {
            rc.attackLocation(best);
          } catch (GameActionException e) {
            System.out.println("HQ attack exception");
            // e.printStackTrace();
          }
        }
      }

      doTransfer();

      // lastOre = rc.getTeamOre();
      rc.yield();
    }
  }
Пример #17
0
  // this method will advance one square towards a target and try to avoid enemies as much as
  // possible
  public static void avoidEnemiesMove(RobotController rc, MapLocation target) {
    try {
      // first we will find all enemy bots near us
      GameObject[] nearByBots = rc.senseNearbyGameObjects(Robot.class, 15, rc.getTeam().opponent());
      Direction direction = rc.getLocation().directionTo(target);

      // if we don't see anything then lets head towards target
      if (nearByBots.length == 0) {
        // rc.setIndicatorString(2, "No enemies detected");
        // rc.setIndicatorString(1, "x: "+target.x + " y: " + target.y);
        direction = rc.getLocation().directionTo(target);
        if (rc.canMove(direction)) // &&
        // !rc.senseTerrainTile(rc.getLocation().add(direction).add(direction)).equals(TerrainTile.VOID))
        {
          if (rc.isActive()) {
            rc.move(direction);
          }
        } else {
          MapLocation target2 = rc.getLocation().add(direction);
          if (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) {
            int j = 0;
            while (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) {
              rc.setIndicatorString(0, "" + j);
              j++;

              target2 = target2.add(direction);
            }
            Utilities.MoveMapLocation(rc, target2, false);
          }
          /*
          int distanceRight = 0;
          int distanceLeft = 0;
             direction = direction.rotateRight();
             while (!rc.canMove(direction) && rc.senseTerrainTile(rc.getLocation().add(direction)).equals(TerrainTile.VOID))
             {
                 direction = direction.rotateRight();
             }
             if (rc.isActive())
             {
                 if (rc.canMove(direction))
                 {
                     rc.move(direction);
                 }
             }
             */
        }
      }
      // otherwise we need to avoid them
      else {
        rc.setIndicatorString(2, "Avoiding enemies");
        rc.setIndicatorString(1, "Numb of Enemies: " + nearByBots.length);
        // now we will calculate the distance form all 5 spots towards are target and the distance
        // from that spot to all enemies we can see
        // we will pick the one with the greatest distance
        int[] distancesToLocations = new int[5];

        for (int k = 0; k < distancesToLocations.length; k++) {
          distancesToLocations[k] = 0;
        }
        MapLocation spot;
        Direction newDir;

        // first we look 90 to our right
        newDir = direction.rotateRight().rotateRight();
        for (int j = 0; j < 5; j++) {
          if (rc.canMove(newDir)) {
            spot = rc.getLocation().add(newDir);
            for (int i = 0; i < nearByBots.length; i++) {
              // System.out.println("entering for loop");
              distancesToLocations[j] += spot.distanceSquaredTo(rc.senseLocationOf(nearByBots[i]));
            }
          } else {
            distancesToLocations[j] = -123;
          }
          // every time through the loop we look one further to the left
          newDir.rotateLeft();
        }

        int indexOfLargest = 0;
        int largest = distancesToLocations[0];
        for (int j = 1; j < distancesToLocations.length; j++) {
          if (largest < distancesToLocations[j]) {
            indexOfLargest = j;
            largest = distancesToLocations[j];
          }
        }

        // now we orientate newDir to the right spot
        newDir = direction.rotateRight().rotateRight();
        for (int i = 0; i <= indexOfLargest; i++) {
          newDir = newDir.rotateLeft();
        }

        while (!rc.isActive()) {
          rc.yield();
        }

        // now we can finally move
        if (rc.isActive()) {
          if (rc.canMove(newDir)) {
            rc.move(newDir);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #18
0
  public static void MoveMapLocation(RobotController rc, MapLocation target, boolean sneak) {
    MapLocation[] pastLocations = new MapLocation[10];
    int side = 45;
    Direction dir;
    Direction newDir;
    rand = new Random();
    // we initialize pastLocations to hold our current location 5 times
    for (int i = 0; i < pastLocations.length; i++) {
      pastLocations[i] = rc.getLocation();
    }

    // this method will run until we get to our target location
    while (!rc.getLocation().equals(target)) {
      // we put the try block inside of the while loop so an exception won't terminate the method
      try {
        dir = rc.getLocation().directionTo(target);
        newDir = Direction.NONE;

        // simple shoot at an enemy if we see one will need to be improved later
        Robot[] nearbyEnemies = rc.senseNearbyGameObjects(Robot.class, 10, rc.getTeam().opponent());
        if (nearbyEnemies.length > 0 && rc.senseRobotInfo((Robot) nearbyEnemies[0]).health < 201) {
          fire(rc);
        }
        // if we can move towards target and we haven't been on the square recently then lets move
        else if (rc.canMove(dir)
            && !MapLocationInArray(rc, rc.getLocation().add(dir), pastLocations)) {
          newDir = dir;
          // if we found a direction to move then we go to it
          if (newDir != Direction.NONE) {
            // now we decide if we are going to sneak or run
            if (sneak) {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                // System.out.println(newDir);
                rc.sneak(newDir);
              }
            } else {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                rc.move(newDir);
              }
            }
          }
          side = 45;
        } else {
          // if their is a robot blocking our way then we just move in a random direction
          if (rc.senseObjectAtLocation(rc.getLocation().add(dir)) != null) {
            // newDir = directions[rand.nextInt(8)];
            MoveDirection(rc, dir, sneak);
          } else {
            // rc.setIndicatorString(2, "Looking elswhere");
            Direction dir2 = dir;
            MapLocation right;
            MapLocation left;
            dir2 = (dir.rotateRight());
            while (!rc.canMove(dir2)) {
              dir2 = dir2.rotateRight();
            }
            right = rc.getLocation().add(dir2);

            dir2 = dir.rotateLeft();
            while (!rc.canMove(dir2)) {
              dir2 = dir2.rotateLeft();
            }

            left = rc.getLocation().add(dir2);

            // left seems better so lets go that way
            if (left.distanceSquaredTo(target) < right.distanceSquaredTo(target)) {
              side = 1;
            }
            // right seems better so lets try that way
            else {
              side = 0;
            }

            // we will go hugging one side of obstacle until we get back on our original line
            while (!dir2.equals(dir) && !rc.getLocation().equals(target)) // && rc.canMove(dir2))
            {
              try {
                if (rc.isActive()) {
                  // rc.setIndicatorString(1, "Trying to Avoid");
                  // rc.setIndicatorString(2, ""+side);
                  dir2 = rc.getLocation().directionTo(target);
                  if (rc.canMove(dir2)
                      && !MapLocationInArray(rc, rc.getLocation().add(dir2), pastLocations)) //  &&
                  // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID))
                  {
                    // rc.setIndicatorString(0, "Going straight");
                  } else {
                    for (int i = 0; i < 4; i++) {
                      if (side == 1) {
                        dir2 = dir2.rotateLeft();
                      } else {
                        dir2 = dir2.rotateRight();
                      }
                      if (rc.senseTerrainTile(rc.getLocation().add(dir2))
                          .equals(TerrainTile.OFF_MAP)) {
                        dir2 = Direction.NONE;
                        i = 48;
                      } else if ((rc.canMove(dir2)
                          || (rc.senseObjectAtLocation(rc.getLocation().add(dir2))
                              != null))) // && !MapLocationInArray(rc, rc.getLocation().add(dir2),
                      // pastLocations))// &&
                      // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID))
                      {
                        i = 48;
                      } else if (i == 3) {
                        dir2 = Direction.NONE;
                        // rc.setIndicatorString(1, "We failed to find a spot");
                      }
                    }
                  }

                  // if we can move
                  if (dir2 != Direction.NONE) {
                    if (rc.isActive()) {
                      if (rc.canMove(dir2)) {
                        if (sneak) {
                          rc.sneak(dir2);
                        } else {
                          rc.move(dir2);
                        }
                      } else {
                        MoveDirection(rc, dir2, sneak);
                      }
                    }
                  } else {
                    if (side == 1) {
                      side = 0;
                    } else {
                      side = 1;
                    }
                  }
                }
                // rc.setIndicatorString(0, "Dir: "+ dir +" Dir2: " + dir2);
              } catch (Exception e) {
                // tell the console we through an exception in utility object for debug purposes
                // System.out.println("Utility Exception");
                // System.out.println(e.toString());
                e.printStackTrace();
                rc.yield();
              }
              if (!rc.getLocation().equals(pastLocations[(pastLocations.length - 1)])) {
                for (int j = 0; j < (pastLocations.length - 1); j++) {
                  pastLocations[j] = pastLocations[j + 1];
                  // System.out.println(pastLocations[j]);
                }
                // stick current local into array
                pastLocations[(pastLocations.length - 1)] = rc.getLocation();
              }

              rc.yield();
            }
            // rc.setIndicatorString(1, "Not trying to Avoid");
          }
        }

        // now we  shift everything up one in pastLocations
        if (rc.getLocation() != pastLocations[(pastLocations.length - 1)]) {
          for (int j = 0; j < (pastLocations.length - 1); j++) {
            pastLocations[j] = pastLocations[j + 1];
            // System.out.println(pastLocations[j]);
          }
          // stick current local into array
          pastLocations[(pastLocations.length - 1)] = rc.getLocation();
        }

        rc.yield();
      } catch (Exception e) {
        // tell the console we through an exception in utility object for debug purposes
        System.out.println("Utility Exception");
        e.printStackTrace();
        // System.out.println(e.toString());
        rc.yield();
      }
    }
  }
Пример #19
0
 public static void run(RobotController myRC) {
   rc = myRC;
   rallyPoint = findRallyPoint();
   while (true) {
     try {
       if (rc.getType() == RobotType.SOLDIER) {
         if (rc.senseEncampmentSquare(rc.getLocation())) {
           if (rc.getTeamPower() > 10 * (rc.senseAlliedEncampmentSquares().length + 10)) {
             rc.captureEncampment(RobotType.SUPPLIER);
           } else {
             rc.captureEncampment(RobotType.GENERATOR);
           }
         } else {
           Robot[] enemyRobots =
               rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam().opponent());
           if (enemyRobots.length == 0
               || (enemyRobots.length == 1
                   && rc.senseRobotInfo(enemyRobots[0]).type
                       == RobotType.HQ)) { // no enemies nearby
             Robot[] alliedRobots = rc.senseNearbyGameObjects(Robot.class, 1000000, rc.getTeam());
             if (alliedRobots.length < 5 * (rc.senseAlliedEncampmentSquares().length) + 1) {
               if (Math.random() < 0.1 && alliedRobots.length < 20) {
                 if (rc.senseMine(rc.getLocation()) == null) {
                   rc.layMine();
                 }
               }
               goToLocation(rallyPoint);
             } else {
               goToLocation(rc.senseEnemyHQLocation());
             }
           } else { // someone spotted
             int closestDist = 100000;
             MapLocation closestEnemy = null;
             for (int i = 0; i < enemyRobots.length; i++) {
               Robot arobot = enemyRobots[i];
               RobotInfo arobotInfo = rc.senseRobotInfo(arobot);
               int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation());
               if (dist < closestDist) {
                 closestDist = dist;
                 closestEnemy = arobotInfo.location;
               }
             }
             Robot[] nearbyAllies = rc.senseNearbyGameObjects(Robot.class, 64, rc.getTeam());
             if (nearbyAllies.length > 2.5 * enemyRobots.length) {
               if (closestDist > 4) {
                 goToLocation(closestEnemy);
               }
             }
             goToLocationAvoidMines(closestEnemy);
           }
         }
       } else {
         hqCode();
       }
     } catch (Exception e) {
       //				System.out.println("caught exception before it killed us:");
       //				e.printStackTrace();
     }
     rc.yield();
   }
 }
Пример #20
0
  public static void hqCode(RobotController myRC) throws GameActionException {
    rc = myRC;
    Direction defaultSpawnDir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
    evaluateMap();
    while (true) {
      rc.broadcast(Constants.attackChannel, 0);
      enemyRobots =
          rc.senseNearbyGameObjects(
              Robot.class, new MapLocation(0, 0), 1000000, rc.getTeam().opponent());

      if (expandOrRally()) {
        rc.broadcast(Constants.commandChannel, Constants.commandExpand);
        broadcastUpdatedBuildOrder();

        MapLocation rally = findRallyPoint();
        rc.broadcast(Constants.rallyXChannel, rally.x);
        rc.broadcast(Constants.rallyYChannel, rally.y);
      } else {
        MapLocation rally = findRallyPoint();
        rc.broadcast(Constants.rallyXChannel, rally.x);
        rc.broadcast(Constants.rallyYChannel, rally.y);
        rc.broadcast(Constants.commandChannel, Constants.commandRally);
      }

      if (rc.isActive()) {
        int readIn = rc.readBroadcast(Constants.campChannel);

        if (!doWeNeedGenerator()) {
          rc.broadcast(Constants.campChannel, Constants.campSupplier);

          // Spawn a soldier
          Team defaultScan = rc.senseMine(rc.getLocation().add(defaultSpawnDir));
          if (rc.canMove(defaultSpawnDir) && (defaultScan == null || defaultScan == rc.getTeam())) {
            rc.spawn(defaultSpawnDir);
          } else {
            for (Direction d : Direction.values()) // TODO: optimize secondary direction finding
            {
              if (d != Direction.OMNI && d != Direction.NONE) {
                Team scan = rc.senseMine(rc.getLocation().add(d));
                if (rc.canMove(d) && (scan == null || scan == rc.getTeam())) {
                  rc.spawn(d);
                  break;
                }
              }
            }
            if (rc.isActive()) {
              // if there are no valid spawn directions
              rc.researchUpgrade(Upgrade.NUKE);
            }
          }
        } else // we do need a generator
        {
          if (readIn == Constants.campSupplier
              || (readIn != Constants.campGenInProduction && readIn != Constants.campGen)) {
            rc.broadcast(Constants.campChannel, Constants.campGen);
          }
          if (!rc.hasUpgrade(Upgrade.FUSION)) {
            rc.researchUpgrade(Upgrade.FUSION);
          } else if (!rc.hasUpgrade(Upgrade.DEFUSION)) {
            rc.researchUpgrade(Upgrade.DEFUSION);
          } else {
            rc.researchUpgrade(Upgrade.NUKE);
          }
        }
      }

      if (Clock.getRoundNum() > 200) {
        if (rc.senseEnemyNukeHalfDone()) {
          rc.broadcast(Constants.commandChannel, Constants.commandEnemyNukeHalfDone);
          while (!rc.hasUpgrade(Upgrade.DEFUSION)) {
            if (rc.isActive()) {
              rc.researchUpgrade(Upgrade.DEFUSION);
            }
            rc.yield();
          }
          enemyRobots =
              rc.senseNearbyGameObjects(
                  Robot.class, new MapLocation(0, 0), 1000000, rc.getTeam().opponent());
        }
      }
      shallWeAllIn();
      rc.yield();
    }
  }
Пример #21
0
 protected void waitUntilMovementIdle() {
   while (myRC.getRoundsUntilMovementIdle() > 0) {
     myRC.yield();
   }
 }