Update simulation.cpp

This commit is contained in:
Thomas Brandého 2016-05-19 14:02:02 +02:00
parent 42f646d02c
commit 3fbb83932b

View File

@ -64,7 +64,7 @@ void Simulation::handleAction(Action *action, Dude *dude){
else{
if(target.type == Pixel::SPAWN)
m_teams[target.data.nbRes].destroySpawn();
p_map->getPixel(targetPos).type = Pixel::GRASS;
target.type = Pixel::GRASS;
}
}
break;
@ -73,16 +73,17 @@ void Simulation::handleAction(Action *action, Dude *dude){
dude->setInventory(target.type);
target.data.nbRes--;
if (target.data.nbRes < 1){
p_map->getPixel(targetPos).type = Pixel::GRASS;
target.type = Pixel::GRASS;
// TODO: change color of target
}
dude->setSuccess(true);
}
break;
case Action::PUT:
dude->setSuccess(true);
if(dude->getInventory() != -1 && (target.type == Pixel::GRASS || target.type == Pixel::MARK || target.type == dude->getInventory())){
if(target.type == Pixel::GRASS || target.type == Pixel::MARK){
p_map->getPixel(targetPos).type = (Pixel::Type) dude->getInventory();
target.type = (Pixel::Type) dude->getInventory();
target.data.nbRes = 1;
}else
target.data.nbRes++;
@ -91,92 +92,71 @@ void Simulation::handleAction(Action *action, Dude *dude){
}else if(target.type == Pixel::SPAWN && dude->getInventory() == Pixel::FOOD){
dude->setInventory(-1);
m_teams[target.data.nbRes].addFood();
}//else{
}else{
// printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
// dude->success = 0;
//}
dude->setSuccess(false);
}
break;
case Action::WORK:
dude->setSuccess(true);
switch(target.type){
case Pixel::ROCK:
target.type = Pixel::STONE;
target.data.nbRes = 1;
break;
case Pixel::BERRIES:
target.type = Pixel::FOOD;
target.data.nbRes = 1;
break;
case Pixel::TREE:
target.type = Pixel::WOOD;
target.data.nbRes = 1;
break;
case Pixel::IRON_ORE:
target.type = Pixel::IRON;
target.data.nbRes = 1;
break;
case Pixel::GRASS:
target.type = Pixel::MARK;
break;
case Pixel::MARK:
target.type = Pixel::GRASS;
break;
case Pixel::WOOD:
switch(target.data.nbRes){
case 1:
target.type = Pixel::WALL;
break;
case 2:
target.type = Pixel::LIBRARY;
//TODO : allocate 128 byte in data ?
break;
default:
dude->setSuccess(true);
break;
}
case Pixel::STONE:
if(target.data.nbRes == 1)
target.type = Pixel::ROAD;
else
dude->setSuccess(false);
break;
case Pixel::IRON:
if(target.data.nbRes == 1)
target.type = Pixel::SWORD;
else
dude->setSuccess(false);
break;
default:
dude->setSuccess(false);
break;
}
// TODO : update pixel
break;
}
}
/*
case WORK :
dude->success = 1;
switch(target.type){
case ROCK :
map[target_pos.x][target_pos.y].type = STONE;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case BERRIES :
map[target_pos.x][target_pos.y].type = FOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case TREE :
map[target_pos.x][target_pos.y].type = WOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case IRON_ORE :
map[target_pos.x][target_pos.y].type = IRON;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case GRASS :
map[target_pos.x][target_pos.y].type = MARK;
map[target_pos.x][target_pos.y].data = NULL;
break;
case MARK :
map[target_pos.x][target_pos.y].type = GRASS;
map[target_pos.x][target_pos.y].data = NULL;
break;
case WOOD :
nb_res = target.data;
switch(*nb_res){
case 1 :
free(target.data);
map[target_pos.x][target_pos.y].type = WALL;
map[target_pos.x][target_pos.y].data = NULL;
break;
case 2 :
free(target.data);
map[target_pos.x][target_pos.y].type = LIBRARY;
map[target_pos.x][target_pos.y].data = malloc(128);
memset(map[target_pos.x][target_pos.y].data, 0, 128);
default :
dude->success = 0;
break;
}
break;
case STONE :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else{
free(target.data);
map[target_pos.x][target_pos.y].type = ROAD;
map[target_pos.x][target_pos.y].data = NULL;
}
break;
case IRON :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else
map[target_pos.x][target_pos.y].type = SWORD;
break;
default :
dude->success = 0;
break;
}
if(dude->success)
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
break;
case WAIT :
dude->success = 1;
break;