-
Notifications
You must be signed in to change notification settings - Fork 0
/
Broadcasts.java
190 lines (172 loc) · 6.16 KB
/
Broadcasts.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package team133;
import battlecode.common.*;
/**
* A broadcast message is an int[] of packets with length modulo the packet size.
* {
* int[0] := team identifier
* ... := Packet[]
* }
* Each packet is an int[PACKET_SIZE] encoded as:
* {
* int[0] := metadata
* int[1] := metadata MAC
* int[2] := map location x
* int[3] := MAC for x
* int[4] := map location y
* int[5] := MAC for y
* }
*
* Messages are sent in the clear with a MAC for all data.
*
* The MAC is computed by padding the payload bits with a one time pad. The message
* is authenticated by xor-ing the mac and payload to achieve the pad.
*
* Every round has a unique one time pad defined in team133.Codex.code_by_round.
*
* By battlecode rules, a robot may receive messages the same round they are sent, or the next
* round, depending on their robot id. To synchronize MACs for all robots:
* - send on odd rounds with MAC[next even round]
* - read on even rounds with MAC[current round]
*/
public class Broadcasts {
// opcodes
public static final int ATTACK_TO_LOC = 0x0000;
public static final int DEFEND_AT_LOC = 0xFFFF;
public static final int BOUND_UP = 0xF000;
public static final int BOUND_DOWN = 0x0F00;
public static final int BOUND_LEFT = 0x00F0;
public static final int BOUND_RIGHT = 0x000F;
public static class Packet {
public final int opcode;
public final MapLocation loc;
public final MapLocation origin;
public final MapLocation rebroadcast_origin;
public Packet(int opcode, MapLocation loc, MapLocation origin, MapLocation rebroadcast_origin) {
this.opcode = opcode;
this.loc = loc;
this.origin = origin;
if (rebroadcast_origin == null) {
this.rebroadcast_origin = this.origin;
} else {
this.rebroadcast_origin = rebroadcast_origin;
}
}
}
private final int team_bits;
public static final int PACKET_SIZE = 8;
public static final int MAX_OUT = 5;
public static final int MAX_IN = 5;
private final RobotController rc;
private final Bot bot;
public Broadcasts(Bot bot) {
this.rc = bot.rc;
this.bot = bot;
this.team_bits = (bot.team == Team.A) ? 0xFFFFFFFF : 0x00000000;
}
public int packets_out_count = 0;
public Packet[] packets_out = new Packet[MAX_OUT];
public void addMessage(Packet p) {
if (packets_out_count >= MAX_OUT) return;
for (int i = 0; i < packets_out_count; i++) {
if (packets_out[i].opcode == p.opcode && packets_out[i].loc == p.loc) return;
}
packets_out[packets_out_count++] = p;
}
/** TODO: clear duplicates (particularly with respect to rebroadcasts)? */
public void send() {
if (Clock.getRoundNum() % 2 == 0 || packets_out_count == 0) return;
if (rc.hasBroadcasted()) return;
if (bot.flux < 1) return;
Message msg = new Message();
msg.ints = new int[1 + packets_out_count * PACKET_SIZE];
msg.ints[0] = team_bits;
for (int i = 0; i < packets_out_count; i++) {
Packet p = packets_out[i];
int ii = 1 + i * PACKET_SIZE;
msg.ints[ii + 1] = p.opcode;
msg.ints[ii + 2] = p.loc.x;
msg.ints[ii + 3] = p.loc.y;
msg.ints[ii + 4] = p.origin.x;
msg.ints[ii + 5] = p.origin.y;
msg.ints[ii + 6] = p.rebroadcast_origin.x;
msg.ints[ii + 7] = p.rebroadcast_origin.y;
msg.ints[ii + 0] = 0;
for (int j = 1; j < PACKET_SIZE; j++) {
msg.ints[ii + 0] += msg.ints[ii + j];
}
msg.ints[ii + 0] = encode(msg.ints[ii + 0]);
}
try {
rc.broadcast(msg);
packets_out_count = 0;
} catch (GameActionException e) {
e.printStackTrace();
}
}
private void debug_badMessage(int a, int b) {
System.out.format("Round %d ||| payload: %x, mac: %x : understood: %x, expected: %x\n",
Clock.getRoundNum(),
a,
b,
(a ^ b),
Codex.code_by_round[Clock.getRoundNum() % Codex.code_by_round.length]);
System.out.format("? prev: %x cur: %x next: %x\n",
Codex.code_by_round[Clock.getRoundNum() - 1 % Codex.code_by_round.length],
Codex.code_by_round[Clock.getRoundNum() % Codex.code_by_round.length],
Codex.code_by_round[Clock.getRoundNum() + 1 % Codex.code_by_round.length]);
}
public int packets_in_count = 0;
public Packet[] packets_in = new Packet[MAX_IN];
public void recv() {
if (Clock.getRoundNum() % 2 == 1) {
return;
} else {
packets_in_count = 0;
}
outer: for (Message msg : rc.getAllMessages()) {
if (msg == null) continue outer;
if (msg.ints == null) continue outer;
if (msg.ints.length < 1) continue outer;
if (msg.ints[0] != team_bits) continue outer;
if ((msg.ints.length - 1) % PACKET_SIZE != 0) continue outer;
inner: for (int i = 1; i < msg.ints.length; i += PACKET_SIZE) {
if (packets_in_count >= MAX_IN) return;
int opcode = msg.ints[i + 1];
int x = msg.ints[i + 2];
int y = msg.ints[i + 3];
int ox = msg.ints[i + 4];
int oy = msg.ints[i + 5];
int rox = msg.ints[i + 6];
int roy = msg.ints[i + 7];
int code = msg.ints[i + 0];
int check = 0;
for (int j= 1; j < PACKET_SIZE; j++) {
check += msg.ints[i + j];
}
if (check != decode(code)) continue outer;
MapLocation loc = new MapLocation(x, y);
MapLocation origin = new MapLocation(ox, oy);
MapLocation rebroadcast_origin = new MapLocation(rox, roy);
packets_in[packets_in_count++] = new Packet(opcode, loc, origin, rebroadcast_origin);
// Rebroadcast
if (bot.type != RobotType.SOLDIER) {
if (origin.equals(rebroadcast_origin) || bot.distanceSquaredTo(origin) > origin.distanceSquaredTo(rebroadcast_origin)) {
addMessage(new Packet(opcode, loc, origin, bot.loc));
}
}
}
}
}
/**
* Compute mac by padding message with the next round's pad.
*/
private int encode(int msg) {
return msg ^ Codex.code_by_round[(Clock.getRoundNum() + 1) % Codex.code_by_round.length];
}
/**
* Decode message send last round with this round's pad.
*/
private int decode(int code) {
return code ^ Codex.code_by_round[Clock.getRoundNum() % Codex.code_by_round.length];
}
}