mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 18:03:37 +02:00
Action attack_dir(): Implement, trivially random now
This commit is contained in:
parent
0398856635
commit
0c29808836
5 changed files with 36 additions and 0 deletions
1
README
1
README
|
@ -23,6 +23,7 @@ The following inputs (in no particular order) are supported:
|
|||
tick <ticknum>
|
||||
BUMP
|
||||
if received, the agent's move failed
|
||||
(or attack of non-existent agent etc.)
|
||||
DEAD
|
||||
if received, the agent is dead!
|
||||
energy <points>
|
||||
|
|
25
agent.cc
25
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)
|
||||
{
|
||||
|
|
1
agent.h
1
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);
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
2
world.h
2
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue