Info is now an interface, implemented by Dude
This commit is contained in:
parent
989742f458
commit
dfb5d471a6
@ -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;
|
|
||||||
}
|
|
@ -34,7 +34,6 @@ struct Action
|
|||||||
PUT,
|
PUT,
|
||||||
WORK,
|
WORK,
|
||||||
WAIT,
|
WAIT,
|
||||||
COLOR,
|
|
||||||
COMMUNICATE
|
COMMUNICATE
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,20 +42,13 @@ struct Action
|
|||||||
Com com_data;
|
Com com_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Info
|
struct Info
|
||||||
{
|
{
|
||||||
private:
|
virtual bool getSuccess() = 0;
|
||||||
Map *p_map;
|
virtual bool getInventory() = 0;
|
||||||
Coord m_pos;
|
virtual const Com& getCom() = 0;
|
||||||
// TODO
|
virtual int getNear(Dir d) = 0;
|
||||||
public:
|
virtual int getInfo(Dir d) = 0;
|
||||||
bool m_success;
|
|
||||||
int m_inventory;
|
|
||||||
Info();
|
|
||||||
Info(Map*);
|
|
||||||
const Com& getCom();
|
|
||||||
int getNear(Dir d);
|
|
||||||
int getInfo(Dir d);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*BehaviorFunction)(Action *action, char *memory, const Info *info);
|
typedef void (*BehaviorFunction)(Action *action, char *memory, const Info *info);
|
||||||
|
38
src/dude.cpp
38
src/dude.cpp
@ -1,14 +1,15 @@
|
|||||||
#include "dude.h"
|
#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)
|
||||||
{
|
{
|
||||||
|
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
|
||||||
}
|
|
||||||
|
|
||||||
Dude::Dude(Coord _pos, Info _info):m_pos(_pos),m_info(_info),m_dead(false)
|
|
||||||
{
|
|
||||||
memset(&m_memory,0,DUDE_MEMORY_SIZE*sizeof(char));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* Dude::getMemory()
|
char* Dude::getMemory()
|
||||||
@ -16,7 +17,24 @@ char* Dude::getMemory()
|
|||||||
return m_memory;
|
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;
|
||||||
}
|
}
|
||||||
|
24
src/dude.h
24
src/dude.h
@ -3,18 +3,32 @@
|
|||||||
|
|
||||||
#include "behavior.h"
|
#include "behavior.h"
|
||||||
|
|
||||||
class Dude
|
class Dude : public Info
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Coord m_pos;
|
Coord m_pos;
|
||||||
Info m_info;
|
Map &m_map;
|
||||||
bool m_dead;
|
bool m_dead;
|
||||||
|
bool m_success;
|
||||||
|
int m_inventory;
|
||||||
char m_memory[DUDE_MEMORY_SIZE];
|
char m_memory[DUDE_MEMORY_SIZE];
|
||||||
|
Com m_comData;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dude();
|
Dude(const Coord &_pos, Map &map);
|
||||||
Dude(Coord,Info);
|
|
||||||
char* getMemory();
|
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
|
#endif // DUDE_H
|
||||||
|
@ -8,18 +8,23 @@ Simulation::Simulation(Map *_map,std::vector<BehaviorFunction> _behaviors):
|
|||||||
int i=0;
|
int i=0;
|
||||||
for(auto behavior : _behaviors){
|
for(auto behavior : _behaviors){
|
||||||
glm::vec3 color;
|
glm::vec3 color;
|
||||||
color[i%3]=i*50;
|
color[i%3]=0.5f*(1 + i/3);
|
||||||
m_teams[i] = Team(color,behavior);
|
m_teams.push_back(Team(color, color, behavior));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulation::run()
|
void Simulation::run()
|
||||||
{
|
{
|
||||||
for(auto t : m_teams){
|
for(int i=0; i<m_teams.size(); ++i){
|
||||||
if (t.second.updateSpawn())
|
Team &t = m_teams[i];
|
||||||
t.second.spawn(p_map->team(t.first));
|
if (t.updateSpawn())
|
||||||
t.second.updateDudes();
|
{
|
||||||
|
Dir randDir = Dir(rand()%4);
|
||||||
|
Coord spawnPos = p_map->team(i) + Coord(randDir);
|
||||||
|
t.spawn(spawnPos, *p_map);
|
||||||
|
}
|
||||||
|
t.updateDudes();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@ class Simulation
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Map *p_map;
|
Map *p_map;
|
||||||
std::map<int,Team> m_teams;
|
std::vector<Team> m_teams;
|
||||||
public:
|
public:
|
||||||
Simulation(Map *_map,std::vector<BehaviorFunction> _behaviors);
|
Simulation(Map *_map, std::vector<BehaviorFunction> _behaviors);
|
||||||
void run();
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
28
src/team.cpp
28
src/team.cpp
@ -1,30 +1,32 @@
|
|||||||
#include "team.h"
|
#include "team.h"
|
||||||
|
|
||||||
Team::Team()
|
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),
|
||||||
Team::Team(glm::vec3 _color,BehaviorFunction _behavior):
|
m_behavior(_behavior)
|
||||||
m_color(_color),m_spawnCooldown(SPAWN_COOLDOWN),
|
|
||||||
m_foodQuantity(NB_STARTING_FOOD), m_behavior(_behavior)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Team::updateSpawn()
|
bool Team::updateSpawn()
|
||||||
{
|
{
|
||||||
return !(--m_spawnCooldown);
|
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()
|
void Team::updateDudes()
|
||||||
{
|
{
|
||||||
for (auto dude : m_dudes){
|
for (Dude &dude : m_dudes){
|
||||||
Action* action;
|
Action* action;
|
||||||
m_behavior(action,dude.getMemory(),dude.getInfo());
|
m_behavior(action, dude.getMemory(), &dude);
|
||||||
|
// TODO perform action
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,16 +14,16 @@
|
|||||||
|
|
||||||
class Team
|
class Team
|
||||||
{
|
{
|
||||||
glm::vec3 m_color;
|
glm::vec3 m_teamColor;
|
||||||
|
glm::vec3 m_dudeColor;
|
||||||
int m_spawnCooldown;
|
int m_spawnCooldown;
|
||||||
int m_foodQuantity;
|
int m_foodQuantity;
|
||||||
std::vector<Dude> m_dudes;
|
std::vector<Dude> m_dudes;
|
||||||
BehaviorFunction m_behavior;
|
BehaviorFunction m_behavior;
|
||||||
public:
|
public:
|
||||||
Team();
|
Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior);
|
||||||
Team(glm::vec3 _color,BehaviorFunction _behavior);
|
|
||||||
bool updateSpawn();
|
bool updateSpawn();
|
||||||
void spawn(Coord &d);
|
void spawn(Coord &d, Map &map);
|
||||||
void updateDudes();
|
void updateDudes();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user