From 9cf99e0bcde2701f08c4f5ce92db916a5c375917 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 05:06:46 +0100 Subject: [PATCH] Connection: Add negotiation phase --- connection.cc | 11 ++++++++++- connection.h | 3 ++- main.cc | 1 - 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/connection.cc b/connection.cc index ff57fd2..cd3da50 100644 --- a/connection.cc +++ b/connection.cc @@ -16,6 +16,8 @@ void connection::senses(int tick_id, class agent &a) { + assert(!negotiation); + char buf[1024]; snprintf(buf, sizeof(buf), "tick %d\r\n" @@ -74,19 +76,26 @@ connection::actions(class agent &agent) if (y < -1) y = -1; if (y > 1) y = 1; if (!agent.move_dir(x, y)) bump(); - } else if (!cmd.compare("attack_dir")) { + + } else if (!negotiation && !cmd.compare("attack_dir")) { int x = 0, y = 0; sscanf(line.c_str(), "%d %d", &x, &y); if (x < -1) x = -1; if (x > 1) x = 1; if (y < -1) y = -1; if (y > 1) y = 1; if (!agent.attack_dir(x, y)) bump(); + } else { std::cout << "unknown line " << cmd << " " << line << " ...\n"; } } in_buf.erase(0, 2); + if (negotiation) { + negotiation = false; + agent.spawn(); + } + pthread_mutex_unlock(&buf_lock); } diff --git a/connection.h b/connection.h index 753f360..2102cda 100644 --- a/connection.h +++ b/connection.h @@ -13,10 +13,11 @@ class agent; class connection { public: int fd; + bool negotiation; bool error; connection(int fd_) - : fd(fd_), error(false) + : fd(fd_), negotiation(true), error(false) { spawn_thread(); } diff --git a/main.cc b/main.cc index 2e4a59c..3e8594e 100644 --- a/main.cc +++ b/main.cc @@ -69,7 +69,6 @@ next_agent: class connection *conn = new class connection(cfd); class agent *a = new class agent(aid++, conn, map); agents.push_back(a); - a->spawn(); } /* Collect and take actions. */