From 0c298088369ebe867487f3d97d1a9cd45c68a725 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 04:10:53 +0100 Subject: [PATCH] Action attack_dir(): Implement, trivially random now --- README | 1 + agent.cc | 25 +++++++++++++++++++++++++ agent.h | 1 + connection.cc | 7 +++++++ world.h | 2 ++ 5 files changed, 36 insertions(+) diff --git a/README b/README index a57782d..ba68506 100644 --- a/README +++ b/README @@ -23,6 +23,7 @@ The following inputs (in no particular order) are supported: tick BUMP if received, the agent's move failed + (or attack of non-existent agent etc.) DEAD if received, the agent is dead! energy diff --git a/agent.cc b/agent.cc index bc80862..5922000 100644 --- a/agent.cc +++ b/agent.cc @@ -45,6 +45,31 @@ agent::move_dir(int dir_x, int dir_y) return true; } +bool +agent::attack_dir(int dir_x, int dir_y) +{ + if (dead) + return false; + + class tile *t2 = &tile->tile_in_dir(dir_x, dir_y); + if (!t2->agent || t2->agent->dead) + return false; + + class agent *a = t2->agent; + chenergy(world::attack_cost); + a->chenergy(world::defense_cost); + if (dead || a->dead) + return true; + + int dice = rand() % (energy + a->energy); + if (dice < energy) { + a->die(); + } else { + die(); + } + return true; +} + void agent::chenergy(int delta) { diff --git a/agent.h b/agent.h index bb666a2..748c956 100644 --- a/agent.h +++ b/agent.h @@ -25,6 +25,7 @@ public: }; bool move_dir(int dir_x, int dir_y); + bool attack_dir(int dir_x, int dir_y); void chenergy(int delta); void die(void); diff --git a/connection.cc b/connection.cc index 3660bb4..8655795 100644 --- a/connection.cc +++ b/connection.cc @@ -72,6 +72,13 @@ connection::actions(class agent &agent) if (y < -1) y = -1; if (y > 1) y = 1; if (!agent.move_dir(x, y)) bump(); + } else if (!cmd.compare("attack_dir")) { + int x = 0, y = 0; + sscanf(line.c_str(), "%d %d", &x, &y); + if (x < -1) x = -1; if (x > 1) x = 1; + if (y < -1) y = -1; if (y > 1) y = 1; + if (!agent.attack_dir(x, y)) + bump(); } else { std::cout << "unknown line " << cmd << " " << line << " ...\n"; } diff --git a/world.h b/world.h index 6da11c8..df8a26e 100644 --- a/world.h +++ b/world.h @@ -4,6 +4,8 @@ struct world { const static int newborn_energy = 500; const static int move_cost = -10; + const static int attack_cost = -40; + const static int defense_cost = -20; const static int sun_energy = 1; const static int dead_body_energy = 500;