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

View File

@ -45,6 +45,10 @@ struct Coord
{ return Coord(x-c.x, y-c.y); } { return Coord(x-c.x, y-c.y); }
Coord& operator-=(const Coord &c) Coord& operator-=(const Coord &c)
{ x-=c.x; y-=c.y; return *this; } { 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 Coord operator*(const Coord &c) const
{ return Coord(x*c.x, y*c.y); } { return Coord(x*c.x, y*c.y); }
Coord& operator*=(const Coord &c) 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); std::memset(&m_memory, 0, DUDE_MEMORY_SIZE);
m_under = p_map->getPixel(m_pos).type; 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; m_underResCount = p_map->getPixel(m_pos).data.nbRes;
p_map->getPixel(m_pos).type = DUDE; p_map->getPixel(m_pos).type = DUDE;
p_map->getPixel(m_pos).data.dudePtr = this; p_map->getPixel(m_pos).data.dudePtr = this;
@ -26,14 +26,14 @@ char* Dude::getMemory()
void Dude::move(Dir d) void Dude::move(Dir d)
{ {
p_map->getPixel(m_pos).type = m_under; 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.nbRes = m_underResCount;
p_map->getPixel(m_pos).data.dudePtr = NULL; p_map->getPixel(m_pos).data.dudePtr = NULL;
m_pos += Coord(d); m_pos += Coord(d);
m_under = p_map->getPixel(m_pos).type; 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; m_underResCount = p_map->getPixel(m_pos).data.nbRes;
p_map->getPixel(m_pos).type = DUDE; p_map->getPixel(m_pos).type = DUDE;
p_map->getPixel(m_pos).data.dudePtr = this; 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 EMPTY = -1
}; };
//we might have to place these function elsewhere ? struct PixelProperty
bool isWalkable(PixelType target); {
bool isDestroyable(PixelType target); //we might have to place these function elsewhere ?
bool isResource(PixelType target); 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 #endif // PIXELTYPE_H

View File

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