diff --git a/src/map.cpp b/src/map.cpp index 12692cd..e14baba 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1,5 +1,6 @@ #include "map.h" #include +#include "string.h" Map::Map(int nbTeams, int width, int height) : m_width(width), @@ -21,3 +22,14 @@ 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 ab47b65..4ecfdea 100644 --- a/src/map.h +++ b/src/map.h @@ -1,8 +1,9 @@ #ifndef MAP_H #define MAP_H -#include "pixeltype.h" -#include "coord.h" +//#include "pixeltype.h" +//#include "coord.h" +#include "behavior.h" class Dude; @@ -19,6 +20,12 @@ 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 @@ -69,7 +76,7 @@ public: { // this is the shader implementation of the toreiller Coord nbRevolutions(c.x/m_width, c.y/m_height); - if(abs(nbRevolutions.y % 2)) + if(std::abs(nbRevolutions.y % 2)) { c.x += m_width/2; nbRevolutions.x = c.x/m_width; diff --git a/src/pixeltype.h b/src/pixeltype.h index 1f04080..ceb0a97 100644 --- a/src/pixeltype.h +++ b/src/pixeltype.h @@ -11,17 +11,11 @@ enum PixelType { struct PixelProperty { - //we might have to place these function elsewhere ? static bool isWalkable(PixelType target) { - return target != WALL - && target != ROCK - && target != BEDROCK - && target != IRON_ORE - && target != TREE - && target != LIBRARY - && target != DUDE - && target != SPAWN; + return target == GRASS + || target == ROAD + || target == MARK; } static bool isDestructible(PixelType target) diff --git a/src/simulation.cpp b/src/simulation.cpp index fd71e5f..ac948ca 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -37,22 +37,23 @@ void Simulation::update() if(t.updateSpawn()) { Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); + std::cout << p_map->getPixel(spawnPos).type << std::endl; if(PixelProperty::isWalkable(p_map->getPixel(spawnPos).type)) { Dude *dude = new Dude(spawnPos,p_map,i); (*p_map)[spawnPos].data.dudePtr = dude; p_map->updatePixel(spawnPos); m_dudes.push_back(dude); - }/* + } else - // TODO delay the spawning - */ + std::cout << "Unpopable" << std::endl; + // TODO check other case or delay the spawning } } } -void Simulation::handleAction(const Action &action, Dude *dude){ - +void Simulation::handleAction(const Action &action, Dude *dude) +{ // initialisation Coord currentPos(dude->getPos()); Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir)); @@ -85,7 +86,7 @@ void Simulation::handleAction(const Action &action, Dude *dude){ } break; case Action::PICK: // DONE - if(PixelProperty::isResource(target.type) && dude->getInventory() == -1){ + if(PixelProperty::isResource(target.type) && dude->getInventory() == EMPTY){ dude->setInventory(target.type); --target.data.nbRes; if(target.data.nbRes < 1) @@ -97,13 +98,13 @@ void Simulation::handleAction(const Action &action, Dude *dude){ } break; case Action::PUT: // DONE - if(dude->getInventory() != -1 + if(dude->getInventory() != EMPTY && (target.type == GRASS || target.type == MARK || target.type == dude->getInventory()) && target.data.nbRes < MAX_RESOURCES_IN_STACK) { if(target.type == GRASS || target.type == MARK) { - target.type = PixelType(dude->getInventory()); + target.type = dude->getInventory(); target.data.nbRes = 1; p_map->updatePixel(targetPos); } @@ -184,10 +185,11 @@ void Simulation::handleAction(const Action &action, Dude *dude){ switch(target.type){ case DUDE: { - // WTF ? -> action.com_data.flag = (action.dir+2)%4; + 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(action.com_data); + targetDude->receiveComData(msg); dude->setSuccess(true); }else dude->setSuccess(false); @@ -198,10 +200,10 @@ void Simulation::handleAction(const Action &action, Dude *dude){ { if(dude->getCom().data == NULL) { - // WTF ? -> action.com_data.flag = action.dir; - dude->receiveComData(action.com_data); - // TODO: understand what the fuck this line is doing - //memcpy(dude->getCom() + sizeof(int), target.data.knowledge + (action->com_data.flag | 3), 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 { @@ -210,6 +212,7 @@ void Simulation::handleAction(const Action &action, Dude *dude){ } } else{ + target.writeBook(action.com_data); // TODO: understand what the fuck this line is doing bis //memcpy(target.data.knowledge + action->com_data.flag, action->com_data.data, COM_SIZE); dude->setSuccess(true);