mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-03 18:33:39 +02:00
Support for multiple connected agents
This commit is contained in:
parent
50fb0bd004
commit
f33f7c1d61
1 changed files with 23 additions and 14 deletions
31
main.cc
31
main.cc
|
@ -1,7 +1,9 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstdio>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iostream>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
@ -39,26 +41,33 @@ main(int argc, char *argv[])
|
||||||
int flags = fcntl(lfd, F_GETFL, 0);
|
int flags = fcntl(lfd, F_GETFL, 0);
|
||||||
fcntl(lfd, F_SETFL, flags | O_NONBLOCK);
|
fcntl(lfd, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
class agent *agent = NULL;
|
std::list<class agent *> agents;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
std::cout << "tick " << tick_id << '\n';
|
std::cout << "tick " << tick_id << '\n';
|
||||||
|
|
||||||
map.on_tick();
|
map.on_tick();
|
||||||
|
|
||||||
if (agent) {
|
for (std::list<class agent *>::iterator agent = agents.begin(); agent != agents.end(); agent++)
|
||||||
agent->on_tick();
|
{
|
||||||
if (agent->conn && agent->conn->error) {
|
next_agent:
|
||||||
delete agent;
|
(*agent)->on_tick();
|
||||||
agent = NULL;
|
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);
|
int cfd = accept(lfd, NULL, NULL);
|
||||||
if (cfd >= 0) {
|
if (cfd >= 0) {
|
||||||
class connection *conn = new class connection(cfd);
|
class connection *conn = new class connection(cfd);
|
||||||
class tile &agentpos = map.tile_at(random() % map.w, random() % map.h);
|
class tile *agentpos;
|
||||||
agent = new class agent(0, agentpos, conn);
|
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();
|
map.print_map();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue