-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBWClient.java
More file actions
133 lines (117 loc) · 4.16 KB
/
BWClient.java
File metadata and controls
133 lines (117 loc) · 4.16 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
package bwapi;
import java.util.Objects;
/**
* Client class to connect to the game with.
*/
public class BWClient {
private BWClientConfiguration configuration;
private final BWEventListener eventListener;
private BotWrapper botWrapper;
private Client client;
private PerformanceMetrics performanceMetrics;
public BWClient(final BWEventListener eventListener) {
Objects.requireNonNull(eventListener);
this.eventListener = eventListener;
}
/**
* Get the {@link Game} instance of the currently running game.
* When running in asynchronous mode, this is the game from the bot's perspective, e.g. potentially a previous frame.
*/
public Game getGame() {
return botWrapper == null ? null : botWrapper.getGame();
}
/**
* @return JBWAPI performance metrics.
*/
public PerformanceMetrics getPerformanceMetrics() {
return performanceMetrics;
}
/**
* @return The current configuration
*/
public BWClientConfiguration getConfiguration() {
return configuration;
}
/**
* @return Whether the current frame should be subject to timing.
*/
boolean doTime() {
return ! configuration.getUnlimitedFrameZero() || (client.isConnected() && client.liveClientData().gameData().getFrameCount() > 0);
}
/**
* @return The number of frames between the one exposed to the bot and the most recent received by JBWAPI.
* This tracks the size of the frame buffer except when the game is paused (which results in multiple frames arriving with the same count).
*/
public int framesBehind() {
return botWrapper == null ? 0 : Math.max(0, client.liveClientData().gameData().getFrameCount() - getGame().getFrameCount());
}
/**
* For internal test use.
*/
Client getClient() {
return client;
}
/**
* Start the game with default settings.
*/
public void startGame() {
startGame(BWClientConfiguration.DEFAULT);
}
/**
* Start the game.
*
* @param autoContinue automatically continue playing the next game(s). false by default
*/
@Deprecated
public void startGame(boolean autoContinue) {
startGame(new BWClientConfiguration.Builder()
.withAutoContinue(autoContinue)
.build());
}
/**
* Start the game.
*
* @param gameConfiguration Settings for playing games with this client.
*/
public void startGame(BWClientConfiguration gameConfiguration) {
this.configuration = gameConfiguration;
this.performanceMetrics = new PerformanceMetrics(configuration);
botWrapper = configuration.getAsync()
? new BotWrapperAsync(configuration, eventListener)
: new BotWrapper(configuration, eventListener);
if (client == null) {
client = new Client(this);
}
client.reconnect();
do {
ClientData.GameData liveGameData = client.liveClientData().gameData();
while (!liveGameData.isInGame()) {
if (!client.isConnected()) {
return;
}
client.sendFrameReceiveFrame();
if (liveGameData.isInGame()) {
performanceMetrics = new PerformanceMetrics(configuration);
botWrapper.startNewGame(client.mapFile(), performanceMetrics);
}
}
while (liveGameData.isInGame()) {
botWrapper.onFrame();
performanceMetrics.getFlushSideEffects().time(() -> getGame().sideEffects.flushTo(liveGameData));
performanceMetrics.getFrameDurationReceiveToSend().stopTiming();
client.sendFrameReceiveFrame();
if (!client.isConnected()) {
System.out.println("Reconnecting...");
client.reconnect();
}
}
botWrapper.endGame();
} while (configuration.getAutoContinue());
}
/**
* Provides a Client. Intended for test consumers only.
*/
void setClient(Client client) {
this.client = client;
}
}