Commit 45572095 authored by Matthew Hausknecht's avatar Matthew Hausknecht

Refactored HFO interface.

parent b7ecb2d5
...@@ -22,7 +22,7 @@ int main() { ...@@ -22,7 +22,7 @@ int main() {
// Get the vector of state features for the current state // Get the vector of state features for the current state
const std::vector<float>& feature_vec = hfo.getState(); const std::vector<float>& feature_vec = hfo.getState();
// Perform the dash // Perform the dash
hfo.act(DASH, 20.0); hfo.act(DASH, 20.0, 0.0);
// Advance the environment and recieve current game status // Advance the environment and recieve current game status
status = hfo.step(); status = hfo.step();
} }
......
...@@ -188,15 +188,14 @@ void HFOEnvironment::connectToAgentServer(int server_port, ...@@ -188,15 +188,14 @@ void HFOEnvironment::connectToAgentServer(int server_port,
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// Get first hear message // First get the length of the hear message
// Message length
uint32_t msgLength; uint32_t msgLength;
if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0){ if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0){
perror("[Agent Client] ERROR recieving hear message length from socket"); perror("[Agent Client] ERROR recieving hear message length from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// Message // Next, receive the message
if (msgLength > 0){ if (msgLength > 0){
std::vector<char> hearMsgBuffer; std::vector<char> hearMsgBuffer;
hearMsgBuffer.resize(msgLength); hearMsgBuffer.resize(msgLength);
...@@ -204,7 +203,7 @@ void HFOEnvironment::connectToAgentServer(int server_port, ...@@ -204,7 +203,7 @@ void HFOEnvironment::connectToAgentServer(int server_port,
perror("[Agent Client] ERROR recieving hear message from socket"); perror("[Agent Client] ERROR recieving hear message from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
hear_msg.assign(&(hearMsgBuffer[0]), hearMsgBuffer.size()); hear_msg.assign(&(hearMsgBuffer[0]), hearMsgBuffer.size());
} }
} }
...@@ -283,7 +282,6 @@ void HFOEnvironment::act(action_t action, ...) { ...@@ -283,7 +282,6 @@ void HFOEnvironment::act(action_t action, ...) {
} }
void HFOEnvironment::say(const std::string& message) { void HFOEnvironment::say(const std::string& message) {
// TODO: [Sanmit] Bounds check message?
say_msg = message; say_msg = message;
} }
...@@ -291,13 +289,12 @@ std::string HFOEnvironment::hear() { ...@@ -291,13 +289,12 @@ std::string HFOEnvironment::hear() {
return hear_msg; return hear_msg;
} }
Player HFOEnvironment::playerOnBall(){ Player HFOEnvironment::playerOnBall() {
return player_on_ball; return player_on_ball;
} }
status_t HFOEnvironment::step() { status_t HFOEnvironment::step() {
status_t game_status; status_t game_status;
// Send the action_type // Send the action_type
if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) { if (send(sockfd, &requested_action, sizeof(action_t), 0) < 0) {
perror("[Agent Client] ERROR sending from socket"); perror("[Agent Client] ERROR sending from socket");
...@@ -313,26 +310,23 @@ status_t HFOEnvironment::step() { ...@@ -313,26 +310,23 @@ status_t HFOEnvironment::step() {
exit(1); exit(1);
} }
} }
// [Sanmit] Send say_msg // Send the message length
// Send message length
uint32_t sendMsgLength = say_msg.size(); uint32_t sendMsgLength = say_msg.size();
if (send(sockfd, &sendMsgLength, sizeof(uint32_t), 0) < 0){ if (send(sockfd, &sendMsgLength, sizeof(uint32_t), 0) < 0) {
perror("[Agent Client] ERROR sending from socket"); perror("[Agent Client] ERROR sending from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// Send message // Send the say message
if (sendMsgLength > 0){ if (sendMsgLength > 0) {
if (send(sockfd, say_msg.c_str(), say_msg.size(), 0) < 0){ if (send(sockfd, say_msg.c_str(), say_msg.size(), 0) < 0) {
perror("[Agent Client] ERROR sending from socket"); perror("[Agent Client] ERROR sending from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
} }
// Clear say message buffer
// Clear say message buffer
say_msg.clear(); say_msg.clear();
// Get the game status // Get the game status
int full_status[3]; int full_status[3];
if (recv(sockfd, &(full_status[0]), 3 * sizeof(int), 0) < 0) { if (recv(sockfd, &(full_status[0]), 3 * sizeof(int), 0) < 0) {
...@@ -343,35 +337,31 @@ status_t HFOEnvironment::step() { ...@@ -343,35 +337,31 @@ status_t HFOEnvironment::step() {
game_status = (status_t)full_status[0]; game_status = (status_t)full_status[0];
player_on_ball.side = (SideID)full_status[1]; player_on_ball.side = (SideID)full_status[1];
player_on_ball.unum = full_status[2]; player_on_ball.unum = full_status[2];
// Get the next game state // Get the next game state
if (recv(sockfd, &(feature_vec.front()), numFeatures * sizeof(float), 0) < 0) { if (recv(sockfd, &(feature_vec.front()), numFeatures * sizeof(float), 0) < 0) {
perror("[Agent Client] ERROR receiving state features from socket"); perror("[Agent Client] ERROR receiving state features from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// [Sanmit] Receive comm_msg
// Clear last message // Clear last message
hear_msg.clear(); hear_msg.clear();
// Message length // Receive message length
uint32_t msgLength; uint32_t msgLength;
if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0){ if (recv(sockfd, &msgLength, sizeof(uint32_t), 0) < 0) {
perror("[Agent Client] ERROR receiving hear message length from socket"); perror("[Agent Client] ERROR receiving hear message length from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// Message // Receive the message
if (msgLength > 0){ if (msgLength > 0) {
std::vector<char> hearMsgBuffer; std::vector<char> hearMsgBuffer;
hearMsgBuffer.resize(msgLength); hearMsgBuffer.resize(msgLength);
if (recv(sockfd, &hearMsgBuffer[0], msgLength, 0) < 0){ if (recv(sockfd, &hearMsgBuffer[0], msgLength, 0) < 0) {
perror("[Agent Client] ERROR receiving hear message from socket"); perror("[Agent Client] ERROR receiving hear message from socket");
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
hear_msg.assign(&(hearMsgBuffer[0]), hearMsgBuffer.size()); hear_msg.assign(&(hearMsgBuffer[0]), hearMsgBuffer.size());
} }
return game_status; return game_status;
} }
...@@ -70,7 +70,7 @@ struct Config { ...@@ -70,7 +70,7 @@ struct Config {
}; };
struct Player { struct Player {
SideID side; SideID side;
int unum; int unum;
}; };
...@@ -103,7 +103,7 @@ class HFOEnvironment { ...@@ -103,7 +103,7 @@ class HFOEnvironment {
virtual void say(const std::string& message); virtual void say(const std::string& message);
virtual std::string hear(); virtual std::string hear();
// Get the current player holding the ball // Get the current player holding the ball
virtual Player playerOnBall(); virtual Player playerOnBall();
// Indicates the agent is done and the environment should // Indicates the agent is done and the environment should
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment