117 lines
2.5 KiB
C++
117 lines
2.5 KiB
C++
#include <cstdlib>
|
|
#include "behavior.h"
|
|
|
|
// g++ -shared simple.cpp -o simple.dll -std=c++11 -I../src
|
|
|
|
// inspired of the old "purple.c" behavior
|
|
|
|
struct purple_data{
|
|
Coord pos;
|
|
bool new_born;
|
|
bool tried;
|
|
bool brings_food;
|
|
Dir last_dir;
|
|
Action::Type last_action;
|
|
};
|
|
|
|
extern "C" void debugOutput(char *outputString, const char *memory)
|
|
{
|
|
purple_data* data = (purple_data*)memory;
|
|
sprintf(outputString, "Simple Dude\ndistance to the spawn : (%d, %d)\n%s\n",
|
|
data->pos.x, data->pos.y,
|
|
data->brings_food ? "Bringing food to spawn" : "searching for food");
|
|
}
|
|
|
|
extern "C" void think(Action *action, char *memory, const Info *info)
|
|
{
|
|
int i;
|
|
PixelType type;
|
|
|
|
bool success = info->getSuccess();
|
|
purple_data* data = (purple_data*)memory;
|
|
|
|
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){
|
|
if(success)
|
|
data->pos += data->last_dir;
|
|
}
|
|
|
|
if(data->tried && success)
|
|
data->brings_food = true;
|
|
data->tried = false;
|
|
|
|
if(data->brings_food){
|
|
int distance = data->pos.dist();
|
|
if(distance == 1){
|
|
action->type = Action::MOVE;
|
|
action->dir = NORTH;
|
|
|
|
for(i=0; i<4; i++){
|
|
type = info->getNear(Dir(i));
|
|
if(type == SPAWN){
|
|
action->dir = Dir(i);
|
|
action->type = Action::PUT;
|
|
data->brings_food = false;
|
|
break;
|
|
}
|
|
}
|
|
}else{
|
|
action->type = Action::MOVE;
|
|
do{
|
|
action->dir = Dir( rand() % 4 );
|
|
}while(~(data->pos + action->dir) > distance && distance != 0);
|
|
|
|
}
|
|
data->last_dir = action->dir;
|
|
data->last_action = action->type;
|
|
return;
|
|
}
|
|
|
|
for(i=0; i<4; i++){
|
|
type = info->getNear(Dir(i));
|
|
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
|
|
action->type = Action::WORK;
|
|
action->dir = Dir(i);
|
|
data->last_dir = action->dir;
|
|
data->last_action = action->type;
|
|
return;
|
|
}else if(type == FOOD){
|
|
action->type = Action::PICK;
|
|
action->dir = Dir(i);
|
|
data->tried = true;
|
|
data->last_dir = action->dir;
|
|
data->last_action = action->type;
|
|
return;
|
|
}
|
|
}
|
|
|
|
int blockedCount = 0;
|
|
action->type = Action::MOVE;
|
|
do{
|
|
action->dir = Dir((data->last_dir + rand()%3)%4);
|
|
type = info->getNear(action->dir);
|
|
++blockedCount;
|
|
if(blockedCount > 3)
|
|
{
|
|
action->type = Action::WAIT;
|
|
break;
|
|
}
|
|
}while(!PixelProperty::isWalkable(type));
|
|
|
|
data->last_dir = action->dir;
|
|
data->last_action = action->type;
|
|
}
|