added basic handling for communication conflict

This commit is contained in:
Lendemor 2016-06-04 03:04:46 +02:00
parent 7ddde5e2ec
commit 8deedd9098
3 changed files with 24 additions and 9 deletions

View File

@ -49,6 +49,7 @@ void Dude::update(BehaviorFunction func)
{ {
func(&m_action, m_memory, (Info*)this); func(&m_action, m_memory, (Info*)this);
m_receivedComData = false; m_receivedComData = false;
memcpy(m_com_data.data, 0, COM_SIZE);
} }
PixelType Dude::getNear(Dir d) const PixelType Dude::getNear(Dir d) const

View File

@ -21,22 +21,30 @@ private:
public: public:
Dude(const Coord &_pos, Map *_map, int &_team); Dude(const Coord &_pos, Map *_map, int &_team);
//general use functions
void update(BehaviorFunction func);
const Action& getAction() { return m_action; } const Action& getAction() { return m_action; }
int getTeam(){ return m_team; } 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; } bool isAlive() { return !m_dead; }
void perish() { m_dead = true; } void perish() { m_dead = true; }
//movement-related function
const Coord& getPos() { return m_pos; } const Coord& getPos() { return m_pos; }
void move(Dir d); 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 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_receivedComData ? &m_com_data : nullptr; } virtual const Com* getCom() const { return m_receivedComData ? &m_com_data : nullptr; }
virtual PixelType getNear(Dir d) const; virtual PixelType getNear(Dir d) const;
virtual int getInfo(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 #endif // DUDE_H

View File

@ -195,17 +195,23 @@ void Simulation::handleAction(Dude *dude)
{ {
Dude *targetDude = target.data.dudePtr; Dude *targetDude = target.data.dudePtr;
// TODO check conflicts between writers // TODO check conflicts between writers
targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data); if(targetDude->getCom() == nullptr){
dude->setSuccess(true); targetDude->receiveComData(Dir((action.dir+2)%4), targetDude->getAction().com_data.data);
dude->setSuccess(true);
}else
dude->setSuccess(false);
break; break;
} }
case LIBRARY: case LIBRARY:
{ {
int offset = (action.com_data.flag & 3)*COM_SIZE; 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); if(dude->getCom() == nullptr){
else 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); memcpy(target.data.knowledge + offset, action.com_data.data, COM_SIZE);
dude->setSuccess(true); dude->setSuccess(true);
} }