mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-03 10:23:38 +02:00
Support for command input
For starters, 'move_dir x y' command is supported.
This commit is contained in:
parent
92940b6054
commit
de6a4d656f
5 changed files with 73 additions and 3 deletions
9
agent.cc
9
agent.cc
|
@ -28,6 +28,15 @@ agent::move_dir(int dir_x, int dir_y)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
agent::on_action_takes(void)
|
||||||
|
{
|
||||||
|
if (!conn)
|
||||||
|
return;
|
||||||
|
|
||||||
|
conn->actions(this);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
agent::on_tick(void)
|
agent::on_tick(void)
|
||||||
{
|
{
|
||||||
|
|
1
agent.h
1
agent.h
|
@ -19,6 +19,7 @@ public:
|
||||||
|
|
||||||
bool move_dir(int dir_x, int dir_y);
|
bool move_dir(int dir_x, int dir_y);
|
||||||
|
|
||||||
|
void on_action_takes(void);
|
||||||
void on_tick(void);
|
void on_tick(void);
|
||||||
void on_senses_update(void);
|
void on_senses_update(void);
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
|
#include <sys/select.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "agent.h"
|
#include "agent.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
@ -20,6 +21,38 @@ connection::senses(int tick_id, char around[4])
|
||||||
pthread_mutex_unlock(&buf_lock);
|
pthread_mutex_unlock(&buf_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
connection::actions(class agent *agent)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&buf_lock);
|
||||||
|
if (in_buf.find("\r\n\r\n") == std::string::npos) {
|
||||||
|
/* Not enough data, needs to wait until next turn, sorry. */
|
||||||
|
pthread_mutex_unlock(&buf_lock);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (in_buf.c_str()[0] != '\r') {
|
||||||
|
int nlofs = in_buf.find("\r\n");
|
||||||
|
std::string line = in_buf.substr(0, nlofs);
|
||||||
|
in_buf.erase(0, nlofs + 2);
|
||||||
|
|
||||||
|
int spofs = line.find(' ');
|
||||||
|
std::string cmd = line.substr(0, spofs);
|
||||||
|
line.erase(0, spofs + 1);
|
||||||
|
|
||||||
|
if (!cmd.compare("move_dir")) {
|
||||||
|
int x = 0, y = 0;
|
||||||
|
sscanf(line.c_str(), "%d %d", &x, &y);
|
||||||
|
agent->move_dir(x, y);
|
||||||
|
} else {
|
||||||
|
std::cout << "unknown line " << cmd << " " << line << " ...\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in_buf.erase(0, 2);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&buf_lock);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
connection::cancel(void)
|
connection::cancel(void)
|
||||||
{
|
{
|
||||||
|
@ -69,7 +102,26 @@ connection::thread_loop(void)
|
||||||
pthread_mutex_unlock(&buf_lock);
|
pthread_mutex_unlock(&buf_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: The reading. ;-) */
|
struct timeval tv;
|
||||||
|
tv.tv_sec = 0; tv.tv_usec = 0;
|
||||||
|
fd_set rfds;
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(fd, &rfds);
|
||||||
|
while (select(fd + 1, &rfds, NULL, NULL, &tv)) {
|
||||||
|
char cbuf[1024];
|
||||||
|
len = read(fd, cbuf, sizeof(cbuf));
|
||||||
|
if (len < 0) {
|
||||||
|
error = true;
|
||||||
|
} else {
|
||||||
|
bool want_moar = false;
|
||||||
|
pthread_mutex_lock(&buf_lock);
|
||||||
|
in_buf.append(cbuf, len);
|
||||||
|
want_moar = (in_buf.find("\r\n\r\n") == std::string::npos);
|
||||||
|
pthread_mutex_unlock(&buf_lock);
|
||||||
|
if (!want_moar)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
usleep(10000); // XXX: Signal-oriented instead.
|
usleep(10000); // XXX: Signal-oriented instead.
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
|
||||||
|
class agent;
|
||||||
|
|
||||||
class connection {
|
class connection {
|
||||||
public:
|
public:
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -25,11 +27,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void senses(int tick_id, char around[4]);
|
void senses(int tick_id, char around[4]);
|
||||||
|
void actions(class agent *);
|
||||||
|
|
||||||
void cancel(void);
|
void cancel(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string out_buf;
|
std::string out_buf, in_buf;
|
||||||
pthread_mutex_t buf_lock;
|
pthread_mutex_t buf_lock;
|
||||||
|
|
||||||
pthread_cond_t cancel_cond;
|
pthread_cond_t cancel_cond;
|
||||||
|
|
5
main.cc
5
main.cc
|
@ -69,6 +69,11 @@ next_agent:
|
||||||
agents.push_back(new class agent(agents.size(), map.agent_startpos(), conn));
|
agents.push_back(new class agent(agents.size(), map.agent_startpos(), conn));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Collect and take actions. */
|
||||||
|
|
||||||
|
for (std::list<class agent *>::iterator agent = agents.begin(); agent != agents.end(); agent++)
|
||||||
|
(*agent)->on_action_takes();
|
||||||
|
|
||||||
/* Run on_tick everywhere. */
|
/* Run on_tick everywhere. */
|
||||||
|
|
||||||
map.on_tick();
|
map.on_tick();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue