Support for agent death

This commit is contained in:
Petr Baudis 2011-11-27 02:48:28 +01:00
parent f6221436a4
commit 64ea015a06
4 changed files with 20 additions and 5 deletions

View file

@ -30,6 +30,12 @@ agent::move_dir(int dir_x, int dir_y)
return true; return true;
} }
void
agent::die(void)
{
dead = true;
}
void void
agent::on_action_takes(void) agent::on_action_takes(void)
{ {
@ -42,7 +48,12 @@ agent::on_action_takes(void)
void void
agent::on_tick(void) agent::on_tick(void)
{ {
energy += world::sun_energy; if (!dead) {
energy += world::sun_energy;
if (energy <= 0)
die();
}
} }
void void
@ -57,7 +68,7 @@ agent::on_senses_update(void)
tile->tile_in_dir(0, 1).symbol(), tile->tile_in_dir(0, 1).symbol(),
tile->tile_in_dir(-1, 0).symbol(), tile->tile_in_dir(-1, 0).symbol(),
}; };
conn->senses(tick_id, energy, around); conn->senses(tick_id, dead, energy, around);
} }
agent::~agent() agent::~agent()

View file

@ -14,16 +14,20 @@ public:
class tile *tile; class tile *tile;
int energy; int energy;
bool dead;
agent(int id_, class tile &tile_, class connection *conn_) agent(int id_, class tile &tile_, class connection *conn_)
: id (id_), conn (conn_), tile (&tile_) : id (id_), conn (conn_), tile (&tile_)
{ {
put_at(tile_); put_at(tile_);
energy = world::newborn_energy; energy = world::newborn_energy;
dead = false;
}; };
bool move_dir(int dir_x, int dir_y); bool move_dir(int dir_x, int dir_y);
void die(void);
void on_action_takes(void); void on_action_takes(void);
void on_tick(void); void on_tick(void);
void on_senses_update(void); void on_senses_update(void);

View file

@ -11,10 +11,10 @@
#include "connection.h" #include "connection.h"
void void
connection::senses(int tick_id, int energy, char around[4]) connection::senses(int tick_id, bool dead, int energy, char around[4])
{ {
char buf[1024]; char buf[1024];
snprintf(buf, sizeof(buf), "tick %d\nenergy %d\naround %c%c%c%c\n\n", tick_id, energy, around[0], around[1], around[2], around[3]); snprintf(buf, sizeof(buf), "tick %d\n%senergy %d\naround %c%c%c%c\n\n", tick_id, dead ? "DEAD\n" : "", energy, around[0], around[1], around[2], around[3]);
pthread_mutex_lock(&buf_lock); pthread_mutex_lock(&buf_lock);
out_buf.append(buf); out_buf.append(buf);

View file

@ -26,7 +26,7 @@ public:
close(fd); close(fd);
} }
void senses(int tick_id, int energy, char around[4]); void senses(int tick_id, bool dead, int energy, char around[4]);
void actions(class agent *); void actions(class agent *);
void cancel(void); void cancel(void);