From 8deedd9098c1cae77ca036de9b29d7199e14a130 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sat, 4 Jun 2016 03:04:46 +0200 Subject: [PATCH] added basic handling for communication conflict --- src/dude.cpp | 1 + src/dude.h | 16 ++++++++++++---- src/simulation.cpp | 16 +++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/dude.cpp b/src/dude.cpp index 0f2c853..d3aad47 100644 --- a/src/dude.cpp +++ b/src/dude.cpp @@ -49,6 +49,7 @@ void Dude::update(BehaviorFunction func) { func(&m_action, m_memory, (Info*)this); m_receivedComData = false; + memcpy(m_com_data.data, 0, COM_SIZE); } PixelType Dude::getNear(Dir d) const diff --git a/src/dude.h b/src/dude.h index bc2f93e..d3cf802 100644 --- a/src/dude.h +++ b/src/dude.h @@ -21,22 +21,30 @@ private: public: Dude(const Coord &_pos, Map *_map, int &_team); + //general use functions + void update(BehaviorFunction func); 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; } + + //life-related function bool isAlive() { return !m_dead; } void perish() { m_dead = true; } + + //movement-related function const Coord& getPos() { return m_pos; } void move(Dir d); - void receiveComData(Dir dir, const char *data); - void update(BehaviorFunction func); + //function inherited from Info virtual bool getSuccess() const { return m_success; } virtual PixelType getInventory() const { return m_inventory; } virtual const Com* getCom() const { return m_receivedComData ? &m_com_data : nullptr; } virtual PixelType getNear(Dir d) const; virtual int getInfo(Dir d) const; + + // setter for the variable returned by functions of Info + void setSuccess(bool success) { m_success = success; } + void setInventory(PixelType item) { m_inventory = item; } + void receiveComData(Dir dir, const char *data); }; #endif // DUDE_H diff --git a/src/simulation.cpp b/src/simulation.cpp index 01252da..bc7b409 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -195,17 +195,23 @@ void Simulation::handleAction(Dude *dude) { Dude *targetDude = target.data.dudePtr; // TODO check conflicts between writers - targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data); - dude->setSuccess(true); + if(targetDude->getCom() == nullptr){ + 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) - dude->receiveComData(action.dir, target.data.knowledge + offset); - else - { + if(dude->getCom() == nullptr){ + dude->receiveComData(action.dir, target.data.knowledge + offset); + dude->setSuccess(true); + }else + dude->setSuccess(false); + else{ memcpy(target.data.knowledge + offset, action.com_data.data, COM_SIZE); dude->setSuccess(true); }