From 64ea015a06bed60f9e2e89a8d1dc27e0d6bea7de Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 02:48:28 +0100 Subject: [PATCH] Support for agent death --- agent.cc | 15 +++++++++++++-- agent.h | 4 ++++ connection.cc | 4 ++-- connection.h | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/agent.cc b/agent.cc index 4f016e1..d63f14d 100644 --- a/agent.cc +++ b/agent.cc @@ -30,6 +30,12 @@ agent::move_dir(int dir_x, int dir_y) return true; } +void +agent::die(void) +{ + dead = true; +} + void agent::on_action_takes(void) { @@ -42,7 +48,12 @@ agent::on_action_takes(void) void agent::on_tick(void) { - energy += world::sun_energy; + if (!dead) { + energy += world::sun_energy; + + if (energy <= 0) + die(); + } } void @@ -57,7 +68,7 @@ agent::on_senses_update(void) tile->tile_in_dir(0, 1).symbol(), tile->tile_in_dir(-1, 0).symbol(), }; - conn->senses(tick_id, energy, around); + conn->senses(tick_id, dead, energy, around); } agent::~agent() diff --git a/agent.h b/agent.h index f9bf0f6..276fc4a 100644 --- a/agent.h +++ b/agent.h @@ -14,16 +14,20 @@ public: class tile *tile; int energy; + bool dead; agent(int id_, class tile &tile_, class connection *conn_) : id (id_), conn (conn_), tile (&tile_) { put_at(tile_); energy = world::newborn_energy; + dead = false; }; bool move_dir(int dir_x, int dir_y); + void die(void); + void on_action_takes(void); void on_tick(void); void on_senses_update(void); diff --git a/connection.cc b/connection.cc index f54cb41..124df8d 100644 --- a/connection.cc +++ b/connection.cc @@ -11,10 +11,10 @@ #include "connection.h" 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]; - 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); out_buf.append(buf); diff --git a/connection.h b/connection.h index 36f98df..ff98597 100644 --- a/connection.h +++ b/connection.h @@ -26,7 +26,7 @@ public: 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 cancel(void);