Connection: Add negotiation phase

This commit is contained in:
Petr Baudis 2011-11-27 05:06:46 +01:00
parent 85e143fd11
commit 9cf99e0bcd
3 changed files with 12 additions and 3 deletions

View file

@ -16,6 +16,8 @@
void void
connection::senses(int tick_id, class agent &a) connection::senses(int tick_id, class agent &a)
{ {
assert(!negotiation);
char buf[1024]; char buf[1024];
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"tick %d\r\n" "tick %d\r\n"
@ -74,19 +76,26 @@ connection::actions(class agent &agent)
if (y < -1) y = -1; if (y > 1) y = 1; if (y < -1) y = -1; if (y > 1) y = 1;
if (!agent.move_dir(x, y)) if (!agent.move_dir(x, y))
bump(); bump();
} else if (!cmd.compare("attack_dir")) {
} else if (!negotiation && !cmd.compare("attack_dir")) {
int x = 0, y = 0; int x = 0, y = 0;
sscanf(line.c_str(), "%d %d", &x, &y); sscanf(line.c_str(), "%d %d", &x, &y);
if (x < -1) x = -1; if (x > 1) x = 1; if (x < -1) x = -1; if (x > 1) x = 1;
if (y < -1) y = -1; if (y > 1) y = 1; if (y < -1) y = -1; if (y > 1) y = 1;
if (!agent.attack_dir(x, y)) if (!agent.attack_dir(x, y))
bump(); bump();
} else { } else {
std::cout << "unknown line " << cmd << " " << line << " ...\n"; std::cout << "unknown line " << cmd << " " << line << " ...\n";
} }
} }
in_buf.erase(0, 2); in_buf.erase(0, 2);
if (negotiation) {
negotiation = false;
agent.spawn();
}
pthread_mutex_unlock(&buf_lock); pthread_mutex_unlock(&buf_lock);
} }

View file

@ -13,10 +13,11 @@ class agent;
class connection { class connection {
public: public:
int fd; int fd;
bool negotiation;
bool error; bool error;
connection(int fd_) connection(int fd_)
: fd(fd_), error(false) : fd(fd_), negotiation(true), error(false)
{ {
spawn_thread(); spawn_thread();
} }

View file

@ -69,7 +69,6 @@ next_agent:
class connection *conn = new class connection(cfd); class connection *conn = new class connection(cfd);
class agent *a = new class agent(aid++, conn, map); class agent *a = new class agent(aid++, conn, map);
agents.push_back(a); agents.push_back(a);
a->spawn();
} }
/* Collect and take actions. */ /* Collect and take actions. */