#include #include #include #include #include #include #include #include #include #include #include #include #include "agent.h" #include "connection.h" #include "main.h" #include "map.h" #include "rawio.h" int tick_id = 0; int agent_id = 0; std::list agents; static void drop_agents(void) { for (std::list::iterator agent = agents.begin(); agent != agents.end(); agent++) { next_agent: if (!(*agent)->conn && !(*agent)->tile) { delete *agent; agent = agents.erase(agent); if (agent != agents.end()) goto next_agent; } } } static void clear(void) { printf("\033[H\033[J"); fflush(stdout); } int main(int argc, char *argv[]) { class map map(argc < 2 ? 40 : atoi(argv[1]), argc < 3 ? 20 : atoi(argv[2])); int herbs = argc < 4 ? map.w * map.h / world::herb_rate : atoi(argv[3]); #ifdef RAWIO if(rawio_cfg(&map)==-1) std::cout<<"rawio_cfg: Cannost open cfg file"<spawn(); } /* Main tick loop. */ while (true) { clear(); std::cout << "tick " << tick_id << '\n'; /* Drop disconnected agents. */ drop_agents(); /* Accept new agents. */ int cfd = accept(lfd, NULL, NULL); if (cfd >= 0) { class connection *conn = new class connection(cfd); class agent *a = new class agent(agent_id++, conn, map); agents.push_back(a); } /* Collect and take actions. */ for (std::list::iterator agent = agents.begin(); agent != agents.end(); agent++) (*agent)->on_action_takes(); drop_agents(); /* Some agents might have died. */ /* Run on_tick everywhere. */ map.on_tick(); for (std::list::iterator agent = agents.begin(); agent != agents.end(); agent++) (*agent)->on_tick(); drop_agents(); /* Some agents might have died. */ /* Update agents' senses. */ for (std::list::iterator agent = agents.begin(); agent != agents.end(); agent++) (*agent)->on_senses_update(); /* Finish a tick. */ map.print_map(); #ifdef RAWIO if(rawio_map(&map)==-1) std::cout<<"Rawio_map: Cannot open map file"<