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"
|
||||
|
||||
void
|
||||
agent::put_at(class tile &t)
|
||||
agent::spawn_at(class tile &t)
|
||||
{
|
||||
tile = &t;
|
||||
if (!t.on_agent_enter(*this)) {
|
||||
std::cerr << "Collision.";
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -19,7 +20,7 @@ agent::put_at(class tile &t)
|
|||
bool
|
||||
agent::move_dir(int dir_x, int dir_y)
|
||||
{
|
||||
if (dead)
|
||||
if (dead || !tile)
|
||||
return false;
|
||||
|
||||
chenergy(world::move_cost);
|
||||
|
@ -48,7 +49,7 @@ agent::move_dir(int dir_x, int dir_y)
|
|||
bool
|
||||
agent::attack_dir(int dir_x, int dir_y)
|
||||
{
|
||||
if (dead)
|
||||
if (dead || !tile)
|
||||
return false;
|
||||
|
||||
class tile *t2 = &tile->tile_in_dir(dir_x, dir_y);
|
||||
|
@ -112,7 +113,7 @@ agent::on_tick(void)
|
|||
void
|
||||
agent::on_senses_update(void)
|
||||
{
|
||||
if (!conn)
|
||||
if (!conn || !tile)
|
||||
return;
|
||||
|
||||
conn->senses(tick_id, *this);
|
||||
|
@ -120,7 +121,8 @@ agent::on_senses_update(void)
|
|||
|
||||
agent::~agent()
|
||||
{
|
||||
tile->on_agent_leave(*this);
|
||||
if (tile)
|
||||
tile->on_agent_leave(*this);
|
||||
if (conn) {
|
||||
conn->cancel();
|
||||
conn = NULL;
|
||||
|
|
10
agent.h
10
agent.h
|
@ -16,13 +16,13 @@ public:
|
|||
int energy;
|
||||
bool dead;
|
||||
|
||||
agent(int id_, class tile &tile_, class connection *conn_)
|
||||
: id (id_), conn (conn_), tile (&tile_)
|
||||
agent(int id_, class connection *conn_)
|
||||
: id (id_), conn (conn_)
|
||||
{
|
||||
put_at(tile_);
|
||||
energy = world::newborn_energy;
|
||||
dead = false;
|
||||
};
|
||||
void spawn_at(class tile &tile);
|
||||
|
||||
bool move_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);
|
||||
|
||||
~agent();
|
||||
|
||||
private:
|
||||
/* Just for initial placement. */
|
||||
void put_at(class tile &tile);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
4
main.cc
4
main.cc
|
@ -67,7 +67,9 @@ next_agent:
|
|||
int cfd = accept(lfd, NULL, NULL);
|
||||
if (cfd >= 0) {
|
||||
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. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue