mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 09:53:38 +02:00
Attack, defense rates
This commit is contained in:
parent
ceb16277fb
commit
b80ccfdffd
5 changed files with 33 additions and 9 deletions
10
README
10
README
|
@ -54,5 +54,11 @@ by empty line), but with these commands instead:
|
|||
|
||||
move <rate>
|
||||
<rate> 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 <rate>
|
||||
<rate> between 0 and 1.
|
||||
defense <rate>
|
||||
<rate> between 0 and 1.
|
||||
|
||||
In general, higher rate means higher energy maintenance of the
|
||||
appropriate actuators.
|
||||
|
|
7
agent.cc
7
agent.cc
|
@ -1,5 +1,6 @@
|
|||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
|
|
4
agent.h
4
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);
|
||||
|
|
|
@ -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);
|
||||
|
|
2
world.h
2
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue