-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathTeamRobot.java
More file actions
204 lines (192 loc) · 5.75 KB
/
Copy pathTeamRobot.java
File metadata and controls
204 lines (192 loc) · 5.75 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2001-2025 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://robocode.sourceforge.io/license/epl-v10.html
*/
package robocode;
import robocode.robotinterfaces.ITeamEvents;
import robocode.robotinterfaces.ITeamRobot;
import robocode.robotinterfaces.peer.ITeamRobotPeer;
import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;
/**
* A TeamRobot is a robot that is made for battles between teams of robots.
* A robot team consists of one to several robots that are all a TeamRobot.
* A TeamRobot is an extension to the {@link AdvancedRobot} and provides
* support for sending messages between team members.
* <p>
* Besides the robots, a robot team is put together by defining a .team file
* that is a Java property file. The filename must be the name of the team
* like e.g. {@code MyFirstTeam.team}. And the .team file and team robots must
* exist in the same folder (package name). But you can let the Robocode UI
* help you create the team from the menu: Robot -> 'Create a robot team'.
* <p>
* The .team file contains a comma-separated list of the full name of the team
* members:
* <pre>
* team.members=sampleteam.MyFirstLeader,sampleteam.MyFirstDroid
* </pre>
* With this example, two different robots are members (MyFirstLeader and
* MyFirstDroid). But you can include any TeamRobot you want.
* <p>
* If you have not done already, you should start by creating a {@link Robot} or
* {@link AdvancedRobot} before you make your first TeamRobot.
*
* @see Droid
* @see AdvancedRobot
* @see Robot
* @see JuniorRobot
* @see RateControlRobot
* @see BorderSentry
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
* @author Pavel Savara (contributor)
*/
public class TeamRobot extends AdvancedRobot implements ITeamRobot, ITeamEvents {
/**
* Broadcasts a message to all teammates.
* <p>
* Example:
* <pre>
* public void run() {
* broadcastMessage("I'm here!");
* }
* </pre>
*
* @param message the message to broadcast to all teammates
* @throws IOException if the message could not be broadcasted to the
* teammates
* @see #isTeammate(String)
* @see #getTeammates()
* @see #sendMessage(String, Serializable)
*/
public void broadcastMessage(Serializable message) throws IOException {
if (peer != null) {
((ITeamRobotPeer) peer).broadcastMessage(message);
} else {
uninitializedException();
}
}
/**
* Returns a vector containing all MessageEvents currently in the robot's
* queue. You might, for example, call this while processing another event.
* <p>
* Example:
* <pre>
* for (MessageEvent e : getMessageEvents()) {
* // do something with e
* }
* </pre>
*
* @return a vector containing all MessageEvents currently in the robot's
* queue
* @see #onMessageReceived(MessageEvent)
* @see MessageEvent
* @since 1.2.6
*/
public Vector<MessageEvent> getMessageEvents() {
if (peer != null) {
return new Vector<MessageEvent>(((ITeamRobotPeer) peer).getMessageEvents());
}
uninitializedException();
return null; // never called
}
/**
* Do not call this method!
* <p>
* {@inheritDoc}
*/
public final ITeamEvents getTeamEventListener() {
return this; // this robot is listening
}
/**
* Returns the names of all teammates, or {@code null} there is no
* teammates.
* <p>
* Example:
* <pre>
* public void run() {
* // Prints out all teammates
* String[] teammates = getTeammates();
* if (teammates != null) {
* for (String member : teammates) {
* out.println(member);
* }
* }
* }
* </pre>
*
* @return a String array containing the names of all your teammates, or
* {@code null} if there is no teammates. The length of the String array
* is equal to the number of teammates.
* @see #isTeammate(String)
* @see #broadcastMessage(Serializable)
* @see #sendMessage(String, Serializable)
*/
public String[] getTeammates() {
if (peer != null) {
return ((ITeamRobotPeer) peer).getTeammates();
}
uninitializedException();
return null;
}
/**
* Checks if a given robot name is the name of one of your teammates.
* <p>
* Example:
* <pre>
* public void onScannedRobot(ScannedRobotEvent e) {
* if (isTeammate(e.getName()) {
* return;
* }
* fire(1);
* }
* </pre>
*
* @param name the robot name to check
* @return {@code true} if the specified name belongs to one of your
* teammates; {@code false} otherwise.
* @see #getTeammates()
* @see #broadcastMessage(Serializable)
* @see #sendMessage(String, Serializable)
*/
public boolean isTeammate(String name) {
if (peer != null) {
return ((ITeamRobotPeer) peer).isTeammate(name);
}
uninitializedException();
return false;
}
/**
* {@inheritDoc}
*/
public void onMessageReceived(MessageEvent event) {}
/**
* Sends a message to one (or more) teammates.
* <p>
* Example:
* <pre>
* public void run() {
* sendMessage("sample.DroidBot", "I'm here!");
* }
* </pre>
*
* @param name the name of the intended recipient of the message
* @param message the message to send
* @throws IOException if the message could not be sent
* @see #isTeammate(String)
* @see #getTeammates()
* @see #broadcastMessage(Serializable)
*/
public void sendMessage(String name, Serializable message) throws IOException {
if (peer != null) {
((ITeamRobotPeer) peer).sendMessage(name, message);
} else {
uninitializedException();
}
}
}