mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-03 18:33:39 +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>
|
move <rate>
|
||||||
<rate> between 0 and 1, describing probability
|
<rate> between 0 and 1, describing probability
|
||||||
of success of move command. Higher probability
|
of success of move command.
|
||||||
means higher energy maintenance of move actuators.
|
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 <cassert>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "agent.h"
|
#include "agent.h"
|
||||||
|
@ -71,8 +72,8 @@ agent::attack_dir(int dir_x, int dir_y)
|
||||||
if (dead || a->dead)
|
if (dead || a->dead)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
int dice = rand() % (energy + a->energy);
|
int dice = random() % ((int) round(attr.attack * energy) + (int) round(a->attr.defense * a->energy));
|
||||||
if (dice < energy) {
|
if (dice < attr.attack * energy) {
|
||||||
a->die();
|
a->die();
|
||||||
} else {
|
} else {
|
||||||
die();
|
die();
|
||||||
|
@ -115,6 +116,8 @@ agent::on_tick(void)
|
||||||
if (!dead) {
|
if (!dead) {
|
||||||
chenergy(world::sun_energy);
|
chenergy(world::sun_energy);
|
||||||
chenergy(attr.move * world::move_idle_cost);
|
chenergy(attr.move * world::move_idle_cost);
|
||||||
|
chenergy(attr.attack * world::attack_idle_cost);
|
||||||
|
chenergy(attr.defense * world::defense_idle_cost);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
energy += world::dead_decay;
|
energy += world::dead_decay;
|
||||||
|
|
4
agent.h
4
agent.h
|
@ -16,6 +16,8 @@ public:
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
double move;
|
double move;
|
||||||
|
double attack;
|
||||||
|
double defense;
|
||||||
} attr;
|
} attr;
|
||||||
|
|
||||||
int energy;
|
int energy;
|
||||||
|
@ -27,6 +29,8 @@ public:
|
||||||
energy = world::newborn_energy;
|
energy = world::newborn_energy;
|
||||||
dead = false;
|
dead = false;
|
||||||
attr.move = 1.0;
|
attr.move = 1.0;
|
||||||
|
attr.attack = 0.5;
|
||||||
|
attr.defense = 0.5;
|
||||||
};
|
};
|
||||||
void spawn(void);
|
void spawn(void);
|
||||||
void spawn_at(class tile &tile);
|
void spawn_at(class tile &tile);
|
||||||
|
|
|
@ -70,10 +70,20 @@ connection::actions(class agent &agent)
|
||||||
line.erase(0, spofs + 1);
|
line.erase(0, spofs + 1);
|
||||||
|
|
||||||
if (negotiation && !cmd.compare("move")) {
|
if (negotiation && !cmd.compare("move")) {
|
||||||
double mprob = 1.0;
|
double rate = agent.attr.move;
|
||||||
sscanf(line.c_str(), "%lf", &mprob);
|
sscanf(line.c_str(), "%lf", &rate);
|
||||||
if (mprob >= 0 && mprob <= 1)
|
if (rate >= 0 && rate <= 1)
|
||||||
agent.attr.move = mprob;
|
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")) {
|
} else if (!negotiation && !cmd.compare("move_dir")) {
|
||||||
int x = 0, y = 0;
|
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 (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 (!negotiation && !cmd.compare("attack_dir")) {
|
} else if (!negotiation && !cmd.compare("attack_dir")) {
|
||||||
int x = 0, y = 0;
|
int x = 0, y = 0;
|
||||||
sscanf(line.c_str(), "%d %d", &x, &y);
|
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 defense_cost = -200;
|
||||||
|
|
||||||
const static int move_idle_cost = -15; /* ... * attr.move */
|
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 sun_energy = 10;
|
||||||
|
|
||||||
const static int dead_body_energy = 2000;
|
const static int dead_body_energy = 2000;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue