added function for manipulating book
This commit is contained in:
parent
e2178df9ea
commit
ff64c7b780
12
src/map.cpp
12
src/map.cpp
@ -1,5 +1,6 @@
|
||||
#include "map.h"
|
||||
#include <glm/vec3.hpp>
|
||||
#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);
|
||||
}
|
||||
|
13
src/map.h
13
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;
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user