-
Notifications
You must be signed in to change notification settings - Fork 0
/
Miner.java
117 lines (104 loc) · 3.54 KB
/
Miner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package team079;
import team079.util.ComSystem;
import battlecode.common.*;
public class Miner extends BaseRobot {
public RobotController rc;
public Direction lastMove;
public Direction defaultMove;
public MapLocation defaultLocation;
public final int MININGAVGCHANNEL = 50;
public int roundLastSkipped = 0;
public Miner(RobotController rcin){
super(rcin);
rc = rcin;
}
@Override
public void run() throws GameActionException {
shootWeakest();
mineAndMove();
supplyChain();
ComSystem.logMiningIfBetter(getOreNear(), rc.getLocation());
//updateMiningInfo();
rc.yield();
}
private void updateMiningInfo() throws GameActionException {
if(lastMove != null)
ComSystem.addToAverage(MININGAVGCHANNEL, getIndexOfDirection(lastMove));
defaultMove = Direction.values()[ComSystem.getAverage(MININGAVGCHANNEL)];
}
//Handles basic mining action
public void mineAndMove() throws GameActionException{
//If there is ore, and we aren't blocking miners, mine it!
boolean skipSquare = false;
if(ComSystem.getUselessMiners()>5){
if(Clock.getRoundNum()-roundLastSkipped > 50){
skipSquare = true;
roundLastSkipped = Clock.getRoundNum();
}
}
if(rc.senseOre(rc.getLocation()) > 1 && !minerBehind()){
if(rc.isCoreReady()&&rc.canMine())
rc.mine();
}else{
//also report that we don't have ore where we are at
ComSystem.reportUselessMiner();
//If there isn't ore, find it!
MapLocation[] toTry = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(), 2);
double bestOre = -1;
Direction selected = null;
//Find the best square near us
for(MapLocation loc:toTry){
if(rc.senseOre(loc)> bestOre){
bestOre = rc.senseOre(loc);
selected = rc.getLocation().directionTo(loc);
}
}
//If all ore < 0.4, follow the crowd
if(bestOre <0.4){
if(rand.nextDouble()<0.0){
selected = rc.getLocation().directionTo(ComSystem.getMiningLoc());
//selected = rc.getLocation().directionTo(rc.senseHQLocation()).opposite();
//selected = rc.getLocation().directionTo(ourHQ.add(ourHQ.directionTo(theirHQ),20));
}else{
if(rc.getID()%2 == 0)
selected = ourHQ.directionTo(theirHQ).rotateLeft().rotateLeft();//rc.getLocation().directionTo(ourHQ.add(ourHQ.directionTo(theirHQ).rotateLeft().rotateLeft(),20));
else
selected = ourHQ.directionTo(theirHQ).rotateRight().rotateRight();//rc.getLocation().directionTo(ourHQ.add(ourHQ.directionTo(theirHQ).rotateRight().rotateRight(),20));
}
}
//Move to the selected square
if(rc.isCoreReady()){
if(basicPathing(selected)){
ComSystem.logMined(rc.getLocation().add(selected));
}
lastMove = selected;
}
}
}
private void supplyChain() throws GameActionException{
RobotInfo[] Robots = rc.senseNearbyRobots(15, rc.getTeam());
for(RobotInfo ri: Robots){
if(ri.supplyLevel<rc.getSupplyLevel()*0.75){
int toSupply = 0;
if(ri.type == RobotType.MINER){
toSupply = (int) ((rc.getSupplyLevel()-ri.supplyLevel)/2);
}
if(rc.senseRobotAtLocation(ri.location) != null && toSupply !=0){
rc.transferSupplies(toSupply, ri.location);
break;
}
}
}
}
private boolean minerBehind() throws GameActionException{
RobotInfo[] miners = robotsOnTeam(RobotType.MINER, rc.getTeam());
for(RobotInfo miner:miners){
if(miner.location.isAdjacentTo(rc.getLocation())){
if(rc.senseOre(miner.location) < 1){
return true;
}
}
}
return false;
}
}