From 0398856635d06d9f3c2c24ad2508bf53d7269fb9 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 04:01:58 +0100 Subject: [PATCH] agent::chenergy(): Implement --- agent.cc | 16 +++++++++++----- agent.h | 1 + world.h | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/agent.cc b/agent.cc index ea097c7..bc80862 100644 --- a/agent.cc +++ b/agent.cc @@ -22,7 +22,7 @@ agent::move_dir(int dir_x, int dir_y) if (dead) return false; - energy -= world::move_cost; + chenergy(world::move_cost); class tile *t2 = &tile->tile_in_dir(dir_x, dir_y); if (t2->agent) { @@ -30,7 +30,7 @@ agent::move_dir(int dir_x, int dir_y) class agent *a = t2->agent; t2->on_agent_leave(*a); /* Nom. */ - energy += a->energy; + chenergy(a->energy); a->energy = 0; } else { return false; @@ -45,6 +45,14 @@ agent::move_dir(int dir_x, int dir_y) return true; } +void +agent::chenergy(int delta) +{ + energy += delta; + if (energy <= 0) + die(); +} + void agent::die(void) { @@ -66,9 +74,7 @@ void agent::on_tick(void) { if (!dead) { - energy += world::sun_energy; - if (energy <= 0) - die(); + chenergy(world::sun_energy); } else { energy += world::dead_decay; diff --git a/agent.h b/agent.h index 276fc4a..bb666a2 100644 --- a/agent.h +++ b/agent.h @@ -26,6 +26,7 @@ public: bool move_dir(int dir_x, int dir_y); + void chenergy(int delta); void die(void); void on_action_takes(void); diff --git a/world.h b/world.h index e8afd4c..6da11c8 100644 --- a/world.h +++ b/world.h @@ -3,7 +3,7 @@ struct world { const static int newborn_energy = 500; - const static int move_cost = 10; + const static int move_cost = -10; const static int sun_energy = 1; const static int dead_body_energy = 500;