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
|
||||
#define BEHAVIOR_H
|
||||
|
||||
#include "pixeltype.h"
|
||||
#include "coord.h"
|
||||
|
||||
#define DUDE_MEMORY_SIZE 128
|
||||
@ -44,11 +45,11 @@ struct Action
|
||||
|
||||
struct Info
|
||||
{
|
||||
virtual bool getSuccess() = 0;
|
||||
virtual int getInventory() = 0;
|
||||
virtual const Com& getCom() = 0;
|
||||
virtual int getNear(Dir d) = 0;
|
||||
virtual int getInfo(Dir d) = 0;
|
||||
virtual bool getSuccess() const = 0;
|
||||
virtual int getInventory() const = 0;
|
||||
virtual const Com& getCom() const = 0;
|
||||
virtual PixelType getNear(Dir d) const = 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));
|
||||
}
|
||||
|
||||
int Dude::getNear(Dir d)
|
||||
PixelType Dude::getNear(Dir d) const
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
10
src/dude.h
10
src/dude.h
@ -26,11 +26,11 @@ public:
|
||||
void move(Dir d);
|
||||
void receiveComData(const Com &comData);
|
||||
|
||||
virtual bool getSuccess() { return m_success; }
|
||||
virtual int getInventory() { return m_inventory; }
|
||||
virtual const Com& getCom() { return m_comData; }
|
||||
virtual int getNear(Dir d);
|
||||
virtual int getInfo(Dir d);
|
||||
virtual bool getSuccess() const { return m_success; }
|
||||
virtual int getInventory() const { return m_inventory; }
|
||||
virtual const Com& getCom() const { return m_comData; }
|
||||
virtual PixelType getNear(Dir d) const;
|
||||
virtual int getInfo(Dir d) const;
|
||||
};
|
||||
|
||||
#endif // DUDE_H
|
||||
|
10
src/map.h
10
src/map.h
@ -1,23 +1,17 @@
|
||||
#ifndef MAP_H
|
||||
#define MAP_H
|
||||
|
||||
#include "pixeltype.h"
|
||||
#include "coord.h"
|
||||
|
||||
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 {
|
||||
int nbRes;
|
||||
void* knowledge;
|
||||
};
|
||||
|
||||
Type type;
|
||||
PixelType type;
|
||||
PixelData data;
|
||||
|
||||
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)
|
||||
|
||||
glm::vec3 MapScene::getColor(Pixel::Type type)
|
||||
glm::vec3 MapScene::getColor(PixelType type)
|
||||
{
|
||||
switch(type){
|
||||
case Pixel::BEDROCK : return HEX_TO_VEC3(0x101020);
|
||||
case Pixel::GRASS : return HEX_TO_VEC3(0x719678);
|
||||
case Pixel::MARK : return HEX_TO_VEC3(0x5D7B62);
|
||||
case Pixel::ROCK : return HEX_TO_VEC3(0x8C8C8C);
|
||||
case Pixel::IRON_ORE : return HEX_TO_VEC3(0x917B61);
|
||||
case Pixel::TREE : return HEX_TO_VEC3(0x003800);
|
||||
case Pixel::BERRIES : return HEX_TO_VEC3(0x4D6394);
|
||||
case Pixel::FOOD : return HEX_TO_VEC3(0xFF7A7A);
|
||||
case Pixel::WOOD : return HEX_TO_VEC3(0x634A22);
|
||||
case Pixel::STONE : return HEX_TO_VEC3(0x454545);
|
||||
case Pixel::IRON : return HEX_TO_VEC3(0x4A4036);
|
||||
case Pixel::DUDE :
|
||||
case BEDROCK : return HEX_TO_VEC3(0x101020);
|
||||
case GRASS : return HEX_TO_VEC3(0x719678);
|
||||
case MARK : return HEX_TO_VEC3(0x5D7B62);
|
||||
case ROCK : return HEX_TO_VEC3(0x8C8C8C);
|
||||
case IRON_ORE : return HEX_TO_VEC3(0x917B61);
|
||||
case TREE : return HEX_TO_VEC3(0x003800);
|
||||
case BERRIES : return HEX_TO_VEC3(0x4D6394);
|
||||
case FOOD : return HEX_TO_VEC3(0xFF7A7A);
|
||||
case WOOD : return HEX_TO_VEC3(0x634A22);
|
||||
case STONE : return HEX_TO_VEC3(0x454545);
|
||||
case IRON : return HEX_TO_VEC3(0x4A4036);
|
||||
case DUDE :
|
||||
// TODO
|
||||
return HEX_TO_VEC3(0x0000FF);
|
||||
case Pixel::SPAWN :
|
||||
case SPAWN :
|
||||
// TODO
|
||||
return HEX_TO_VEC3(0x0000FF);
|
||||
case Pixel::WALL : return HEX_TO_VEC3(0xE6B2A1);
|
||||
case Pixel::ROAD : return HEX_TO_VEC3(0xEDB287);
|
||||
case Pixel::SWORD : return HEX_TO_VEC3(0xEBEBEB);
|
||||
case Pixel::LIBRARY : return HEX_TO_VEC3(0xA37A50);
|
||||
case Pixel::DEAD_DUDE : return HEX_TO_VEC3(0xFF0000);
|
||||
case WALL : return HEX_TO_VEC3(0xE6B2A1);
|
||||
case ROAD : return HEX_TO_VEC3(0xEDB287);
|
||||
case SWORD : return HEX_TO_VEC3(0xEBEBEB);
|
||||
case LIBRARY : return HEX_TO_VEC3(0xA37A50);
|
||||
case DEAD_DUDE : return HEX_TO_VEC3(0xFF0000);
|
||||
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));
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
@ -13,11 +13,11 @@ public:
|
||||
|
||||
~MapScene();
|
||||
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();
|
||||
bool updateNecessary() { return !m_pixels.empty(); }
|
||||
void draw();
|
||||
static glm::vec3 getColor(Pixel::Type type);
|
||||
static glm::vec3 getColor(PixelType type);
|
||||
|
||||
private:
|
||||
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);
|
||||
switch(action->type){
|
||||
case Action::MOVE:
|
||||
if (target.type != Pixel::WALL && target.type != Pixel::ROCK
|
||||
&& target.type != Pixel::BEDROCK && target.type != Pixel::IRON_ORE
|
||||
&& target.type != Pixel::TREE && target.type != Pixel::LIBRARY)
|
||||
if (target.type != WALL && target.type != ROCK
|
||||
&& target.type != BEDROCK && target.type != IRON_ORE
|
||||
&& target.type != TREE && target.type != LIBRARY)
|
||||
NULL; // TODO: move action
|
||||
break;
|
||||
case Action::ATTACK:
|
||||
if (target.type == Pixel::DUDE
|
||||
|| target.type == Pixel::SPAWN
|
||||
|| target.type == Pixel::WALL
|
||||
|| target.type == Pixel::LIBRARY
|
||||
|| target.type == Pixel::ROAD)
|
||||
if (target.type == DUDE
|
||||
|| target.type == SPAWN
|
||||
|| target.type == WALL
|
||||
|| target.type == LIBRARY
|
||||
|| target.type == ROAD)
|
||||
{
|
||||
dude->setSuccess(true);
|
||||
if (target.type == Pixel::DUDE)
|
||||
if (target.type == DUDE)
|
||||
NULL; // TODO: add fight between dude and targetDude
|
||||
else{
|
||||
if(target.type == Pixel::SPAWN)
|
||||
if(target.type == SPAWN)
|
||||
m_teams[target.data.nbRes].destroySpawn();
|
||||
target.type = Pixel::GRASS;
|
||||
target.type = GRASS;
|
||||
}
|
||||
}
|
||||
break;
|
||||
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);
|
||||
target.data.nbRes--;
|
||||
if (target.data.nbRes < 1){
|
||||
target.type = Pixel::GRASS;
|
||||
target.type = GRASS;
|
||||
// TODO: change color of target
|
||||
}
|
||||
dude->setSuccess(true);
|
||||
@ -82,15 +82,15 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
||||
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){
|
||||
target.type = (Pixel::Type) dude->getInventory();
|
||||
if(dude->getInventory() != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->getInventory())){
|
||||
if(target.type == GRASS || target.type == MARK){
|
||||
target.type = PixelType(dude->getInventory());
|
||||
target.data.nbRes = 1;
|
||||
}else
|
||||
target.data.nbRes++;
|
||||
dude->setInventory(-1);
|
||||
// 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);
|
||||
m_teams[target.data.nbRes].addFood();
|
||||
}else{
|
||||
@ -101,50 +101,50 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
||||
case Action::WORK:
|
||||
dude->setSuccess(true);
|
||||
switch(target.type){
|
||||
case Pixel::ROCK:
|
||||
target.type = Pixel::STONE;
|
||||
case ROCK:
|
||||
target.type = STONE;
|
||||
target.data.nbRes = 1;
|
||||
break;
|
||||
case Pixel::BERRIES:
|
||||
target.type = Pixel::FOOD;
|
||||
case BERRIES:
|
||||
target.type = FOOD;
|
||||
target.data.nbRes = 1;
|
||||
break;
|
||||
case Pixel::TREE:
|
||||
target.type = Pixel::WOOD;
|
||||
case TREE:
|
||||
target.type = WOOD;
|
||||
target.data.nbRes = 1;
|
||||
break;
|
||||
case Pixel::IRON_ORE:
|
||||
target.type = Pixel::IRON;
|
||||
case IRON_ORE:
|
||||
target.type = IRON;
|
||||
target.data.nbRes = 1;
|
||||
break;
|
||||
case Pixel::GRASS:
|
||||
target.type = Pixel::MARK;
|
||||
case GRASS:
|
||||
target.type = MARK;
|
||||
break;
|
||||
case Pixel::MARK:
|
||||
target.type = Pixel::GRASS;
|
||||
case MARK:
|
||||
target.type = GRASS;
|
||||
break;
|
||||
case Pixel::WOOD:
|
||||
case WOOD:
|
||||
switch(target.data.nbRes){
|
||||
case 1:
|
||||
target.type = Pixel::WALL;
|
||||
target.type = WALL;
|
||||
break;
|
||||
case 2:
|
||||
target.type = Pixel::LIBRARY;
|
||||
target.type = LIBRARY;
|
||||
//TODO : allocate 128 byte in data ?
|
||||
break;
|
||||
default:
|
||||
dude->setSuccess(true);
|
||||
break;
|
||||
}
|
||||
case Pixel::STONE:
|
||||
case STONE:
|
||||
if(target.data.nbRes == 1)
|
||||
target.type = Pixel::ROAD;
|
||||
target.type = ROAD;
|
||||
else
|
||||
dude->setSuccess(false);
|
||||
break;
|
||||
case Pixel::IRON:
|
||||
case IRON:
|
||||
if(target.data.nbRes == 1)
|
||||
target.type = Pixel::SWORD;
|
||||
target.type = SWORD;
|
||||
else
|
||||
dude->setSuccess(false);
|
||||
break;
|
||||
@ -159,7 +159,7 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
||||
break;
|
||||
case Action::COMMUNICATE:
|
||||
switch(target.type){
|
||||
case Pixel::DUDE:
|
||||
case DUDE:
|
||||
action->com_data.flag = (action->dir+2)%4;
|
||||
// TODO : find a way to get targetDude
|
||||
//targetDude =
|
||||
@ -169,7 +169,7 @@ void Simulation::handleAction(Action *action, Dude *dude){
|
||||
}else
|
||||
dude->setSuccess(false);
|
||||
break;
|
||||
case Pixel::LIBRARY:
|
||||
case LIBRARY:
|
||||
if(action->com_data.flag & Com::READ)
|
||||
{
|
||||
if(dude->getCom().data == NULL)
|
||||
|
Loading…
x
Reference in New Issue
Block a user