debugged simple behavior, finshed verifying MOVE, PICK, PUT and WORK actions

This commit is contained in:
Anselme 2016-05-27 13:38:01 +02:00
parent 67c18703a1
commit ee95e2cfb0
7 changed files with 94 additions and 67 deletions

View File

@ -6,5 +6,5 @@
extern "C" void think(Action *action, char *memory, const Info *info)
{
action->type = Action::MOVE; // the Dude will move
action->dir = Dir(0);//std::rand() % 4 ); // the direction of the movement will be random
action->dir = Dir(std::rand() % 4 ); // the direction of the movement will be random
}

View File

@ -5,14 +5,14 @@
// inspired of the old "purple.c" behavior
typedef struct{
struct purple_data{
Coord pos;
bool new_born;
bool tried;
bool brings_food;
Dir last_dir;
Action::Type last_action;
} purple_data;
};
extern "C" void think(Action *action, char *memory, const Info *info)
{
@ -25,6 +25,15 @@ extern "C" void think(Action *action, char *memory, const Info *info)
if(!data->new_born){
success = false;
data->new_born = true;
for(i=0; i<4; i++)
{
type = info->getNear(Dir(i));
if(type == SPAWN)
{
data->pos = Coord(0) - Dir(i);
break;
}
}
}
if(data->last_action == Action::MOVE){
@ -39,9 +48,9 @@ extern "C" void think(Action *action, char *memory, const Info *info)
if(data->brings_food){
int distance = data->pos.dist();
if(distance == 1){
action->type = Action::WAIT;
action->type = Action::MOVE;
action->dir = NORTH;
for(i=0; i<4; i++){
type = info->getNear(Dir(i));
if(type == SPAWN){
@ -85,12 +94,7 @@ extern "C" void think(Action *action, char *memory, const Info *info)
do{
action->dir = Dir((data->last_dir + rand()%3)%4);
type = info->getNear(action->dir);
}while(type == WALL
&& type == ROCK
&& type == BEDROCK
&& type == IRON_ORE
&& type == TREE
&& type == LIBRARY);
}while(!PixelProperty::isWalkable(type));
data->last_dir = action->dir;
data->last_action = action->type;

View File

@ -45,6 +45,10 @@ struct Coord
{ return Coord(x-c.x, y-c.y); }
Coord& operator-=(const Coord &c)
{ x-=c.x; y-=c.y; return *this; }
Coord operator-(Dir d) const
{ return *this - Coord(d); }
Coord& operator-=(Dir d)
{ return *this -= Coord(d); }
Coord operator*(const Coord &c) const
{ return Coord(x*c.x, y*c.y); }
Coord& operator*=(const Coord &c)

View File

@ -12,7 +12,7 @@ Dude::Dude(const Coord &_pos, Map *_map, int &_team) :
{
std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
m_under = p_map->getPixel(m_pos).type;
if(isResource(m_under))
if(PixelProperty::isResource(m_under))
m_underResCount = p_map->getPixel(m_pos).data.nbRes;
p_map->getPixel(m_pos).type = DUDE;
p_map->getPixel(m_pos).data.dudePtr = this;
@ -26,14 +26,14 @@ char* Dude::getMemory()
void Dude::move(Dir d)
{
p_map->getPixel(m_pos).type = m_under;
if(isResource(m_under))
if(PixelProperty::isResource(m_under))
p_map->getPixel(m_pos).data.nbRes = m_underResCount;
p_map->getPixel(m_pos).data.dudePtr = NULL;
m_pos += Coord(d);
m_under = p_map->getPixel(m_pos).type;
if(isResource(m_under))
if(PixelProperty::isResource(m_under))
m_underResCount = p_map->getPixel(m_pos).data.nbRes;
p_map->getPixel(m_pos).type = DUDE;
p_map->getPixel(m_pos).data.dudePtr = this;

View File

@ -1,22 +0,0 @@
#include "pixeltype.h"
bool isWalkable(PixelType target){
return target != WALL && target != ROCK && target != BEDROCK
&& target != IRON_ORE && target != TREE && target != LIBRARY
&& target != DUDE && target != SPAWN;
}
bool isResource(PixelType target)
{
return target == FOOD
|| target == WOOD
|| target == STONE
|| target == IRON
|| target == SWORD;
}
bool isDestroyable(PixelType target){
return (target == DUDE || target == SPAWN || target == WALL
|| target == LIBRARY || target == ROAD);
}

View File

@ -9,9 +9,38 @@ enum PixelType {
EMPTY = -1
};
//we might have to place these function elsewhere ?
bool isWalkable(PixelType target);
bool isDestroyable(PixelType target);
bool isResource(PixelType target);
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;
}
static bool isDestructible(PixelType target)
{
return target == DUDE
|| target == SPAWN
|| target == WALL
|| target == LIBRARY
|| target == ROAD;
}
static bool isResource(PixelType target)
{
return target == FOOD
|| target == WOOD
|| target == STONE
|| target == IRON
|| target == SWORD;
}
};
#endif // PIXELTYPE_H

View File

@ -7,6 +7,8 @@
#include "mapscene.h"
#include "pixeltype.h"
#define MAX_RESOURCES_IN_STACK 5
Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors):
p_map(_map)
{
@ -35,7 +37,7 @@ void Simulation::update()
if(t.updateSpawn())
{
Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4));
if(isWalkable(p_map->getPixel(spawnPos).type))
if(PixelProperty::isWalkable(p_map->getPixel(spawnPos).type))
{
Dude *dude = new Dude(spawnPos,p_map,i);
(*p_map)[spawnPos].data.dudePtr = dude;
@ -60,7 +62,7 @@ void Simulation::handleAction(const Action &action, Dude *dude){
switch(action.type){
case Action::MOVE: // DONE
if(isWalkable(target.type)){
if(PixelProperty::isWalkable(target.type)){
dude->move(action.dir);
p_map->updatePixel(currentPos);
p_map->updatePixel(targetPos);
@ -68,48 +70,57 @@ void Simulation::handleAction(const Action &action, Dude *dude){
}
break;
case Action::ATTACK:
if (isDestroyable(target.type))
if(PixelProperty::isDestructible(target.type))
{
dude->setSuccess(true);
if (target.type == DUDE)
NULL; // TODO: add fight between dude and targetDude
else{
if (target.type == DUDE) // TODO: add fight between dude and targetDude
NULL;
else // DONE
{
if(target.type == SPAWN)
m_teams[target.data.nbRes].destroySpawn();
target.type = GRASS;
p_map->updatePixel(targetPos);
}
}
break;
case Action::PICK:
if(target.type >= FOOD && target.type <= SWORD && dude->getInventory() == -1){
case Action::PICK: // DONE
if(PixelProperty::isResource(target.type) && dude->getInventory() == -1){
dude->setInventory(target.type);
target.data.nbRes--;
if (target.data.nbRes < 1){
--target.data.nbRes;
if(target.data.nbRes < 1)
{
target.type = GRASS;
// TODO: change color of target
p_map->updatePixel(targetPos);
}
dude->setSuccess(true);
}
break;
case Action::PUT:
dude->setSuccess(true);
if(dude->getInventory() != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->getInventory())){
if(target.type == GRASS || target.type == MARK){
case Action::PUT: // DONE
if(dude->getInventory() != -1
&& (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.data.nbRes = 1;
}else
target.data.nbRes++;
p_map->updatePixel(targetPos);
}
else
++target.data.nbRes;
dude->setInventory(EMPTY);
// TODO: change pixel to new type
}else if(target.type == SPAWN && dude->getInventory() == FOOD){
dude->setSuccess(true);
}
else if(target.type == SPAWN && dude->getInventory() == FOOD)
{
dude->setInventory(EMPTY);
m_teams[target.data.nbRes].addFood();
}else{
// printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
dude->setSuccess(false);
dude->setSuccess(true);
}
p_map->updatePixel(targetPos);
break;
case Action::WORK:
case Action::WORK: // DONE
dude->setSuccess(true);
switch(target.type){
case ROCK:
@ -141,10 +152,10 @@ void Simulation::handleAction(const Action &action, Dude *dude){
break;
case 2:
target.type = LIBRARY;
//TODO : allocate 128 byte in data ?
target.data.knowledge = new char[LIBRARY_SIZE];
break;
default:
dude->setSuccess(true);
dude->setSuccess(false);
break;
}
case STONE:
@ -163,9 +174,10 @@ void Simulation::handleAction(const Action &action, Dude *dude){
dude->setSuccess(false);
break;
}
// TODO : update pixel
if(dude->getSuccess())
p_map->updatePixel(targetPos);
break;
case Action::WAIT:
case Action::WAIT: // DONE
dude->setSuccess(true);
break;
case Action::COMMUNICATE: