added PixelType, added simple behavior (conversion of old work) as example
This commit is contained in:
parent
708eeb15fd
commit
82d240c1c3
@ -1,8 +1,10 @@
|
|||||||
#include <iostream>
|
#include <cstdlib>
|
||||||
|
#include "behavior.h"
|
||||||
|
|
||||||
// g++ -shared hello.cpp -o hello.dll
|
// g++ -shared hello.cpp -o hello.dll -I../src
|
||||||
|
|
||||||
extern "C" void think(void)
|
extern "C" void think(Action *action, char *memory, const Info *info)
|
||||||
{
|
{
|
||||||
std::cout << "Hello World !" << std::endl;
|
action->type = Action::MOVE; // the Dude will move
|
||||||
|
action->dir = Dir( std::rand() % 4 ); // the direction of the movement will be random
|
||||||
}
|
}
|
||||||
|
97
behaviors/simple.cpp
Normal file
97
behaviors/simple.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#include <cstdlib>
|
||||||
|
#include "behavior.h"
|
||||||
|
|
||||||
|
// g++ -shared simple.cpp -o simple.dll -I../src
|
||||||
|
|
||||||
|
// inspired of the old "purple.c" behavior
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
PixelType type;
|
||||||
|
|
||||||
|
bool success = info->getSuccess();
|
||||||
|
purple_data* data = (purple_data*)memory;
|
||||||
|
|
||||||
|
if(!data->new_born){
|
||||||
|
success = false;
|
||||||
|
data->new_born = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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::WAIT;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
action->type = Action::MOVE;
|
||||||
|
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);
|
||||||
|
|
||||||
|
data->last_dir = action->dir;
|
||||||
|
data->last_action = action->type;
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef BEHAVIOR_H
|
#ifndef BEHAVIOR_H
|
||||||
#define BEHAVIOR_H
|
#define BEHAVIOR_H
|
||||||
|
|
||||||
|
#include "pixeltype.h"
|
||||||
#include "coord.h"
|
#include "coord.h"
|
||||||
|
|
||||||
#define DUDE_MEMORY_SIZE 128
|
#define DUDE_MEMORY_SIZE 128
|
||||||
@ -44,11 +45,11 @@ struct Action
|
|||||||
|
|
||||||
struct Info
|
struct Info
|
||||||
{
|
{
|
||||||
virtual bool getSuccess() = 0;
|
virtual bool getSuccess() const = 0;
|
||||||
virtual int getInventory() = 0;
|
virtual int getInventory() const = 0;
|
||||||
virtual const Com& getCom() = 0;
|
virtual const Com& getCom() const = 0;
|
||||||
virtual int getNear(Dir d) = 0;
|
virtual PixelType getNear(Dir d) const = 0;
|
||||||
virtual int getInfo(Dir d) = 0;
|
virtual int getInfo(Dir d) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,12 +30,12 @@ void Dude::receiveComData(const Com &comData)
|
|||||||
std::memcpy(&m_comData, &comData, sizeof(Com));
|
std::memcpy(&m_comData, &comData, sizeof(Com));
|
||||||
}
|
}
|
||||||
|
|
||||||
int Dude::getNear(Dir d)
|
PixelType Dude::getNear(Dir d) const
|
||||||
{
|
{
|
||||||
return p_map->getPixel(Coord(d) + m_pos).type;
|
return p_map->getPixel(Coord(d) + m_pos).type;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Dude::getInfo(Dir d)
|
int Dude::getInfo(Dir d) const
|
||||||
{
|
{
|
||||||
return p_map->getPixel(Coord(d) + m_pos).data.nbRes;
|
return p_map->getPixel(Coord(d) + m_pos).data.nbRes;
|
||||||
}
|
}
|
||||||
|
10
src/dude.h
10
src/dude.h
@ -26,11 +26,11 @@ public:
|
|||||||
void move(Dir d);
|
void move(Dir d);
|
||||||
void receiveComData(const Com &comData);
|
void receiveComData(const Com &comData);
|
||||||
|
|
||||||
virtual bool getSuccess() { return m_success; }
|
virtual bool getSuccess() const { return m_success; }
|
||||||
virtual int getInventory() { return m_inventory; }
|
virtual int getInventory() const { return m_inventory; }
|
||||||
virtual const Com& getCom() { return m_comData; }
|
virtual const Com& getCom() const { return m_comData; }
|
||||||
virtual int getNear(Dir d);
|
virtual PixelType getNear(Dir d) const;
|
||||||
virtual int getInfo(Dir d);
|
virtual int getInfo(Dir d) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DUDE_H
|
#endif // DUDE_H
|
||||||
|
10
src/map.h
10
src/map.h
@ -1,23 +1,17 @@
|
|||||||
#ifndef MAP_H
|
#ifndef MAP_H
|
||||||
#define MAP_H
|
#define MAP_H
|
||||||
|
|
||||||
|
#include "pixeltype.h"
|
||||||
#include "coord.h"
|
#include "coord.h"
|
||||||
|
|
||||||
struct Pixel
|
struct Pixel
|
||||||
{
|
{
|
||||||
enum Type {
|
|
||||||
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
|
||||||
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
|
||||||
DUDE, DEAD_DUDE, // humans
|
|
||||||
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
|
|
||||||
};
|
|
||||||
|
|
||||||
union PixelData {
|
union PixelData {
|
||||||
int nbRes;
|
int nbRes;
|
||||||
void* knowledge;
|
void* knowledge;
|
||||||
};
|
};
|
||||||
|
|
||||||
Type type;
|
PixelType type;
|
||||||
PixelData data;
|
PixelData data;
|
||||||
|
|
||||||
Pixel() : type(GRASS) {}
|
Pixel() : type(GRASS) {}
|
||||||
|
@ -3,31 +3,31 @@
|
|||||||
|
|
||||||
#define HEX_TO_VEC3(hex) glm::vec3(float((hex & 0xFF0000) >> 16)/255, float((hex & 0x00FF00) >> 8)/255, float(hex & 0x0000FF)/255)
|
#define HEX_TO_VEC3(hex) glm::vec3(float((hex & 0xFF0000) >> 16)/255, float((hex & 0x00FF00) >> 8)/255, float(hex & 0x0000FF)/255)
|
||||||
|
|
||||||
glm::vec3 MapScene::getColor(Pixel::Type type)
|
glm::vec3 MapScene::getColor(PixelType type)
|
||||||
{
|
{
|
||||||
switch(type){
|
switch(type){
|
||||||
case Pixel::BEDROCK : return HEX_TO_VEC3(0x101020);
|
case BEDROCK : return HEX_TO_VEC3(0x101020);
|
||||||
case Pixel::GRASS : return HEX_TO_VEC3(0x719678);
|
case GRASS : return HEX_TO_VEC3(0x719678);
|
||||||
case Pixel::MARK : return HEX_TO_VEC3(0x5D7B62);
|
case MARK : return HEX_TO_VEC3(0x5D7B62);
|
||||||
case Pixel::ROCK : return HEX_TO_VEC3(0x8C8C8C);
|
case ROCK : return HEX_TO_VEC3(0x8C8C8C);
|
||||||
case Pixel::IRON_ORE : return HEX_TO_VEC3(0x917B61);
|
case IRON_ORE : return HEX_TO_VEC3(0x917B61);
|
||||||
case Pixel::TREE : return HEX_TO_VEC3(0x003800);
|
case TREE : return HEX_TO_VEC3(0x003800);
|
||||||
case Pixel::BERRIES : return HEX_TO_VEC3(0x4D6394);
|
case BERRIES : return HEX_TO_VEC3(0x4D6394);
|
||||||
case Pixel::FOOD : return HEX_TO_VEC3(0xFF7A7A);
|
case FOOD : return HEX_TO_VEC3(0xFF7A7A);
|
||||||
case Pixel::WOOD : return HEX_TO_VEC3(0x634A22);
|
case WOOD : return HEX_TO_VEC3(0x634A22);
|
||||||
case Pixel::STONE : return HEX_TO_VEC3(0x454545);
|
case STONE : return HEX_TO_VEC3(0x454545);
|
||||||
case Pixel::IRON : return HEX_TO_VEC3(0x4A4036);
|
case IRON : return HEX_TO_VEC3(0x4A4036);
|
||||||
case Pixel::DUDE :
|
case DUDE :
|
||||||
// TODO
|
// TODO
|
||||||
return HEX_TO_VEC3(0x0000FF);
|
return HEX_TO_VEC3(0x0000FF);
|
||||||
case Pixel::SPAWN :
|
case SPAWN :
|
||||||
// TODO
|
// TODO
|
||||||
return HEX_TO_VEC3(0x0000FF);
|
return HEX_TO_VEC3(0x0000FF);
|
||||||
case Pixel::WALL : return HEX_TO_VEC3(0xE6B2A1);
|
case WALL : return HEX_TO_VEC3(0xE6B2A1);
|
||||||
case Pixel::ROAD : return HEX_TO_VEC3(0xEDB287);
|
case ROAD : return HEX_TO_VEC3(0xEDB287);
|
||||||
case Pixel::SWORD : return HEX_TO_VEC3(0xEBEBEB);
|
case SWORD : return HEX_TO_VEC3(0xEBEBEB);
|
||||||
case Pixel::LIBRARY : return HEX_TO_VEC3(0xA37A50);
|
case LIBRARY : return HEX_TO_VEC3(0xA37A50);
|
||||||
case Pixel::DEAD_DUDE : return HEX_TO_VEC3(0xFF0000);
|
case DEAD_DUDE : return HEX_TO_VEC3(0xFF0000);
|
||||||
default : return HEX_TO_VEC3(0x0000FF); // bleu absolu = bug
|
default : return HEX_TO_VEC3(0x0000FF); // bleu absolu = bug
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ void MapScene::changePixel(const Coord &c, glm::vec3 color)
|
|||||||
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), color));
|
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), color));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapScene::changePixel(const Coord &c, Pixel::Type type)
|
void MapScene::changePixel(const Coord &c, PixelType type)
|
||||||
{
|
{
|
||||||
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), getColor(type)));
|
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), getColor(type)));
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,11 @@ public:
|
|||||||
|
|
||||||
~MapScene();
|
~MapScene();
|
||||||
void changePixel(const Coord &c, glm::vec3 color);
|
void changePixel(const Coord &c, glm::vec3 color);
|
||||||
void changePixel(const Coord &c, Pixel::Type type);
|
void changePixel(const Coord &c, PixelType type);
|
||||||
void initDraw();
|
void initDraw();
|
||||||
bool updateNecessary() { return !m_pixels.empty(); }
|
bool updateNecessary() { return !m_pixels.empty(); }
|
||||||
void draw();
|
void draw();
|
||||||
static glm::vec3 getColor(Pixel::Type type);
|
static glm::vec3 getColor(PixelType type);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int m_vao;
|
unsigned int m_vao;
|
||||||
|
11
src/pixeltype.h
Normal file
11
src/pixeltype.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef PIXELTYPE_H
|
||||||
|
#define PIXELTYPE_H
|
||||||
|
|
||||||
|
enum PixelType {
|
||||||
|
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
|
||||||
|
FOOD, WOOD, STONE, IRON, SWORD, // resources
|
||||||
|
DUDE, DEAD_DUDE, // humans
|
||||||
|
SPAWN, WALL, ROAD, MARK, LIBRARY // buildings
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PIXELTYPE_H
|
@ -47,34 +47,34 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
dude->setSuccess(false);
|
dude->setSuccess(false);
|
||||||
switch(action->type){
|
switch(action->type){
|
||||||
case Action::MOVE:
|
case Action::MOVE:
|
||||||
if (target.type != Pixel::WALL && target.type != Pixel::ROCK
|
if (target.type != WALL && target.type != ROCK
|
||||||
&& target.type != Pixel::BEDROCK && target.type != Pixel::IRON_ORE
|
&& target.type != BEDROCK && target.type != IRON_ORE
|
||||||
&& target.type != Pixel::TREE && target.type != Pixel::LIBRARY)
|
&& target.type != TREE && target.type != LIBRARY)
|
||||||
NULL; // TODO: move action
|
NULL; // TODO: move action
|
||||||
break;
|
break;
|
||||||
case Action::ATTACK:
|
case Action::ATTACK:
|
||||||
if (target.type == Pixel::DUDE
|
if (target.type == DUDE
|
||||||
|| target.type == Pixel::SPAWN
|
|| target.type == SPAWN
|
||||||
|| target.type == Pixel::WALL
|
|| target.type == WALL
|
||||||
|| target.type == Pixel::LIBRARY
|
|| target.type == LIBRARY
|
||||||
|| target.type == Pixel::ROAD)
|
|| target.type == ROAD)
|
||||||
{
|
{
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
if (target.type == Pixel::DUDE)
|
if (target.type == DUDE)
|
||||||
NULL; // TODO: add fight between dude and targetDude
|
NULL; // TODO: add fight between dude and targetDude
|
||||||
else{
|
else{
|
||||||
if(target.type == Pixel::SPAWN)
|
if(target.type == SPAWN)
|
||||||
m_teams[target.data.nbRes].destroySpawn();
|
m_teams[target.data.nbRes].destroySpawn();
|
||||||
target.type = Pixel::GRASS;
|
target.type = GRASS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Action::PICK:
|
case Action::PICK:
|
||||||
if(target.type >= Pixel::FOOD && target.type <= Pixel::SWORD && dude->getInventory() == -1){
|
if(target.type >= FOOD && target.type <= SWORD && 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 = Pixel::GRASS;
|
target.type = GRASS;
|
||||||
// TODO: change color of target
|
// TODO: change color of target
|
||||||
}
|
}
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
@ -82,15 +82,15 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
break;
|
break;
|
||||||
case Action::PUT:
|
case Action::PUT:
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
if(dude->getInventory() != -1 && (target.type == Pixel::GRASS || target.type == Pixel::MARK || target.type == dude->getInventory())){
|
if(dude->getInventory() != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->getInventory())){
|
||||||
if(target.type == Pixel::GRASS || target.type == Pixel::MARK){
|
if(target.type == GRASS || target.type == MARK){
|
||||||
target.type = (Pixel::Type) dude->getInventory();
|
target.type = PixelType(dude->getInventory());
|
||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
}else
|
}else
|
||||||
target.data.nbRes++;
|
target.data.nbRes++;
|
||||||
dude->setInventory(-1);
|
dude->setInventory(-1);
|
||||||
// TODO: change pixel to new type
|
// TODO: change pixel to new type
|
||||||
}else if(target.type == Pixel::SPAWN && dude->getInventory() == Pixel::FOOD){
|
}else if(target.type == SPAWN && dude->getInventory() == FOOD){
|
||||||
dude->setInventory(-1);
|
dude->setInventory(-1);
|
||||||
m_teams[target.data.nbRes].addFood();
|
m_teams[target.data.nbRes].addFood();
|
||||||
}else{
|
}else{
|
||||||
@ -101,50 +101,50 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
case Action::WORK:
|
case Action::WORK:
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
switch(target.type){
|
switch(target.type){
|
||||||
case Pixel::ROCK:
|
case ROCK:
|
||||||
target.type = Pixel::STONE;
|
target.type = STONE;
|
||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
break;
|
break;
|
||||||
case Pixel::BERRIES:
|
case BERRIES:
|
||||||
target.type = Pixel::FOOD;
|
target.type = FOOD;
|
||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
break;
|
break;
|
||||||
case Pixel::TREE:
|
case TREE:
|
||||||
target.type = Pixel::WOOD;
|
target.type = WOOD;
|
||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
break;
|
break;
|
||||||
case Pixel::IRON_ORE:
|
case IRON_ORE:
|
||||||
target.type = Pixel::IRON;
|
target.type = IRON;
|
||||||
target.data.nbRes = 1;
|
target.data.nbRes = 1;
|
||||||
break;
|
break;
|
||||||
case Pixel::GRASS:
|
case GRASS:
|
||||||
target.type = Pixel::MARK;
|
target.type = MARK;
|
||||||
break;
|
break;
|
||||||
case Pixel::MARK:
|
case MARK:
|
||||||
target.type = Pixel::GRASS;
|
target.type = GRASS;
|
||||||
break;
|
break;
|
||||||
case Pixel::WOOD:
|
case WOOD:
|
||||||
switch(target.data.nbRes){
|
switch(target.data.nbRes){
|
||||||
case 1:
|
case 1:
|
||||||
target.type = Pixel::WALL;
|
target.type = WALL;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
target.type = Pixel::LIBRARY;
|
target.type = LIBRARY;
|
||||||
//TODO : allocate 128 byte in data ?
|
//TODO : allocate 128 byte in data ?
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
dude->setSuccess(true);
|
dude->setSuccess(true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Pixel::STONE:
|
case STONE:
|
||||||
if(target.data.nbRes == 1)
|
if(target.data.nbRes == 1)
|
||||||
target.type = Pixel::ROAD;
|
target.type = ROAD;
|
||||||
else
|
else
|
||||||
dude->setSuccess(false);
|
dude->setSuccess(false);
|
||||||
break;
|
break;
|
||||||
case Pixel::IRON:
|
case IRON:
|
||||||
if(target.data.nbRes == 1)
|
if(target.data.nbRes == 1)
|
||||||
target.type = Pixel::SWORD;
|
target.type = SWORD;
|
||||||
else
|
else
|
||||||
dude->setSuccess(false);
|
dude->setSuccess(false);
|
||||||
break;
|
break;
|
||||||
@ -159,7 +159,7 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
break;
|
break;
|
||||||
case Action::COMMUNICATE:
|
case Action::COMMUNICATE:
|
||||||
switch(target.type){
|
switch(target.type){
|
||||||
case Pixel::DUDE:
|
case DUDE:
|
||||||
action->com_data.flag = (action->dir+2)%4;
|
action->com_data.flag = (action->dir+2)%4;
|
||||||
// TODO : find a way to get targetDude
|
// TODO : find a way to get targetDude
|
||||||
//targetDude =
|
//targetDude =
|
||||||
@ -169,7 +169,7 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
|||||||
}else
|
}else
|
||||||
dude->setSuccess(false);
|
dude->setSuccess(false);
|
||||||
break;
|
break;
|
||||||
case Pixel::LIBRARY:
|
case LIBRARY:
|
||||||
if(action->com_data.flag & Com::READ)
|
if(action->com_data.flag & Com::READ)
|
||||||
{
|
{
|
||||||
if(dude->getCom().data == NULL)
|
if(dude->getCom().data == NULL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user