Action attack_dir(): Implement, trivially random now

This commit is contained in:
Petr Baudis 2011-11-27 04:10:53 +01:00
parent 0398856635
commit 0c29808836
5 changed files with 36 additions and 0 deletions

1
README
View file

@ -23,6 +23,7 @@ The following inputs (in no particular order) are supported:
tick <ticknum> tick <ticknum>
BUMP BUMP
if received, the agent's move failed if received, the agent's move failed
(or attack of non-existent agent etc.)
DEAD DEAD
if received, the agent is dead! if received, the agent is dead!
energy <points> energy <points>

View file

@ -45,6 +45,31 @@ agent::move_dir(int dir_x, int dir_y)
return true; 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 void
agent::chenergy(int delta) agent::chenergy(int delta)
{ {

View file

@ -25,6 +25,7 @@ public:
}; };
bool move_dir(int dir_x, int dir_y); bool move_dir(int dir_x, int dir_y);
bool attack_dir(int dir_x, int dir_y);
void chenergy(int delta); void chenergy(int delta);
void die(void); void die(void);

View file

@ -72,6 +72,13 @@ connection::actions(class agent &agent)
if (y < -1) y = -1; if (y > 1) y = 1; if (y < -1) y = -1; if (y > 1) y = 1;
if (!agent.move_dir(x, y)) if (!agent.move_dir(x, y))
bump(); 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 { } else {
std::cout << "unknown line " << cmd << " " << line << " ...\n"; std::cout << "unknown line " << cmd << " " << line << " ...\n";
} }

View file

@ -4,6 +4,8 @@
struct world { struct world {
const static int newborn_energy = 500; const static int newborn_energy = 500;
const static int move_cost = -10; 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 sun_energy = 1;
const static int dead_body_energy = 500; const static int dead_body_energy = 500;