From b80ccfdffd93474bcb37648fadafe2e23e191688 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 27 Nov 2011 05:18:52 +0100 Subject: [PATCH] Attack, defense rates --- README | 10 ++++++++-- agent.cc | 7 +++++-- agent.h | 4 ++++ connection.cc | 19 ++++++++++++++----- world.h | 2 ++ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README b/README index d788e5d..79f6117 100644 --- a/README +++ b/README @@ -54,5 +54,11 @@ by empty line), but with these commands instead: move between 0 and 1, describing probability - of success of move command. Higher probability - means higher energy maintenance of move actuators. + of success of move command. + attack + between 0 and 1. + defense + between 0 and 1. + +In general, higher rate means higher energy maintenance of the +appropriate actuators. diff --git a/agent.cc b/agent.cc index 7a10765..a595140 100644 --- a/agent.cc +++ b/agent.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include "agent.h" @@ -71,8 +72,8 @@ agent::attack_dir(int dir_x, int dir_y) if (dead || a->dead) return true; - int dice = rand() % (energy + a->energy); - if (dice < energy) { + int dice = random() % ((int) round(attr.attack * energy) + (int) round(a->attr.defense * a->energy)); + if (dice < attr.attack * energy) { a->die(); } else { die(); @@ -115,6 +116,8 @@ agent::on_tick(void) if (!dead) { chenergy(world::sun_energy); chenergy(attr.move * world::move_idle_cost); + chenergy(attr.attack * world::attack_idle_cost); + chenergy(attr.defense * world::defense_idle_cost); } else { energy += world::dead_decay; diff --git a/agent.h b/agent.h index 56a7b7e..d974402 100644 --- a/agent.h +++ b/agent.h @@ -16,6 +16,8 @@ public: struct { double move; + double attack; + double defense; } attr; int energy; @@ -27,6 +29,8 @@ public: energy = world::newborn_energy; dead = false; attr.move = 1.0; + attr.attack = 0.5; + attr.defense = 0.5; }; void spawn(void); void spawn_at(class tile &tile); diff --git a/connection.cc b/connection.cc index 022eba7..8a4c740 100644 --- a/connection.cc +++ b/connection.cc @@ -70,10 +70,20 @@ connection::actions(class agent &agent) line.erase(0, spofs + 1); if (negotiation && !cmd.compare("move")) { - double mprob = 1.0; - sscanf(line.c_str(), "%lf", &mprob); - if (mprob >= 0 && mprob <= 1) - agent.attr.move = mprob; + double rate = agent.attr.move; + sscanf(line.c_str(), "%lf", &rate); + if (rate >= 0 && rate <= 1) + agent.attr.move = rate; + } else if (negotiation && !cmd.compare("attack")) { + double rate = agent.attr.attack; + sscanf(line.c_str(), "%lf", &rate); + if (rate >= 0 && rate <= 1) + agent.attr.attack = rate; + } else if (negotiation && !cmd.compare("defense")) { + double rate = agent.attr.defense; + sscanf(line.c_str(), "%lf", &rate); + if (rate >= 0 && rate <= 1) + agent.attr.defense = rate; } else if (!negotiation && !cmd.compare("move_dir")) { int x = 0, y = 0; @@ -82,7 +92,6 @@ connection::actions(class agent &agent) if (y < -1) y = -1; if (y > 1) y = 1; if (!agent.move_dir(x, y)) bump(); - } else if (!negotiation && !cmd.compare("attack_dir")) { int x = 0, y = 0; sscanf(line.c_str(), "%d %d", &x, &y); diff --git a/world.h b/world.h index a8e0687..dff7ccd 100644 --- a/world.h +++ b/world.h @@ -8,6 +8,8 @@ struct world { const static int defense_cost = -200; const static int move_idle_cost = -15; /* ... * attr.move */ + const static int attack_idle_cost = -15; /* ... * attr.attack */ + const static int defense_idle_cost = -15; /* ... * attr.defense */ const static int sun_energy = 10; const static int dead_body_energy = 2000;