mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 18:03:37 +02:00
agent::spawn_at(): Disassociate from constructor, allow tile == NULL
This commit is contained in:
parent
3e5366a5c4
commit
7714906baa
3 changed files with 13 additions and 13 deletions
12
agent.cc
12
agent.cc
|
@ -8,8 +8,9 @@
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
agent::put_at(class tile &t)
|
agent::spawn_at(class tile &t)
|
||||||
{
|
{
|
||||||
|
tile = &t;
|
||||||
if (!t.on_agent_enter(*this)) {
|
if (!t.on_agent_enter(*this)) {
|
||||||
std::cerr << "Collision.";
|
std::cerr << "Collision.";
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -19,7 +20,7 @@ agent::put_at(class tile &t)
|
||||||
bool
|
bool
|
||||||
agent::move_dir(int dir_x, int dir_y)
|
agent::move_dir(int dir_x, int dir_y)
|
||||||
{
|
{
|
||||||
if (dead)
|
if (dead || !tile)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
chenergy(world::move_cost);
|
chenergy(world::move_cost);
|
||||||
|
@ -48,7 +49,7 @@ agent::move_dir(int dir_x, int dir_y)
|
||||||
bool
|
bool
|
||||||
agent::attack_dir(int dir_x, int dir_y)
|
agent::attack_dir(int dir_x, int dir_y)
|
||||||
{
|
{
|
||||||
if (dead)
|
if (dead || !tile)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
class tile *t2 = &tile->tile_in_dir(dir_x, dir_y);
|
class tile *t2 = &tile->tile_in_dir(dir_x, dir_y);
|
||||||
|
@ -112,7 +113,7 @@ agent::on_tick(void)
|
||||||
void
|
void
|
||||||
agent::on_senses_update(void)
|
agent::on_senses_update(void)
|
||||||
{
|
{
|
||||||
if (!conn)
|
if (!conn || !tile)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
conn->senses(tick_id, *this);
|
conn->senses(tick_id, *this);
|
||||||
|
@ -120,7 +121,8 @@ agent::on_senses_update(void)
|
||||||
|
|
||||||
agent::~agent()
|
agent::~agent()
|
||||||
{
|
{
|
||||||
tile->on_agent_leave(*this);
|
if (tile)
|
||||||
|
tile->on_agent_leave(*this);
|
||||||
if (conn) {
|
if (conn) {
|
||||||
conn->cancel();
|
conn->cancel();
|
||||||
conn = NULL;
|
conn = NULL;
|
||||||
|
|
10
agent.h
10
agent.h
|
@ -16,13 +16,13 @@ public:
|
||||||
int energy;
|
int energy;
|
||||||
bool dead;
|
bool dead;
|
||||||
|
|
||||||
agent(int id_, class tile &tile_, class connection *conn_)
|
agent(int id_, class connection *conn_)
|
||||||
: id (id_), conn (conn_), tile (&tile_)
|
: id (id_), conn (conn_)
|
||||||
{
|
{
|
||||||
put_at(tile_);
|
|
||||||
energy = world::newborn_energy;
|
energy = world::newborn_energy;
|
||||||
dead = false;
|
dead = false;
|
||||||
};
|
};
|
||||||
|
void spawn_at(class tile &tile);
|
||||||
|
|
||||||
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);
|
bool attack_dir(int dir_x, int dir_y);
|
||||||
|
@ -35,10 +35,6 @@ public:
|
||||||
void on_senses_update(void);
|
void on_senses_update(void);
|
||||||
|
|
||||||
~agent();
|
~agent();
|
||||||
|
|
||||||
private:
|
|
||||||
/* Just for initial placement. */
|
|
||||||
void put_at(class tile &tile);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
4
main.cc
4
main.cc
|
@ -67,7 +67,9 @@ next_agent:
|
||||||
int cfd = accept(lfd, NULL, NULL);
|
int cfd = accept(lfd, NULL, NULL);
|
||||||
if (cfd >= 0) {
|
if (cfd >= 0) {
|
||||||
class connection *conn = new class connection(cfd);
|
class connection *conn = new class connection(cfd);
|
||||||
agents.push_back(new class agent(aid++, map.agent_startpos(), conn));
|
class agent *a = new class agent(aid++, conn);
|
||||||
|
agents.push_back(a);
|
||||||
|
a->spawn_at(map.agent_startpos());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Collect and take actions. */
|
/* Collect and take actions. */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue