implemented COMMUNICATE

This commit is contained in:
Anselme 2016-05-30 17:20:37 +02:00
parent 43eb4c48a7
commit 2422cbe873
9 changed files with 34 additions and 65 deletions

View File

@ -22,8 +22,8 @@ struct Com
WRITE = 4
};
int flag;
char data[COM_SIZE];
int flag = 0;
char data[COM_SIZE] = {0};
};
struct Action

View File

@ -18,11 +18,6 @@ Dude::Dude(const Coord &_pos, Map *_map, int &_team) :
p_map->getPixel(m_pos).data.dudePtr = this;
}
char* Dude::getMemory()
{
return m_memory;
}
void Dude::move(Dir d)
{
p_map->getPixel(m_pos).type = m_under;
@ -40,9 +35,15 @@ void Dude::move(Dir d)
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

View File

@ -15,22 +15,24 @@ private:
PixelType m_under;
int m_underResCount;
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:
Dude(const Coord &_pos, Map *_map, int &_team);
char* getMemory();
const Action& getAction() { return m_action; }
int getTeam(){ return m_team; }
void setSuccess(bool success) { m_success = success; }
void setInventory(PixelType item) { m_inventory = item; }
void trespass() { m_dead = true; }
void perish() { m_dead = true; }
const Coord& getPos() { return m_pos; }
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 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 int getInfo(Dir d) const;
};

View File

@ -22,14 +22,3 @@ Map::~Map()
if(m_nbTeams)
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);
}

View File

@ -1,8 +1,6 @@
#ifndef MAP_H
#define MAP_H
//#include "pixeltype.h"
//#include "coord.h"
#include "behavior.h"
class Dude;
@ -20,12 +18,6 @@ struct Pixel
PixelData data;
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

View File

@ -27,9 +27,8 @@ void Simulation::update()
std::random_shuffle(m_dudes.begin(), m_dudes.end());
for (int i=0; i<m_dudes.size(); ++i){
Dude *dude = m_dudes[i];
Action *action = m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team
handleAction(*action, dude);
delete action;
m_teams[dude->getTeam()].update(dude); //get action for this dude from behavior function in team
handleAction(dude);
}
// for each team, spawn dude if condition met
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
Coord currentPos(dude->getPos());
Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir));
@ -182,37 +182,24 @@ void Simulation::handleAction(const Action &action, Dude *dude)
switch(target.type){
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;
if(targetDude->getCom().data == NULL){
targetDude->receiveComData(msg);
// TODO check conflicts between writers
targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data);
dude->setSuccess(true);
}else
dude->setSuccess(false);
break;
}
case LIBRARY:
{
int offset = (action.com_data.flag & 3)*COM_SIZE;
if(action.com_data.flag & Com::READ)
{
if(dude->getCom().data == NULL)
{
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);
}
dude->receiveComData(action.dir, target.data.knowledge + offset);
else
{
dude->setSuccess(false);
break;
}
}
else{
target.writeBook(action.com_data);
memcpy(target.data.knowledge + offset, action.com_data.data, COM_SIZE);
dude->setSuccess(true);
}
break;
}
default:
break;
}

View File

@ -22,7 +22,7 @@ public:
* @brief update runs one step of simulation
*/
void update();
void handleAction(const Action &action, Dude* dude);
void handleAction(Dude* dude);
};
#endif // SIMULATION_H

View File

@ -37,9 +37,7 @@ void Team::destroySpawn(){
}
Action* Team::update(Dude *dude)
void Team::update(Dude *dude)
{
Action* action = new Action();
m_behavior(action, dude->getMemory(),(Info*) dude);
return action;
dude->update(m_behavior);
}

View File

@ -27,7 +27,7 @@ public:
void popDude();
void addFood() {m_foodQuantity++; }
void destroySpawn();
Action* update(Dude*);
void update(Dude*);
glm::vec3 getSpawnColor() { return m_spawnColor; }
glm::vec3 getDudeColor() { return m_dudeColor; }
};