added function for manipulating book

This commit is contained in:
Lendemor 2016-05-27 22:03:47 +02:00
parent e2178df9ea
commit ff64c7b780
4 changed files with 42 additions and 26 deletions

View File

@ -1,5 +1,6 @@
#include "map.h" #include "map.h"
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "string.h"
Map::Map(int nbTeams, int width, int height) : Map::Map(int nbTeams, int width, int height) :
m_width(width), m_width(width),
@ -21,3 +22,14 @@ Map::~Map()
if(m_nbTeams) if(m_nbTeams)
delete m_teams; 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,9 @@
#ifndef MAP_H #ifndef MAP_H
#define MAP_H #define MAP_H
#include "pixeltype.h" //#include "pixeltype.h"
#include "coord.h" //#include "coord.h"
#include "behavior.h"
class Dude; class Dude;
@ -19,6 +20,12 @@ struct Pixel
PixelData data; PixelData data;
Pixel() : type(GRASS) {} 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 class Map
@ -69,7 +76,7 @@ public:
{ {
// this is the shader implementation of the toreiller // this is the shader implementation of the toreiller
Coord nbRevolutions(c.x/m_width, c.y/m_height); 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; c.x += m_width/2;
nbRevolutions.x = c.x/m_width; nbRevolutions.x = c.x/m_width;

View File

@ -11,17 +11,11 @@ enum PixelType {
struct PixelProperty struct PixelProperty
{ {
//we might have to place these function elsewhere ?
static bool isWalkable(PixelType target) static bool isWalkable(PixelType target)
{ {
return target != WALL return target == GRASS
&& target != ROCK || target == ROAD
&& target != BEDROCK || target == MARK;
&& target != IRON_ORE
&& target != TREE
&& target != LIBRARY
&& target != DUDE
&& target != SPAWN;
} }
static bool isDestructible(PixelType target) static bool isDestructible(PixelType target)

View File

@ -37,22 +37,23 @@ void Simulation::update()
if(t.updateSpawn()) if(t.updateSpawn())
{ {
Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); 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)) if(PixelProperty::isWalkable(p_map->getPixel(spawnPos).type))
{ {
Dude *dude = new Dude(spawnPos,p_map,i); Dude *dude = new Dude(spawnPos,p_map,i);
(*p_map)[spawnPos].data.dudePtr = dude; (*p_map)[spawnPos].data.dudePtr = dude;
p_map->updatePixel(spawnPos); p_map->updatePixel(spawnPos);
m_dudes.push_back(dude); m_dudes.push_back(dude);
}/* }
else 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 // initialisation
Coord currentPos(dude->getPos()); Coord currentPos(dude->getPos());
Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir)); Coord targetPos = p_map->toreillerLoop(currentPos + Coord(action.dir));
@ -85,7 +86,7 @@ void Simulation::handleAction(const Action &action, Dude *dude){
} }
break; break;
case Action::PICK: // DONE case Action::PICK: // DONE
if(PixelProperty::isResource(target.type) && dude->getInventory() == -1){ if(PixelProperty::isResource(target.type) && dude->getInventory() == EMPTY){
dude->setInventory(target.type); dude->setInventory(target.type);
--target.data.nbRes; --target.data.nbRes;
if(target.data.nbRes < 1) if(target.data.nbRes < 1)
@ -97,13 +98,13 @@ void Simulation::handleAction(const Action &action, Dude *dude){
} }
break; break;
case Action::PUT: // DONE case Action::PUT: // DONE
if(dude->getInventory() != -1 if(dude->getInventory() != EMPTY
&& (target.type == GRASS || target.type == MARK || target.type == dude->getInventory()) && (target.type == GRASS || target.type == MARK || target.type == dude->getInventory())
&& target.data.nbRes < MAX_RESOURCES_IN_STACK) && target.data.nbRes < MAX_RESOURCES_IN_STACK)
{ {
if(target.type == GRASS || target.type == MARK) if(target.type == GRASS || target.type == MARK)
{ {
target.type = PixelType(dude->getInventory()); target.type = dude->getInventory();
target.data.nbRes = 1; target.data.nbRes = 1;
p_map->updatePixel(targetPos); p_map->updatePixel(targetPos);
} }
@ -184,10 +185,11 @@ void Simulation::handleAction(const Action &action, Dude *dude){
switch(target.type){ switch(target.type){
case DUDE: 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; Dude *targetDude = target.data.dudePtr;
if(targetDude->getCom().data == NULL){ if(targetDude->getCom().data == NULL){
targetDude->receiveComData(action.com_data); targetDude->receiveComData(msg);
dude->setSuccess(true); dude->setSuccess(true);
}else }else
dude->setSuccess(false); dude->setSuccess(false);
@ -198,10 +200,10 @@ void Simulation::handleAction(const Action &action, Dude *dude){
{ {
if(dude->getCom().data == NULL) if(dude->getCom().data == NULL)
{ {
// WTF ? -> action.com_data.flag = action.dir; Com msg(action.com_data);
dude->receiveComData(action.com_data); target.readBook(msg);
// TODO: understand what the fuck this line is doing msg.flag = action.dir; // dude is receiving information from where he asked to read
//memcpy(dude->getCom() + sizeof(int), target.data.knowledge + (action->com_data.flag | 3), COM_SIZE); dude->receiveComData(msg);
} }
else else
{ {
@ -210,6 +212,7 @@ void Simulation::handleAction(const Action &action, Dude *dude){
} }
} }
else{ else{
target.writeBook(action.com_data);
// TODO: understand what the fuck this line is doing bis // TODO: understand what the fuck this line is doing bis
//memcpy(target.data.knowledge + action->com_data.flag, action->com_data.data, COM_SIZE); //memcpy(target.data.knowledge + action->com_data.flag, action->com_data.data, COM_SIZE);
dude->setSuccess(true); dude->setSuccess(true);