implemented COMMUNICATE
This commit is contained in:
parent
43eb4c48a7
commit
2422cbe873
@ -22,8 +22,8 @@ struct Com
|
|||||||
WRITE = 4
|
WRITE = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
int flag;
|
int flag = 0;
|
||||||
char data[COM_SIZE];
|
char data[COM_SIZE] = {0};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Action
|
struct Action
|
||||||
|
15
src/dude.cpp
15
src/dude.cpp
@ -18,11 +18,6 @@ Dude::Dude(const Coord &_pos, Map *_map, int &_team) :
|
|||||||
p_map->getPixel(m_pos).data.dudePtr = this;
|
p_map->getPixel(m_pos).data.dudePtr = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* Dude::getMemory()
|
|
||||||
{
|
|
||||||
return m_memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Dude::move(Dir d)
|
void Dude::move(Dir d)
|
||||||
{
|
{
|
||||||
p_map->getPixel(m_pos).type = m_under;
|
p_map->getPixel(m_pos).type = m_under;
|
||||||
@ -40,9 +35,15 @@ void Dude::move(Dir d)
|
|||||||
p_map->toreillerLoop(m_pos);
|
p_map->toreillerLoop(m_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dude::receiveComData(const Com &comData)
|
void Dude::receiveComData(Dir dir, const char *data)
|
||||||
{
|
{
|
||||||
std::memcpy(&m_comData, &comData, sizeof(Com));
|
m_com_data.flag = dir;
|
||||||
|
memcpy(m_com_data.data, data, COM_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Dude::update(BehaviorFunction func)
|
||||||
|
{
|
||||||
|
func(&m_action, m_memory, (Info*)this);
|
||||||
}
|
}
|
||||||
|
|
||||||
PixelType Dude::getNear(Dir d) const
|
PixelType Dude::getNear(Dir d) const
|
||||||
|
12
src/dude.h
12
src/dude.h
@ -15,22 +15,24 @@ private:
|
|||||||
PixelType m_under;
|
PixelType m_under;
|
||||||
int m_underResCount;
|
int m_underResCount;
|
||||||
char m_memory[DUDE_MEMORY_SIZE];
|
char m_memory[DUDE_MEMORY_SIZE];
|
||||||
Com m_comData;
|
Action m_action; // action containing output com data
|
||||||
|
Com m_com_data; // input com data
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dude(const Coord &_pos, Map *_map, int &_team);
|
Dude(const Coord &_pos, Map *_map, int &_team);
|
||||||
char* getMemory();
|
const Action& getAction() { return m_action; }
|
||||||
int getTeam(){ return m_team; }
|
int getTeam(){ return m_team; }
|
||||||
void setSuccess(bool success) { m_success = success; }
|
void setSuccess(bool success) { m_success = success; }
|
||||||
void setInventory(PixelType item) { m_inventory = item; }
|
void setInventory(PixelType item) { m_inventory = item; }
|
||||||
void trespass() { m_dead = true; }
|
void perish() { m_dead = true; }
|
||||||
const Coord& getPos() { return m_pos; }
|
const Coord& getPos() { return m_pos; }
|
||||||
void move(Dir d);
|
void move(Dir d);
|
||||||
void receiveComData(const Com &comData);
|
void receiveComData(Dir dir, const char *data);
|
||||||
|
void update(BehaviorFunction func);
|
||||||
|
|
||||||
virtual bool getSuccess() const { return m_success; }
|
virtual bool getSuccess() const { return m_success; }
|
||||||
virtual PixelType getInventory() const { return m_inventory; }
|
virtual PixelType getInventory() const { return m_inventory; }
|
||||||
virtual const Com& getCom() const { return m_comData; }
|
virtual const Com& getCom() const { return m_com_data; }
|
||||||
virtual PixelType getNear(Dir d) const;
|
virtual PixelType getNear(Dir d) const;
|
||||||
virtual int getInfo(Dir d) const;
|
virtual int getInfo(Dir d) const;
|
||||||
};
|
};
|
||||||
|
11
src/map.cpp
11
src/map.cpp
@ -22,14 +22,3 @@ Map::~Map()
|
|||||||
if(m_nbTeams)
|
if(m_nbTeams)
|
||||||
delete m_teams;
|
delete m_teams;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pixel::readBook(Com &msg)
|
|
||||||
{
|
|
||||||
//why the fuck +sizeof(int) ? u_u
|
|
||||||
memcpy(msg.data + sizeof(int), data.knowledge + (msg.flag | 3), COM_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pixel::writeBook(const Com &msg)
|
|
||||||
{
|
|
||||||
memcpy(data.knowledge + msg.flag, msg.data, COM_SIZE);
|
|
||||||
}
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
#ifndef MAP_H
|
#ifndef MAP_H
|
||||||
#define MAP_H
|
#define MAP_H
|
||||||
|
|
||||||
//#include "pixeltype.h"
|
|
||||||
//#include "coord.h"
|
|
||||||
#include "behavior.h"
|
#include "behavior.h"
|
||||||
|
|
||||||
class Dude;
|
class Dude;
|
||||||
@ -20,12 +18,6 @@ struct Pixel
|
|||||||
PixelData data;
|
PixelData data;
|
||||||
|
|
||||||
Pixel() : type(GRASS) {}
|
Pixel() : type(GRASS) {}
|
||||||
/* The two following function are used to read and write
|
|
||||||
* from book in a library.
|
|
||||||
* msg should be the incoming message which asked for reading/writing
|
|
||||||
*/
|
|
||||||
void readBook(Com &msg);
|
|
||||||
void writeBook(const Com &msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Map
|
class Map
|
||||||
|
@ -27,9 +27,8 @@ void Simulation::update()
|
|||||||
std::random_shuffle(m_dudes.begin(), m_dudes.end());
|
std::random_shuffle(m_dudes.begin(), m_dudes.end());
|
||||||
for (int i=0; i<m_dudes.size(); ++i){
|
for (int i=0; i<m_dudes.size(); ++i){
|
||||||
Dude *dude = m_dudes[i];
|
Dude *dude = m_dudes[i];
|
||||||
Action *action = m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team
|
m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team
|
||||||
handleAction(*action, dude);
|
handleAction(dude);
|
||||||
delete action;
|
|
||||||
}
|
}
|
||||||
// for each team, spawn dude if condition met
|
// for each team, spawn dude if condition met
|
||||||
for(int i=0; i<m_teams.size(); ++i){
|
for(int i=0; i<m_teams.size(); ++i){
|
||||||
@ -49,8 +48,9 @@ void Simulation::update()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Simulation::handleAction(const Action &action, Dude *dude)
|
void Simulation::handleAction(Dude *dude)
|
||||||
{
|
{
|
||||||
|
const Action &action = dude->getAction();
|
||||||
// initialisation
|
// initialisation
|
||||||
Coord currentPos(dude->getPos());
|
Coord currentPos(dude->getPos());
|
||||||
Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir));
|
Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir));
|
||||||
@ -182,37 +182,24 @@ void Simulation::handleAction(const Action &action, Dude *dude)
|
|||||||
switch(target.type){
|
switch(target.type){
|
||||||
case DUDE:
|
case DUDE:
|
||||||
{
|
{
|
||||||
Com msg(action.com_data);
|
|
||||||
msg.flag = (action.dir+2)%4; //show the source direction of the message
|
|
||||||
Dude *targetDude = target.data.dudePtr;
|
Dude *targetDude = target.data.dudePtr;
|
||||||
if(targetDude->getCom().data == NULL){
|
// TODO check conflicts between writers
|
||||||
targetDude->receiveComData(msg);
|
targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data);
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
}else
|
|
||||||
dude->setSuccess(false);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LIBRARY:
|
case LIBRARY:
|
||||||
|
{
|
||||||
|
int offset = (action.com_data.flag & 3)*COM_SIZE;
|
||||||
if(action.com_data.flag & Com::READ)
|
if(action.com_data.flag & Com::READ)
|
||||||
|
dude->receiveComData(action.dir, target.data.knowledge + offset);
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if(dude->getCom().data == NULL)
|
memcpy(target.data.knowledge + offset, action.com_data.data, COM_SIZE);
|
||||||
{
|
|
||||||
Com msg(action.com_data);
|
|
||||||
target.readBook(msg);
|
|
||||||
msg.flag = action.dir; // dude is receiving information from where he asked to read
|
|
||||||
dude->receiveComData(msg);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
dude->setSuccess(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
target.writeBook(action.com_data);
|
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ public:
|
|||||||
* @brief update runs one step of simulation
|
* @brief update runs one step of simulation
|
||||||
*/
|
*/
|
||||||
void update();
|
void update();
|
||||||
void handleAction(const Action &action, Dude* dude);
|
void handleAction(Dude* dude);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SIMULATION_H
|
#endif // SIMULATION_H
|
||||||
|
@ -37,9 +37,7 @@ void Team::destroySpawn(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Action* Team::update(Dude *dude)
|
void Team::update(Dude *dude)
|
||||||
{
|
{
|
||||||
Action* action = new Action();
|
dude->update(m_behavior);
|
||||||
m_behavior(action, dude->getMemory(),(Info*) dude);
|
|
||||||
return action;
|
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ public:
|
|||||||
void popDude();
|
void popDude();
|
||||||
void addFood() {m_foodQuantity++; }
|
void addFood() {m_foodQuantity++; }
|
||||||
void destroySpawn();
|
void destroySpawn();
|
||||||
Action* update(Dude*);
|
void update(Dude*);
|
||||||
glm::vec3 getSpawnColor() { return m_spawnColor; }
|
glm::vec3 getSpawnColor() { return m_spawnColor; }
|
||||||
glm::vec3 getDudeColor() { return m_dudeColor; }
|
glm::vec3 getDudeColor() { return m_dudeColor; }
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user