Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 257 additions & 3 deletions RobotPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Random; //Use this instead of Math.random(); seeded by the robot's id so more likely to be random than Math.random
import java.util.Arrays;
import java.util.Collection;
import java.util.ArrayList;

public class RobotPlayer{
/**
Expand Down Expand Up @@ -488,8 +489,8 @@ public static void collectEnemyArchonID() {
* @return array of MapLocations in sight but not on the map
*
*/
public static MapLocation[] inSightButOffMap(){
MapLocation[] allInSight = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(),rc.getType.sensorRadiusSquared);
public static MapLocation[] inSightButOffMap() throws GameActionException{
MapLocation[] allInSight = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(),rc.getType().sensorRadiusSquared);
int numOffMap = 0;
for(int i = 0; i < allInSight.length; i++){
if(rc.onTheMap(allInSight[i])){
Expand All @@ -508,6 +509,86 @@ public static MapLocation[] inSightButOffMap(){
}
return ret;
}

/**
* boolean BUG
*
* Pathfinding method. Here's how it basically works:
* * * If we can move directly towards target, do that.
* * * Otherwise, we move along side of obstacle until we can move directly towards target
* * * * * Remember Branches (where we start/stop following walls. If we return to one, obviously following the one way didn't help, so we try the other one. If both are tried, we run the first way again for all following runs)
*
* @param target: where we are trying to get to
* @return true if we move, false if we don't
*
*/
public static boolean BUG(MapLocation target) throws GameActionException{
rc.setIndicatorString(1,target.toString());
if(!rc.isCoreReady()) return false;
MapLocation current = rc.getLocation();
Direction directionToTarget = current.directionTo(target);
if(rc.canMove(directionToTarget) && (Branch.last == null || !Branch.last.contains(current.add(directionToTarget)))){
rc.setIndicatorString(2,"Starting no branch // ");
rc.setIndicatorString(0,current.add(directionToTarget).toString() + " from no branch");
Branch.last = new ArrayList<MapLocation>();
Branch.last.add(current);
rc.move(directionToTarget);
Branch.lastStatus = 0;
if(current.add(directionToTarget).equals(target)){
Branch.resetPath();
}
return true;
}else if(Branch.lastStatus == 0){
rc.setIndicatorString(2,"Starting choosing branch");
Branch decision = Branch.fork(current,target);
rc.setIndicatorString(1,"Made it past $fork");
MapLocation bestChoice = decision.bestBranch();
rc.setIndicatorString(1,"Made it past @bestBranch");
if(bestChoice != null){
rc.setIndicatorString(0,bestChoice.toString() + " from choosing branch");
Branch.last.add(current);
rc.move(current.directionTo(bestChoice));
return true;
}
rc.setIndicatorString(0,"Failed in choosing branch");
return false;
}else if(Branch.lastStatus == 1){
rc.setIndicatorString(2,"Starting right branch");
Branch currentStep = new Branch(current,target);
Branch temp = Branch.getFromEarlier(currentStep);
if(temp != null){
currentStep = temp;
}
MapLocation nextChoice = currentStep.getRightCanditate();
if(nextChoice != null){
rc.setIndicatorString(0,nextChoice.toString() + " from right branch");
Branch.last.add(current);
rc.move(current.directionTo(nextChoice));
return true;
}
rc.setIndicatorString(0,"Failed in right branch");
return false;
}else if(Branch.lastStatus == -1){
rc.setIndicatorString(2,"Starting left branch");
Branch currentStep = new Branch(current,target);
Branch temp = Branch.getFromEarlier(currentStep);
if(temp != null){
currentStep = temp;

}
MapLocation nextChoice = currentStep.getLeftCanditate();
if(nextChoice != null){
rc.setIndicatorString(0,nextChoice.toString() + " from left branch");
Branch.last.add(current);
rc.move(current.directionTo(nextChoice));
return true;
}
rc.setIndicatorString(0,"Failed in choosing branch");
return false;
}
rc.setIndicatorString(0,"Failed outside of branch");
return false;
}
}

/**
Expand Down Expand Up @@ -677,5 +758,178 @@ public static Tuple<Integer,Integer> encrypt(int type,int first,int second){
first |= type;
return new Tuple<Integer,Integer>(first,second);
}
}
}

/**
* class Branch
*
* Represents a branch in the BUG pathfinding
*
*
*
*/
public static class Branch{
public MapLocation branchPoint;
public boolean branchedLeft = false,branchedRight = false;
public MapLocation target;
public static ArrayList<Branch> branchesInCurrentPath = new ArrayList<Branch>();
public static int lastStatus = 0;
public Direction lastDirection = null;
private MapLocation leftCanditate;
private MapLocation rightCanditate;
private MapLocation bestCanditate;
public static ArrayList<MapLocation> last = new ArrayList<MapLocation>();
public Branch(MapLocation whereWeAre,MapLocation targe){
branchPoint = whereWeAre;
target = targe;
}
public static Branch fork(MapLocation here,MapLocation targe){
Branch n = new Branch(here,targe);
Branch e = getFromEarlier(n);
if(e == null){
return n;
}
return e;
}
public static Branch getFromEarlier(Branch n){
for(int i = 0; i < branchesInCurrentPath.size(); i++){
System.out.println(n.branchPoint.toString() + "::" + branchesInCurrentPath.get(i).branchPoint.toString());
if(n.equal(branchesInCurrentPath.get(i))){
return branchesInCurrentPath.get(i);
}
}
return null;
}
public static void resetPath(){
branchesInCurrentPath = new ArrayList<Branch>();
}
public MapLocation bestBranch(){
if(branchedLeft && !branchedRight){
lastStatus = 1;
return getRightCanditate();
}
if(branchedRight && !branchedLeft){
lastStatus = -1;
return getLeftCanditate();
}
if(bestCanditate != null){
lastStatus = bestCanditate.equals(leftCanditate) ? -1 : 1 ;
return bestCanditate;
}
MapLocation left = getLeftCanditate();
MapLocation right = getRightCanditate();
if(left == null && right != null){
lastStatus = 1;
bestCanditate = right;
return right;
}else if(right == null && left != null){
lastStatus = -1;
bestCanditate = left; return left;
}else if(right == null && left == null){
return null;
}
if(left.distanceSquaredTo(target) < right.distanceSquaredTo(target)){
lastStatus = -1;
bestCanditate = left;
return left;
}
lastStatus = 1;
bestCanditate = right;
return right;
}
public MapLocation getLeftCanditate(){
if(leftCanditate != null){
return leftCanditate;
}
ArrayList<MapLocation> lefts = getAllLeftCanditates();
if(lefts.size() == 0){
return null;
}
int min = 0;
int accmin = lefts.get(0).distanceSquaredTo(target);
for(int i = 1; i < lefts.size(); i++){
int newMin = lefts.get(i).distanceSquaredTo(target);
if(accmin > newMin){
min = i;
accmin = newMin;
}
}
return lefts.get(min);
}
public MapLocation getRightCanditate(){
if(rightCanditate != null){
return rightCanditate;
}
ArrayList<MapLocation> rights = getAllRightCanditates();
if(rights.size() == 0){
return null;
}
int min = 0;
int accmin = rights.get(0).distanceSquaredTo(target);
for(int i = 1; i < rights.size(); i++){
int newMin = rights.get(i).distanceSquaredTo(target);
if(accmin > newMin){
min = i;
accmin = newMin;
}
}
return rights.get(min);
}
public ArrayList<MapLocation> getAllRightCanditates(){
ArrayList<MapLocation> base = new ArrayList<MapLocation>();
MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3);
try{
for(int i = 0; i < options.length; i++){
boolean isRight = false;
Direction forward = branchPoint.directionTo(options[i]);
Direction[] rights = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 5) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 6) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 7) % 8)};
if(rc.senseRubble(options[i]) < 100 && rc.onTheMap(options[i]) && !branchPoint.equals(options[i])){
for(int j = 0; j < rights.length && !isRight; j++){
if(rc.senseRubble(options[i].add(rights[j])) > 100 && !last.contains(options[i])){
isRight = true;
}
}
}
if(isRight){
base.add(options[i]);
}
}
}catch(Exception e){
e.printStackTrace();
}
return base;
}
public ArrayList<MapLocation> getAllLeftCanditates(){
ArrayList<MapLocation> base = new ArrayList<MapLocation>();
try{
MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3);
for(int i = 0; i < options.length; i++){
boolean isLeft = false;
Direction forward = branchPoint.directionTo(options[i]);
Direction[] lefts = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 1) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 2) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 3) % 8)};
if(rc.senseRubble(options[i]) < 100 && rc.onTheMap(options[i]) && !branchPoint.equals(options[i])){
for(int j = 0; j < lefts.length && !isLeft; j++){
if(rc.senseRubble(options[i].add(lefts[j])) > 100&& !last.contains(options[i])){
isLeft = true;
}
}
}
if(isLeft){
base.add(options[i]);
}
}
}catch(Exception e){
e.printStackTrace();
}
return base;
}
public boolean equal(Object other){
Branch o = (Branch)other;
System.out.println(this.branchPoint.toString() + "::" + o.branchPoint.toString());
if(o.branchPoint.equals(this.branchPoint)){
return true;
}
return false;
}
}
}