-
Notifications
You must be signed in to change notification settings - Fork 5
/
Communications.java
96 lines (85 loc) · 3.33 KB
/
Communications.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
package lectureplayer;
import battlecode.common.*;
import java.util.ArrayList;
public class Communications {
RobotController rc;
// state related only to communications should go here
// all messages from our team should start with this so we can tell them apart
static final int teamSecret = 444444444;
// the second entry in every message tells us what kind of message it is. e.g. 0 means it contains the HQ location
static final String[] messageType = {
"HQ loc",
"design school created",
"soup location",
};
public Communications(RobotController r) {
rc = r;
}
public void sendHqLoc(MapLocation loc) throws GameActionException {
int[] message = new int[7];
message[0] = teamSecret;
message[1] = 0;
message[2] = loc.x; // x coord of HQ
message[3] = loc.y; // y coord of HQ
if (rc.canSubmitTransaction(message, 3))
rc.submitTransaction(message, 3);
}
public MapLocation getHqLocFromBlockchain() throws GameActionException {
for (int i = 1; i < rc.getRoundNum(); i++){
for(Transaction tx : rc.getBlock(i)) {
int[] mess = tx.getMessage();
if(mess[0] == teamSecret && mess[1] == 0){
System.out.println("found the HQ!");
return new MapLocation(mess[2], mess[3]);
}
}
}
return null;
}
public boolean broadcastedCreation = false;
public void broadcastDesignSchoolCreation(MapLocation loc) throws GameActionException {
if(broadcastedCreation) return; // don't re-broadcast
int[] message = new int[7];
message[0] = teamSecret;
message[1] = 1;
message[2] = loc.x; // x coord of HQ
message[3] = loc.y; // y coord of HQ
if (rc.canSubmitTransaction(message, 3)) {
rc.submitTransaction(message, 3);
broadcastedCreation = true;
}
}
// check the latest block for unit creation messages
public int getNewDesignSchoolCount() throws GameActionException {
int count = 0;
for(Transaction tx : rc.getBlock(rc.getRoundNum() - 1)) {
int[] mess = tx.getMessage();
if(mess[0] == teamSecret && mess[1] == 1){
System.out.println("heard about a cool new school");
count += 1;
}
}
return count;
}
public void broadcastSoupLocation(MapLocation loc ) throws GameActionException {
int[] message = new int[7];
message[0] = teamSecret;
message[1] = 2;
message[2] = loc.x; // x coord of HQ
message[3] = loc.y; // y coord of HQ
if (rc.canSubmitTransaction(message, 3)) {
rc.submitTransaction(message, 3);
System.out.println("new soup!" + loc);
}
}
public void updateSoupLocations(ArrayList<MapLocation> soupLocations) throws GameActionException {
for(Transaction tx : rc.getBlock(rc.getRoundNum() - 1)) {
int[] mess = tx.getMessage();
if(mess[0] == teamSecret && mess[1] == 2){
// TODO: don't add duplicate locations
System.out.println("heard about a tasty new soup location");
soupLocations.add(new MapLocation(mess[2], mess[3]));
}
}
}
}