Info is now an interface, implemented by Dude

This commit is contained in:
Anselme 2016-05-18 17:31:50 +02:00
parent 989742f458
commit dfb5d471a6
8 changed files with 85 additions and 75 deletions

View File

@ -1,21 +0,0 @@
#include "behavior.h"
#include "map.h"
// TODO Info functions
Info::Info(){
}
Info::Info(Map *_map):p_map(_map){
}
int Info::getNear(Dir d)
{
return (*p_map)[Coord(d)].type;
}
int Info::getInfo(Dir d)
{
return (*p_map)[Coord(d)].data.nbRes;
}

View File

@ -34,7 +34,6 @@ struct Action
PUT,
WORK,
WAIT,
COLOR,
COMMUNICATE
};
@ -43,20 +42,13 @@ struct Action
Com com_data;
};
class Info
struct Info
{
private:
Map *p_map;
Coord m_pos;
// TODO
public:
bool m_success;
int m_inventory;
Info();
Info(Map*);
const Com& getCom();
int getNear(Dir d);
int getInfo(Dir d);
virtual bool getSuccess() = 0;
virtual bool getInventory() = 0;
virtual const Com& getCom() = 0;
virtual int getNear(Dir d) = 0;
virtual int getInfo(Dir d) = 0;
};
typedef void (*BehaviorFunction)(Action *action, char *memory, const Info *info);

View File

@ -1,14 +1,15 @@
#include "dude.h"
#include "string.h"
#include "map.h"
#include <cstring>
Dude::Dude()
Dude::Dude(const Coord &_pos, Map &map) :
m_pos(_pos),
m_map(map),
m_dead(false),
m_success(true),
m_inventory(-1)
{
}
Dude::Dude(Coord _pos, Info _info):m_pos(_pos),m_info(_info),m_dead(false)
{
memset(&m_memory,0,DUDE_MEMORY_SIZE*sizeof(char));
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
}
char* Dude::getMemory()
@ -16,7 +17,24 @@ char* Dude::getMemory()
return m_memory;
}
Info* Dude::getInfo()
void Dude::move(Dir d)
{
return &m_info;
m_pos += Coord(d);
m_pos.x %= m_map.getWidth();
m_pos.y %= m_map.getHeight();
}
void Dude::receiveComData(const Com &comData)
{
std::memcpy(&m_comData, &comData, sizeof(Com));
}
int Dude::getNear(Dir d)
{
return m_map[Coord(d) + m_pos].type;
}
int Dude::getInfo(Dir d)
{
return m_map[Coord(d) + m_pos].data.nbRes;
}

View File

@ -3,18 +3,32 @@
#include "behavior.h"
class Dude
class Dude : public Info
{
private:
Coord m_pos;
Info m_info;
Map &m_map;
bool m_dead;
bool m_success;
int m_inventory;
char m_memory[DUDE_MEMORY_SIZE];
Com m_comData;
public:
Dude();
Dude(Coord,Info);
Dude(const Coord &_pos, Map &map);
char* getMemory();
Info* getInfo();
void setSuccess(bool success) { m_success = success; }
void setInventory(int item) { m_inventory = item; }
void trespass() { m_dead = true; }
const Coord& getPos() { return m_pos; }
void move(Dir d);
void receiveComData(const Com &comData);
virtual bool getSuccess() { return m_success; }
virtual bool getInventory() { return m_inventory; }
virtual const Com& getCom() { return m_comData; }
virtual int getNear(Dir d);
virtual int getInfo(Dir d);
};
#endif // DUDE_H

View File

@ -8,18 +8,23 @@ Simulation::Simulation(Map *_map,std::vector<BehaviorFunction> _behaviors):
int i=0;
for(auto behavior : _behaviors){
glm::vec3 color;
color[i%3]=i*50;
m_teams[i] = Team(color,behavior);
color[i%3]=0.5f*(1 + i/3);
m_teams.push_back(Team(color, color, behavior));
i++;
}
}
void Simulation::run()
{
for(auto t : m_teams){
if (t.second.updateSpawn())
t.second.spawn(p_map->team(t.first));
t.second.updateDudes();
for(int i=0; i<m_teams.size(); ++i){
Team &t = m_teams[i];
if (t.updateSpawn())
{
Dir randDir = Dir(rand()%4);
Coord spawnPos = p_map->team(i) + Coord(randDir);
t.spawn(spawnPos, *p_map);
}
t.updateDudes();
}
}

View File

@ -13,7 +13,7 @@ class Simulation
{
private:
Map *p_map;
std::map<int,Team> m_teams;
std::vector<Team> m_teams;
public:
Simulation(Map *_map, std::vector<BehaviorFunction> _behaviors);
void run();

View File

@ -1,30 +1,32 @@
#include "team.h"
Team::Team()
{
}
Team::Team(glm::vec3 _color,BehaviorFunction _behavior):
m_color(_color),m_spawnCooldown(SPAWN_COOLDOWN),
m_foodQuantity(NB_STARTING_FOOD), m_behavior(_behavior)
Team::Team(const glm::vec3 &teamColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior):
m_teamColor(teamColor),
m_dudeColor(dudeColor),
m_spawnCooldown(1),
m_foodQuantity(NB_STARTING_FOOD),
m_behavior(_behavior)
{
}
bool Team::updateSpawn()
{
if(m_foodQuantity > 0)
return !(--m_spawnCooldown);
}
void Team::spawn(Coord &d)
void Team::spawn(Coord &d, Map &map)
{
m_dudes.push_back(Dude(d,Info()));
m_spawnCooldown = SPAWN_COOLDOWN;
--m_foodQuantity;
m_dudes.push_back(Dude(d, map));
}
void Team::updateDudes()
{
for (auto dude : m_dudes){
for (Dude &dude : m_dudes){
Action* action;
m_behavior(action,dude.getMemory(),dude.getInfo());
m_behavior(action, dude.getMemory(), &dude);
// TODO perform action
}
}

View File

@ -14,16 +14,16 @@
class Team
{
glm::vec3 m_color;
glm::vec3 m_teamColor;
glm::vec3 m_dudeColor;
int m_spawnCooldown;
int m_foodQuantity;
std::vector<Dude> m_dudes;
BehaviorFunction m_behavior;
public:
Team();
Team(glm::vec3 _color,BehaviorFunction _behavior);
Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior);
bool updateSpawn();
void spawn(Coord &d);
void spawn(Coord &d, Map &map);
void updateDudes();
};