diff --git a/main.cc b/main.cc index 14cf7fe..51c914e 100644 --- a/main.cc +++ b/main.cc @@ -66,11 +66,7 @@ next_agent: 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(agents.size(), *agentpos, conn)); + agents.push_back(new class agent(agents.size(), map.agent_startpos(), conn)); } /* Run on_tick everywhere. */ diff --git a/map.cc b/map.cc index cb1a3ef..631551b 100644 --- a/map.cc +++ b/map.cc @@ -1,5 +1,6 @@ -#include #include +#include +#include #include "agent.h" #include "map.h" @@ -47,6 +48,17 @@ tile::tile_in_dir(int dir_x, int dir_y) } +class tile & +map::agent_startpos(void) +{ + /* Find a random starting tile that is not occupied yet. */ + class tile *tile; + do { + tile = &tile_at(random() % w, random() % h); + } while (tile->agent); + return *tile; +} + void map::on_tick(void) { diff --git a/map.h b/map.h index 821cefc..286e65b 100644 --- a/map.h +++ b/map.h @@ -55,6 +55,8 @@ public: return *tiles[y * w + x]; }; + class tile &agent_startpos(void); + void on_tick(void); void print_map(void);