diff --git a/Makefile b/Makefile index 200ed5a..f3174b7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS=-Wall -O3 -g -OBJS=main.o map.o agent.o +OBJS=main.o map.o agent.o connection.o brmlife: $(OBJS) $(CXX) -o $@ $^ diff --git a/agent.h b/agent.h index af68f63..622363b 100644 --- a/agent.h +++ b/agent.h @@ -3,12 +3,15 @@ #include "map.h" +class connection; + class agent { public: int id; class tile *tile; + class connection &conn; - agent(int id_, class tile &tile_) : id (id_), tile (&tile_) + agent(int id_, class tile &tile_, class connection &conn_) : id (id_), tile (&tile_), conn (conn_) { put_at(tile_); }; diff --git a/connection.cc b/connection.cc new file mode 100644 index 0000000..3a6f0a4 --- /dev/null +++ b/connection.cc @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +#include "agent.h" +#include "connection.h" + +void +connection::senses(char around[4]) +{ + char buf[1024]; + snprintf(buf, sizeof(buf), "around %c%c%c%c\n\n", around[0], around[1], around[2], around[3]); + write(fd, buf, strlen(buf)); +} diff --git a/connection.h b/connection.h new file mode 100644 index 0000000..7d8f9e9 --- /dev/null +++ b/connection.h @@ -0,0 +1,17 @@ +#ifndef BRMLIFE__CONNECTION_H +#define BRMLIFE__CONNECTION_H + +#include + +#include "map.h" + +class connection { +public: + int fd; + + connection(int fd_) : fd(fd_) {} + + void senses(char around[4]); +}; + +#endif diff --git a/main.cc b/main.cc index b654c35..0b1e456 100644 --- a/main.cc +++ b/main.cc @@ -1,8 +1,15 @@ #include #include #include +#include + +#include +#include +#include +#include #include "agent.h" +#include "connection.h" #include "map.h" int @@ -12,19 +19,40 @@ main(int argc, char *argv[]) srandom(time(NULL)); - class tile &agentpos = map.tile_at(random() % map.w, random() % map.h); - class agent agent(0, agentpos); + int lfd = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in sin; + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = htons(27753); + sin.sin_addr.s_addr = INADDR_ANY; + int optval = 1; + setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); + bind(lfd, (struct sockaddr *) &sin, sizeof(sin)); + listen(lfd, 10); - map.print_map(); - std::cout << '\n'; + int cfd; + while ((cfd = accept(lfd, NULL, NULL)) >= 0) { + class connection conn(cfd); + class tile &agentpos = map.tile_at(random() % map.w, random() % map.h); + class agent agent(0, agentpos, conn); - agent.move_dir(1, 0); - map.print_map(); - std::cout << '\n'; + while (true) { + map.print_map(); + std::cout << '\n'; - agent.move_dir(0, -1); - map.print_map(); - std::cout << '\n'; + char around[4] = { + agent.tile->tile_in_dir(0, -1).symbol(), + agent.tile->tile_in_dir(1, 0).symbol(), + agent.tile->tile_in_dir(0, 1).symbol(), + agent.tile->tile_in_dir(-1, 0).symbol(), + }; + conn.senses(around); + + usleep(1000000); + } + + /* TODO: destroy agent cleanly */ + } return 0; }