From 2422cbe873fd4d799ff254fd51742a1a879ee302 Mon Sep 17 00:00:00 2001 From: Anselme Date: Mon, 30 May 2016 17:20:37 +0200 Subject: [PATCH] implemented COMMUNICATE --- src/behavior.h | 4 ++-- src/dude.cpp | 15 ++++++++------- src/dude.h | 12 +++++++----- src/map.cpp | 11 ----------- src/map.h | 8 -------- src/simulation.cpp | 39 +++++++++++++-------------------------- src/simulation.h | 2 +- src/team.cpp | 6 ++---- src/team.h | 2 +- 9 files changed, 34 insertions(+), 65 deletions(-) diff --git a/src/behavior.h b/src/behavior.h index 6db5bdb..67bdb7f 100644 --- a/src/behavior.h +++ b/src/behavior.h @@ -22,8 +22,8 @@ struct Com WRITE = 4 }; - int flag; - char data[COM_SIZE]; + int flag = 0; + char data[COM_SIZE] = {0}; }; struct Action diff --git a/src/dude.cpp b/src/dude.cpp index 768b865..c25e4b9 100644 --- a/src/dude.cpp +++ b/src/dude.cpp @@ -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 diff --git a/src/dude.h b/src/dude.h index 8515eb1..03124c2 100644 --- a/src/dude.h +++ b/src/dude.h @@ -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; }; diff --git a/src/map.cpp b/src/map.cpp index fd737eb..ededf23 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -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); -} diff --git a/src/map.h b/src/map.h index 4ecfdea..44f4dc0 100644 --- a/src/map.h +++ b/src/map.h @@ -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 diff --git a/src/simulation.cpp b/src/simulation.cpp index a2916e8..966f2bb 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -27,9 +27,8 @@ void Simulation::update() std::random_shuffle(m_dudes.begin(), m_dudes.end()); for (int i=0; igetTeam()].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; igetAction(); // 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); - dude->setSuccess(true); - }else - dude->setSuccess(false); + // TODO check conflicts between writers + targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data); + dude->setSuccess(true); break; } case LIBRARY: + { + int offset = (action.com_data.flag & 3)*COM_SIZE; if(action.com_data.flag & Com::READ) + dude->receiveComData(action.dir, target.data.knowledge + offset); + else { - 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); - } - 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; } diff --git a/src/simulation.h b/src/simulation.h index 6ae42a1..804fbd5 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -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 diff --git a/src/team.cpp b/src/team.cpp index 0302f48..b21da5b 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -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); } diff --git a/src/team.h b/src/team.h index 0b94f01..1816336 100644 --- a/src/team.h +++ b/src/team.h @@ -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; } };