From f33f7c1d61a1d2eaf5d130cb94ba55f35e78c7c4 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 00:22:29 +0100 Subject: [PATCH] Support for multiple connected agents --- main.cc | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/main.cc b/main.cc index b0c8595..b20a6c0 100644 --- a/main.cc +++ b/main.cc @@ -1,7 +1,9 @@ #include +#include #include -#include #include +#include +#include #include #include @@ -39,26 +41,33 @@ main(int argc, char *argv[]) int flags = fcntl(lfd, F_GETFL, 0); fcntl(lfd, F_SETFL, flags | O_NONBLOCK); - class agent *agent = NULL; + std::list agents; + while (true) { std::cout << "tick " << tick_id << '\n'; map.on_tick(); - if (agent) { - agent->on_tick(); - if (agent->conn && agent->conn->error) { - delete agent; - agent = NULL; + for (std::list::iterator agent = agents.begin(); agent != agents.end(); agent++) + { +next_agent: + (*agent)->on_tick(); + if ((*agent)->conn && (*agent)->conn->error) { + delete *agent; + agent = agents.erase(agent); + if (agent != agents.end()) + goto next_agent; } + } - } else { - int cfd = accept(lfd, NULL, NULL); - if (cfd >= 0) { - class connection *conn = new class connection(cfd); - class tile &agentpos = map.tile_at(random() % map.w, random() % map.h); - agent = new class agent(0, agentpos, conn); - } + int cfd = accept(lfd, NULL, NULL); + if (cfd >= 0) { + class connection *conn = new class connection(cfd); + class tile *agentpos; + do { + agentpos = &map.tile_at(random() % map.w, random() % map.h); + } while (agentpos->agent); + agents.push_back(new class agent(0, *agentpos, conn)); } map.print_map();